2 Copyright 2005-2007 Adobe Systems Incorporated
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
10 /*************************************************************************************************/
15 ////////////////////////////////////////////////////////////////////////////////////////
17 /// \brief Templated image
18 /// \author Lubomir Bourdev and Hailin Jin \n
19 /// Adobe Systems Incorporated
20 /// \date 2005-2007 \n Last updated on February 12, 2007
22 ////////////////////////////////////////////////////////////////////////////////////////
26 #include "gil_config.hpp"
27 #include "image_view.hpp"
28 #include "metafunctions.hpp"
29 #include "algorithm.hpp"
31 namespace boost { namespace gil {
34 //#pragma warning(push)
35 //#pragma warning(disable : 4244) // conversion from 'gil::image<V,Alloc>::coord_t' to 'int', possible loss of data (visual studio compiler doesn't realize that the two types are the same)
38 ////////////////////////////////////////////////////////////////////////////////////////
39 /// \ingroup ImageModel PixelBasedModel
40 /// \brief container interface over image view. Models ImageConcept, PixelBasedConcept
42 /// A 2D container whose elements are pixels. It is templated over the pixel type, a boolean
43 /// indicating whether it should be planar, and an optional allocator.
45 /// Note that its element type does not have to be a pixel. \p image can be instantiated with any Regular element,
46 /// in which case it models the weaker RandomAccess2DImageConcept and does not model PixelBasedConcept
48 ////////////////////////////////////////////////////////////////////////////////////////
50 template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
53 typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
54 typedef typename view_type_from_pixel<Pixel, IsPlanar>::type view_t;
55 typedef typename view_t::const_t const_view_t;
56 typedef typename view_t::point_t point_t;
57 typedef typename view_t::coord_t coord_t;
58 typedef typename view_t::value_type value_type;
59 typedef coord_t x_coord_t;
60 typedef coord_t y_coord_t;
62 const point_t& dimensions() const { return _view.dimensions(); }
63 x_coord_t width() const { return _view.width(); }
64 y_coord_t height() const { return _view.height(); }
66 explicit image(std::size_t alignment=0,
67 const Alloc alloc_in = Alloc()) :
68 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {}
70 // Create with dimensions and optional initial value and alignment
71 image(const point_t& dimensions,
72 std::size_t alignment=0,
73 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
74 allocate_and_default_construct(dimensions);
76 image(x_coord_t width, y_coord_t height,
77 std::size_t alignment=0,
78 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
79 allocate_and_default_construct(point_t(width,height));
81 image(const point_t& dimensions,
83 std::size_t alignment,
84 const Alloc alloc_in = Alloc()) :
85 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
86 allocate_and_fill(dimensions, p_in);
88 image(x_coord_t width, y_coord_t height,
90 std::size_t alignment,
91 const Alloc alloc_in = Alloc()) :
92 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
93 allocate_and_fill(point_t(width,height),p_in);
96 image(const image& img) :
97 _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
98 allocate_and_copy(img.dimensions(),img._view);
101 template <typename P2, bool IP2, typename Alloc2>
102 image(const image<P2,IP2,Alloc2>& img) :
103 _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
104 allocate_and_copy(img.dimensions(),img._view);
106 image& operator=(const image& img) {
107 if (dimensions() == img.dimensions())
108 copy_pixels(img._view,_view);
116 template <typename Img>
117 image& operator=(const Img& img) {
118 if (dimensions() == img.dimensions())
119 copy_pixels(img._view,_view);
128 destruct_pixels(_view);
129 deallocate(_view.dimensions());
132 Alloc& allocator() { return _alloc; }
133 Alloc const& allocator() const { return _alloc; }
135 void swap(image& img) { // required by MutableContainerConcept
137 swap(_align_in_bytes, img._align_in_bytes);
138 swap(_memory, img._memory);
139 swap(_view, img._view);
140 swap(_alloc, img._alloc);
143 void recreate(const point_t& dims, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
144 if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
145 image tmp(dims, alignment, alloc_in);
149 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
150 recreate(point_t(width,height),alignment,alloc_in);
152 void recreate(const point_t& dims,
153 const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
154 if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
155 image tmp(dims, p_in, alignment, alloc_in);
159 void recreate(x_coord_t width, y_coord_t height,
160 const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
161 recreate(point_t(width,height),p_in,alignment,alloc_in);
164 view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
166 unsigned char* _memory;
167 std::size_t _align_in_bytes;
168 allocator_type _alloc;
170 void allocate_and_default_construct(const point_t& dimensions) {
172 allocate_(dimensions,mpl::bool_<IsPlanar>());
173 default_construct_pixels(_view);
174 } catch(...) { deallocate(dimensions); throw; }
177 void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
179 allocate_(dimensions,mpl::bool_<IsPlanar>());
180 uninitialized_fill_pixels(_view, p_in);
181 } catch(...) { deallocate(dimensions); throw; }
184 template <typename View>
185 void allocate_and_copy(const point_t& dimensions, const View& v) {
187 allocate_(dimensions,mpl::bool_<IsPlanar>());
188 uninitialized_copy_pixels(v,_view);
189 } catch(...) { deallocate(dimensions); throw; }
192 void deallocate(const point_t& dimensions) {
193 if (_memory) _alloc.deallocate(_memory, total_allocated_size_in_bytes(dimensions));
196 std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
198 typedef typename view_t::x_iterator x_iterator;
200 // when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
201 const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
202 , num_channels< view_t >
206 std::size_t size_in_units = get_row_size_in_memunits(dimensions.x)*dimensions.y;
209 size_in_units = size_in_units * _channels_in_image ;
211 // return the size rounded up to the nearest byte
212 return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
213 / byte_to_memunit<x_iterator>::value
214 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
217 std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
218 std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
219 if (_align_in_bytes>0) {
220 std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
221 return align(size_in_memunits, alignment_in_memunits);
223 return size_in_memunits;
226 void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
227 _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
228 unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
229 _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
232 void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
233 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
234 std::size_t plane_size=row_size*dimensions.y;
235 _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
236 unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
237 typename view_t::x_iterator first;
238 for (int i=0; i<num_channels<view_t>::value; ++i) {
239 dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
240 memunit_advance(dynamic_at_c(first,i), plane_size*i);
242 _view=view_t(dimensions, typename view_t::locator(first, row_size));
246 template <typename Pixel, bool IsPlanar, typename Alloc>
247 void swap(image<Pixel, IsPlanar, Alloc>& im1,image<Pixel, IsPlanar, Alloc>& im2) {
251 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
252 bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
253 if ((void*)(&im1)==(void*)(&im2)) return true;
254 if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
255 return equal_pixels(const_view(im1),const_view(im2));
257 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
258 bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
261 /// \name view, const_view
262 /// \brief Get an image view from an image
264 /// \ingroup ImageModel
266 /// \brief Returns the non-constant-pixel view of an image
267 template <typename Pixel, bool IsPlanar, typename Alloc> inline
268 const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
270 /// \brief Returns the constant-pixel view of an image
271 template <typename Pixel, bool IsPlanar, typename Alloc> inline
272 const typename image<Pixel,IsPlanar,Alloc>::const_view_t const_view(const image<Pixel,IsPlanar,Alloc>& img) {
273 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
277 /////////////////////////////
279 /////////////////////////////
281 template <typename Pixel, bool IsPlanar, typename Alloc>
282 struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
284 template <typename Pixel, bool IsPlanar, typename Alloc>
285 struct color_space_type<image<Pixel,IsPlanar,Alloc> > : public color_space_type<Pixel> {};
287 template <typename Pixel, bool IsPlanar, typename Alloc>
288 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
290 template <typename Pixel, bool IsPlanar, typename Alloc>
291 struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
294 //#pragma warning(pop)
297 } } // namespace boost::gil