ViSP  2.9.0
vpDisplayWin32.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayWin32.cpp 4661 2014-02-10 19:34:58Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Windows 32 display base class
36  *
37  * Authors:
38  * Bruno Renier
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 #if ( defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) )
44 
45 #define FLUSH_ROI
46 #include <visp/vpDisplayWin32.h>
47 #include <visp/vpDisplayException.h>
48 #include <string>
49 
50 
51 
52 const int vpDisplayWin32::MAX_INIT_DELAY = 5000;
53 
58 void vpCreateWindow(threadParam * param)
59 {
60  //char* title = param->title;
61  (param->vpDisp)->window.initWindow(param->title.c_str(), param->x, param->y,
62  param->w, param->h);
63  delete param;
64 }
65 
69 vpDisplayWin32::vpDisplayWin32(vpWin32Renderer * rend) :
70  iStatus(false), window(rend)
71 {
72 }
73 
74 
79 {
80  closeDisplay();
81 }
82 
83 
95  int x,
96  int y,
97  const char *title)
98 {
99  if ((I.getHeight() == 0) || (I.getWidth()==0))
100  {
101  vpERROR_TRACE("Image not initialized " ) ;
103  "Image not initialized")) ;
104  }
105 
106  window.renderer->setImg(I);
107 
108  init (I.getWidth(), I.getHeight(), x, y, title) ;
109  I.display = this ;
110 }
111 
112 
122  int x,
123  int y,
124  const char *title)
125 {
126  if ((I.getHeight() == 0) || (I.getWidth()==0))
127  {
128  vpERROR_TRACE("Image not initialized " ) ;
130  "Image not initialized")) ;
131  }
132 
133  window.renderer->setImg(I);
134 
135  init (I.getWidth(), I.getHeight(), x, y, title) ;
136  I.display = this ;
137 }
138 
139 
148 void vpDisplayWin32::init(unsigned int width, unsigned int height,
149  int x, int y,
150  const char *title)
151 {
152  if (title != NULL)
153  title_ = std::string(title);
154  else
155  title_ = std::string(" ");
156 
157  if (x != -1)
158  windowXPosition = x;
159  if (y != -1)
160  windowYPosition = y;
161 
162  //we prepare the window's thread creation
163  threadParam * param = new threadParam;
164  param->x = windowXPosition;
165  param->y = windowYPosition;
166  param->w = width;
167  param->h = height;
168  param->vpDisp = this;
169  param->title = this->title_;
170 
171  //creates the window in a separate thread
172  hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)vpCreateWindow,
173  param,0,&threadId);
174 
175  //the initialization worked
176  iStatus = (hThread != (HANDLE)NULL);
177 
179 }
180 
181 
187 {
188  //if the window is not initialized yet
189  if(!window.isInitialized())
190  {
191  //wait
192  if( WAIT_OBJECT_0 != WaitForSingleObject(window.semaInit,MAX_INIT_DELAY))
194  "Window not initialized")) ;
195  //problem : the window is not initialized
196  }
197 }
198 
199 
212 {
213  //waits if the window is not initialized
214  waitForInit();
215 
216  //sets the image to render
217  window.renderer->setImg(I);
218  //sends a message to the window
219  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
220 }
221 
222 
240 void vpDisplayWin32::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
241 {
242  //waits if the window is not initialized
243  waitForInit();
244 
245  //sets the image to render
246  window.renderer->setImgROI(I,iP,width,height);
247  //sends a message to the window
248  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
249 }
250 
251 
264 {
265  //wait if the window is not initialized
266  waitForInit();
267 
268  //sets the image to render
269  window.renderer->setImg(I);
270  //sends a message to the window
271  //PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
272 }
273 
291 void vpDisplayWin32::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
292 {
293  //waits if the window is not initialized
294  waitForInit();
295 
296  //sets the image to render
297  window.renderer->setImgROI(I,iP,width,height);
298  //sends a message to the window
299  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
300 }
301 
302 
318 bool vpDisplayWin32::getClick( bool blocking)
319 {
320  //wait if the window is not initialized
321  waitForInit();
322  bool ret = false;
323  //sends a message to the window
324 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
325 
326  //waits for a button to be pressed
327  if(blocking){
328  WaitForSingleObject(window.semaClick, 0);
329  WaitForSingleObject(window.semaClickUp, 0); //to erase previous events
330  WaitForSingleObject(window.semaClick, INFINITE);
331  ret = true;
332  }
333  else {
334  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
335  }
336 
337  return ret;
338 }
339 
340 
357 bool vpDisplayWin32::getClick(vpImagePoint &ip, bool blocking)
358 {
359  //wait if the window is not initialized
360  waitForInit();
361 
362  bool ret = false ;
363  double u, v;
364  //tells the window there has been a getclick demand
365 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
366  //waits for a click
367  if(blocking){
368  WaitForSingleObject(window.semaClick, 0);
369  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
370  WaitForSingleObject(window.semaClick, INFINITE);
371  ret = true;
372  }
373  else {
374  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
375  }
376 
377  u = window.clickX;
378  v = window.clickY;
379  ip.set_u( u );
380  ip.set_v( v );
381 
382  return ret;
383 }
384 
385 
406  bool blocking)
407 {
408  //wait if the window is not initialized
409  waitForInit();
410  bool ret = false;
411  double u, v;
412  //tells the window there has been a getclickup demand
413 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
414  //waits for a click
415  if(blocking){
416  WaitForSingleObject(window.semaClick, 0);
417  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
418  WaitForSingleObject(window.semaClick, INFINITE);
419  ret = true;
420  }
421  else
422  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
423 
424  u = window.clickX;
425  v = window.clickY;
426  ip.set_u( u );
427  ip.set_v( v );
428  button = window.clickButton;
429 
430  return ret;
431 }
432 
433 
458  bool blocking)
459 {
460  //wait if the window is not initialized
461  waitForInit();
462  bool ret = false;
463  double u, v;
464  //tells the window there has been a getclickup demand
465 // PostMessage(window.getHWnd(), vpWM_GETCLICKUP, 0,0);
466 
467  //waits for a click release
468  if(blocking){
469  WaitForSingleObject(window.semaClickUp, 0);
470  WaitForSingleObject(window.semaClick, 0);//to erase previous events
471  WaitForSingleObject(window.semaClickUp, INFINITE);
472  ret = true;
473  }
474  else
475  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClickUp, 0));
476 
477  u = window.clickXUp;
478  v = window.clickYUp;
479  ip.set_u( u );
480  ip.set_v( v );
481  button = window.clickButtonUp;
482 
483  return ret;
484 }
485 
501 bool vpDisplayWin32::getKeyboardEvent( bool blocking )
502 {
503  //wait if the window is not initialized
504  waitForInit();
505 
506  bool ret = false ;
507  //waits for a keyboard event
508  if(blocking){
509  WaitForSingleObject(window.semaKey, 0); // key down
510  WaitForSingleObject(window.semaKey, 0); // key up
511  WaitForSingleObject(window.semaKey, INFINITE);
512  ret = true;
513  }
514  else
515  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
516 
517  return ret;
518 }
538 bool vpDisplayWin32::getKeyboardEvent(char *string, bool blocking)
539 {
540  //wait if the window is not initialized
541  waitForInit();
542 
543  bool ret = false ;
544  //waits for a keyboard event
545  if(blocking){
546  WaitForSingleObject(window.semaKey, 0); // key down
547  WaitForSingleObject(window.semaKey, 0); // key up
548  WaitForSingleObject(window.semaKey, INFINITE);
549  ret = true;
550  }
551  else {
552  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
553  }
554  // printf("key: %ud\n", window.key);
555  sprintf(string, "%s", window.lpString);
556 
557  return ret;
558 }
569 bool
571 {
572  //wait if the window is not initialized
573  waitForInit();
574 
575  bool ret = false;
576 
577  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaMove, 0));
578  if (ret)
579  {
580  double u, v;
581  std::cout << "toto";
582  //tells the window there has been a getclick demand
583  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
584 
585  u = window.coordX;
586  v = window.coordY;
587  ip.set_u( u );
588  ip.set_v( v );
589  }
590 
591  return ret;
592 }
593 
604 bool
606 {
607  //wait if the window is not initialized
608  waitForInit();
609 
610  bool ret = true ;
611  double u, v;
612  //tells the window there has been a getclick demand
613  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
614 
615  u = window.coordX;
616  v = window.coordY;
617  ip.set_u( u );
618  ip.set_v( v );
619 
620  return ret;
621 }
622 
629 void vpDisplayWin32::setWindowPosition(int winx, int winy)
630 {
631  //wait if the window is not initialized
632  waitForInit();
633 
634  //cahange the window position only
635  SetWindowPos(window.hWnd,HWND_TOP, winx, winy, 0, 0,
636  SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOZORDER |SWP_NOSIZE);
637 
638 }
639 
640 
646 void vpDisplayWin32::setTitle(const char *windowtitle)
647 {
648  //wait if the window is not initialized
649  waitForInit();
650  SetWindowText(window.hWnd, windowtitle);
651 }
652 
653 
659 void vpDisplayWin32::setFont(const char * /* fontname */)
660 {
661  vpERROR_TRACE("Not yet implemented" ) ;
662 }
663 
664 
671 {
672  //waits if the window is not initialized
673  waitForInit();
674 
675  //sends a message to the window
676  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
677 }
678 
684 void vpDisplayWin32::flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
685 {
686  //waits if the window is not initialized
687  waitForInit();
688 
689  /*
690  Under windows, flushing an ROI takes more time than
691  flushing the whole image.
692  Therefore, we update the maximum area even when asked to update a region.
693  */
694 #ifdef FLUSH_ROI
695  typedef struct _half_rect_t{
696  unsigned short left_top;
697  unsigned short right_bottom;
698  } half_rect_t;
699 
700  half_rect_t hr1;
701  half_rect_t hr2;
702 
703  hr1.left_top = (unsigned short)iP.get_u();
704  hr1.right_bottom = (unsigned short)(iP.get_u()+width-1);
705 
706  hr2.left_top = (unsigned short)iP.get_v();
707  hr2.right_bottom = (unsigned short)(iP.get_v()+height-1);
708 
709  //sends a message to the window
710 # if 1 // new version FS
711  WPARAM wp = (WPARAM)(hr1.left_top << sizeof(unsigned short)) + hr1.right_bottom;
712  LPARAM lp = (hr2.left_top << sizeof(unsigned short)) + hr2.right_bottom;
713 # else // produce warnings with MinGW
714  WPARAM wp=*((WPARAM*)(&hr1));
715  LPARAM lp=*((WPARAM*)(&hr2));
716 # endif
717  PostMessage(window.getHWnd(), vpWM_DISPLAY_ROI, wp,lp);
718 #else
719  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
720 #endif
721 }
722 
723 
730  const vpColor &color )
731 {
732  //wait if the window is not initialized
733  waitForInit();
734  window.renderer->setPixel(ip, color);
735 }
736 
744  const vpImagePoint &ip2,
745  const vpColor &color,
746  unsigned int thickness )
747 {
748  //wait if the window is not initialized
749  waitForInit();
750  window.renderer->drawLine(ip1, ip2, color, thickness);
751 }
752 
753 
764  const vpImagePoint &ip2,
765  const vpColor &color,
766  unsigned int thickness )
767 {
768  //wait if the window is not initialized
769  waitForInit();
770  window.renderer->drawLine(ip1,ip2,color,thickness,PS_DASHDOT);
771 }
772 
787  unsigned int width, unsigned int height,
788  const vpColor &color, bool fill,
789  unsigned int thickness )
790 {
791  //wait if the window is not initialized
792  waitForInit();
793  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
794 }
795 
796 
810  const vpImagePoint &bottomRight,
811  const vpColor &color, bool fill,
812  unsigned int thickness )
813 {
814  //wait if the window is not initialized
815  waitForInit();
816  unsigned int width = static_cast<unsigned int>( bottomRight.get_j() - topLeft.get_j() );
817  unsigned int height = static_cast<unsigned int>(bottomRight.get_i() - topLeft.get_i() );
818  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
819 }
820 
833  const vpColor &color, bool fill,
834  unsigned int thickness )
835 {
836  //wait if the window is not initialized
837  waitForInit();
838  vpImagePoint topLeft;
839  topLeft.set_i(rectangle.getTop());
840  topLeft.set_j(rectangle.getLeft());
841  window.renderer->drawRect(topLeft,
842  static_cast<unsigned int>( rectangle.getWidth() ),
843  static_cast<unsigned int>( rectangle.getHeight() ),
844  color, fill, thickness);
845 }
846 
847 
858  unsigned int radius,
859  const vpColor &color,
860  bool fill,
861  unsigned int thickness )
862 {
863  //wait if the window is not initialized
864  waitForInit();
865  window.renderer->drawCircle(center,radius,color,fill,thickness);
866 }
867 
875  const char *text,
876  const vpColor &color )
877 {
878  //wait if the window is not initialized
879  waitForInit();
880  window.renderer->drawText(ip,text,color);
881 }
882 
891  unsigned int size,
892  const vpColor &color,
893  unsigned int thickness)
894 {
895  //wait if the window is not initialized
896  waitForInit();
897  window.renderer->drawCross(ip, size, color, thickness);
898 }
899 
900 
909  const vpImagePoint &ip2,
910  const vpColor &color,
911  unsigned int w,unsigned int h,
912  unsigned int thickness)
913 
914 {
915  //wait if the window is not initialized
916  waitForInit();
917  window.renderer->drawArrow(ip1, ip2, color, w, h, thickness);
918 }
919 
920 
926  //wait if the window is not initialized
927  waitForInit();
928  window.renderer->clear(color);
929 }
930 
931 
937 {
939  waitForInit();
940  PostMessage(window.getHWnd(), vpWM_CLOSEDISPLAY, 0,0);
941  //if the destructor is called for a reason different than a
942  //problem in the thread creation
943  if (iStatus) {
944  //waits for the thread to end
945  WaitForSingleObject(hThread, INFINITE);
946  CloseHandle(hThread);
947  }
948  displayHasBeenInitialized = false ;
949  window.initialized = false ;
950  }
951 }
952 
958 {
959  //wait if the window is not initialized
960  waitForInit();
961  window.renderer->getImage(I);
962 }
963 
964 #endif
vpDisplay * display
Definition: vpImage.h:121
static const int MAX_INIT_DELAY
Maximum delay for window initialization.
DWORD threadId
Id of the window's thread.
double getTop() const
Definition: vpRect.h:174
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
double get_v() const
Definition: vpImagePoint.h:263
void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
unsigned int width
Definition: vpDisplay.h:185
double get_i() const
Definition: vpImagePoint.h:194
unsigned int getWidth() const
Definition: vpImage.h:159
virtual ~vpDisplayWin32()
bool getClick(bool blocking=true)
#define vpERROR_TRACE
Definition: vpDebug.h:395
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
double get_u() const
Definition: vpImagePoint.h:252
void setFont(const char *fontname)
Set the font used to display text.
double getHeight() const
Definition: vpRect.h:155
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:180
void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
bool iStatus
Initialization status.
void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
double get_j() const
Definition: vpImagePoint.h:205
void displayImageROI(const vpImage< unsigned char > &I, const vpImagePoint &iP, const unsigned int width, const unsigned int height)
bool getKeyboardEvent(bool blocking=true)
vpDisplayWin32(vpWin32Renderer *rend=NULL)
vpWin32Window window
The window.
bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)
std::string title_
Definition: vpDisplay.h:187
void set_i(const double ii)
Definition: vpImagePoint.h:158
double getWidth() const
Definition: vpRect.h:193
void displayImage(const vpImage< vpRGBa > &I)
void set_u(const double u)
Definition: vpImagePoint.h:216
unsigned int height
Definition: vpDisplay.h:186
void set_v(const double v)
Definition: vpImagePoint.h:227
void setTitle(const char *windowtitle)
void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
void displayPoint(const vpImagePoint &ip, const vpColor &color)
friend void vpCreateWindow(threadParam *param)
Function used to launch the window in a thread.
void set_j(const double jj)
Definition: vpImagePoint.h:169
bool getPointerMotionEvent(vpImagePoint &ip)
Error that can be emited by the vpDisplay class and its derivates.
HANDLE hThread
Handle of the window's thread.
int windowXPosition
display position
Definition: vpDisplay.h:182
unsigned int getHeight() const
Definition: vpImage.h:150
Defines a rectangle in the plane.
Definition: vpRect.h:85
void flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
flush the Win32 buffer It's necessary to use this function to see the results of any drawing ...
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void clearDisplay(const vpColor &color=vpColor::white)
void flushDisplay()
flush the Win32 buffer It's necessary to use this function to see the results of any drawing ...
int windowYPosition
display position
Definition: vpDisplay.h:184
void setWindowPosition(int winx, int winy)
double getLeft() const
Definition: vpRect.h:161
bool getPointerPosition(vpImagePoint &ip)
void getImage(vpImage< vpRGBa > &I)