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 typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
64 typedef typename view_t::point_t point_t;
65 typedef typename view_t::coord_t coord_t;
66 typedef typename view_t::value_type value_type;
67 typedef coord_t x_coord_t;
68 typedef coord_t y_coord_t;
70 const point_t& dimensions()
const {
return _view.dimensions(); }
71 x_coord_t width()
const {
return _view.width(); }
72 y_coord_t height()
const {
return _view.height(); }
74 explicit image(std::size_t alignment=0,
75 const Alloc alloc_in = Alloc()) :
76 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
79 image(
const point_t& dimensions,
80 std::size_t alignment=0,
81 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
82 , _allocated_bytes( 0 ) {
83 allocate_and_default_construct(dimensions);
86 image(x_coord_t width, y_coord_t height,
87 std::size_t alignment=0,
88 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
89 , _allocated_bytes( 0 ) {
90 allocate_and_default_construct(point_t(width,height));
93 image(
const point_t& dimensions,
95 std::size_t alignment,
96 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
97 , _allocated_bytes( 0 ) {
98 allocate_and_fill(dimensions, p_in);
100 image(x_coord_t width, y_coord_t height,
102 std::size_t alignment = 0,
103 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
104 , _allocated_bytes ( 0 ) {
105 allocate_and_fill(point_t(width,height),p_in);
108 image(
const image& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
109 , _allocated_bytes( img._allocated_bytes ) {
110 allocate_and_copy(img.dimensions(),img._view);
113 template <
typename P2,
bool IP2,
typename Alloc2>
115 , _allocated_bytes( img._allocated_bytes ) {
116 allocate_and_copy(img.dimensions(),img._view);
119 image& operator=(
const image& img) {
120 if (dimensions() == img.dimensions())
129 template <
typename Img>
130 image& operator=(
const Img& img) {
131 if (dimensions() == img.dimensions())
145 Alloc& allocator() {
return _alloc; }
146 Alloc
const& allocator()
const {
return _alloc; }
148 void swap(image& img) {
150 swap(_align_in_bytes, img._align_in_bytes);
151 swap(_memory, img._memory);
152 swap(_view, img._view);
153 swap(_alloc, img._alloc);
154 swap(_allocated_bytes, img._allocated_bytes );
163 void recreate(
const point_t& dims, std::size_t alignment = 0 )
165 if( dims == _view.dimensions() && _align_in_bytes == alignment )
170 _align_in_bytes = alignment;
172 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
177 ,
typename mpl::bool_<IsPlanar>()
184 image tmp( dims, alignment );
189 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment = 0 )
191 recreate( point_t( width, height ), alignment );
195 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment = 0 )
197 if( dims == _view.dimensions() && _align_in_bytes == alignment )
202 _align_in_bytes = alignment;
204 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
209 ,
typename mpl::bool_<IsPlanar>()
216 image tmp( dims, p_in, alignment );
221 void recreate( x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment = 0 )
223 recreate( point_t( width, height ), p_in, alignment );
228 void recreate(
const point_t& dims, std::size_t alignment,
const Alloc alloc_in )
230 if( dims == _view.dimensions()
231 && _align_in_bytes == alignment
232 && alloc_in == _alloc
238 _align_in_bytes = alignment;
240 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
245 ,
typename mpl::bool_<IsPlanar>()
252 image tmp( dims, alignment, alloc_in );
257 void recreate( x_coord_t width, y_coord_t height, std::size_t alignment,
const Alloc alloc_in )
259 recreate( point_t( width, height ), alignment, alloc_in );
262 void recreate(
const point_t& dims,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
264 if( dims == _view.dimensions()
265 && _align_in_bytes == alignment
266 && alloc_in == _alloc
272 _align_in_bytes = alignment;
274 if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
279 ,
typename mpl::bool_<IsPlanar>()
286 image tmp( dims, p_in, alignment, alloc_in );
291 void recreate(x_coord_t width, y_coord_t height,
const Pixel& p_in, std::size_t alignment,
const Alloc alloc_in )
293 recreate( point_t( width, height ), p_in,alignment, alloc_in );
300 unsigned char* _memory;
301 std::size_t _align_in_bytes;
302 allocator_type _alloc;
304 std::size_t _allocated_bytes;
307 void allocate_and_default_construct(
const point_t& dimensions) {
309 allocate_(dimensions,mpl::bool_<IsPlanar>());
311 }
catch(...) { deallocate();
throw; }
314 void allocate_and_fill(
const point_t& dimensions,
const Pixel& p_in) {
316 allocate_(dimensions,mpl::bool_<IsPlanar>());
318 }
catch(...) { deallocate();
throw; }
321 template <
typename View>
322 void allocate_and_copy(
const point_t& dimensions,
const View& v) {
324 allocate_(dimensions,mpl::bool_<IsPlanar>());
326 }
catch(...) { deallocate();
throw; }
330 if (_memory && _allocated_bytes > 0 )
332 _alloc.deallocate(_memory, _allocated_bytes );
336 std::size_t is_planar_impl(
const std::size_t size_in_units
337 ,
const std::size_t channels_in_image
341 return size_in_units * channels_in_image;
344 std::size_t is_planar_impl(
const std::size_t size_in_units
349 return size_in_units;
352 std::size_t total_allocated_size_in_bytes(
const point_t& dimensions)
const {
354 typedef typename view_t::x_iterator x_iterator;
357 const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
362 std::size_t size_in_units = is_planar_impl( get_row_size_in_memunits( dimensions.x ) * dimensions.y
364 ,
typename mpl::bool_<IsPlanar>()
370 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 );
373 std::size_t get_row_size_in_memunits(x_coord_t width)
const {
374 std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
375 if (_align_in_bytes>0) {
377 return align(size_in_memunits, alignment_in_memunits);
379 return size_in_memunits;
382 void allocate_(
const point_t& dimensions, mpl::false_) {
384 _allocated_bytes = total_allocated_size_in_bytes(dimensions);
385 _memory=_alloc.allocate( _allocated_bytes );
387 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
388 _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
391 void allocate_(
const point_t& dimensions, mpl::true_) {
392 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
393 std::size_t plane_size=row_size*dimensions.y;
395 _allocated_bytes = total_allocated_size_in_bytes( dimensions );
397 _memory = _alloc.allocate( _allocated_bytes );
399 unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
400 typename view_t::x_iterator first;
401 for (
int i=0; i<num_channels<view_t>::value; ++i) {
402 dynamic_at_c(first,i) = (
typename channel_type<view_t>::type*)tmp;
403 memunit_advance(dynamic_at_c(first,i), plane_size*i);
405 _view=view_t(dimensions,
typename view_t::locator(first, row_size));
408 void create_view(
const point_t& dims
412 std::size_t row_size=get_row_size_in_memunits(dims.x);
413 std::size_t plane_size=row_size*dims.y;
415 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
419 typename view_t::x_iterator first;
421 for (
int i = 0; i < num_channels< view_t >::value; ++i )
423 dynamic_at_c( first, i ) = (
typename channel_type<view_t>::type*) tmp;
425 memunit_advance( dynamic_at_c(first,i)
431 ,
typename view_t::locator( first
437 void create_view(
const point_t& dims
441 unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char* ) align( (std::size_t) _memory
447 ,
typename view_t::locator(
typename view_t::x_iterator( tmp )
448 , get_row_size_in_memunits( dims.x )
454 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
459 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
460 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
461 if ((
void*)(&im1)==(
void*)(&im2))
return true;
465 template <
typename Pixel1,
bool IsPlanar1,
typename Alloc1,
typename Pixel2,
bool IsPlanar2,
typename Alloc2>
466 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
475 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
479 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
inline
489 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
490 struct channel_type<image<Pixel,IsPlanar,Alloc> > :
public channel_type<Pixel> {};
492 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
493 struct color_space_type<image<Pixel,IsPlanar,Alloc> > :
public color_space_type<Pixel> {};
495 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
496 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > :
public channel_mapping_type<Pixel> {};
498 template <
typename Pixel,
bool IsPlanar,
typename Alloc>
499 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:542
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:957
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:290
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:672
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:476
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:725
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:480
Returns the number of channels of a pixel-based GIL construct.
Definition: gil_concept.hpp:61
BOOST_FORCEINLINE void destruct_pixels(const View &img_view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:485
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