-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgaussian.im
225 lines (181 loc) · 5.18 KB
/
gaussian.im
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
#define IMAGER_NO_CONTEXT
#include "imager.h"
#include <math.h>
static double
gauss(int x, double std) {
return 1.0/(sqrt(2.0*PI)*std)*exp(-(double)(x)*(double)(x)/(2*std*std));
}
/* Counters are as follows
l: lines
i: columns
c: filter coeffs
ch: channels
pc: coeff equalization
*/
int
i_gaussian(i_img *im, double stddev) {
return i_gaussian2( im, stddev, stddev );
}
typedef struct s_gauss_coeff {
int diameter;
int radius;
double *coeff;
} t_gauss_coeff;
static t_gauss_coeff *build_coeff( i_img *im, double stddev ) {
double *coeff = NULL;
double pc;
int radius, diameter, i;
t_gauss_coeff *ret = mymalloc(sizeof(struct s_gauss_coeff));
ret->coeff = NULL;
if (im->bits <= 8)
radius = ceil(2 * stddev);
else
radius = ceil(3 * stddev);
diameter = 1 + radius * 2;
coeff = mymalloc(sizeof(double) * diameter);
for(i=0;i <= radius;i++)
coeff[radius + i]=coeff[radius - i]=gauss(i, stddev);
pc=0.0;
for(i=0; i < diameter; i++)
pc+=coeff[i];
for(i=0;i < diameter;i++) {
coeff[i] /= pc;
// im_log((aIMCTX, 1, "i_gaussian2 Y i=%i coeff=%.2f\n", i, coeff[i] ));
}
ret->diameter = diameter;
ret->radius = radius;
ret->coeff = coeff;
return ret;
}
static void free_coeff(t_gauss_coeff *co ) {
if( co->coeff != NULL )
myfree( co->coeff );
myfree( co );
}
#define img_copy(dest, src) i_copyto( (dest), (src), 0,0, (src)->xsize,(src)->ysize, 0,0);
int
i_gaussian2(i_img *im, double stddevX, double stddevY) {
int c, ch;
i_img_dim x, y;
double pc;
t_gauss_coeff *co = NULL;
double res[MAXCHANNELS];
i_img *timg;
i_img *yin;
i_img *yout;
dIMCTXim(im);
im_log((aIMCTX, 1,"i_gaussian2(im %p, stddev %.2f,%.2f)\n",im,stddevX,stddevY));
i_clear_error();
if (stddevX < 0) {
i_push_error(0, "stddevX must be positive");
return 0;
}
if (stddevY < 0) {
i_push_error(0, "stddevY must be positive");
return 0;
}
if( stddevX == stddevY && stddevY == 0 ) {
i_push_error(0, "stddevX or stddevY must be positive");
return 0;
}
/* totally silly cutoff */
if (stddevX > 1000) {
stddevX = 1000;
}
if (stddevY > 1000) {
stddevY = 1000;
}
timg = i_sametype(im, im->xsize, im->ysize);
if( stddevX > 0 ) {
/* Build Y coefficient matrix */
co = build_coeff( im, stddevX );
im_log((aIMCTX, 1, "i_gaussian2 X coeff radius=%i diamter=%i coeff=%p\n", co->radius, co->diameter, co->coeff));
/******************/
/* Process X blur */
im_log((aIMCTX, 1, "i_gaussian2 X blur from im=%p to timg=%p\n", im, timg));
#code im->bits <= 8
IM_COLOR rcolor;
for(y = 0; y < im->ysize; y++) {
for(x = 0; x < im->xsize; x++) {
pc=0.0;
for(ch=0;ch<im->channels;ch++)
res[ch]=0;
for(c = 0;c < co->diameter; c++)
if (IM_GPIX(im,x+c-co->radius,y,&rcolor)!=-1) {
for(ch=0;ch<im->channels;ch++)
res[ch]+= rcolor.channel[ch] * co->coeff[c];
pc+=co->coeff[c];
}
for(ch=0;ch<im->channels;ch++) {
double value = res[ch] / pc;
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value);
}
IM_PPIX(timg, x, y, &rcolor);
}
}
#/code
/* processing is im -> timg=yin -> im=yout */
yin = timg;
yout = im;
}
else {
im_log((aIMCTX, 1, "i_gaussian2 X coeff is unity\n"));
/* processing is im=yin -> timg=yout -> im */
yin = im;
yout = timg;
}
if( stddevY > 0 ) {
if( stddevX != stddevY ) {
if( co != NULL ) {
free_coeff(co);
co = NULL;
}
/* Build Y coefficient matrix */
co = build_coeff( im, stddevY );
im_log((aIMCTX, 1, "i_gaussian2 Y coeff radius=%i diamter=%i coeff=%p\n", co->radius, co->diameter, co->coeff));
}
/******************/
/* Process Y blur */
im_log((aIMCTX, 1, "i_gaussian2 Y blur from yin=%p to yout=%p\n", yin, yout));
#code im->bits <= 8
IM_COLOR rcolor;
for(x = 0;x < im->xsize; x++) {
for(y = 0; y < im->ysize; y++) {
pc=0.0;
for(ch=0; ch<im->channels; ch++)
res[ch]=0;
for(c=0; c < co->diameter; c++)
if (IM_GPIX(yin, x, y+c-co->radius, &rcolor)!=-1) {
for(ch=0;ch<yin->channels;ch++)
res[ch]+= rcolor.channel[ch] * co->coeff[c];
pc+=co->coeff[c];
}
for(ch=0;ch<yin->channels;ch++) {
double value = res[ch]/pc;
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value);
}
IM_PPIX(yout, x, y, &rcolor);
}
}
#/code
if( im != yout ) {
im_log((aIMCTX, 1, "i_gaussian2 copying yout=%p to im=%p\n", yout, im));
img_copy( im, yout );
}
}
else {
im_log((aIMCTX, 1, "i_gaussian2 Y coeff is unity\n"));
if( yin == timg ) {
im_log((aIMCTX, 1, "i_gaussian2 copying timg=%p to im=%p\n", timg, im));
img_copy( im, timg );
}
}
im_log((aIMCTX, 1, "i_gaussian2 im=%p\n", im));
im_log((aIMCTX, 1, "i_gaussian2 timg=%p\n", timg));
im_log((aIMCTX, 1, "i_gaussian2 yin=%p\n", yin));
im_log((aIMCTX, 1, "i_gaussian2 yout=%p\n", yout));
if( co != NULL )
free_coeff(co);
i_img_destroy(timg);
return 1;
}