]> git.imager.perl.org - imager.git/blob - conv.im
report library version numbers where we already have the XS for it
[imager.git] / conv.im
1 #include "imager.h"
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
14 void
15 i_conv(i_img *im,const float *coeff,int len) {
16   int i,l,c,ch,center;
17   double pc;
18   double res[11];
19   i_img *timg;
20
21   mm_log((1,"i_conv(im %p, coeff %p, len %d)\n",im,coeff,len));
22  
23   timg = i_sametype(im, im->xsize, im->ysize);
24
25   center=(len-1)/2;
26
27 #code im->bits <= 8
28   IM_COLOR rcolor;
29   /* don't move the calculation of pc up here, it depends on which pixels
30      are readable */
31   for(l=0;l<im->ysize;l++) {
32     for(i=0;i<im->xsize;i++) {
33       pc=0.0;
34       for(ch=0;ch<im->channels;ch++) 
35         res[ch]=0;
36       for(c=0;c<len;c++)
37         if (IM_GPIX(im,i+c-center,l,&rcolor)!=-1) {
38           for(ch=0;ch<im->channels;ch++) 
39             res[ch] += (rcolor.channel[ch])*coeff[c];
40           pc+=coeff[c];
41         }
42       for(ch=0;ch<im->channels;ch++) {
43         double temp = res[ch]/pc;
44         rcolor.channel[ch] = 
45           temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
46       }
47       IM_PPIX(timg,i,l,&rcolor);
48     }
49   }
50
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];
60         }
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);
68     }
69   }
70 #/code
71
72   i_img_destroy(timg);
73 }
74
75
76
77
78
79
80
81
82