Visual Servoing Platform  version 3.2.0
testDisplays.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  * Anthony Saunier
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpDebug.h>
41 
42 #include <iostream>
43 #include <stdlib.h>
44 #include <string>
45 
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/core/vpRect.h>
52 #include <visp3/io/vpImageIo.h>
53 #include <visp3/io/vpParseArgv.h>
54 
55 #include <visp3/gui/vpDisplayD3D.h>
56 #include <visp3/gui/vpDisplayGDI.h>
57 #include <visp3/gui/vpDisplayGTK.h>
58 #include <visp3/gui/vpDisplayOpenCV.h>
59 #include <visp3/gui/vpDisplayX.h>
60 
68 // List of allowed command line options
69 #define GETOPTARGS "hldc"
70 
71 void usage(const char *name, const char *badparam);
72 bool getOptions(int argc, const char **argv, bool &list, bool &click_allowed, bool &display);
73 void draw(vpImage<vpRGBa> &I);
74 
83 void usage(const char *name, const char *badparam)
84 {
85  fprintf(stdout, "\n\
86 Test video devices or display.\n\
87 \n\
88 SYNOPSIS\n\
89  %s [-l] [-c] [-d] [-h]\n\
90 ", name);
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, bool &click_allowed, bool &display)
124 {
125  const char *optarg_;
126  int c;
127  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
128 
129  switch (c) {
130  case 'l':
131  list = true;
132  break;
133  case 'h':
134  usage(argv[0], NULL);
135  return false;
136  break;
137  case 'c':
138  click_allowed = false;
139  break;
140  case 'd':
141  display = false;
142  break;
143 
144  default:
145  usage(argv[0], optarg_);
146  return false;
147  break;
148  }
149  }
150 
151  if ((c == 1) || (c == -1)) {
152  // standalone param or error
153  usage(argv[0], NULL);
154  std::cerr << "ERROR: " << std::endl;
155  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
156  return false;
157  }
158 
159  return true;
160 }
161 
162 void draw(vpImage<vpRGBa> &I)
163 {
164  vpImagePoint iP1, iP2;
165  unsigned int w, h;
166 
167  iP1.set_i(20);
168  iP1.set_j(10);
169  iP2.set_i(20);
170  iP2.set_j(30);
171  vpDisplay::displayArrow(I, iP1, iP2, vpColor::green, 4, 2, 3);
172 
173  iP1.set_i(20);
174  iP1.set_j(60);
175  vpDisplay::displayText(I, iP1, "Test...", vpColor::black);
176 
177  iP1.set_i(80);
178  iP1.set_j(220);
179  iP2.set_i(80);
180  iP2.set_j(480);
181  vpDisplay::displayCircle(I, iP1, 30, vpColor::red, false, 3);
182  vpDisplay::displayCircle(I, iP2, 30, vpColor::red, true, 3);
183 
184  iP1.set_i(20);
185  iP1.set_j(220);
186  vpDisplay::displayCross(I, iP1, 5, vpColor::blue, 1);
187 
188  iP1.set_i(140);
189  iP1.set_j(10);
190  iP2.set_i(140);
191  iP2.set_j(50);
192  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
193 
194  iP1.set_i(120);
195  iP1.set_j(180);
196  iP2.set_i(160);
197  iP2.set_j(250);
198  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
199 
200  iP1.set_i(160);
201  iP1.set_j(280);
202  iP2.set_i(120);
203  iP2.set_j(340);
204  vpDisplay::displayDotLine(I, iP1, iP2, vpColor::blue, 3);
205 
206  // static void displayFrame (const vpImage< vpRGBa > &I, const
207  // vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size,
208  // vpColor color)
209 
210  iP1.set_i(140);
211  iP1.set_j(80);
212  iP2.set_i(140);
213  iP2.set_j(150);
214  vpDisplay::displayLine(I, iP1, iP2, vpColor::orange, 3);
215 
216  iP1.set_i(140);
217  iP1.set_j(400);
219 
220  iP1.set_i(350);
221  iP1.set_j(20);
222  w = 60;
223  h = 50;
224  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, false, 3);
225 
226  iP1.set_i(350);
227  iP1.set_j(110);
228  vpDisplay::displayRectangle(I, iP1, w, h, vpColor::red, true, 3);
229 
230  iP1.set_i(350);
231  iP1.set_j(200);
232  iP2.set_i(400);
233  iP2.set_j(260);
234  vpDisplay::displayRectangle(I, iP1, iP2, vpColor::orange, false, 3);
235 
236  iP1.set_i(350);
237  iP1.set_j(290);
238  iP2.set_i(400);
239  iP2.set_j(350);
240  vpRect rectangle(iP1, iP2);
241  vpDisplay::displayRectangle(I, rectangle, vpColor::yellow, false, 3);
242 
243  iP1.set_i(380);
244  iP1.set_j(400);
245  vpDisplay::displayRectangle(I, iP1, 45, w, h, vpColor::green, 3);
246 }
247 
248 int main(int argc, const char **argv)
249 {
250  try {
251  bool opt_list = false; // To print the list of video devices
252  bool opt_click_allowed = true;
253  bool opt_display = true;
254 
255  // Read the command line options
256  if (getOptions(argc, argv, opt_list, opt_click_allowed, opt_display) == false) {
257  exit(-1);
258  }
259 
260  // Print the list of video-devices available
261  if (opt_list) {
262  unsigned nbDevices = 0;
263  std::cout << "List of video-devices available: \n";
264 #if defined VISP_HAVE_GTK
265  std::cout << " GTK\n";
266  nbDevices++;
267 #endif
268 #if defined VISP_HAVE_X11
269  std::cout << " X11\n";
270  nbDevices++;
271 #endif
272 #if defined VISP_HAVE_GDI
273  std::cout << " GDI\n";
274  nbDevices++;
275 #endif
276 #if defined VISP_HAVE_D3D9
277  std::cout << " D3D\n";
278  nbDevices++;
279 #endif
280 #if defined VISP_HAVE_OPENCV
281  std::cout << " OpenCV\n";
282  nbDevices++;
283 #endif
284  if (!nbDevices) {
285  std::cout << " No display is available\n";
286  }
287  return (0);
288  }
289 
290  // Create a grey level image
292  // Create a color image
293  vpImage<vpRGBa> Irgba;
294 
295  // Create a color image for each display.
296  vpImage<vpRGBa> Ix;
297  vpImage<vpRGBa> Igtk;
298  vpImage<vpRGBa> Icv;
299  vpImage<vpRGBa> Igdi;
300  vpImage<vpRGBa> Id3d;
301 
302 #if defined VISP_HAVE_X11
303  vpDisplayX *displayX = NULL;
304  displayX = new vpDisplayX;
305  Ix.init(480, 640, 255);
306  if (opt_display) {
307  displayX->init(Ix, 100, 100, "Display X11");
308  vpDisplay::display(Ix);
309  draw(Ix);
310  vpDisplay::flush(Ix);
311  if (opt_click_allowed)
313  }
314 #endif
315 
316 #if defined(VISP_HAVE_OPENCV)
317  vpDisplayOpenCV *displayCv = NULL;
318  displayCv = new vpDisplayOpenCV;
319  Icv.init(480, 640, 255);
320  if (opt_display) {
321  displayCv->init(Icv, 100, 100, "Display OpenCV");
322  vpDisplay::display(Icv);
323  draw(Icv);
324  vpDisplay::flush(Icv);
325  if (opt_click_allowed)
326  vpDisplay::getClick(Icv);
327  }
328 #endif
329 
330 #if defined VISP_HAVE_GTK
331  vpDisplayGTK *displayGtk = NULL;
332  displayGtk = new vpDisplayGTK;
333  Igtk.init(480, 640, 255);
334  if (opt_display) {
335  displayGtk->init(Igtk, 100, 100, "Display GTK");
336  vpDisplay::display(Igtk);
337  draw(Igtk);
338  vpDisplay::flush(Igtk);
339  if (opt_click_allowed)
340  vpDisplay::getClick(Igtk);
341  }
342 #endif
343 
344 #if defined VISP_HAVE_GDI
345  vpDisplayGDI *displayGdi = NULL;
346  displayGdi = new vpDisplayGDI;
347  Igdi.init(480, 640, 255);
348  if (opt_display) {
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  displayD3d->init(Id3d, 100, 100, "Display Direct 3D");
364  vpDisplay::display(Id3d);
365  draw(Id3d);
366  vpDisplay::flush(Id3d);
367  if (opt_click_allowed)
368  vpDisplay::getClick(Id3d);
369  }
370 #endif
371 
372 #if defined VISP_HAVE_X11
373  delete displayX;
374 #endif
375 
376 #if defined VISP_HAVE_GTK
377  delete displayGtk;
378 #endif
379 
380 #if defined(VISP_HAVE_OPENCV)
381  delete displayCv;
382 #endif
383 
384 #if defined VISP_HAVE_GDI
385  delete displayGdi;
386 #endif
387 
388 #if defined VISP_HAVE_D3D9
389  delete displayD3d;
390 #endif
391  } catch (const vpException &e) {
392  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
393  return (-1);
394  }
395 }
396 
397 #else
398 int main() { std::cout << "You do not have display functionalities..." << std::endl; }
399 
400 #endif
vpImagePoint::set_i
void set_i(const double ii)
Definition: vpImagePoint.h:166
vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
vpDisplay::displayDotLine
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:206
vpColor::orange
static const vpColor orange
Definition: vpColor.h:189
vpImagePoint::set_j
void set_j(const double jj)
Definition: vpImagePoint.h:177
vpDisplay::displayArrow
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:60
vpDisplayGDI
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
vpColor::yellow
static const vpColor yellow
Definition: vpColor.h:187
vpDisplay::displayRectangle
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:484
vpDisplayD3D
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
vpDisplay::displayPoint
static void displayPoint(const vpImage< unsigned char > &I, const vpImagePoint &ip, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:438
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:141
vpDisplay::displayCircle
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:149
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:68
vpColor::green
static const vpColor green
Definition: vpColor.h:182
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:676
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
vpDisplay::displayText
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay_uchar.cpp:600
vpDisplayOpenCV::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayOpenCV.cpp:346
vpImage::init
void init(unsigned int height, unsigned int width)
Set the size of the image.
Definition: vpImage.h:659
vpColor::black
static const vpColor black
Definition: vpColor.h:173
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
vpException::getMessage
const char * getMessage(void) const
Definition: vpException.cpp:89
vpDisplayWin32::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayWin32.cpp:78
vpDisplayX::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:257
vpColor::blue
static const vpColor blue
Definition: vpColor.h:185
vpDisplayGTK::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayGTK.cpp:257
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:652
vpDisplay::displayCross
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:179
vpImage< vpRGBa >
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:701
vpDisplay::displayLine
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:391
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:70
vpColor::red
static const vpColor red
Definition: vpColor.h:179
vpRect
Defines a rectangle in the plane.
Definition: vpRect.h:77
vpDisplayGDI::vpDisplayGDI
vpDisplayGDI()
Basic constructor.
Definition: vpDisplayGDI.cpp:54