VideoReader.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: VideoReader.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "VideoReader.h"
49 #include "Image/ByteImage.h"
50 #include "Helpers/helpers.h"
51 #include <ctype.h>
52 
53 
54 // ****************************************************************************
55 // Defines
56 // ****************************************************************************
57 
58 #define MAX_HEADER_LENGTH 8192
59 
60 
61 
62 // ****************************************************************************
63 // Constructor / Destructor
64 // ****************************************************************************
65 
67 {
68  m_file = 0;
69  m_nBytesToRead = 0;
70  m_nImageWidth = -1;
71  m_nImageHeight = -1;
72  m_pTempBuffer = new unsigned char[1];
73  m_pImage = new CByteImage();
74 
75  header = new unsigned char[MAX_HEADER_LENGTH];
76 }
77 
79 {
80  delete [] header;
81  delete m_pImage;
82  Close();
83 }
84 
85 
86 // ****************************************************************************
87 // Methods
88 // ****************************************************************************
89 
90 bool CVideoReader::OpenUncompressedAVI(const char *pFileName)
91 {
92  // just to make sure
93  Close();
94 
95  m_file = fopen(pFileName, "rb");
96  if (!m_file)
97  return false;
98 
99  // parse header
101  {
102  Close();
103  return false;
104  }
105 
106  // TODO (easy)
108 
109  // create new image
110  delete m_pImage;
111  delete [] m_pTempBuffer;
112  m_pImage = new CByteImage(m_nImageWidth, m_nImageHeight, CByteImage::eRGB24);
113  m_pTempBuffer = new unsigned char[m_nBytesToRead];
114 
115  return true;
116 }
117 
119 {
120  if (!m_file)
121  return 0;
122 
123  if (fread(m_pTempBuffer, m_nBytesToRead, 1, m_file) != 1)
124  return 0;
125 
126  const int width_bytes = 3 * m_nImageWidth;
127 
128  unsigned char *pHelper1 = m_pImage->pixels;
129  unsigned char *pHelper2 = m_pTempBuffer + m_nBytesToRead - width_bytes;
130 
131  // convert from BGR to RGB, and from bottom-left to top-left
132  for (int i = 0; i < m_nImageHeight; i++)
133  {
134  for (int j = 0; j < width_bytes; j += 3)
135  {
136  pHelper1[j] = pHelper2[j + 2];
137  pHelper1[j + 1] = pHelper2[j + 1];
138  pHelper1[j + 2] = pHelper2[j];
139  }
140 
141  pHelper1 += width_bytes;
142  pHelper2 -= width_bytes;
143  }
144 
145  fseek(m_file, 8, SEEK_CUR);
146 
147  return m_pImage;
148 }
149 
151 {
152  if (m_file)
153  {
154  fclose(m_file);
155  m_file = 0;
156  }
157 
158  m_nImageWidth = -1;
159  m_nImageHeight = -1;
160 }
161 
162 
163 static inline unsigned int ConvertFourByteCode(const char *pCode)
164 {
165  return *((unsigned int *) pCode);
166 }
167 
169 {
170  if (fread(header, 8192, 1, m_file) != 1)
171  return false;
172 
173  unsigned int length, *p = (unsigned int *) header;
174 
175 #ifdef IVT_BIG_ENDIAN
176  if (*p++ != ConvertFourByteCode("RIFF"))
177  return false;
178 
179  // skip length information
180  p++;
181 
182  if (*p++ != ConvertFourByteCode("AVI "))
183  return false;
184 
185  if (*p++ != ConvertFourByteCode("LIST"))
186  return false;
187 
188  // skip length information
189  p++;
190 
191  if (*p++ != ConvertFourByteCode("hdrl"))
192  return false;
193 
194  if (*p++ != ConvertFourByteCode("avih"))
195  return false;
196 
199 
200  length = invert_byte_order_int(*p++);
201  if (length > 256)
202  return false;
203 
204  // skip avi header
205  p = (unsigned int *) (((unsigned char *) p) + length);
206 
207  if (*p++ != ConvertFourByteCode("LIST"))
208  return false;
209 
210  // skip length information
211  p++;
212 
213  if (*p++ != ConvertFourByteCode("strl"))
214  return false;
215 
216  if (*p++ != ConvertFourByteCode("strh"))
217  return false;
218 
219  length = invert_byte_order_int(*p++);
220  if (length > 256)
221  return false;
222 
223  // skip stream header
224  p = (unsigned int *) (((unsigned char *) p) + length);
225 
226  if (*p++ != ConvertFourByteCode("strf"))
227  return false;
228 
229  length = invert_byte_order_int(*p++);
230 
231  // skip stream format
232  p = (unsigned int *) (((unsigned char *) p) + length);
233  if (length > 4096)
234  return false;
235 
236  // now re-read the file
237  unsigned int offset = (unsigned int) ((unsigned char *) p - header);
238  fseek(m_file, offset, SEEK_SET);
239 
240  if (*p++ == ConvertFourByteCode("JUNK"))
241  {
242  // skip it
243  length = invert_byte_order_int(*p);
244  fseek(m_file, length + 8, SEEK_CUR);
245  }
246 
247  if (fread(header, 20, 1, m_file) != 1)
248  return false;
249 
250  p = (unsigned int *) header;
251 
252  if (*p++ != ConvertFourByteCode("LIST"))
253  return false;
254 
255  // skip length information
256  p++;
257 
258  if (*p++ != ConvertFourByteCode("movi"))
259  return false;
260 
261  if (*p++ != ConvertFourByteCode("00db"))
262  return false;
263 
265  return false;
266 #else
267  if (*p++ != ConvertFourByteCode("RIFF"))
268  return false;
269 
270  // skip length information
271  p++;
272 
273  if (*p++ != ConvertFourByteCode("AVI "))
274  return false;
275 
276  if (*p++ != ConvertFourByteCode("LIST"))
277  return false;
278 
279  // skip length information
280  p++;
281 
282  if (*p++ != ConvertFourByteCode("hdrl"))
283  return false;
284 
285  if (*p++ != ConvertFourByteCode("avih"))
286  return false;
287 
288  m_nImageWidth = p[9];
289  m_nImageHeight = p[10];
290 
291  length = *p++;
292  if (length > 256)
293  return false;
294 
295  // skip avi header
296  p = (unsigned int *) (((unsigned char *) p) + length);
297 
298  if (*p++ != ConvertFourByteCode("LIST"))
299  return false;
300 
301  // skip length information
302  p++;
303 
304  if (*p++ != ConvertFourByteCode("strl"))
305  return false;
306 
307  if (*p++ != ConvertFourByteCode("strh"))
308  return false;
309 
310  length = *p++;
311  if (length > 256)
312  return false;
313 
314  // skip stream header
315  p = (unsigned int *) (((unsigned char *) p) + length);
316 
317  if (*p++ != ConvertFourByteCode("strf"))
318  return false;
319 
320  length = *p++;
321 
322  // skip stream format
323  p = (unsigned int *) (((unsigned char *) p) + length);
324  if (length > 4096)
325  return false;
326 
327  // now re-read the file
328  unsigned int offset = (unsigned int) ((unsigned char *) p - header);
329  fseek(m_file, offset, SEEK_SET);
330 
331  if (*p++ == ConvertFourByteCode("JUNK"))
332  {
333  // skip it
334  length = *p;
335  fseek(m_file, length + 8, SEEK_CUR);
336  }
337 
338  if (fread(header, 20, 1, m_file) != 1)
339  return false;
340 
341  p = (unsigned int *) header;
342 
343  if (*p++ != ConvertFourByteCode("LIST"))
344  return false;
345 
346  // skip length information
347  p++;
348 
349  if (*p++ != ConvertFourByteCode("movi"))
350  return false;
351 
352  if (*p++ != ConvertFourByteCode("00db"))
353  return false;
354 
355  if (*p != (unsigned int) (m_nImageWidth * m_nImageHeight * 3))
356  return false;
357 #endif
358 
359  return true;
360 }
#define MAX_HEADER_LENGTH
Definition: VideoReader.cpp:58
FILE * m_file
Definition: VideoReader.h:85
unsigned char * m_pTempBuffer
Definition: VideoReader.h:87
int m_nBytesToRead
Definition: VideoReader.h:89
bool OpenUncompressedAVI(const char *pFileName)
Definition: VideoReader.cpp:90
static unsigned int ConvertFourByteCode(const char *pCode)
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
bool ParseUncompressedAVIHeader()
GLintptr offset
Definition: glext.h:3389
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
unsigned int invert_byte_order_int(unsigned int x)
Definition: helpers.cpp:89
int m_nImageWidth
Definition: VideoReader.h:90
unsigned char * header
Definition: VideoReader.h:93
CByteImage * m_pImage
Definition: VideoReader.h:86
CByteImage * ReadNextFrame()
GLuint GLsizei GLsizei * length
Definition: glext.h:3509
int m_nImageHeight
Definition: VideoReader.h:91
GLfloat GLfloat p
Definition: glext.h:5178


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28