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 #pragma once
00022 
00023 #include <cstring>
00024 
00025 namespace distance_transform
00026 {
00027 
00028   template <class T>
00029   class image {
00030    public:
00031     /* create an image */
00032     image(const int width, const int height, const bool init = true);
00033 
00034     /* delete an image */
00035     ~image();
00036 
00037     /* init an image */
00038     void init(const T &val);
00039 
00040     /* copy an image */
00041     image<T> *copy() const;
00042     
00043     /* get the width of an image. */
00044     int width() const { return w; }
00045     
00046     /* get the height of an image. */
00047     int height() const { return h; }
00048     
00049     /* image data. */
00050     T *data;
00051     
00052     /* row pointers. */
00053     T **access;
00054     
00055    private:
00056     int w, h;
00057   };
00058 
00059   /* use imRef to access image data. */
00060 #define imRef(im, x, y) (im->access[y][x])
00061     
00062   /* use imPtr to get pointer to image data. */
00063 #define imPtr(im, x, y) &(im->access[y][x])
00064 
00065   template <class T>
00066   image<T>::image(const int width, const int height, const bool init) {
00067     w = width;
00068     h = height;
00069     data = new T[w * h];  // allocate space for image data
00070     access = new T*[h];   // allocate space for row pointers
00071     
00072     // initialize row pointers
00073     for (int i = 0; i < h; i++)
00074       access[i] = data + (i * w);  
00075     
00076     if (init)
00077       memset(data, 0, w * h * sizeof(T));
00078   }
00079 
00080   template <class T>
00081   image<T>::~image() {
00082     delete [] data; 
00083     delete [] access;
00084   }
00085 
00086   template <class T>
00087   void image<T>::init(const T &val) {
00088     T *ptr = imPtr(this, 0, 0);
00089     T *end = imPtr(this, w-1, h-1);
00090     while (ptr <= end)
00091       *ptr++ = val;
00092   }
00093 
00094   template <class T>
00095   image<T> *image<T>::copy() const {
00096     image<T> *im = new image<T>(w, h, false);
00097     memcpy(im->data, data, w * h * sizeof(T));
00098     return im;
00099   }
00100 
00101 } // namespace


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Mon Oct 9 2017 03:09:34