Skip to content

Commit

Permalink
- add tests for scaleX()/scaleY()
Browse files Browse the repository at this point in the history
- expand documentation of scaleX()/scaleY()
  • Loading branch information
Tony Cook committed Feb 10, 2006
1 parent 00b8840 commit f794f00
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Imager/Changes
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,8 @@ Revision history for Perl extension Imager.
- eliminate some of the duplication of -I and -L options in LIBS and INC
- Makefile.PL now uses strict.
- the search for freetype1.x headers is now smarter
- add tests for scaleX()/scaleY()
- expand documentation of scaleX()/scaleY()

=================================================================

Expand Down
53 changes: 39 additions & 14 deletions Imager/Imager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1818,33 +1818,48 @@ sub scale {
# Scales only along the X axis

sub scaleX {
my $self=shift;
my %opts=(scalefactor=>0.5,@_);
my $self = shift;
my %opts = ( scalefactor=>0.5, @_ );

unless (defined wantarray) {
my @caller = caller;
warn "scaleX() called in void context - scaleX() returns the scaled image at $caller[1] line $caller[2]\n";
return;
}

unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
unless ($self->{IMG}) {
$self->{ERRSTR} = 'empty input image';
return undef;
}

my $img = Imager->new();

if ($opts{pixels}) { $opts{scalefactor}=$opts{pixels}/$self->getwidth(); }
my $scalefactor = $opts{scalefactor};

unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
$img->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},0);
if ($opts{pixels}) {
$scalefactor = $opts{pixels} / $self->getwidth();
}

unless ($self->{IMG}) {
$self->{ERRSTR}='empty input image';
return undef;
}

$img->{IMG} = i_scaleaxis($self->{IMG}, $scalefactor, 0);

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;
}

# Scales only along the Y axis

sub scaleY {
my $self=shift;
my %opts=(scalefactor=>0.5,@_);
my $self = shift;
my %opts = ( scalefactor => 0.5, @_ );

unless (defined wantarray) {
my @caller = caller;
Expand All @@ -1856,16 +1871,26 @@ sub scaleY {

my $img = Imager->new();

if ($opts{pixels}) { $opts{scalefactor}=$opts{pixels}/$self->getheight(); }
my $scalefactor = $opts{scalefactor};

unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; }
$img->{IMG}=i_scaleaxis($self->{IMG},$opts{scalefactor},1);
if ($opts{pixels}) {
$scalefactor = $opts{pixels} / $self->getheight();
}

unless ($self->{IMG}) {
$self->{ERRSTR} = 'empty input image';
return undef;
}
$img->{IMG}=i_scaleaxis($self->{IMG}, $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;
}


# Transform returns a spatial transformation of the input image
# this moves pixels to a new location in the returned image.
# NOTE - should make a utility function to check transforms for
Expand Down
52 changes: 47 additions & 5 deletions Imager/lib/Imager/Transformations.pod
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Returns the scaled image on success.
Returns false on failure, check the errstr() method for the reason for
failure.

A mandatory warning is produced if scale() is called in void context.

# setup
my $image = Imager->new;
$image->read(file => 'somefile.jpg')
Expand Down Expand Up @@ -218,20 +220,60 @@ failure.

=item scaleX

scaleX() will scale along the X dimension, changing the width of the
image:
scaleX() will scale along the X dimension, return a new image with the
new width:

$newimg = $img->scaleX(pixels=>400); # 400x500
my $newimg = $img->scaleX(pixels=>400); # 400x500
$newimg = $img->scaleX(scalefactor=>0.25) # 175x500

=over

=item *

scalefactor - the amount to scale the X axis. Ignored if C<pixels> is
provided. Default: 0.5.

=item *

pixels - the new width of the image.

=back

Returns the scaled image on success.

Returns false on failure, check the errstr() method for the reason for
failure.

A mandatory warning is produced if scaleX() is called in void context.

=item scaleY

scaleY() will scale along the Y dimension, changing the height of the
image:
scaleY() will scale along the Y dimension, return a new image with the
new height:

$newimg = $img->scaleY(pixels=>400); # 700x400
$newimg = $img->scaleY(scalefactor=>0.25) # 700x125

=over

=item *

scalefactor - the amount to scale the Y axis. Ignored if C<pixels> is
provided. Default: 0.5.

=item *

pixels - the new height of the image.

=back

Returns the scaled image on success.

Returns false on failure, check the errstr() method for the reason for
failure.

A mandatory warning is produced if scaleY() is called in void context.

=item crop

Another way to resize an image is to crop it. The parameters to
Expand Down
42 changes: 31 additions & 11 deletions Imager/t/t40scale.t
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!perl -w
use strict;
use lib 't';
use Test::More tests => 46;
use Test::More tests => 68;

BEGIN { use_ok(Imager=>':all') }

Expand Down Expand Up @@ -66,6 +66,12 @@ ok($scaleimg->write(file=>'testout/t40scale2.ppm',type=>'pnm'),
my $im = Imager->new;
ok(!$im->scale(scalefactor => 0.5), "try to scale empty image");
is($im->errstr, "empty input image", "check error message");

# scaleX/scaleY
ok(!$im->scaleX(scalefactor => 0.5), "try to scaleX empty image");
is($im->errstr, "empty input image", "check error message");
ok(!$im->scaleY(scalefactor => 0.5), "try to scaleY empty image");
is($im->errstr, "empty input image", "check error message");
}

{ # invalid qtype value
Expand Down Expand Up @@ -93,29 +99,43 @@ SKIP:
{ # scale size checks
my $im = Imager->new(xsize => 160, ysize => 96); # some random size

scale_test($im, 80, 48, "48 x 48 def type",
scale_test($im, 'scale', 80, 48, "48 x 48 def type",
xpixels => 48, ypixels => 48);
scale_test($im, 80, 48, "48 x 48 max type",
scale_test($im, 'scale', 80, 48, "48 x 48 max type",
xpixels => 48, ypixels => 48, type => 'max');
scale_test($im, 80, 48, "80 x 80 min type",
scale_test($im, 'scale', 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",
scale_test($im, 'scale', 80, 48, "no scale parameters (default to 0.5 scalefactor)");
scale_test($im, 'scale', 120, 72, "0.75 scalefactor",
scalefactor => 0.75);
scale_test($im, 80, 48, "80 width",
scale_test($im, 'scale', 80, 48, "80 width",
xpixels => 80);
scale_test($im, 120, 72, "72 height",
scale_test($im, 'scale', 120, 72, "72 height",
ypixels => 72);

# scaleX
scale_test($im, 'scaleX', 80, 96, "defaults");
scale_test($im, 'scaleX', 40, 96, "0.25 scalefactor",
scalefactor => 0.25);
scale_test($im, 'scaleX', 120, 96, "pixels 120",
pixels => 120);

# scaleY
scale_test($im, 'scaleY', 160, 48, "defaults");
scale_test($im, 'scaleY', 160, 192, "2.0 scalefactor",
scalefactor => 2.0);
scale_test($im, 'scaleY', 160, 144, "pixels 144",
pixels => 144);
}

sub scale_test {
my ($in, $exp_width, $exp_height, $note, @parms) = @_;
my ($in, $method, $exp_width, $exp_height, $note, @parms) = @_;

print "# $note: @parms\n";
SKIP:
{
my $scaled = $in->scale(@parms);
ok($scaled, "scale $note")
my $scaled = $in->$method(@parms);
ok($scaled, "$method $note")
or skip("failed to scale", 2);
is($scaled->getwidth, $exp_width, "check width");
is($scaled->getheight, $exp_height, "check height");
Expand Down

0 comments on commit f794f00

Please sign in to comment.