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