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