1 // Unit tests for PaddingEffect.
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"
16 TEST(PaddingEffectTest, SimpleCenter) {
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,
27 float out_data[4 * 4];
29 EffectChainTester tester(nullptr, 4, 4);
32 format.color_space = COLORSPACE_sRGB;
33 format.gamma_curve = GAMMA_LINEAR;
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);
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));
45 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
46 expect_equal(expected_data, out_data, 4, 4);
49 TEST(PaddingEffectTest, WhiteBorderColor) {
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,
60 float out_data[4 * 4];
62 EffectChainTester tester(nullptr, 4, 4);
65 format.color_space = COLORSPACE_sRGB;
66 format.gamma_curve = GAMMA_LINEAR;
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);
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));
78 RGBATuple border_color(1.0f, 1.0f, 1.0f, 1.0f);
79 CHECK(effect->set_vec4("border_color", (float *)&border_color));
81 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
82 expect_equal(expected_data, out_data, 4, 4);
85 TEST(PaddingEffectTest, BorderColorIsInLinearGamma) {
87 0.2f, 0.4f, 0.6f, 0.8f,
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.
93 float out_data[4 * 2];
95 EffectChainTester tester(nullptr, 1, 2);
98 format.color_space = COLORSPACE_sRGB;
99 format.gamma_curve = GAMMA_LINEAR;
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);
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));
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));
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);
118 TEST(PaddingEffectTest, DifferentXAndYOffset) {
119 float data[1 * 1] = {
122 float expected_data[3 * 3] = {
127 float out_data[3 * 3];
129 EffectChainTester tester(nullptr, 3, 3);
132 format.color_space = COLORSPACE_sRGB;
133 format.gamma_curve = GAMMA_LINEAR;
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);
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));
145 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
146 expect_equal(expected_data, out_data, 3, 3);
149 TEST(PaddingEffectTest, NonIntegerOffset) {
150 float data[4 * 1] = {
151 0.25f, 0.50f, 0.75f, 1.0f,
153 float expected_data[5 * 2] = {
154 0.1875f, 0.4375f, 0.6875f, 0.9375f, 0.25f,
155 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
157 float out_data[5 * 2];
159 EffectChainTester tester(nullptr, 5, 2);
162 format.color_space = COLORSPACE_sRGB;
163 format.gamma_curve = GAMMA_LINEAR;
165 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 4, 1);
166 input->set_pixel_data(data);
167 tester.get_chain()->add_input(input);
169 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
170 CHECK(effect->set_int("width", 5));
171 CHECK(effect->set_int("height", 2));
172 CHECK(effect->set_float("left", 0.25f));
173 CHECK(effect->set_float("top", 0.0f));
175 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
176 expect_equal(expected_data, out_data, 5, 2);
179 TEST(PaddingEffectTest, Crop) {
180 float data[2 * 2] = {
184 float expected_data[1 * 1] = {
187 float out_data[1 * 1];
189 EffectChainTester tester(nullptr, 1, 1);
192 format.color_space = COLORSPACE_sRGB;
193 format.gamma_curve = GAMMA_LINEAR;
195 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
196 input->set_pixel_data(data);
197 tester.get_chain()->add_input(input);
199 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
200 CHECK(effect->set_int("width", 1));
201 CHECK(effect->set_int("height", 1));
202 CHECK(effect->set_float("left", -1.0f));
203 CHECK(effect->set_float("top", -1.0f));
205 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
206 expect_equal(expected_data, out_data, 1, 1);
209 TEST(PaddingEffectTest, AlphaIsCorrectEvenWithNonLinearInputsAndOutputs) {
210 float data[2 * 1] = {
214 float expected_data[4 * 4] = {
215 1.0f, 1.0f, 1.0f, 0.5f,
216 1.0f, 1.0f, 1.0f, 1.0f,
217 0.8f, 0.8f, 0.8f, 1.0f,
218 1.0f, 1.0f, 1.0f, 0.5f,
220 float out_data[4 * 4];
222 EffectChainTester tester(nullptr, 1, 4);
225 format.color_space = COLORSPACE_REC_601_625;
226 format.gamma_curve = GAMMA_REC_709;
228 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 1, 2);
229 input->set_pixel_data(data);
230 tester.get_chain()->add_input(input);
232 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
233 CHECK(effect->set_int("width", 1));
234 CHECK(effect->set_int("height", 4));
235 CHECK(effect->set_float("left", 0.0f));
236 CHECK(effect->set_float("top", 1.0f));
238 RGBATuple border_color(1.0f, 1.0f, 1.0f, 0.5f);
239 CHECK(effect->set_vec4("border_color", (float *)&border_color));
241 tester.run(out_data, GL_RGBA, COLORSPACE_REC_601_625, GAMMA_REC_709, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
242 expect_equal(expected_data, out_data, 4, 4);
245 TEST(PaddingEffectTest, RedBorder) { // Not black nor white, but still a saturated primary.
246 float data[2 * 1] = {
250 float expected_data[4 * 4] = {
251 1.0f, 0.0f, 0.0f, 1.0f,
252 1.0f, 1.0f, 1.0f, 1.0f,
253 0.8f, 0.8f, 0.8f, 1.0f,
254 1.0f, 0.0f, 0.0f, 1.0f,
256 float out_data[4 * 4];
258 EffectChainTester tester(nullptr, 1, 4);
261 format.color_space = COLORSPACE_REC_601_625;
262 format.gamma_curve = GAMMA_REC_709;
264 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 1, 2);
265 input->set_pixel_data(data);
266 tester.get_chain()->add_input(input);
268 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
269 CHECK(effect->set_int("width", 1));
270 CHECK(effect->set_int("height", 4));
271 CHECK(effect->set_float("left", 0.0f));
272 CHECK(effect->set_float("top", 1.0f));
274 RGBATuple border_color(1.0f, 0.0f, 0.0f, 1.0f);
275 CHECK(effect->set_vec4("border_color", (float *)&border_color));
277 tester.run(out_data, GL_RGBA, COLORSPACE_REC_709, GAMMA_REC_709, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
278 expect_equal(expected_data, out_data, 4, 4);
281 TEST(PaddingEffectTest, BorderOffsetTopAndBottom) {
282 float data[2 * 2] = {
286 float expected_data[4 * 4] = {
287 0.0f, 0.000f, 0.000f, 0.0f,
288 0.0f, 0.750f, 0.375f, 0.0f,
289 0.0f, 0.800f, 0.300f, 0.0f,
290 0.0f, 0.200f, 0.075f, 0.0f, // Repeated pixels, 25% opacity.
292 float out_data[4 * 4];
294 EffectChainTester tester(nullptr, 4, 4);
297 format.color_space = COLORSPACE_sRGB;
298 format.gamma_curve = GAMMA_LINEAR;
300 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
301 input->set_pixel_data(data);
302 tester.get_chain()->add_input(input);
304 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
305 CHECK(effect->set_int("width", 4));
306 CHECK(effect->set_int("height", 4));
307 CHECK(effect->set_float("left", 1.0f));
308 CHECK(effect->set_float("top", 1.0f));
309 CHECK(effect->set_float("border_offset_top", 0.25f));
310 CHECK(effect->set_float("border_offset_bottom", 0.25f));
312 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
313 expect_equal(expected_data, out_data, 4, 4);
316 TEST(PaddingEffectTest, BorderOffsetLeftAndRight) {
317 float data[3 * 2] = {
321 float expected_data[4 * 2] = {
322 0.750f, 0.5f, 0.3f, 0.0f,
323 0.600f, 0.3f, 0.1f, 0.0f
325 float out_data[4 * 2];
327 EffectChainTester tester(nullptr, 4, 2);
330 format.color_space = COLORSPACE_sRGB;
331 format.gamma_curve = GAMMA_LINEAR;
333 FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 3, 2);
334 input->set_pixel_data(data);
335 tester.get_chain()->add_input(input);
337 Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
338 CHECK(effect->set_int("width", 4));
339 CHECK(effect->set_int("height", 2));
340 CHECK(effect->set_float("left", 0.0f));
341 CHECK(effect->set_float("top", 0.0f));
342 CHECK(effect->set_float("border_offset_left", 0.25f));
343 CHECK(effect->set_float("border_offset_right", -0.5f));
345 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
346 expect_equal(expected_data, out_data, 4, 2);