1 // Unit tests for BlurEffect.
5 #include "blur_effect.h"
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
8 #include "image_format.h"
11 TEST(BlurEffectTest, IdentityTransformDoesNothing) {
14 float data[size * size] = {
20 float out_data[size * size];
22 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
23 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
24 ASSERT_TRUE(blur_effect->set_float("radius", 0.0f));
25 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
27 expect_equal(data, out_data, size, size);
32 void add_blurred_point(float *out, int size, int x0, int y0, float strength, float sigma)
34 // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization.
35 const float c1 = M_PI / (sigma * 4 * sqrt(3.0f));
36 const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f));
38 for (int y = 0; y < size; ++y) {
39 for (int x = 0; x < size; ++x) {
40 float xd = c2 * (x - x0);
41 float yd = c2 * (y - y0);
42 out[y * size + x] += (strength * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd));
49 TEST(BlurEffectTest, BlurTwoDotsSmallRadius) {
50 const float sigma = 3.0f;
57 float data[size * size], out_data[size * size], expected_data[size * size];
58 memset(data, 0, sizeof(data));
59 memset(expected_data, 0, sizeof(expected_data));
61 data[y1 * size + x1] = 1.0f;
62 data[y2 * size + x2] = 1.0f;
64 add_blurred_point(expected_data, size, x1, y1, 1.0f, sigma);
65 add_blurred_point(expected_data, size, x2, y2, 1.0f, sigma);
67 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
68 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
69 ASSERT_TRUE(blur_effect->set_float("radius", sigma));
70 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
72 // Set the limits a bit tighter than usual, since there is so little energy in here.
73 expect_equal(expected_data, out_data, size, size, 1e-3, 1e-5);
76 TEST(BlurEffectTest, BlurTwoDotsLargeRadius) {
77 const float sigma = 20.0f; // Large enough that we will begin scaling.
84 static float data[size * size], out_data[size * size], expected_data[size * size];
85 memset(data, 0, sizeof(data));
86 memset(expected_data, 0, sizeof(expected_data));
88 data[y1 * size + x1] = 128.0f;
89 data[y2 * size + x2] = 128.0f;
91 add_blurred_point(expected_data, size, x1, y1, 128.0f, sigma);
92 add_blurred_point(expected_data, size, x2, y2, 128.0f, sigma);
94 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
95 Effect *blur_effect = tester.get_chain()->add_effect(new BlurEffect());
96 ASSERT_TRUE(blur_effect->set_float("radius", sigma));
97 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
99 expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);