OpenCV转CBitmap

BOOL IplImage2Bmp(IplImage *pImage,CBitmap *bitmap)
{
 if( pImage && pImage->depth == IPL_DEPTH_8U )
    {
        uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
        BITMAPINFO* bmi = (BITMAPINFO*)buffer;
        int bmp_w = pImage->width, bmp_h = pImage->height;
  
  int width=bmp_w;
  int height=bmp_h;
  int bpp=pImage ? (pImage->depth & 255)*pImage->nChannels : 0;
  int origin=pImage->origin;
  HBITMAP hBitmap;
  char* dst_data = 0;
  
  assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
  
  BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
  
  memset( bmih, 0, sizeof(*bmih));
  bmih->biSize = sizeof(BITMAPINFOHEADER);
  bmih->biWidth = width;
  bmih->biHeight = origin ? abs(height) : -abs(height);
  bmih->biPlanes = 1;
  bmih->biBitCount = (unsigned short)bpp;
  bmih->biCompression = BI_RGB;
  
  if( bpp == 8 )
  {
   RGBQUAD* palette = bmi->bmiColors;
   int i;
   for( i = 0; i < 256; i++ )
   {
    palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
    palette[i].rgbReserved = 0;
   }
  }
  if(bitmap->m_hObject!=NULL)
  {
   bitmap->Detach();
  }
  hBitmap=CreateDIBitmap(CClientDC(NULL),bmih,CBM_INIT,pImage->imageData,bmi,DIB_RGB_COLORS);
  bitmap->Attach(hBitmap);
  return TRUE;
    }
 return FALSE;
}
posted on 2012-03-23 11:35  carekee  阅读(983)  评论(0编辑  收藏  举报