0.70 release
[imager.git] / conv.im
CommitLineData
92bda632 1#include "imager.h"
02d1d628
AMH
2
3/*
4 General convolution for 2d decoupled filters
5 end effects are acounted for by increasing
6 scaling the result with the sum of used coefficients
7
8 coeff: (float array) coefficients for filter
9 len: length of filter.. number of coefficients
10 note that this has to be an odd number
11 (since the filter is even);
12*/
13
14void
97ac0a96 15i_conv(i_img *im,const float *coeff,int len) {
02d1d628 16 int i,l,c,ch,center;
bd8052a6
TC
17 double pc;
18 double res[11];
19 i_img *timg;
02d1d628 20
a73aeb5f 21 mm_log((1,"i_conv(im %p, coeff %p, len %d)\n",im,coeff,len));
02d1d628 22
bd8052a6 23 timg = i_sametype(im, im->xsize, im->ysize);
02d1d628
AMH
24
25 center=(len-1)/2;
26
bd8052a6
TC
27#code im->bits <= 8
28 IM_COLOR rcolor;
6e473a59
TC
29 /* don't move the calculation of pc up here, it depends on which pixels
30 are readable */
02d1d628
AMH
31 for(l=0;l<im->ysize;l++) {
32 for(i=0;i<im->xsize;i++) {
6e473a59 33 pc=0.0;
bd8052a6
TC
34 for(ch=0;ch<im->channels;ch++)
35 res[ch]=0;
02d1d628 36 for(c=0;c<len;c++)
bd8052a6 37 if (IM_GPIX(im,i+c-center,l,&rcolor)!=-1) {
0b76fec8 38 for(ch=0;ch<im->channels;ch++)
bd8052a6 39 res[ch] += (rcolor.channel[ch])*coeff[c];
6e473a59 40 pc+=coeff[c];
02d1d628 41 }
0b76fec8
TC
42 for(ch=0;ch<im->channels;ch++) {
43 double temp = res[ch]/pc;
44 rcolor.channel[ch] =
bd8052a6 45 temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
0b76fec8 46 }
bd8052a6 47 IM_PPIX(timg,i,l,&rcolor);
02d1d628
AMH
48 }
49 }
50
bd8052a6
TC
51 for(l=0;l<im->xsize;l++) {
52 for(i=0;i<im->ysize;i++) {
53 pc=0.0;
54 for(ch=0;ch<im->channels;ch++) res[ch]=0;
55 for(c=0;c<len;c++) {
56 if (IM_GPIX(timg,l,i+c-center,&rcolor)!=-1) {
57 for(ch=0;ch<im->channels;ch++)
58 res[ch] += (rcolor.channel[ch])*coeff[c];
59 pc+=coeff[c];
02d1d628 60 }
bd8052a6
TC
61 }
62 for(ch=0;ch<im->channels;ch++) {
63 double temp = res[ch]/pc;
64 rcolor.channel[ch]=
65 temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
66 }
67 IM_PPIX(im,l,i,&rcolor);
02d1d628 68 }
bd8052a6
TC
69 }
70#/code
71
72 i_img_destroy(timg);
02d1d628
AMH
73}
74
75
76
77
78
79
80
81
82