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> 21 #include <type_traits> 23 namespace boost {
namespace gil {
39 template<
typename Pixel,
bool IsPlanar = false,
typename Alloc=std::allocator<
unsigned char> >
42 #if defined(BOOST_NO_CXX11_ALLOCATOR) 43 using allocator_type =
typename Alloc::template rebind<unsigned char>::other;
45 using allocator_type =
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
48 using const_view_t =
typename view_t::const_t;
49 using point_t =
typename view_t::point_t;
50 using coord_t =
typename view_t::coord_t;
51 using value_type =
typename view_t::value_type;
52 using x_coord_t = coord_t;
53 using y_coord_t = coord_t;
55 const point_t& dimensions()
const {
return _view.dimensions(); }
56 x_coord_t width()
const {
return _view.width(); }
57 y_coord_t height()
const {
return _view.height(); }
59 explicit image(std::size_t alignment=0,
60 const Alloc alloc_in = Alloc()) :
61 _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
64 image(
const point_t& dimensions,
65 std::size_t alignment=0,
66 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
67 , _allocated_bytes( 0 ) {
68 allocate_and_default_construct(dimensions);
71 image(x_coord_t width, y_coord_t height,
72 std::size_t alignment=0,
73 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
74 , _allocated_bytes( 0 ) {
75 allocate_and_default_construct(point_t(width,height));
78 image(
const point_t& dimensions,
80 std::size_t alignment,
81 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
82 , _allocated_bytes( 0 ) {
83 allocate_and_fill(dimensions, p_in);
85 image(x_coord_t width, y_coord_t height,
87 std::size_t alignment = 0,
88 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
89 , _allocated_bytes ( 0 ) {
90 allocate_and_fill(point_t(width,height),p_in);
93 image(
const image& img) : _memory(
nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
94 , _allocated_bytes( img._allocated_bytes ) {
95 allocate_and_copy(img.dimensions(),img._view);
98 template <
typename P2,
bool IP2,
typename Alloc2>
100 , _allocated_bytes( img._allocated_bytes ) {
101 allocate_and_copy(img.dimensions(),img._view);
105 if (dimensions() == img.dimensions())
114 template <
typename Img>
115 image& operator=(
const Img& img) {
116 if (dimensions() == img.dimensions())
130 Alloc& allocator() {
return _alloc; }
131 Alloc
const& allocator()
const {
return _alloc; }
133 void swap(
image& img) {
135 swap(_align_in_bytes, img._align_in_bytes);
136 swap(_memory, img._memory);
137 swap(_view, img._view);
138 swap(_alloc, img._alloc);
139 swap(_allocated_bytes, img._allocated_bytes );
147 void recreate(
const point_t& dims, std::size_t alignment = 0)
149 if (dims == _view.dimensions() && _align_in_bytes == alignment)
152 _align_in_bytes = alignment;
154 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
157 create_view(dims, std::integral_constant<bool, IsPlanar>());
162 image tmp(dims, alignment);
167 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
169 recreate(point_t(width, height), alignment);
172 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0)
174 if (dims == _view.dimensions() && _align_in_bytes == alignment)
177 _align_in_bytes = alignment;
179 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
182 create_view(dims,
typename std::integral_constant<bool, IsPlanar>());
187 image tmp(dims, p_in, alignment);
192 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
194 recreate( point_t( width, height ), p_in, alignment );
198 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in)
200 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
203 _align_in_bytes = alignment;
205 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
208 create_view(dims, std::integral_constant<bool, IsPlanar>());
213 image tmp(dims, alignment, alloc_in);
218 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in)
220 recreate(point_t(width, height), alignment, alloc_in);
223 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in)
225 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
228 _align_in_bytes = alignment;
230 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
233 create_view(dims, std::integral_constant<bool, IsPlanar>());
238 image tmp(dims, p_in, alignment, alloc_in);
243 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
245 recreate(point_t(width, height), p_in, alignment, alloc_in);
250 unsigned char* _memory;
251 std::size_t _align_in_bytes;
252 allocator_type _alloc;
254 std::size_t _allocated_bytes;
256 void allocate_and_default_construct(point_t
const& dimensions)
260 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
263 catch (...) { deallocate();
throw; }
266 void allocate_and_fill(
const point_t& dimensions, Pixel
const& p_in)
270 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
273 catch(...) { deallocate();
throw; }
276 template <
typename View>
277 void allocate_and_copy(
const point_t& dimensions, View
const& v)
281 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
284 catch(...) { deallocate();
throw; }
289 if (_memory && _allocated_bytes > 0)
290 _alloc.deallocate(_memory, _allocated_bytes);
293 std::size_t is_planar_impl(
294 std::size_t
const size_in_units,
295 std::size_t
const channels_in_image,
296 std::true_type)
const 298 return size_in_units * channels_in_image;
301 std::size_t is_planar_impl(
302 std::size_t
const size_in_units,
304 std::false_type)
const 306 return size_in_units;
309 std::size_t total_allocated_size_in_bytes(point_t
const& dimensions)
const 311 using x_iterator =
typename view_t::x_iterator;
314 constexpr std::size_t _channels_in_image =
317 is_pixel<value_type>::value,
319 std::integral_constant<std::size_t, 1>
322 std::size_t size_in_units = is_planar_impl(
323 get_row_size_in_memunits(dimensions.x) * dimensions.y,
325 std::integral_constant<bool, IsPlanar>());
330 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
333 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
334 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
335 if (_align_in_bytes>0) {
337 return align(size_in_memunits, alignment_in_memunits);
339 return size_in_memunits;
342 void allocate_(point_t
const& dimensions, std::false_type)
345 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
346 _memory=_alloc.allocate( _allocated_bytes );
348 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
349 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp), get_row_size_in_memunits(dimensions.x)));
351 BOOST_ASSERT(_view.width() == dimensions.x);
352 BOOST_ASSERT(_view.height() == dimensions.y);
355 void allocate_(point_t
const& dimensions, std::true_type)
358 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
359 std::size_t plane_size=row_size*dimensions.y;
361 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
363 _memory = _alloc.allocate( _allocated_bytes );
365 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
366 typename view_t::x_iterator first;
367 for (
int i=0; i<num_channels<view_t>::value; ++i) {
369 memunit_advance(dynamic_at_c(first,i), plane_size*i);
371 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
373 BOOST_ASSERT(_view.width() == dimensions.x);
374 BOOST_ASSERT(_view.height() == dimensions.y);
377 void create_view(point_t
const& dims, std::true_type)
379 std::size_t row_size=get_row_size_in_memunits(dims.x);
380 std::size_t plane_size=row_size*dims.y;
382 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
386 typename view_t::x_iterator first;
388 for (
int i = 0; i < num_channels< view_t >::value; ++i )
392 memunit_advance( dynamic_at_c(first,i)
397 _view = view_t(dims,
typename view_t::locator(first, row_size));
399 BOOST_ASSERT(_view.width() == dims.x);
400 BOOST_ASSERT(_view.height() == dims.y);
403 void create_view(point_t
const& dims, std::false_type)
405 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
411 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
412 , get_row_size_in_memunits( dims.x )
416 BOOST_ASSERT(_view.width() == dims.x);
417 BOOST_ASSERT(_view.height() == dims.y);
421 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
426 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
427 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
428 if ((
void*)(&im1)==(
void*)(&im2))
return true;
432 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
433 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
442 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 446 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 448 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
456 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
457 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
459 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
460 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
462 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
463 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
465 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
466 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};
Definition: pixel_iterator.hpp:124
void default_construct_pixels(View const &view)
Invokes the in-place default constructor on every pixel of the (uninitialized) view....
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....
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:40
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:577
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:529
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:443
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:447
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