Add unit tests for WhiteBalanceEffect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Oct 2012 16:03:28 +0000 (18:03 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Oct 2012 16:03:28 +0000 (18:03 +0200)
.gitignore
Makefile
white_balance_effect_test.cpp [new file with mode: 0644]

index 2c7cfb7..a5589c9 100644 (file)
@@ -18,5 +18,6 @@ deconvolution_sharpen_effect_test
 blur_effect_test
 unsharp_mask_effect_test
 diffusion_effect_test
+white_balance_effect_test
 flat_input_test
 chain-*.frag
index 27bca34..427e7dd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ TESTS += deconvolution_sharpen_effect_test
 TESTS += blur_effect_test
 TESTS += unsharp_mask_effect_test
 TESTS += diffusion_effect_test
+TESTS += white_balance_effect_test
 TESTS += flat_input_test
 
 # Core.
@@ -83,6 +84,8 @@ unsharp_mask_effect_test: unsharp_mask_effect_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
 diffusion_effect_test: diffusion_effect_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
+white_balance_effect_test: white_balance_effect_test.o $(TEST_OBJS) libmovit.a
+       $(CXX) -o $@ $^ $(LDFLAGS)
 flat_input_test: flat_input_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
 
diff --git a/white_balance_effect_test.cpp b/white_balance_effect_test.cpp
new file mode 100644 (file)
index 0000000..966cba3
--- /dev/null
@@ -0,0 +1,100 @@
+// Unit tests for WhiteBalanceEffect.
+
+#include "test_util.h"
+#include "gtest/gtest.h"
+#include "white_balance_effect.h"
+
+TEST(WhiteBalanceEffectTest, GrayNeutralDoesNothing) {
+       float data[] = {
+               0.0f, 0.0f, 0.0f, 1.0f,
+               0.5f, 0.5f, 0.5f, 0.3f,
+               1.0f, 0.0f, 0.0f, 1.0f,
+               0.0f, 1.0f, 0.0f, 0.7f,
+               0.0f, 0.0f, 1.0f, 1.0f,
+       };
+       float neutral[] = {
+               0.5f, 0.5f, 0.5f
+       };
+
+       float out_data[5 * 4];
+       EffectChainTester tester(data, 1, 5, FORMAT_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
+       ASSERT_TRUE(white_balance_effect->set_vec3("neutral_color", neutral));
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(data, out_data, 4, 5);
+}
+
+TEST(WhiteBalanceEffectTest, SettingReddishNeutralColorNeutralizesReddishColor) {
+       float data[] = {
+               0.0f, 0.0f, 0.0f, 1.0f,
+               0.6f, 0.5f, 0.5f, 1.0f,
+               0.5f, 0.5f, 0.5f, 1.0f,
+       };
+       float neutral[] = {
+               0.6f, 0.5f, 0.5f
+       };
+
+       float out_data[3 * 4];
+       EffectChainTester tester(data, 1, 3, FORMAT_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
+       ASSERT_TRUE(white_balance_effect->set_vec3("neutral_color", neutral));
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Black should stay black.
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 0]);
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 1]);
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 2]);
+       EXPECT_FLOAT_EQ(1.0f, out_data[4 * 0 + 3]);
+
+       // The neutral color should now have R=G=B.
+       EXPECT_NEAR(out_data[4 * 1 + 0], out_data[4 * 1 + 1], 0.001);
+       EXPECT_NEAR(out_data[4 * 1 + 0], out_data[4 * 1 + 2], 0.001);
+       EXPECT_FLOAT_EQ(1.0f, out_data[4 * 1 + 3]);
+
+       // It should also have kept its luminance.
+       float old_luminance = 0.2126 * data[4 * 1 + 0] +
+               0.7152 * data[4 * 1 + 1] +
+               0.0722 * data[4 * 1 + 2];       
+       float new_luminance = 0.2126 * out_data[4 * 1 + 0] +
+               0.7152 * out_data[4 * 1 + 1] +
+               0.0722 * out_data[4 * 1 + 2];
+       EXPECT_NEAR(old_luminance, new_luminance, 0.001);
+
+       // Finally, the old gray should now have significantly less red than green and blue.
+       EXPECT_GT(out_data[4 * 2 + 1] - out_data[4 * 2 + 0], 0.05);
+       EXPECT_GT(out_data[4 * 2 + 2] - out_data[4 * 2 + 0], 0.05);
+}
+
+TEST(WhiteBalanceEffectTest, HigherColorTemperatureIncreasesBlue) {
+       float data[] = {
+               0.0f, 0.0f, 0.0f, 1.0f,
+               0.5f, 0.5f, 0.5f, 1.0f,
+       };
+
+       float out_data[2 * 4];
+       EffectChainTester tester(data, 1, 2, FORMAT_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *white_balance_effect = tester.get_chain()->add_effect(new WhiteBalanceEffect());
+       ASSERT_TRUE(white_balance_effect->set_float("output_color_temperature", 10000.0f));
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Black should stay black.
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 0]);
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 1]);
+       EXPECT_FLOAT_EQ(0.0f, out_data[4 * 0 + 2]);
+       EXPECT_FLOAT_EQ(1.0f, out_data[4 * 0 + 3]);
+
+       // The neutral color should have kept its luminance.
+       float old_luminance = 0.2126 * data[4 * 1 + 0] +
+               0.7152 * data[4 * 1 + 1] +
+               0.0722 * data[4 * 1 + 2];       
+       float new_luminance = 0.2126 * out_data[4 * 1 + 0] +
+               0.7152 * out_data[4 * 1 + 1] +
+               0.0722 * out_data[4 * 1 + 2];
+       EXPECT_NEAR(old_luminance, new_luminance, 0.001);
+
+       // It should also have significantly more blue then green,
+       // and significantly less red than green.
+       EXPECT_GT(out_data[4 * 1 + 2] - out_data[4 * 1 + 1], 0.05);
+       EXPECT_GT(out_data[4 * 1 + 1] - out_data[4 * 1 + 0], 0.05);
+}