00001 #if !defined(AFX_COLORCMatrix_H__138A3C06_93D3_4A65_80F8_3D9F2CF43EC4__INCLUDED_)
00002 #define AFX_COLORCMatrix_H__138A3C06_93D3_4A65_80F8_3D9F2CF43EC4__INCLUDED_
00003 #include <assert.h>
00005
00008
00013 template<class T>
00014 class CMatrix
00015 {
00016 public:
00017 CMatrix();
00018 CMatrix(int x, int y);
00019 virtual ~CMatrix();
00020
00021
00022
00023
00024
00026 int xmax() {return Xmax;};
00028 int ymax() {return Ymax;};
00030 int xmin() {return Xmin;};
00032 int ymin() {return Ymin;};
00034 int size() {return TotSize;};
00036 int x() {return TotX;};
00038 int y() {return TotY;};
00040 T & operator()( const unsigned int x, const unsigned int y);
00042 CMatrix<T>& operator=(CMatrix<T> &RHS);
00044 double interpolate( double x, double y);
00046 T & operator[]( const unsigned int val){return m_CMatrix[val];};
00048 void Initialize(T val){for(int i=0; i<size(); i++) m_CMatrix[i]=val;};
00049
00057 void SetRange(int xtop, int ytop, int xbottom, int ybottom, bool copy=true);
00058
00060 void SetRange(CMatrix<T> OLD, bool copy=true){SetRange(OLD.xmin(),OLD.ymin(),OLD.xmax(),OLD.ymax(),copy);};
00061
00063 void SetSize(int x, int y);
00065
00066 protected:
00067 int Xmin,Ymin,Xmax,Ymax, TotSize, TotX, TotY;
00068
00069 };
00070
00071
00072
00073
00074
00075
00076
00078
00079
00081
00082
00084
00086
00087 template<class T>
00088 CMatrix<T>::CMatrix():
00089 Xmin(0),Ymin(0),Xmax(0),Ymax(0),TotSize(0), TotX(0), TotY(0)
00090 {
00091 }
00092
00093 template<class T>
00094 CMatrix<T>::CMatrix(int x, int y):
00095 Xmin(0),Ymin(0),Xmax(x),Ymax(y),TotSize(x*y), TotX(x), TotY(y)
00096 {
00097 m_CMatrix=new T[x*y];
00098 }
00099
00100
00102
00104
00105 T& CMatrix<T>::operator()( const unsigned int x, const unsigned int y)
00106 {
00107 assert(x>=Xmin && x<Xmax);
00108 assert(y>=Ymin && y<Ymax);
00109 return zeroloc[ TotX * y + x ];
00110 }
00111
00112
00113 template<class T>
00114 double CMatrix<T>::interpolate(double x, double y)
00115 {
00116 double fpartx, fparty, x1,y1;
00117 fpartx=modf(x,&x1);
00118 fparty=modf(y,&y1);
00119 return (*this)((int)x1,(int)y1)*(1-fpartx)*(1-fparty)+
00120 (*this)((int)x1+1,(int)y1)*fpartx*(1-fparty)+
00121 (*this)((int)x1,(int)y1+1)*(1-fpartx)*fparty+
00122 (*this)((int)x1+1,(int)y1+1)*fpartx*fparty;
00123 }
00124
00125
00126 template<class T>
00127 CMatrix<T> & CMatrix<T>::operator=(CMatrix<T> &RHS)
00128 {
00129 SetRange(RHS.xmin(),RHS.ymin(),RHS.xmax(),RHS.ymax(),false);
00130 for(int i=0; i<TotSize; i++)
00131 m_CMatrix[i]=RHS[i];
00132 return *this;
00133 }
00134
00136
00138
00139 void CMatrix<T>::SetRange(int xtop, int ytop, int xbottom, int ybottom, bool copy)
00140 {
00141 int i, j;
00142 assert(xtop<xbottom);
00143 assert(ytop<ybottom);
00144 if(xtop!=Xmin || ytop!=Ymin || xbottom!=Xmax || ybottom!=Ymax)
00145 {
00146 T * tempzero,* tempCMatrix = new T[(xtop-xbottom)*(ytop-ybottom)];
00147
00148 tempzero=tempCMatrix+(xtop-xbottom) * (-ytop) - xtop;
00149
00150
00151 if(copy)
00152 {
00153 Xmin=Xmin>xtop?Xmin:xtop;
00154 Xmax=Xmax<xbottom?Xmax:xbottom;
00155 Ymin=Ymin>ytop?Ymin:ytop;
00156 Ymax=Ymax<ybottom?Ymax:ybottom;
00157 for(i=Xmin; i<Xmax; i++)
00158 for(j=Ymin; j<Ymax; j++)
00159 tempzero[((xbottom-xtop) * j + i)] = (*this)(i,j);
00160 }
00161 if(TotSize>0)
00162 delete [] m_CMatrix;
00163 zeroloc=tempzero;
00164 m_CMatrix=tempCMatrix;
00165 Xmin=xtop;
00166 Ymin=ytop;
00167 Xmax=xbottom;
00168 Ymax=ybottom;
00169 TotSize=(xtop-xbottom)*(ytop-ybottom);
00170 TotX=xbottom-xtop;
00171 TotY=ybottom-ytop;
00172 }
00173 }
00174
00175
00176 template<class T>
00177 void CMatrix<T>::SetSize(int x, int y)
00178 {
00179 SetRange(0,0,x,y);
00180 }
00181
00182 template<class T>
00183 CMatrix<T>::~CMatrix()
00184 {
00185 if(TotSize>0) delete [] m_CMatrix;
00186 }
00187
00188 #endif // !defined(AFX_COLORCMatrix_H__138A3C06_93D3_4A65_80F8_3D9F2CF43EC4__INCLUDED_)