image_view.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_VIEW_HPP
  9. #define BOOST_GIL_IMAGE_VIEW_HPP
  10. #include <boost/gil/dynamic_step.hpp>
  11. #include <boost/gil/iterator_from_2d.hpp>
  12. #include <cstddef>
  13. #include <iterator>
  14. namespace boost { namespace gil {
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16. /// \class image_view
  17. /// \ingroup ImageViewModel PixelBasedModel
  18. /// \brief A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept
  19. ///
  20. /// Image view consists of a pixel 2D locator (defining the mechanism for navigating in 2D)
  21. /// and the image dimensions.
  22. ///
  23. /// Image views to images are what ranges are to STL containers. They are lightweight objects,
  24. /// that don't own the pixels. It is the user's responsibility that the underlying data remains
  25. /// valid for the lifetime of the image view.
  26. ///
  27. /// Similar to iterators and ranges, constness of views does not extend to constness of pixels.
  28. /// A const \p image_view does not allow changing its location in memory (resizing, moving) but does
  29. /// not prevent one from changing the pixels. The latter requires an image view whose value_type
  30. /// is const.
  31. ///
  32. /// Images have interfaces consistent with STL 1D random access containers, so they can be used
  33. /// directly in STL algorithms like:
  34. /// \code
  35. /// std::fill(img.begin(), img.end(), red_pixel);
  36. /// \endcode
  37. ///
  38. /// In addition, horizontal, vertical and 2D random access iterators are provided.
  39. ///
  40. /// Note also that \p image_view does not require that its element type be a pixel. It could be
  41. /// instantiated with a locator whose \p value_type models only \p Regular. In this case the image
  42. /// view models the weaker RandomAccess2DImageViewConcept, and does not model PixelBasedConcept.
  43. /// Many generic algorithms don't require the elements to be pixels.
  44. ///
  45. ////////////////////////////////////////////////////////////////////////////////////////
  46. template <typename Loc> // Models 2D Pixel Locator
  47. class image_view {
  48. public:
  49. // aliases required by ConstRandomAccessNDImageViewConcept
  50. static const std::size_t num_dimensions=2;
  51. using value_type = typename Loc::value_type;
  52. using reference = typename Loc::reference; // result of dereferencing
  53. using coord_t = typename Loc::coord_t; // 1D difference type (same for all dimensions)
  54. using difference_type = coord_t; // result of operator-(1d_iterator,1d_iterator)
  55. using point_t = typename Loc::point_t;
  56. using locator = Loc;
  57. using const_t = image_view<typename Loc::const_t>; // same as this type, but over const values
  58. template <std::size_t D> struct axis
  59. {
  60. using coord_t = typename Loc::template axis<D>::coord_t; // difference_type along each dimension
  61. using iterator = typename Loc::template axis<D>::iterator; // 1D iterator type along each dimension
  62. };
  63. using iterator = iterator_from_2d<Loc>; // 1D iterator type for each pixel left-to-right inside top-to-bottom
  64. using const_iterator = typename const_t::iterator; // may be used to examine, but not to modify values
  65. using const_reference = typename const_t::reference; // behaves as a const reference
  66. using pointer = typename std::iterator_traits<iterator>::pointer; // behaves as a pointer to the value type
  67. using reverse_iterator = std::reverse_iterator<iterator>;
  68. using size_type = std::size_t;
  69. // aliases required by ConstRandomAccess2DImageViewConcept
  70. using xy_locator = locator;
  71. using x_iterator = typename xy_locator::x_iterator; // pixel iterator along a row
  72. using y_iterator = typename xy_locator::y_iterator; // pixel iterator along a column
  73. using x_coord_t = typename xy_locator::x_coord_t;
  74. using y_coord_t = typename xy_locator::y_coord_t;
  75. template <typename Deref> struct add_deref
  76. {
  77. using type = image_view<typename Loc::template add_deref<Deref>::type>;
  78. static type make(const image_view<Loc>& iv, const Deref& d)
  79. {
  80. return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d));
  81. }
  82. };
  83. image_view() : _dimensions(0,0) {}
  84. template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
  85. template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {}
  86. template <typename L2> image_view(coord_t width, coord_t height, const L2& loc) : _dimensions(x_coord_t(width),y_coord_t(height)), _pixels(loc) {}
  87. template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
  88. image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
  89. template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
  90. template <typename View> bool operator!=(const View& v) const { return !(*this==v); }
  91. template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
  92. /// \brief Exchanges the elements of the current view with those of \a other
  93. /// in constant time.
  94. ///
  95. /// \note Required by the Collection concept
  96. /// \see https://www.boost.org/libs/utility/Collection.html
  97. void swap(image_view<Loc>& other)
  98. {
  99. using boost::gil::swap;
  100. swap(*this, other);
  101. }
  102. /// \brief Returns true if the view has no elements, false otherwise.
  103. ///
  104. /// \note Required by the Collection concept
  105. /// \see https://www.boost.org/libs/utility/Collection.html
  106. bool empty() const { return !(width() > 0 && height() > 0); }
  107. /// \brief Returns a reference to the first element in raster order.
  108. ///
  109. /// \note Required by the ForwardCollection, since view model the concept.
  110. /// \see https://www.boost.org/libs/utility/Collection.html
  111. reference front() const { return *begin(); }
  112. /// \brief Returns a reference to the last element in raster order.
  113. ///
  114. /// \note Required by the ForwardCollection, since view model the concept.
  115. /// \see https://www.boost.org/libs/utility/Collection.html
  116. reference back() const { return *rbegin(); }
  117. const point_t& dimensions() const { return _dimensions; }
  118. const locator& pixels() const { return _pixels; }
  119. x_coord_t width() const { return dimensions().x; }
  120. y_coord_t height() const { return dimensions().y; }
  121. std::size_t num_channels() const { return gil::num_channels<value_type>::value; }
  122. bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); }
  123. //\{@
  124. /// \name 1D navigation
  125. size_type size() const { return width()*height(); }
  126. iterator begin() const { return iterator(_pixels,_dimensions.x); }
  127. iterator end() const { return begin()+(difference_type)size(); } // potential performance problem!
  128. reverse_iterator rbegin() const { return reverse_iterator(end()); }
  129. reverse_iterator rend() const { return reverse_iterator(begin()); }
  130. reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem!
  131. iterator at(difference_type i)const { return begin()+i; }
  132. iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; }
  133. iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
  134. //\}@
  135. //\{@
  136. /// \name 2-D navigation
  137. reference operator()(const point_t& p) const { return _pixels(p.x,p.y); }
  138. reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
  139. template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.template axis_iterator<D>(p); }
  140. xy_locator xy_at(x_coord_t x, y_coord_t y) const { return _pixels+point_t(x_coord_t(x),y_coord_t(y)); }
  141. locator xy_at(const point_t& p) const { return _pixels+p; }
  142. //\}@
  143. //\{@
  144. /// \name X navigation
  145. x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); }
  146. x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); }
  147. x_iterator row_begin(y_coord_t y) const { return x_at(0,y); }
  148. x_iterator row_end(y_coord_t y) const { return x_at(width(),y); }
  149. //\}@
  150. //\{@
  151. /// \name Y navigation
  152. y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); }
  153. y_iterator y_at(const point_t& p) const { return xy_at(p).y(); }
  154. y_iterator col_begin(x_coord_t x) const { return y_at(x,0); }
  155. y_iterator col_end(x_coord_t x) const { return y_at(x,height()); }
  156. //\}@
  157. private:
  158. template <typename L2> friend class image_view;
  159. point_t _dimensions;
  160. xy_locator _pixels;
  161. };
  162. template <typename L2>
  163. inline void swap(image_view<L2>& x, image_view<L2>& y) {
  164. using std::swap;
  165. swap(x._dimensions,y._dimensions);
  166. swap(x._pixels, y._pixels); // TODO: Extend further
  167. }
  168. /////////////////////////////
  169. // PixelBasedConcept
  170. /////////////////////////////
  171. template <typename L>
  172. struct channel_type<image_view<L> > : public channel_type<L> {};
  173. template <typename L>
  174. struct color_space_type<image_view<L> > : public color_space_type<L> {};
  175. template <typename L>
  176. struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
  177. template <typename L>
  178. struct is_planar<image_view<L> > : public is_planar<L> {};
  179. /////////////////////////////
  180. // HasDynamicXStepTypeConcept
  181. /////////////////////////////
  182. template <typename L>
  183. struct dynamic_x_step_type<image_view<L> > {
  184. using type = image_view<typename dynamic_x_step_type<L>::type>;
  185. };
  186. /////////////////////////////
  187. // HasDynamicYStepTypeConcept
  188. /////////////////////////////
  189. template <typename L>
  190. struct dynamic_y_step_type<image_view<L> > {
  191. using type = image_view<typename dynamic_y_step_type<L>::type>;
  192. };
  193. /////////////////////////////
  194. // HasTransposedTypeConcept
  195. /////////////////////////////
  196. template <typename L>
  197. struct transposed_type<image_view<L> > {
  198. using type = image_view<typename transposed_type<L>::type>;
  199. };
  200. }} // namespace boost::gil
  201. #endif