pixel_iterator.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_PIXEL_ITERATOR_HPP
  9. #define BOOST_GIL_PIXEL_ITERATOR_HPP
  10. #include <boost/gil/concepts.hpp>
  11. #include <boost/gil/dynamic_step.hpp>
  12. #include <boost/gil/utilities.hpp>
  13. #include <boost/gil/pixel.hpp>
  14. #include <iterator>
  15. namespace boost { namespace gil {
  16. //forwarded declaration (as this file is included in step_iterator.hpp)
  17. template <typename Iterator>
  18. class memory_based_step_iterator;
  19. /// \brief metafunction predicate determining whether the given iterator is a plain one or an adaptor over another iterator.
  20. /// Examples of adaptors are the step iterator and the dereference iterator adaptor.
  21. template <typename It>
  22. struct is_iterator_adaptor : public mpl::false_{};
  23. /// \brief returns the base iterator for a given iterator adaptor. Provide an specialization when introducing new iterator adaptors
  24. template <typename It>
  25. struct iterator_adaptor_get_base;
  26. /// \brief Changes the base iterator of an iterator adaptor. Provide an specialization when introducing new iterator adaptors
  27. template <typename It, typename NewBaseIt>
  28. struct iterator_adaptor_rebind;
  29. /// \brief Returns the type of an iterator just like the input iterator, except operating over immutable values
  30. template <typename It>
  31. struct const_iterator_type;
  32. // The default implementation when the iterator is a C pointer is to use the standard constness semantics
  33. template <typename T> struct const_iterator_type<T*> { using type = T const*; };
  34. template <typename T> struct const_iterator_type<T const*> { using type = T const*; };
  35. /// \brief Metafunction predicate returning whether the given iterator allows for changing its values
  36. /// \ingroup GILIsMutable
  37. template <typename It>
  38. struct iterator_is_mutable{};
  39. // The default implementation when the iterator is a C pointer is to use the standard constness semantics
  40. template <typename T> struct iterator_is_mutable< T*> : public mpl::true_{};
  41. template <typename T> struct iterator_is_mutable<const T*> : public mpl::false_{};
  42. /// \defgroup PixelIteratorModelInterleavedPtr C pointer to a pixel
  43. /// \ingroup PixelIteratorModel
  44. /// \brief Iterators over interleaved pixels.
  45. /// A C pointer to a model of PixelValueConcept is used as an iterator over interleaved pixels. Models PixelIteratorConcept, HomogeneousPixelBasedConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept
  46. /////////////////////////////
  47. // HasDynamicXStepTypeConcept
  48. /////////////////////////////
  49. /// \ingroup PixelIteratorModelInterleavedPtr
  50. template <typename Pixel>
  51. struct dynamic_x_step_type<Pixel*> {
  52. using type = memory_based_step_iterator<Pixel *>;
  53. };
  54. /// \ingroup PixelIteratorModelInterleavedPtr
  55. template <typename Pixel>
  56. struct dynamic_x_step_type<const Pixel*> {
  57. using type = memory_based_step_iterator<const Pixel *>;
  58. };
  59. /////////////////////////////
  60. // PixelBasedConcept
  61. /////////////////////////////
  62. template <typename Pixel> struct color_space_type< Pixel*> : public color_space_type<Pixel> {};
  63. template <typename Pixel> struct color_space_type<const Pixel*> : public color_space_type<Pixel> {};
  64. template <typename Pixel> struct channel_mapping_type< Pixel*> : public channel_mapping_type<Pixel> {};
  65. template <typename Pixel> struct channel_mapping_type<const Pixel*> : public channel_mapping_type<Pixel> {};
  66. template <typename Pixel> struct is_planar< Pixel*> : public is_planar<Pixel> {};
  67. template <typename Pixel> struct is_planar<const Pixel*> : public is_planar<Pixel> {};
  68. /////////////////////////////
  69. // HomogeneousPixelBasedConcept
  70. /////////////////////////////
  71. template <typename Pixel> struct channel_type<Pixel*> : public channel_type<Pixel> {};
  72. template <typename Pixel> struct channel_type<const Pixel*> : public channel_type<Pixel> {};
  73. ////////////////////////////////////////////////////////////////////////////////////////
  74. ///
  75. /// Support for pixel iterator movement measured in memory units (bytes or bits) as opposed to pixel type. \n
  76. /// Necessary to handle image row alignment and channel plane alignment.
  77. ///
  78. ////////////////////////////////////////////////////////////////////////////////////////
  79. /////////////////////////////
  80. // MemoryBasedIteratorConcept
  81. /////////////////////////////
  82. template <typename T>
  83. struct byte_to_memunit : public mpl::int_<1> {};
  84. template <typename P>
  85. inline std::ptrdiff_t memunit_step(const P*) { return sizeof(P); }
  86. template <typename P>
  87. inline std::ptrdiff_t memunit_distance(const P* p1, const P* p2) {
  88. return (gil_reinterpret_cast_c<const unsigned char*>(p2)-gil_reinterpret_cast_c<const unsigned char*>(p1));
  89. }
  90. template <typename P>
  91. inline void memunit_advance(P* &p, std::ptrdiff_t diff) {
  92. p=(P*)((unsigned char*)(p)+diff);
  93. }
  94. template <typename P>
  95. inline P* memunit_advanced(const P* p, std::ptrdiff_t diff) {
  96. return (P*)((char*)(p)+diff);
  97. }
  98. // memunit_advanced_ref
  99. // (shortcut to advancing a pointer by a given number of memunits and taking the reference in case the compiler is not smart enough)
  100. template <typename P>
  101. inline P& memunit_advanced_ref(P* p, std::ptrdiff_t diff) {
  102. return *memunit_advanced(p,diff);
  103. }
  104. } } // namespace boost::gil
  105. #endif