image.hpp 21 KB

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