Add some sRGB conversion functions to test_util.{h,cpp}.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Feb 2016 23:28:31 +0000 (00:28 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 27 Feb 2016 23:28:31 +0000 (00:28 +0100)
gamma_compression_effect_test.cpp
gamma_expansion_effect_test.cpp
test_util.cpp
test_util.h

index 1c87680..db20234 100644 (file)
@@ -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);
index 499a0d4..1e399dc 100644 (file)
@@ -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);
index b377309..05eac31 100644 (file)
@@ -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
index 69efe35..43d0216 100644 (file)
@@ -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)