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);
159 void vpGDIRenderer::setImgROI(
const vpImage<vpRGBa>& im,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height )
162 convertROI(im, bmp, iP, width, height);
188 convertROI(im, bmp, iP, width, height);
197 bool vpGDIRenderer::render()
201 HDC hDCScreen = BeginPaint(hWnd, &ps);
204 HDC hDCMem = CreateCompatibleDC(hDCScreen);
207 EnterCriticalSection(&CriticalSection);
208 SelectObject(hDCMem, bmp);
212 BitBlt(hDCScreen, 0, 0,
213 static_cast<int>( nbCols ),
214 static_cast<int>( nbRows ),
215 hDCMem, 0, 0, SRCCOPY);
217 LeaveCriticalSection(&CriticalSection);
219 DeleteObject(hDCMem);
239 bool needPad = ((w%2) == 0) ?
false :
true;
240 unsigned int newW = w;
243 newW = (needPad) ? (w+1) : w;
246 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
252 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
278 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
288 updateBitmap(hBmp,imBuffer, newW, h);
299 void vpGDIRenderer::convertROI(
const vpImage<vpRGBa> &I, HBITMAP& hBmp,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height)
302 unsigned int w = width;
303 unsigned int h = height;
307 bool needPad = ((w%2) == 0) ?
false :
true;
308 unsigned int newW = w;
311 newW = (needPad) ? (w+1) : w;
314 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
318 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
325 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
339 imBuffer[i+0] = (bitmap+k)->B;
340 imBuffer[i+1] = (bitmap+k)->G;
341 imBuffer[i+2] = (bitmap+k)->R;
342 imBuffer[i+3] = (bitmap+k)->A;
349 bitmap = bitmap+iwidth;
358 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
360 imBuffer[i+0] = (bitmap+k)->B;
361 imBuffer[i+1] = (bitmap+k)->G;
362 imBuffer[i+2] = (bitmap+k)->R;
363 imBuffer[i+3] = (bitmap+k)->A;
368 bitmap = bitmap+iwidth;
375 updateBitmapROI(hBmp,imBuffer,iP, newW, h);
395 bool needPad = ((w%2) == 0) ?
false :
true;
396 unsigned int newW = w;
399 newW = (needPad) ? (w+1) : w;
402 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
408 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
422 imBuffer[i+0] = I.
bitmap[k];
423 imBuffer[i+1] = I.
bitmap[k];
424 imBuffer[i+2] = I.
bitmap[k];
425 imBuffer[i+3] = I.
bitmap[k];
433 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
435 imBuffer[i+0] = I.
bitmap[k];
436 imBuffer[i+1] = I.
bitmap[k];
437 imBuffer[i+2] = I.
bitmap[k];
438 imBuffer[i+3] = I.
bitmap[k];
443 updateBitmap(hBmp,imBuffer, newW, h);
458 unsigned int w = width;
459 unsigned int h = height;
463 bool needPad = ((w%2) == 0) ?
false :
true;
464 unsigned int newW = w;
467 newW = (needPad) ? (w+1) : w;
470 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
472 unsigned char* bitmap = I.
bitmap;
474 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
481 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
495 imBuffer[i+0] = *(bitmap+k);
496 imBuffer[i+1] = *(bitmap+k);
497 imBuffer[i+2] = *(bitmap+k);
498 imBuffer[i+3] = *(bitmap+k);
505 bitmap = bitmap+iwidth;
514 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
516 imBuffer[i+0] = *(bitmap+k);
517 imBuffer[i+1] = *(bitmap+k);
518 imBuffer[i+2] = *(bitmap+k);
519 imBuffer[i+3] = *(bitmap+k);
524 bitmap = bitmap+iwidth;
531 updateBitmapROI(hBmp,imBuffer,iP, newW, h);
547 bool vpGDIRenderer::updateBitmap(HBITMAP& hBmp,
unsigned char * imBuffer,
548 unsigned int w,
unsigned int h)
552 EnterCriticalSection(&CriticalSection);
555 if( (nbCols==w) && (nbRows==h) )
558 SetBitmapBits(hBmp, w * h * 4, imBuffer);
568 if( (hBmp = CreateBitmap(static_cast<int>(w),static_cast<int>(h),
569 1,32,(
void*)imBuffer)) == NULL)
573 LeaveCriticalSection(&CriticalSection);
589 bool vpGDIRenderer::updateBitmapROI(HBITMAP& hBmp,
unsigned char * imBuffer,
const vpImagePoint &iP,
590 unsigned int w,
unsigned int h)
592 int w_ =
static_cast<int>(w);
593 int h_ =
static_cast<int>(h);
594 HBITMAP htmp = CreateBitmap(w_,h_,1,32,(
void*)imBuffer);
597 HDC hDCScreen = GetDC(hWnd);
598 HDC hDCMem = CreateCompatibleDC(hDCScreen);
599 HDC hDCMem2 = CreateCompatibleDC(hDCScreen);
602 EnterCriticalSection(&CriticalSection);
603 SelectObject(hDCMem, bmp);
604 SelectObject(hDCMem2, htmp);
606 BitBlt(hDCMem,(
int)iP.
get_u(),(int)iP.
get_v(),w_,h_, hDCMem2, 0, 0,SRCCOPY);
607 LeaveCriticalSection(&CriticalSection);
610 ReleaseDC(hWnd, hDCScreen);
626 HDC hDCScreen = GetDC(hWnd);
627 HDC hDCMem = CreateCompatibleDC(hDCScreen);
630 EnterCriticalSection(&CriticalSection);
631 SelectObject(hDCMem, bmp);
637 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
644 LeaveCriticalSection(&CriticalSection);
647 ReleaseDC(hWnd, hDCScreen);
660 unsigned int thickness,
int style)
662 HDC hDCScreen,hDCMem;
667 hDCScreen = GetDC(hWnd);
668 if(!hDCScreen)
continue;
669 hDCMem = CreateCompatibleDC(hDCScreen);
671 ReleaseDC(hWnd, hDCScreen);
677 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
679 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
680 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
684 ReleaseDC(hWnd, hDCScreen);
687 if(!SetBkMode(hDCMem, TRANSPARENT)){
690 ReleaseDC(hWnd, hDCScreen);
695 EnterCriticalSection(&CriticalSection);
697 if(!SelectObject(hDCMem, bmp)){
698 LeaveCriticalSection(&CriticalSection);
701 ReleaseDC(hWnd, hDCScreen);
706 if(!SelectObject(hDCMem, hPen)){
707 LeaveCriticalSection(&CriticalSection);
710 ReleaseDC(hWnd, hDCScreen);
718 hDCScreen = GetDC(hWnd);
719 hDCMem = CreateCompatibleDC(hDCScreen);
724 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
726 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
727 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
729 SetBkMode(hDCMem, TRANSPARENT);
732 EnterCriticalSection(&CriticalSection);
733 SelectObject(hDCMem, bmp);
736 SelectObject(hDCMem, hPen);
752 LeaveCriticalSection(&CriticalSection);
756 ReleaseDC(hWnd, hDCScreen);
769 void vpGDIRenderer::drawRect(
const vpImagePoint &topLeft,
770 unsigned int width,
unsigned int height,
771 const vpColor &color,
bool fill,
772 unsigned int thickness)
774 if (thickness == 0) thickness = 1;
776 HDC hDCScreen = GetDC(hWnd);
777 HDC hDCMem = CreateCompatibleDC(hDCScreen);
781 COLORREF gdicolor = RGB(0,0,0);
784 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
786 gdicolor = RGB(color.
R, color.
G, color.
B);
787 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
793 lBrush.lbStyle = BS_SOLID;
795 lBrush.lbColor = colors[color.
id];
797 lBrush.lbColor = gdicolor;
800 else lBrush.lbStyle = BS_HOLLOW;
801 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
804 EnterCriticalSection(&CriticalSection);
805 SelectObject(hDCMem, bmp);
808 SelectObject(hDCMem, hbrush);
810 SelectObject(hDCMem, hPen);
820 LeaveCriticalSection(&CriticalSection);
822 DeleteObject(hbrush);
825 ReleaseDC(hWnd, hDCScreen);
833 void vpGDIRenderer::clear(
const vpColor &color)
838 drawRect(ip, nbCols, nbRows, color,
true, 0);
850 void vpGDIRenderer::drawCircle(
const vpImagePoint ¢er,
unsigned int radius,
851 const vpColor &color,
bool fill,
unsigned int thickness)
855 HDC hDCScreen = GetDC(hWnd);
856 HDC hDCMem = CreateCompatibleDC(hDCScreen);
861 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
863 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
864 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
869 lBrush.lbStyle = BS_HOLLOW;
870 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
873 int radius_ =
static_cast<int>(radius);
880 EnterCriticalSection(&CriticalSection);
881 SelectObject(hDCMem, bmp);
884 SelectObject(hDCMem, hbrush);
886 SelectObject(hDCMem, hPen);
890 Ellipse(hDCMem, x1, y1, x2, y2);
900 Ellipse(hDCMem, x1, y1, x2, y2);
907 LeaveCriticalSection(&CriticalSection);
909 DeleteObject(hbrush);
912 ReleaseDC(hWnd, hDCScreen);
922 void vpGDIRenderer::drawText(
const vpImagePoint &ip,
const char * text,
926 HDC hDCScreen = GetDC(hWnd);
927 HDC hDCMem = CreateCompatibleDC(hDCScreen);
930 EnterCriticalSection(&CriticalSection);
931 SelectObject(hDCMem, bmp);
934 SelectObject(hDCMem, hFont);
938 SetTextColor(hDCMem, colors[color.
id]);
940 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
941 SetTextColor(hDCMem, gdicolor);
945 SetBkMode(hDCMem, TRANSPARENT);
948 int length = (int) strlen(text);
951 GetTextExtentPoint32(hDCMem, text, length, &size);
959 LeaveCriticalSection(&CriticalSection);
962 ReleaseDC(hWnd, hDCScreen);
974 void vpGDIRenderer::drawCross(
const vpImagePoint &ip,
unsigned int size,
975 const vpColor &color,
unsigned int thickness)
977 int half_size =
static_cast<int>( size/2 );
984 HDC hDCScreen = GetDC(hWnd);
985 HDC hDCMem = CreateCompatibleDC(hDCScreen);
990 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
992 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
993 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
997 EnterCriticalSection(&CriticalSection);
998 SelectObject(hDCMem, bmp);
1001 SelectObject(hDCMem, hPen);
1017 LeaveCriticalSection(&CriticalSection);
1021 ReleaseDC(hWnd, hDCScreen);
1024 setPixel(ip, color);
1040 unsigned int w,
unsigned int h,
unsigned int thickness)
1053 HDC hDCScreen = GetDC(hWnd);
1054 HDC hDCMem = CreateCompatibleDC(hDCScreen);
1059 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
1061 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
1062 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1066 EnterCriticalSection(&CriticalSection);
1067 SelectObject(hDCMem, bmp);
1070 SelectObject(hDCMem, hPen);
1121 LeaveCriticalSection(&CriticalSection);
1125 ReleaseDC(hWnd, hDCScreen);
1135 unsigned int size = nbCols*nbRows*4;
1136 unsigned char * imBuffer =
new unsigned char[size];
1139 GetBitmapBits(bmp, static_cast<LONG>(size), (
void *)imBuffer);
1142 I.
resize(nbRows, nbCols);
1145 for(
unsigned int i=0 ; i<size ; i+=4)
1147 I.
bitmap[i>>2].
R = imBuffer[i+2];
1148 I.
bitmap[i>>2].
G = imBuffer[i+1];
1149 I.
bitmap[i>>2].
B = imBuffer[i+0];
void set_j(const double j)
unsigned int getWidth() const
unsigned char B
Blue component.
Type * bitmap
points toward the bitmap
static const vpColor black
static const vpColor darkRed
Class to define colors available for display functionnalities.
void resize(const unsigned int height, const unsigned int width)
set the size of the image
void set_i(const double i)
static const vpColor lightGray
static double measureTimeMs()
static const vpColor darkBlue
unsigned char G
Green component.
static const vpColor green
static int round(const double x)
static const vpColor lightRed
Class that defines a RGB 32 bits structure.
static const vpColor orange
static const vpColor cyan
static const vpColor lightGreen
static double sqr(double x)
unsigned char A
Additionnal component.
static const vpColor gray
static const vpColor darkGray
Error that can be emited by the vpDisplay class and its derivates.
unsigned char R
Red component.
unsigned int getHeight() const
static const vpColor darkGreen
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
static const vpColor yellow
static const vpColor lightBlue
static const vpColor purple
static const vpColor white
static const vpColor blue