1 // Unit tests for WhiteBalanceEffect.
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
9 #include "white_balance_effect.h"
13 TEST(WhiteBalanceEffectTest, GrayNeutralDoesNothing) {
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,
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);
31 expect_equal(data, out_data, 4, 5);
34 TEST(WhiteBalanceEffectTest, SettingReddishNeutralColorNeutralizesReddishColor) {
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,
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);
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]);
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]);
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);
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);
75 TEST(WhiteBalanceEffectTest, HigherColorTemperatureIncreasesBlue) {
77 0.0f, 0.0f, 0.0f, 1.0f,
78 0.5f, 0.5f, 0.5f, 1.0f,
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);
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]);
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);
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);