From: Steinar H. Gunderson Date: Sat, 27 Feb 2016 23:28:31 +0000 (+0100) Subject: Add some sRGB conversion functions to test_util.{h,cpp}. X-Git-Tag: 1.4.0~13 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=d2eee22b0440147f69038e3cf829910b18526efc Add some sRGB conversion functions to test_util.{h,cpp}. --- diff --git a/gamma_compression_effect_test.cpp b/gamma_compression_effect_test.cpp index 1c87680..db20234 100644 --- a/gamma_compression_effect_test.cpp +++ b/gamma_compression_effect_test.cpp @@ -54,13 +54,7 @@ TEST(GammaCompressionEffectTest, sRGB_Accuracy) { double x = i / 255.0; expected_data[i] = x; - - // From the Wikipedia article on sRGB. - if (x < 0.04045) { - data[i] = x / 12.92; - } else { - data[i] = pow((x + 0.055) / 1.055, 2.4); - } + data[i] = srgb_to_linear(x); } EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F); diff --git a/gamma_expansion_effect_test.cpp b/gamma_expansion_effect_test.cpp index 499a0d4..1e399dc 100644 --- a/gamma_expansion_effect_test.cpp +++ b/gamma_expansion_effect_test.cpp @@ -62,13 +62,7 @@ TEST(GammaExpansionEffectTest, sRGB_Accuracy) { double x = i / 255.0; data[i] = x; - - // From the Wikipedia article on sRGB. - if (x < 0.04045) { - expected_data[i] = x / 12.92; - } else { - expected_data[i] = pow((x + 0.055) / 1.055, 2.4); - } + expected_data[i] = srgb_to_linear(x); } EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F); diff --git a/test_util.cpp b/test_util.cpp index b377309..05eac31 100644 --- a/test_util.cpp +++ b/test_util.cpp @@ -351,4 +351,24 @@ void test_accuracy(const float *expected, const float *result, unsigned num_valu EXPECT_LT(rms, rms_limit); } +double srgb_to_linear(double x) +{ + // From the Wikipedia article on sRGB. + if (x < 0.04045) { + return x / 12.92; + } else { + return pow((x + 0.055) / 1.055, 2.4); + } +} + +double linear_to_srgb(double x) +{ + // From the Wikipedia article on sRGB. + if (x < 0.0031308) { + return 12.92 * x; + } else { + return 1.055 * pow(x, 1.0 / 2.4) - 0.055; + } +} + } // namespace movit diff --git a/test_util.h b/test_util.h index 69efe35..43d0216 100644 --- a/test_util.h +++ b/test_util.h @@ -48,6 +48,14 @@ void expect_equal(const float *ref, const float *result, unsigned width, unsigne void expect_equal(const unsigned char *ref, const unsigned char *result, unsigned width, unsigned height, unsigned largest_difference_limit = 1, float rms_limit = 0.2); void test_accuracy(const float *expected, const float *result, unsigned num_values, double absolute_error_limit, double relative_error_limit, double local_relative_error_limit, double rms_limit); +// Convert an sRGB encoded value (0.0 to 1.0, inclusive) to linear light. +// Undefined for values outside 0.0..1.0. +double srgb_to_linear(double x); + +// Convert a value in linear light (0.0 to 1.0, inclusive) to sRGB. +// Undefined for values outside 0.0..1.0. +double linear_to_srgb(double x); + } // namespace movit #endif // !defined(_MOVIT_TEST_UTIL_H)