From ac5f7aa64ab690120a71fe45ee4b9bbc56610191 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 13 Oct 2012 15:41:31 +0200 Subject: [PATCH] Add a unit test for BlurEffect. --- .gitignore | 1 + Makefile | 3 ++ blur_effect_test.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++ effect_chain_test.cpp | 1 + 4 files changed, 102 insertions(+) create mode 100644 blur_effect_test.cpp diff --git a/.gitignore b/.gitignore index 8e11285..a7069f6 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ gamma_expansion_effect_test mix_effect_test saturation_effect_test deconvolution_sharpen_effect_test +blur_effect_test chain-*.frag diff --git a/Makefile b/Makefile index 41f0de4..e93f0a5 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ TESTS += gamma_expansion_effect_test TESTS += gamma_compression_effect_test TESTS += saturation_effect_test TESTS += deconvolution_sharpen_effect_test +TESTS += blur_effect_test # Core. LIB_OBJS=util.o widgets.o effect.o effect_chain.o @@ -70,6 +71,8 @@ saturation_effect_test: saturation_effect_test.o $(TEST_OBJS) libmovit.a $(CXX) -o $@ $^ $(LDFLAGS) deconvolution_sharpen_effect_test: deconvolution_sharpen_effect_test.o $(TEST_OBJS) libmovit.a $(CXX) -o $@ $^ $(LDFLAGS) +blur_effect_test: blur_effect_test.o $(TEST_OBJS) libmovit.a + $(CXX) -o $@ $^ $(LDFLAGS) OBJS=$(DEMO_OBJS) $(LIB_OBJS) $(TEST_OBJS) $(TESTS:=.o) diff --git a/blur_effect_test.cpp b/blur_effect_test.cpp new file mode 100644 index 0000000..d458e45 --- /dev/null +++ b/blur_effect_test.cpp @@ -0,0 +1,97 @@ +// Unit tests for BlurEffect. +#include + +#include "test_util.h" +#include "gtest/gtest.h" +#include "blur_effect.h" + +TEST(BlurEffectTest, IdentityTransformDoesNothing) { + 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 *blur_effect = tester.get_chain()->add_effect(new BlurEffect()); + ASSERT_TRUE(blur_effect->set_float("radius", 0.0f)); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(data, out_data, size, size); +} + +namespace { + +void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma) +{ + // 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 - x0); + float yd = c2 * (y - y0); + out[y * size + x] += (strength * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd)); + } + } +} + +} // namespace + +TEST(BlurEffectTest, BlurTwoDotsSmallRadius) { + const float sigma = 3.0f; + const int size = 32; + const int x1 = 8; + const int y1 = 8; + const int x2 = 20; + const int y2 = 10; + + float data[size * size], out_data[size * size], expected_data[size * size]; + memset(data, 0, sizeof(data)); + memset(expected_data, 0, sizeof(expected_data)); + + data[y1 * size + x1] = 1.0f; + data[y2 * size + x2] = 1.0f; + + add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma); + add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma); + + EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); + Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect()); + ASSERT_TRUE(blur_effect->set_float("radius", sigma)); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + // Set the limits a bit tighter than usual, since there is so little energy in here. + expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5); +} + +TEST(BlurEffectTest, BlurTwoDotsLargeRadius) { + const float sigma = 20.0f; // Large enough that we will begin scaling. + const int size = 256; + const int x1 = 64; + const int y1 = 64; + const int x2 = 160; + const int y2 = 120; + + static float data[size * size], out_data[size * size], expected_data[size * size]; + memset(data, 0, sizeof(data)); + memset(expected_data, 0, sizeof(expected_data)); + + data[y1 * size + x1] = 128.0f; + data[y2 * size + x2] = 128.0f; + + add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma); + add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma); + + EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); + Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect()); + ASSERT_TRUE(blur_effect->set_float("radius", sigma)); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3); +} diff --git a/effect_chain_test.cpp b/effect_chain_test.cpp index 2ae38dd..28197f9 100644 --- a/effect_chain_test.cpp +++ b/effect_chain_test.cpp @@ -6,6 +6,7 @@ #include "flat_input.h" #include "gtest/gtest.h" #include "mirror_effect.h" +#include "resize_effect.h" #include "opengl.h" #include "test_util.h" -- 2.39.2