PfmParser.C

Go to the documentation of this file.
00001 /*!@file Raster/PfmParser.C Parse pfm 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: Laurent Itti <Itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Raster/PfmParser.C $
00035 // $Id: PfmParser.C 8790 2007-09-28 22:24:10Z rjpeters $
00036 //
00037 
00038 #include "Raster/PfmParser.H"
00039 
00040 #include "Util/Assert.H"
00041 #include "Image/Image.H"
00042 #include "Image/Pixels.H"
00043 #include "Raster/GenericFrame.H"
00044 #include "Util/FileUtil.H"
00045 #include "Util/log.H"
00046 #include "rutz/shared_ptr.h"
00047 
00048 #include <istream>
00049 #include <limits>
00050 #include <string>
00051 #include <deque>
00052 
00053 using std::deque;
00054 using std::string;
00055 
00056 // ######################################################################
00057 struct PfmParser::Rep
00058 {
00059   Rep(const std::string& fname) :
00060     strm(openMaybeCompressedFile(fname)),
00061     mode('\0'), w(-1), h(-1), maxGrey(1), comments("")
00062   {
00063     ASSERT(strm.get() != 0);
00064   }
00065 
00066   rutz::shared_ptr<std::istream> strm;
00067   char mode;
00068   int w, h;
00069   float maxGrey;
00070   string comments;
00071   deque<string> tagName;
00072   deque<string> tagValue;
00073 };
00074 
00075 // ######################################################################
00076 PfmParser::PfmParser(const std::string& fname) :
00077   rep(new Rep(fname))
00078 {
00079   const int c = rep->strm->get();
00080   if (c != 'P')
00081     LFATAL("Missing magic number in pbm file '%s'.", fname.c_str());
00082 
00083   (*rep->strm) >> rep->mode >> std::ws;
00084 
00085   string temp;
00086 
00087   // copy and concatenate optional comment line(s) starting with '#'
00088   // into comments string
00089   while (rep->strm->peek() == '#')
00090     {
00091       // get the full line
00092       std::getline((*rep->strm), temp, '\n');
00093       rep->comments += temp;
00094     }
00095 
00096   (*rep->strm) >> rep->w >> rep->h >> std::ws;
00097 
00098   // check for extra optional tags
00099   while (rep->strm->peek() == '!')
00100     {
00101       // get the full line, remove leading !
00102       std::getline((*rep->strm), temp, '\n');
00103       temp = temp.substr(1);
00104       rep->tagName.push_back(temp);
00105 
00106       // the next line contains the data with this tag
00107       std::getline((*rep->strm), temp, '\n');
00108       rep->tagValue.push_back(temp);
00109     }
00110 
00111   (*rep->strm) >> rep->maxGrey;
00112 
00113   // read one more character of whitespace from the stream after maxGrey
00114   const int spc = rep->strm->get();
00115   if (!isspace(spc))
00116     LFATAL("Missing whitespace after maxGrey in pfm file '%s'.", fname.c_str());
00117   LDEBUG("PFM Reading Image: %s", fname.c_str());
00118 }
00119 
00120 // ######################################################################
00121 PfmParser::~PfmParser()
00122 {
00123   delete rep;
00124 }
00125 
00126 // ######################################################################
00127 GenericFrameSpec PfmParser::getFrameSpec() const
00128 {
00129   GenericFrameSpec result;
00130 
00131   result.nativeType = GenericFrame::GRAY_F32;
00132   result.videoFormat = VIDFMT_AUTO;
00133   result.videoByteSwap = false;
00134   result.dims = Dims(rep->w, rep->h);
00135   result.floatFlags = 0;
00136 
00137   return result;
00138 }
00139 
00140 // ######################################################################
00141 string PfmParser::getComments() const
00142 { return rep->comments; }
00143 
00144 // ######################################################################
00145 uint PfmParser::getTagCount() const
00146 { return rep->tagName.size(); }
00147 
00148 // ######################################################################
00149 bool PfmParser::getTag(uint tag, string& name, string& value) const
00150 {
00151   if (tag < rep->tagName.size())
00152     {
00153       name  = rep->tagName[tag];
00154       value = rep->tagValue[tag];
00155       return true;
00156     }
00157   else
00158     return false;
00159 }
00160 
00161 // ######################################################################
00162 GenericFrame PfmParser::getFrame()
00163 {
00164   ASSERT(rep->mode == 'F');
00165 
00166   Image<float> img(rep->w, rep->h, NO_INIT);
00167   rep->strm->read(reinterpret_cast<char*>(img.getArrayPtr()),
00168                   img.getSize() * sizeof(float));
00169   return GenericFrame(img, /* flags */ 0);
00170 }
00171 
00172 // ######################################################################
00173 /* So things look consistent in everyone's emacs... */
00174 /* Local Variables: */
00175 /* indent-tabs-mode: nil */
00176 /* End: */
Generated on Sun May 8 08:05:34 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3