9 #ifndef BOOST_GIL_IMAGE_HPP
10 #define BOOST_GIL_IMAGE_HPP
12 #include <boost/gil/algorithm.hpp>
13 #include <boost/gil/image_view.hpp>
14 #include <boost/gil/metafunctions.hpp>
15 #include <boost/gil/detail/mp11.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/core/exchange.hpp>
23 #include <type_traits>
25 namespace boost {
namespace gil {
41 template<
typename Pixel,
bool IsPlanar,
typename Alloc>
45 #if defined(BOOST_NO_CXX11_ALLOCATOR)
46 using allocator_type =
typename Alloc::template rebind<unsigned char>::other;
48 using allocator_type =
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
51 using const_view_t =
typename view_t::const_t;
52 using point_t =
typename view_t::point_t;
53 using coord_t =
typename view_t::coord_t;
54 using value_type =
typename view_t::value_type;
55 using x_coord_t = coord_t;
56 using y_coord_t = coord_t;
58 const point_t& dimensions()
const {
return _view.dimensions(); }
59 x_coord_t width()
const {
return _view.width(); }
60 y_coord_t height()
const {
return _view.height(); }
62 explicit image(std::size_t alignment=0,
63 const Alloc alloc_in = Alloc()) :
64 _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
67 image(
const point_t& dimensions,
68 std::size_t alignment=0,
69 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
70 , _allocated_bytes( 0 )
72 allocate_and_default_construct(dimensions);
75 image(x_coord_t width, y_coord_t height,
76 std::size_t alignment=0,
77 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
78 , _allocated_bytes( 0 )
80 allocate_and_default_construct(point_t(width,height));
83 image(
const point_t& dimensions,
85 std::size_t alignment = 0,
86 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
87 , _allocated_bytes( 0 )
89 allocate_and_fill(dimensions, p_in);
92 image(x_coord_t width, y_coord_t height,
94 std::size_t alignment = 0,
95 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
96 , _allocated_bytes ( 0 )
98 allocate_and_fill(point_t(width,height),p_in);
101 image(
const image& img) : _memory(
nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
102 , _allocated_bytes( img._allocated_bytes )
104 allocate_and_copy(img.dimensions(),img._view);
107 template <
typename P2,
bool IP2,
typename Alloc2>
109 , _allocated_bytes( img._allocated_bytes )
111 allocate_and_copy(img.dimensions(),img._view);
114 template <
typename Loc,
115 typename std::enable_if<pixels_are_compatible<typename Loc::value_type, Pixel>::value,
int>::type = 0>
117 std::size_t alignment = 0,
118 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
119 , _allocated_bytes( 0 )
121 allocate_and_copy(
view.dimensions(),
view);
127 _memory(img._memory),
128 _align_in_bytes(img._align_in_bytes),
129 _alloc(std::move(img._alloc)),
130 _allocated_bytes(img._allocated_bytes)
132 img._view = view_t();
133 img._memory =
nullptr;
134 img._align_in_bytes = 0;
135 img._allocated_bytes = 0;
140 if (dimensions() == img.dimensions())
150 template <
typename Img>
151 image& operator=(
const Img& img)
153 if (dimensions() == img.dimensions())
164 using propagate_allocators = std::true_type;
165 using no_propagate_allocators = std::false_type;
167 template <
class Alloc2>
168 using choose_pocma =
typename std::conditional<
170 std::is_empty<Alloc2>::value,
172 typename std::allocator_traits<Alloc2>::propagate_on_container_move_assignment::type
175 static void exchange_memory(
image& lhs,
image& rhs)
177 lhs._memory = boost::exchange(rhs._memory,
nullptr);
178 lhs._align_in_bytes = boost::exchange(rhs._align_in_bytes, 0);
179 lhs._allocated_bytes = boost::exchange(rhs._allocated_bytes, 0);
180 lhs._view = boost::exchange(rhs._view, image::view_t{});
183 void move_assign(
image& img, propagate_allocators) noexcept {
187 this->_alloc = img._alloc;
188 exchange_memory(*
this, img);
191 void move_assign(
image& img, no_propagate_allocators) {
192 if (_alloc == img._alloc) {
196 exchange_memory(*
this, img);
201 allocate_and_copy(img.dimensions(), img._view);
204 img._view = image::view_t{};
210 this->_view = view_t{};
219 if (
this != std::addressof(img))
221 move_assign(img, choose_pocma<allocator_type>{});
232 Alloc& allocator() {
return _alloc; }
233 Alloc
const& allocator()
const {
return _alloc; }
235 void swap(
image& img)
238 swap(_align_in_bytes, img._align_in_bytes);
239 swap(_memory, img._memory);
240 swap(_view, img._view);
241 swap(_alloc, img._alloc);
242 swap(_allocated_bytes, img._allocated_bytes );
250 void recreate(
const point_t& dims, std::size_t alignment = 0)
252 if (dims == _view.dimensions() && _align_in_bytes == alignment)
255 _align_in_bytes = alignment;
257 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
260 create_view(dims, std::integral_constant<bool, IsPlanar>());
265 image tmp(dims, alignment);
270 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
272 recreate(point_t(width, height), alignment);
275 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0)
277 if (dims == _view.dimensions() && _align_in_bytes == alignment)
280 _align_in_bytes = alignment;
282 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
285 create_view(dims,
typename std::integral_constant<bool, IsPlanar>());
290 image tmp(dims, p_in, alignment);
295 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
297 recreate( point_t( width, height ), p_in, alignment );
301 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in)
303 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
306 _align_in_bytes = alignment;
308 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
311 create_view(dims, std::integral_constant<bool, IsPlanar>());
316 image tmp(dims, alignment, alloc_in);
321 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in)
323 recreate(point_t(width, height), alignment, alloc_in);
326 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in)
328 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
331 _align_in_bytes = alignment;
333 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
336 create_view(dims, std::integral_constant<bool, IsPlanar>());
341 image tmp(dims, p_in, alignment, alloc_in);
346 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
348 recreate(point_t(width, height), p_in, alignment, alloc_in);
354 template <
typename P2,
bool IP2,
typename Alloc2>
friend class image;
356 unsigned char* _memory;
357 std::size_t _align_in_bytes;
358 allocator_type _alloc;
360 std::size_t _allocated_bytes;
362 void allocate_and_default_construct(point_t
const& dimensions)
366 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
369 catch (...) { deallocate();
throw; }
372 void allocate_and_fill(
const point_t& dimensions, Pixel
const& p_in)
376 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
379 catch(...) { deallocate();
throw; }
382 template <
typename View>
383 void allocate_and_copy(
const point_t& dimensions, View
const& v)
387 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
390 catch(...) { deallocate();
throw; }
395 if (_memory && _allocated_bytes > 0)
396 _alloc.deallocate(_memory, _allocated_bytes);
399 std::size_t is_planar_impl(
400 std::size_t
const size_in_units,
401 std::size_t
const channels_in_image,
402 std::true_type)
const
404 return size_in_units * channels_in_image;
407 std::size_t is_planar_impl(
408 std::size_t
const size_in_units,
410 std::false_type)
const
412 return size_in_units;
415 std::size_t total_allocated_size_in_bytes(point_t
const& dimensions)
const
417 using x_iterator =
typename view_t::x_iterator;
420 constexpr std::size_t _channels_in_image =
423 is_pixel<value_type>::value,
425 std::integral_constant<std::size_t, 1>
428 std::size_t size_in_units = is_planar_impl(
429 get_row_size_in_memunits(dimensions.x) * dimensions.y,
431 std::integral_constant<bool, IsPlanar>());
436 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
439 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
440 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
441 if (_align_in_bytes>0) {
443 return align(size_in_memunits, alignment_in_memunits);
445 return size_in_memunits;
448 void allocate_(point_t
const& dimensions, std::false_type)
451 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
452 _memory=_alloc.allocate( _allocated_bytes );
454 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
455 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp), get_row_size_in_memunits(dimensions.x)));
457 BOOST_ASSERT(_view.width() == dimensions.x);
458 BOOST_ASSERT(_view.height() == dimensions.y);
461 void allocate_(point_t
const& dimensions, std::true_type)
464 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
465 std::size_t plane_size=row_size*dimensions.y;
467 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
469 _memory = _alloc.allocate( _allocated_bytes );
471 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
472 typename view_t::x_iterator first;
473 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
476 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
478 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
480 BOOST_ASSERT(_view.width() == dimensions.x);
481 BOOST_ASSERT(_view.height() == dimensions.y);
484 void create_view(point_t
const& dims, std::true_type)
486 std::size_t row_size=get_row_size_in_memunits(dims.x);
487 std::size_t plane_size=row_size*dims.y;
489 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
493 typename view_t::x_iterator first;
495 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
498 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
501 _view = view_t(dims,
typename view_t::locator(first, row_size));
503 BOOST_ASSERT(_view.width() == dims.x);
504 BOOST_ASSERT(_view.height() == dims.y);
507 void create_view(point_t
const& dims, std::false_type)
509 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
515 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
516 , get_row_size_in_memunits( dims.x )
520 BOOST_ASSERT(_view.width() == dims.x);
521 BOOST_ASSERT(_view.height() == dims.y);
525 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
531 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
532 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2)
534 if ((
void*)(&im1)==(
void*)(&im2))
return true;
538 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
539 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
548 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
552 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
555 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
563 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
564 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
566 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
567 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
569 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
570 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
572 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
573 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};