Visual Servoing Platform  version 3.6.1 under development (2024-06-15)
vpDiskGrabber.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Disk framegrabber.
32  */
33 
34 #include <visp3/io/vpDiskGrabber.h>
35 
37 
39  : m_image_number(0), m_image_number_next(0), m_image_step(1), m_number_of_zero(0), m_directory("/tmp"),
40  m_base_name("I"), m_extension("pgm"), m_use_generic_name(false), m_generic_name("empty")
41 {
42  init = false;
43 }
44 
45 
46 vpDiskGrabber::vpDiskGrabber(const std::string &generic_name)
47  : m_image_number(0), m_image_number_next(0), m_image_step(1), m_number_of_zero(0), m_directory("/tmp"),
48  m_base_name("I"), m_extension("pgm"), m_use_generic_name(true), m_generic_name(generic_name)
49 {
50  init = false;
51 }
52 
53 vpDiskGrabber::vpDiskGrabber(const std::string &dir, const std::string &basename, long number, int step,
54  unsigned int noz, const std::string &ext)
55  : m_image_number(number), m_image_number_next(number), m_image_step(step), m_number_of_zero(noz), m_directory(dir),
56  m_base_name(basename), m_extension(ext), m_use_generic_name(false), m_generic_name("empty"), m_image_name()
57 {
58  init = false;
59 }
60 
62 {
63  long first_number = getImageNumber();
64 
65  acquire(I);
66 
67  setImageNumber(first_number);
68 
69  width = I.getWidth();
70  height = I.getHeight();
71 
72  init = true;
73 }
74 
76 {
77  // First we save the image number, so that it can be reaffected after the
78  // acquisition. That means that the first image is readed twice
79  long first_number = getImageNumber();
80 
81  acquire(I);
82 
83  setImageNumber(first_number);
84 
85  width = I.getWidth();
86  height = I.getHeight();
87 
88  init = true;
89 }
90 
92 {
93  // First we save the image number, so that it can be reaffected after the
94  // acquisition. That means that the first image is readed twice
95  long first_number = getImageNumber();
96 
97  acquire(I);
98 
99  setImageNumber(first_number);
100 
101  width = I.getWidth();
102  height = I.getHeight();
103 
104  init = true;
105 }
106 
108 {
109  m_image_number = m_image_number_next;
110  std::stringstream ss;
111 
112  if (m_use_generic_name) {
113  char filename[FILENAME_MAX];
114  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
115  ss << filename;
116  }
117  else {
118  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
119  << m_extension;
120  }
121 
122  m_image_number_next += m_image_step;
123  m_image_name = ss.str();
124  vpImageIo::read(I, m_image_name);
125 
126  width = I.getWidth();
127  height = I.getHeight();
128 }
129 
131 {
132  m_image_number = m_image_number_next;
133  std::stringstream ss;
134 
135  if (m_use_generic_name) {
136  char filename[FILENAME_MAX];
137  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
138  ss << filename;
139  }
140  else {
141  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
142  << m_extension;
143  }
144 
145  m_image_number_next += m_image_step;
146  m_image_name = ss.str();
147  vpImageIo::read(I, m_image_name);
148 
149  width = I.getWidth();
150  height = I.getHeight();
151 }
152 
154 {
155  m_image_number = m_image_number_next;
156  std::stringstream ss;
157  if (m_use_generic_name) {
158  char filename[FILENAME_MAX];
159  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
160  ss << filename;
161  }
162  else {
163  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
164  << m_extension;
165  }
166 
167  m_image_number_next += m_image_step;
168 
169  vpImageIo::readPFM(I, ss.str());
170 
171  width = I.getWidth();
172  height = I.getHeight();
173 }
174 
176 {
177  m_image_number = image_number;
178  std::stringstream ss;
179  if (m_use_generic_name) {
180  char filename[FILENAME_MAX];
181  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
182  ss << filename;
183  }
184  else {
185  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
186  << m_extension;
187  }
188 
189  m_image_number_next = m_image_number + m_image_step;
190 
191  vpImageIo::read(I, ss.str());
192 
193  width = I.getWidth();
194  height = I.getHeight();
195 }
196 
197 void vpDiskGrabber::acquire(vpImage<vpRGBa> &I, long image_number)
198 {
199  m_image_number = image_number;
200  std::stringstream ss;
201  if (m_use_generic_name) {
202  char filename[FILENAME_MAX];
203  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
204  ss << filename;
205  }
206  else {
207  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << m_image_number << "."
208  << m_extension;
209  }
210 
211  m_image_number_next = m_image_number + m_image_step;
212 
213  vpImageIo::read(I, ss.str());
214 
215  width = I.getWidth();
216  height = I.getHeight();
217 }
218 
219 void vpDiskGrabber::acquire(vpImage<float> &I, long image_number)
220 {
221  m_image_number = m_image_number_next;
222  std::stringstream ss;
223  if (m_use_generic_name) {
224  char filename[FILENAME_MAX];
225  snprintf(filename, FILENAME_MAX, m_generic_name.c_str(), m_image_number);
226  ss << filename;
227  }
228  else {
229  ss << m_directory << "/" << m_base_name << std::setfill('0') << std::setw(m_number_of_zero) << image_number << "."
230  << m_extension;
231  }
232 
233  m_image_number_next += m_image_step;
234 
235  vpImageIo::readPFM(I, ss.str());
236 
237  width = I.getWidth();
238  height = I.getHeight();
239 }
240 
242 {
243  m_image_number = number;
244  m_image_number_next = number;
245 }
246 
247 void vpDiskGrabber::setGenericName(const std::string &generic_name)
248 {
249  m_generic_name = generic_name;
250  m_use_generic_name = true;
251 }
252 
void open(vpImage< unsigned char > &I)
void setGenericName(const std::string &genericName)
void setImageNumber(long number)
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:1344
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
unsigned int getWidth() const
Definition: vpImage.h:246
unsigned int getHeight() const
Definition: vpImage.h:185