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