]> git.imager.perl.org - imager.git/blob - conv.c
read failures of pnm files now report low-level errors
[imager.git] / conv.c
1 #include "image.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,float *coeff,int len) {
16   int i,l,c,ch,center;
17   float pc;
18   i_color rcolor;
19   float res[11];
20   i_img timg;
21
22   mm_log((1,"i_conv(im* 0x%x,coeff 0x%x,len %d)\n",im,coeff,len));
23  
24   i_img_empty_ch(&timg,im->xsize,im->ysize,im->channels);
25
26   center=(len-1)/2;
27
28
29   for(l=0;l<im->ysize;l++) {
30     for(i=0;i<im->xsize;i++) {
31       pc=0.0;
32       for(ch=0;ch<im->channels;ch++) res[ch]=0;
33       for(c=0;c<len;c++)
34         if (i_gpix(im,i+c-center,l,&rcolor)!=-1) {
35           for(ch=0;ch<im->channels;ch++) res[ch]+=(float)(rcolor.channel[ch])*coeff[c];
36           pc+=coeff[c];
37         }
38       for(ch=0;ch<im->channels;ch++) rcolor.channel[ch]=(unsigned char)(((res[ch]/pc>255.0)?255.0:res[ch]/pc));
39       i_ppix(&timg,i,l,&rcolor);
40     }
41   }
42
43   for(l=0;l<im->xsize;l++)
44     {
45       for(i=0;i<im->ysize;i++)
46         {
47           pc=0.0;
48           for(ch=0;ch<im->channels;ch++) res[ch]=0;
49           for(c=0;c<len;c++)
50             if (i_gpix(&timg,l,i+c-center,&rcolor)!=-1)
51               {
52                 for(ch=0;ch<im->channels;ch++) res[ch]+=(float)(rcolor.channel[ch])*coeff[c];
53                 pc+=coeff[c];
54               }
55           for(ch=0;ch<im->channels;ch++) rcolor.channel[ch]=(unsigned char)(((res[ch]/(float)(pc)>255.0)?255.0:res[ch]/(float)(pc)));
56           i_ppix(im,l,i,&rcolor);
57         }
58     }
59 }
60
61
62
63
64
65
66
67
68