1 // Unit tests for GammaCompressionEffect.
3 // Pretty much the inverse of the GammaExpansionEffect tests;
4 // EffectChainTest tests that they are actually inverses.
5 // However, the accuracy tests are somewhat simpler, since we
6 // only need to care about absolute errors and not relative.
11 #include "gtest/gtest.h"
12 #include "gtest/gtest-message.h"
13 #include "image_format.h"
14 #include "test_util.h"
16 TEST(GammaCompressionEffectTest, sRGB_KeyValues) {
19 0.00309f, 0.00317f, // On either side of the discontinuity.
20 -0.5f, 1.5f, // To check clamping.
22 float expected_data[] = {
28 EffectChainTester tester(data, 2, 3, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
29 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
31 expect_equal(expected_data, out_data, 2, 3);
34 TEST(GammaCompressionEffectTest, sRGB_RampAlwaysIncreases) {
35 float data[256], out_data[256];
36 for (unsigned i = 0; i < 256; ++i) {
39 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
40 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
42 for (unsigned i = 1; i < 256; ++i) {
43 EXPECT_GT(out_data[i], out_data[i - 1])
44 << "No increase between " << i-1 << " and " << i;
48 TEST(GammaCompressionEffectTest, sRGB_Accuracy) {
49 float data[256], expected_data[256], out_data[256];
51 for (int i = 0; i < 256; ++i) {
56 // From the Wikipedia article on sRGB.
60 data[i] = pow((x + 0.055) / 1.055, 2.4);
64 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
65 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
67 // Maximum absolute error is 25% of one pixel level. For comparison,
68 // a straightforward ALU solution (using a branch and pow()), used as a
69 // “high anchor” to indicate limitations of float arithmetic etc.,
70 // reaches maximum absolute error of 3.7% of one pixel level
72 expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-4);
75 TEST(GammaCompressionEffectTest, Rec709_KeyValues) {
78 0.017778f, 0.018167f, // On either side of the discontinuity.
80 float expected_data[] = {
85 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
86 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
88 expect_equal(expected_data, out_data, 2, 2);
91 TEST(GammaCompressionEffectTest, Rec709_RampAlwaysIncreases) {
92 float data[256], out_data[256];
93 for (unsigned i = 0; i < 256; ++i) {
96 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
97 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
99 for (unsigned i = 1; i < 256; ++i) {
100 EXPECT_GT(out_data[i], out_data[i - 1])
101 << "No increase between " << i-1 << " and " << i;
105 TEST(GammaCompressionEffectTest, Rec709_Accuracy) {
106 float data[256], expected_data[256], out_data[256];
108 for (int i = 0; i < 256; ++i) {
109 double x = i / 255.0;
111 expected_data[i] = x;
113 // Rec. 2020, page 3.
114 if (x < 0.018 * 4.5) {
117 data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
121 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
122 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
124 // Maximum absolute error is 25% of one pixel level. For comparison,
125 // a straightforward ALU solution (using a branch and pow()), used as a
126 // “high anchor” to indicate limitations of float arithmetic etc.,
127 // reaches maximum absolute error of 3.7% of one pixel level
128 // and rms of 3.5e-6.
129 expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-5);
132 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
133 // input range and somewhat looser error bounds. (One could claim that this is
134 // already on the limit of what we can reasonably do with fp16 input, if you
135 // look at the local relative error.)
136 TEST(GammaCompressionEffectTest, Rec2020_10Bit_Accuracy) {
137 float data[1024], expected_data[1024], out_data[1024];
139 for (int i = 0; i < 1024; ++i) {
140 double x = i / 1023.0;
142 expected_data[i] = x;
144 // Rec. 2020, page 3.
145 if (x < 0.018 * 4.5) {
148 data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
152 EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
153 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT);
155 // Maximum absolute error is 30% of one pixel level. For comparison,
156 // a straightforward ALU solution (using a branch and pow()), used as a
157 // “high anchor” to indicate limitations of float arithmetic etc.,
158 // reaches maximum absolute error of 25.2% of one pixel level
159 // and rms of 1.8e-6, so this is probably mostly related to input precision.
160 expect_equal(expected_data, out_data, 1024, 1, 0.30 / 1023.0, 1e-5);
163 TEST(GammaCompressionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
165 for (unsigned i = 0; i < 4096; ++i) {
166 data[i] = i / 4095.0f;
168 float out_data_709[4096];
169 float out_data_2020[4096];
171 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
172 tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
173 EffectChainTester tester2(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
174 tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
177 for (unsigned i = 0; i < 4096; ++i) {
178 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 0.001);
179 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
181 EXPECT_GT(sqdiff, 1e-6);
184 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
185 // all of the possible 12-bit input values (every other level translates to the
186 // same value). Thus, this test has extremely loose bounds; if we ever decide
187 // to start supporting fp32, we should re-run this and tighten them a lot.
188 TEST(GammaCompressionEffectTest, Rec2020_12Bit_Inaccuracy) {
189 float data[4096], expected_data[4096], out_data[4096];
191 for (int i = 0; i < 4096; ++i) {
192 double x = i / 4095.0;
194 expected_data[i] = x;
196 // Rec. 2020, page 3.
197 if (x < 0.0181 * 4.5) {
200 data[i] = pow((x + 0.0993) / 1.0993, 1.0 / 0.45);
204 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
205 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
207 // Maximum absolute error is 120% of one pixel level. For comparison,
208 // a straightforward ALU solution (using a branch and pow()), used as a
209 // “high anchor” to indicate limitations of float arithmetic etc.,
210 // reaches maximum absolute error of 71.1% of one pixel level
211 // and rms of 0.9e-6, so this is probably a combination of input
212 // precision and inaccuracies in the polynomial approximation.
213 expect_equal(expected_data, out_data, 4096, 1, 1.2 / 4095.0, 1e-5);