dc50f911f9dca122e1e6e67a1eee6131b1305dbd
[movit] / gamma_expansion_effect_test.cpp
1 // Unit tests for GammaExpansionEffect.
2
3 #include <GL/glew.h>
4
5 #include "gamma_expansion_effect.h"
6 #include "gtest/gtest.h"
7 #include "test_util.h"
8
9 TEST(GammaExpansionEffectTest, sRGB_KeyValues) {
10         float data[] = {
11                 0.0f, 1.0f,
12                 0.040f, 0.041f,  // On either side of the discontinuity.
13         };
14         float expected_data[] = {
15                 0.0f, 1.0f,
16                 0.00309f, 0.00317f, 
17         };
18         float out_data[4];
19         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
20         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
21
22         expect_equal(expected_data, out_data, 2, 2);
23 }
24
25 TEST(GammaExpansionEffectTest, sRGB_RampAlwaysIncreases) {
26         float data[256], out_data[256];
27         for (unsigned i = 0; i < 256; ++i) {
28                 data[i] = i / 255.0f;
29         }
30         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
31         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
32
33         for (unsigned i = 1; i < 256; ++i) {
34                 EXPECT_GT(out_data[i], out_data[i - 1])
35                    << "No increase between " << i-1 << " and " << i;
36         }
37 }
38
39 TEST(GammaExpansionEffectTest, sRGB_AlphaIsUnchanged) {
40         float data[] = {
41                 0.0f, 0.0f, 0.0f, 0.0f,
42                 0.0f, 0.0f, 0.0f, 0.25f,
43                 0.0f, 0.0f, 0.0f, 0.5f,
44                 0.0f, 0.0f, 0.0f, 0.75f,
45                 0.0f, 0.0f, 0.0f, 1.0f,
46         };
47         float out_data[5 * 4];
48         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
49         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
50
51         expect_equal(data, out_data, 5, 1);
52 }
53
54 TEST(GammaExpansionEffectTest, Rec709_KeyValues) {
55         float data[] = {
56                 0.0f, 1.0f,
57                 0.080f, 0.082f,  // On either side of the discontinuity.
58         };
59         float expected_data[] = {
60                 0.0f, 1.0f,
61                 0.017778f, 0.018167f,
62         };
63         float out_data[4];
64         EffectChainTester tester(data, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
65         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
66
67         expect_equal(expected_data, out_data, 2, 2);
68 }
69
70 TEST(GammaExpansionEffectTest, Rec709_RampAlwaysIncreases) {
71         float data[256], out_data[256];
72         for (unsigned i = 0; i < 256; ++i) {
73                 data[i] = i / 255.0f;
74         }
75         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
76         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
77
78         for (unsigned i = 1; i < 256; ++i) {
79                 EXPECT_GT(out_data[i], out_data[i - 1])
80                    << "No increase between " << i-1 << " and " << i;
81         }
82 }
83
84 TEST(GammaExpansionEffectTest, Rec709_AlphaIsUnchanged) {
85         float data[] = {
86                 0.0f, 0.0f, 0.0f, 0.0f,
87                 0.0f, 0.0f, 0.0f, 0.25f,
88                 0.0f, 0.0f, 0.0f, 0.5f,
89                 0.0f, 0.0f, 0.0f, 0.75f,
90                 0.0f, 0.0f, 0.0f, 1.0f,
91         };
92         float out_data[5 * 4];
93         EffectChainTester tester(data, 5, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_REC_709);
94         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
95
96         expect_equal(data, out_data, 5, 1);
97 }
98
99 TEST(GammaExpansionEffectTest, Rec2020_12BitIsVeryCloseToRec709) {
100         float data[256];
101         for (unsigned i = 0; i < 256; ++i) {
102                 data[i] = i / 255.0f;
103         }
104         float out_data_709[256];
105         float out_data_2020[256];
106
107         EffectChainTester tester(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_709);
108         tester.run(out_data_709, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
109         EffectChainTester tester2(data, 256, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_REC_2020_12_BIT);
110         tester2.run(out_data_2020, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
111
112         double sqdiff = 0.0;
113         for (unsigned i = 0; i < 256; ++i) {
114                 EXPECT_NEAR(out_data_709[i], out_data_2020[i], 1e-3);
115                 sqdiff += (out_data_709[i] - out_data_2020[i]) * (out_data_709[i] - out_data_2020[i]);
116         }
117         EXPECT_GT(sqdiff, 1e-6);
118 }