ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testDisplays.cpp
1 /****************************************************************************
2  *
3  * $Id: testDisplays.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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  * Anthony Saunier
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 #include <visp/vpDebug.h>
44 
45 #include <stdlib.h>
46 #include <iostream>
47 #include <string>
48 
49 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV))
50 
51 #include <visp/vpImage.h>
52 #include <visp/vpImageIo.h>
53 #include <visp/vpParseArgv.h>
54 #include <visp/vpIoTools.h>
55 #include <visp/vpRect.h>
56 
57 #include <visp/vpDisplayOpenCV.h>
58 #include <visp/vpDisplayGTK.h>
59 #include <visp/vpDisplayX.h>
60 #include <visp/vpDisplayGDI.h>
61 #include <visp/vpDisplayD3D.h>
62 
70 // List of allowed command line options
71 #define GETOPTARGS "hl:dc"
72 
81 void usage(const char *name, const char *badparam)
82 {
83  fprintf(stdout, "\n\
84 Test video devices or display.\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
88 ", name);
89 
90  std::string display;
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -c\n\
95  Disable the mouse click. Useful to automaze the \n\
96  execution of this program without humain intervention.\n\
97 \n\
98  -d \n\
99  Turn off the display.\n\
100 \n\
101  -l\n\
102  Print the list of video-devices available and exit.\n\
103 \n\
104  -h\n\
105  Print the help.\n\n");
106 
107  if (badparam)
108  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
109 }
110 
123 bool getOptions(int argc, const char **argv, bool &list,
124  bool &click_allowed, bool &display )
125 {
126  const char *optarg;
127  int c;
128  std::string sDisplayType;
129  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
130 
131  switch (c) {
132  case 'l': list = true; break;
133  case 'h': usage(argv[0], NULL); return false; break;
134  case 'c': click_allowed = false; break;
135  case 'd': display = false; break;
136 
137  default:
138  usage(argv[0], optarg); return false; break;
139  }
140  }
141 
142 
143  if ((c == 1) || (c == -1)) {
144  // standalone param or error
145  usage(argv[0], NULL);
146  std::cerr << "ERROR: " << std::endl;
147  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
148  return false;
149  }
150 
151  return true;
152 }
153 
154 
155 void draw(vpImage<vpRGBa> &I)
156 {
157  vpImagePoint iP1, iP2;
158  unsigned int w,h;
159 
160  iP1.set_i(20);
161  iP1.set_j(10);
162  iP2.set_i(20);
163  iP2.set_j(30);
164  vpDisplay::displayArrow (I, iP1, iP2, vpColor::green, 4, 2, 3);
165 
166  iP1.set_i(20);
167  iP1.set_j(60);
168  vpDisplay::displayCharString (I, iP1, "Test...", vpColor::black);
169 
170  iP1.set_i(80);
171  iP1.set_j(220);
172  iP2.set_i(80);
173  iP2.set_j(480);
174  vpDisplay::displayCircle (I, iP1, 30, vpColor::red, false, 3);
175  vpDisplay::displayCircle (I, iP2, 30, vpColor::red, true, 3);
176 
177  iP1.set_i(20);
178  iP1.set_j(220);
179  vpDisplay::displayCross (I, iP1, 5, vpColor::blue, 1);
180 
181  iP1.set_i(140);
182  iP1.set_j(10);
183  iP2.set_i(140);
184  iP2.set_j(50);
185  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
186 
187  iP1.set_i(120);
188  iP1.set_j(180);
189  iP2.set_i(160);
190  iP2.set_j(250);
191  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
192 
193  iP1.set_i(160);
194  iP1.set_j(280);
195  iP2.set_i(120);
196  iP2.set_j(340);
197  vpDisplay::displayDotLine (I, iP1, iP2, vpColor::blue, 3);
198 
199  //static void displayFrame (const vpImage< vpRGBa > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, vpColor color)
200 
201  iP1.set_i(140);
202  iP1.set_j(80);
203  iP2.set_i(140);
204  iP2.set_j(150);
205  vpDisplay::displayLine (I, iP1, iP2, vpColor::orange, 3);
206 
207  iP1.set_i(140);
208  iP1.set_j(400);
210 
211  iP1.set_i(350);
212  iP1.set_j(20);
213  w = 60;
214  h = 50;
215  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, false, 3);
216 
217  iP1.set_i(350);
218  iP1.set_j(110);
219  vpDisplay::displayRectangle (I, iP1, w, h, vpColor::red, true, 3);
220 
221  iP1.set_i(350);
222  iP1.set_j(200);
223  iP2.set_i(400);
224  iP2.set_j(260);
225  vpDisplay::displayRectangle (I, iP1, iP2, vpColor::orange, false, 3);
226 
227  iP1.set_i(350);
228  iP1.set_j(290);
229  iP2.set_i(400);
230  iP2.set_j(350);
231  vpRect rectangle(iP1, iP2);
232  vpDisplay::displayRectangle (I, rectangle, vpColor::yellow, false, 3);
233 
234  iP1.set_i(380);
235  iP1.set_j(400);
236  vpDisplay::displayRectangle (I, iP1, 45, w, h, vpColor::green, 3);
237 
238 
239 }
240 
241 int
242 main(int argc, const char ** argv)
243 {
244  try{
245  bool opt_list = false; // To print the list of video devices
246  bool opt_click_allowed = true;
247  bool opt_display = true;
248 
249 
250  // Read the command line options
251  if (getOptions(argc, argv, opt_list,
252  opt_click_allowed, opt_display) == false) {
253  exit (-1);
254  }
255 
256  // Print the list of video-devices available
257  if (opt_list) {
258  unsigned nbDevices = 0;
259  std::cout << "List of video-devices available: \n";
260 #if defined VISP_HAVE_GTK
261  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
262  nbDevices ++;
263 #endif
264 #if defined VISP_HAVE_X11
265  std::cout << " X11 (use \"-t X11\" option to use it)\n";
266  nbDevices ++;
267 #endif
268 #if defined VISP_HAVE_GDI
269  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
270  nbDevices ++;
271 #endif
272 #if defined VISP_HAVE_D3D9
273  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
274  nbDevices ++;
275 #endif
276 #if defined VISP_HAVE_OPENCV
277  std::cout << " CV (use \"-t CV\" option to use it)\n";
278  nbDevices ++;
279 #endif
280  if (!nbDevices) {
281  std::cout << " No display is available\n";
282  }
283  return (0);
284  }
285 
286  // Create a grey level image
288  // Create a color image
289  vpImage<vpRGBa> Irgba;
290 
291  // Create a color image for each display.
292  vpImage<vpRGBa> Ix;
293  vpImage<vpRGBa> Igtk;
294  vpImage<vpRGBa> Icv;
295  vpImage<vpRGBa> Igdi;
296  vpImage<vpRGBa> Id3d;
297 
298 #if defined VISP_HAVE_X11
299  vpDisplayX *displayX = NULL;
300  displayX = new vpDisplayX;
301  Ix.init(480, 640, 255);
302  if (opt_display)
303  {
304  displayX->init(Ix, 100, 100,"Display X11") ;
305  vpDisplay::display(Ix) ;
306  draw(Ix);
307  vpDisplay::flush(Ix);
308  if (opt_click_allowed)
310  }
311 #endif
312 
313 #if defined VISP_HAVE_OPENCV
314  vpDisplayOpenCV *displayCv = NULL;
315  displayCv = new vpDisplayOpenCV;
316  Icv.init(480, 640, 255);
317  if (opt_display)
318  {
319  displayCv->init(Icv, 100, 100,"Display OpenCV") ;
320  vpDisplay::display(Icv) ;
321  draw(Icv);
322  vpDisplay::flush(Icv);
323  if (opt_click_allowed)
324  vpDisplay::getClick(Icv);
325  }
326 #endif
327 
328 #if defined VISP_HAVE_GTK
329  vpDisplayGTK *displayGtk = NULL;
330  displayGtk = new vpDisplayGTK;
331  Igtk.init(480, 640, 255);
332  if (opt_display)
333  {
334  displayGtk->init(Igtk, 100, 100,"Display GTK") ;
335  vpDisplay::display(Igtk) ;
336  draw(Igtk);
337  vpDisplay::flush(Igtk);
338  if (opt_click_allowed)
339  vpDisplay::getClick(Igtk);
340  }
341 #endif
342 
343 #if defined VISP_HAVE_GDI
344  vpDisplayGDI *displayGdi = NULL;
345  displayGdi = new vpDisplayGDI;
346  Igdi.init(480, 640, 255);
347  if (opt_display)
348  {
349  displayGdi->init(Igdi, 100, 100,"Display GDI") ;
350  vpDisplay::display(Igdi) ;
351  draw(Igdi);
352  vpDisplay::flush(Igdi);
353  if (opt_click_allowed)
354  vpDisplay::getClick(Igdi);
355  }
356 #endif
357 
358 #if defined VISP_HAVE_D3D9
359  vpDisplayD3D *displayD3d = NULL;
360  displayD3d = new vpDisplayD3D;
361  Id3d.init(480, 640, 255);
362  if (opt_display)
363  {
364  displayD3d->init(Id3d, 100, 100,"Display Direct 3D") ;
365  vpDisplay::display(Id3d) ;
366  draw(Id3d);
367  vpDisplay::flush(Id3d);
368  if (opt_click_allowed)
369  vpDisplay::getClick(Id3d);
370  }
371 #endif
372 
373 
374 
375 #if defined VISP_HAVE_X11
376  delete displayX;
377 #endif
378 
379 #if defined VISP_HAVE_GTK
380  delete displayGtk;
381 #endif
382 
383 #if defined VISP_HAVE_OPENCV
384  delete displayCv;
385 #endif
386 
387 #if defined VISP_HAVE_GDI
388  delete displayGdi;
389 #endif
390 
391 #if defined VISP_HAVE_D3D9
392  delete displayD3d;
393 #endif
394  }
395  catch(...) {
396  vpERROR_TRACE("Error while displaying the image") ;
397  exit(-1);
398  }
399 }
400 
401 #else
402 int
403 main()
404 {
405  vpERROR_TRACE("You do not have display functionalities...");
406 }
407 
408 #endif