1 // Unit tests for AlphaMultiplicationEffect.
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(NULL, 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(NULL, 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(NULL, 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(NULL, 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 // 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,
159 float out_data[5 * 2];
161 EffectChainTester tester(NULL, 5, 2);
164 format.color_space = COLORSPACE_sRGB;
165 format.gamma_curve = GAMMA_LINEAR;
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);
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));
177 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
178 expect_equal(expected_data, out_data, 5, 2);
181 TEST(PaddingEffectTest, Crop) {
182 float data[2 * 2] = {
186 float expected_data[1 * 1] = {
189 float out_data[1 * 1];
191 EffectChainTester tester(NULL, 1, 1);
194 format.color_space = COLORSPACE_sRGB;
195 format.gamma_curve = GAMMA_LINEAR;
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);
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));
207 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
208 expect_equal(expected_data, out_data, 1, 1);
211 TEST(PaddingEffectTest, AlphaIsCorrectEvenWithNonLinearInputsAndOutputs) {
212 float data[2 * 1] = {
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,
222 float out_data[4 * 4];
224 EffectChainTester tester(NULL, 1, 4);
227 format.color_space = COLORSPACE_REC_601_625;
228 format.gamma_curve = GAMMA_REC_709;
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);
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));
240 RGBATuple border_color(1.0f, 1.0f, 1.0f, 0.5f);
241 CHECK(effect->set_vec4("border_color", (float *)&border_color));
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);