Ipl2DIB.cpp
上传用户:zhaobiao
上传日期:2013-10-27
资源大小:92k
文件大小:3k
- // Ipl2DIB.cpp: implementation of the CIpl2DIB class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- //#include "Ipl2ImgTest2.h"
- #include "Ipl2DIB.h"
- #define CImage CvvImage
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CIpl2DIB::CIpl2DIB(HWND hWnd)
- {
- m_HDC = ::GetDC(hWnd);
- m_hWnd = hWnd;
- m_Rect.SetRect(0,0,320,240);
- }
- CIpl2DIB::~CIpl2DIB()
- {
- ReleaseDC(m_hWnd,m_HDC);
- }
- void CIpl2DIB::SetRegin(CRect Rect)
- {
- m_Rect = Rect;
- }
- HBITMAP CIpl2DIB::IplImage2DIB(const IplImage *Image)
- {
- int bpp = Image->nChannels * 8;
- assert(Image->width >= 0 && Image->height >= 0 &&
- (bpp == 8 || bpp == 24 || bpp == 32));
- CvMat dst;
- void* dst_ptr = 0;
- HBITMAP hbmp = NULL;
- unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
- BITMAPINFO* bmi = (BITMAPINFO*)buffer;
- BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
-
- ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
- bmih->biSize = sizeof(BITMAPINFOHEADER);
- bmih->biWidth = Image->width;
- bmih->biHeight = Image->origin ? abs(Image->height) :
- -abs(Image->height);
- bmih->biPlanes = 1;
- bmih->biBitCount = bpp;
- bmih->biCompression = BI_RGB;
-
- if (bpp == 8) {
- RGBQUAD* palette = bmi->bmiColors;
- int i;
- for (i = 0; i < 256; i++) {
- palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue
- = (BYTE)i;
- palette[i].rgbReserved = 0;
- }
- }
-
- hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0, 0);
- //int pix_size = CV_ELEM_SIZE(CV_8UC1);
- cvInitMatHeader(&dst, Image->height, Image->width, CV_8UC1,
- dst_ptr, (Image->width * Image->nChannels + 3) & -4);
- cvConvertImage(Image, &dst, 0/*Image->origin ? CV_CVTIMG_FLIP : 0*/);
-
- return hbmp;
- }
- void CIpl2DIB::Display(const IplImage *Image,BOOL stretch)
- {
- HBITMAP hBmp;
- if(!stretch)
- {
- m_Rect.SetRect(m_Rect.left,m_Rect.top,Image->width,Image->height);
- }
- hBmp = IplImage2DIB(Image);
- HDC hMemDC = CreateCompatibleDC(m_HDC);
- HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDC, hBmp);
- //BitBlt(m_HDC, m_Rect.left, m_Rect.top, m_Rect.right,m_Rect.bottom, hMemDC, 0, 0, SRCCOPY);
- StretchBlt(m_HDC,m_Rect.left, m_Rect.top, m_Rect.right,m_Rect.bottom, hMemDC, 0, 0, Image->width,Image->height,SRCCOPY);
-
- SelectObject(hMemDC, hOldBmp);
- DeleteObject(hBmp);
- DeleteDC(hMemDC);
- }