1 // Unit tests for BlurEffect.
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"
14 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
17 float data[size * size] = {
23 float out_data[size * size];
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);
32 expect_equal(data, out_data, size, size);
38 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
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));
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));
55 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
56 const float sigma = 3.0f;
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));
67 data[y1 * size + x1] = 1.0f;
68 data[y2 * size + x2] = 1.0f;
70 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
71 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
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);
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);
82 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
83 const float sigma = 20.0f; // Large enough that we will begin scaling.
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));
94 data[y1 * size + x1] = 128.0f;
95 data[y2 * size + x2] = 128.0f;
97 add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
98 add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
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);
105 expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);
108 TEST(BlurEffectTest, BlurTwoDotsSmallRadiusFewerTaps) {
109 const float sigma = 3.0f;
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));
120 data[y1 * size + x1] = 1.0f;
121 data[y2 * size + x2] = 1.0f;
123 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
124 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
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);
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);