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