MgzDecoder.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
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
00078
00079 uint32 wht[3];
00080 int nr = gzread(itsFile, wht, 3 * sizeof(uint32));
00081 if (nr == 0 && gzeof(itsFile))
00082 return GenericFrame();
00083 else if (nr == -1)
00084 {
00085 if (errno == 0) return GenericFrame();
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
00102 int32 flags;
00103 if (gzread(itsFile, &flags, sizeof(int32)) != int(sizeof(int32)))
00104 PLFATAL("Error reading image flags from file");
00105
00106
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
00138 int32 flags;
00139 if (gzread(itsFile, &flags, sizeof(int32)) != int(sizeof(int32)))
00140 PLFATAL("Error reading image flags from file");
00141
00142
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
00181 break;
00182
00183 default:
00184 LFATAL("Cannot read frames of type %d", int(typ));
00185 }
00186
00187 return frame;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197 #endif // MEDIA_MGZDECODER_C_DEFINED