Skip to content

Commit

Permalink
colorspace: Add support for additional color primaries
Browse files Browse the repository at this point in the history
This includes bt470bg, bt470m, smpte-st428, and jedec-p22.
  • Loading branch information
kodawah committed Oct 9, 2017
1 parent ad1e5ec commit ef1f49f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/testapp/colorspaceapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 7 additions & 2 deletions src/testapp/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,18 @@ const zimg::static_string_map<TransferCharacteristics, 12> g_transfer_table{
{ "arib_b67", TransferCharacteristics::ARIB_B67 },
};

const zimg::static_string_map<ColorPrimaries, 6> g_primaries_table{
const zimg::static_string_map<ColorPrimaries, 12> 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<DitherType, 4> g_dither_table{
Expand Down
2 changes: 1 addition & 1 deletion src/testapp/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern const zimg::static_string_map<zimg::CPUClass, 8> g_cpu_table;
extern const zimg::static_string_map<zimg::PixelType, 4> g_pixel_table;
extern const zimg::static_string_map<zimg::colorspace::MatrixCoefficients, 12> g_matrix_table;
extern const zimg::static_string_map<zimg::colorspace::TransferCharacteristics, 12> g_transfer_table;
extern const zimg::static_string_map<zimg::colorspace::ColorPrimaries, 6> g_primaries_table;
extern const zimg::static_string_map<zimg::colorspace::ColorPrimaries, 12> g_primaries_table;
extern const zimg::static_string_map<zimg::depth::DitherType, 4> g_dither_table;
extern const zimg::static_string_map<std::unique_ptr<zimg::resize::Filter>(*)(double, double), 7> g_resize_table;

Expand Down
7 changes: 6 additions & 1 deletion src/zimg/api/zimg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<zimg_color_primaries_e, ColorPrimaries, 7> map{
static SM_CONSTEXPR_14 const zimg::static_map<zimg_color_primaries_e, ColorPrimaries, 12> 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");
}
Expand Down
7 changes: 6 additions & 1 deletion src/zimg/api/zimg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
5 changes: 5 additions & 0 deletions src/zimg/colorspace/colorspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/**
Expand Down
20 changes: 20 additions & 0 deletions src/zimg/colorspace/colorspace_param.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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_<error::InternalError>("unrecognized primaries");
}
Expand Down
7 changes: 7 additions & 0 deletions src/zimg/colorspace/colorspace_param.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ef1f49f

Please sign in to comment.