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 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);
30 expect_equal(data, out_data, size, size);
35 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
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));
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));
52 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
53 const float sigma = 3.0f;
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));
64 data[y1 * size + x1] = 1.0f;
65 data[y2 * size + x2] = 1.0f;
67 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
68 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
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);
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);
79 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
80 const float sigma = 20.0f; // Large enough that we will begin scaling.
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));
91 data[y1 * size + x1] = 128.0f;
92 data[y2 * size + x2] = 128.0f;
94 add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
95 add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
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);
102 expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);