公式如下:


//数字RGB与YCbCr颜色空间转换的精度  

 

  //在这两个公式中RGB和YCbCr各分量的值的范围均为0-255。 

 

  // RGB转换为YCbCr 

 

  // 这个公式来自:Genesis Microchip. gm6010/gm6015 Programming Guide[M]. California US: Genesis Microchip Company, 2002:85-90 

 
// |Y   |    |16  |                    |65.738   129.057  25.06 |   |R|
// |Cb| = |128| + (1/256)*|-37.945  -74.494  112.43| *|G|
// |Cr |    |128|                   |112.439  -94.154  -18.28|   |B| 

  // YCbCr转换为RGB 

 

  // 这个公式来自:Genesis Microchip. gm6015 Preliminary Data Sheet[M]. California US: Genesis Microchip Company, 2001:33-34 
 
//|R|                   |298.082  0               408.58 |   |Y   -16  |
//|G| = (1/256)*|298.082  -100.291 -208.12|* |Cb-128|
//|B|                   |298.082  516.411   0           |   |Cr -128|

加一点我的代码,为什么要加我的代码呢,不是因为写的代码好,只不过是我把上面公式中的矩阵/256算成小数了,这样子我们可以省点时间:


 


 


// RGB转换为YCbCr

for(i =0 ; i < dest->dwSize; )
{
 UCHAR r,g,b;
 r = sBuf[i+0];
 g = sBuf[i+1];
 b = sBuf[i+2];
 dBuf[i+0]= (unsigned char)(r * 0.256789 + g * 0.504129 + b * 0.097906)+ 16; 
 dBuf[i+1]= (unsigned char)(r *-0.148223 + g * -0.290992 + b * 0.439215)+ 128;
 dBuf[i+2]= (unsigned char)(r * 0.439215 + g * -0.367789 + b *-0.071426)+ 128; 
 i += 3;
}


 


// YCbCr转换为RGB

for(i =0 ; i < dest->dwSize; )
{
 UCHAR y,u,v;
 y = sBuf[i+0];
 u = sBuf[i+1];
 v = sBuf[i+2];
 dBuf[i+0]= (unsigned char)(1.164383*(y- 16) + 0 + 1.596027*(v - 128)); 
 dBuf[i+1]= (unsigned char)(1.164383*(y- 16) - 0.391762*(u - 128) - 0.812969*(v - 128)); 
 dBuf[i+2]= (unsigned char)(1.164383*(y- 16) + 2.017230*(u - 128) + 0 );
 i += 3;
}