本文转自 http://blog.csdn.net/hardVB/archive/2007/10/18/1831810.aspx
/YUY2视频转化RGB格式 by hardy 2007-10-17
//http://www.fourcc.org/fccyvrgb.php
inline void YUY2_RGB(BYTE *YUY2buff,BYTE *RGBbuff,DWORD dwSize)
{
//B = 1.164(Y - 16) + 2.018(U - 128)
//G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
//R = 1.164(Y - 16) + 1.596(V - 128)
BYTE *orgRGBbuff = RGBbuff;
for(DWORD count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
float Y0 = *YUY2buff;
float U = *(++YUY2buff);
float Y1 = *(++YUY2buff);
float V = *(++YUY2buff);
++YUY2buff;
*(RGBbuff) = (BYTE)(Y0 + (1.370705 * (V-128)));
*(++RGBbuff) = (BYTE)(Y0 - (0.698001 * (V-128)) - (0.337633 * (U-128)));
*(++RGBbuff) = (BYTE)(Y0 + (1.732446 * (U-128)));
*(++RGBbuff) = (BYTE)(Y1 + (1.370705 * (V-128)));
*(++RGBbuff) = (BYTE)(Y1 - (0.698001 * (V-128)) - (0.337633 * (U-128)));
*(++RGBbuff) = (BYTE)(Y1 + (1.732446 * (U-128)));
++RGBbuff;
}
}
inline void YUY2_RGB2(BYTE *YUY2buff,BYTE *RGBbuff,DWORD dwSize)
{
//R = Y + 1.14V
//G = Y - 0.39U - 0.58V
//B = Y + 2.03U
// R = Y + 1.403V'
//G = Y - 0.344U' - 0.714V'
//B = Y + 1.770U'
BYTE *orgRGBbuff = RGBbuff;
for(DWORD count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
float Y0 = *YUY2buff;
float U = *(++YUY2buff);
float Y1 = *(++YUY2buff);
float V = *(++YUY2buff);
++YUY2buff;
float R,G,B;
R = (Y0 + 1.14f*V);
G=(Y0 - 0.39f*U-0.58f*V);
B=(Y0 +2.03f*U);
if(R<0) R =0;
if(R>255) R=255;
if(G<0) G =0;
if(G>255) G=255;
if(B<0) B =0;
if(B>255) B=255;
*(RGBbuff) = (BYTE)B;
*(++RGBbuff) = (BYTE)G;
*(++RGBbuff) = (BYTE)R;
R = (Y1 + 1.14f*V);
G=(Y1 - 0.39f*U-0.58f*V);
B=(Y1 +2.03f*U) ;
if(R<0) R =0;
if(R>255) R=255;
if(G<0) G =0;
if(G>255) G=255;
if(B<0) B =0;
if(B>255) B=255;
*(++RGBbuff) = (BYTE)B;
*(++RGBbuff) = (BYTE)G;
*(++RGBbuff) = (BYTE)R;
++RGBbuff;
}
}
inline void YUY2_RGB4(BYTE *YUY2buff,BYTE *RGBbuff,DWORD dwSize)
{
// R = Y + 1.403V'
//G = Y - 0.344U' - 0.714V'
//B = Y + 1.770U'
BYTE *orgRGBbuff = RGBbuff;
for(DWORD count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
float Y0 = *YUY2buff;
float U = *(++YUY2buff);
float Y1 = *(++YUY2buff);
float V = *(++YUY2buff);
++YUY2buff;
float R,G,B;
R = (Y0 + 1.403f*V);
G=(Y0 - 0.344f*U-0.714f*V);
B=(Y0 +1.77f*U);
if(R<0) R =0;
if(R>255) R=255;
if(G<0) G =0;
if(G>255) G=255;
if(B<0) B =0;
if(B>255) B=255;
*(RGBbuff) = (BYTE)R;
*(++RGBbuff) = (BYTE)G;
*(++RGBbuff) = (BYTE)B;
R = (Y1 + 1.403f*V);
G=(Y1 - 0.344f*U-0.714f*V);
B=(Y1 +1.77f*U);
if(R<0) R =0;
if(R>255) R=255;
if(G<0) G =0;
if(G>255) G=255;
if(B<0) B =0;
if(B>255) B=255;
*(++RGBbuff) = (BYTE)R;
*(++RGBbuff) = (BYTE)G;
*(++RGBbuff) = (BYTE)B;
++RGBbuff;
}
}
inline void YUY2_RGB3(BYTE *YUY2buff,BYTE *RGBbuff,DWORD dwSize)
{
//B = 1.164(Y - 16) + 2.018(U - 128)
//G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
//R = 1.164(Y - 16) + 1.596(V - 128)
BYTE *orgRGBbuff = RGBbuff;
for(DWORD count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
float Y0 = *YUY2buff;
float U = *(++YUY2buff);
float Y1 = *(++YUY2buff);
float V = *(++YUY2buff);
++YUY2buff;
*(RGBbuff) = (BYTE)(1.164f*(Y0 - 16)+ 2.018f*(U - 128));
*(++RGBbuff) = (BYTE)(1.164f*(Y0 - 16) - 0.813f*(V - 128) - 0.391f*(U - 128));
*(++RGBbuff) = (BYTE)(1.164f*(Y0 - 16) + 1.596f*(V - 128));
*(++RGBbuff) = (BYTE)(1.164f*(Y0 - 16)+ 2.018f*(U - 128));
*(++RGBbuff) = (BYTE)(1.164f*(Y0 - 16) - 0.813f*(V - 128) - 0.391f*(U - 128));
*(++RGBbuff) = (BYTE)(1.164f*(Y0 - 16) + 1.596f*(V - 128));
++RGBbuff;
}
}
// http://msdn2.microsoft.com/en-us/library/ms893078.aspx
void VC4000Graber::YUY2_RGB(BYTE *YUY2buff,BYTE *RGBbuff,DWORD dwSize)
{
//
//C = Y - 16
//D = U - 128
//E = V - 128
//R = clip(( 298 * C + 409 * E + 128) >> 8)
//G = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8)
//B = clip(( 298 * C + 516 * D + 128) >> 8)
BYTE *orgRGBbuff = RGBbuff;
for(DWORD count=0;count<dwSize;count+=4)
{
//Y0 U0 Y1 V0
BYTE Y0 = *YUY2buff;
BYTE U = *(++YUY2buff);
BYTE Y1 = *(++YUY2buff);
BYTE V = *(++YUY2buff);
++YUY2buff;
LONG Y,C,D,E;
BYTE R,G,B;
Y = Y0;
C = Y-16;
D = U-128;
E = V-128;
R = clip255(( 298 * C + 409 * E + 128) >> 8);
G = clip255(( 298 * C - 100 * D - 208 * E + 128) >> 8);
B = clip255(( 298 * C + 516 * D + 128) >> 8);
*(RGBbuff) = B;
*(++RGBbuff) = G;
*(++RGBbuff) = R;
Y = Y1;
C = Y-16;
D = U-128;
E = V-128;
R = clip255(( 298 * C + 409 * E + 128) >> 8);
G = clip255(( 298 * C - 100 * D - 208 * E + 128) >> 8);
B = clip255(( 298 * C + 516 * D + 128) >> 8);
*(++RGBbuff) = B;
*(++RGBbuff) = G;
*(++RGBbuff) = R;
++RGBbuff;
}
}
BYTE VC4000Graber::clip255(LONG v)
{
if(v<0) v=0;
else if(v>255) v=255;
return (BYTE)v;
}