ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Modules Pages
grabDisk.cpp
1 /****************************************************************************
2  *
3  * $Id: grabDisk.cpp 4659 2014-02-09 14:11:51Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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_GDI))
48 
49 #include <visp/vpDiskGrabber.h>
50 #include <visp/vpImage.h>
51 #include <visp/vpDisplay.h>
52 #include <visp/vpDisplayX.h>
53 #include <visp/vpDisplayGDI.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 void usage(const char *name, const char *badparam, std::string ipath, std::string basename,
70  std::string ext, int first, unsigned int nimages, int step, unsigned int nzero);
71 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &basename,
72  std::string &ext, int &first, unsigned int &nimages,
73  int &step, unsigned int &nzero, bool &display);
74 
75 /*
76 
77  Print the program options.
78 
79  \param name : Program name.
80  \param badparam : Bad parameter name.
81  \param ipath : Input image path.
82  \param basename : Input image base name.
83  \param ext : Input image extension.
84  \param first : First image number to read.
85  \param nimages : Number of images to read.
86  \param step : Step between two successive images to read.
87  \param nzero : Number of zero for the image number coding.
88 
89  */
90 void usage(const char *name, const char *badparam, std::string ipath, std::string basename,
91  std::string ext, int first, unsigned int nimages, int step, unsigned int nzero)
92 {
93  fprintf(stdout, "\n\
94 Read an image sequence from the disk. Display it using X11 or GTK.\n\
95 The sequence is made of separate images. Each image corresponds\n\
96 to a PGM file.\n\
97 \n\
98 SYNOPSIS\n\
99  %s [-i <input image path>] [-b <base name>] [-e <extension>] \n\
100  [-f <first frame>] [-n <number of images> [-s <step>] \n\
101  [-z <number of zero>] [-d] [-h]\n", name);
102 
103  fprintf(stdout, "\n\
104 OPTIONS: Default\n\
105  -i <input image path> %s\n\
106  Set image input path.\n\
107  From this path read \"ViSP-images/cube/image.%%04d.pgm\"\n\
108  images.\n\
109  Setting the VISP_INPUT_IMAGE_PATH environment\n\
110  variable produces the same behaviour than using\n\
111  this option.\n\
112 \n\
113  -b <base name> %s\n\
114  Specify the base name of the files of the sequence\n\
115  containing the images to process. \n\
116  By image sequence, we mean one file per image.\n\
117  The following image file formats PNM (PGM P5, PPM P6)\n\
118  are supported. The format is selected by analysing \n\
119  the filename extension.\n\
120 \n\
121  -e <extension> %s\n\
122  Specify the extension of the files.\n\
123  Not taken into account for the moment. Will be a\n\
124  future feature...\n\
125 \n\
126  -f <first frame> %u\n\
127  First frame number of the sequence\n\
128 \n\
129  -n <number of images> %u\n\
130  Number of images to load from the sequence.\n\
131 \n\
132  -s <step> %u\n\
133  Step between two images.\n\
134 \n\
135  -z <number of zero> %u\n\
136  Number of digits to encode the image number.\n\
137 \n\
138  -d \n\
139  Turn off the display.\n\
140 \n\
141  -h \n\
142  Print the help.\n\n",
143  ipath.c_str(), basename.c_str(), ext.c_str(), first,
144  nimages, step, nzero);
145 
146  if (badparam)
147  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
148 }
167 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &basename,
168  std::string &ext, int &first, unsigned int &nimages,
169  int &step, unsigned int &nzero, bool &display)
170 {
171  const char *optarg_;
172  int c;
173  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
174 
175  switch (c) {
176  case 'b': basename = optarg_; break;
177  case 'd': display = false; break;
178  case 'e': ext = optarg_; break;
179  case 'f': first = atoi(optarg_); break;
180  case 'i': ipath = optarg_; break;
181  case 'n': nimages = (unsigned) atoi(optarg_); break;
182  case 's': step = atoi(optarg_); break;
183  case 'z': nzero = (unsigned) atoi(optarg_); break;
184  case 'h': usage(argv[0], NULL, ipath, basename, ext, first, nimages,
185  step, nzero); return false; break;
186 
187  default:
188  usage(argv[0], optarg_, ipath, basename, ext, first, nimages, step, nzero);
189  return false; break;
190  }
191  }
192 
193  if ((c == 1) || (c == -1)) {
194  // standalone param or error
195  usage(argv[0], NULL, ipath, basename, ext, first, nimages, step, nzero);
196  std::cerr << "ERROR: " << std::endl;
197  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
198  return false;
199  }
200 
201  return true;
202 }
203 
204 
214 int main(int argc, const char ** argv)
215 {
216  try {
217  std::string env_ipath;
218  std::string opt_ipath;
219  std::string ipath;
220  std::string opt_basename = "ViSP-images/cube/image.";
221  std::string opt_ext = "pgm";
222  bool opt_display = true;
223 
224  int opt_first = 5;
225  unsigned int opt_nimages = 70;
226  int opt_step = 1;
227  unsigned int opt_nzero = 4;
228 
229  // Get the VISP_IMAGE_PATH environment variable value
230  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
231  if (ptenv != NULL)
232  env_ipath = ptenv;
233 
234  // Set the default input path
235  if (! env_ipath.empty())
236  ipath = env_ipath;
237 
238  // Read the command line options
239  if (getOptions(argc, argv, opt_ipath, opt_basename, opt_ext, opt_first,
240  opt_nimages, opt_step, opt_nzero, opt_display) == false) {
241  exit (-1);
242  }
243 
244  // Get the option values
245  if (!opt_ipath.empty())
246  ipath = opt_ipath;
247 
248  // Compare ipath and env_ipath. If they differ, we take into account
249  // the input path comming from the command line option
250  if (!opt_ipath.empty() && !env_ipath.empty()) {
251  if (ipath != env_ipath) {
252  std::cout << std::endl
253  << "WARNING: " << std::endl;
254  std::cout << " Since -i <visp image path=" << ipath << "> "
255  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
256  << " we skip the environment variable." << std::endl;
257  }
258  }
259 
260  // Test if an input path is set
261  if (opt_ipath.empty() && env_ipath.empty()){
262  usage(argv[0], NULL, ipath, opt_basename, opt_ext, opt_first,
263  opt_nimages, opt_step, opt_nzero);
264  std::cerr << std::endl
265  << "ERROR:" << std::endl;
266  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
267  << std::endl
268  << " environment variable to specify the location of the " << std::endl
269  << " image path where test images are located." << std::endl << std::endl;
270  exit(-1);
271  }
272 
273 
274  // Declare an image, this is a gray level image (unsigned char)
275  // it size is not defined yet, it will be defined when the image will
276  // read on the disk
278 
279 
280  // Declare a framegrabber able to read a sequence of successive
281  // images from the disk
282  vpDiskGrabber g;
283 
284  // Set the path to the directory containing the sequence
285  g.setDirectory(ipath.c_str());
286  // Set the image base name. The directory and the base name constitute
287  // the constant part of the full filename
288  g.setBaseName(opt_basename.c_str());
289  // Set the step between two images of the sequence
290  g.setStep(opt_step);
291  // Set the number of digits to build the image number
292  g.setNumberOfZero(opt_nzero);
293  // Set the first frame number of the sequence
294  g.setImageNumber(opt_first);
295  // Set the image extension
296  g.setExtension(opt_ext.c_str());
297 
298  // Open the framegrabber by loading the first image of the sequence
299  g.open(I) ;
300 
301  std::cout << "Image size: width : " << I.getWidth() << " height: "
302  << I.getHeight() << std::endl;
303 
304  // We open a window using either X11 or GDI.
305  // Its size is automatically defined by the image (I) size
306 #if defined(VISP_HAVE_X11)
307  vpDisplayX display(I);
308 #elif defined(VISP_HAVE_GDI)
309  vpDisplayGDI display(I);
310 #else
311  std::cout << "No image viewer is available..." << std::endl;
312 #endif
313 
314  if (opt_display) {
315  display.init(I,100,100,"Disk Framegrabber");
316 
317  // display the image
318  // The image class has a member that specify a pointer toward
319  // the display that has been initialized in the display declaration
320  // therefore is is no longuer necessary to make a reference to the
321  // display variable.
322  vpDisplay::display(I) ;
323  vpDisplay::flush(I) ;
324  }
325 
326  unsigned cpt = 1;
327  // this is the loop over the image sequence
328 
329  while(cpt ++ < opt_nimages)
330  {
331  double tms = vpTime::measureTimeMs();
332  // read the image and then increment the image counter so that the next
333  // call to acquire(I) will get the next image
334  g.acquire(I) ;
335  if (opt_display) {
336  // Display the image
337  vpDisplay::display(I) ;
338  // Flush the display
339  vpDisplay::flush(I) ;
340  }
341  // Synchronise the loop to 40 ms
342  vpTime::wait(tms, 40) ;
343  }
344  return 0;
345  }
346  catch(vpException e) {
347  std::cout << "Catch an exception: " << e << std::endl;
348  return 1;
349  }
350 }
351 
352 #else
353 int
354 main()
355 {
356  vpERROR_TRACE("You do not have X11 or GTK display functionalities...");
357 }
358 
359 #endif
360 
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
void setBaseName(const char *name)
Define the X11 console to display images.
Definition: vpDisplayX.h:152
error that can be emited by ViSP classes.
Definition: vpException.h:76
void setDirectory(const char *dir)
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:1994
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:206
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)