]> git.imager.perl.org - imager.git/commitdiff
Added checks to flood fill functions for seed pixel being outside of
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Tue, 30 Apr 2002 00:35:13 +0000 (00:35 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Tue, 30 Apr 2002 00:35:13 +0000 (00:35 +0000)
the image.

Imager.pm
Imager.xs
draw.c
image.h

index bd0deca96f625432925f2708ff21aa65ee89275f..6369ce7d04d788bf412ad5a33b490f0c5f73b1f4 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -2161,7 +2161,7 @@ sub polybezier {
 sub flood_fill {
   my $self = shift;
   my %opts = ( color=>Imager::Color->new(255, 255, 255), @_ );
-
+       my $rc;
   unless (exists $opts{'x'} && exists $opts{'y'}) {
     $self->{ERRSTR} = "missing seed x and y parameters";
     return undef;
@@ -2176,7 +2176,7 @@ sub flood_fill {
         return;
       }
     }
-    i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill});
+    $rc = i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill});
   }
   else {
     my $color = _color($opts{'color'});
@@ -2184,10 +2184,9 @@ sub flood_fill {
       $self->{ERRSTR} = $Imager::ERRSTR; 
       return; 
     }
-    i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color);
+    $rc = i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color);
   }
-
-  $self;
+       if ($rc) { $self; } else { $self->{ERRSTR} = $self->_error_as_msg(); return (); }
 }
 
 sub setpixel {
index 6051ea3e87e7aa27cacf30db67940d279a1f5949..a89349b8788795857efd99e84196c41a4b591c48 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -1429,14 +1429,14 @@ i_poly_aa_cfill(im,xc,yc,fill)
 
 
 
-void
+undef_int
 i_flood_fill(im,seedx,seedy,dcol)
     Imager::ImgRaw     im
               int     seedx
               int     seedy
      Imager::Color     dcol
 
-void
+undef_int
 i_flood_cfill(im,seedx,seedy,fill)
     Imager::ImgRaw     im
               int     seedx
diff --git a/draw.c b/draw.c
index c57a2d437cfe7066ed6a28773aea3eeaf39bcc93..a4a244faef2190e9810dd99cf9e5091f12bcac41 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -686,8 +686,8 @@ i_rspan(i_img *im,int seedx,int seedy,i_color *val) {
 
 #define INSIDE(x,y) ((!btm_test(btm,x,y) && ( i_gpix(im,x,y,&cval),i_ccomp(&val,&cval,channels)  ) ))
 
-void
-i_flood_fill(i_img *im,int seedx,int seedy,i_color *dcol) {
+undef_int
+i_flood_fill(i_img *im, int seedx, int seedy, i_color *dcol) {
 
   int lx,rx;
   int y;
@@ -711,6 +711,14 @@ i_flood_fill(i_img *im,int seedx,int seedy,i_color *dcol) {
   xsize    = im->xsize;
   ysize    = im->ysize;
 
+  if (seedx < 0 || seedx >= xsize ||
+      seedy < 0 || seedy >= ysize) {
+    
+    i_push_error(0, "Seed pixel outside of image");
+    return 0;
+  }
+      
+
   btm = btm_new(xsize,ysize);
   st = llist_new(100,sizeof(struct stack_element*));
 
@@ -812,6 +820,7 @@ i_flood_fill(i_img *im,int seedx,int seedy,i_color *dcol) {
   btm_destroy(btm);
   mm_log((1, "DESTROY\n"));
   llist_destroy(st);
+  return 1;
 }
 
 static struct i_bitmap *
@@ -945,13 +954,21 @@ i_flood_fill_low(i_img *im,int seedx,int seedy,
   return btm;
 }
 
-void
+undef_int
 i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
   int bxmin, bxmax, bymin, bymax;
   struct i_bitmap *btm;
   int x, y;
   int start;
 
+  
+  if (seedx < 0 || seedx >= im->xsize ||
+      seedy < 0 || seedy >= im->ysize) {
+    i_push_error(0, "Seed pixel outside of image");
+    return 0;
+  }
+
+
   btm = i_flood_fill_low(im, seedx, seedy, &bxmin, &bxmax, &bymin, &bymax);
 
   if (im->bits == i_8_bits && fill->fill_with_color) {
@@ -1026,4 +1043,5 @@ i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill) {
   }
 
   btm_destroy(btm);
+  return 1;
 }
diff --git a/image.h b/image.h
index a918ca51791960d2adbc925b1876253ed652fa3b..a02f5faebb084a838e3135a6494145d7be4b5f1a 100644 (file)
--- a/image.h
+++ b/image.h
@@ -222,7 +222,9 @@ void i_bezier_multi(i_img *im,int l,double *x,double *y,i_color *val);
 void i_poly_aa     (i_img *im,int l,double *x,double *y,i_color *val);
 void i_poly_aa_cfill(i_img *im,int l,double *x,double *y,i_fill_t *fill);
 
-void i_flood_fill  (i_img *im,int seedx,int seedy,i_color *dcol);
+undef_int i_flood_fill  (i_img *im,int seedx,int seedy,i_color *dcol);
+undef_int i_flood_cfill(i_img *im, int seedx, int seedy, i_fill_t *fill);
+
 
 /* image processing functions */