]> git.imager.perl.org - imager.git/blobdiff - rubthru.im
extend some variable types to avoid overflows for mediancut
[imager.git] / rubthru.im
index 4c3b08e77727da97e053db43acf3115f55c0af1e..514ec09c3ec4f9985ed9b6c148d557889e6c02ef 100644 (file)
@@ -2,13 +2,13 @@
 
 static int
 rubthru_targ_noalpha(i_img *im, i_img *src,
-                     int tx, int ty, 
-                     int src_minx, int src_miny,
-                     int src_maxx, int src_maxy) {
-  int x, y, ttx, tty;
+                     i_img_dim tx, i_img_dim ty, 
+                     i_img_dim src_minx, i_img_dim src_miny,
+                     i_img_dim src_maxx, i_img_dim src_maxy) {
+  i_img_dim x, y, tty;
   int alphachan;
   int ch;
-  int width = src_maxx - src_minx;
+  i_img_dim width = src_maxx - src_minx;
   int want_channels;
 
   i_clear_error();
@@ -37,7 +37,6 @@ rubthru_targ_noalpha(i_img *im, i_img *src,
   for(y = src_miny; y < src_maxy; y++) {
     IM_COLOR *srcp = src_line;
     IM_COLOR *destp = dest_line;
-    ttx = tx;
     IM_GLIN(src, src_minx, src_maxx, y, src_line);
     IM_GLIN(im, tx, tx + width, tty, dest_line);
     if (src->channels != want_channels)
@@ -64,15 +63,15 @@ rubthru_targ_noalpha(i_img *im, i_img *src,
 }
 
 static int
-rubthru_targ_alpha(i_img *im, i_img *src, int tx, int ty, 
-                   int src_minx, int src_miny,
-                   int src_maxx, int src_maxy) {
-  int x, y, ttx, tty;
+rubthru_targ_alpha(i_img *im, i_img *src, i_img_dim tx, i_img_dim ty, 
+                   i_img_dim src_minx, i_img_dim src_miny,
+                   i_img_dim src_maxx, i_img_dim src_maxy) {
+  i_img_dim x, y, ttx, tty;
   int want_channels;
   int alphachan;
   int ch;
   int targ_alpha_chan;
-  int width = src_maxx - src_minx;
+  i_img_dim width = src_maxx - src_minx;
   
   if (im->channels == 4 && (src->channels == 4 || src->channels == 2)) {
     alphachan = 3;
@@ -98,7 +97,7 @@ rubthru_targ_alpha(i_img *im, i_img *src, int tx, int ty,
 
   tty = ty;
   for(y = src_miny; y < src_maxy; y++) {
-    int min_x, max_x;
+    i_img_dim min_x, max_x;
     IM_COLOR *srcp = src_line;
     IM_COLOR *destp = dest_line;
     IM_GLIN(src, src_minx, src_maxx, y, src_line);
@@ -111,17 +110,17 @@ rubthru_targ_alpha(i_img *im, i_img *src, int tx, int ty,
       ++min_x;
       ++srcp;
     }
-    while (max_x > min_x && src_line[max_x-1].channel[alphachan] == 0) {
+    while (max_x > min_x && src_line[max_x-1 - src_minx].channel[alphachan] == 0) {
       --max_x;
     }
 
     if (max_x > min_x) {
-      int work_left = tx + min_x - src_minx;
-      int work_width = max_x - min_x;
+      i_img_dim work_left = tx + min_x - src_minx;
+      i_img_dim work_width = max_x - min_x;
       ttx = work_left;
       IM_GLIN(im, work_left, work_left + work_width, tty, dest_line);
       
-      for(x = src_minx; x < src_maxx; x++) {
+      for(x = min_x; x < max_x; x++) {
        src_alpha = srcp->channel[alphachan];
        if (src_alpha) {
          remains = IM_SAMPLE_MAX - src_alpha;
@@ -153,24 +152,24 @@ rubthru_targ_alpha(i_img *im, i_img *src, int tx, int ty,
 }
 
 /*
-=item i_rubthru(im, src, tx, ty, src_minx, src_miny, src_maxx, src_maxy )
+=item i_rubthru(C<im>, C<src>, C<tx>, C<ty>, C<src_minx>, C<src_miny>, C<src_maxx>, C<src_maxy>)
 
 =category Image
 
-Takes the sub image I<src[src_minx, src_maxx)[src_miny, src_maxy)> and
-overlays it at (I<tx>,I<ty>) on the image object.
+Takes the sub image C<src>[C<src_minx>, C<src_maxx>)[C<src_miny>, C<src_maxy>)> and
+overlays it at (C<tx>,C<ty>) on the image object.
 
-The alpha channel of each pixel in I<src> is used to control how much
-the existing colour in I<im> is replaced, if it is 255 then the colour
-is completely replaced, if it is 0 then the original colour is left 
+The alpha channel of each pixel in C<src> is used to control how much
+the existing color in C<im> is replaced, if it is 255 then the color
+is completely replaced, if it is 0 then the original color is left
 unmodified.
 
 =cut
 */
 
 int
-i_rubthru(i_img *im, i_img *src, int tx, int ty, int src_minx, int src_miny,
-         int src_maxx, int src_maxy) {
+i_rubthru(i_img *im, i_img *src, i_img_dim tx, i_img_dim ty, i_img_dim src_minx, i_img_dim src_miny,
+         i_img_dim src_maxx, i_img_dim src_maxy) {
   if (src_minx < 0) {
     tx -= src_minx;
     src_minx = 0;
@@ -196,8 +195,9 @@ i_rubthru(i_img *im, i_img *src, int tx, int ty, int src_minx, int src_miny,
   if (tx >= im->xsize || ty >= im->ysize
       || src_minx >= src_maxx || src_miny >= src_maxy) {
     i_clear_error();
-    i_push_error(0, "rubthrough: nothing to do");
-    return 0;
+    /* just do nothing, attempting to rubthrough outside the target isn't
+       worth being an error */
+    return 1;
   }
 
   if (im->channels == 1 || im->channels == 3)