From ef1f49f6ffbf4d6046fbb142476e68c630edacee Mon Sep 17 00:00:00 2001 From: Vittorio Giovara Date: Tue, 19 Sep 2017 16:42:33 +0200 Subject: [PATCH] colorspace: Add support for additional color primaries This includes bt470bg, bt470m, smpte-st428, and jedec-p22. --- src/testapp/colorspaceapp.cpp | 2 +- src/testapp/table.cpp | 9 +++++++-- src/testapp/table.h | 2 +- src/zimg/api/zimg.cpp | 7 ++++++- src/zimg/api/zimg.h | 7 ++++++- src/zimg/colorspace/colorspace.h | 5 +++++ src/zimg/colorspace/colorspace_param.cpp | 20 ++++++++++++++++++++ src/zimg/colorspace/colorspace_param.h | 7 +++++++ 8 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/testapp/colorspaceapp.cpp b/src/testapp/colorspaceapp.cpp index b322e18f..99de9526 100644 --- a/src/testapp/colorspaceapp.cpp +++ b/src/testapp/colorspaceapp.cpp @@ -102,7 +102,7 @@ const char help_str[] = "Colorspace specifier format: matrix:transfer:primaries\n" "matrix: unspec, rgb, 601, 709, fcc, 240m, ycgco, 2020_ncl, 2020_cl, chroma_ncl, chroma_cl, ictcp\n" "transfer: unspec, linear, 709, srgb, st_2084, arib_b67\n" -"primaries: unspec, smpte_c, 709, 2020, dcip3_d65\n" +"primaries: unspec, 470_m, 470_bg, smpte_c, 709, film, 2020, st_428, dcip3_d65, jedec_p22\n" "\n" PATH_SPECIFIER_HELP_STR; diff --git a/src/testapp/table.cpp b/src/testapp/table.cpp index 175b39d3..a96b6299 100644 --- a/src/testapp/table.cpp +++ b/src/testapp/table.cpp @@ -93,13 +93,18 @@ const zimg::static_string_map g_transfer_table{ { "arib_b67", TransferCharacteristics::ARIB_B67 }, }; -const zimg::static_string_map g_primaries_table{ +const zimg::static_string_map g_primaries_table{ { "unspec", ColorPrimaries::UNSPECIFIED }, + { "470_m", ColorPrimaries::REC_470_M }, + { "470_bg", ColorPrimaries::REC_470_BG }, { "smpte_c", ColorPrimaries::SMPTE_C }, { "709", ColorPrimaries::REC_709 }, + { "film", ColorPrimaries::FILM }, { "2020", ColorPrimaries::REC_2020 }, + { "st_428", ColorPrimaries::ST_428 }, { "dcip3", ColorPrimaries::DCI_P3 }, - { "dcip3_d65", ColorPrimaries::DCI_P3_D65 } + { "dcip3_d65", ColorPrimaries::DCI_P3_D65 }, + { "jedec_p22", ColorPrimaries::JEDEC_P22 }, }; const zimg::static_string_map g_dither_table{ diff --git a/src/testapp/table.h b/src/testapp/table.h index b491795a..6a1d45e6 100644 --- a/src/testapp/table.h +++ b/src/testapp/table.h @@ -38,7 +38,7 @@ extern const zimg::static_string_map g_cpu_table; extern const zimg::static_string_map g_pixel_table; extern const zimg::static_string_map g_matrix_table; extern const zimg::static_string_map g_transfer_table; -extern const zimg::static_string_map g_primaries_table; +extern const zimg::static_string_map g_primaries_table; extern const zimg::static_string_map g_dither_table; extern const zimg::static_string_map(*)(double, double), 7> g_resize_table; diff --git a/src/zimg/api/zimg.cpp b/src/zimg/api/zimg.cpp index 59b3923e..808e19f0 100644 --- a/src/zimg/api/zimg.cpp +++ b/src/zimg/api/zimg.cpp @@ -276,14 +276,19 @@ zimg::colorspace::ColorPrimaries translate_primaries(zimg_color_primaries_e prim { using zimg::colorspace::ColorPrimaries; - static SM_CONSTEXPR_14 const zimg::static_map map{ + static SM_CONSTEXPR_14 const zimg::static_map map{ + { ZIMG_PRIMARIES_470_M, ColorPrimaries::REC_470_M }, + { ZIMG_PRIMARIES_470_BG, ColorPrimaries::REC_470_BG }, { ZIMG_PRIMARIES_709, ColorPrimaries::REC_709 }, { ZIMG_PRIMARIES_UNSPECIFIED, ColorPrimaries::UNSPECIFIED }, { ZIMG_PRIMARIES_170M, ColorPrimaries::SMPTE_C }, { ZIMG_PRIMARIES_240M, ColorPrimaries::SMPTE_C }, + { ZIMG_PRIMARIES_FILM, ColorPrimaries::FILM }, { ZIMG_PRIMARIES_2020, ColorPrimaries::REC_2020 }, + { ZIMG_PRIMARIES_ST428, ColorPrimaries::ST_428 }, { ZIMG_PRIMARIES_ST431_2, ColorPrimaries::DCI_P3 }, { ZIMG_PRIMARIES_ST432_1, ColorPrimaries::DCI_P3_D65 }, + { ZIMG_PRIMARIES_EBU3213_E, ColorPrimaries::JEDEC_P22 }, }; return search_itu_enum_map(map, primaries, "unrecognized color primaries"); } diff --git a/src/zimg/api/zimg.h b/src/zimg/api/zimg.h index d70649a3..2539e82d 100644 --- a/src/zimg/api/zimg.h +++ b/src/zimg/api/zimg.h @@ -301,11 +301,16 @@ typedef enum zimg_color_primaries_e { ZIMG_PRIMARIES_INTERNAL = -1, /**< Not part of the API. */ ZIMG_PRIMARIES_709 = 1, ZIMG_PRIMARIES_UNSPECIFIED = 2, + ZIMG_PRIMARIES_470_M = 4, + ZIMG_PRIMARIES_470_BG = 5, ZIMG_PRIMARIES_170M = 6, ZIMG_PRIMARIES_240M = 7, /* Equivalent to 6. */ + ZIMG_PRIMARIES_FILM = 8, ZIMG_PRIMARIES_2020 = 9, + ZIMG_PRIMARIES_ST428 = 10, ZIMG_PRIMARIES_ST431_2 = 11, - ZIMG_PRIMARIES_ST432_1 = 12 + ZIMG_PRIMARIES_ST432_1 = 12, + ZIMG_PRIMARIES_EBU3213_E = 22 } zimg_color_primaries_e; /** diff --git a/src/zimg/colorspace/colorspace.h b/src/zimg/colorspace/colorspace.h index 8df61410..ee9ad007 100644 --- a/src/zimg/colorspace/colorspace.h +++ b/src/zimg/colorspace/colorspace.h @@ -51,11 +51,16 @@ enum class TransferCharacteristics { enum class ColorPrimaries { UNSPECIFIED, + REC_470_M, + REC_470_BG, SMPTE_C, REC_709, + FILM, REC_2020, + ST_428, DCI_P3, DCI_P3_D65, + JEDEC_P22, }; /** diff --git a/src/zimg/colorspace/colorspace_param.cpp b/src/zimg/colorspace/colorspace_param.cpp index 0b76770f..5aab4e0c 100644 --- a/src/zimg/colorspace/colorspace_param.cpp +++ b/src/zimg/colorspace/colorspace_param.cpp @@ -55,6 +55,11 @@ Vector3 xy_to_xyz(double x, double y) Vector3 get_white_point(ColorPrimaries primaries) { switch (primaries) { + case ColorPrimaries::REC_470_M: + case ColorPrimaries::FILM: + return xy_to_xyz(ILLUMINANT_C[0], ILLUMINANT_C[1]); + case ColorPrimaries::ST_428: + return xy_to_xyz(ILLUMINANT_E[0], ILLUMINANT_E[1]); case ColorPrimaries::DCI_P3: return xy_to_xyz(ILLUMINANT_DCI[0], ILLUMINANT_DCI[1]); default: @@ -65,19 +70,34 @@ Vector3 get_white_point(ColorPrimaries primaries) void get_primaries_xy(double out[3][2], ColorPrimaries primaries) { switch (primaries) { + case ColorPrimaries::REC_470_M: + memcpy(out, REC_470_M_PRIMARIES, sizeof(REC_470_M_PRIMARIES)); + break; + case ColorPrimaries::REC_470_BG: + memcpy(out, REC_470_BG_PRIMARIES, sizeof(REC_470_BG_PRIMARIES)); + break; case ColorPrimaries::SMPTE_C: memcpy(out, SMPTE_C_PRIMARIES, sizeof(SMPTE_C_PRIMARIES)); break; case ColorPrimaries::REC_709: memcpy(out, REC_709_PRIMARIES, sizeof(REC_709_PRIMARIES)); break; + case ColorPrimaries::FILM: + memcpy(out, FILM_PRIMARIES, sizeof(FILM_PRIMARIES)); + break; case ColorPrimaries::REC_2020: memcpy(out, REC_2020_PRIMARIES, sizeof(REC_2020_PRIMARIES)); break; + case ColorPrimaries::ST_428: + memcpy(out, ST_428_PRIMARIES, sizeof(ST_428_PRIMARIES)); + break; case ColorPrimaries::DCI_P3: case ColorPrimaries::DCI_P3_D65: memcpy(out, DCI_P3_PRIMARIES, sizeof(DCI_P3_PRIMARIES)); break; + case ColorPrimaries::JEDEC_P22: + memcpy(out, JEDEC_P22_PRIMARIES, sizeof(JEDEC_P22_PRIMARIES)); + break; default: error::throw_("unrecognized primaries"); } diff --git a/src/zimg/colorspace/colorspace_param.h b/src/zimg/colorspace/colorspace_param.h index 6a0f98f1..6e6f362e 100644 --- a/src/zimg/colorspace/colorspace_param.h +++ b/src/zimg/colorspace/colorspace_param.h @@ -24,14 +24,21 @@ constexpr double REC_2020_KR = 0.2627; constexpr double REC_2020_KB = 0.0593; // R, G, B primaries in XY. +constexpr double REC_470_M_PRIMARIES[3][2] = { { 0.670, 0.330 }, { 0.210, 0.710 }, { 0.140, 0.080 } }; +constexpr double REC_470_BG_PRIMARIES[3][2] = { { 0.640, 0.330 }, { 0.290, 0.600 }, { 0.150, 0.060 } }; constexpr double SMPTE_C_PRIMARIES[3][2] = { { 0.630, 0.340 }, { 0.310, 0.595 }, { 0.155, 0.070 } }; constexpr double REC_709_PRIMARIES[3][2] = { { 0.640, 0.330 }, { 0.300, 0.600 }, { 0.150, 0.060 } }; +constexpr double FILM_PRIMARIES[3][2] = { { 0.681, 0.319 }, { 0.243, 0.692 }, { 0.145, 0.049 } }; constexpr double REC_2020_PRIMARIES[3][2] = { { 0.708, 0.292 }, { 0.170, 0.797 }, { 0.131, 0.046 } }; constexpr double DCI_P3_PRIMARIES[3][2] = { { 0.680, 0.320 } , { 0.265, 0.690 }, { 0.150, 0.060 } }; +constexpr double ST_428_PRIMARIES[3][2] = { { 0.735, 0.265 }, { 0.274, 0.718 }, { 0.167, 0.009 } }; +constexpr double JEDEC_P22_PRIMARIES[3][2] = { { 0.630, 0.340 }, { 0.295, 0.605 }, { 0.155, 0.077 } }; // White points in XY. +constexpr double ILLUMINANT_C[2] = { 0.31, 0.316 }; constexpr double ILLUMINANT_DCI[2] = { 0.314, 0.351 }; constexpr double ILLUMINANT_D65[2] = { 0.3127, 0.3290 }; +constexpr double ILLUMINANT_E[2] = { 1.0 / 3.0, 1.0 / 3.0 }; /** * Obtain 3x3 matrix for converting from YUV to RGB.