Skip to content
Newer
Older
100644 106 lines (91 sloc) 2.52 KB
f77774f @tonycoz update conv.im to IMAGER_NO_CONTEXT
authored
1 #define IMAGER_NO_CONTEXT
92bda63 - start of external Imager API access:
Tony Cook authored
2 #include "imager.h"
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
3 #include "imageri.h"
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
4
5 /*
6 General convolution for 2d decoupled filters
7 end effects are acounted for by increasing
8 scaling the result with the sum of used coefficients
9
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored
10 coeff: (double array) coefficients for filter
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
11 len: length of filter.. number of coefficients
12 note that this has to be an odd number
13 (since the filter is even);
14 */
15
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
16 int
17 i_conv(i_img *im, const double *coeff,int len) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored
18 i_img_dim xo, yo; /* output pixel co-ordinate */
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
19 int c, ch, center;
bd8052a merge in tiff re-work branch
Tony Cook authored
20 double pc;
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
21 double res[MAXCHANNELS];
bd8052a merge in tiff re-work branch
Tony Cook authored
22 i_img *timg;
f77774f @tonycoz update conv.im to IMAGER_NO_CONTEXT
authored
23 dIMCTXim(im);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
24
f77774f @tonycoz update conv.im to IMAGER_NO_CONTEXT
authored
25 im_log((aIMCTX,1,"i_conv(im %p, coeff %p, len %d)\n",im,coeff,len));
26 im_clear_error(aIMCTX);
1e0418f @tonycoz [rt #68993] check the number of conv coefficients is positive
authored
27
28 if (len < 1) {
f77774f @tonycoz update conv.im to IMAGER_NO_CONTEXT
authored
29 im_push_error(aIMCTX, 0, "there must be at least one coefficient");
1e0418f @tonycoz [rt #68993] check the number of conv coefficients is positive
authored
30 return 0;
31 }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
32
33 center=(len-1)/2;
34
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
35 pc = 0;
36 for (c = 0; c < len; ++c)
37 pc += coeff[c];
38
39 if (pc == 0) {
40 i_push_error(0, "sum of coefficients is zero");
41 return 0;
42 }
43
44 timg = i_sametype(im, im->xsize, im->ysize);
45
bd8052a merge in tiff re-work branch
Tony Cook authored
46 #code im->bits <= 8
47 IM_COLOR rcolor;
6e473a5 revert the precalculation of the sum of the coefficients - it
Tony Cook authored
48 /* don't move the calculation of pc up here, it depends on which pixels
49 are readable */
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
50 for(yo = 0; yo < im->ysize; yo++) {
51 for(xo = 0; xo < im->xsize; xo++) {
52 for(ch = 0;ch < im->channels; ch++)
53 res[ch] = 0;
54 for(c = 0;c < len; c++) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored
55 i_img_dim xi = xo + c - center;
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
56 if (xi < 0)
57 xi = 0;
58 else if (xi >= im->xsize)
59 xi = im->xsize - 1;
60
61 if (IM_GPIX(im, xi, yo, &rcolor)!=-1) {
62 for(ch = 0; ch < im->channels; ch++)
63 res[ch] += (rcolor.channel[ch]) *coeff[c];
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
64 }
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
65 }
66 im_assert(pc != 0);
67 for(ch = 0; ch < im->channels; ch++) {
68 double temp = res[ch] / pc;
0b76fec clamp low end too
Tony Cook authored
69 rcolor.channel[ch] =
bd8052a merge in tiff re-work branch
Tony Cook authored
70 temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
0b76fec clamp low end too
Tony Cook authored
71 }
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
72 IM_PPIX(timg, xo, yo, &rcolor);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
73 }
74 }
75
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
76 for(xo = 0; xo < im->xsize; xo++) {
77 for(yo = 0;yo < im->ysize; yo++) {
78 for(ch = 0; ch < im->channels; ch++)
79 res[ch] = 0;
80 for(c = 0; c < len; c++) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored
81 i_img_dim yi = yo + c - center;
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
82 if (yi < 0)
83 yi = 0;
84 else if (yi >= im->ysize)
85 yi = im->ysize - 1;
86 if (IM_GPIX(timg, xo, yi, &rcolor) != -1) {
87 for(ch = 0;ch < im->channels; ch++)
88 res[ch] += (rcolor.channel[ch]) * coeff[c];
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
89 }
bd8052a merge in tiff re-work branch
Tony Cook authored
90 }
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
91 im_assert(pc != 0);
92 for(ch = 0;ch < im->channels; ch++) {
93 double temp = res[ch] / pc;
94 rcolor.channel[ch] =
bd8052a merge in tiff re-work branch
Tony Cook authored
95 temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
96 }
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
97 IM_PPIX(im, xo, yo,&rcolor);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
98 }
bd8052a merge in tiff re-work branch
Tony Cook authored
99 }
100 #/code
101
102 i_img_destroy(timg);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored
103
6a3cbae - the conv filter now enforces that the sum of the coefficients is
Tony Cook authored
104 return 1;
105 }
Something went wrong with that request. Please try again.