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

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