image.h
Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2006 Pedro Felzenszwalb
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 */
00018 
00019 /* a simple image class */
00020 
00021 #ifndef IMAGE_H
00022 #define IMAGE_H
00023 
00024 #include <cstring>
00025 
00026 template <class T>
00027 class image {
00028  public:
00029   /* create an image */
00030   image(const int width, const int height, const bool init = true);
00031     image() {}
00032   /* delete an image */
00033   ~image();
00034 
00035   /* init an image */
00036   void init(const T &val);
00037 
00038   /* copy an image */
00039   image<T> *copy() const;
00040 
00041   /* get the width of an image. */
00042   int width() const { return w; }
00043 
00044   /* get the height of an image. */
00045   int height() const { return h; }
00046 
00047   /* image data. */
00048   T *data;
00049 
00050   /* row pointers. */
00051   T **access;
00052 
00053  private:
00054   int w, h;
00055 };
00056 
00057 /* use imRef to access image data. */
00058 #define imRef(im, x, y) (im->access[y][x])
00059 
00060 /* use imPtr to get pointer to image data. */
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];  // allocate space for image data
00068   access = new T*[h];   // allocate space for row pointers
00069 
00070   // initialize row pointers
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 


cpl_superpixels
Author(s): Tucker Hermans
autogenerated on Wed Nov 27 2013 11:38:57