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) {
66 // From the Wikipedia article on sRGB.
68 expected_data[i] = x / 12.92;
70 expected_data[i] = pow((x + 0.055) / 1.055, 2.4);
74 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB, GL_RGBA32F);
75 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
77 // Accuracy limits; for comparison, limits for a straightforward ALU solution
78 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
79 // indicate limitations of float arithmetic etc.:
81 // Maximum absolute error: 0.1% of max energy (0.051%)
82 // Maximum relative error: 2.5% of correct answer (0.093%)
83 // 25% of difference to next pixel level (6.18%)
84 // Allowed RMS error: 0.0001 (0.000010)
86 test_accuracy(expected_data, out_data, 256, 1e-3, 0.025, 0.25, 1e-4);
89 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
92 0.080f, 0.082f, // On either side of the discontinuity.
94 float expected_data[] = {
99 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
100 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
102 expect_equal(expected_data, out_data, 2, 2);
105 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
106 float data[256], out_data[256];
107 for (unsigned i = 0; i < 256; ++i) {
108 data[i] = i / 255.0f;
110 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
111 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
113 for (unsigned i = 1; i < 256; ++i) {
114 EXPECT_GT(out_data[i], out_data[i - 1])
115 << "No increase between " << i-1 << " and " << i;
119 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
121 0.0f, 0.0f, 0.0f, 0.0f,
122 0.0f, 0.0f, 0.0f, 0.25f,
123 0.0f, 0.0f, 0.0f, 0.5f,
124 0.0f, 0.0f, 0.0f, 0.75f,
125 0.0f, 0.0f, 0.0f, 1.0f,
127 float out_data[5 * 4];
128 EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
129 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
131 expect_equal(data, out_data, 5, 1);
134 TEST(GammaExpansionEffectTest, Rec709_Accuracy) {
135 float data[256], expected_data[256], out_data[256];
137 for (int i = 0; i < 256; ++i) {
138 double x = i / 255.0;
142 // Rec. 2020, page 3.
143 if (x < 0.018 * 4.5) {
144 expected_data[i] = x / 4.5;
146 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
150 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709, GL_RGBA32F);
151 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
153 // Accuracy limits; for comparison, limits for a straightforward ALU solution
154 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
155 // indicate limitations of float arithmetic etc.:
157 // Maximum absolute error: 0.1% of max energy (0.046%)
158 // Maximum relative error: 1.0% of correct answer (0.080%)
159 // 10% of difference to next pixel level (6.19%)
160 // Allowed RMS error: 0.0001 (0.000010)
162 test_accuracy(expected_data, out_data, 256, 1e-3, 0.01, 0.1, 1e-4);
165 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
166 // input range and somewhat looser error bounds. (One could claim that this is
167 // already on the limit of what we can reasonably do with fp16 input, if you
168 // look at the local relative error.)
169 TEST(GammaExpansionEffectTest, Rec2020_10Bit_Accuracy) {
170 float data[1024], expected_data[1024], out_data[1024];
172 for (int i = 0; i < 1024; ++i) {
173 double x = i / 1023.0;
177 // Rec. 2020, page 3.
178 if (x < 0.018 * 4.5) {
179 expected_data[i] = x / 4.5;
181 expected_data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
185 EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT, GL_RGBA32F);
186 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
188 // Accuracy limits; for comparison, limits for a straightforward ALU solution
189 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
190 // indicate limitations of float arithmetic etc.:
192 // Maximum absolute error: 0.1% of max energy (0.036%)
193 // Maximum relative error: 1.0% of correct answer (0.064%)
194 // 30% of difference to next pixel level (24.9%)
195 // Allowed RMS error: 0.0001 (0.000005)
197 test_accuracy(expected_data, out_data, 1024, 1e-3, 0.01, 0.30, 1e-4);
200 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
202 for (unsigned i = 0; i < 256; ++i) {
203 data[i] = i / 255.0f;
205 float out_data_709[256];
206 float out_data_2020[256];
208 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
209 tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
210 EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
211 tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
214 for (unsigned i = 0; i < 256; ++i) {
215 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
216 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
218 EXPECT_GT(sqdiff, 1e-6);
221 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
222 // all of the possible 12-bit input values (every other level translates to the
223 // same value). Thus, this test has extremely loose bounds; if we ever decide
224 // to start supporting fp32, we should re-run this and tighten them a lot.
225 TEST(GammaExpansionEffectTest, Rec2020_12Bit_Inaccuracy) {
226 float data[4096], expected_data[4096], out_data[4096];
228 for (int i = 0; i < 4096; ++i) {
229 double x = i / 4095.0;
233 // Rec. 2020, page 3.
234 if (x < 0.0181 * 4.5) {
235 expected_data[i] = x / 4.5;
237 expected_data[i] = pow((x + 0.0993) / 1.0993, 1.0/0.45);
241 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT, GL_RGBA32F);
242 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
244 // Accuracy limits; for comparison, limits for a straightforward ALU solution
245 // (using a branch and pow()) in parenthesis, used as a “high anchor” to
246 // indicate limitations of float arithmetic etc.:
248 // Maximum absolute error: 0.1% of max energy (0.050%)
249 // Maximum relative error: 1.0% of correct answer (0.050%)
250 // 250% of difference to next pixel level (100.00%)
251 // Allowed RMS error: 0.0001 (0.000003)
253 test_accuracy(expected_data, out_data, 4096, 1e-3, 0.01, 2.50, 1e-4);