Boost GIL


image.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_IMAGE_HPP
9 #define BOOST_GIL_IMAGE_HPP
10 
11 #include <boost/gil/algorithm.hpp>
12 #include <boost/gil/image_view.hpp>
13 #include <boost/gil/metafunctions.hpp>
14 #include <boost/gil/detail/mp11.hpp>
15 
16 #include <cstddef>
17 #include <memory>
18 #include <type_traits>
19 
20 namespace boost { namespace gil {
21 
35 
36 template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
37 class image {
38 public:
39 #if defined(BOOST_NO_CXX11_ALLOCATOR)
40  using allocator_type = typename Alloc::template rebind<unsigned char>::other;
41 #else
42  using allocator_type = typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
43 #endif
44  using view_t = typename view_type_from_pixel<Pixel, IsPlanar>::type;
45  using const_view_t = typename view_t::const_t;
46  using point_t = typename view_t::point_t;
47  using coord_t = typename view_t::coord_t;
48  using value_type = typename view_t::value_type;
49  using x_coord_t = coord_t;
50  using y_coord_t = coord_t;
51 
52  const point_t& dimensions() const { return _view.dimensions(); }
53  x_coord_t width() const { return _view.width(); }
54  y_coord_t height() const { return _view.height(); }
55 
56  explicit image(std::size_t alignment=0,
57  const Alloc alloc_in = Alloc()) :
58  _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
59 
60  // Create with dimensions and optional initial value and alignment
61  image(const point_t& dimensions,
62  std::size_t alignment=0,
63  const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
64  , _allocated_bytes( 0 ) {
65  allocate_and_default_construct(dimensions);
66  }
67 
68  image(x_coord_t width, y_coord_t height,
69  std::size_t alignment=0,
70  const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
71  , _allocated_bytes( 0 ) {
72  allocate_and_default_construct(point_t(width,height));
73  }
74 
75  image(const point_t& dimensions,
76  const Pixel& p_in,
77  std::size_t alignment,
78  const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
79  , _allocated_bytes( 0 ) {
80  allocate_and_fill(dimensions, p_in);
81  }
82  image(x_coord_t width, y_coord_t height,
83  const Pixel& p_in,
84  std::size_t alignment = 0,
85  const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
86  , _allocated_bytes ( 0 ) {
87  allocate_and_fill(point_t(width,height),p_in);
88  }
89 
90  image(const image& img) : _memory(nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
91  , _allocated_bytes( img._allocated_bytes ) {
92  allocate_and_copy(img.dimensions(),img._view);
93  }
94 
95  template <typename P2, bool IP2, typename Alloc2>
96  image(const image<P2,IP2,Alloc2>& img) : _memory(nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
97  , _allocated_bytes( img._allocated_bytes ) {
98  allocate_and_copy(img.dimensions(),img._view);
99  }
100 
101  image& operator=(const image& img) {
102  if (dimensions() == img.dimensions())
103  copy_pixels(img._view,_view);
104  else {
105  image tmp(img);
106  swap(tmp);
107  }
108  return *this;
109  }
110 
111  template <typename Img>
112  image& operator=(const Img& img) {
113  if (dimensions() == img.dimensions())
114  copy_pixels(img._view,_view);
115  else {
116  image tmp(img);
117  swap(tmp);
118  }
119  return *this;
120  }
121 
122  ~image() {
123  destruct_pixels(_view);
124  deallocate();
125  }
126 
127  Alloc& allocator() { return _alloc; }
128  Alloc const& allocator() const { return _alloc; }
129 
130  void swap(image& img) { // required by MutableContainerConcept
131  using std::swap;
132  swap(_align_in_bytes, img._align_in_bytes);
133  swap(_memory, img._memory);
134  swap(_view, img._view);
135  swap(_alloc, img._alloc);
136  swap(_allocated_bytes, img._allocated_bytes );
137  }
138 
140  // recreate
142 
143  // without Allocator
144  void recreate(const point_t& dims, std::size_t alignment = 0)
145  {
146  if (dims == _view.dimensions() && _align_in_bytes == alignment)
147  return;
148 
149  _align_in_bytes = alignment;
150 
151  if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
152  {
153  destruct_pixels(_view);
154  create_view(dims, std::integral_constant<bool, IsPlanar>());
156  }
157  else
158  {
159  image tmp(dims, alignment);
160  swap(tmp);
161  }
162  }
163 
164  void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
165  {
166  recreate(point_t(width, height), alignment);
167  }
168 
169  void recreate(const point_t& dims, const Pixel& p_in, std::size_t alignment = 0)
170  {
171  if (dims == _view.dimensions() && _align_in_bytes == alignment)
172  return;
173 
174  _align_in_bytes = alignment;
175 
176  if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
177  {
178  destruct_pixels(_view);
179  create_view(dims, typename std::integral_constant<bool, IsPlanar>());
180  uninitialized_fill_pixels(_view, p_in);
181  }
182  else
183  {
184  image tmp(dims, p_in, alignment);
185  swap(tmp);
186  }
187  }
188 
189  void recreate( x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment = 0 )
190  {
191  recreate( point_t( width, height ), p_in, alignment );
192  }
193 
194  // with Allocator
195  void recreate(const point_t& dims, std::size_t alignment, const Alloc alloc_in)
196  {
197  if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
198  return;
199 
200  _align_in_bytes = alignment;
201 
202  if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
203  {
204  destruct_pixels(_view);
205  create_view(dims, std::integral_constant<bool, IsPlanar>());
207  }
208  else
209  {
210  image tmp(dims, alignment, alloc_in);
211  swap(tmp);
212  }
213  }
214 
215  void recreate(x_coord_t width, y_coord_t height, std::size_t alignment, const Alloc alloc_in)
216  {
217  recreate(point_t(width, height), alignment, alloc_in);
218  }
219 
220  void recreate(const point_t& dims, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in)
221  {
222  if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
223  return;
224 
225  _align_in_bytes = alignment;
226 
227  if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
228  {
229  destruct_pixels(_view);
230  create_view(dims, std::integral_constant<bool, IsPlanar>());
231  uninitialized_fill_pixels(_view, p_in);
232  }
233  else
234  {
235  image tmp(dims, p_in, alignment, alloc_in);
236  swap(tmp);
237  }
238  }
239 
240  void recreate(x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in )
241  {
242  recreate(point_t(width, height), p_in, alignment, alloc_in);
243  }
244 
245  view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
246 private:
247  unsigned char* _memory;
248  std::size_t _align_in_bytes;
249  allocator_type _alloc;
250 
251  std::size_t _allocated_bytes;
252 
253  void allocate_and_default_construct(point_t const& dimensions)
254  {
255  try
256  {
257  allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
259  }
260  catch (...) { deallocate(); throw; }
261  }
262 
263  void allocate_and_fill(const point_t& dimensions, Pixel const& p_in)
264  {
265  try
266  {
267  allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
268  uninitialized_fill_pixels(_view, p_in);
269  }
270  catch(...) { deallocate(); throw; }
271  }
272 
273  template <typename View>
274  void allocate_and_copy(const point_t& dimensions, View const& v)
275  {
276  try
277  {
278  allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
279  uninitialized_copy_pixels(v, _view);
280  }
281  catch(...) { deallocate(); throw; }
282  }
283 
284  void deallocate()
285  {
286  if (_memory && _allocated_bytes > 0)
287  _alloc.deallocate(_memory, _allocated_bytes);
288  }
289 
290  std::size_t is_planar_impl(
291  std::size_t const size_in_units,
292  std::size_t const channels_in_image,
293  std::true_type) const
294  {
295  return size_in_units * channels_in_image;
296  }
297 
298  std::size_t is_planar_impl(
299  std::size_t const size_in_units,
300  std::size_t const,
301  std::false_type) const
302  {
303  return size_in_units;
304  }
305 
306  std::size_t total_allocated_size_in_bytes(point_t const& dimensions) const
307  {
308  using x_iterator = typename view_t::x_iterator;
309 
310  // when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
311  constexpr std::size_t _channels_in_image =
312  std::conditional
313  <
314  is_pixel<value_type>::value,
316  std::integral_constant<std::size_t, 1>
317  >::type::value;
318 
319  std::size_t size_in_units = is_planar_impl(
320  get_row_size_in_memunits(dimensions.x) * dimensions.y,
321  _channels_in_image,
322  std::integral_constant<bool, IsPlanar>());
323 
324  // return the size rounded up to the nearest byte
325  return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
327  + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
328  }
329 
330  std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
331  std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
332  if (_align_in_bytes>0) {
333  std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
334  return align(size_in_memunits, alignment_in_memunits);
335  }
336  return size_in_memunits;
337  }
338 
339  void allocate_(point_t const& dimensions, std::false_type)
340  {
341  // if it throws and _memory!=0 the client must deallocate _memory
342  _allocated_bytes = total_allocated_size_in_bytes(dimensions);
343  _memory=_alloc.allocate( _allocated_bytes );
344 
345  unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
346  _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
347  }
348 
349  void allocate_(point_t const& dimensions, std::true_type)
350  {
351  // if it throws and _memory!=0 the client must deallocate _memory
352  std::size_t row_size=get_row_size_in_memunits(dimensions.x);
353  std::size_t plane_size=row_size*dimensions.y;
354 
355  _allocated_bytes = total_allocated_size_in_bytes( dimensions );
356 
357  _memory = _alloc.allocate( _allocated_bytes );
358 
359  unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
360  typename view_t::x_iterator first;
361  for (int i=0; i<num_channels<view_t>::value; ++i) {
362  dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
363  memunit_advance(dynamic_at_c(first,i), plane_size*i);
364  }
365  _view=view_t(dimensions, typename view_t::locator(first, row_size));
366  }
367 
368  void create_view(point_t const& dims, std::true_type) // is planar
369  {
370  std::size_t row_size=get_row_size_in_memunits(dims.x);
371  std::size_t plane_size=row_size*dims.y;
372 
373  unsigned char* tmp = ( _align_in_bytes > 0 ) ? (unsigned char*) align( (std::size_t) _memory
374  ,_align_in_bytes
375  )
376  : _memory;
377  typename view_t::x_iterator first;
378 
379  for (int i = 0; i < num_channels< view_t >::value; ++i )
380  {
381  dynamic_at_c( first, i ) = (typename channel_type<view_t>::type*) tmp;
382 
383  memunit_advance( dynamic_at_c(first,i)
384  , plane_size*i
385  );
386  }
387 
388  _view=view_t( dims
389  , typename view_t::locator( first
390  , row_size
391  )
392  );
393  }
394 
395  void create_view(point_t const& dims, std::false_type) // is planar
396  {
397  unsigned char* tmp = ( _align_in_bytes > 0 ) ? ( unsigned char* ) align( (std::size_t) _memory
398  , _align_in_bytes
399  )
400  : _memory;
401 
402  _view = view_t( dims
403  , typename view_t::locator( typename view_t::x_iterator( tmp )
404  , get_row_size_in_memunits( dims.x )
405  )
406  );
407  }
408 };
409 
410 template <typename Pixel, bool IsPlanar, typename Alloc>
412  im1.swap(im2);
413 }
414 
415 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
416 bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
417  if ((void*)(&im1)==(void*)(&im2)) return true;
418  if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
419  return equal_pixels(const_view(im1),const_view(im2));
420 }
421 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
422 bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
423 
427 
429 
431 template <typename Pixel, bool IsPlanar, typename Alloc> inline
432 const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
433 
435 template <typename Pixel, bool IsPlanar, typename Alloc> inline
436 const typename image<Pixel,IsPlanar,Alloc>::const_view_t const_view(const image<Pixel,IsPlanar,Alloc>& img) {
437  return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
438 }
440 
442 // PixelBasedConcept
444 
445 template <typename Pixel, bool IsPlanar, typename Alloc>
446 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
447 
448 template <typename Pixel, bool IsPlanar, typename Alloc>
449 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
450 
451 template <typename Pixel, bool IsPlanar, typename Alloc>
452 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
453 
454 template <typename Pixel, bool IsPlanar, typename Alloc>
455 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};
456 
457 }} // namespace boost::gil
458 
459 #endif
Definition: pixel_iterator.hpp:124
Definition: algorithm.hpp:30
void default_construct_pixels(View const &view)
Invokes the in-place default constructor on every pixel of the (uninitialized) view. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place default-constructed pixels.
Definition: algorithm.hpp:714
void uninitialized_copy_pixels(View1 const &view1, View2 const &view2)
std::uninitialized_copy for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed objects
Definition: algorithm.hpp:778
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:1051
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:282
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition: image.hpp:37
void uninitialized_fill_pixels(const View &view, const Value &val)
std::uninitialized_fill for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed pixels
Definition: algorithm.hpp:577
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:524
Definition: color_convert.hpp:31
const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:432
const image< Pixel, IsPlanar, Alloc >::const_view_t const_view(const image< Pixel, IsPlanar, Alloc > &img)
Returns the constant-pixel view of an image.
Definition: image.hpp:436
Returns the number of channels of a pixel-based GIL construct.
Definition: locator.hpp:38
Returns the type of a view the pixel type, whether it operates on planar data and whether it has a st...
Definition: metafunctions.hpp:556
BOOST_FORCEINLINE void destruct_pixels(View const &view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:508