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