1 // Unit tests for YCbCrInput.
2 // FIXME: This class really ought to support mipmaps.
5 #include "gtest/gtest.h"
6 #include "ycbcr_input.h"
8 TEST(YCbCrInput, Simple444) {
12 // Pure-color test inputs, calculated with the formulas in Rec. 601
14 unsigned char y[width * height] = {
17 unsigned char cb[width * height] = {
18 128, 128, 90, 54, 240,
20 unsigned char cr[width * height] = {
21 128, 128, 240, 34, 110,
23 float expected_data[4 * width * height] = {
30 float out_data[4 * width * height];
32 EffectChainTester tester(NULL, width, height);
35 format.color_space = COLORSPACE_sRGB;
36 format.gamma_curve = GAMMA_sRGB;
38 YCbCrFormat ycbcr_format;
39 ycbcr_format.luma_coefficients = YCBCR_REC_601;
40 ycbcr_format.full_range = false;
41 ycbcr_format.chroma_subsampling_x = 1;
42 ycbcr_format.chroma_subsampling_y = 1;
43 ycbcr_format.chroma_x_position = 0.5f;
44 ycbcr_format.chroma_y_position = 0.5f;
46 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
47 input->set_pixel_data(0, y);
48 input->set_pixel_data(1, cb);
49 input->set_pixel_data(2, cr);
50 tester.get_chain()->add_input(input);
52 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
54 // Y'CbCr isn't 100% accurate (the input values are rounded),
55 // so we need some leeway.
56 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
59 TEST(YCbCrInput, FullRangeRec601) {
63 // Pure-color test inputs, calculated with the formulas in Rec. 601
64 // section 2.5.4 but without the scaling factors applied
65 // (so both R, G, B, Y, Cb and R vary from 0 to 255).
66 unsigned char y[width * height] = {
69 unsigned char cb[width * height] = {
70 128, 128, 85, 44, 255,
72 unsigned char cr[width * height] = {
73 128, 128, 255, 21, 107,
75 float expected_data[4 * width * height] = {
82 float out_data[4 * width * height];
84 EffectChainTester tester(NULL, width, height);
87 format.color_space = COLORSPACE_sRGB;
88 format.gamma_curve = GAMMA_sRGB;
90 YCbCrFormat ycbcr_format;
91 ycbcr_format.luma_coefficients = YCBCR_REC_601;
92 ycbcr_format.full_range = true;
93 ycbcr_format.chroma_subsampling_x = 1;
94 ycbcr_format.chroma_subsampling_y = 1;
95 ycbcr_format.chroma_x_position = 0.5f;
96 ycbcr_format.chroma_y_position = 0.5f;
98 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
99 input->set_pixel_data(0, y);
100 input->set_pixel_data(1, cb);
101 input->set_pixel_data(2, cr);
102 tester.get_chain()->add_input(input);
104 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_sRGB);
106 // Y'CbCr isn't 100% accurate (the input values are rounded),
107 // so we need some leeway.
108 expect_equal(expected_data, out_data, 4 * width, height, 0.025, 0.002);
111 TEST(YCbCrInput, Subsampling420) {
113 const int height = 4;
115 unsigned char y[width * height] = {
121 unsigned char cb[(width/2) * (height/2)] = {
125 unsigned char cr[(width/2) * (height/2)] = {
130 // Note: This is only the blue channel. The chroma samples (with associated
131 // values for blue) are marked off in comments.
132 float expected_data[width * height] = {
133 0.000, 0.125, 0.375, 0.500,
135 0.125, 0.250, 0.500, 0.625,
137 0.375, 0.500, 0.750, 0.875,
139 0.500, 0.625, 0.875, 1.000,
141 float out_data[width * height];
143 EffectChainTester tester(NULL, width, height);
146 format.color_space = COLORSPACE_sRGB;
147 format.gamma_curve = GAMMA_sRGB;
149 YCbCrFormat ycbcr_format;
150 ycbcr_format.luma_coefficients = YCBCR_REC_601;
151 ycbcr_format.full_range = false;
152 ycbcr_format.chroma_subsampling_x = 2;
153 ycbcr_format.chroma_subsampling_y = 2;
154 ycbcr_format.chroma_x_position = 0.5f;
155 ycbcr_format.chroma_y_position = 0.5f;
157 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
158 input->set_pixel_data(0, y);
159 input->set_pixel_data(1, cb);
160 input->set_pixel_data(2, cr);
161 tester.get_chain()->add_input(input);
163 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
165 // Y'CbCr isn't 100% accurate (the input values are rounded),
166 // so we need some leeway.
167 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);
170 TEST(YCbCrInput, Subsampling420WithNonCenteredSamples) {
172 const int height = 4;
174 unsigned char y[width * height] = {
180 unsigned char cb[(width/2) * (height/2)] = {
184 unsigned char cr[(width/2) * (height/2)] = {
189 // Note: This is only the blue channel. The chroma samples (with associated
190 // values for blue) are marked off in comments.
191 float expected_data[width * height] = {
192 0.000, 0.250, 0.500, 0.500,
194 0.125, 0.375, 0.625, 0.625,
196 0.375, 0.625, 0.875, 0.875,
198 0.500, 0.750, 1.000, 1.000,
200 float out_data[width * height];
202 EffectChainTester tester(NULL, width, height);
205 format.color_space = COLORSPACE_sRGB;
206 format.gamma_curve = GAMMA_sRGB;
208 YCbCrFormat ycbcr_format;
209 ycbcr_format.luma_coefficients = YCBCR_REC_601;
210 ycbcr_format.full_range = false;
211 ycbcr_format.chroma_subsampling_x = 2;
212 ycbcr_format.chroma_subsampling_y = 2;
213 ycbcr_format.chroma_x_position = 0.0f;
214 ycbcr_format.chroma_y_position = 0.5f;
216 YCbCrInput *input = new YCbCrInput(format, ycbcr_format, width, height);
217 input->set_pixel_data(0, y);
218 input->set_pixel_data(1, cb);
219 input->set_pixel_data(2, cr);
220 tester.get_chain()->add_input(input);
222 tester.run(out_data, GL_BLUE, COLORSPACE_sRGB, GAMMA_sRGB);
224 // Y'CbCr isn't 100% accurate (the input values are rounded),
225 // so we need some leeway.
226 expect_equal(expected_data, out_data, width, height, 0.01, 0.001);