OpencvのイメージフォーマットIplImageとCBitmapの変換


 BOOL IplImage2Bmp(HDC dc,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;
 
        //FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), pImage->origin );
  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(dc,bmih,CBM_INIT,pImage->imageData,bmi,DIB_RGB_COLORS);
  bitmap->Attach(hBitmap);
  return TRUE;
    }
 return FALSE;
 
}