pixel_iterator.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. Pixel Iterator
  2. ==============
  3. .. contents::
  4. :local:
  5. :depth: 2
  6. Overview
  7. --------
  8. Pixel iterators are random traversal iterators whose ``value_type
  9. models`` ``PixelValueConcept``.
  10. Fundamental Iterator
  11. --------------------
  12. Pixel iterators provide metafunctions to determine whether they are mutable
  13. (i.e. whether they allow for modifying the pixel they refer to), to get the
  14. immutable (read-only) type of the iterator, and to determine whether they are
  15. plain iterators or adaptors over another pixel iterator:
  16. .. code-block:: cpp
  17. concept PixelIteratorConcept<RandomAccessTraversalIteratorConcept Iterator>
  18. : PixelBasedConcept<Iterator>
  19. {
  20. where PixelValueConcept<value_type>;
  21. typename const_iterator_type<It>::type;
  22. where PixelIteratorConcept<const_iterator_type<It>::type>;
  23. static const bool iterator_is_mutable<It>::value;
  24. static const bool is_iterator_adaptor<It>::value; // is it an iterator adaptor
  25. };
  26. template <typename Iterator>
  27. concept MutablePixelIteratorConcept : PixelIteratorConcept<Iterator>, MutableRandomAccessIteratorConcept<Iterator> {};
  28. .. seealso::
  29. - `PixelIteratorConcept<Iterator> <reference/group___pixel_iterator_concept_pixel_iterator.html>`_
  30. - `MutablePixelIteratorConcept<Iterator> <reference/structboost_1_1gil_1_1_mutable_pixel_iterator_concept.html>`_
  31. Models
  32. ^^^^^^
  33. A built-in pointer to pixel, ``pixel<ChannelValue,Layout>*``, is GIL model for
  34. pixel iterator over interleaved homogeneous pixels. Similarly,
  35. ``packed_pixel<PixelData,ChannelRefVec,Layout>*`` is GIL model for an iterator
  36. over interleaved packed pixels.
  37. For planar homogeneous pixels, GIL provides the class
  38. ``planar_pixel_iterator``, templated over a channel iterator and color space.
  39. Here is how the standard mutable and read-only planar RGB iterators over
  40. unsigned char are defined:
  41. .. code-block:: cpp
  42. template <typename ChannelPtr, typename ColorSpace>
  43. struct planar_pixel_iterator;
  44. // GIL provided typedefs
  45. typedef planar_pixel_iterator<const bits8*, rgb_t> rgb8c_planar_ptr_t;
  46. typedef planar_pixel_iterator< bits8*, rgb_t> rgb8_planar_ptr_t;
  47. ``planar_pixel_iterator`` also models ``HomogeneousColorBaseConcept`` (it
  48. subclasses from ``homogeneous_color_base``) and, as a result, all color base
  49. algorithms apply to it. The element type of its color base is a channel
  50. iterator. For example, GIL implements ``operator++`` of planar iterators
  51. approximately like this:
  52. .. code-block:: cpp
  53. template <typename T>
  54. struct inc : public std::unary_function<T,T>
  55. {
  56. T operator()(T x) const { return ++x; }
  57. };
  58. template <typename ChannelPtr, typename ColorSpace>
  59. planar_pixel_iterator<ChannelPtr,ColorSpace>&
  60. planar_pixel_iterator<ChannelPtr,ColorSpace>::operator++()
  61. {
  62. static_transform(*this,*this,inc<ChannelPtr>());
  63. return *this;
  64. }
  65. Since ``static_transform`` uses compile-time recursion, incrementing an
  66. instance of ``rgb8_planar_ptr_t`` amounts to three pointer increments.
  67. GIL also uses the class ``bit_aligned_pixel_iterator`` as a model for a pixel
  68. iterator over bit-aligned pixels. Internally it keeps track of the current
  69. byte and the bit offset.
  70. Iterator Adaptor
  71. ----------------
  72. Iterator adaptor is an iterator that wraps around another iterator. Its
  73. ``is_iterator_adaptor`` metafunction must evaluate to true, and it needs to
  74. provide a member method to return the base iterator, a metafunction to get its
  75. type, and a metafunction to rebind to another base iterator:
  76. .. code-block:: cpp
  77. concept IteratorAdaptorConcept<RandomAccessTraversalIteratorConcept Iterator>
  78. {
  79. where SameType<is_iterator_adaptor<Iterator>::type, mpl::true_>;
  80. typename iterator_adaptor_get_base<Iterator>;
  81. where Metafunction<iterator_adaptor_get_base<Iterator> >;
  82. where boost_concepts::ForwardTraversalConcept<iterator_adaptor_get_base<Iterator>::type>;
  83. typename another_iterator;
  84. typename iterator_adaptor_rebind<Iterator,another_iterator>::type;
  85. where boost_concepts::ForwardTraversalConcept<another_iterator>;
  86. where IteratorAdaptorConcept<iterator_adaptor_rebind<Iterator,another_iterator>::type>;
  87. const iterator_adaptor_get_base<Iterator>::type& Iterator::base() const;
  88. };
  89. template <boost_concepts::Mutable_ForwardIteratorConcept Iterator>
  90. concept MutableIteratorAdaptorConcept : IteratorAdaptorConcept<Iterator> {};
  91. .. seealso::
  92. - `IteratorAdaptorConcept<Iterator> <reference/structboost_1_1gil_1_1_iterator_adaptor_concept.html>`_
  93. - `MutableIteratorAdaptorConcept<Iterator> <reference/structboost_1_1gil_1_1_mutable_iterator_adaptor_concept.html>`_
  94. Models
  95. ^^^^^^
  96. GIL provides several models of ``IteratorAdaptorConcept``:
  97. - ``memory_based_step_iterator<Iterator>``: An iterator adaptor that changes
  98. the fundamental step of the base iterator
  99. (see :ref:`design/pixel_iterator:Step Iterator`)
  100. - ``dereference_iterator_adaptor<Iterator,Fn>``: An iterator that applies a
  101. unary function ``Fn`` upon dereferencing. It is used, for example, for
  102. on-the-fly color conversion. It can be used to construct a shallow image
  103. "view" that pretends to have a different color space or channel depth.
  104. See :doc:`image_view` for more. The unary function ``Fn`` must
  105. model ``PixelDereferenceAdaptorConcept`` (see below).
  106. Pixel Dereference Adaptor
  107. -------------------------
  108. Pixel dereference adaptor is a unary function that can be applied upon
  109. dereferencing a pixel iterator. Its argument type could be anything (usually a
  110. ``PixelConcept``) and the result type must be convertible to ``PixelConcept``:
  111. .. code-block:: cpp
  112. template <boost::UnaryFunctionConcept D>
  113. concept PixelDereferenceAdaptorConcept:
  114. DefaultConstructibleConcept<D>,
  115. CopyConstructibleConcept<D>,
  116. AssignableConcept<D>
  117. {
  118. typename const_t; where PixelDereferenceAdaptorConcept<const_t>;
  119. typename value_type; where PixelValueConcept<value_type>;
  120. typename reference; where PixelConcept<remove_reference<reference>::type>; // may be mutable
  121. typename const_reference; // must not be mutable
  122. static const bool D::is_mutable;
  123. where Convertible<value_type, result_type>;
  124. };
  125. Models
  126. ^^^^^^
  127. GIL provides several models of ``PixelDereferenceAdaptorConcept``:
  128. * ``color_convert_deref_fn``: a function object that performs color conversion
  129. * ``detail::nth_channel_deref_fn``: a function object that returns a grayscale
  130. pixel corresponding to the n-th channel of a given pixel
  131. * ``deref_compose``: a function object that composes two models of
  132. ``PixelDereferenceAdaptorConcept``. Similar to ``std::unary_compose``,
  133. except it needs to pull the additional typedefs required by
  134. ``PixelDereferenceAdaptorConcept``
  135. GIL uses pixel dereference adaptors to implement image views that perform
  136. color conversion upon dereferencing, or that return the N-th channel of the
  137. underlying pixel. They can be used to model virtual image views that perform
  138. an arbitrary function upon dereferencing, for example a view of the Mandelbrot
  139. set. ``dereference_iterator_adaptor<Iterator,Fn>`` is an iterator wrapper over
  140. a pixel iterator ``Iterator`` that invokes the given dereference iterator
  141. adaptor ``Fn`` upon dereferencing.
  142. Step Iterator
  143. -------------
  144. Sometimes we want to traverse pixels with a unit step other than the one
  145. provided by the fundamental pixel iterators. Examples where this would be
  146. useful:
  147. * a single-channel view of the red channel of an RGB interleaved image
  148. * left-to-right flipped image (step = -fundamental_step)
  149. * subsampled view, taking every N-th pixel (step = N*fundamental_step)
  150. * traversal in vertical direction (step = number of bytes per row)
  151. * any combination of the above (steps are multiplied)
  152. Step iterators are forward traversal iterators that allow changing the step
  153. between adjacent values:
  154. .. code-block:: cpp
  155. concept StepIteratorConcept<boost_concepts::ForwardTraversalConcept Iterator>
  156. {
  157. template <Integral D> void Iterator::set_step(D step);
  158. };
  159. concept MutableStepIteratorConcept<boost_concepts::Mutable_ForwardIteratorConcept Iterator>
  160. : StepIteratorConcept<Iterator>
  161. {};
  162. GIL currently provides a step iterator whose ``value_type models``
  163. ``PixelValueConcept``. In addition, the step is specified in memory units
  164. (which are bytes or bits). This is necessary, for example, when implementing
  165. an iterator navigating along a column of pixels - the size of a row of pixels
  166. may sometimes not be divisible by the size of a pixel; for example rows may be
  167. word-aligned.
  168. To advance in bytes/bits, the base iterator must model
  169. ``MemoryBasedIteratorConcept``. A memory-based iterator has an inherent memory
  170. unit, which is either a bit or a byte. It must supply functions returning the
  171. number of bits per memory unit (1 or 8), the current step in memory units, the
  172. memory-unit distance between two iterators, and a reference a given distance
  173. in memunits away. It must also supply a function that advances an iterator a
  174. given distance in memory units. ``memunit_advanced`` and
  175. ``memunit_advanced_ref`` have a default implementation but some iterators may
  176. supply a more efficient version:
  177. .. code-block:: cpp
  178. concept MemoryBasedIteratorConcept
  179. <
  180. boost_concepts::RandomAccessTraversalConcept Iterator
  181. >
  182. {
  183. typename byte_to_memunit<Iterator>; where metafunction<byte_to_memunit<Iterator> >;
  184. std::ptrdiff_t memunit_step(const Iterator&);
  185. std::ptrdiff_t memunit_distance(const Iterator& , const Iterator&);
  186. void memunit_advance(Iterator&, std::ptrdiff_t diff);
  187. Iterator memunit_advanced(const Iterator& p, std::ptrdiff_t diff) { Iterator tmp; memunit_advance(tmp,diff); return tmp; }
  188. Iterator::reference memunit_advanced_ref(const Iterator& p, std::ptrdiff_t diff) { return *memunit_advanced(p,diff); }
  189. };
  190. It is useful to be able to construct a step iterator over another iterator.
  191. More generally, given a type, we want to be able to construct an equivalent
  192. type that allows for dynamically specified horizontal step:
  193. .. code-block:: cpp
  194. concept HasDynamicXStepTypeConcept<typename T>
  195. {
  196. typename dynamic_x_step_type<T>;
  197. where Metafunction<dynamic_x_step_type<T> >;
  198. };
  199. All models of pixel iterators, locators and image views that GIL provides
  200. support ``HasDynamicXStepTypeConcept``.
  201. .. seealso::
  202. - `StepIteratorConcept<Iterator> <reference/structboost_1_1gil_1_1_step_iterator_concept.html>`_
  203. - `MutableStepIteratorConcept<Iterator> <reference/structboost_1_1gil_1_1_mutable_step_iterator_concept.html>`_
  204. - `MemoryBasedIteratorConcept<Iterator> <reference/structboost_1_1gil_1_1_memory_based_iterator_concept.html>`_
  205. - `HasDynamicXStepTypeConcept<T> <reference/structboost_1_1gil_1_1_has_dynamic_x_step_type_concept.html>`_
  206. Models
  207. ^^^^^^
  208. All standard memory-based iterators GIL currently provides model
  209. ``MemoryBasedIteratorConcept``. GIL provides the class
  210. ``memory_based_step_iterator`` which models ``PixelIteratorConcept``,
  211. ``StepIteratorConcept``, and ``MemoryBasedIteratorConcept``. It takes the base
  212. iterator as a template parameter (which must model ``PixelIteratorConcept``
  213. and ``MemoryBasedIteratorConcept``) and allows changing the step dynamically.
  214. GIL implementation contains the base iterator and a ``ptrdiff_t`` denoting the
  215. number of memory units (bytes or bits) to skip for a unit step. It may also be
  216. used with a negative number. GIL provides a function to create a step iterator
  217. from a base iterator and a step:
  218. .. code-block:: cpp
  219. // Iterator models MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept
  220. template <typename Iterator>
  221. typename dynamic_x_step_type<Iterator>::type make_step_iterator(Iterator const& it, std::ptrdiff_t step);
  222. GIL also provides a model of an iterator over a virtual array of pixels,
  223. ``position_iterator``. It is a step iterator that keeps track of the pixel
  224. position and invokes a function object to get the value of the pixel upon
  225. dereferencing. It models ``PixelIteratorConcept`` and ``StepIteratorConcept``
  226. but not ``MemoryBasedIteratorConcept``.