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> 18 #include <type_traits> 20 namespace boost {
namespace gil {
36 template<
typename Pixel,
bool IsPlanar = false,
typename Alloc=std::allocator<
unsigned char> >
39 #if defined(BOOST_NO_CXX11_ALLOCATOR) 40 using allocator_type =
typename Alloc::template rebind<unsigned char>::other;
42 using allocator_type =
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
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;
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(); }
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 ) {}
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);
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));
75 image(
const point_t& dimensions,
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);
82 image(x_coord_t width, y_coord_t height,
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);
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);
95 template <
typename P2,
bool IP2,
typename Alloc2>
97 , _allocated_bytes( img._allocated_bytes ) {
98 allocate_and_copy(img.dimensions(),img._view);
101 image& operator=(
const image& img) {
102 if (dimensions() == img.dimensions())
111 template <
typename Img>
112 image& operator=(
const Img& img) {
113 if (dimensions() == img.dimensions())
127 Alloc& allocator() {
return _alloc; }
128 Alloc
const& allocator()
const {
return _alloc; }
130 void swap(image& img) {
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 );
144 void recreate(
const point_t& dims, std::size_t alignment = 0)
146 if (dims == _view.dimensions() && _align_in_bytes == alignment)
149 _align_in_bytes = alignment;
151 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
154 create_view(dims, std::integral_constant<bool, IsPlanar>());
159 image tmp(dims, alignment);
164 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
166 recreate(point_t(width, height), alignment);
169 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0)
171 if (dims == _view.dimensions() && _align_in_bytes == alignment)
174 _align_in_bytes = alignment;
176 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
179 create_view(dims,
typename std::integral_constant<bool, IsPlanar>());
184 image tmp(dims, p_in, alignment);
189 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
191 recreate( point_t( width, height ), p_in, alignment );
195 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in)
197 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
200 _align_in_bytes = alignment;
202 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
205 create_view(dims, std::integral_constant<bool, IsPlanar>());
210 image tmp(dims, alignment, alloc_in);
215 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in)
217 recreate(point_t(width, height), alignment, alloc_in);
220 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in)
222 if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
225 _align_in_bytes = alignment;
227 if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
230 create_view(dims, std::integral_constant<bool, IsPlanar>());
235 image tmp(dims, p_in, alignment, alloc_in);
240 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
242 recreate(point_t(width, height), p_in, alignment, alloc_in);
247 unsigned char* _memory;
248 std::size_t _align_in_bytes;
249 allocator_type _alloc;
251 std::size_t _allocated_bytes;
253 void allocate_and_default_construct(point_t
const& dimensions)
257 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
260 catch (...) { deallocate();
throw; }
263 void allocate_and_fill(
const point_t& dimensions, Pixel
const& p_in)
267 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
270 catch(...) { deallocate();
throw; }
273 template <
typename View>
274 void allocate_and_copy(
const point_t& dimensions, View
const& v)
278 allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
281 catch(...) { deallocate();
throw; }
286 if (_memory && _allocated_bytes > 0)
287 _alloc.deallocate(_memory, _allocated_bytes);
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 295 return size_in_units * channels_in_image;
298 std::size_t is_planar_impl(
299 std::size_t
const size_in_units,
301 std::false_type)
const 303 return size_in_units;
306 std::size_t total_allocated_size_in_bytes(point_t
const& dimensions)
const 308 using x_iterator =
typename view_t::x_iterator;
311 constexpr std::size_t _channels_in_image =
314 is_pixel<value_type>::value,
316 std::integral_constant<std::size_t, 1>
319 std::size_t size_in_units = is_planar_impl(
320 get_row_size_in_memunits(dimensions.x) * dimensions.y,
322 std::integral_constant<bool, IsPlanar>());
327 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
330 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
331 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
332 if (_align_in_bytes>0) {
334 return align(size_in_memunits, alignment_in_memunits);
336 return size_in_memunits;
339 void allocate_(point_t
const& dimensions, std::false_type)
342 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
343 _memory=_alloc.allocate( _allocated_bytes );
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)));
349 void allocate_(point_t
const& dimensions, std::true_type)
352 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
353 std::size_t plane_size=row_size*dimensions.y;
355 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
357 _memory = _alloc.allocate( _allocated_bytes );
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) {
363 memunit_advance(dynamic_at_c(first,i), plane_size*i);
365 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
368 void create_view(point_t
const& dims, std::true_type)
370 std::size_t row_size=get_row_size_in_memunits(dims.x);
371 std::size_t plane_size=row_size*dims.y;
373 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
377 typename view_t::x_iterator first;
379 for (
int i = 0; i < num_channels< view_t >::value; ++i )
383 memunit_advance( dynamic_at_c(first,i)
389 ,
typename view_t::locator( first
395 void create_view(point_t
const& dims, std::false_type)
397 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
403 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
404 , get_row_size_in_memunits( dims.x )
410 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
415 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
417 if ((
void*)(&im1)==(
void*)(&im2))
return true;
421 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
431 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 435 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 437 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
445 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
448 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
449 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
451 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
452 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
454 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
455 struct is_planar<image<Pixel, IsPlanar, Alloc>> : std::integral_constant<bool, IsPlanar> {};
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