RasterlistInputSeries.C

Go to the documentation of this file.
00001 /*!@file Transport/RasterlistInputSeries.C Reads image files from a list of filenames contained in a separate file */
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/Transport/RasterlistInputSeries.C $
00035 // $Id: RasterlistInputSeries.C 13831 2010-08-26 20:45:58Z lior $
00036 //
00037 
00038 #ifndef TRANSPORT_RASTERLISTINPUTSERIES_C_DEFINED
00039 #define TRANSPORT_RASTERLISTINPUTSERIES_C_DEFINED
00040 
00041 #include "Transport/RasterlistInputSeries.H"
00042 
00043 #include "Raster/Raster.H"
00044 
00045 #include <fstream>
00046 
00047 // ######################################################################
00048 RasterlistInputSeries::RasterlistInputSeries(OptionManager& mgr)
00049   :
00050   FrameIstream(mgr, "Raster-List Input Series", "RasterlistInputSeries"),
00051   itsListFname(),
00052   itsFiles(),
00053   itsFrameNumber(-1)
00054 {}
00055 
00056 // ######################################################################
00057 RasterlistInputSeries::~RasterlistInputSeries()
00058 {}
00059 
00060 // ######################################################################
00061 void RasterlistInputSeries::setConfigInfo(const std::string& fname)
00062 {
00063   // NOTE: if you modify any behavior here, then please update the
00064   // corresponding documentation for the global "--in" option inside
00065   // the OPT_InputFrameSource definition in Media/MediaOpts.C
00066 
00067   this->setListFile(fname);
00068 }
00069 
00070 // ######################################################################
00071 bool RasterlistInputSeries::setFrameNumber(int n)
00072 {
00073   ASSERT(n >= 0);
00074   itsFrameNumber = n;
00075 
00076   return true;
00077 }
00078 
00079 // ######################################################################
00080 GenericFrameSpec RasterlistInputSeries::peekFrameSpec()
00081 {
00082   return itsFrameSpec;
00083 }
00084 
00085 // ######################################################################
00086 GenericFrame RasterlistInputSeries::readFrame()
00087 {
00088   if (itsFrameNumber < 0)
00089     LFATAL("expected a non-negative frame number, but got %d",
00090            itsFrameNumber);
00091 
00092   if (size_t(itsFrameNumber) >= itsFiles.size())
00093     // out of input; end of stream
00094     return GenericFrame();
00095 
00096   if (!Raster::fileExists(itsFiles[itsFrameNumber]))
00097     {
00098       LINFO("at line %d of %s: no such file: %s",
00099             itsFrameNumber, itsListFname.c_str(),
00100             itsFiles[itsFrameNumber].c_str());
00101       return GenericFrame();
00102     }
00103 
00104   const GenericFrame result =
00105     Raster::ReadFrame(itsFiles[itsFrameNumber]);
00106 
00107   //if (result.frameSpec() != itsFrameSpec)
00108   //  LFATAL("at line %d of %s: expected %s to have format '%s', "
00109   //         "but got format '%s'",
00110   //         itsFrameNumber, itsListFname.c_str(),
00111   //         itsFiles[itsFrameNumber].c_str(),
00112   //         itsFrameSpec.getDescription().c_str(),
00113   //         result.frameSpec().getDescription().c_str());
00114 
00115   return result;
00116 }
00117 
00118 // ######################################################################
00119 void RasterlistInputSeries::setListFile(const std::string& s)
00120 {
00121   if (s.length() == 0)
00122     LFATAL("expected a valid filename, but got an empty string");
00123 
00124   std::ifstream ifs(s.c_str());
00125 
00126   if (!ifs.is_open())
00127     LFATAL("couldn't open file '%s' for reading", s.c_str());
00128 
00129   std::vector<std::string> names;
00130 
00131   std::string line;
00132   while (std::getline(ifs, line))
00133     names.push_back(line);
00134 
00135   LINFO("read %"ZU" filenames from %s", names.size(), s.c_str());
00136 
00137   if (names.size() == 0)
00138     LFATAL("Expected at least one image filename in %s, "
00139            "but found none", s.c_str());
00140 
00141   itsFrameSpec = Raster::getFrameSpec(names[0]);
00142   itsListFname = s;
00143   itsFiles.swap(names);
00144 }
00145 
00146 // ######################################################################
00147 /* So things look consistent in everyone's emacs... */
00148 /* Local Variables: */
00149 /* mode: c++ */
00150 /* indent-tabs-mode: nil */
00151 /* End: */
00152 
00153 #endif // TRANSPORT_RASTERLISTINPUTSERIES_C_DEFINED
Generated on Sun May 8 08:06:57 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3