]> git.sesse.net Git - movit/blob - blur_effect_test.cpp
Rework PaddingEffect alpha handling, which also fixes a long-standing assertion failure.
[movit] / blur_effect_test.cpp
1 // Unit tests for BlurEffect.
2 #include <epoxy/gl.h>
3 #include <math.h>
4 #include <string.h>
5
6 #include "blur_effect.h"
7 #include "effect_chain.h"
8 #include "gtest/gtest.h"
9 #include "image_format.h"
10 #include "test_util.h"
11
12 namespace movit {
13
14 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
15         const int size = 4;
16
17         float data[size * size] = {
18                 0.0, 1.0, 0.0, 1.0,
19                 0.0, 1.0, 1.0, 0.0,
20                 0.0, 0.5, 1.0, 0.5,
21                 0.0, 0.0, 0.0, 0.0,
22         };
23         float out_data[size * size];
24
25         for (int num_taps = 2; num_taps < 20; num_taps += 2) {
26                 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
27                 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
28                 ASSERT_TRUE(blur_effect->set_float("radius", 0.0f));
29                 ASSERT_TRUE(blur_effect->set_int("num_taps", num_taps));
30                 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
31
32                 expect_equal(data, out_data, size, size);
33         }
34 }
35
36 namespace {
37
38 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
39 {
40         // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization.
41         const float c1 = M_PI / (sigma * 4 * sqrt(3.0f));
42         const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f));
43
44         for (int y = 0; y < size; ++y) {
45                 for (int x = 0; x < size; ++x) {
46                         float xd = c2 * (x - x0);
47                         float yd = c2 * (y - y0);
48                         out[y * size + x] += (strength * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd));
49                 }
50         }
51 }
52
53 }  // namespace
54
55 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
56         const float sigma = 3.0f;
57         const int size = 32;
58         const int x1 = 8;
59         const int y1 = 8;
60         const int x2 = 20;
61         const int y2 = 10;
62
63         float data[size * size], out_data[size * size], expected_data[size * size];
64         memset(data, 0, sizeof(data));
65         memset(expected_data, 0, sizeof(expected_data));
66
67         data[y1 * size + x1] = 1.0f;
68         data[y2 * size + x2] = 1.0f;
69
70         add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
71         add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
72
73         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
74         Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
75         ASSERT_TRUE(blur_effect->set_float("radius", sigma));
76         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
77
78         // Set the limits a bit tighter than usual, since there is so little energy in here.
79         expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5);
80 }
81
82 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
83         const float sigma = 20.0f;  // Large enough that we will begin scaling.
84         const int size = 256;
85         const int x1 = 64;
86         const int y1 = 64;
87         const int x2 = 160;
88         const int y2 = 120;
89
90         static float data[size * size], out_data[size * size], expected_data[size * size];
91         memset(data, 0, sizeof(data));
92         memset(expected_data, 0, sizeof(expected_data));
93
94         data[y1 * size + x1] = 128.0f;
95         data[y2 * size + x2] = 128.0f;
96
97         add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
98         add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
99
100         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
101         Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
102         ASSERT_TRUE(blur_effect->set_float("radius", sigma));
103         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
104
105         expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);
106 }
107
108 TEST(BlurEffectTest, BlurTwoDotsSmallRadiusFewerTaps) {
109         const float sigma = 3.0f;
110         const int size = 32;
111         const int x1 = 8;
112         const int y1 = 8;
113         const int x2 = 20;
114         const int y2 = 10;
115
116         float data[size * size], out_data[size * size], expected_data[size * size];
117         memset(data, 0, sizeof(data));
118         memset(expected_data, 0, sizeof(expected_data));
119
120         data[y1 * size + x1] = 1.0f;
121         data[y2 * size + x2] = 1.0f;
122
123         add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
124         add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
125
126         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
127         Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
128         ASSERT_TRUE(blur_effect->set_float("radius", sigma));
129         ASSERT_TRUE(blur_effect->set_int("num_taps", 10));
130         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
131
132         // Set the limits a bit tighter than usual, since there is so little energy in here.
133         expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5);
134 }
135
136 }  // namespace movit