Visual Servoing Platform  version 3.6.1 under development (2024-05-18)
grabDisk.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 *****************************************************************************/
35 
36 #include <stdlib.h>
37 #include <visp3/core/vpConfig.h>
38 #include <visp3/core/vpDebug.h>
39 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI))
40 
41 #include <visp3/core/vpDisplay.h>
42 #include <visp3/core/vpImage.h>
43 #include <visp3/core/vpIoTools.h>
44 #include <visp3/core/vpTime.h>
45 #include <visp3/gui/vpDisplayGDI.h>
46 #include <visp3/gui/vpDisplayX.h>
47 #include <visp3/io/vpDiskGrabber.h>
48 #include <visp3/io/vpParseArgv.h>
49 
60 // List of allowed command line options
61 #define GETOPTARGS "b:de:f:hi:l:s:z:"
62 
63 /*
64 
65  Print the program options.
66 
67  \param name : Program name.
68  \param badparam : Bad parameter name.
69  \param ipath : Input image path.
70  \param basename : Input image base name.
71  \param ext : Input image extension.
72  \param first : First image number to read.
73  \param last : Number of images to read.
74  \param step : Step between two successive images to read.
75  \param nzero : Number of zero for the image number coding.
76 
77  */
78 void usage(const char *name, const char *badparam, std::string ipath, std::string basename, std::string ext, long first,
79  long last, long step, unsigned int nzero)
80 {
81  fprintf(stdout, "\n\
82 Read an image sequence from the disk. Display it using X11 or GTK.\n\
83 The sequence is made of separate images. Each image corresponds\n\
84 to a PGM file.\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-i <input image path>] [-b <base name>] [-e <extension>] \n\
88  [-f <first frame>] [-l <last image> [-s <step>] \n\
89  [-z <number of zero>] [-d] [-h]\n",
90  name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -i <input image path> %s\n\
95  Set image input path.\n\
96  From this path read \"cube/image.%%04d.%s\"\n\
97  images.\n\
98  Setting the VISP_INPUT_IMAGE_PATH environment\n\
99  variable produces the same behaviour than using\n\
100  this option.\n\
101 \n\
102  -b <base name> %s\n\
103  Specify the base name of the files of the sequence\n\
104  containing the images to process. \n\
105  By image sequence, we mean one file per image.\n\
106  The format is selected by analysing the filename extension.\n\
107 \n\
108  -e <extension> %s\n\
109  Specify the extension of the files.\n\
110  Not taken into account for the moment. Will be a\n\
111  future feature...\n\
112 \n\
113  -f <first frame> %ld\n\
114  First frame number of the sequence.\n\
115 \n\
116  -l <last image > %ld\n\
117  Last frame number of the sequence.\n\
118 \n\
119  -s <step> %ld\n\
120  Step between two images.\n\
121 \n\
122  -z <number of zero> %u\n\
123  Number of digits to encode the image number.\n\
124 \n\
125  -d \n\
126  Turn off the display.\n\
127 \n\
128  -h \n\
129  Print the help.\n\n",
130  ipath.c_str(), ext.c_str(), basename.c_str(), ext.c_str(), first, last, step, nzero);
131 
132  if (badparam)
133  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
134 }
153 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &basename, std::string &ext, long &first,
154  long &last, long &step, unsigned int &nzero, bool &display)
155 {
156  const char *optarg_;
157  int c;
158  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
159 
160  switch (c) {
161  case 'b':
162  basename = optarg_;
163  break;
164  case 'd':
165  display = false;
166  break;
167  case 'e':
168  ext = optarg_;
169  break;
170  case 'f':
171  first = atol(optarg_);
172  break;
173  case 'i':
174  ipath = optarg_;
175  break;
176  case 'l':
177  last = std::atol(optarg_);
178  break;
179  case 's':
180  step = atol(optarg_);
181  break;
182  case 'z':
183  nzero = (unsigned)atoi(optarg_);
184  break;
185  case 'h':
186  usage(argv[0], nullptr, ipath, basename, ext, first, last, step, nzero);
187  return false;
188  break;
189 
190  default:
191  usage(argv[0], optarg_, ipath, basename, ext, first, last, step, nzero);
192  return false;
193  break;
194  }
195  }
196 
197  if ((c == 1) || (c == -1)) {
198  // standalone param or error
199  usage(argv[0], nullptr, ipath, basename, ext, first, last, step, nzero);
200  std::cerr << "ERROR: " << std::endl;
201  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
202  return false;
203  }
204 
205  return true;
206 }
207 
217 int main(int argc, const char **argv)
218 {
219  try {
220  std::string env_ipath;
221  std::string opt_ipath;
222  std::string ipath;
223  std::string opt_basename = "cube/image.";
224 #if VISP_HAVE_DATASET_VERSION >= 0x030600
225  std::string opt_ext("png");
226 #else
227  std::string opt_ext("pgm");
228 #endif
229 
230  bool opt_display = true;
231 
232  long opt_first = 5;
233  long opt_last = 70;
234  long opt_step = 1;
235  unsigned int opt_nzero = 4;
236 
237  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
238  // environment variable value
239  env_ipath = vpIoTools::getViSPImagesDataPath();
240 
241  // Set the default input path
242  if (!env_ipath.empty())
243  ipath = env_ipath;
244 
245  // Read the command line options
246  if (getOptions(argc, argv, opt_ipath, opt_basename, opt_ext, opt_first, opt_last, opt_step, opt_nzero,
247  opt_display) == false) {
248  return EXIT_FAILURE;
249  }
250 
251  // Get the option values
252  if (!opt_ipath.empty())
253  ipath = opt_ipath;
254 
255  // Compare ipath and env_ipath. If they differ, we take into account
256  // the input path coming from the command line option
257  if (!opt_ipath.empty() && !env_ipath.empty()) {
258  if (ipath != env_ipath) {
259  std::cout << std::endl << "WARNING: " << std::endl;
260  std::cout << " Since -i <visp image path=" << ipath << "> "
261  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
262  << " we skip the environment variable." << std::endl;
263  }
264  }
265 
266  // Test if an input path is set
267  if (opt_ipath.empty() && env_ipath.empty()) {
268  usage(argv[0], nullptr, ipath, opt_basename, opt_ext, opt_first, opt_last, opt_step, opt_nzero);
269  std::cerr << std::endl << "ERROR:" << std::endl;
270  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
271  << " environment variable to specify the location of the " << std::endl
272  << " image path where test images are located." << std::endl
273  << std::endl;
274  return EXIT_FAILURE;
275  }
276 
277  // Declare an image, this is a gray level image (unsigned char)
278  // it size is not defined yet, it will be defined when the image will
279  // read on the disk
281 
282  // Declare a framegrabber able to read a sequence of successive
283  // images from the disk
284  vpDiskGrabber g;
285 
286  // Set the path to the directory containing the sequence
287  g.setDirectory(ipath.c_str());
288  // Set the image base name. The directory and the base name constitute
289  // the constant part of the full filename
290  g.setBaseName(opt_basename.c_str());
291  // Set the step between two images of the sequence
292  g.setStep(opt_step);
293  // Set the number of digits to build the image number
294  g.setNumberOfZero(opt_nzero);
295  // Set the first frame number of the sequence
296  g.setImageNumber(opt_first);
297  // Set the image extension
298  g.setExtension(opt_ext.c_str());
299 
300  // Open the framegrabber by loading the first image of the sequence
301  g.open(I);
302 
303  std::cout << "Image size: width : " << I.getWidth() << " height: " << I.getHeight() << std::endl;
304 
305  // We open a window using either X11 or GDI.
306  // Its size is automatically defined by the image (I) size
307 #if defined(VISP_HAVE_X11)
308  vpDisplayX display(I);
309 #elif defined(VISP_HAVE_GDI)
311 #else
312  std::cout << "No image viewer is available..." << std::endl;
313 #endif
314 
315  if (opt_display) {
316  display.init(I, 100, 100, "Disk Framegrabber");
317 
318  // display the image
319  // The image class has a member that specify a pointer toward
320  // the display that has been initialized in the display declaration
321  // therefore is is no longer necessary to make a reference to the
322  // display variable.
324  vpDisplay::flush(I);
325  }
326 
327  // this is the loop over the image sequence
328  while (g.getImageNumber() < opt_last) {
329  double tms = vpTime::measureTimeMs();
330  // read the image and then increment the image counter so that the next
331  // call to acquire(I) will get the next image
332  g.acquire(I);
333  if (opt_display) {
334  // Display the image
336  // Flush the display
337  vpDisplay::flush(I);
338  }
339  // Synchronise the loop to 40 ms
340  vpTime::wait(tms, 40);
341  }
342  return EXIT_SUCCESS;
343  }
344  catch (const vpException &e) {
345  std::cout << "Catch an exception: " << e << std::endl;
346  return EXIT_FAILURE;
347  }
348 }
349 
350 #else
351 int main()
352 {
353  std::cout << "You do not have X11, or GDI (Graphical Device Interface) functionalities to display images..."
354  << std::endl;
355  std::cout << "Tip if you are on a unix-like system:" << std::endl;
356  std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
357  std::cout << "Tip if you are on a windows-like system:" << std::endl;
358  std::cout << "- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
359  return EXIT_SUCCESS;
360 }
361 #endif
Class to grab (ie. read) images from the disk.
void setStep(long step)
void setDirectory(const std::string &dir)
void setExtension(const std::string &ext)
void open(vpImage< unsigned char > &I)
void setImageNumber(long number)
void setNumberOfZero(unsigned int noz)
void setBaseName(const std::string &name)
long getImageNumber() const
void acquire(vpImage< unsigned char > &I)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.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 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
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1834
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()