core/vidl_vil1/vidl_vil1_io.cxx

Go to the documentation of this file.
00001 //:
00002 // \file
00003 
00004 #include "vidl_vil1_io.h"
00005 #include <vcl_compiler.h>
00006 #include <vidl_vil1/vidl_vil1_movie.h>
00007 #include <vidl_vil1/vidl_vil1_clip.h>
00008 #include <vidl_vil1/vidl_vil1_image_list_codec.h>
00009 
00010 #include <vul/vul_file.h>
00011 #include <vul/vul_sequence_filename_map.h>
00012 #include <vul/vul_file_iterator.h>
00013 
00014 #include <vcl_cassert.h>
00015 #include <vcl_iostream.h>
00016 #include <vcl_list.h>
00017 #include <vcl_vector.h>
00018 #include <vcl_string.h>
00019 #ifdef HAS_MPEG2
00020 # include <vidl_vil1/vidl_vil1_mpegcodec.h>
00021 void (* vidl_vil1_io::load_mpegcodec_callback)(vidl_vil1_codec*) = 0;
00022 #endif
00023 
00024 vcl_list<vidl_vil1_codec_sptr> vidl_vil1_io::supported_types_;
00025 
00026 static bool looks_like_a_file_list(char const* fname);
00027 static vidl_vil1_clip_sptr load_from_file_list(vcl_string const& fname);
00028 static vidl_vil1_clip_sptr load_from_directory(vcl_string const& fname)
00029 {
00030   vcl_list<vcl_string> filenames;
00031 
00032   vcl_string s(fname);
00033   s += "/*.*";
00034   for (vul_file_iterator fit = s;fit; ++fit) {
00035     // check to see if file is a directory.
00036     if (vul_file::is_directory(fit()))
00037       continue;
00038     filenames.push_back(fit());
00039   }
00040 
00041   // Call load_images and return the result
00042   return vidl_vil1_io::load_images(filenames, 'r');
00043 }
00044 
00045 //-----------------------------------------------------------------------------
00046 
00047 //: Load a movie, takes a file name and return the created movie.
00048 // A starting frame, ending frame and increment number are optionals
00049 vidl_vil1_movie_sptr vidl_vil1_io::load_movie(
00050         vcl_string const& fname,
00051         int start,
00052         int end,
00053         int increment,
00054         char mode
00055         )
00056 {
00057   vidl_vil1_clip_sptr clip = load_clip(fname, start, end, increment, mode);
00058 
00059   // Stop here if the clip was not created
00060   if (!clip)
00061     return 0;
00062 
00063   vidl_vil1_movie_sptr movie = new vidl_vil1_movie(clip);
00064 
00065   return movie;
00066 }
00067 
00068 //: Loads and creates movie from a list of image file names.
00069 // A starting frame, ending frame and increment number are optionals
00070 vidl_vil1_movie_sptr  vidl_vil1_io::load_movie(
00071         const vcl_list<vcl_string> &fnames,
00072         int start,
00073         int end,
00074         int increment,
00075         char mode
00076         )
00077 {
00078   vidl_vil1_clip_sptr clip = load_clip(fnames, start, end, increment, mode);
00079 
00080   // Stop here if the clip was not created
00081   if (!clip)
00082     return 0;
00083 
00084   vidl_vil1_movie_sptr movie = new vidl_vil1_movie(clip);
00085 
00086   return movie;
00087 }
00088 
00089 //: Loads and creates movie from a vector of image file names.
00090 // A starting frame, ending frame and increment number are optionals
00091 vidl_vil1_movie_sptr  vidl_vil1_io::load_movie(
00092         const vcl_vector<vcl_string> &fnames,
00093         int start,
00094         int end,
00095         int increment,
00096         char mode
00097         )
00098 {
00099   vidl_vil1_clip_sptr clip = load_clip(fnames, start, end, increment, mode);
00100 
00101   // Stop here if the clip was not created
00102   if (!clip)
00103     return 0;
00104 
00105   vidl_vil1_movie_sptr movie = new vidl_vil1_movie(clip);
00106 
00107   return movie;
00108 }
00109 
00110 //: Load a clip, takes a file name and return the created clip.
00111 // A starting frame, ending frame and increment number are optionals
00112 vidl_vil1_clip_sptr  vidl_vil1_io::load_clip(
00113         vcl_string const& fname,
00114         int start,
00115         int end,
00116         int increment,
00117         char mode
00118         )
00119 {
00120   // make sure that fname is sane
00121   if (fname == "") { return 0; }
00122 
00123   // test if fname is a directory
00124   if (vul_file::is_directory(fname))
00125     return load_from_directory(fname);
00126     //    return load_from_directory(fname);
00127   else if (looks_like_a_file_list(fname.c_str()))
00128     return load_from_file_list(fname);
00129 
00130   // The file is not a directory,
00131   // Let us try all the known video formats,
00132   // hoping to find the good one
00133   vcl_list<vidl_vil1_codec_sptr>::iterator i = supported_types_.begin();
00134   if (i == supported_types_.end())
00135     vcl_cerr << "vidl_vil1_io: warning: no codecs installed\n";
00136 
00137   while (i != supported_types_.end())
00138   {
00139     if ((*i)->probe(fname))
00140     {
00141       vidl_vil1_codec_sptr codec = (*i)->load(fname, mode);
00142       if (!codec)
00143         return 0;
00144 
00145 #ifdef HAS_MPEG2
00146       //this calls the dialog box necessary for initialization
00147       //of the mpeg codec.
00148       vidl_vil1_mpegcodec * vmp = (*i)->castto_vidl_vil1_mpegcodec();
00149       if (vmp) {
00150         assert (load_mpegcodec_callback);
00151         load_mpegcodec_callback(vmp);
00152       }
00153 #endif
00154 
00155       vidl_vil1_clip_sptr clip = new vidl_vil1_clip(codec, start, end, increment);
00156       vcl_cout << "vidl_vil1_io::load_move. just got a new clip.\n";
00157       return clip;
00158     }
00159 
00160     ++i;
00161   }
00162 
00163   // We did not find a codec corresponding
00164   // to this file name
00165   // Return error
00166   return 0;
00167 }
00168 
00169 //: Loads and creates clip from a list of image file names.
00170 // A starting frame, ending frame and increment number are optionals
00171 vidl_vil1_clip_sptr vidl_vil1_io::load_clip(
00172         const vcl_list<vcl_string> &fnames,
00173         int start,
00174         int end,
00175         int increment,
00176         char mode
00177        )
00178 {
00179   // Check if the input is correct
00180   if (fnames.empty())
00181     return 0;
00182 
00183   // If we have only one filename, we run the special function written for that
00184   if (fnames.size()==1)
00185     return load_clip((*fnames.begin()).c_str(), start, end, increment, mode);
00186 
00187   // We have severall files, so we suppose that the video is a set of images.
00188   return load_images(fnames, start, end, increment, mode);
00189 }
00190 
00191 //: Loads and creates clip from a list of image file names.
00192 // A starting frame, ending frame and increment number are optionals
00193 vidl_vil1_clip_sptr  vidl_vil1_io::load_clip(
00194         const vcl_vector<vcl_string> &fnames,
00195         int start,
00196         int end,
00197         int increment,
00198         char mode
00199        )
00200 {
00201   // Check if the input is correct
00202   if (fnames.empty())
00203     return 0;
00204 
00205   // If we have only one filename, we run the special function written for that
00206   if (fnames.size()==1)
00207     return load_clip((*fnames.begin()).c_str(), start, end, increment, mode);
00208 
00209   // We have severall files, so we suppose that the video is a set of images.
00210   return load_images(fnames, start, end, increment, mode);
00211 }
00212 
00213 //: load a list of images as a clip.
00214 // This function should not be called unless
00215 // you are sure you are dealing with images
00216 vidl_vil1_clip_sptr  vidl_vil1_io::load_images(
00217         const vcl_list<vcl_string> &fnames,
00218         int start,
00219         int end,
00220         int increment,
00221         char mode
00222        )
00223 {
00224   // This should be a video represented by a set of images
00225   vidl_vil1_image_list_codec_sptr ImCodec = new vidl_vil1_image_list_codec();
00226 
00227   vidl_vil1_codec_sptr codec = ImCodec->load(fnames, mode);
00228   if (!codec)
00229     return 0;
00230   vidl_vil1_clip_sptr clip = new vidl_vil1_clip(codec, start, end, increment);
00231   return clip;
00232 }
00233 
00234 //: load a vector of images as a clip.
00235 // This function should not be called unless
00236 // you are sure you are dealing with images
00237 vidl_vil1_clip_sptr  vidl_vil1_io::load_images(
00238         const vcl_vector<vcl_string> &fnames,
00239         int start,
00240         int end,
00241         int increment,
00242         char mode
00243        )
00244 {
00245   // This should be a video represented by a set of images
00246   vidl_vil1_image_list_codec_sptr ImCodec = new vidl_vil1_image_list_codec();
00247 
00248   vidl_vil1_codec_sptr codec = ImCodec->load(fnames, mode);
00249   if (!codec)
00250     return 0;
00251   vidl_vil1_clip_sptr clip = new vidl_vil1_clip(codec, start, end, increment);
00252   return clip;
00253 }
00254 
00255 //: Save a video into a file "fname" as type "type"
00256 bool vidl_vil1_io::save(vidl_vil1_movie* movie, vcl_string const& fname, vcl_string const& type)
00257 {
00258   // Go along the vcl_list of supported videoCODECs,
00259   // find the one of the type asked if it does exist.
00260   vcl_list<vidl_vil1_codec_sptr>::iterator i = supported_types_.begin();
00261 
00262   while ((i != supported_types_.end()) && (*i)->type() != type)
00263   {
00264 #ifdef DEBUG
00265     vcl_cout << "debug : " << (*i)->type() << " type : " << type << vcl_endl;
00266 #endif
00267     ++i;
00268   }
00269 
00270   // Check if the type asked really exists in the context
00271   // If it does not, Try the vcl_list of images mode.
00272   if (i==supported_types_.end())
00273     return save_images(movie, fname, type);//return false;
00274 
00275 
00276   // Try to save it
00277   if ((*i)->save(movie, fname))
00278     return true;
00279   else
00280     return false;
00281 }
00282 
00283 // This function should be removed and integrated in save()
00284 bool vidl_vil1_io::save_images(vidl_vil1_movie* movie, vcl_string const& fname,  vcl_string const& type)
00285 {
00286   vidl_vil1_image_list_codec codec;
00287 
00288   // Try to save and return success or failure
00289   return codec.save(movie, fname, type);
00290 }
00291 
00292 //: Return the list of the supported video coder/decoder types
00293 vcl_list<vcl_string> vidl_vil1_io::supported_types()
00294 {
00295   // Create the vcl_list with type() for all the codecs
00296   vcl_list<vcl_string> ret;
00297   for (vcl_list<vidl_vil1_codec_sptr>::iterator i=supported_types_.begin(); i!=supported_types_.end(); ++i)
00298     ret.push_back((*i)->type().c_str());
00299 
00300   // Return the vcl_list of type supported codecs
00301   return ret;
00302 }
00303 
00304 //: register a new coder
00305 void vidl_vil1_io::register_codec(vidl_vil1_codec* codec)
00306 {
00307   supported_types_.push_back(codec);
00308 }
00309 
00310 //: Destroy codecs.
00311 // Must call this before the MPEG library is deleted, i.e. on exit.
00312 void vidl_vil1_io::close()
00313 {
00314   for (vcl_list<vidl_vil1_codec_sptr>::iterator i=supported_types_.begin(); i!=supported_types_.end(); ++i)
00315     (*i)->close();
00316   supported_types_.erase(supported_types_.begin(), supported_types_.end());
00317 }
00318 
00319 static bool looks_like_a_file_list(char const* fname)
00320 {
00321   while (*fname) {
00322     if (*fname == '%' || *fname == '#')
00323       return true;
00324     ++fname;
00325   }
00326   return false;
00327 }
00328 
00329 static vidl_vil1_clip_sptr load_from_file_list(vcl_string const& fname)
00330 {
00331   // Declare the vcl_list of image filenames
00332   vcl_list<vcl_string> filenames;
00333 
00334   vul_sequence_filename_map map(fname);
00335 
00336   for (int i = 0;i < map.get_nviews(); ++i)
00337   {
00338     vcl_string fullpath = map.image_name(i);
00339     // check to see if file is a directory.
00340     if (vul_file::is_directory(fullpath))
00341       continue;
00342     filenames.push_back(fullpath);
00343   }
00344 
00345   // Call load_images and return the result
00346   return vidl_vil1_io::load_images(filenames, 'r');
00347 }

Generated on Tue Dec 2 05:09:13 2008 for core/vidl_vil1 by  doxygen 1.5.1