Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testVideoDevice.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 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpDebug.h>
41 
42 #include <stdlib.h>
43 #include <iostream>
44 #include <string>
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 
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayGTK.h>
54 #include <visp3/gui/vpDisplayX.h>
55 #include <visp3/gui/vpDisplayGDI.h>
56 #include <visp3/gui/vpDisplayD3D.h>
57 
65 // List of allowed command line options
66 #define GETOPTARGS "i:hlt:dc"
67 
68 typedef enum {
69  vpX11,
70  vpGTK,
71  vpGDI,
72  vpD3D,
73  vpCV
74 } vpDisplayType;
75 
76 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
77 bool getOptions(int argc, const char **argv,
78  std::string &ipath, vpDisplayType &dtype, bool &list,
79  bool &click_allowed, bool &display );
80 
91 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
92 {
93  fprintf(stdout, "\n\
94 Test video devices or display.\n\
95 \n\
96 SYNOPSIS\n\
97  %s [-i <input image path>] \n\
98  [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
99 ", name);
100 
101  std::string display;
102  switch(dtype) {
103  case vpX11: display = "X11"; break;
104  case vpGTK: display = "GTK"; break;
105  case vpGDI: display = "GDI"; break;
106  case vpD3D: display = "D3D"; break;
107  case vpCV: display = "CV"; break;
108  }
109 
110  fprintf(stdout, "\n\
111 OPTIONS: Default\n\
112  -i <input image path> %s\n\
113  Set image input path.\n\
114  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
115  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
116  Setting the VISP_INPUT_IMAGE_PATH environment\n\
117  variable produces the same behaviour than using\n\
118  this option.\n\
119 \n\
120  -t <type of video device> \"%s\"\n\
121  String specifying the video device to use.\n\
122  Possible values:\n\
123  \"X11\": only on UNIX platforms,\n\
124  \"GTK\": on all plaforms,\n\
125  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
126  \"D3D\": only on Windows platform (Direct3D).\n\
127  \"CV\" : (OpenCV).\n\
128 \n\
129  -c\n\
130  Disable the mouse click. Useful to automaze the \n\
131  execution of this program without humain intervention.\n\
132 \n\
133  -d \n\
134  Turn off the display.\n\
135 \n\
136  -l\n\
137  Print the list of video-devices available and exit.\n\
138 \n\
139  -h\n\
140  Print the help.\n\n",
141  ipath.c_str(), display.c_str());
142 
143  if (badparam)
144  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
145 }
146 
161 bool getOptions(int argc, const char **argv,
162  std::string &ipath, vpDisplayType &dtype, bool &list,
163  bool &click_allowed, bool &display )
164 {
165  const char *optarg_;
166  int c;
167  std::string sDisplayType;
168  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
169 
170  switch (c) {
171  case 'i': ipath = optarg_; break;
172  case 'l': list = true; break;
173  case 't': sDisplayType = optarg_;
174  // Parse the display type option
175  if (sDisplayType.compare("X11") == 0) {
176  dtype = vpX11;
177  }
178  else if (sDisplayType.compare("GTK") == 0) {
179  dtype = vpGTK;
180  }
181  else if (sDisplayType.compare("GDI") == 0) {
182  dtype = vpGDI;
183  }
184  else if (sDisplayType.compare("D3D") == 0) {
185  dtype = vpD3D;
186  }
187  else if (sDisplayType.compare("CV") == 0) {
188  dtype = vpCV;
189  }
190 
191  break;
192  case 'h': usage(argv[0], NULL, ipath,dtype); return false; break;
193  case 'c': click_allowed = false; break;
194  case 'd': display = false; break;
195 
196  default:
197  usage(argv[0], optarg_, ipath,dtype); return false; break;
198  }
199  }
200 
201 
202  if ((c == 1) || (c == -1)) {
203  // standalone param or error
204  usage(argv[0], NULL, ipath, dtype);
205  std::cerr << "ERROR: " << std::endl;
206  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
207  return false;
208  }
209 
210  return true;
211 }
212 
213 int
214 main(int argc, const char ** argv)
215 {
216  try{
217  std::string env_ipath;
218  std::string opt_ipath;
219  bool opt_list = false; // To print the list of video devices
220  vpDisplayType opt_dtype; // Type of display to use
221  std::string ipath;
222  std::string filename;
223  bool opt_click_allowed = true;
224  bool opt_display = true;
225 
226  // Default display is one available
227 #if defined VISP_HAVE_GTK
228  opt_dtype = vpGTK;
229 #elif defined VISP_HAVE_X11
230  opt_dtype = vpX11;
231 #elif defined VISP_HAVE_GDI
232  opt_dtype = vpGDI;
233 #elif defined VISP_HAVE_D3D9
234  opt_dtype = vpD3D;
235 #elif defined VISP_HAVE_OPENCV
236  opt_dtype = vpCV;
237 #endif
238 
239  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
240  env_ipath = vpIoTools::getViSPImagesDataPath();
241 
242  // Set the default input path
243  if (! env_ipath.empty())
244  ipath = env_ipath;
245 
246  // Read the command line options
247  if (getOptions(argc, argv, opt_ipath, opt_dtype, 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 (use \"-t GTK\" option to use it)\n";
258  nbDevices ++;
259 #endif
260 #if defined VISP_HAVE_X11
261  std::cout << " X11 (use \"-t X11\" option to use it)\n";
262  nbDevices ++;
263 #endif
264 #if defined VISP_HAVE_GDI
265  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
266  nbDevices ++;
267 #endif
268 #if defined VISP_HAVE_D3D9
269  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
270  nbDevices ++;
271 #endif
272 #if defined VISP_HAVE_OPENCV
273  std::cout << " CV (use \"-t CV\" option to use it)\n";
274  nbDevices ++;
275 #endif
276  if (!nbDevices) {
277  std::cout << " No display is available\n";
278  }
279  return (0);
280  }
281 
282  // Get the option values
283  if (!opt_ipath.empty())
284  ipath = opt_ipath;
285 
286  // Compare ipath and env_ipath. If they differ, we take into account
287  // the input path comming from the command line option
288  if (!opt_ipath.empty() && !env_ipath.empty()) {
289  if (ipath != env_ipath) {
290  std::cout << std::endl
291  << "WARNING: " << std::endl;
292  std::cout << " Since -i <visp image path=" << ipath << "> "
293  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
294  << " we skip the environment variable." << std::endl;
295  }
296  }
297 
298  // Test if an input path is set
299  if (opt_ipath.empty() && env_ipath.empty()){
300  usage(argv[0], NULL, ipath, opt_dtype);
301  std::cerr << std::endl
302  << "ERROR:" << std::endl;
303  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
304  << std::endl
305  << " environment variable to specify the location of the " << std::endl
306  << " image path where test images are located." << std::endl << std::endl;
307  exit(-1);
308  }
309 
310  // Create a grey level image
312  // Create a color image
313  vpImage<vpRGBa> Irgba ;
314 
315  // Load a grey image from the disk
316  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
317  vpCTRACE << "Load " << filename << std::endl;
318  vpImageIo::read(I, filename) ;
319 
320  // Load a color image from the disk
321  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
322  vpCTRACE << "Load " << filename << std::endl;
323  vpImageIo::read(Irgba, filename) ;
324 
325 
326  // Create a display for the image
327  vpDisplay *display = NULL;
328 
329  switch(opt_dtype) {
330  case vpX11:
331  std::cout << "Requested X11 display functionnalities..." << std::endl;
332 #if defined VISP_HAVE_X11
333  display = new vpDisplayX;
334 #else
335  std::cout << " Sorry, X11 video device is not available.\n";
336  std::cout << "Use \"" << argv[0]
337  << " -l\" to print the list of available devices.\n";
338  return 0;
339 #endif
340  break;
341  case vpGTK:
342  std::cout << "Requested GTK display functionnalities..." << std::endl;
343 #if defined VISP_HAVE_GTK
344  display = new vpDisplayGTK;
345 #else
346  std::cout << " Sorry, GTK video device is not available.\n";
347  std::cout << "Use \"" << argv[0]
348  << " -l\" to print the list of available devices.\n";
349  return 0;
350 #endif
351  break;
352  case vpGDI:
353  std::cout << "Requested GDI display functionnalities..." << std::endl;
354 #if defined VISP_HAVE_GDI
355  display = new vpDisplayGDI;
356 #else
357  std::cout << " Sorry, GDI video device is not available.\n";
358  std::cout << "Use \"" << argv[0]
359  << " -l\" to print the list of available devices.\n";
360  return 0;
361 #endif
362  break;
363  case vpD3D:
364  std::cout << "Requested D3D display functionnalities..." << std::endl;
365 #if defined VISP_HAVE_D3D9
366  display = new vpDisplayD3D;
367 #else
368  std::cout << " Sorry, D3D video device is not available.\n";
369  std::cout << "Use \"" << argv[0]
370  << " -l\" to print the list of available devices.\n";
371  return 0;
372 #endif
373  break;
374  case vpCV:
375  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
376 #if defined(VISP_HAVE_OPENCV)
377  display = new vpDisplayOpenCV;
378 #else
379  std::cout << " Sorry, OpenCV video device is not available.\n";
380  std::cout << "Use \"" << argv[0]
381  << " -l\" to print the list of available devices.\n";
382  return 0;
383 #endif
384  break;
385  }
386  if (opt_display){
387 
388  // We open a window using either X11 or GTK or GDI or D3D.
389  // Its size is automatically defined by the image (I) size
390  display->init(I, 100, 100,"Display...") ;
391 
392  // Display the image
393  // The image class has a member that specify a pointer toward
394  // the display that has been initialized in the display declaration
395  // therefore is is no longuer necessary to make a reference to the
396  // display variable.
397  vpDisplay::display(I) ;
398  //Flush the display
399  vpDisplay::flush(I) ;
400  std::cout << "A click to continue...\n";
401  if ( opt_click_allowed )
403 
404  display->close(I);
405 
406  // We open a window using either X11 or GTK or GDI or D3D
407  // but using anothe function who doesn't take title.
408  // Its size is automatically defined by the image (I) size
409 
410  display->init(I, 100, 100);
411 
412  // Display the image
413  // The image class has a member that specify a pointer toward
414  // the display that has been initialized in the display declaration
415  // therefore is is no longuer necessary to make a reference to the
416  // display variable.
417  vpDisplay::display(I) ;
418  //Flush the display
419  vpDisplay::flush(I) ;
420  std::cout << "A click to continue...\n";
421  if ( opt_click_allowed )
423 
424  display->close(I);
425 
426  // We open a window using either X11 or GTK or GDI or D3D.
427  // Its size is automatically defined by the image (I) size
428 
429  display->init(Irgba, 100, 100,"Color display...");
430 
431  // Display the image
432  // The image class has a member that specify a pointer toward
433  // the display that has been initialized in the display declaration
434  // therefore is is no longuer necessary to make a reference to the
435  // display variable.
436  vpDisplay::display(Irgba) ;
437  //Flush the display
438  vpDisplay::flush(Irgba) ;
439 
440  std::cout << "A click to continue...\n";
441  if ( opt_click_allowed )
442  vpDisplay::getClick(Irgba) ;
443 
444  display->close(Irgba);
445 
446  // We open a window using either X11 or GTK or GDI or D3D
447  // but using anothe function who doesn't take title.
448  // Its size is automatically defined by the image (I) size
449 
450  display->init(Irgba, 100, 100);
451 
452  // Display the image
453  // The image class has a member that specify a pointer toward
454  // the display that has been initialized in the display declaration
455  // therefore is is no longuer necessary to make a reference to the
456  // display variable.
457  vpDisplay::display(Irgba) ;
458  //Flush the display
459  vpDisplay::flush(Irgba) ;
460 
461  std::cout << "A click to exit...\n";
462  if ( opt_click_allowed )
463  vpDisplay::getClick(Irgba) ;
464  }
465  delete display;
466  }
467  catch(...) {
468  vpERROR_TRACE("Error while displaying the image") ;
469  exit(-1);
470  }
471 }
472 
473 #else
474 int
475 main()
476 {
477  vpERROR_TRACE("You do not have display functionalities...");
478 }
479 
480 #endif
481 
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:169
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
static void close(vpImage< unsigned char > &I)
#define vpERROR_TRACE
Definition: vpDebug.h:391
vpDisplayGDI()
Basic constructor.
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:153
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:107
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
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
#define vpCTRACE
Definition: vpDebug.h:337
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205