42 #include <visp/vpConfig.h>
44 #if ( defined(VISP_HAVE_GDI) )
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
49 #include <visp/vpGDIRenderer.h>
54 vpGDIRenderer::vpGDIRenderer()
57 if( GetDeviceCaps(GetDC(NULL),BITSPIXEL) != 32 )
59 "Only works in 32bits mode!");
61 InitializeCriticalSection(&CriticalSection);
111 vpGDIRenderer::~vpGDIRenderer()
114 DeleteCriticalSection(&CriticalSection);
127 bool vpGDIRenderer::init(HWND hWindow,
unsigned int width,
unsigned int height)
135 hFont = CreateFont(18, 0, 0, 0, FW_NORMAL,
false,
false,
false,
136 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
137 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
138 DEFAULT_PITCH | FF_DONTCARE, NULL);
161 void vpGDIRenderer::setImgROI(
const vpImage<vpRGBa>& I,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height )
164 convertROI(I, iP, width, height);
192 convertROI(I, iP, width, height);
201 bool vpGDIRenderer::render()
205 HDC hDCScreen = BeginPaint(hWnd, &ps);
208 HDC hDCMem = CreateCompatibleDC(hDCScreen);
211 EnterCriticalSection(&CriticalSection);
212 SelectObject(hDCMem, bmp);
216 BitBlt(hDCScreen, 0, 0,
217 static_cast<int>( nbCols ),
218 static_cast<int>( nbRows ),
219 hDCMem, 0, 0, SRCCOPY);
221 LeaveCriticalSection(&CriticalSection);
223 DeleteObject(hDCMem);
243 bool needPad = ((w%2) == 0) ?
false :
true;
244 unsigned int newW = w;
247 newW = (needPad) ? (w+1) : w;
250 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
256 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
282 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
292 updateBitmap(hBmp,imBuffer, newW, h);
304 void vpGDIRenderer::convertROI(
const vpImage<vpRGBa> &I,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height)
307 unsigned int w = width;
308 unsigned int h = height;
312 bool needPad = ((w%2) == 0) ?
false :
true;
313 unsigned int newW = w;
316 newW = (needPad) ? (w+1) : w;
319 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
323 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
330 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
344 imBuffer[i+0] = (bitmap+k)->B;
345 imBuffer[i+1] = (bitmap+k)->G;
346 imBuffer[i+2] = (bitmap+k)->R;
347 imBuffer[i+3] = (bitmap+k)->A;
354 bitmap = bitmap+iwidth;
363 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
365 imBuffer[i+0] = (bitmap+k)->B;
366 imBuffer[i+1] = (bitmap+k)->G;
367 imBuffer[i+2] = (bitmap+k)->R;
368 imBuffer[i+3] = (bitmap+k)->A;
373 bitmap = bitmap+iwidth;
380 updateBitmapROI(imBuffer,iP, newW, h);
400 bool needPad = ((w%2) == 0) ?
false :
true;
401 unsigned int newW = w;
404 newW = (needPad) ? (w+1) : w;
407 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
413 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
427 imBuffer[i+0] = I.
bitmap[k];
428 imBuffer[i+1] = I.
bitmap[k];
429 imBuffer[i+2] = I.
bitmap[k];
430 imBuffer[i+3] = I.
bitmap[k];
438 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
440 imBuffer[i+0] = I.
bitmap[k];
441 imBuffer[i+1] = I.
bitmap[k];
442 imBuffer[i+2] = I.
bitmap[k];
443 imBuffer[i+3] = I.
bitmap[k];
448 updateBitmap(hBmp,imBuffer, newW, h);
464 unsigned int w = width;
465 unsigned int h = height;
469 bool needPad = ((w%2) == 0) ?
false :
true;
470 unsigned int newW = w;
473 newW = (needPad) ? (w+1) : w;
476 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
478 unsigned char* bitmap = I.
bitmap;
480 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
487 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
501 imBuffer[i+0] = *(bitmap+k);
502 imBuffer[i+1] = *(bitmap+k);
503 imBuffer[i+2] = *(bitmap+k);
504 imBuffer[i+3] = *(bitmap+k);
511 bitmap = bitmap+iwidth;
520 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
522 imBuffer[i+0] = *(bitmap+k);
523 imBuffer[i+1] = *(bitmap+k);
524 imBuffer[i+2] = *(bitmap+k);
525 imBuffer[i+3] = *(bitmap+k);
530 bitmap = bitmap+iwidth;
537 updateBitmapROI(imBuffer,iP, newW, h);
553 bool vpGDIRenderer::updateBitmap(HBITMAP& hBmp,
unsigned char * imBuffer,
554 unsigned int w,
unsigned int h)
558 EnterCriticalSection(&CriticalSection);
561 if( (nbCols==w) && (nbRows==h) )
564 SetBitmapBits(hBmp, w * h * 4, imBuffer);
574 if( (hBmp = CreateBitmap(static_cast<int>(w),static_cast<int>(h),
575 1,32,(
void*)imBuffer)) == NULL)
579 LeaveCriticalSection(&CriticalSection);
594 bool vpGDIRenderer::updateBitmapROI(
unsigned char * imBuffer,
const vpImagePoint &iP,
595 unsigned int w,
unsigned int h)
597 int w_ =
static_cast<int>(w);
598 int h_ =
static_cast<int>(h);
599 HBITMAP htmp = CreateBitmap(w_,h_,1,32,(
void*)imBuffer);
602 HDC hDCScreen = GetDC(hWnd);
603 HDC hDCMem = CreateCompatibleDC(hDCScreen);
604 HDC hDCMem2 = CreateCompatibleDC(hDCScreen);
607 EnterCriticalSection(&CriticalSection);
608 SelectObject(hDCMem, bmp);
609 SelectObject(hDCMem2, htmp);
611 BitBlt(hDCMem,(
int)iP.
get_u(),(int)iP.
get_v(),w_,h_, hDCMem2, 0, 0,SRCCOPY);
612 LeaveCriticalSection(&CriticalSection);
615 ReleaseDC(hWnd, hDCScreen);
631 HDC hDCScreen = GetDC(hWnd);
632 HDC hDCMem = CreateCompatibleDC(hDCScreen);
635 EnterCriticalSection(&CriticalSection);
636 SelectObject(hDCMem, bmp);
642 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
649 LeaveCriticalSection(&CriticalSection);
652 ReleaseDC(hWnd, hDCScreen);
665 unsigned int thickness,
int style)
667 HDC hDCScreen= NULL, hDCMem = NULL;
672 hDCScreen = GetDC(hWnd);
673 if(!hDCScreen)
continue;
674 hDCMem = CreateCompatibleDC(hDCScreen);
676 ReleaseDC(hWnd, hDCScreen);
682 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
684 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
685 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
689 ReleaseDC(hWnd, hDCScreen);
692 if(!SetBkMode(hDCMem, TRANSPARENT)){
695 ReleaseDC(hWnd, hDCScreen);
700 EnterCriticalSection(&CriticalSection);
702 if(!SelectObject(hDCMem, bmp)){
703 LeaveCriticalSection(&CriticalSection);
706 ReleaseDC(hWnd, hDCScreen);
711 if(!SelectObject(hDCMem, hPen)){
712 LeaveCriticalSection(&CriticalSection);
715 ReleaseDC(hWnd, hDCScreen);
723 hDCScreen = GetDC(hWnd);
724 hDCMem = CreateCompatibleDC(hDCScreen);
728 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
730 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
731 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
733 SetBkMode(hDCMem, TRANSPARENT);
736 EnterCriticalSection(&CriticalSection);
737 SelectObject(hDCMem, bmp);
740 SelectObject(hDCMem, hPen);
756 LeaveCriticalSection(&CriticalSection);
760 ReleaseDC(hWnd, hDCScreen);
773 void vpGDIRenderer::drawRect(
const vpImagePoint &topLeft,
774 unsigned int width,
unsigned int height,
775 const vpColor &color,
bool fill,
776 unsigned int thickness)
778 if (thickness == 0) thickness = 1;
780 HDC hDCScreen = GetDC(hWnd);
781 HDC hDCMem = CreateCompatibleDC(hDCScreen);
785 COLORREF gdicolor = RGB(0,0,0);
788 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
790 gdicolor = RGB(color.
R, color.
G, color.
B);
791 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
797 lBrush.lbStyle = BS_SOLID;
799 lBrush.lbColor = colors[color.
id];
801 lBrush.lbColor = gdicolor;
804 else lBrush.lbStyle = BS_HOLLOW;
805 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
808 EnterCriticalSection(&CriticalSection);
809 SelectObject(hDCMem, bmp);
812 SelectObject(hDCMem, hbrush);
814 SelectObject(hDCMem, hPen);
824 LeaveCriticalSection(&CriticalSection);
826 DeleteObject(hbrush);
829 ReleaseDC(hWnd, hDCScreen);
837 void vpGDIRenderer::clear(
const vpColor &color)
842 drawRect(ip, nbCols, nbRows, color,
true, 0);
854 void vpGDIRenderer::drawCircle(
const vpImagePoint ¢er,
unsigned int radius,
855 const vpColor &color,
bool fill,
unsigned int thickness)
859 HDC hDCScreen = GetDC(hWnd);
860 HDC hDCMem = CreateCompatibleDC(hDCScreen);
865 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
867 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
868 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
873 lBrush.lbStyle = BS_HOLLOW;
874 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
877 int radius_ =
static_cast<int>(radius);
884 EnterCriticalSection(&CriticalSection);
885 SelectObject(hDCMem, bmp);
888 SelectObject(hDCMem, hbrush);
890 SelectObject(hDCMem, hPen);
894 Ellipse(hDCMem, x1, y1, x2, y2);
904 Ellipse(hDCMem, x1, y1, x2, y2);
911 LeaveCriticalSection(&CriticalSection);
913 DeleteObject(hbrush);
916 ReleaseDC(hWnd, hDCScreen);
926 void vpGDIRenderer::drawText(
const vpImagePoint &ip,
const char * text,
930 HDC hDCScreen = GetDC(hWnd);
931 HDC hDCMem = CreateCompatibleDC(hDCScreen);
934 EnterCriticalSection(&CriticalSection);
935 SelectObject(hDCMem, bmp);
938 SelectObject(hDCMem, hFont);
942 SetTextColor(hDCMem, colors[color.
id]);
944 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
945 SetTextColor(hDCMem, gdicolor);
949 SetBkMode(hDCMem, TRANSPARENT);
952 int length = (int) strlen(text);
955 GetTextExtentPoint32(hDCMem, text, length, &size);
963 LeaveCriticalSection(&CriticalSection);
966 ReleaseDC(hWnd, hDCScreen);
978 void vpGDIRenderer::drawCross(
const vpImagePoint &ip,
unsigned int size,
979 const vpColor &color,
unsigned int thickness)
981 int half_size =
static_cast<int>( size/2 );
988 HDC hDCScreen = GetDC(hWnd);
989 HDC hDCMem = CreateCompatibleDC(hDCScreen);
994 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
996 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
997 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1001 EnterCriticalSection(&CriticalSection);
1002 SelectObject(hDCMem, bmp);
1005 SelectObject(hDCMem, hPen);
1021 LeaveCriticalSection(&CriticalSection);
1025 ReleaseDC(hWnd, hDCScreen);
1028 setPixel(ip, color);
1044 unsigned int w,
unsigned int h,
unsigned int thickness)
1057 HDC hDCScreen = GetDC(hWnd);
1058 HDC hDCMem = CreateCompatibleDC(hDCScreen);
1063 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
1065 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
1066 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1070 EnterCriticalSection(&CriticalSection);
1071 SelectObject(hDCMem, bmp);
1074 SelectObject(hDCMem, hPen);
1125 LeaveCriticalSection(&CriticalSection);
1129 ReleaseDC(hWnd, hDCScreen);
1139 unsigned int size = nbCols*nbRows*4;
1140 unsigned char * imBuffer =
new unsigned char[size];
1143 GetBitmapBits(bmp, static_cast<LONG>(size), (
void *)imBuffer);
1146 I.
resize(nbRows, nbCols);
1149 for(
unsigned int i=0 ; i<size ; i+=4)
1151 I.
bitmap[i>>2].
R = imBuffer[i+2];
1152 I.
bitmap[i>>2].
G = imBuffer[i+1];
1153 I.
bitmap[i>>2].
B = imBuffer[i+0];