contrib/brl/bbas/vidl2/vidl2_image_list_istream.cxx

Go to the documentation of this file.
00001 // This is brl/bbas/vidl2/vidl2_image_list_istream.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Matt Leotta
00008 // \date   19 Dec 2005
00009 //
00010 //-----------------------------------------------------------------------------
00011 
00012 #include "vidl2_image_list_istream.h"
00013 #include "vidl2_frame.h"
00014 #include "vidl2_convert.h"
00015 #include <vcl_algorithm.h>
00016 #include <vul/vul_file_iterator.h>
00017 #include <vul/vul_file.h>
00018 #include <vil/vil_image_resource_sptr.h>
00019 #include <vil/vil_load.h>
00020 
00021 //--------------------------------------------------------------------------------
00022 
00023 
00024 //: The initial frame index
00025 // \note the initial frame index is invalid until advance() is called
00026 static const unsigned int INIT_INDEX = unsigned(-1);
00027 
00028 
00029 //: Constructor
00030 vidl2_image_list_istream::
00031 vidl2_image_list_istream()
00032   : index_(INIT_INDEX),
00033     ni_(0), nj_(0),
00034     format_(VIDL2_PIXEL_FORMAT_UNKNOWN),
00035     current_frame_(NULL) {}
00036 
00037 
00038 //: Constructor
00039 vidl2_image_list_istream::
00040 vidl2_image_list_istream(const vcl_string& glob)
00041   : index_(INIT_INDEX),
00042     ni_(0), nj_(0),
00043     format_(VIDL2_PIXEL_FORMAT_UNKNOWN),
00044     current_frame_(NULL)
00045 {
00046   open(glob);
00047 }
00048 
00049 
00050 //: Open a new stream using a file glob (see vul_file_iterator)
00051 // \note files are loaded in alphanumeric order by path name
00052 bool
00053 vidl2_image_list_istream::
00054 open(const vcl_string& glob)
00055 {
00056   vcl_vector<vcl_string> filenames;
00057 
00058   for (vul_file_iterator fit=glob; fit; ++fit) {
00059     // check to see if file is a directory.
00060     if (vul_file::is_directory(fit()))
00061       continue;
00062     filenames.push_back(fit());
00063   }
00064 
00065   // no matching filenames
00066   if (filenames.empty()){
00067     vcl_cerr << "In vidl2_image_list_istream(.) - no files to open\n";
00068     return false;
00069   }
00070   // Sort - because the file iterator uses readdir() it does not
00071   //        iterate over files in alphanumeric order
00072   vcl_sort(filenames.begin(),filenames.end());
00073 
00074   bool can_open = open(filenames);
00075 
00076   if(!can_open){
00077     vcl_cerr << "In vidl2_image_list_istream(.) -can't open files as images\n";
00078     for(vcl_vector<vcl_string>::iterator fit = filenames.begin();
00079         fit != filenames.end(); ++fit)
00080       vcl_cerr << *fit << '\n';
00081     return false;
00082   }
00083 
00084   return true;
00085 }
00086 
00087 
00088 //: Open a new stream using a vector of file paths
00089 bool
00090 vidl2_image_list_istream::
00091 open(const vcl_vector<vcl_string>& paths)
00092 {
00093   image_paths_.clear();
00094   // test each file to ensure it exists and is a supported image format
00095   for (vcl_vector<vcl_string>::const_iterator i = paths.begin(); i!=paths.end(); ++i)
00096   {
00097     vil_image_resource_sptr img = vil_load_image_resource(i->c_str());
00098     if (img)
00099     {
00100       if(ni_ == 0 || nj_ == 0)
00101       {
00102         ni_ = img->ni();
00103         nj_ = img->nj();
00104         // convert the first frame to get the pixel format
00105         format_ = vidl2_convert_to_frame(img->get_view())->pixel_format();
00106       }
00107       else if(ni_ != img->ni() || nj_ != img->nj())
00108         continue;
00109       image_paths_.push_back(*i);
00110     }
00111   }
00112   index_ = INIT_INDEX;
00113   current_frame_ = NULL;
00114   return !image_paths_.empty();
00115 }
00116 
00117 
00118 //: Close the stream
00119 void
00120 vidl2_image_list_istream::
00121 close()
00122 {
00123   image_paths_.clear();
00124   index_ = INIT_INDEX;
00125   current_frame_ = NULL;
00126   ni_ = 0;
00127   nj_ = 0;
00128   format_ = VIDL2_PIXEL_FORMAT_UNKNOWN;
00129 }
00130 
00131 
00132 //: Advance to the next frame (but do not load the next image)
00133 bool
00134 vidl2_image_list_istream::
00135 advance()
00136 {
00137   current_frame_ = NULL;
00138   if(index_ < image_paths_.size() || index_ == INIT_INDEX )
00139     return ++index_ < image_paths_.size();
00140 
00141   return false;
00142 }
00143 
00144 
00145 //: Read the next frame from the stream
00146 vidl2_frame_sptr
00147 vidl2_image_list_istream::read_frame()
00148 {
00149   advance();
00150   return current_frame();
00151 }
00152 
00153 
00154 //: Return the current frame in the stream
00155 vidl2_frame_sptr
00156 vidl2_image_list_istream::current_frame()
00157 {
00158   if (is_valid()){
00159     if(!current_frame_){
00160       vil_image_resource_sptr img = vil_load_image_resource(image_paths_[index_].c_str());
00161       current_frame_ = vidl2_convert_to_frame(img->get_view());
00162     }
00163     return current_frame_;
00164   }
00165   return NULL;
00166 }
00167 
00168 
00169 //: Return the path to the current image in the stream
00170 vcl_string
00171 vidl2_image_list_istream::current_path() const
00172 {
00173   if (is_valid()){
00174     return image_paths_[index_];
00175   }
00176   return "";
00177 }
00178 
00179 
00180 //: Seek to the given frame number (but do not load the image)
00181 // \returns true if successful
00182 bool
00183 vidl2_image_list_istream::
00184 seek_frame(unsigned int frame_number)
00185 {
00186   if (is_open() && frame_number < image_paths_.size()){
00187     if(index_ != frame_number)
00188       current_frame_ = NULL;
00189     index_ = frame_number;
00190     return true;
00191   }
00192   return false;
00193 }
00194 

Generated on Fri Nov 21 05:20:57 2008 for contrib/brl/bbas/vidl2 by  doxygen 1.5.1