core/vidl/vidl_image_list_ostream.cxx
Go to the documentation of this file.
00001 // This is core/vidl/vidl_image_list_ostream.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Matt Leotta
00008 // \date   19 Dec 2005
00009 //
00010 //-----------------------------------------------------------------------------
00011 
00012 #include "vidl_image_list_ostream.h"
00013 #include "vidl_convert.h"
00014 #include <vul/vul_file.h>
00015 #include <vul/vul_sprintf.h>
00016 #include <vil/vil_file_format.h>
00017 #include <vil/vil_image_view.h>
00018 #include <vil/vil_save.h>
00019 
00020 //------------------------------------------------------------------------------
00021 
00022 
00023 //: Constructor
00024 vidl_image_list_ostream::
00025 vidl_image_list_ostream()
00026   : index_(0),
00027     dir_(),
00028     name_format_(),
00029     file_format_()
00030 {
00031 }
00032 
00033 
00034 //: Constructor - opens a stream
00035 vidl_image_list_ostream::
00036 vidl_image_list_ostream(const vcl_string& directory,
00037                         const vcl_string& name_format,
00038                         const vcl_string& file_format,
00039                         const unsigned int init_index)
00040 {
00041   open(directory, name_format, file_format, init_index);
00042 }
00043 
00044 
00045 //: Open the stream
00046 bool
00047 vidl_image_list_ostream::
00048 open(const vcl_string& directory,
00049      const vcl_string& name_format,
00050      const vcl_string& file_format,
00051      const unsigned int init_index)
00052 {
00053   if (!vul_file::is_directory(directory)) {
00054     close();
00055     vcl_cerr << __FILE__ ": Directory does not exist\n   "<<directory<<vcl_endl;
00056     return false;
00057   }
00058 
00059   bool valid_file_format = false;
00060   for (vil_file_format** p = vil_file_format::all(); *p; ++p) {
00061     if (file_format == (*p)->tag()) {
00062       valid_file_format = true;
00063       break;
00064     }
00065   }
00066 
00067   if (!valid_file_format) {
00068     close();
00069     vcl_cerr << __FILE__ ": File format \'"<<file_format<<"\' not supported\n"
00070              << "   valid formats are: ";
00071     for (vil_file_format** p = vil_file_format::all(); *p; ++p)
00072       vcl_cerr << " \'" << (*p)->tag() << "\' " << vcl_flush;
00073     vcl_cerr << vcl_endl;
00074     return false;
00075   }
00076 
00077   dir_ = directory;
00078   name_format_ = name_format;
00079   file_format_ = file_format;
00080   index_ = init_index;
00081   return true;
00082 }
00083 
00084 
00085 //: Close the stream
00086 void
00087 vidl_image_list_ostream::
00088 close()
00089 {
00090   dir_ = "";
00091   name_format_ = "";
00092   file_format_ = "";
00093   index_ = 0;
00094 }
00095 
00096 
00097 //: Return true if the stream is open for writing
00098 bool
00099 vidl_image_list_ostream::
00100 is_open() const
00101 {
00102   return file_format_ != "";
00103 }
00104 
00105 
00106 //: Return the next file name to be written to
00107 vcl_string
00108 vidl_image_list_ostream::
00109 next_file_name() const
00110 {
00111   return dir_ + '/' +
00112          vul_sprintf(name_format_.c_str(),index_) +
00113          '.' + file_format_;
00114 }
00115 
00116 
00117 //: Write and image to the stream
00118 // \retval false if the image could not be written
00119 bool
00120 vidl_image_list_ostream::
00121 write_frame(const vidl_frame_sptr& frame)
00122 {
00123   vcl_string file_name = next_file_name();
00124   ++index_;
00125   if (!frame)
00126     return false;
00127   vil_image_view_base_sptr v = vidl_convert_wrap_in_view(*frame);
00128   if (!v){
00129     vil_image_view<vxl_byte> image;
00130     vidl_convert_to_view(*frame,image,VIDL_PIXEL_COLOR_RGB);
00131     return vil_save(image,file_name.c_str(),file_format_.c_str()); 
00132   }
00133 
00134   return vil_save(*v,file_name.c_str(),file_format_.c_str());                
00135 }
00136