Visual Servoing Platform  version 3.5.1 under development (2022-12-02)
vpDiskGrabber.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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"), m_image_name()
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  m_image_name = ss.str();
160  vpImageIo::read(I, m_image_name);
161 
162  width = I.getWidth();
163  height = I.getHeight();
164 }
165 
173 {
174  m_image_number = m_image_number_next;
175  std::stringstream ss;
176 
177  if (m_use_generic_name) {
178  char filename[FILENAME_MAX];
179  sprintf(filename, m_generic_name.c_str(), m_image_number);
180  ss << filename;
181  } else {
182  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
183  << m_extension;
184  }
185 
186  m_image_number_next += m_image_step;
187  m_image_name = ss.str();
188  vpImageIo::read(I, m_image_name);
189 
190  width = I.getWidth();
191  height = I.getHeight();
192 }
193 
201 {
202  m_image_number = m_image_number_next;
203  std::stringstream ss;
204  if (m_use_generic_name) {
205  char filename[FILENAME_MAX];
206  sprintf(filename, m_generic_name.c_str(), m_image_number);
207  ss << filename;
208  } else {
209  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
210  << m_extension;
211  }
212 
213  m_image_number_next += m_image_step;
214 
215  vpImageIo::readPFM(I, ss.str());
216 
217  width = I.getWidth();
218  height = I.getHeight();
219 }
220 
229 {
230  m_image_number = img_number;
231  std::stringstream ss;
232  if (m_use_generic_name) {
233  char filename[FILENAME_MAX];
234  sprintf(filename, m_generic_name.c_str(), m_image_number);
235  ss << filename;
236  } else {
237  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
238  << m_extension;
239  }
240 
241  m_image_number_next = m_image_number + m_image_step;
242 
243  vpImageIo::read(I, ss.str());
244 
245  width = I.getWidth();
246  height = I.getHeight();
247 }
248 
256 void vpDiskGrabber::acquire(vpImage<vpRGBa> &I, long img_number)
257 {
258  m_image_number = img_number;
259  std::stringstream ss;
260  if (m_use_generic_name) {
261  char filename[FILENAME_MAX];
262  sprintf(filename, m_generic_name.c_str(), m_image_number);
263  ss << filename;
264  } else {
265  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
266  << m_extension;
267  }
268 
269  m_image_number_next = m_image_number + m_image_step;
270 
271  vpImageIo::read(I, ss.str());
272 
273  width = I.getWidth();
274  height = I.getHeight();
275 }
276 
284 void vpDiskGrabber::acquire(vpImage<float> &I, long img_number)
285 {
286  m_image_number = m_image_number_next;
287  std::stringstream ss;
288  if (m_use_generic_name) {
289  char filename[FILENAME_MAX];
290  sprintf(filename, m_generic_name.c_str(), m_image_number);
291  ss << filename;
292  } else {
293  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << img_number << "."
294  << m_extension;
295  }
296 
297  m_image_number_next += m_image_step;
298 
299  vpImageIo::readPFM(I, ss.str());
300 
301  width = I.getWidth();
302  height = I.getHeight();
303 }
304 
311 {
312  // Nothing do do here...
313 }
314 
321 
325 void vpDiskGrabber::setDirectory(const std::string &dir) { m_directory = dir; }
326 
330 void vpDiskGrabber::setBaseName(const std::string &name) { m_base_name = name; }
331 
335 void vpDiskGrabber::setExtension(const std::string &ext) { m_extension = ext; }
336 
341 {
342  m_image_number = number;
343  m_image_number_next = number;
344 }
345 
349 void vpDiskGrabber::setStep(long step) { m_image_step = step; }
353 void vpDiskGrabber::setNumberOfZero(unsigned int noz) { m_number_of_zero = noz; }
354 
355 void vpDiskGrabber::setGenericName(const std::string &generic_name)
356 {
357  m_generic_name = generic_name;
358  m_use_generic_name = true;
359 }
void setStep(long step)
virtual ~vpDiskGrabber()
void setDirectory(const std::string &dir)
void setExtension(const std::string &ext)
void open(vpImage< unsigned char > &I)
void setGenericName(const std::string &genericName)
void setImageNumber(long number)
void setNumberOfZero(unsigned int noz)
void setBaseName(const std::string &name)
long getImageNumber() const
void acquire(vpImage< unsigned char > &I)
unsigned int height
Number of rows in the image.
bool init
Set to true if the frame grabber has been initialized.
unsigned int width
Number of columns in the image.
static void readPFM(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:803
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:188