virtual_locator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_VIRTUAL_LOCATOR_HPP
  9. #define BOOST_GIL_VIRTUAL_LOCATOR_HPP
  10. #include <boost/gil/dynamic_step.hpp>
  11. #include <boost/gil/position_iterator.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/iterator/iterator_facade.hpp>
  14. namespace boost { namespace gil {
  15. /// \ingroup PixelLocatorModel PixelBasedModel
  16. /// \brief A 2D locator over a virtual image
  17. /// Upon dereferencing, invokes a given function object passing it its coordinates.
  18. /// Models:
  19. /// PixelLocatorConcept,
  20. /// HasDynamicXStepTypeConcept,
  21. /// HasDynamicYStepTypeConcept,
  22. /// HasTransposedTypeConcept
  23. ///
  24. /// \tparam DerefFn Function object that given a point returns a reference.
  25. /// Models PixelDereferenceAdaptorConcept.
  26. /// \tparam IsTransposed Indicates if locator should navigate in transposed mode.
  27. template <typename DerefFn, bool IsTransposed>
  28. class virtual_2d_locator
  29. : public pixel_2d_locator_base
  30. <
  31. virtual_2d_locator<DerefFn, IsTransposed>,
  32. position_iterator<DerefFn, IsTransposed>,
  33. position_iterator<DerefFn, 1-IsTransposed>
  34. >
  35. {
  36. using this_t = virtual_2d_locator<DerefFn, IsTransposed>;
  37. public:
  38. using parent_t = pixel_2d_locator_base
  39. <
  40. virtual_2d_locator<DerefFn, IsTransposed>,
  41. position_iterator<DerefFn, IsTransposed>,
  42. position_iterator<DerefFn, 1-IsTransposed>
  43. >;
  44. using const_t = virtual_2d_locator<typename DerefFn::const_t, IsTransposed>;
  45. using deref_fn_t = DerefFn;
  46. using point_t = typename parent_t::point_t;
  47. using coord_t = typename parent_t::coord_t;
  48. using x_coord_t = typename parent_t::x_coord_t;
  49. using y_coord_t = typename parent_t::y_coord_t;
  50. using x_iterator = typename parent_t::x_iterator;
  51. using y_iterator = typename parent_t::y_iterator;
  52. template <typename NewDerefFn>
  53. struct add_deref
  54. {
  55. using type = virtual_2d_locator<deref_compose<NewDerefFn, DerefFn>, IsTransposed>;
  56. static type make(this_t const& loc, NewDerefFn const& new_deref_fn)
  57. {
  58. return type(loc.pos(), loc.step(),
  59. deref_compose<NewDerefFn, DerefFn>(new_deref_fn, loc.deref_fn()));
  60. }
  61. };
  62. virtual_2d_locator(
  63. point_t const& p = {0, 0},
  64. point_t const& step = {1, 1},
  65. deref_fn_t const& deref_fn = deref_fn_t())
  66. : y_pos_(p, step, deref_fn)
  67. {}
  68. template <typename D, bool TR>
  69. virtual_2d_locator(virtual_2d_locator<D, TR> const &loc, coord_t y_step)
  70. : y_pos_(loc.pos(), point_t(loc.step().x, loc.step().y * y_step), loc.deref_fn())
  71. {}
  72. template <typename D, bool TR>
  73. virtual_2d_locator(virtual_2d_locator<D, TR> const& loc, coord_t x_step, coord_t y_step, bool transpose = false)
  74. : y_pos_(loc.pos()
  75. , transpose ?
  76. point_t(loc.step().x * y_step, loc.step().y * x_step) :
  77. point_t(loc.step().x * x_step, loc.step().y * y_step)
  78. , loc.deref_fn())
  79. {
  80. BOOST_ASSERT(transpose == (IsTransposed != TR));
  81. }
  82. template <typename D, bool TR>
  83. virtual_2d_locator(virtual_2d_locator<D, TR> const& other) : y_pos_(other.y_pos_) {}
  84. virtual_2d_locator(virtual_2d_locator const& other) : y_pos_(other.y_pos_) {}
  85. virtual_2d_locator& operator=(virtual_2d_locator const& other) = default;
  86. bool operator==(const this_t& p) const { return y_pos_ == p.y_pos_; }
  87. auto x() -> x_iterator&
  88. {
  89. return *gil_reinterpret_cast<x_iterator*>(this);
  90. }
  91. auto x() const -> x_iterator const&
  92. {
  93. return *gil_reinterpret_cast_c<x_iterator const*>(this);
  94. }
  95. auto y() -> y_iterator& { return y_pos_; }
  96. auto y() const -> y_iterator const& { return y_pos_; }
  97. /// Returns the y distance between two x_iterators given the difference of their x positions
  98. auto y_distance_to(this_t const& it2, x_coord_t) const -> y_coord_t
  99. {
  100. return (it2.pos()[1 - IsTransposed] - pos()[1 - IsTransposed])
  101. / step()[1 - IsTransposed];
  102. }
  103. /// \todo TODO: is there no gap at the end of each row?
  104. /// i.e. can we use x_iterator to visit every pixel instead of nested loops?
  105. bool is_1d_traversable(x_coord_t) const { return false; }
  106. // Methods specific for virtual 2D locator
  107. auto pos() const -> point_t const& { return y_pos_.pos(); }
  108. auto step() const -> point_t const& { return y_pos_.step(); }
  109. auto deref_fn() const -> deref_fn_t const& { return y_pos_.deref_fn(); }
  110. private:
  111. template <typename D, bool TR>
  112. friend class virtual_2d_locator;
  113. y_iterator y_pos_; // current position, the step and the dereference object
  114. };
  115. /////////////////////////////
  116. // PixelBasedConcept
  117. /////////////////////////////
  118. template <typename D, bool TR>
  119. struct channel_type<virtual_2d_locator<D, TR>>
  120. : channel_type<typename virtual_2d_locator<D, TR>::parent_t>
  121. {
  122. };
  123. template <typename D, bool TR>
  124. struct color_space_type<virtual_2d_locator<D, TR>>
  125. : color_space_type<typename virtual_2d_locator<D, TR>::parent_t>
  126. {
  127. };
  128. template <typename D, bool TR>
  129. struct channel_mapping_type<virtual_2d_locator<D, TR>>
  130. : channel_mapping_type<typename virtual_2d_locator<D, TR>::parent_t>
  131. {
  132. };
  133. template <typename D, bool TR>
  134. struct is_planar<virtual_2d_locator<D, TR>>
  135. : is_planar<typename virtual_2d_locator<D, TR>::parent_t>
  136. {
  137. };
  138. /////////////////////////////
  139. // HasDynamicXStepTypeConcept
  140. /////////////////////////////
  141. template <typename D, bool TR>
  142. struct dynamic_x_step_type<virtual_2d_locator<D,TR>>
  143. {
  144. using type = virtual_2d_locator<D,TR>;
  145. };
  146. /////////////////////////////
  147. // HasDynamicYStepTypeConcept
  148. /////////////////////////////
  149. template <typename D, bool TR>
  150. struct dynamic_y_step_type<virtual_2d_locator<D,TR>>
  151. {
  152. using type = virtual_2d_locator<D,TR>;
  153. };
  154. /////////////////////////////
  155. // HasTransposedTypeConcept
  156. /////////////////////////////
  157. template <typename D, bool IsTransposed>
  158. struct transposed_type<virtual_2d_locator<D,IsTransposed>>
  159. {
  160. using type = virtual_2d_locator<D,1-IsTransposed>;
  161. };
  162. }} // namespace boost::gil
  163. #endif