]> git.imager.perl.org - imager.git/blobdiff - conv.im
avoid a possible sign-extension for offsets/sizes in SGI
[imager.git] / conv.im
diff --git a/conv.im b/conv.im
index 986bbae74b8625e75d4ddd2d4da6a63b30047e1f..ced813ea75ffe500ebfcddbd4193f046cd2e36c4 100644 (file)
--- a/conv.im
+++ b/conv.im
+#define IMAGER_NO_CONTEXT
 #include "imager.h"
+#include "imageri.h"
 
 /*
   General convolution for 2d decoupled filters
   end effects are acounted for by increasing
   scaling the result with the sum of used coefficients
 
-  coeff: (float array) coefficients for filter
+  coeff: (double array) coefficients for filter
     len: length of filter.. number of coefficients
            note that this has to be an odd number
            (since the filter is even);
 */
 
-void
-i_conv(i_img *im,const float *coeff,int len) {
-  int i,l,c,ch,center;
+int
+i_conv(i_img *im, const double *coeff,int len) {
+  i_img_dim xo, yo; /* output pixel co-ordinate */
+  int c, ch, center;
   double pc;
-  double res[11];
+  double res[MAXCHANNELS];
   i_img *timg;
+  dIMCTXim(im);
 
-  mm_log((1,"i_conv(im %p, coeff %p, len %d)\n",im,coeff,len));
-  timg = i_sametype(im, im->xsize, im->ysize);
+  im_log((aIMCTX,1,"i_conv(im %p, coeff %p, len %d)\n",im,coeff,len));
+  im_clear_error(aIMCTX);
 
+  if (len < 1) {
+    im_push_error(aIMCTX, 0, "there must be at least one coefficient");
+    return 0;
+  }
   center=(len-1)/2;
 
+  pc = 0;
+  for (c = 0; c < len; ++c)
+    pc += coeff[c];
+
+  if (pc == 0) {
+    i_push_error(0, "sum of coefficients is zero");
+    return 0;
+  }
+
+  timg = i_sametype(im, im->xsize, im->ysize);
+
 #code im->bits <= 8
   IM_COLOR rcolor;
   /* don't move the calculation of pc up here, it depends on which pixels
      are readable */
-  for(l=0;l<im->ysize;l++) {
-    for(i=0;i<im->xsize;i++) {
-      pc=0.0;
-      for(ch=0;ch<im->channels;ch++) 
-       res[ch]=0;
-      for(c=0;c<len;c++)
-       if (IM_GPIX(im,i+c-center,l,&rcolor)!=-1) {
-         for(ch=0;ch<im->channels;ch++) 
-            res[ch] += (rcolor.channel[ch])*coeff[c];
-         pc+=coeff[c];
+  for(yo = 0; yo < im->ysize; yo++) {
+    for(xo = 0; xo < im->xsize; xo++) {
+      for(ch = 0;ch < im->channels; ch++) 
+       res[ch] = 0;
+      for(c = 0;c < len; c++) {
+       i_img_dim xi = xo + c - center;
+       if (xi < 0)
+         xi = 0;
+       else if (xi >= im->xsize)
+         xi = im->xsize - 1;
+
+       if (IM_GPIX(im, xi, yo, &rcolor)!=-1) {
+         for(ch = 0; ch < im->channels; ch++) 
+            res[ch] += (rcolor.channel[ch])  *coeff[c];
        }
-      for(ch=0;ch<im->channels;ch++) {
-        double temp = res[ch]/pc;
+      }
+      im_assert(pc != 0);
+      for(ch = 0; ch < im->channels; ch++) {
+        double temp = res[ch] / pc;
         rcolor.channel[ch] = 
           temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
       }
-      IM_PPIX(timg,i,l,&rcolor);
+      IM_PPIX(timg, xo, yo, &rcolor);
     }
   }
 
-  for(l=0;l<im->xsize;l++) {
-    for(i=0;i<im->ysize;i++) {
-      pc=0.0;  
-      for(ch=0;ch<im->channels;ch++) res[ch]=0;
-      for(c=0;c<len;c++) {
-       if (IM_GPIX(timg,l,i+c-center,&rcolor)!=-1) {
-         for(ch=0;ch<im->channels;ch++) 
-           res[ch] += (rcolor.channel[ch])*coeff[c];
-         pc+=coeff[c];
+  for(xo = 0; xo < im->xsize; xo++) {
+    for(yo = 0;yo < im->ysize; yo++) {
+      for(ch =  0; ch < im->channels; ch++)
+       res[ch] = 0;
+      for(c = 0; c < len; c++) {
+       i_img_dim yi = yo + c - center;
+       if (yi < 0)
+         yi = 0;
+       else if (yi >= im->ysize)
+         yi = im->ysize - 1;
+       if (IM_GPIX(timg, xo, yi, &rcolor) != -1) {
+         for(ch = 0;ch < im->channels; ch++) 
+           res[ch] += (rcolor.channel[ch]) * coeff[c];
        }
       }
-      for(ch=0;ch<im->channels;ch++) {
-       double temp = res[ch]/pc;
-       rcolor.channel[ch]= 
+      im_assert(pc != 0);
+      for(ch = 0;ch < im->channels; ch++) {
+       double temp = res[ch] / pc;
+       rcolor.channel[ch] = 
          temp < 0 ? 0 : temp > IM_SAMPLE_MAX ? IM_SAMPLE_MAX : (IM_SAMPLE_T)temp;
       }
-      IM_PPIX(im,l,i,&rcolor);
+      IM_PPIX(im, xo, yo,&rcolor);
     }
   }
 #/code
 
   i_img_destroy(timg);
-}
-
-
-
-
-
-
-
-
 
+  return 1;
+}