1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef INCLUDED_BASEBMP_PIXELITERATOR_HXX
29*cdf0e10cSrcweir #define INCLUDED_BASEBMP_PIXELITERATOR_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <basebmp/metafunctions.hxx>
32*cdf0e10cSrcweir #include <basebmp/stridedarrayiterator.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <vigra/metaprogramming.hxx>
35*cdf0e10cSrcweir #include <vigra/diff2d.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace basebmp
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir template< typename Valuetype > class PixelColumnIterator
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir public:
43*cdf0e10cSrcweir     typedef Valuetype                           value_type;
44*cdf0e10cSrcweir     typedef Valuetype&                          reference;
45*cdf0e10cSrcweir     typedef reference                           index_reference;
46*cdf0e10cSrcweir     typedef Valuetype*                          pointer;
47*cdf0e10cSrcweir     typedef int                                 difference_type;
48*cdf0e10cSrcweir     typedef image_traverser_tag                 iterator_category;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     typedef StridedArrayIterator< value_type >  MoveY;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir private:
53*cdf0e10cSrcweir     MoveY           y;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir     bool equal( PixelColumnIterator const & rhs ) const
56*cdf0e10cSrcweir     {
57*cdf0e10cSrcweir         return rhs.y == y;
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     bool less( PixelColumnIterator const & rhs ) const
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         return y < rhs.y;
63*cdf0e10cSrcweir     }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir public:
66*cdf0e10cSrcweir     PixelColumnIterator() :
67*cdf0e10cSrcweir         y(0)
68*cdf0e10cSrcweir     {}
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     explicit PixelColumnIterator( const MoveY& pos ) :
71*cdf0e10cSrcweir         y(pos)
72*cdf0e10cSrcweir     {}
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     PixelColumnIterator( const MoveY& pos, int x ) :
75*cdf0e10cSrcweir         y(pos,x)
76*cdf0e10cSrcweir     {}
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     PixelColumnIterator& operator+=( difference_type d )
79*cdf0e10cSrcweir     {
80*cdf0e10cSrcweir         y += d;
81*cdf0e10cSrcweir         return *this;
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     PixelColumnIterator& operator-=( difference_type d )
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         y -= d;
87*cdf0e10cSrcweir         return *this;
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     PixelColumnIterator operator+( difference_type d )
91*cdf0e10cSrcweir     {
92*cdf0e10cSrcweir         PixelColumnIterator res(*this);
93*cdf0e10cSrcweir         res += d;
94*cdf0e10cSrcweir         return res;
95*cdf0e10cSrcweir     }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     PixelColumnIterator operator-( difference_type d )
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir         PixelColumnIterator res(*this);
100*cdf0e10cSrcweir         res -= d;
101*cdf0e10cSrcweir         return res;
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     PixelColumnIterator& operator++()
105*cdf0e10cSrcweir     {
106*cdf0e10cSrcweir         ++y;
107*cdf0e10cSrcweir         return *this;
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     PixelColumnIterator& operator--()
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         --y;
113*cdf0e10cSrcweir         return *this;
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir     PixelColumnIterator operator++(int)
117*cdf0e10cSrcweir     {
118*cdf0e10cSrcweir         PixelColumnIterator res(*this);
119*cdf0e10cSrcweir         ++y;
120*cdf0e10cSrcweir         return res;
121*cdf0e10cSrcweir     }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir     PixelColumnIterator operator--(int)
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         PixelColumnIterator res(*this);
126*cdf0e10cSrcweir         --y;
127*cdf0e10cSrcweir         return res;
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir     bool operator==(PixelColumnIterator const & rhs) const
131*cdf0e10cSrcweir     {
132*cdf0e10cSrcweir         return equal( rhs );
133*cdf0e10cSrcweir     }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     bool operator!=(PixelColumnIterator const & rhs) const
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         return !equal( rhs );
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     bool operator<(PixelColumnIterator const & rhs) const
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir         return less(rhs);
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     bool operator<=(PixelColumnIterator const & rhs) const
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir         return !rhs.less(*this);
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     bool operator>(PixelColumnIterator const & rhs) const
151*cdf0e10cSrcweir     {
152*cdf0e10cSrcweir         return rhs.less(*this);
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     bool operator>=(PixelColumnIterator const & rhs) const
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         return !less(rhs);
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     difference_type operator-(PixelColumnIterator const & rhs) const
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         return y - rhs.y;
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     value_type get() const
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         return *y();
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     value_type get(difference_type d) const
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         return *y(d);
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     void set( value_type v ) const
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         *y() = v;
178*cdf0e10cSrcweir     }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir     void set( value_type v, difference_type d ) const
181*cdf0e10cSrcweir     {
182*cdf0e10cSrcweir         *y(d) = v;
183*cdf0e10cSrcweir     }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     reference operator*() const
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         return *y();
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     pointer operator->() const
191*cdf0e10cSrcweir     {
192*cdf0e10cSrcweir         return y();
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     reference operator[](difference_type d) const
196*cdf0e10cSrcweir     {
197*cdf0e10cSrcweir         return *y(d);
198*cdf0e10cSrcweir     }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     reference operator()(int dy) const
201*cdf0e10cSrcweir     {
202*cdf0e10cSrcweir         return *y(dy);
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir };
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir template< typename Valuetype > class PixelIterator
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir public:
209*cdf0e10cSrcweir     typedef Valuetype                          value_type;
210*cdf0e10cSrcweir     typedef Valuetype&                         reference;
211*cdf0e10cSrcweir     typedef reference                          index_reference;
212*cdf0e10cSrcweir     typedef Valuetype*                         pointer;
213*cdf0e10cSrcweir     typedef vigra::Diff2D                      difference_type;
214*cdf0e10cSrcweir     typedef image_traverser_tag                iterator_category;
215*cdf0e10cSrcweir     typedef pointer                            row_iterator;
216*cdf0e10cSrcweir     typedef PixelColumnIterator<value_type>    column_iterator;
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     typedef int                                MoveX;
219*cdf0e10cSrcweir     typedef StridedArrayIterator< value_type > MoveY;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     // TODO(F2): direction of iteration (ImageIterator can be made to
222*cdf0e10cSrcweir     // run backwards)
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir private:
225*cdf0e10cSrcweir     bool equal(PixelIterator const & rhs) const
226*cdf0e10cSrcweir     {
227*cdf0e10cSrcweir         return (x == rhs.x) && (y == rhs.y);
228*cdf0e10cSrcweir     }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     pointer current() const
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         return y() + x;
233*cdf0e10cSrcweir     }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     pointer current(int dx, int dy) const
236*cdf0e10cSrcweir     {
237*cdf0e10cSrcweir         return y(dy) + x+dx;
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir public:
241*cdf0e10cSrcweir     PixelIterator() :
242*cdf0e10cSrcweir         x(0),
243*cdf0e10cSrcweir         y(0)
244*cdf0e10cSrcweir     {}
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir     PixelIterator(pointer base, int ystride) :
247*cdf0e10cSrcweir         x(0),
248*cdf0e10cSrcweir         y(ystride,base)
249*cdf0e10cSrcweir     {}
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir     bool operator==(PixelIterator const & rhs) const
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         return equal(rhs);
254*cdf0e10cSrcweir     }
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     bool operator!=(PixelIterator const & rhs) const
257*cdf0e10cSrcweir     {
258*cdf0e10cSrcweir         return !equal(rhs);
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     difference_type operator-(PixelIterator const & rhs) const
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         return difference_type(x - rhs.x, y - rhs.y);
264*cdf0e10cSrcweir     }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir     MoveX x;
267*cdf0e10cSrcweir     MoveY y;
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir     PixelIterator & operator+=(difference_type const & s)
270*cdf0e10cSrcweir     {
271*cdf0e10cSrcweir         x += s.x;
272*cdf0e10cSrcweir         y += s.y;
273*cdf0e10cSrcweir         return *this;
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     PixelIterator & operator-=(difference_type const & s)
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         x -= s.x;
279*cdf0e10cSrcweir         y -= s.y;
280*cdf0e10cSrcweir         return *this;
281*cdf0e10cSrcweir     }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     PixelIterator operator+(difference_type const & s) const
284*cdf0e10cSrcweir     {
285*cdf0e10cSrcweir         PixelIterator ret(*this);
286*cdf0e10cSrcweir         ret += s;
287*cdf0e10cSrcweir         return ret;
288*cdf0e10cSrcweir     }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     PixelIterator operator-(difference_type const & s) const
291*cdf0e10cSrcweir     {
292*cdf0e10cSrcweir         PixelIterator ret(*this);
293*cdf0e10cSrcweir         ret -= s;
294*cdf0e10cSrcweir         return ret;
295*cdf0e10cSrcweir     }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir     row_iterator rowIterator() const
298*cdf0e10cSrcweir     {
299*cdf0e10cSrcweir         return row_iterator(y()+x);
300*cdf0e10cSrcweir     }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     column_iterator columnIterator() const
303*cdf0e10cSrcweir     {
304*cdf0e10cSrcweir         return column_iterator(y,x);
305*cdf0e10cSrcweir     }
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir     value_type get() const
308*cdf0e10cSrcweir     {
309*cdf0e10cSrcweir         return *current();
310*cdf0e10cSrcweir     }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     value_type get(difference_type const & d) const
313*cdf0e10cSrcweir     {
314*cdf0e10cSrcweir         return *current(d.y, d.x);
315*cdf0e10cSrcweir     }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir     void set( value_type v ) const
318*cdf0e10cSrcweir     {
319*cdf0e10cSrcweir         *current() = v;
320*cdf0e10cSrcweir     }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir     void set( value_type v, difference_type const & d ) const
323*cdf0e10cSrcweir     {
324*cdf0e10cSrcweir         *current(d.y,d.x) = v;
325*cdf0e10cSrcweir     }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     reference operator*() const
328*cdf0e10cSrcweir     {
329*cdf0e10cSrcweir         return *current();
330*cdf0e10cSrcweir     }
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir     pointer operator->() const
333*cdf0e10cSrcweir     {
334*cdf0e10cSrcweir         return current();
335*cdf0e10cSrcweir     }
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir     reference operator[]( const vigra::Diff2D& d ) const
338*cdf0e10cSrcweir     {
339*cdf0e10cSrcweir         return *current(d.x,d.y);
340*cdf0e10cSrcweir     }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir     reference operator()(int dx, int dy) const
343*cdf0e10cSrcweir     {
344*cdf0e10cSrcweir         return *current(dx,dy);
345*cdf0e10cSrcweir     }
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir     pointer operator[](int dy) const
348*cdf0e10cSrcweir     {
349*cdf0e10cSrcweir         return y(dy) + x;
350*cdf0e10cSrcweir     }
351*cdf0e10cSrcweir };
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir } // namespace basebmp
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir #endif /* INCLUDED_BASEBMP_PIXELITERATOR_HXX */
356