- make scale() fail if an invalid type is supplied (previously
authorTony Cook <tony@develop=help.com>
Sun, 5 Feb 2006 13:04:49 +0000 (13:04 +0000)
committerTony Cook <tony@develop=help.com>
Sun, 5 Feb 2006 13:04:49 +0000 (13:04 +0000)
  documented as undefined behaviour)
- add error handling tests for scale()

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

diff --git a/Changes b/Changes
index 4a16b5d09dd5b28d4435d4a88e72f99dcc33173f..09b25a04f263b5e71f71da84bb2b6583b87323e7 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1325,6 +1325,10 @@ Revision history for Perl extension Imager.
 - document index parameter of Imager::Font->new()
 - change faxable output to use a more fax natural PHOTOMETRIC_MINISWHITE, 
   since T.4 normally works that way, and MINISBLACK confuses some readers.
+- clean up scale() method for readability
+- make scale() fail if an invalid type is supplied (previously
+  documented as undefined behaviour)
+- add error handling tests for scale()
 
 =================================================================
 
index e3aa3643d549c6a7468a0bf2b507cdf5e9071c0a..611f59c3c175c98fe06cf48874303cd6c2af3fd4 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -1748,28 +1748,59 @@ sub scale {
     return;
   }
 
-  unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
+  unless ($self->{IMG}) { 
+    $self->_set_error('empty input image'); 
+    return undef;
+  }
 
+  # work out the scaling
   if ($opts{xpixels} and $opts{ypixels} and $opts{'type'}) {
-    my ($xpix,$ypix)=( $opts{xpixels}/$self->getwidth() , $opts{ypixels}/$self->getheight() );
-    if ($opts{'type'} eq 'min') { $opts{scalefactor}=min($xpix,$ypix); }
-    if ($opts{'type'} eq 'max') { $opts{scalefactor}=max($xpix,$ypix); }
-  } elsif ($opts{xpixels}) { $opts{scalefactor}=$opts{xpixels}/$self->getwidth(); }
-  elsif ($opts{ypixels}) { $opts{scalefactor}=$opts{ypixels}/$self->getheight(); }
+    my ($xpix, $ypix)=( $opts{xpixels}/$self->getwidth() , 
+                      $opts{ypixels}/$self->getheight() );
+    if ($opts{'type'} eq 'min') { 
+      $opts{scalefactor}=min($xpix,$ypix); 
+    }
+    elsif ($opts{'type'} eq 'max') {
+      $opts{scalefactor}=max($xpix,$ypix);
+    }
+    else {
+      $self->_set_error('invalid value for type parameter');
+      return undef;
+    }
+  } elsif ($opts{xpixels}) { 
+    $opts{scalefactor}=$opts{xpixels}/$self->getwidth();
+  }
+  elsif ($opts{ypixels}) { 
+    $opts{scalefactor}=$opts{ypixels}/$self->getheight();
+  }
 
   if ($opts{qtype} eq 'normal') {
     $tmp->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},0);
-    if ( !defined($tmp->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; }
+    if ( !defined($tmp->{IMG}) ) { 
+      $self->{ERRSTR}='unable to scale image';
+      return undef;
+    }
     $img->{IMG}=i_scaleaxis($tmp->{IMG},$opts{scalefactor},1);
-    if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; }
+    if ( !defined($img->{IMG}) ) { 
+      $self->{ERRSTR}='unable to scale image'; 
+      return undef;
+    }
+
     return $img;
   }
-  if ($opts{'qtype'} eq 'preview') {
-    $img->{IMG}=i_scale_nn($self->{IMG},$opts{'scalefactor'},$opts{'scalefactor'}); 
-    if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='unable to scale image'; return undef; }
+  elsif ($opts{'qtype'} eq 'preview') {
+    $img->{IMG} = i_scale_nn($self->{IMG}, $opts{'scalefactor'},
+                          $opts{'scalefactor'}); 
+    if ( !defined($img->{IMG}) ) { 
+      $self->{ERRSTR}='unable to scale image'; 
+      return undef;
+    }
     return $img;
   }
-  $self->{ERRSTR}='scale: invalid value for qtype'; return undef;
+  else {
+    $self->_set_error('invalid value for qtype parameter');
+    return undef;
+  }
 }
 
 # Scales only along the X axis
index b1f2e297cdeedcfe2d076ee54247cef09a973499..8fc23700131c67fb1301fd047be4a8e2a4cc46a8 100644 (file)
@@ -81,7 +81,7 @@ if you want to keep an original after doing something that changes the image.
 
 =item scale
 
-To scale an image so porportions are maintained use the
+X<scale>To scale an image so porportions are maintained use the
 C<$img-E<gt>scale()> method.  if you give either a xpixels or ypixels
 parameter they will determine the width or height respectively.  If
 both are given the one resulting in a larger image is used, unless you
@@ -129,7 +129,7 @@ max - the larger of the 2 sizes.  This is the default.
 
 =back
 
-The behaviour when C<type> is set to some other value is undefined.
+scale() will fail if C<type> is set to some other value.
 
 For example, if the original image is 400 pixels wide by 200 pixels
 high and C<xpixels> is set to 300, and C<ypixels> is set to 160.  When
@@ -154,6 +154,8 @@ preview - lower quality.
 
 =back
 
+scale() will fail if C<qtype> is set to some other value.
+
 =back
 
 To scale an image on a given axis without maintaining proportions, it
index 8ef22e65b705df7b71d0427101a1676cceb7bcc3..0268e51cda9c5c4eb243821af8158581dcace481 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use lib 't';
-use Test::More tests => 16;
+use Test::More tests => 22;
 
 BEGIN { use_ok(Imager=>':all') }
 
@@ -61,3 +61,20 @@ ok($scaleimg->write(file=>'testout/t40scale2.ppm',type=>'pnm'),
   is($out->getwidth, 1, "min scale width (preview)");
   is($out->getheight, 1, "min scale height (preview)");
 }
+
+{ # error handling - NULL image
+  my $im = Imager->new;
+  ok(!$im->scale(scalefactor => 0.5), "try to scale empty image");
+  is($im->errstr, "empty input image", "check error message");
+}
+
+{ # invalid qtype value
+  my $im = Imager->new(xsize => 100, ysize => 100);
+  ok(!$im->scale(scalefactor => 0.5, qtype=>'unknown'), "unknown qtype");
+  is($im->errstr, "invalid value for qtype parameter", "check error message");
+  
+  # invalid type value
+  ok(!$im->scale(xpixels => 10, ypixels=>50, type=>"unknown"), "unknown type");
+  is($im->errstr, "invalid value for type parameter", "check error message");
+}
+