From 33f87ab1cd51e1ee2869a98e82f9bc11ae49c429 Mon Sep 17 00:00:00 2001 From: Christian Henning Date: Mon, 29 Apr 2013 02:12:40 +0000 Subject: [PATCH] Fixed bug with recreate and planar images. [SVN r84081] --- include/boost/gil/image.hpp | 107 +++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 32 deletions(-) diff --git a/include/boost/gil/image.hpp b/include/boost/gil/image.hpp index 28e42476f..9e4132eb2 100644 --- a/include/boost/gil/image.hpp +++ b/include/boost/gil/image.hpp @@ -70,20 +70,20 @@ public: 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 @@ public: 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 - image(const image& img) : - _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) { + image(const image& 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 @@ public: ~image() { destruct_pixels(_view); - deallocate(_original_dims); + deallocate(); } Alloc& allocator() { return _alloc; } @@ -145,11 +144,11 @@ public: 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 @@ public: 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 @@ public: 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 @@ private: 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_()); 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_()); uninitialized_fill_pixels(_view, p_in); - } catch(...) { deallocate(dimensions); throw; } + } catch(...) { deallocate(); throw; } } template @@ -224,11 +219,14 @@ private: try { allocate_(dimensions,mpl::bool_()); 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 @@ private: } 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 @@ private: 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::value; ++i) { @@ -295,6 +300,44 @@ private: } _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::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