[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

imageiteratoradapter.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 
37 #ifndef VIGRA_IMAGEITERATORADAPTER_HXX
38 #define VIGRA_IMAGEITERATORADAPTER_HXX
39 
40 #include <iterator> // iterator tags
41 
42 namespace vigra {
43 
44 /** \addtogroup ImageIteratorAdapters Image Iterator Adapters
45 
46  Iterate over rows, columns, neighborhoods, contours, and other image subsets
47 */
48 //@{
49 
50 /********************************************************/
51 /* */
52 /* ColumnIterator */
53 /* */
54 /********************************************************/
55 
56 /** \brief Iterator adapter to linearly access colums.
57 
58  This iterator may be initialized from any standard ImageIterator,
59  a MultibandImageIterator and so on.
60  It gives you STL-compatible (random access iterator) access to
61  one column of the image. If the underlying iterator is a const iterator,
62  the column iterator will also be const (i.e. doesn't allow to change
63  the values it points to).
64  The iterator gets associated with the accessor of the base iterator.
65 
66  Note that image iterators usually have a member <TT>columnIterator()</TT>
67  which returns a column iterator optimized for that particular image class.
68  ColumnIterator is only necessary if this 'native' column iterator
69  is not usable in a particular situation or is not provided.
70 
71  <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
72 
73  Namespace: vigra
74 
75 */
76 template <class IMAGE_ITERATOR>
77 class ColumnIterator : private IMAGE_ITERATOR
78 {
79  public:
80  /** the iterator's value type
81  */
82  typedef typename IMAGE_ITERATOR::value_type value_type;
83 
84  /** the iterator's value type
85  */
86  typedef typename IMAGE_ITERATOR::value_type PixelType;
87 
88  /** the iterator's reference type (return type of <TT>*iter</TT>)
89  */
90  typedef typename IMAGE_ITERATOR::reference reference;
91 
92  /** the iterator's index reference type (return type of <TT>iter[n]</TT>)
93  */
94  typedef typename IMAGE_ITERATOR::index_reference index_reference;
95 
96  /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
97  */
98  typedef typename IMAGE_ITERATOR::pointer pointer;
99 
100  /** the iterator's difference type (argument type of <TT>iter[diff]</TT>)
101  */
102  typedef typename IMAGE_ITERATOR::difference_type::MoveY difference_type;
103 
104  /** the iterator tag (random access iterator)
105  */
106  typedef std::random_access_iterator_tag iterator_category;
107 
108  /** the type of the adapted iterator
109  */
110  typedef IMAGE_ITERATOR Adaptee;
111 
112  /** Construct from an the image iterator to be adapted.
113  */
114  ColumnIterator(IMAGE_ITERATOR const & i)
115  : IMAGE_ITERATOR(i)
116  {}
117 
118  /** Assignment.
119  */
121  {
123 
124  return *this;
125  }
126 
127  /** Assign a new base iterator.
128  */
129  ColumnIterator & operator=(IMAGE_ITERATOR const & i)
130  {
132 
133  return *this;
134  }
135 
136  /** @name Navigation */
137  //@{
138  ///
139  ColumnIterator & operator++()
140  {
141  ++(this->y);
142  return *this;
143  }
144  ///
145  ColumnIterator operator++(int)
146  {
147  ColumnIterator ret(*this);
148  (this->y)++;
149  return ret;
150  }
151 
152  ///
153  ColumnIterator & operator--()
154  {
155  --(this->y);
156  return *this;
157  }
158 
159  ///
160  ColumnIterator operator--(int)
161  {
162  ColumnIterator ret(*this);
163  (this->y)--;
164  return ret;
165  }
166 
167  ///
168  ColumnIterator & operator+=(int d)
169  {
170  this->y += d;
171  return *this;
172  }
173 
174  ///
175  ColumnIterator & operator-=(int d)
176  {
177  this->y -= d;
178  return *this;
179  }
180  //@}
181 
182  /** @name Methods */
183  //@{
184  /** Construct iterator at a distance.
185  */
187  {
188  IMAGE_ITERATOR ret(*this);
189  ret.y += d;
190  return ColumnIterator(ret);
191  }
192  /** Construct iterator at a distance.
193  */
195  {
196  IMAGE_ITERATOR ret(*this);
197  ret.y -= d;
198  return ColumnIterator(ret);
199  }
200  /** Calculate distance.
201  */
202  int operator-(ColumnIterator const & c) const
203  {
204  return this->y - c.y;
205  }
206 
207  /** Equality.
208  */
209  bool operator==(ColumnIterator const & c) const
210  {
211  return IMAGE_ITERATOR::operator==(c);
212  }
213 
214  /** Inequality.
215  */
216  bool operator!=(ColumnIterator const & c) const
217  {
218  return IMAGE_ITERATOR::operator!=(c);
219  }
220 
221  /** Smaller than.
222  */
223  bool operator<(ColumnIterator const & c) const
224  {
225  return this->y < c.y;
226  }
227 
228  /** Access current pixel.
229  */
231  {
232  return IMAGE_ITERATOR::operator*();
233  }
234 
235  /** Access pixel at distance d.
236  */
238  {
239  return IMAGE_ITERATOR::operator()(0, d);
240  }
241 
242  /** Call member function of current pixel.
243  */
245  {
247  }
248 
249  /** Get a reference to the adapted iterator
250  */
251  Adaptee & adaptee() const { return (Adaptee &)*this; }
252 
253  //@}
254 };
255 
256 /********************************************************/
257 /* */
258 /* RowIterator */
259 /* */
260 /********************************************************/
261 
262 /** \brief Iterator adapter to linearly access row.
263 
264  This iterator may be initialized from a standard ImageIterator,
265  a MultibandImageIterator and so on.
266  It gives you STL-compatible (random access iterator) access to
267  one row of the image. If the underlying iterator is a const iterator,
268  the row iterator will also be const (i.e. doesn't allow to change
269  the values it points to).
270  The iterator gets associated with the accessor of the base iterator.
271 
272  Note that image iterators usually have a member <TT>rowIterator()</TT>
273  which returns a row iterator optimized for that particular image class.
274  RowIterator is only necessary if this 'native' row iterator
275  is not usable in a particular situation or is not provided.
276 
277  <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
278 
279  Namespace: vigra
280 
281 */
282 template <class IMAGE_ITERATOR>
283 class RowIterator : private IMAGE_ITERATOR
284 {
285  public:
286  /** the iterator's value type
287  */
288  typedef typename IMAGE_ITERATOR::value_type value_type;
289 
290  /** the iterator's value type
291  */
292  typedef typename IMAGE_ITERATOR::value_type PixelType;
293 
294  /** the iterator's reference type (return type of <TT>*iter</TT>)
295  */
296  typedef typename IMAGE_ITERATOR::reference reference;
297 
298  /** the iterator's index reference type (return type of <TT>iter[n]</TT>)
299  */
300  typedef typename IMAGE_ITERATOR::index_reference index_reference;
301 
302  /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
303  */
304  typedef typename IMAGE_ITERATOR::pointer pointer;
305 
306  /** the iterator's difference type (argument type of <TT>iter[diff]</TT>)
307  */
308  typedef typename IMAGE_ITERATOR::difference_type::MoveY difference_type;
309 
310  /** the iterator tag (random access iterator)
311  */
312  typedef std::random_access_iterator_tag iterator_category;
313 
314  /** the type of the adapted iterator
315  */
316  typedef IMAGE_ITERATOR Adaptee;
317 
318  /** Construct from an the image iterator to be adapted.
319  */
320  RowIterator(IMAGE_ITERATOR const & i)
321  : IMAGE_ITERATOR(i)
322  {}
323 
324  /** Assignment.
325  */
327  {
329 
330  return *this;
331  }
332 
333  /** Assign a new base iterator.
334  */
335  RowIterator & operator=(IMAGE_ITERATOR const & i)
336  {
338 
339  return *this;
340  }
341 
342  /** @name Navigation */
343  //@{
344  ///
345  RowIterator & operator++()
346  {
347  ++(this->x);
348  return *this;
349  }
350  ///
351  RowIterator operator++(int)
352  {
353  RowIterator ret(*this);
354  (this->x)++;
355  return ret;
356  }
357 
358  ///
359  RowIterator & operator--()
360  {
361  --(this->x);
362  return *this;
363  }
364 
365  ///
366  RowIterator operator--(int)
367  {
368  RowIterator ret(*this);
369  (this->x)--;
370  return ret;
371  }
372 
373  ///
374  RowIterator & operator+=(int d)
375  {
376  this->x += d;
377  return *this;
378  }
379 
380  ///
381  RowIterator & operator-=(int d)
382  {
383  this->x -= d;
384  return *this;
385  }
386  //@}
387 
388  /** @name Methods */
389  //@{
390  /** Construct iterator at a distance.
391  */
392  RowIterator operator+(int d) const
393  {
394  IMAGE_ITERATOR ret(*this);
395  ret.x += d;
396  return RowIterator(ret);
397  }
398  /** Construct iterator at a distance.
399  */
400  RowIterator operator-(int d) const
401  {
402  IMAGE_ITERATOR ret(*this);
403  ret.x -= d;
404  return RowIterator(ret);
405  }
406  /** Calculate distance.
407  */
408  int operator-(RowIterator const & c) const
409  {
410  return this->x - c.x;
411  }
412 
413  /** Equality.
414  */
415  bool operator==(RowIterator const & c) const
416  {
417  return IMAGE_ITERATOR::operator==(c);
418  }
419 
420  /** Inequality.
421  */
422  bool operator!=(RowIterator const & c) const
423  {
424  return IMAGE_ITERATOR::operator!=(c);
425  }
426 
427  /** Smaller than.
428  */
429  bool operator<(RowIterator const & c) const
430  {
431  return this->x < c.x;
432  }
433 
434  /** Access current pixel.
435  */
437  {
438  return IMAGE_ITERATOR::operator*();
439  }
440 
441  /** Access pixel at distance d.
442  */
444  {
445  return IMAGE_ITERATOR::operator()(d, 0);
446  }
447 
448  /** Call member function of current pixel.
449  */
451  {
453  }
454 
455  /** Get a reference to the adapted iterator
456  */
457  Adaptee & adaptee() const { return (Adaptee &)*this; }
458 
459  //@}
460 };
461 
462 /********************************************************/
463 /* */
464 /* LineIterator */
465 /* */
466 /********************************************************/
467 
468 /** \brief Iterator adapter to iterate along an arbitrary line on the image.
469 
470  This iterator may be initialized from a standard ImageIterator,
471  a MultibandImageIterator and so on.
472  It gives you STL-compatible (forward iterator) access to
473  an arbitraty line on the image.
474  The iterator gets associated with the accessor of the base iterator.
475 
476  <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
477 
478  Namespace: vigra
479 
480 */
481 template <class IMAGE_ITERATOR>
482 class LineIterator : private IMAGE_ITERATOR
483 {
484  public:
485  /** the iterator's value type
486  */
487  typedef typename IMAGE_ITERATOR::value_type value_type;
488 
489  /** the iterator's value type
490  */
491  typedef typename IMAGE_ITERATOR::value_type PixelType;
492 
493  /** the iterator's reference type (return type of <TT>*iter</TT>)
494  */
495  typedef typename IMAGE_ITERATOR::reference reference;
496 
497  /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
498  */
499  typedef typename IMAGE_ITERATOR::pointer pointer;
500 
501  /** the iterator tag (forward iterator)
502  */
503  typedef std::forward_iterator_tag iterator_category;
504 
505  /** the type of the adapted iterator
506  */
507  typedef IMAGE_ITERATOR Adaptee;
508 
509  /** Construct from an the image iterator to be adapted.
510  */
511  LineIterator(IMAGE_ITERATOR const & start,
512  IMAGE_ITERATOR const & end)
513  : IMAGE_ITERATOR(start), x_(0.0), y_(0.0)
514  {
515  int dx = end.x - start.x;
516  int dy = end.y - start.y;
517  int adx = (dx < 0) ? -dx : dx;
518  int ady = (dy < 0) ? -dy : dy;
519  int dd = (adx > ady) ? adx : ady;
520  if(dd == 0) dd = 1;
521 
522  dx_ = (double)dx / dd;
523  dy_ = (double)dy / dd;
524  if(adx > ady) y_ += dy_ / 2.0;
525  else x_ += dx_ / 2.0;
526  }
527 
528  /** @name Navigation */
529  //@{
530  ///
531  LineIterator & operator++()
532  {
533  x_ += dx_;
534  if(x_ >= 1.0) {
535  x_ -= 1.0;
536  ++(this->x);
537  }
538  else if(x_ <= -1.0) {
539  x_ += 1.0;
540  --(this->x);
541  }
542  y_ += dy_;
543  if(y_ >= 1.0) {
544  y_ -= 1.0;
545  ++(this->y);
546  }
547  else if(y_ <= -1.0) {
548  y_ += 1.0;
549  --(this->y);
550  }
551  return *this;
552  }
553  ///
554  LineIterator operator++(int)
555  {
556  LineIterator ret(*this);
557  operator++();
558  return ret;
559  }
560 
561  //@}
562 
563  /** @name Methods */
564  //@{
565  /** Equality.
566  */
567  bool operator==(LineIterator const & c) const
568  {
569  return IMAGE_ITERATOR::operator==(c);
570  }
571 
572  /** Inequality.
573  */
574  bool operator!=(LineIterator const & c) const
575  {
576  return IMAGE_ITERATOR::operator!=(c);
577  }
578 
579  /** Access current pixel.
580  */
582  {
583  return IMAGE_ITERATOR::operator*();
584  }
585 
586  /** Call member function for current pixel.
587  */
589  {
591  }
592 
593  /** Get a reference to the adapted iterator
594  */
595  Adaptee & adaptee() const { return (Adaptee &)*this; }
596 
597  //@}
598 
599  private:
600 
601  double x_, y_, dx_, dy_;
602 };
603 
604 //@}
605 
606 } // namespace vigra
607 
608 #endif // VIGRA_IMAGEITERATORADAPTER_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Tue Jul 10 2012)