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