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

details vigra/imagecontainer.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 IMAGECONTAINER_HXX
00024 #define IMAGECONTAINER_HXX
00025 
00026 #include "vigra/utilities.hxx"
00027 #include <vector>
00028 
00029 namespace vigra {
00030 
00031 /** \addtogroup ImageContainers Image Containers
00032     Classes to manage multiple images (ImageArray..)
00033 */
00034 //@{
00035 
00036 /********************************************************/
00037 /*                                                      */
00038 /*                      ImageArray                      */
00039 /*                                                      */
00040 /********************************************************/
00041 
00042 /** \brief Fundamental class template for arrays of equal-sized images.
00043 
00044     An ImageArray manages an array of images of the type given as
00045     template parameter. Use it like a std::vector<ImageType>, it has
00046     the same interface, only operator< is missing from ImageArray. It
00047     offers additional functions for resizing the images and querying
00048     their common size. See \ref imageSize() for additional notes.
00049 
00050     <b>\#include</b> "<a href="imagecontainer_8hxx-source.html">vigra/imagecontainer.hxx</a>"
00051 
00052     Namespace: vigra
00053 */
00054 template <class ImageType>
00055 class ImageArray
00056 {
00057     Size2D imageSize_;
00058 
00059 protected:
00060     std::vector<ImageType> images_;
00061 
00062 public:
00063         /** the type of the contained values/images
00064          */
00065     typedef ImageType    value_type;
00066 
00067     typedef typename std::vector<ImageType>::iterator iterator;
00068     typedef typename std::vector<ImageType>::const_iterator const_iterator;
00069     typedef typename std::vector<ImageType>::reverse_iterator reverse_iterator;
00070     typedef typename std::vector<ImageType>::const_reverse_iterator const_reverse_iterator;
00071     typedef typename std::vector<ImageType>::reference reference;
00072     typedef typename std::vector<ImageType>::const_reference const_reference;
00073 #if !defined(_MSC_VER) || _MSC_VER >= 1300
00074     typedef typename std::vector<ImageType>::pointer pointer;
00075 #endif
00076     typedef typename std::vector<ImageType>::difference_type difference_type;
00077     typedef typename std::vector<ImageType>::size_type size_type;
00078 
00079         /** init an array of numImages equal-sized images
00080          */
00081     ImageArray(unsigned int numImages, const Diff2D &imageSize)
00082         : imageSize_(imageSize),
00083           images_(numImages)
00084     {
00085         for(unsigned int i=0; i<numImages; i++)
00086             images_[i].resize(imageSize);
00087     }
00088 
00089         /** Init an array of numImages equal-sized images, the size
00090             depends on ImageType's default constructor.
00091          */
00092     ImageArray(unsigned int numImages= 0)
00093         : images_(numImages)
00094     {
00095         imageSize_= empty()? Size2D(0, 0) : front().size();
00096     }
00097 
00098         /** fill constructor: Init an array with numImages copies of
00099             the given image. (STL-Sequence interface)
00100          */
00101     ImageArray(unsigned int numImages, const ImageType &image)
00102         : imageSize_(image.size()),
00103           images_(numImages, image)
00104     {
00105     }
00106     
00107         /** range constructor: Construct an array containing copies of
00108             the images in [begin, end). Those images must all have the
00109             same size, see \ref imageSize(). (STL-Sequence interface)
00110          */
00111     template<class InputIterator>
00112     ImageArray(InputIterator begin, InputIterator end)
00113         : imageSize_(begin!=end? (*begin).size() : Size2D(0,0)),
00114           images_(begin, end)
00115     {
00116     }
00117 
00118     virtual ~ImageArray() {}
00119 
00120         /** Operator for a vector-like access to the contained images
00121             (STL-Vector interface)
00122          */
00123     reference operator [](size_type index)
00124     {
00125         return images_[index];
00126     }
00127 
00128         /** Operator for a vector-like access to the contained images
00129             (STL-Vector interface)
00130          */
00131     const_reference operator [](size_type index) const
00132     {
00133         return images_[index];
00134     }
00135 
00136         /** Returns an iterator pointing to the first image
00137             (STL-Container interface)
00138          */
00139     iterator begin()
00140     {
00141         return images_.begin();
00142     }
00143 
00144         /** Returns an iterator pointing to the first image
00145             (STL-Container interface)
00146          */
00147     const_iterator begin() const
00148     {
00149         return images_.begin();
00150     }
00151 
00152         /** Returns an iterator pointing behind the last image
00153             (STL-Container interface)
00154          */
00155     iterator end()
00156     {
00157         return images_.end();
00158     }
00159 
00160         /** Returns an iterator pointing behind the last image
00161             (STL-Container interface)
00162          */
00163     const_iterator end() const
00164     {
00165         return images_.end();
00166     }
00167 
00168         /** Returns a reverse_iterator pointing to the first image of
00169             the reversed view of this array (STL-Reversable Container
00170             interface)
00171          */
00172     reverse_iterator rbegin()
00173     {
00174         return images_.rbegin();
00175     }
00176 
00177         /** Returns a reverse_iterator pointing to the first image of
00178             the reversed view of this array (STL-Reversable Container
00179             interface)
00180          */
00181     const_reverse_iterator rbegin() const
00182     {
00183         return images_.rbegin();
00184     }
00185 
00186         /** Returns a reverse_iterator pointing behind the last image
00187             of the reversed view of this array (STL-Reversable
00188             Container interface)
00189          */
00190     reverse_iterator rend()
00191     {
00192         return images_.rend();
00193     }
00194 
00195         /** Returns a reverse_iterator pointing behind the last image
00196             of the reversed view of this array (STL-Reversable
00197             Container interface)
00198          */
00199     const_reverse_iterator rend() const
00200     {
00201         return images_.rend();
00202     }
00203 
00204         /** Query size of this ImageArray, that is: the number of
00205             images. (STL-Container interface)
00206         */
00207     size_type size() const
00208     {
00209         return images_.size();
00210     }
00211 
00212         /** Query maximum size of this ImageArray, that is: the
00213             max. parameter you may pass to resize(). (STL-Container
00214             interface)
00215         */
00216     size_type max_size() const
00217     {
00218         return images_.max_size();
00219     }
00220 
00221         /** Returns true if and only if there are no contained
00222             images. (STL-Container interface)
00223         */
00224     bool empty()
00225     {
00226         return images_.empty();
00227     }
00228 
00229         /** Returns true if and only if both ImageArrays have exactly
00230             the same contents and all images did compare equal with the
00231             corresponding image in the other ImageArray. (STL-Forward
00232             Container interface)
00233          */
00234     bool operator ==(const ImageArray<ImageType> &other)
00235     {
00236         return (imageSize() == other.imageSize())
00237                 && (images_ == other.images_);
00238     }
00239 
00240         /** Insert image at/before pos. (STL-Sequence interface)
00241          */
00242     iterator insert(iterator pos, const_reference image)
00243     {
00244         return images_.insert(pos, image);
00245     }
00246 
00247         /** Insert count copies of image at/before pos. (STL-Sequence
00248             interface)
00249          */
00250     void insert (iterator pos, size_type count, const_reference image);
00251 
00252         /** Insert copies of images from [begin, end) at/before
00253             pos. (STL-Sequence interface)
00254          */
00255     template<class InputIterator>
00256     void insert(iterator pos, InputIterator begin, InputIterator end)
00257     {
00258         images_.insert(pos, begin, end);
00259     }
00260 
00261         /** Removes the image at pos from this array. (STL-Sequence
00262             interface)
00263          */
00264     iterator erase(iterator pos)
00265     {
00266         return images_.erase(pos);
00267     }
00268 
00269         /** Removes the images from [begin, end) from this
00270             array. (STL-Sequence interface)
00271          */
00272     iterator erase(iterator begin, iterator end)
00273     {
00274         return images_.erase(begin, end);
00275     }
00276 
00277         /** Empty this array. (STL-Sequence interface)
00278          */
00279     void clear()
00280     {
00281         images_.clear();
00282     }
00283 
00284         /** Resize this ImageArray, throwing the last images away if
00285             you make the array smaller or appending new images of the
00286             right size at the end of the array if you make it
00287             larger. (STL-Sequence interface)
00288         */
00289     void resize(size_type newSize)
00290     {
00291         if (newSize != size())
00292         {
00293             size_type oldSize= size();
00294             images_.resize(newSize);
00295             for (size_type i= oldSize; i<newSize; i++)
00296                 images_[i].resize(imageSize());
00297         }
00298     }
00299 
00300         /** Resize this ImageArray, throwing the last images away if
00301             you make the array smaller or appending new copies of image
00302             at the end of the array if you make it larger.
00303             precondition: <tt>image.size() == imageSize()</tt>
00304             (STL-Sequence interface)
00305         */
00306     void resize(size_type newSize, ImageType &image)
00307     {
00308         if (newSize != size())
00309         {
00310             vigra_precondition(image.size() == imageSize(),
00311                                "trying to append images of wrong size to ImageArray with resize()");
00312             images_.resize(newSize, image);
00313         }
00314     }
00315 
00316         /** return the first image. (STL-Sequence interface)
00317          */
00318     reference front()
00319     {
00320         return images_.front();
00321     }
00322 
00323         /** return the first image. (STL-Sequence interface)
00324          */
00325     const_reference front() const
00326     {
00327         return images_.front();
00328     }
00329 
00330         /** return the last image. (STL-Vector interface)
00331          */
00332     reference back()
00333     {
00334         return images_.back();
00335     }
00336 
00337         /** return the last image. (STL-Vector interface)
00338          */
00339     const_reference back() const
00340     {
00341         return images_.back();
00342     }
00343 
00344         /** append image to array (STL-Back Insertion Sequence interface)
00345          */
00346     void push_back(const_reference image)
00347     {
00348         images_.push_back(image);
00349     }
00350 
00351         /** remove last image from array (STL-Back Insertion Sequence interface)
00352          */
00353     void pop_back()
00354     {
00355         images_.pop_back();
00356     }
00357 
00358         /** swap contents of this array with the contents of other
00359             (STL-Container interface)
00360          */
00361     void swap(const_reference other)
00362     {
00363         Size2D oldImageSize = imageSize_;
00364         images_.swap(other.images_);
00365         imageSize_ = other.imageSize_;
00366         other.imageSize_ = oldImageSize;
00367     }
00368 
00369         /** number of image objects for which memory has been allocated
00370             (STL-Vector interface)
00371         */
00372     size_type capacity() const
00373     {
00374         return images_.capacity();
00375     }
00376 
00377         /** increase capacity(). (STL-Vector interface)
00378          */
00379     void reserve(size_type n)
00380     {
00381         images_.reserve(n);
00382     }
00383 
00384         /** Query the size of the contained images. ImageArray will
00385             maintain an array of equal-sized images of this
00386             size. However, <em>do not resize the contained images
00387             manually</em>. ImageArray currently has no way to detect or
00388             prevent this.
00389          */
00390     Size2D imageSize() const
00391         { return imageSize_; }
00392 
00393         /** Resize all images to a common new size (No-op if
00394             <tt>newSize == imageSize()</tt>). See \ref imageSize() for
00395             an important note about resizing the images.
00396         */
00397     virtual void resizeImages(const Diff2D &newSize)
00398     {
00399         if (newSize!=imageSize())
00400         {
00401             for(unsigned int i=0; i<size(); i++)
00402                 images_[i].resize(newSize);
00403             imageSize_= newSize;
00404         }
00405     }
00406 
00407         /** Resize all images to a common new size (No-op if
00408             <tt>newSize == imageSize()</tt>). See \ref imageSize() for
00409             an important note about resizing the images.
00410             
00411             (Convenience function, same as calling
00412             <tt>resizeImages(Diff2D(width, height));</tt>.)
00413         */
00414     void resizeImages(int width, int height)
00415     {
00416         resizeImages(Size2D(width, height));
00417     }
00418 };
00419 
00420 //@}
00421 
00422 } // namespace vigra
00423 
00424 #endif // IMAGECONTAINER_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)