00001
00002
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
00036 if (vul_file::is_directory(fit()))
00037 continue;
00038 filenames.push_back(fit());
00039 }
00040
00041
00042 return vidl_vil1_io::load_images(filenames, 'r');
00043 }
00044
00045
00046
00047
00048
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
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
00069
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
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
00090
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
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
00111
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
00121 if (fname == "") { return 0; }
00122
00123
00124 if (vul_file::is_directory(fname))
00125 return load_from_directory(fname);
00126
00127 else if (looks_like_a_file_list(fname.c_str()))
00128 return load_from_file_list(fname);
00129
00130
00131
00132
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
00147
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
00164
00165
00166 return 0;
00167 }
00168
00169
00170
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
00180 if (fnames.empty())
00181 return 0;
00182
00183
00184 if (fnames.size()==1)
00185 return load_clip((*fnames.begin()).c_str(), start, end, increment, mode);
00186
00187
00188 return load_images(fnames, start, end, increment, mode);
00189 }
00190
00191
00192
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
00202 if (fnames.empty())
00203 return 0;
00204
00205
00206 if (fnames.size()==1)
00207 return load_clip((*fnames.begin()).c_str(), start, end, increment, mode);
00208
00209
00210 return load_images(fnames, start, end, increment, mode);
00211 }
00212
00213
00214
00215
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
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
00235
00236
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
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
00256 bool vidl_vil1_io::save(vidl_vil1_movie* movie, vcl_string const& fname, vcl_string const& type)
00257 {
00258
00259
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
00271
00272 if (i==supported_types_.end())
00273 return save_images(movie, fname, type);
00274
00275
00276
00277 if ((*i)->save(movie, fname))
00278 return true;
00279 else
00280 return false;
00281 }
00282
00283
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
00289 return codec.save(movie, fname, type);
00290 }
00291
00292
00293 vcl_list<vcl_string> vidl_vil1_io::supported_types()
00294 {
00295
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
00301 return ret;
00302 }
00303
00304
00305 void vidl_vil1_io::register_codec(vidl_vil1_codec* codec)
00306 {
00307 supported_types_.push_back(codec);
00308 }
00309
00310
00311
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
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
00340 if (vul_file::is_directory(fullpath))
00341 continue;
00342 filenames.push_back(fullpath);
00343 }
00344
00345
00346 return vidl_vil1_io::load_images(filenames, 'r');
00347 }