Chris@16
|
1 /*
|
Chris@16
|
2 Copyright 2005-2007 Adobe Systems Incorporated
|
Chris@16
|
3
|
Chris@16
|
4 Use, modification and distribution are subject to the Boost Software License,
|
Chris@16
|
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt).
|
Chris@16
|
7
|
Chris@16
|
8 See http://opensource.adobe.com/gil for most recent version including documentation.
|
Chris@16
|
9 */
|
Chris@16
|
10 /*************************************************************************************************/
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef GIL_IMAGE_H
|
Chris@16
|
13 #define GIL_IMAGE_H
|
Chris@16
|
14
|
Chris@16
|
15 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
16 /// \file
|
Chris@16
|
17 /// \brief Templated image
|
Chris@16
|
18 /// \author Lubomir Bourdev and Hailin Jin \n
|
Chris@16
|
19 /// Adobe Systems Incorporated
|
Chris@16
|
20 /// \date 2005-2007 \n Last updated on February 12, 2007
|
Chris@16
|
21 ///
|
Chris@16
|
22 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
23
|
Chris@16
|
24 #include <cstddef>
|
Chris@16
|
25 #include <memory>
|
Chris@16
|
26 #include "gil_config.hpp"
|
Chris@16
|
27 #include "image_view.hpp"
|
Chris@16
|
28 #include "metafunctions.hpp"
|
Chris@16
|
29 #include "algorithm.hpp"
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost { namespace gil {
|
Chris@16
|
32
|
Chris@16
|
33 //#ifdef _MSC_VER
|
Chris@16
|
34 //#pragma warning(push)
|
Chris@16
|
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)
|
Chris@16
|
36 //#endif
|
Chris@16
|
37
|
Chris@16
|
38 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
39 /// \ingroup ImageModel PixelBasedModel
|
Chris@16
|
40 /// \brief container interface over image view. Models ImageConcept, PixelBasedConcept
|
Chris@16
|
41 ///
|
Chris@16
|
42 /// A 2D container whose elements are pixels. It is templated over the pixel type, a boolean
|
Chris@16
|
43 /// indicating whether it should be planar, and an optional allocator.
|
Chris@16
|
44 ///
|
Chris@16
|
45 /// Note that its element type does not have to be a pixel. \p image can be instantiated with any Regular element,
|
Chris@16
|
46 /// in which case it models the weaker RandomAccess2DImageConcept and does not model PixelBasedConcept
|
Chris@16
|
47 ///
|
Chris@16
|
48 ////////////////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
49
|
Chris@16
|
50 template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
|
Chris@16
|
51 class image {
|
Chris@16
|
52 public:
|
Chris@16
|
53 typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
|
Chris@16
|
54 typedef typename view_type_from_pixel<Pixel, IsPlanar>::type view_t;
|
Chris@16
|
55 typedef typename view_t::const_t const_view_t;
|
Chris@16
|
56 typedef typename view_t::point_t point_t;
|
Chris@16
|
57 typedef typename view_t::coord_t coord_t;
|
Chris@16
|
58 typedef typename view_t::value_type value_type;
|
Chris@16
|
59 typedef coord_t x_coord_t;
|
Chris@16
|
60 typedef coord_t y_coord_t;
|
Chris@16
|
61
|
Chris@16
|
62 const point_t& dimensions() const { return _view.dimensions(); }
|
Chris@16
|
63 x_coord_t width() const { return _view.width(); }
|
Chris@16
|
64 y_coord_t height() const { return _view.height(); }
|
Chris@16
|
65
|
Chris@16
|
66 explicit image(std::size_t alignment=0,
|
Chris@16
|
67 const Alloc alloc_in = Alloc()) :
|
Chris@16
|
68 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {}
|
Chris@16
|
69
|
Chris@16
|
70 // Create with dimensions and optional initial value and alignment
|
Chris@16
|
71 image(const point_t& dimensions,
|
Chris@16
|
72 std::size_t alignment=0,
|
Chris@16
|
73 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
|
Chris@16
|
74 allocate_and_default_construct(dimensions);
|
Chris@16
|
75 }
|
Chris@16
|
76 image(x_coord_t width, y_coord_t height,
|
Chris@16
|
77 std::size_t alignment=0,
|
Chris@16
|
78 const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
|
Chris@16
|
79 allocate_and_default_construct(point_t(width,height));
|
Chris@16
|
80 }
|
Chris@16
|
81 image(const point_t& dimensions,
|
Chris@16
|
82 const Pixel& p_in,
|
Chris@16
|
83 std::size_t alignment,
|
Chris@16
|
84 const Alloc alloc_in = Alloc()) :
|
Chris@16
|
85 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
|
Chris@16
|
86 allocate_and_fill(dimensions, p_in);
|
Chris@16
|
87 }
|
Chris@16
|
88 image(x_coord_t width, y_coord_t height,
|
Chris@16
|
89 const Pixel& p_in,
|
Chris@16
|
90 std::size_t alignment,
|
Chris@16
|
91 const Alloc alloc_in = Alloc()) :
|
Chris@16
|
92 _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
|
Chris@16
|
93 allocate_and_fill(point_t(width,height),p_in);
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 image(const image& img) :
|
Chris@16
|
97 _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
|
Chris@16
|
98 allocate_and_copy(img.dimensions(),img._view);
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 template <typename P2, bool IP2, typename Alloc2>
|
Chris@16
|
102 image(const image<P2,IP2,Alloc2>& img) :
|
Chris@16
|
103 _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
|
Chris@16
|
104 allocate_and_copy(img.dimensions(),img._view);
|
Chris@16
|
105 }
|
Chris@16
|
106 image& operator=(const image& img) {
|
Chris@16
|
107 if (dimensions() == img.dimensions())
|
Chris@16
|
108 copy_pixels(img._view,_view);
|
Chris@16
|
109 else {
|
Chris@16
|
110 image tmp(img);
|
Chris@16
|
111 swap(tmp);
|
Chris@16
|
112 }
|
Chris@16
|
113 return *this;
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 template <typename Img>
|
Chris@16
|
117 image& operator=(const Img& img) {
|
Chris@16
|
118 if (dimensions() == img.dimensions())
|
Chris@16
|
119 copy_pixels(img._view,_view);
|
Chris@16
|
120 else {
|
Chris@16
|
121 image tmp(img);
|
Chris@16
|
122 swap(tmp);
|
Chris@16
|
123 }
|
Chris@16
|
124 return *this;
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 ~image() {
|
Chris@16
|
128 destruct_pixels(_view);
|
Chris@16
|
129 deallocate(_view.dimensions());
|
Chris@16
|
130 }
|
Chris@16
|
131
|
Chris@16
|
132 Alloc& allocator() { return _alloc; }
|
Chris@16
|
133 Alloc const& allocator() const { return _alloc; }
|
Chris@16
|
134
|
Chris@16
|
135 void swap(image& img) { // required by MutableContainerConcept
|
Chris@16
|
136 using std::swap;
|
Chris@16
|
137 swap(_align_in_bytes, img._align_in_bytes);
|
Chris@16
|
138 swap(_memory, img._memory);
|
Chris@16
|
139 swap(_view, img._view);
|
Chris@16
|
140 swap(_alloc, img._alloc);
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 void recreate(const point_t& dims, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
|
Chris@16
|
144 if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
|
Chris@16
|
145 image tmp(dims, alignment, alloc_in);
|
Chris@16
|
146 swap(tmp);
|
Chris@16
|
147 }
|
Chris@16
|
148 }
|
Chris@16
|
149 void recreate(x_coord_t width, y_coord_t height, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
|
Chris@16
|
150 recreate(point_t(width,height),alignment,alloc_in);
|
Chris@16
|
151 }
|
Chris@16
|
152 void recreate(const point_t& dims,
|
Chris@16
|
153 const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
|
Chris@16
|
154 if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
|
Chris@16
|
155 image tmp(dims, p_in, alignment, alloc_in);
|
Chris@16
|
156 swap(tmp);
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159 void recreate(x_coord_t width, y_coord_t height,
|
Chris@16
|
160 const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
|
Chris@16
|
161 recreate(point_t(width,height),p_in,alignment,alloc_in);
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
|
Chris@16
|
165 private:
|
Chris@16
|
166 unsigned char* _memory;
|
Chris@16
|
167 std::size_t _align_in_bytes;
|
Chris@16
|
168 allocator_type _alloc;
|
Chris@16
|
169
|
Chris@16
|
170 void allocate_and_default_construct(const point_t& dimensions) {
|
Chris@16
|
171 try {
|
Chris@16
|
172 allocate_(dimensions,mpl::bool_<IsPlanar>());
|
Chris@16
|
173 default_construct_pixels(_view);
|
Chris@16
|
174 } catch(...) { deallocate(dimensions); throw; }
|
Chris@16
|
175 }
|
Chris@16
|
176
|
Chris@16
|
177 void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
|
Chris@16
|
178 try {
|
Chris@16
|
179 allocate_(dimensions,mpl::bool_<IsPlanar>());
|
Chris@16
|
180 uninitialized_fill_pixels(_view, p_in);
|
Chris@16
|
181 } catch(...) { deallocate(dimensions); throw; }
|
Chris@16
|
182 }
|
Chris@16
|
183
|
Chris@16
|
184 template <typename View>
|
Chris@16
|
185 void allocate_and_copy(const point_t& dimensions, const View& v) {
|
Chris@16
|
186 try {
|
Chris@16
|
187 allocate_(dimensions,mpl::bool_<IsPlanar>());
|
Chris@16
|
188 uninitialized_copy_pixels(v,_view);
|
Chris@16
|
189 } catch(...) { deallocate(dimensions); throw; }
|
Chris@16
|
190 }
|
Chris@16
|
191
|
Chris@16
|
192 void deallocate(const point_t& dimensions) {
|
Chris@16
|
193 if (_memory) _alloc.deallocate(_memory, total_allocated_size_in_bytes(dimensions));
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
|
Chris@16
|
197
|
Chris@16
|
198 typedef typename view_t::x_iterator x_iterator;
|
Chris@16
|
199
|
Chris@16
|
200 // when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
|
Chris@16
|
201 const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
|
Chris@16
|
202 , num_channels< view_t >
|
Chris@16
|
203 , mpl::int_< 1 >
|
Chris@16
|
204 >::type::value;
|
Chris@16
|
205
|
Chris@16
|
206 std::size_t size_in_units = get_row_size_in_memunits(dimensions.x)*dimensions.y;
|
Chris@16
|
207
|
Chris@16
|
208 if (IsPlanar)
|
Chris@16
|
209 size_in_units = size_in_units * _channels_in_image ;
|
Chris@16
|
210
|
Chris@16
|
211 // return the size rounded up to the nearest byte
|
Chris@16
|
212 return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
|
Chris@16
|
213 / byte_to_memunit<x_iterator>::value
|
Chris@16
|
214 + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
|
Chris@16
|
215 }
|
Chris@16
|
216
|
Chris@16
|
217 std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
|
Chris@16
|
218 std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
|
Chris@16
|
219 if (_align_in_bytes>0) {
|
Chris@16
|
220 std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
|
Chris@16
|
221 return align(size_in_memunits, alignment_in_memunits);
|
Chris@16
|
222 }
|
Chris@16
|
223 return size_in_memunits;
|
Chris@16
|
224 }
|
Chris@16
|
225
|
Chris@16
|
226 void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
|
Chris@16
|
227 _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
|
Chris@16
|
228 unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
|
Chris@16
|
229 _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
|
Chris@16
|
233 std::size_t row_size=get_row_size_in_memunits(dimensions.x);
|
Chris@16
|
234 std::size_t plane_size=row_size*dimensions.y;
|
Chris@16
|
235 _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
|
Chris@16
|
236 unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
|
Chris@16
|
237 typename view_t::x_iterator first;
|
Chris@16
|
238 for (int i=0; i<num_channels<view_t>::value; ++i) {
|
Chris@16
|
239 dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
|
Chris@16
|
240 memunit_advance(dynamic_at_c(first,i), plane_size*i);
|
Chris@16
|
241 }
|
Chris@16
|
242 _view=view_t(dimensions, typename view_t::locator(first, row_size));
|
Chris@16
|
243 }
|
Chris@16
|
244 };
|
Chris@16
|
245
|
Chris@16
|
246 template <typename Pixel, bool IsPlanar, typename Alloc>
|
Chris@16
|
247 void swap(image<Pixel, IsPlanar, Alloc>& im1,image<Pixel, IsPlanar, Alloc>& im2) {
|
Chris@16
|
248 im1.swap(im2);
|
Chris@16
|
249 }
|
Chris@16
|
250
|
Chris@16
|
251 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
|
Chris@16
|
252 bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
|
Chris@16
|
253 if ((void*)(&im1)==(void*)(&im2)) return true;
|
Chris@16
|
254 if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
|
Chris@16
|
255 return equal_pixels(const_view(im1),const_view(im2));
|
Chris@16
|
256 }
|
Chris@16
|
257 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
|
Chris@16
|
258 bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
|
Chris@16
|
259
|
Chris@16
|
260 ///@{
|
Chris@16
|
261 /// \name view, const_view
|
Chris@16
|
262 /// \brief Get an image view from an image
|
Chris@16
|
263
|
Chris@16
|
264 /// \ingroup ImageModel
|
Chris@16
|
265
|
Chris@16
|
266 /// \brief Returns the non-constant-pixel view of an image
|
Chris@16
|
267 template <typename Pixel, bool IsPlanar, typename Alloc> inline
|
Chris@16
|
268 const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
|
Chris@16
|
269
|
Chris@16
|
270 /// \brief Returns the constant-pixel view of an image
|
Chris@16
|
271 template <typename Pixel, bool IsPlanar, typename Alloc> inline
|
Chris@16
|
272 const typename image<Pixel,IsPlanar,Alloc>::const_view_t const_view(const image<Pixel,IsPlanar,Alloc>& img) {
|
Chris@16
|
273 return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
|
Chris@16
|
274 }
|
Chris@16
|
275 ///@}
|
Chris@16
|
276
|
Chris@16
|
277 /////////////////////////////
|
Chris@16
|
278 // PixelBasedConcept
|
Chris@16
|
279 /////////////////////////////
|
Chris@16
|
280
|
Chris@16
|
281 template <typename Pixel, bool IsPlanar, typename Alloc>
|
Chris@16
|
282 struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
|
Chris@16
|
283
|
Chris@16
|
284 template <typename Pixel, bool IsPlanar, typename Alloc>
|
Chris@16
|
285 struct color_space_type<image<Pixel,IsPlanar,Alloc> > : public color_space_type<Pixel> {};
|
Chris@16
|
286
|
Chris@16
|
287 template <typename Pixel, bool IsPlanar, typename Alloc>
|
Chris@16
|
288 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
|
Chris@16
|
289
|
Chris@16
|
290 template <typename Pixel, bool IsPlanar, typename Alloc>
|
Chris@16
|
291 struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
|
Chris@16
|
292
|
Chris@16
|
293 //#ifdef _MSC_VER
|
Chris@16
|
294 //#pragma warning(pop)
|
Chris@16
|
295 //#endif
|
Chris@16
|
296
|
Chris@16
|
297 } } // namespace boost::gil
|
Chris@16
|
298
|
Chris@16
|
299 #endif
|