00001 /*!@file Transport/RasterInputSeries.C */ 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/RasterInputSeries.C $ 00035 // $Id: RasterInputSeries.C 12962 2010-03-06 02:13:53Z irock $ 00036 // 00037 00038 #ifndef TRANSPORT_RASTERINPUTSERIES_C_DEFINED 00039 #define TRANSPORT_RASTERINPUTSERIES_C_DEFINED 00040 00041 #include "Transport/RasterInputSeries.H" 00042 00043 #include "Component/GlobalOpts.H" 00044 #include "Component/ModelOptionDef.H" 00045 #include "Component/OptionManager.H" 00046 #include "Image/Image.H" 00047 #include "Image/Pixels.H" 00048 #include "Raster/GenericFrame.H" 00049 #include "Raster/Raster.H" 00050 #include "Transport/RasterInputOptions.H" 00051 #include "Transport/TransportOpts.H" 00052 #include "Util/TextLog.H" 00053 #include "Util/sformat.H" 00054 00055 #include <cstdio> // for sscanf() 00056 00057 namespace 00058 { 00059 // ###################################################################### 00060 //! compute input filename for current frame 00061 std::string computeInputFileName(const std::string& stem, 00062 const int framenumber) 00063 { 00064 // NOTE: if you modify any behavior here, then please update the 00065 // corresponding documentation for the global "--in" option inside 00066 // the OPT_InputFrameSource definition in Media/MediaOpts.C 00067 00068 ASSERT(framenumber >= 0); 00069 00070 // if there is a '#' in the stem, replace it with the frame number 00071 const std::string::size_type hashpos = stem.find_first_of('#'); 00072 00073 if (hashpos != stem.npos) 00074 { 00075 std::string fname = stem; 00076 00077 // now, if there is a second '#' in the stem, then the 00078 // characters in between the two hashes will determine the 00079 // numeric format 00080 const std::string::size_type hashpos2 = 00081 (hashpos + 1 < stem.size()) 00082 ? stem.find_first_of('#', hashpos + 1) 00083 : stem.npos; 00084 00085 if (hashpos2 == hashpos + 1) 00086 { 00087 fname.replace(hashpos, 2, sformat("%d", framenumber)); 00088 } 00089 else if (hashpos2 != stem.npos) 00090 { 00091 ASSERT(hashpos2 > hashpos); 00092 00093 const std::string::size_type flen = hashpos2-hashpos-1; 00094 00095 const std::string format = stem.substr(hashpos+1, flen); 00096 int width = -1; 00097 if (sscanf(format.c_str(), "%d", &width) != 1 00098 || width < 0 00099 || width >= 256) 00100 LFATAL("invalid number format '#%s#' in file stem %s; " 00101 "expected '#nnn#' where nnn is a non-negative integer " 00102 "less than 256", 00103 format.c_str(), stem.c_str()); 00104 fname.replace(hashpos, flen+2, 00105 sformat("%0*d", width, framenumber)); 00106 } 00107 else 00108 { 00109 fname.replace(hashpos, 1, sformat("%06d", framenumber)); 00110 } 00111 00112 return fname; 00113 } 00114 00115 // else... no '#', so just return the filename as-is 00116 return stem; 00117 } 00118 } 00119 00120 // ###################################################################### 00121 RasterInputSeries::RasterInputSeries(OptionManager& mgr) 00122 : 00123 FrameIstream(mgr, "Raster Input Series", "RasterInputSeries"), 00124 itsOptions(new RasterInputOptions(mgr)), 00125 itsLogFile(&OPT_TextLogFile, this), 00126 itsRasterFileFormat(&OPT_InputRasterFileFormat, this), 00127 itsStem(""), 00128 itsPrevFilename(""), 00129 itsFrameNumber(-1) 00130 { 00131 this->addSubComponent(itsOptions); 00132 } 00133 00134 // ###################################################################### 00135 RasterInputSeries::~RasterInputSeries() 00136 {} 00137 00138 // ###################################################################### 00139 void RasterInputSeries::setConfigInfo(const std::string& filestem) 00140 { 00141 // NOTE: if you modify any behavior here, then please update the 00142 // corresponding documentation for the global "--in" option inside 00143 // the OPT_InputFrameSource definition in Media/MediaOpts.C 00144 00145 this->setFileStem(filestem); 00146 } 00147 00148 // ###################################################################### 00149 bool RasterInputSeries::setFrameNumber(int n) 00150 { 00151 ASSERT(n >= 0); 00152 itsFrameNumber = n; 00153 00154 return true; 00155 } 00156 00157 // ###################################################################### 00158 GenericFrameSpec RasterInputSeries::peekFrameSpec() 00159 { 00160 if (itsFrameNumber < 0) 00161 LFATAL("frame number is %d, but peekFrameSpec() requires a " 00162 "non-negative frame number", itsFrameNumber); 00163 00164 const std::string fname(computeInputFileName(itsStem, itsFrameNumber)); 00165 00166 return Raster::getFrameSpec(fname, itsRasterFileFormat.getVal()); 00167 } 00168 00169 // ###################################################################### 00170 GenericFrame RasterInputSeries::readFrame() 00171 { 00172 if (itsFrameNumber < 0) 00173 LFATAL("frame number is %d, but readFrame() requires a " 00174 "non-negative frame number", itsFrameNumber); 00175 00176 // figure out the file name to use: 00177 const std::string fname(computeInputFileName(itsStem, itsFrameNumber)); 00178 00179 // check if we've already read that file; if so, then return an 00180 // empty image 00181 if (fname == itsPrevFilename) 00182 { 00183 LINFO("repeated input file skipped: %s", fname.c_str()); 00184 return GenericFrame(); 00185 } 00186 00187 // check whether the file exists; if not, then return an empty image: 00188 if (!Raster::fileExists(fname, itsRasterFileFormat.getVal())) 00189 { 00190 LINFO("no such file: %s", fname.c_str()); 00191 return GenericFrame(); 00192 } 00193 00194 // load the image: 00195 const GenericFrame ima = 00196 Raster::ReadFrame(fname, itsRasterFileFormat.getVal()); 00197 00198 itsPrevFilename = fname; 00199 00200 textLog(itsLogFile.getVal(), "ReadFrame", fname); 00201 00202 return ima; 00203 } 00204 00205 // ###################################################################### 00206 void RasterInputSeries::setFileStem(const std::string& s) 00207 { 00208 itsStem = s; 00209 } 00210 00211 // ###################################################################### 00212 /* So things look consistent in everyone's emacs... */ 00213 /* Local Variables: */ 00214 /* indent-tabs-mode: nil */ 00215 /* End: */ 00216 00217 #endif // TRANSPORT_RASTERINPUTSERIES_C_DEFINED