ViSP  2.8.0
grabDirectShow.cpp
1 /****************************************************************************
2  *
3  * $Id: grabDirectShow.cpp 4323 2013-07-18 09:24:01Z 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  * Acquire images using DirectShow (under Windows only) and display it
36  * using GTK or GDI.
37  *
38  * Authors:
39  * Bruno Renier
40  * Fabien Spindler
41  *
42  *****************************************************************************/
43 
44 #include <visp/vpConfig.h>
45 #include <visp/vpDebug.h>
46 
54 #if defined (VISP_HAVE_DIRECTSHOW)
55 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
56 
57 
58 #include <visp/vpDirectShowGrabber.h>
59 #include <visp/vpImage.h>
60 #include <visp/vpImageIo.h>
61 #include <visp/vpDisplayGTK.h>
62 #include <visp/vpDisplayGDI.h>
63 #include <visp/vpParseArgv.h>
64 #include <visp/vpTime.h>
65 
66 // List of allowed command line options
67 #define GETOPTARGS "dhn:o:"
68 
79 void usage(const char *name, const char *badparam, unsigned &nframes, std::string &opath)
80 {
81  fprintf(stdout, "\n\
82 Acquire images using DirectShow (under Windows only) and display\n\
83 it using GTK or the windows GDI if GTK is not available.\n\
84 \n\
85 SYNOPSIS\n\
86  %s [-d] [-n] [-o] [-h] \n", name);
87 
88  fprintf(stdout, "\n\
89 OPTIONS: Default\n\
90  -d \n\
91  Turn off the display.\n\
92 \n\
93  -n [%%u] %u\n\
94  Number of frames to acquire. \n\
95 \n\
96  -o [%%s] \n\
97  Filename for image saving. \n\
98  Example: -o %s\n\
99  The %%d is for the image numbering.\n\
100 \n\
101  -h \n\
102  Print the help.\n\
103 \n", nframes, opath.c_str());
104  if (badparam) {
105  fprintf(stderr, "ERROR: \n" );
106  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
107  }
108 }
125 bool getOptions(int argc, const char **argv, bool &display,
126  unsigned &nframes, bool &save, std::string &opath)
127 {
128  const char *optarg;
129  int c;
130  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
131 
132  switch (c) {
133  case 'd': display = false; break;
134  case 'n':
135  nframes = atoi(optarg); break;
136  case 'o':
137  save = true;
138  opath = optarg; break;
139  case 'h': usage(argv[0], NULL, nframes, opath); return false; break;
140 
141  default:
142  usage(argv[0], optarg, nframes, opath);
143  return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL, nframes, opath);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 
167 int
168 main(int argc, const char ** argv)
169 {
170  bool opt_display = true;
171  unsigned nframes = 50;
172  bool save = false;
173 
174  // Declare an image. It size is not defined yet. It will be defined when the
175  // image will acquired the first time.
176 #ifdef GRAB_COLOR
177  vpImage<vpRGBa> I; // This is a color image (in RGBa format)
178 #else
179  vpImage<unsigned char> I; // This is a B&W image
180 #endif
181 
182  // Set default output image name for saving
183 #ifdef GRAB_COLOR
184  // Color images will be saved in PGM P6 format
185  std::string opath = "C:/temp/I%04d.ppm";
186 #else
187  // B&W images will be saved in PGM P5 format
188  std::string opath = "C:/temp/I%04d.pgm";
189 #endif
190 
191  // Read the command line options
192  if (getOptions(argc, argv, opt_display, nframes, save, opath) == false) {
193  exit (-1);
194  }
195  vpDirectShowGrabber* grabber = NULL;
196  try {
197  // Create the grabber
198  grabber = new vpDirectShowGrabber();
199 
200  //test if a camera is connected
201  if(grabber->getDeviceNumber() == 0)
202  {
203  vpCTRACE << "there is no camera detected on your computer." << std::endl ;
204  grabber->close();
205  exit(0);
206  }
207  // Initialize the grabber
208  grabber->open(I);
209 
210  // Acquire an image
211  grabber->acquire(I);
212  }
213  catch(...)
214  {
215  if (grabber !=NULL) delete grabber;
216  vpCTRACE << "Cannot acquire an image... " << std::endl ;
217  exit(-1);
218  }
219 
220  std::cout << "Image size: width : " << I.getWidth() << " height: "
221  << I.getHeight() << std::endl;
222 
223  // Creates a display
224 #if defined VISP_HAVE_GTK
225  vpDisplayGTK display;
226 #elif defined VISP_HAVE_GDI
227  vpDisplayGDI display;
228 #endif
229 
230  if (opt_display) {
231  display.init(I,100,100,"DirectShow Framegrabber");
232  }
233 
234  try {
235  double tbegin=0, tend=0, tloop=0, ttotal=0;
236 
237  ttotal = 0;
238  tbegin = vpTime::measureTimeMs();
239  // Loop for image acquisition and display
240  for (unsigned i = 0; i < nframes; i++) {
241  //Acquires an RGBa image
242  grabber->acquire(I);
243 
244  if (opt_display) {
245  //Displays the grabbed rgba image
247  vpDisplay::flush(I);
248  }
249 
250  if (save) {
251  char buf[FILENAME_MAX];
252  sprintf(buf, opath.c_str(), i);
253  std::string filename(buf);
254  std::cout << "Write: " << filename << std::endl;
255  vpImageIo::write(I, filename);
256  }
257  tend = vpTime::measureTimeMs();
258  tloop = tend - tbegin;
259  tbegin = tend;
260  std::cout << "loop time: " << tloop << " ms" << std::endl;
261  ttotal += tloop;
262  }
263  std::cout << "Mean loop time: " << ttotal / nframes << " ms" << std::endl;
264  std::cout << "Mean frequency: " << 1000./(ttotal / nframes) << " fps" << std::endl;
265 
266  // Release the framegrabber
267  if (grabber !=NULL) delete grabber;
268 
269  }
270  catch(...)
271  {
272  vpCERROR << "Failure: exit" << std::endl;
273  if (grabber !=NULL) delete grabber;
274  return(-1);
275  }
276 }
277 #else // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
278 
279 int
280 main()
281 {
282  vpTRACE("GDI or GTK is not available...") ;
283 }
284 #endif // (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))
285 #else // defined (VISP_HAVE_DIRECTSHOW)
286 int
287 main()
288 {
289  vpTRACE("DirectShow is not available...") ;
290 }
291 #endif // defined (VISP_HAVE_DIRECTSHOW)
292 
293 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:442
unsigned int getWidth() const
Definition: vpImage.h:159
#define vpTRACE
Definition: vpDebug.h:401
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:133
#define vpCERROR
Definition: vpDebug.h:354
static double measureTimeMs()
Definition: vpTime.cpp:86
class for windows direct show devices
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
#define vpCTRACE
Definition: vpDebug.h:327
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:203
void acquire(vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
unsigned int getDeviceNumber()
unsigned int getHeight() const
Definition: vpImage.h:150