- 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()
=================================================================
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
=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
=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
=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
#!perl -w
use strict;
use lib 't';
-use Test::More tests => 16;
+use Test::More tests => 22;
BEGIN { use_ok(Imager=>':all') }
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");
+}
+