pixel_locator.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_CONCEPTS_PIXEL_LOCATOR_HPP
  9. #define BOOST_GIL_CONCEPTS_PIXEL_LOCATOR_HPP
  10. #include <boost/gil/concepts/basic.hpp>
  11. #include <boost/gil/concepts/concept_check.hpp>
  12. #include <boost/gil/concepts/fwd.hpp>
  13. #include <boost/gil/concepts/pixel_dereference.hpp>
  14. #include <boost/gil/concepts/pixel_iterator.hpp>
  15. #include <boost/gil/concepts/point.hpp>
  16. #include <boost/gil/concepts/detail/utility.hpp>
  17. #include <cstddef>
  18. #include <iterator>
  19. #if defined(BOOST_CLANG)
  20. #pragma clang diagnostic push
  21. #pragma clang diagnostic ignored "-Wunused-local-typedefs"
  22. #endif
  23. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  24. #pragma GCC diagnostic push
  25. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  26. #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
  27. #endif
  28. namespace boost { namespace gil {
  29. /// \defgroup LocatorNDConcept RandomAccessNDLocatorConcept
  30. /// \ingroup PixelLocatorConcept
  31. /// \brief N-dimensional locator
  32. /// \defgroup Locator2DConcept RandomAccess2DLocatorConcept
  33. /// \ingroup PixelLocatorConcept
  34. /// \brief 2-dimensional locator
  35. /// \defgroup PixelLocator2DConcept PixelLocatorConcept
  36. /// \ingroup PixelLocatorConcept
  37. /// \brief 2-dimensional locator over pixel data
  38. /// \ingroup LocatorNDConcept
  39. /// \brief N-dimensional locator over immutable values
  40. ///
  41. /// \code
  42. /// concept RandomAccessNDLocatorConcept<Regular Loc>
  43. /// {
  44. /// typename value_type; // value over which the locator navigates
  45. /// typename reference; // result of dereferencing
  46. /// typename difference_type; where PointNDConcept<difference_type>; // return value of operator-.
  47. /// typename const_t; // same as Loc, but operating over immutable values
  48. /// typename cached_location_t; // type to store relative location (for efficient repeated access)
  49. /// typename point_t = difference_type;
  50. ///
  51. /// static const size_t num_dimensions; // dimensionality of the locator
  52. /// where num_dimensions = point_t::num_dimensions;
  53. ///
  54. /// // The difference_type and iterator type along each dimension. The iterators may only differ in
  55. /// // difference_type. Their value_type must be the same as Loc::value_type
  56. /// template <size_t D>
  57. /// struct axis
  58. /// {
  59. /// typename coord_t = point_t::axis<D>::coord_t;
  60. /// typename iterator; where RandomAccessTraversalConcept<iterator>; // iterator along D-th axis.
  61. /// where iterator::value_type == value_type;
  62. /// };
  63. ///
  64. /// // Defines the type of a locator similar to this type, except it invokes Deref upon dereferencing
  65. /// template <PixelDereferenceAdaptorConcept Deref>
  66. /// struct add_deref
  67. /// {
  68. /// typename type;
  69. /// where RandomAccessNDLocatorConcept<type>;
  70. /// static type make(const Loc& loc, const Deref& deref);
  71. /// };
  72. ///
  73. /// Loc& operator+=(Loc&, const difference_type&);
  74. /// Loc& operator-=(Loc&, const difference_type&);
  75. /// Loc operator+(const Loc&, const difference_type&);
  76. /// Loc operator-(const Loc&, const difference_type&);
  77. ///
  78. /// reference operator*(const Loc&);
  79. /// reference operator[](const Loc&, const difference_type&);
  80. ///
  81. /// // Storing relative location for faster repeated access and accessing it
  82. /// cached_location_t Loc::cache_location(const difference_type&) const;
  83. /// reference operator[](const Loc&,const cached_location_t&);
  84. ///
  85. /// // Accessing iterators along a given dimension at the current location or at a given offset
  86. /// template <size_t D> axis<D>::iterator& Loc::axis_iterator();
  87. /// template <size_t D> axis<D>::iterator const& Loc::axis_iterator() const;
  88. /// template <size_t D> axis<D>::iterator Loc::axis_iterator(const difference_type&) const;
  89. /// };
  90. /// \endcode
  91. template <typename Loc>
  92. struct RandomAccessNDLocatorConcept
  93. {
  94. void constraints()
  95. {
  96. gil_function_requires<Regular<Loc>>();
  97. // TODO: Should these be concept-checked instead of ignored? --mloskot
  98. using value_type = typename Loc::value_type;
  99. ignore_unused_variable_warning(value_type{});
  100. // result of dereferencing
  101. using reference = typename Loc::reference;
  102. //ignore_unused_variable_warning(reference{});
  103. // result of operator-(pixel_locator, pixel_locator)
  104. using difference_type = typename Loc::difference_type;
  105. ignore_unused_variable_warning(difference_type{});
  106. // type used to store relative location (to allow for more efficient repeated access)
  107. using cached_location_t = typename Loc::cached_location_t;
  108. ignore_unused_variable_warning(cached_location_t{});
  109. // same as this type, but over const values
  110. using const_t = typename Loc::const_t;
  111. ignore_unused_variable_warning(const_t{});
  112. // same as difference_type
  113. using point_t = typename Loc::point_t;
  114. ignore_unused_variable_warning(point_t{});
  115. static std::size_t const N = Loc::num_dimensions; ignore_unused_variable_warning(N);
  116. using first_it_type = typename Loc::template axis<0>::iterator;
  117. using last_it_type = typename Loc::template axis<N-1>::iterator;
  118. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<first_it_type>>();
  119. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<last_it_type>>();
  120. // point_t must be an N-dimensional point, each dimension of which must
  121. // have the same type as difference_type of the corresponding iterator
  122. gil_function_requires<PointNDConcept<point_t>>();
  123. static_assert(point_t::num_dimensions == N, "");
  124. static_assert(is_same
  125. <
  126. typename std::iterator_traits<first_it_type>::difference_type,
  127. typename point_t::template axis<0>::coord_t
  128. >::value, "");
  129. static_assert(is_same
  130. <
  131. typename std::iterator_traits<last_it_type>::difference_type,
  132. typename point_t::template axis<N-1>::coord_t
  133. >::value, "");
  134. difference_type d;
  135. loc += d;
  136. loc -= d;
  137. loc = loc + d;
  138. loc = loc - d;
  139. reference r1 = loc[d]; ignore_unused_variable_warning(r1);
  140. reference r2 = *loc; ignore_unused_variable_warning(r2);
  141. cached_location_t cl = loc.cache_location(d); ignore_unused_variable_warning(cl);
  142. reference r3 = loc[d]; ignore_unused_variable_warning(r3);
  143. first_it_type fi = loc.template axis_iterator<0>();
  144. fi = loc.template axis_iterator<0>(d);
  145. last_it_type li = loc.template axis_iterator<N-1>();
  146. li = loc.template axis_iterator<N-1>(d);
  147. using deref_t = PixelDereferenceAdaptorArchetype<typename Loc::value_type>;
  148. using dtype = typename Loc::template add_deref<deref_t>::type;
  149. // TODO: infinite recursion - FIXME?
  150. //gil_function_requires<RandomAccessNDLocatorConcept<dtype>>();
  151. }
  152. Loc loc;
  153. };
  154. /// \ingroup Locator2DConcept
  155. /// \brief 2-dimensional locator over immutable values
  156. ///
  157. /// \code
  158. /// concept RandomAccess2DLocatorConcept<RandomAccessNDLocatorConcept Loc>
  159. /// {
  160. /// where num_dimensions==2;
  161. /// where Point2DConcept<point_t>;
  162. ///
  163. /// typename x_iterator = axis<0>::iterator;
  164. /// typename y_iterator = axis<1>::iterator;
  165. /// typename x_coord_t = axis<0>::coord_t;
  166. /// typename y_coord_t = axis<1>::coord_t;
  167. ///
  168. /// // Only available to locators that have dynamic step in Y
  169. /// //Loc::Loc(const Loc& loc, y_coord_t);
  170. ///
  171. /// // Only available to locators that have dynamic step in X and Y
  172. /// //Loc::Loc(const Loc& loc, x_coord_t, y_coord_t, bool transposed=false);
  173. ///
  174. /// x_iterator& Loc::x();
  175. /// x_iterator const& Loc::x() const;
  176. /// y_iterator& Loc::y();
  177. /// y_iterator const& Loc::y() const;
  178. ///
  179. /// x_iterator Loc::x_at(const difference_type&) const;
  180. /// y_iterator Loc::y_at(const difference_type&) const;
  181. /// Loc Loc::xy_at(const difference_type&) const;
  182. ///
  183. /// // x/y versions of all methods that can take difference type
  184. /// x_iterator Loc::x_at(x_coord_t, y_coord_t) const;
  185. /// y_iterator Loc::y_at(x_coord_t, y_coord_t) const;
  186. /// Loc Loc::xy_at(x_coord_t, y_coord_t) const;
  187. /// reference operator()(const Loc&, x_coord_t, y_coord_t);
  188. /// cached_location_t Loc::cache_location(x_coord_t, y_coord_t) const;
  189. ///
  190. /// bool Loc::is_1d_traversable(x_coord_t width) const;
  191. /// y_coord_t Loc::y_distance_to(const Loc& loc2, x_coord_t x_diff) const;
  192. /// };
  193. /// \endcode
  194. template <typename Loc>
  195. struct RandomAccess2DLocatorConcept
  196. {
  197. void constraints()
  198. {
  199. gil_function_requires<RandomAccessNDLocatorConcept<Loc>>();
  200. static_assert(Loc::num_dimensions == 2, "");
  201. using dynamic_x_step_t = typename dynamic_x_step_type<Loc>::type;
  202. using dynamic_y_step_t = typename dynamic_y_step_type<Loc>::type;
  203. using transposed_t = typename transposed_type<Loc>::type;
  204. using cached_location_t = typename Loc::cached_location_t;
  205. gil_function_requires<Point2DConcept<typename Loc::point_t>>();
  206. using x_iterator = typename Loc::x_iterator;
  207. using y_iterator = typename Loc::y_iterator;
  208. using x_coord_t = typename Loc::x_coord_t;
  209. using y_coord_t = typename Loc::y_coord_t;
  210. x_coord_t xd = 0; ignore_unused_variable_warning(xd);
  211. y_coord_t yd = 0; ignore_unused_variable_warning(yd);
  212. typename Loc::difference_type d;
  213. typename Loc::reference r=loc(xd,yd); ignore_unused_variable_warning(r);
  214. dynamic_x_step_t loc2(dynamic_x_step_t(), yd);
  215. dynamic_x_step_t loc3(dynamic_x_step_t(), xd, yd);
  216. using dynamic_xy_step_transposed_t = typename dynamic_y_step_type
  217. <
  218. typename dynamic_x_step_type<transposed_t>::type
  219. >::type;
  220. dynamic_xy_step_transposed_t loc4(loc, xd,yd,true);
  221. bool is_contiguous = loc.is_1d_traversable(xd);
  222. ignore_unused_variable_warning(is_contiguous);
  223. loc.y_distance_to(loc, xd);
  224. loc = loc.xy_at(d);
  225. loc = loc.xy_at(xd, yd);
  226. x_iterator xit = loc.x_at(d);
  227. xit = loc.x_at(xd, yd);
  228. xit = loc.x();
  229. y_iterator yit = loc.y_at(d);
  230. yit = loc.y_at(xd, yd);
  231. yit = loc.y();
  232. cached_location_t cl = loc.cache_location(xd, yd);
  233. ignore_unused_variable_warning(cl);
  234. }
  235. Loc loc;
  236. };
  237. /// \ingroup PixelLocator2DConcept
  238. /// \brief GIL's 2-dimensional locator over immutable GIL pixels
  239. /// \code
  240. /// concept PixelLocatorConcept<RandomAccess2DLocatorConcept Loc>
  241. /// {
  242. /// where PixelValueConcept<value_type>;
  243. /// where PixelIteratorConcept<x_iterator>;
  244. /// where PixelIteratorConcept<y_iterator>;
  245. /// where x_coord_t == y_coord_t;
  246. ///
  247. /// typename coord_t = x_coord_t;
  248. /// };
  249. /// \endcode
  250. template <typename Loc>
  251. struct PixelLocatorConcept
  252. {
  253. void constraints()
  254. {
  255. gil_function_requires<RandomAccess2DLocatorConcept<Loc>>();
  256. gil_function_requires<PixelIteratorConcept<typename Loc::x_iterator>>();
  257. gil_function_requires<PixelIteratorConcept<typename Loc::y_iterator>>();
  258. using coord_t = typename Loc::coord_t;
  259. static_assert(is_same<typename Loc::x_coord_t, typename Loc::y_coord_t>::value, "");
  260. }
  261. Loc loc;
  262. };
  263. namespace detail {
  264. /// \tparam Loc Models RandomAccessNDLocatorConcept
  265. template <typename Loc>
  266. struct RandomAccessNDLocatorIsMutableConcept
  267. {
  268. void constraints()
  269. {
  270. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept
  271. <
  272. typename Loc::template axis<0>::iterator
  273. >>();
  274. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept
  275. <
  276. typename Loc::template axis<Loc::num_dimensions-1>::iterator
  277. >>();
  278. typename Loc::difference_type d; initialize_it(d);
  279. typename Loc::value_type v; initialize_it(v);
  280. typename Loc::cached_location_t cl = loc.cache_location(d);
  281. *loc = v;
  282. loc[d] = v;
  283. loc[cl] = v;
  284. }
  285. Loc loc;
  286. };
  287. // \tparam Loc Models RandomAccess2DLocatorConcept
  288. template <typename Loc>
  289. struct RandomAccess2DLocatorIsMutableConcept
  290. {
  291. void constraints()
  292. {
  293. gil_function_requires<detail::RandomAccessNDLocatorIsMutableConcept<Loc>>();
  294. typename Loc::x_coord_t xd = 0; ignore_unused_variable_warning(xd);
  295. typename Loc::y_coord_t yd = 0; ignore_unused_variable_warning(yd);
  296. typename Loc::value_type v; initialize_it(v);
  297. loc(xd, yd) = v;
  298. }
  299. Loc loc;
  300. };
  301. } // namespace detail
  302. /// \ingroup LocatorNDConcept
  303. /// \brief N-dimensional locator over mutable pixels
  304. ///
  305. /// \code
  306. /// concept MutableRandomAccessNDLocatorConcept<RandomAccessNDLocatorConcept Loc>
  307. /// {
  308. /// where Mutable<reference>;
  309. /// };
  310. /// \endcode
  311. template <typename Loc>
  312. struct MutableRandomAccessNDLocatorConcept
  313. {
  314. void constraints()
  315. {
  316. gil_function_requires<RandomAccessNDLocatorConcept<Loc>>();
  317. gil_function_requires<detail::RandomAccessNDLocatorIsMutableConcept<Loc>>();
  318. }
  319. };
  320. /// \ingroup Locator2DConcept
  321. /// \brief 2-dimensional locator over mutable pixels
  322. ///
  323. /// \code
  324. /// concept MutableRandomAccess2DLocatorConcept<RandomAccess2DLocatorConcept Loc>
  325. /// : MutableRandomAccessNDLocatorConcept<Loc> {};
  326. /// \endcode
  327. template <typename Loc>
  328. struct MutableRandomAccess2DLocatorConcept
  329. {
  330. void constraints()
  331. {
  332. gil_function_requires<RandomAccess2DLocatorConcept<Loc>>();
  333. gil_function_requires<detail::RandomAccess2DLocatorIsMutableConcept<Loc>>();
  334. }
  335. };
  336. /// \ingroup PixelLocator2DConcept
  337. /// \brief GIL's 2-dimensional locator over mutable GIL pixels
  338. ///
  339. /// \code
  340. /// concept MutablePixelLocatorConcept<PixelLocatorConcept Loc>
  341. /// : MutableRandomAccess2DLocatorConcept<Loc> {};
  342. /// \endcode
  343. template <typename Loc>
  344. struct MutablePixelLocatorConcept
  345. {
  346. void constraints()
  347. {
  348. gil_function_requires<PixelLocatorConcept<Loc>>();
  349. gil_function_requires<detail::RandomAccess2DLocatorIsMutableConcept<Loc>>();
  350. }
  351. };
  352. }} // namespace boost::gil
  353. #if defined(BOOST_CLANG)
  354. #pragma clang diagnostic pop
  355. #endif
  356. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  357. #pragma GCC diagnostic pop
  358. #endif
  359. #endif