pixel_iterator.hpp 5.2 KB

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