-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage.cpp
268 lines (213 loc) · 6.44 KB
/
image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "image.h"
#include "std.h"
/********** Create/Delete **********/
/* allocate a new image of size width x height */
image_t *image_new(int width, int height)
{
image_t *image = NEW(image_t);
if(image == NULL)
{
fprintf(stderr, "Error: image_new() - not enough memory !\n");
exit(1);
}
image->width = width;
image->height = height;
image->stride = ( (width+3) / 4 ) * 4;
image->data = NEWA(float, image->stride*height*sizeof(float));
if(image->data == NULL)
{
fprintf(stderr, "Error: image_new() - not enough memory !\n");
exit(1);
}
return image;
}
/* allocate a new image and copy the content from src */
image_t *image_cpy(const image_t *src)
{
image_t *dst = image_new(src->width, src->height);
memcpy(dst->data, src->data, src->stride*src->height*sizeof(float));
return dst;
}
/* set all pixels values to zeros */
void image_erase(image_t *image)
{
memset(image->data, 0, image->stride*image->height*sizeof(float));
}
/* multiply an image by a scalar */
void image_mul_scalar(image_t *image, float scalar)
{
int i;
for( i=0 ; i<image->stride*image->height ; i++)
image->data[i] *= scalar;
}
/* free memory of an image */
void image_delete(image_t *image)
{
if(image == NULL)
{
//fprintf(stderr, "Warning: Delete image --> Ignore action (image not allocated)\n");
}
else
{
free(image->data);
free(image);
}
}
/* allocate a new color image of size width x height */
color_image_t *color_image_new(int width, int height)
{
size_t stride_channel = width*height*sizeof(float);
char *buffer = NEWA(char, sizeof(color_image_t) + 3*stride_channel);
if(buffer == NULL)
{
fprintf(stderr, "Error: color_image_new() - not enough memory !\n");
exit(1);
}
color_image_t *image = (color_image_t*) buffer;
image->width = width;
image->height = height;
image->c1 = (float*) (buffer + sizeof(color_image_t));
image->c2 = (float*) (buffer + sizeof(color_image_t) + stride_channel);
image->c3 = (float*) (buffer + sizeof(color_image_t) + 2*stride_channel);
return image;
}
/* allocate a new color image and copy the content from src */
color_image_t *color_image_cpy(const color_image_t *src)
{
color_image_t *dst = color_image_new(src->width, src->height);
memcpy(dst->c1, src->c1, 3*src->width*src->height*sizeof(float));
return dst;
}
/* set all pixels values to zeros */
void color_image_erase(color_image_t *image)
{
memset(image->c1, 0, 3*image->width*image->height*sizeof(float));
}
/* free memory of a color image */
void color_image_delete(color_image_t *image)
{
if(image)
{
free(image); // the image is allocated such that the data is stored just after the pointer
}
}
/* convert a color image to a gray-scale image */
image_t* image_gray_from_color( color_image_t* img )
{
image_t* res = image_new(img->width, img->height);
int n=0;
for(int j=0; j<img->height; j++)
for(int i=0; i<img->width; i++,n++)
res->data[i+j*res->stride] = (img->c1[n] + img->c2[n] + img->c3[n])/3;
return res;
}
/* reallocate the memory of an image to fit the new width height */
void resize_if_needed_newsize(image_t *im, int w, int h)
{
if(im->width != w || im->height != h)
{
im->width = w;
im->height = h;
im->stride = ((w+3)/4)*4;
float *data = NEWA(float,im->stride*h*sizeof(float));
if(data == NULL)
{
fprintf(stderr, "Error: resize_if_needed_newsize() - not enough memory !\n");
exit(1);
}
free(im->data);
im->data = data;
}
}
/************ Resizing *********/
/* resize an image to a new size (assumes a difference only in width) */
void image_resize_horiz(image_t *dst, const image_t *src)
{
int i;
float real_scale = ((float) src->width-1) / ((float) dst->width-1);
for(i = 0; i < dst->height; i++)
{
int j;
for(j = 0; j < dst->width; j++)
{
float dx;
int x;
x = floor((float) j * real_scale);
dx = j * real_scale - x;
if(x >= (src->width - 1))
{
dst->data[i * dst->stride + j] =
src->data[i * src->stride + src->width - 1];
}
else
{
dst->data[i * dst->stride + j] =
(1.0f - dx) * src->data[i * src->stride + x ] +
( dx) * src->data[i * src->stride + x + 1];
}
}
}
}
/* resize an image to a new size (assumes a difference only in height) */
void image_resize_vert(image_t *dst, const image_t *src)
{
int i;
float real_scale = ((float) src->height-1) / ((float) dst->height-1);
for(i = 0; i < dst->width; i++)
{
int j;
for(j = 0; j < dst->height; j++)
{
int y;
float dy;
y = floor((float) j * real_scale);
dy = j * real_scale - y;
if(y >= (src->height - 1))
{
dst->data[j * dst->stride + i] =
src->data[i + (src->height - 1) * src->stride];
}
else
{
dst->data[j * dst->stride + i] =
(1.0f - dy) * src->data[i + (y ) * src->stride] +
( dy) * src->data[i + (y + 1) * src->stride];
}
}
}
}
/* resize an image with bilinear interpolation to fit the new weidht, height ; reallocation is done if necessary */
void image_resize_bilinear_newsize(image_t *dst, const image_t *src, int new_width, int new_height)
{
resize_if_needed_newsize(dst,new_width,new_height);
if(new_width < new_height)
{
image_t *tmp = image_new(new_width,src->height);
image_resize_horiz(tmp,src);
image_resize_vert(dst,tmp);
image_delete(tmp);
}
else
{
image_t *tmp = image_new(src->width,new_height);
image_resize_vert(tmp,src);
image_resize_horiz(dst,tmp);
image_delete(tmp);
}
}
/* resize an image with bilinear interpolation */
image_t *image_resize_bilinear_scale(const image_t *src, float scale) {
const int new_width = int(0.5 + src->width * scale);
const int new_height = int(0.5 + src->height * scale);
image_t *res = image_new(new_width,src->height);
image_resize_bilinear_newsize(res, src, new_width, new_height);
return res;
}
/* crop an image (in-place) */
void image_crop(image_t* img, int width, int height)
{
assert(width<=img->width);
img->width = width;
assert(height<=img->height);
img->height = height;
}