image.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "gil_config.hpp"
  22. #include "image_view.hpp"
  23. #include "metafunctions.hpp"
  24. #include "algorithm.hpp"
  25. namespace boost { namespace gil {
  26. //#ifdef _MSC_VER
  27. //#pragma warning(push)
  28. //#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)
  29. //#endif
  30. ////////////////////////////////////////////////////////////////////////////////////////
  31. /// \ingroup ImageModel PixelBasedModel
  32. /// \brief container interface over image view. Models ImageConcept, PixelBasedConcept
  33. ///
  34. /// A 2D container whose elements are pixels. It is templated over the pixel type, a boolean
  35. /// indicating whether it should be planar, and an optional allocator.
  36. ///
  37. /// Note that its element type does not have to be a pixel. \p image can be instantiated with any Regular element,
  38. /// in which case it models the weaker RandomAccess2DImageConcept and does not model PixelBasedConcept
  39. ///
  40. ////////////////////////////////////////////////////////////////////////////////////////
  41. template <typename Pixel, bool IsPlanar, typename Alloc=std::allocator<unsigned char> >
  42. class image {
  43. public:
  44. typedef typename Alloc::template rebind<unsigned char>::other allocator_type;
  45. typedef typename view_type_from_pixel<Pixel, IsPlanar>::type view_t;
  46. typedef typename view_t::const_t const_view_t;
  47. typedef typename view_t::point_t point_t;
  48. typedef typename view_t::coord_t coord_t;
  49. typedef typename view_t::value_type value_type;
  50. typedef coord_t x_coord_t;
  51. typedef coord_t y_coord_t;
  52. const point_t& dimensions() const { return _view.dimensions(); }
  53. x_coord_t width() const { return _view.width(); }
  54. y_coord_t height() const { return _view.height(); }
  55. explicit image(std::size_t alignment=0,
  56. const Alloc alloc_in = Alloc()) :
  57. _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {}
  58. // Create with dimensions and optional initial value and alignment
  59. image(const point_t& dimensions,
  60. std::size_t alignment=0,
  61. const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
  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(0), _align_in_bytes(alignment), _alloc(alloc_in) {
  67. allocate_and_default_construct(point_t(width,height));
  68. }
  69. image(const point_t& dimensions,
  70. const Pixel& p_in,
  71. std::size_t alignment,
  72. const Alloc alloc_in = Alloc()) :
  73. _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
  74. allocate_and_fill(dimensions, p_in);
  75. }
  76. image(x_coord_t width, y_coord_t height,
  77. const Pixel& p_in,
  78. std::size_t alignment,
  79. const Alloc alloc_in = Alloc()) :
  80. _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
  81. allocate_and_fill(point_t(width,height),p_in);
  82. }
  83. image(const image& img) :
  84. _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
  85. allocate_and_copy(img.dimensions(),img._view);
  86. }
  87. template <typename P2, bool IP2, typename Alloc2>
  88. image(const image<P2,IP2,Alloc2>& img) :
  89. _memory(0), _align_in_bytes(img._align_in_bytes), _alloc(img._alloc) {
  90. allocate_and_copy(img.dimensions(),img._view);
  91. }
  92. image& operator=(const image& img) {
  93. if (dimensions() == img.dimensions())
  94. copy_pixels(img._view,_view);
  95. else {
  96. image tmp(img);
  97. swap(tmp);
  98. }
  99. return *this;
  100. }
  101. template <typename Img>
  102. image& operator=(const Img& img) {
  103. if (dimensions() == img.dimensions())
  104. copy_pixels(img._view,_view);
  105. else {
  106. image tmp(img);
  107. swap(tmp);
  108. }
  109. return *this;
  110. }
  111. ~image() {
  112. destruct_pixels(_view);
  113. deallocate(_view.dimensions());
  114. }
  115. Alloc& allocator() { return _alloc; }
  116. Alloc const& allocator() const { return _alloc; }
  117. void swap(image& img) { // required by MutableContainerConcept
  118. using std::swap;
  119. swap(_align_in_bytes, img._align_in_bytes);
  120. swap(_memory, img._memory);
  121. swap(_view, img._view);
  122. swap(_alloc, img._alloc);
  123. }
  124. void recreate(const point_t& dims, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
  125. if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
  126. image tmp(dims, alignment, alloc_in);
  127. swap(tmp);
  128. }
  129. }
  130. void recreate(x_coord_t width, y_coord_t height, std::size_t alignment=0, const Alloc alloc_in = Alloc()) {
  131. recreate(point_t(width,height),alignment,alloc_in);
  132. }
  133. void recreate(const point_t& dims,
  134. const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
  135. if (dims!=_view.dimensions() || _align_in_bytes!=alignment || alloc_in!=_alloc) {
  136. image tmp(dims, p_in, alignment, alloc_in);
  137. swap(tmp);
  138. }
  139. }
  140. void recreate(x_coord_t width, y_coord_t height,
  141. const Pixel& p_in, std::size_t alignment, const Alloc alloc_in = Alloc()) {
  142. recreate(point_t(width,height),p_in,alignment,alloc_in);
  143. }
  144. view_t _view; // contains pointer to the pixels, the image size and ways to navigate pixels
  145. private:
  146. unsigned char* _memory;
  147. std::size_t _align_in_bytes;
  148. allocator_type _alloc;
  149. void allocate_and_default_construct(const point_t& dimensions) {
  150. try {
  151. allocate_(dimensions,mpl::bool_<IsPlanar>());
  152. default_construct_pixels(_view);
  153. } catch(...) { deallocate(dimensions); throw; }
  154. }
  155. void allocate_and_fill(const point_t& dimensions, const Pixel& p_in) {
  156. try {
  157. allocate_(dimensions,mpl::bool_<IsPlanar>());
  158. uninitialized_fill_pixels(_view, p_in);
  159. } catch(...) { deallocate(dimensions); throw; }
  160. }
  161. template <typename View>
  162. void allocate_and_copy(const point_t& dimensions, const View& v) {
  163. try {
  164. allocate_(dimensions,mpl::bool_<IsPlanar>());
  165. uninitialized_copy_pixels(v,_view);
  166. } catch(...) { deallocate(dimensions); throw; }
  167. }
  168. void deallocate(const point_t& dimensions) {
  169. if (_memory) _alloc.deallocate(_memory, total_allocated_size_in_bytes(dimensions));
  170. }
  171. std::size_t total_allocated_size_in_bytes(const point_t& dimensions) const {
  172. std::size_t size_in_units = get_row_size_in_memunits(dimensions.x)*dimensions.y;
  173. if (IsPlanar)
  174. size_in_units = size_in_units*num_channels<view_t>::value;
  175. // return the size rounded up to the nearest byte
  176. return (size_in_units + byte_to_memunit<typename view_t::x_iterator>::value - 1) / byte_to_memunit<typename view_t::x_iterator>::value
  177. + (_align_in_bytes>0 ? _align_in_bytes-1:0); // add extra padding in case we need to align the first image pixel
  178. }
  179. std::size_t get_row_size_in_memunits(x_coord_t width) const { // number of units per row
  180. std::size_t size_in_memunits = width*memunit_step(typename view_t::x_iterator());
  181. if (_align_in_bytes>0) {
  182. std::size_t alignment_in_memunits=_align_in_bytes*byte_to_memunit<typename view_t::x_iterator>::value;
  183. return align(size_in_memunits, alignment_in_memunits);
  184. }
  185. return size_in_memunits;
  186. }
  187. void allocate_(const point_t& dimensions, mpl::false_) { // if it throws and _memory!=0 the client must deallocate _memory
  188. _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
  189. unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
  190. _view=view_t(dimensions,typename view_t::locator(typename view_t::x_iterator(tmp),get_row_size_in_memunits(dimensions.x)));
  191. }
  192. void allocate_(const point_t& dimensions, mpl::true_) { // if it throws and _memory!=0 the client must deallocate _memory
  193. std::size_t row_size=get_row_size_in_memunits(dimensions.x);
  194. std::size_t plane_size=row_size*dimensions.y;
  195. _memory=_alloc.allocate(total_allocated_size_in_bytes(dimensions));
  196. unsigned char* tmp=(_align_in_bytes>0) ? (unsigned char*)align((std::size_t)_memory,_align_in_bytes) : _memory;
  197. typename view_t::x_iterator first;
  198. for (int i=0; i<num_channels<view_t>::value; ++i) {
  199. dynamic_at_c(first,i) = (typename channel_type<view_t>::type*)tmp;
  200. memunit_advance(dynamic_at_c(first,i), plane_size*i);
  201. }
  202. _view=view_t(dimensions, typename view_t::locator(first, row_size));
  203. }
  204. };
  205. template <typename Pixel, bool IsPlanar, typename Alloc>
  206. void swap(image<Pixel, IsPlanar, Alloc>& im1,image<Pixel, IsPlanar, Alloc>& im2) {
  207. im1.swap(im2);
  208. }
  209. template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
  210. bool operator==(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {
  211. if ((void*)(&im1)==(void*)(&im2)) return true;
  212. if (const_view(im1).dimensions()!=const_view(im2).dimensions()) return false;
  213. return equal_pixels(const_view(im1),const_view(im2));
  214. }
  215. template <typename Pixel1, bool IsPlanar1, typename Alloc1, typename Pixel2, bool IsPlanar2, typename Alloc2>
  216. bool operator!=(const image<Pixel1,IsPlanar1,Alloc1>& im1,const image<Pixel2,IsPlanar2,Alloc2>& im2) {return !(im1==im2);}
  217. ///@{
  218. /// \name view, const_view
  219. /// \brief Get an image view from an image
  220. /// \ingroup ImageModel
  221. /// \brief Returns the non-constant-pixel view of an image
  222. template <typename Pixel, bool IsPlanar, typename Alloc> inline
  223. const typename image<Pixel,IsPlanar,Alloc>::view_t& view(image<Pixel,IsPlanar,Alloc>& img) { return img._view; }
  224. /// \brief Returns the constant-pixel view of an image
  225. template <typename Pixel, bool IsPlanar, typename Alloc> inline
  226. const typename image<Pixel,IsPlanar,Alloc>::const_view_t const_view(const image<Pixel,IsPlanar,Alloc>& img) {
  227. return static_cast<const typename image<Pixel,IsPlanar,Alloc>::const_view_t>(img._view);
  228. }
  229. ///@}
  230. /////////////////////////////
  231. // PixelBasedConcept
  232. /////////////////////////////
  233. template <typename Pixel, bool IsPlanar, typename Alloc>
  234. struct channel_type<image<Pixel,IsPlanar,Alloc> > : public channel_type<Pixel> {};
  235. template <typename Pixel, bool IsPlanar, typename Alloc>
  236. struct color_space_type<image<Pixel,IsPlanar,Alloc> > : public color_space_type<Pixel> {};
  237. template <typename Pixel, bool IsPlanar, typename Alloc>
  238. struct channel_mapping_type<image<Pixel,IsPlanar,Alloc> > : public channel_mapping_type<Pixel> {};
  239. template <typename Pixel, bool IsPlanar, typename Alloc>
  240. struct is_planar<image<Pixel,IsPlanar,Alloc> > : public mpl::bool_<IsPlanar> {};
  241. //#ifdef _MSC_VER
  242. //#pragma warning(pop)
  243. //#endif
  244. } } // namespace boost::gil
  245. #endif