ViSP
testVideoDeviceDual.cpp
1 /****************************************************************************
2  *
3  * $Id: testVideoDeviceDual.cpp 5023 2014-12-03 16:07:48Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 
43 #include <visp/vpConfig.h>
44 #include <visp/vpImage.h>
45 #include <visp/vpDisplay.h>
46 #include <visp/vpDisplayOpenCV.h>
47 #include <visp/vpDisplayGTK.h>
48 #include <visp/vpDisplayX.h>
49 #include <visp/vpDisplayGDI.h>
50 #include <visp/vpDisplayD3D.h>
51 #include <visp/vpParseArgv.h>
52 #include <stdlib.h>
53 #include <iostream>
54 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV))
55 
64 // List of allowed command line options
65 #define GETOPTARGS "hlt:dc"
66 
67 typedef enum {
68  vpX11,
69  vpGTK,
70  vpGDI,
71  vpD3D,
72  vpCV
73 } vpDisplayType;
74 
75 void usage(const char *name, const char *badparam, vpDisplayType &dtype);
76 bool getOptions(int argc, const char **argv, vpDisplayType &dtype, bool &list, bool &click_allowed, bool &display);
77 
87 void usage(const char *name, const char *badparam, vpDisplayType &dtype)
88 {
89  fprintf(stdout, "\n\
90 Test to open video devices or display.\n\
91 \n\
92 SYNOPSIS\n\
93  %s [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
94 ", name);
95 
96  std::string display;
97  switch(dtype) {
98  case vpX11: display = "X11"; break;
99  case vpGTK: display = "GTK"; break;
100  case vpGDI: display = "GDI"; break;
101  case vpD3D: display = "D3D"; break;
102  case vpCV: display = "CV"; 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",
128  display.c_str());
129 
130  if (badparam)
131  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
132 }
133 
147 bool getOptions(int argc, const char **argv,
148  vpDisplayType &dtype, bool &list,
149  bool &click_allowed, bool &display)
150 {
151  const char *optarg_;
152  int c;
153  std::string sDisplayType;
154  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
155 
156  switch (c) {
157  case 'l': list = true; break;
158  case 't': sDisplayType = optarg_;
159  // Parse the display type option
160  if (sDisplayType.compare("X11") == 0) {
161  dtype = vpX11;
162  }
163  else if (sDisplayType.compare("GTK") == 0) {
164  dtype = vpGTK;
165  }
166  else if (sDisplayType.compare("GDI") == 0) {
167  dtype = vpGDI;
168  }
169  else if (sDisplayType.compare("D3D") == 0) {
170  dtype = vpD3D;
171  }
172  else if (sDisplayType.compare("CV") == 0) {
173  dtype = vpCV;
174  }
175 
176  break;
177  case 'h': usage(argv[0], NULL, dtype); return false; break;
178  case 'c': click_allowed = false; break;
179  case 'd': display = false; break;
180 
181  default:
182  usage(argv[0], optarg_, dtype); return false; break;
183  }
184  }
185 
186  if ((c == 1) || (c == -1)) {
187  // standalone param or error
188  usage(argv[0], NULL, dtype);
189  std::cerr << "ERROR: " << std::endl;
190  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
191  return false;
192  }
193 
194  return true;
195 }
196 
197 int main(int argc, const char ** argv)
198 {
199  try {
200  bool opt_list = false; // To print the list of video devices
201  vpDisplayType opt_dtype; // Type of display to use
202  bool opt_click_allowed = true;
203  bool opt_display = true;
204 
205  // Default display is one available
206 #if defined VISP_HAVE_GTK
207  opt_dtype = vpGTK;
208 #elif defined VISP_HAVE_X11
209  opt_dtype = vpX11;
210 #elif defined VISP_HAVE_GDI
211  opt_dtype = vpGDI;
212 #elif defined VISP_HAVE_D3D9
213  opt_dtype = vpD3D;
214 #elif defined VISP_HAVE_OPENCV
215  opt_dtype = vpCV;
216 #endif
217 
218  // Read the command line options
219  if (getOptions(argc, argv, opt_dtype, opt_list,
220  opt_click_allowed, opt_display) == false) {
221  exit (-1);
222  }
223 
224  // Print the list of video-devices available
225  if (opt_list) {
226  unsigned nbDevices = 0;
227  std::cout << "List of video-devices available: \n";
228 #if defined VISP_HAVE_GTK
229  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
230  nbDevices ++;
231 #endif
232 #if defined VISP_HAVE_X11
233  std::cout << " X11 (use \"-t X11\" option to use it)\n";
234  nbDevices ++;
235 #endif
236 #if defined VISP_HAVE_GDI
237  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
238  nbDevices ++;
239 #endif
240 #if defined VISP_HAVE_D3D9
241  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
242  nbDevices ++;
243 #endif
244 #if defined VISP_HAVE_OPENCV
245  std::cout << " CV (use \"-t CV\" option to use it)\n";
246  nbDevices ++;
247 #endif
248  if (!nbDevices) {
249  std::cout << " No display is available\n";
250  }
251  return (0);
252  }
253 
254  // Create 2 images
255  vpImage<unsigned char> I1(240, 320), I2(240, 320);
256  I1 = 128;
257  I2 = 255;
258 
259  // Create 2 display
260  vpDisplay *d1 = NULL, *d2 = NULL;
261 
262  // Initialize the displays
263  switch(opt_dtype) {
264  case vpX11:
265  std::cout << "Requested X11 display functionnalities..." << std::endl;
266 #if defined VISP_HAVE_X11
267  d1 = new vpDisplayX;
268  d2 = new vpDisplayX;
269 #else
270  std::cout << " Sorry, X11 video device is not available.\n";
271  std::cout << "Use \"" << argv[0]
272  << " -l\" to print the list of available devices.\n";
273  return 0;
274 #endif
275  break;
276  case vpGTK:
277  std::cout << "Requested GTK display functionnalities..." << std::endl;
278 #if defined VISP_HAVE_GTK
279  d1 = new vpDisplayGTK;
280  d2 = new vpDisplayGTK;
281 #else
282  std::cout << " Sorry, GTK video device is not available.\n";
283  std::cout << "Use \"" << argv[0]
284  << " -l\" to print the list of available devices.\n";
285  return 0;
286 #endif
287  break;
288  case vpGDI:
289  std::cout << "Requested GDI display functionnalities..." << std::endl;
290 #if defined VISP_HAVE_GDI
291  d1 = new vpDisplayGDI;
292  d2 = new vpDisplayGDI;
293 #else
294  std::cout << " Sorry, GDI video device is not available.\n";
295  std::cout << "Use \"" << argv[0]
296  << " -l\" to print the list of available devices.\n";
297  return 0;
298 #endif
299  break;
300  case vpD3D:
301  std::cout << "Requested D3D display functionnalities..." << std::endl;
302 #if defined VISP_HAVE_D3D9
303  d1 = new vpDisplayD3D;
304  d2 = new vpDisplayD3D;
305 #else
306  std::cout << " Sorry, D3D video device is not available.\n";
307  std::cout << "Use \"" << argv[0]
308  << " -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]
320  << " -l\" to print the list of available devices.\n";
321  return 0;
322 #endif
323  break;
324  }
325 
326  if (opt_display){
327  int winx1 = 100, winy1 = 200;
328  d1->init(I1, winx1, winy1, "Display 1");
329 
330  int winx2 = winx1+10+(int)I1.getWidth(), winy2 = winy1;
331  d2->init(I2, winx2, winy2, "Display 2");
332 
333  vpDisplay::display(I1);
334  vpDisplay::display(I2);
335 
336  vpDisplay::flush(I1);
337  vpDisplay::flush(I2);
338  }
339 
340  std::cout << "A click in display 1 to exit..." << std::endl;
341  if ( opt_click_allowed )
343 
344  delete d1;
345  delete d2;
346  }
347  catch(vpException e) {
348  std::cout << "Catch an exception: " << e << std::endl;
349  return 1;
350  }
351 }
352 
353 #else
354 int
355 main()
356 {
357  vpERROR_TRACE("You do not have display functionalities...");
358 }
359 
360 #endif
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:174
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
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