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> 15 #include <boost/mpl/arithmetic.hpp> 16 #include <boost/mpl/bool.hpp> 17 #include <boost/mpl/if.hpp> 22 namespace boost {
namespace gil {
38 template<
typename Pixel,
bool IsPlanar = false,
typename Alloc=std::allocator<
unsigned char> >
41 #if defined(BOOST_NO_CXX11_ALLOCATOR) 42 using allocator_type =
typename Alloc::template rebind<unsigned char>::other;
44 using allocator_type =
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
47 using const_view_t =
typename view_t::const_t;
48 using point_t =
typename view_t::point_t;
49 using coord_t =
typename view_t::coord_t;
50 using value_type =
typename view_t::value_type;
51 using x_coord_t = coord_t;
52 using y_coord_t = coord_t;
54 const point_t& dimensions()
const {
return _view.dimensions(); }
55 x_coord_t width()
const {
return _view.width(); }
56 y_coord_t height()
const {
return _view.height(); }
58 explicit image(std::size_t alignment=0,
59 const Alloc alloc_in = Alloc()) :
60 _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
63 image(
const point_t& dimensions,
64 std::size_t alignment=0,
65 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
66 , _allocated_bytes( 0 ) {
67 allocate_and_default_construct(dimensions);
70 image(x_coord_t width, y_coord_t height,
71 std::size_t alignment=0,
72 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
73 , _allocated_bytes( 0 ) {
74 allocate_and_default_construct(point_t(width,height));
77 image(
const point_t& dimensions,
79 std::size_t alignment,
80 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
81 , _allocated_bytes( 0 ) {
82 allocate_and_fill(dimensions, p_in);
84 image(x_coord_t width, y_coord_t height,
86 std::size_t alignment = 0,
87 const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
88 , _allocated_bytes ( 0 ) {
89 allocate_and_fill(point_t(width,height),p_in);
92 image(
const image& img) : _memory(
nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
93 , _allocated_bytes( img._allocated_bytes ) {
94 allocate_and_copy(img.dimensions(),img._view);
97 template <
typename P2,
bool IP2,
typename Alloc2>
99 , _allocated_bytes( img._allocated_bytes ) {
100 allocate_and_copy(img.dimensions(),img._view);
103 image& operator=(
const image& img) {
104 if (dimensions() == img.dimensions())
113 template <
typename Img>
114 image& operator=(
const Img& img) {
115 if (dimensions() == img.dimensions())
129 Alloc& allocator() {
return _alloc; }
130 Alloc
const& allocator()
const {
return _alloc; }
132 void swap(image& img) {
134 swap(_align_in_bytes, img._align_in_bytes);
135 swap(_memory, img._memory);
136 swap(_view, img._view);
137 swap(_alloc, img._alloc);
138 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 )
154 _align_in_bytes = alignment;
156 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
161 ,
typename mpl::bool_<IsPlanar>()
168 image tmp( dims, alignment );
173 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment = 0 )
175 recreate( point_t( width, height ), alignment );
179 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0 )
181 if( dims == _view.dimensions() && _align_in_bytes == alignment )
186 _align_in_bytes = alignment;
188 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
193 ,
typename mpl::bool_<IsPlanar>()
200 image tmp( dims, p_in, alignment );
205 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
207 recreate( point_t( width, height ), p_in, alignment );
212 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in )
214 if( dims == _view.dimensions()
215 && _align_in_bytes == alignment
216 && alloc_in == _alloc
222 _align_in_bytes = alignment;
224 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
229 ,
typename mpl::bool_<IsPlanar>()
236 image tmp( dims, alignment, alloc_in );
241 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in )
243 recreate( point_t( width, height ), alignment, alloc_in );
246 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
248 if( dims == _view.dimensions()
249 && _align_in_bytes == alignment
250 && alloc_in == _alloc
256 _align_in_bytes = alignment;
258 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
263 ,
typename mpl::bool_<IsPlanar>()
270 image tmp( dims, p_in, alignment, alloc_in );
275 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
277 recreate( point_t( width, height ), p_in,alignment, alloc_in );
284 unsigned char* _memory;
285 std::size_t _align_in_bytes;
286 allocator_type _alloc;
288 std::size_t _allocated_bytes;
291 void allocate_and_default_construct(
const point_t& dimensions) {
293 allocate_(dimensions,mpl::bool_<IsPlanar>());
295 }
catch(...) { deallocate();
throw; }
298 void allocate_and_fill(
const point_t& dimensions,
const Pixel& p_in) {
300 allocate_(dimensions,mpl::bool_<IsPlanar>());
302 }
catch(...) { deallocate();
throw; }
305 template <
typename View>
306 void allocate_and_copy(
const point_t& dimensions,
const View& v) {
308 allocate_(dimensions,mpl::bool_<IsPlanar>());
310 }
catch(...) { deallocate();
throw; }
314 if (_memory && _allocated_bytes > 0 )
316 _alloc.deallocate(_memory, _allocated_bytes );
320 std::size_t is_planar_impl(
const std::size_t size_in_units
321 ,
const std::size_t channels_in_image
325 return size_in_units * channels_in_image;
328 std::size_t is_planar_impl(
const std::size_t size_in_units
333 return size_in_units;
336 std::size_t total_allocated_size_in_bytes(
const point_t& dimensions)
const {
338 using x_iterator =
typename view_t::x_iterator;
341 const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
346 std::size_t size_in_units = is_planar_impl( get_row_size_in_memunits( dimensions.x ) * dimensions.y
348 ,
typename mpl::bool_<IsPlanar>()
354 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
357 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
358 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
359 if (_align_in_bytes>0) {
361 return align(size_in_memunits, alignment_in_memunits);
363 return size_in_memunits;
366 void allocate_(
const point_t& dimensions, mpl::false_) {
368 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
369 _memory=_alloc.allocate( _allocated_bytes );
371 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
372 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
375 void allocate_(
const point_t& dimensions, mpl::true_) {
376 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
377 std::size_t plane_size=row_size*dimensions.y;
379 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
381 _memory = _alloc.allocate( _allocated_bytes );
383 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
384 typename view_t::x_iterator first;
385 for (
int i=0; i<num_channels<view_t>::value; ++i) {
387 memunit_advance(dynamic_at_c(first,i), plane_size*i);
389 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
392 void create_view(
const point_t& dims
396 std::size_t row_size=get_row_size_in_memunits(dims.x);
397 std::size_t plane_size=row_size*dims.y;
399 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
403 typename view_t::x_iterator first;
405 for (
int i = 0; i < num_channels< view_t >::value; ++i )
409 memunit_advance( dynamic_at_c(first,i)
415 ,
typename view_t::locator( first
421 void create_view(
const point_t& dims
425 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
431 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
432 , get_row_size_in_memunits( dims.x )
438 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
443 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
445 if ((
void*)(&im1)==(
void*)(&im2))
return true;
449 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
459 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 463 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline 465 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
473 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
476 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
477 struct color_space_type<image<Pixel,IsPlanar,Alloc> > :
public color_space_type<Pixel> {};
479 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
480 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > :
public channel_mapping_type<Pixel> {};
482 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
483 struct is_planar<image<Pixel,IsPlanar,Alloc> > :
public mpl::bool_<IsPlanar> {};
Definition: pixel_iterator.hpp:110
void uninitialized_fill_pixels(const View &img_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:537
Definition: algorithm.hpp:30
void swap(const boost::gil::packed_channel_reference< BF, FB, NB, M > x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:485
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:951
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:270
void default_construct_pixels(const View &img_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:666
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition: image.hpp:39
Definition: color_convert.hpp:30
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:460
void uninitialized_copy_pixels(const View1 &view1, const View2 &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:719
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:464
Returns the number of channels of a pixel-based GIL construct.
Definition: locator.hpp:38
BOOST_FORCEINLINE void destruct_pixels(const View &img_view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:480
Returns the type of a view the pixel type, whether it operates on planar data and whether it has a st...
Definition: metafunctions.hpp:428