contrib/mul/vil3d/vil3d_file_format.cxx
Go to the documentation of this file.
00001 // This is mul/vil3d/vil3d_file_format.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief Base for objects capable of reading/writing different image formats.
00008 // \author Tim Cootes - Manchester
00009 
00010 #include "vil3d_file_format.h"
00011 #include <vcl_vector.h>
00012 #include <vil/vil_open.h>
00013 #include <vil3d/file_formats/vil3d_analyze_format.h>
00014 #include <vil3d/file_formats/vil3d_gipl_format.h>
00015 #include <vil3d/file_formats/vil3d_slice_list.h>
00016 #include <vil3d/file_formats/vil3d_meta_image_format.h>
00017 
00018 #if 0 // commented out
00019 
00020 #include <vil3d/vil3d_header_data.h>
00021 
00022 //: Read header and image from named file if possible
00023 bool vil3d_file_format::read_file(vil3d_header_data_sptr& header,
00024                                   vil3d_image_view_base_sptr& image,
00025                                   const vcl_string& filename)
00026 {
00027   vil_stream *is = vil_open(filename.c_str(), "r");
00028   if (is) return read_stream(header,image,is);
00029 
00030   vcl_cerr << __FILE__ ": Failed to load [" << filename << "]\n";
00031   return false;
00032 }
00033 
00034     //: Write header and image to named file if possible
00035 bool vil3d_file_format::write_file(vil3d_header_data_sptr& header,
00036                                    vil3d_image_view_base_sptr& image,
00037                                    const vcl_string& filename)
00038 {
00039   vil_stream* os = vil_open(filename.c_str(), "w");
00040   if (!os->ok()) {
00041     vcl_cerr << __FILE__ ": Invalid stream for \"" << filename << "\"\n";
00042     return false;
00043   }
00044 
00045   return write_stream(header,image,os);
00046 }
00047 
00048 #endif // 0
00049 
00050 //: Store list of file_formats in this class to ensure tidy deletion.
00051 class vil3d_file_formats
00052 {
00053  public:
00054   vcl_vector<vil3d_file_format *> v;
00055   vil3d_file_formats()
00056   {
00057     v.push_back(new vil3d_analyze_format);
00058     v.push_back(new vil3d_gipl_format);
00059     v.push_back(new vil3d_slice_list_format);
00060     v.push_back(new vil3d_meta_image_format);
00061   }
00062   ~vil3d_file_formats()
00063   {
00064     for (unsigned i=0; i<v.size(); ++i)
00065       delete v[i];
00066 
00067     v.clear();
00068   }
00069 };
00070 
00071 static vil3d_file_formats formats_available;
00072 
00073 //: Add a format reader to current list of those available
00074 void vil3d_file_format::add_format(vil3d_file_format* new_format)
00075 {
00076   formats_available.v.push_back(new_format);
00077 }
00078 
00079 //: Number of formats available (number added by add_format()
00080 unsigned vil3d_file_format::n_formats()
00081 {
00082   return (unsigned)(formats_available.v.size());
00083 }
00084 
00085 //: Access to available format readers supplied by add_format
00086 const vil3d_file_format& vil3d_file_format::format(unsigned i)
00087 {
00088   return *formats_available.v[i];
00089 }
00090