IplImage  *  BmpTOIpl( int  width, int  height,unsigned  char    * pBuffer)
{
    IPlImage 
*p_w_picpath =cvCreateImage(cvSize(width,height),8,1);
   p_w_picpath
->p_w_picpathData=pBuffer;
   
return p_w_picpath;
}





CDibPtr IplToDib(IplImage 
* p_w_picpath)
{
   CDibPtr m_ptrBitmaps;
   CSize ImageSize;
   ImageSize.cx
=p_w_picpath->width;
   ImageSize.cy
=p_w_picpath->height;
   m_ptrBitmaps.Create(ImageSize, 
8, CDib::TopDown, CDib::Monochrome);
  (unsigned 
char*) m_ptrBitmaps->GetPixels()=p_w_picpath->ImageData;
  
return m_ptrBitmaps;
}
IplImage *  hBitmap2Ipl(HBITMAP hBmp) 

BITMAP bmp; 

::GetObject(hBmp,
sizeof(BITMAP),&bmp); 

int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel/8 ; 
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U; 

IplImage
* img = cvCreateImageHeader( cvSize(bmp.bmWidth, bmp.bmHeight) 
, depth, nChannels ); 

img
->p_w_picpathData = 
(
char*)malloc(bmp.bmHeight*bmp.bmWidth*nChannels*sizeof(char)); 
memcpy(img
->p_w_picpathData,(char*)(bmp.bmBits),bmp.bmHeight*bmp.bmWidth*nChannels); 

return img; 
}
 


void  createDIB(IplImage *   & pict)
IplImage 
* Red=cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), 
IPL_DEPTH_8U, 
1 ); 
IplImage 
* Green=cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), 
IPL_DEPTH_8U, 
1 ); 
IplImage 
* Blue=cvCreateImage( cvSize(IMAGE_WIDTH,IMAGE_HEIGHT), 
IPL_DEPTH_8U, 
1 ); 
cvSetImageCOI( pict, 
3); 
cvCopy(pict,Red); 
cvSetImageCOI( pict, 
2); 
cvCopy(pict,Green); 
cvSetImageCOI(pict, 
1); 
cvCopy(pict,Blue); 
//Initialize the BMP display buffer 
bmi = (BITMAPINFO*)buffer; 
bmih 
= &(bmi->bmiHeader); 
memset( bmih, 
0sizeof(*bmih)); 
bmih
->biSize = sizeof(BITMAPINFOHEADER); 
bmih
->biWidth = IMAGE_WIDTH; 
bmih
->biHeight = IMAGE_HEIGHT; // -IMAGE_HEIGHT; 
bmih->biPlanes = 1
bmih
->biCompression = BI_RGB; 
bmih
->biBitCount = 24
palette 
= bmi->bmiColors; 
forint i = 0; i < 256; i++ )
palette[i].rgbBlue 
= palette[i].rgbGreen = palette[i].rgbRed = 
(BYTE)i; 
palette[i].rgbReserved 
= 0
}
 
cvReleaseImage(
&Red); 
cvReleaseImage(
&Green); 
cvReleaseImage(
&Blue); 
}