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