Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
displaySequence.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  * Read an image sequence from the disk and display it.
33  *
34 *****************************************************************************/
45 #include <iomanip>
46 #include <sstream>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpIoTools.h>
52 #include <visp3/io/vpParseArgv.h>
53 #if (defined(VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
54 
55 #include <visp3/core/vpImage.h>
56 #include <visp3/io/vpImageIo.h>
57 #include <visp3/gui/vpDisplayGDI.h>
58 #include <visp3/gui/vpDisplayGTK.h>
59 #include <visp3/gui/vpDisplayX.h>
60 #include <visp3/core/vpTime.h>
61 
72 // List of allowed command line options
73 #define GETOPTARGS "di:p:hf:l:s:w"
74 
75 #ifdef ENABLE_VISP_NAMESPACE
76 using namespace VISP_NAMESPACE_NAME;
77 #endif
78 
92 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath, unsigned first,
93  unsigned last, unsigned step)
94 {
95 #if VISP_HAVE_DATASET_VERSION >= 0x030600
96  std::string ext("png");
97 #else
98  std::string ext("pgm");
99 #endif
100  fprintf(stdout, "\n\
101 Read an image sequence from the disk and display it.\n\
102 The sequence is made of separate images. Each image corresponds\n\
103 to a PGM file.\n\
104 \n\
105 SYNOPSIS\n\
106  %s [-i <test image path>] [-p <personal image path>]\n\
107  [-f <first image>] [-l <last image>] [-s <step>] \n\
108  [-w] [-d] [-h]\n", name);
109 
110  fprintf(stdout, "\n\
111  OPTIONS: Default\n\
112  -i <test image path> %s\n\
113  Set image input path.\n\
114  From this path read \"cube/image.%%04d.%s\"\n\
115  images. These images come from ViSP-images-x.y.z.tar.gz\n\
116  available on the ViSP website.\n\
117  Setting the VISP_INPUT_IMAGE_PATH environment\n\
118  variable produces the same behaviour than using\n\
119  this option.\n\
120  \n\
121  -p <personal image path> %s\n\
122  Specify a personal sequence containing images \n\
123  to process.\n\
124  By image sequence, we mean one file per image.\n\
125  The format is selected by analysing the filename extension.\n\
126  Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
127  %%04d is for the image numbering.\n\
128  \n\
129  -f <first image> %u\n\
130  First image number of the sequence.\n\
131  \n\
132  -l <last image> %u\n\
133  last image number of the sequence.\n\
134  \n\
135  -s <step> %u\n\
136  Step between two images.\n\
137 \n\
138  -d \n\
139  Disable the image display. This can be useful \n\
140  for automatic tests using crontab under Unix or \n\
141  using the task manager under Windows.\n\
142 \n\
143  -w\n\
144  Wait for a mouse click between two images.\n\
145  If the image display is disabled (using -d)\n\
146  this option is without effect.\n\
147 \n\
148  -h\n\
149  Print the help.\n\n",
150  ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str(), first, last, step);
151 
152  if (badparam)
153  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
154 }
176 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, unsigned &first, unsigned &last,
177  unsigned &step, bool &display, bool &wait)
178 {
179  const char *optarg_;
180  int c;
181  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
182 
183  switch (c) {
184  case 'd':
185  display = false;
186  break;
187  case 'i':
188  ipath = optarg_;
189  break;
190  case 'p':
191  ppath = optarg_;
192  break;
193  case 'f':
194  first = (unsigned)atoi(optarg_);
195  break;
196  case 'l':
197  last = (unsigned)atoi(optarg_);
198  break;
199  case 's':
200  step = (unsigned)atoi(optarg_);
201  break;
202  case 'w':
203  wait = true;
204  break;
205  case 'h':
206  usage(argv[0], nullptr, ipath, ppath, first, last, step);
207  return false;
208  break;
209 
210  default:
211  usage(argv[0], optarg_, ipath, ppath, first, last, step);
212  return false;
213  break;
214  }
215  }
216 
217  if ((c == 1) || (c == -1)) {
218  // standalone param or error
219  usage(argv[0], nullptr, ipath, ppath, first, last, step);
220  std::cerr << "ERROR: " << std::endl;
221  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
222  return false;
223  }
224 
225  return true;
226 }
227 
228 int main(int argc, const char **argv)
229 {
230  try {
231  std::string env_ipath;
232  std::string opt_ipath;
233  std::string ipath;
234  std::string opt_ppath;
235  std::string dirname;
236  std::string filename;
237  unsigned opt_first = 0;
238  unsigned opt_last = 80;
239  unsigned opt_step = 1;
240  bool opt_display = true;
241  bool opt_wait = false;
242 
243 #if VISP_HAVE_DATASET_VERSION >= 0x030600
244  std::string ext("png");
245 #else
246  std::string ext("pgm");
247 #endif
248 
249  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
250  // environment variable value
251  env_ipath = vpIoTools::getViSPImagesDataPath();
252 
253  // Set the default input path
254  if (!env_ipath.empty())
255  ipath = env_ipath;
256 
257  // Read the command line options
258  if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_last, opt_step, opt_display, opt_wait) ==
259  false) {
260  return EXIT_FAILURE;
261  }
262 
263  if (!opt_display)
264  opt_wait = false; // turn off the waiting
265 
266  // Get the option values
267  if (!opt_ipath.empty())
268  ipath = opt_ipath;
269 
270  // Compare ipath and env_ipath. If they differ, we take into account
271  // the input path coming from the command line option
272  if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
273  if (ipath != env_ipath) {
274  std::cout << std::endl << "WARNING: " << std::endl;
275  std::cout << " Since -i <visp image path=" << ipath << "> "
276  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
277  << " we skip the environment variable." << std::endl;
278  }
279  }
280 
281  // Test if an input path is set
282  if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
283  usage(argv[0], nullptr, ipath, opt_ppath, opt_first, opt_last, opt_step);
284  std::cerr << std::endl << "ERROR:" << std::endl;
285  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
286  << " environment variable to specify the location of the " << std::endl
287  << " image path where test images are located." << std::endl
288  << " Use -p <personal image path> option if you want to " << std::endl
289  << " use personal images." << std::endl
290  << std::endl;
291 
292  return EXIT_FAILURE;
293  }
294 
295  // Declare an image, this is a gray level image (unsigned char)
296  // it size is not defined yet, it will be defined when the image will
297  // read on the disk
299 
300  unsigned iter = opt_first;
301  std::ostringstream s;
302  char cfilename[FILENAME_MAX];
303 
304  if (opt_ppath.empty()) {
305  // Warning : the daset is available https://visp.inria.fr/download/
306  dirname = vpIoTools::createFilePath(ipath, "cube");
307 
308  // Build the name of the image file
309 
310  s.setf(std::ios::right, std::ios::adjustfield);
311  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
312  filename = vpIoTools::createFilePath(dirname, s.str());
313  }
314  else {
315  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
316  filename = cfilename;
317  }
318  // Read image named "filename" and put the bitmap in I
319  try {
320  vpImageIo::read(I, filename);
321  }
322  catch (...) {
323  std::cerr << std::endl << "ERROR:" << std::endl;
324  std::cerr << " Cannot read " << filename << std::endl;
325  std::cerr << " Check your -i " << ipath << " option, " << std::endl
326  << " or your -p " << opt_ppath << " option " << std::endl
327  << " or VISP_INPUT_IMAGE_PATH environment variable" << std::endl;
328  return EXIT_FAILURE;
329  }
330 
331 #if defined(VISP_HAVE_GTK)
332  vpDisplayGTK display;
333 #elif defined(VISP_HAVE_X11)
334  vpDisplayX display;
335 #elif defined(VISP_HAVE_GDI)
336  vpDisplayGDI display;
337 #endif
338  if (opt_display) {
339 
340  // We open a window using either X11 or GTK or GDI.
341  // Its size is automatically defined by the image (I) size
342  display.init(I, 100, 100, "Display...");
343 
344  // Display the image
345  // The image class has a member that specify a pointer toward
346  // the display that has been initialized in the display declaration
347  // therefore is is no longer necessary to make a reference to the
348  // display variable.
350  vpDisplay::flush(I);
351  }
352 
353  // this is the loop over the image sequence
354  while (iter < opt_last) {
355  double tms = vpTime::measureTimeMs();
356 
357  // set the new image name
358  if (opt_ppath.empty()) {
359  s.str("");
360  s << "image." << std::setw(4) << std::setfill('0') << iter << "." << ext;
361  filename = vpIoTools::createFilePath(dirname, s.str());
362  }
363  else {
364  snprintf(cfilename, FILENAME_MAX, opt_ppath.c_str(), iter);
365  filename = cfilename;
366  }
367 
368  std::cout << "read : " << filename << std::endl;
369  // read the image
370  vpImageIo::read(I, filename);
371  if (opt_display) {
372  // Display the image
374  // Flush the display
375  vpDisplay::flush(I);
376  }
377  if (opt_wait) {
378  std::cout << "A click in the image to continue..." << std::endl;
379  // Wait for a blocking mouse click
381  }
382  else {
383  // Synchronise the loop to 40 ms
384  vpTime::wait(tms, 40);
385  }
386 
387  iter += opt_step;
388  }
389  // double tms_2 = vpTime::measureTimeMs() ;
390  // double tms_total = tms_2 - tms_1 ;
391  // std::cout << "Total Time : "<< tms_total<<std::endl;
392  return EXIT_SUCCESS;
393  }
394  catch (const vpException &e) {
395  std::cout << "Catch an exception: " << e << std::endl;
396  return EXIT_FAILURE;
397  }
398 }
399 #else
400 int main()
401 {
402  std::cout << "You do not have X11, or GDI (Graphical Device Interface), or GTK functionalities to display images..."
403  << std::endl;
404  std::cout << "Tip if you are on a unix-like system:" << std::endl;
405  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
406  std::cout << "Tip if you are on a windows-like system:" << std::endl;
407  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
408  return EXIT_SUCCESS;
409  }
410 #endif
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
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)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
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()