1 // Unit tests for GammaExpansionEffect.
6 #include "gamma_expansion_effect.h"
7 #include "gtest/gtest.h"
8 #include "gtest/gtest-message.h"
13 TEST(GammaExpansionEffectTest, sRGB_KeyValues) {
16 0.040f, 0.041f, // On either side of the discontinuity.
18 float expected_data[] = {
23 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
24 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
26 expect_equal(expected_data, out_data, 2, 2);
29 TEST(GammaExpansionEffectTest, sRGB_RampAlwaysIncreases) {
30 float data[256], out_data[256];
31 for (unsigned i = 0; i < 256; ++i) {
34 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
35 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
37 for (unsigned i = 1; i < 256; ++i) {
38 EXPECT_GT(out_data[i], out_data[i - 1])
39 << "No increase between " << i-1 << " and " << i;
43 TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
45 0.0f, 0.0f, 0.0f, 0.0f,
46 0.0f, 0.0f, 0.0f, 0.25f,
47 0.0f, 0.0f, 0.0f, 0.5f,
48 0.0f, 0.0f, 0.0f, 0.75f,
49 0.0f, 0.0f, 0.0f, 1.0f,
51 float out_data[5 * 4];
52 EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
53 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
55 expect_equal(data, out_data, 5, 1);
58 TEST(GammaExpansionEffectTest, sRGB_Accuracy) {
59 float data[256], expected_data[256], out_data[256];
61 for (int i = 0; i < 256; ++i) {
65 expected_data[i] = srgb_to_linear(x);
68 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
69 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
71 // Accuracy limits; for comparison, limits for a straightforward ALU solution
72 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
73 // indicate limitations of float arithmetic etc.:
75 // Maximum absolute error: 0.1% of max energy (0.051%)
76 // Maximum relative error: 2.5% of correct answer (0.093%)
77 // 25% of difference to next pixel level (6.18%)
78 // Allowed RMS error: 0.0001 (0.000010)
80 test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
83 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
86 0.080f, 0.082f, // On either side of the discontinuity.
88 float expected_data[] = {
93 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
94 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
96 expect_equal(expected_data, out_data, 2, 2);
99 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
100 float data[256], out_data[256];
101 for (unsigned i = 0; i < 256; ++i) {
102 data[i] = i / 255.0f;
104 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
105 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
107 for (unsigned i = 1; i < 256; ++i) {
108 EXPECT_GT(out_data[i], out_data[i - 1])
109 << "No increase between " << i-1 << " and " << i;
113 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
115 0.0f, 0.0f, 0.0f, 0.0f,
116 0.0f, 0.0f, 0.0f, 0.25f,
117 0.0f, 0.0f, 0.0f, 0.5f,
118 0.0f, 0.0f, 0.0f, 0.75f,
119 0.0f, 0.0f, 0.0f, 1.0f,
121 float out_data[5 * 4];
122 EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
123 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
125 expect_equal(data, out_data, 5, 1);
128 TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
129 float data[256], expected_data[256], out_data[256];
131 for (int i = 0; i < 256; ++i) {
132 double x = i / 255.0;
136 // Rec. 2020, page 3.
137 if (x < 0.018 * 4.5) {
138 expected_data[i] = x / 4.5;
140 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
144 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
145 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
147 // Accuracy limits; for comparison, limits for a straightforward ALU solution
148 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
149 // indicate limitations of float arithmetic etc.:
151 // Maximum absolute error: 0.1% of max energy (0.046%)
152 // Maximum relative error: 1.0% of correct answer (0.080%)
153 // 10% of difference to next pixel level (6.19%)
154 // Allowed RMS error: 0.0001 (0.000010)
156 test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
159 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
160 // input range and somewhat looser error bounds. (One could claim that this is
161 // already on the limit of what we can reasonably do with fp16 input, if you
162 // look at the local relative error.)
163 TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
164 float data[1024], expected_data[1024], out_data[1024];
166 for (int i = 0; i < 1024; ++i) {
167 double x = i / 1023.0;
171 // Rec. 2020, page 3.
172 if (x < 0.018 * 4.5) {
173 expected_data[i] = x / 4.5;
175 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
179 EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
180 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
182 // Accuracy limits; for comparison, limits for a straightforward ALU solution
183 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
184 // indicate limitations of float arithmetic etc.:
186 // Maximum absolute error: 0.1% of max energy (0.036%)
187 // Maximum relative error: 1.0% of correct answer (0.064%)
188 // 30% of difference to next pixel level (24.9%)
189 // Allowed RMS error: 0.0001 (0.000005)
191 test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
194 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
196 for (unsigned i = 0; i < 256; ++i) {
197 data[i] = i / 255.0f;
199 float out_data_709[256];
200 float out_data_2020[256];
202 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
203 tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
204 EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
205 tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
208 for (unsigned i = 0; i < 256; ++i) {
209 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
210 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
212 EXPECT_GT(sqdiff, 1e-6);
215 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
216 // all of the possible 12-bit input values (every other level translates to the
217 // same value). Thus, this test has extremely loose bounds; if we ever decide
218 // to start supporting fp32, we should re-run this and tighten them a lot.
219 TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
220 float data[4096], expected_data[4096], out_data[4096];
222 for (int i = 0; i < 4096; ++i) {
223 double x = i / 4095.0;
227 // Rec. 2020, page 3.
228 if (x < 0.0181 * 4.5) {
229 expected_data[i] = x / 4.5;
231 expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
235 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
236 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
238 // Accuracy limits; for comparison, limits for a straightforward ALU solution
239 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
240 // indicate limitations of float arithmetic etc.:
242 // Maximum absolute error: 0.1% of max energy (0.050%)
243 // Maximum relative error: 1.0% of correct answer (0.050%)
244 // 250% of difference to next pixel level (100.00%)
245 // Allowed RMS error: 0.0001 (0.000003)
247 test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);