pixel_locator.hpp 14 KB

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