locator.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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_LOCATOR_HPP
  9. #define BOOST_GIL_LOCATOR_HPP
  10. #include <boost/gil/dynamic_step.hpp>
  11. #include <boost/gil/pixel_iterator.hpp>
  12. #include <boost/gil/point.hpp>
  13. #include <boost/assert.hpp>
  14. #include <cstddef>
  15. namespace boost { namespace gil {
  16. /// Pixel 2D locator
  17. //forward declarations
  18. template <typename P> std::ptrdiff_t memunit_step(const P*);
  19. template <typename P> P* memunit_advanced(const P* p, std::ptrdiff_t diff);
  20. template <typename P> P& memunit_advanced_ref(P* p, std::ptrdiff_t diff);
  21. template <typename Iterator, typename D> struct iterator_add_deref;
  22. template <typename T> class point;
  23. namespace detail {
  24. // helper class specialized for each axis of pixel_2d_locator
  25. template <std::size_t D, typename Loc> class locator_axis;
  26. }
  27. template <typename T> struct channel_type;
  28. template <typename T> struct color_space_type;
  29. template <typename T> struct channel_mapping_type;
  30. template <typename T> struct is_planar;
  31. template <typename T> struct num_channels;
  32. /// Base template for types that model HasTransposedTypeConcept.
  33. /// The type of a locator or a view that has X and Y swapped.
  34. /// By default it is the same.
  35. template <typename LocatorOrView>
  36. struct transposed_type
  37. {
  38. using type = LocatorOrView;
  39. };
  40. /// \class pixel_2d_locator_base
  41. /// \brief base class for models of PixelLocatorConcept
  42. /// \ingroup PixelLocatorModel PixelBasedModel
  43. ///
  44. /// Pixel locator is similar to a pixel iterator, but allows for 2D navigation of pixels within an image view.
  45. /// It has a 2D difference_type and supports random access operations like:
  46. /// \code
  47. /// difference_type offset2(2,3);
  48. /// locator+=offset2;
  49. /// locator[offset2]=my_pixel;
  50. /// \endcode
  51. ///
  52. /// In addition, each coordinate acts as a random-access iterator that can be modified separately:
  53. /// "++locator.x()" or "locator.y()+=10" thereby moving the locator horizontally or vertically.
  54. ///
  55. /// It is called a locator because it doesn't implement the complete interface of a random access iterator.
  56. /// For example, increment and decrement operations don't make sense (no way to specify dimension).
  57. /// Also 2D difference between two locators cannot be computed without knowledge of the X position within the image.
  58. ///
  59. /// This base class provides most of the methods and type aliases needed to create a model of a locator. GIL provides two
  60. /// locator models as subclasses of \p pixel_2d_locator_base. A memory-based locator, \p memory_based_2d_locator and a virtual
  61. /// locator, \p virtual_2d_locator.
  62. /// The minimum functionality a subclass must provide is this:
  63. /// \code
  64. /// class my_locator : public pixel_2d_locator_base<my_locator, ..., ...> { // supply the types for x-iterator and y-iterator
  65. /// using const_t = ...; // read-only locator
  66. ///
  67. /// template <typename Deref> struct add_deref {
  68. /// using type = ...; // locator that invokes the Deref dereference object upon pixel access
  69. /// static type make(const my_locator& loc, const Deref& d);
  70. /// };
  71. ///
  72. /// my_locator();
  73. /// my_locator(const my_locator& pl);
  74. ///
  75. /// // constructors with dynamic step in y (and x). Only valid for locators with dynamic steps
  76. /// my_locator(const my_locator& loc, coord_t y_step);
  77. /// my_locator(const my_locator& loc, coord_t x_step, coord_t y_step, bool transpose);
  78. ///
  79. /// bool operator==(const my_locator& p) const;
  80. ///
  81. /// // return _references_ to horizontal/vertical iterators. Advancing them moves this locator
  82. /// x_iterator& x();
  83. /// y_iterator& y();
  84. /// x_iterator const& x() const;
  85. /// y_iterator const& y() const;
  86. ///
  87. /// // return the vertical distance to another locator. Some models need the horizontal distance to compute it
  88. /// y_coord_t y_distance_to(const my_locator& loc2, x_coord_t xDiff) const;
  89. ///
  90. /// // return true iff incrementing an x-iterator located at the last column will position it at the first
  91. /// // column of the next row. Some models need the image width to determine that.
  92. /// bool is_1d_traversable(x_coord_t width) const;
  93. /// };
  94. /// \endcode
  95. ///
  96. /// Models may choose to override some of the functions in the base class with more efficient versions.
  97. ///
  98. template <typename Loc, typename XIterator, typename YIterator> // The concrete subclass, the X-iterator and the Y-iterator
  99. class pixel_2d_locator_base
  100. {
  101. public:
  102. using x_iterator = XIterator;
  103. using y_iterator = YIterator;
  104. // aliasesrequired by ConstRandomAccessNDLocatorConcept
  105. static const std::size_t num_dimensions=2;
  106. using value_type = typename std::iterator_traits<x_iterator>::value_type;
  107. using reference = typename std::iterator_traits<x_iterator>::reference; // result of dereferencing
  108. using coord_t = typename std::iterator_traits<x_iterator>::difference_type; // 1D difference type (same for all dimensions)
  109. using difference_type = point<coord_t>; // result of operator-(locator,locator)
  110. using point_t = difference_type;
  111. template <std::size_t D> struct axis
  112. {
  113. using coord_t = typename detail::locator_axis<D,Loc>::coord_t;
  114. using iterator = typename detail::locator_axis<D,Loc>::iterator;
  115. };
  116. // aliases required by ConstRandomAccess2DLocatorConcept
  117. using x_coord_t = typename point_t::template axis<0>::coord_t;
  118. using y_coord_t = typename point_t::template axis<1>::coord_t;
  119. bool operator!=(const Loc& p) const { return !(concrete()==p); }
  120. x_iterator x_at(x_coord_t dx, y_coord_t dy) const { Loc tmp=concrete(); tmp+=point_t(dx,dy); return tmp.x(); }
  121. x_iterator x_at(const difference_type& d) const { Loc tmp=concrete(); tmp+=d; return tmp.x(); }
  122. y_iterator y_at(x_coord_t dx, y_coord_t dy) const { Loc tmp=concrete(); tmp+=point_t(dx,dy); return tmp.y(); }
  123. y_iterator y_at(const difference_type& d) const { Loc tmp=concrete(); tmp+=d; return tmp.y(); }
  124. Loc xy_at(x_coord_t dx, y_coord_t dy) const { Loc tmp=concrete(); tmp+=point_t(dx,dy); return tmp; }
  125. Loc xy_at(const difference_type& d) const { Loc tmp=concrete(); tmp+=d; return tmp; }
  126. template <std::size_t D> typename axis<D>::iterator& axis_iterator() { return detail::locator_axis<D,Loc>()(concrete()); }
  127. template <std::size_t D> typename axis<D>::iterator const& axis_iterator() const { return detail::locator_axis<D,Loc>()(concrete()); }
  128. template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return detail::locator_axis<D,Loc>()(concrete(),p); }
  129. reference operator()(x_coord_t dx, y_coord_t dy) const { return *x_at(dx,dy); }
  130. reference operator[](const difference_type& d) const { return *x_at(d.x,d.y); }
  131. reference operator*() const { return *concrete().x(); }
  132. Loc& operator+=(const difference_type& d) { concrete().x()+=d.x; concrete().y()+=d.y; return concrete(); }
  133. Loc& operator-=(const difference_type& d) { concrete().x()-=d.x; concrete().y()-=d.y; return concrete(); }
  134. Loc operator+(const difference_type& d) const { return xy_at(d); }
  135. Loc operator-(const difference_type& d) const { return xy_at(-d); }
  136. // Some locators can cache 2D coordinates for faster subsequent access. By default there is no caching
  137. using cached_location_t = difference_type;
  138. cached_location_t cache_location(const difference_type& d) const { return d; }
  139. cached_location_t cache_location(x_coord_t dx, y_coord_t dy)const { return difference_type(dx,dy); }
  140. private:
  141. Loc& concrete() { return (Loc&)*this; }
  142. const Loc& concrete() const { return (const Loc&)*this; }
  143. template <typename X> friend class pixel_2d_locator;
  144. };
  145. // helper classes for each axis of pixel_2d_locator_base
  146. namespace detail {
  147. template <typename Loc>
  148. class locator_axis<0,Loc> {
  149. using point_t = typename Loc::point_t;
  150. public:
  151. using coord_t = typename point_t::template axis<0>::coord_t;
  152. using iterator = typename Loc::x_iterator;
  153. inline iterator& operator()( Loc& loc) const { return loc.x(); }
  154. inline iterator const& operator()(const Loc& loc) const { return loc.x(); }
  155. inline iterator operator()( Loc& loc, const point_t& d) const { return loc.x_at(d); }
  156. inline iterator operator()(const Loc& loc, const point_t& d) const { return loc.x_at(d); }
  157. };
  158. template <typename Loc>
  159. class locator_axis<1,Loc> {
  160. using point_t = typename Loc::point_t;
  161. public:
  162. using coord_t = typename point_t::template axis<1>::coord_t;
  163. using iterator = typename Loc::y_iterator;
  164. inline iterator& operator()( Loc& loc) const { return loc.y(); }
  165. inline iterator const& operator()(const Loc& loc) const { return loc.y(); }
  166. inline iterator operator()( Loc& loc, const point_t& d) const { return loc.y_at(d); }
  167. inline iterator operator()(const Loc& loc, const point_t& d) const { return loc.y_at(d); }
  168. };
  169. }
  170. template <typename Loc, typename XIt, typename YIt>
  171. struct channel_type<pixel_2d_locator_base<Loc,XIt,YIt> > : public channel_type<XIt> {};
  172. template <typename Loc, typename XIt, typename YIt>
  173. struct color_space_type<pixel_2d_locator_base<Loc,XIt,YIt> > : public color_space_type<XIt> {};
  174. template <typename Loc, typename XIt, typename YIt>
  175. struct channel_mapping_type<pixel_2d_locator_base<Loc,XIt,YIt> > : public channel_mapping_type<XIt> {};
  176. template <typename Loc, typename XIt, typename YIt>
  177. struct is_planar<pixel_2d_locator_base<Loc,XIt,YIt> > : public is_planar<XIt> {};
  178. /// \class memory_based_2d_locator
  179. /// \brief Memory-based pixel locator. Models: PixelLocatorConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept
  180. /// \ingroup PixelLocatorModel PixelBasedModel
  181. ///
  182. /// The class takes a step iterator as a parameter. The step iterator provides navigation along the vertical axis
  183. /// while its base iterator provides horizontal navigation.
  184. ///
  185. /// Each instantiation is optimal in terms of size and efficiency.
  186. /// For example, xy locator over interleaved rgb image results in a step iterator consisting of
  187. /// one std::ptrdiff_t for the row size and one native pointer (8 bytes total). ++locator.x() resolves to pointer
  188. /// increment. At the other extreme, a 2D navigation of the even pixels of a planar CMYK image results in a step
  189. /// iterator consisting of one std::ptrdiff_t for the doubled row size, and one step iterator consisting of
  190. /// one std::ptrdiff_t for the horizontal step of two and a CMYK planar_pixel_iterator consisting of 4 pointers (24 bytes).
  191. /// In this case ++locator.x() results in four native pointer additions.
  192. ///
  193. /// Note also that \p memory_based_2d_locator does not require that its element type be a pixel. It could be
  194. /// instantiated with an iterator whose \p value_type models only \p Regular. In this case the locator
  195. /// models the weaker RandomAccess2DLocatorConcept, and does not model PixelBasedConcept.
  196. /// Many generic algorithms don't require the elements to be pixels.
  197. ////////////////////////////////////////////////////////////////////////////////////////
  198. template <typename StepIterator>
  199. class memory_based_2d_locator : public pixel_2d_locator_base<memory_based_2d_locator<StepIterator>, typename iterator_adaptor_get_base<StepIterator>::type, StepIterator> {
  200. using this_t = memory_based_2d_locator<StepIterator>;
  201. GIL_CLASS_REQUIRE(StepIterator, boost::gil, StepIteratorConcept)
  202. public:
  203. using parent_t = pixel_2d_locator_base<memory_based_2d_locator<StepIterator>, typename iterator_adaptor_get_base<StepIterator>::type, StepIterator>;
  204. using const_t = memory_based_2d_locator<typename const_iterator_type<StepIterator>::type>; // same as this type, but over const values
  205. using coord_t = typename parent_t::coord_t;
  206. using x_coord_t = typename parent_t::x_coord_t;
  207. using y_coord_t = typename parent_t::y_coord_t;
  208. using x_iterator = typename parent_t::x_iterator;
  209. using y_iterator = typename parent_t::y_iterator;
  210. using difference_type = typename parent_t::difference_type;
  211. using reference = typename parent_t::reference;
  212. template <typename Deref> struct add_deref
  213. {
  214. using type = memory_based_2d_locator<typename iterator_add_deref<StepIterator,Deref>::type>;
  215. static type make(const memory_based_2d_locator<StepIterator>& loc, const Deref& nderef) {
  216. return type(iterator_add_deref<StepIterator,Deref>::make(loc.y(),nderef));
  217. }
  218. };
  219. memory_based_2d_locator() {}
  220. memory_based_2d_locator(const StepIterator& yit) : _p(yit) {}
  221. template <typename SI> memory_based_2d_locator(const memory_based_2d_locator<SI>& loc, coord_t y_step) : _p(loc.x(), loc.row_size()*y_step) {}
  222. template <typename SI> memory_based_2d_locator(const memory_based_2d_locator<SI>& loc, coord_t x_step, coord_t y_step, bool transpose=false)
  223. : _p(make_step_iterator(loc.x(),(transpose ? loc.row_size() : loc.pixel_size())*x_step),
  224. (transpose ? loc.pixel_size() : loc.row_size())*y_step ) {}
  225. memory_based_2d_locator(x_iterator xit, std::ptrdiff_t row_bytes) : _p(xit,row_bytes) {}
  226. template <typename X> memory_based_2d_locator(const memory_based_2d_locator<X>& pl) : _p(pl._p) {}
  227. memory_based_2d_locator(const memory_based_2d_locator& pl) : _p(pl._p) {}
  228. memory_based_2d_locator& operator=(memory_based_2d_locator const& other) = default;
  229. bool operator==(const this_t& p) const { return _p==p._p; }
  230. x_iterator const& x() const { return _p.base(); }
  231. y_iterator const& y() const { return _p; }
  232. x_iterator& x() { return _p.base(); }
  233. y_iterator& y() { return _p; }
  234. // These are faster versions of functions already provided in the superclass
  235. x_iterator x_at (x_coord_t dx, y_coord_t dy) const { return memunit_advanced(x(), offset(dx,dy)); }
  236. x_iterator x_at (const difference_type& d) const { return memunit_advanced(x(), offset(d.x,d.y)); }
  237. this_t xy_at (x_coord_t dx, y_coord_t dy) const { return this_t(x_at( dx , dy ), row_size()); }
  238. this_t xy_at (const difference_type& d) const { return this_t(x_at( d.x, d.y), row_size()); }
  239. reference operator()(x_coord_t dx, y_coord_t dy) const { return memunit_advanced_ref(x(),offset(dx,dy)); }
  240. reference operator[](const difference_type& d) const { return memunit_advanced_ref(x(),offset(d.x,d.y)); }
  241. this_t& operator+=(const difference_type& d) { memunit_advance(x(),offset(d.x,d.y)); return *this; }
  242. this_t& operator-=(const difference_type& d) { memunit_advance(x(),offset(-d.x,-d.y)); return *this; }
  243. // Memory-based locators can have 1D caching of 2D relative coordinates
  244. using cached_location_t = std::ptrdiff_t; // type used to store relative location (to allow for more efficient repeated access)
  245. cached_location_t cache_location(const difference_type& d) const { return offset(d.x,d.y); }
  246. cached_location_t cache_location(x_coord_t dx, y_coord_t dy)const { return offset(dx,dy); }
  247. reference operator[](const cached_location_t& loc) const { return memunit_advanced_ref(x(),loc); }
  248. // Only make sense for memory-based locators
  249. std::ptrdiff_t row_size() const { return memunit_step(y()); } // distance in mem units (bytes or bits) between adjacent rows
  250. std::ptrdiff_t pixel_size() const { return memunit_step(x()); } // distance in mem units (bytes or bits) between adjacent pixels on the same row
  251. bool is_1d_traversable(x_coord_t width) const { return row_size()-pixel_size()*width==0; } // is there no gap at the end of each row?
  252. // Returns the vertical distance (it2.y-it1.y) between two x_iterators given the difference of their x positions
  253. std::ptrdiff_t y_distance_to(this_t const& p2, x_coord_t xDiff) const
  254. {
  255. std::ptrdiff_t rowDiff = memunit_distance(x(), p2.x()) - pixel_size() * xDiff;
  256. BOOST_ASSERT((rowDiff % row_size()) == 0);
  257. return rowDiff / row_size();
  258. }
  259. private:
  260. template <typename X> friend class memory_based_2d_locator;
  261. std::ptrdiff_t offset(x_coord_t x, y_coord_t y) const { return y*row_size() + x*pixel_size(); }
  262. StepIterator _p;
  263. };
  264. /////////////////////////////
  265. // PixelBasedConcept
  266. /////////////////////////////
  267. template <typename SI>
  268. struct color_space_type<memory_based_2d_locator<SI> > : public color_space_type<typename memory_based_2d_locator<SI>::parent_t> {
  269. };
  270. template <typename SI>
  271. struct channel_mapping_type<memory_based_2d_locator<SI> > : public channel_mapping_type<typename memory_based_2d_locator<SI>::parent_t> {
  272. };
  273. template <typename SI>
  274. struct is_planar<memory_based_2d_locator<SI> > : public is_planar<typename memory_based_2d_locator<SI>::parent_t> {
  275. };
  276. template <typename SI>
  277. struct channel_type<memory_based_2d_locator<SI> > : public channel_type<typename memory_based_2d_locator<SI>::parent_t> {
  278. };
  279. /////////////////////////////
  280. // HasDynamicXStepTypeConcept
  281. /////////////////////////////
  282. // Take the base iterator of SI (which is typically a step iterator) and change it to have a step in x
  283. template <typename SI>
  284. struct dynamic_x_step_type<memory_based_2d_locator<SI> > {
  285. private:
  286. using base_iterator_t = typename iterator_adaptor_get_base<SI>::type;
  287. using base_iterator_step_t = typename dynamic_x_step_type<base_iterator_t>::type;
  288. using dynamic_step_base_t = typename iterator_adaptor_rebind<SI, base_iterator_step_t>::type;
  289. public:
  290. using type = memory_based_2d_locator<dynamic_step_base_t>;
  291. };
  292. /////////////////////////////
  293. // HasDynamicYStepTypeConcept
  294. /////////////////////////////
  295. template <typename SI>
  296. struct dynamic_y_step_type<memory_based_2d_locator<SI> > {
  297. using type = memory_based_2d_locator<SI>;
  298. };
  299. } } // namespace boost::gil
  300. #endif