8 #ifndef BOOST_GIL_IMAGE_HPP
9 #define BOOST_GIL_IMAGE_HPP
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>
16 #include <boost/assert.hpp>
17 #include <boost/core/exchange.hpp>
22 #include <type_traits>
24 namespace boost {
namespace gil {
40 template<
typename Pixel,
bool IsPlanar = false,
typename Alloc=std::allocator<
unsigned char> >
44 #if defined(BOOST_NO_CXX11_ALLOCATOR)
45 using allocator_type =
typename Alloc::template rebind<unsigned char>::other;
47 using allocator_type =
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
49 using view_t =
typename view_type_from_pixel<Pixel, IsPlanar>::type;
50 using const_view_t =
typename view_t::const_t;
51 using point_t =
typename view_t::point_t;
52 using coord_t =
typename view_t::coord_t;
53 using value_type =
typename view_t::value_type;
54 using x_coord_t = coord_t;
55 using y_coord_t = coord_t;
57 const point_t& dimensions()
const {
return _view.dimensions(); }
58 x_coord_t width()
const {
return _view.width(); }
59 y_coord_t height()
const {
return _view.height(); }
61 explicit image(std::size_t alignment=0,
62 const Alloc alloc_in = Alloc()) :
63 _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
66 image(
const point_t& dimensions,
67 std::size_t alignment=0,
68 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
69 , _allocated_bytes( 0 )
71 allocate_and_default_construct(dimensions);
74 image(x_coord_t width, y_coord_t height,
75 std::size_t alignment=0,
76 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
77 , _allocated_bytes( 0 )
79 allocate_and_default_construct(point_t(width,height));
82 image(
const point_t& dimensions,
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 )
88 allocate_and_fill(dimensions, p_in);
91 image(x_coord_t width, y_coord_t height,
93 std::size_t alignment = 0,
94 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
95 , _allocated_bytes ( 0 )
97 allocate_and_fill(point_t(width,height),p_in);
100 image(
const image& img) : _memory(
nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
101 , _allocated_bytes( img._allocated_bytes )
103 allocate_and_copy(img.dimensions(),img._view);
106 template <
typename P2,
bool IP2,
typename Alloc2>
108 , _allocated_bytes( img._allocated_bytes )
110 allocate_and_copy(img.dimensions(),img._view);
113 template <
typename Loc,
114 typename std::enable_if<pixels_are_compatible<typename Loc::value_type, Pixel>::value,
int>::type = 0>
116 std::size_t alignment = 0,
117 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
118 , _allocated_bytes( 0 )
120 allocate_and_copy(
view.dimensions(),
view);
126 _memory(img._memory),
127 _align_in_bytes(img._align_in_bytes),
128 _alloc(std::move(img._alloc)),
129 _allocated_bytes(img._allocated_bytes)
131 img._view = view_t();
132 img._memory =
nullptr;
133 img._align_in_bytes = 0;
134 img._allocated_bytes = 0;
139 if (dimensions() == img.dimensions())
149 template <
typename Img>
150 image& operator=(
const Img& img)
152 if (dimensions() == img.dimensions())
163 using propagate_allocators = std::true_type;
164 using no_propagate_allocators = std::false_type;
166 template <
class Alloc2>
167 using choose_pocma =
typename std::conditional<
169 std::is_empty<Alloc2>::value,
171 typename std::allocator_traits<Alloc2>::propagate_on_container_move_assignment::type
174 static void exchange_memory(
image& lhs,
image& rhs)
176 lhs._memory = boost::exchange(rhs._memory,
nullptr);
177 lhs._align_in_bytes = boost::exchange(rhs._align_in_bytes, 0);
178 lhs._allocated_bytes = boost::exchange(rhs._allocated_bytes, 0);
179 lhs._view = boost::exchange(rhs._view, image::view_t{});
182 void move_assign(
image& img, propagate_allocators) noexcept {
186 this->_alloc = img._alloc;
187 exchange_memory(*
this, img);
190 void move_assign(
image& img, no_propagate_allocators) {
191 if (_alloc == img._alloc) {
195 exchange_memory(*
this, img);
200 allocate_and_copy(img.dimensions(), img._view);
203 img._view = image::view_t{};
209 this->_view = view_t{};
218 if (
this != std::addressof(img))
220 move_assign(img, choose_pocma<allocator_type>{});
231 Alloc& allocator() {
return _alloc; }
232 Alloc
const& allocator()
const {
return _alloc; }
234 void swap(
image& img)
237 swap(_align_in_bytes, img._align_in_bytes);
238 swap(_memory, img._memory);
239 swap(_view, img._view);
240 swap(_alloc, img._alloc);
241 swap(_allocated_bytes, img._allocated_bytes );
249 void recreate(
const point_t& dims, std::size_t alignment = 0)
251 if (dims == _view.dimensions() && _align_in_bytes == alignment)
254 _align_in_bytes = alignment;
256 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
259 create_view(dims, std::integral_constant<bool, IsPlanar>());
264 image tmp(dims, alignment);
269 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
271 recreate(point_t(width, height), alignment);
274 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0)
276 if (dims == _view.dimensions() && _align_in_bytes == alignment)
279 _align_in_bytes = alignment;
281 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
284 create_view(dims,
typename std::integral_constant<bool, IsPlanar>());
289 image tmp(dims, p_in, alignment);
294 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
296 recreate( point_t( width, height ), p_in, alignment );
300 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in)
302 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
305 _align_in_bytes = alignment;
307 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
310 create_view(dims, std::integral_constant<bool, IsPlanar>());
315 image tmp(dims, alignment, alloc_in);
320 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in)
322 recreate(point_t(width, height), alignment, alloc_in);
325 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in)
327 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
330 _align_in_bytes = alignment;
332 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
335 create_view(dims, std::integral_constant<bool, IsPlanar>());
340 image tmp(dims, p_in, alignment, alloc_in);
345 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
347 recreate(point_t(width, height), p_in, alignment, alloc_in);
353 template <
typename P2,
bool IP2,
typename Alloc2>
friend class image;
355 unsigned char* _memory;
356 std::size_t _align_in_bytes;
357 allocator_type _alloc;
359 std::size_t _allocated_bytes;
361 void allocate_and_default_construct(point_t
const& dimensions)
365 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
368 catch (...) { deallocate();
throw; }
371 void allocate_and_fill(
const point_t& dimensions, Pixel
const& p_in)
375 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
378 catch(...) { deallocate();
throw; }
381 template <
typename View>
382 void allocate_and_copy(
const point_t& dimensions, View
const& v)
386 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
389 catch(...) { deallocate();
throw; }
394 if (_memory && _allocated_bytes > 0)
395 _alloc.deallocate(_memory, _allocated_bytes);
398 std::size_t is_planar_impl(
399 std::size_t
const size_in_units,
400 std::size_t
const channels_in_image,
401 std::true_type)
const
403 return size_in_units * channels_in_image;
406 std::size_t is_planar_impl(
407 std::size_t
const size_in_units,
409 std::false_type)
const
411 return size_in_units;
414 std::size_t total_allocated_size_in_bytes(point_t
const& dimensions)
const
416 using x_iterator =
typename view_t::x_iterator;
419 constexpr std::size_t _channels_in_image =
422 is_pixel<value_type>::value,
424 std::integral_constant<std::size_t, 1>
427 std::size_t size_in_units = is_planar_impl(
428 get_row_size_in_memunits(dimensions.x) * dimensions.y,
430 std::integral_constant<bool, IsPlanar>());
435 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
438 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
439 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
440 if (_align_in_bytes>0) {
442 return align(size_in_memunits, alignment_in_memunits);
444 return size_in_memunits;
447 void allocate_(point_t
const& dimensions, std::false_type)
450 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
451 _memory=_alloc.allocate( _allocated_bytes );
453 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
454 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp), get_row_size_in_memunits(dimensions.x)));
456 BOOST_ASSERT(_view.width() == dimensions.x);
457 BOOST_ASSERT(_view.height() == dimensions.y);
460 void allocate_(point_t
const& dimensions, std::true_type)
463 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
464 std::size_t plane_size=row_size*dimensions.y;
466 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
468 _memory = _alloc.allocate( _allocated_bytes );
470 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
471 typename view_t::x_iterator first;
472 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
475 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
477 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
479 BOOST_ASSERT(_view.width() == dimensions.x);
480 BOOST_ASSERT(_view.height() == dimensions.y);
483 void create_view(point_t
const& dims, std::true_type)
485 std::size_t row_size=get_row_size_in_memunits(dims.x);
486 std::size_t plane_size=row_size*dims.y;
488 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
492 typename view_t::x_iterator first;
494 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
497 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
500 _view = view_t(dims,
typename view_t::locator(first, row_size));
502 BOOST_ASSERT(_view.width() == dims.x);
503 BOOST_ASSERT(_view.height() == dims.y);
506 void create_view(point_t
const& dims, std::false_type)
508 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
514 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
515 , get_row_size_in_memunits( dims.x )
519 BOOST_ASSERT(_view.width() == dims.x);
520 BOOST_ASSERT(_view.height() == dims.y);
524 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
530 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
531 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2)
533 if ((
void*)(&im1)==(
void*)(&im2))
return true;
537 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
538 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
547 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
551 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
554 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
562 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
563 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
565 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
566 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
568 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
569 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
571 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
572 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};