Visual Servoing Platform  version 3.1.0
vpDiskGrabber.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 http://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  * Disk framegrabber.
33  *
34  * Authors:
35  * Eric Marchand
36  *
37  *****************************************************************************/
38 
39 #include <visp3/io/vpDiskGrabber.h>
40 
45  : m_image_number(0), m_image_number_next(0), m_image_step(1), m_number_of_zero(0), m_directory("/tmp"),
46  m_base_name("I"), m_extension("pgm"), m_use_generic_name(false), m_generic_name("empty")
47 {
48  init = false;
49 }
50 
54 vpDiskGrabber::vpDiskGrabber(const std::string &generic_name)
55  : m_image_number(0), m_image_number_next(0), m_image_step(1), m_number_of_zero(0), m_directory("/tmp"),
56  m_base_name("I"), m_extension("pgm"), m_use_generic_name(true), m_generic_name(generic_name)
57 {
58  init = false;
59 }
60 
72 vpDiskGrabber::vpDiskGrabber(const std::string &dir, const std::string &basename, long number, int step,
73  unsigned int noz, const std::string &ext)
74  : m_image_number(number), m_image_number_next(number), m_image_step(step), m_number_of_zero(noz), m_directory(dir),
75  m_base_name(basename), m_extension(ext), m_use_generic_name(false), m_generic_name("empty")
76 {
77  init = false;
78 }
79 
85 {
86  long first_number = getImageNumber();
87 
88  acquire(I);
89 
90  setImageNumber(first_number);
91 
92  width = I.getWidth();
93  height = I.getHeight();
94 
95  init = true;
96 }
97 
103 {
104  // First we save the image number, so that it can be reaffected after the
105  // acquisition. That means that the first image is readed twice
106  long first_number = getImageNumber();
107 
108  acquire(I);
109 
110  setImageNumber(first_number);
111 
112  width = I.getWidth();
113  height = I.getHeight();
114 
115  init = true;
116 }
117 
123 {
124  // First we save the image number, so that it can be reaffected after the
125  // acquisition. That means that the first image is readed twice
126  long first_number = getImageNumber();
127 
128  acquire(I);
129 
130  setImageNumber(first_number);
131 
132  width = I.getWidth();
133  height = I.getHeight();
134 
135  init = true;
136 }
137 
145 {
146  m_image_number = m_image_number_next;
147  std::stringstream ss;
148 
149  if (m_use_generic_name) {
150  char filename[FILENAME_MAX];
151  sprintf(filename, m_generic_name.c_str(), m_image_number);
152  ss << filename;
153  } else {
154  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
155  << m_extension;
156  }
157 
158  m_image_number_next += m_image_step;
159  vpImageIo::read(I, ss.str());
160 
161  width = I.getWidth();
162  height = I.getHeight();
163 }
164 
172 {
173  m_image_number = m_image_number_next;
174  std::stringstream ss;
175 
176  if (m_use_generic_name) {
177  char filename[FILENAME_MAX];
178  sprintf(filename, m_generic_name.c_str(), m_image_number);
179  ss << filename;
180  } else {
181  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
182  << m_extension;
183  }
184 
185  m_image_number_next += m_image_step;
186 
187  vpImageIo::read(I, ss.str());
188 
189  width = I.getWidth();
190  height = I.getHeight();
191 }
192 
200 {
201  m_image_number = m_image_number_next;
202  std::stringstream ss;
203  if (m_use_generic_name) {
204  char filename[FILENAME_MAX];
205  sprintf(filename, m_generic_name.c_str(), m_image_number);
206  ss << filename;
207  } else {
208  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
209  << m_extension;
210  }
211 
212  m_image_number_next += m_image_step;
213 
214  vpImageIo::readPFM(I, ss.str());
215 
216  width = I.getWidth();
217  height = I.getHeight();
218 }
219 
228 {
229  m_image_number = m_image_number_next;
230  std::stringstream ss;
231  if (m_use_generic_name) {
232  char filename[FILENAME_MAX];
233  sprintf(filename, m_generic_name.c_str(), m_image_number);
234  ss << filename;
235  } else {
236  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << img_number << "."
237  << m_extension;
238  }
239 
240  m_image_number_next += m_image_step;
241 
242  vpImageIo::read(I, ss.str());
243 
244  width = I.getWidth();
245  height = I.getHeight();
246 }
247 
255 void vpDiskGrabber::acquire(vpImage<vpRGBa> &I, long img_number)
256 {
257  m_image_number = m_image_number_next;
258  std::stringstream ss;
259  if (m_use_generic_name) {
260  char filename[FILENAME_MAX];
261  sprintf(filename, m_generic_name.c_str(), m_image_number);
262  ss << filename;
263  } else {
264  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << img_number << "."
265  << m_extension;
266  }
267 
268  m_image_number_next += m_image_step;
269 
270  vpImageIo::read(I, ss.str());
271 
272  width = I.getWidth();
273  height = I.getHeight();
274 }
275 
283 void vpDiskGrabber::acquire(vpImage<float> &I, long img_number)
284 {
285  m_image_number = m_image_number_next;
286  std::stringstream ss;
287  if (m_use_generic_name) {
288  char filename[FILENAME_MAX];
289  sprintf(filename, m_generic_name.c_str(), m_image_number);
290  ss << filename;
291  } else {
292  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << img_number << "."
293  << m_extension;
294  }
295 
296  m_image_number_next += m_image_step;
297 
298  vpImageIo::readPFM(I, ss.str());
299 
300  width = I.getWidth();
301  height = I.getHeight();
302 }
303 
310 {
311  // Nothing do do here...
312 }
313 
320 
324 void vpDiskGrabber::setDirectory(const std::string &dir) { m_directory = dir; }
325 
329 void vpDiskGrabber::setBaseName(const std::string &name) { m_base_name = name; }
330 
334 void vpDiskGrabber::setExtension(const std::string &ext) { m_extension = ext; }
335 
340 {
341  m_image_number = number;
342  m_image_number_next = number;
343 }
344 
348 void vpDiskGrabber::setStep(long step) { m_image_step = step; }
352 void vpDiskGrabber::setNumberOfZero(unsigned int noz) { m_number_of_zero = noz; }
353 
354 void vpDiskGrabber::setGenericName(const std::string &generic_name)
355 {
356  m_generic_name = generic_name;
357  m_use_generic_name = true;
358 }
void setGenericName(const std::string &genericName)
void setStep(long step)
void setDirectory(const std::string &dir)
void setBaseName(const std::string &name)
virtual ~vpDiskGrabber()
void setExtension(const std::string &ext)
void setNumberOfZero(unsigned int noz)
unsigned int height
Number of rows in the image.
void setImageNumber(long number)
void open(vpImage< unsigned char > &I)
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:665
bool init
Set to true if the frame grabber has been initialized.
long getImageNumber()
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
void acquire(vpImage< unsigned char > &I)
unsigned int getWidth() const
Definition: vpImage.h:229
unsigned int width
Number of columns in the image.