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 point_t
const& 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(point_t
const& 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(point_t
const& 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 #ifdef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE
242 swap(_alloc, img._alloc);
244 if constexpr (std::allocator_traits<Alloc>::propagate_on_container_swap::value)
245 swap(_alloc, img._alloc);
247 BOOST_ASSERT(_alloc == img._alloc);
249 swap(_allocated_bytes, img._allocated_bytes );
257 void recreate(point_t
const& dims, std::size_t alignment = 0)
259 if (dims == _view.dimensions() && _align_in_bytes == alignment)
262 _align_in_bytes = alignment;
264 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
267 create_view(dims, std::integral_constant<bool, IsPlanar>());
272 image tmp(dims, alignment);
277 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
279 recreate(point_t(width, height), alignment);
282 void recreate(point_t
const& dims,
const Pixel& p_in, std::size_t alignment = 0)
284 if (dims == _view.dimensions() && _align_in_bytes == alignment)
287 _align_in_bytes = alignment;
289 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
292 create_view(dims,
typename std::integral_constant<bool, IsPlanar>());
297 image tmp(dims, p_in, alignment);
302 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
304 recreate( point_t( width, height ), p_in, alignment );
308 void recreate(point_t
const& dims, std::size_t alignment,
const Alloc alloc_in)
310 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
313 _align_in_bytes = alignment;
315 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
318 create_view(dims, std::integral_constant<bool, IsPlanar>());
323 image tmp(dims, alignment, alloc_in);
328 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in)
330 recreate(point_t(width, height), alignment, alloc_in);
333 void recreate(point_t
const& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in)
335 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
338 _align_in_bytes = alignment;
340 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
343 create_view(dims, std::integral_constant<bool, IsPlanar>());
348 image tmp(dims, p_in, alignment, alloc_in);
353 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
355 recreate(point_t(width, height), p_in, alignment, alloc_in);
361 template <
typename P2,
bool IP2,
typename Alloc2>
friend class image;
363 unsigned char* _memory;
364 std::size_t _align_in_bytes;
365 allocator_type _alloc;
367 std::size_t _allocated_bytes;
369 void allocate_and_default_construct(point_t
const& dimensions)
373 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
376 catch (...) { deallocate();
throw; }
379 void allocate_and_fill(point_t
const& dimensions, Pixel
const& p_in)
383 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
386 catch(...) { deallocate();
throw; }
389 template <
typename View>
390 void allocate_and_copy(point_t
const& dimensions, View
const& v)
394 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
397 catch(...) { deallocate();
throw; }
402 if (_memory && _allocated_bytes > 0)
403 _alloc.deallocate(_memory, _allocated_bytes);
406 std::size_t is_planar_impl(
407 std::size_t
const size_in_units,
408 std::size_t
const channels_in_image,
409 std::true_type)
const
411 return size_in_units * channels_in_image;
414 std::size_t is_planar_impl(
415 std::size_t
const size_in_units,
417 std::false_type)
const
419 return size_in_units;
422 std::size_t total_allocated_size_in_bytes(point_t
const& dimensions)
const
424 using x_iterator =
typename view_t::x_iterator;
427 constexpr std::size_t _channels_in_image =
430 is_pixel<value_type>::value,
432 std::integral_constant<std::size_t, 1>
435 std::size_t size_in_units = is_planar_impl(
436 get_row_size_in_memunits(dimensions.x) * dimensions.y,
438 std::integral_constant<bool, IsPlanar>());
443 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
446 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
447 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
448 if (_align_in_bytes>0) {
450 return align(size_in_memunits, alignment_in_memunits);
452 return size_in_memunits;
455 void allocate_(point_t
const& dimensions, std::false_type)
458 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
459 if (_allocated_bytes == 0)
464 _memory=_alloc.allocate( _allocated_bytes );
466 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
467 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp), get_row_size_in_memunits(dimensions.x)));
469 BOOST_ASSERT(_view.width() == dimensions.x);
470 BOOST_ASSERT(_view.height() == dimensions.y);
473 void allocate_(point_t
const& dimensions, std::true_type)
476 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
477 std::size_t plane_size=row_size*dimensions.y;
479 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
480 if (_allocated_bytes == 0)
485 _memory = _alloc.allocate( _allocated_bytes );
487 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
488 typename view_t::x_iterator first;
489 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
492 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
494 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
496 BOOST_ASSERT(_view.width() == dimensions.x);
497 BOOST_ASSERT(_view.height() == dimensions.y);
500 void create_view(point_t
const& dims, std::true_type)
502 std::size_t row_size=get_row_size_in_memunits(dims.x);
503 std::size_t plane_size=row_size*dims.y;
505 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
509 typename view_t::x_iterator first;
511 for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
514 memunit_advance(dynamic_at_c(first, i),
static_cast<std::ptrdiff_t
>(plane_size * i));
517 _view = view_t(dims,
typename view_t::locator(first, row_size));
519 BOOST_ASSERT(_view.width() == dims.x);
520 BOOST_ASSERT(_view.height() == dims.y);
523 void create_view(point_t
const& dims, std::false_type)
525 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
531 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
532 , get_row_size_in_memunits( dims.x )
536 BOOST_ASSERT(_view.width() == dims.x);
537 BOOST_ASSERT(_view.height() == dims.y);
541 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
547 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
548 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2)
550 if ((
void*)(&im1)==(
void*)(&im2))
return true;
554 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
555 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
564 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
566 ->
typename image<Pixel,IsPlanar,Alloc>::view_t
const&
572 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
574 ->
typename image<Pixel,IsPlanar,Alloc>::const_view_t
const
576 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
584 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
585 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
587 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
588 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
590 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
591 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
593 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
594 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,...
Definition: image_view.hpp:54
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition: image.hpp:43
auto const_view(const image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::const_view_t const
Returns the constant-pixel view of an image.
Definition: image.hpp:573
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:292
void default_construct_pixels(View const &view)
Invokes the in-place default constructor on every pixel of the (uninitialized) view....
Definition: algorithm.hpp:724
BOOST_FORCEINLINE void destruct_pixels(View const &view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:518
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:1109
void uninitialized_copy_pixels(View1 const &view1, View2 const &view2)
std::uninitialized_copy for image views. Does not support planar heterogeneous views....
Definition: algorithm.hpp:823
void uninitialized_fill_pixels(const View &view, const Value &val)
std::uninitialized_fill for image views. Does not support planar heterogeneous views....
Definition: algorithm.hpp:587
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:583
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Definition: pixel_iterator.hpp:124
Definition: color_convert.hpp:31
Returns the number of channels of a pixel-based GIL construct.
Definition: pixel.hpp:54
Returns the type of a view the pixel type, whether it operates on planar data and whether it has a st...
Definition: metafunctions.hpp:557