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