]> git.sesse.net Git - movit/blob - unsharp_mask_effect_test.cpp
Comment and README updates about Rec. 2020 in light of the accuracy test results.
[movit] / unsharp_mask_effect_test.cpp
1 // Unit tests for UnsharpMaskEffect.
2
3 #include <math.h>
4
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
8 #include "test_util.h"
9 #include "unsharp_mask_effect.h"
10
11 TEST(UnsharpMaskEffectTest, NoAmountDoesNothing) {
12         const int size = 4;
13
14         float data[size * size] = {
15                 0.0, 1.0, 0.0, 1.0,
16                 0.0, 1.0, 1.0, 0.0,
17                 0.0, 0.5, 1.0, 0.5,
18                 0.0, 0.0, 0.0, 0.0,
19         };
20         float out_data[size * size];
21
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);
27
28         expect_equal(data, out_data, size, size);
29 }
30
31 TEST(UnsharpMaskEffectTest, UnblursGaussianBlur) {
32         const int size = 13;
33         const float sigma = 0.5f;
34
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, 
50         };
51
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);
57                 }
58         }
59
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);
64
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;
70
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);
73 }