VideoReader.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  VideoReader.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2004
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "VideoReader.h"
00049 #include "Image/ByteImage.h"
00050 #include "Helpers/helpers.h"
00051 #include <ctype.h>
00052 
00053 
00054 // ****************************************************************************
00055 // Defines
00056 // ****************************************************************************
00057 
00058 #define MAX_HEADER_LENGTH       8192
00059 
00060 
00061 
00062 // ****************************************************************************
00063 // Constructor / Destructor
00064 // ****************************************************************************
00065 
00066 CVideoReader::CVideoReader()
00067 {
00068         m_file = 0;
00069         m_nBytesToRead = 0;
00070         m_nImageWidth = -1;
00071         m_nImageHeight = -1;
00072         m_pTempBuffer = new unsigned char[1];
00073         m_pImage = new CByteImage();
00074         
00075         header = new unsigned char[MAX_HEADER_LENGTH];
00076 }
00077 
00078 CVideoReader::~CVideoReader()
00079 {
00080         delete [] header;
00081         delete m_pImage;
00082         Close();
00083 }
00084 
00085 
00086 // ****************************************************************************
00087 // Methods
00088 // ****************************************************************************
00089 
00090 bool CVideoReader::OpenUncompressedAVI(const char *pFileName)
00091 {
00092         // just to make sure
00093         Close();
00094 
00095         m_file = fopen(pFileName, "rb");
00096         if (!m_file)
00097                 return false;
00098 
00099         // parse header
00100         if (!ParseUncompressedAVIHeader())
00101         {
00102                 Close();
00103                 return false;
00104         }
00105         
00106         // TODO (easy)
00107         m_nBytesToRead = m_nImageWidth * m_nImageHeight * 3;
00108 
00109         // create new image
00110         delete m_pImage;
00111         delete [] m_pTempBuffer;
00112         m_pImage = new CByteImage(m_nImageWidth, m_nImageHeight, CByteImage::eRGB24);
00113         m_pTempBuffer = new unsigned char[m_nBytesToRead];
00114 
00115         return true;
00116 }
00117 
00118 CByteImage *CVideoReader::ReadNextFrame()
00119 {
00120         if (!m_file)
00121                 return 0;
00122 
00123         if (fread(m_pTempBuffer, m_nBytesToRead, 1, m_file) != 1)
00124                 return 0;
00125                 
00126         const int width_bytes = 3 * m_nImageWidth;
00127                 
00128         unsigned char *pHelper1 = m_pImage->pixels;
00129         unsigned char *pHelper2 = m_pTempBuffer + m_nBytesToRead - width_bytes;
00130                 
00131         // convert from BGR to RGB, and from bottom-left to top-left
00132         for (int i = 0; i < m_nImageHeight; i++)
00133         {
00134                 for (int j = 0; j < width_bytes; j += 3)
00135                 {
00136                         pHelper1[j] = pHelper2[j + 2];
00137                         pHelper1[j + 1] = pHelper2[j + 1];
00138                         pHelper1[j + 2] = pHelper2[j];
00139                 }
00140                 
00141                 pHelper1 += width_bytes;
00142                 pHelper2 -= width_bytes;
00143         }
00144 
00145         fseek(m_file, 8, SEEK_CUR);
00146 
00147         return m_pImage;
00148 }
00149 
00150 void CVideoReader::Close()
00151 {
00152         if (m_file)
00153         {
00154                 fclose(m_file);
00155                 m_file = 0;
00156         }
00157         
00158         m_nImageWidth = -1;
00159         m_nImageHeight = -1;
00160 }
00161 
00162 
00163 static inline unsigned int ConvertFourByteCode(const char *pCode)
00164 {
00165         return *((unsigned int *) pCode);
00166 }
00167 
00168 bool CVideoReader::ParseUncompressedAVIHeader()
00169 {
00170         if (fread(header, 8192, 1, m_file) != 1)
00171                 return false;
00172 
00173         unsigned int length, *p = (unsigned int *) header;
00174 
00175 #ifdef IVT_BIG_ENDIAN
00176         if (*p++ != ConvertFourByteCode("RIFF"))
00177                 return false;
00178 
00179         // skip length information
00180         p++;
00181 
00182         if (*p++ != ConvertFourByteCode("AVI "))
00183                 return false;
00184 
00185         if (*p++ != ConvertFourByteCode("LIST"))
00186                 return false;
00187 
00188         // skip length information
00189         p++;
00190 
00191         if (*p++ != ConvertFourByteCode("hdrl"))
00192                 return false;
00193 
00194         if (*p++ != ConvertFourByteCode("avih"))
00195                 return false;
00196                 
00197         m_nImageWidth = invert_byte_order_int(p[9]);
00198         m_nImageHeight = invert_byte_order_int(p[10]);
00199 
00200         length = invert_byte_order_int(*p++);
00201         if (length > 256)
00202                 return false;
00203 
00204         // skip avi header
00205         p = (unsigned int *) (((unsigned char *) p) + length);
00206 
00207         if (*p++ != ConvertFourByteCode("LIST"))
00208                 return false;
00209 
00210         // skip length information
00211         p++;
00212 
00213         if (*p++ != ConvertFourByteCode("strl"))
00214                 return false;
00215 
00216         if (*p++ != ConvertFourByteCode("strh"))
00217                 return false;
00218 
00219         length = invert_byte_order_int(*p++);
00220         if (length > 256)
00221                 return false;
00222 
00223         // skip stream header
00224         p = (unsigned int *) (((unsigned char *) p) + length);
00225 
00226         if (*p++ != ConvertFourByteCode("strf"))
00227                 return false;
00228 
00229         length = invert_byte_order_int(*p++);
00230 
00231         // skip stream format
00232         p = (unsigned int *) (((unsigned char *) p) + length);
00233         if (length > 4096)
00234                 return false;
00235 
00236         // now re-read the file
00237         unsigned int offset = (unsigned int) ((unsigned char *) p - header);
00238         fseek(m_file, offset, SEEK_SET);
00239 
00240         if (*p++ == ConvertFourByteCode("JUNK"))
00241         {
00242                 // skip it
00243                 length = invert_byte_order_int(*p);
00244                 fseek(m_file, length + 8, SEEK_CUR);
00245         }
00246 
00247         if (fread(header, 20, 1, m_file) != 1)
00248                 return false;
00249 
00250         p = (unsigned int *) header;
00251 
00252         if (*p++ != ConvertFourByteCode("LIST"))
00253                 return false;
00254 
00255         // skip length information
00256         p++;
00257 
00258         if (*p++ != ConvertFourByteCode("movi"))
00259                 return false;
00260 
00261         if (*p++ != ConvertFourByteCode("00db"))
00262                 return false;
00263 
00264         if (invert_byte_order_int(*p) != m_nImageWidth * m_nImageHeight * 3)
00265                 return false;
00266 #else
00267         if (*p++ != ConvertFourByteCode("RIFF"))
00268                 return false;
00269                 
00270         // skip length information
00271         p++;
00272 
00273         if (*p++ != ConvertFourByteCode("AVI "))
00274                 return false;
00275         
00276         if (*p++ != ConvertFourByteCode("LIST"))
00277                 return false;
00278         
00279         // skip length information
00280         p++;
00281 
00282         if (*p++ != ConvertFourByteCode("hdrl"))
00283                 return false;
00284         
00285         if (*p++ != ConvertFourByteCode("avih"))
00286                 return false;
00287         
00288         m_nImageWidth = p[9];
00289         m_nImageHeight = p[10];
00290         
00291         length = *p++;
00292         if (length > 256)
00293                 return false;
00294 
00295         // skip avi header
00296         p = (unsigned int *) (((unsigned char *) p) + length);
00297 
00298         if (*p++ != ConvertFourByteCode("LIST"))
00299                 return false;
00300 
00301         // skip length information
00302         p++;
00303 
00304         if (*p++ != ConvertFourByteCode("strl"))
00305                 return false;
00306 
00307         if (*p++ != ConvertFourByteCode("strh"))
00308                 return false;
00309 
00310         length = *p++;
00311         if (length > 256)
00312                 return false;
00313 
00314         // skip stream header
00315         p = (unsigned int *) (((unsigned char *) p) + length);
00316 
00317         if (*p++ != ConvertFourByteCode("strf"))
00318                 return false;
00319         
00320         length = *p++;
00321 
00322         // skip stream format
00323         p = (unsigned int *) (((unsigned char *) p) + length);
00324         if (length > 4096)
00325                 return false;
00326         
00327         // now re-read the file
00328         unsigned int offset = (unsigned int) ((unsigned char *) p - header);
00329         fseek(m_file, offset, SEEK_SET);
00330         
00331         if (*p++ == ConvertFourByteCode("JUNK"))
00332         {
00333                 // skip it
00334                 length = *p;
00335                 fseek(m_file, length + 8, SEEK_CUR);
00336         }
00337         
00338         if (fread(header, 20, 1, m_file) != 1)
00339                 return false;
00340         
00341         p = (unsigned int *) header;
00342         
00343         if (*p++ != ConvertFourByteCode("LIST"))
00344                 return false;
00345         
00346         // skip length information
00347         p++;
00348         
00349         if (*p++ != ConvertFourByteCode("movi"))
00350                 return false;
00351         
00352         if (*p++ != ConvertFourByteCode("00db"))
00353                 return false;
00354         
00355         if (*p != (unsigned int) (m_nImageWidth * m_nImageHeight * 3))
00356                 return false;
00357 #endif
00358     
00359         return true;
00360 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:58