38 #include <visp3/core/vpConfig.h>
40 #if ( defined(VISP_HAVE_GDI) )
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45 #include <visp3/core/vpGDIRenderer.h>
50 vpGDIRenderer::vpGDIRenderer()
53 int bpp = GetDeviceCaps(GetDC(NULL), BITSPIXEL);
56 "vpGDIRenderer supports only 32bits depth: screen is %dbits depth!", bpp);
58 InitializeCriticalSection(&CriticalSection);
108 vpGDIRenderer::~vpGDIRenderer()
111 DeleteCriticalSection(&CriticalSection);
124 bool vpGDIRenderer::init(HWND hWindow,
unsigned int width,
unsigned int height)
132 hFont = CreateFont(18, 0, 0, 0, FW_NORMAL,
false,
false,
false,
133 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
134 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
135 DEFAULT_PITCH | FF_DONTCARE, NULL);
161 convertROI(I, iP, width, height);
189 convertROI(I, iP, width, height);
198 bool vpGDIRenderer::render()
202 HDC hDCScreen = BeginPaint(hWnd, &ps);
205 HDC hDCMem = CreateCompatibleDC(hDCScreen);
208 EnterCriticalSection(&CriticalSection);
209 SelectObject(hDCMem, bmp);
213 BitBlt(hDCScreen, 0, 0,
214 static_cast<int>( nbCols ),
215 static_cast<int>( nbRows ),
216 hDCMem, 0, 0, SRCCOPY);
218 LeaveCriticalSection(&CriticalSection);
220 DeleteObject(hDCMem);
240 bool needPad = ((w%2) == 0) ?
false :
true;
241 unsigned int newW = w;
244 newW = (needPad) ? (w+1) : w;
247 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
253 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
279 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
289 updateBitmap(hBmp,imBuffer, newW, h);
301 void vpGDIRenderer::convertROI(
const vpImage<vpRGBa> &I,
const vpImagePoint &iP,
const unsigned int width,
const unsigned int height)
304 unsigned int w =
width;
309 bool needPad = ((w%2) == 0) ?
false :
true;
310 unsigned int newW = w;
313 newW = (needPad) ? (w+1) : w;
316 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
320 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
327 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
341 imBuffer[i+0] = (bitmap+k)->B;
342 imBuffer[i+1] = (bitmap+k)->G;
343 imBuffer[i+2] = (bitmap+k)->R;
344 imBuffer[i+3] = (bitmap+k)->A;
351 bitmap = bitmap+iwidth;
360 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
362 imBuffer[i+0] = (bitmap+k)->B;
363 imBuffer[i+1] = (bitmap+k)->G;
364 imBuffer[i+2] = (bitmap+k)->R;
365 imBuffer[i+3] = (bitmap+k)->A;
370 bitmap = bitmap+iwidth;
377 updateBitmapROI(imBuffer,iP, newW, h);
397 bool needPad = ((w%2) == 0) ?
false :
true;
398 unsigned int newW = w;
401 newW = (needPad) ? (w+1) : w;
404 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
410 for(
unsigned int i=0, k=0 ; i<newW * h * 4; i+=4, k++)
424 imBuffer[i+0] = I.
bitmap[k];
425 imBuffer[i+1] = I.
bitmap[k];
426 imBuffer[i+2] = I.
bitmap[k];
427 imBuffer[i+3] = I.
bitmap[k];
435 for(
unsigned int i=0, k=0 ; i<w * h * 4 ; i+=4, k++)
437 imBuffer[i+0] = I.
bitmap[k];
438 imBuffer[i+1] = I.
bitmap[k];
439 imBuffer[i+2] = I.
bitmap[k];
440 imBuffer[i+3] = I.
bitmap[k];
445 updateBitmap(hBmp,imBuffer, newW, h);
461 unsigned int w =
width;
466 bool needPad = ((w%2) == 0) ?
false :
true;
467 unsigned int newW = w;
470 newW = (needPad) ? (w+1) : w;
473 unsigned char * imBuffer =
new unsigned char[newW * h * 4];
475 unsigned char* bitmap = I.
bitmap;
477 bitmap = bitmap + (int)(iP.
get_i()*iwidth+ iP.
get_j());
484 for(
unsigned int i=0 ; i<newW * h * 4; i+=4)
498 imBuffer[i+0] = *(bitmap+k);
499 imBuffer[i+1] = *(bitmap+k);
500 imBuffer[i+2] = *(bitmap+k);
501 imBuffer[i+3] = *(bitmap+k);
508 bitmap = bitmap+iwidth;
517 for (
unsigned int i=0 ; i < w * h * 4 ; i+=4)
519 imBuffer[i+0] = *(bitmap+k);
520 imBuffer[i+1] = *(bitmap+k);
521 imBuffer[i+2] = *(bitmap+k);
522 imBuffer[i+3] = *(bitmap+k);
527 bitmap = bitmap+iwidth;
534 updateBitmapROI(imBuffer,iP, newW, h);
550 bool vpGDIRenderer::updateBitmap(HBITMAP& hBmp,
unsigned char * imBuffer,
551 unsigned int w,
unsigned int h)
555 EnterCriticalSection(&CriticalSection);
558 if( (nbCols==w) && (nbRows==h) )
561 SetBitmapBits(hBmp, w * h * 4, imBuffer);
571 if( (hBmp = CreateBitmap(static_cast<int>(w),static_cast<int>(h),
572 1,32,(
void*)imBuffer)) == NULL)
576 LeaveCriticalSection(&CriticalSection);
591 bool vpGDIRenderer::updateBitmapROI(
unsigned char * imBuffer,
const vpImagePoint &iP,
592 unsigned int w,
unsigned int h)
594 int w_ =
static_cast<int>(w);
595 int h_ =
static_cast<int>(h);
596 HBITMAP htmp = CreateBitmap(w_,h_,1,32,(
void*)imBuffer);
599 HDC hDCScreen = GetDC(hWnd);
600 HDC hDCMem = CreateCompatibleDC(hDCScreen);
601 HDC hDCMem2 = CreateCompatibleDC(hDCScreen);
604 EnterCriticalSection(&CriticalSection);
605 SelectObject(hDCMem, bmp);
606 SelectObject(hDCMem2, htmp);
608 BitBlt(hDCMem,(
int)iP.
get_u(),(int)iP.
get_v(),w_,h_, hDCMem2, 0, 0,SRCCOPY);
609 LeaveCriticalSection(&CriticalSection);
612 ReleaseDC(hWnd, hDCScreen);
628 HDC hDCScreen = GetDC(hWnd);
629 HDC hDCMem = CreateCompatibleDC(hDCScreen);
632 EnterCriticalSection(&CriticalSection);
633 SelectObject(hDCMem, bmp);
639 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
646 LeaveCriticalSection(&CriticalSection);
649 ReleaseDC(hWnd, hDCScreen);
662 unsigned int thickness,
int style)
664 HDC hDCScreen= NULL, hDCMem = NULL;
669 hDCScreen = GetDC(hWnd);
670 if(!hDCScreen)
continue;
671 hDCMem = CreateCompatibleDC(hDCScreen);
673 ReleaseDC(hWnd, hDCScreen);
679 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
681 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
682 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
686 ReleaseDC(hWnd, hDCScreen);
689 if(!SetBkMode(hDCMem, TRANSPARENT)){
692 ReleaseDC(hWnd, hDCScreen);
697 EnterCriticalSection(&CriticalSection);
699 if(!SelectObject(hDCMem, bmp)){
700 LeaveCriticalSection(&CriticalSection);
703 ReleaseDC(hWnd, hDCScreen);
708 if(!SelectObject(hDCMem, hPen)){
709 LeaveCriticalSection(&CriticalSection);
712 ReleaseDC(hWnd, hDCScreen);
720 hDCScreen = GetDC(hWnd);
721 hDCMem = CreateCompatibleDC(hDCScreen);
725 hPen = CreatePen(style, static_cast<int>(thickness), colors[color.
id]);
727 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
728 hPen = CreatePen(style, static_cast<int>(thickness), gdicolor);
730 SetBkMode(hDCMem, TRANSPARENT);
733 EnterCriticalSection(&CriticalSection);
734 SelectObject(hDCMem, bmp);
737 SelectObject(hDCMem, hPen);
753 LeaveCriticalSection(&CriticalSection);
757 ReleaseDC(hWnd, hDCScreen);
770 void vpGDIRenderer::drawRect(
const vpImagePoint &topLeft,
771 unsigned int width,
unsigned int height,
772 const vpColor &color,
bool fill,
773 unsigned int thickness)
775 if (thickness == 0) thickness = 1;
777 HDC hDCScreen = GetDC(hWnd);
778 HDC hDCMem = CreateCompatibleDC(hDCScreen);
782 COLORREF gdicolor = RGB(0,0,0);
785 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
787 gdicolor = RGB(color.
R, color.
G, color.
B);
788 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
794 lBrush.lbStyle = BS_SOLID;
796 lBrush.lbColor = colors[color.
id];
798 lBrush.lbColor = gdicolor;
801 else lBrush.lbStyle = BS_HOLLOW;
802 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
805 EnterCriticalSection(&CriticalSection);
806 SelectObject(hDCMem, bmp);
809 SelectObject(hDCMem, hbrush);
811 SelectObject(hDCMem, hPen);
821 LeaveCriticalSection(&CriticalSection);
823 DeleteObject(hbrush);
826 ReleaseDC(hWnd, hDCScreen);
834 void vpGDIRenderer::clear(
const vpColor &color)
839 drawRect(ip, nbCols, nbRows, color,
true, 0);
851 void vpGDIRenderer::drawCircle(
const vpImagePoint ¢er,
unsigned int radius,
852 const vpColor &color,
bool fill,
unsigned int thickness)
856 HDC hDCScreen = GetDC(hWnd);
857 HDC hDCMem = CreateCompatibleDC(hDCScreen);
862 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
864 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
865 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
870 lBrush.lbStyle = BS_HOLLOW;
871 HBRUSH hbrush = CreateBrushIndirect(&lBrush);
874 int radius_ =
static_cast<int>(radius);
881 EnterCriticalSection(&CriticalSection);
882 SelectObject(hDCMem, bmp);
885 SelectObject(hDCMem, hbrush);
887 SelectObject(hDCMem, hPen);
891 Ellipse(hDCMem, x1, y1, x2, y2);
901 Ellipse(hDCMem, x1, y1, x2, y2);
908 LeaveCriticalSection(&CriticalSection);
910 DeleteObject(hbrush);
913 ReleaseDC(hWnd, hDCScreen);
923 void vpGDIRenderer::drawText(
const vpImagePoint &ip,
const char * text,
927 HDC hDCScreen = GetDC(hWnd);
928 HDC hDCMem = CreateCompatibleDC(hDCScreen);
931 EnterCriticalSection(&CriticalSection);
932 SelectObject(hDCMem, bmp);
935 SelectObject(hDCMem, hFont);
939 SetTextColor(hDCMem, colors[color.
id]);
941 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
942 SetTextColor(hDCMem, gdicolor);
946 SetBkMode(hDCMem, TRANSPARENT);
949 int length = (int) strlen(text);
952 GetTextExtentPoint32(hDCMem, text, length, &size);
960 LeaveCriticalSection(&CriticalSection);
963 ReleaseDC(hWnd, hDCScreen);
975 void vpGDIRenderer::drawCross(
const vpImagePoint &ip,
unsigned int size,
976 const vpColor &color,
unsigned int thickness)
978 int half_size =
static_cast<int>( size/2 );
985 HDC hDCScreen = GetDC(hWnd);
986 HDC hDCMem = CreateCompatibleDC(hDCScreen);
991 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
993 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
994 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
998 EnterCriticalSection(&CriticalSection);
999 SelectObject(hDCMem, bmp);
1002 SelectObject(hDCMem, hPen);
1018 LeaveCriticalSection(&CriticalSection);
1022 ReleaseDC(hWnd, hDCScreen);
1025 setPixel(ip, color);
1041 unsigned int w,
unsigned int h,
unsigned int thickness)
1054 HDC hDCScreen = GetDC(hWnd);
1055 HDC hDCMem = CreateCompatibleDC(hDCScreen);
1060 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), colors[color.
id]);
1062 COLORREF gdicolor = RGB(color.
R, color.
G, color.
B);
1063 hPen = CreatePen(PS_SOLID, static_cast<int>(thickness), gdicolor);
1067 EnterCriticalSection(&CriticalSection);
1068 SelectObject(hDCMem, bmp);
1071 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];
1160 #elif !defined(VISP_BUILD_SHARED_LIBS)
1162 void dummy_vpGDIRenderer() {};
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.
static const vpColor lightGray
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
void set_i(const double ii)
static const vpColor cyan
static const vpColor lightGreen
static double sqr(double x)
unsigned char A
Additionnal component.
void resize(const unsigned int h, const unsigned int w)
set the size of the image without initializing it.
static const vpColor gray
static const vpColor darkGray
VISP_EXPORT double measureTimeMs()
void set_j(const double jj)
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 double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static const vpColor blue