1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX
25 #define INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX
26 
27 #include <basebmp/metafunctions.hxx>
28 
29 namespace basebmp
30 {
31 
32 /** Like vigra::StridedArrayIterator
33 
34     Changed semantics re. DirectionSelector<StridedArrayTag>: stride
35     now counts in <em>raw</em> bytes
36 
37     Adapts given ptr, in a way that iterator increments move forward
38     in strided steps. Stride can, by the way, also be negative
39  */
40 template< typename T > class StridedArrayIterator
41 {
42 public:
43     typedef typename clone_const<T, unsigned char>::type  internal_type;
44 
45     /** Create iterator
46 
47         @param stride
48 
49         Stride in bytes. Given value should better match memory layout
50         of T, as memory gets reinterpret-casted.
51      */
StridedArrayIterator(int stride,T * ptr=0)52     explicit StridedArrayIterator(int stride, T* ptr = 0) :
53         stride_(stride),
54         current_(reinterpret_cast<internal_type*>(ptr))
55     {}
56 
57     /** Copy from other StridedArrayIterator, plus given offset
58 
59         @param offset
60         Offset in bytes
61      */
StridedArrayIterator(StridedArrayIterator const & rSrc,int offset)62     StridedArrayIterator( StridedArrayIterator const& rSrc,
63                           int                         offset ) :
64         stride_(rSrc.stride_),
65         current_(reinterpret_cast<internal_type*>(
66                      reinterpret_cast<T*>(rSrc.current_)+offset))
67     {}
68 
operator ++()69     void operator++()       {current_ += stride_; }
operator ++(int)70     void operator++(int)    {current_ += stride_; }
operator --()71     void operator--()       {current_ -= stride_; }
operator --(int)72     void operator--(int)    {current_ -= stride_; }
operator +=(int dy)73     void operator+=(int dy) {current_ += dy*stride_; }
operator -=(int dy)74     void operator-=(int dy) {current_ -= dy*stride_; }
75 
operator -(StridedArrayIterator const & rhs) const76     int operator-(StridedArrayIterator const & rhs) const
77     { return (current_ - rhs.current_) / stride_; }
78 
operator ==(StridedArrayIterator const & rhs) const79     bool operator==(StridedArrayIterator const & rhs) const
80     { return current_ == rhs.current_; }
81 
operator !=(StridedArrayIterator const & rhs) const82     bool operator!=(StridedArrayIterator const & rhs) const
83     { return current_ != rhs.current_; }
84 
operator <(StridedArrayIterator const & rhs) const85     bool operator<(StridedArrayIterator const & rhs) const
86     { return *this - rhs < 0; }
87 
operator <=(StridedArrayIterator const & rhs) const88     bool operator<=(StridedArrayIterator const & rhs) const
89     { return *this - rhs <= 0; }
90 
operator >(StridedArrayIterator const & rhs) const91     bool operator>(StridedArrayIterator const & rhs) const
92     { return *this - rhs > 0; }
93 
operator >=(StridedArrayIterator const & rhs) const94     bool operator>=(StridedArrayIterator const & rhs) const
95     { return *this - rhs >= 0; }
96 
operator ()() const97     T* operator()() const
98     { return reinterpret_cast<T*>(current_); }
99 
operator ()(int d) const100     T* operator()(int d) const
101     { return reinterpret_cast<T*>(current_ + d*stride_); }
102 
103 private:
104     int            stride_;
105     internal_type* current_;
106 };
107 
108 } // namespace basebmp
109 
110 #endif /* INCLUDED_BASEBMP_STRIDEDARRAYITERATOR_HXX */
111