Skip to content

Commit

Permalink
colorspace: add ST.428-1 (gamma 2.6) transfer function
Browse files Browse the repository at this point in the history
  • Loading branch information
sekrit-twc committed Jun 30, 2023
1 parent 3d83f6a commit f80b515
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/testapp/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const zimg::static_string_map<MatrixCoefficients, 12> g_matrix_table{
{ "ictcp", MatrixCoefficients::REC_2100_ICTCP },
};

const zimg::static_string_map<TransferCharacteristics, 12> g_transfer_table{
const zimg::static_string_map<TransferCharacteristics, 13> g_transfer_table{
{ "unspec", TransferCharacteristics::UNSPECIFIED },
{ "linear", TransferCharacteristics::LINEAR },
{ "log100", TransferCharacteristics::LOG_100 },
Expand All @@ -93,6 +93,7 @@ const zimg::static_string_map<TransferCharacteristics, 12> g_transfer_table{
{ "xvycc", TransferCharacteristics::XVYCC },
{ "srgb", TransferCharacteristics::SRGB },
{ "st_2084", TransferCharacteristics::ST_2084 },
{ "st_428", TransferCharacteristics::ST_428 },
{ "arib_b67", TransferCharacteristics::ARIB_B67 },
};

Expand Down
2 changes: 1 addition & 1 deletion src/testapp/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Filter;
extern const zimg::static_string_map<zimg::CPUClass, 9> 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::TransferCharacteristics, 13> g_transfer_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), 8> g_resize_table;
Expand Down
3 changes: 2 additions & 1 deletion src/zimg/api/zimg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ zimg::colorspace::TransferCharacteristics translate_transfer(zimg_transfer_chara
{
using zimg::colorspace::TransferCharacteristics;

static SM_CONSTEXPR_14 const zimg::static_map<zimg_transfer_characteristics_e, TransferCharacteristics, 15> map{
static SM_CONSTEXPR_14 const zimg::static_map<zimg_transfer_characteristics_e, TransferCharacteristics, 16> map{
{ ZIMG_TRANSFER_BT709, TransferCharacteristics::REC_709 },
{ ZIMG_TRANSFER_UNSPECIFIED, TransferCharacteristics::UNSPECIFIED },
{ ZIMG_TRANSFER_ST240_M, TransferCharacteristics::SMPTE_240M },
Expand All @@ -281,6 +281,7 @@ zimg::colorspace::TransferCharacteristics translate_transfer(zimg_transfer_chara
{ ZIMG_TRANSFER_LOG_100, TransferCharacteristics::LOG_100 },
{ ZIMG_TRANSFER_LOG_316, TransferCharacteristics::LOG_316 },
{ ZIMG_TRANSFER_ST2084, TransferCharacteristics::ST_2084 },
{ ZIMG_TRANSFER_ST428, TransferCharacteristics::ST_428 },
{ ZIMG_TRANSFER_ARIB_B67, TransferCharacteristics::ARIB_B67 },
};
return search_itu_enum_map(map, transfer, "unrecognized transfer characteristics");
Expand Down
1 change: 1 addition & 0 deletions src/zimg/api/zimg.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ typedef enum zimg_transfer_characteristics_e {
ZIMG_TRANSFER_BT2020_10 = 14, /* Equivalent to 1. */
ZIMG_TRANSFER_BT2020_12 = 15, /* Equivalent to 1. */
ZIMG_TRANSFER_ST2084 = 16,
ZIMG_TRANSFER_ST428 = 17,
ZIMG_TRANSFER_ARIB_B67 = 18
#define ZIMG_TRANSFER_709 ZIMG_TRANSFER_BT709 /**< Deprecated. */
#define ZIMG_TRANSFER_470_M ZIMG_TRANSFER_BT470_M /**< Deprecated. */
Expand Down
1 change: 1 addition & 0 deletions src/zimg/colorspace/colorspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class TransferCharacteristics {
XVYCC,
SRGB,
ST_2084,
ST_428,
ARIB_B67,
};

Expand Down
14 changes: 14 additions & 0 deletions src/zimg/colorspace/gamma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ float st_2084_inverse_eotf(float x) noexcept
return x;
}

float st_428_eotf(float x) noexcept
{
return x < 0.0f ? 0.0f : zimg_x_powf(x, 2.6f);
}

float st_428_inverse_eotf(float x) noexcept
{
return x < 0.0f ? 0.0f : zimg_x_powf(x, 1.0f / 2.6f);
}

// Applies a per-channel correction instead of the iterative method specified in Rec.2100.
float arib_b67_eotf(float x) noexcept
{
Expand Down Expand Up @@ -345,6 +355,10 @@ TransferFunction select_transfer_function(TransferCharacteristics transfer, doub
func.to_linear_scale = static_cast<float>(ST2084_PEAK_LUMINANCE / peak_luminance);
func.to_gamma_scale = static_cast<float>(peak_luminance / ST2084_PEAK_LUMINANCE);
break;
case TransferCharacteristics::ST_428:
func.to_linear = st_428_eotf;
func.to_gamma = st_428_inverse_eotf;
break;
case TransferCharacteristics::ARIB_B67:
func.to_linear = scene_referred ? arib_b67_inverse_oetf : arib_b67_eotf;
func.to_gamma = scene_referred ? arib_b67_oetf : arib_b67_inverse_eotf;
Expand Down
3 changes: 3 additions & 0 deletions src/zimg/colorspace/gamma.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ float srgb_inverse_eotf(float x) noexcept;
float st_2084_eotf(float x) noexcept;
float st_2084_inverse_eotf(float x) noexcept;

float st_428_eotf(float x) noexcept;
float st_428_inverse_eotf(float x) noexcept;

// Derived functions.
float arib_b67_eotf(float x) noexcept;
float arib_b67_inverse_eotf(float x) noexcept;
Expand Down
5 changes: 4 additions & 1 deletion test/colorspace/gamma_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ TEST(GammaTest, test_log316)
test_monotonic(zimg::colorspace::log316_oetf, 1.0f, 2.0f, 1UL << 16);
}

TEST(GammaTest, test_rec_470m_470bg)
TEST(GammaTest, test_rec_470m_470bg_st428)
{
SCOPED_TRACE("470m");
test_accuracy(zimg::colorspace::rec_470m_oetf, zimg::colorspace::rec_470m_inverse_oetf, 0.0f, 1.0f, 1e-6f, 1e-6f);
test_accuracy(zimg::colorspace::rec_470m_inverse_oetf, zimg::colorspace::rec_470m_oetf, 0.0f, 1.0f, 1e-6f, 1e-6f);
SCOPED_TRACE("470bg");
test_accuracy(zimg::colorspace::rec_470bg_oetf, zimg::colorspace::rec_470bg_inverse_oetf, 0.0f, 1.0f, 1e-6f, 1e-6f);
test_accuracy(zimg::colorspace::rec_470bg_inverse_oetf, zimg::colorspace::rec_470bg_oetf, 0.0f, 1.0f, 1e-6f, 1e-6f);
SCOPED_TRACE("st428");
test_accuracy(zimg::colorspace::st_428_eotf, zimg::colorspace::st_428_inverse_eotf, 0.0f, 1.0f, 1e-6f, 1e-6f);
test_accuracy(zimg::colorspace::st_428_inverse_eotf, zimg::colorspace::st_428_eotf, 0.0f, 1.0f, 1e-6f, 1e-6f);
}

TEST(GammaTest, test_smpte_240m)
Expand Down

0 comments on commit f80b515

Please sign in to comment.