YUY2toRGB24 YUY2toYV12

#define MIN(x,y) (x)<(y) ? (x) : (y);

void YUY2toRGB24(BYTE *pDst, BYTE *pSrc)
{
 /*for(long lSrc=0,lDst=0; lSrc<320*240*2; lSrc=lSrc+4,lDst=lDst+6)
 {
  int C,D,E;
  C = *(pSrc+lSrc+0)-16;
  D = *(pSrc+lSrc+1)-128;
  E = *(pSrc+lSrc+3)-128;
  *(pDst+lDst+2) = clip(( 298 * C           + 409 * E + 128) >> 8);
  *(pDst+lDst+1) = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8);
  *(pDst+lDst+0) = clip(( 298 * C + 516 * D           + 128) >> 8);
  C = *(pSrc+lSrc+2)-16;
  *(pDst+lDst+5) = clip(( 298 * C           + 409 * E + 128) >> 8);
  *(pDst+lDst+4) = clip(( 298 * C - 100 * D - 208 * E + 128) >> 8);
  *(pDst+lDst+3) = clip(( 298 * C + 516 * D           + 128) >> 8);
 }*/

 for(long lSrc=0,lDst=0; lSrc<320*240*2; lSrc=lSrc+4,lDst=lDst+6)
 {
  int y,u,v;
  y = *(pSrc+lSrc+0);
  u = *(pSrc+lSrc+1);
  v = *(pSrc+lSrc+3);
  *(pDst+lDst+2) = MIN((9535 * (y - 16) + 13074 * (v - 128)) >> 13, 255);
  *(pDst+lDst+1) = MIN((9535 * (y - 16) - 6660 * (v - 128) - 3203 * (u - 128)) >> 13, 255);
  *(pDst+lDst+0) = MIN((9535 * (y - 16) + 16531 * (u - 128)) >> 13, 255);
  y = *(pSrc+lSrc+2);
  *(pDst+lDst+5) = MIN((9535 * (y - 16) + 13074 * (v - 128)) >> 13, 255);
  *(pDst+lDst+4) = MIN((9535 * (y - 16) - 6660 * (v - 128) - 3203 * (u - 128)) >> 13, 255);
  *(pDst+lDst+3) = MIN((9535 * (y - 16) + 16531 * (u - 128)) >> 13, 255);
 }
}

void YUY2toYV12(BYTE * pDst, BYTE * pSrc)
{
 for(long lSrc=0; lSrc<320*240*2; lSrc=lSrc+2)
 {
  *(pDst++) = *(pSrc+lSrc);
 }

 long lDistance = 160*120;
 for(int y=0; y<240; ++y)
 {
  if(y%2==0)
  {
   for(int x=0; x<320; x=x+4)
   {
     *(pDst++) = *(pSrc+x+1);
                 *(pDst+lDistance) = *(pSrc+x+3);
   }
  }
 }
}

void RGB24toYUY2(CString str, BYTE *pBuf)
{
 HANDLE hFile = NULL;
 BOOL bResult = FALSE;

 BITMAPFILEHEADER bfh;
 BITMAPINFOHEADER bih;

 memset(&bfh,0,sizeof(BITMAPFILEHEADER));
 memset(&bih,0,sizeof(BITMAPINFOHEADER));

   hFile = CreateFile(str,    // file to open
  GENERIC_READ,          // open for reading
  FILE_SHARE_READ,       // share for reading
  NULL,                  // default security
  OPEN_EXISTING,         // existing file only
  FILE_ATTRIBUTE_NORMAL, // normal file
  NULL);                 // no attr. template

 if (hFile == INVALID_HANDLE_VALUE)
 {
  printf("Could not open file (error %d)/n", GetLastError());
  return ;
 }

 DWORD dwBytes;
 bResult = ReadFile(hFile,&bfh,sizeof(BITMAPFILEHEADER),&dwBytes,NULL);
 if(!bResult)
 {
  printf("Could not read file (error %d)/n",GetLastError());
  return ;
 }

 bResult = ReadFile(hFile,&bih,sizeof(BITMAPINFOHEADER),&dwBytes,NULL);
 if(!bResult)
 {
  printf("Could not read file (error %d)/n",GetLastError());
  return ;
 }

 BYTE *pYUV = NULL;
 pYUV = new BYTE[320*240*3];
 if(pYUV==NULL)
  return;

 bResult = ReadFile(hFile,pYUV,320*240*3,&dwBytes,NULL);
 if(!bResult)
 {
  //printf("Could not read file (error %d)/n",GetLastError());
  DWORD dwResult = GetLastError();
  return ;
 }


 /*for(long lSrc=0,lDst=0; lSrc<320*240*3; lSrc=lSrc+6,lDst=lDst+4)
 {
  *(pBuf+lDst+0) = ( (  66 * (*(pYUV+lSrc+2)) + 129 * (*(pYUV+lSrc+1)) +  25 * (*(pYUV+lSrc+0)) + 128) >> 8) +  16;
  *(pBuf+lDst+1) = ( ( -38 * (*(pYUV+lSrc+2)) -  74 * (*(pYUV+lSrc+1)) + 112 * (*(pYUV+lSrc+0)) + 128) >> 8) + 128;
  *(pBuf+lDst+3) = ( ( 112 * (*(pYUV+lSrc+2)) -  94 * (*(pYUV+lSrc+1)) -  18 * (*(pYUV+lSrc+0)) + 128) >> 8) + 128;
  *(pBuf+lDst+2) = ( (  66 * (*(pYUV+lSrc+5)) + 129 * (*(pYUV+lSrc+4)) +  25 * (*(pYUV+lSrc+3)) + 128) >> 8) +  16;
 }*/

 long lDst = 0;
 for(int y=239; y>=0; --y)
 {
  for(int x=0; x<320/2; ++x)
  {
   *(pBuf+lDst+0) = ( (  66 * (*(pYUV+y*320*3+x*6+2)) + 129 * (*(pYUV+y*320*3+x*6+1)) +  25 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) +  16;
   *(pBuf+lDst+1) = ( ( -38 * (*(pYUV+y*320*3+x*6+2)) -  74 * (*(pYUV+y*320*3+x*6+1)) + 112 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) + 128;
   *(pBuf+lDst+3) = ( ( 112 * (*(pYUV+y*320*3+x*6+2)) -  94 * (*(pYUV+y*320*3+x*6+1)) -  18 * (*(pYUV+y*320*3+x*6+0)) + 128) >> 8) + 128;
   *(pBuf+lDst+2) = ( (  66 * (*(pYUV+y*320*3+x*6+5)) + 129 * (*(pYUV+y*320*3+x*6+4)) +  25 * (*(pYUV+y*320*3+x*6+3)) + 128) >> 8) +  16;
   lDst = lDst+4;
  }
 }

 delete [] pYUV;
 pYUV = NULL;

 CloseHandle(hFile);

 return ;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值