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