1 | /* |
2 | Copyright 2005-2007 Adobe Systems Incorporated |
3 | |
4 | Use, modification and distribution are subject to the Boost Software License, |
5 | Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
6 | http://www.boost.org/LICENSE_1_0.txt). |
7 | |
8 | See http://opensource.adobe.com/gil for most recent version including documentation. |
9 | */ |
10 | |
11 | /*************************************************************************************************/ |
12 | |
13 | #ifndef GIL_PIXEL_ITERATOR_H |
14 | #define GIL_PIXEL_ITERATOR_H |
15 | |
16 | //////////////////////////////////////////////////////////////////////////////////////// |
17 | /// \file |
18 | /// \brief pixel iterator support |
19 | /// \author Lubomir Bourdev and Hailin Jin \n |
20 | /// Adobe Systems Incorporated |
21 | /// \date 2005-2007 \n May 16, 2006 |
22 | /// |
23 | //////////////////////////////////////////////////////////////////////////////////////// |
24 | |
25 | #include <cassert> |
26 | #include <iterator> |
27 | #include "gil_config.hpp" |
28 | #include "gil_concept.hpp" |
29 | #include "utilities.hpp" |
30 | #include "pixel.hpp" |
31 | |
32 | namespace boost { namespace gil { |
33 | |
34 | //forwarded declaration (as this file is included in step_iterator.hpp) |
35 | template <typename Iterator> |
36 | class memory_based_step_iterator; |
37 | |
38 | template <typename Iterator> struct dynamic_x_step_type; |
39 | |
40 | /// \brief metafunction predicate determining whether the given iterator is a plain one or an adaptor over another iterator. |
41 | /// Examples of adaptors are the step iterator and the dereference iterator adaptor. |
42 | template <typename It> |
43 | struct is_iterator_adaptor : public mpl::false_{}; |
44 | |
45 | /// \brief returns the base iterator for a given iterator adaptor. Provide an specialization when introducing new iterator adaptors |
46 | template <typename It> |
47 | struct iterator_adaptor_get_base; |
48 | |
49 | /// \brief Changes the base iterator of an iterator adaptor. Provide an specialization when introducing new iterator adaptors |
50 | template <typename It, typename NewBaseIt> |
51 | struct iterator_adaptor_rebind; |
52 | |
53 | /// \brief Returns the type of an iterator just like the input iterator, except operating over immutable values |
54 | template <typename It> |
55 | struct const_iterator_type; |
56 | |
57 | // The default implementation when the iterator is a C pointer is to use the standard constness semantics |
58 | template <typename T> struct const_iterator_type< T*> { typedef const T* type; }; |
59 | template <typename T> struct const_iterator_type<const T*> { typedef const T* type; }; |
60 | |
61 | /// \brief Metafunction predicate returning whether the given iterator allows for changing its values |
62 | /// \ingroup GILIsMutable |
63 | template <typename It> |
64 | struct iterator_is_mutable{}; |
65 | |
66 | // The default implementation when the iterator is a C pointer is to use the standard constness semantics |
67 | template <typename T> struct iterator_is_mutable< T*> : public mpl::true_{}; |
68 | template <typename T> struct iterator_is_mutable<const T*> : public mpl::false_{}; |
69 | |
70 | /// \defgroup PixelIteratorModelInterleavedPtr C pointer to a pixel |
71 | /// \ingroup PixelIteratorModel |
72 | /// \brief Iterators over interleaved pixels. |
73 | /// A C pointer to a model of PixelValueConcept is used as an iterator over interleaved pixels. Models PixelIteratorConcept, HomogeneousPixelBasedConcept, HasDynamicXStepTypeConcept, MemoryBasedIteratorConcept |
74 | |
75 | |
76 | |
77 | ///////////////////////////// |
78 | // HasDynamicXStepTypeConcept |
79 | ///////////////////////////// |
80 | |
81 | /// \ingroup PixelIteratorModelInterleavedPtr |
82 | template <typename Pixel> |
83 | struct dynamic_x_step_type<Pixel*> { |
84 | typedef memory_based_step_iterator<Pixel*> type; |
85 | }; |
86 | |
87 | /// \ingroup PixelIteratorModelInterleavedPtr |
88 | template <typename Pixel> |
89 | struct dynamic_x_step_type<const Pixel*> { |
90 | typedef memory_based_step_iterator<const Pixel*> type; |
91 | }; |
92 | |
93 | |
94 | ///////////////////////////// |
95 | // PixelBasedConcept |
96 | ///////////////////////////// |
97 | |
98 | template <typename Pixel> struct color_space_type< Pixel*> : public color_space_type<Pixel> {}; |
99 | template <typename Pixel> struct color_space_type<const Pixel*> : public color_space_type<Pixel> {}; |
100 | |
101 | template <typename Pixel> struct channel_mapping_type< Pixel*> : public channel_mapping_type<Pixel> {}; |
102 | template <typename Pixel> struct channel_mapping_type<const Pixel*> : public channel_mapping_type<Pixel> {}; |
103 | |
104 | template <typename Pixel> struct is_planar< Pixel*> : public is_planar<Pixel> {}; |
105 | template <typename Pixel> struct is_planar<const Pixel*> : public is_planar<Pixel> {}; |
106 | |
107 | ///////////////////////////// |
108 | // HomogeneousPixelBasedConcept |
109 | ///////////////////////////// |
110 | |
111 | template <typename Pixel> struct channel_type<Pixel*> : public channel_type<Pixel> {}; |
112 | template <typename Pixel> struct channel_type<const Pixel*> : public channel_type<Pixel> {}; |
113 | |
114 | //////////////////////////////////////////////////////////////////////////////////////// |
115 | /// |
116 | /// Support for pixel iterator movement measured in memory units (bytes or bits) as opposed to pixel type. \n |
117 | /// Necessary to handle image row alignment and channel plane alignment. |
118 | /// |
119 | //////////////////////////////////////////////////////////////////////////////////////// |
120 | |
121 | ///////////////////////////// |
122 | // MemoryBasedIteratorConcept |
123 | ///////////////////////////// |
124 | |
125 | template <typename T> |
126 | struct byte_to_memunit : public mpl::int_<1> {}; |
127 | |
128 | template <typename P> |
129 | inline std::ptrdiff_t memunit_step(const P*) { return sizeof(P); } |
130 | |
131 | template <typename P> |
132 | inline std::ptrdiff_t memunit_distance(const P* p1, const P* p2) { |
133 | return (gil_reinterpret_cast_c<const unsigned char*>(p2)-gil_reinterpret_cast_c<const unsigned char*>(p1)); |
134 | } |
135 | |
136 | template <typename P> |
137 | inline void memunit_advance(P* &p, std::ptrdiff_t diff) { |
138 | p=(P*)((unsigned char*)(p)+diff); |
139 | } |
140 | |
141 | template <typename P> |
142 | inline P* memunit_advanced(const P* p, std::ptrdiff_t diff) { |
143 | return (P*)((char*)(p)+diff); |
144 | } |
145 | |
146 | // memunit_advanced_ref |
147 | // (shortcut to advancing a pointer by a given number of memunits and taking the reference in case the compiler is not smart enough) |
148 | |
149 | template <typename P> |
150 | inline P& memunit_advanced_ref(P* p, std::ptrdiff_t diff) { |
151 | return *memunit_advanced(p,diff); |
152 | } |
153 | |
154 | } } // namespace boost::gil |
155 | |
156 | #endif |
157 | |