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