00001 /*!@file Media/MgzDecoder.C Low-level decoder for multi-frame "mgz" file format */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Media/MgzDecoder.C $ 00035 // $Id: MgzDecoder.C 8915 2007-10-26 00:55:39Z rjpeters $ 00036 // 00037 00038 #ifndef MEDIA_MGZDECODER_C_DEFINED 00039 #define MEDIA_MGZDECODER_C_DEFINED 00040 00041 #include "Media/MgzDecoder.H" 00042 #include <sys/errno.h> 00043 00044 // ###################################################################### 00045 MgzDecoder::MgzDecoder(const std::string& fname) 00046 : 00047 itsFileName(fname) 00048 { 00049 // open the file: 00050 itsFile = gzopen(fname.c_str(), "rb"); 00051 00052 if (itsFile == NULL) 00053 { 00054 if (errno) PLFATAL("Could not open '%s' for reading", fname.c_str()); 00055 else LFATAL("No memory to open '%s' for reading", fname.c_str()); 00056 } 00057 } 00058 00059 // ###################################################################### 00060 MgzDecoder::~MgzDecoder() 00061 { 00062 if (itsFile) 00063 { 00064 int err = gzclose(itsFile); 00065 if (err == Z_ERRNO) 00066 PLFATAL("Error closing file"); 00067 else if (err) 00068 LFATAL("Error closing file: %s", gzerror(itsFile, &err)); 00069 } 00070 } 00071 00072 // ###################################################################### 00073 GenericFrame MgzDecoder::readFrame() 00074 { 00075 if (itsFile == NULL) LFATAL("No file open for reading"); 00076 00077 // NOTE: This is not portable! 00078 // read the dims: 00079 uint32 wht[3]; 00080 int nr = gzread(itsFile, wht, 3 * sizeof(uint32)); 00081 if (nr == 0 && gzeof(itsFile)) 00082 return GenericFrame(); // input exhausted 00083 else if (nr == -1) 00084 { 00085 if (errno == 0) return GenericFrame(); // undocumented EOF condition? 00086 else PLFATAL("Error reading image header from file"); 00087 } 00088 else if (nr != 3 * sizeof(uint32)) 00089 LFATAL("Short read on image header"); 00090 00091 const Dims dims(wht[0], wht[1]); 00092 const GenericFrame::NativeType typ = GenericFrame::NativeType(wht[2]); 00093 const size_t siz = dims.sz(); 00094 00095 GenericFrame frame; 00096 00097 switch(typ) 00098 { 00099 case GenericFrame::GRAY_F32: 00100 { 00101 // read the flags: 00102 int32 flags; 00103 if (gzread(itsFile, &flags, sizeof(int32)) != int(sizeof(int32))) 00104 PLFATAL("Error reading image flags from file"); 00105 00106 // read the image: 00107 Image<float> f(dims, NO_INIT); 00108 const size_t s = siz * sizeof(float); 00109 if (gzread(itsFile, f.getArrayPtr(), s) != int(s)) 00110 PLFATAL("Error reading image data from file"); 00111 frame = GenericFrame(f, flags); 00112 } 00113 break; 00114 00115 case GenericFrame::GRAY_U8: 00116 { 00117 Image<byte> f(dims, NO_INIT); 00118 const size_t s = siz * sizeof(byte); 00119 if (gzread(itsFile, f.getArrayPtr(), s) != int(s)) 00120 PLFATAL("Error reading image data from file"); 00121 frame = GenericFrame(f); 00122 } 00123 break; 00124 00125 case GenericFrame::RGB_U8: 00126 { 00127 Image< PixRGB<byte> > f(dims, NO_INIT); 00128 const size_t s = siz * 3 * sizeof(byte); 00129 if (gzread(itsFile, f.getArrayPtr(), s) != int(s)) 00130 PLFATAL("Error reading image data from file"); 00131 frame = GenericFrame(f); 00132 } 00133 break; 00134 00135 case GenericFrame::RGB_F32: 00136 { 00137 // read the flags: 00138 int32 flags; 00139 if (gzread(itsFile, &flags, sizeof(int32)) != int(sizeof(int32))) 00140 PLFATAL("Error reading image flags from file"); 00141 00142 // read the image: 00143 Image< PixRGB<float> > f(dims, NO_INIT); 00144 const size_t s = siz * sizeof(PixRGB<float>); 00145 if (gzread(itsFile, f.getArrayPtr(), s) != int(s)) 00146 PLFATAL("Error reading image data from file"); 00147 frame = GenericFrame(f, flags); 00148 } 00149 break; 00150 00151 case GenericFrame::VIDEO: 00152 { 00153 int32 vidformat; 00154 if (gzread(itsFile, &vidformat, sizeof(vidformat)) != 00155 int(sizeof(vidformat))) 00156 PLFATAL("Error reading video format from file"); 00157 00158 if (vidformat < 0 || vidformat > VIDFMT_AUTO) 00159 PLFATAL("Invalid VideoFormat value %d in mgz file %s", 00160 int(vidformat), itsFileName.c_str()); 00161 00162 int32 byteswap; 00163 if (gzread(itsFile, &byteswap, sizeof(byteswap)) != 00164 int(sizeof(byteswap))) 00165 PLFATAL("Error reading byteswap from file"); 00166 00167 const size_t s = getFrameSize(VideoFormat(vidformat), dims); 00168 00169 ArrayHandle<byte> f(new ArrayData<byte>(Dims(s,1), NO_INIT)); 00170 if (gzread(itsFile, f.uniq().dataw(), s) != int(s)) 00171 PLFATAL("Error reading video data from file"); 00172 00173 frame = GenericFrame(VideoFrame(f, dims, 00174 VideoFormat(vidformat), 00175 bool(byteswap))); 00176 } 00177 break; 00178 00179 case GenericFrame::NONE: 00180 // nothing to read here, just leave the GenericFrame as empty 00181 break; 00182 00183 default: 00184 LFATAL("Cannot read frames of type %d", int(typ)); 00185 } 00186 00187 return frame; 00188 } 00189 00190 // ###################################################################### 00191 /* So things look consistent in everyone's emacs... */ 00192 /* Local Variables: */ 00193 /* mode: c++ */ 00194 /* indent-tabs-mode: nil */ 00195 /* End: */ 00196 00197 #endif // MEDIA_MGZDECODER_C_DEFINED