core/vidl_vil1/vidl_vil1_image_list_codec.cxx

Go to the documentation of this file.
00001 
00002 //:
00003 // \file
00004 
00005 #include "vidl_vil1_image_list_codec.h"
00006 
00007 #include <vcl_cassert.h>
00008 #include <vcl_iostream.h>
00009 
00010 #include <vul/vul_sprintf.h>
00011 
00012 #include <vidl_vil1/vidl_vil1_codec.h>
00013 #include <vidl_vil1/vidl_vil1_movie.h>
00014 #include <vidl_vil1/vidl_vil1_frame.h>
00015 #include <vil1/vil1_image.h>
00016 #include <vil1/vil1_load.h>
00017 #include <vil1/vil1_save.h>
00018 
00019 //=========================================================================
00020 //  Methods for vidl_vil1_image_list_codec.
00021 //_________________________________________________________________________
00022 
00023 
00024 vcl_string vidl_vil1_image_list_codec::default_initialization_image_type_ = "tiff";
00025 
00026 //------------------------------------------------------------------------
00027 // CONSTRUCTOR(S) AND DESTRUCTOR
00028 
00029 
00030 //: Constructor, from a list of images
00031 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec(vcl_list<vil1_image>& images)
00032 {
00033   // Set the image type to the default value
00034   default_image_type_ = default_initialization_image_type_;
00035 
00036   for (vcl_list<vil1_image>::iterator i=images.begin(); i!= images.end(); ++i)
00037     images_.push_back(*i);
00038 
00039   if (!init())
00040     vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00041 }
00042 
00043 //: Constructor, from a vector of images
00044 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec(vcl_vector<vil1_image>& images)
00045 {
00046   // Set the image type to the default value
00047   default_image_type_ = default_initialization_image_type_;
00048 
00049   for (vcl_vector<vil1_image>::iterator i=images.begin(); i!= images.end(); ++i)
00050     images_.push_back(*i);
00051 
00052   if (!init())
00053     vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00054 }
00055 
00056 //: Basic constructor. Should not be called unless we initialize the codec by some ways.
00057 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec()
00058 {
00059   // Set the image type to the default value
00060   default_image_type_ = default_initialization_image_type_;
00061 
00062   // Nothing to do, here
00063   // Caution, a call to this constructor
00064   // creates an instance of this class in bad shape
00065 }
00066 
00067 //: Initialize
00068 bool vidl_vil1_image_list_codec::init()
00069 {
00070   if (images_.empty())
00071     return false;
00072 
00073   //   unfinished !!!!! TODO
00074 
00075   set_number_frames(images_.size());
00076   vil1_image first = images_[0];
00077 
00078   // Comes from TargetJr, don't know the vxl equivalent
00079 //set_format(first->get_format());
00080 //set_image_class(first->get_image_class());
00081   set_bits_pixel(first.bits_per_component() * first.components());
00082   set_width(first.width());
00083   set_height(first.height());
00084 
00085   return true;
00086 }
00087 
00088 
00089 //: Get a section of pixels in function of the frame number, position and size.
00090 bool vidl_vil1_image_list_codec::get_section(int position, void* ib, int x0, int y0, int w, int h) const
00091 {
00092   return images_[position].get_section(ib, x0, y0, w, h);
00093 }
00094 
00095 //: Put a section of pixels in function of the frame number, position and size.
00096 int vidl_vil1_image_list_codec::put_section(int /*position*/, void* /*ib*/, int /*x0*/, int /*y0*/, int /*w*/, int /*h*/)
00097 {
00098   vcl_cerr << "vidl_vil1_image_list_codec::put_section not implemented\n";
00099   return 0;
00100 }
00101 
00102 //: Load from a file name.
00103 // fname should contain "%d", which will be replaced with 0, 1, 2, etc. in turn.
00104 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(vcl_string const& fname, char mode)
00105 {
00106   // will try and load as many images as possible starting with
00107   //   index 0 and stopping when we run out of images
00108   assert(mode == 'r');
00109 
00110   for (int i=0; true; i++)
00111   {
00112     vil1_image img = vil1_load(vul_sprintf(fname.c_str(), i).c_str());
00113 
00114     if (img)
00115       images_.push_back(img);
00116     else
00117       break;
00118   }
00119 
00120   if (!init())
00121   {
00122     vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00123     return NULL;
00124   }
00125 
00126   return this;
00127 }
00128 
00129 //: Load a 'movie' from a list of filenames, return a codec.
00130 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(const vcl_list<vcl_string> &fnames, char mode)
00131 {
00132   // Makes sure image loaders are registered
00133   //register_image_loaders();
00134   assert(mode == 'r');
00135 
00136   for (vcl_list<vcl_string>::const_iterator i = fnames.begin(); i!=fnames.end(); ++i)
00137   {
00138     vil1_image img =  vil1_load((*i).c_str());
00139     if (img)
00140       images_.push_back(img);
00141   }
00142 
00143   // Initialize the codec
00144   if (!init())
00145   {
00146     vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00147     return NULL;
00148   }
00149 
00150   // Every thing was all right,
00151   // return myself
00152   return this;
00153 }
00154 
00155 //: Load a 'movie' from a vector of filenames, return a codec.
00156 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(const vcl_vector<vcl_string> &fnames, char mode)
00157 {
00158   // Make sure image loaders are registered
00159   //register_image_loaders();
00160   assert(mode == 'r');
00161 
00162   for (vcl_vector<vcl_string>::const_iterator i = fnames.begin(); i!=fnames.end(); ++i)
00163   {
00164     vil1_image img =  vil1_load((*i).c_str());
00165     if (img)
00166       images_.push_back(img);
00167   }
00168 
00169   // Initialize the codec
00170   if (!init())
00171   {
00172     vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00173     return NULL;
00174   }
00175 
00176   // Every thing was all right,
00177   // return myself
00178   return this;
00179 }
00180 
00181 //: Supposed to check the validity of this codec for a special filename.
00182 // Not so well implemented for this codec.
00183 // This could check if the filename is a valid image type
00184 // by probing all the image types.
00185 bool vidl_vil1_image_list_codec::probe(vcl_string const&  /*fname*/)
00186 {
00187   return false;
00188 }
00189 
00190 //: Save the given video as a set of images of the default set type.
00191 bool vidl_vil1_image_list_codec::save(vidl_vil1_movie* movie, vcl_string const& fname)
00192 {
00193   if (default_image_type_ == "")
00194   {
00195     vcl_cerr << "No default image type defined to save the video as a list of images.\n";
00196     return false;
00197   }
00198 
00199   return save(movie, fname, default_image_type_);
00200 }
00201 
00202 //: Save the given video as a set of images of the type given.
00203 bool vidl_vil1_image_list_codec::save(
00204         vidl_vil1_movie* movie,
00205         vcl_string const& fname,
00206         vcl_string const& type
00207         )
00208 {
00209   // The value to be returned
00210   bool ret = true;
00211 
00212   // Create the extension for filenames
00213   vcl_string extension = type.substr(0, type.size()-5); // To get rid of "Image" string
00214 
00215   for (vidl_vil1_movie::frame_iterator pframe = movie->begin();
00216        pframe <= movie->last();
00217        ++pframe)
00218   {
00219     // Get the image from the frame
00220     vil1_image image = pframe->get_image();
00221 
00222     // Create a name for the current image to be saved
00223     vcl_string currentname = vul_sprintf("%s%05d.%s", fname.c_str(),
00224                                          pframe.current_frame_number(),
00225                                          extension.c_str());
00226 
00227     if (! vil1_save(image, currentname.c_str(), type.c_str()))
00228       ret = false;
00229   }
00230 
00231   return ret;
00232 }

Generated on Thu Nov 20 05:09:11 2008 for core/vidl_vil1 by  doxygen 1.5.1