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