Boost GIL


image.hpp
Go to the documentation of this file.
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 
23 
24 #include <cstddef>
25 #include <memory>
26 
27 #include <boost/mpl/bool.hpp>
28 #include <boost/mpl/if.hpp>
29 #include <boost/mpl/arithmetic.hpp>
30 
31 
32 #include "gil_config.hpp"
33 #include "image_view.hpp"
34 #include "metafunctions.hpp"
35 #include "algorithm.hpp"
36 
37 namespace boost { namespace gil {
38 
39 //#ifdef _MSC_VER
40 //#pragma warning(push)
41 //#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)
42 //#endif
43 
57 
58 template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
59 class image {
60 public:
61  typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
62  typedef typename view_type_from_pixel<Pixel, IsPlanar>::type view_t;
63  typedef typename view_t::const_t const_view_t;
64  typedef typename view_t::point_t point_t;
65  typedef typename view_t::coord_t coord_t;
66  typedef typename view_t::value_type value_type;
67  typedef coord_t x_coord_t;
68  typedef coord_t y_coord_t;
69 
70  const point_t& dimensions() const { return _view.dimensions(); }
71  x_coord_t width() const { return _view.width(); }
72  y_coord_t height() const { return _view.height(); }
73 
74  explicit image(std::size_t alignment=0,
75  const Alloc alloc_in = Alloc()) :
76  _memory(0), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
77 
78  // Create with dimensions and optional initial value and alignment
79  image(const point_t& dimensions,
80  std::size_t alignment=0,
81  const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
82  , _allocated_bytes( 0 ) {
83  allocate_and_default_construct(dimensions);
84  }
85 
86  image(x_coord_t width, y_coord_t height,
87  std::size_t alignment=0,
88  const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
89  , _allocated_bytes( 0 ) {
90  allocate_and_default_construct(point_t(width,height));
91  }
92 
93  image(const point_t& dimensions,
94  const Pixel& p_in,
95  std::size_t alignment,
96  const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
97  , _allocated_bytes( 0 ) {
98  allocate_and_fill(dimensions, p_in);
99  }
100  image(x_coord_t width, y_coord_t height,
101  const Pixel& p_in,
102  std::size_t alignment = 0,
103  const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in)
104  , _allocated_bytes ( 0 ) {
105  allocate_and_fill(point_t(width,height),p_in);
106  }
107 
108  image(const image& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
109  , _allocated_bytes( img._allocated_bytes ) {
110  allocate_and_copy(img.dimensions(),img._view);
111  }
112 
113  template <typename P2, bool IP2, typename Alloc2>
114  image(const image<P2,IP2,Alloc2>& img) : _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
115  , _allocated_bytes( img._allocated_bytes ) {
116  allocate_and_copy(img.dimensions(),img._view);
117  }
118 
119  image& operator=(const image& img) {
120  if (dimensions() == img.dimensions())
121  copy_pixels(img._view,_view);
122  else {
123  image tmp(img);
124  swap(tmp);
125  }
126  return *this;
127  }
128 
129  template <typename Img>
130  image& operator=(const Img& img) {
131  if (dimensions() == img.dimensions())
132  copy_pixels(img._view,_view);
133  else {
134  image tmp(img);
135  swap(tmp);
136  }
137  return *this;
138  }
139 
140  ~image() {
141  destruct_pixels(_view);
142  deallocate();
143  }
144 
145  Alloc& allocator() { return _alloc; }
146  Alloc const& allocator() const { return _alloc; }
147 
148  void swap(image& img) { // required by MutableContainerConcept
149  using std::swap;
150  swap(_align_in_bytes, img._align_in_bytes);
151  swap(_memory, img._memory);
152  swap(_view, img._view);
153  swap(_alloc, img._alloc);
154  swap(_allocated_bytes, img._allocated_bytes );
155  }
156 
158  // recreate
160 
161  // without Allocator
162 
163  void recreate( const point_t& dims, std::size_t alignment = 0 )
164  {
165  if( dims == _view.dimensions() && _align_in_bytes == alignment )
166  {
167  return;
168  }
169 
170  _align_in_bytes = alignment;
171 
172  if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
173  {
174  destruct_pixels( _view );
175 
176  create_view( dims
177  , typename mpl::bool_<IsPlanar>()
178  );
179 
180  default_construct_pixels( _view );
181  }
182  else
183  {
184  image tmp( dims, alignment );
185  swap( tmp );
186  }
187  }
188 
189  void recreate( x_coord_t width, y_coord_t height, std::size_t alignment = 0 )
190  {
191  recreate( point_t( width, height ), alignment );
192  }
193 
194 
195  void recreate( const point_t& dims, const Pixel& p_in, std::size_t alignment = 0 )
196  {
197  if( dims == _view.dimensions() && _align_in_bytes == alignment )
198  {
199  return;
200  }
201 
202  _align_in_bytes = alignment;
203 
204  if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
205  {
206  destruct_pixels( _view );
207 
208  create_view( dims
209  , typename mpl::bool_<IsPlanar>()
210  );
211 
212  uninitialized_fill_pixels(_view, p_in);
213  }
214  else
215  {
216  image tmp( dims, p_in, alignment );
217  swap( tmp );
218  }
219  }
220 
221  void recreate( x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment = 0 )
222  {
223  recreate( point_t( width, height ), p_in, alignment );
224  }
225 
226 
227  // with Allocator
228  void recreate(const point_t& dims, std::size_t alignment, const Alloc alloc_in )
229  {
230  if( dims == _view.dimensions()
231  && _align_in_bytes == alignment
232  && alloc_in == _alloc
233  )
234  {
235  return;
236  }
237 
238  _align_in_bytes = alignment;
239 
240  if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
241  {
242  destruct_pixels( _view );
243 
244  create_view( dims
245  , typename mpl::bool_<IsPlanar>()
246  );
247 
248  default_construct_pixels( _view );
249  }
250  else
251  {
252  image tmp( dims, alignment, alloc_in );
253  swap( tmp );
254  }
255  }
256 
257  void recreate( x_coord_t width, y_coord_t height, std::size_t alignment, const Alloc alloc_in )
258  {
259  recreate( point_t( width, height ), alignment, alloc_in );
260  }
261 
262  void recreate(const point_t& dims, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in )
263  {
264  if( dims == _view.dimensions()
265  && _align_in_bytes == alignment
266  && alloc_in == _alloc
267  )
268  {
269  return;
270  }
271 
272  _align_in_bytes = alignment;
273 
274  if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
275  {
276  destruct_pixels( _view );
277 
278  create_view( dims
279  , typename mpl::bool_<IsPlanar>()
280  );
281 
282  uninitialized_fill_pixels(_view, p_in);
283  }
284  else
285  {
286  image tmp( dims, p_in, alignment, alloc_in );
287  swap( tmp );
288  }
289  }
290 
291  void recreate(x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in )
292  {
293  recreate( point_t( width, height ), p_in,alignment, alloc_in );
294  }
295 
296 
297 
298  view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
299 private:
300  unsigned char* _memory;
301  std::size_t _align_in_bytes;
302  allocator_type _alloc;
303 
304  std::size_t _allocated_bytes;
305 
306 
307  void allocate_and_default_construct(const point_t& dimensions) {
308  try {
309  allocate_(dimensions,mpl::bool_<IsPlanar>());
311  } catch(...) { deallocate(); throw; }
312  }
313 
314  void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
315  try {
316  allocate_(dimensions,mpl::bool_<IsPlanar>());
317  uninitialized_fill_pixels(_view, p_in);
318  } catch(...) { deallocate(); throw; }
319  }
320 
321  template <typename View>
322  void allocate_and_copy(const point_t& dimensions, const View& v) {
323  try {
324  allocate_(dimensions,mpl::bool_<IsPlanar>());
325  uninitialized_copy_pixels(v,_view);
326  } catch(...) { deallocate(); throw; }
327  }
328 
329  void deallocate() {
330  if (_memory && _allocated_bytes > 0 )
331  {
332  _alloc.deallocate(_memory, _allocated_bytes );
333  }
334  }
335 
336  std::size_t is_planar_impl( const std::size_t size_in_units
337  , const std::size_t channels_in_image
338  , mpl::true_
339  ) const
340  {
341  return size_in_units * channels_in_image;
342  }
343 
344  std::size_t is_planar_impl( const std::size_t size_in_units
345  , const std::size_t
346  , mpl::false_
347  ) const
348  {
349  return size_in_units;
350  }
351 
352  std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
353 
354  typedef typename view_t::x_iterator x_iterator;
355 
356  // when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
357  const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
359  , mpl::int_< 1 >
360  >::type::value;
361 
362  std::size_t size_in_units = is_planar_impl( get_row_size_in_memunits( dimensions.x ) * dimensions.y
363  , _channels_in_image
364  , typename mpl::bool_<IsPlanar>()
365  );
366 
367  // return the size rounded up to the nearest byte
368  return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
370  + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
371  }
372 
373  std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
374  std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
375  if (_align_in_bytes>0) {
376  std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
377  return align(size_in_memunits, alignment_in_memunits);
378  }
379  return size_in_memunits;
380  }
381 
382  void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
383 
384  _allocated_bytes = total_allocated_size_in_bytes(dimensions);
385  _memory=_alloc.allocate( _allocated_bytes );
386 
387  unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
388  _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
389  }
390 
391  void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
392  std::size_t row_size=get_row_size_in_memunits(dimensions.x);
393  std::size_t plane_size=row_size*dimensions.y;
394 
395  _allocated_bytes = total_allocated_size_in_bytes( dimensions );
396 
397  _memory = _alloc.allocate( _allocated_bytes );
398 
399  unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
400  typename view_t::x_iterator first;
401  for (int i=0; i<num_channels<view_t>::value; ++i) {
402  dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
403  memunit_advance(dynamic_at_c(first,i), plane_size*i);
404  }
405  _view=view_t(dimensions, typename view_t::locator(first, row_size));
406  }
407 
408  void create_view( const point_t& dims
409  , mpl::true_ // is planar
410  )
411  {
412  std::size_t row_size=get_row_size_in_memunits(dims.x);
413  std::size_t plane_size=row_size*dims.y;
414 
415  unsigned char* tmp = ( _align_in_bytes > 0 ) ? (unsigned char*) align( (std::size_t) _memory
416  ,_align_in_bytes
417  )
418  : _memory;
419  typename view_t::x_iterator first;
420 
421  for (int i = 0; i < num_channels< view_t >::value; ++i )
422  {
423  dynamic_at_c( first, i ) = (typename channel_type<view_t>::type*) tmp;
424 
425  memunit_advance( dynamic_at_c(first,i)
426  , plane_size*i
427  );
428  }
429 
430  _view=view_t( dims
431  , typename view_t::locator( first
432  , row_size
433  )
434  );
435  }
436 
437  void create_view( const point_t& dims
438  , mpl::false_ // is planar
439  )
440  {
441  unsigned char* tmp = ( _align_in_bytes > 0 ) ? ( unsigned char* ) align( (std::size_t) _memory
442  , _align_in_bytes
443  )
444  : _memory;
445 
446  _view = view_t( dims
447  , typename view_t::locator( typename view_t::x_iterator( tmp )
448  , get_row_size_in_memunits( dims.x )
449  )
450  );
451  }
452 };
453 
454 template <typename Pixel, bool IsPlanar, typename Alloc>
456  im1.swap(im2);
457 }
458 
459 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
460 bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
461  if ((void*)(&im1)==(void*)(&im2)) return true;
462  if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
463  return equal_pixels(const_view(im1),const_view(im2));
464 }
465 template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
466 bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
467 
471 
473 
475 template <typename Pixel, bool IsPlanar, typename Alloc> inline
476 const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
477 
479 template <typename Pixel, bool IsPlanar, typename Alloc> inline
481  return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
482 }
484 
486 // PixelBasedConcept
488 
489 template <typename Pixel, bool IsPlanar, typename Alloc>
490 struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
491 
492 template <typename Pixel, bool IsPlanar, typename Alloc>
493 struct color_space_type<image<Pixel,IsPlanar,Alloc> > : public color_space_type<Pixel> {};
494 
495 template <typename Pixel, bool IsPlanar, typename Alloc>
496 struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
497 
498 template <typename Pixel, bool IsPlanar, typename Alloc>
499 struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
500 
501 //#ifdef _MSC_VER
502 //#pragma warning(pop)
503 //#endif
504 
505 } } // namespace boost::gil
506 
507 #endif
Definition: pixel_iterator.hpp:126
image view class
void uninitialized_fill_pixels(const View &img_view, const Value &val)
std::uninitialized_fill for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed pixels
Definition: algorithm.hpp:542
Some basic STL-style algorithms when applied to image views.
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:68
metafunctions that construct types or return type properties
BOOST_FORCEINLINE bool equal_pixels(const View1 &v1, const View2 &v2)
std::equal for image views
Definition: algorithm.hpp:957
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition: algorithm.hpp:290
void default_construct_pixels(const View &img_view)
Invokes the in-place default constructor on every pixel of the (uninitialized) view. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place default-constructed pixels.
Definition: algorithm.hpp:672
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition: image.hpp:59
const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:476
GIL configuration file.
void uninitialized_copy_pixels(const View1 &view1, const View2 &view2)
std::uninitialized_copy for image views. Does not support planar heterogeneous views. If an exception is thrown destructs any in-place copy-constructed objects
Definition: algorithm.hpp:725
const image< Pixel, IsPlanar, Alloc >::const_view_t const_view(const image< Pixel, IsPlanar, Alloc > &img)
Returns the constant-pixel view of an image.
Definition: image.hpp:480
Returns the number of channels of a pixel-based GIL construct.
Definition: gil_concept.hpp:61
BOOST_FORCEINLINE void destruct_pixels(const View &img_view)
Invokes the in-place destructor on every pixel of the view.
Definition: algorithm.hpp:485
Returns the type of a view the pixel type, whether it operates on planar data and whether it has a st...
Definition: metafunctions.hpp:426