]> git.imager.perl.org - imager.git/commitdiff
Added flip() and docs to Imager.pm and i_flipxy() to image.{c,h}.
authorArnar Mar Hrafnkelsson <addi@cpan.org>
Fri, 4 May 2001 07:15:09 +0000 (07:15 +0000)
committerArnar Mar Hrafnkelsson <addi@cpan.org>
Fri, 4 May 2001 07:15:09 +0000 (07:15 +0000)
Changes
Imager.pm
Imager.xs
image.c
image.h

diff --git a/Changes b/Changes
index 77288c542fdb6238638f05a90617458c8ee045a6..6556d1ab7d6b6cd6a88467f3f713ef21c3cba5fa 100644 (file)
--- a/Changes
+++ b/Changes
@@ -379,7 +379,7 @@ Revision history for Perl extension Imager.
          Imager.xs, Imager.pm
         - each of the image formats now have their own test file,
           extracted from t10formats.t, usually with som extra tests
-
+  - Added flip() and docs to Imager.pm and i_flipxy to image.c.
 
 ~~~~~~~~~~~~~^ ^ ^~~~~~~~~~~~~~
 
index 2095b9a7a6c4c26a7098ae8e54e4a827804e7e88..df7c464c5d8b3e27bfab9d0d8d1b5cf09221117d 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -993,6 +993,18 @@ sub rubthrough {
 }
 
 
+sub flip {
+  my $self  = shift;
+  my %opts  = @_;
+  my %xlate = (x=>0, y=>1, xy=>2, yx=>2);
+  my $dir;
+  return () unless defined $opts{'dir'} and defined $xlate{$opts{'dir'}};
+  $dir = $xlate{$opts{'dir'}};
+  return $self if i_flipxy($self->{IMG}, $dir);
+  return ();
+}
+
+
 
 # These two are supported for legacy code only
 
@@ -1887,7 +1899,6 @@ the function return undef.  Examples:
 =head2 Drawing Methods
 
 IMPLEMENTATION MORE OR LESS DONE CHECK THE TESTS
-
 DOCUMENTATION OF THIS SECTION OUT OF SYNC
 
 It is possible to draw with graphics primitives onto images.  Such
@@ -1994,10 +2005,21 @@ To copy an image to onto another image use the C<paste()> method.
 That copies the entire C<$logo> image onto the C<$dest> image so that the
 upper left corner of the C<$logo> image is at (40,20).
 
-=head2 Blending Images
-To put an image or a part of an image directly into another it is
-best to call the C<paste()> method on the image you want to add to.
+
+=head2 Flipping images
+
+An inplace horizontal or vertical flip is possible by calling the
+C<flip()> method.  If the original is to be preserved it's possible
+to make a copy first.
+
+  $img->flip(dir=>"x");       # horizontal flip
+  $img->flip(dir=>"xy");      # vertical and horizontal flip
+  $nimg = $img->copy->flip(dir=>"y"); # make a copy and flip the copy vertically
+
+
+=head2 Blending Images To put an image or a part of an image directly
+into another it is best to call the C<paste()> method on the image you
+want to add to.
 
   $img->paste(img=>$srcimage,left=>30,top=>50);
 
index 6cd956901477ac50a481109c7007b80b1ae0f0b8..71390c9f6ba3bb9a2d7139c14eb6fd05f1d51f19 100644 (file)
--- a/Imager.xs
+++ b/Imager.xs
@@ -745,6 +745,11 @@ i_rubthru(im,src,tx,ty)
               int     tx
               int     ty
 
+undef_int
+i_flipxy(im, direction)
+    Imager::ImgRaw     im
+              int     direction
+
 
 void
 i_gaussian(im,stdev)
diff --git a/image.c b/image.c
index dbbf1be2a245a97c7d301167e226a78c2460a792..b83be16296a890e20f5a492da553abfc0ed8fe94 100644 (file)
--- a/image.c
+++ b/image.c
@@ -32,6 +32,7 @@ Some of these functions are internal.
 
 #define XAXIS 0
 #define YAXIS 1
+#define XYAXIS 2
 
 #define minmax(a,b,i) ( ((a>=i)?a: ( (b<=i)?b:i   )) )
 
@@ -252,8 +253,8 @@ i_img_empty(i_img *im,int x,int y) {
 Re-new image reference 
 
    im - Image pointer
-   x - xsize of destination image
-   y - ysize of destination image
+   x  - xsize of destination image
+   y  - ysize of destination image
    ch - number of channels
 
 =cut
@@ -595,6 +596,7 @@ unmodified.
 
 =cut
 */
+
 void
 i_rubthru(i_img *im,i_img *src,int tx,int ty) {
   i_color pv,orig,dest;
@@ -624,6 +626,94 @@ i_rubthru(i_img *im,i_img *src,int tx,int ty) {
     }
 }
 
+
+/*
+=item i_flipxy(im, axis)
+
+Flips the image inplace around the axis specified.
+Returns 0 if parameters are invalid.
+
+   im   - Image pointer
+   axis - 0 = x, 1 = y, 2 = both
+
+=cut
+*/
+
+undef_int
+i_flipxy(i_img *im, int direction) {
+  int x, x2, y, y2, xm, ym;
+  int xs = im->xsize;
+  int ys = im->ysize;
+  
+  mm_log((1, "i_flipxy(im %p, direction %d)\n", im, direction ));
+
+  if (!im) return 0;
+
+  switch (direction) {
+  case XAXIS: /* Horizontal flip */
+    xm = xs/2;
+    ym = ys;
+    for(y=0; y<ym; y++) {
+      x2 = xs-1;
+      for(x=0; x<xm; x++) {
+       i_color val1, val2;
+       i_gpix(im, x,  y,  &val1);
+       i_gpix(im, x2, y,  &val2);
+       i_ppix(im, x,  y,  &val2);
+       i_ppix(im, x2, y,  &val1);
+       x2--;
+      }
+    }
+    break;
+  case YAXIS:
+    xm = xs;
+    ym = ys/2;
+    y2 = ys-1;
+    for(y=0; y<ym; y++) {
+      for(x=0; x<xm; x++) {
+       i_color val1, val2;
+       i_gpix(im, x,  y,  &val1);
+       i_gpix(im, x,  y2, &val2);
+       i_ppix(im, x,  y,  &val2);
+       i_ppix(im, x,  y2, &val1);
+      }
+      y2--;
+    }
+    break;
+  case XYAXIS:
+    xm = xs/2;
+    ym = ys/2;
+    y2 = ys-1;
+    for(y=0; y<ym; y++) {
+      x2 = xs-1;
+      for(x=0; x<xm; x++) {
+       i_color val1, val2;
+       i_gpix(im, x,  y,  &val1);
+       i_gpix(im, x2, y2, &val2);
+       i_ppix(im, x,  y,  &val2);
+       i_ppix(im, x2, y2, &val1);
+
+       i_gpix(im, x2, y,  &val1);
+       i_gpix(im, x,  y2, &val2);
+       i_ppix(im, x2, y,  &val2);
+       i_ppix(im, x,  y2, &val1);
+       x2--;
+      }
+      y2--;
+    }
+    break;
+  default:
+    mm_log((1, "i_flipxy: direction is invalid\n" ));
+    return 0;
+  }
+  return 1;
+}
+
+
+
+
+
+static
 float
 Lanczos(float x) {
   float PIx, PIx2;
diff --git a/image.h b/image.h
index 3bfb76a4dbbcd37a576afa74569f98108cdc54d2..f8213abe324e05c412f538315c75512daf5b5874 100644 (file)
--- a/image.h
+++ b/image.h
@@ -77,6 +77,10 @@ void i_copyto_trans(i_img *im,i_img *src,int x1,int y1,int x2,int y2,int tx,int
 void i_copy        (i_img *im,i_img *src);
 void i_rubthru     (i_img *im,i_img *src,int tx,int ty);
 
+undef_int i_flipxy (i_img *im, int direction);
+
+
+
 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);