00001
00002
00003 #ifndef IMAGE_H
00004 #define IMAGE_H
00005
00006 #include <cstring>
00007
00008 template <class T>
00009 class image {
00010 public:
00011
00012 image(const int width, const int height, const bool init = true);
00013
00014
00015 ~image();
00016
00017
00018 void init(const T &val);
00019
00020
00021 image<T> *copy() const;
00022
00023
00024 int width() const { return w; }
00025
00026
00027 int height() const { return h; }
00028
00029
00030 T *data;
00031
00032
00033 T **access;
00034
00035 private:
00036 int w, h;
00037 };
00038
00039
00040 #define imRef(im, x, y) (im->access[y][x])
00041
00042
00043 #define imPtr(im, x, y) &(im->access[y][x])
00044
00045 template <class T>
00046 image<T>::image(const int width, const int height, const bool init) {
00047 w = width;
00048 h = height;
00049 data = new T[w * h];
00050 access = new T*[h];
00051
00052
00053 for (int i = 0; i < h; i++)
00054 access[i] = data + (i * w);
00055
00056 if (init)
00057 memset(data, 0, w * h * sizeof(T));
00058 }
00059
00060 template <class T>
00061 image<T>::~image() {
00062 delete [] data;
00063 delete [] access;
00064 }
00065
00066 template <class T>
00067 void image<T>::init(const T &val) {
00068 T *ptr = imPtr(this, 0, 0);
00069 T *end = imPtr(this, w-1, h-1);
00070 while (ptr <= end)
00071 *ptr++ = val;
00072 }
00073
00074
00075 template <class T>
00076 image<T> *image<T>::copy() const {
00077 image<T> *im = new image<T>(w, h, false);
00078 memcpy(im->data, data, w * h * sizeof(T));
00079 return im;
00080 }
00081
00082 #endif
00083