region.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 3 -*-
00002  *
00003  * Copyright (c), GREYC.
00004  * All rights reserved
00005  *
00006  * You may use this file under the terms of the BSD license as follows:
00007  *
00008  * "Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are
00010  * met:
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above copyright
00014  *     notice, this list of conditions and the following disclaimer in
00015  *     the documentation and/or other materials provided with the
00016  *     distribution.
00017  *   * Neither the name of the GREYC, nor the name of its
00018  *     contributors may be used to endorse or promote products
00019  *     derived from this software without specific prior written
00020  *     permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00033  *
00034  *
00035  * For more information, refer to:
00036  * https://clouard.users.greyc.fr/Pandore
00037  */
00038 
00044 #ifndef __PREGIONH__
00045 #define __PREGIONH__
00046 
00047 namespace pandore {
00048 
00049    // Definitions of region maps types.
00050    class Reg1d;
00051    class Reg2d;
00052    class Reg3d;
00053 
00054    /* @brief Trait that returns the name of the Pandore type TYPE.
00055     *
00056     * TypeName is a trait that retunrs the name
00057     * of the Pandore type T.
00058     */
00059    template<>
00060    struct TypeName< Reg1d > {
00065          static std::string Name() {
00066             return "Reg1d";
00067          }
00068    };
00069    
00070    /* @brief Trait that returns the name of the Pandore type TYPE.
00071     *
00072     * TypeName is a trait that retunrs the name
00073     * of the Pandore type T.
00074     */
00075    template<>
00076    struct TypeName< Reg2d > {
00081          static std::string Name() {
00082             return "Reg2d";
00083          }
00084    };
00085    
00086    
00087    /* @brief Trait that returns the name of the Pandore type TYPE.
00088     *
00089     * TypeName is a trait that retunrs the name
00090     * of the Pandore type T.
00091     */
00092    template<>
00093    struct TypeName< Reg3d > {
00098          static std::string Name() {
00099             return "Reg3d";
00100          }
00101    };
00102    
00120    class Reg1d: public Img1d<Long> {
00121       protected:
00123          Long nlabels;
00124 
00125       public:
00127          typedef Ulong ValueType;
00128 
00133          Typobj Type() const {
00134             return Po_Reg1d;
00135          }
00136 
00141          std::string Name() const {
00142             return TypeName< Reg1d >::Name();
00143          }
00144 
00149          Ulong Labels() const {
00150             return nlabels;
00151          }
00152 
00158          Ulong Labels( Ulong n ) {
00159             return nlabels = n;
00160          }
00161 
00166          PobjectProps Props() const {
00167             return PobjectProps(nbands, ncol, 0, 0, (PColorSpace)0, nlabels, nlabels + 1);
00168          }
00169    
00173          Reg1d(): Img1d<Long>(), nlabels(0) {}
00174 
00181          Reg1d( Long w ): Img1d<Long>(w), nlabels(0) {}
00182 
00189          Reg1d( const Dimension1d& d ): Img1d<Long>(d), nlabels(0) {}
00190 
00197          Reg1d( const PobjectProps& p ): Img1d<Long>(p.ncol), nlabels(p.nlabels) {}
00198    
00204          void New( Long w ) { Img1d<Long>::New(w); nlabels = 0L; }
00205 
00211          void New( const Dimension1d& d ) { New(d.w); }
00212 
00218          void New( const PobjectProps& p ) {
00219             New(p.ncol);
00220             nlabels = p.nlabels;
00221          }
00222 
00226          void Delete () { Img1d<Long>::Delete(); nlabels = 0L; }
00227 
00232          Pobject* Clone( ) const {
00233             Reg1d *tmp = new Reg1d(ncol);
00234             *tmp = *this;
00235             return tmp;
00236          }
00237 
00242          ValueType* Vector() const {
00243             return (ValueType*)Img1d<Long>::Vector();
00244          }
00245 
00251          ValueType& operator[]( Long col ) {
00252             return (ValueType&)Imx1d<Long>::operator[](0)[col];
00253          }
00254 
00260          const ValueType& operator[]( Long col ) const {
00261             return reinterpret_cast<const ValueType&>(Imx1d<Long>::operator[](0)[col]);
00262          }
00263    
00269          ValueType& operator()( Long col ) {
00270             return (ValueType&)Imx1d<Long>::operator()(0, col);
00271          }
00272    
00278          const ValueType& operator()( Long col ) const {
00279             return reinterpret_cast<const ValueType&>(Imx1d<Long>::operator()(0, col));
00280          }
00281 
00287          ValueType& operator[]( const Point1d& p ) {
00288             return (ValueType&)Imx1d<Long>::operator[](0)[p.x];
00289          }
00290    
00296          const ValueType& operator[]( const Point1d& p ) const {
00297             return reinterpret_cast<const ValueType&>(Imx1d<Long>::operator[](0)[p.x]);
00298          }
00299    
00306          Reg1d& operator=(const Reg1d::ValueType val) {
00307             Img1d<Long>::operator=(val);
00308             nlabels = (val > 0) ? val : 0;
00309             return *this;
00310          }
00311 
00317          Reg1d& operator=(const Reg1d &rgs) {
00318             nlabels = rgs.nlabels;
00319             memcpy(Vector(), rgs.Vector(), ncol * sizeof(Reg1d::ValueType));
00320             return *this;
00321          }
00322    
00329          Errc LoadAttributes( FILE *file );
00330 
00336          Errc SaveAttributes( FILE *file ) const;
00337    
00343          Errc LoadData( FILE *file );
00344    
00350          Errc SaveData( FILE *file ) const;
00351    
00360          Pobject *Mask( const Pobject *mask );
00361    
00371          Pobject *UnMask( const Pobject *mask, const Pobject *reference );
00372    
00379          Reg1d( const Reg1d &rgs ): Img1d<Long>() {
00380             New(rgs.Props());
00381             *this = rgs;
00382          } 
00383    };
00384 
00402    class Reg2d: public Img2d<Long> {
00403    public:
00408          class Band1dr {
00409             public:
00410                // 1D data accessors.
00417                explicit Band1dr( const Imx3d<Long> * const region, Long *offset ) : _region(region), _offset(offset) {}
00418                Ulong &operator[]( Long x ) {
00419                   return (Ulong&)_offset[x];
00420                }
00421                const Ulong &operator[]( Long x ) const {
00422                   return (Ulong&)_offset[x];
00423                }
00424             private:
00425                const Imx3d<Long> *_region;
00426                Long *_offset;
00427          };
00428          
00429 
00431          Ulong nlabels;
00432 
00433       public:
00435          typedef Ulong ValueType;
00436 
00441          Typobj Type() const {
00442             return Po_Reg2d;
00443          }
00444 
00449          std::string Name() const {
00450             return TypeName< Reg2d >::Name();
00451          }
00452 
00457          Ulong Labels() const {
00458             return nlabels;
00459          }
00460 
00466          Ulong Labels( Ulong n ) {
00467             return nlabels = n;
00468          }
00469 
00474          PobjectProps Props() const {
00475             return PobjectProps(nbands, ncol, nrow, 0, (PColorSpace)0, nlabels, nlabels + 1);
00476          }
00477    
00481          Reg2d(): Img2d<Long>(), nlabels(0) {}
00482 
00490          Reg2d(Long h, Long w): Img2d<Long>(h, w), nlabels(0) {}
00491 
00498          Reg2d(const Dimension2d& d): Img2d<Long>(d), nlabels(0) {}
00499 
00506       Reg2d(const PobjectProps& p): Img2d<Long>(p.nrow, p.ncol), nlabels(p.nlabels) { }
00507 
00514          void New( Long h, Long w ) { Img2d<Long>::New(h, w); nlabels = 0L; }
00515 
00521          void New( const Dimension2d &d ) { New(d.h, d.w); }
00522 
00528          void New( const PobjectProps &p, Ulong * data = 0 ) {
00529             Img2d<Long>::New(p.nrow, p.ncol, (Long*)data);
00530             nlabels = p.nlabels;
00531          }
00532          
00536          void Delete() { Img2d<Long>::Delete(); nlabels = 0L; }
00537 
00542          Pobject* Clone( ) const {
00543             Reg2d *tmp = new Reg2d(nrow, ncol);
00544             *tmp = *this;
00545             return tmp;
00546          }
00547 
00552          ValueType* Vector() const {
00553             return (ValueType*)Img2d<Long>::Vector();
00554          }
00555       
00561          Band1dr operator[]( Long row ) {
00562             return Band1dr(this, &_data[ncol*row]);
00563          }
00564 
00570          const Band1dr operator[]( Long row ) const {
00571             return Band1dr(this, &_data[ncol*row]);
00572          }
00573 
00580          ValueType& operator()( Long y, Long x ) {
00581             return (ValueType&) Imx2d<Long>::operator()(0, y, x);
00582          }
00583    
00590          const ValueType& operator()( Long y, Long x ) const {
00591             return reinterpret_cast<const ValueType&>(Imx2d<Long>::operator()(0, y, x));
00592          }
00593 
00599          ValueType& operator[]( const Point2d& p ) {
00600             return (ValueType&) Imx2d<Long>::operator()(0, p.y, p.x);
00601          }
00602    
00608          const ValueType& operator[]( const Point2d& p ) const {
00609             return reinterpret_cast<const ValueType&>(Imx2d<Long>::operator()(0, p.y, p.x));
00610          }
00611    
00618          Reg2d& operator=( const Reg2d::ValueType val ) {
00619             Img2d<Long>::operator=(val);
00620             nlabels = (val > 0) ? val : 0;
00621             return *this;
00622          }
00623 
00629          Reg2d& operator=( const Reg2d &rgs ) {
00630             nlabels = rgs.nlabels;
00631             memcpy(Vector(), rgs.Vector(), nrow * ncol * sizeof(Reg2d::ValueType));
00632             return *this;
00633          }
00634    
00641          Errc LoadAttributes( FILE *file );
00642 
00648          Errc SaveAttributes( FILE *file ) const;
00654          Errc LoadData( FILE *file );
00655 
00662          Errc SaveData( FILE *file ) const;
00663 
00672          Pobject* Mask( const Pobject *mask );
00673 
00683          Pobject* UnMask( const Pobject *mask, const Pobject *reference );
00684 
00691          Reg2d( const Reg2d &rgs ): Img2d<Long>() {
00692             New(rgs.Props());
00693             *this = rgs;
00694          }
00695    };
00696 
00714    class Reg3d: public Img3d<Long> {
00720          class Band2dr {
00721             public:
00722                // 2D data accessors.
00729                explicit Band2dr( const Imx3d<Long> *region, Long *offset ) : _region(region), _offset(offset) {}
00730                explicit Band2dr(): _region(0), _offset(0) {};
00731                Reg2d::Band1dr operator[]( Long y ) {
00732                   return Reg2d::Band1dr(_region, &_offset[_region->Width() * y]);
00733                }
00734                const Reg2d::Band1dr operator[]( Long y ) const {
00735                   return Reg2d::Band1dr(_region, &_offset[_region->Width() * y]);
00736                }
00737          private:
00738                const Imx3d<Long> *_region;
00739                Long *_offset;
00740          };
00741       protected:
00743          Ulong nlabels;
00744 
00745       public:
00747          typedef Ulong ValueType;
00748 
00753          Typobj Type() const {
00754             return Po_Reg3d;
00755          }
00756 
00761          std::string Name() const {
00762             return TypeName< Reg3d >::Name();
00763          }
00764 
00769          Ulong Labels() const {
00770             return nlabels;
00771          }
00772 
00778          Ulong Labels( Ulong n ) {
00779             return nlabels = n;
00780          }
00781 
00786          PobjectProps Props() const {
00787             return PobjectProps(nbands, ncol, nrow, ndep, (PColorSpace)0, nlabels, nlabels + 1);
00788          }
00789 
00793          Reg3d(): Img3d<Long>(), nlabels(0) {}
00794 
00803          Reg3d( Long d, Long h, Long w ): Img3d<Long>(d, h, w), nlabels(0) {}
00804 
00811          Reg3d( const Dimension3d& d ): Img3d<Long>(d), nlabels(0) {}
00812 
00819          Reg3d(const PobjectProps& p): Img3d<Long>(p.ndep, p.nrow, p.ncol), nlabels(p.nlabels) {}
00820 
00828          void New( Long d, Long h, Long w ) { Img3d<Long>::New(d, h, w); nlabels = 0L; }
00829 
00835          void New( const Dimension3d& d ) { New(d.d, d.h, d.w); }
00836 
00842          void New( const PobjectProps& p, Long *data = 0 ) {
00843             Img3d<Long>::New(p.ndep, p.nrow, p.ncol, (Long*)data);
00844             nlabels = p.nlabels;
00845          }
00846 
00850          void Delete () { Img3d<Long>::Delete(); nlabels = 0L; }
00851 
00856          Pobject* Clone( ) const {
00857             Reg3d *tmp = new Reg3d(ndep, nrow, ncol);
00858             *tmp = *this;
00859             return tmp;
00860          }
00861 
00866          ValueType* Vector() const {
00867             return (ValueType*)Img3d<Long>::Vector(); 
00868             
00869          }
00870 
00877          Reg3d& operator=( const Reg3d::ValueType val ) {
00878             Img3d<Long>::operator=(val);
00879             nlabels = (val > 0) ? val : 0;
00880             return *this;
00881          }
00882 
00888          Reg3d& operator=( const Reg3d &rgs ) {
00889             nlabels = rgs.nlabels;
00890             memcpy(Vector(), rgs.Vector(), ndep * nrow * ncol * sizeof(Reg3d::ValueType));
00891             return *this;
00892          }
00893 
00899          Band2dr operator[]( Long dep ) {
00900             return Band2dr(this, &_data[matrixSize * dep]);
00901          }
00902 
00908          const Band2dr operator[]( Long dep ) const {
00909             return Band2dr(this, &_data[matrixSize * dep]);
00910          }
00911 
00919          ValueType& operator()( Long z, Long y, Long x ) {
00920             return (ValueType&) Imx3d<Long>::operator()(0, z, y, x);
00921          }
00922 
00931          const ValueType& operator()( Long z, Long y, Long x ) const {
00932             return reinterpret_cast<const ValueType&>(Imx3d<Long>::operator()(0, z, y, x));
00933          }
00934 
00940          ValueType& operator[]( const Point3d& p ) {
00941             return (ValueType&) Imx3d<Long>::operator()(0, p.z, p.y, p.x);
00942          }
00943 
00949          const ValueType& operator[]( const Point3d& p ) const {
00950             return reinterpret_cast<const ValueType&>(Imx3d<Long>::operator()(0, p.z, p.y, p.x));
00951          }
00952 
00959          Errc LoadAttributes( FILE *file );
00960 
00966          Errc SaveAttributes( FILE *file ) const;
00967 
00973          Errc LoadData( FILE *file );
00974 
00981          Errc SaveData( FILE *file ) const;
00982 
00990          Pobject* Mask( const Pobject *mask );
00991 
01001          Pobject* UnMask( const Pobject *mask, const Pobject *reference );
01002 
01009          Reg3d( const Reg3d &rgs ): Img3d<Long>() {
01010             New(rgs.Props());
01011             *this = rgs;
01012          }
01013    };
01014    
01015 } //End of pandore:: namespace
01016 #endif // __PREGIONH__

The Pantheon project
Image Team GREYC Laboratory
UMR CNRS 6072 - ENSICAEN - University of Caen, France
This page was last modified on 19 June 2015