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>    17 #include <boost/core/exchange.hpp>    22 #include <type_traits>    24 namespace boost { 
namespace gil {
    40 template< 
typename Pixel, 
bool IsPlanar = false, 
typename Alloc=std::allocator<
unsigned char> >
    44 #if defined(BOOST_NO_CXX11_ALLOCATOR)    45     using allocator_type = 
typename Alloc::template rebind<unsigned char>::other;
    47     using allocator_type = 
typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
    50     using const_view_t = 
typename view_t::const_t;
    51     using point_t = 
typename view_t::point_t;
    52     using coord_t = 
typename view_t::coord_t;
    53     using value_type = 
typename view_t::value_type;
    54     using x_coord_t = coord_t;
    55     using y_coord_t = coord_t;
    57     const point_t&          dimensions()
            const { 
return _view.dimensions(); }
    58     x_coord_t               width()
                 const { 
return _view.width(); }
    59     y_coord_t               height()
                const { 
return _view.height(); }
    61     explicit image(std::size_t alignment=0,
    62                    const Alloc alloc_in = Alloc()) :
    63         _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
    66     image(
const point_t& dimensions,
    67           std::size_t alignment=0,
    68           const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
    69                                           , _allocated_bytes( 0 )
    71         allocate_and_default_construct(dimensions);
    74     image(x_coord_t width, y_coord_t height,
    75           std::size_t alignment=0,
    76           const Alloc alloc_in = Alloc()) : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
    77                                           , _allocated_bytes( 0 )
    79         allocate_and_default_construct(point_t(width,height));
    82     image(
const point_t& dimensions,
    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 )
    88         allocate_and_fill(dimensions, p_in);
    91     image(x_coord_t width, y_coord_t height,
    93           std::size_t alignment = 0,
    94           const Alloc alloc_in = Alloc())  : _memory(
nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
    95                                            , _allocated_bytes ( 0 )
    97         allocate_and_fill(point_t(width,height),p_in);
   100     image(
const image& img) : _memory(
nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
   101                             , _allocated_bytes( img._allocated_bytes )
   103         allocate_and_copy(img.dimensions(),img._view);
   106     template <
typename P2, 
bool IP2, 
typename Alloc2>
   108                                            , _allocated_bytes( img._allocated_bytes )
   110        allocate_and_copy(img.dimensions(),img._view);
   116       _memory(img._memory),
   117       _align_in_bytes(img._align_in_bytes),
   118       _alloc(std::move(img._alloc)),
   119       _allocated_bytes(img._allocated_bytes)
   121         img._view = view_t();
   122         img._memory = 
nullptr;
   123         img._align_in_bytes = 0;
   124         img._allocated_bytes = 0;
   129         if (dimensions() == img.dimensions())
   139     template <
typename Img>
   140     image& operator=(
const Img& img)
   142         if (dimensions() == img.dimensions())
   153       using propagate_allocators = std::true_type;
   154       using no_propagate_allocators = std::false_type;
   156       template <
class Alloc2>
   157       using choose_pocma = 
typename std::conditional<
   159           std::is_empty<Alloc2>::value,
   161           typename std::allocator_traits<Alloc2>::propagate_on_container_move_assignment::type
   164       static void exchange_memory(
image& lhs, 
image& rhs)
   166           lhs._memory = boost::exchange(rhs._memory, 
nullptr);
   167           lhs._align_in_bytes = boost::exchange(rhs._align_in_bytes, 0);
   168           lhs._allocated_bytes = boost::exchange(rhs._allocated_bytes, 0);
   169           lhs._view = boost::exchange(rhs._view, image::view_t{});
   172       void move_assign(
image& img, propagate_allocators) noexcept {
   176           this->_alloc = img._alloc;
   177           exchange_memory(*
this, img);
   180       void move_assign(
image& img, no_propagate_allocators) {
   181           if (_alloc == img._alloc) {
   185               exchange_memory(*
this, img);
   190                   allocate_and_copy(img.dimensions(), img._view);
   193                   img._view = image::view_t{};
   199                   this->_view = view_t{};
   208           if (
this != std::addressof(img))
   210               move_assign(img, choose_pocma<allocator_type>{});
   221     Alloc&       allocator() { 
return _alloc; }
   222     Alloc 
const& allocator()
 const { 
return _alloc; }
   224     void swap(
image& img) 
   227         swap(_align_in_bytes,  img._align_in_bytes);
   228         swap(_memory,          img._memory);
   229         swap(_view,            img._view);
   230         swap(_alloc,           img._alloc);
   231         swap(_allocated_bytes, img._allocated_bytes );
   239     void recreate(
const point_t& dims, std::size_t alignment = 0)
   241         if (dims == _view.dimensions() && _align_in_bytes == alignment)
   244         _align_in_bytes = alignment;
   246         if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
   249             create_view(dims, std::integral_constant<bool, IsPlanar>());
   254             image tmp(dims, alignment);
   259     void recreate(x_coord_t width, y_coord_t height, std::size_t alignment = 0)
   261         recreate(point_t(width, height), alignment);
   264     void recreate(
const point_t& dims, 
const Pixel& p_in, std::size_t alignment = 0)
   266         if (dims == _view.dimensions() && _align_in_bytes == alignment)
   269         _align_in_bytes = alignment;
   271         if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
   274             create_view(dims, 
typename std::integral_constant<bool, IsPlanar>());
   279             image tmp(dims, p_in, alignment);
   284     void recreate( x_coord_t width, y_coord_t height, 
const Pixel& p_in, std::size_t alignment = 0 )
   286         recreate( point_t( width, height ), p_in, alignment );
   290     void recreate(
const point_t& dims, std::size_t alignment, 
const Alloc alloc_in)
   292         if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
   295         _align_in_bytes = alignment;
   297         if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
   300             create_view(dims, std::integral_constant<bool, IsPlanar>());
   305             image tmp(dims, alignment, alloc_in);
   310     void recreate(x_coord_t width, y_coord_t height, std::size_t alignment, 
const Alloc alloc_in)
   312         recreate(point_t(width, height), alignment, alloc_in);
   315     void recreate(
const point_t& dims, 
const Pixel& p_in, std::size_t alignment, 
const Alloc alloc_in)
   317         if (dims == _view.dimensions() && _align_in_bytes == alignment && alloc_in == _alloc)
   320         _align_in_bytes = alignment;
   322         if (_allocated_bytes >= total_allocated_size_in_bytes(dims))
   325             create_view(dims, std::integral_constant<bool, IsPlanar>());
   330             image tmp(dims, p_in, alignment, alloc_in);
   335     void recreate(x_coord_t width, y_coord_t height, 
const Pixel& p_in, std::size_t alignment, 
const Alloc alloc_in )
   337         recreate(point_t(width, height), p_in, alignment, alloc_in);
   343     template <
typename P2, 
bool IP2, 
typename Alloc2> 
friend class image;
   345     unsigned char* _memory;
   346     std::size_t    _align_in_bytes;
   347     allocator_type _alloc;
   349     std::size_t _allocated_bytes;
   351     void allocate_and_default_construct(point_t 
const& dimensions)
   355             allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
   358         catch (...) { deallocate(); 
throw; }
   361     void allocate_and_fill(
const point_t& dimensions, Pixel 
const& p_in)
   365             allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
   368         catch(...) { deallocate(); 
throw; }
   371     template <
typename View>
   372     void allocate_and_copy(
const point_t& dimensions, View 
const& v)
   376             allocate_(dimensions, std::integral_constant<bool, IsPlanar>());
   379         catch(...) { deallocate(); 
throw; }
   384         if (_memory && _allocated_bytes > 0)
   385             _alloc.deallocate(_memory, _allocated_bytes);
   388     std::size_t is_planar_impl(
   389         std::size_t 
const size_in_units,
   390         std::size_t 
const channels_in_image,
   391         std::true_type)
 const   393         return size_in_units * channels_in_image;
   396     std::size_t is_planar_impl(
   397         std::size_t 
const size_in_units,
   399         std::false_type)
 const   401         return size_in_units;
   404     std::size_t total_allocated_size_in_bytes(point_t 
const& dimensions)
 const   406         using x_iterator = 
typename view_t::x_iterator;
   409         constexpr std::size_t _channels_in_image =
   412                 is_pixel<value_type>::value,
   414                 std::integral_constant<std::size_t, 1>
   417         std::size_t size_in_units = is_planar_impl(
   418             get_row_size_in_memunits(dimensions.x) * dimensions.y,
   420             std::integral_constant<bool, IsPlanar>());
   425             + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); 
   428     std::size_t get_row_size_in_memunits(x_coord_t width)
 const {   
   429         std::size_t size_in_memunits = width*memunit_step(
typename view_t::x_iterator());
   430         if (_align_in_bytes>0) {
   432             return align(size_in_memunits, alignment_in_memunits);
   434         return size_in_memunits;
   437     void allocate_(point_t 
const& dimensions, std::false_type)
   440         _allocated_bytes = total_allocated_size_in_bytes(dimensions);
   441         _memory=_alloc.allocate( _allocated_bytes );
   443         unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
   444         _view=view_t(dimensions,
typename view_t::locator(
typename view_t::x_iterator(tmp), get_row_size_in_memunits(dimensions.x)));
   446         BOOST_ASSERT(_view.width() == dimensions.x);
   447         BOOST_ASSERT(_view.height() == dimensions.y);
   450     void allocate_(point_t 
const& dimensions, std::true_type)
   453         std::size_t row_size=get_row_size_in_memunits(dimensions.x);
   454         std::size_t plane_size=row_size*dimensions.y;
   456         _allocated_bytes = total_allocated_size_in_bytes( dimensions );
   458         _memory = _alloc.allocate( _allocated_bytes );
   460         unsigned char* tmp=(_align_in_bytes>0) ? (
unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
   461         typename view_t::x_iterator first;
   462         for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
   465             memunit_advance(dynamic_at_c(first, i), static_cast<std::ptrdiff_t>(plane_size * i));
   467         _view=view_t(dimensions, 
typename view_t::locator(first, row_size));
   469         BOOST_ASSERT(_view.width() == dimensions.x);
   470         BOOST_ASSERT(_view.height() == dimensions.y);
   473     void create_view(point_t 
const& dims, std::true_type) 
   475         std::size_t row_size=get_row_size_in_memunits(dims.x);
   476         std::size_t plane_size=row_size*dims.y;
   478         unsigned char* tmp = ( _align_in_bytes > 0 ) ? (
unsigned char*) align( (std::size_t) _memory
   482         typename view_t::x_iterator first;
   484         for (std::size_t i = 0; i < num_channels<view_t>::value; ++i)
   487             memunit_advance(dynamic_at_c(first, i), static_cast<std::ptrdiff_t>(plane_size * i));
   490         _view = view_t(dims, 
typename view_t::locator(first, row_size));
   492         BOOST_ASSERT(_view.width() == dims.x);
   493         BOOST_ASSERT(_view.height() == dims.y);
   496     void create_view(point_t 
const& dims, std::false_type) 
   498         unsigned char* tmp = ( _align_in_bytes > 0 ) ? ( 
unsigned char* ) align( (std::size_t) _memory
   504                       , 
typename view_t::locator( 
typename view_t::x_iterator( tmp )
   505                                                 , get_row_size_in_memunits( dims.x )
   509         BOOST_ASSERT(_view.width() == dims.x);
   510         BOOST_ASSERT(_view.height() == dims.y);
   514 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc>
   520 template <
typename Pixel1, 
bool IsPlanar1, 
typename Alloc1, 
typename Pixel2, 
bool IsPlanar2, 
typename Alloc2>
   521 bool operator==(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2)
   523     if ((
void*)(&im1)==(
void*)(&im2)) 
return true;
   527 template <
typename Pixel1, 
bool IsPlanar1, 
typename Alloc1, 
typename Pixel2, 
bool IsPlanar2, 
typename Alloc2>
   528 bool operator!=(
const image<Pixel1,IsPlanar1,Alloc1>& im1,
const image<Pixel2,IsPlanar2,Alloc2>& im2) {
return !(im1==im2);}
   537 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc> 
inline   541 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc> 
inline   544     return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t
>(img._view);
   552 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc>
   553 struct channel_type<image<Pixel, IsPlanar, Alloc>> : channel_type<Pixel> {};
   555 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc>
   556 struct color_space_type<image<Pixel, IsPlanar, Alloc>> : color_space_type<Pixel> {};
   558 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc>
   559 struct channel_mapping_type<image<Pixel, IsPlanar, Alloc>> : channel_mapping_type<Pixel> {};
   561 template <
typename Pixel, 
bool IsPlanar, 
typename Alloc>
   562 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:41
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:538
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:542
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