image.hpp 20 KB

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