00001
00009 #ifndef _TableauBiDim_h_
00010 #define _TableauBiDim_h_
00011
00012 #include <assert.h>
00013 #include <string>
00014
00015 template <class T>
00016 class TableauBiDim
00017 {
00018 private:
00019
00020 unsigned int XSize;
00021
00022 unsigned int YSize;
00023
00024
00025 T* buffer;
00026
00027 public:
00028
00029
00030 TableauBiDim() : XSize(0), YSize(0), buffer(NULL) { }
00031
00032
00033 TableauBiDim(const unsigned int sizeX, const unsigned int sizeY);
00034
00035
00036 TableauBiDim(const TableauBiDim& tab);
00037
00038
00039 ~TableauBiDim();
00040
00041
00042
00043 bool isInitialized() const { return buffer; }
00044
00045
00046
00047 unsigned int getXSize() const { return XSize; }
00048
00049
00050 unsigned int getYSize() const { return YSize; }
00051
00052
00053 T operator() (unsigned int x, unsigned int y) const { return buffer[y*XSize+x]; }
00054
00055
00056 T& operator() (unsigned int x, unsigned int y) { return buffer[y*XSize+x]; }
00057
00058
00059 const T* data() const { assert(isInitialized()); return buffer; }
00060
00061
00062 T* data() { assert(isInitialized()); return buffer; }
00063
00064
00065
00066 void set(unsigned int sizeX, unsigned int sizeY);
00067 };
00068
00069 template <class T>
00070 TableauBiDim<T>::TableauBiDim(const unsigned int sizeX, const unsigned int sizeY) : XSize(sizeX), YSize(sizeY)
00071 {
00072 buffer = new T[sizeX*sizeY];
00073 if (buffer == NULL)
00074 std::cerr << "TableauBiDim error: memory allocation failed in constructor." << std::endl;
00075 }
00076
00077 template <class T>
00078 TableauBiDim<T>::TableauBiDim(const TableauBiDim& tab) : XSize(tab.XSize), YSize(tab.YSize)
00079 {
00080 if (tab.buffer == NULL)
00081 buffer = NULL;
00082 else {
00083 buffer = new T[XSize*YSize];
00084 if (buffer == NULL)
00085 std::cerr << "TableauBiDim error: memory allocation failed in constructor." << std::endl;
00086 else
00087 memcpy(buffer, tab.buffer, XSize*YSize*sizeof(T));
00088 }
00089 }
00090
00091 template <class T>
00092 TableauBiDim<T>::~TableauBiDim()
00093 {
00094 if (buffer)
00095 delete [] buffer;
00096 }
00097
00098 template <class T>
00099 void TableauBiDim<T>::set(unsigned int sizeX, unsigned int sizeY)
00100 {
00101 XSize = sizeX;
00102 YSize = sizeY;
00103 if (buffer)
00104 delete [] buffer;
00105 buffer = new T[sizeX*sizeY];
00106 if (buffer == NULL)
00107 std::cerr << "RGBImage error: memory allocation failed in function set." << std::endl;
00108 }
00109
00110 #endif