2

I'm trying to understand these calculations for YUV420P to RGB conversion on an OpenGL fragment shader. On https://en.wikipedia.org/wiki/YUV there are lots of calculations but none of them look like the one below. Why take 0.0625 and 0.5 and 0.5 in the first part? And where did the second part come from?

yuv.r = texture(tex_y, TexCoord).r - 0.0625;
yuv.g = texture(tex_u, TexCoord).r - 0.5;
yuv.b = texture(tex_v, TexCoord).r - 0.5;

rgba.r = yuv.r + 1.596 * yuv.b
rgba.g = yuv.r - 0.813 * yuv.b - 0.391 * yuv.g;
rgba.b = yuv.r + 2.018 * yuv.g;

It may be an special color conversion for some specific YUV color scheme but I couldn't find anything on the internet.

PPP
  • 1,279
  • 1
  • 28
  • 71

1 Answers1

4

Why take [...] 0.5 and 0.5 in the first part?

U and V are stored in the green and blue color channel of the texture. The values in the color channels are stored in the range [0.0, 1.0]. For the computations the values have to be in mapped to the range [-0.5, 0.5]:

yuv.g = texture(tex_u, TexCoord).r - 0.5;
yuv.b = texture(tex_v, TexCoord).r - 0.5;

Subtracting 0.0625 from the red color channel is just an optimization. Thus, It does not have to be subtracted separately in each expression later.

The algorithm is the same as in How to convert RGB -> YUV -> RGB (both ways) or various books.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I just didn't get the `1.164*Y` on https://stackoverflow.com/a/17934865/6655884. There's none on mine (`yuv.r`) – PPP Jul 11 '20 at 09:59
  • @LucasZanella There are different formulas. See [Color Conversion](https://web.archive.org/web/20180423091842/http://www.equasys.de/colorconversion.html). There is not the one and only transformation the color space can be interpreted differently. – Rabbid76 Jul 11 '20 at 10:25