1 // Unit tests for GammaExpansionEffect.
6 #include "gamma_expansion_effect.h"
7 #include "gtest/gtest.h"
8 #include "gtest/gtest-message.h"
11 TEST(GammaExpansionEffectTest, sRGB_KeyValues) {
14 0.040f, 0.041f, // On either side of the discontinuity.
16 float expected_data[] = {
21 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
22 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
24 expect_equal(expected_data, out_data, 2, 2);
27 TEST(GammaExpansionEffectTest, sRGB_RampAlwaysIncreases) {
28 float data[256], out_data[256];
29 for (unsigned i = 0; i < 256; ++i) {
32 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
33 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
35 for (unsigned i = 1; i < 256; ++i) {
36 EXPECT_GT(out_data[i], out_data[i - 1])
37 << "No increase between " << i-1 << " and " << i;
41 TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
43 0.0f, 0.0f, 0.0f, 0.0f,
44 0.0f, 0.0f, 0.0f, 0.25f,
45 0.0f, 0.0f, 0.0f, 0.5f,
46 0.0f, 0.0f, 0.0f, 0.75f,
47 0.0f, 0.0f, 0.0f, 1.0f,
49 float out_data[5 * 4];
50 EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
51 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
53 expect_equal(data, out_data, 5, 1);
56 TEST(GammaExpansionEffectTest, sRGB_Accuracy) {
57 float data[256], expected_data[256], out_data[256];
59 for (int i = 0; i < 256; ++i) {
64 // From the Wikipedia article on sRGB.
66 expected_data[i] = x / 12.92;
68 expected_data[i] = pow((x + 0.055) / 1.055, 2.4);
72 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
73 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
75 // Accuracy limits; for comparison, limits for a straightforward ALU solution
76 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
77 // indicate limitations of float arithmetic etc.:
79 // Maximum absolute error: 0.1% of max energy (0.051%)
80 // Maximum relative error: 2.5% of correct answer (0.093%)
81 // 25% of difference to next pixel level (6.18%)
82 // Allowed RMS error: 0.0001 (0.000010)
84 test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
87 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
90 0.080f, 0.082f, // On either side of the discontinuity.
92 float expected_data[] = {
97 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
98 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
100 expect_equal(expected_data, out_data, 2, 2);
103 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
104 float data[256], out_data[256];
105 for (unsigned i = 0; i < 256; ++i) {
106 data[i] = i / 255.0f;
108 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
109 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
111 for (unsigned i = 1; i < 256; ++i) {
112 EXPECT_GT(out_data[i], out_data[i - 1])
113 << "No increase between " << i-1 << " and " << i;
117 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
119 0.0f, 0.0f, 0.0f, 0.0f,
120 0.0f, 0.0f, 0.0f, 0.25f,
121 0.0f, 0.0f, 0.0f, 0.5f,
122 0.0f, 0.0f, 0.0f, 0.75f,
123 0.0f, 0.0f, 0.0f, 1.0f,
125 float out_data[5 * 4];
126 EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
127 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
129 expect_equal(data, out_data, 5, 1);
132 TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
133 float data[256], expected_data[256], out_data[256];
135 for (int i = 0; i < 256; ++i) {
136 double x = i / 255.0;
140 // Rec. 2020, page 3.
141 if (x < 0.018 * 4.5) {
142 expected_data[i] = x / 4.5;
144 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
148 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
149 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
151 // Accuracy limits; for comparison, limits for a straightforward ALU solution
152 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
153 // indicate limitations of float arithmetic etc.:
155 // Maximum absolute error: 0.1% of max energy (0.046%)
156 // Maximum relative error: 1.0% of correct answer (0.080%)
157 // 10% of difference to next pixel level (6.19%)
158 // Allowed RMS error: 0.0001 (0.000010)
160 test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
163 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
164 // input range and somewhat looser error bounds. (One could claim that this is
165 // already on the limit of what we can reasonably do with fp16 input, if you
166 // look at the local relative error.)
167 TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
168 float data[1024], expected_data[1024], out_data[1024];
170 for (int i = 0; i < 1024; ++i) {
171 double x = i / 1023.0;
175 // Rec. 2020, page 3.
176 if (x < 0.018 * 4.5) {
177 expected_data[i] = x / 4.5;
179 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
183 EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
184 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
186 // Accuracy limits; for comparison, limits for a straightforward ALU solution
187 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
188 // indicate limitations of float arithmetic etc.:
190 // Maximum absolute error: 0.1% of max energy (0.036%)
191 // Maximum relative error: 1.0% of correct answer (0.064%)
192 // 30% of difference to next pixel level (24.9%)
193 // Allowed RMS error: 0.0001 (0.000005)
195 test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
198 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
200 for (unsigned i = 0; i < 256; ++i) {
201 data[i] = i / 255.0f;
203 float out_data_709[256];
204 float out_data_2020[256];
206 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
207 tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
208 EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
209 tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
212 for (unsigned i = 0; i < 256; ++i) {
213 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
214 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
216 EXPECT_GT(sqdiff, 1e-6);
219 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
220 // all of the possible 12-bit input values (every other level translates to the
221 // same value). Thus, this test has extremely loose bounds; if we ever decide
222 // to start supporting fp32, we should re-run this and tighten them a lot.
223 TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
224 float data[4096], expected_data[4096], out_data[4096];
226 for (int i = 0; i < 4096; ++i) {
227 double x = i / 4095.0;
231 // Rec. 2020, page 3.
232 if (x < 0.0181 * 4.5) {
233 expected_data[i] = x / 4.5;
235 expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
239 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
240 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
242 // Accuracy limits; for comparison, limits for a straightforward ALU solution
243 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
244 // indicate limitations of float arithmetic etc.:
246 // Maximum absolute error: 0.1% of max energy (0.050%)
247 // Maximum relative error: 1.0% of correct answer (0.050%)
248 // 250% of difference to next pixel level (100.00%)
249 // Allowed RMS error: 0.0001 (0.000003)
251 test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);