ViSP  2.7.0
vpDisplayWin32.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayWin32.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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(title, 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 
153 
154  if (this->title != NULL)//delete apr�s init du thread.... ou destructeur
155  {
156  delete [] this->title;
157  this->title = NULL;
158  }
159 
160  if (title != NULL) {
161  this->title = new char[strlen(title) + 1] ;
162  strcpy(this->title, title) ;
163  }
164 
165  //we prepare the window's thread creation
166  threadParam * param = new threadParam;
167  param->x = x;
168  param->y = y;
169  param->w = width;
170  param->h = height;
171  param->vpDisp = this;
172  param->title = this->title;
173 
174  //creates the window in a separate thread
175  hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)vpCreateWindow,
176  param,0,&threadId);
177 
178  //the initialization worked
179  iStatus = (hThread != (HANDLE)NULL);
180 
182 }
183 
184 
190 {
191  //if the window is not initialized yet
192  if(!window.isInitialized())
193  {
194  //wait
195  if( WAIT_OBJECT_0 != WaitForSingleObject(window.semaInit,MAX_INIT_DELAY))
197  "Window not initialized")) ;
198  //problem : the window is not initialized
199  }
200 }
201 
202 
215 {
216  //waits if the window is not initialized
217  waitForInit();
218 
219  //sets the image to render
220  window.renderer->setImg(I);
221  //sends a message to the window
222  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
223 }
224 
225 
243 void vpDisplayWin32::displayImageROI ( const vpImage<vpRGBa> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
244 {
245  //waits if the window is not initialized
246  waitForInit();
247 
248  //sets the image to render
249  window.renderer->setImgROI(I,iP,width,height);
250  //sends a message to the window
251  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
252 }
253 
254 
267 {
268  //wait if the window is not initialized
269  waitForInit();
270 
271  //sets the image to render
272  window.renderer->setImg(I);
273  //sends a message to the window
274  //PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
275 }
276 
294 void vpDisplayWin32::displayImageROI ( const vpImage<unsigned char> &I,const vpImagePoint &iP, const unsigned int width, const unsigned int height )
295 {
296  //waits if the window is not initialized
297  waitForInit();
298 
299  //sets the image to render
300  window.renderer->setImgROI(I,iP,width,height);
301  //sends a message to the window
302  //PostMessage(window.getHWnd(),vpWM_DISPLAY,0,0);
303 }
304 
305 
321 bool vpDisplayWin32::getClick( bool blocking)
322 {
323  //wait if the window is not initialized
324  waitForInit();
325  bool ret = false;
326  //sends a message to the window
327 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
328 
329  //waits for a button to be pressed
330  if(blocking){
331  WaitForSingleObject(window.semaClick, 0);
332  WaitForSingleObject(window.semaClickUp, 0); //to erase previous events
333  WaitForSingleObject(window.semaClick, INFINITE);
334  ret = true;
335  }
336  else {
337  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
338  }
339 
340  return ret;
341 }
342 
343 
360 bool vpDisplayWin32::getClick(vpImagePoint &ip, bool blocking)
361 {
362  //wait if the window is not initialized
363  waitForInit();
364 
365  bool ret = false ;
366  double u, v;
367  //tells the window there has been a getclick demand
368 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
369  //waits for a click
370  if(blocking){
371  WaitForSingleObject(window.semaClick, 0);
372  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
373  WaitForSingleObject(window.semaClick, INFINITE);
374  ret = true;
375  }
376  else {
377  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
378  }
379 
380  u = window.clickX;
381  v = window.clickY;
382  ip.set_u( u );
383  ip.set_v( v );
384 
385  return ret;
386 }
387 
388 
409  bool blocking)
410 {
411  //wait if the window is not initialized
412  waitForInit();
413  bool ret = false;
414  double u, v;
415  //tells the window there has been a getclickup demand
416 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
417  //waits for a click
418  if(blocking){
419  WaitForSingleObject(window.semaClick, 0);
420  WaitForSingleObject(window.semaClickUp, 0);//to erase previous events
421  WaitForSingleObject(window.semaClick, INFINITE);
422  ret = true;
423  }
424  else
425  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, 0));
426 
427  u = window.clickX;
428  v = window.clickY;
429  ip.set_u( u );
430  ip.set_v( v );
431  button = window.clickButton;
432 
433  return ret;
434 }
435 
436 
461  bool blocking)
462 {
463  //wait if the window is not initialized
464  waitForInit();
465  bool ret = false;
466  double u, v;
467  //tells the window there has been a getclickup demand
468 // PostMessage(window.getHWnd(), vpWM_GETCLICKUP, 0,0);
469 
470  //waits for a click release
471  if(blocking){
472  WaitForSingleObject(window.semaClickUp, 0);
473  WaitForSingleObject(window.semaClick, 0);//to erase previous events
474  WaitForSingleObject(window.semaClickUp, INFINITE);
475  ret = true;
476  }
477  else
478  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClickUp, 0));
479 
480  u = window.clickXUp;
481  v = window.clickYUp;
482  ip.set_u( u );
483  ip.set_v( v );
484  button = window.clickButtonUp;
485 
486  return ret;
487 }
488 
504 bool vpDisplayWin32::getKeyboardEvent( bool blocking )
505 {
506  //wait if the window is not initialized
507  waitForInit();
508 
509  bool ret = false ;
510  //waits for a keyboard event
511  if(blocking){
512  WaitForSingleObject(window.semaKey, 0); // key down
513  WaitForSingleObject(window.semaKey, 0); // key up
514  WaitForSingleObject(window.semaKey, INFINITE);
515  ret = true;
516  }
517  else
518  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
519 
520  return ret;
521 }
541 bool vpDisplayWin32::getKeyboardEvent(char *string, bool blocking)
542 {
543  //wait if the window is not initialized
544  waitForInit();
545 
546  bool ret = false ;
547  //waits for a keyboard event
548  if(blocking){
549  WaitForSingleObject(window.semaKey, 0); // key down
550  WaitForSingleObject(window.semaKey, 0); // key up
551  WaitForSingleObject(window.semaKey, INFINITE);
552  ret = true;
553  }
554  else {
555  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, 0));
556  }
557  // printf("key: %ud\n", window.key);
558  sprintf(string, "%s", window.lpString);
559 
560  return ret;
561 }
572 bool
574 {
575  //wait if the window is not initialized
576  waitForInit();
577 
578  bool ret = false;
579 
580  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaMove, 0));
581  if (ret)
582  {
583  double u, v;
584  std::cout << "toto";
585  //tells the window there has been a getclick demand
586  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
587 
588  u = window.coordX;
589  v = window.coordY;
590  ip.set_u( u );
591  ip.set_v( v );
592  }
593 
594  return ret;
595 }
596 
607 bool
609 {
610  //wait if the window is not initialized
611  waitForInit();
612 
613  bool ret = true ;
614  double u, v;
615  //tells the window there has been a getclick demand
616  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
617 
618  u = window.coordX;
619  v = window.coordY;
620  ip.set_u( u );
621  ip.set_v( v );
622 
623  return ret;
624 }
625 
632 void vpDisplayWin32::setWindowPosition(int winx, int winy)
633 {
634  //wait if the window is not initialized
635  waitForInit();
636 
637  //cahange the window position only
638  SetWindowPos(window.hWnd,HWND_TOP, winx, winy, 0, 0,
639  SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOZORDER |SWP_NOSIZE);
640 
641 }
642 
643 
649 void vpDisplayWin32::setTitle(const char *windowtitle)
650 {
651  //wait if the window is not initialized
652  waitForInit();
653  SetWindowText(window.hWnd, windowtitle);
654 }
655 
656 
662 void vpDisplayWin32::setFont(const char * /* fontname */)
663 {
664  vpERROR_TRACE("Not yet implemented" ) ;
665 }
666 
667 
674 {
675  //waits if the window is not initialized
676  waitForInit();
677 
678  //sends a message to the window
679  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
680 }
681 
687 void vpDisplayWin32::flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
688 {
689  //waits if the window is not initialized
690  waitForInit();
691 
692  /*
693  Under windows, flushing an ROI takes more time than
694  flushing the whole image.
695  Therefore, we update the maximum area even when asked to update a region.
696  */
697 #ifdef FLUSH_ROI
698  typedef struct _half_rect_t{
699  unsigned short left_top;
700  unsigned short right_bottom;
701  } half_rect_t;
702 
703  half_rect_t hr1;
704  half_rect_t hr2;
705 
706  hr1.left_top = (unsigned short)iP.get_u();
707  hr1.right_bottom = (unsigned short)(iP.get_u()+width-1);
708 
709  hr2.left_top = (unsigned short)iP.get_v();
710  hr2.right_bottom = (unsigned short)(iP.get_v()+height-1);
711 
712  //sends a message to the window
713 # if 1 // new version FS
714  WPARAM wp = (hr1.left_top << sizeof(unsigned short)) + hr1.right_bottom;
715  LPARAM lp = (hr2.left_top << sizeof(unsigned short)) + hr2.right_bottom;
716 # else // produce warnings with MinGW
717  WPARAM wp=*((WPARAM*)(&hr1));
718  LPARAM lp=*((WPARAM*)(&hr2));
719 # endif
720  PostMessage(window.getHWnd(), vpWM_DISPLAY_ROI, wp,lp);
721 #else
722  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
723 #endif
724 }
725 
726 
733  const vpColor &color )
734 {
735  //wait if the window is not initialized
736  waitForInit();
737  window.renderer->setPixel(ip, color);
738 }
739 
747  const vpImagePoint &ip2,
748  const vpColor &color,
749  unsigned int thickness )
750 {
751  //wait if the window is not initialized
752  waitForInit();
753  window.renderer->drawLine(ip1, ip2, color, thickness);
754 }
755 
756 
767  const vpImagePoint &ip2,
768  const vpColor &color,
769  unsigned int thickness )
770 {
771  //wait if the window is not initialized
772  waitForInit();
773  window.renderer->drawLine(ip1,ip2,color,thickness,PS_DASHDOT);
774 }
775 
790  unsigned int width, unsigned int height,
791  const vpColor &color, bool fill,
792  unsigned int thickness )
793 {
794  //wait if the window is not initialized
795  waitForInit();
796  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
797 }
798 
799 
813  const vpImagePoint &bottomRight,
814  const vpColor &color, bool fill,
815  unsigned int thickness )
816 {
817  //wait if the window is not initialized
818  waitForInit();
819  unsigned int width = static_cast<unsigned int>( bottomRight.get_j() - topLeft.get_j() );
820  unsigned int height = static_cast<unsigned int>(bottomRight.get_i() - topLeft.get_i() );
821  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
822 }
823 
836  const vpColor &color, bool fill,
837  unsigned int thickness )
838 {
839  //wait if the window is not initialized
840  waitForInit();
841  vpImagePoint topLeft;
842  topLeft.set_i(rectangle.getTop());
843  topLeft.set_j(rectangle.getLeft());
844  window.renderer->drawRect(topLeft,
845  static_cast<unsigned int>( rectangle.getWidth() ),
846  static_cast<unsigned int>( rectangle.getHeight() ),
847  color, fill, thickness);
848 }
849 
850 
861  unsigned int radius,
862  const vpColor &color,
863  bool fill,
864  unsigned int thickness )
865 {
866  //wait if the window is not initialized
867  waitForInit();
868  window.renderer->drawCircle(center,radius,color,fill,thickness);
869 }
870 
878  const char *text,
879  const vpColor &color )
880 {
881  //wait if the window is not initialized
882  waitForInit();
883  window.renderer->drawText(ip,text,color);
884 }
885 
894  unsigned int size,
895  const vpColor &color,
896  unsigned int thickness)
897 {
898  //wait if the window is not initialized
899  waitForInit();
900  window.renderer->drawCross(ip, size, color, thickness);
901 }
902 
903 
912  const vpImagePoint &ip2,
913  const vpColor &color,
914  unsigned int w,unsigned int h,
915  unsigned int thickness)
916 
917 {
918  //wait if the window is not initialized
919  waitForInit();
920  window.renderer->drawArrow(ip1, ip2, color, w, h, thickness);
921 }
922 
923 
929  //wait if the window is not initialized
930  waitForInit();
931  window.renderer->clear(color);
932 }
933 
934 
940 {
942  waitForInit();
943  PostMessage(window.getHWnd(), vpWM_CLOSEDISPLAY, 0,0);
944  //if the destructor is called for a reason different than a
945  //problem in the thread creation
946  if (iStatus) {
947  //waits for the thread to end
948  WaitForSingleObject(hThread, INFINITE);
949  CloseHandle(hThread);
950  }
951  displayHasBeenInitialized = false ;
952  window.initialized = false ;
953  }
954  if (this->title != NULL) {
955  delete [] this->title;
956  this->title = NULL;
957  }
958 }
959 
965 {
966  //wait if the window is not initialized
967  waitForInit();
968  window.renderer->getImage(I);
969 }
970 
971 #endif
void set_j(const double j)
Definition: vpImagePoint.h:156
vpDisplay * display
Definition: vpImage.h:116
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:169
void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color=vpColor::green)
double get_v() const
Definition: vpImagePoint.h:250
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:179
double get_i() const
Definition: vpImagePoint.h:181
unsigned int getWidth() const
Definition: vpImage.h:154
virtual ~vpDisplayWin32()
bool getClick(bool blocking=true)
#define vpERROR_TRACE
Definition: vpDebug.h:379
Class to define colors available for display functionnalities.
Definition: vpColor.h:123
double get_u() const
Definition: vpImagePoint.h:239
void setFont(const char *fontname)
Set the font used to display text.
void set_i(const double i)
Definition: vpImagePoint.h:145
double getHeight() const
Definition: vpRect.h:150
char * title
display title
Definition: vpDisplay.h:177
bool displayHasBeenInitialized
display has been initialized
Definition: vpDisplay.h:171
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:192
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)
double getWidth() const
Definition: vpRect.h:188
void displayImage(const vpImage< vpRGBa > &I)
void set_u(const double u)
Definition: vpImagePoint.h:203
unsigned int height
Definition: vpDisplay.h:180
void set_v(const double v)
Definition: vpImagePoint.h:214
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.
bool getPointerMotionEvent(vpImagePoint &ip)
Error that can be emited by the vpDisplay class and its derivates.
HANDLE hThread
Handle of the window's thread.
unsigned int getHeight() const
Definition: vpImage.h:145
Defines a rectangle in the plane.
Definition: vpRect.h:82
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 ...
void setWindowPosition(int winx, int winy)
double getLeft() const
Definition: vpRect.h:156
bool getPointerPosition(vpImagePoint &ip)
void getImage(vpImage< vpRGBa > &I)