- // Matrix to convert RGB to YCbCr. See e.g. Rec. 601.
- Matrix3d rgb_to_ycbcr;
- rgb_to_ycbcr(0,0) = coeff[0];
- rgb_to_ycbcr(0,1) = coeff[1];
- rgb_to_ycbcr(0,2) = coeff[2];
-
- float cb_fac = (224.0 / 219.0) / (coeff[0] + coeff[1] + 1.0f - coeff[2]);
- rgb_to_ycbcr(1,0) = -coeff[0] * cb_fac;
- rgb_to_ycbcr(1,1) = -coeff[1] * cb_fac;
- rgb_to_ycbcr(1,2) = (1.0f - coeff[2]) * cb_fac;
-
- float cr_fac = (224.0 / 219.0) / (1.0f - coeff[0] + coeff[1] + coeff[2]);
- rgb_to_ycbcr(2,0) = (1.0f - coeff[0]) * cr_fac;
- rgb_to_ycbcr(2,1) = -coeff[1] * cr_fac;
- rgb_to_ycbcr(2,2) = -coeff[2] * cr_fac;
-
- // Inverting the matrix gives us what we need to go from YCbCr back to RGB.
- Matrix3d ycbcr_to_rgb = rgb_to_ycbcr.inverse();
-
- std::string frag_shader;
-
- frag_shader = output_glsl_mat3("PREFIX(inv_ycbcr_matrix)", ycbcr_to_rgb);
-
- char buf[256];
- sprintf(buf, "const vec3 PREFIX(offset) = vec3(%.8f, %.8f, %.8f);\n",
- offset[0], offset[1], offset[2]);
- frag_shader += buf;
-
- sprintf(buf, "const vec3 PREFIX(scale) = vec3(%.8f, %.8f, %.8f);\n",
- scale[0], scale[1], scale[2]);
- frag_shader += buf;
-
- float chroma_offset_x = compute_chroma_offset(
- ycbcr_format.chroma_x_position, ycbcr_format.chroma_subsampling_x, widths[1]);
- float chroma_offset_y = compute_chroma_offset(
- ycbcr_format.chroma_y_position, ycbcr_format.chroma_subsampling_y, heights[1]);
- sprintf(buf, "const vec2 PREFIX(chroma_offset) = vec2(%.8f, %.8f);\n",
- chroma_offset_x, chroma_offset_y);
- frag_shader += buf;
-