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