ViSP
testDisplays.cpp
1 /****************************************************************************
2  *
3  * $Id: testDisplays.cpp 5126 2015-01-05 22:07:11Z 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  * Test for image display.
36  *
37  * Authors:
38  * Anthony Saunier
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 #include <visp/vpDebug.h>
44 
45 #include <stdlib.h>
46 #include <iostream>
47 #include <string>
48 
49 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV))
50 
51 #include <visp/vpImage.h>
52 #include <visp/vpImageIo.h>
53 #include <visp/vpParseArgv.h>
54 #include <visp/vpIoTools.h>
55 #include <visp/vpRect.h>
56 
57 #include <visp/vpDisplayOpenCV.h>
58 #include <visp/vpDisplayGTK.h>
59 #include <visp/vpDisplayX.h>
60 #include <visp/vpDisplayGDI.h>
61 #include <visp/vpDisplayD3D.h>
62 
70 // List of allowed command line options
71 #define GETOPTARGS "hldc"
72 
73 void usage(const char *name, const char *badparam);
74 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display);
75 void draw(vpImage<vpRGBa> &I);
76 
85 void usage(const char *name, const char *badparam)
86 {
87  fprintf(stdout, "\n\
88 Test video devices or display.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
92 ", name);
93 
94  std::string display;
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -c\n\
99  Disable the mouse click. Useful to automaze the \n\
100  execution of this program without humain intervention.\n\
101 \n\
102  -d \n\
103  Turn off the display.\n\
104 \n\
105  -l\n\
106  Print the list of video-devices available and exit.\n\
107 \n\
108  -h\n\
109  Print the help.\n\n");
110 
111  if (badparam)
112  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
113 }
114 
127 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display)
128 {
129  const char *optarg_;
130  int c;
131  std::string sDisplayType;
132  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
133 
134  switch (c) {
135  case 'l': list = true; break;
136  case 'h': usage(argv[0], NULL); return false; break;
137  case 'c': click_allowed = false; break;
138  case 'd': display = false; break;
139 
140  default:
141  usage(argv[0], optarg_); return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 
157 void draw(vpImage<vpRGBa> &I)
158 {
159  vpImagePoint iP1, iP2;
160  unsigned int w,h;
161 
162  iP1.set_i(20);
163  iP1.set_j(10);
164  iP2.set_i(20);
165  iP2.set_j(30);
166  vpDisplay::displayArrow (I, iP1, iP2, vpColor::green, 4, 2, 3);
167 
168  iP1.set_i(20);
169  iP1.set_j(60);
170  vpDisplay::displayText (I, iP1, "Test...", vpColor::black);
171 
172  iP1.set_i(80);
173  iP1.set_j(220);
174  iP2.set_i(80);
175  iP2.set_j(480);
176  vpDisplay::displayCircle (I, iP1, 30, vpColor::red, false, 3);
177  vpDisplay::displayCircle (I, iP2, 30, vpColor::red, true, 3);
178 
179  iP1.set_i(20);
180  iP1.set_j(220);
181  vpDisplay::displayCross (I, iP1, 5, vpColor::blue, 1);
182 
183  iP1.set_i(140);
184  iP1.set_j(10);
185  iP2.set_i(140);
186  iP2.set_j(50);
187  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
188 
189  iP1.set_i(120);
190  iP1.set_j(180);
191  iP2.set_i(160);
192  iP2.set_j(250);
193  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
194 
195  iP1.set_i(160);
196  iP1.set_j(280);
197  iP2.set_i(120);
198  iP2.set_j(340);
199  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
200 
201  //static void displayFrame (const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, vpColor color)
202 
203  iP1.set_i(140);
204  iP1.set_j(80);
205  iP2.set_i(140);
206  iP2.set_j(150);
207  vpDisplay::displayLine (I, iP1, iP2, vpColor::orange, 3);
208 
209  iP1.set_i(140);
210  iP1.set_j(400);
212 
213  iP1.set_i(350);
214  iP1.set_j(20);
215  w = 60;
216  h = 50;
217  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, false, 3);
218 
219  iP1.set_i(350);
220  iP1.set_j(110);
221  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, true, 3);
222 
223  iP1.set_i(350);
224  iP1.set_j(200);
225  iP2.set_i(400);
226  iP2.set_j(260);
227  vpDisplay::displayRectangle (I, iP1, iP2, vpColor::orange, false, 3);
228 
229  iP1.set_i(350);
230  iP1.set_j(290);
231  iP2.set_i(400);
232  iP2.set_j(350);
233  vpRect rectangle(iP1, iP2);
234  vpDisplay::displayRectangle (I, rectangle, vpColor::yellow, false, 3);
235 
236  iP1.set_i(380);
237  iP1.set_j(400);
238  vpDisplay::displayRectangle (I, iP1, 45, w, h, vpColor::green, 3);
239 }
240 
241 int
242 main(int argc, const char ** argv)
243 {
244  try{
245  bool opt_list = false; // To print the list of video devices
246  bool opt_click_allowed = true;
247  bool opt_display = true;
248 
249 
250  // Read the command line options
251  if (getOptions(argc, argv, opt_list,
252  opt_click_allowed, opt_display) == false) {
253  exit (-1);
254  }
255 
256  // Print the list of video-devices available
257  if (opt_list) {
258  unsigned nbDevices = 0;
259  std::cout << "List of video-devices available: \n";
260 #if defined VISP_HAVE_GTK
261  std::cout << " GTK\n";
262  nbDevices ++;
263 #endif
264 #if defined VISP_HAVE_X11
265  std::cout << " X11\n";
266  nbDevices ++;
267 #endif
268 #if defined VISP_HAVE_GDI
269  std::cout << " GDI\n";
270  nbDevices ++;
271 #endif
272 #if defined VISP_HAVE_D3D9
273  std::cout << " D3D\n";
274  nbDevices ++;
275 #endif
276 #if defined VISP_HAVE_OPENCV
277  std::cout << " OpenCV\n";
278  nbDevices ++;
279 #endif
280  if (!nbDevices) {
281  std::cout << " No display is available\n";
282  }
283  return (0);
284  }
285 
286  // Create a grey level image
288  // Create a color image
289  vpImage<vpRGBa> Irgba;
290 
291  // Create a color image for each display.
292  vpImage<vpRGBa> Ix;
293  vpImage<vpRGBa> Igtk;
294  vpImage<vpRGBa> Icv;
295  vpImage<vpRGBa> Igdi;
296  vpImage<vpRGBa> Id3d;
297 
298 #if defined VISP_HAVE_X11
299  vpDisplayX *displayX = NULL;
300  displayX = new vpDisplayX;
301  Ix.init(480, 640, 255);
302  if (opt_display)
303  {
304  displayX->init(Ix, 100, 100,"Display X11") ;
305  vpDisplay::display(Ix) ;
306  draw(Ix);
307  vpDisplay::flush(Ix);
308  if (opt_click_allowed)
310  }
311 #endif
312 
313 #if defined(VISP_HAVE_OPENCV)
314  vpDisplayOpenCV *displayCv = NULL;
315  displayCv = new vpDisplayOpenCV;
316  Icv.init(480, 640, 255);
317  if (opt_display)
318  {
319  displayCv->init(Icv, 100, 100,"Display OpenCV") ;
320  vpDisplay::display(Icv) ;
321  draw(Icv);
322  vpDisplay::flush(Icv);
323  if (opt_click_allowed)
324  vpDisplay::getClick(Icv);
325  }
326 #endif
327 
328 #if defined VISP_HAVE_GTK
329  vpDisplayGTK *displayGtk = NULL;
330  displayGtk = new vpDisplayGTK;
331  Igtk.init(480, 640, 255);
332  if (opt_display)
333  {
334  displayGtk->init(Igtk, 100, 100,"Display GTK") ;
335  vpDisplay::display(Igtk) ;
336  draw(Igtk);
337  vpDisplay::flush(Igtk);
338  if (opt_click_allowed)
339  vpDisplay::getClick(Igtk);
340  }
341 #endif
342 
343 #if defined VISP_HAVE_GDI
344  vpDisplayGDI *displayGdi = NULL;
345  displayGdi = new vpDisplayGDI;
346  Igdi.init(480, 640, 255);
347  if (opt_display)
348  {
349  displayGdi->init(Igdi, 100, 100,"Display GDI") ;
350  vpDisplay::display(Igdi) ;
351  draw(Igdi);
352  vpDisplay::flush(Igdi);
353  if (opt_click_allowed)
354  vpDisplay::getClick(Igdi);
355  }
356 #endif
357 
358 #if defined VISP_HAVE_D3D9
359  vpDisplayD3D *displayD3d = NULL;
360  displayD3d = new vpDisplayD3D;
361  Id3d.init(480, 640, 255);
362  if (opt_display)
363  {
364  displayD3d->init(Id3d, 100, 100,"Display Direct 3D") ;
365  vpDisplay::display(Id3d) ;
366  draw(Id3d);
367  vpDisplay::flush(Id3d);
368  if (opt_click_allowed)
369  vpDisplay::getClick(Id3d);
370  }
371 #endif
372 
373 
374 
375 #if defined VISP_HAVE_X11
376  delete displayX;
377 #endif
378 
379 #if defined VISP_HAVE_GTK
380  delete displayGtk;
381 #endif
382 
383 #if defined(VISP_HAVE_OPENCV)
384  delete displayCv;
385 #endif
386 
387 #if defined VISP_HAVE_GDI
388  delete displayGdi;
389 #endif
390 
391 #if defined VISP_HAVE_D3D9
392  delete displayD3d;
393 #endif
394  }
395  catch(...) {
396  vpERROR_TRACE("Error while displaying the image") ;
397  exit(-1);
398  }
399 }
400 
401 #else
402 int
403 main()
404 {
405  vpERROR_TRACE("You do not have display functionalities...");
406 }
407 
408 #endif
virtual void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
virtual 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)=0
void init(unsigned int height, unsigned int width)
set the size of the image
Definition: vpImage.h:386
#define vpERROR_TRACE
Definition: vpDebug.h:395
static const vpColor black
Definition: vpColor.h:161
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void set_i(const double ii)
Definition: vpImagePoint.h:159
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
The vpDisplayOpenCV allows to display image using the opencv library.
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
virtual void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void set_j(const double jj)
Definition: vpImagePoint.h:170
Defines a rectangle in the plane.
Definition: vpRect.h:85
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
Definition: vpDisplayX.cpp:194
static const vpColor yellow
Definition: vpColor.h:175
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
static const vpColor blue
Definition: vpColor.h:173