Merge branch 'master' into epoxy
[movit] / lift_gamma_gain_effect_test.cpp
1 // Unit tests for LiftGammaGainEffect.
2
3 #include <epoxy/gl.h>
4
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
8 #include "lift_gamma_gain_effect.h"
9 #include "test_util.h"
10
11 namespace movit {
12
13 TEST(LiftGammaGainEffectTest, DefaultIsNoop) {
14         float data[] = {
15                 0.0f, 0.0f, 0.0f, 1.0f,
16                 0.5f, 0.5f, 0.5f, 0.3f,
17                 1.0f, 0.0f, 0.0f, 1.0f,
18                 0.0f, 1.0f, 0.0f, 0.7f,
19                 0.0f, 0.0f, 1.0f, 1.0f,
20         };
21
22         float out_data[5 * 4];
23         EffectChainTester tester(data, 1, 5, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
24         tester.get_chain()->add_effect(new LiftGammaGainEffect());
25         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
26
27         expect_equal(data, out_data, 4, 5);
28 }
29
30 TEST(LiftGammaGainEffectTest, Gain) {
31         float data[] = {
32                 0.0f, 0.0f, 0.0f, 1.0f,
33                 0.5f, 0.5f, 0.5f, 0.3f,
34                 1.0f, 0.0f, 0.0f, 1.0f,
35                 0.0f, 1.0f, 0.0f, 0.7f,
36                 0.0f, 0.0f, 1.0f, 1.0f,
37         };
38         float gain[3] = { 0.8f, 1.0f, 1.2f };
39         float expected_data[] = {
40                 0.0f, 0.0f, 0.0f, 1.0f,
41                 0.4f, 0.5f, 0.6f, 0.3f,
42                 0.8f, 0.0f, 0.0f, 1.0f,
43                 0.0f, 1.0f, 0.0f, 0.7f,
44                 0.0f, 0.0f, 1.2f, 1.0f,
45         };
46
47         float out_data[5 * 4];
48         EffectChainTester tester(data, 1, 5, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
49         Effect *lgg_effect = tester.get_chain()->add_effect(new LiftGammaGainEffect());
50         ASSERT_TRUE(lgg_effect->set_vec3("gain", gain));
51         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
52
53         expect_equal(expected_data, out_data, 4, 5);
54 }
55
56 TEST(LiftGammaGainEffectTest, LiftIsDoneInApproximatelysRGB) {
57         float data[] = {
58                 0.0f, 0.0f, 0.0f, 1.0f,
59                 0.5f, 0.5f, 0.5f, 0.3f,
60                 1.0f, 0.0f, 0.0f, 1.0f,
61                 0.0f, 1.0f, 0.0f, 0.7f,
62                 0.0f, 0.0f, 1.0f, 1.0f,
63         };
64         float lift[3] = { 0.0f, 0.1f, 0.2f };
65         float expected_data[] = {
66                 0.0f, 0.1f , 0.2f, 1.0f,
67                 0.5f, 0.55f, 0.6f, 0.3f,
68                 1.0f, 0.1f,  0.2f, 1.0f,
69                 0.0f, 1.0f,  0.2f, 0.7f,
70                 0.0f, 0.1f,  1.0f, 1.0f,
71         };
72
73         float out_data[5 * 4];
74         EffectChainTester tester(data, 1, 5, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
75         Effect *lgg_effect = tester.get_chain()->add_effect(new LiftGammaGainEffect());
76         ASSERT_TRUE(lgg_effect->set_vec3("lift", lift));
77         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
78
79         // sRGB is only approximately gamma-2.2, so loosen up the limits a bit.
80         expect_equal(expected_data, out_data, 4, 5, 0.03, 0.003);
81 }
82
83 TEST(LiftGammaGainEffectTest, Gamma22IsApproximatelysRGB) {
84         float data[] = {
85                 0.0f, 0.0f, 0.0f, 1.0f,
86                 0.5f, 0.5f, 0.5f, 0.3f,
87                 1.0f, 0.0f, 0.0f, 1.0f,
88                 0.0f, 1.0f, 0.0f, 0.7f,
89                 0.0f, 0.0f, 1.0f, 1.0f,
90         };
91         float gamma[3] = { 2.2f, 2.2f, 2.2f };
92
93         float out_data[5 * 4];
94         EffectChainTester tester(data, 1, 5, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
95         Effect *lgg_effect = tester.get_chain()->add_effect(new LiftGammaGainEffect());
96         ASSERT_TRUE(lgg_effect->set_vec3("gamma", gamma));
97         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
98
99         expect_equal(data, out_data, 4, 5);
100 }
101
102 TEST(LiftGammaGainEffectTest, OutOfGamutColorsAreClipped) {
103         float data[] = {
104                 -0.5f, 0.3f, 0.0f, 1.0f,
105                  0.5f, 0.0f, 0.0f, 1.0f,
106                  0.0f, 1.5f, 0.5f, 0.3f,
107         };
108         float expected_data[] = {
109                  0.0f, 0.3f, 0.0f, 1.0f,  // Clipped to zero.
110                  0.5f, 0.0f, 0.0f, 1.0f,
111                  0.0f, 1.5f, 0.5f, 0.3f,
112         };
113
114         float out_data[3 * 4];
115         EffectChainTester tester(data, 1, 3, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
116         tester.get_chain()->add_effect(new LiftGammaGainEffect());
117         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
118
119         expect_equal(expected_data, out_data, 4, 3);
120 }
121
122 }  // namespace movit