Add missing unit test.
[movit] / padding_effect_test.cpp
1 // Unit tests for AlphaMultiplicationEffect.
2
3 #include "test_util.h"
4 #include "flat_input.h"
5 #include "padding_effect.h"
6 #include "gtest/gtest.h"
7
8 TEST(PaddingEffectTest, SimpleCenter) {
9         float data[2 * 2] = {
10                 1.0f, 0.5f,
11                 0.8f, 0.3f,
12         };
13         float expected_data[4 * 4] = {
14                 0.0f, 0.0f, 0.0f, 0.0f,
15                 0.0f, 1.0f, 0.5f, 0.0f,
16                 0.0f, 0.8f, 0.3f, 0.0f,
17                 0.0f, 0.0f, 0.0f, 0.0f,
18         };
19         float out_data[4 * 4];
20
21         EffectChainTester tester(NULL, 4, 4);
22
23         ImageFormat format;
24         format.color_space = COLORSPACE_sRGB;
25         format.gamma_curve = GAMMA_LINEAR;
26
27         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
28         input->set_pixel_data(data);
29         tester.get_chain()->add_input(input);
30
31         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
32         CHECK(effect->set_int("width", 4));
33         CHECK(effect->set_int("height", 4));
34         CHECK(effect->set_float("left", 1.0f));
35         CHECK(effect->set_float("top", 1.0f));
36
37         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
38         expect_equal(expected_data, out_data, 4, 4);
39 }
40
41 TEST(PaddingEffectTest, WhiteBorderColor) {
42         float data[2 * 2] = {
43                 1.0f, 0.5f,
44                 0.8f, 0.3f,
45         };
46         float expected_data[4 * 4] = {
47                 1.0f, 1.0f, 1.0f, 1.0f,
48                 1.0f, 1.0f, 0.5f, 1.0f,
49                 1.0f, 0.8f, 0.3f, 1.0f,
50                 1.0f, 1.0f, 1.0f, 1.0f,
51         };
52         float out_data[4 * 4];
53
54         EffectChainTester tester(NULL, 4, 4);
55
56         ImageFormat format;
57         format.color_space = COLORSPACE_sRGB;
58         format.gamma_curve = GAMMA_LINEAR;
59
60         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
61         input->set_pixel_data(data);
62         tester.get_chain()->add_input(input);
63
64         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
65         CHECK(effect->set_int("width", 4));
66         CHECK(effect->set_int("height", 4));
67         CHECK(effect->set_float("left", 1.0f));
68         CHECK(effect->set_float("top", 1.0f));
69
70         RGBATriplet border_color(1.0f, 1.0f, 1.0f, 1.0f);
71         CHECK(effect->set_vec4("border_color", (float *)&border_color));
72
73         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
74         expect_equal(expected_data, out_data, 4, 4);
75 }
76
77 TEST(PaddingEffectTest, BorderColorIsInLinearGamma) {
78         float data[4 * 1] = {
79                 0.2f, 0.4f, 0.6f, 0.8f,
80         };
81         float expected_data[4 * 2] = {
82                 0.5005, 0.7051, 0.8677, 0.7998,  // Pixel from data[].
83                 0.5005, 0.7051, 0.8677, 0.7998,  // Pixel from the border color.
84         };
85         float out_data[4 * 2];
86
87         EffectChainTester tester(NULL, 1, 2);
88
89         ImageFormat format;
90         format.color_space = COLORSPACE_sRGB;
91         format.gamma_curve = GAMMA_LINEAR;
92
93         FlatInput *input = new FlatInput(format, FORMAT_RGBA_PREMULTIPLIED_ALPHA, GL_FLOAT, 1, 1);
94         input->set_pixel_data(data);
95         tester.get_chain()->add_input(input);
96
97         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
98         CHECK(effect->set_int("width", 1));
99         CHECK(effect->set_int("height", 2));
100         CHECK(effect->set_float("left", 0.0f));
101         CHECK(effect->set_float("top", 0.0f));
102
103         RGBATriplet border_color(0.2f, 0.4f, 0.6f, 0.8f);  // Same as the pixel in data[].
104         CHECK(effect->set_vec4("border_color", (float *)&border_color));
105
106         tester.run(out_data, GL_RGBA, COLORSPACE_REC_601_625, GAMMA_REC_601, OUTPUT_ALPHA_POSTMULTIPLIED);
107         expect_equal(expected_data, out_data, 4, 2);
108 }
109
110 TEST(PaddingEffectTest, DifferentXAndYOffset) {
111         float data[1 * 1] = {
112                 1.0f
113         };
114         float expected_data[3 * 3] = {
115                 0.0f, 0.0f, 0.0f,
116                 0.0f, 0.0f, 1.0f,
117                 0.0f, 0.0f, 0.0f,
118         };
119         float out_data[3 * 3];
120
121         EffectChainTester tester(NULL, 3, 3);
122
123         ImageFormat format;
124         format.color_space = COLORSPACE_sRGB;
125         format.gamma_curve = GAMMA_LINEAR;
126
127         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 1, 1);
128         input->set_pixel_data(data);
129         tester.get_chain()->add_input(input);
130
131         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
132         CHECK(effect->set_int("width", 3));
133         CHECK(effect->set_int("height", 3));
134         CHECK(effect->set_float("left", 2.0f));
135         CHECK(effect->set_float("top", 1.0f));
136
137         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
138         expect_equal(expected_data, out_data, 3, 3);
139 }
140
141 TEST(PaddingEffectTest, NonIntegerOffset) {
142         float data[4 * 1] = {
143                 0.25f, 0.50f, 0.75f, 1.0f,
144         };
145         // Note that the first pixel is completely blank, since the cutoff goes
146         // at the immediate left of the texel.
147         float expected_data[5 * 2] = {
148                 0.0f, 0.4375f, 0.6875f, 0.9375f, 0.0f,
149                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
150         };
151         float out_data[5 * 2];
152
153         EffectChainTester tester(NULL, 5, 2);
154
155         ImageFormat format;
156         format.color_space = COLORSPACE_sRGB;
157         format.gamma_curve = GAMMA_LINEAR;
158
159         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 4, 1);
160         input->set_pixel_data(data);
161         tester.get_chain()->add_input(input);
162
163         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
164         CHECK(effect->set_int("width", 5));
165         CHECK(effect->set_int("height", 2));
166         CHECK(effect->set_float("left", 0.25f));
167         CHECK(effect->set_float("top", 0.0f));
168
169         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
170         expect_equal(expected_data, out_data, 5, 2);
171 }
172
173 TEST(PaddingEffectTest, Crop) {
174         float data[2 * 2] = {
175                 1.0f, 0.5f,
176                 0.8f, 0.3f,
177         };
178         float expected_data[1 * 1] = {
179                 0.3f,
180         };
181         float out_data[1 * 1];
182
183         EffectChainTester tester(NULL, 1, 1);
184
185         ImageFormat format;
186         format.color_space = COLORSPACE_sRGB;
187         format.gamma_curve = GAMMA_LINEAR;
188
189         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
190         input->set_pixel_data(data);
191         tester.get_chain()->add_input(input);
192
193         Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
194         CHECK(effect->set_int("width", 1));
195         CHECK(effect->set_int("height", 1));
196         CHECK(effect->set_float("left", -1.0f));
197         CHECK(effect->set_float("top", -1.0f));
198
199         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
200         expect_equal(expected_data, out_data, 1, 1);
201 }