- scale() can now expect an Image::Math::Constrain object as a scaling
authorTony Cook <tony@develop=help.com>
Sun, 5 Feb 2006 14:07:23 +0000 (14:07 +0000)
committerTony Cook <tony@develop=help.com>
Sun, 5 Feb 2006 14:07:23 +0000 (14:07 +0000)
  constraint via the constrain parameter.
- added tests for the various ways we can specify scaling size
- documented scale()'s scalefactor parameter

Changes
Imager.pm
lib/Imager/Transformations.pod
t/t40scale.t

diff --git a/Changes b/Changes
index 7eab3a6036907aba048f310179d0f149d7a13b49..4d0b4e18cc7d59e15355675ac91e97e2b47be155 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1331,6 +1331,10 @@ Revision history for Perl extension Imager.
 - add error handling tests for scale()
 - smarter warning removal
 - handle effects of byte ordering when testing tiff error messages
+- scale() can now expect an Image::Math::Constrain object as a scaling
+  constraint via the constrain parameter.
+- added tests for the various ways we can specify scaling size
+- documented scale()'s scalefactor parameter
 
 =================================================================
 
index 611f59c3c175c98fe06cf48874303cd6c2af3fd4..b017488e81843398ba3a19636f092ab3c31ad0f5 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -1773,6 +1773,17 @@ sub scale {
   elsif ($opts{ypixels}) { 
     $opts{scalefactor}=$opts{ypixels}/$self->getheight();
   }
+  elsif ($opts{constrain} && ref $opts{constrain}
+        && $opts{constrain}->can('constrain')) {
+    # we've been passed an Image::Math::Constrain object or something
+    # that looks like one
+    (undef, undef, $opts{scalefactor})
+      = $opts{constrain}->constrain($self->getwidth, $self->getheight);
+    unless ($opts{scalefactor}) {
+      $self->_set_error('constrain method failed on constrain parameter');
+      return undef;
+    }
+  }
 
   if ($opts{qtype} eq 'normal') {
     $tmp->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},0);
index 8fc23700131c67fb1301fd047be4a8e2a4cc46a8..9fa66fe383a0c37ea76b21612cfb13be1945f905 100644 (file)
@@ -114,6 +114,16 @@ whether the larger or smaller of the two possible sizes is chosen.
 
 =item *
 
+constrain - an Image::Math::Constrain object defining the way in which
+the image size should be constrained.
+
+=item *
+
+scalefactor - if none of xpixels, ypixels or constrain is supplied
+then this is used as the ratio to scale by.  Default: 0.5.
+
+=item *
+
 type - controls whether the larger or smaller of the two possible
 sizes is chosen, possible values are:
 
@@ -169,6 +179,43 @@ Returns the scaled image on success.
 Returns false on failure, check the errstr() method for the reason for
 failure.
 
+  # setup
+  my $image = Imager->new;
+  $image->read(file => 'somefile.jpg')
+    or die $image->errstr;
+
+  # all full quality unless indicated otherwise
+  # half the size:
+  my $half = $image->scale;
+
+  # double the size
+  my $double = $image->scale(scalefactor => 2.0);
+
+  # so a 400 x 400 box fits in the resulting image:
+  my $fit400x400inside = $image->scale(xpixels => 400, ypixels => 400);
+  my $fit400x400inside2 = $image->scale(xpixels => 400, ypixels => 400,
+                                        type=>'max');
+
+  # fit inside a 400 x 400 box
+  my $inside400x400 = $image->scale(xpixels => 400, ypixels => 400,
+                              type=>'min');
+
+  # make it 400 pixels wide or high
+  my $width400 = $image->scale(xpixels => 400);
+  my $height400 = $image->scale(ypixels => 400);
+
+  # low quality scales:
+  # to half size
+  my $low = $image->scale(qtype => 'preview');
+
+  # using an Image::Math::Constrain object
+  use Image::Math::Constrain;
+  my $constrain = Image::Math::Constrain->new(800, 600);
+  my $scaled = $image->scale(constrain => $constrain);
+
+  # same as Image::Math::Constrain version
+  my $scaled2 = $image->scale(xpixels => 800, ypixels => 600, type => 'min');
+
 =item scaleX
 
 scaleX() will scale along the X dimension, changing the width of the
index 0268e51cda9c5c4eb243821af8158581dcace481..e27e69847c009f87e9a7d7fe247744a0d84fb1d1 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use lib 't';
-use Test::More tests => 22;
+use Test::More tests => 46;
 
 BEGIN { use_ok(Imager=>':all') }
 
@@ -78,3 +78,46 @@ ok($scaleimg->write(file=>'testout/t40scale2.ppm',type=>'pnm'),
   is($im->errstr, "invalid value for type parameter", "check error message");
 }
 
+SKIP:
+{ # Image::Math::Constrain support
+  eval "require Image::Math::Constrain;";
+  $@ and skip "module optional Image::Math::Constrain not installed", 3;
+  my $constrain = Image::Math::Constrain->new(20, 100);
+  my $im = Imager->new(xsize => 160, ysize => 96);
+  my $result = $im->scale(constrain => $constrain);
+  ok($result, "successful scale with Image::Math::Constrain");
+  is($result->getwidth, 20, "check result width");
+  is($result->getheight, 12, "check result height");
+}
+
+{ # scale size checks
+  my $im = Imager->new(xsize => 160, ysize => 96); # some random size
+
+  scale_test($im, 80, 48, "48 x 48 def type",
+            xpixels => 48, ypixels => 48);
+  scale_test($im, 80, 48, "48 x 48 max type",
+            xpixels => 48, ypixels => 48, type => 'max');
+  scale_test($im, 80, 48, "80 x 80 min type",
+            xpixels => 80, ypixels => 80, type => 'min');
+  scale_test($im, 80, 48, "no scale parameters (default to 0.5 scalefactor)");
+  scale_test($im, 120, 72, "0.75 scalefactor",
+            scalefactor => 0.75);
+  scale_test($im, 80, 48, "80 width",
+            xpixels => 80);
+  scale_test($im, 120, 72, "72 height",
+            ypixels => 72);
+}
+
+sub scale_test {
+  my ($in, $exp_width, $exp_height, $note, @parms) = @_;
+
+  print "# $note: @parms\n";
+ SKIP:
+  {
+    my $scaled = $in->scale(@parms);
+    ok($scaled, "scale $note")
+      or skip("failed to scale", 2);
+    is($scaled->getwidth, $exp_width, "check width");
+    is($scaled->getheight, $exp_height, "check height");
+  }
+}