Permalink
Browse files

Merged in the scale branch:

- adds a new scaling mechanism 'mixing' based on the method
implemented by pnmscale.  Produces better detail when scaling down and
is faster than the 'normal' method.

- the scale() method can now scale non-proportionally if the caller
specifically asks for it with xscalefactor/yscalefactor or by setting
type to 'nonprop'.
  • Loading branch information...
1 parent 4cda4e7 commit 658f724ef1381b7c7d0844fc9a2e2c7868e22188 Tony Cook committed Aug 18, 2006
Showing with 355 additions and 27 deletions.
  1. +43 −10 Imager.pm
  2. +6 −0 Imager.xs
  3. +1 −0 MANIFEST
  4. +1 −1 Makefile.PL
  5. +3 −1 TODO
  6. +1 −0 imager.h
  7. +44 −7 lib/Imager/Transformations.pod
  8. +221 −0 scale.c
  9. +35 −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,13 +2048,25 @@ 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 * $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);
+ unless ($img->{IMG}) {
+ $self->_set_error(Imager->_error_as_meg);
+ return;
+ }
+ return $img;
+ }
else {
$self->_set_error('invalid value for qtype parameter');
return undef;
View
@@ -2974,6 +2974,12 @@ i_scale_nn(im,scx,scy)
float scy
Imager::ImgRaw
+i_scale_mixing(im, width, height)
+ Imager::ImgRaw im
+ int width
+ int height
+
+Imager::ImgRaw
i_haar(im)
Imager::ImgRaw im
View
@@ -189,6 +189,7 @@ samples/samp-tags.cgi Demonstrate image upload via a HTML form
samples/samp-tags.html Form for samp-tags.cgi
samples/slant_text.pl Using $font->transform() to slant text
samples/tk-photo.pl
+scale.c Newer scaling code
spot.perl For making an ordered dither matrix from a spot function
stackmach.c
stackmach.h
View
@@ -155,7 +155,7 @@ my @objs = qw(Imager.o draw.o polygon.o image.o io.o iolayer.o
regmach.o trans2.o quant.o error.o convert.o
map.o tags.o palimg.o maskimg.o img16.o rotate.o
bmp.o tga.o rgb.o color.o fills.o imgdouble.o limits.o hlines.o
- imext.o);
+ imext.o scale.o);
$Recommends{Imager} =
{ 'Parse::RecDescent' => 0 };
View
4 TODO
@@ -14,10 +14,12 @@ remove gif query from makefile.pl (done)
fallback for read/write_multi to read/write
-pnmscale based alternative scale method
+pnmscale based alternative scale method (done)
rubthrough 4 on 4
+replace dummy test script in dynfilt with a real one
+
BEFORE 0.50:
skip t82inline.t tests if directory has spaces in name
View
@@ -417,6 +417,7 @@ undef_int i_writergb_wiol(i_img *img, io_glue *ig, int wierdpack, int compress,
i_img * i_scaleaxis(i_img *im, float Value, int Axis);
i_img * i_scale_nn(i_img *im, float scx, float scy);
+i_img * i_scale_mixing(i_img *src, int width, int height);
i_img * i_haar(i_img *im);
int i_count_colors(i_img *im,int maxc);
@@ -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.
@@ -156,11 +169,22 @@ qtype - defines the quality of scaling performed. Possible values are:
=item *
-normal - high quality scaling. This is the default.
+C<normal> - high quality scaling. This is the default.
=item *
-preview - lower quality.
+C<preview> - lower quality. When scaling down this will skip input
+pixels, eg. scaling by 0.5 will skip every other pixel. When scaling
+up this will duplicate pixels.
+
+=item *
+
+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. When
+scaling up this will mix pixels when the sampling grid crosses a pixel
+boundary but will otherwise copy pixel values.
=back
@@ -174,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
@@ -210,6 +244,9 @@ A mandatory warning is produced if scale() is called in void context.
# to half size
my $low = $image->scale(qtype => 'preview');
+ # mixing method scale
+ my $mixed = $image->scale(qtype => 'mixing', scalefactor => 0.1);
+
# using an Image::Math::Constrain object
use Image::Math::Constrain;
my $constrain = Image::Math::Constrain->new(800, 600);
Oops, something went wrong.

0 comments on commit 658f724

Please sign in to comment.