]> git.sesse.net Git - movit/blob - flat_input_test.cpp
Make building the demo app optional if SDL_image and/or libpng12 are not found.
[movit] / flat_input_test.cpp
1 // Unit tests for FlatInput.
2
3 #include <stddef.h>
4
5 #include "effect_chain.h"
6 #include "flat_input.h"
7 #include "gtest/gtest.h"
8 #include "test_util.h"
9
10 TEST(FlatInput, SimpleGrayscale) {
11         const int size = 4;
12
13         float data[size] = {
14                 0.0,
15                 0.5,
16                 0.7,
17                 1.0,
18         };
19         float expected_data[4 * size] = {
20                 0.0, 0.0, 0.0, 1.0,
21                 0.5, 0.5, 0.5, 1.0,
22                 0.7, 0.7, 0.7, 1.0,
23                 1.0, 1.0, 1.0, 1.0,
24         };
25         float out_data[4 * size];
26
27         EffectChainTester tester(data, 1, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
28         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
29
30         expect_equal(expected_data, out_data, 4, size);
31 }
32
33 TEST(FlatInput, RGB) {
34         const int size = 5;
35
36         float data[3 * size] = {
37                 0.0, 0.0, 0.0,
38                 0.5, 0.0, 0.0,
39                 0.0, 0.5, 0.0,
40                 0.0, 0.0, 0.7,
41                 0.0, 0.3, 0.7,
42         };
43         float expected_data[4 * size] = {
44                 0.0, 0.0, 0.0, 1.0,
45                 0.5, 0.0, 0.0, 1.0,
46                 0.0, 0.5, 0.0, 1.0,
47                 0.0, 0.0, 0.7, 1.0,
48                 0.0, 0.3, 0.7, 1.0,
49         };
50         float out_data[4 * size];
51
52         EffectChainTester tester(data, 1, size, FORMAT_RGB, COLORSPACE_sRGB, GAMMA_LINEAR);
53         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
54
55         expect_equal(expected_data, out_data, 4, size);
56 }
57
58 TEST(FlatInput, RGBA) {
59         const int size = 5;
60
61         float data[4 * size] = {
62                 0.0, 0.0, 0.0, 1.0,
63                 0.5, 0.0, 0.0, 0.3,
64                 0.0, 0.5, 0.0, 0.7,
65                 0.0, 0.0, 0.7, 1.0,
66                 0.0, 0.3, 0.7, 0.2,
67         };
68         float expected_data[4 * size] = {
69                 0.0, 0.0, 0.0, 1.0,
70                 0.5, 0.0, 0.0, 0.3,
71                 0.0, 0.5, 0.0, 0.7,
72                 0.0, 0.0, 0.7, 1.0,
73                 0.0, 0.3, 0.7, 0.2,
74         };
75         float out_data[4 * size];
76
77         EffectChainTester tester(data, 1, size, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
78         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
79
80         expect_equal(expected_data, out_data, 4, size);
81 }
82
83 // Note: The sRGB conversion itself is tested in EffectChainTester,
84 // since it also wants to test the chain building itself.
85 // Here, we merely test that alpha is left alone; the test will usually
86 // run using the sRGB OpenGL extension, but might be run with a
87 // GammaExpansionEffect if the card/driver happens not to support that.
88 TEST(FlatInput, AlphaIsNotModifiedBySRGBConversion) {
89         const int size = 5;
90
91         unsigned char data[4 * size] = {
92                 0, 0, 0, 0,
93                 0, 0, 0, 63,
94                 0, 0, 0, 127,
95                 0, 0, 0, 191,
96                 0, 0, 0, 255,
97         };
98         float expected_data[4 * size] = {
99                 0, 0, 0, 0.0 / 255.0,
100                 0, 0, 0, 63.0 / 255.0,
101                 0, 0, 0, 127.0 / 255.0,
102                 0, 0, 0, 191.0 / 255.0,
103                 0, 0, 0, 255.0 / 255.0,
104         };
105         float out_data[4 * size];
106
107         EffectChainTester tester(NULL, 1, size);
108         tester.add_input(data, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_sRGB);
109         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
110
111         expect_equal(expected_data, out_data, 4, size);
112 }
113
114 TEST(FlatInput, BGR) {
115         const int size = 5;
116
117         float data[3 * size] = {
118                 0.0, 0.0, 0.0,
119                 0.5, 0.0, 0.0,
120                 0.0, 0.5, 0.0,
121                 0.0, 0.0, 0.7,
122                 0.0, 0.3, 0.7,
123         };
124         float expected_data[4 * size] = {
125                 0.0, 0.0, 0.0, 1.0,
126                 0.0, 0.0, 0.5, 1.0,
127                 0.0, 0.5, 0.0, 1.0,
128                 0.7, 0.0, 0.0, 1.0,
129                 0.7, 0.3, 0.0, 1.0,
130         };
131         float out_data[4 * size];
132
133         EffectChainTester tester(data, 1, size, FORMAT_BGR, COLORSPACE_sRGB, GAMMA_LINEAR);
134         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
135
136         expect_equal(expected_data, out_data, 4, size);
137 }
138
139 TEST(FlatInput, BGRA) {
140         const int size = 5;
141
142         float data[4 * size] = {
143                 0.0, 0.0, 0.0, 1.0,
144                 0.5, 0.0, 0.0, 0.3,
145                 0.0, 0.5, 0.0, 0.7,
146                 0.0, 0.0, 0.7, 1.0,
147                 0.0, 0.3, 0.7, 0.2,
148         };
149         float expected_data[4 * size] = {
150                 0.0, 0.0, 0.0, 1.0,
151                 0.0, 0.0, 0.5, 0.3,
152                 0.0, 0.5, 0.0, 0.7,
153                 0.7, 0.0, 0.0, 1.0,
154                 0.7, 0.3, 0.0, 0.2,
155         };
156         float out_data[4 * size];
157
158         EffectChainTester tester(data, 1, size, FORMAT_BGRA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
159         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
160
161         expect_equal(expected_data, out_data, 4, size);
162 }
163
164 TEST(FlatInput, Pitch) {
165         const int pitch = 3;
166         const int width = 2;
167         const int height = 4;
168
169         float data[pitch * height] = {
170                 0.0, 1.0, 999.0f,
171                 0.5, 0.5, 999.0f,
172                 0.7, 0.2, 999.0f,
173                 1.0, 0.6, 999.0f,
174         };
175         float expected_data[4 * width * height] = {
176                 0.0, 0.0, 0.0, 1.0,  1.0, 1.0, 1.0, 1.0,
177                 0.5, 0.5, 0.5, 1.0,  0.5, 0.5, 0.5, 1.0,
178                 0.7, 0.7, 0.7, 1.0,  0.2, 0.2, 0.2, 1.0,
179                 1.0, 1.0, 1.0, 1.0,  0.6, 0.6, 0.6, 1.0,
180         };
181         float out_data[4 * width * height];
182
183         EffectChainTester tester(NULL, width, height);
184
185         ImageFormat format;
186         format.color_space = COLORSPACE_sRGB;
187         format.gamma_curve = GAMMA_LINEAR;
188
189         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
190         input->set_pitch(pitch);
191         input->set_pixel_data(data);
192         tester.get_chain()->add_input(input);
193
194         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
195         expect_equal(expected_data, out_data, 4 * width, height);
196 }
197
198 TEST(FlatInput, UpdatedData) {
199         const int width = 2;
200         const int height = 4;
201
202         float data[width * height] = {
203                 0.0, 1.0,
204                 0.5, 0.5,
205                 0.7, 0.2,
206                 1.0, 0.6,
207         };
208         float out_data[width * height];
209
210         EffectChainTester tester(NULL, width, height);
211
212         ImageFormat format;
213         format.color_space = COLORSPACE_sRGB;
214         format.gamma_curve = GAMMA_LINEAR;
215
216         FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, width, height);
217         input->set_pixel_data(data);
218         tester.get_chain()->add_input(input);
219
220         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
221         expect_equal(data, out_data, width, height);
222
223         data[6] = 0.3;
224         input->invalidate_pixel_data();
225
226         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
227         expect_equal(data, out_data, width, height);
228 }