Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testDisplays.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 [-l] [-c] [-d] [-h]\n\
88 ", name);
89 
90  fprintf(stdout, "\n\
91 OPTIONS: Default\n\
92  -c\n\
93  Disable the mouse click. Useful to automaze the \n\
94  execution of this program without humain intervention.\n\
95 \n\
96  -d \n\
97  Turn off the display.\n\
98 \n\
99  -l\n\
100  Print the list of video-devices available and exit.\n\
101 \n\
102  -h\n\
103  Print the help.\n\n");
104 
105  if (badparam)
106  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107 }
108 
121 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display)
122 {
123  const char *optarg_;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126 
127  switch (c) {
128  case 'l': list = true; break;
129  case 'h': usage(argv[0], NULL); return false; break;
130  case 'c': click_allowed = false; break;
131  case 'd': display = false; break;
132 
133  default:
134  usage(argv[0], optarg_); return false; break;
135  }
136  }
137 
138  if ((c == 1) || (c == -1)) {
139  // standalone param or error
140  usage(argv[0], NULL);
141  std::cerr << "ERROR: " << std::endl;
142  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143  return false;
144  }
145 
146  return true;
147 }
148 
149 
150 void draw(vpImage<vpRGBa> &I)
151 {
152  vpImagePoint iP1, iP2;
153  unsigned int w,h;
154 
155  iP1.set_i(20);
156  iP1.set_j(10);
157  iP2.set_i(20);
158  iP2.set_j(30);
159  vpDisplay::displayArrow (I, iP1, iP2, vpColor::green, 4, 2, 3);
160 
161  iP1.set_i(20);
162  iP1.set_j(60);
163  vpDisplay::displayText (I, iP1, "Test...", vpColor::black);
164 
165  iP1.set_i(80);
166  iP1.set_j(220);
167  iP2.set_i(80);
168  iP2.set_j(480);
169  vpDisplay::displayCircle (I, iP1, 30, vpColor::red, false, 3);
170  vpDisplay::displayCircle (I, iP2, 30, vpColor::red, true, 3);
171 
172  iP1.set_i(20);
173  iP1.set_j(220);
174  vpDisplay::displayCross (I, iP1, 5, vpColor::blue, 1);
175 
176  iP1.set_i(140);
177  iP1.set_j(10);
178  iP2.set_i(140);
179  iP2.set_j(50);
180  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
181 
182  iP1.set_i(120);
183  iP1.set_j(180);
184  iP2.set_i(160);
185  iP2.set_j(250);
186  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
187 
188  iP1.set_i(160);
189  iP1.set_j(280);
190  iP2.set_i(120);
191  iP2.set_j(340);
192  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
193 
194  //static void displayFrame (const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, vpColor color)
195 
196  iP1.set_i(140);
197  iP1.set_j(80);
198  iP2.set_i(140);
199  iP2.set_j(150);
200  vpDisplay::displayLine (I, iP1, iP2, vpColor::orange, 3);
201 
202  iP1.set_i(140);
203  iP1.set_j(400);
205 
206  iP1.set_i(350);
207  iP1.set_j(20);
208  w = 60;
209  h = 50;
210  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, false, 3);
211 
212  iP1.set_i(350);
213  iP1.set_j(110);
214  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, true, 3);
215 
216  iP1.set_i(350);
217  iP1.set_j(200);
218  iP2.set_i(400);
219  iP2.set_j(260);
220  vpDisplay::displayRectangle (I, iP1, iP2, vpColor::orange, false, 3);
221 
222  iP1.set_i(350);
223  iP1.set_j(290);
224  iP2.set_i(400);
225  iP2.set_j(350);
226  vpRect rectangle(iP1, iP2);
227  vpDisplay::displayRectangle (I, rectangle, vpColor::yellow, false, 3);
228 
229  iP1.set_i(380);
230  iP1.set_j(400);
231  vpDisplay::displayRectangle (I, iP1, 45, w, h, vpColor::green, 3);
232 }
233 
234 int
235 main(int argc, const char ** argv)
236 {
237  try{
238  bool opt_list = false; // To print the list of video devices
239  bool opt_click_allowed = true;
240  bool opt_display = true;
241 
242  // Read the command line options
243  if (getOptions(argc, argv, opt_list,
244  opt_click_allowed, opt_display) == false) {
245  exit (-1);
246  }
247 
248  // Print the list of video-devices available
249  if (opt_list) {
250  unsigned nbDevices = 0;
251  std::cout << "List of video-devices available: \n";
252 #if defined VISP_HAVE_GTK
253  std::cout << " GTK\n";
254  nbDevices ++;
255 #endif
256 #if defined VISP_HAVE_X11
257  std::cout << " X11\n";
258  nbDevices ++;
259 #endif
260 #if defined VISP_HAVE_GDI
261  std::cout << " GDI\n";
262  nbDevices ++;
263 #endif
264 #if defined VISP_HAVE_D3D9
265  std::cout << " D3D\n";
266  nbDevices ++;
267 #endif
268 #if defined VISP_HAVE_OPENCV
269  std::cout << " OpenCV\n";
270  nbDevices ++;
271 #endif
272  if (!nbDevices) {
273  std::cout << " No display is available\n";
274  }
275  return (0);
276  }
277 
278  // Create a grey level image
280  // Create a color image
281  vpImage<vpRGBa> Irgba;
282 
283  // Create a color image for each display.
284  vpImage<vpRGBa> Ix;
285  vpImage<vpRGBa> Igtk;
286  vpImage<vpRGBa> Icv;
287  vpImage<vpRGBa> Igdi;
288  vpImage<vpRGBa> Id3d;
289 
290 #if defined VISP_HAVE_X11
291  vpDisplayX *displayX = NULL;
292  displayX = new vpDisplayX;
293  Ix.init(480, 640, 255);
294  if (opt_display)
295  {
296  displayX->init(Ix, 100, 100,"Display X11") ;
297  vpDisplay::display(Ix) ;
298  draw(Ix);
299  vpDisplay::flush(Ix);
300  if (opt_click_allowed)
302  }
303 #endif
304 
305 #if defined(VISP_HAVE_OPENCV)
306  vpDisplayOpenCV *displayCv = NULL;
307  displayCv = new vpDisplayOpenCV;
308  Icv.init(480, 640, 255);
309  if (opt_display)
310  {
311  displayCv->init(Icv, 100, 100,"Display OpenCV") ;
312  vpDisplay::display(Icv) ;
313  draw(Icv);
314  vpDisplay::flush(Icv);
315  if (opt_click_allowed)
316  vpDisplay::getClick(Icv);
317  }
318 #endif
319 
320 #if defined VISP_HAVE_GTK
321  vpDisplayGTK *displayGtk = NULL;
322  displayGtk = new vpDisplayGTK;
323  Igtk.init(480, 640, 255);
324  if (opt_display)
325  {
326  displayGtk->init(Igtk, 100, 100,"Display GTK") ;
327  vpDisplay::display(Igtk) ;
328  draw(Igtk);
329  vpDisplay::flush(Igtk);
330  if (opt_click_allowed)
331  vpDisplay::getClick(Igtk);
332  }
333 #endif
334 
335 #if defined VISP_HAVE_GDI
336  vpDisplayGDI *displayGdi = NULL;
337  displayGdi = new vpDisplayGDI;
338  Igdi.init(480, 640, 255);
339  if (opt_display)
340  {
341  displayGdi->init(Igdi, 100, 100,"Display GDI") ;
342  vpDisplay::display(Igdi) ;
343  draw(Igdi);
344  vpDisplay::flush(Igdi);
345  if (opt_click_allowed)
346  vpDisplay::getClick(Igdi);
347  }
348 #endif
349 
350 #if defined VISP_HAVE_D3D9
351  vpDisplayD3D *displayD3d = NULL;
352  displayD3d = new vpDisplayD3D;
353  Id3d.init(480, 640, 255);
354  if (opt_display)
355  {
356  displayD3d->init(Id3d, 100, 100,"Display Direct 3D") ;
357  vpDisplay::display(Id3d) ;
358  draw(Id3d);
359  vpDisplay::flush(Id3d);
360  if (opt_click_allowed)
361  vpDisplay::getClick(Id3d);
362  }
363 #endif
364 
365 #if defined VISP_HAVE_X11
366  delete displayX;
367 #endif
368 
369 #if defined VISP_HAVE_GTK
370  delete displayGtk;
371 #endif
372 
373 #if defined(VISP_HAVE_OPENCV)
374  delete displayCv;
375 #endif
376 
377 #if defined VISP_HAVE_GDI
378  delete displayGdi;
379 #endif
380 
381 #if defined VISP_HAVE_D3D9
382  delete displayD3d;
383 #endif
384  }
385  catch(const vpException &e) {
386  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
387  return(-1);
388  }
389 }
390 
391 #else
392 int
393 main()
394 {
395  std::cout << "You do not have display functionalities..." << std::endl;
396 }
397 
398 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:254
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:664
static const vpColor black
Definition: vpColor.h:157
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)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
error that can be emited by ViSP classes.
Definition: vpException.h:73
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
static const vpColor green
Definition: vpColor.h:166
static void flush(const vpImage< unsigned char > &I)
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 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
void set_i(const double ii)
Definition: vpImagePoint.h:163
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
const char * getMessage(void) const
Definition: vpException.cpp:97
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
void set_j(const double jj)
Definition: vpImagePoint.h:174
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Defines a rectangle in the plane.
Definition: vpRect.h:82
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor yellow
Definition: vpColor.h:171
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor blue
Definition: vpColor.h:169