]> git.imager.perl.org - imager.git/blobdiff - filters.im
avoid a possible sign-extension for offsets/sizes in SGI
[imager.git] / filters.im
index d980fcb6bab19f49009c36ec2ce7009f5eb93ebd..7c23a5a56ab0fa4d5779c9cb58d03aa22f286d59 100644 (file)
@@ -696,34 +696,47 @@ i_autolevels_mono(i_img *im, float lsat, float usat) {
   }
   
   min_lum = 0;
-  max_lum = 255;
-
-  lower_accum = upper_accum = 0;
-  
-  for(i=0; i<256; i++) { 
-    lower_accum += hist[i];
+  lower_accum = 0;
+  for (i = 0; i < 256; ++i) {
     if (lower_accum < sum_lum * lsat)
       min_lum = i;
+    lower_accum += hist[i];
+  }
 
-    upper_accum  += hist[255-i];
+  max_lum = 255;
+  upper_accum = 0;
+  for(i = 255; i >= 0; i--) {
     if (upper_accum < sum_lum * usat)
-      max_lum = 255-i;
+      max_lum = i;
+    upper_accum  += hist[i];
   }
 
 #code im->bits <= 8
   IM_SAMPLE_T *srow = mymalloc(color_samples * sizeof(IM_SAMPLE_T));
 #ifdef IM_EIGHT_BIT
   IM_WORK_T low = min_lum;
+  i_sample_t lookup[256];
 #else
   IM_WORK_T low = min_lum / 255.0 * IM_SAMPLE_MAX;
 #endif
-  IM_WORK_T scale = 255.0 / (max_lum - min_lum);
+  double scale = 255.0 / (max_lum - min_lum);
+
+#ifdef IM_EIGHT_BIT
+  for (i = 0; i < 256; ++i) {
+    IM_WORK_T tmp = (i - low) * scale;
+    lookup[i] = IM_LIMIT(tmp);
+  }
+#endif
 
   for(y = 0; y < im->ysize; y++) {
     IM_GSAMP(im, 0, im->xsize, y, srow, NULL, color_channels);
     for(i = 0; i < color_samples; ++i) {
+#ifdef IM_EIGHT_BIT
+      srow[i] = lookup[srow[i]];
+#else
       IM_WORK_T tmp = (srow[i] - low) * scale;
       srow[i] = IM_LIMIT(tmp);
+#endif
     }
     IM_PSAMP(im, 0, im->xsize, y, srow, NULL, color_channels);
   }
@@ -1320,6 +1333,10 @@ i_nearest_color(i_img *im, int num, i_img_dim *xo, i_img_dim *yo, i_color *oval,
 
   i_nearest_color_foo(im, num, xo, yo, ival, dmeasure);
 
+  myfree(cmatch);
+  myfree(ival);
+  myfree(tval);
+
   return 1;
 }
 
@@ -1751,8 +1768,9 @@ i_fountain(i_img *im, double xa, double ya, double xb, double yb,
   line = mymalloc(line_bytes); /* checked 17feb2005 tonyc */
 
   i_get_combine(combine, &combine_func, &combinef_func);
-  if (combinef_func)
+  if (combinef_func) {
     work = mymalloc(line_bytes); /* checked 17feb2005 tonyc */
+  }
 
   fount_init_state(&state, xa, ya, xb, yb, type, repeat, combine, 
                    super_sample, ssample_param, count, segs);
@@ -1767,18 +1785,18 @@ i_fountain(i_img *im, double xa, double ya, double xb, double yb,
       else
         got_one = state.ssfunc(&c, x, y, &state);
       if (got_one) {
-        if (combine)
+        if (combinef_func)
           work[x] = c;
         else 
           line[x] = c;
       }
     }
-    if (combine)
+    if (combinef_func)
       combinef_func(line, work, im->channels, im->xsize);
     i_plinf(im, 0, im->xsize, y, line);
   }
   fount_finish_state(&state);
-  if (work) myfree(work);
+  myfree(work);
   myfree(line);
 
   return 1;
@@ -1954,11 +1972,13 @@ fount_init_state(struct fount_state *state, double xa, double ya,
   case i_fts_random:
   case i_fts_circle:
     ssample_param = floor(0.5+ssample_param);
-    bytes = sizeof(i_fcolor) * ssample_param;
-    if (bytes / sizeof(i_fcolor) == ssample_param) {
-      state->ssample_data = mymalloc(sizeof(i_fcolor) * ssample_param);
+    if (im_size_t_max / sizeof(i_fcolor) > ssample_param) {
+      bytes = sizeof(i_fcolor) * ssample_param;
+      state->ssample_data = mymalloc(bytes);
     }
     else {
+      dIMCTX;
+      im_log((aIMCTX, 1,"size_t overflow calculating super-sample array size for random or circl"));
       super_sample = i_fts_none;
     }
     break;