Optimized Belief Propagation (CPU and GPU)
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 #ifndef IMAGE_H
22 #define IMAGE_H
23 
24 #include <cstring>
25 
30 namespace bp_single_thread_imp {
31 
32 template <class T>
33 class image {
34  public:
35  /* create an image */
36  image(int width, int height, bool init = true);
37 
38  /* delete an image */
39  ~image();
40 
41  /* init an image */
42  void init(const T &val);
43 
44  /* copy an image */
45  image<T> *copy() const;
46 
47  /* get the width of an image. */
48  int width() const { return w; }
49 
50  /* get the height of an image. */
51  int height() const { return h; }
52 
53  /* image data. */
54  T *data;
55 
56  /* row pointers. */
57  T **access;
58 
59  private:
60  int w, h;
61 };
62 
63 /* use imRef to access image data. */
64 #define imRef(im, x, y) (im->access[y][x])
65 
66 /* use imPtr to get pointer to image data. */
67 #define imPtr(im, x, y) &(im->access[y][x])
68 
69 template <class T>
70 image<T>::image(int width, int height, bool init) {
71  w = width;
72  h = height;
73  data = new T[w * h]; // allocate space for image data
74  access = new T*[h]; // allocate space for row pointers
75 
76  // initialize row pointers
77  for (int i = 0; i < h; i++)
78  access[i] = data + (i * w);
79 
80  if (init)
81  memset(data, 0, w * h * sizeof(T));
82 }
83 
84 template <class T>
86  delete [] data;
87  delete [] access;
88 }
89 
90 template <class T>
91 void image<T>::init(const T &val) {
92  T *ptr = imPtr(this, 0, 0);
93  T *end = imPtr(this, w-1, h-1);
94  while (ptr <= end)
95  *ptr++ = val;
96 }
97 
98 
99 template <class T>
101  image<T> *im = new image<T>(w, h, false);
102  memcpy(im->data, data, w * h * sizeof(T));
103  return im;
104 }
105 
106 };
107 
108 #endif
109 
image(int width, int height, bool init=true)
Definition: image.h:70
int height() const
Definition: image.h:51
void init(const T &val)
Definition: image.h:91
image< T > * copy() const
Definition: image.h:100
#define imPtr(im, x, y)
Definition: image.h:67
Class and structs in single-thread CPU bp implementation by Pedro Felzenwalb available at https://cs....
Definition: convolve.h:33