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

vigra/imageiteratoradapter.hxx |

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