ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Modules Pages
testVideoDevice.cpp
1 /****************************************************************************
2  *
3  * $Id: testVideoDevice.cpp 4658 2014-02-09 09:50:14Z 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_IMAGE_PATH environment variable value
244  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
245  if (ptenv != NULL)
246  env_ipath = ptenv;
247 
248  // Set the default input path
249  if (! env_ipath.empty())
250  ipath = env_ipath;
251 
252  // Read the command line options
253  if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list,
254  opt_click_allowed, opt_display) == false) {
255  exit (-1);
256  }
257 
258  // Print the list of video-devices available
259  if (opt_list) {
260  unsigned nbDevices = 0;
261  std::cout << "List of video-devices available: \n";
262 #if defined VISP_HAVE_GTK
263  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
264  nbDevices ++;
265 #endif
266 #if defined VISP_HAVE_X11
267  std::cout << " X11 (use \"-t X11\" option to use it)\n";
268  nbDevices ++;
269 #endif
270 #if defined VISP_HAVE_GDI
271  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
272  nbDevices ++;
273 #endif
274 #if defined VISP_HAVE_D3D9
275  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
276  nbDevices ++;
277 #endif
278 #if defined VISP_HAVE_OPENCV
279  std::cout << " CV (use \"-t CV\" option to use it)\n";
280  nbDevices ++;
281 #endif
282  if (!nbDevices) {
283  std::cout << " No display is available\n";
284  }
285  return (0);
286  }
287 
288 
289  // Get the option values
290  if (!opt_ipath.empty())
291  ipath = opt_ipath;
292 
293  // Compare ipath and env_ipath. If they differ, we take into account
294  // the input path comming from the command line option
295  if (!opt_ipath.empty() && !env_ipath.empty()) {
296  if (ipath != env_ipath) {
297  std::cout << std::endl
298  << "WARNING: " << std::endl;
299  std::cout << " Since -i <visp image path=" << ipath << "> "
300  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
301  << " we skip the environment variable." << std::endl;
302  }
303  }
304 
305  // Test if an input path is set
306  if (opt_ipath.empty() && env_ipath.empty()){
307  usage(argv[0], NULL, ipath, opt_dtype);
308  std::cerr << std::endl
309  << "ERROR:" << std::endl;
310  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
311  << std::endl
312  << " environment variable to specify the location of the " << std::endl
313  << " image path where test images are located." << std::endl << std::endl;
314  exit(-1);
315  }
316 
317  // Create a grey level image
319  // Create a color image
320  vpImage<vpRGBa> Irgba ;
321 
322  // Load a grey image from the disk
323  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
324  vpCTRACE << "Load " << filename << std::endl;
325  vpImageIo::read(I, filename) ;
326 
327  // Load a color image from the disk
328  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
329  vpCTRACE << "Load " << filename << std::endl;
330  vpImageIo::read(Irgba, filename) ;
331 
332 
333  // Create a display for the image
334  vpDisplay *display = NULL;
335 
336  switch(opt_dtype) {
337  case vpX11:
338  std::cout << "Requested X11 display functionnalities..." << std::endl;
339 #if defined VISP_HAVE_X11
340  display = new vpDisplayX;
341 #else
342  std::cout << " Sorry, X11 video device is not available.\n";
343  std::cout << "Use \"" << argv[0]
344  << " -l\" to print the list of available devices.\n";
345  return 0;
346 #endif
347  break;
348  case vpGTK:
349  std::cout << "Requested GTK display functionnalities..." << std::endl;
350 #if defined VISP_HAVE_GTK
351  display = new vpDisplayGTK;
352 #else
353  std::cout << " Sorry, GTK video device is not available.\n";
354  std::cout << "Use \"" << argv[0]
355  << " -l\" to print the list of available devices.\n";
356  return 0;
357 #endif
358  break;
359  case vpGDI:
360  std::cout << "Requested GDI display functionnalities..." << std::endl;
361 #if defined VISP_HAVE_GDI
362  display = new vpDisplayGDI;
363 #else
364  std::cout << " Sorry, GDI video device is not available.\n";
365  std::cout << "Use \"" << argv[0]
366  << " -l\" to print the list of available devices.\n";
367  return 0;
368 #endif
369  break;
370  case vpD3D:
371  std::cout << "Requested D3D display functionnalities..." << std::endl;
372 #if defined VISP_HAVE_D3D9
373  display = new vpDisplayD3D;
374 #else
375  std::cout << " Sorry, D3D video device is not available.\n";
376  std::cout << "Use \"" << argv[0]
377  << " -l\" to print the list of available devices.\n";
378  return 0;
379 #endif
380  break;
381  case vpCV:
382  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
383 #if defined VISP_HAVE_OPENCV
384  display = new vpDisplayOpenCV;
385 #else
386  std::cout << " Sorry, OpenCV video device is not available.\n";
387  std::cout << "Use \"" << argv[0]
388  << " -l\" to print the list of available devices.\n";
389  return 0;
390 #endif
391  break;
392  }
393  if (opt_display){
394 
395  // We open a window using either X11 or GTK or GDI or D3D.
396  // Its size is automatically defined by the image (I) size
397  display->init(I, 100, 100,"Display...") ;
398 
399  // Display the image
400  // The image class has a member that specify a pointer toward
401  // the display that has been initialized in the display declaration
402  // therefore is is no longuer necessary to make a reference to the
403  // display variable.
404  vpDisplay::display(I) ;
405  //Flush the display
406  vpDisplay::flush(I) ;
407  std::cout << "A click to continue...\n";
408  if ( opt_click_allowed )
410 
411  display->close(I);
412 
413  // We open a window using either X11 or GTK or GDI or D3D
414  // but using anothe function who doesn't take title.
415  // Its size is automatically defined by the image (I) size
416 
417  display->init(I, 100, 100);
418 
419  // Display the image
420  // The image class has a member that specify a pointer toward
421  // the display that has been initialized in the display declaration
422  // therefore is is no longuer necessary to make a reference to the
423  // display variable.
424  vpDisplay::display(I) ;
425  //Flush the display
426  vpDisplay::flush(I) ;
427  std::cout << "A click to continue...\n";
428  if ( opt_click_allowed )
430 
431  display->close(I);
432 
433  // We open a window using either X11 or GTK or GDI or D3D.
434  // Its size is automatically defined by the image (I) size
435 
436  display->init(Irgba, 100, 100,"Color display...");
437 
438  // Display the image
439  // The image class has a member that specify a pointer toward
440  // the display that has been initialized in the display declaration
441  // therefore is is no longuer necessary to make a reference to the
442  // display variable.
443  vpDisplay::display(Irgba) ;
444  //Flush the display
445  vpDisplay::flush(Irgba) ;
446 
447  std::cout << "A click to continue...\n";
448  if ( opt_click_allowed )
449  vpDisplay::getClick(Irgba) ;
450 
451  display->close(Irgba);
452 
453  // We open a window using either X11 or GTK or GDI or D3D
454  // but using anothe function who doesn't take title.
455  // Its size is automatically defined by the image (I) size
456 
457  display->init(Irgba, 100, 100);
458 
459  // Display the image
460  // The image class has a member that specify a pointer toward
461  // the display that has been initialized in the display declaration
462  // therefore is is no longuer necessary to make a reference to the
463  // display variable.
464  vpDisplay::display(Irgba) ;
465  //Flush the display
466  vpDisplay::flush(Irgba) ;
467 
468  std::cout << "A click to exit...\n";
469  if ( opt_click_allowed )
470  vpDisplay::getClick(Irgba) ;
471  }
472  delete display;
473  }
474  catch(...) {
475  vpERROR_TRACE("Error while displaying the image") ;
476  exit(-1);
477  }
478 }
479 
480 #else
481 int
482 main()
483 {
484  vpERROR_TRACE("You do not have display functionalities...");
485 }
486 
487 #endif
488 
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:176
static void close(vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2031
#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 std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:341
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
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