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