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