1 // Unit tests for YCbCrInput.
2 // FIXME: This class really ought to support mipmaps.
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
9 #include "ycbcr_input.h"
11 TEST(YCbCrInput, Simple444) {
15 // Pure-color test inputs, calculated with the formulas in Rec. 601
17 unsigned char y[width * height] = {
20 unsigned char cb[width * height] = {
21 128, 128, 90, 54, 240,
23 unsigned char cr[width * height] = {
24 128, 128, 240, 34, 110,
26 float expected_data[4 * width * height] = {
33 float out_data[4 * width * height];
35 EffectChainTester tester(NULL, width, height);
38 format.color_space = COLORSPACE_sRGB;
39 format.gamma_curve = GAMMA_sRGB;
41 YCbCrFormat ycbcr_format;
42 ycbcr_format.luma_coefficients = YCBCR_REC_601;
43 ycbcr_format.full_range = false;
44 ycbcr_format.chroma_subsampling_x = 1;
45 ycbcr_format.chroma_subsampling_y = 1;
46 ycbcr_format.cb_x_position = 0.5f;
47 ycbcr_format.cb_y_position = 0.5f;
48 ycbcr_format.cr_x_position = 0.5f;
49 ycbcr_format.cr_y_position = 0.5f;
51 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
52 input->set_pixel_data(0, y);
53 input->set_pixel_data(1, cb);
54 input->set_pixel_data(2, cr);
55 tester.get_chain()->add_input(input);
57 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
59 // Y'CbCr isn't 100% accurate (the input values are rounded),
60 // so we need some leeway.
61 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
64 TEST(YCbCrInput, FullRangeRec601) {
68 // Pure-color test inputs, calculated with the formulas in Rec. 601
69 // section 2.5.4 but without the scaling factors applied
70 // (so both R, G, B, Y, Cb and R vary from 0 to 255).
71 unsigned char y[width * height] = {
74 unsigned char cb[width * height] = {
75 128, 128, 85, 44, 255,
77 unsigned char cr[width * height] = {
78 128, 128, 255, 21, 107,
80 float expected_data[4 * width * height] = {
87 float out_data[4 * width * height];
89 EffectChainTester tester(NULL, width, height);
92 format.color_space = COLORSPACE_sRGB;
93 format.gamma_curve = GAMMA_sRGB;
95 YCbCrFormat ycbcr_format;
96 ycbcr_format.luma_coefficients = YCBCR_REC_601;
97 ycbcr_format.full_range = true;
98 ycbcr_format.chroma_subsampling_x = 1;
99 ycbcr_format.chroma_subsampling_y = 1;
100 ycbcr_format.cb_x_position = 0.5f;
101 ycbcr_format.cb_y_position = 0.5f;
102 ycbcr_format.cr_x_position = 0.5f;
103 ycbcr_format.cr_y_position = 0.5f;
105 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
106 input->set_pixel_data(0, y);
107 input->set_pixel_data(1, cb);
108 input->set_pixel_data(2, cr);
109 tester.get_chain()->add_input(input);
111 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
113 // Y'CbCr isn't 100% accurate (the input values are rounded),
114 // so we need some leeway.
115 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
118 TEST(YCbCrInput, Rec709) {
120 const int height = 5;
122 // Pure-color test inputs, calculated with the formulas in Rec. 709
123 // page 19, items 3.4 and 3.5.
124 unsigned char y[width * height] = {
125 16, 235, 63, 173, 32,
127 unsigned char cb[width * height] = {
128 128, 128, 102, 42, 240,
130 unsigned char cr[width * height] = {
131 128, 128, 240, 26, 118,
133 float expected_data[4 * width * height] = {
140 float out_data[4 * width * height];
142 EffectChainTester tester(NULL, width, height);
145 format.color_space = COLORSPACE_sRGB;
146 format.gamma_curve = GAMMA_sRGB;
148 YCbCrFormat ycbcr_format;
149 ycbcr_format.luma_coefficients = YCBCR_REC_709;
150 ycbcr_format.full_range = false;
151 ycbcr_format.chroma_subsampling_x = 1;
152 ycbcr_format.chroma_subsampling_y = 1;
153 ycbcr_format.cb_x_position = 0.5f;
154 ycbcr_format.cb_y_position = 0.5f;
155 ycbcr_format.cr_x_position = 0.5f;
156 ycbcr_format.cr_y_position = 0.5f;
158 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
159 input->set_pixel_data(0, y);
160 input->set_pixel_data(1, cb);
161 input->set_pixel_data(2, cr);
162 tester.get_chain()->add_input(input);
164 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
166 // Y'CbCr isn't 100% accurate (the input values are rounded),
167 // so we need some leeway.
168 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
171 TEST(YCbCrInput, Subsampling420) {
173 const int height = 4;
175 unsigned char y[width * height] = {
181 unsigned char cb[(width/2) * (height/2)] = {
185 unsigned char cr[(width/2) * (height/2)] = {
190 // Note: This is only the blue channel. The chroma samples (with associated
191 // values for blue) are marked off in comments.
192 float expected_data[width * height] = {
193 0.000, 0.125, 0.375, 0.500,
195 0.125, 0.250, 0.500, 0.625,
197 0.375, 0.500, 0.750, 0.875,
199 0.500, 0.625, 0.875, 1.000,
201 float out_data[width * height];
203 EffectChainTester tester(NULL, width, height);
206 format.color_space = COLORSPACE_sRGB;
207 format.gamma_curve = GAMMA_sRGB;
209 YCbCrFormat ycbcr_format;
210 ycbcr_format.luma_coefficients = YCBCR_REC_601;
211 ycbcr_format.full_range = false;
212 ycbcr_format.chroma_subsampling_x = 2;
213 ycbcr_format.chroma_subsampling_y = 2;
214 ycbcr_format.cb_x_position = 0.5f;
215 ycbcr_format.cb_y_position = 0.5f;
216 ycbcr_format.cr_x_position = 0.5f;
217 ycbcr_format.cr_y_position = 0.5f;
219 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
220 input->set_pixel_data(0, y);
221 input->set_pixel_data(1, cb);
222 input->set_pixel_data(2, cr);
223 tester.get_chain()->add_input(input);
225 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
227 // Y'CbCr isn't 100% accurate (the input values are rounded),
228 // so we need some leeway.
229 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
232 TEST(YCbCrInput, Subsampling420WithNonCenteredSamples) {
234 const int height = 4;
236 unsigned char y[width * height] = {
242 unsigned char cb[(width/2) * (height/2)] = {
246 unsigned char cr[(width/2) * (height/2)] = {
251 // Note: This is only the blue channel. The chroma samples (with associated
252 // values for blue) are marked off in comments.
253 float expected_data[width * height] = {
254 0.000, 0.250, 0.500, 0.500,
256 0.125, 0.375, 0.625, 0.625,
258 0.375, 0.625, 0.875, 0.875,
260 0.500, 0.750, 1.000, 1.000,
262 float out_data[width * height];
264 EffectChainTester tester(NULL, width, height);
267 format.color_space = COLORSPACE_sRGB;
268 format.gamma_curve = GAMMA_sRGB;
270 YCbCrFormat ycbcr_format;
271 ycbcr_format.luma_coefficients = YCBCR_REC_601;
272 ycbcr_format.full_range = false;
273 ycbcr_format.chroma_subsampling_x = 2;
274 ycbcr_format.chroma_subsampling_y = 2;
275 ycbcr_format.cb_x_position = 0.0f;
276 ycbcr_format.cb_y_position = 0.5f;
277 ycbcr_format.cr_x_position = 0.0f;
278 ycbcr_format.cr_y_position = 0.5f;
280 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
281 input->set_pixel_data(0, y);
282 input->set_pixel_data(1, cb);
283 input->set_pixel_data(2, cr);
284 tester.get_chain()->add_input(input);
286 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
288 // Y'CbCr isn't 100% accurate (the input values are rounded),
289 // so we need some leeway.
290 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
293 // Yes, some 4:2:2 formats actually have this craziness.
294 TEST(YCbCrInput, DifferentCbAndCrPositioning) {
296 const int height = 4;
298 unsigned char y[width * height] = {
304 unsigned char cb[(width/2) * height] = {
310 unsigned char cr[(width/2) * height] = {
317 // Chroma samples in this csae are always co-sited with a luma sample;
318 // their associated color values and position are marked off in comments.
319 float expected_data_blue[width * height] = {
320 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */, 0.500,
321 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */, 1.000,
322 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
323 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
325 float expected_data_red[width * height] = {
326 0.000, 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */,
327 0.500, 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */,
328 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
329 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
331 float out_data[width * height];
333 EffectChainTester tester(NULL, width, height);
336 format.color_space = COLORSPACE_sRGB;
337 format.gamma_curve = GAMMA_sRGB;
339 YCbCrFormat ycbcr_format;
340 ycbcr_format.luma_coefficients = YCBCR_REC_601;
341 ycbcr_format.full_range = false;
342 ycbcr_format.chroma_subsampling_x = 2;
343 ycbcr_format.chroma_subsampling_y = 1;
344 ycbcr_format.cb_x_position = 0.0f;
345 ycbcr_format.cb_y_position = 0.5f;
346 ycbcr_format.cr_x_position = 1.0f;
347 ycbcr_format.cr_y_position = 0.5f;
349 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
350 input->set_pixel_data(0, y);
351 input->set_pixel_data(1, cb);
352 input->set_pixel_data(2, cr);
353 tester.get_chain()->add_input(input);
355 // Y'CbCr isn't 100% accurate (the input values are rounded),
356 // so we need some leeway.
357 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
358 expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
360 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
361 expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);