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

vigra/imageiteratoradapter.hxx

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    The VIGRA Website is                                              */
00008 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00009 /*    Please direct questions, bug reports, and contributions to        */
00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00011 /*        vigra@informatik.uni-hamburg.de                               */
00012 /*                                                                      */
00013 /*    Permission is hereby granted, free of charge, to any person       */
00014 /*    obtaining a copy of this software and associated documentation    */
00015 /*    files (the "Software"), to deal in the Software without           */
00016 /*    restriction, including without limitation the rights to use,      */
00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00018 /*    sell copies of the Software, and to permit persons to whom the    */
00019 /*    Software is furnished to do so, subject to the following          */
00020 /*    conditions:                                                       */
00021 /*                                                                      */
00022 /*    The above copyright notice and this permission notice shall be    */
00023 /*    included in all copies or substantial portions of the             */
00024 /*    Software.                                                         */
00025 /*                                                                      */
00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
00034 /*                                                                      */
00035 /************************************************************************/
00036 
00037 
00038 #ifndef VIGRA_IMAGEITERATORADAPTER_HXX
00039 #define VIGRA_IMAGEITERATORADAPTER_HXX
00040 
00041 #include <iterator>   // iterator tags
00042 
00043 namespace vigra {
00044 
00045 /** \addtogroup ImageIteratorAdapters Image Iterator Adapters
00046 
00047      Iterate over rows, columns, neighborhoods, contours, and other image subsets
00048 */
00049 //@{
00050 
00051 /********************************************************/
00052 /*                                                      */
00053 /*                      ColumnIterator                  */
00054 /*                                                      */
00055 /********************************************************/
00056 
00057 /** \brief Iterator adapter to linearly access colums.
00058 
00059     This iterator may be initialized from any standard ImageIterator,
00060     a MultibandImageIterator and so on.
00061     It gives you STL-compatible (random access iterator) access to
00062     one column of the image. If the underlying iterator is a const iterator,
00063     the column iterator will also be const (i.e. doesn't allow to change
00064     the values it points to).
00065     The iterator gets associated with the accessor of the base iterator.
00066 
00067     Note that image iterators usually have a member <TT>columnIterator()</TT>
00068     which returns a column iterator optimized for that particular image class.
00069     ColumnIterator is only necessary if this 'native' column iterator
00070     is not usable in a particular situation or is not provided.
00071 
00072     <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
00073 
00074     Namespace: vigra
00075 
00076 */
00077 template <class IMAGE_ITERATOR>
00078 class ColumnIterator : private IMAGE_ITERATOR
00079 {
00080   public:
00081         /** the iterator's value type
00082         */
00083     typedef typename IMAGE_ITERATOR::value_type value_type;
00084 
00085         /** the iterator's value type
00086         */
00087     typedef typename IMAGE_ITERATOR::value_type PixelType;
00088 
00089         /** the iterator's reference type (return type of <TT>*iter</TT>)
00090         */
00091     typedef typename IMAGE_ITERATOR::reference              reference;
00092 
00093         /** the iterator's index reference type (return type of <TT>iter[n]</TT>)
00094         */
00095     typedef typename IMAGE_ITERATOR::index_reference        index_reference;
00096 
00097         /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
00098         */
00099     typedef typename IMAGE_ITERATOR::pointer                pointer;
00100 
00101         /** the iterator's difference type (argument type of <TT>iter[diff]</TT>)
00102         */
00103     typedef typename IMAGE_ITERATOR::difference_type::MoveY difference_type;
00104 
00105         /** the iterator tag (random access iterator)
00106         */
00107     typedef std::random_access_iterator_tag                 iterator_category;
00108 
00109         /** the type of the adapted iterator
00110         */
00111     typedef IMAGE_ITERATOR Adaptee;
00112 
00113         /** Construct from an the image iterator to be adapted.
00114        */
00115     ColumnIterator(IMAGE_ITERATOR  const & i)
00116     : IMAGE_ITERATOR(i)
00117     {}
00118 
00119         /** Assignment.
00120         */
00121     ColumnIterator & operator=(ColumnIterator  const & i)
00122     {
00123         IMAGE_ITERATOR::operator=(i);
00124 
00125         return *this;
00126     }
00127 
00128         /** Assign a new base iterator.
00129         */
00130     ColumnIterator & operator=(IMAGE_ITERATOR  const & i)
00131     {
00132         IMAGE_ITERATOR::operator=(i);
00133 
00134         return *this;
00135     }
00136 
00137     /** @name Navigation */
00138     //@{
00139         ///
00140     ColumnIterator &  operator++()
00141     {
00142         ++(this->y);
00143         return *this;
00144     }
00145         ///
00146     ColumnIterator  operator++(int)
00147     {
00148         ColumnIterator ret(*this);
00149         (this->y)++;
00150         return ret;
00151     }
00152 
00153         ///
00154     ColumnIterator &  operator--()
00155     {
00156         --(this->y);
00157         return *this;
00158     }
00159 
00160         ///
00161     ColumnIterator  operator--(int)
00162     {
00163         ColumnIterator ret(*this);
00164         (this->y)--;
00165         return ret;
00166     }
00167 
00168         ///
00169     ColumnIterator &  operator+=(int d)
00170     {
00171         this->y += d;
00172         return *this;
00173     }
00174 
00175         ///
00176     ColumnIterator &  operator-=(int d)
00177     {
00178         this->y -= d;
00179         return *this;
00180     }
00181     //@}
00182 
00183     /** @name Methods */
00184     //@{
00185         /** Construct iterator at a distance.
00186         */
00187     ColumnIterator operator+(int d) const
00188     {
00189         IMAGE_ITERATOR ret(*this);
00190         ret.y += d;
00191         return ColumnIterator(ret);
00192     }
00193         /** Construct iterator at a distance.
00194         */
00195     ColumnIterator operator-(int d) const
00196     {
00197         IMAGE_ITERATOR ret(*this);
00198         ret.y -= d;
00199         return ColumnIterator(ret);
00200     }
00201         /** Calculate distance.
00202         */
00203     int operator-(ColumnIterator const & c) const
00204     {
00205         return this->y - c.y;
00206     }
00207 
00208         /** Equality.
00209         */
00210     bool operator==(ColumnIterator const & c) const
00211     {
00212         return IMAGE_ITERATOR::operator==(c);
00213     }
00214 
00215         /** Inequality.
00216         */
00217     bool operator!=(ColumnIterator const & c) const
00218     {
00219         return IMAGE_ITERATOR::operator!=(c);
00220     }
00221 
00222         /** Smaller than.
00223         */
00224     bool operator<(ColumnIterator const & c) const
00225     {
00226         return this->y < c.y;
00227     }
00228 
00229         /** Access current pixel.
00230         */
00231     reference operator*() const
00232     {
00233         return IMAGE_ITERATOR::operator*();
00234     }
00235 
00236         /** Access pixel at distance d.
00237         */
00238     index_reference operator[](int d) const
00239     {
00240         return IMAGE_ITERATOR::operator()(0, d);
00241     }
00242 
00243         /** Call member function of current pixel.
00244         */
00245     pointer operator->() const
00246     {
00247         return IMAGE_ITERATOR::operator->();
00248     }
00249 
00250         /** Get a reference to the adapted iterator
00251         */
00252     Adaptee & adaptee() const { return (Adaptee &)*this; }
00253 
00254     //@}
00255 };
00256 
00257 /********************************************************/
00258 /*                                                      */
00259 /*                      RowIterator                     */
00260 /*                                                      */
00261 /********************************************************/
00262 
00263 /** \brief Iterator adapter to linearly access row.
00264 
00265     This iterator may be initialized from a standard ImageIterator,
00266      a MultibandImageIterator and so on.
00267     It gives you STL-compatible (random access iterator) access to
00268     one row of the image. If the underlying iterator is a const iterator,
00269     the row iterator will also be const (i.e. doesn't allow to change
00270     the values it points to).
00271     The iterator gets associated with the accessor of the base iterator.
00272 
00273     Note that image iterators usually have a member <TT>rowIterator()</TT>
00274     which returns a row iterator optimized for that particular image class.
00275     RowIterator is only necessary if this 'native' row iterator
00276     is not usable in a particular situation or is not provided.
00277 
00278     <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
00279 
00280     Namespace: vigra
00281 
00282 */
00283 template <class IMAGE_ITERATOR>
00284 class RowIterator : private IMAGE_ITERATOR
00285 {
00286   public:
00287         /** the iterator's value type
00288         */
00289     typedef typename IMAGE_ITERATOR::value_type value_type;
00290 
00291         /** the iterator's value type
00292         */
00293     typedef typename IMAGE_ITERATOR::value_type PixelType;
00294 
00295         /** the iterator's reference type (return type of <TT>*iter</TT>)
00296         */
00297     typedef typename IMAGE_ITERATOR::reference              reference;
00298 
00299         /** the iterator's index reference type (return type of <TT>iter[n]</TT>)
00300         */
00301     typedef typename IMAGE_ITERATOR::index_reference        index_reference;
00302 
00303         /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
00304         */
00305     typedef typename IMAGE_ITERATOR::pointer                pointer;
00306 
00307         /** the iterator's difference type (argument type of <TT>iter[diff]</TT>)
00308         */
00309     typedef typename IMAGE_ITERATOR::difference_type::MoveY difference_type;
00310 
00311         /** the iterator tag (random access iterator)
00312         */
00313     typedef std::random_access_iterator_tag                 iterator_category;
00314 
00315         /** the type of the adapted iterator
00316         */
00317     typedef IMAGE_ITERATOR Adaptee;
00318 
00319         /** Construct from an the image iterator to be adapted.
00320         */
00321     RowIterator(IMAGE_ITERATOR  const & i)
00322     : IMAGE_ITERATOR(i)
00323     {}
00324 
00325         /** Assignment.
00326         */
00327     RowIterator & operator=(RowIterator  const & i)
00328     {
00329         IMAGE_ITERATOR::operator=(i);
00330 
00331         return *this;
00332     }
00333 
00334         /** Assign a new base iterator.
00335         */
00336     RowIterator & operator=(IMAGE_ITERATOR  const & i)
00337     {
00338         IMAGE_ITERATOR::operator=(i);
00339 
00340         return *this;
00341     }
00342 
00343     /** @name Navigation */
00344     //@{
00345         ///
00346     RowIterator &  operator++()
00347     {
00348         ++(this->x);
00349         return *this;
00350     }
00351         ///
00352     RowIterator  operator++(int)
00353     {
00354         RowIterator ret(*this);
00355         (this->x)++;
00356         return ret;
00357     }
00358 
00359         ///
00360     RowIterator &  operator--()
00361     {
00362         --(this->x);
00363         return *this;
00364     }
00365 
00366         ///
00367     RowIterator  operator--(int)
00368     {
00369         RowIterator ret(*this);
00370         (this->x)--;
00371         return ret;
00372     }
00373 
00374         ///
00375     RowIterator &  operator+=(int d)
00376     {
00377         this->x += d;
00378         return *this;
00379     }
00380 
00381         ///
00382     RowIterator &  operator-=(int d)
00383     {
00384         this->x -= d;
00385         return *this;
00386     }
00387     //@}
00388 
00389     /** @name Methods */
00390     //@{
00391         /** Construct iterator at a distance.
00392         */
00393     RowIterator operator+(int d) const
00394     {
00395         IMAGE_ITERATOR ret(*this);
00396         ret.x += d;
00397         return RowIterator(ret);
00398     }
00399         /** Construct iterator at a distance.
00400         */
00401     RowIterator operator-(int d) const
00402     {
00403         IMAGE_ITERATOR ret(*this);
00404         ret.x -= d;
00405         return RowIterator(ret);
00406     }
00407         /** Calculate distance.
00408         */
00409     int operator-(RowIterator const & c) const
00410     {
00411         return this->x - c.x;
00412     }
00413 
00414         /** Equality.
00415         */
00416     bool operator==(RowIterator const & c) const
00417     {
00418         return IMAGE_ITERATOR::operator==(c);
00419     }
00420 
00421         /** Inequality.
00422         */
00423     bool operator!=(RowIterator const & c) const
00424     {
00425         return IMAGE_ITERATOR::operator!=(c);
00426     }
00427 
00428         /** Smaller than.
00429         */
00430     bool operator<(RowIterator const & c) const
00431     {
00432         return this->x < c.x;
00433     }
00434 
00435         /** Access current pixel.
00436         */
00437     reference operator*() const
00438     {
00439         return IMAGE_ITERATOR::operator*();
00440     }
00441 
00442         /** Access pixel at distance d.
00443         */
00444     index_reference operator[](int d) const
00445     {
00446         return IMAGE_ITERATOR::operator()(d, 0);
00447     }
00448 
00449         /** Call member function of current pixel.
00450         */
00451     pointer operator->() const
00452     {
00453         return IMAGE_ITERATOR::operator->();
00454     }
00455 
00456         /** Get a reference to the adapted iterator
00457         */
00458     Adaptee & adaptee() const { return (Adaptee &)*this; }
00459 
00460     //@}
00461 };
00462 
00463 /********************************************************/
00464 /*                                                      */
00465 /*                     LineIterator                     */
00466 /*                                                      */
00467 /********************************************************/
00468 
00469 /** \brief Iterator adapter to iterate along an arbitrary line on the image.
00470 
00471     This iterator may be initialized from a standard ImageIterator,
00472      a MultibandImageIterator and so on.
00473     It gives you STL-compatible (forward iterator) access to
00474     an arbitraty line on the image.
00475     The iterator gets associated with the accessor of the base iterator.
00476 
00477     <b>\#include</b> <<a href="imageiteratoradapter_8hxx-source.html">vigra/imageiteratoradapter.hxx</a>>
00478 
00479     Namespace: vigra
00480 
00481 */
00482 template <class IMAGE_ITERATOR>
00483 class LineIterator : private IMAGE_ITERATOR
00484 {
00485   public:
00486         /** the iterator's value type
00487         */
00488     typedef typename IMAGE_ITERATOR::value_type value_type;
00489 
00490         /** the iterator's value type
00491         */
00492     typedef typename IMAGE_ITERATOR::value_type PixelType;
00493 
00494         /** the iterator's reference type (return type of <TT>*iter</TT>)
00495         */
00496     typedef typename IMAGE_ITERATOR::reference              reference;
00497 
00498         /** the iterator's pointer type (return type of <TT>iter.operator->()</TT>)
00499         */
00500     typedef typename IMAGE_ITERATOR::pointer                pointer;
00501 
00502         /** the iterator tag (forward iterator)
00503         */
00504     typedef std::forward_iterator_tag                       iterator_category;
00505 
00506         /** the type of the adapted iterator
00507         */
00508     typedef IMAGE_ITERATOR Adaptee;
00509 
00510         /** Construct from an the image iterator to be adapted.
00511         */
00512     LineIterator(IMAGE_ITERATOR  const & start,
00513                  IMAGE_ITERATOR  const & end)
00514     : IMAGE_ITERATOR(start), x_(0.0), y_(0.0)
00515     {
00516         int dx = end.x - start.x;
00517         int dy = end.y - start.y;
00518         int adx = (dx < 0) ? -dx : dx;
00519         int ady = (dy < 0) ? -dy : dy;
00520         int dd = (adx > ady) ? adx : ady;
00521         if(dd == 0) dd = 1;
00522 
00523         dx_ = (double)dx / dd;
00524         dy_ = (double)dy / dd;
00525         if(adx > ady) y_ += dy_ / 2.0;
00526         else          x_ += dx_ / 2.0;
00527     }
00528 
00529     /** @name Navigation */
00530     //@{
00531         ///
00532     LineIterator &  operator++()
00533     {
00534         x_ += dx_;
00535         if(x_ >= 1.0) {
00536             x_ -= 1.0;
00537             ++(this->x);
00538         }
00539         else if(x_ <= -1.0) {
00540             x_ += 1.0;
00541             --(this->x);
00542         }
00543         y_ += dy_;
00544         if(y_ >= 1.0) {
00545             y_ -= 1.0;
00546             ++(this->y);
00547         }
00548         else if(y_ <= -1.0) {
00549             y_ += 1.0;
00550             --(this->y);
00551         }
00552         return *this;
00553     }
00554         ///
00555     LineIterator  operator++(int)
00556     {
00557         LineIterator ret(*this);
00558         operator++();
00559         return ret;
00560     }
00561 
00562     //@}
00563 
00564     /** @name Methods */
00565     //@{
00566         /** Equality.
00567        */
00568     bool operator==(LineIterator const & c) const
00569     {
00570         return IMAGE_ITERATOR::operator==(c);
00571     }
00572 
00573         /** Inequality.
00574        */
00575     bool operator!=(LineIterator const & c) const
00576     {
00577         return IMAGE_ITERATOR::operator!=(c);
00578     }
00579 
00580         /** Access current pixel.
00581        */
00582     reference operator*() const
00583     {
00584         return IMAGE_ITERATOR::operator*();
00585     }
00586 
00587         /** Call member function for current pixel.
00588        */
00589     pointer operator->() const
00590     {
00591         return IMAGE_ITERATOR::operator->();
00592     }
00593 
00594         /** Get a reference to the adapted iterator
00595        */
00596     Adaptee & adaptee() const { return (Adaptee &)*this; }
00597 
00598     //@}
00599 
00600   private:
00601 
00602     double x_, y_, dx_, dy_;
00603 };
00604 
00605 //@}
00606 
00607 } // namespace vigra
00608 
00609 #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.6.0 (5 Nov 2009)