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