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), current_frame_(NULL) {} 00033 00034 00035 //: Constructor 00036 vidl2_image_list_istream:: 00037 vidl2_image_list_istream(const vcl_string& glob) 00038 : index_(INIT_INDEX), current_frame_(NULL) { open(glob); } 00039 00040 00041 //: Open a new stream using a file glob (see vul_file_iterator) 00042 // \note files are loaded in alphanumeric order by path name 00043 bool 00044 vidl2_image_list_istream:: 00045 open(const vcl_string& glob) 00046 { 00047 vcl_vector<vcl_string> filenames; 00048 00049 for (vul_file_iterator fit=glob; fit; ++fit) { 00050 // check to see if file is a directory. 00051 if (vul_file::is_directory(fit())) 00052 continue; 00053 filenames.push_back(fit()); 00054 } 00055 00056 // no matching filenames 00057 if (filenames.empty()) 00058 return false; 00059 00060 // Sort - because the file iterator uses readdir() it does not 00061 // iterate over files in alphanumeric order 00062 vcl_sort(filenames.begin(),filenames.end()); 00063 00064 return open(filenames); 00065 } 00066 00067 00068 //: Open a new stream using a vector of file paths 00069 bool 00070 vidl2_image_list_istream:: 00071 open(const vcl_vector<vcl_string>& paths) 00072 { 00073 image_paths_.clear(); 00074 // test each file to ensure it exists and is a supported image format 00075 for (vcl_vector<vcl_string>::const_iterator i = paths.begin(); i!=paths.end(); ++i) 00076 { 00077 if (vil_load_image_resource(i->c_str())) 00078 image_paths_.push_back(*i); 00079 } 00080 index_ = INIT_INDEX; 00081 current_frame_ = NULL; 00082 return !image_paths_.empty(); 00083 } 00084 00085 00086 //: Close the stream 00087 void 00088 vidl2_image_list_istream:: 00089 close() 00090 { 00091 image_paths_.clear(); 00092 index_ = INIT_INDEX; 00093 current_frame_ = NULL; 00094 } 00095 00096 00097 //: Advance to the next frame (but do not load the next image) 00098 bool 00099 vidl2_image_list_istream:: 00100 advance() 00101 { 00102 current_frame_ = NULL; 00103 if(index_ < image_paths_.size() || index_ == INIT_INDEX ) 00104 return ++index_ < image_paths_.size(); 00105 00106 return false; 00107 } 00108 00109 00110 //: Read the next frame from the stream 00111 vidl2_frame_sptr 00112 vidl2_image_list_istream::read_frame() 00113 { 00114 advance(); 00115 return current_frame(); 00116 } 00117 00118 00119 //: Return the current frame in the stream 00120 vidl2_frame_sptr 00121 vidl2_image_list_istream::current_frame() 00122 { 00123 if (is_valid()){ 00124 if(!current_frame_){ 00125 vil_image_resource_sptr img = vil_load_image_resource(image_paths_[index_].c_str()); 00126 current_frame_ = vidl2_convert_to_frame(img->get_view()); 00127 } 00128 return current_frame_; 00129 } 00130 return NULL; 00131 } 00132 00133 00134 //: Seek to the given frame number (but do not load the image) 00135 // \returns true if successful 00136 bool 00137 vidl2_image_list_istream:: 00138 seek_frame(unsigned int frame_number) 00139 { 00140 if (is_open() && frame_number < image_paths_.size()){ 00141 if(index_ != frame_number) 00142 current_frame_ = NULL; 00143 index_ = frame_number; 00144 return true; 00145 } 00146 return false; 00147 } 00148
1.4.4