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

© Ullrich Köthe (koethe@informatik.uni-hamburg.de) Cognitive Systems Group, University of Hamburg, Germany | html generated using doxygen and Python VIGRA 1.4.0 (21 Dec 2005)