]> git.sesse.net Git - movit/blob - blur_effect_test.cpp
Run include-what-you-use over all of movit. Some hand tuning.
[movit] / blur_effect_test.cpp
1 // Unit tests for BlurEffect.
2 #include <math.h>
3 #include <string.h>
4
5 #include "blur_effect.h"
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
8 #include "image_format.h"
9 #include "test_util.h"
10
11 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
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 *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
24         ASSERT_TRUE(blur_effect->set_float("radius", 0.0f));
25         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
26
27         expect_equal(data, out_data, size, size);
28 }
29
30 namespace {
31
32 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
33 {
34         // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization.
35         const float c1 = M_PI / (sigma * 4 * sqrt(3.0f));
36         const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f));
37
38         for (int y = 0; y < size; ++y) {
39                 for (int x = 0; x < size; ++x) {
40                         float xd = c2 * (x - x0);
41                         float yd = c2 * (y - y0);
42                         out[y * size + x] += (strength * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd));
43                 }
44         }
45 }
46
47 }  // namespace
48
49 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
50         const float sigma = 3.0f;
51         const int size = 32;
52         const int x1 = 8;
53         const int y1 = 8;
54         const int x2 = 20;
55         const int y2 = 10;
56
57         float data[size * size], out_data[size * size], expected_data[size * size];
58         memset(data, 0, sizeof(data));
59         memset(expected_data, 0, sizeof(expected_data));
60
61         data[y1 * size + x1] = 1.0f;
62         data[y2 * size + x2] = 1.0f;
63
64         add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
65         add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
66
67         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
68         Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
69         ASSERT_TRUE(blur_effect->set_float("radius", sigma));
70         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
71
72         // Set the limits a bit tighter than usual, since there is so little energy in here.
73         expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5);
74 }
75
76 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
77         const float sigma = 20.0f;  // Large enough that we will begin scaling.
78         const int size = 256;
79         const int x1 = 64;
80         const int y1 = 64;
81         const int x2 = 160;
82         const int y2 = 120;
83
84         static float data[size * size], out_data[size * size], expected_data[size * size];
85         memset(data, 0, sizeof(data));
86         memset(expected_data, 0, sizeof(expected_data));
87
88         data[y1 * size + x1] = 128.0f;
89         data[y2 * size + x2] = 128.0f;
90
91         add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
92         add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
93
94         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
95         Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
96         ASSERT_TRUE(blur_effect->set_float("radius", sigma));
97         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
98
99         expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);
100 }