ViSP  2.8.0
grabDisk.cpp
1 /****************************************************************************
2  *
3  * $Id: grabDisk.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Read an image sequence from the disk and display it.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 
44 #include <visp/vpDebug.h>
45 #include <visp/vpConfig.h>
46 #include <stdlib.h>
47 #if (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
48 
49 #include <visp/vpDiskGrabber.h>
50 #include <visp/vpImage.h>
51 #include <visp/vpDisplay.h>
52 #include <visp/vpDisplayX.h>
53 #include <visp/vpDisplayGTK.h>
54 #include <visp/vpTime.h>
55 #include <visp/vpParseArgv.h>
56 
66 // List of allowed command line options
67 #define GETOPTARGS "b:de:f:i:hn:s:z:"
68 
69 /*
70 
71  Print the program options.
72 
73  \param name : Program name.
74  \param badparam : Bad parameter name.
75  \param ipath : Input image path.
76  \param basename : Input image base name.
77  \param ext : Input image extension.
78  \param first : First image number to read.
79  \param nimages : Number of images to read.
80  \param step : Step between two successive images to read.
81  \param nzero : Number of zero for the image number coding.
82 
83  */
84 void usage(const char *name, const char *badparam, std::string ipath, std::string basename,
85  std::string ext, int first, unsigned int nimages, int step,
86  unsigned int nzero)
87 {
88  fprintf(stdout, "\n\
89 Read an image sequence from the disk. Display it using X11 or GTK.\n\
90 The sequence is made of separate images. Each image corresponds\n\
91 to a PGM file.\n\
92 \n\
93 SYNOPSIS\n\
94  %s [-i <input image path>] [-b <base name>] [-e <extension>] \n\
95  [-f <first frame>] [-n <number of images> [-s <step>] \n\
96  [-z <number of zero>] [-d] [-h]\n", name);
97 
98  fprintf(stdout, "\n\
99 OPTIONS: Default\n\
100  -i <input image path> %s\n\
101  Set image input path.\n\
102  From this path read \"ViSP-images/cube/image.%%04d.pgm\"\n\
103  images.\n\
104  Setting the VISP_INPUT_IMAGE_PATH environment\n\
105  variable produces the same behaviour than using\n\
106  this option.\n\
107 \n\
108  -b <base name> %s\n\
109  Specify the base name of the files of the sequence\n\
110  containing the images to process. \n\
111  By image sequence, we mean one file per image.\n\
112  The following image file formats PNM (PGM P5, PPM P6)\n\
113  are supported. The format is selected by analysing \n\
114  the filename extension.\n\
115 \n\
116  -e <extension> %s\n\
117  Specify the extension of the files.\n\
118  Not taken into account for the moment. Will be a\n\
119  future feature...\n\
120 \n\
121  -f <first frame> %u\n\
122  First frame number of the sequence\n\
123 \n\
124  -n <number of images> %u\n\
125  Number of images to load from the sequence.\n\
126 \n\
127  -s <step> %u\n\
128  Step between two images.\n\
129 \n\
130  -z <number of zero> %u\n\
131  Number of digits to encode the image number.\n\
132 \n\
133  -d \n\
134  Turn off the display.\n\
135 \n\
136  -h \n\
137  Print the help.\n\n",
138  ipath.c_str(), basename.c_str(), ext.c_str(), first,
139  nimages, step, nzero);
140 
141  if (badparam)
142  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
143 }
162 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &basename,
163  std::string &ext, int &first, unsigned int &nimages,
164  int &step, unsigned int &nzero, bool &display)
165 {
166  const char *optarg;
167  int c;
168  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
169 
170  switch (c) {
171  case 'b': basename = optarg; break;
172  case 'd': display = false; break;
173  case 'e': ext = optarg; break;
174  case 'f': first = atoi(optarg); break;
175  case 'i': ipath = optarg; break;
176  case 'n': nimages = (unsigned) atoi(optarg); break;
177  case 's': step = atoi(optarg); break;
178  case 'z': nzero = (unsigned) atoi(optarg); break;
179  case 'h': usage(argv[0], NULL, ipath, basename, ext, first, nimages,
180  step, nzero); return false; break;
181 
182  default:
183  usage(argv[0], optarg, ipath, basename, ext, first, nimages,
184  step, nzero);
185  return false; break;
186  }
187  }
188 
189  if ((c == 1) || (c == -1)) {
190  // standalone param or error
191  usage(argv[0], NULL, ipath, basename, ext, first, nimages, step, nzero);
192  std::cerr << "ERROR: " << std::endl;
193  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
194  return false;
195  }
196 
197  return true;
198 }
199 
200 
210 int
211 main(int argc, const char ** argv)
212 {
213  std::string env_ipath;
214  std::string opt_ipath;
215  std::string ipath;
216  std::string opt_basename = "ViSP-images/cube/image.";
217  std::string opt_ext = "pgm";
218  bool opt_display = true;
219 
220  int opt_first = 5;
221  unsigned int opt_nimages = 70;
222  int opt_step = 1;
223  unsigned int opt_nzero = 4;
224 
225  // Get the VISP_IMAGE_PATH environment variable value
226  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
227  if (ptenv != NULL)
228  env_ipath = ptenv;
229 
230  // Set the default input path
231  if (! env_ipath.empty())
232  ipath = env_ipath;
233 
234  // Read the command line options
235  if (getOptions(argc, argv, opt_ipath, opt_basename, opt_ext, opt_first,
236  opt_nimages, opt_step, opt_nzero, opt_display) == false) {
237  exit (-1);
238  }
239 
240  // Get the option values
241  if (!opt_ipath.empty())
242  ipath = opt_ipath;
243 
244  // Compare ipath and env_ipath. If they differ, we take into account
245  // the input path comming from the command line option
246  if (!opt_ipath.empty() && !env_ipath.empty()) {
247  if (ipath != env_ipath) {
248  std::cout << std::endl
249  << "WARNING: " << std::endl;
250  std::cout << " Since -i <visp image path=" << ipath << "> "
251  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
252  << " we skip the environment variable." << std::endl;
253  }
254  }
255 
256  // Test if an input path is set
257  if (opt_ipath.empty() && env_ipath.empty()){
258  usage(argv[0], NULL, ipath, opt_basename, opt_ext, opt_first,
259  opt_nimages, opt_step, opt_nzero);
260  std::cerr << std::endl
261  << "ERROR:" << std::endl;
262  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
263  << std::endl
264  << " environment variable to specify the location of the " << std::endl
265  << " image path where test images are located." << std::endl << std::endl;
266  exit(-1);
267  }
268 
269 
270  // Declare an image, this is a gray level image (unsigned char)
271  // it size is not defined yet, it will be defined when the image will
272  // read on the disk
274 
275 
276  // Declare a framegrabber able to read a sequence of successive
277  // images from the disk
278  vpDiskGrabber g;
279 
280  // Set the path to the directory containing the sequence
281  g.setDirectory(ipath.c_str());
282  // Set the image base name. The directory and the base name constitute
283  // the constant part of the full filename
284  g.setBaseName(opt_basename.c_str());
285  // Set the step between two images of the sequence
286  g.setStep(opt_step);
287  // Set the number of digits to build the image number
288  g.setNumberOfZero(opt_nzero);
289  // Set the first frame number of the sequence
290  g.setImageNumber(opt_first);
291  // Set the image extension
292  g.setExtension(opt_ext.c_str());
293 
294  // Open the framegrabber by loading the first image of the sequence
295  try {
296  g.open(I) ;
297  }
298  catch (...) {
299  vpERROR_TRACE("Cannot open the first image of the sequence... ") ;
300  exit(-1);
301  }
302 
303  std::cout << "Image size: width : " << I.getWidth() << " height: "
304  << I.getHeight() << std::endl;
305 
306  // We open a window using either X11 or GTK.
307  // Its size is automatically defined by the image (I) size
308 #if defined VISP_HAVE_X11
309  vpDisplayX display;
310 #elif defined VISP_HAVE_GTK
311  vpDisplayGTK display;
312 #endif
313 
314  if (opt_display) {
315  try {
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 longuer necessary to make a reference to the
322  // display variable.
323  vpDisplay::display(I) ;
324  vpDisplay::flush(I) ;
325  }
326  catch(...)
327  {
328  vpERROR_TRACE("Cannot display the image ") ;
329  exit(-1);
330  }
331  }
332 
333  unsigned cpt = 1;
334  // this is the loop over the image sequence
335  try {
336  while(cpt ++ < opt_nimages)
337  {
338  double tms = vpTime::measureTimeMs();
339  // read the image and then increment the image counter so that the next
340  // call to acquire(I) will get the next image
341  g.acquire(I) ;
342  if (opt_display) {
343  // Display the image
344  vpDisplay::display(I) ;
345  // Flush the display
346  vpDisplay::flush(I) ;
347  }
348  // Synchronise the loop to 40 ms
349  vpTime::wait(tms, 40) ;
350 
351  }
352  }
353  catch(...) {
354  vpERROR_TRACE("Error during the framegrabbing...");
355  }
356 }
357 
358 #else
359 int
360 main()
361 {
362  vpERROR_TRACE("You do not have X11 or GTK display functionalities...");
363 }
364 
365 #endif
366 
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpERROR_TRACE
Definition: vpDebug.h:379
void setBaseName(const char *name)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
void setDirectory(const char *dir)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1991
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
void setNumberOfZero(unsigned int noz)
void setStep(int a)
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void setImageNumber(long number)
void open(vpImage< unsigned char > &I)
Class to grab (ie. read) images from the disk.
void setExtension(const char *ext)
unsigned int getHeight() const
Definition: vpImage.h:150
void acquire(vpImage< unsigned char > &I)