Permalink
Browse files

allow scale() to non-proportionally scale, if the called asks for it.

  • Loading branch information...
1 parent b4c8178 commit 44caacb7ed340458f8ea9647ef8dec739ff9f562 Tony Cook committed Aug 18, 2006
Showing with 91 additions and 27 deletions.
  1. +33 −12 Imager.pm
  2. +32 −7 lib/Imager/Transformations.pod
  3. +26 −8 t/t40scale.t
View
@@ -1963,11 +1963,10 @@ sub register_filter {
sub scale {
my $self=shift;
- my %opts=(scalefactor=>0.5,'type'=>'max',qtype=>'normal',@_);
+ my %opts=('type'=>'max',qtype=>'normal',@_);
my $img = Imager->new();
my $tmp = Imager->new();
-
- my $scalefactor = $opts{scalefactor};
+ my ($x_scale, $y_scale);
unless (defined wantarray) {
my @caller = caller;
@@ -1980,45 +1979,67 @@ sub scale {
return undef;
}
+ if ($opts{'xscalefactor'} && $opts{'yscalefactor'}) {
+ $x_scale = $opts{'xscalefactor'};
+ $y_scale = $opts{'yscalefactor'};
+ }
+ elsif ($opts{'xscalefactor'}) {
+ $x_scale = $opts{'xscalefactor'};
+ $y_scale = $opts{'scalefactor'} || $x_scale;
+ }
+ elsif ($opts{'yscalefactor'}) {
+ $y_scale = $opts{'yscalefactor'};
+ $x_scale = $opts{'scalefactor'} || $y_scale;
+ }
+ else {
+ $x_scale = $y_scale = $opts{'scalefactor'} || 0.5;
+ }
+
# 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') {
- $scalefactor = _min($xpix,$ypix);
+ $x_scale = $y_scale = _min($xpix,$ypix);
}
elsif ($opts{'type'} eq 'max') {
- $scalefactor = _max($xpix,$ypix);
+ $x_scale = $y_scale = _max($xpix,$ypix);
+ }
+ elsif ($opts{'type'} eq 'nonprop' || $opts{'type'} eq 'non-proportional') {
+ $x_scale = $xpix;
+ $y_scale = $ypix;
}
else {
$self->_set_error('invalid value for type parameter');
return undef;
}
} elsif ($opts{xpixels}) {
- $scalefactor = $opts{xpixels} / $self->getwidth();
+ $x_scale = $y_scale = $opts{xpixels} / $self->getwidth();
}
elsif ($opts{ypixels}) {
- $scalefactor = $opts{ypixels}/$self->getheight();
+ $x_scale = $y_scale = $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
+ my $scalefactor;
(undef, undef, $scalefactor)
= $opts{constrain}->constrain($self->getwidth, $self->getheight);
unless ($scalefactor) {
$self->_set_error('constrain method failed on constrain parameter');
return undef;
}
+ $x_scale = $y_scale = $scalefactor;
}
if ($opts{qtype} eq 'normal') {
- $tmp->{IMG} = i_scaleaxis($self->{IMG}, $scalefactor, 0);
+ $tmp->{IMG} = i_scaleaxis($self->{IMG}, $x_scale, 0);
if ( !defined($tmp->{IMG}) ) {
$self->{ERRSTR} = 'unable to scale image';
return undef;
}
- $img->{IMG}=i_scaleaxis($tmp->{IMG}, $scalefactor, 1);
+ $img->{IMG}=i_scaleaxis($tmp->{IMG}, $y_scale, 1);
if ( !defined($img->{IMG}) ) {
$self->{ERRSTR}='unable to scale image';
return undef;
@@ -2027,16 +2048,16 @@ sub scale {
return $img;
}
elsif ($opts{'qtype'} eq 'preview') {
- $img->{IMG} = i_scale_nn($self->{IMG}, $scalefactor, $scalefactor);
+ $img->{IMG} = i_scale_nn($self->{IMG}, $x_scale, $y_scale);
if ( !defined($img->{IMG}) ) {
$self->{ERRSTR}='unable to scale image';
return undef;
}
return $img;
}
elsif ($opts{'qtype'} eq 'mixing') {
- my $new_width = int(0.5 + $self->getwidth * $scalefactor);
- my $new_height = int(0.5 + $self->getheight * $scalefactor);
+ my $new_width = int(0.5 + $self->getwidth * $x_scale);
+ my $new_height = int(0.5 + $self->getheight * $y_scale);
$new_width >= 1 or $new_width = 1;
$new_height >= 1 or $new_height = 1;
$img->{IMG} = i_scale_mixing($self->{IMG}, $new_width, $new_height);
@@ -94,6 +94,8 @@ wide and 500 pixels tall.
$newimg = $img->scale(xpixels=>400,ypixels=>400); # 560x400
$newimg = $img->scale(xpixels=>400,ypixels=>400,type=>'min'); # 400x285
+ $newimg = $img->scale(xpixels=>400, ypixels=>400),type=>'nonprop'); # 400x400
+
$newimg = $img->scale(scalefactor=>0.25); 175x125
$newimg = $img->scale(); # 350x250
@@ -108,9 +110,9 @@ the original.
=item *
-xpixels, ypixels - desired size of the scaled image. The resulting
-image is always scaled proportionally. The C<type> parameter controls
-whether the larger or smaller of the two possible sizes is chosen.
+xpixels, ypixels - desired size of the scaled image. The C<type>
+parameter controls whether the larger or smaller of the two possible
+sizes is chosen, or if the image is scaled non-proportionally.
=item *
@@ -119,8 +121,15 @@ 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.
+scalefactor - if none of xpixels, ypixels, xscalefactor, yscalefactor
+or constrain is supplied then this is used as the ratio to scale by.
+Default: 0.5.
+
+=item *
+
+xscalefactor, yscalefactor - if both are supplied then the image is
+scaled as per these parameters, whether this is proportionally or not.
+New in Imager 0.54.
=item *
@@ -137,6 +146,10 @@ min - the smaller of the 2 sizes are chosen.
max - the larger of the 2 sizes. This is the default.
+=item *
+
+nonprop - non-proportional scaling. New in Imager 0.54.
+
=back
scale() will fail if C<type> is set to some other value.
@@ -169,7 +182,9 @@ up this will duplicate pixels.
C<mixing> - implements the mixing algorithm implemented by pnmscale.
This retains more detail when scaling down than C<normal>. When
scaling down this proportionally accumulates sample data from the
-pixels, resulting in a proportional mix of all of the pixels.
+pixels, resulting in a proportional mix of all of the pixels. When
+scaling up this will mix pixels when the sampling grid crosses a pixel
+boundary but will otherwise copy pixel values.
=back
@@ -183,6 +198,16 @@ dimensions. eg.
my $scaled = $img->scaleX(pixels=>400)->scaleY(pixels=>200);
+From Imager 0.54 you can scale without maintaining proportions either
+by supplying both the xscalefactor and yscalefactor arguments:
+
+ my $scaled = $img->scale(xscalefactor => 0.5, yscalefactor => 0.67);
+
+or by supplying C<xpixels> and C<ypixels> and setting C<type> to
+"nonprop":
+
+ my $scaled = $im->scale(xpixels => 200, ypixels => 200, type => 'nonprop');
+
Returns the scaled image on success.
Returns false on failure, check the errstr() method for the reason for
@@ -219,7 +244,7 @@ A mandatory warning is produced if scale() is called in void context.
# to half size
my $low = $image->scale(qtype => 'preview');
- # mixing scale
+ # mixing method scale
my $mixed = $image->scale(qtype => 'mixing', scalefactor => 0.1);
# using an Image::Math::Constrain object
View
@@ -1,7 +1,7 @@
#!perl -w
use strict;
use lib 't';
-use Test::More tests => 72;
+use Test::More tests => 213;
BEGIN { use_ok(Imager=>':all') }
@@ -122,6 +122,22 @@ SKIP:
scale_test($im, 'scale', 120, 72, "72 height",
ypixels => 72);
+ # new scaling parameters in 0.54
+ scale_test($im, 'scale', 80, 48, "xscale 0.5",
+ xscalefactor => 0.5);
+ scale_test($im, 'scale', 80, 48, "yscale 0.5",
+ yscalefactor => 0.5);
+ scale_test($im, 'scale', 40, 48, "xscale 0.25 yscale 0.5",
+ xscalefactor => 0.25, yscalefactor => 0.5);
+ scale_test($im, 'scale', 160, 48, "xscale 1.0 yscale 0.5",
+ xscalefactor => 1.0, yscalefactor => 0.5);
+ scale_test($im, 'scale', 160, 48, "xpixels 160 ypixels 48 type nonprop",
+ xpixels => 160, ypixels => 48, type => 'nonprop');
+ scale_test($im, 'scale', 160, 96, "xpixels 160 ypixels 96",
+ xpixels => 160, ypixels => 96);
+ scale_test($im, 'scale', 80, 96, "xpixels 80 ypixels 96 type nonprop",
+ xpixels => 80, ypixels => 96, type => 'nonprop');
+
# scaleX
scale_test($im, 'scaleX', 80, 96, "defaults");
scale_test($im, 'scaleX', 40, 96, "0.25 scalefactor",
@@ -141,12 +157,14 @@ sub scale_test {
my ($in, $method, $exp_width, $exp_height, $note, @parms) = @_;
print "# $note: @parms\n";
- SKIP:
- {
- 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");
+ for my $qtype (qw(normal preview mixing)) {
+ SKIP:
+ {
+ my $scaled = $in->$method(@parms, qtype => $qtype);
+ ok($scaled, "$method $note qtype $qtype")
+ or skip("failed to scale", 2);
+ is($scaled->getwidth, $exp_width, "check width");
+ is($scaled->getheight, $exp_height, "check height");
+ }
}
}

0 comments on commit 44caacb

Please sign in to comment.