Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testVideoDeviceDual.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  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <iostream>
40 #include <stdlib.h>
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpDisplay.h>
43 #include <visp3/core/vpImage.h>
44 #include <visp3/gui/vpDisplayD3D.h>
45 #include <visp3/gui/vpDisplayGDI.h>
46 #include <visp3/gui/vpDisplayGTK.h>
47 #include <visp3/gui/vpDisplayOpenCV.h>
48 #include <visp3/gui/vpDisplayX.h>
49 #include <visp3/io/vpParseArgv.h>
50 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || \
51  defined(VISP_HAVE_OPENCV))
52 
60 // List of allowed command line options
61 #define GETOPTARGS "hlt:dc"
62 
63 typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
64 
65 void usage(const char *name, const char *badparam, vpDisplayType &dtype);
66 bool getOptions(int argc, const char **argv, vpDisplayType &dtype, bool &list, bool &click_allowed, bool &display);
67 
77 void usage(const char *name, const char *badparam, vpDisplayType &dtype)
78 {
79  fprintf(stdout, "\n\
80 Test to open video devices or display.\n\
81 \n\
82 SYNOPSIS\n\
83  %s [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
84 ", name);
85 
86  std::string display;
87  switch (dtype) {
88  case vpX11:
89  display = "X11";
90  break;
91  case vpGTK:
92  display = "GTK";
93  break;
94  case vpGDI:
95  display = "GDI";
96  break;
97  case vpD3D:
98  display = "D3D";
99  break;
100  case vpCV:
101  display = "CV";
102  break;
103  }
104 
105  fprintf(stdout, "\n\
106 OPTIONS: Default\n\
107  -t <type of video device> \"%s\"\n\
108  String specifying the video device to use.\n\
109  Possible values:\n\
110  \"X11\": only on UNIX platforms,\n\
111  \"GTK\": on all plaforms,\n\
112  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
113  \"D3D\": only on Windows platform (Direct3D).\n\
114  \"CV\" : (OpenCV).\n\
115 \n\
116  -c\n\
117  Disable the mouse click. Useful to automaze the \n\
118  execution of this program without humain intervention.\n\
119 \n\
120  -d \n\
121  Turn off the display.\n\
122 \n\
123  -l\n\
124  Print the list of video-devices available and exit.\n\
125 \n\
126  -h\n\
127  Print the help.\n\n", display.c_str());
128 
129  if (badparam)
130  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
131 }
132 
146 bool getOptions(int argc, const char **argv, vpDisplayType &dtype, bool &list, bool &click_allowed, bool &display)
147 {
148  const char *optarg_;
149  int c;
150  std::string sDisplayType;
151  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
152 
153  switch (c) {
154  case 'l':
155  list = true;
156  break;
157  case 't':
158  sDisplayType = optarg_;
159  // Parse the display type option
160  if (sDisplayType.compare("X11") == 0) {
161  dtype = vpX11;
162  } else if (sDisplayType.compare("GTK") == 0) {
163  dtype = vpGTK;
164  } else if (sDisplayType.compare("GDI") == 0) {
165  dtype = vpGDI;
166  } else if (sDisplayType.compare("D3D") == 0) {
167  dtype = vpD3D;
168  } else if (sDisplayType.compare("CV") == 0) {
169  dtype = vpCV;
170  }
171 
172  break;
173  case 'h':
174  usage(argv[0], NULL, dtype);
175  return false;
176  break;
177  case 'c':
178  click_allowed = false;
179  break;
180  case 'd':
181  display = false;
182  break;
183 
184  default:
185  usage(argv[0], optarg_, dtype);
186  return false;
187  break;
188  }
189  }
190 
191  if ((c == 1) || (c == -1)) {
192  // standalone param or error
193  usage(argv[0], NULL, dtype);
194  std::cerr << "ERROR: " << std::endl;
195  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
196  return false;
197  }
198 
199  return true;
200 }
201 
202 int main(int argc, const char **argv)
203 {
204  try {
205  bool opt_list = false; // To print the list of video devices
206  vpDisplayType opt_dtype; // Type of display to use
207  bool opt_click_allowed = true;
208  bool opt_display = true;
209 
210 // Default display is one available
211 #if defined VISP_HAVE_GTK
212  opt_dtype = vpGTK;
213 #elif defined VISP_HAVE_X11
214  opt_dtype = vpX11;
215 #elif defined VISP_HAVE_GDI
216  opt_dtype = vpGDI;
217 #elif defined VISP_HAVE_D3D9
218  opt_dtype = vpD3D;
219 #elif defined VISP_HAVE_OPENCV
220  opt_dtype = vpCV;
221 #endif
222 
223  // Read the command line options
224  if (getOptions(argc, argv, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
225  exit(-1);
226  }
227 
228  // Print the list of video-devices available
229  if (opt_list) {
230  unsigned nbDevices = 0;
231  std::cout << "List of video-devices available: \n";
232 #if defined VISP_HAVE_GTK
233  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
234  nbDevices++;
235 #endif
236 #if defined VISP_HAVE_X11
237  std::cout << " X11 (use \"-t X11\" option to use it)\n";
238  nbDevices++;
239 #endif
240 #if defined VISP_HAVE_GDI
241  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
242  nbDevices++;
243 #endif
244 #if defined VISP_HAVE_D3D9
245  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
246  nbDevices++;
247 #endif
248 #if defined VISP_HAVE_OPENCV
249  std::cout << " CV (use \"-t CV\" option to use it)\n";
250  nbDevices++;
251 #endif
252  if (!nbDevices) {
253  std::cout << " No display is available\n";
254  }
255  return (0);
256  }
257 
258  // Create 2 images
259  vpImage<unsigned char> I1(240, 320), I2(240, 320);
260  I1 = 128;
261  I2 = 255;
262 
263  // Create 2 display
264  vpDisplay *d1 = NULL, *d2 = NULL;
265 
266  // Initialize the displays
267  switch (opt_dtype) {
268  case vpX11:
269  std::cout << "Requested X11 display functionnalities..." << std::endl;
270 #if defined VISP_HAVE_X11
271  d1 = new vpDisplayX;
272  d2 = new vpDisplayX;
273 #else
274  std::cout << " Sorry, X11 video device is not available.\n";
275  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
276  return 0;
277 #endif
278  break;
279  case vpGTK:
280  std::cout << "Requested GTK display functionnalities..." << std::endl;
281 #if defined VISP_HAVE_GTK
282  d1 = new vpDisplayGTK;
283  d2 = new vpDisplayGTK;
284 #else
285  std::cout << " Sorry, GTK video device is not available.\n";
286  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
287  return 0;
288 #endif
289  break;
290  case vpGDI:
291  std::cout << "Requested GDI display functionnalities..." << std::endl;
292 #if defined VISP_HAVE_GDI
293  d1 = new vpDisplayGDI;
294  d2 = new vpDisplayGDI;
295 #else
296  std::cout << " Sorry, GDI video device is not available.\n";
297  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
298  return 0;
299 #endif
300  break;
301  case vpD3D:
302  std::cout << "Requested D3D display functionnalities..." << std::endl;
303 #if defined VISP_HAVE_D3D9
304  d1 = new vpDisplayD3D;
305  d2 = new vpDisplayD3D;
306 #else
307  std::cout << " Sorry, D3D video device is not available.\n";
308  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
309  return 0;
310 #endif
311  break;
312  case vpCV:
313  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
314 #if defined(VISP_HAVE_OPENCV)
315  d1 = new vpDisplayOpenCV;
316  d2 = new vpDisplayOpenCV;
317 #else
318  std::cout << " Sorry, OpenCV video device is not available.\n";
319  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
320  return 0;
321 #endif
322  break;
323  }
324 
325  if (opt_display) {
326  int winx1 = 100, winy1 = 200;
327  d1->init(I1, winx1, winy1, "Display 1");
328 
329  int winx2 = winx1 + 10 + (int)I1.getWidth(), winy2 = winy1;
330  d2->init(I2, winx2, winy2, "Display 2");
331 
332  vpDisplay::display(I1);
333  vpDisplay::display(I2);
334 
335  vpDisplay::flush(I1);
336  vpDisplay::flush(I2);
337  }
338 
339  std::cout << "A click in display 1 to exit..." << std::endl;
340  if (opt_click_allowed)
342 
343  delete d1;
344  delete d2;
345  } catch (const vpException &e) {
346  std::cout << "Catch an exception: " << e << std::endl;
347  return 1;
348  }
349 }
350 
351 #else
352 int main() { vpERROR_TRACE("You do not have display functionalities..."); }
353 
354 #endif
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Definition: vpDisplay.h:239
#define vpERROR_TRACE
Definition: vpDebug.h:393
vpDisplayGDI()
Basic constructor.
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
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:107
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:138