forked from ugocapeto/thepainter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_image.c
209 lines (171 loc) · 3.44 KB
/
scale_image.c
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
#include "header.h"
void scale_image(
int *image_arr1,
int width1,
int height1,
int *image_arr2,
int width2,
int height2,
char *method,
char *gaussian
)
{
int *imageb_arr1;
double factor_x;
double factor_y;
double zoom_sigma_zero= 0.6;
double sigma_x;
double sigma_y;
int precision= 5;
int debug_flag= 0;
int err_flag;
int i1;
int j1;
int pixel1;
if ( debug_flag == 1 ) {
/*
Print image_arr1
*/
err_flag= write_image(
(char *)"image_arr1.png",
image_arr1,
width1,
height1
);
if ( err_flag == 1 ) {
error_handler((char *)"scale_image");
}
}
factor_x= (double)width2/(double)width1;
factor_y= (double)height2/(double)height1;
if ( factor_x < 1.0 ) {
/*
We are downsampling along the x direction
This requires a Gaussian blur in the x direction
*/
sigma_x= zoom_sigma_zero*sqrt(1./(factor_x*factor_x)-1.);
}
else {
/*
We are upampling along the x direction
This doesn not require a Gaussian blur in the x direction
We are gonna apply a Gaussian blur in the x direction
using a very small sigma in the x direction
*/
sigma_x= 0.00001;
}
if ( factor_y < 1.0 ) {
/*
We are downsampling along the y direction
This requires a Gaussian blur in the y direction
*/
sigma_y= zoom_sigma_zero*sqrt(1./(factor_y*factor_y)-1.);
}
else {
/*
We are upampling along the y direction
This doesn not require a Gaussian blur in the y direction
We are gonna apply a Gaussian blur in the y direction
using a very small sigma in the y direction
*/
sigma_y= 0.00001;
}
if ( strcmp(gaussian,"yes") == 0 ) {
/*
Apply a Gaussian blur to image_arr1
*/
imageb_arr1= (int *)calloc(width1*height1,sizeof(int));
gaussian_blur_image_2(
image_arr1,
width1,
height1,
sigma_x,
sigma_y,
precision,
imageb_arr1
);
}
else if ( strcmp(gaussian,"no") == 0 ) {
/*
Do not apply a Gaussian blur to image_arr1
*/
imageb_arr1= (int *)calloc(width1*height1,sizeof(int));
for ( i1= 0 ; i1< height1 ; i1++ ) {
for ( j1= 0 ; j1< width1 ; j1++ ) {
pixel1= i1*width1+j1;
imageb_arr1[pixel1]= image_arr1[pixel1];
}
}
}
else {
error_handler((char *)"scale_image");
}
if ( debug_flag == 1 ) {
/*
Print imageb_arr1
*/
err_flag= write_image(
(char *)"imageb_arr1.png",
imageb_arr1,
width1,
height1
);
if ( err_flag == 1 ) {
error_handler((char *)"scale_image");
}
}
/*
Scale down imageb_arr1
*/
if ( strcmp(method,"nearest_neighbor") == 0 ) {
scale_image_nearest_neighbor(
imageb_arr1,
width1,
height1,
image_arr2,
width2,
height2
);
}
else if ( strcmp(method,"linear") == 0 ) {
scale_image_linear(
imageb_arr1,
width1,
height1,
image_arr2,
width2,
height2
);
}
else if ( strcmp(method,"cubic") == 0 ) {
scale_image_cubic(
imageb_arr1,
width1,
height1,
image_arr2,
width2,
height2
);
}
else {
error_handler((char *)"scale_image");
}
if ( debug_flag == 1 ) {
/*
Print image_arr2
*/
err_flag= write_image(
(char *)"image_arr2.png",
image_arr2,
width2,
height2
);
if ( err_flag == 1 ) {
error_handler((char *)"scale_image");
}
}
/*
Free imageb_arr1
*/
free(imageb_arr1);
}