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