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