vil_io_image_view.h
Go to the documentation of this file.
1 // This is core/vil/io/vil_io_image_view.h
2 #ifndef vil_io_image_view_h_
3 #define vil_io_image_view_h_
4 //:
5 // \file
6 // \author Tim Cootes
7 
8 #include <cstddef>
9 #include <iostream>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 #include <vil/vil_image_view.h>
15 
16 //: Binary save vil_image_view<T> to stream.
17 // \relatesalso vil_image_view
18 template<class T>
19 inline void vsl_b_write(vsl_b_ostream &os, const vil_image_view<T>& image)
20 {
21  constexpr short io_version_no = 1;
22  vsl_b_write(os, io_version_no);
23  vsl_b_write(os, image.ni());
24  vsl_b_write(os, image.nj());
25  vsl_b_write(os, image.nplanes());
26  vsl_b_write(os, image.istep());
27  vsl_b_write(os, image.jstep());
28  vsl_b_write(os, image.planestep());
29  if (image.size()>0)
30  {
31  vsl_b_write(os, image.memory_chunk());
32 
33  std::ptrdiff_t offset = (image.top_left_ptr()-
34  reinterpret_cast<const T*>(image.memory_chunk()->data()));
35  vsl_b_write(os, offset);
36  }
37 }
38 
39 
40 //: Binary load vil_image_view<T> from stream.
41 // \relatesalso vil_image_view
42 template<class T>
43 inline void vsl_b_read(vsl_b_istream &is, vil_image_view<T>& image)
44 {
45  if (!is) return;
46 
47  unsigned ni,nj,np;
48  std::ptrdiff_t istep,jstep,pstep;
50  std::ptrdiff_t offset;
51 
52  short w;
53  vsl_b_read(is, w);
54  switch (w)
55  {
56  case 1:
57  vsl_b_read(is, ni);
58  vsl_b_read(is, nj);
59  vsl_b_read(is, np);
60  vsl_b_read(is, istep);
61  vsl_b_read(is, jstep);
62  vsl_b_read(is, pstep);
63  if (ni*nj*np==0)
64  image.set_size(0,0,0);
65  else
66  {
67  vsl_b_read(is, chunk);
68  if (vil_pixel_format_component_format(image.pixel_format()) != chunk->pixel_format())
69  {
70  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
71  << " Mismatched pixel format.\n";
72  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
73  return;
74  }
75  vsl_b_read(is, offset);
76  const T* data = reinterpret_cast<const T*>(chunk->data());
77 
78  if (chunk->size() < np*ni*nj*sizeof(T) ||
79  offset < 0 || offset*sizeof(T) >= chunk->size())
80  {
81  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
82  << " Image details not compatible with chunk data.\n";
83  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
84  return;
85  }
86 
87  image = vil_image_view<T>(chunk,data+offset,
88  ni,nj,np,istep,jstep,pstep);
89  }
90  break;
91 
92  default:
93  std::cerr << "I/O ERROR: vsl_b_read(vsl_b_istream&, vil_image_view<T>&)\n"
94  << " Unknown version number "<< w << '\n';
95  is.is().clear(std::ios::badbit); // Set an unrecoverable IO error on stream
96  break;
97  }
98 }
99 
100 //: Binary load vil_image_view<T> from stream onto the heap
101 // \relatesalso vil_image_view
102 template<class T>
103 inline void vsl_b_read(vsl_b_istream &is, vil_image_view<T>*& p)
104 {
105  delete p;
106  bool not_null_ptr;
107  vsl_b_read(is, not_null_ptr);
108  if (not_null_ptr)
109  {
110  p = new vil_image_view<T>();
111  vsl_b_read(is, *p);
112  }
113  else
114  p = 0;
115 }
116 
117 //: Print human readable summary of a vil_image_view<T> object to a stream
118 // \relatesalso vil_image_view
119 template<class T>
120 inline void vsl_print_summary(std::ostream& os,const vil_image_view<T>& image)
121 {
122  image.print(os);
123 }
124 
125 #endif // vil_io_image_view_h_
vil_pixel_format vil_pixel_format_component_format(enum vil_pixel_format f)
Return the number of components in pixel format f.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
std::ptrdiff_t jstep() const
Add this to your pixel pointer to get next j pixel.
unsigned ni() const
Width.
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.
unsigned nj() const
Height.
std::ptrdiff_t planestep() const
Add this to your pixel pointer to get pixel on next plane.
void print(std::ostream &) const override
Print a 1-line summary of contents.
void vsl_b_write(vsl_b_ostream &os, const vil_image_view< T > &image)
Binary save vil_image_view<T> to stream.
A base class reference-counting view of some image data.
unsigned long size() const
The number of pixels.
T * top_left_ptr()
Pointer to the first (top left in plane 0) pixel.
unsigned nplanes() const
Number of planes.
vil_pixel_format pixel_format() const override
Return a description of the concrete data pixel type.
const vil_memory_chunk_sptr & memory_chunk() const
Smart pointer to the object holding the data for this view.
void vsl_b_read(vsl_b_istream &is, vil_image_view< T > &image)
Binary load vil_image_view<T> from stream.
std::ptrdiff_t istep() const
Add this to your pixel pointer to get next i pixel.