Visual Servoing Platform  version 3.4.0
testVideoDevice.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test for image display.
33  *
34  * Authors:
35  * Anthony Saunier
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpDebug.h>
41 
42 #include <iostream>
43 #include <stdlib.h>
44 #include <string>
45 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || \
46  defined(VISP_HAVE_OPENCV))
47 
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/io/vpImageIo.h>
51 #include <visp3/io/vpParseArgv.h>
52 
53 #include <visp3/gui/vpDisplayD3D.h>
54 #include <visp3/gui/vpDisplayGDI.h>
55 #include <visp3/gui/vpDisplayGTK.h>
56 #include <visp3/gui/vpDisplayOpenCV.h>
57 #include <visp3/gui/vpDisplayX.h>
58 
66 // List of allowed command line options
67 #define GETOPTARGS "i:hlt:dc"
68 
69 typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
70 
71 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
72 bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
73  bool &display);
74 
85 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
86 {
87  fprintf(stdout, "\n\
88 Test video devices or display.\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-i <input image path>] \n\
92  [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
93 ", name);
94 
95  std::string display;
96  switch (dtype) {
97  case vpX11:
98  display = "X11";
99  break;
100  case vpGTK:
101  display = "GTK";
102  break;
103  case vpGDI:
104  display = "GDI";
105  break;
106  case vpD3D:
107  display = "D3D";
108  break;
109  case vpCV:
110  display = "CV";
111  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 \"Klimt/Klimt.pgm\"\n\
119  and \"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", ipath.c_str(), display.c_str());
145 
146  if (badparam)
147  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
148 }
149 
164 bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
165  bool &display)
166 {
167  const char *optarg_;
168  int c;
169  std::string sDisplayType;
170  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
171 
172  switch (c) {
173  case 'i':
174  ipath = optarg_;
175  break;
176  case 'l':
177  list = true;
178  break;
179  case 't':
180  sDisplayType = optarg_;
181  // Parse the display type option
182  if (sDisplayType.compare("X11") == 0) {
183  dtype = vpX11;
184  } else if (sDisplayType.compare("GTK") == 0) {
185  dtype = vpGTK;
186  } else if (sDisplayType.compare("GDI") == 0) {
187  dtype = vpGDI;
188  } else if (sDisplayType.compare("D3D") == 0) {
189  dtype = vpD3D;
190  } else if (sDisplayType.compare("CV") == 0) {
191  dtype = vpCV;
192  }
193 
194  break;
195  case 'h':
196  usage(argv[0], NULL, ipath, dtype);
197  return false;
198  break;
199  case 'c':
200  click_allowed = false;
201  break;
202  case 'd':
203  display = false;
204  break;
205 
206  default:
207  usage(argv[0], optarg_, ipath, dtype);
208  return false;
209  break;
210  }
211  }
212 
213  if ((c == 1) || (c == -1)) {
214  // standalone param or error
215  usage(argv[0], NULL, ipath, dtype);
216  std::cerr << "ERROR: " << std::endl;
217  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
218  return false;
219  }
220 
221  return true;
222 }
223 
224 int main(int argc, const char **argv)
225 {
226  try {
227  std::string env_ipath;
228  std::string opt_ipath;
229  bool opt_list = false; // To print the list of video devices
230  vpDisplayType opt_dtype; // Type of display to use
231  std::string ipath;
232  std::string filename;
233  bool opt_click_allowed = true;
234  bool opt_display = true;
235 
236 // Default display is one available
237 #if defined VISP_HAVE_GTK
238  opt_dtype = vpGTK;
239 #elif defined VISP_HAVE_X11
240  opt_dtype = vpX11;
241 #elif defined VISP_HAVE_GDI
242  opt_dtype = vpGDI;
243 #elif defined VISP_HAVE_D3D9
244  opt_dtype = vpD3D;
245 #elif defined VISP_HAVE_OPENCV
246  opt_dtype = vpCV;
247 #endif
248 
249  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
250  // environment variable value
251  env_ipath = vpIoTools::getViSPImagesDataPath();
252 
253  // Set the default input path
254  if (!env_ipath.empty())
255  ipath = env_ipath;
256 
257  // Read the command line options
258  if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
259  exit(-1);
260  }
261 
262  // Print the list of video-devices available
263  if (opt_list) {
264  unsigned nbDevices = 0;
265  std::cout << "List of video-devices available: \n";
266 #if defined VISP_HAVE_GTK
267  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
268  nbDevices++;
269 #endif
270 #if defined VISP_HAVE_X11
271  std::cout << " X11 (use \"-t X11\" option to use it)\n";
272  nbDevices++;
273 #endif
274 #if defined VISP_HAVE_GDI
275  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
276  nbDevices++;
277 #endif
278 #if defined VISP_HAVE_D3D9
279  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
280  nbDevices++;
281 #endif
282 #if defined VISP_HAVE_OPENCV
283  std::cout << " CV (use \"-t CV\" option to use it)\n";
284  nbDevices++;
285 #endif
286  if (!nbDevices) {
287  std::cout << " No display is available\n";
288  }
289  return (0);
290  }
291 
292  // Get the option values
293  if (!opt_ipath.empty())
294  ipath = opt_ipath;
295 
296  // Compare ipath and env_ipath. If they differ, we take into account
297  // the input path comming from the command line option
298  if (!opt_ipath.empty() && !env_ipath.empty()) {
299  if (ipath != env_ipath) {
300  std::cout << std::endl << "WARNING: " << std::endl;
301  std::cout << " Since -i <visp image path=" << ipath << "> "
302  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
303  << " we skip the environment variable." << std::endl;
304  }
305  }
306 
307  // Test if an input path is set
308  if (opt_ipath.empty() && env_ipath.empty()) {
309  usage(argv[0], NULL, ipath, opt_dtype);
310  std::cerr << std::endl << "ERROR:" << std::endl;
311  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
312  << " environment variable to specify the location of the " << std::endl
313  << " image path where test images are located." << std::endl
314  << std::endl;
315  exit(-1);
316  }
317 
318  // Create a grey level image
320  // Create a color image
321  vpImage<vpRGBa> Irgba;
322 
323  // Load a grey image from the disk
324  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
325  vpCTRACE << "Load " << filename << std::endl;
326  vpImageIo::read(I, filename);
327 
328  // Load a color image from the disk
329  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
330  vpCTRACE << "Load " << filename << std::endl;
331  vpImageIo::read(Irgba, filename);
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] << " -l\" to print the list of available devices.\n";
344  return 0;
345 #endif
346  break;
347  case vpGTK:
348  std::cout << "Requested GTK display functionnalities..." << std::endl;
349 #if defined VISP_HAVE_GTK
350  display = new vpDisplayGTK;
351 #else
352  std::cout << " Sorry, GTK video device is not available.\n";
353  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
354  return 0;
355 #endif
356  break;
357  case vpGDI:
358  std::cout << "Requested GDI display functionnalities..." << std::endl;
359 #if defined VISP_HAVE_GDI
360  display = new vpDisplayGDI;
361 #else
362  std::cout << " Sorry, GDI video device is not available.\n";
363  std::cout << "Use \"" << argv[0] << " -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] << " -l\" to print the list of available devices.\n";
374  return 0;
375 #endif
376  break;
377  case vpCV:
378  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
379 #if defined(VISP_HAVE_OPENCV)
380  display = new vpDisplayOpenCV;
381 #else
382  std::cout << " Sorry, OpenCV video device is not available.\n";
383  std::cout << "Use \"" << argv[0] << " -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.
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.
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  } catch (...) {
469  vpERROR_TRACE("Error while displaying the image");
470  exit(-1);
471  }
472 }
473 
474 #else
475 int main() { vpERROR_TRACE("You do not have display functionalities..."); }
476 
477 #endif
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:177
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1202
static void close(vpImage< unsigned char > &I)
#define vpERROR_TRACE
Definition: vpDebug.h:393
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed...
Definition: vpDisplayD3D.h:106
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1446
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:134
#define vpCTRACE
Definition: vpDebug.h:338
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:244