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

| vigra/basicimageview.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 #ifndef VIGRA_BASICIMAGEVIEW_HXX
    00039 #define VIGRA_BASICIMAGEVIEW_HXX
    00040
    00041 #include "vigra/imageiterator.hxx"
    00042 #include "vigra/initimage.hxx"
    00043
    00044 namespace vigra {
    00045
    00046
    00047 /********************************************************/
    00048 /*                                                      */
    00049 /*                     BasicImageView                   */
    00050 /*                                                      */
    00051 /********************************************************/
    00052
    00053 /** \brief BasicImage using foreign memory.
    00054
    00055     This class provides the same interface as \ref vigra::BasicImage
    00056     (with the exception of <tt>resize()</tt>) but the image's
    00057     memory is provided from the outside instead of allocated internally.
    00058
    00059     A <tt>BasicImageView</tt> can also be created from a
    00060     \ref vigra::MultiArrayView with the appropriate shape -- see
    00061     \ref MultiArrayToImage.
    00062
    00063     <b>\#include</b> "<a href="basicimageview_8hxx-source.html">vigra/basicimageview.hxx</a>"
    00064
    00065     Namespace: vigra
    00066 */
    00067 template <class PIXELTYPE>
    00068 class BasicImageView
    00069 {
    00070   public:
    00071
    00072         /** the BasicImageView's pixel type
    00073         */
    00074     typedef PIXELTYPE value_type;
    00075
    00076         /** the BasicImageView's pixel type
    00077         */
    00078     typedef PIXELTYPE PixelType;
    00079
    00080         /** the BasicImageView's reference type (i.e. the
    00081             return type of image[diff] and image(dx,dy))
    00082         */
    00083     typedef PIXELTYPE &       reference;
    00084
    00085         /** the BasicImageView's const reference type (i.e. the
    00086             return type of image[diff] and image(dx,dy) when image is const)
    00087         */
    00088     typedef PIXELTYPE const & const_reference;
    00089
    00090         /** the BasicImageView's pointer type
    00091         */
    00092     typedef PIXELTYPE *       pointer;
    00093
    00094         /** the BasicImageView's const pointer type
    00095         */
    00096     typedef PIXELTYPE const * const_pointer;
    00097
    00098         /** the BasicImageView's 1D random access iterator
    00099             (note: lower case 'iterator' is a STL compatible 1D random
    00100              access iterator, don't confuse with capitalized Iterator)
    00101         */
    00102     typedef PIXELTYPE * iterator;
    00103
    00104         /** deprecated, use <TT>iterator</TT> instead
    00105         */
    00106     typedef PIXELTYPE * ScanOrderIterator;
    00107
    00108         /** the BasicImageView's 1D random access const iterator
    00109             (note: lower case 'const_iterator' is a STL compatible 1D
    00110             random access const iterator)
    00111         */
    00112     typedef PIXELTYPE const * const_iterator;
    00113
    00114         /** deprecated, use <TT>const_iterator</TT> instead
    00115         */
    00116     typedef PIXELTYPE const * ConstScanOrderIterator;
    00117
    00118         /** the BasicImageView's 2D random access iterator ('traverser')
    00119         */
    00120     typedef ImageIterator<value_type> traverser;
    00121
    00122         /** deprecated, use <TT>traverser</TT> instead
    00123         */
    00124     typedef ImageIterator<value_type> Iterator;
    00125
    00126         /** the BasicImageView's 2D random access const iterator ('const traverser')
    00127         */
    00128     typedef ConstImageIterator<value_type> const_traverser;
    00129
    00130         /** deprecated, use <TT>const_traverser</TT> instead
    00131         */
    00132     typedef ConstImageIterator<value_type> ConstIterator;
    00133
    00134         /** the row iterator associated with the traverser
    00135         */
    00136     typedef typename traverser::row_iterator row_iterator;
    00137
    00138         /** the const row iterator associated with the const_traverser
    00139         */
    00140     typedef typename const_traverser::row_iterator const_row_iterator;
    00141
    00142         /** the column iterator associated with the traverser
    00143         */
    00144     typedef typename traverser::column_iterator column_iterator;
    00145
    00146         /** the const column iterator associated with the const_traverser
    00147         */
    00148     typedef typename const_traverser::column_iterator const_column_iterator;
    00149
    00150         /** the BasicImageView's difference type (argument type of image[diff])
    00151         */
    00152     typedef Diff2D difference_type;
    00153
    00154          /** the BasicImageView's size type (result type of image.size())
    00155         */
    00156     typedef Size2D size_type;
    00157
    00158        /** the BasicImageView's default accessor
    00159         */
    00160     typedef typename
    00161           IteratorTraits<traverser>::DefaultAccessor Accessor;
    00162
    00163         /** the BasicImageView's default const accessor
    00164         */
    00165     typedef typename
    00166           IteratorTraits<const_traverser>::DefaultAccessor ConstAccessor;
    00167
    00168         /** construct image of size 0x0
    00169         */
    00170     BasicImageView()
    00171     : data_(0),
    00172       width_(0),
    00173       height_(0),
    00174       stride_(0)
    00175     {}
    00176
    00177         /** construct view of size w x h
    00178         */
    00179     BasicImageView(const_pointer data, int w, int h, int stride = 0)
    00180     : data_(const_cast<pointer>(data)),
    00181       width_(w),
    00182       height_(h),
    00183       stride_(stride == 0 ? w : stride)
    00184     {}
    00185
    00186         /** construct view of size size.x x size.y
    00187         */
    00188     BasicImageView(const_pointer data, difference_type const & size, int stride = 0)
    00189     : data_(const_cast<pointer>(data)),
    00190       width_(size.x),
    00191       height_(size.y),
    00192       stride_(stride == 0 ? size.x : stride)
    00193     {}
    00194
    00195         /** set Image with const value
    00196         */
    00197     BasicImageView & init(value_type const & pixel)
    00198     {
    00199         initImage(upperLeft(), lowerRight(), accessor(), pixel);
    00200
    00201         return *this;
    00202     }
    00203
    00204         /** width of Image
    00205         */
    00206     int width() const
    00207     {
    00208         return width_;
    00209     }
    00210
    00211         /** height of Image
    00212         */
    00213     int height() const
    00214     {
    00215         return height_;
    00216     }
    00217
    00218         /** stride of Image.
    00219             Memory offset between the start of two successive rows.
    00220         */
    00221     int stride() const
    00222     {
    00223         return stride_;
    00224     }
    00225
    00226         /** size of Image
    00227         */
    00228     size_type size() const
    00229     {
    00230         return size_type(width(), height());
    00231     }
    00232
    00233         /** test whether a given coordinate is inside the image
    00234         */
    00235     bool isInside(difference_type const & d) const
    00236     {
    00237         return d.x >= 0 && d.y >= 0 &&
    00238                d.x < width() && d.y < height();
    00239     }
    00240
    00241         /** access pixel at given location. <br>
    00242         usage: <TT> value_type value = image[Diff2D(1,2)] </TT>
    00243         */
    00244     reference operator[](difference_type const & d)
    00245     {
    00246         return data_[d.y*stride_ + d.x];
    00247     }
    00248
    00249         /** read pixel at given location. <br>
    00250         usage: <TT> value_type value = image[Diff2D(1,2)] </TT>
    00251         */
    00252     const_reference operator[](difference_type const & d) const
    00253     {
    00254         return data_[d.y*stride_ + d.x];
    00255     }
    00256
    00257         /** access pixel at given location. <br>
    00258         usage: <TT> value_type value = image(1,2) </TT>
    00259         */
    00260     reference operator()(int dx, int dy)
    00261     {
    00262         return data_[dy*stride_ + dx];
    00263     }
    00264
    00265         /** read pixel at given location. <br>
    00266         usage: <TT> value_type value = image(1,2) </TT>
    00267         */
    00268     const_reference operator()(int dx, int dy) const
    00269     {
    00270         return data_[dy*stride_ + dx];
    00271     }
    00272
    00273         /** access pixel at given location.
    00274             Note that the 'x' index is the trailing index. <br>
    00275         usage: <TT> value_type value = image[2][1] </TT>
    00276         */
    00277     pointer operator[](int dy)
    00278     {
    00279         return data_ + dy*stride_;
    00280     }
    00281
    00282         /** read pixel at given location.
    00283             Note that the 'x' index is the trailing index. <br>
    00284         usage: <TT> value_type value = image[2][1] </TT>
    00285         */
    00286     const_pointer operator[](int dy) const
    00287     {
    00288         return data_ + dy*stride_;
    00289     }
    00290
    00291         /** init 2D random access iterator poining to upper left pixel
    00292         */
    00293     traverser upperLeft()
    00294     {
    00295         return traverser(data_, stride_);
    00296     }
    00297
    00298         /** init 2D random access iterator poining to
    00299          pixel(width, height), i.e. one pixel right and below lower right
    00300          corner of the image as is common in C/C++.
    00301         */
    00302     traverser lowerRight()
    00303     {
    00304         return upperLeft() + size();
    00305     }
    00306
    00307         /** init 2D random access const iterator poining to upper left pixel
    00308         */
    00309     const_traverser upperLeft() const
    00310     {
    00311         return const_traverser(data_, stride_);
    00312     }
    00313
    00314         /** init 2D random access const iterator poining to
    00315          pixel(width, height), i.e. one pixel right and below lower right
    00316          corner of the image as is common in C/C++.
    00317         */
    00318     const_traverser lowerRight() const
    00319     {
    00320         return upperLeft() + size();
    00321     }
    00322
    00323         /** init 1D random access iterator pointing to first pixel.
    00324             Note: Only works if stride equals width.
    00325         */
    00326     iterator begin()
    00327     {
    00328         vigra_precondition(stride_ == width_,
    00329             "BasicImageView::begin(): "
    00330             "can only create scan order iterator if width() == stride().");
    00331         return data_;
    00332     }
    00333
    00334         /** init 1D random access iterator pointing past the end.
    00335             Note: Only works if stride equals width.
    00336         */
    00337     iterator end()
    00338     {
    00339         vigra_precondition(stride_ == width_,
    00340             "BasicImageView::end(): "
    00341             "can only create scan order iterator if width() == stride().");
    00342         return data_ + width() * height();
    00343     }
    00344
    00345         /** init 1D random access const iterator pointing to first pixel.
    00346             Note: Only works if stride equals width.
    00347         */
    00348     const_iterator begin() const
    00349     {
    00350         vigra_precondition(stride_ == width_,
    00351             "BasicImageView::begin(): "
    00352             "can only create scan order iterator if width() == stride().");
    00353         return data_;
    00354     }
    00355
    00356         /** init 1D random access const iterator pointing past the end.
    00357             Note: Only works if stride equals width.
    00358         */
    00359     const_iterator end() const
    00360     {
    00361         vigra_precondition(stride_ == width_,
    00362             "BasicImageView::end(): "
    00363             "can only create scan order iterator if width() == stride().");
    00364         return data_ + width() * height();
    00365     }
    00366
    00367         /** init 1D random access iterator pointing to first pixel of row \a y
    00368         */
    00369     row_iterator rowBegin(int y)
    00370     {
    00371         return data_ + stride_ * y;
    00372     }
    00373
    00374         /** init 1D random access iterator pointing past the end of row \a y
    00375         */
    00376     row_iterator rowEnd(int y)
    00377     {
    00378         return rowBegin(y) + width();
    00379     }
    00380
    00381         /** init 1D random access const iterator pointing to first pixel of row \a y
    00382         */
    00383     const_row_iterator rowBegin(int y) const
    00384     {
    00385         return data_ + stride_ * y;
    00386     }
    00387
    00388         /** init 1D random access const iterator pointing past the end of row \a y
    00389         */
    00390     const_row_iterator rowEnd(int y) const
    00391     {
    00392         return rowBegin(y) + width();
    00393     }
    00394
    00395         /** init 1D random access iterator pointing to first pixel of column \a x
    00396         */
    00397     column_iterator columnBegin(int x)
    00398     {
    00399         typedef typename column_iterator::BaseType Iter;
    00400         return column_iterator(Iter(data_ + x, stride_));
    00401     }
    00402
    00403         /** init 1D random access iterator pointing past the end of column \a x
    00404         */
    00405     column_iterator columnEnd(int x)
    00406     {
    00407         return columnBegin(x) + height();
    00408     }
    00409
    00410         /** init 1D random access const iterator pointing to first pixel of column \a x
    00411         */
    00412     const_column_iterator columnBegin(int x) const
    00413     {
    00414         typedef typename const_column_iterator::BaseType Iter;
    00415         return const_column_iterator(Iter(data_ + x, stride_));
    00416     }
    00417
    00418         /** init 1D random access const iterator pointing past the end of column \a x
    00419         */
    00420     const_column_iterator columnEnd(int x) const
    00421     {
    00422         return columnBegin(x) + height();
    00423     }
    00424
    00425         /** get a pointer to the internal data
    00426         */
    00427     const_pointer data() const
    00428     {
    00429         return data_;
    00430     }
    00431
    00432         /** return default accessor
    00433         */
    00434     Accessor accessor()
    00435     {
    00436         return Accessor();
    00437     }
    00438
    00439         /** return default const accessor
    00440         */
    00441     ConstAccessor accessor() const
    00442     {
    00443         return ConstAccessor();
    00444     }
    00445
    00446   private:
    00447
    00448     pointer data_;
    00449     int width_, height_, stride_;
    00450 };
    00451
    00452
    00453 /********************************************************/
    00454 /*                                                      */
    00455 /*              argument object factories               */
    00456 /*                                                      */
    00457 /********************************************************/
    00458
    00459 template <class PixelType, class Accessor>
    00460 inline triple<typename BasicImageView<PixelType>::const_traverser,
    00461               typename BasicImageView<PixelType>::const_traverser, Accessor>
    00462 srcImageRange(BasicImageView<PixelType> const & img, Accessor a)
    00463 {
    00464     return triple<typename BasicImageView<PixelType>::const_traverser,
    00465                   typename BasicImageView<PixelType>::const_traverser,
    00466           Accessor>(img.upperLeft(),
    00467                     img.lowerRight(),
    00468                     a);
    00469 }
    00470
    00471 template <class PixelType, class Accessor>
    00472 inline pair<typename BasicImageView<PixelType>::const_traverser, Accessor>
    00473 srcImage(BasicImageView<PixelType> const & img, Accessor a)
    00474 {
    00475     return pair<typename BasicImageView<PixelType>::const_traverser,
    00476                 Accessor>(img.upperLeft(), a);
    00477 }
    00478
    00479 template <class PixelType, class Accessor>
    00480 inline triple<typename BasicImageView<PixelType>::traverser,
    00481               typename BasicImageView<PixelType>::traverser, Accessor>
    00482 destImageRange(BasicImageView<PixelType> & img, Accessor a)
    00483 {
    00484     return triple<typename BasicImageView<PixelType>::traverser,
    00485                   typename BasicImageView<PixelType>::traverser,
    00486           Accessor>(img.upperLeft(),
    00487                     img.lowerRight(),
    00488                     a);
    00489 }
    00490
    00491 template <class PixelType, class Accessor>
    00492 inline pair<typename BasicImageView<PixelType>::traverser, Accessor>
    00493 destImage(BasicImageView<PixelType> & img, Accessor a)
    00494 {
    00495     return pair<typename BasicImageView<PixelType>::traverser,
    00496                 Accessor>(img.upperLeft(), a);
    00497 }
    00498
    00499 template <class PixelType, class Accessor>
    00500 inline pair<typename BasicImageView<PixelType>::const_traverser, Accessor>
    00501 maskImage(BasicImageView<PixelType> const & img, Accessor a)
    00502 {
    00503     return pair<typename BasicImageView<PixelType>::const_traverser,
    00504                 Accessor>(img.upperLeft(), a);
    00505 }
    00506
    00507 /****************************************************************/
    00508
    00509 template <class PixelType>
    00510 inline triple<typename BasicImageView<PixelType>::const_traverser,
    00511               typename BasicImageView<PixelType>::const_traverser,
    00512               typename BasicImageView<PixelType>::ConstAccessor>
    00513 srcImageRange(BasicImageView<PixelType> const & img)
    00514 {
    00515     return triple<typename BasicImageView<PixelType>::const_traverser,
    00516                   typename BasicImageView<PixelType>::const_traverser,
    00517                   typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(),
    00518                                                                      img.lowerRight(),
    00519                                                                      img.accessor());
    00520 }
    00521
    00522 template <class PixelType>
    00523 inline pair< typename BasicImageView<PixelType>::const_traverser,
    00524              typename BasicImageView<PixelType>::ConstAccessor>
    00525 srcImage(BasicImageView<PixelType> const & img)
    00526 {
    00527     return pair<typename BasicImageView<PixelType>::const_traverser,
    00528                 typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(),
    00529                                                                    img.accessor());
    00530 }
    00531
    00532 template <class PixelType>
    00533 inline triple< typename BasicImageView<PixelType>::traverser,
    00534                typename BasicImageView<PixelType>::traverser,
    00535                typename BasicImageView<PixelType>::Accessor>
    00536 destImageRange(BasicImageView<PixelType> & img)
    00537 {
    00538     return triple<typename BasicImageView<PixelType>::traverser,
    00539                   typename BasicImageView<PixelType>::traverser,
    00540                   typename BasicImageView<PixelType>::Accessor>(img.upperLeft(),
    00541                                                                 img.lowerRight(),
    00542                                                                 img.accessor());
    00543 }
    00544
    00545 template <class PixelType>
    00546 inline pair< typename BasicImageView<PixelType>::traverser,
    00547              typename BasicImageView<PixelType>::Accessor>
    00548 destImage(BasicImageView<PixelType> & img)
    00549 {
    00550     return pair<typename BasicImageView<PixelType>::traverser,
    00551                 typename BasicImageView<PixelType>::Accessor>(img.upperLeft(),
    00552                                                               img.accessor());
    00553 }
    00554
    00555 template <class PixelType>
    00556 inline pair< typename BasicImageView<PixelType>::const_traverser,
    00557              typename BasicImageView<PixelType>::ConstAccessor>
    00558 maskImage(BasicImageView<PixelType> const & img)
    00559 {
    00560     return pair<typename BasicImageView<PixelType>::const_traverser,
    00561                 typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(),
    00562                                                                    img.accessor());
    00563 }
    00564
    00565 } // namespace vigra
    00566
    00567 #endif /* VIGRA_BASICIMAGEVIEW_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)