Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GenMask2d.TCC

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------+
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 /**
00029  * @file   GenMask2d.TCC
00030  * @brief  Implementation of function members of class qgar::GenMask2d.
00031  *
00032  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00033  * @date   January 27, 2005  22:02
00034  * @since  Qgar 2.2
00035  */
00036 
00037 
00038 
00039 // QGAR
00040 #include <qgarlib/array.H>
00041 
00042 
00043 
00044 namespace qgar
00045 {
00046 
00047 
00048 // -------------------------------------------------------------------
00049 // C O N S T R U C T O R S
00050 // -------------------------------------------------------------------
00051 
00052 
00053 // DEFAULT CONSTRUCTOR
00054 
00055 template <class T>
00056 GenMask2d<T>::GenMask2d()
00057 
00058   : _pRefCnt(new int(0)),
00059     _width(0),
00060     _height(0),
00061     _pCoeffMap(0)
00062 
00063 {
00064   // VOID
00065 }
00066 
00067 
00068 // COPY CONSTRUCTOR
00069 
00070 template <class T>
00071 GenMask2d<T>::GenMask2d(const GenMask2d<T>& aMask)
00072 
00073   : _pRefCnt(new int(0)),
00074     _width(aMask._width),
00075     _height(aMask._height)
00076 
00077 {
00078   int size = _width * _height;
00079   _pCoeffMap = new T[size];
00080 
00081   memcpy(_pCoeffMap, aMask.pCoeffMap(), size * sizeof(T));
00082 }
00083 
00084 
00085 // INITIALIZE WITH GIVEN WIDTH, HEIGHT AND VALUE
00086 
00087 template <class T>
00088 GenMask2d<T>::GenMask2d(unsigned int aWidth,
00089                         unsigned int aHeight,
00090                         T aValue)
00091 
00092   : _pRefCnt(new int(0)),
00093     _width(aWidth),
00094     _height(aHeight)
00095 
00096 {
00097   int size = _width * _height;
00098   _pCoeffMap = new T[size];
00099   qgFill(_pCoeffMap, size, aValue);
00100 }
00101 
00102 
00103 // INITIALIZE FROM A GIVEN ARRAY OF VALUES
00104 
00105 template <class T>
00106 GenMask2d<T>::GenMask2d(unsigned int aWidth,
00107                         unsigned int aHeight,
00108                         const T * const valArray)
00109 
00110   : _pRefCnt(new int(0)),
00111     _width(aWidth),
00112     _height(aHeight)
00113 
00114 {
00115   int size = _width * _height;
00116   _pCoeffMap = new T[size];
00117   memcpy(_pCoeffMap, valArray, size * sizeof(T));
00118 }
00119 
00120 
00121 // SHALLOW COPY CONSTRUCTOR
00122 
00123 template <class T>
00124 GenMask2d<T>::GenMask2d(const GenMask2d& rhs, int)
00125 
00126   : _pRefCnt(rhs._pRefCnt),
00127     _width(rhs._width),
00128     _height(rhs._height),
00129     _pCoeffMap(rhs._pCoeffMap)
00130 
00131 {
00132   (*_pRefCnt)++;
00133   
00134 }
00135 
00136 
00137 // -------------------------------------------------------------------
00138 // D E S T R U C T O R 
00139 // -------------------------------------------------------------------
00140 
00141 
00142 template <class T>
00143 GenMask2d<T>::~GenMask2d()
00144 {
00145   if (*_pRefCnt == 0)
00146     {
00147       // The space allocated to the coefficient map
00148       // is not shared with another object
00149       delete [] _pCoeffMap;
00150       delete _pRefCnt;
00151     }
00152   else
00153     {
00154       // The space allocated to the coefficient map is shared
00155       // with at least one other object: Just decrement
00156       // the reference counter
00157       (*_pRefCnt)--;
00158     }
00159 }
00160 
00161 
00162 // -------------------------------------------------------------------
00163 // A C C E S S   T O   M A S K   C H A R A C T E R I S T I C S
00164 // -------------------------------------------------------------------
00165 
00166 
00167 // GET MASK WIDTH
00168 
00169 template <class T>
00170 inline int
00171 GenMask2d<T>::width() const
00172 {
00173   return _width;
00174 }
00175 
00176 
00177 // GET MASK HEIGHT
00178 
00179 template <class T>
00180 inline int
00181 GenMask2d<T>::height() const
00182 {
00183   return _height;
00184 }
00185 
00186 
00187 // -------------------------------------------------------------------
00188 // A C C E S S   T O   C O E F F I C I E N T   V A L U E S
00189 // -------------------------------------------------------------------
00190 
00191 
00192 // GET A COEFFICIENT VALUE
00193 
00194 template <class T>
00195 T
00196 GenMask2d<T>::coeff(int aX, int aY) const
00197 {
00198   return *(_pCoeffMap + (aY * _width) + aX);
00199 }
00200 
00201 
00202 // -------------------------------------------------------------------
00203 // ACCESS TO DIRECT TRANSFORMATIONS OF THE COEFFICIENT MAP
00204 // -------------------------------------------------------------------
00205 
00206 
00207 // GET THE POINTER TO THE COEFFICIENT MAP
00208 
00209 template <class T>
00210 inline T*
00211 GenMask2d<T>::pCoeffMap() const
00212 {
00213   return _pCoeffMap;
00214 }
00215 
00216 
00217 // -------------------------------------------------------------------
00218 // T R A N S F O R M A T I O N
00219 // -------------------------------------------------------------------
00220 
00221 
00222 // SET COEFFICIENT AT GIVEN POSITION
00223 
00224 template <class T>
00225 void
00226 GenMask2d<T>::setCoeff(int aX, int aY, T aCoeff)
00227 {
00228   *(_pCoeffMap + (aY * _width) + aX) = aCoeff;
00229 }
00230 
00231 
00232 // -------------------------------------------------------------------
00233 // C O P Y
00234 // -------------------------------------------------------------------
00235 
00236 
00237 // SHALLOW COPY: THE COEFFICIENT MAP OF THE SOURCE MASK IS NOT DUPLICATED
00238 // When the copy is completed, the pixel map of the destination
00239 // mask is the same memory space as the pixel map of the source mask
00240 
00241 template <class T>
00242 GenMask2d<T> GenMask2d<T>::shallowCopy()
00243 {
00244   return GenMask2d(*this, 0);
00245 }
00246 
00247 
00248 // AUXILIARY: STORE A DEEP COPY OF A GIVEN MASK INTO THE CURRENT MASK
00249 
00250 template <class T>
00251 void
00252 GenMask2d<T>::PRIVATEdeepCopy(const GenMask2d<T>& aMask)
00253 {
00254   // Are source and destination the same object?
00255   if (this != &aMask)
00256     {
00257       _pRefCnt = new int(0);
00258       _width   = aMask._width;
00259       _height  = aMask._height;
00260 
00261       // Free space allocated to the coefficent map of the destination mask
00262       delete [] _pCoeffMap;
00263  
00264      // Allocate space to the coefficent map
00265       int size = _width * _height;
00266       _pCoeffMap = new T[size];
00267 
00268       // Copy the coefficients
00269       memcpy(_pCoeffMap, aMask.pCoeffMap(), size * sizeof(T));
00270     }
00271 }
00272 
00273 
00274 // -------------------------------------------------------------------
00275 // O P E R A T O R S
00276 // -------------------------------------------------------------------
00277 
00278 
00279 // ASSIGNMENT
00280 
00281 template <class T>
00282 GenMask2d<T>&
00283 GenMask2d<T>::operator=(const GenMask2d<T>& aMask)
00284 {
00285   PRIVATEdeepCopy(aMask);
00286   return *this;
00287 }
00288 
00289 
00290 // -------------------------------------------------------------------
00291 
00292 
00293 } // namespace qgar