1 // Unit tests for YCbCrInput. Also tests the matrix functions in ycbcr.cpp directly.
9 #include "effect_chain.h"
10 #include "gtest/gtest.h"
11 #include "test_util.h"
13 #include "resource_pool.h"
14 #include "ycbcr_input.h"
18 TEST(YCbCrInputTest, Simple444) {
22 // Pure-color test inputs, calculated with the formulas in Rec. 601
24 unsigned char y[width * height] = {
27 unsigned char cb[width * height] = {
28 128, 128, 90, 54, 240,
30 unsigned char cr[width * height] = {
31 128, 128, 240, 34, 110,
33 float expected_data[4 * width * height] = {
40 float out_data[4 * width * height];
42 EffectChainTester tester(NULL, width, height);
45 format.color_space = COLORSPACE_sRGB;
46 format.gamma_curve = GAMMA_sRGB;
48 YCbCrFormat ycbcr_format;
49 ycbcr_format.luma_coefficients = YCBCR_REC_601;
50 ycbcr_format.full_range = false;
51 ycbcr_format.num_levels = 256;
52 ycbcr_format.chroma_subsampling_x = 1;
53 ycbcr_format.chroma_subsampling_y = 1;
54 ycbcr_format.cb_x_position = 0.5f;
55 ycbcr_format.cb_y_position = 0.5f;
56 ycbcr_format.cr_x_position = 0.5f;
57 ycbcr_format.cr_y_position = 0.5f;
59 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
60 input->set_pixel_data(0, y);
61 input->set_pixel_data(1, cb);
62 input->set_pixel_data(2, cr);
63 tester.get_chain()->add_input(input);
65 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
67 // Y'CbCr isn't 100% accurate (the input values are rounded),
68 // so we need some leeway.
69 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
72 TEST(YCbCrInputTest, FullRangeRec601) {
76 // Pure-color test inputs, calculated with the formulas in Rec. 601
77 // section 2.5.4 but without the scaling factors applied
78 // (so both R, G, B, Y, Cb and R vary from 0 to 255).
79 unsigned char y[width * height] = {
82 unsigned char cb[width * height] = {
83 128, 128, 85, 44, 255,
85 unsigned char cr[width * height] = {
86 128, 128, 255, 21, 107,
88 float expected_data[4 * width * height] = {
95 float out_data[4 * width * height];
97 EffectChainTester tester(NULL, width, height);
100 format.color_space = COLORSPACE_sRGB;
101 format.gamma_curve = GAMMA_sRGB;
103 YCbCrFormat ycbcr_format;
104 ycbcr_format.luma_coefficients = YCBCR_REC_601;
105 ycbcr_format.full_range = true;
106 ycbcr_format.num_levels = 256;
107 ycbcr_format.chroma_subsampling_x = 1;
108 ycbcr_format.chroma_subsampling_y = 1;
109 ycbcr_format.cb_x_position = 0.5f;
110 ycbcr_format.cb_y_position = 0.5f;
111 ycbcr_format.cr_x_position = 0.5f;
112 ycbcr_format.cr_y_position = 0.5f;
114 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
115 input->set_pixel_data(0, y);
116 input->set_pixel_data(1, cb);
117 input->set_pixel_data(2, cr);
118 tester.get_chain()->add_input(input);
120 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
122 // Y'CbCr isn't 100% accurate (the input values are rounded),
123 // so we need some leeway.
124 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
127 TEST(YCbCrInputTest, Rec709) {
129 const int height = 5;
131 // Pure-color test inputs, calculated with the formulas in Rec. 709
132 // page 19, items 3.4 and 3.5.
133 unsigned char y[width * height] = {
134 16, 235, 63, 173, 32,
136 unsigned char cb[width * height] = {
137 128, 128, 102, 42, 240,
139 unsigned char cr[width * height] = {
140 128, 128, 240, 26, 118,
142 float expected_data[4 * width * height] = {
149 float out_data[4 * width * height];
151 EffectChainTester tester(NULL, width, height);
154 format.color_space = COLORSPACE_sRGB;
155 format.gamma_curve = GAMMA_sRGB;
157 YCbCrFormat ycbcr_format;
158 ycbcr_format.luma_coefficients = YCBCR_REC_709;
159 ycbcr_format.full_range = false;
160 ycbcr_format.num_levels = 256;
161 ycbcr_format.chroma_subsampling_x = 1;
162 ycbcr_format.chroma_subsampling_y = 1;
163 ycbcr_format.cb_x_position = 0.5f;
164 ycbcr_format.cb_y_position = 0.5f;
165 ycbcr_format.cr_x_position = 0.5f;
166 ycbcr_format.cr_y_position = 0.5f;
168 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
169 input->set_pixel_data(0, y);
170 input->set_pixel_data(1, cb);
171 input->set_pixel_data(2, cr);
172 tester.get_chain()->add_input(input);
174 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
176 // Y'CbCr isn't 100% accurate (the input values are rounded),
177 // so we need some leeway.
178 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
181 TEST(YCbCrInputTest, Rec2020) {
183 const int height = 5;
185 // Pure-color test inputs, calculated with the formulas in Rec. 2020
186 // page 4, tables 4 and 5 (for conventional non-constant luminance).
187 // Note that we still use 8-bit inputs, even though Rec. 2020 is only
188 // defined for 10- and 12-bit.
189 unsigned char y[width * height] = {
190 16, 235, 74, 164, 29,
192 unsigned char cb[width * height] = {
193 128, 128, 97, 47, 240,
195 unsigned char cr[width * height] = {
196 128, 128, 240, 25, 119,
198 float expected_data[4 * width * height] = {
205 float out_data[4 * width * height];
207 EffectChainTester tester(NULL, width, height);
210 format.color_space = COLORSPACE_sRGB;
211 format.gamma_curve = GAMMA_sRGB;
213 YCbCrFormat ycbcr_format;
214 ycbcr_format.luma_coefficients = YCBCR_REC_2020;
215 ycbcr_format.full_range = false;
216 ycbcr_format.num_levels = 256;
217 ycbcr_format.chroma_subsampling_x = 1;
218 ycbcr_format.chroma_subsampling_y = 1;
219 ycbcr_format.cb_x_position = 0.5f;
220 ycbcr_format.cb_y_position = 0.5f;
221 ycbcr_format.cr_x_position = 0.5f;
222 ycbcr_format.cr_y_position = 0.5f;
224 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
225 input->set_pixel_data(0, y);
226 input->set_pixel_data(1, cb);
227 input->set_pixel_data(2, cr);
228 tester.get_chain()->add_input(input);
230 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
232 // Y'CbCr isn't 100% accurate (the input values are rounded),
233 // so we need some leeway.
234 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
237 TEST(YCbCrInputTest, Subsampling420) {
239 const int height = 4;
241 unsigned char y[width * height] = {
247 unsigned char cb[(width/2) * (height/2)] = {
251 unsigned char cr[(width/2) * (height/2)] = {
256 // Note: This is only the blue channel. The chroma samples (with associated
257 // values for blue) are marked off in comments.
258 float expected_data[width * height] = {
259 0.000, 0.125, 0.375, 0.500,
261 0.125, 0.250, 0.500, 0.625,
263 0.375, 0.500, 0.750, 0.875,
265 0.500, 0.625, 0.875, 1.000,
267 float out_data[width * height];
269 EffectChainTester tester(NULL, width, height);
272 format.color_space = COLORSPACE_sRGB;
273 format.gamma_curve = GAMMA_sRGB;
275 YCbCrFormat ycbcr_format;
276 ycbcr_format.luma_coefficients = YCBCR_REC_601;
277 ycbcr_format.full_range = false;
278 ycbcr_format.num_levels = 256;
279 ycbcr_format.chroma_subsampling_x = 2;
280 ycbcr_format.chroma_subsampling_y = 2;
281 ycbcr_format.cb_x_position = 0.5f;
282 ycbcr_format.cb_y_position = 0.5f;
283 ycbcr_format.cr_x_position = 0.5f;
284 ycbcr_format.cr_y_position = 0.5f;
286 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
287 input->set_pixel_data(0, y);
288 input->set_pixel_data(1, cb);
289 input->set_pixel_data(2, cr);
290 tester.get_chain()->add_input(input);
292 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
294 // Y'CbCr isn't 100% accurate (the input values are rounded),
295 // so we need some leeway.
296 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
299 TEST(YCbCrInputTest, Subsampling420WithNonCenteredSamples) {
301 const int height = 4;
303 unsigned char y[width * height] = {
309 unsigned char cb[(width/2) * (height/2)] = {
313 unsigned char cr[(width/2) * (height/2)] = {
318 // Note: This is only the blue channel. The chroma samples (with associated
319 // values for blue) are marked off in comments.
320 float expected_data[width * height] = {
321 0.000, 0.250, 0.500, 0.500,
323 0.125, 0.375, 0.625, 0.625,
325 0.375, 0.625, 0.875, 0.875,
327 0.500, 0.750, 1.000, 1.000,
329 float out_data[width * height];
331 EffectChainTester tester(NULL, width, height);
334 format.color_space = COLORSPACE_sRGB;
335 format.gamma_curve = GAMMA_sRGB;
337 YCbCrFormat ycbcr_format;
338 ycbcr_format.luma_coefficients = YCBCR_REC_601;
339 ycbcr_format.full_range = false;
340 ycbcr_format.num_levels = 256;
341 ycbcr_format.chroma_subsampling_x = 2;
342 ycbcr_format.chroma_subsampling_y = 2;
343 ycbcr_format.cb_x_position = 0.0f;
344 ycbcr_format.cb_y_position = 0.5f;
345 ycbcr_format.cr_x_position = 0.0f;
346 ycbcr_format.cr_y_position = 0.5f;
348 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
349 input->set_pixel_data(0, y);
350 input->set_pixel_data(1, cb);
351 input->set_pixel_data(2, cr);
352 tester.get_chain()->add_input(input);
354 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
356 // Y'CbCr isn't 100% accurate (the input values are rounded),
357 // so we need some leeway.
358 expect_equal(expected_data, out_data, width, height, 0.01, 0.0012);
361 // Yes, some 4:2:2 formats actually have this craziness.
362 TEST(YCbCrInputTest, DifferentCbAndCrPositioning) {
364 const int height = 4;
366 unsigned char y[width * height] = {
372 unsigned char cb[(width/2) * height] = {
378 unsigned char cr[(width/2) * height] = {
385 // Chroma samples in this csae are always co-sited with a luma sample;
386 // their associated color values and position are marked off in comments.
387 float expected_data_blue[width * height] = {
388 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */, 0.500,
389 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */, 1.000,
390 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
391 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */, 0.500,
393 float expected_data_red[width * height] = {
394 0.000, 0.000 /* 0.0 */, 0.250, 0.500 /* 0.5 */,
395 0.500, 0.500 /* 0.5 */, 0.750, 1.000 /* 1.0 */,
396 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
397 0.500, 0.500 /* 0.5 */, 0.500, 0.500 /* 0.5 */,
399 float out_data[width * height];
401 EffectChainTester tester(NULL, width, height);
404 format.color_space = COLORSPACE_sRGB;
405 format.gamma_curve = GAMMA_sRGB;
407 YCbCrFormat ycbcr_format;
408 ycbcr_format.luma_coefficients = YCBCR_REC_601;
409 ycbcr_format.full_range = false;
410 ycbcr_format.num_levels = 256;
411 ycbcr_format.chroma_subsampling_x = 2;
412 ycbcr_format.chroma_subsampling_y = 1;
413 ycbcr_format.cb_x_position = 0.0f;
414 ycbcr_format.cb_y_position = 0.5f;
415 ycbcr_format.cr_x_position = 1.0f;
416 ycbcr_format.cr_y_position = 0.5f;
418 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
419 input->set_pixel_data(0, y);
420 input->set_pixel_data(1, cb);
421 input->set_pixel_data(2, cr);
422 tester.get_chain()->add_input(input);
424 // Y'CbCr isn't 100% accurate (the input values are rounded),
425 // so we need some leeway.
426 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
427 expect_equal(expected_data_red, out_data, width, height, 0.02, 0.002);
429 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
430 expect_equal(expected_data_blue, out_data, width, height, 0.01, 0.001);
433 TEST(YCbCrInputTest, PBO) {
435 const int height = 5;
437 // Pure-color test inputs, calculated with the formulas in Rec. 601
439 unsigned char data[width * height * 3] = {
440 16, 235, 81, 145, 41,
441 128, 128, 90, 54, 240,
442 128, 128, 240, 34, 110,
444 float expected_data[4 * width * height] = {
451 float out_data[4 * width * height];
454 glGenBuffers(1, &pbo);
455 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
456 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * 3, data, GL_STREAM_DRAW);
457 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
459 EffectChainTester tester(NULL, width, height);
462 format.color_space = COLORSPACE_sRGB;
463 format.gamma_curve = GAMMA_sRGB;
465 YCbCrFormat ycbcr_format;
466 ycbcr_format.luma_coefficients = YCBCR_REC_601;
467 ycbcr_format.full_range = false;
468 ycbcr_format.num_levels = 256;
469 ycbcr_format.chroma_subsampling_x = 1;
470 ycbcr_format.chroma_subsampling_y = 1;
471 ycbcr_format.cb_x_position = 0.5f;
472 ycbcr_format.cb_y_position = 0.5f;
473 ycbcr_format.cr_x_position = 0.5f;
474 ycbcr_format.cr_y_position = 0.5f;
476 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
477 input->set_pixel_data(0, (unsigned char *)BUFFER_OFFSET(0), pbo);
478 input->set_pixel_data(1, (unsigned char *)BUFFER_OFFSET(width * height), pbo);
479 input->set_pixel_data(2, (unsigned char *)BUFFER_OFFSET(width * height * 2), pbo);
480 tester.get_chain()->add_input(input);
482 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
484 // Y'CbCr isn't 100% accurate (the input values are rounded),
485 // so we need some leeway.
486 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
488 glDeleteBuffers(1, &pbo);
491 TEST(YCbCrInputTest, CombinedCbAndCr) {
493 const int height = 5;
495 // Pure-color test inputs, calculated with the formulas in Rec. 601
497 unsigned char y[width * height] = {
498 16, 235, 81, 145, 41,
500 unsigned char cb_cr[width * height * 2] = {
507 float expected_data[4 * width * height] = {
514 float out_data[4 * width * height];
516 EffectChainTester tester(NULL, width, height);
519 format.color_space = COLORSPACE_sRGB;
520 format.gamma_curve = GAMMA_sRGB;
522 YCbCrFormat ycbcr_format;
523 ycbcr_format.luma_coefficients = YCBCR_REC_601;
524 ycbcr_format.full_range = false;
525 ycbcr_format.num_levels = 256;
526 ycbcr_format.chroma_subsampling_x = 1;
527 ycbcr_format.chroma_subsampling_y = 1;
528 ycbcr_format.cb_x_position = 0.5f;
529 ycbcr_format.cb_y_position = 0.5f;
530 ycbcr_format.cr_x_position = 0.5f;
531 ycbcr_format.cr_y_position = 0.5f;
533 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
534 input->set_pixel_data(0, y);
535 input->set_pixel_data(1, cb_cr);
536 tester.get_chain()->add_input(input);
538 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
540 // Y'CbCr isn't 100% accurate (the input values are rounded),
541 // so we need some leeway.
542 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
545 TEST(YCbCrInputTest, ExternalTexture) {
547 const int height = 5;
549 // Pure-color test inputs, calculated with the formulas in Rec. 601
551 unsigned char y[width * height] = {
552 16, 235, 81, 145, 41,
554 unsigned char cb[width * height] = {
555 128, 128, 90, 54, 240,
557 unsigned char cr[width * height] = {
558 128, 128, 240, 34, 110,
560 float expected_data[4 * width * height] = {
567 float out_data[4 * width * height];
569 EffectChainTester tester(NULL, width, height);
572 format.color_space = COLORSPACE_sRGB;
573 format.gamma_curve = GAMMA_sRGB;
575 YCbCrFormat ycbcr_format;
576 ycbcr_format.luma_coefficients = YCBCR_REC_601;
577 ycbcr_format.full_range = false;
578 ycbcr_format.num_levels = 256;
579 ycbcr_format.chroma_subsampling_x = 1;
580 ycbcr_format.chroma_subsampling_y = 1;
581 ycbcr_format.cb_x_position = 0.5f;
582 ycbcr_format.cb_y_position = 0.5f;
583 ycbcr_format.cr_x_position = 0.5f;
584 ycbcr_format.cr_y_position = 0.5f;
586 // Make a texture for the Cb data; keep the others as regular uploads.
588 GLuint cb_tex = pool.create_2d_texture(GL_R8, width, height);
590 glBindTexture(GL_TEXTURE_2D, cb_tex);
592 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
594 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
596 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, cb);
598 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
600 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
603 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
604 input->set_pixel_data(0, y);
605 input->set_texture_num(1, cb_tex);
606 input->set_pixel_data(2, cr);
607 tester.get_chain()->add_input(input);
609 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
611 pool.release_2d_texture(cb_tex);
613 // Y'CbCr isn't 100% accurate (the input values are rounded),
614 // so we need some leeway.
615 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
618 TEST(YCbCrTest, WikipediaRec601ForwardMatrix) {
619 YCbCrFormat ycbcr_format;
620 ycbcr_format.luma_coefficients = YCBCR_REC_601;
621 ycbcr_format.full_range = false;
622 ycbcr_format.num_levels = 256;
625 Eigen::Matrix3d ycbcr_to_rgb;
626 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
628 Eigen::Matrix3d rgb_to_ycbcr = ycbcr_to_rgb.inverse() * 255.0;
630 // Values from https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion.
631 EXPECT_NEAR( 65.481, rgb_to_ycbcr(0,0), 1e-3);
632 EXPECT_NEAR( 128.553, rgb_to_ycbcr(0,1), 1e-3);
633 EXPECT_NEAR( 24.966, rgb_to_ycbcr(0,2), 1e-3);
635 EXPECT_NEAR( -37.797, rgb_to_ycbcr(1,0), 1e-3);
636 EXPECT_NEAR( -74.203, rgb_to_ycbcr(1,1), 1e-3);
637 EXPECT_NEAR( 112.000, rgb_to_ycbcr(1,2), 1e-3);
639 EXPECT_NEAR( 112.000, rgb_to_ycbcr(2,0), 1e-3);
640 EXPECT_NEAR( -93.786, rgb_to_ycbcr(2,1), 1e-3);
641 EXPECT_NEAR( -18.214, rgb_to_ycbcr(2,2), 1e-3);
643 EXPECT_NEAR( 16.0, offset[0] * 255.0, 1e-3);
644 EXPECT_NEAR(128.0, offset[1] * 255.0, 1e-3);
645 EXPECT_NEAR(128.0, offset[2] * 255.0, 1e-3);
648 TEST(YCbCrTest, WikipediaJPEGMatrices) {
649 YCbCrFormat ycbcr_format;
650 ycbcr_format.luma_coefficients = YCBCR_REC_601;
651 ycbcr_format.full_range = true;
652 ycbcr_format.num_levels = 256;
655 Eigen::Matrix3d ycbcr_to_rgb;
656 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
658 // Values from https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
659 EXPECT_NEAR( 1.00000, ycbcr_to_rgb(0,0), 1e-5);
660 EXPECT_NEAR( 0.00000, ycbcr_to_rgb(0,1), 1e-5);
661 EXPECT_NEAR( 1.40200, ycbcr_to_rgb(0,2), 1e-5);
663 EXPECT_NEAR( 1.00000, ycbcr_to_rgb(1,0), 1e-5);
664 EXPECT_NEAR(-0.34414, ycbcr_to_rgb(1,1), 1e-5);
665 EXPECT_NEAR(-0.71414, ycbcr_to_rgb(1,2), 1e-5);
667 EXPECT_NEAR( 1.00000, ycbcr_to_rgb(2,0), 1e-5);
668 EXPECT_NEAR( 1.77200, ycbcr_to_rgb(2,1), 1e-5);
669 EXPECT_NEAR( 0.00000, ycbcr_to_rgb(2,2), 1e-5);
671 Eigen::Matrix3d rgb_to_ycbcr = ycbcr_to_rgb.inverse();
674 EXPECT_NEAR( 0.299000, rgb_to_ycbcr(0,0), 1e-6);
675 EXPECT_NEAR( 0.587000, rgb_to_ycbcr(0,1), 1e-6);
676 EXPECT_NEAR( 0.114000, rgb_to_ycbcr(0,2), 1e-6);
678 EXPECT_NEAR(-0.168736, rgb_to_ycbcr(1,0), 1e-6);
679 EXPECT_NEAR(-0.331264, rgb_to_ycbcr(1,1), 1e-6);
680 EXPECT_NEAR( 0.500000, rgb_to_ycbcr(1,2), 1e-6);
682 EXPECT_NEAR( 0.500000, rgb_to_ycbcr(2,0), 1e-6);
683 EXPECT_NEAR(-0.418688, rgb_to_ycbcr(2,1), 1e-6);
684 EXPECT_NEAR(-0.081312, rgb_to_ycbcr(2,2), 1e-6);
686 EXPECT_NEAR( 0.0, offset[0] * 255.0, 1e-3);
687 EXPECT_NEAR(128.0, offset[1] * 255.0, 1e-3);
688 EXPECT_NEAR(128.0, offset[2] * 255.0, 1e-3);
691 TEST(YCbCrTest, BlackmagicForwardMatrix) {
692 YCbCrFormat ycbcr_format;
693 ycbcr_format.luma_coefficients = YCBCR_REC_709;
694 ycbcr_format.full_range = false;
695 ycbcr_format.num_levels = 256;
698 Eigen::Matrix3d ycbcr_to_rgb;
699 compute_ycbcr_matrix(ycbcr_format, offset, &ycbcr_to_rgb);
701 Eigen::Matrix3d rgb_to_ycbcr = ycbcr_to_rgb.inverse();
703 // Values from DeckLink SDK documentation.
704 EXPECT_NEAR( 0.183, rgb_to_ycbcr(0,0), 1e-3);
705 EXPECT_NEAR( 0.614, rgb_to_ycbcr(0,1), 1e-3);
706 EXPECT_NEAR( 0.062, rgb_to_ycbcr(0,2), 1e-3);
708 EXPECT_NEAR(-0.101, rgb_to_ycbcr(1,0), 1e-3);
709 EXPECT_NEAR(-0.338, rgb_to_ycbcr(1,1), 1e-3);
710 EXPECT_NEAR( 0.439, rgb_to_ycbcr(1,2), 1e-3);
712 EXPECT_NEAR( 0.439, rgb_to_ycbcr(2,0), 1e-3);
713 EXPECT_NEAR(-0.399, rgb_to_ycbcr(2,1), 1e-3);
714 EXPECT_NEAR(-0.040, rgb_to_ycbcr(2,2), 1e-3);
716 EXPECT_NEAR( 16.0, offset[0] * 255.0, 1e-3);
717 EXPECT_NEAR(128.0, offset[1] * 255.0, 1e-3);
718 EXPECT_NEAR(128.0, offset[2] * 255.0, 1e-3);
721 TEST(YCbCrInputTest, NoData) {
723 const int height = 5;
725 float out_data[4 * width * height];
727 EffectChainTester tester(NULL, width, height);
730 format.color_space = COLORSPACE_sRGB;
731 format.gamma_curve = GAMMA_sRGB;
733 YCbCrFormat ycbcr_format;
734 ycbcr_format.luma_coefficients = YCBCR_REC_601;
735 ycbcr_format.full_range = false;
736 ycbcr_format.num_levels = 256;
737 ycbcr_format.chroma_subsampling_x = 1;
738 ycbcr_format.chroma_subsampling_y = 1;
739 ycbcr_format.cb_x_position = 0.5f;
740 ycbcr_format.cb_y_position = 0.5f;
741 ycbcr_format.cr_x_position = 0.5f;
742 ycbcr_format.cr_y_position = 0.5f;
744 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
745 tester.get_chain()->add_input(input);
747 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
749 // Don't care what the output was, just that it does not crash.