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