Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84081 - trunk/boost/gil
From: chhenning_at_[hidden]
Date: 2013-04-28 22:12:40


Author: chhenning
Date: 2013-04-28 22:12:40 EDT (Sun, 28 Apr 2013)
New Revision: 84081
URL: http://svn.boost.org/trac/boost/changeset/84081

Log:
Fixed bug with recreate and planar images.
Text files modified:
   trunk/boost/gil/image.hpp | 107 ++++++++++++++++++++++++++++-----------
   1 files changed, 75 insertions(+), 32 deletions(-)

Modified: trunk/boost/gil/image.hpp
==============================================================================
--- trunk/boost/gil/image.hpp (original)
+++ trunk/boost/gil/image.hpp 2013-04-28 22:12:40 EDT (Sun, 28 Apr 2013)
@@ -70,20 +70,20 @@
 
     explicit image(std::size_t alignment=0,
                    const Alloc alloc_in = Alloc()) :
- _memory(0), _align_in_bytes(alignment), _alloc(alloc_in), _original_dims(0,0) {}
+ _memory(0), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
 
     // Create with dimensions and optional initial value and alignment
     image(const point_t& dimensions,
           std::size_t alignment=0,
           const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
- , _original_dims( dimensions ) {
+ , _allocated_bytes( 0 ) {
         allocate_and_default_construct(dimensions);
     }
 
     image(x_coord_t width, y_coord_t height,
           std::size_t alignment=0,
           const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
- , _original_dims( width, height ) {
+ , _allocated_bytes( 0 ) {
         allocate_and_default_construct(point_t(width,height));
     }
 
@@ -91,27 +91,26 @@
           const Pixel& p_in,
           std::size_t alignment,
           const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
- , _original_dims( dimensions ) {
+ , _allocated_bytes( 0 ) {
         allocate_and_fill(dimensions, p_in);
     }
     image(x_coord_t width, y_coord_t height,
           const Pixel& p_in,
           std::size_t alignment,
           const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
- , _original_dims( width, height ) {
+ , _allocated_bytes ( 0 ) {
         allocate_and_fill(point_t(width,height),p_in);
     }
 
     image(const image& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
- , _original_dims( img._original_dims ) {
+ , _allocated_bytes( img._allocated_bytes ) {
         allocate_and_copy(img.dimensions(),img._view);
     }
 
     template <typename P2, bool IP2, typename Alloc2>
- image(const image<P2,IP2,Alloc2>& img) :
- _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
+ image(const image<P2,IP2,Alloc2>& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
+ , _allocated_bytes( img._allocated_bytes ) {
        allocate_and_copy(img.dimensions(),img._view);
- _original_dims = img.dimensions();
     }
 
     image& operator=(const image& img) {
@@ -137,7 +136,7 @@
 
     ~image() {
         destruct_pixels(_view);
- deallocate(_original_dims);
+ deallocate();
     }
 
     Alloc& allocator() { return _alloc; }
@@ -145,11 +144,11 @@
 
     void swap(image& img) { // required by MutableContainerConcept
         using std::swap;
- swap(_align_in_bytes, img._align_in_bytes);
- swap(_memory, img._memory);
- swap(_view, img._view);
- swap(_alloc, img._alloc);
- swap( _original_dims, img._original_dims );
+ swap(_align_in_bytes, img._align_in_bytes);
+ swap(_memory, img._memory);
+ swap(_view, img._view);
+ swap(_alloc, img._alloc);
+ swap(_allocated_bytes, img._allocated_bytes );
     }
 
     void recreate(const point_t& dims, std::size_t alignment=0, const Alloc alloc_in = Alloc())
@@ -159,18 +158,13 @@
             return;
         }
 
- std::size_t original_size = total_allocated_size_in_bytes( _original_dims );
- std::size_t new_size = total_allocated_size_in_bytes( dims );
-
- if( original_size >= new_size )
+ if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
         {
             destruct_pixels( _view );
 
- _view = view_t( dims
- , typename view_t::locator( typename view_t::x_iterator( _memory )
- , get_row_size_in_memunits( dims.x )
- )
- );
+ create_view( dims
+ , typename boost::conditional< IsPlanar, mpl::true_, mpl::false_ >::type()
+ );
 
             default_construct_pixels( _view );
         }
@@ -180,6 +174,7 @@
             swap(tmp);
         }
     }
+
     void recreate(x_coord_t width, y_coord_t height, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
         recreate(point_t(width,height),alignment,alloc_in);
     }
@@ -202,21 +197,21 @@
     std::size_t _align_in_bytes;
     allocator_type _alloc;
 
- point_t _original_dims;
+ std::size_t _allocated_bytes;
 
 
     void allocate_and_default_construct(const point_t& dimensions) {
         try {
             allocate_(dimensions,mpl::bool_<IsPlanar>());
             default_construct_pixels(_view);
- } catch(...) { deallocate(dimensions); throw; }
+ } catch(...) { deallocate(); throw; }
     }
 
     void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
         try {
             allocate_(dimensions,mpl::bool_<IsPlanar>());
             uninitialized_fill_pixels(_view, p_in);
- } catch(...) { deallocate(dimensions); throw; }
+ } catch(...) { deallocate(); throw; }
     }
 
     template <typename View>
@@ -224,11 +219,14 @@
         try {
             allocate_(dimensions,mpl::bool_<IsPlanar>());
             uninitialized_copy_pixels(v,_view);
- } catch(...) { deallocate(dimensions); throw; }
+ } catch(...) { deallocate(); throw; }
     }
 
- void deallocate(const point_t& dimensions) {
- if (_memory) _alloc.deallocate(_memory, total_allocated_size_in_bytes(dimensions));
+ void deallocate() {
+ if (_memory && _allocated_bytes > 0 )
+ {
+ _alloc.deallocate(_memory, _allocated_bytes );
+ }
     }
 
     std::size_t is_planar_impl( const std::size_t size_in_units
@@ -278,7 +276,10 @@
     }
 
     void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
- _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
+
+ _allocated_bytes = total_allocated_size_in_bytes(dimensions);
+ _memory=_alloc.allocate( _allocated_bytes );
+
         unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
         _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
     }
@@ -286,7 +287,11 @@
     void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
         std::size_t row_size=get_row_size_in_memunits(dimensions.x);
         std::size_t plane_size=row_size*dimensions.y;
- _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
+
+ _allocated_bytes = total_allocated_size_in_bytes( dimensions );
+
+ _memory = _alloc.allocate( _allocated_bytes );
+
         unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
         typename view_t::x_iterator first;
         for (int i=0; i<num_channels<view_t>::value; ++i) {
@@ -295,6 +300,44 @@
         }
         _view=view_t(dimensions, typename view_t::locator(first, row_size));
     }
+
+ void create_view( const point_t& dims
+ , mpl::true_ // is planar
+ )
+ {
+ std::size_t row_size=get_row_size_in_memunits(dims.x);
+ std::size_t plane_size=row_size*dims.y;
+
+ unsigned char* tmp= ( _align_in_bytes > 0 ) ? (unsigned char*) align( (std::size_t) _memory,_align_in_bytes)
+ : _memory;
+ typename view_t::x_iterator first;
+
+ for (int i = 0; i < num_channels< view_t >::value; ++i )
+ {
+ dynamic_at_c( first, i ) = (typename channel_type<view_t>::type*) tmp;
+
+ memunit_advance( dynamic_at_c(first,i)
+ , plane_size*i
+ );
+ }
+
+ _view=view_t( dims
+ , typename view_t::locator( first
+ , row_size
+ )
+ );
+ }
+
+ void create_view( const point_t& dims
+ , mpl::false_ // is planar
+ )
+ {
+ _view = view_t( dims
+ , typename view_t::locator( typename view_t::x_iterator( _memory )
+ , get_row_size_in_memunits( dims.x )
+ )
+ );
+ }
 };
 
 template <typename Pixel, bool IsPlanar, typename Alloc>


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk