1 /*
2     Copyright 2005-2007 Adobe Systems Incorporated
3 
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).
7 
8     See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 /*************************************************************************************************/
11 
12 #ifndef GIL_IMAGE_H
13 #define GIL_IMAGE_H
14 
15 ////////////////////////////////////////////////////////////////////////////////////////
16 /// \file
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
21 ///
22 ////////////////////////////////////////////////////////////////////////////////////////
23 
24 #include <cstddef>
25 #include <memory>
26 #include "gil_config.hpp"
27 #include "image_view.hpp"
28 #include "metafunctions.hpp"
29 #include "algorithm.hpp"
30 
31 namespace boost { namespace gil {
32 
33 //#ifdef _MSC_VER
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)
36 //#endif
37 
38 ////////////////////////////////////////////////////////////////////////////////////////
39 /// \ingroup ImageModel PixelBasedModel
40 /// \brief container interface over image view. Models ImageConcept, PixelBasedConcept
41 ///
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.
44 ///
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
47 ///
48 ////////////////////////////////////////////////////////////////////////////////////////
49 
50 template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
51 class image {
52 public:
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;
61 
dimensions() const62     const point_t&          dimensions()            const { return _view.dimensions(); }
width() const63     x_coord_t               width()                 const { return _view.width(); }
height() const64     y_coord_t               height()                const { return _view.height(); }
65 
image(std::size_t alignment=0,const Alloc alloc_in=Alloc ())66     explicit image(std::size_t alignment=0,
67                    const Alloc alloc_in = Alloc()) :
68         _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {}
69 
70     // Create with dimensions and optional initial value and alignment
image(const point_t & dimensions,std::size_t alignment=0,const Alloc alloc_in=Alloc ())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);
75     }
image(x_coord_t width,y_coord_t height,std::size_t alignment=0,const Alloc alloc_in=Alloc ())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));
80     }
image(const point_t & dimensions,const Pixel & p_in,std::size_t alignment,const Alloc alloc_in=Alloc ())81     image(const point_t& dimensions,
82           const Pixel& p_in,
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);
87     }
image(x_coord_t width,y_coord_t height,const Pixel & p_in,std::size_t alignment,const Alloc alloc_in=Alloc ())88     image(x_coord_t width, y_coord_t height,
89           const Pixel& p_in,
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);
94     }
95 
image(const image & img)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);
99     }
100 
101     template <typename P2, bool IP2, typename Alloc2>
image(const image<P2,IP2,Alloc2> & img)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);
105     }
operator =(const image & img)106     image& operator=(const image& img) {
107         if (dimensions() == img.dimensions())
108             copy_pixels(img._view,_view);
109         else {
110             image tmp(img);
111             swap(tmp);
112         }
113         return *this;
114     }
115 
116     template <typename Img>
operator =(const Img & img)117     image& operator=(const Img& img) {
118         if (dimensions() == img.dimensions())
119             copy_pixels(img._view,_view);
120         else {
121             image tmp(img);
122             swap(tmp);
123         }
124         return *this;
125     }
126 
~image()127     ~image() {
128         destruct_pixels(_view);
129         deallocate(_view.dimensions());
130     }
131 
allocator()132     Alloc&       allocator() { return _alloc; }
allocator() const133     Alloc const& allocator() const { return _alloc; }
134 
swap(image & img)135     void swap(image& img) { // required by MutableContainerConcept
136         using std::swap;
137         swap(_align_in_bytes, img._align_in_bytes);
138         swap(_memory,         img._memory);
139         swap(_view,           img._view);
140         swap(_alloc,          img._alloc);
141     }
142 
recreate(const point_t & dims,std::size_t alignment=0,const Alloc alloc_in=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);
146             swap(tmp);
147         }
148     }
recreate(x_coord_t width,y_coord_t height,std::size_t alignment=0,const Alloc alloc_in=Alloc ())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);
151     }
recreate(const point_t & dims,const Pixel & p_in,std::size_t alignment,const Alloc alloc_in=Alloc ())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);
156             swap(tmp);
157         }
158     }
recreate(x_coord_t width,y_coord_t height,const Pixel & p_in,std::size_t alignment,const Alloc alloc_in=Alloc ())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);
162     }
163 
164     view_t       _view;      // contains pointer to the pixels, the image size and ways to navigate pixels
165 private:
166     unsigned char* _memory;
167     std::size_t    _align_in_bytes;
168     allocator_type _alloc;
169 
allocate_and_default_construct(const point_t & dimensions)170     void allocate_and_default_construct(const point_t& dimensions) {
171         try {
172             allocate_(dimensions,mpl::bool_<IsPlanar>());
173             default_construct_pixels(_view);
174         } catch(...) { deallocate(dimensions); throw; }
175     }
176 
allocate_and_fill(const point_t & dimensions,const Pixel & p_in)177     void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
178         try {
179             allocate_(dimensions,mpl::bool_<IsPlanar>());
180             uninitialized_fill_pixels(_view, p_in);
181         } catch(...) { deallocate(dimensions); throw; }
182     }
183 
184     template <typename View>
allocate_and_copy(const point_t & dimensions,const View & v)185     void allocate_and_copy(const point_t& dimensions, const View& v) {
186         try {
187             allocate_(dimensions,mpl::bool_<IsPlanar>());
188             uninitialized_copy_pixels(v,_view);
189         } catch(...) { deallocate(dimensions); throw; }
190     }
191 
deallocate(const point_t & dimensions)192     void deallocate(const point_t& dimensions) {
193         if (_memory) _alloc.deallocate(_memory, total_allocated_size_in_bytes(dimensions));
194     }
195 
total_allocated_size_in_bytes(const point_t & dimensions) const196     std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
197 
198         typedef typename view_t::x_iterator x_iterator;
199 
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 >
203                                                            , mpl::int_< 1 >
204 														   >::type::value;
205 
206         std::size_t size_in_units = get_row_size_in_memunits(dimensions.x)*dimensions.y;
207 
208         if (IsPlanar)
209             size_in_units = size_in_units * _channels_in_image ;
210 
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
215     }
216 
get_row_size_in_memunits(x_coord_t width) const217     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);
222         }
223         return size_in_memunits;
224     }
225 
allocate_(const point_t & dimensions,mpl::false_)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)));
230     }
231 
allocate_(const point_t & dimensions,mpl::true_)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);
241         }
242         _view=view_t(dimensions, typename view_t::locator(first, row_size));
243     }
244 };
245 
246 template <typename Pixel, bool IsPlanar, typename Alloc>
swap(image<Pixel,IsPlanar,Alloc> & im1,image<Pixel,IsPlanar,Alloc> & im2)247 void swap(image<Pixel, IsPlanar, Alloc>& im1,image<Pixel, IsPlanar, Alloc>& im2) {
248     im1.swap(im2);
249 }
250 
251 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
operator ==(const image<Pixel1,IsPlanar1,Alloc1> & im1,const image<Pixel2,IsPlanar2,Alloc2> & im2)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));
256 }
257 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
operator !=(const image<Pixel1,IsPlanar1,Alloc1> & im1,const image<Pixel2,IsPlanar2,Alloc2> & im2)258 bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
259 
260 ///@{
261 /// \name view, const_view
262 /// \brief Get an image view from an image
263 
264 /// \ingroup ImageModel
265 
266 /// \brief Returns the non-constant-pixel view of an image
267 template <typename Pixel, bool IsPlanar, typename Alloc> inline
view(image<Pixel,IsPlanar,Alloc> & img)268 const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
269 
270 /// \brief Returns the constant-pixel view of an image
271 template <typename Pixel, bool IsPlanar, typename Alloc> inline
const_view(const image<Pixel,IsPlanar,Alloc> & img)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);
274 }
275 ///@}
276 
277 /////////////////////////////
278 //  PixelBasedConcept
279 /////////////////////////////
280 
281 template <typename Pixel, bool IsPlanar, typename Alloc>
282 struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
283 
284 template <typename Pixel, bool IsPlanar, typename Alloc>
285 struct color_space_type<image<Pixel,IsPlanar,Alloc> >  : public color_space_type<Pixel> {};
286 
287 template <typename Pixel, bool IsPlanar, typename Alloc>
288 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
289 
290 template <typename Pixel, bool IsPlanar, typename Alloc>
291 struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
292 
293 //#ifdef _MSC_VER
294 //#pragma warning(pop)
295 //#endif
296 
297 } }  // namespace boost::gil
298 
299 #endif
300