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