contrib/mul/mvl2/mvl2_video_from_sequence.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief A class for reading video files on windows platform
00004 // \author Louise Butcher
00005 
00006 #include "mvl2_video_from_sequence.h"
00007 #include <vil/vil_load.h>
00008 #include <vil/vil_convert.h>
00009 #include <vul/vul_file.h>
00010 #include <vul/vul_sprintf.h>
00011 #include <vcl_fstream.h>
00012 #include <vcl_cassert.h>
00013 #include <vcl_cstddef.h> // for std::size_t
00014 
00015 mvl2_video_from_sequence::mvl2_video_from_sequence()
00016 {
00017   use_seq_file_=false;
00018   is_initialized_=false;
00019   current_frame_=0;
00020   frame_rate_=25;
00021   width_=0;
00022   height_=0;
00023 }
00024 
00025 mvl2_video_from_sequence::~mvl2_video_from_sequence()
00026 {
00027 }
00028 
00029 vcl_string mvl2_video_from_sequence::is_a() const
00030 {
00031   return vcl_string("mvl2_video_from_sequence");
00032 }
00033 
00034 mvl2_video_reader* mvl2_video_from_sequence::clone() const
00035 {
00036   return new mvl2_video_from_sequence(*this);
00037 }
00038 
00039 // possible options : Grey
00040 bool mvl2_video_from_sequence::initialize( int /* width */, int /* height */,
00041                                            vcl_string format, vcl_string file_name)
00042 {
00043   use_colour_=true;
00044   if (!format.find(vcl_string("Grey"))) use_colour_=false;
00045   if (!vul_file::exists(file_name) || vul_file::is_directory(file_name))
00046   {
00047     vcl_cerr<<"File "<<file_name<<" does not exist\n";
00048     is_initialized_=false;
00049     return false;
00050   }
00051 
00052   if (vul_file::extension(file_name.c_str())==vcl_string(".seq"))
00053   {
00054     char buffer[201];
00055     vcl_ifstream ifile(file_name.c_str());
00056     use_seq_file_=true;
00057 
00058     while (!ifile.eof())
00059     {
00060       ifile.getline(buffer,200);
00061       vcl_string filename(buffer);
00062       if (filename.length()>0) list_files_.push_back(filename);
00063     }
00064     current_frame_=0;
00065     is_initialized_=true;
00066   }
00067 
00068   //knock off the extension
00069   vcl_size_t name_length=file_name.length();
00070   vcl_size_t dot_pos = file_name.find_last_of(".");
00071   file_name.erase(dot_pos, name_length);
00072 
00073   //Extract the largest possible number off the end
00074 
00075   no_digits_=0;
00076   offset_=0;
00077   name_length=file_name.length();
00078   for (int i=1;i<9;++i)
00079   {
00080     vcl_string last_i;
00081     last_i=file_name.substr(name_length-i,i);
00082     int curr_no=atoi(last_i.c_str());
00083     if (curr_no>0)
00084     {
00085       no_digits_=i;
00086       offset_=curr_no;
00087       file_stem_=file_name.substr(0,name_length-i);
00088     }
00089     else break;
00090   }
00091 
00092   current_frame_=0;
00093   is_initialized_=true;
00094   vil_image_view<vxl_byte> loc_img;
00095   get_frame(loc_img);
00096   height_=loc_img.nj();
00097   width_=loc_img.ni();
00098   return true;
00099 }
00100 
00101 void mvl2_video_from_sequence::uninitialize()
00102 {
00103 }
00104 
00105 int mvl2_video_from_sequence::next_frame()
00106 {
00107   if (!is_initialized_) return -1;
00108 
00109   return ++current_frame_;
00110 }
00111 
00112 bool mvl2_video_from_sequence::get_frame(vil_image_view<vxl_byte>& image)
00113 {
00114   vcl_string curr_file;
00115 
00116   if (use_seq_file_)
00117   {
00118     if (current_frame_<list_files_.size())
00119     {
00120       curr_file=list_files_[current_frame_];
00121     }
00122     else
00123     {
00124       return false;
00125     }
00126   }
00127   else
00128   {
00129     int currno=current_frame_+offset_;
00130     curr_file=vul_sprintf("%s%d.jpg",file_stem_.c_str(),currno);
00131   }
00132 
00133   vil_image_view_base_sptr image_view_sptr;
00134   if (!(image_view_sptr=vil_load(curr_file.c_str())))
00135   {
00136     vcl_cout<<"Unable to load : " << curr_file<<vcl_endl;
00137     return false;
00138   }
00139   if (use_colour_)
00140   {
00141     image.deep_copy(image_view_sptr);
00142   }
00143   else
00144   {
00145     image.deep_copy(vil_convert_to_grey_using_rgb_weighting(image_view_sptr));
00146   }
00147   width_=image.ni();
00148   height_=image.nj();
00149   return true;
00150 }
00151 
00152 void mvl2_video_from_sequence::reset_frame()
00153 {
00154   current_frame_=0;
00155 }
00156 
00157 void mvl2_video_from_sequence::set_frame_rate(double /*frame_rate*/)
00158 {
00159   vcl_cerr << "mvl2_video_from_sequence::set_frame_rate() NYI\n";
00160 }
00161 
00162 double mvl2_video_from_sequence::get_frame_rate() const
00163 {
00164   return frame_rate_;
00165 }
00166 
00167 int mvl2_video_from_sequence::get_width() const
00168 {
00169   return width_;
00170 }
00171 
00172 int mvl2_video_from_sequence::get_height() const
00173 {
00174   return height_;
00175 }
00176 
00177 void mvl2_video_from_sequence::set_capture_size(int /*width*/,int /*height*/)
00178 {
00179   vcl_cerr << "mvl2_video_from_sequence::set_capture_size() NYI\n";
00180 }
00181 
00182 int mvl2_video_from_sequence::length()
00183 {
00184   if (!is_initialized_) return -1;
00185   return int(list_files_.size());
00186 }
00187 
00188 int mvl2_video_from_sequence::seek(int frame_number)
00189 {
00190   assert (frame_number >= 0);
00191   current_frame_=frame_number;
00192   return current_frame_;
00193 }