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"
18 TEST(GammaCompressionEffectTest, sRGB_KeyValues) {
21 0.00309f, 0.00317f, // On either side of the discontinuity.
22 -0.5f, 1.5f, // To check clamping.
24 float expected_data[] = {
30 EffectChainTester tester(data, 2, 3, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
31 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
33 expect_equal(expected_data, out_data, 2, 3);
36 TEST(GammaCompressionEffectTest, sRGB_RampAlwaysIncreases) {
37 float data[256], out_data[256];
38 for (unsigned i = 0; i < 256; ++i) {
41 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
42 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
44 for (unsigned i = 1; i < 256; ++i) {
45 EXPECT_GT(out_data[i], out_data[i - 1])
46 << "No increase between " << i-1 << " and " << i;
50 TEST(GammaCompressionEffectTest, sRGB_Accuracy) {
51 float data[256], expected_data[256], out_data[256];
53 for (int i = 0; i < 256; ++i) {
57 data[i] = srgb_to_linear(x);
60 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
61 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
63 // Maximum absolute error is 25% of one pixel level. For comparison,
64 // a straightforward ALU solution (using a branch and pow()), used as a
65 // “high anchor” to indicate limitations of float arithmetic etc.,
66 // reaches maximum absolute error of 3.7% of one pixel level
68 expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-4);
71 TEST(GammaCompressionEffectTest, Rec709_KeyValues) {
74 0.017778f, 0.018167f, // On either side of the discontinuity.
76 float expected_data[] = {
81 EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
82 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
84 expect_equal(expected_data, out_data, 2, 2);
87 TEST(GammaCompressionEffectTest, Rec709_RampAlwaysIncreases) {
88 float data[256], out_data[256];
89 for (unsigned i = 0; i < 256; ++i) {
92 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
93 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
95 for (unsigned i = 1; i < 256; ++i) {
96 EXPECT_GT(out_data[i], out_data[i - 1])
97 << "No increase between " << i-1 << " and " << i;
101 TEST(GammaCompressionEffectTest, Rec709_Accuracy) {
102 float data[256], expected_data[256], out_data[256];
104 for (int i = 0; i < 256; ++i) {
105 double x = i / 255.0;
107 expected_data[i] = x;
109 // Rec. 2020, page 3.
110 if (x < 0.018 * 4.5) {
113 data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
117 EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
118 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
120 // Maximum absolute error is 25% of one pixel level. For comparison,
121 // a straightforward ALU solution (using a branch and pow()), used as a
122 // “high anchor” to indicate limitations of float arithmetic etc.,
123 // reaches maximum absolute error of 3.7% of one pixel level
124 // and rms of 3.5e-6.
125 expect_equal(expected_data, out_data, 256, 1, 0.25 / 255.0, 1e-5);
128 // This test tests the same gamma ramp as Rec709_Accuracy, but with 10-bit
129 // input range and somewhat looser error bounds. (One could claim that this is
130 // already on the limit of what we can reasonably do with fp16 input, if you
131 // look at the local relative error.)
132 TEST(GammaCompressionEffectTest, Rec2020_10Bit_Accuracy) {
133 float data[1024], expected_data[1024], out_data[1024];
135 for (int i = 0; i < 1024; ++i) {
136 double x = i / 1023.0;
138 expected_data[i] = x;
140 // Rec. 2020, page 3.
141 if (x < 0.018 * 4.5) {
144 data[i] = pow((x + 0.099) / 1.099, 1.0 / 0.45);
148 EffectChainTester tester(data, 1024, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
149 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_10_BIT);
151 // Maximum absolute error is 30% of one pixel level. For comparison,
152 // a straightforward ALU solution (using a branch and pow()), used as a
153 // “high anchor” to indicate limitations of float arithmetic etc.,
154 // reaches maximum absolute error of 25.2% of one pixel level
155 // and rms of 1.8e-6, so this is probably mostly related to input precision.
156 expect_equal(expected_data, out_data, 1024, 1, 0.30 / 1023.0, 1e-5);
159 TEST(GammaCompressionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
161 for (unsigned i = 0; i < 4096; ++i) {
162 data[i] = i / 4095.0f;
164 float out_data_709[4096];
165 float out_data_2020[4096];
167 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
168 tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_REC_709);
169 EffectChainTester tester2(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
170 tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
173 for (unsigned i = 0; i < 4096; ++i) {
174 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 0.001);
175 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
177 EXPECT_GT(sqdiff, 1e-6);
180 // The fp16 _input_ provided by FlatInput is not enough to distinguish between
181 // all of the possible 12-bit input values (every other level translates to the
182 // same value). Thus, this test has extremely loose bounds; if we ever decide
183 // to start supporting fp32, we should re-run this and tighten them a lot.
184 TEST(GammaCompressionEffectTest, Rec2020_12Bit_Inaccuracy) {
185 float data[4096], expected_data[4096], out_data[4096];
187 for (int i = 0; i < 4096; ++i) {
188 double x = i / 4095.0;
190 expected_data[i] = x;
192 // Rec. 2020, page 3.
193 if (x < 0.0181 * 4.5) {
196 data[i] = pow((x + 0.0993) / 1.0993, 1.0 / 0.45);
200 EffectChainTester tester(data, 4096, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA32F);
201 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
203 // Maximum absolute error is 120% of one pixel level. For comparison,
204 // a straightforward ALU solution (using a branch and pow()), used as a
205 // “high anchor” to indicate limitations of float arithmetic etc.,
206 // reaches maximum absolute error of 71.1% of one pixel level
207 // and rms of 0.9e-6, so this is probably a combination of input
208 // precision and inaccuracies in the polynomial approximation.
209 expect_equal(expected_data, out_data, 4096, 1, 1.2 / 4095.0, 1e-5);