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