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 | #include <misc.h> |
---|
26 | /* |
---|
27 | template <class T> |
---|
28 | class image { |
---|
29 | public: |
---|
30 | |
---|
31 | image(const int width, const int height, const bool init = true); |
---|
32 | |
---|
33 | |
---|
34 | ~image(); |
---|
35 | |
---|
36 | |
---|
37 | void init(const T &val); |
---|
38 | |
---|
39 | image<T> *copy() const; |
---|
40 | |
---|
41 | int width() const { return w; } |
---|
42 | |
---|
43 | int height() const { return h; } |
---|
44 | |
---|
45 | T *data; |
---|
46 | |
---|
47 | T **access; |
---|
48 | |
---|
49 | private: |
---|
50 | int w, h; |
---|
51 | }; |
---|
52 | */ |
---|
53 | |
---|
54 | typedef struct{ |
---|
55 | int w,h; |
---|
56 | rgb *data; |
---|
57 | rgb **access; |
---|
58 | }image; |
---|
59 | |
---|
60 | typedef struct{ |
---|
61 | int w,h; |
---|
62 | float *data; |
---|
63 | float **access; |
---|
64 | }imagef; |
---|
65 | |
---|
66 | image *image_construct(const int width, const int height, const bool init); |
---|
67 | void image_destruct(image *self){ |
---|
68 | free(self->data); |
---|
69 | free(self->access); |
---|
70 | free(self); |
---|
71 | } |
---|
72 | void image_init(image *self,const rgb *val); |
---|
73 | image *image_copy(image *self); |
---|
74 | int image_width(image *self){return self->w;} |
---|
75 | int image_height(image *self){return self->h;} |
---|
76 | |
---|
77 | /* use imRef to access image data. */ |
---|
78 | #define imRef(im, x, y) (im->access[y][x]) |
---|
79 | |
---|
80 | /* use imPtr to get pointer to image data. */ |
---|
81 | #define imPtr(im, x, y) &(im->access[y][x]) |
---|
82 | |
---|
83 | image *image_construct(const int width, const int height, const bool init) { |
---|
84 | image *self = (image *)malloc(sizeof(image)); |
---|
85 | self->w = width; |
---|
86 | self->h = height; |
---|
87 | self->data = (rgb *)malloc(self->w * self->h * sizeof(rgb)); // allocate space for image data |
---|
88 | self->access = (rgb **)malloc(self->h* sizeof (rgb *)); // allocate space for row pointers |
---|
89 | |
---|
90 | // initialize row pointers |
---|
91 | for (int i = 0; i < self->h; i++) |
---|
92 | self->access[i] = self->data + (i * self->w); |
---|
93 | |
---|
94 | if (init) |
---|
95 | memset(self->data, 0, self->w * self->h * sizeof(rgb)); |
---|
96 | return self; |
---|
97 | } |
---|
98 | |
---|
99 | void image_init(image *self,const rgb *val){ |
---|
100 | rgb *ptr = imPtr(self,0,0); |
---|
101 | rgb *end = imPtr(self,self->w-1, self->h -1); |
---|
102 | while (ptr <=end) |
---|
103 | *ptr++ = *val; |
---|
104 | } |
---|
105 | |
---|
106 | image * image_copy(image *self){ |
---|
107 | image *im = image_construct(self->w, self->h,false); |
---|
108 | memcpy(im->data, self->data, self->w * self->h *sizeof(rgb)); |
---|
109 | return im; |
---|
110 | } |
---|
111 | |
---|
112 | //imagef |
---|
113 | |
---|
114 | imagef *imagef_construct(const int width, const int height, const bool init); |
---|
115 | void imagef_destruct(imagef *self){ |
---|
116 | free(self->data); |
---|
117 | free(self->access); |
---|
118 | free(self); |
---|
119 | } |
---|
120 | void imagef_init(imagef *self,const float *val); |
---|
121 | imagef *imagef_copy(imagef *self); |
---|
122 | int imagef_width(imagef *self){return self->w;} |
---|
123 | int imagef_height(imagef *self){return self->h;} |
---|
124 | |
---|
125 | /* use imRef to access image data. */ |
---|
126 | #define imRef(im, x, y) (im->access[y][x]) |
---|
127 | |
---|
128 | /* use imPtr to get pointer to image data. */ |
---|
129 | #define imPtr(im, x, y) &(im->access[y][x]) |
---|
130 | |
---|
131 | imagef *imagef_construct(const int width, const int height, const bool init) { |
---|
132 | imagef *self = (imagef *)malloc(sizeof(imagef)); |
---|
133 | self->w = width; |
---|
134 | self->h = height; |
---|
135 | self->data = (float *)malloc(self->w * self->h * sizeof(float)); // allocate space for image data |
---|
136 | self->access = (float **)malloc(self->h* sizeof (float *)); // allocate space for row pointers |
---|
137 | |
---|
138 | // initialize row pointers |
---|
139 | for (int i = 0; i < self->h; i++) |
---|
140 | self->access[i] = self->data + (i * self->w); |
---|
141 | |
---|
142 | if (init) |
---|
143 | memset(self->data, 0, self->w * self->h * sizeof(float)); |
---|
144 | return self; |
---|
145 | } |
---|
146 | |
---|
147 | void imagef_init(imagef *self,const float *val){ |
---|
148 | float *ptr = imPtr(self,0,0); |
---|
149 | float *end = imPtr(self,self->w-1, self->h -1); |
---|
150 | while (ptr <=end) |
---|
151 | *ptr++ = *val; |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | imagef * imagef_copy(image *self){ |
---|
156 | imagef *im = imagef_construct(self->w, self->h,false); |
---|
157 | memcpy(im->data, self->data, self->w * self->h *sizeof(float)); |
---|
158 | return im; |
---|
159 | } |
---|
160 | |
---|
161 | #endif |
---|
162 | |
---|