1 // Unit tests for BlurEffect.
5 #include "gtest/gtest.h"
6 #include "blur_effect.h"
8 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
11 float data[size * size] = {
17 float out_data[size * size];
19 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
20 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
21 ASSERT_TRUE(blur_effect->set_float("radius", 0.0f));
22 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
24 expect_equal(data, out_data, size, size);
29 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
31 // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization.
32 const float c1 = M_PI / (sigma * 4 * sqrt(3.0f));
33 const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f));
35 for (int y = 0; y < size; ++y) {
36 for (int x = 0; x < size; ++x) {
37 float xd = c2 * (x - x0);
38 float yd = c2 * (y - y0);
39 out[y * size + x] += (strength * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd));
46 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
47 const float sigma = 3.0f;
54 float data[size * size], out_data[size * size], expected_data[size * size];
55 memset(data, 0, sizeof(data));
56 memset(expected_data, 0, sizeof(expected_data));
58 data[y1 * size + x1] = 1.0f;
59 data[y2 * size + x2] = 1.0f;
61 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
62 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
64 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
65 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
66 ASSERT_TRUE(blur_effect->set_float("radius", sigma));
67 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
69 // Set the limits a bit tighter than usual, since there is so little energy in here.
70 expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5);
73 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
74 const float sigma = 20.0f; // Large enough that we will begin scaling.
81 static float data[size * size], out_data[size * size], expected_data[size * size];
82 memset(data, 0, sizeof(data));
83 memset(expected_data, 0, sizeof(expected_data));
85 data[y1 * size + x1] = 128.0f;
86 data[y2 * size + x2] = 128.0f;
88 add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
89 add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
91 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
92 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
93 ASSERT_TRUE(blur_effect->set_float("radius", sigma));
94 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
96 expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);