Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testMouseEvent.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Read an image sequence from the disk and display it.
32  */
33 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/io/vpParseArgv.h>
48 
49 #include <iomanip>
50 #include <sstream>
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9))
55 
56 #include <visp3/core/vpImage.h>
57 #include <visp3/io/vpImageIo.h>
58 
59 #include <visp3/core/vpMouseButton.h>
60 #include <visp3/gui/vpDisplayD3D.h>
61 #include <visp3/gui/vpDisplayGDI.h>
62 #include <visp3/gui/vpDisplayGTK.h>
63 #include <visp3/gui/vpDisplayX.h>
64 
65 #include <visp3/core/vpTime.h>
66 
67 // List of allowed command line options
68 #define GETOPTARGS "cdi:Lp:ht:f:l:s:w"
69 
70 #ifdef ENABLE_VISP_NAMESPACE
71 using namespace VISP_NAMESPACE_NAME;
72 #endif
73 
74 typedef enum
75 {
76  vpX11,
77  vpGTK,
78  vpGDI,
79  vpD3D,
80 } vpDisplayType;
81 
96 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first, unsigned last,
97  unsigned step, vpDisplayType &dtype)
98 {
99 #if VISP_HAVE_DATASET_VERSION >= 0x030600
100  std::string ext("png");
101 #else
102  std::string ext("pgm");
103 #endif
104 
105  fprintf(stdout, "\n\
106 Read an image sequence from the disk and display it.\n\
107 The sequence is made of separate images. Each image corresponds\n\
108 to a PGM file.\n\
109 \n\
110 SYNOPSIS\n\
111  %s [-i <test image path>] [-p <personal image path>]\n\
112  [-f <first image>] [-l <last image>] [-s <step>] \n\
113  [-t <type of video device>] [-L] [-w] [-c] [-d] [-h]\n\
114  ",
115  name);
116 
117  std::string display;
118  switch (dtype) {
119  case vpX11:
120  display = "X11";
121  break;
122  case vpGTK:
123  display = "GTK";
124  break;
125  case vpGDI:
126  display = "GDI";
127  break;
128  case vpD3D:
129  display = "D3D";
130  break;
131  }
132 
133  fprintf(stdout, "\n\
134  OPTIONS: Default\n\
135  -i <test image path> %s\n\
136  Set image input path.\n\
137  From this path read \"cube/image.%%04d.%s\"\n\
138  images. These images come from ViSP-images-x.y.z.tar.gz\n\
139  available on the ViSP website.\n\
140  Setting the VISP_INPUT_IMAGE_PATH environment\n\
141  variable produces the same behaviour than using\n\
142  this option.\n\
143  \n\
144  -p <personal image path> %s\n\
145  Specify a personal sequence containing images \n\
146  to process.\n\
147  By image sequence, we mean one file per image.\n\
148  The format is selected by analysing the filename extension.\n\
149  Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
150  %%04d is for the image numbering.\n\
151  \n\
152  -f <first image> %u\n\
153  First image number of the sequence.\n\
154  \n\
155  -l <last image> %u\n\
156  Last image number of the sequence.\n\
157  \n\
158  -s <step> %u\n\
159  Step between two images.\n\
160 \n\
161  -t <type of video device> \"%s\"\n\
162  String specifying the video device to use.\n\
163  Possible values:\n\
164  \"X11\": only on UNIX platforms,\n\
165  \"GTK\": on all plaforms,\n\
166  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
167  \"D3D\": only on Windows platform (Direct3D).\n\
168 \n\
169  -L\n\
170  Print the list of video-devices available and exit.\n\
171 \n\
172  -c\n\
173  Disable mouse click.\n\
174 \n\
175  -d\n\
176  Disable the image display. This can be useful \n\
177  for automatic tests using crontab under Unix or \n\
178  using the task manager under Windows.\n\
179 \n\
180  -w\n\
181  Wait for a mouse click between two images.\n\
182  If the image display is disabled (using -d)\n\
183  this option is without effect.\n\
184 \n\
185  -h\n\
186  Print the help.\n\n",
187  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step, display.c_str());
188 
189  if (badparam)
190  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
191 }
216 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
217  unsigned &step, vpDisplayType &dtype, bool &list, bool &display, bool &click, bool &wait)
218 {
219  const char *optarg_;
220  int c;
221  std::string sDisplayType;
222  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
223 
224  switch (c) {
225  case 'c':
226  click = false;
227  break;
228  case 'd':
229  display = false;
230  break;
231  case 't':
232  sDisplayType = optarg_;
233  // Parse the display type option
234  if (sDisplayType.compare("X11") == 0) {
235  dtype = vpX11;
236  }
237  else if (sDisplayType.compare("GTK") == 0) {
238  dtype = vpGTK;
239  }
240  else if (sDisplayType.compare("GDI") == 0) {
241  dtype = vpGDI;
242  }
243  else if (sDisplayType.compare("D3D") == 0) {
244  dtype = vpD3D;
245  }
246 
247  break;
248  case 'i':
249  ipath = optarg_;
250  break;
251  case 'L':
252  list = true;
253  break;
254  case 'p':
255  ppath = optarg_;
256  break;
257  case 'f':
258  first = (unsigned)atoi(optarg_);
259  break;
260  case 'l':
261  last = (unsigned)atoi(optarg_);
262  break;
263  case 's':
264  step = (unsigned)atoi(optarg_);
265  break;
266  case 'w':
267  wait = true;
268  break;
269  case 'h':
270  usage(argv[0], nullptr, ipath, ppath, first, last, step, dtype);
271  return false;
272  break;
273 
274  default:
275  usage(argv[0], optarg_, ipath, ppath, first, last, step, dtype);
276  return false;
277  break;
278  }
279  }
280 
281  if ((c == 1) || (c == -1)) {
282  // standalone param or error
283  usage(argv[0], nullptr, ipath, ppath, first, last, step, dtype);
284  std::cerr << "ERROR: " << std::endl;
285  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
286  return false;
287  }
288 
289  return true;
290 }
291 
292 int main(int argc, const char **argv)
293 {
294  std::string env_ipath;
295  std::string opt_ipath;
296  std::string ipath;
297  std::string opt_ppath;
298  std::string dirname;
299  std::string filename;
300  unsigned opt_first = 30;
301  unsigned opt_last = 40;
302  unsigned opt_step = 1;
303  vpDisplayType opt_dtype; // Type of display to use
304  bool opt_list = false; // To print the list of video devices
305  bool opt_display = true;
306  bool opt_click = true;
307  bool opt_click_blocking = false;
308 
309 #if VISP_HAVE_DATASET_VERSION >= 0x030600
310  std::string ext("png");
311 #else
312  std::string ext("pgm");
313 #endif
314 
315 // Default display is one available
316 #if defined(VISP_HAVE_GTK)
317  opt_dtype = vpGTK;
318 #elif defined(VISP_HAVE_X11)
319  opt_dtype = vpX11;
320 #elif defined(VISP_HAVE_GDI)
321  opt_dtype = vpGDI;
322 #elif defined(VISP_HAVE_D3D9)
323  opt_dtype = vpD3D;
324 #endif
325 
326  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
327  // environment variable value
328  env_ipath = vpIoTools::getViSPImagesDataPath();
329 
330  // Set the default input path
331  if (!env_ipath.empty())
332  ipath = env_ipath;
333 
334  // Read the command line options
335  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_dtype, opt_list, opt_display,
336  opt_click, opt_click_blocking) == false) {
337  return EXIT_FAILURE;
338  }
339  // Print the list of video-devices available
340  if (opt_list) {
341  unsigned nbDevices = 0;
342  std::cout << "List of video-devices available: \n";
343 #if defined(VISP_HAVE_GTK)
344  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
345  nbDevices++;
346 #endif
347 #if defined(VISP_HAVE_X11)
348  std::cout << " X11 (use \"-t X11\" option to use it)\n";
349  nbDevices++;
350 #endif
351 #if defined(VISP_HAVE_GDI)
352 
353  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
354  nbDevices++;
355 #endif
356 #if defined(VISP_HAVE_D3D9)
357  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
358  nbDevices++;
359 #endif
360  if (!nbDevices) {
361  std::cout << " No display is available\n";
362  }
363  return EXIT_FAILURE;
364  }
365 
366  if (!opt_display)
367  opt_click_blocking = false; // turn off the waiting
368 
369  // Get the option values
370  if (!opt_ipath.empty())
371  ipath = opt_ipath;
372 
373  // Compare ipath and env_ipath. If they differ, we take into account
374  // the input path coming from the command line option
375  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
376  if (ipath != env_ipath) {
377  std::cout << std::endl << "WARNING: " << std::endl;
378  std::cout << " Since -i <visp image path=" << ipath << "> "
379  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
380  << " we skip the environment variable." << std::endl;
381  }
382  }
383 
384  // Test if an input path is set
385  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
386  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step, opt_dtype);
387  std::cerr << std::endl << "ERROR:" << std::endl;
388  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
389  << " environment variable to specify the location of the " << std::endl
390  << " image path where test images are located." << std::endl
391  << " Use -p <personal image path> option if you want to " << std::endl
392  << " use personal images." << std::endl
393  << std::endl;
394 
395  return EXIT_FAILURE;
396  }
397 
398  // Declare an image, this is a gray level image (unsigned char)
399  // it size is not defined yet, it will be defined when the image will
400  // read on the disk
402 
403  unsigned iter = opt_first;
404  std::ostringstream s;
405  char cfilename[FILENAME_MAX];
406 
407  if (opt_ppath.empty()) {
408  // Warning : the datset is available on https://visp.inria.fr/download/
409  dirname = vpIoTools::createFilePath(ipath, "cube");
410 
411  // Build the name of the image file
412  s.setf(std::ios::right, std::ios::adjustfield);
413  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
414  filename = vpIoTools::createFilePath(dirname, s.str());
415  }
416  else {
417 
418  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
419  filename = cfilename;
420  }
421  // Read image named "filename" and put the bitmap in I
422  try {
423  vpImageIo::read(I, filename);
424  }
425  catch (...) {
426  std::cerr << std::endl << "ERROR:" << std::endl;
427  std::cerr << " Cannot read " << filename << std::endl;
428  std::cerr << " Check your -i " << ipath << " option, " << std::endl
429  << " or your -p " << opt_ppath << " option " << std::endl
430  << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
431  return EXIT_FAILURE;
432  }
433  // Create a display for the image
434  vpDisplay *display = nullptr;
435 
436  switch (opt_dtype) {
437  case vpX11:
438  std::cout << "Requested X11 display functionalities..." << std::endl;
439 #if defined(VISP_HAVE_X11)
440  display = new vpDisplayX;
441 #else
442  std::cout << " Sorry, X11 video device is not available.\n";
443  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
444  return EXIT_FAILURE;
445 #endif
446  break;
447  case vpGTK:
448  std::cout << "Requested GTK display functionalities..." << std::endl;
449 #if defined(VISP_HAVE_GTK)
450  display = new vpDisplayGTK;
451 #else
452  std::cout << " Sorry, GTK video device is not available.\n";
453  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
454  return EXIT_FAILURE;
455 #endif
456  break;
457  case vpGDI:
458  std::cout << "Requested GDI display functionalities..." << std::endl;
459 #if defined(VISP_HAVE_GDI)
460 
461  display = new vpDisplayGDI;
462 #else
463  std::cout << " Sorry, GDI video device is not available.\n";
464  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
465  return EXIT_FAILURE;
466 #endif
467  break;
468  case vpD3D:
469  std::cout << "Requested D3D display functionalities..." << std::endl;
470 #if defined(VISP_HAVE_D3D9)
471  display = new vpDisplayD3D;
472 #else
473  std::cout << " Sorry, D3D video device is not available.\n";
474  std::cout << "Use \"" << argv[0] << " -l\" to print the list of available devices.\n";
475  return EXIT_FAILURE;
476 #endif
477  break;
478  }
479 
480  if (opt_display) {
481  try {
482  // We open a window using either X11 or GTK or GDI.
483  // Its size is automatically defined by the image (I) size
484  display->init(I, 100, 100, "Display...");
485 
486  // Display the image
487  // The image class has a member that specify a pointer toward
488  // the display that has been initialized in the display declaration
489  // therefore is is no longer necessary to make a reference to the
490  // display variable.
492  vpDisplay::flush(I);
493  }
494  catch (...) {
495  vpERROR_TRACE("Error while displaying the image");
496  delete display;
497  return EXIT_FAILURE;
498  }
499  }
500 
501  // this is the loop over the image sequence
502  while (iter < opt_last) {
503  try {
504  double tms = vpTime::measureTimeMs();
505 
506  // set the new image name
507  if (opt_ppath.empty()) {
508  s.str("");
509  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
510  filename = vpIoTools::createFilePath(dirname, s.str());
511  }
512  else {
513  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
514  filename = cfilename;
515  }
516 
517  std::cout << "read : " << filename << std::endl;
518  // read the image
519  vpImageIo::read(I, filename);
520  if (opt_display) {
521  // Display the image
523  // Flush the display
524  vpDisplay::flush(I);
525 
526  if (opt_click_blocking) {
527  std::cout << "A click in the image to continue..." << std::endl;
528  }
529  vpImagePoint ip;
530 
531  if (opt_click) {
533  bool pressed = vpDisplay::getClick(I, ip, button, opt_click_blocking);
534  if (pressed) {
535  switch (button) {
537  std::cout << "Left button was pressed." << std::endl;
538  break;
540  std::cout << "Middle button was pressed." << std::endl;
541  break;
543  std::cout << "Right button was pressed. Bye. " << std::endl;
544  delete display;
545  return EXIT_SUCCESS;
546  break;
547  case vpMouseButton::none:
548  break;
549  }
550  }
551  }
552  vpTime::wait(tms, 1000);
553  }
554  else {
555  // Synchronise the loop to 40 ms
556  vpTime::wait(tms, 40);
557  }
558  }
559  catch (...) {
560  delete display;
561  return EXIT_FAILURE;
562  }
563  iter += opt_step;
564  }
565  delete display;
566 }
567 #else
568 int main() { vpERROR_TRACE("You do not have X11 or GTK display functionalities..."); }
569 
570 #endif
Display for windows using Direct3D 3rd party. Thus to enable this class Direct3D should be installed....
Definition: vpDisplayD3D.h:106
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:133
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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: vpImageIo.cpp:147
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()