pixel_iterator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_ITERATOR_HPP
  9. #define BOOST_GIL_CONCEPTS_PIXEL_ITERATOR_HPP
  10. #include <boost/gil/concepts/channel.hpp>
  11. #include <boost/gil/concepts/color.hpp>
  12. #include <boost/gil/concepts/concept_check.hpp>
  13. #include <boost/gil/concepts/fwd.hpp>
  14. #include <boost/gil/concepts/pixel.hpp>
  15. #include <boost/gil/concepts/pixel_based.hpp>
  16. #include <boost/iterator/iterator_concepts.hpp>
  17. #include <cstddef>
  18. #include <iterator>
  19. #include <type_traits>
  20. #if defined(BOOST_CLANG)
  21. #pragma clang diagnostic push
  22. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  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. #endif
  29. namespace boost { namespace gil {
  30. // Forward declarations
  31. template <typename It> struct const_iterator_type;
  32. template <typename It> struct iterator_is_mutable;
  33. template <typename It> struct is_iterator_adaptor;
  34. template <typename It, typename NewBaseIt> struct iterator_adaptor_rebind;
  35. template <typename It> struct iterator_adaptor_get_base;
  36. // These iterator mutability concepts are taken from Boost concept_check.hpp.
  37. // Isolating mutability to result in faster compile time
  38. namespace detail {
  39. // Preconditions: TT Models boost_concepts::ForwardTraversalConcept
  40. template <class TT>
  41. struct ForwardIteratorIsMutableConcept
  42. {
  43. void constraints()
  44. {
  45. auto const tmp = *i;
  46. *i++ = tmp; // require postincrement and assignment
  47. }
  48. TT i;
  49. };
  50. // Preconditions: TT Models boost::BidirectionalIteratorConcept
  51. template <class TT>
  52. struct BidirectionalIteratorIsMutableConcept
  53. {
  54. void constraints()
  55. {
  56. gil_function_requires< ForwardIteratorIsMutableConcept<TT>>();
  57. auto const tmp = *i;
  58. *i-- = tmp; // require postdecrement and assignment
  59. }
  60. TT i;
  61. };
  62. // Preconditions: TT Models boost_concepts::RandomAccessTraversalConcept
  63. template <class TT>
  64. struct RandomAccessIteratorIsMutableConcept
  65. {
  66. void constraints()
  67. {
  68. gil_function_requires<BidirectionalIteratorIsMutableConcept<TT>>();
  69. typename std::iterator_traits<TT>::difference_type n = 0;
  70. ignore_unused_variable_warning(n);
  71. i[n] = *i; // require element access and assignment
  72. }
  73. TT i;
  74. };
  75. // Iterators that can be used as the base of memory_based_step_iterator require some additional functions
  76. // \tparam Iterator Models boost_concepts::RandomAccessTraversalConcept
  77. template <typename Iterator>
  78. struct RandomAccessIteratorIsMemoryBasedConcept
  79. {
  80. void constraints()
  81. {
  82. std::ptrdiff_t bs = memunit_step(it);
  83. ignore_unused_variable_warning(bs);
  84. it = memunit_advanced(it, 3);
  85. std::ptrdiff_t bd = memunit_distance(it, it);
  86. ignore_unused_variable_warning(bd);
  87. memunit_advance(it,3);
  88. // for performace you may also provide a customized implementation of memunit_advanced_ref
  89. }
  90. Iterator it;
  91. };
  92. /// \tparam Iterator Models PixelIteratorConcept
  93. template <typename Iterator>
  94. struct PixelIteratorIsMutableConcept
  95. {
  96. void constraints()
  97. {
  98. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<Iterator>>();
  99. using ref_t = typename std::remove_reference
  100. <
  101. typename std::iterator_traits<Iterator>::reference
  102. >::type;
  103. using channel_t = typename element_type<ref_t>::type;
  104. gil_function_requires<detail::ChannelIsMutableConcept<channel_t>>();
  105. }
  106. };
  107. } // namespace detail
  108. /// \ingroup PixelLocatorConcept
  109. /// \brief Concept for locators and views that can define a type just like the given locator or view, except X and Y is swapped
  110. /// \code
  111. /// concept HasTransposedTypeConcept<typename T>
  112. /// {
  113. /// typename transposed_type<T>;
  114. /// where Metafunction<transposed_type<T> >;
  115. /// };
  116. /// \endcode
  117. template <typename T>
  118. struct HasTransposedTypeConcept
  119. {
  120. void constraints()
  121. {
  122. using type = typename transposed_type<T>::type;
  123. ignore_unused_variable_warning(type{});
  124. }
  125. };
  126. /// \defgroup PixelIteratorConceptPixelIterator PixelIteratorConcept
  127. /// \ingroup PixelIteratorConcept
  128. /// \brief STL iterator over pixels
  129. /// \ingroup PixelIteratorConceptPixelIterator
  130. /// \brief An STL random access traversal iterator over a model of PixelConcept.
  131. ///
  132. /// GIL's iterators must also provide the following metafunctions:
  133. /// - \p const_iterator_type<Iterator>: Returns a read-only equivalent of \p Iterator
  134. /// - \p iterator_is_mutable<Iterator>: Returns whether the given iterator is read-only or mutable
  135. /// - \p is_iterator_adaptor<Iterator>: Returns whether the given iterator is an adaptor over another iterator. See IteratorAdaptorConcept for additional requirements of adaptors.
  136. ///
  137. /// \code
  138. /// concept PixelIteratorConcept<typename Iterator>
  139. /// : boost_concepts::RandomAccessTraversalConcept<Iterator>, PixelBasedConcept<Iterator>
  140. /// {
  141. /// where PixelValueConcept<value_type>;
  142. /// typename const_iterator_type<It>::type;
  143. /// where PixelIteratorConcept<const_iterator_type<It>::type>;
  144. /// static const bool iterator_is_mutable<It>::value;
  145. /// static const bool is_iterator_adaptor<It>::value; // is it an iterator adaptor
  146. /// };
  147. /// \endcode
  148. template <typename Iterator>
  149. struct PixelIteratorConcept
  150. {
  151. void constraints()
  152. {
  153. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<Iterator>>();
  154. gil_function_requires<PixelBasedConcept<Iterator>>();
  155. using value_type = typename std::iterator_traits<Iterator>::value_type;
  156. gil_function_requires<PixelValueConcept<value_type>>();
  157. using const_t = typename const_iterator_type<Iterator>::type;
  158. static bool const is_mutable = iterator_is_mutable<Iterator>::value;
  159. ignore_unused_variable_warning(is_mutable);
  160. // immutable iterator must be constructible from (possibly mutable) iterator
  161. const_t const_it(it);
  162. ignore_unused_variable_warning(const_it);
  163. check_base(typename is_iterator_adaptor<Iterator>::type());
  164. }
  165. void check_base(std::false_type) {}
  166. void check_base(std::true_type)
  167. {
  168. using base_t = typename iterator_adaptor_get_base<Iterator>::type;
  169. gil_function_requires<PixelIteratorConcept<base_t>>();
  170. }
  171. Iterator it;
  172. };
  173. /// \brief Pixel iterator that allows for changing its pixel
  174. /// \ingroup PixelIteratorConceptPixelIterator
  175. /// \code
  176. /// concept MutablePixelIteratorConcept<PixelIteratorConcept Iterator>
  177. /// : MutableRandomAccessIteratorConcept<Iterator> {};
  178. /// \endcode
  179. template <typename Iterator>
  180. struct MutablePixelIteratorConcept
  181. {
  182. void constraints()
  183. {
  184. gil_function_requires<PixelIteratorConcept<Iterator>>();
  185. gil_function_requires<detail::PixelIteratorIsMutableConcept<Iterator>>();
  186. }
  187. };
  188. /// \defgroup PixelIteratorConceptStepIterator StepIteratorConcept
  189. /// \ingroup PixelIteratorConcept
  190. /// \brief Iterator that advances by a specified step
  191. /// \ingroup PixelIteratorConceptStepIterator
  192. /// \brief Concept of a random-access iterator that can be advanced in memory units (bytes or bits)
  193. /// \code
  194. /// concept MemoryBasedIteratorConcept<boost_concepts::RandomAccessTraversalConcept Iterator>
  195. /// {
  196. /// typename byte_to_memunit<Iterator>; where metafunction<byte_to_memunit<Iterator> >;
  197. /// std::ptrdiff_t memunit_step(const Iterator&);
  198. /// std::ptrdiff_t memunit_distance(const Iterator& , const Iterator&);
  199. /// void memunit_advance(Iterator&, std::ptrdiff_t diff);
  200. /// Iterator memunit_advanced(const Iterator& p, std::ptrdiff_t diff) { Iterator tmp; memunit_advance(tmp,diff); return tmp; }
  201. /// Iterator::reference memunit_advanced_ref(const Iterator& p, std::ptrdiff_t diff) { return *memunit_advanced(p,diff); }
  202. /// };
  203. /// \endcode
  204. template <typename Iterator>
  205. struct MemoryBasedIteratorConcept
  206. {
  207. void constraints()
  208. {
  209. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<Iterator>>();
  210. gil_function_requires<detail::RandomAccessIteratorIsMemoryBasedConcept<Iterator>>();
  211. }
  212. };
  213. /// \ingroup PixelIteratorConceptStepIterator
  214. /// \brief Step iterator concept
  215. ///
  216. /// Step iterators are iterators that have a set_step method
  217. /// \code
  218. /// concept StepIteratorConcept<boost_concepts::ForwardTraversalConcept Iterator>
  219. /// {
  220. /// template <Integral D>
  221. /// void Iterator::set_step(D step);
  222. /// };
  223. /// \endcode
  224. template <typename Iterator>
  225. struct StepIteratorConcept
  226. {
  227. void constraints()
  228. {
  229. gil_function_requires<boost_concepts::ForwardTraversalConcept<Iterator>>();
  230. it.set_step(0);
  231. }
  232. Iterator it;
  233. };
  234. /// \ingroup PixelIteratorConceptStepIterator
  235. /// \brief Step iterator that allows for modifying its current value
  236. /// \code
  237. /// concept MutableStepIteratorConcept<Mutable_ForwardIteratorConcept Iterator>
  238. /// : StepIteratorConcept<Iterator> {};
  239. /// \endcode
  240. template <typename Iterator>
  241. struct MutableStepIteratorConcept
  242. {
  243. void constraints()
  244. {
  245. gil_function_requires<StepIteratorConcept<Iterator>>();
  246. gil_function_requires<detail::ForwardIteratorIsMutableConcept<Iterator>>();
  247. }
  248. };
  249. /// \defgroup PixelIteratorConceptIteratorAdaptor IteratorAdaptorConcept
  250. /// \ingroup PixelIteratorConcept
  251. /// \brief Adaptor over another iterator
  252. /// \ingroup PixelIteratorConceptIteratorAdaptor
  253. /// \brief Iterator adaptor is a forward iterator adapting another forward iterator.
  254. ///
  255. /// In addition to GIL iterator requirements,
  256. /// GIL iterator adaptors must provide the following metafunctions:
  257. /// - \p is_iterator_adaptor<Iterator>: Returns \p std::true_type
  258. /// - \p iterator_adaptor_get_base<Iterator>: Returns the base iterator type
  259. /// - \p iterator_adaptor_rebind<Iterator,NewBase>: Replaces the base iterator with the new one
  260. ///
  261. /// The adaptee can be obtained from the iterator via the "base()" method.
  262. ///
  263. /// \code
  264. /// concept IteratorAdaptorConcept<boost_concepts::ForwardTraversalConcept Iterator>
  265. /// {
  266. /// where SameType<is_iterator_adaptor<Iterator>::type, std::true_type>;
  267. ///
  268. /// typename iterator_adaptor_get_base<Iterator>;
  269. /// where Metafunction<iterator_adaptor_get_base<Iterator> >;
  270. /// where boost_concepts::ForwardTraversalConcept<iterator_adaptor_get_base<Iterator>::type>;
  271. ///
  272. /// typename another_iterator;
  273. /// typename iterator_adaptor_rebind<Iterator,another_iterator>::type;
  274. /// where boost_concepts::ForwardTraversalConcept<another_iterator>;
  275. /// where IteratorAdaptorConcept<iterator_adaptor_rebind<Iterator,another_iterator>::type>;
  276. ///
  277. /// const iterator_adaptor_get_base<Iterator>::type& Iterator::base() const;
  278. /// };
  279. /// \endcode
  280. template <typename Iterator>
  281. struct IteratorAdaptorConcept
  282. {
  283. void constraints()
  284. {
  285. gil_function_requires<boost_concepts::ForwardTraversalConcept<Iterator>>();
  286. using base_t = typename iterator_adaptor_get_base<Iterator>::type;
  287. gil_function_requires<boost_concepts::ForwardTraversalConcept<base_t>>();
  288. static_assert(is_iterator_adaptor<Iterator>::value, "");
  289. using rebind_t = typename iterator_adaptor_rebind<Iterator, void*>::type;
  290. base_t base = it.base();
  291. ignore_unused_variable_warning(base);
  292. }
  293. Iterator it;
  294. };
  295. /// \brief Iterator adaptor that is mutable
  296. /// \ingroup PixelIteratorConceptIteratorAdaptor
  297. /// \code
  298. /// concept MutableIteratorAdaptorConcept<Mutable_ForwardIteratorConcept Iterator>
  299. /// : IteratorAdaptorConcept<Iterator> {};
  300. /// \endcode
  301. template <typename Iterator>
  302. struct MutableIteratorAdaptorConcept
  303. {
  304. void constraints()
  305. {
  306. gil_function_requires<IteratorAdaptorConcept<Iterator>>();
  307. gil_function_requires<detail::ForwardIteratorIsMutableConcept<Iterator>>();
  308. }
  309. };
  310. }} // namespace boost::gil
  311. #if defined(BOOST_CLANG)
  312. #pragma clang diagnostic pop
  313. #endif
  314. #if defined(BOOST_GCC) && (BOOST_GCC >= 40900)
  315. #pragma GCC diagnostic pop
  316. #endif
  317. #endif