1 // This is mul/vil3d/vil3d_file_format.cxx
2 //:
3 // \file
4 // \brief Base for objects capable of reading/writing different image formats.
5 // \author Tim Cootes - Manchester
6 
7 #include <iostream>
8 #include <vector>
9 #include "vil3d_file_format.h"
10 #ifdef _MSC_VER
11 #  include "vcl_msvc_warnings.h"
12 #endif
13 #include "vil/vil_open.h"
14 #include <vil3d/file_formats/vil3d_analyze_format.h>
15 #include <vil3d/file_formats/vil3d_gipl_format.h>
16 #include <vil3d/file_formats/vil3d_slice_list.h>
17 #include <vil3d/file_formats/vil3d_meta_image_format.h>
18 
19 #if 0 // commented out
20 
21 #include <vil3d/vil3d_header_data.h>
22 
23 //: Read header and image from named file if possible
24 bool vil3d_file_format::read_file(vil3d_header_data_sptr& header,
25                                   vil3d_image_view_base_sptr& image,
26                                   const std::string& filename)
27 {
28   vil_stream *is = vil_open(filename.c_str(), "r");
29   if (is) return read_stream(header,image,is);
30 
31   std::cerr << __FILE__ ": Failed to load [" << filename << "]\n";
32   return false;
33 }
34 
35     //: Write header and image to named file if possible
36 bool vil3d_file_format::write_file(vil3d_header_data_sptr& header,
37                                    vil3d_image_view_base_sptr& image,
38                                    const std::string& filename)
39 {
40   vil_stream* os = vil_open(filename.c_str(), "w");
41   if (!os->ok()) {
42     std::cerr << __FILE__ ": Invalid stream for \"" << filename << "\"\n";
43     return false;
44   }
45 
46   return write_stream(header,image,os);
47 }
48 
49 #endif // 0
50 
51 //: Store list of file_formats in this class to ensure tidy deletion.
52 class vil3d_file_formats
53 {
54  public:
55   std::vector<vil3d_file_format *> v;
vil3d_file_formats()56   vil3d_file_formats()
57   {
58     v.push_back(new vil3d_analyze_format);
59     v.push_back(new vil3d_gipl_format);
60     v.push_back(new vil3d_slice_list_format);
61     v.push_back(new vil3d_meta_image_format);
62   }
~vil3d_file_formats()63   ~vil3d_file_formats()
64   {
65     for (auto & i : v)
66       delete i;
67 
68     v.clear();
69   }
70 };
71 
72 static vil3d_file_formats formats_available;
73 
74 //: Add a format reader to current list of those available
add_format(vil3d_file_format * new_format)75 void vil3d_file_format::add_format(vil3d_file_format* new_format)
76 {
77   formats_available.v.push_back(new_format);
78 }
79 
80 //: Number of formats available (number added by add_format()
n_formats()81 unsigned vil3d_file_format::n_formats()
82 {
83   return (unsigned)(formats_available.v.size());
84 }
85 
86 //: Access to available format readers supplied by add_format
format(unsigned i)87 const vil3d_file_format& vil3d_file_format::format(unsigned i)
88 {
89   return *formats_available.v[i];
90 }
91