00001
00002
00003
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
00021
00022
00023
00024 vcl_string vidl_vil1_image_list_codec::default_initialization_image_type_ = "tiff";
00025
00026
00027
00028
00029
00030
00031 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec(vcl_list<vil1_image>& images)
00032 {
00033
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
00044 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec(vcl_vector<vil1_image>& images)
00045 {
00046
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
00057 vidl_vil1_image_list_codec::vidl_vil1_image_list_codec()
00058 {
00059
00060 default_image_type_ = default_initialization_image_type_;
00061
00062
00063
00064
00065 }
00066
00067
00068 bool vidl_vil1_image_list_codec::init()
00069 {
00070 if (images_.empty())
00071 return false;
00072
00073
00074
00075 set_number_frames(images_.size());
00076 vil1_image first = images_[0];
00077
00078
00079
00080
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
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
00096 int vidl_vil1_image_list_codec::put_section(int , void* , int , int , int , int )
00097 {
00098 vcl_cerr << "vidl_vil1_image_list_codec::put_section not implemented\n";
00099 return 0;
00100 }
00101
00102
00103
00104 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(vcl_string const& fname, char mode)
00105 {
00106
00107
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
00130 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(const vcl_list<vcl_string> &fnames, char mode)
00131 {
00132
00133
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
00144 if (!init())
00145 {
00146 vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00147 return NULL;
00148 }
00149
00150
00151
00152 return this;
00153 }
00154
00155
00156 vidl_vil1_codec_sptr vidl_vil1_image_list_codec::load(const vcl_vector<vcl_string> &fnames, char mode)
00157 {
00158
00159
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
00170 if (!init())
00171 {
00172 vcl_cerr << "Failed to initialize the ImageList Codec.\n";
00173 return NULL;
00174 }
00175
00176
00177
00178 return this;
00179 }
00180
00181
00182
00183
00184
00185 bool vidl_vil1_image_list_codec::probe(vcl_string const& )
00186 {
00187 return false;
00188 }
00189
00190
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
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
00210 bool ret = true;
00211
00212
00213 vcl_string extension = type.substr(0, type.size()-5);
00214
00215 for (vidl_vil1_movie::frame_iterator pframe = movie->begin();
00216 pframe <= movie->last();
00217 ++pframe)
00218 {
00219
00220 vil1_image image = pframe->get_image();
00221
00222
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 }