vil_load.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_load.cxx
2 //:
3 // \file
4 
5 #include <iostream>
6 #include "vil_load.h"
7 #ifdef _MSC_VER
8 # include <vcl_msvc_warnings.h>
9 #endif
10 #include <vil/vil_open.h>
11 #include <vil/vil_new.h>
12 #include <vil/vil_file_format.h>
13 #include <vil/vil_stream.h>
14 #include <vil/vil_image_resource.h>
16 #include <vil/vil_image_view.h>
17 #include <vil/vil_exception.h>
18 
20  bool verbose)
21 {
22  std::list<vil_file_format*>& l = vil_file_format::all();
23  for (auto & p : l) {
24 #if 0 // debugging
25  std::cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n";
26 #endif
27  is->seek(0);
28  vil_image_resource_sptr im = p->make_input_image(is);
29  if (im)
30  return im;
31  }
32 
33  // failed.
34  if (verbose) {
35  std::cerr << __FILE__ ": Unable to load image;\ntried";
36  for (auto & p : l)
37  // 'flush' in case of segfault next time through loop. Else, we
38  // will not see those printed tags still in the stream buffer.
39  std::cerr << " \'" << p->tag() << "\'" << std::flush;
40  std::cerr << std::endl;
41  }
42 
43  return nullptr;
44 }
45 
47  bool verbose)
48 {
49  vil_smart_ptr<vil_stream> is = vil_open(filename, "r");
50  vil_image_resource_sptr isp = nullptr;
51  if (is)
52  {
53  try
54  {
55  isp = vil_load_image_resource_raw(is.as_pointer(), verbose);
56  }
57  catch (const vil_exception_corrupt_image_file &e)
58  {
60  }
61  }
62 
63  if (!isp && verbose)
64  std::cerr << __FILE__ ": Failed to load [" << filename << "]\n";
65  return isp;
66 }
67 
69  bool verbose)
70 {
72  if (!im)
73  im=vil_load_image_resource_raw(filename, verbose);
74  if (!im && verbose)
75  std::cerr << __FILE__ ": Failed to load [" << filename << "]\n";
76  return im;
77 }
78 
79 
81 {
82  vil_image_resource_plugin im_resource_plugin;
83  if (im_resource_plugin.can_be_loaded(filename))
84  {
87  vil_image_view_base_sptr im_view(img);
88  if (im_resource_plugin.load_the_image(im_view,filename))
89  {
90  im=vil_new_image_resource(im_view->ni(),im_view->nj(),
91  im_view->nplanes(),im_view->pixel_format());
92  if (im->put_view((const vil_image_view_base&)*im_view,0,0))
93  return im;
94  }
95  }
96  return vil_image_resource_sptr(nullptr);
97 }
98 
100 vil_load_pyramid_resource(char const* directory_or_file, bool verbose)
101 {
102  std::list<vil_file_format*>& l = vil_file_format::all();
103  for (auto & p : l) {
104 #if 0 // debugging
105  std::cerr << __FILE__ " : trying \'" << (*p)->tag() << "\'\n";
106 
107 
108  std::cerr << "make_input_pyramid_image(" << directory_or_file << ")\n";
109 #endif
111  p->make_input_pyramid_image(directory_or_file);
112  if (pir)
113  return pir;
114  }
115  // failed.
116  if (verbose) {
117  std::cerr << __FILE__ ": Unable to load pyramid image;\ntried";
118  for (auto & p : l)
119  // 'flush' in case of segfault next time through loop. Else, we
120  // will not see those printed tags still in the stream buffer.
121  std::cerr << " \'" << p->tag() << "\'" << std::flush;
122  std::cerr << std::endl;
123  }
124  return nullptr;
125 }
126 
127 //: Convenience function for loading an image into an image view.
128 vil_image_view_base_sptr vil_load(const char *file, bool verbose)
129 {
130  vil_image_resource_sptr data = vil_load_image_resource(file, verbose);
131  if (!data) return nullptr;
132  return data -> get_view();
133 }
134 
135 
136 #if defined(_WIN32) && VXL_USE_WIN_WCHAR_T
137 // --------------------------------------------------------------------------------
138 // Windows' wchar_t overloading version
139 //
140 //
141 vil_image_resource_sptr vil_load_image_resource_raw(wchar_t const* filename, bool verbose)
142 {
143  vil_smart_ptr<vil_stream> is = vil_open(filename, "r");
144  vil_image_resource_sptr isp = 0;
145  if (is)
146  isp = vil_load_image_resource_raw(is.as_pointer(), verbose);
147  if (!isp && verbose)
148  std::wcerr << __FILE__ << L": Failed to load [" << filename << L"]\n";
149  return isp;
150 }
151 
152 vil_image_resource_sptr vil_load_image_resource(wchar_t const* filename, bool verbose)
153 {
154  // do not support image resource plugin for the time being
155  //vil_image_resource_sptr im = vil_load_image_resource_plugin(filename);
156  //if (!im)
157  // im=vil_load_image_resource_raw(filename);
159  return im;
160 }
161 
162 //: Convenience function for loading an image into an image view.
163 vil_image_view_base_sptr vil_load(const wchar_t *file, bool verbose)
164 {
165  vil_image_resource_sptr data = vil_load_image_resource(file, verbose);
166  if (!data) return 0;
167  return data -> get_view();
168 }
169 
170 #endif //defined(_WIN32) && VXL_USE_WIN_WCHAR_T
Stream interface for VIL image loaders.
An abstract base class of smart pointers to actual image data in memory.
vil_image_resource_sptr vil_load_image_resource_raw(vil_stream *, bool verbose=true)
Load from a stream.
Definition: vil_load.cxx:19
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
vil_image_resource_sptr vil_new_image_resource(unsigned ni, unsigned nj, unsigned nplanes, vil_pixel_format format)
Make a new image of given format.
Definition: vil_new.cxx:32
vil_image_resource_sptr vil_load_image_resource_plugin(char const *)
Load from a filename with a plugin.
Definition: vil_load.cxx:80
virtual void seek(vil_streampos position)=0
Goto file pointer.
vil_smart_ptr< vil_image_resource > vil_image_resource_sptr
Definition: vil_fwd.h:16
Exceptions thrown by vil, and a mechanism for turning them off.
Interface for loading new image formats.
virtual bool can_be_loaded(const std::string &filename)
Check whether a filename is a potential candidate for loading and if it is available.
read an image from a file
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
static std::list< vil_file_format * > & all()
Indicates unexpected problems image file's data.
virtual bool load_the_image(vil_image_view_base_sptr &image, const std::string &path)
Attempt to load image from named file.
A base class reference-counting view of some image data.
Make a new image.
make a vil_stream from a filename, an URL, etc.
T * as_pointer() const
These methods all return the raw/dumb pointer.
vil_image_resource_sptr vil_load_image_resource(char const *filename, bool verbose=true)
Load an image resource object from a file.
Definition: vil_load.cxx:68
Representation of a generic image source or destination.
Base class for image formats.
vil_stream * vil_open(char const *what, char const *how="r")
make a vil_stream from a filename, an URL, etc.
Definition: vil_open.cxx:18
A base class for a plugin for vil images loading.
vil_image_view_base_sptr vil_load(const char *, bool verbose=true)
Convenience function for loading an image into an image view.
Definition: vil_load.cxx:128
vil_pyramid_image_resource_sptr vil_load_pyramid_resource(char const *directory_or_file, bool verbose=true)
Load a pyramid image resource object from a file or directory.
Definition: vil_load.cxx:100