contrib/mul/mvl2/mvl2_image_format_plugin.cxx
Go to the documentation of this file.
00001 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00002 #pragma implementation
00003 #endif
00004 //:
00005 // \file
00006 // \brief Interface for loading avi frames as image formats
00007 // \author Franck Bettinger
00008 // \date   Mon Mar 18 06:57:49 2002
00009 // This class implements the plugin to load avi frames as a new formats
00010 //
00011 // \verbatim
00012 //  Modifications
00013 // \endverbatim
00014 
00015 #include "mvl2_image_format_plugin.h"
00016 #include <vcl_iostream.h>
00017 #include <vcl_string.h>
00018 #include <vcl_cstdlib.h> // for vcl_atoi()
00019 #include <vul/vul_file.h>
00020 #include <mvl2/mvl2_video_from_avi.h>
00021 #include <mvl2/mvl2_video_from_sequence.h>
00022 #include <vil/vil_copy.h>
00023 #include <vil/vil_image_view.h>
00024 
00025 //=======================================================================
00026 
00027 mvl2_image_format_plugin::mvl2_image_format_plugin()
00028 {
00029 }
00030 
00031 //=======================================================================
00032 
00033 mvl2_image_format_plugin::~mvl2_image_format_plugin()
00034 {
00035   for (vcl_map<vcl_string,mvl2_video_reader*>::iterator it=mvl2_list_.begin();
00036        it!=mvl2_list_.end(); ++it)
00037   {
00038     if ((*it).second!=0)
00039     {
00040       (*it).second->uninitialize();
00041       delete (*it).second;
00042     }
00043     (*it).second = 0;
00044     mvl2_list_.erase(it);
00045   }
00046 }
00047 
00048 //=======================================================================
00049 
00050 vcl_string mvl2_image_format_plugin::is_a() const
00051 {
00052   return vcl_string("mvl2_image_format_plugin");
00053 }
00054 
00055 //=======================================================================
00056 
00057 bool mvl2_image_format_plugin::get_frame_number_and_filename(
00058     vcl_string& filename, int& frame_number, const vcl_string& path)
00059 {
00060   // find extension of the file
00061 
00062   vcl_string extension;
00063   extension=path.substr(path.length()-4);
00064   if (extension!=vcl_string(".seq") &&
00065       extension!=vcl_string(".avi"))
00066   {
00067     return false;
00068   }
00069 
00070   // find the frame number and file name
00071 
00072   vcl_size_t frame_sep_pos=path.rfind('_');
00073   if (frame_sep_pos<1)
00074   {
00075     return false;
00076   }
00077 
00078   vcl_string frame_string=path.substr(frame_sep_pos+1,path.length()-5-frame_sep_pos);
00079 
00080   frame_number=vcl_atoi(frame_string.c_str());
00081   filename=path.substr(0,frame_sep_pos)+extension;
00082 
00083   if (!vul_file::exists(filename))
00084   {
00085     return false;
00086   }
00087 
00088   return true;
00089 }
00090 
00091 //=======================================================================
00092 
00093 bool mvl2_image_format_plugin::load_the_image (
00094     vil_image_view_base_sptr& image,
00095     const vcl_string & path, const vcl_string & /*filetype*/,
00096     const vcl_string & colour)
00097 {
00098   int frame_number;
00099   vcl_string filename;
00100   vcl_string extension;
00101 
00102   if (!get_frame_number_and_filename(filename, frame_number, path))
00103   {
00104     return false;
00105   }
00106 
00107   extension=vul_file::extension(filename.c_str());
00108 
00109   // opens the file and initialise the video
00110 
00111   vcl_map<vcl_string,mvl2_video_reader*>::iterator mvl2_list_iterator;
00112   mvl2_list_iterator=mvl2_list_.find(filename);
00113   if (mvl2_list_iterator==mvl2_list_.end())
00114   {
00115     // filename not found in the cache
00116     mvl2_video_reader *video_reader;
00117     video_reader=0;
00118     if (extension==vcl_string(".avi"))
00119         video_reader=new mvl2_video_from_avi();
00120     if (extension==vcl_string(".seq"))
00121         video_reader=new mvl2_video_from_sequence();
00122     if (video_reader==0)
00123     {
00124       vcl_cout <<"WARNING : cannot allocate memory for video class.\n";
00125       return false;
00126     }
00127     if (!image)
00128     {
00129       return false;
00130     }
00131     if (width_==-1)
00132     {
00133       width_=image->ni();
00134     }
00135     if (height_==-1)
00136     {
00137       height_=image->nj();
00138     }
00139     if (!video_reader->initialize(width_,height_,colour,filename))
00140     {
00141       vcl_cout << "WARNING : unable to initialize avi file.\n";
00142       return false;
00143     }
00144     mvl2_list_[filename]=video_reader;
00145     mvl2_list_iterator=mvl2_list_.find(filename);
00146     if (mvl2_list_iterator==mvl2_list_.end())
00147     {
00148       return false;
00149     }
00150   }
00151 
00152   (*mvl2_list_iterator).second->seek(frame_number);
00153   vil_image_view<vxl_byte> the_image;
00154   bool ok=(*mvl2_list_iterator).second->get_frame(the_image);
00155 
00156   image->set_size((*mvl2_list_iterator).second->get_width(),
00157                   (*mvl2_list_iterator).second->get_height(),
00158                   the_image.nplanes());
00159   vil_copy_deep(the_image,(vil_image_view<vxl_byte>&)*image);
00160 
00161   return ok;
00162 }
00163 
00164 //=======================================================================
00165 
00166 bool mvl2_image_format_plugin::can_be_loaded(const vcl_string& filename)
00167 {
00168   vcl_string real_filename;
00169   int frame_number;
00170 
00171   return get_frame_number_and_filename(real_filename,frame_number,filename);
00172 }
00173