views:

34

answers:

3

I have Y,Cb,Cr values each of 8 bit, can you give me simple C function which converts it to R,G,B each of 8 bit.

Here is a prototype of it.

void convertYCbCrToRGB(unsigned char Y, unsigned char cg, unsigned char cb, unsigned char &r, unsigned &g, unsigned &b);

P.S
I am looking for correct conversion forumla only. Because I am finding it different everywhere. Even I am also expert in C,C++

A: 

Check out this page. It contains useful information on conversion formulae.

As an aside, you could return a unsigned int with the values for RGBA encoded from the most significant byte to least significant byte, i.e.

unsigned int YCbCrToRGBA(unsigned char Y, unsigned char Cb, unsigned char Cb) {
   unsigned char R = // conversion;
   unsigned char G = // conversion;
   unsigned char B = // conversion;

   return (R << 3) + (G << 2) + (B << 1) + 255;
}
Adrian Regan
I am looking for correct conversion forumla only. Because I am finding it different everywhere. Even I am also expert in C,C++. :-)
Sunny
The site I pointed you to is pretty definitive, but I know where you are coming from with regards to different perspectives on color space conversion.
Adrian Regan
+2  A: 

The problem comes that nearly everybody confuses YCbCr YUV and YPbPr. So literature you can find is often crappy. First you have to know if you really have YCbCr or if someone lies to you :-).

  • YUV coded data comes from analog sources (PAL video decoder, S-Video, ...)
  • YPbPr coded data also comes from analog sources but produces better color results as YUV (Component Video)
  • YCbCr coded data comes from digital sources (DVB, HDMI, ...)

YPbPr and YCbCr are related. Here are the right formulae:

http://www.equasys.de/colorconversion.html

jdehaan
Very Very Helpful link. Thanks for it.
Sunny
A: 

Here is my solution to my question.
This one is Full Range YCbCr to RGB conversion routine.

Color GetColorFromYCbCr(int y, int cb, int cr, int a)
{
 double Y = (double) y;
 double Cb = (double) cb;
 double Cr = (double) cr;

 int r = (int) (Y + 1.40200 * (Cr - 0x80));
 int g = (int) (Y - 0.34414 * (Cb - 0x80) - 0.71414 * (Cr - 0x80));
 int b = (int) (Y + 1.77200 * (Cb - 0x80));

 r = Max(0, Min(255, r));
 g = Max(0, Min(255, g));
 b = Max(0, Min(255, b));

 return Color.FromArgb(a, r, g, b);
}
Sunny