image.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_IMAGE_HPP
  9. #define BOOST_GIL_IMAGE_HPP
  10. #include <boost/gil/algorithm.hpp>
  11. #include <boost/gil/image_view.hpp>
  12. #include <boost/gil/metafunctions.hpp>
  13. #include <boost/mpl/arithmetic.hpp>
  14. #include <boost/mpl/bool.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <cstddef>
  17. #include <memory>
  18. namespace boost { namespace gil {
  19. ////////////////////////////////////////////////////////////////////////////////////////
  20. /// \ingroup ImageModel PixelBasedModel
  21. /// \brief container interface over image view. Models ImageConcept, PixelBasedConcept
  22. ///
  23. /// A 2D container whose elements are pixels. It is templated over the pixel type, a boolean
  24. /// indicating whether it should be planar, and an optional allocator.
  25. ///
  26. /// Note that its element type does not have to be a pixel. \p image can be instantiated with any Regular element,
  27. /// in which case it models the weaker RandomAccess2DImageConcept and does not model PixelBasedConcept
  28. ///
  29. /// When recreating an image of the same or smaller size the memory will be reused if possible.
  30. ///
  31. ////////////////////////////////////////////////////////////////////////////////////////
  32. template< typename Pixel, bool IsPlanar = false, typename Alloc=std::allocator<unsigned char> >
  33. class image {
  34. public:
  35. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  36. using allocator_type = typename Alloc::template rebind<unsigned char>::other;
  37. #else
  38. using allocator_type = typename std::allocator_traits<Alloc>::template rebind_alloc<unsigned char>;
  39. #endif
  40. using view_t = typename view_type_from_pixel<Pixel, IsPlanar>::type;
  41. using const_view_t = typename view_t::const_t;
  42. using point_t = typename view_t::point_t;
  43. using coord_t = typename view_t::coord_t;
  44. using value_type = typename view_t::value_type;
  45. using x_coord_t = coord_t;
  46. using y_coord_t = coord_t;
  47. const point_t& dimensions() const { return _view.dimensions(); }
  48. x_coord_t width() const { return _view.width(); }
  49. y_coord_t height() const { return _view.height(); }
  50. explicit image(std::size_t alignment=0,
  51. const Alloc alloc_in = Alloc()) :
  52. _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in), _allocated_bytes( 0 ) {}
  53. // Create with dimensions and optional initial value and alignment
  54. image(const point_t& dimensions,
  55. std::size_t alignment=0,
  56. const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
  57. , _allocated_bytes( 0 ) {
  58. allocate_and_default_construct(dimensions);
  59. }
  60. image(x_coord_t width, y_coord_t height,
  61. std::size_t alignment=0,
  62. const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
  63. , _allocated_bytes( 0 ) {
  64. allocate_and_default_construct(point_t(width,height));
  65. }
  66. image(const point_t& dimensions,
  67. const Pixel& p_in,
  68. std::size_t alignment,
  69. const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
  70. , _allocated_bytes( 0 ) {
  71. allocate_and_fill(dimensions, p_in);
  72. }
  73. image(x_coord_t width, y_coord_t height,
  74. const Pixel& p_in,
  75. std::size_t alignment = 0,
  76. const Alloc alloc_in = Alloc()) : _memory(nullptr), _align_in_bytes(alignment), _alloc(alloc_in)
  77. , _allocated_bytes ( 0 ) {
  78. allocate_and_fill(point_t(width,height),p_in);
  79. }
  80. image(const image& img) : _memory(nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
  81. , _allocated_bytes( img._allocated_bytes ) {
  82. allocate_and_copy(img.dimensions(),img._view);
  83. }
  84. template <typename P2, bool IP2, typename Alloc2>
  85. image(const image<P2,IP2,Alloc2>& img) : _memory(nullptr), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc)
  86. , _allocated_bytes( img._allocated_bytes ) {
  87. allocate_and_copy(img.dimensions(),img._view);
  88. }
  89. image& operator=(const image& img) {
  90. if (dimensions() == img.dimensions())
  91. copy_pixels(img._view,_view);
  92. else {
  93. image tmp(img);
  94. swap(tmp);
  95. }
  96. return *this;
  97. }
  98. template <typename Img>
  99. image& operator=(const Img& img) {
  100. if (dimensions() == img.dimensions())
  101. copy_pixels(img._view,_view);
  102. else {
  103. image tmp(img);
  104. swap(tmp);
  105. }
  106. return *this;
  107. }
  108. ~image() {
  109. destruct_pixels(_view);
  110. deallocate();
  111. }
  112. Alloc& allocator() { return _alloc; }
  113. Alloc const& allocator() const { return _alloc; }
  114. void swap(image& img) { // required by MutableContainerConcept
  115. using std::swap;
  116. swap(_align_in_bytes, img._align_in_bytes);
  117. swap(_memory, img._memory);
  118. swap(_view, img._view);
  119. swap(_alloc, img._alloc);
  120. swap(_allocated_bytes, img._allocated_bytes );
  121. }
  122. /////////////////////
  123. // recreate
  124. /////////////////////
  125. // without Allocator
  126. void recreate( const point_t& dims, std::size_t alignment = 0 )
  127. {
  128. if( dims == _view.dimensions() && _align_in_bytes == alignment )
  129. {
  130. return;
  131. }
  132. _align_in_bytes = alignment;
  133. if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
  134. {
  135. destruct_pixels( _view );
  136. create_view( dims
  137. , typename mpl::bool_<IsPlanar>()
  138. );
  139. default_construct_pixels( _view );
  140. }
  141. else
  142. {
  143. image tmp( dims, alignment );
  144. swap( tmp );
  145. }
  146. }
  147. void recreate( x_coord_t width, y_coord_t height, std::size_t alignment = 0 )
  148. {
  149. recreate( point_t( width, height ), alignment );
  150. }
  151. void recreate( const point_t& dims, const Pixel& p_in, std::size_t alignment = 0 )
  152. {
  153. if( dims == _view.dimensions() && _align_in_bytes == alignment )
  154. {
  155. return;
  156. }
  157. _align_in_bytes = alignment;
  158. if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
  159. {
  160. destruct_pixels( _view );
  161. create_view( dims
  162. , typename mpl::bool_<IsPlanar>()
  163. );
  164. uninitialized_fill_pixels(_view, p_in);
  165. }
  166. else
  167. {
  168. image tmp( dims, p_in, alignment );
  169. swap( tmp );
  170. }
  171. }
  172. void recreate( x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment = 0 )
  173. {
  174. recreate( point_t( width, height ), p_in, alignment );
  175. }
  176. // with Allocator
  177. void recreate(const point_t& dims, std::size_t alignment, const Alloc alloc_in )
  178. {
  179. if( dims == _view.dimensions()
  180. && _align_in_bytes == alignment
  181. && alloc_in == _alloc
  182. )
  183. {
  184. return;
  185. }
  186. _align_in_bytes = alignment;
  187. if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
  188. {
  189. destruct_pixels( _view );
  190. create_view( dims
  191. , typename mpl::bool_<IsPlanar>()
  192. );
  193. default_construct_pixels( _view );
  194. }
  195. else
  196. {
  197. image tmp( dims, alignment, alloc_in );
  198. swap( tmp );
  199. }
  200. }
  201. void recreate( x_coord_t width, y_coord_t height, std::size_t alignment, const Alloc alloc_in )
  202. {
  203. recreate( point_t( width, height ), alignment, alloc_in );
  204. }
  205. void recreate(const point_t& dims, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in )
  206. {
  207. if( dims == _view.dimensions()
  208. && _align_in_bytes == alignment
  209. && alloc_in == _alloc
  210. )
  211. {
  212. return;
  213. }
  214. _align_in_bytes = alignment;
  215. if( _allocated_bytes >= total_allocated_size_in_bytes( dims ) )
  216. {
  217. destruct_pixels( _view );
  218. create_view( dims
  219. , typename mpl::bool_<IsPlanar>()
  220. );
  221. uninitialized_fill_pixels(_view, p_in);
  222. }
  223. else
  224. {
  225. image tmp( dims, p_in, alignment, alloc_in );
  226. swap( tmp );
  227. }
  228. }
  229. void recreate(x_coord_t width, y_coord_t height, const Pixel& p_in, std::size_t alignment, const Alloc alloc_in )
  230. {
  231. recreate( point_t( width, height ), p_in,alignment, alloc_in );
  232. }
  233. view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
  234. private:
  235. unsigned char* _memory;
  236. std::size_t _align_in_bytes;
  237. allocator_type _alloc;
  238. std::size_t _allocated_bytes;
  239. void allocate_and_default_construct(const point_t& dimensions) {
  240. try {
  241. allocate_(dimensions,mpl::bool_<IsPlanar>());
  242. default_construct_pixels(_view);
  243. } catch(...) { deallocate(); throw; }
  244. }
  245. void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
  246. try {
  247. allocate_(dimensions,mpl::bool_<IsPlanar>());
  248. uninitialized_fill_pixels(_view, p_in);
  249. } catch(...) { deallocate(); throw; }
  250. }
  251. template <typename View>
  252. void allocate_and_copy(const point_t& dimensions, const View& v) {
  253. try {
  254. allocate_(dimensions,mpl::bool_<IsPlanar>());
  255. uninitialized_copy_pixels(v,_view);
  256. } catch(...) { deallocate(); throw; }
  257. }
  258. void deallocate() {
  259. if (_memory && _allocated_bytes > 0 )
  260. {
  261. _alloc.deallocate(_memory, _allocated_bytes );
  262. }
  263. }
  264. std::size_t is_planar_impl( const std::size_t size_in_units
  265. , const std::size_t channels_in_image
  266. , mpl::true_
  267. ) const
  268. {
  269. return size_in_units * channels_in_image;
  270. }
  271. std::size_t is_planar_impl( const std::size_t size_in_units
  272. , const std::size_t
  273. , mpl::false_
  274. ) const
  275. {
  276. return size_in_units;
  277. }
  278. std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
  279. using x_iterator = typename view_t::x_iterator;
  280. // when value_type is a non-pixel, like int or float, num_channels< ... > doesn't work.
  281. const std::size_t _channels_in_image = mpl::eval_if< is_pixel< value_type >
  282. , num_channels< view_t >
  283. , mpl::int_< 1 >
  284. >::type::value;
  285. std::size_t size_in_units = is_planar_impl( get_row_size_in_memunits( dimensions.x ) * dimensions.y
  286. , _channels_in_image
  287. , typename mpl::bool_<IsPlanar>()
  288. );
  289. // return the size rounded up to the nearest byte
  290. return ( size_in_units + byte_to_memunit< x_iterator >::value - 1 )
  291. / byte_to_memunit<x_iterator>::value
  292. + ( _align_in_bytes > 0 ? _align_in_bytes - 1 : 0 ); // add extra padding in case we need to align the first image pixel
  293. }
  294. std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
  295. std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
  296. if (_align_in_bytes>0) {
  297. std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
  298. return align(size_in_memunits, alignment_in_memunits);
  299. }
  300. return size_in_memunits;
  301. }
  302. void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
  303. _allocated_bytes = total_allocated_size_in_bytes(dimensions);
  304. _memory=_alloc.allocate( _allocated_bytes );
  305. unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
  306. _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
  307. }
  308. void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
  309. std::size_t row_size=get_row_size_in_memunits(dimensions.x);
  310. std::size_t plane_size=row_size*dimensions.y;
  311. _allocated_bytes = total_allocated_size_in_bytes( dimensions );
  312. _memory = _alloc.allocate( _allocated_bytes );
  313. unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
  314. typename view_t::x_iterator first;
  315. for (int i=0; i<num_channels<view_t>::value; ++i) {
  316. dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
  317. memunit_advance(dynamic_at_c(first,i), plane_size*i);
  318. }
  319. _view=view_t(dimensions, typename view_t::locator(first, row_size));
  320. }
  321. void create_view( const point_t& dims
  322. , mpl::true_ // is planar
  323. )
  324. {
  325. std::size_t row_size=get_row_size_in_memunits(dims.x);
  326. std::size_t plane_size=row_size*dims.y;
  327. unsigned char* tmp = ( _align_in_bytes > 0 ) ? (unsigned char*) align( (std::size_t) _memory
  328. ,_align_in_bytes
  329. )
  330. : _memory;
  331. typename view_t::x_iterator first;
  332. for (int i = 0; i < num_channels< view_t >::value; ++i )
  333. {
  334. dynamic_at_c( first, i ) = (typename channel_type<view_t>::type*) tmp;
  335. memunit_advance( dynamic_at_c(first,i)
  336. , plane_size*i
  337. );
  338. }
  339. _view=view_t( dims
  340. , typename view_t::locator( first
  341. , row_size
  342. )
  343. );
  344. }
  345. void create_view( const point_t& dims
  346. , mpl::false_ // is planar
  347. )
  348. {
  349. unsigned char* tmp = ( _align_in_bytes > 0 ) ? ( unsigned char* ) align( (std::size_t) _memory
  350. , _align_in_bytes
  351. )
  352. : _memory;
  353. _view = view_t( dims
  354. , typename view_t::locator( typename view_t::x_iterator( tmp )
  355. , get_row_size_in_memunits( dims.x )
  356. )
  357. );
  358. }
  359. };
  360. template <typename Pixel, bool IsPlanar, typename Alloc>
  361. void swap(image<Pixel, IsPlanar, Alloc>& im1,image<Pixel, IsPlanar, Alloc>& im2) {
  362. im1.swap(im2);
  363. }
  364. template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
  365. bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
  366. if ((void*)(&im1)==(void*)(&im2)) return true;
  367. if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
  368. return equal_pixels(const_view(im1),const_view(im2));
  369. }
  370. template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
  371. bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
  372. ///@{
  373. /// \name view, const_view
  374. /// \brief Get an image view from an image
  375. /// \ingroup ImageModel
  376. /// \brief Returns the non-constant-pixel view of an image
  377. template <typename Pixel, bool IsPlanar, typename Alloc> inline
  378. const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
  379. /// \brief Returns the constant-pixel view of an image
  380. template <typename Pixel, bool IsPlanar, typename Alloc> inline
  381. const typename image<Pixel,IsPlanar,Alloc>::const_view_t const_view(const image<Pixel,IsPlanar,Alloc>& img) {
  382. return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
  383. }
  384. ///@}
  385. /////////////////////////////
  386. // PixelBasedConcept
  387. /////////////////////////////
  388. template <typename Pixel, bool IsPlanar, typename Alloc>
  389. struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
  390. template <typename Pixel, bool IsPlanar, typename Alloc>
  391. struct color_space_type<image<Pixel,IsPlanar,Alloc> > : public color_space_type<Pixel> {};
  392. template <typename Pixel, bool IsPlanar, typename Alloc>
  393. struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
  394. template <typename Pixel, bool IsPlanar, typename Alloc>
  395. struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
  396. }} // namespace boost::gil
  397. #endif