Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

image.h

Go to the documentation of this file.
00001 /* a simple image class */
00002 
00003 #ifndef IMAGE_H
00004 #define IMAGE_H
00005 
00006 #include <cstring>
00007 
00008 template <class T>
00009 class image {
00010  public:
00011   /* create an image */
00012   image(const int width, const int height, const bool init = true);
00013 
00014   /* delete an image */
00015   ~image();
00016 
00017   /* init an image */
00018   void init(const T &val);
00019 
00020   /* copy an image */
00021   image<T> *copy() const;
00022   
00023   /* get the width of an image. */
00024   int width() const { return w; }
00025   
00026   /* get the height of an image. */
00027   int height() const { return h; }
00028   
00029   /* image data. */
00030   T *data;
00031   
00032   /* row pointers. */
00033   T **access;
00034   
00035  private:
00036   int w, h;
00037 };
00038 
00039 /* use imRef to access image data. */
00040 #define imRef(im, x, y) (im->access[y][x])
00041   
00042 /* use imPtr to get pointer to image data. */
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];  // allocate space for image data
00050   access = new T*[h];   // allocate space for row pointers
00051   
00052   // initialize row pointers
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   

Generated on Wed Oct 5 14:36:12 2005 for RobotFlow by  doxygen 1.4.4