]> git.sesse.net Git - movit/blob - white_balance_effect_test.cpp
Invert the meaning of the last_phase boolean to execute_phase().
[movit] / white_balance_effect_test.cpp
1 // Unit tests for WhiteBalanceEffect.
2
3 #include <epoxy/gl.h>
4
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
8 #include "test_util.h"
9 #include "white_balance_effect.h"
10
11 namespace movit {
12
13 TEST(WhiteBalanceEffectTest, GrayNeutralDoesNothing) {
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         float neutral[] = {
22                 0.5f, 0.5f, 0.5f
23         };
24
25         float out_data[5 * 4];
26         EffectChainTester tester(data, 1, 5, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
27         Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
28         ASSERT_TRUE(white_balance_effect->set_vec3("neutral_color", neutral));
29         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
30
31         expect_equal(data, out_data, 4, 5);
32 }
33
34 TEST(WhiteBalanceEffectTest, SettingReddishNeutralColorNeutralizesReddishColor) {
35         float data[] = {
36                 0.0f, 0.0f, 0.0f, 1.0f,
37                 0.6f, 0.5f, 0.5f, 1.0f,
38                 0.5f, 0.5f, 0.5f, 1.0f,
39         };
40         float neutral[] = {
41                 0.6f, 0.5f, 0.5f
42         };
43
44         float out_data[3 * 4];
45         EffectChainTester tester(data, 1, 3, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
46         Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
47         ASSERT_TRUE(white_balance_effect->set_vec3("neutral_color", neutral));
48         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
49
50         // Black should stay black.
51         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 0]);
52         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 1]);
53         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 2]);
54         EXPECT_FLOAT_EQ(1.0f, out_data[4 * 0 + 3]);
55
56         // The neutral color should now have R=G=B.
57         EXPECT_NEAR(out_data[4 * 1 + 0], out_data[4 * 1 + 1], 0.001);
58         EXPECT_NEAR(out_data[4 * 1 + 0], out_data[4 * 1 + 2], 0.001);
59         EXPECT_FLOAT_EQ(1.0f, out_data[4 * 1 + 3]);
60
61         // It should also have kept its luminance.
62         float old_luminance = 0.2126 * data[4 * 1 + 0] +
63                 0.7152 * data[4 * 1 + 1] +
64                 0.0722 * data[4 * 1 + 2];       
65         float new_luminance = 0.2126 * out_data[4 * 1 + 0] +
66                 0.7152 * out_data[4 * 1 + 1] +
67                 0.0722 * out_data[4 * 1 + 2];
68         EXPECT_NEAR(old_luminance, new_luminance, 0.001);
69
70         // Finally, the old gray should now have significantly less red than green and blue.
71         EXPECT_GT(out_data[4 * 2 + 1] - out_data[4 * 2 + 0], 0.05);
72         EXPECT_GT(out_data[4 * 2 + 2] - out_data[4 * 2 + 0], 0.05);
73 }
74
75 TEST(WhiteBalanceEffectTest, HigherColorTemperatureIncreasesBlue) {
76         float data[] = {
77                 0.0f, 0.0f, 0.0f, 1.0f,
78                 0.5f, 0.5f, 0.5f, 1.0f,
79         };
80
81         float out_data[2 * 4];
82         EffectChainTester tester(data, 1, 2, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
83         Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
84         ASSERT_TRUE(white_balance_effect->set_float("output_color_temperature", 10000.0f));
85         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
86
87         // Black should stay black.
88         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 0]);
89         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 1]);
90         EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 2]);
91         EXPECT_FLOAT_EQ(1.0f, out_data[4 * 0 + 3]);
92
93         // The neutral color should have kept its luminance.
94         float old_luminance = 0.2126 * data[4 * 1 + 0] +
95                 0.7152 * data[4 * 1 + 1] +
96                 0.0722 * data[4 * 1 + 2];       
97         float new_luminance = 0.2126 * out_data[4 * 1 + 0] +
98                 0.7152 * out_data[4 * 1 + 1] +
99                 0.0722 * out_data[4 * 1 + 2];
100         EXPECT_NEAR(old_luminance, new_luminance, 0.001);
101
102         // It should also have significantly more blue then green,
103         // and significantly less red than green.
104         EXPECT_GT(out_data[4 * 1 + 2] - out_data[4 * 1 + 1], 0.05);
105         EXPECT_GT(out_data[4 * 1 + 1] - out_data[4 * 1 + 0], 0.05);
106 }
107
108 }  // namespace movit