vil_new.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_new.cxx
2 //:
3 // \file
4 // \author Andrew W. Fitzgibbon, Oxford RRG
5 // \date 16 Feb 00
6 
7 #include <cstring>
8 #include "vil_new.h"
9 
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 #include <cassert>
14 #include <vil/vil_save.h>
15 #include <vil/vil_file_format.h>
16 #include <vil/vil_stream_fstream.h>
17 #include <vil/vil_image_resource.h>
18 #include <vil/vil_memory_image.h>
23 // The first two functions really should be upgraded to create an image in
24 // a temporary file on disk if the sizes are large. - TODO
25 
26 //: Make a new image of given format.
27 // If the format is not scalar, the number of planes must be 1. When you create
28 // a multi-component image in this way, the vil_image_resource API will treat
29 // it as a scalar pixel image with multiple planes. (This doesn't affect the
30 // underlying data storage.)
31 // \relatesalso vil_image_resource
32 vil_image_resource_sptr vil_new_image_resource(unsigned ni, unsigned nj, unsigned nplanes,
33  vil_pixel_format format)
34 {
35  assert(nplanes == 1 || vil_pixel_format_num_components(format) == 1);
36 
37  return new vil_memory_image(ni, nj, nplanes, format);
38 }
39 
40 
41 //: Make a new image of given format with interleaved planes.
42 // The format must be scalar.
43 // \relatesalso vil_image_resource
45  unsigned nplanes,
46  vil_pixel_format format)
47 {
48  assert(vil_pixel_format_num_components(format) == 1);
49 
50  return new vil_memory_image(ni, nj, 1, format, nplanes);
51 }
52 
53 
54 //: Make a new image resource that is a wrapper on an existing view's data.
55 // \note The output will be a shallow copy of the input, so changing the pixel values
56 // of one may change the pixel value of the other. Thanks to the magic of smart pointers,
57 // the output will remain valid even if you destroy the input. When you wrap
58 // a multi-component image in this way, the vil_image_resource API will treat
59 // it as a scalar pixel image with multiple planes. (This doesn't affect the
60 // underlying data storage.)
61 // \relatesalso vil_image_resource
63 {
64  return new vil_memory_image(view);
65 }
66 
67 //: Make a new image, similar format to the prototype.
68 // \relatesalso vil_image_resource
69 vil_image_resource_sptr vil_new_image_resource(unsigned ni, unsigned nj, vil_image_resource_sptr const& prototype)
70 {
71  return vil_new_image_resource(ni, nj, prototype->nplanes(),
72  prototype->pixel_format());
73 }
74 
75 //: Make a new image.
76 // \relatesalso vil_image_resource
78  unsigned ni, unsigned nj,
79  unsigned nplanes,
80  vil_pixel_format format,
81  char const* file_format)
82 {
83  if (!file_format) // avoid segfault in strcmp()
84  file_format = "pnm";
85 
86  vil_image_resource_sptr outimage = nullptr;
87  std::list<vil_file_format*>& l = vil_file_format::all();
88  for (auto fmt : l)
89  {
90  if (std::strcmp(fmt->tag(), file_format) == 0) {
91  outimage = fmt->make_output_image(os, ni, nj, nplanes, format);
92  if (!outimage)
93  std::cerr << "vil_new: Cannot new to type [" << file_format << "]\n";
94  return outimage;
95  }
96  }
97 
98  std::cerr << "vil_new: Unknown file type [" << file_format << "]\n";
99  return nullptr;
100 }
101 
102 //: Make a new vil_image_resource, writing to file "filename", size ni x nj, copying pixel format etc from "prototype".
103 // \relatesalso vil_image_resource
105  unsigned ni, unsigned nj,
106  vil_image_resource_sptr const& prototype,
107  char const* file_format)
108 {
109 #ifdef VIL_USE_FSTREAM64
110  vil_stream_fstream64* os = new vil_stream_fstream64(filename, "w");
111 #else //VIL_USE_FSTREAM64
112  auto* os = new vil_stream_fstream(filename, "w");
113 #endif //VIL_USE_FSTREAM64
114  return vil_new_image_resource(os,
115  ni, nj,
116  prototype->nplanes(),
117  prototype->pixel_format(),
118  file_format ? file_format : prototype->file_format());
119 }
120 
121 //: Make a new image.
122 // \relatesalso vil_image_resource
124  unsigned ni, unsigned nj,
125  unsigned nplanes,
126  vil_pixel_format format,
127  char const* file_format)
128 {
129 #ifdef VIL_USE_FSTREAM64
130  vil_stream_fstream64* os = new vil_stream_fstream64(filename, "w");
131 #else //VIL_USE_FSTREAM64
132  auto* os = new vil_stream_fstream(filename, "w");
133 #endif //VIL_USE_FSTREAM64
134 
135  if (!file_format || !*file_format)
136  file_format = vil_save_guess_file_format(filename);
137  return vil_new_image_resource(os, ni, nj, nplanes, format, file_format);
138 }
139 
140 
141 //: Make a new vil_image_resource, writing to stream "os", size ni x nj, copying pixel format etc from "prototype".
142 // \relatesalso vil_image_resource
144  unsigned ni, unsigned nj,
145  vil_image_resource_sptr const& prototype,
146  char const* file_format)
147 {
148  return vil_new_image_resource(os,
149  prototype->nplanes(),
150  ni, nj,
151  prototype->pixel_format(),
152  file_format ? file_format : prototype->file_format());
153 }
154 
156 vil_new_blocked_image_resource(vil_stream* os, unsigned ni, unsigned nj,
157  unsigned nplanes, vil_pixel_format format,
158  unsigned size_block_i, unsigned size_block_j,
159  char const* file_format)
160 {
161  if (!file_format) // avoid segfault in strcmp()
162  file_format = "pnm";
163 
164  vil_blocked_image_resource_sptr outimage = nullptr;
165  std::list<vil_file_format*>& l = vil_file_format::all();
166  for (auto fmt : l)
167  {
168  if (std::strcmp(fmt->tag(), file_format) == 0) {
169  outimage = fmt->make_blocked_output_image(os, ni, nj, nplanes,
170  size_block_i, size_block_j, format);
171  if (!outimage)
172  std::cerr << "vil_new: Cannot new a blocked resource to type [" << file_format << "]\n";
173  return outimage;
174  }
175  }
176 
177  std::cerr << "vil_new: Unknown file type [" << file_format << "]\n";
178  return nullptr;
179 }
180 
182 vil_new_blocked_image_resource(char const* filename, unsigned ni, unsigned nj,
183  unsigned nplanes, vil_pixel_format format,
184  unsigned size_block_i, unsigned size_block_j,
185  char const* file_format)
186 {
187 #ifdef VIL_USE_FSTREAM64
188  vil_stream_fstream64* os = new vil_stream_fstream64(filename, "w");
189 #else //VIL_USE_FSTREAM64
190  auto* os = new vil_stream_fstream(filename, "w");
191 #endif //VIL_USE_FSTREAM64
192  return vil_new_blocked_image_resource(os, ni, nj, nplanes, format,
193  size_block_i, size_block_j,
194  file_format);
195 }
196 
199  unsigned size_block_i, unsigned size_block_j)
200 {
201  return new vil_blocked_image_facade(src, size_block_i, size_block_j);
202 }
203 
204 
207  const unsigned cache_size)
208 {
209  return new vil_cached_image_resource(bir, cache_size);
210 }
211 
213 vil_new_pyramid_image_resource(char const* file_or_directory,
214  char const* file_format)
215 {
216  if (!file_format) // avoid segfault in strcmp()
217  file_format = "tiff";
218  vil_pyramid_image_resource_sptr outimage = nullptr;
219  std::list<vil_file_format*>& l = vil_file_format::all();
220  for (auto fmt : l)
221  {
222  if (std::strcmp(fmt->tag(), file_format) == 0) {
223  outimage = fmt->make_pyramid_output_image(file_or_directory);
224  if (!outimage)
225  std::cerr << "vil_new: Cannot new a pyramid resource to type [" << file_format << "]\n";
226  return outimage;
227  }
228  }
229  std::cerr << "vil_new: Unknown file type [" << file_format << "]\n";
230  return nullptr;
231 }
232 
234  vil_new_pyramid_image_from_base(char const* filename,
235  vil_image_resource_sptr const& base_image,
236  unsigned nlevels,
237  char const* file_format,
238  char const* temp_dir)
239 {
240  if (!file_format) // avoid segfault in strcmp()
241  file_format = "tiff";
242  vil_pyramid_image_resource_sptr outimage = nullptr;
243  std::list<vil_file_format*>& l = vil_file_format::all();
244  for (auto fmt : l)
245  {
246  if (std::strcmp(fmt->tag(), file_format) == 0) {
247  outimage = fmt->make_pyramid_image_from_base(filename,
248  base_image,
249  nlevels,
250  temp_dir);
251  if (!outimage)
252  std::cerr << "vil_new: Cannot new a pyramid resource to type [" << file_format << "]\n";
253  return outimage;
254  }
255  }
256  std::cerr << "vil_new: Unknown file type [" << file_format << "]\n";
257  return nullptr;
258 }
259 
260 //for now there is only one directory based pyramid format
263  vil_image_resource_sptr const& base_image,
264  unsigned nlevels,
265  bool copy_base,
266  char const* level_file_format,
267  char const* filename)
268 {
270  return vpilf.make_pyramid_image_from_base(directory, base_image, nlevels,
271  copy_base, level_file_format,
272  filename);
273 }
274 
275 //: Create a shallow copy of an image and wrap it in a vil_image_view_base_sptr
276 // \note vil_image_view_base_sptr almost certainly doesn't behave as
277 // you would expect, and this function should really only be used by experts.
279 {
282  {
283 #ifndef DOXYGEN_SHOULD_SKIP_THIS
284 #define macro( F , T ) \
285  case F: { \
286  dest = new vil_image_view<T>(src); \
287  break; }
288  macro(VIL_PIXEL_FORMAT_BYTE, vxl_byte )
289  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
290 #if VXL_HAS_INT_64
291  macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
292  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
293 #endif
294  macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
295  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
296  macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
297  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
298  macro(VIL_PIXEL_FORMAT_FLOAT , float )
299  macro(VIL_PIXEL_FORMAT_DOUBLE , double )
301 #undef macro
302 #endif // DOXYGEN_SHOULD_SKIP_THIS
303  default: /* do nothing */;
304  }
305  return dest;
306 }
307 
308 #if defined(_WIN32) && VXL_USE_WIN_WCHAR_T
309 #include <windows.h>
310 
311 //: Make a new image.
312 // \relatesalso vil_image_resource
314  unsigned ni, unsigned nj,
315  unsigned nplanes,
316  vil_pixel_format format,
317  wchar_t const* file_format)
318 {
319  if (!file_format) // avoid segfault in strcmp()
320  file_format = L"pnm";
321 
322  constexpr unsigned int size = 200;
323  char fmt_buffer[size]; // should be enough
324  BOOL useless;
325  // ret indicates the number of characters successfully converted
326  const int ret = WideCharToMultiByte(CP_ACP, 0, file_format, int(wcslen(file_format)), fmt_buffer, size, 0, &useless );
327  fmt_buffer[ret] = '\0';
328  if (!ret) return 0;
329 
330  return vil_new_image_resource(os, ni, nj, nplanes, format, fmt_buffer);
331 }
332 
333 //: Make a new vil_image_resource, writing to file "filename", size ni x nj, copying pixel format etc from "prototype".
334 // \relatesalso vil_image_resource
335 vil_image_resource_sptr vil_new_image_resource(wchar_t const* filename,
336  unsigned ni, unsigned nj,
337  vil_image_resource_sptr const& prototype,
338  wchar_t const* file_format)
339 {
340 #ifdef VIL_USE_FSTREAM64
341  vil_stream_fstream64* os = new vil_stream_fstream64(filename, "w");
342 #else //VIL_USE_FSTREAM64
343  vil_stream_fstream* os = new vil_stream_fstream(filename, "w");
344 #endif //VIL_USE_FSTREAM64
345 
346  constexpr unsigned int size = 200; // should be enough
347  wchar_t tag_buffer[size];
348  if ( !file_format )
349  {
350  char const* tag = prototype->file_format();
351  const int ret = MultiByteToWideChar(CP_ACP, 0, tag, std::strlen(tag), tag_buffer, size);
352  assert(ret);
353  file_format = tag_buffer; // use the file format of the given resource
354  }
355 
356  return vil_new_image_resource(os,
357  ni, nj,
358  prototype->nplanes(),
359  prototype->pixel_format(),
360  file_format );
361 }
362 
363 //: Make a new image.
364 // \relatesalso vil_image_resource
365 vil_image_resource_sptr vil_new_image_resource(wchar_t const* filename,
366  unsigned ni, unsigned nj,
367  unsigned nplanes,
368  vil_pixel_format format,
369  wchar_t const* file_format)
370 {
371 #ifdef VIL_USE_FSTREAM64
372  vil_stream_fstream64* os = new vil_stream_fstream64(filename, "w");
373 #else //VIL_USE_FSTREAM64
374  vil_stream_fstream* os = new vil_stream_fstream(filename, "w");
375 #endif //VIL_USE_FSTREAM64
376 
377  if (!file_format || !*file_format)
378  file_format = vil_save_guess_file_format(filename);
379  return vil_new_image_resource(os, ni, nj, nplanes, format, file_format);
380 }
381 
382 #endif //defined(_WIN32) && VXL_USE_WIN_WCHAR_T
An abstract base class of smart pointers to actual image data in memory.
vil_pixel_format
Describes the type of the concrete data.
vil_pixel_format vil_pixel_format_component_format(enum vil_pixel_format f)
Return the number of components in pixel format f.
vil_pyramid_image_resource_sptr make_pyramid_image_from_base(char const *directory, vil_image_resource_sptr const &base_image, unsigned int nlevels, bool copy_base, char const *level_file_format, char const *filename) override
Construct a pyramid image resource from a base image.
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_pyramid_image_resource_sptr vil_new_pyramid_image_list_from_base(char const *directory, vil_image_resource_sptr const &base_image, unsigned nlevels, bool copy_base, char const *level_file_format, char const *filename)
Construct a new pyramid image resource from a base image.
Definition: vil_new.cxx:262
A vil_stream implementation using std::fstream.
vil_image_resource_sptr vil_new_image_resource_interleaved(unsigned ni, unsigned nj, unsigned nplanes, vil_pixel_format format)
Make a new image of given format with interleaved planes.
Definition: vil_new.cxx:44
vil_blocked_image_resource_sptr vil_new_cached_image_resource(const vil_blocked_image_resource_sptr &bir, const unsigned cache_size)
Make a new cached resource.
Definition: vil_new.cxx:206
A cached and blocked representation of the image_resource.
vil_blocked_image_resource_sptr vil_new_blocked_image_resource(vil_stream *os, unsigned ni, unsigned nj, unsigned nplanes, vil_pixel_format format, unsigned size_block_i, unsigned size_block_j, char const *file_format)
Make a new blocked resource file.
Definition: vil_new.cxx:156
vil_blocked_image_resource_sptr vil_new_blocked_image_facade(const vil_image_resource_sptr &src, unsigned size_block_i, unsigned size_block_j)
create a blocked interface around any image resource.
Definition: vil_new.cxx:198
char const * vil_save_guess_file_format(char const *filename)
Given a filename, guess the file format tag.
Definition: vil_save.cxx:80
Stream interface for VIL image loaders.
Definition: vil_stream.h:21
A vil_stream implementation using std::fstream.
virtual enum vil_pixel_format pixel_format() const =0
Return a description of the concrete data pixel type.
static std::list< vil_file_format * > & all()
Generic image implementation for PNM files.
A blocked image facade for any image resource.
Representation of a pyramid resolution hierarchy.
unsigned vil_pixel_format_num_components(enum vil_pixel_format f)
Return the number of components in pixel format f.
vil_pyramid_image_resource_sptr vil_new_pyramid_image_resource(char const *file_or_directory, char const *file_format)
Make a new pyramid image resource for writing.
Definition: vil_new.cxx:213
Make a new image.
vil_image_resource_sptr vil_new_image_resource_of_view(vil_image_view_base const &view)
Make a new image resource that is a wrapper on an existing view's data.
Definition: vil_new.cxx:62
vil_image_view_base_sptr vil_new_image_view_base_sptr(const vil_image_view_base &)
Create a shallow copy of an image and wrap it in a vil_image_view_base_sptr.
Definition: vil_new.cxx:278
Representation of a generic image source or destination.
#define macro(F, T)
Base class for image formats.
vil_pyramid_image_resource_sptr vil_new_pyramid_image_from_base(char const *filename, vil_image_resource_sptr const &base_image, unsigned nlevels, char const *file_format, char const *temp_dir)
Construct a pyramid image resource from a base image.
Definition: vil_new.cxx:234
A pyramid image resource based on multiple file-based image resources.