1 // Unit tests for AlphaMultiplicationEffect.
4 #include "flat_input.h"
5 #include "padding_effect.h"
6 #include "gtest/gtest.h"
8 TEST(PaddingEffectTest, SimpleCenter) {
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,
19 float out_data[4 * 4];
21 EffectChainTester tester(NULL, 4, 4);
24 format.color_space = COLORSPACE_sRGB;
25 format.gamma_curve = GAMMA_LINEAR;
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);
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));
37 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
38 expect_equal(expected_data, out_data, 4, 4);
41 TEST(PaddingEffectTest, WhiteBorderColor) {
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,
52 float out_data[4 * 4];
54 EffectChainTester tester(NULL, 4, 4);
57 format.color_space = COLORSPACE_sRGB;
58 format.gamma_curve = GAMMA_LINEAR;
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);
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));
70 RGBATriplet border_color(1.0f, 1.0f, 1.0f, 1.0f);
71 CHECK(effect->set_vec4("border_color", (float *)&border_color));
73 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
74 expect_equal(expected_data, out_data, 4, 4);
77 TEST(PaddingEffectTest, BorderColorIsInLinearGamma) {
79 0.2f, 0.4f, 0.6f, 0.8f,
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.
85 float out_data[4 * 2];
87 EffectChainTester tester(NULL, 1, 2);
90 format.color_space = COLORSPACE_sRGB;
91 format.gamma_curve = GAMMA_LINEAR;
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);
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));
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));
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);
110 TEST(PaddingEffectTest, DifferentXAndYOffset) {
111 float data[1 * 1] = {
114 float expected_data[3 * 3] = {
119 float out_data[3 * 3];
121 EffectChainTester tester(NULL, 3, 3);
124 format.color_space = COLORSPACE_sRGB;
125 format.gamma_curve = GAMMA_LINEAR;
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);
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));
137 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
138 expect_equal(expected_data, out_data, 3, 3);
141 TEST(PaddingEffectTest, NonIntegerOffset) {
142 float data[4 * 1] = {
143 0.25f, 0.50f, 0.75f, 1.0f,
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,
151 float out_data[5 * 2];
153 EffectChainTester tester(NULL, 5, 2);
156 format.color_space = COLORSPACE_sRGB;
157 format.gamma_curve = GAMMA_LINEAR;
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);
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));
169 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
170 expect_equal(expected_data, out_data, 5, 2);
173 TEST(PaddingEffectTest, Crop) {
174 float data[2 * 2] = {
178 float expected_data[1 * 1] = {
181 float out_data[1 * 1];
183 EffectChainTester tester(NULL, 1, 1);
186 format.color_space = COLORSPACE_sRGB;
187 format.gamma_curve = GAMMA_LINEAR;
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);
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));
199 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_PREMULTIPLIED);
200 expect_equal(expected_data, out_data, 1, 1);