]> git.sesse.net Git - movit/blob - glow_effect_test.cpp
11083a9ca75a3412fe88a9c6b2e4c4914df6b7d5
[movit] / glow_effect_test.cpp
1 // Unit tests for GlowEffect.
2
3 #include <GL/glew.h>
4 #include <math.h>
5
6 #include "effect_chain.h"
7 #include "glow_effect.h"
8 #include "gtest/gtest.h"
9 #include "image_format.h"
10 #include "test_util.h"
11
12 namespace movit {
13
14 TEST(GlowEffectTest, NoAmountDoesNothing) {
15         const int size = 4;
16
17         float data[size * size] = {
18                 0.0, 1.0, 0.0, 1.0,
19                 0.0, 1.0, 1.0, 0.0,
20                 0.0, 0.5, 1.0, 0.5,
21                 0.0, 0.0, 0.0, 0.0,
22         };
23         float out_data[size * size];
24
25         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
26         Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect());
27         ASSERT_TRUE(glow_effect->set_float("radius", 2.0f));
28         ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", 0.0f));
29         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
30
31         expect_equal(data, out_data, size, size);
32 }
33
34 TEST(GlowEffectTest, SingleDot) {
35         const int size = 13;
36         const float sigma = 0.5f;
37         const float amount = 0.2f;
38
39         float data[] = {  // One single dot in the middle.
40                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
41                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
42                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
43                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
44                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
45                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
46                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
47                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
48                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
49                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
50                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
51                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
52                 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
53         };
54         float expected_data[size * size], out_data[size * size];
55
56         // The output should be equal to the input, plus approximately a logistic blob.
57         // From http://en.wikipedia.org/wiki/Logistic_distribution#Alternative_parameterization.
58         const float c1 = M_PI / (sigma * 4 * sqrt(3.0f));
59         const float c2 = M_PI / (sigma * 2.0 * sqrt(3.0f));
60
61         for (int y = 0; y < size; ++y) {
62                 for (int x = 0; x < size; ++x) {
63                         float xd = c2 * (x - 6);
64                         float yd = c2 * (y - 6);
65                         expected_data[y * size + x] = data[y * size + x] +
66                                 (amount * c1 * c1) / (cosh(xd) * cosh(xd) * cosh(yd) * cosh(yd));
67                 }
68         }
69
70         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
71         Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect());
72         ASSERT_TRUE(glow_effect->set_float("radius", sigma));
73         ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", amount));
74         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
75
76         expect_equal(expected_data, out_data, size, size, 0.1f, 1e-3);
77 }
78
79 TEST(GlowEffectTest, GlowsOntoZeroAlpha) {
80         const int size = 7;
81         const float sigma = 1.0f;
82         const float amount = 1.0f;
83
84         float data[4 * size] = {
85                 0.0, 0.0, 0.0, 0.0,
86                 0.0, 0.0, 0.0, 0.0,
87                 0.0, 0.0, 0.0, 0.0,
88                 0.0, 1.0, 0.0, 0.5,
89                 0.0, 0.0, 0.0, 0.0,
90                 0.0, 0.0, 0.0, 0.0,
91                 0.0, 0.0, 0.0, 0.0,
92         };
93         float expected_data[4 * size] = {
94                 0.0, 1.0, 0.0, 0.002, 
95                 0.0, 1.0, 0.0, 0.014,
96                 0.0, 1.0, 0.0, 0.065, 
97                 0.0, 1.0, 0.0, 0.635, 
98                 0.0, 1.0, 0.0, 0.065, 
99                 0.0, 1.0, 0.0, 0.014,
100                 0.0, 1.0, 0.0, 0.002, 
101         };
102
103         float out_data[4 * size];
104
105         EffectChainTester tester(data, 1, size, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
106         Effect *glow_effect = tester.get_chain()->add_effect(new GlowEffect());
107         ASSERT_TRUE(glow_effect->set_float("radius", sigma));
108         ASSERT_TRUE(glow_effect->set_float("blurred_mix_amount", amount));
109         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
110
111         expect_equal(expected_data, out_data, 4, size);
112 }
113
114 }  // namespace movit