YuvParser.C

Go to the documentation of this file.
00001 /*!@file Raster/YuvParser.C Parse raw YUV image files. */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // 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@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Raster/YuvParser.C $
00035 // $Id: YuvParser.C 8897 2007-10-24 22:22:40Z rjpeters $
00036 //
00037 
00038 #include "Raster/YuvParser.H"
00039 
00040 #include "Raster/GenericFrame.H"
00041 #include "Util/FileUtil.H"
00042 #include "Util/StringConversions.H"
00043 #include "Video/VideoFrame.H"
00044 #include "rutz/bzip2stream.h"
00045 #include "rutz/error_context.h"
00046 #include "rutz/gzstreambuf.h"
00047 #include "rutz/sfmt.h"
00048 #include "rutz/shared_ptr.h"
00049 #include "rutz/trace.h"
00050 
00051 // ######################################################################
00052 namespace dummy_namespace_to_avoid_gcc411_bug_YuvParser_C
00053 {
00054   Dims defaultDims(640, 480);
00055   bool strictLength = true;
00056 
00057   enum CompType
00058     {
00059       COMP_NONE,
00060       COMP_GZIP,
00061       COMP_BZIP2
00062     };
00063 
00064   struct VideoFileInfo
00065   {
00066     VideoFormat format;
00067     Dims dims;
00068     CompType ctype;
00069     bool beStrict;
00070   };
00071 
00072   VideoFileInfo getVideoFileInfoFromFilename(const std::string& fname)
00073   {
00074     VideoFileInfo result;
00075 
00076     std::string base;
00077     std::string ext = nodotExtension(fname, &base);
00078 
00079     LDEBUG("ext is %s", ext.c_str());
00080 
00081     if (ext.compare("gz") == 0)
00082       {
00083         ext = nodotExtension(base, &base);
00084         LDEBUG("new ext is %s", ext.c_str());
00085         result.ctype = COMP_GZIP;
00086       }
00087     else if (ext.compare("bz2") == 0)
00088       {
00089         ext = nodotExtension(base, &base);
00090         LDEBUG("new ext is %s", ext.c_str());
00091         result.ctype = COMP_BZIP2;
00092       }
00093     else
00094       {
00095         result.ctype = COMP_NONE;
00096       }
00097 
00098     const std::string dimsstr = nodotExtension(base);
00099 
00100     LDEBUG("dimsstr is '%s'", dimsstr.c_str());
00101 
00102     if (dimsstr.size() == 0)
00103       {
00104         LERROR("no <width>x<height> specification found in '%s'; "
00105                "assuming default dims of %dx%d instead",
00106                fname.c_str(), defaultDims.w(), defaultDims.h());
00107         result.dims = defaultDims;
00108 
00109         // we didn't get explicit dims, so let's be picky about the
00110         // file size matching the defaultDims (--yuv-dims), unless the
00111         // user also requests loose matching (--yuv-dims-loose)
00112         result.beStrict = strictLength;
00113       }
00114     else
00115       {
00116         result.dims = fromStr<Dims>(dimsstr);
00117         LDEBUG("parsed dims as %dx%d",
00118                result.dims.w(), result.dims.h());
00119 
00120         // OK, the user gave us some explicit dims, so let's not be
00121         // picky about whether the file size matches the
00122         // dims+pixformat
00123         result.beStrict = false;
00124       }
00125 
00126     result.format = fromStr<VideoFormat>(ext);
00127 
00128     return result;
00129   }
00130 }
00131 
00132 using namespace dummy_namespace_to_avoid_gcc411_bug_YuvParser_C;
00133 
00134 // ######################################################################
00135 struct YuvParser::Rep
00136 {
00137   Rep(const char* fname_) :
00138     fname(fname_),
00139     info(getVideoFileInfoFromFilename(fname))
00140   {}
00141 
00142   const std::string fname;
00143   const VideoFileInfo info;
00144 };
00145 
00146 // ######################################################################
00147 YuvParser::YuvParser(const char* fname) :
00148   rep(new Rep(fname))
00149 {
00150 GVX_TRACE(__PRETTY_FUNCTION__);
00151 }
00152 
00153 // ######################################################################
00154 YuvParser::~YuvParser()
00155 {
00156 GVX_TRACE(__PRETTY_FUNCTION__);
00157   delete rep;
00158 }
00159 
00160 // ######################################################################
00161 void YuvParser::setDefaultDims(const Dims& d)
00162 {
00163 GVX_TRACE(__PRETTY_FUNCTION__);
00164   defaultDims = d;
00165 }
00166 
00167 // ######################################################################
00168 Dims YuvParser::getDefaultDims()
00169 {
00170 GVX_TRACE(__PRETTY_FUNCTION__);
00171   return defaultDims;
00172 }
00173 
00174 // ######################################################################
00175 void YuvParser::setStrictDims(bool v)
00176 {
00177 GVX_TRACE(__PRETTY_FUNCTION__);
00178   strictLength = v;
00179 }
00180 
00181 // ######################################################################
00182 bool getStrictDims()
00183 {
00184 GVX_TRACE(__PRETTY_FUNCTION__);
00185   return strictLength;
00186 }
00187 
00188 // ######################################################################
00189 GenericFrameSpec YuvParser::getFrameSpec() const
00190 {
00191   GenericFrameSpec result;
00192 
00193   result.nativeType = GenericFrame::VIDEO;
00194   result.videoFormat = rep->info.format;
00195   result.videoByteSwap = false;
00196   result.dims = rep->info.dims;
00197   result.floatFlags = 0;
00198 
00199   return result;
00200 }
00201 
00202 // ######################################################################
00203 std::string YuvParser::getComments() const
00204 { return ""; /* no comments in raw YUV files */ }
00205 
00206 // ######################################################################
00207 uint YuvParser::getTagCount() const
00208 { return 0; }
00209 
00210 // ######################################################################
00211 bool YuvParser::getTag(uint tag, std::string &name, std::string &value) const
00212 { return false; }
00213 
00214 // ######################################################################
00215 GenericFrame YuvParser::getFrame()
00216 {
00217 GVX_TRACE(__PRETTY_FUNCTION__);
00218   const bool byteswap = false;
00219 
00220   const VideoFormat vf = rep->info.format;
00221 
00222   if (rep->info.ctype == COMP_GZIP)
00223     {
00224       GVX_ERR_CONTEXT(rutz::sfmt("reading gzipped video file '%s'",
00225                                  rep->fname.c_str()));
00226 
00227       rutz::shared_ptr<std::istream> strm =
00228         rutz::igzopen(rep->fname.c_str());
00229 
00230       return GenericFrame
00231         (VideoFrame::fromStream(*strm, rep->info.dims, vf, byteswap));
00232     }
00233 
00234   else if (rep->info.ctype == COMP_BZIP2)
00235     {
00236       GVX_ERR_CONTEXT(rutz::sfmt("reading bzip2-compressed "
00237                                  "video file '%s'",
00238                                  rep->fname.c_str()));
00239 
00240       rutz::shared_ptr<std::istream> strm =
00241         rutz::ibzip2open(rep->fname.c_str());
00242 
00243       return GenericFrame
00244         (VideoFrame::fromStream(*strm, rep->info.dims, vf, byteswap));
00245     }
00246 
00247   // else...
00248   GVX_ERR_CONTEXT(rutz::sfmt("reading raw video file '%s'",
00249                              rep->fname.c_str()));
00250 
00251   return GenericFrame
00252     (VideoFrame::fromFile(rep->fname.c_str(), rep->info.dims,
00253                           vf, byteswap, rep->info.beStrict));
00254 }
00255 
00256 // ######################################################################
00257 /* So things look consistent in everyone's emacs... */
00258 /* Local Variables: */
00259 /* mode: c++ */
00260 /* indent-tabs-mode: nil */
00261 /* End: */
Generated on Sun May 8 08:05:34 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3