vil_io_memory_chunk.cxx
Go to the documentation of this file.
1 // This is core/vil/io/vil_io_memory_chunk.cxx
2 #include "vil_io_memory_chunk.h"
3 //:
4 // \file
5 // \author Tim Cootes
6 // \verbatim
7 // Modifications
8 // Feb.2003 - Ian Scott - Upgraded IO to use vsl_block_binary io
9 // 23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
10 // \endverbatim
11 
12 #include <vsl/vsl_block_binary.h>
13 #include <vsl/vsl_complex_io.h>
14 
15 #define write_case_macro(T)\
16 vsl_b_write(os,unsigned(chunk.size()/sizeof(T ))); \
17 vsl_block_binary_write(os,(const T*) chunk.const_data(),chunk.size()/sizeof(T))
18 
19 
20 //: Binary save vil_memory_chunk to stream.
21 void vsl_b_write(vsl_b_ostream &os, const vil_memory_chunk& chunk)
22 {
23  constexpr short io_version_no = 3;
24  vsl_b_write(os, io_version_no);
25  vsl_b_write(os, int(chunk.pixel_format()));
26 
28  {
29 #if VXL_HAS_INT_64
30  case VIL_PIXEL_FORMAT_UINT_64:
31  write_case_macro(vxl_uint_64);
32  break;
33  case VIL_PIXEL_FORMAT_INT_64:
34  write_case_macro(vxl_int_64);
35  break;
36 #endif
38  write_case_macro(vxl_uint_32);
39  break;
41  write_case_macro(vxl_int_32);
42  break;
44  write_case_macro(vxl_uint_16);
45  break;
47  write_case_macro(vxl_int_16);
48  break;
50  write_case_macro(vxl_byte);
51  break;
53  write_case_macro(vxl_sbyte);
54  break;
56  write_case_macro(float);
57  break;
59  write_case_macro(double);
60  break;
62  write_case_macro(bool);
63  break;
65  write_case_macro(std::complex<float>);
66  break;
68  write_case_macro(std::complex<double>);
69  break;
70  default:
71  std::cerr << "I/O ERROR: vsl_b_write(vsl_b_istream&, vil_memory_chunk&)\n"
72  << " Unknown component type\n";
73  return;
74  }
75 }
76 
77 #undef write_case_macro
78 
79 
80 // This file never uses the fast versions of vsl_b_read_block, so just locally
81 //implement the old slow version.
82 #define read_case_macro_v1(T)\
83 chunk.set_size(n*sizeof(T ),pixel_format); \
84  for (unsigned i=0; i<n; ++i)\
85  vsl_b_read(is, static_cast<T *>(chunk.data())[i]);
86 
87 #define read_case_macro_v2(T)\
88 chunk.set_size(n*sizeof(T ),pixel_format); \
89 vsl_block_binary_read_confirm_specialisation(is, false); \
90  for (unsigned i=0; i<n; ++i)\
91  vsl_b_read(is, static_cast<T *>(chunk.data())[i]);
92 
93 #define read_case_macro_v3(T)\
94 chunk.set_size(n*sizeof(T ),pixel_format); \
95 vsl_block_binary_read(is,static_cast<T *>(chunk.data()),n)
96 
97 
98 //: Binary load vil_memory_chunk from stream.
99 void vsl_b_read(vsl_b_istream &is, vil_memory_chunk& chunk)
100 {
101  if (!is) return;
102 
103  short w;
104  vsl_b_read(is, w);
105  int format;
106  vil_pixel_format pixel_format;
107  unsigned n;
108  switch (w)
109  {
110  case 1:
111  vsl_b_read(is, format); pixel_format=vil_pixel_format(format);
112  vsl_b_read(is, n);
113  switch (pixel_format)
114  {
115 #if VXL_HAS_INT_64
116  case VIL_PIXEL_FORMAT_UINT_64:
117  read_case_macro_v1(vxl_uint_64);
118  break;
119  case VIL_PIXEL_FORMAT_INT_64:
120  read_case_macro_v1(vxl_int_64);
121  break;
122 #endif
124  read_case_macro_v1(vxl_uint_32);
125  break;
127  read_case_macro_v1(vxl_int_32);
128  break;
130  read_case_macro_v1(vxl_uint_16);
131  break;
133  read_case_macro_v1(vxl_int_16);
134  break;
136  read_case_macro_v1(vxl_byte);
137  break;
139  read_case_macro_v1(vxl_sbyte);
140  break;
142  read_case_macro_v1(float);
143  break;
145  read_case_macro_v1(double);
146  break;
148  read_case_macro_v1(bool);
149  break;
150  // No version 1 complex images were ever written.
151  default:
152  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_memory_chunk&)\n"
153  << " Unknown pixel format "<< format << '\n';
154  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
155  return;
156  }
157  break;
158 
159  case 2:
160  vsl_b_read(is, format); pixel_format=vil_pixel_format(format);
161  vsl_b_read(is, n);
162  switch (pixel_format)
163  {
164 #if VXL_HAS_INT_64
165  case VIL_PIXEL_FORMAT_UINT_64:
166  read_case_macro_v3(vxl_uint_64);
167  break;
168  case VIL_PIXEL_FORMAT_INT_64:
169  read_case_macro_v3(vxl_int_64);
170  break;
171 #endif
173  read_case_macro_v3(vxl_uint_32);
174  break;
176  read_case_macro_v3(vxl_int_32);
177  break;
179  read_case_macro_v3(vxl_uint_16);
180  break;
182  read_case_macro_v3(vxl_int_16);
183  break;
185  read_case_macro_v2(vxl_byte);
186  break;
188  read_case_macro_v2(vxl_sbyte);
189  break;
191  read_case_macro_v3(float);
192  break;
194  read_case_macro_v3(double);
195  break;
197  read_case_macro_v3(bool);
198  break;
200  read_case_macro_v3(std::complex<float>);
201  break;
203  read_case_macro_v3(std::complex<double>);
204  break;
205  default:
206  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_memory_chunk&)\n"
207  << " Unknown pixel format "<< format << '\n';
208  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
209  return;
210  }
211  break;
212 
213  case 3:
214  vsl_b_read(is, format); pixel_format=vil_pixel_format(format);
215  vsl_b_read(is, n);
216  switch (pixel_format)
217  {
218 #if VXL_HAS_INT_64
219  case VIL_PIXEL_FORMAT_UINT_64:
220  read_case_macro_v3(vxl_uint_64);
221  break;
222  case VIL_PIXEL_FORMAT_INT_64:
223  read_case_macro_v3(vxl_int_64);
224  break;
225 #endif
227  read_case_macro_v3(vxl_uint_32);
228  break;
230  read_case_macro_v3(vxl_int_32);
231  break;
233  read_case_macro_v3(vxl_uint_16);
234  break;
236  read_case_macro_v3(vxl_int_16);
237  break;
239  read_case_macro_v3(vxl_byte);
240  break;
242  read_case_macro_v3(vxl_sbyte);
243  break;
245  read_case_macro_v3(float);
246  break;
248  read_case_macro_v3(double);
249  break;
251  read_case_macro_v3(bool);
252  break;
254  read_case_macro_v3(std::complex<float>);
255  break;
257  read_case_macro_v3(std::complex<double>);
258  break;
259  default:
260  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_memory_chunk&)\n"
261  << " Unknown pixel format "<< format << '\n';
262  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
263  return;
264  }
265  break;
266 
267  default:
268  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_memory_chunk&)\n"
269  << " Unknown version number "<< w << '\n';
270  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
271  return;
272  }
273 }
274 
275 #undef read_case_macro
276 
277 //: Binary save vil_memory_chunk to stream by pointer
278 void vsl_b_write(vsl_b_ostream &os, const vil_memory_chunk* chunk_ptr)
279 {
280  bool not_null_ptr = (chunk_ptr!=nullptr);
281  vsl_b_write(os,not_null_ptr);
282  if (not_null_ptr)
283  vsl_b_write(os,*chunk_ptr);
284 }
285 
286 //: Binary load vil_memory_chunk from stream onto the heap
287 void vsl_b_read(vsl_b_istream &is, vil_memory_chunk*& p)
288 {
289  delete p;
290  bool not_null_ptr;
291  vsl_b_read(is, not_null_ptr);
292  if (not_null_ptr)
293  {
294  p = new vil_memory_chunk();
295  vsl_b_read(is, *p);
296  }
297  else
298  p = nullptr;
299 }
300 
301 //: Print human readable summary of a vil_memory_chunk object to a stream
302 void vsl_print_summary(std::ostream& os,const vil_memory_chunk& chunk)
303 {
304  os<<"vil_memory_chunk containing "<<chunk.size()<<" bytes of "<<chunk.pixel_format();
305 }
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.
#define read_case_macro_v2(T)
std::complex<float> is a scalar for vil's purposes.
std::size_t size() const
Number of bytes allocated.
#define read_case_macro_v1(T)
void vsl_print_summary(std::ostream &os, const vil_image_view< T > &image)
Print human readable summary of a vil_image_view<T> object to a stream.
#define read_case_macro_v3(T)
#define write_case_macro(T)
void vsl_b_write(vsl_b_ostream &os, const vil_image_view< T > &image)
Binary save vil_image_view<T> to stream.
Ref. counted block of data on the heap.
vil_pixel_format pixel_format() const
Indicate what format data is to be saved as in binary IO.
void vsl_b_read(vsl_b_istream &is, vil_image_view< T > &image)
Binary load vil_image_view<T> from stream.
std::complex<double> is a scalar for vil's purposes.