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