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