From: Steinar H. Gunderson Date: Sat, 19 Jan 2013 22:11:33 +0000 (+0100) Subject: Add unit tests for GlowEffect. X-Git-Tag: 1.0~161 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=020ba675b7be6ceeb284be91039efc0dc0986227;hp=6a8b7d750dff76dee320ad973a52d4d9720510b9 Add unit tests for GlowEffect. --- diff --git a/Makefile b/Makefile index 306a730..8bfaf9f 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ TESTS += saturation_effect_test TESTS += deconvolution_sharpen_effect_test TESTS += blur_effect_test TESTS += unsharp_mask_effect_test +TESTS += glow_effect_test TESTS += diffusion_effect_test TESTS += white_balance_effect_test TESTS += lift_gamma_gain_effect_test diff --git a/glow_effect.h b/glow_effect.h index c0ac9ce..8b21e52 100644 --- a/glow_effect.h +++ b/glow_effect.h @@ -3,9 +3,6 @@ // Glow: Cut out the highlights of the image (everything above a certain threshold), // blur them, and overlay them onto the original image. -// -// FIXME: This might be broken after MixEffect started working in premultiplied alpha. -// We need to think about how this is going to work, and then add a test. #include "effect.h" diff --git a/glow_effect_test.cpp b/glow_effect_test.cpp new file mode 100644 index 0000000..77c9d12 --- /dev/null +++ b/glow_effect_test.cpp @@ -0,0 +1,107 @@ +// Unit tests for GlowEffect. + +#include + +#include "test_util.h" +#include "gtest/gtest.h" +#include "glow_effect.h" + +TEST(GlowEffectTest, NoAmountDoesNothing) { + const int size = 4; + + float data[size * size] = { + 0.0, 1.0, 0.0, 1.0, + 0.0, 1.0, 1.0, 0.0, + 0.0, 0.5, 1.0, 0.5, + 0.0, 0.0, 0.0, 0.0, + }; + float out_data[size * size]; + + EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); + Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect()); + ASSERT_TRUE(glow_effect->set_float("radius", 2.0f)); + ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", 0.0f)); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(data, out_data, size, size); +} + +TEST(GlowEffectTest, SingleDot) { + const int size = 13; + const float sigma = 0.5f; + const float amount = 0.2f; + + float data[] = { // One single dot in the middle. + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, + }; + float expected_data[size * size], out_data[size * size]; + + // The output should be equal to the input, plus approximately a logistic blob. + // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization. + const float c1 = M_PI / (sigma * 4 * sqrt(3.0f)); + const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f)); + + for (int y = 0; y < size; ++y) { + for (int x = 0; x < size; ++x) { + float xd = c2 * (x - 6); + float yd = c2 * (y - 6); + expected_data[y * size + x] = data[y * size + x] + + (amount * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd)); + } + } + + EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); + Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect()); + ASSERT_TRUE(glow_effect->set_float("radius", sigma)); + ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", amount)); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3); +} + +TEST(GlowEffectTest, GlowsOntoZeroAlpha) { + const int size = 7; + const float sigma = 1.0f; + const float amount = 1.0f; + + float data[4 * size] = { + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.5, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + 0.0, 0.0, 0.0, 0.0, + }; + float expected_data[4 * size] = { + 0.0, 1.0, 0.0, 0.002, + 0.0, 1.0, 0.0, 0.014, + 0.0, 1.0, 0.0, 0.065, + 0.0, 1.0, 0.0, 0.635, + 0.0, 1.0, 0.0, 0.065, + 0.0, 1.0, 0.0, 0.014, + 0.0, 1.0, 0.0, 0.002, + }; + + float out_data[4 * size]; + + EffectChainTester tester(data, 1, size, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR); + Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect()); + ASSERT_TRUE(glow_effect->set_float("radius", sigma)); + ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", amount)); + tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(expected_data, out_data, 4, size); +}