27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/if.hpp>
29 #include <boost/mpl/arithmetic.hpp>
37 namespace boost {
namespace gil {
58 template<
typename Pixel,
bool IsPlanar = false,
typename Alloc=std::allocator<
unsigned char> >
61 #if defined(BOOST_NO_CXX11_ALLOCATOR)
62 typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
64 typedef typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char> allocator_type;
68 typedef typename view_t::point_t point_t;
69 typedef typename view_t::coord_t coord_t;
70 typedef typename view_t::value_type value_type;
71 typedef coord_t x_coord_t;
72 typedef coord_t y_coord_t;
74 const point_t& dimensions()
const {
return _view.dimensions(); }
75 x_coord_t width()
const {
return _view.width(); }
76 y_coord_t height()
const {
return _view.height(); }
78 explicit image(std::size_t alignment=0,
79 const Alloc alloc_in = Alloc()) :
80 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
83 image(
const point_t& dimensions,
84 std::size_t alignment=0,
85 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
86 , _allocated_bytes( 0 ) {
87 allocate_and_default_construct(dimensions);
90 image(x_coord_t width, y_coord_t height,
91 std::size_t alignment=0,
92 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
93 , _allocated_bytes( 0 ) {
94 allocate_and_default_construct(point_t(width,height));
97 image(
const point_t& dimensions,
99 std::size_t alignment,
100 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
101 , _allocated_bytes( 0 ) {
102 allocate_and_fill(dimensions, p_in);
104 image(x_coord_t width, y_coord_t height,
106 std::size_t alignment = 0,
107 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
108 , _allocated_bytes ( 0 ) {
109 allocate_and_fill(point_t(width,height),p_in);
112 image(
const image& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
113 , _allocated_bytes( img._allocated_bytes ) {
114 allocate_and_copy(img.dimensions(),img._view);
117 template <
typename P2,
bool IP2,
typename Alloc2>
119 , _allocated_bytes( img._allocated_bytes ) {
120 allocate_and_copy(img.dimensions(),img._view);
123 image& operator=(
const image& img) {
124 if (dimensions() == img.dimensions())
133 template <
typename Img>
134 image& operator=(
const Img& img) {
135 if (dimensions() == img.dimensions())
149 Alloc& allocator() {
return _alloc; }
150 Alloc
const& allocator()
const {
return _alloc; }
152 void swap(image& img) {
154 swap(_align_in_bytes, img._align_in_bytes);
155 swap(_memory, img._memory);
156 swap(_view, img._view);
157 swap(_alloc, img._alloc);
158 swap(_allocated_bytes, img._allocated_bytes );
167 void recreate(
const point_t& dims, std::size_t alignment = 0 )
169 if( dims == _view.dimensions() && _align_in_bytes == alignment )
174 _align_in_bytes = alignment;
176 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
181 ,
typename mpl::bool_<IsPlanar>()
188 image tmp( dims, alignment );
193 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment = 0 )
195 recreate( point_t( width, height ), alignment );
199 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0 )
201 if( dims == _view.dimensions() && _align_in_bytes == alignment )
206 _align_in_bytes = alignment;
208 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
213 ,
typename mpl::bool_<IsPlanar>()
220 image tmp( dims, p_in, alignment );
225 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
227 recreate( point_t( width, height ), p_in, alignment );
232 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in )
234 if( dims == _view.dimensions()
235 && _align_in_bytes == alignment
236 && alloc_in == _alloc
242 _align_in_bytes = alignment;
244 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
249 ,
typename mpl::bool_<IsPlanar>()
256 image tmp( dims, alignment, alloc_in );
261 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in )
263 recreate( point_t( width, height ), alignment, alloc_in );
266 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
268 if( dims == _view.dimensions()
269 && _align_in_bytes == alignment
270 && alloc_in == _alloc
276 _align_in_bytes = alignment;
278 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
283 ,
typename mpl::bool_<IsPlanar>()
290 image tmp( dims, p_in, alignment, alloc_in );
295 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
297 recreate( point_t( width, height ), p_in,alignment, alloc_in );
304 unsigned char* _memory;
305 std::size_t _align_in_bytes;
306 allocator_type _alloc;
308 std::size_t _allocated_bytes;
311 void allocate_and_default_construct(
const point_t& dimensions) {
313 allocate_(dimensions,mpl::bool_<IsPlanar>());
315 }
catch(...) { deallocate();
throw; }
318 void allocate_and_fill(
const point_t& dimensions,
const Pixel& p_in) {
320 allocate_(dimensions,mpl::bool_<IsPlanar>());
322 }
catch(...) { deallocate();
throw; }
325 template <
typename View>
326 void allocate_and_copy(
const point_t& dimensions,
const View& v) {
328 allocate_(dimensions,mpl::bool_<IsPlanar>());
330 }
catch(...) { deallocate();
throw; }
334 if (_memory && _allocated_bytes > 0 )
336 _alloc.deallocate(_memory, _allocated_bytes );
340 std::size_t is_planar_impl(
const std::size_t size_in_units
341 ,
const std::size_t channels_in_image
345 return size_in_units * channels_in_image;
348 std::size_t is_planar_impl(
const std::size_t size_in_units
353 return size_in_units;
356 std::size_t total_allocated_size_in_bytes(
const point_t& dimensions)
const {
358 typedef typename view_t::x_iterator x_iterator;
361 const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
366 std::size_t size_in_units = is_planar_impl( get_row_size_in_memunits( dimensions.x ) * dimensions.y
368 ,
typename mpl::bool_<IsPlanar>()
374 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
377 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
378 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
379 if (_align_in_bytes>0) {
381 return align(size_in_memunits, alignment_in_memunits);
383 return size_in_memunits;
386 void allocate_(
const point_t& dimensions, mpl::false_) {
388 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
389 _memory=_alloc.allocate( _allocated_bytes );
391 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
392 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
395 void allocate_(
const point_t& dimensions, mpl::true_) {
396 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
397 std::size_t plane_size=row_size*dimensions.y;
399 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
401 _memory = _alloc.allocate( _allocated_bytes );
403 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
404 typename view_t::x_iterator first;
405 for (
int i=0; i<num_channels<view_t>::value; ++i) {
406 dynamic_at_c(first,i) = (
typename channel_type<view_t>::type*)tmp;
407 memunit_advance(dynamic_at_c(first,i), plane_size*i);
409 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
412 void create_view(
const point_t& dims
416 std::size_t row_size=get_row_size_in_memunits(dims.x);
417 std::size_t plane_size=row_size*dims.y;
419 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
423 typename view_t::x_iterator first;
425 for (
int i = 0; i < num_channels< view_t >::value; ++i )
427 dynamic_at_c( first, i ) = (
typename channel_type<view_t>::type*) tmp;
429 memunit_advance( dynamic_at_c(first,i)
435 ,
typename view_t::locator( first
441 void create_view(
const point_t& dims
445 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
451 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
452 , get_row_size_in_memunits( dims.x )
458 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
463 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
464 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
465 if ((
void*)(&im1)==(
void*)(&im2))
return true;
469 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
470 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
479 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
483 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
493 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
494 struct channel_type<image<Pixel,IsPlanar,Alloc> > :
public channel_type<Pixel> {};
496 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
497 struct color_space_type<image<Pixel,IsPlanar,Alloc> > :
public color_space_type<Pixel> {};
499 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
500 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > :
public channel_mapping_type<Pixel> {};
502 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
503 struct is_planar<image<Pixel,IsPlanar,Alloc> > :
public mpl::bool_<IsPlanar> {};
Definition: pixel_iterator.hpp:126
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:541
Some basic STL-style algorithms when applied to image views.
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:68
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:956
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:289
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:671
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition: image.hpp:59
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:480
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:724
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:484
Returns the number of channels of a pixel-based GIL construct.
Definition: gil_concept.hpp:66
BOOST_FORCEINLINE void destruct_pixels(const View &img_view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:484
Returns the type of a view the pixel type, whether it operates on planar data and whether it has a st...
Definition: metafunctions.hpp:426