]> git.sesse.net Git - movit/commitdiff
Add unit tests for UnsharpMaskEffect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Oct 2012 19:28:41 +0000 (21:28 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 13 Oct 2012 19:28:41 +0000 (21:28 +0200)
.gitignore
Makefile
unsharp_mask_effect_test.cpp [new file with mode: 0644]

index a7069f60143b43b49f5405186c238e956c38a3cd..afc81dcf91febf28abe9c3471858b933095887d4 100644 (file)
@@ -15,4 +15,5 @@ mix_effect_test
 saturation_effect_test
 deconvolution_sharpen_effect_test
 blur_effect_test
+unsharp_mask_effect_test
 chain-*.frag
index e93f0a5809309975d7e808722ba769cf96600abc..42d8cc613e74ed566335fcfde23aca3b2666c025 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -21,6 +21,7 @@ TESTS += gamma_compression_effect_test
 TESTS += saturation_effect_test
 TESTS += deconvolution_sharpen_effect_test
 TESTS += blur_effect_test
+TESTS += unsharp_mask_effect_test
 
 # Core.
 LIB_OBJS=util.o widgets.o effect.o effect_chain.o
@@ -73,6 +74,8 @@ deconvolution_sharpen_effect_test: deconvolution_sharpen_effect_test.o $(TEST_OB
        $(CXX) -o $@ $^ $(LDFLAGS)
 blur_effect_test: blur_effect_test.o $(TEST_OBJS) libmovit.a
        $(CXX) -o $@ $^ $(LDFLAGS)
+unsharp_mask_effect_test: unsharp_mask_effect_test.o $(TEST_OBJS) libmovit.a
+       $(CXX) -o $@ $^ $(LDFLAGS)
 
 OBJS=$(DEMO_OBJS) $(LIB_OBJS) $(TEST_OBJS) $(TESTS:=.o)
 
diff --git a/unsharp_mask_effect_test.cpp b/unsharp_mask_effect_test.cpp
new file mode 100644 (file)
index 0000000..a957ef1
--- /dev/null
@@ -0,0 +1,71 @@
+// Unit tests for UnsharpMaskEffect.
+
+#include <math.h>
+
+#include "test_util.h"
+#include "gtest/gtest.h"
+#include "unsharp_mask_effect.h"
+
+TEST(UnsharpMaskEffectTest, 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 *unsharp_mask_effect = tester.get_chain()->add_effect(new UnsharpMaskEffect());
+       ASSERT_TRUE(unsharp_mask_effect->set_float("radius", 2.0f));
+       ASSERT_TRUE(unsharp_mask_effect->set_float("amount", 0.0f));
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       expect_equal(data, out_data, size, size);
+}
+
+TEST(UnsharpMaskEffectTest, UnblursGaussianBlur) {
+       const int size = 13;
+       const float sigma = 0.5f;
+
+       float data[size * size], out_data[size * size];
+       float expected_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, 
+       };
+
+       // Create a Gaussian input. (Note that our blur is not Gaussian.)
+       for (int y = 0; y < size; ++y) {
+               for (int x = 0; x < size; ++x) {
+                       float z = hypot(x - 6, y - 6);
+                       data[y * size + x] = exp(-z*z / (2.0 * sigma * sigma)) / (2.0 * M_PI * sigma * sigma);
+               }
+       }
+
+       EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *unsharp_mask_effect = tester.get_chain()->add_effect(new UnsharpMaskEffect());
+       ASSERT_TRUE(unsharp_mask_effect->set_float("radius", sigma));
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       // Check the center sample separately; it's bound to be the sample with the largest
+       // single error, and we know we can't get it perfect anyway.
+       int center = size / 2;
+       EXPECT_GT(out_data[center * size + center], 0.45f);
+       out_data[center * size + center] = 1.0f;
+
+       // Add some leeway for the rest; unsharp masking is not expected to be extremely good.
+       expect_equal(expected_data, out_data, size, size, 0.1, 0.001);
+}