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"
12 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
15 float data[size * size] = {
21 float out_data[size * size];
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);
28 expect_equal(data, out_data, size, size);
33 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
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));
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));
50 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
51 const float sigma = 3.0f;
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));
62 data[y1 * size + x1] = 1.0f;
63 data[y2 * size + x2] = 1.0f;
65 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
66 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
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);
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);
77 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
78 const float sigma = 20.0f; // Large enough that we will begin scaling.
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));
89 data[y1 * size + x1] = 128.0f;
90 data[y2 * size + x2] = 128.0f;
92 add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
93 add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
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);
100 expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);