YUY2转RGB 的各种版本

79 篇文章 2 订阅
//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值