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 | |
---|
26 | template <class T> |
---|
27 | class image { |
---|
28 | public: |
---|
29 | /* create an image */ |
---|
30 | image(const int width, const int height, const bool init = true); |
---|
31 | |
---|
32 | /* delete an image */ |
---|
33 | ~image(); |
---|
34 | |
---|
35 | /* init an image */ |
---|
36 | void init(const T &val); |
---|
37 | |
---|
38 | /* copy an image */ |
---|
39 | image<T> *copy() const; |
---|
40 | |
---|
41 | /* get the width of an image. */ |
---|
42 | int width() const { return w; } |
---|
43 | |
---|
44 | /* get the height of an image. */ |
---|
45 | int height() const { return h; } |
---|
46 | |
---|
47 | /* image data. */ |
---|
48 | T *data; |
---|
49 | |
---|
50 | /* row pointers. */ |
---|
51 | T **access; |
---|
52 | |
---|
53 | private: |
---|
54 | int w, h; |
---|
55 | }; |
---|
56 | |
---|
57 | /* use imRef to access image data. */ |
---|
58 | #define imRef(im, x, y) (im->access[y][x]) |
---|
59 | |
---|
60 | /* use imPtr to get pointer to image data. */ |
---|
61 | #define imPtr(im, x, y) &(im->access[y][x]) |
---|
62 | |
---|
63 | template <class T> |
---|
64 | image<T>::image(const int width, const int height, const bool init) { |
---|
65 | w = width; |
---|
66 | h = height; |
---|
67 | data = new T[w * h]; // allocate space for image data |
---|
68 | access = new T*[h]; // allocate space for row pointers |
---|
69 | |
---|
70 | // initialize row pointers |
---|
71 | for (int i = 0; i < h; i++) |
---|
72 | access[i] = data + (i * w); |
---|
73 | |
---|
74 | if (init) |
---|
75 | memset(data, 0, w * h * sizeof(T)); |
---|
76 | } |
---|
77 | |
---|
78 | template <class T> |
---|
79 | image<T>::~image() { |
---|
80 | delete [] data; |
---|
81 | delete [] access; |
---|
82 | } |
---|
83 | |
---|
84 | template <class T> |
---|
85 | void image<T>::init(const T &val) { |
---|
86 | T *ptr = imPtr(this, 0, 0); |
---|
87 | T *end = imPtr(this, w-1, h-1); |
---|
88 | while (ptr <= end) |
---|
89 | *ptr++ = val; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | template <class T> |
---|
94 | image<T> *image<T>::copy() const { |
---|
95 | image<T> *im = new image<T>(w, h, false); |
---|
96 | memcpy(im->data, data, w * h * sizeof(T)); |
---|
97 | return im; |
---|
98 | } |
---|
99 | |
---|
100 | #endif |
---|
101 | |
---|