Visual Servoing Platform  version 3.0.0
testDisplays.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test for image display.
32  *
33  * Authors:
34  * Anthony Saunier
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpConfig.h>
39 #include <visp3/core/vpDebug.h>
40 
41 #include <stdlib.h>
42 #include <iostream>
43 #include <string>
44 
45 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV))
46 
47 #include <visp3/core/vpImage.h>
48 #include <visp3/io/vpImageIo.h>
49 #include <visp3/io/vpParseArgv.h>
50 #include <visp3/core/vpIoTools.h>
51 #include <visp3/core/vpRect.h>
52 
53 #include <visp3/gui/vpDisplayOpenCV.h>
54 #include <visp3/gui/vpDisplayGTK.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayD3D.h>
58 
66 // List of allowed command line options
67 #define GETOPTARGS "hldc"
68 
69 void usage(const char *name, const char *badparam);
70 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display);
71 void draw(vpImage<vpRGBa> &I);
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Test video devices or display.\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
88 ", name);
89 
90  std::string display;
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -c\n\
95  Disable the mouse click. Useful to automaze the \n\
96  execution of this program without humain intervention.\n\
97 \n\
98  -d \n\
99  Turn off the display.\n\
100 \n\
101  -l\n\
102  Print the list of video-devices available and exit.\n\
103 \n\
104  -h\n\
105  Print the help.\n\n");
106 
107  if (badparam)
108  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
109 }
110 
123 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display)
124 {
125  const char *optarg_;
126  int c;
127  std::string sDisplayType;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
129 
130  switch (c) {
131  case 'l': list = true; break;
132  case 'h': usage(argv[0], NULL); return false; break;
133  case 'c': click_allowed = false; break;
134  case 'd': display = false; break;
135 
136  default:
137  usage(argv[0], optarg_); return false; break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], NULL);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 
153 void draw(vpImage<vpRGBa> &I)
154 {
155  vpImagePoint iP1, iP2;
156  unsigned int w,h;
157 
158  iP1.set_i(20);
159  iP1.set_j(10);
160  iP2.set_i(20);
161  iP2.set_j(30);
162  vpDisplay::displayArrow (I, iP1, iP2, vpColor::green, 4, 2, 3);
163 
164  iP1.set_i(20);
165  iP1.set_j(60);
166  vpDisplay::displayText (I, iP1, "Test...", vpColor::black);
167 
168  iP1.set_i(80);
169  iP1.set_j(220);
170  iP2.set_i(80);
171  iP2.set_j(480);
172  vpDisplay::displayCircle (I, iP1, 30, vpColor::red, false, 3);
173  vpDisplay::displayCircle (I, iP2, 30, vpColor::red, true, 3);
174 
175  iP1.set_i(20);
176  iP1.set_j(220);
177  vpDisplay::displayCross (I, iP1, 5, vpColor::blue, 1);
178 
179  iP1.set_i(140);
180  iP1.set_j(10);
181  iP2.set_i(140);
182  iP2.set_j(50);
183  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
184 
185  iP1.set_i(120);
186  iP1.set_j(180);
187  iP2.set_i(160);
188  iP2.set_j(250);
189  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
190 
191  iP1.set_i(160);
192  iP1.set_j(280);
193  iP2.set_i(120);
194  iP2.set_j(340);
195  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
196 
197  //static void displayFrame (const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, vpColor color)
198 
199  iP1.set_i(140);
200  iP1.set_j(80);
201  iP2.set_i(140);
202  iP2.set_j(150);
203  vpDisplay::displayLine (I, iP1, iP2, vpColor::orange, 3);
204 
205  iP1.set_i(140);
206  iP1.set_j(400);
208 
209  iP1.set_i(350);
210  iP1.set_j(20);
211  w = 60;
212  h = 50;
213  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, false, 3);
214 
215  iP1.set_i(350);
216  iP1.set_j(110);
217  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, true, 3);
218 
219  iP1.set_i(350);
220  iP1.set_j(200);
221  iP2.set_i(400);
222  iP2.set_j(260);
223  vpDisplay::displayRectangle (I, iP1, iP2, vpColor::orange, false, 3);
224 
225  iP1.set_i(350);
226  iP1.set_j(290);
227  iP2.set_i(400);
228  iP2.set_j(350);
229  vpRect rectangle(iP1, iP2);
230  vpDisplay::displayRectangle (I, rectangle, vpColor::yellow, false, 3);
231 
232  iP1.set_i(380);
233  iP1.set_j(400);
234  vpDisplay::displayRectangle (I, iP1, 45, w, h, vpColor::green, 3);
235 }
236 
237 int
238 main(int argc, const char ** argv)
239 {
240  try{
241  bool opt_list = false; // To print the list of video devices
242  bool opt_click_allowed = true;
243  bool opt_display = true;
244 
245 
246  // Read the command line options
247  if (getOptions(argc, argv, opt_list,
248  opt_click_allowed, opt_display) == false) {
249  exit (-1);
250  }
251 
252  // Print the list of video-devices available
253  if (opt_list) {
254  unsigned nbDevices = 0;
255  std::cout << "List of video-devices available: \n";
256 #if defined VISP_HAVE_GTK
257  std::cout << " GTK\n";
258  nbDevices ++;
259 #endif
260 #if defined VISP_HAVE_X11
261  std::cout << " X11\n";
262  nbDevices ++;
263 #endif
264 #if defined VISP_HAVE_GDI
265  std::cout << " GDI\n";
266  nbDevices ++;
267 #endif
268 #if defined VISP_HAVE_D3D9
269  std::cout << " D3D\n";
270  nbDevices ++;
271 #endif
272 #if defined VISP_HAVE_OPENCV
273  std::cout << " OpenCV\n";
274  nbDevices ++;
275 #endif
276  if (!nbDevices) {
277  std::cout << " No display is available\n";
278  }
279  return (0);
280  }
281 
282  // Create a grey level image
284  // Create a color image
285  vpImage<vpRGBa> Irgba;
286 
287  // Create a color image for each display.
288  vpImage<vpRGBa> Ix;
289  vpImage<vpRGBa> Igtk;
290  vpImage<vpRGBa> Icv;
291  vpImage<vpRGBa> Igdi;
292  vpImage<vpRGBa> Id3d;
293 
294 #if defined VISP_HAVE_X11
295  vpDisplayX *displayX = NULL;
296  displayX = new vpDisplayX;
297  Ix.init(480, 640, 255);
298  if (opt_display)
299  {
300  displayX->init(Ix, 100, 100,"Display X11") ;
301  vpDisplay::display(Ix) ;
302  draw(Ix);
303  vpDisplay::flush(Ix);
304  if (opt_click_allowed)
306  }
307 #endif
308 
309 #if defined(VISP_HAVE_OPENCV)
310  vpDisplayOpenCV *displayCv = NULL;
311  displayCv = new vpDisplayOpenCV;
312  Icv.init(480, 640, 255);
313  if (opt_display)
314  {
315  displayCv->init(Icv, 100, 100,"Display OpenCV") ;
316  vpDisplay::display(Icv) ;
317  draw(Icv);
318  vpDisplay::flush(Icv);
319  if (opt_click_allowed)
320  vpDisplay::getClick(Icv);
321  }
322 #endif
323 
324 #if defined VISP_HAVE_GTK
325  vpDisplayGTK *displayGtk = NULL;
326  displayGtk = new vpDisplayGTK;
327  Igtk.init(480, 640, 255);
328  if (opt_display)
329  {
330  displayGtk->init(Igtk, 100, 100,"Display GTK") ;
331  vpDisplay::display(Igtk) ;
332  draw(Igtk);
333  vpDisplay::flush(Igtk);
334  if (opt_click_allowed)
335  vpDisplay::getClick(Igtk);
336  }
337 #endif
338 
339 #if defined VISP_HAVE_GDI
340  vpDisplayGDI *displayGdi = NULL;
341  displayGdi = new vpDisplayGDI;
342  Igdi.init(480, 640, 255);
343  if (opt_display)
344  {
345  displayGdi->init(Igdi, 100, 100,"Display GDI") ;
346  vpDisplay::display(Igdi) ;
347  draw(Igdi);
348  vpDisplay::flush(Igdi);
349  if (opt_click_allowed)
350  vpDisplay::getClick(Igdi);
351  }
352 #endif
353 
354 #if defined VISP_HAVE_D3D9
355  vpDisplayD3D *displayD3d = NULL;
356  displayD3d = new vpDisplayD3D;
357  Id3d.init(480, 640, 255);
358  if (opt_display)
359  {
360  displayD3d->init(Id3d, 100, 100,"Display Direct 3D") ;
361  vpDisplay::display(Id3d) ;
362  draw(Id3d);
363  vpDisplay::flush(Id3d);
364  if (opt_click_allowed)
365  vpDisplay::getClick(Id3d);
366  }
367 #endif
368 
369 
370 
371 #if defined VISP_HAVE_X11
372  delete displayX;
373 #endif
374 
375 #if defined VISP_HAVE_GTK
376  delete displayGtk;
377 #endif
378 
379 #if defined(VISP_HAVE_OPENCV)
380  delete displayCv;
381 #endif
382 
383 #if defined VISP_HAVE_GDI
384  delete displayGdi;
385 #endif
386 
387 #if defined VISP_HAVE_D3D9
388  delete displayD3d;
389 #endif
390  }
391  catch(...) {
392  vpERROR_TRACE("Error while displaying the image") ;
393  exit(-1);
394  }
395 }
396 
397 #else
398 int
399 main()
400 {
401  vpERROR_TRACE("You do not have display functionalities...");
402 }
403 
404 #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:377
static const vpColor black
Definition: vpColor.h:157
#define vpERROR_TRACE
Definition: vpDebug.h:391
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
vpDisplayGDI()
Basic constructor.
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static const vpColor red
Definition: vpColor.h:163
static const vpColor orange
Definition: vpColor.h:173
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:105
void set_i(const double ii)
Definition: vpImagePoint.h:154
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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:141
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:165
Defines a rectangle in the plane.
Definition: vpRect.h:81
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:88
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:190
static const vpColor yellow
Definition: vpColor.h:171
virtual void displayPoint(const vpImagePoint &ip, const vpColor &color)=0
static const vpColor blue
Definition: vpColor.h:169