none
yuy2 转换为rgb 的bmp图像怎么转换 RRS feed

  • 常规讨论

  • 大家好:
        最近我在写一个用摄像头采集图片的程序,我用的vfw的库,我用的摄像头是无驱动的,但是采集出来的图片不是我想要的格式,后来在网上一查是yuy2格式的,我想怎样可以把它转换为rgb格式的图片,我在网上找了一些资料,但是没有转换成功,我把代码贴出来,大家帮我看看是什么问题,我觉的是我的参数传的不对

    void CFrameGrabber::update_buffer_size()
    {
     vfs=capGetVideoFormatSize(GetSafeHwnd());
       if(!vfs)
       {
        if(imageData)
         {HGLOBAL hg = GlobalHandle(imageData);
         GlobalUnlock(hg);
         GlobalFree(hg);}
         vfs = 0;
         imageData = NULL;
        return;  
       }
     
      DWORD lastBufferSize=0;
      if(imageData)
       lastBufferSize = vfs +
       IMAGEWIDTH(imageData)*IMAGEHEIGHT(imageData)*IMAGEBITS(imageData)/8;
      
      DWORD newBufferSize;
      
     
      BITMAPINFO *lpBmpInfo =(BITMAPINFO *)( new char[vfs]);
      
      ((LPBITMAPINFOHEADER)lpBmpInfo)->biSize= sizeof BITMAPINFOHEADER;
      
      if(!capGetVideoFormat(GetSafeHwnd(), lpBmpInfo, (WORD)vfs))
      {
       delete lpBmpInfo;
       
       if(imageData)
        {HGLOBAL hg = GlobalHandle(imageData);
        GlobalUnlock(hg);
        GlobalFree(hg);}
        vfs = 0;
        imageData = NULL;
       return;  
      }
     
      newBufferSize = vfs + IMAGEWIDTH(lpBmpInfo)*IMAGEHEIGHT(lpBmpInfo)*IMAGEBITS(lpBmpInfo)/8;
      
      SetWindowPos(NULL,0,0,IMAGEWIDTH(lpBmpInfo),IMAGEHEIGHT(lpBmpInfo),
         SWP_NOMOVE|SWP_NOZORDER);
     
      if(newBufferSize > lastBufferSize)
      {
       if(imageData)//reallocate
       { HGLOBAL hg = GlobalHandle(imageData);
        GlobalUnlock(hg);
        GlobalFree(hg);}
     
       imageData = GlobalLock(GlobalAlloc(GMEM_MOVEABLE, newBufferSize+256/*add 256 bytes, just in case...*/));
      }
     
      //memcpy(imageData, lpBmpInfo, vfs);
      //memcpy(lpbuffer, lpBmpInfo, vfs);
      YUY2_RGB((BYTE*)imageData,(BYTE*)lpBmpInfo,vfs);  
      delete lpBmpInfo;  
    }

    void CFrameGrabber::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 CFrameGrabber::clip255(LONG v)
    {
      if(v<0) v=0;
      else if(v>255) v=255;
      return (BYTE)v;
    }

     


    黄春雨
    2009年9月1日 6:05