ViSP  2.6.2
vpDisplayWin32.cpp
1 /****************************************************************************
2  *
3  * $Id: vpDisplayWin32.cpp 3682 2012-04-11 14:28:29Z fnovotny $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 #define FLUSH_ROI
44 #if ( defined(WIN32) )
45 
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, NULL);
332  WaitForSingleObject(window.semaClickUp, NULL); //to erase previous events
333  WaitForSingleObject(window.semaClick, INFINITE);
334  ret = true;
335  }
336  else
337  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, NULL));
338 
339  return ret;
340 }
341 
342 
359 bool vpDisplayWin32::getClick(vpImagePoint &ip, bool blocking)
360 {
361  //wait if the window is not initialized
362  waitForInit();
363 
364  bool ret = false ;
365  double u, v;
366  //tells the window there has been a getclick demand
367 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
368  //waits for a click
369  if(blocking){
370  WaitForSingleObject(window.semaClick, NULL);
371  WaitForSingleObject(window.semaClickUp, NULL);//to erase previous events
372  WaitForSingleObject(window.semaClick, INFINITE);
373  ret = true;
374  }
375  else
376  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, NULL));
377 
378  u = window.clickX;
379  v = window.clickY;
380  ip.set_u( u );
381  ip.set_v( v );
382 
383  return ret;
384 }
385 
386 
407  bool blocking)
408 {
409  //wait if the window is not initialized
410  waitForInit();
411  bool ret = false;
412  double u, v;
413  //tells the window there has been a getclickup demand
414 // PostMessage(window.getHWnd(), vpWM_GETCLICK, 0,0);
415  //waits for a click
416  if(blocking){
417  WaitForSingleObject(window.semaClick, NULL);
418  WaitForSingleObject(window.semaClickUp, NULL);//to erase previous events
419  WaitForSingleObject(window.semaClick, INFINITE);
420  ret = true;
421  }
422  else
423  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClick, NULL));
424 
425  u = window.clickX;
426  v = window.clickY;
427  ip.set_u( u );
428  ip.set_v( v );
429  button = window.clickButton;
430 
431  return ret;
432 }
433 
434 
459  bool blocking)
460 {
461  //wait if the window is not initialized
462  waitForInit();
463  bool ret = false;
464  double u, v;
465  //tells the window there has been a getclickup demand
466 // PostMessage(window.getHWnd(), vpWM_GETCLICKUP, 0,0);
467 
468  //waits for a click release
469  if(blocking){
470  WaitForSingleObject(window.semaClickUp, NULL);
471  WaitForSingleObject(window.semaClick, NULL);//to erase previous events
472  WaitForSingleObject(window.semaClickUp, INFINITE);
473  ret = true;
474  }
475  else
476  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaClickUp, NULL));
477 
478  u = window.clickXUp;
479  v = window.clickYUp;
480  ip.set_u( u );
481  ip.set_v( v );
482  button = window.clickButtonUp;
483 
484  return ret;
485 }
486 
502 bool vpDisplayWin32::getKeyboardEvent( bool blocking )
503 {
504  //wait if the window is not initialized
505  waitForInit();
506 
507  bool ret = false ;
508  //waits for a keyboard event
509  if(blocking){
510  WaitForSingleObject(window.semaKey, NULL); // key down
511  WaitForSingleObject(window.semaKey, NULL); // key up
512  WaitForSingleObject(window.semaKey, INFINITE);
513  ret = true;
514  }
515  else
516  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, NULL));
517 
518  return ret;
519 }
539 bool vpDisplayWin32::getKeyboardEvent(char *string, bool blocking)
540 {
541  //wait if the window is not initialized
542  waitForInit();
543 
544  bool ret = false ;
545  //waits for a keyboard event
546  if(blocking){
547  WaitForSingleObject(window.semaKey, NULL); // key down
548  WaitForSingleObject(window.semaKey, NULL); // key up
549  WaitForSingleObject(window.semaKey, INFINITE);
550  ret = true;
551  }
552  else {
553  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaKey, NULL));
554  }
555  // printf("key: %ud\n", window.key);
556  sprintf(string, "%s", window.lpString);
557 
558  return ret;
559 }
570 bool
572 {
573  //wait if the window is not initialized
574  waitForInit();
575 
576  bool ret = false;
577 
578  ret = (WAIT_OBJECT_0 == WaitForSingleObject(window.semaMove, NULL));
579  if (ret)
580  {
581  double u, v;
582  std::cout << "toto";
583  //tells the window there has been a getclick demand
584  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
585 
586  u = window.coordX;
587  v = window.coordY;
588  ip.set_u( u );
589  ip.set_v( v );
590  }
591 
592  return ret;
593 }
594 
605 bool
607 {
608  //wait if the window is not initialized
609  waitForInit();
610 
611  bool ret = true ;
612  double u, v;
613  //tells the window there has been a getclick demand
614  //PostMessage(window.getHWnd(), vpWM_GETPOINTERMOTIONEVENT, 0,0);
615 
616  u = window.coordX;
617  v = window.coordY;
618  ip.set_u( u );
619  ip.set_v( v );
620 
621  return ret;
622 }
623 
630 void vpDisplayWin32::setWindowPosition(int winx, int winy)
631 {
632  //wait if the window is not initialized
633  waitForInit();
634 
635  //cahange the window position only
636  SetWindowPos(window.hWnd,HWND_TOP, winx, winy, 0, 0,
637  SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOZORDER |SWP_NOSIZE);
638 
639 }
640 
641 
647 void vpDisplayWin32::setTitle(const char *windowtitle)
648 {
649  //wait if the window is not initialized
650  waitForInit();
651  SetWindowText(window.hWnd, windowtitle);
652 }
653 
654 
660 void vpDisplayWin32::setFont(const char *fontname)
661 {
662  vpERROR_TRACE("Not yet implemented" ) ;
663 }
664 
665 
672 {
673  //waits if the window is not initialized
674  waitForInit();
675 
676  //sends a message to the window
677  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
678 }
679 
685 void vpDisplayWin32::flushDisplayROI(const vpImagePoint &iP, const unsigned int width, const unsigned int height)
686 {
687  //waits if the window is not initialized
688  waitForInit();
689 
690  /*
691  Under windows, flushing an ROI takes more time than
692  flushing the whole image.
693  Therefore, we update the maximum area even when asked to update a region.
694  */
695 #ifdef FLUSH_ROI
696  typedef struct _half_rect_t{
697  unsigned short left_top;
698  unsigned short right_bottom;
699  } half_rect_t;
700 
701  half_rect_t hr1;
702  half_rect_t hr2;
703 
704  hr1.left_top = (unsigned short)iP.get_u();
705  hr1.right_bottom = (unsigned short)(iP.get_u()+width-1);
706 
707  hr2.left_top = (unsigned short)iP.get_v();
708  hr2.right_bottom = (unsigned short)(iP.get_v()+height-1);
709 
710  //sends a message to the window
711  WPARAM wp=*((WPARAM*)(&hr1));
712  LPARAM lp=*((WPARAM*)(&hr2));
713  PostMessage(window.getHWnd(), vpWM_DISPLAY_ROI, wp,lp);
714 #else
715  PostMessage(window.getHWnd(), vpWM_DISPLAY, 0,0);
716 #endif
717 }
718 
719 
726  const vpColor &color )
727 {
728  //wait if the window is not initialized
729  waitForInit();
730  window.renderer->setPixel(ip, color);
731 }
732 
740  const vpImagePoint &ip2,
741  const vpColor &color,
742  unsigned int thickness )
743 {
744  //wait if the window is not initialized
745  waitForInit();
746  window.renderer->drawLine(ip1, ip2, color, thickness);
747 }
748 
749 
760  const vpImagePoint &ip2,
761  const vpColor &color,
762  unsigned int thickness )
763 {
764  //wait if the window is not initialized
765  waitForInit();
766  window.renderer->drawLine(ip1,ip2,color,thickness,PS_DASHDOT);
767 }
768 
783  unsigned int width, unsigned int height,
784  const vpColor &color, bool fill,
785  unsigned int thickness )
786 {
787  //wait if the window is not initialized
788  waitForInit();
789  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
790 }
791 
792 
806  const vpImagePoint &bottomRight,
807  const vpColor &color, bool fill,
808  unsigned int thickness )
809 {
810  //wait if the window is not initialized
811  waitForInit();
812  unsigned int width = static_cast<unsigned int>( bottomRight.get_j() - topLeft.get_j() );
813  unsigned int height = static_cast<unsigned int>(bottomRight.get_i() - topLeft.get_i() );
814  window.renderer->drawRect(topLeft,width,height,color, fill, thickness);
815 }
816 
829  const vpColor &color, bool fill,
830  unsigned int thickness )
831 {
832  //wait if the window is not initialized
833  waitForInit();
834  vpImagePoint topLeft;
835  topLeft.set_i(rectangle.getTop());
836  topLeft.set_j(rectangle.getLeft());
837  window.renderer->drawRect(topLeft,
838  static_cast<unsigned int>( rectangle.getWidth() ),
839  static_cast<unsigned int>( rectangle.getHeight() ),
840  color, fill, thickness);
841 }
842 
843 
854  unsigned int radius,
855  const vpColor &color,
856  bool fill,
857  unsigned int thickness )
858 {
859  //wait if the window is not initialized
860  waitForInit();
861  window.renderer->drawCircle(center,radius,color,fill,thickness);
862 }
863 
871  const char *text,
872  const vpColor &color )
873 {
874  //wait if the window is not initialized
875  waitForInit();
876  window.renderer->drawText(ip,text,color);
877 }
878 
887  unsigned int size,
888  const vpColor &color,
889  unsigned int thickness)
890 {
891  //wait if the window is not initialized
892  waitForInit();
893  window.renderer->drawCross(ip, size, color, thickness);
894 }
895 
896 
905  const vpImagePoint &ip2,
906  const vpColor &color,
907  unsigned int w,unsigned int h,
908  unsigned int thickness)
909 
910 {
911  //wait if the window is not initialized
912  waitForInit();
913  window.renderer->drawArrow(ip1, ip2, color, w, h, thickness);
914 }
915 
916 
922  //wait if the window is not initialized
923  waitForInit();
924  window.renderer->clear(color);
925 }
926 
927 
933 {
935  waitForInit();
936  PostMessage(window.getHWnd(), vpWM_CLOSEDISPLAY, 0,0);
937  //if the destructor is called for a reason different than a
938  //problem in the thread creation
939  if (iStatus) {
940  //waits for the thread to end
941  WaitForSingleObject(hThread, INFINITE);
942  CloseHandle(hThread);
943  }
944  displayHasBeenInitialized = false ;
945  window.initialized = false ;
946  }
947  if (this->title != NULL) {
948  delete [] this->title;
949  this->title = NULL;
950  }
951 }
952 
958 {
959  //wait if the window is not initialized
960  waitForInit();
961  window.renderer->getImage(I);
962 }
963 
964 #endif
965 
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)