gaussian.im

filter: Statementstmt 91.9% 103 / 112 Branchbran 89.6% 95 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.8% 198 / 218 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« color.c ↑ Summary compose.im »
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include <math.h>
4  
5 static double
6 101 gauss(int x, double std) {
7 101 return 1.0/(sqrt(2.0*PI)*std)*exp(-(double)(x)*(double)(x)/(2*std*std));
8 }
9  
10 /* Counters are as follows
11 l: lines
12 i: columns
13 c: filter coeffs
14 ch: channels
15 pc: coeff equalization
16 */
17  
18  
19  
20 int
21 3 i_gaussian(i_img *im, double stddev) {
22 3 return i_gaussian2( im, stddev, stddev );
23 }
24  
25 typedef struct s_gauss_coeff {
26 int diameter;
27 int radius;
28 double *coeff;
29 } t_gauss_coeff;
30  
31
32 10 static t_gauss_coeff *build_coeff( i_img *im, double stddev ) {
33 10 double *coeff = NULL;
34 double pc;
35 int radius, diameter, i;
36 10 t_gauss_coeff *ret = mymalloc(sizeof(struct s_gauss_coeff));
37 10 ret->coeff = NULL;
38  
39 10 ▶ if (im->bits <= 8)
Branch: 1 branch
BranchTrueFalseTotal
if (im->bits <= 8) 5 5 10
40 5 radius = ceil(2 * stddev);
41 else
42 5 radius = ceil(3 * stddev);
43  
44 10 diameter = 1 + radius * 2;
45  
46 10 coeff = mymalloc(sizeof(double) * diameter);
47  
48 111 ▶ for(i=0;i <= radius;i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i <= radius;i++) 101 10 111
49 101 coeff[radius + i]=coeff[radius - i]=gauss(i, stddev);
50 10 pc=0.0;
51 202 ▶ for(i=0; i < diameter; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i < diameter; i++) 192 10 202
52 192 pc+=coeff[i];
53 202 ▶ for(i=0;i < diameter;i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i < diameter;i++) { 192 10 202
54 192 coeff[i] /= pc;
55 // im_log((aIMCTX, 1, "i_gaussian2 Y i=%i coeff=%.2f\n", i, coeff[i] ));
56 }
57  
58 10 ret->diameter = diameter;
59 10 ret->radius = radius;
60 10 ret->coeff = coeff;
61 10 return ret;
62 }
63  
64 10 static void free_coeff(t_gauss_coeff *co ) {
65  
66 10 ▶ if( co->coeff != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( co->coeff != NULL ) 10 0 10
67 10 myfree( co->coeff );
68 10 myfree( co );
69 10 }
70  
71 #define img_copy(dest, src) i_copyto( (dest), (src), 0,0, (src)->xsize,(src)->ysize, 0,0);
72  
73  
74  
75 int
76 8 i_gaussian2(i_img *im, double stddevX, double stddevY) {
77 int c, ch;
78 i_img_dim x, y;
79 double pc;
80 8 t_gauss_coeff *co = NULL;
81 double res[MAXCHANNELS];
82 i_img *timg;
83 i_img *yin;
84 i_img *yout;
85 8 dIMCTXim(im);
86  
87 8 im_log((aIMCTX, 1,"i_gaussian2(im %p, stddev %.2f,%.2f)\n",im,stddevX,stddevY));
88 8 i_clear_error();
89  
90 8 ▶ if (stddevX < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stddevX < 0) { 0 8 8
91 0 i_push_error(0, "stddevX must be positive");
92 0 return 0;
93 }
94 8 ▶ if (stddevY < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stddevY < 0) { 0 8 8
95 0 i_push_error(0, "stddevY must be positive");
96 0 return 0;
97 }
98  
99 8 ▶ if( stddevX == stddevY && stddevY == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if( stddevX == stddevY && stddevY == 0 ) { 4 4 8
if( stddevX == stddevY && stddevY == 0 ) { 0 4 4
100 0 i_push_error(0, "stddevX or stddevY must be positive");
101 0 return 0;
102 }
103  
104  
105 /* totally silly cutoff */
106 8 ▶ if (stddevX > 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if (stddevX > 1000) { 0 8 8
107 0 stddevX = 1000;
108 }
109 8 ▶ if (stddevY > 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if (stddevY > 1000) { 0 8 8
110 0 stddevY = 1000;
111 }
112  
113 8 timg = i_sametype(im, im->xsize, im->ysize);
114 8 ▶ if (!timg)
Branch: 1 branch
BranchTrueFalseTotal
if (!timg) 0 8 8
115 0 return 0;
116  
117 8 ▶ if( stddevX > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stddevX > 0 ) { 7 1 8
118 /* Build Y coefficient matrix */
119 7 co = build_coeff( im, stddevX );
120 7 im_log((aIMCTX, 1, "i_gaussian2 X coeff radius=%i diamter=%i coeff=%p\n", co->radius, co->diameter, co->coeff));
121  
122 /******************/
123 /* Process X blur */
124 7 im_log((aIMCTX, 1, "i_gaussian2 X blur from im=%p to timg=%p\n", im, timg));
125  
126 7 ▶#code im->bits <= 8
Branch: 1 branch
BranchTrueFalseTotal
#code im->bits <= 8 4 3 7
127 IM_COLOR rcolor;
128  
129 1057 ▶ for(y = 0; y < im->ysize; y++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) { 600 4 604
for(y = 0; y < im->ysize; y++) { 450 3 453
130 158550 ▶ for(x = 0; x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(x = 0; x < im->xsize; x++) { 90000 600 90600
for(x = 0; x < im->xsize; x++) { 67500 450 67950
131 157500 pc=0.0;
132 630000 ▶ for(ch=0;ch<im->channels;ch++)
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) 270000 90000 360000
for(ch=0;ch<im->channels;ch++) 202500 67500 270000
133 472500 res[ch]=0;
134 3240000 ▶ for(c = 0;c < co->diameter; c++)
Branch: 2 branches
BranchTrueFalseTotal
for(c = 0;c < co->diameter; c++) 1620000 90000 1710000
for(c = 0;c < co->diameter; c++) 1462500 67500 1530000
135 3082500 ▶ if (IM_GPIX(im,x+c-co->radius,y,&rcolor)!=-1) {
Branch: 2 branches
BranchTrueFalseTotal
if (IM_GPIX(im,x+c-co->radius,y,&rcolor)!=-1) { 1567500 52500 1620000
if (IM_GPIX(im,x+c-co->radius,y,&rcolor)!=-1) { 1390200 72300 1462500
136 11830800 ▶ for(ch=0;ch<im->channels;ch++)
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) 4702500 1567500 6270000
for(ch=0;ch<im->channels;ch++) 4170600 1390200 5560800
137 8873100 res[ch]+= rcolor.channel[ch] * co->coeff[c];
138 2957700 pc+=co->coeff[c];
139 }
140 630000 ▶ for(ch=0;ch<im->channels;ch++) {
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<im->channels;ch++) { 270000 90000 360000
for(ch=0;ch<im->channels;ch++) { 202500 67500 270000
141 472500 double value = res[ch] / pc;
142 472500 ▶ rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value);
Branch: 2 branches
BranchTrueFalseTotal
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value); 490 269510 270000
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value); 0 202500 202500
143 }
144 157500 IM_PPIX(timg, x, y, &rcolor);
145 }
146 }
147 #/code
148 /* processing is im -> timg=yin -> im=yout */
149 7 yin = timg;
150 7 yout = im;
151 }
152 else {
153 1 im_log((aIMCTX, 1, "i_gaussian2 X coeff is unity\n"));
154  
155 /* processing is im=yin -> timg=yout -> im */
156 1 yin = im;
157 1 yout = timg;
158 }
159
160 8 ▶ if( stddevY > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stddevY > 0 ) { 7 1 8
161 7 ▶ if( stddevX != stddevY ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stddevX != stddevY ) { 3 4 7
162 3 ▶ if( co != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if( co != NULL ) { 2 1 3
163 2 free_coeff(co);
164 2 co = NULL;
165 }
166  
167 /* Build Y coefficient matrix */
168 3 co = build_coeff( im, stddevY );
169 3 im_log((aIMCTX, 1, "i_gaussian2 Y coeff radius=%i diamter=%i coeff=%p\n", co->radius, co->diameter, co->coeff));
170 }
171  
172 /******************/
173 /* Process Y blur */
174 7 im_log((aIMCTX, 1, "i_gaussian2 Y blur from yin=%p to yout=%p\n", yin, yout));
175 7 ▶#code im->bits <= 8
Branch: 1 branch
BranchTrueFalseTotal
#code im->bits <= 8 4 3 7
176 IM_COLOR rcolor;
177 1057 ▶ for(x = 0;x < im->xsize; x++) {
Branch: 2 branches
BranchTrueFalseTotal
for(x = 0;x < im->xsize; x++) { 600 4 604
for(x = 0;x < im->xsize; x++) { 450 3 453
178 158550 ▶ for(y = 0; y < im->ysize; y++) {
Branch: 2 branches
BranchTrueFalseTotal
for(y = 0; y < im->ysize; y++) { 90000 600 90600
for(y = 0; y < im->ysize; y++) { 67500 450 67950
179 157500 pc=0.0;
180 630000 ▶ for(ch=0; ch<im->channels; ch++)
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0; ch<im->channels; ch++) 270000 90000 360000
for(ch=0; ch<im->channels; ch++) 202500 67500 270000
181 472500 res[ch]=0;
182 3240000 ▶ for(c=0; c < co->diameter; c++)
Branch: 2 branches
BranchTrueFalseTotal
for(c=0; c < co->diameter; c++) 1620000 90000 1710000
for(c=0; c < co->diameter; c++) 1462500 67500 1530000
183 3082500 ▶ if (IM_GPIX(yin, x, y+c-co->radius, &rcolor)!=-1) {
Branch: 2 branches
BranchTrueFalseTotal
if (IM_GPIX(yin, x, y+c-co->radius, &rcolor)!=-1) { 1567500 52500 1620000
if (IM_GPIX(yin, x, y+c-co->radius, &rcolor)!=-1) { 1390200 72300 1462500
184 11830800 ▶ for(ch=0;ch<yin->channels;ch++)
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<yin->channels;ch++) 4702500 1567500 6270000
for(ch=0;ch<yin->channels;ch++) 4170600 1390200 5560800
185 8873100 res[ch]+= rcolor.channel[ch] * co->coeff[c];
186 2957700 pc+=co->coeff[c];
187 }
188 630000 ▶ for(ch=0;ch<yin->channels;ch++) {
Branch: 2 branches
BranchTrueFalseTotal
for(ch=0;ch<yin->channels;ch++) { 270000 90000 360000
for(ch=0;ch<yin->channels;ch++) { 202500 67500 270000
189 472500 double value = res[ch]/pc;
190 472500 ▶ rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value);
Branch: 2 branches
BranchTrueFalseTotal
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value); 2519 267481 270000
rcolor.channel[ch] = value > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : IM_ROUND(value); 0 202500 202500
191 }
192 157500 IM_PPIX(yout, x, y, &rcolor);
193 }
194 }
195 #/code
196 7 ▶ if( im != yout ) {
Branch: 1 branch
BranchTrueFalseTotal
if( im != yout ) { 1 6 7
197 1 im_log((aIMCTX, 1, "i_gaussian2 copying yout=%p to im=%p\n", yout, im));
198 1 img_copy( im, yout );
199 }
200 }
201 else {
202 1 im_log((aIMCTX, 1, "i_gaussian2 Y coeff is unity\n"));
203 1 ▶ if( yin == timg ) {
Branch: 1 branch
BranchTrueFalseTotal
if( yin == timg ) { 1 0 1
204 1 im_log((aIMCTX, 1, "i_gaussian2 copying timg=%p to im=%p\n", timg, im));
205 1 img_copy( im, timg );
206 }
207 }
208  
209 8 im_log((aIMCTX, 1, "i_gaussian2 im=%p\n", im));
210 8 im_log((aIMCTX, 1, "i_gaussian2 timg=%p\n", timg));
211 8 im_log((aIMCTX, 1, "i_gaussian2 yin=%p\n", yin));
212 8 im_log((aIMCTX, 1, "i_gaussian2 yout=%p\n", yout));
213  
214 8 ▶ if( co != NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( co != NULL ) 8 0 8
215 8 free_coeff(co);
216  
217 8 i_img_destroy(timg);
218
219 8 return 1;
220 }
221  
222  
223  
224  
225  
226  
227  
« color.c ↑ Summary compose.im »