1 // Unit tests for UnsharpMaskEffect.
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
9 #include "unsharp_mask_effect.h"
11 TEST(UnsharpMaskEffectTest, NoAmountDoesNothing) {
14 float data[size * size] = {
20 float out_data[size * size];
22 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
23 Effect *unsharp_mask_effect = tester.get_chain()->add_effect(new UnsharpMaskEffect());
24 ASSERT_TRUE(unsharp_mask_effect->set_float("radius", 2.0f));
25 ASSERT_TRUE(unsharp_mask_effect->set_float("amount", 0.0f));
26 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
28 expect_equal(data, out_data, size, size);
31 TEST(UnsharpMaskEffectTest, UnblursGaussianBlur) {
33 const float sigma = 0.5f;
35 float data[size * size], out_data[size * size];
36 float expected_data[] = { // One single dot in the middle.
37 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
38 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
39 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
40 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
41 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
42 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
43 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,
44 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
45 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
46 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
47 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
48 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
49 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
52 // Create a Gaussian input. (Note that our blur is not Gaussian.)
53 for (int y = 0; y < size; ++y) {
54 for (int x = 0; x < size; ++x) {
55 float z = hypot(x - 6, y - 6);
56 data[y * size + x] = exp(-z*z / (2.0 * sigma * sigma)) / (2.0 * M_PI * sigma * sigma);
60 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
61 Effect *unsharp_mask_effect = tester.get_chain()->add_effect(new UnsharpMaskEffect());
62 ASSERT_TRUE(unsharp_mask_effect->set_float("radius", sigma));
63 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
65 // Check the center sample separately; it's bound to be the sample with the largest
66 // single error, and we know we can't get it perfect anyway.
67 int center = size / 2;
68 EXPECT_GT(out_data[center * size + center], 0.45f);
69 out_data[center * size + center] = 1.0f;
71 // Add some leeway for the rest; unsharp masking is not expected to be extremely good.
72 expect_equal(expected_data, out_data, size, size, 0.1, 0.001);