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

details vigra/basicimageview.hxx VIGRA

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.2.0, Aug 07 2003 )                                    */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the VIGRA distribution.                                           */
00011 /*                                                                      */
00012 /*    The VIGRA Website is                                              */
00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        koethe@informatik.uni-hamburg.de                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
00020 /*                                                                      */
00021 /************************************************************************/
00022 
00023 #ifndef VIGRA_BASICIMAGEVIEW_HXX
00024 #define VIGRA_BASICIMAGEVIEW_HXX
00025 
00026 #include "vigra/imageiterator.hxx"
00027 #include "vigra/initimage.hxx"
00028 
00029 namespace vigra {
00030 
00031 
00032 /********************************************************/
00033 /*                                                      */
00034 /*                     BasicImageView                   */
00035 /*                                                      */
00036 /********************************************************/
00037 
00038 /** \brief BasicImage using foreign memory.
00039 
00040     This class provides the same interface as \ref vigra::BasicImage
00041     (with the exception of <tt>resize()</tt>) but the image's
00042     memory is provided from the outside instead of allocated internally.
00043     
00044     <b>\#include</b> "<a href="basicimageview_8hxx-source.html">vigra/basicimageview.hxx</a>"
00045 
00046     Namespace: vigra
00047 */
00048 template <class PIXELTYPE>
00049 class BasicImageView
00050 {
00051   public:
00052 
00053         /** the BasicImageView's pixel type
00054         */
00055     typedef PIXELTYPE value_type;
00056 
00057         /** the BasicImageView's pixel type
00058         */
00059     typedef PIXELTYPE PixelType;
00060 
00061         /** the BasicImageView's reference type (i.e. the
00062             return type of image[diff] and image(dx,dy))
00063         */
00064     typedef PIXELTYPE &       reference;
00065 
00066         /** the BasicImageView's const reference type (i.e. the
00067             return type of image[diff] and image(dx,dy) when image is const)
00068         */
00069     typedef PIXELTYPE const & const_reference;
00070 
00071         /** the BasicImageView's pointer type
00072         */
00073     typedef PIXELTYPE *       pointer;
00074 
00075         /** the BasicImageView's const pointer type
00076         */
00077     typedef PIXELTYPE const * const_pointer;
00078 
00079         /** the BasicImageView's 1D random access iterator
00080             (note: lower case 'iterator' is a STL compatible 1D random
00081              access iterator, don't confuse with capitalized Iterator)
00082         */
00083     typedef PIXELTYPE * iterator;
00084 
00085         /** deprecated, use <TT>iterator</TT> instead
00086         */
00087     typedef PIXELTYPE * ScanOrderIterator;
00088 
00089         /** the BasicImageView's 1D random access const iterator
00090             (note: lower case 'const_iterator' is a STL compatible 1D
00091             random access const iterator)
00092         */
00093     typedef PIXELTYPE const * const_iterator;
00094 
00095         /** deprecated, use <TT>const_iterator</TT> instead
00096         */
00097     typedef PIXELTYPE const * ConstScanOrderIterator;
00098 
00099         /** the BasicImageView's 2D random access iterator ('traverser')
00100         */
00101     typedef ImageIterator<value_type> traverser;
00102 
00103         /** deprecated, use <TT>traverser</TT> instead
00104         */
00105     typedef ImageIterator<value_type> Iterator;
00106 
00107         /** the BasicImageView's 2D random access const iterator ('const traverser')
00108         */
00109     typedef ConstImageIterator<value_type> const_traverser;
00110 
00111         /** deprecated, use <TT>const_traverser</TT> instead
00112         */
00113     typedef ConstImageIterator<value_type> ConstIterator;
00114 
00115         /** the BasicImageView's difference type (argument type of image[diff])
00116         */
00117     typedef Diff2D difference_type;
00118 
00119          /** the BasicImageView's size type (result type of image.size())
00120         */
00121     typedef Size2D size_type;
00122 
00123        /** the BasicImageView's default accessor
00124         */
00125     typedef typename
00126           IteratorTraits<traverser>::DefaultAccessor Accessor;
00127 
00128         /** the BasicImageView's default const accessor
00129         */
00130     typedef typename
00131           IteratorTraits<const_traverser>::DefaultAccessor ConstAccessor;
00132 
00133         /** construct image of size 0x0
00134         */
00135     BasicImageView()
00136     : data_(0),
00137       width_(0),
00138       height_(0),
00139       stride_(0)
00140     {}
00141 
00142         /** construct view of size w x h
00143         */
00144     BasicImageView(const_pointer data, int w, int h, int stride = 0)
00145     : data_(const_cast<pointer>(data)),
00146       width_(w),
00147       height_(h),
00148       stride_(stride == 0 ? w : stride)
00149     {}
00150 
00151         /** construct view of size size.x x size.y
00152         */
00153     BasicImageView(const_pointer data, difference_type const & size, int stride = 0)
00154     : data_(const_cast<pointer>(data)),
00155       width_(size.x),
00156       height_(size.y),
00157       stride_(stride == 0 ? size.x : stride)
00158     {}
00159 
00160         /** set Image with const value
00161         */
00162     BasicImageView & init(value_type const & pixel)
00163     {
00164         initImage(upperLeft(), lowerRight(), accessor(), pixel);
00165 
00166         return *this;
00167     }
00168 
00169         /** width of Image
00170         */
00171     int width() const
00172     {
00173         return width_;
00174     }
00175 
00176         /** height of Image
00177         */
00178     int height() const
00179     {
00180         return height_;
00181     }
00182 
00183         /** stride of Image. 
00184             Memory offset between the start of two successive rows.
00185         */
00186     int stride() const
00187     {
00188         return stride_;
00189     }
00190 
00191         /** size of Image
00192         */
00193     size_type size() const
00194     {
00195         return size_type(width(), height());
00196     }
00197 
00198         /** test whether a given coordinate is inside the image
00199         */
00200     bool isInside(difference_type const & d) const
00201     {
00202         return d.x >= 0 && d.y >= 0 &&
00203                d.x < width() && d.y < height();
00204     }
00205 
00206         /** access pixel at given location. <br>
00207         usage: <TT> value_type value = image[Diff2D(1,2)] </TT>
00208         */
00209     reference operator[](difference_type const & d)
00210     {
00211         return data_[d.y*stride_ + d.x];
00212     }
00213 
00214         /** read pixel at given location. <br>
00215         usage: <TT> value_type value = image[Diff2D(1,2)] </TT>
00216         */
00217     const_reference operator[](difference_type const & d) const
00218     {
00219         return data_[d.y*stride_ + d.x];
00220     }
00221 
00222         /** access pixel at given location. <br>
00223         usage: <TT> value_type value = image(1,2) </TT>
00224         */
00225     reference operator()(int dx, int dy)
00226     {
00227         return data_[dy*stride_ + dx];
00228     }
00229 
00230         /** read pixel at given location. <br>
00231         usage: <TT> value_type value = image(1,2) </TT>
00232         */
00233     const_reference operator()(int dx, int dy) const
00234     {
00235         return data_[dy*stride_ + dx];
00236     }
00237 
00238         /** access pixel at given location.
00239             Note that the 'x' index is the trailing index. <br>
00240         usage: <TT> value_type value = image[2][1] </TT>
00241         */
00242     pointer operator[](int dy)
00243     {
00244         return data_ + dy*stride_;
00245     }
00246 
00247         /** read pixel at given location.
00248             Note that the 'x' index is the trailing index. <br>
00249         usage: <TT> value_type value = image[2][1] </TT>
00250         */
00251     const_pointer operator[](int dy) const
00252     {
00253         return data_ + dy*stride_;
00254     }
00255 
00256         /** init 2D random access iterator poining to upper left pixel
00257         */
00258     traverser upperLeft()
00259     {
00260         return traverser(data_, stride_);
00261     }
00262 
00263         /** init 2D random access iterator poining to
00264          pixel(width, height), i.e. one pixel right and below lower right
00265          corner of the image as is common in C/C++.
00266         */
00267     traverser lowerRight()
00268     {
00269         return upperLeft() + size();
00270     }
00271 
00272         /** init 2D random access const iterator poining to upper left pixel
00273         */
00274     const_traverser upperLeft() const
00275     {
00276         return const_traverser(data_, stride_);
00277     }
00278 
00279         /** init 2D random access const iterator poining to
00280          pixel(width, height), i.e. one pixel right and below lower right
00281          corner of the image as is common in C/C++.
00282         */
00283     const_traverser lowerRight() const
00284     {
00285         return upperLeft() + size();
00286     }
00287 
00288         /** init 1D random access iterator pointing to first pixel.
00289             Note: Only works if stride equals width.
00290         */
00291     iterator begin()
00292     {
00293         vigra_precondition(stride_ == width_,
00294             "BasicImageView::begin(): "
00295             "can only create scan order iterator if width() == stride().");
00296         return data_;
00297     }
00298 
00299         /** init 1D random access iterator pointing past the end.
00300             Note: Only works if stride equals width.
00301         */
00302     iterator end()
00303     {
00304         vigra_precondition(stride_ == width_,
00305             "BasicImageView::end(): "
00306             "can only create scan order iterator if width() == stride().");
00307         return data_ + width() * height();
00308     }
00309 
00310         /** init 1D random access const iterator pointing to first pixel.
00311             Note: Only works if stride equals width.
00312         */
00313     const_iterator begin() const
00314     {
00315         vigra_precondition(stride_ == width_,
00316             "BasicImageView::begin(): "
00317             "can only create scan order iterator if width() == stride().");
00318         return data_;
00319     }
00320 
00321         /** init 1D random access const iterator pointing past the end.
00322             Note: Only works if stride equals width.
00323         */
00324     const_iterator end() const
00325     {
00326         vigra_precondition(stride_ == width_,
00327             "BasicImageView::end(): "
00328             "can only create scan order iterator if width() == stride().");
00329         return data_ + width() * height();
00330     }
00331 
00332         /** return default accessor
00333         */
00334     Accessor accessor()
00335     {
00336         return Accessor();
00337     }
00338 
00339         /** return default const accessor
00340         */
00341     ConstAccessor accessor() const
00342     {
00343         return ConstAccessor();
00344     }
00345 
00346   private:
00347 
00348     pointer data_;
00349     int width_, height_, stride_;
00350 };
00351 
00352 
00353 /********************************************************/
00354 /*                                                      */
00355 /*              argument object factories               */
00356 /*                                                      */
00357 /********************************************************/
00358 
00359 template <class PixelType, class Accessor>
00360 inline triple<typename BasicImageView<PixelType>::const_traverser, 
00361               typename BasicImageView<PixelType>::const_traverser, Accessor>
00362 srcImageRange(BasicImageView<PixelType> const & img, Accessor a)
00363 {
00364     return triple<typename BasicImageView<PixelType>::const_traverser, 
00365                   typename BasicImageView<PixelType>::const_traverser, 
00366           Accessor>(img.upperLeft(),
00367                     img.lowerRight(),
00368                     a);
00369 }
00370 
00371 template <class PixelType, class Accessor>
00372 inline pair<typename BasicImageView<PixelType>::const_traverser, Accessor>
00373 srcImage(BasicImageView<PixelType> const & img, Accessor a)
00374 {
00375     return pair<typename BasicImageView<PixelType>::const_traverser, 
00376                 Accessor>(img.upperLeft(), a);
00377 }
00378 
00379 template <class PixelType, class Accessor>
00380 inline triple<typename BasicImageView<PixelType>::traverser, 
00381               typename BasicImageView<PixelType>::traverser, Accessor>
00382 destImageRange(BasicImageView<PixelType> & img, Accessor a)
00383 {
00384     return triple<typename BasicImageView<PixelType>::traverser, 
00385                   typename BasicImageView<PixelType>::traverser, 
00386           Accessor>(img.upperLeft(),
00387                     img.lowerRight(),
00388                     a);
00389 }
00390 
00391 template <class PixelType, class Accessor>
00392 inline pair<typename BasicImageView<PixelType>::traverser, Accessor>
00393 destImage(BasicImageView<PixelType> & img, Accessor a)
00394 {
00395     return pair<typename BasicImageView<PixelType>::traverser, 
00396                 Accessor>(img.upperLeft(), a);
00397 }
00398 
00399 template <class PixelType, class Accessor>
00400 inline pair<typename BasicImageView<PixelType>::const_traverser, Accessor>
00401 maskImage(BasicImageView<PixelType> const & img, Accessor a)
00402 {
00403     return pair<typename BasicImageView<PixelType>::const_traverser, 
00404                 Accessor>(img.upperLeft(), a);
00405 }
00406 
00407 /****************************************************************/
00408 
00409 template <class PixelType>
00410 inline triple<typename BasicImageView<PixelType>::const_traverser, 
00411               typename BasicImageView<PixelType>::const_traverser, 
00412               typename BasicImageView<PixelType>::ConstAccessor>
00413 srcImageRange(BasicImageView<PixelType> const & img)
00414 {
00415     return triple<typename BasicImageView<PixelType>::const_traverser, 
00416                   typename BasicImageView<PixelType>::const_traverser, 
00417                   typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(),
00418                                                                      img.lowerRight(),
00419                                                                      img.accessor());
00420 }
00421 
00422 template <class PixelType>
00423 inline pair< typename BasicImageView<PixelType>::const_traverser, 
00424              typename BasicImageView<PixelType>::ConstAccessor>
00425 srcImage(BasicImageView<PixelType> const & img)
00426 {
00427     return pair<typename BasicImageView<PixelType>::const_traverser, 
00428                 typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(), 
00429                                                                    img.accessor());
00430 }
00431 
00432 template <class PixelType>
00433 inline triple< typename BasicImageView<PixelType>::traverser, 
00434                typename BasicImageView<PixelType>::traverser, 
00435                typename BasicImageView<PixelType>::Accessor>
00436 destImageRange(BasicImageView<PixelType> & img)
00437 {
00438     return triple<typename BasicImageView<PixelType>::traverser, 
00439                   typename BasicImageView<PixelType>::traverser, 
00440                   typename BasicImageView<PixelType>::Accessor>(img.upperLeft(),
00441                                                                 img.lowerRight(),
00442                                                                 img.accessor());
00443 }
00444 
00445 template <class PixelType>
00446 inline pair< typename BasicImageView<PixelType>::traverser, 
00447              typename BasicImageView<PixelType>::Accessor>
00448 destImage(BasicImageView<PixelType> & img)
00449 {
00450     return pair<typename BasicImageView<PixelType>::traverser, 
00451                 typename BasicImageView<PixelType>::Accessor>(img.upperLeft(), 
00452                                                               img.accessor());
00453 }
00454 
00455 template <class PixelType>
00456 inline pair< typename BasicImageView<PixelType>::const_traverser, 
00457              typename BasicImageView<PixelType>::ConstAccessor>
00458 maskImage(BasicImageView<PixelType> const & img)
00459 {
00460     return pair<typename BasicImageView<PixelType>::const_traverser, 
00461                 typename BasicImageView<PixelType>::ConstAccessor>(img.upperLeft(), 
00462                                                                    img.accessor());
00463 }
00464 
00465 } // namespace vigra
00466 
00467 #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.2.0 (7 Aug 2003)