Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testVideoDevice.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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#if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || \
46 defined(VISP_HAVE_OPENCV))
47
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpIoTools.h>
50#include <visp3/io/vpImageIo.h>
51#include <visp3/io/vpParseArgv.h>
52
53#include <visp3/gui/vpDisplayD3D.h>
54#include <visp3/gui/vpDisplayGDI.h>
55#include <visp3/gui/vpDisplayGTK.h>
56#include <visp3/gui/vpDisplayOpenCV.h>
57#include <visp3/gui/vpDisplayX.h>
58
66// List of allowed command line options
67#define GETOPTARGS "i:hlt:dc"
68
69typedef enum { vpX11, vpGTK, vpGDI, vpD3D, vpCV } vpDisplayType;
70
71void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
72bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
73 bool &display);
74
85void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
86{
87 fprintf(stdout, "\n\
88Test video devices or display.\n\
89\n\
90SYNOPSIS\n\
91 %s [-i <input image path>] \n\
92 [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
93",
94 name);
95
96 std::string display;
97 switch (dtype) {
98 case vpX11:
99 display = "X11";
100 break;
101 case vpGTK:
102 display = "GTK";
103 break;
104 case vpGDI:
105 display = "GDI";
106 break;
107 case vpD3D:
108 display = "D3D";
109 break;
110 case vpCV:
111 display = "CV";
112 break;
113 }
114
115 fprintf(stdout, "\n\
116OPTIONS: Default\n\
117 -i <input image path> %s\n\
118 Set image input path.\n\
119 From this path read \"Klimt/Klimt.pgm\"\n\
120 and \"Klimt/Klimt.ppm\" images.\n\
121 Setting the VISP_INPUT_IMAGE_PATH environment\n\
122 variable produces the same behaviour than using\n\
123 this option.\n\
124\n\
125 -t <type of video device> \"%s\"\n\
126 String specifying the video device to use.\n\
127 Possible values:\n\
128 \"X11\": only on UNIX platforms,\n\
129 \"GTK\": on all plaforms,\n\
130 \"GDI\": only on Windows platform (Graphics Device Interface),\n\
131 \"D3D\": only on Windows platform (Direct3D).\n\
132 \"CV\" : (OpenCV).\n\
133\n\
134 -c\n\
135 Disable the mouse click. Useful to automate the \n\
136 execution of this program without human intervention.\n\
137\n\
138 -d \n\
139 Turn off the display.\n\
140\n\
141 -l\n\
142 Print the list of video-devices available and exit.\n\
143\n\
144 -h\n\
145 Print the help.\n\n",
146 ipath.c_str(), display.c_str());
147
148 if (badparam)
149 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
150}
151
166bool getOptions(int argc, const char **argv, std::string &ipath, vpDisplayType &dtype, bool &list, bool &click_allowed,
167 bool &display)
168{
169 const char *optarg_;
170 int c;
171 std::string sDisplayType;
172 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
173
174 switch (c) {
175 case 'i':
176 ipath = optarg_;
177 break;
178 case 'l':
179 list = true;
180 break;
181 case 't':
182 sDisplayType = optarg_;
183 // Parse the display type option
184 if (sDisplayType.compare("X11") == 0) {
185 dtype = vpX11;
186 } else if (sDisplayType.compare("GTK") == 0) {
187 dtype = vpGTK;
188 } else if (sDisplayType.compare("GDI") == 0) {
189 dtype = vpGDI;
190 } else if (sDisplayType.compare("D3D") == 0) {
191 dtype = vpD3D;
192 } else if (sDisplayType.compare("CV") == 0) {
193 dtype = vpCV;
194 }
195
196 break;
197 case 'h':
198 usage(argv[0], NULL, ipath, dtype);
199 return false;
200 break;
201 case 'c':
202 click_allowed = false;
203 break;
204 case 'd':
205 display = false;
206 break;
207
208 default:
209 usage(argv[0], optarg_, ipath, dtype);
210 return false;
211 break;
212 }
213 }
214
215 if ((c == 1) || (c == -1)) {
216 // standalone param or error
217 usage(argv[0], NULL, ipath, dtype);
218 std::cerr << "ERROR: " << std::endl;
219 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
220 return false;
221 }
222
223 return true;
224}
225
226int main(int argc, const char **argv)
227{
228 try {
229 std::string env_ipath;
230 std::string opt_ipath;
231 bool opt_list = false; // To print the list of video devices
232 vpDisplayType opt_dtype; // Type of display to use
233 std::string ipath;
234 std::string filename;
235 bool opt_click_allowed = true;
236 bool opt_display = true;
237
238// Default display is one available
239#if defined(VISP_HAVE_GTK)
240 opt_dtype = vpGTK;
241#elif defined(VISP_HAVE_X11)
242 opt_dtype = vpX11;
243#elif defined(VISP_HAVE_GDI)
244 opt_dtype = vpGDI;
245#elif defined(VISP_HAVE_D3D9)
246 opt_dtype = vpD3D;
247#elif defined VISP_HAVE_OPENCV
248 opt_dtype = vpCV;
249#endif
250
251 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
252 // environment variable value
254
255 // Set the default input path
256 if (!env_ipath.empty())
257 ipath = env_ipath;
258
259 // Read the command line options
260 if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list, opt_click_allowed, opt_display) == false) {
261 return EXIT_FAILURE;
262 }
263
264 // Print the list of video-devices available
265 if (opt_list) {
266 unsigned nbDevices = 0;
267 std::cout << "List of video-devices available: \n";
268#if defined(VISP_HAVE_GTK)
269 std::cout << " GTK (use \"-t GTK\" option to use it)\n";
270 nbDevices++;
271#endif
272#if defined(VISP_HAVE_X11)
273 std::cout << " X11 (use \"-t X11\" option to use it)\n";
274 nbDevices++;
275#endif
276#if defined(VISP_HAVE_GDI)
277
278 std::cout << " GDI (use \"-t GDI\" option to use it)\n";
279 nbDevices++;
280#endif
281#if defined(VISP_HAVE_D3D9)
282 std::cout << " D3D (use \"-t D3D\" option to use it)\n";
283 nbDevices++;
284#endif
285#if defined VISP_HAVE_OPENCV
286 std::cout << " CV (use \"-t CV\" option to use it)\n";
287 nbDevices++;
288#endif
289 if (!nbDevices) {
290 std::cout << " No display is available\n";
291 }
292 return EXIT_FAILURE;
293 }
294
295 // Get the option values
296 if (!opt_ipath.empty())
297 ipath = opt_ipath;
298
299 // Compare ipath and env_ipath. If they differ, we take into account
300 // the input path comming from the command line option
301 if (!opt_ipath.empty() && !env_ipath.empty()) {
302 if (ipath != env_ipath) {
303 std::cout << std::endl << "WARNING: " << std::endl;
304 std::cout << " Since -i <visp image path=" << ipath << "> "
305 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
306 << " we skip the environment variable." << std::endl;
307 }
308 }
309
310 // Test if an input path is set
311 if (opt_ipath.empty() && env_ipath.empty()) {
312 usage(argv[0], NULL, ipath, opt_dtype);
313 std::cerr << std::endl << "ERROR:" << std::endl;
314 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
315 << " environment variable to specify the location of the " << std::endl
316 << " image path where test images are located." << std::endl
317 << std::endl;
318 return EXIT_FAILURE;
319 }
320
321 // Create a grey level image
323 // Create a color image
324 vpImage<vpRGBa> Irgba;
325
326 // Load a grey image from the disk
327 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
328 vpCTRACE << "Load " << filename << std::endl;
329 vpImageIo::read(I, filename);
330
331 // Load a color image from the disk
332 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
333 vpCTRACE << "Load " << filename << std::endl;
334 vpImageIo::read(Irgba, filename);
335
336 // Create a display for the image
337 vpDisplay *display = NULL;
338
339 switch (opt_dtype) {
340 case vpX11:
341 std::cout << "Requested X11 display functionalities..." << std::endl;
342#if defined(VISP_HAVE_X11)
343 display = new vpDisplayX;
344#else
345 std::cout << " Sorry, X11 video device is not available.\n";
346 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
347 return EXIT_FAILURE;
348#endif
349 break;
350 case vpGTK:
351 std::cout << "Requested GTK display functionalities..." << std::endl;
352#if defined(VISP_HAVE_GTK)
353 display = new vpDisplayGTK;
354#else
355 std::cout << " Sorry, GTK video device is not available.\n";
356 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
357 return EXIT_FAILURE;
358#endif
359 break;
360 case vpGDI:
361 std::cout << "Requested GDI display functionalities..." << std::endl;
362#if defined(VISP_HAVE_GDI)
363
364 display = new vpDisplayGDI;
365#else
366 std::cout << " Sorry, GDI video device is not available.\n";
367 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
368 return EXIT_FAILURE;
369#endif
370 break;
371 case vpD3D:
372 std::cout << "Requested D3D display functionalities..." << std::endl;
373#if defined(VISP_HAVE_D3D9)
374 display = new vpDisplayD3D;
375#else
376 std::cout << " Sorry, D3D video device is not available.\n";
377 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
378 return EXIT_FAILURE;
379#endif
380 break;
381 case vpCV:
382 std::cout << "Requested OpenCV display functionalities..." << std::endl;
383#if defined(HAVE_OPENCV_HIGHGUI)
384 display = new vpDisplayOpenCV;
385#else
386 std::cout << " Sorry, OpenCV video device is not available.\n";
387 std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
388 return EXIT_FAILURE;
389#endif
390 break;
391 }
392 if (opt_display) {
393
394 // We open a window using either X11 or GTK or GDI or D3D.
395 // Its size is automatically defined by the image (I) size
396 display->init(I, 100, 100, "Display...");
397
398 // Display the image
399 // The image class has a member that specify a pointer toward
400 // the display that has been initialized in the display declaration
401 // therefore is is no longer necessary to make a reference to the
402 // display variable.
404 // Flush the display
406 std::cout << "A click to continue...\n";
407 if (opt_click_allowed)
409
410 display->close(I);
411
412 // We open a window using either X11 or GTK or GDI or D3D
413 // but using anothe function who doesn't take title.
414 // Its size is automatically defined by the image (I) size
415
416 display->init(I, 100, 100);
417
418 // Display the image
419 // The image class has a member that specify a pointer toward
420 // the display that has been initialized in the display declaration
421 // therefore is is no longer necessary to make a reference to the
422 // display variable.
424 // Flush the display
426 std::cout << "A click to continue...\n";
427 if (opt_click_allowed)
429
430 display->close(I);
431
432 // We open a window using either X11 or GTK or GDI or D3D.
433 // Its size is automatically defined by the image (I) size
434
435 display->init(Irgba, 100, 100, "Color display...");
436
437 // Display the image
438 // The image class has a member that specify a pointer toward
439 // the display that has been initialized in the display declaration
440 // therefore is is no longer necessary to make a reference to the
441 // display variable.
442 vpDisplay::display(Irgba);
443 // Flush the display
444 vpDisplay::flush(Irgba);
445
446 std::cout << "A click to continue...\n";
447 if (opt_click_allowed)
448 vpDisplay::getClick(Irgba);
449
450 display->close(Irgba);
451
452 // We open a window using either X11 or GTK or GDI or D3D
453 // but using anothe function who doesn't take title.
454 // Its size is automatically defined by the image (I) size
455
456 display->init(Irgba, 100, 100);
457
458 // Display the image
459 // The image class has a member that specify a pointer toward
460 // the display that has been initialized in the display declaration
461 // therefore is is no longer necessary to make a reference to the
462 // display variable.
463 vpDisplay::display(Irgba);
464 // Flush the display
465 vpDisplay::flush(Irgba);
466
467 std::cout << "A click to exit...\n";
468 if (opt_click_allowed)
469 vpDisplay::getClick(Irgba);
470 }
471 delete display;
472 } catch (...) {
473 vpERROR_TRACE("Error while displaying the image");
474 return EXIT_FAILURE;
475 }
476}
477
478#else
479int main() { vpERROR_TRACE("You do not have display functionalities..."); }
480
481#endif
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition vpDisplayX.h:132
Class that defines generic functionalities for display.
Definition vpDisplay.h:173
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:135
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
#define vpCTRACE
Definition vpDebug.h:333
#define vpERROR_TRACE
Definition vpDebug.h:388