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

GenMask1d.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   GenMask1d.TCC
00030  * @brief  Implementation of function members of class qgar::GenMask1d.
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  21:55
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 GenMask1d<T>::GenMask1d()
00057 
00058   : _pRefCnt(new int(0)),
00059     _width(0),
00060     _pCoeffMap(0)
00061 
00062 {
00063   // VOID
00064 }
00065 
00066 
00067 // COPY CONSTRUCTOR
00068 
00069 template <class T>
00070 GenMask1d<T>::GenMask1d(const GenMask1d<T>& aMask)
00071 
00072   : _pRefCnt(new int(0)),
00073     _width(aMask.width())
00074 
00075 {
00076   _pCoeffMap = new T[_width];
00077   memcpy(_pCoeffMap, aMask.pCoeffMap(), _width * sizeof(T));
00078 }
00079 
00080 
00081 // CONVERSION CONSTRUCTOR
00082 
00083 template <class T>
00084 template <class U>
00085 GenMask1d<T>::GenMask1d(const GenMask1d<U>& aMask)
00086 
00087   : _pRefCnt(new int(0)),
00088     _width(aMask.width())
00089 
00090 {
00091   _pCoeffMap = new T[_width];
00092 
00093   for (int i=0; i < _width; i++)
00094     {
00095       _pCoeffMap[i] = static_cast<T>(aMask.pCoeffMap()[i]);
00096     }
00097 }
00098 
00099 
00100 // INITIALIZE WITH GIVEN WIDTH AND VALUE
00101 
00102 template <class T>
00103 GenMask1d<T>::GenMask1d(unsigned int aWidth, T aValue)
00104 
00105   : _pRefCnt(new int(0)),
00106     _width(aWidth)
00107 
00108 {
00109   _pCoeffMap = new T[_width];
00110   qgFill(_pCoeffMap, _width, aValue);
00111 }
00112 
00113 
00114 // INITIALIZE WITH GIVEN WIDTH AND VAL ARRAY
00115 
00116 template <class T>
00117 GenMask1d<T>::GenMask1d(unsigned int aWidth, const T * const pVals)
00118 
00119   : _pRefCnt(new int(0)),
00120     _width(aWidth)
00121 
00122 {
00123   _pCoeffMap = new T[aWidth];
00124   memcpy(_pCoeffMap, pVals, aWidth * sizeof(T));
00125 }
00126 
00127 
00128 // SHALLOW COPY CONSTRUCTOR
00129 
00130 template <class T>
00131 GenMask1d<T>::GenMask1d(const GenMask1d& rhs, int)
00132 
00133   : _pRefCnt(rhs._pRefCnt),
00134     _width(rhs._width), 
00135     _pCoeffMap(rhs._pCoeffMap)
00136 
00137 {
00138   (*_pRefCnt)++;
00139 }
00140 
00141 
00142 // -------------------------------------------------------------------
00143 // D E S T R U C T O R 
00144 // -------------------------------------------------------------------
00145 
00146 
00147 // FREE SPACE ALLOCATED TO THE COEFFICIENT MAP
00148 
00149 template <class T>
00150 GenMask1d<T>::~GenMask1d()
00151 {
00152   if (*_pRefCnt == 0)
00153     {
00154       // The space allocated to the coefficient map
00155       // is not shared with another object
00156       delete [] _pCoeffMap;
00157       delete _pRefCnt;
00158     }
00159   else
00160     {
00161       // The space allocated to the coefficient map is shared
00162       // with at least one other object: Just decrement
00163       // the reference counter
00164       (*_pRefCnt)--;
00165     }
00166 }
00167 
00168 
00169 // -------------------------------------------------------------------
00170 // 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
00171 // -------------------------------------------------------------------
00172 
00173 
00174 // GET MASK WIDTH
00175 
00176 template <class T>
00177 inline int
00178 GenMask1d<T>::width() const
00179 {
00180   return _width;
00181 }
00182 
00183 
00184 // -------------------------------------------------------------------
00185 // 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
00186 // -------------------------------------------------------------------
00187 
00188 
00189 // GET A COEFFICIENT VALUE
00190 
00191 template <class T>
00192 T
00193 GenMask1d<T>::coeff(int anIdx) const
00194 {
00195   return *(_pCoeffMap + anIdx);
00196 }
00197 
00198 
00199 // -------------------------------------------------------------------
00200 // ACCESS TO DIRECT TRANSFORMATIONS OF THE COEFFICIENT MAP
00201 // -------------------------------------------------------------------
00202 
00203 
00204 // GET THE POINTER TO THE COEFFICIENT MAP
00205 
00206 template <class T>
00207 inline T*
00208 GenMask1d<T>::pCoeffMap() const
00209 {
00210   return _pCoeffMap;
00211 }
00212 
00213 
00214 // -------------------------------------------------------------------
00215 // T R A N S F O R M A T I O N
00216 // -------------------------------------------------------------------
00217 
00218 
00219 // SET COEFFICIENT AT GIVEN INDEX
00220 
00221 template <class T>
00222 void
00223 GenMask1d<T>::setCoeff(int anIdx, T aCoeff)
00224 {
00225   *(_pCoeffMap + anIdx) = aCoeff;
00226 }
00227 
00228 
00229 // -------------------------------------------------------------------
00230 // C O P Y
00231 // -------------------------------------------------------------------
00232 
00233 
00234 // SHALLOW COPY: THE COEFFICIENT MAP OF THE SOURCE MASK IS NOT DUPLICATED.
00235 // When the copy is completed, the pixel map of the destination
00236 // mask is the same memory space as the pixel map of the source mask.
00237 
00238 template <class T>
00239 GenMask1d<T>*
00240 GenMask1d<T>::shallowCopy()
00241 {
00242   return new GenMask1d(*this, 0);
00243 }
00244 
00245 // AUXILIARY: STORE A DEEP COPY OF A GIVEN MASK INTO THE CURRENT MASK
00246 template <class T>
00247   void GenMask1d<T>::deepCopyAux(const GenMask1d<T>& aMask)
00248 {
00249   // Are source and destination the same object?
00250   if (this != &aMask)
00251     {
00252       _pRefCnt = new int(0);
00253       _width   = aMask._width;
00254 
00255       // Free space allocated to the coefficent map of the destination mask
00256       delete [] _pCoeffMap;
00257       // Allocate space to the coefficent map
00258       _pCoeffMap = new T[_width];
00259       // Copy the coefficients
00260       memcpy(_pCoeffMap, aMask.pCoeffMap(), _width * sizeof(T));
00261     }
00262 }
00263 
00264 
00265 // -------------------------------------------------------------------
00266 // O P E R A T O R S
00267 // -------------------------------------------------------------------
00268 
00269 
00270 // ASSIGNMENT
00271 
00272 template <class T>
00273 GenMask1d<T>&
00274 GenMask1d<T>::operator=(const GenMask1d<T>& aMask)
00275 {
00276   deepCopyAux(aMask);
00277   return *this;
00278 }
00279 
00280 
00281 // -------------------------------------------------------------------
00282 
00283 
00284 } // namespace qgar