image.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2006 Pedro Felzenszwalb
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 /* a simple image class */
20 
21 #pragma once
22 
23 #include <cstring>
24 
25 namespace distance_transform
26 {
27 
28  template <class T>
29  class image {
30  public:
31  /* create an image */
32  image(const int width, const int height, const bool init = true);
33 
34  /* delete an image */
35  ~image();
36 
37  /* init an image */
38  void init(const T &val);
39 
40  /* copy an image */
41  image<T> *copy() const;
42 
43  /* get the width of an image. */
44  int width() const { return w; }
45 
46  /* get the height of an image. */
47  int height() const { return h; }
48 
49  /* image data. */
50  T *data;
51 
52  /* row pointers. */
53  T **access;
54 
55  private:
56  int w, h;
57  };
58 
59  /* use imRef to access image data. */
60 #define imRef(im, x, y) (im->access[y][x])
61 
62  /* use imPtr to get pointer to image data. */
63 #define imPtr(im, x, y) &(im->access[y][x])
64 
65  template <class T>
66  image<T>::image(const int width, const int height, const bool init) {
67  w = width;
68  h = height;
69  data = new T[w * h]; // allocate space for image data
70  access = new T*[h]; // allocate space for row pointers
71 
72  // initialize row pointers
73  for (int i = 0; i < h; i++)
74  access[i] = data + (i * w);
75 
76  if (init)
77  memset(data, 0, w * h * sizeof(T));
78  }
79 
80  template <class T>
82  delete [] data;
83  delete [] access;
84  }
85 
86  template <class T>
87  void image<T>::init(const T &val) {
88  T *ptr = imPtr(this, 0, 0);
89  T *end = imPtr(this, w-1, h-1);
90  while (ptr <= end)
91  *ptr++ = val;
92  }
93 
94  template <class T>
96  image<T> *im = new image<T>(w, h, false);
97  memcpy(im->data, data, w * h * sizeof(T));
98  return im;
99  }
100 
101 } // namespace
int width() const
Definition: image.h:44
void init(const T &val)
Definition: image.h:87
int height() const
Definition: image.h:47
image< T > * copy() const
Definition: image.h:95
image(const int width, const int height, const bool init=true)
Definition: image.h:66
#define imPtr(im, x, y)
Definition: image.h:63


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:49