vil_memory_image.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_memory_image.cxx
2 //:
3 // \file
4 // \author Ian Scott
5 //
6 // \verbatim
7 // Modifications
8 // 23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
9 // \endverbatim
10 
11 #include <cstdlib>
12 #include "vil_memory_image.h"
13 #include <cassert>
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #include <vxl_config.h> // for vxl_uint_32 etc.
18 #include <vil/vil_image_view.h>
19 #include <vil/vil_copy.h>
20 #include <vil/vil_pixel_format.h>
21 
23 
25 : view_(new vil_image_view<vxl_byte>()) {}
26 
27 
28 //: Create an in-memory image of given size and pixel type.
29 // If not interleaved, pixel type must be scalar or nplanes must be 1.
30 // If n_interleaved_planes is not 1, pixel type must be scalar, and n_planes must be 1.
31 vil_memory_image::vil_memory_image(unsigned n_i, unsigned n_j, unsigned n_planes,
32  vil_pixel_format format, unsigned n_interleaved_planes/*=1*/)
33 {
34  // Check that only once source of multiple planes is not 1.
35  assert( ( (n_planes==1?0:1) + (n_interleaved_planes==1?0:1) +
36  (vil_pixel_format_num_components(format)==1?0:1) ) <= 1) ;
37 
38  if (vil_pixel_format_num_components(format)!=1)
39  n_interleaved_planes = vil_pixel_format_num_components(format);
40 
41  switch (vil_pixel_format_component_format(format))
42  {
43 #define macro( F , T ) \
44  case F : view_ = new vil_image_view<T >(n_i, n_j, n_planes, n_interleaved_planes); \
45  break;
46  macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte)
47  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte)
48 #if VXL_HAS_INT_64
49  macro(VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64)
50  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64)
51 #endif
52  macro(VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32)
53  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32)
54  macro(VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16)
55  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16)
59  macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT , std::complex<float>)
60  macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , std::complex<double>)
61 #undef macro
62  default:
63  std::cerr << "ERROR: vil_memory_image::vil_memory_image\n"
64  "\t unknown format " << format << std::endl;
65  std::abort();
66  }
67 }
68 
69 
70 //: Create a wrapper around the given image_view
72 {
74  {
75 #define macro( F , T ) \
76  case F : view_ = new vil_image_view<T >(view); break;
77  macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
78  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
79 #if VXL_HAS_INT_64
80  macro(VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64 )
81  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
82 #endif
83  macro(VIL_PIXEL_FORMAT_UINT_32, vxl_uint_32 )
84  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
85  macro(VIL_PIXEL_FORMAT_UINT_16, vxl_uint_16 )
86  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
90  macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT , std::complex<float>)
91  macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , std::complex<double>)
92 #undef macro
93  default:
94  std::cerr << "ERROR: vil_memory_image::vil_memory_image\n\tunknown format "
96  std::abort();
97  }
98  assert (view_->ni() == view.ni() && view_->nj() == view.nj());
99 }
100 
101 
102 //: Create a read/write view of a copy of this data.
103 // Currently not yet implemented.
104 // \return 0 if unable to get view of correct size.
106  unsigned j0, unsigned n_j) const
107 {
108  if (i0 + n_i > view_->ni() || j0 + n_j > view_->nj()) return nullptr;
109 
110  switch (view_->pixel_format())
111  {
112 #define macro( F , T ) \
113  case F : { \
114  const vil_image_view< T > &v = static_cast<const vil_image_view< T > &>(*view_); \
115  vil_image_view< T > w(v.memory_chunk(), &v(i0,j0), \
116  n_i, n_j, v.nplanes(), \
117  v.istep(), v.jstep(), v.planestep()); \
118  return new vil_image_view< T >(vil_copy_deep(w)); }
119  macro(VIL_PIXEL_FORMAT_BYTE, vxl_byte )
120  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
121 #if VXL_HAS_INT_64
122  macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
123  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
124 #endif
125  macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
126  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
127  macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
128  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
130  macro(VIL_PIXEL_FORMAT_FLOAT , float )
131  macro(VIL_PIXEL_FORMAT_DOUBLE , double )
132  macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT , std::complex<float>)
133  macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , std::complex<double>)
134 #undef macro
135  default:
136  return nullptr;
137  }
138 }
139 
140 //: Create a read/write view of a copy of this data.
141 // \return 0 if unable to get view of correct size.
143  unsigned j0, unsigned n_j) const
144 {
145  if (i0 + n_i > view_->ni() || j0 + n_j > view_->nj()) return nullptr;
146 
147  switch (view_->pixel_format())
148  {
149 #define macro( F , T ) \
150  case F : { \
151  const vil_image_view< T > &v = static_cast<const vil_image_view< T > &>(*view_); \
152  return new vil_image_view< T >(v.memory_chunk(), &v(i0,j0), \
153  n_i, n_j, v.nplanes(), \
154  v.istep(), v.jstep(), v.planestep()); }
155  macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
156  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
157 #if VXL_HAS_INT_64
158  macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
159  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
160 #endif
161  macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
162  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
163  macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
164  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
166  macro(VIL_PIXEL_FORMAT_FLOAT , float )
167  macro(VIL_PIXEL_FORMAT_DOUBLE , double )
168  macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT , std::complex<float>)
169  macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , std::complex<double>)
170 #undef macro
171  default:
172  return nullptr;
173  }
174 }
175 
176 
177 //: Put the data in this view back into the image source.
178 // \return true on success.
179 bool vil_memory_image::put_view(const vil_image_view_base& im,unsigned i0, unsigned j0)
180 {
181  if (view_-> pixel_format() != im.pixel_format()) return false;
182  if (!view_fits(im, i0, j0)) return false;
183 
184  switch (view_->pixel_format())
185  {
186 #define macro( F , T ) \
187  case F : { \
188  vil_image_view< T > &v = static_cast<vil_image_view< T > &>(*view_); \
189  const vil_image_view< T > &w = static_cast<const vil_image_view< T > &>(im); \
190  if (v.memory_chunk() == w.memory_chunk()) \
191  { \
192  if (&v(i0,j0) != w.top_left_ptr()) { \
193  std::cerr << "ERROR: vil_memory_image::put_view()\n" \
194  << "different window from that used in get_view()\n"; \
195  std::abort(); } \
196  else return true; /* The user has already modified the data in place. */ \
197  } \
198  vil_copy_to_window(w, v, i0, j0); \
199  return true; }
200 
201  macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
202  macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
203 #if VXL_HAS_INT_64
204  macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
205  macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
206 #endif
207  macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
208  macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
209  macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
210  macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
212  macro(VIL_PIXEL_FORMAT_FLOAT , float )
213  macro(VIL_PIXEL_FORMAT_DOUBLE , double )
214  macro(VIL_PIXEL_FORMAT_COMPLEX_FLOAT , std::complex<float>)
215  macro(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE , std::complex<double>)
216 #undef macro
217  default:
218  std::cerr << "WARNING: vil_memory_image::put_view()\n"
219  << "\t Unexpected pixel type" << view_->pixel_format() << std::endl;
220  return false;
221  }
222 }
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.
virtual bool view_fits(const vil_image_view_base &im, unsigned i0, unsigned j0)
Check that a view will fit into the data at the given offset.
std::complex<float> is a scalar for vil's purposes.
Concrete view of image data of type T held in memory.
Definition: vil_fwd.h:13
#define macro(F, T)
vil_image_view_base * view_
Management of the memory image is devolved to an internal image_view.
unsigned ni() const
Width.
unsigned nj() const
Height.
virtual enum vil_pixel_format pixel_format() const =0
Return a description of the concrete data pixel type.
unsigned vil_pixel_format_num_components(enum vil_pixel_format f)
Return the number of components in pixel format f.
Various image copying functions.
vil_image_view_base_sptr get_copy_view() const
Create a read/write view of a copy of all the data.
A base class reference-counting view of some image data.
bool put_view(const vil_image_view_base &im, unsigned i0, unsigned j0) override
Put the data in this view back into the image source.
enum vil_pixel_format pixel_format() const override
Pixel Format.
vil_image_view_base_sptr get_view() const
Create a read/write view of all the data.
std::complex<double> is a scalar for vil's purposes.
vil_memory_image()
Create an empty memory image.