image.hpp 18 KB

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