Debian Codegraph @

cvd/diskbuffer2.h

raw symbols SCIP
1 #ifndef CVD_DISKBUFFER2_H
2 #define CVD_DISKBUFFER2_H
3
4 #include <errno.h>
5 #include <fstream>
6 #include <string>
7 #include <utility>
8 #include <vector>
9
10 #include <cvd/diskbuffer2_frame.h>
11 #include <cvd/image_io.h>
12 #include <cvd/localvideobuffer.h>
13 #include <cvd/videobufferflags.h>
14
15 namespace CVD
16 {
17 //
18 // GLOBLIST
19 /// Make a list of strings from a UNIX-style pattern
20 /// pathname expansion. Tilde expansion is done, and * ? [] and {} can all be
21 /// used as normal. The filenames are returned in alphabetical (and numerical) order.
22 /// @param gl The pattern from which to generate the strings
23 /// @relatesalso DiskBuffer2
24 std::vector<std::string> globlist(const std::string& gl);
25
26 namespace Exceptions
27 {
28 /// %Exceptions specific to DiskBuffer2
29 /// @ingroup gException
30 namespace DiskBuffer2
31 {
32 /// Base class for all DiskBuffer2 exceptions
33 /// @ingroup gException
34 struct All : public CVD::Exceptions::VideoBuffer::All
35 {
36 using CVD::Exceptions::VideoBuffer::All::All;
37 };
38 /// An empty list of filename strings was passed to the buffer
39 /// @ingroup gException
40 struct NoFiles : public All
41 {
42 NoFiles();
43 };
44 /// An error occurred trying to open a file
45 /// @ingroup gException
46 struct BadFile : public All
47 {
48 BadFile(const std::string&, int); ///< Construct from filename and error number
49 };
50 /// An error occurred trying to read a file as an image
51 /// @ingroup gException
52 struct BadImage : public All
53 {
54 BadImage(const std::string& file, const std::string& error); ///< Construct from filename and error string
55 };
56 /// The file loaded was a different size from the first frame
57 /// @ingroup gException
58 struct BadImageSize : public All
59 {
60 BadImageSize(const std::string& file); ///< Construct from filename string
61 };
62 /// get_frame() was called when at the end of the buffer
63 /// @ingroup gException
64 struct EndOfBuffer : public All
65 {
66 EndOfBuffer();
67 };
68 /// seek_to() was called for an invalid timestamp
69 /// @ingroup gException
70 struct BadSeek : public All
71 {
72 BadSeek(double t); ///< Construct from invalid timestamp
73 };
74
75 }
76 }
77
78 /// Play a series of image files as a video stream.
79 /// Provides frames of type CVD::DiskBuffer2Frame and throws exceptions of type
80 /// CVD::Exceptions::DiskBuffer2
81 /// @param T The pixel type of the frames to provide (usually <code>CVD::Rgb<CVD::byte></code>
82 /// or <code>CVD::byte</code>. If the image files are of a different type, they will be automatically
83 /// converted (see @link gImageIO Image loading and saving, and format conversion@endlink).
84 /// @ingroup gVideoBuffer
85 template <typename T>
86 class DiskBuffer2 : public CVD::LocalVideoBuffer<T>
87 {
88 public:
89 /// Construct a DiskBuffer2 from a vector of filenames.
90 /// Typically the globlist() helper function is used to provide the filenames
91 /// e.g. <code>DiskBuffer2 buffer(globlist("~/Images/lab*.jpg"), 25);</code>
92 /// @param names The filenames to use (played in the order that they are in the vector)
93 /// @param fps The frames per second to report for this VideoBuffer
94 /// @param eob What should the buffer do when it reaches the end of the list of files?
95 DiskBuffer2(const std::vector<std::string>& names, double fps, VideoBufferFlags::OnEndOfBuffer eob = VideoBufferFlags::RepeatLastFrame);
96
97 virtual ImageRef size() { return my_size; }
98
99 /// Is there another frame waiting in the buffer? By default, this always
100 /// returns true, but if the VideoBufferFlags::OnEndOfBuffer setting is VideoBufferFlags::UnsetPending, this will return
101 /// false after the last frame has been returned by get_frame()
102 virtual bool frame_pending() { return frame_ready; }
103
104 virtual DiskBuffer2Frame<T>* get_frame();
105 virtual void put_frame(VideoFrame<T>* f);
106 virtual void seek_to(double t);
107
108 /// What should the buffer do when it reaches the end of the list of files?
109 /// @param eob The desired behaviour
110 virtual void on_end_of_buffer(VideoBufferFlags::OnEndOfBuffer eob)
111 {
112 end_of_buffer_behaviour = eob;
113 }
114
115 virtual double frame_rate()
116 {
117 return frames_per_sec;
118 }
119
120 protected:
121 ImageRef my_size;
122 int next_frame;
123 double start_time;
124 double time_per_frame, frames_per_sec;
125 bool frame_ready;
126 std::vector<std::string> file_names;
127 VideoBufferFlags::OnEndOfBuffer end_of_buffer_behaviour;
128 };
129
130 //
131 // CONSTRUCTOR
132 //
133 template <typename T>
134 inline DiskBuffer2<T>::DiskBuffer2(const std::vector<std::string>& names, double fps, VideoBufferFlags::OnEndOfBuffer eob)
135 : LocalVideoBuffer<T>(VideoBufferType::NotLive)
136 , end_of_buffer_behaviour(eob)
137 {
138 frames_per_sec = fps;
139
140 start_time = 0;
141 next_frame = 0;
142 time_per_frame = 1 / fps;
143
144 file_names = names;
145
146 if(file_names.size() == 0)
147 throw Exceptions::DiskBuffer2::NoFiles();
148
149 Image<T> foo;
150 std::ifstream im;
151 im.open(names[0].c_str(), std::ios::in | std::ios::binary);
152
153 if(!im.good())
154 throw Exceptions::DiskBuffer2::BadFile(names[0], errno);
155
156 try
157 {
158 img_load(foo, im);
159 }
160 catch(Exceptions::Image_IO::All err)
161 {
162 throw Exceptions::DiskBuffer2::BadImage(names[0], err.what());
163 }
164
165 my_size = foo.size();
166 frame_ready = true;
167 }
168
169 //
170 // GET FRAME
171 //
172 template <typename T>
173 inline DiskBuffer2Frame<T>* DiskBuffer2<T>::get_frame()
174 {
175 if(next_frame < 0)
176 next_frame = 0;
177
178 if(!frame_pending())
179 throw Exceptions::DiskBuffer2::EndOfBuffer();
180
181 Image<T> foo(my_size);
182
183 std::ifstream im_file(file_names[next_frame].c_str(), std::ios::in | std::ios::binary);
184
185 if(!im_file.good())
186 throw Exceptions::DiskBuffer2::BadFile(file_names[next_frame], errno);
187
188 try
189 {
190 img_load(foo, im_file);
191 }
192 catch(CVD::Exceptions::Image_IO::All err)
193 {
194 throw Exceptions::DiskBuffer2::BadImage(file_names[next_frame], err.what());
195 }
196
197 DiskBuffer2Frame<T>* vf = new DiskBuffer2Frame<T>(next_frame * time_per_frame + start_time, std::move(foo), file_names[next_frame]);
198
199 next_frame++;
200
201 if(next_frame > (int)file_names.size() - 1)
202 {
203 switch(end_of_buffer_behaviour)
204 {
205 case VideoBufferFlags::RepeatLastFrame:
206 next_frame = file_names.size() - 1;
207 break;
208
209 case VideoBufferFlags::UnsetPending:
210 frame_ready = false;
211 break;
212
213 case VideoBufferFlags::Loop:
214 next_frame = 0;
215 break;
216 }
217 }
218
219 return vf;
220 }
221
222 //
223 // PUT FRAME
224 //
225 template <typename T>
226 inline void DiskBuffer2<T>::put_frame(VideoFrame<T>* f)
227 {
228 //Check that the type is correct...
229 DiskBuffer2Frame<T>* db2f = dynamic_cast<DiskBuffer2Frame<T>*>(f);
230
231 if(db2f == NULL)
232 throw CVD::Exceptions::VideoBuffer::BadPutFrame();
233 else
234 delete db2f;
235 }
236
237 //
238 // SEEK TO
239 //
240 template <typename T>
241 inline void DiskBuffer2<T>::seek_to(double t)
242 {
243 // t is in ms, but work in seconds
244 // round the answer to the nearest whole frame
245 int frameno = static_cast<int>((t - start_time) / time_per_frame + 0.5);
246 if(frameno < 0 || static_cast<unsigned int>(frameno) > (file_names.size() - 1))
247 throw Exceptions::DiskBuffer2::BadSeek(t);
248 next_frame = frameno;
249 frame_ready = true;
250 }
251 }
252
253 #endif