pixel_locator.hpp 14 KB

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