image.hpp 17 KB

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