convert scale.c to scale.im so we have 8 bit/sample and double/sample
[imager.git] / t / t40scale.t
CommitLineData
1501d9b3
TC
1#!perl -w
2use strict;
01edbe4f 3use lib 't';
a10945af 4use Test::More tests => 223;
02d1d628 5
01edbe4f 6BEGIN { use_ok(Imager=>':all') }
02d1d628 7
1501d9b3
TC
8require "t/testtools.pl";
9
02d1d628 10Imager::init('log'=>'testout/t40scale.log');
1501d9b3 11my $img=Imager->new();
02d1d628 12
01edbe4f
TC
13ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'),
14 "load test image") or print "# ",$img->errstr,"\n";
02d1d628 15
01edbe4f
TC
16my $scaleimg=$img->scale(scalefactor=>0.25)
17 or print "# ",$img->errstr,"\n";
18ok($scaleimg, "scale it (good mode)");
02d1d628 19
01edbe4f
TC
20ok($scaleimg->write(file=>'testout/t40scale1.ppm',type=>'pnm'),
21 "save scaled image") or print "# ",$img->errstr,"\n";
02d1d628 22
01edbe4f
TC
23$scaleimg=$img->scale(scalefactor=>0.25,qtype=>'preview');
24ok($scaleimg, "scale it (preview)") or print "# ",$img->errstr,"\n";
02d1d628 25
01edbe4f
TC
26ok($scaleimg->write(file=>'testout/t40scale2.ppm',type=>'pnm'),
27 "write preview scaled image") or print "# ",$img->errstr,"\n";
02d1d628 28
658f724e
TC
29$scaleimg = $img->scale(scalefactor => 0.25, qtype => 'mixing');
30ok($scaleimg, "scale it (mixing)") or print "# ", $img->errstr, "\n";
31ok($scaleimg->write(file=>'testout/t40scale3.ppm', type=>'pnm'),
32 "write mixing scaled image") or print "# ", $img->errstr, "\n";
33
a10945af
TC
34{ # double image scaling with mixing, since it has code to handle it
35 my $dimg = Imager->new(xsize => $img->getwidth, ysize => $img->getheight,
36 channels => $img->getchannels,
37 bits => 'double');
38 ok($dimg, "create double/sample image");
39 $dimg->paste(src => $img);
40 $scaleimg = $dimg->scale(scalefactor => 0.25, qtype => 'mixing');
41 ok($scaleimg, "scale it (mixing, double)");
42 ok($scaleimg->write(file => 'testout/t40mixdbl.ppm', type => 'pnm'),
43 "write double/mixing scaled image");
44 is($scaleimg->bits, 'double', "got the right image type as output");
45
46 # hscale only, mixing
47 $scaleimg = $dimg->scale(xscalefactor => 0.33, yscalefactor => 1.0,
48 qtype => 'mixing');
49 ok($scaleimg, "scale it (hscale, mixing, double)");
50 is($scaleimg->getheight, $dimg->getheight, "same height");
51 ok($scaleimg->write(file => 'testout/t40hscdmix.ppm', type => 'pnm'),
52 "save it");
53
54 # vscale only, mixing
55 $scaleimg = $dimg->scale(xscalefactor => 1.0, yscalefactor => 0.33,
56 qtype => 'mixing');
57 ok($scaleimg, "scale it (vscale, mixing, double)");
58 is($scaleimg->getwidth, $dimg->getwidth, "same width");
59 ok($scaleimg->write(file => 'testout/t40vscdmix.ppm', type => 'pnm'),
60 "save it");
61}
62
1501d9b3
TC
63{
64 # check for a warning when scale() is called in void context
65 my $warning;
66 local $SIG{__WARN__} =
67 sub {
68 $warning = "@_";
69 my $printed = $warning;
70 $printed =~ s/\n$//;
71 $printed =~ s/\n/\n\#/g;
72 print "# ",$printed, "\n";
73 };
74 $img->scale(scalefactor=>0.25);
01edbe4f
TC
75 cmp_ok($warning, '=~', qr/void/, "check warning");
76 cmp_ok($warning, '=~', qr/t40scale\.t/, "check filename");
34b3f7e6
TC
77 $warning = '';
78 $img->scaleX(scalefactor=>0.25);
79 cmp_ok($warning, '=~', qr/void/, "check warning");
80 cmp_ok($warning, '=~', qr/t40scale\.t/, "check filename");
81 $warning = '';
82 $img->scaleY(scalefactor=>0.25);
83 cmp_ok($warning, '=~', qr/void/, "check warning");
84 cmp_ok($warning, '=~', qr/t40scale\.t/, "check filename");
1501d9b3
TC
85}
86{ # https://rt.cpan.org/Ticket/Display.html?id=7467
87 # segfault in Imager 0.43
88 # make sure scale() doesn't let us make an image zero pixels high or wide
89 # it does this by making the given axis as least 1 pixel high
90 my $out = $img->scale(scalefactor=>0.00001);
01edbe4f
TC
91 is($out->getwidth, 1, "min scale width");
92 is($out->getheight, 1, "min scale height");
02d1d628 93
1501d9b3 94 $out = $img->scale(scalefactor=>0.00001, qtype => 'preview');
01edbe4f
TC
95 is($out->getwidth, 1, "min scale width (preview)");
96 is($out->getheight, 1, "min scale height (preview)");
658f724e
TC
97
98 $out = $img->scale(scalefactor=>0.00001, qtype => 'mixing');
99 is($out->getwidth, 1, "min scale width (mixing)");
100 is($out->getheight, 1, "min scale height (mixing)");
1501d9b3 101}
5168ca3a
TC
102
103{ # error handling - NULL image
104 my $im = Imager->new;
105 ok(!$im->scale(scalefactor => 0.5), "try to scale empty image");
106 is($im->errstr, "empty input image", "check error message");
15327bf5
TC
107
108 # scaleX/scaleY
109 ok(!$im->scaleX(scalefactor => 0.5), "try to scaleX empty image");
110 is($im->errstr, "empty input image", "check error message");
111 ok(!$im->scaleY(scalefactor => 0.5), "try to scaleY empty image");
112 is($im->errstr, "empty input image", "check error message");
5168ca3a
TC
113}
114
115{ # invalid qtype value
116 my $im = Imager->new(xsize => 100, ysize => 100);
117 ok(!$im->scale(scalefactor => 0.5, qtype=>'unknown'), "unknown qtype");
118 is($im->errstr, "invalid value for qtype parameter", "check error message");
119
120 # invalid type value
121 ok(!$im->scale(xpixels => 10, ypixels=>50, type=>"unknown"), "unknown type");
122 is($im->errstr, "invalid value for type parameter", "check error message");
123}
124
41c7d053
TC
125SKIP:
126{ # Image::Math::Constrain support
127 eval "require Image::Math::Constrain;";
4f579313 128 $@ and skip "optional module Image::Math::Constrain not installed", 3;
41c7d053
TC
129 my $constrain = Image::Math::Constrain->new(20, 100);
130 my $im = Imager->new(xsize => 160, ysize => 96);
131 my $result = $im->scale(constrain => $constrain);
132 ok($result, "successful scale with Image::Math::Constrain");
133 is($result->getwidth, 20, "check result width");
134 is($result->getheight, 12, "check result height");
135}
136
137{ # scale size checks
138 my $im = Imager->new(xsize => 160, ysize => 96); # some random size
139
15327bf5 140 scale_test($im, 'scale', 80, 48, "48 x 48 def type",
41c7d053 141 xpixels => 48, ypixels => 48);
15327bf5 142 scale_test($im, 'scale', 80, 48, "48 x 48 max type",
41c7d053 143 xpixels => 48, ypixels => 48, type => 'max');
15327bf5 144 scale_test($im, 'scale', 80, 48, "80 x 80 min type",
41c7d053 145 xpixels => 80, ypixels => 80, type => 'min');
15327bf5
TC
146 scale_test($im, 'scale', 80, 48, "no scale parameters (default to 0.5 scalefactor)");
147 scale_test($im, 'scale', 120, 72, "0.75 scalefactor",
41c7d053 148 scalefactor => 0.75);
15327bf5 149 scale_test($im, 'scale', 80, 48, "80 width",
41c7d053 150 xpixels => 80);
15327bf5 151 scale_test($im, 'scale', 120, 72, "72 height",
41c7d053 152 ypixels => 72);
15327bf5 153
658f724e
TC
154 # new scaling parameters in 0.54
155 scale_test($im, 'scale', 80, 48, "xscale 0.5",
156 xscalefactor => 0.5);
157 scale_test($im, 'scale', 80, 48, "yscale 0.5",
158 yscalefactor => 0.5);
159 scale_test($im, 'scale', 40, 48, "xscale 0.25 yscale 0.5",
160 xscalefactor => 0.25, yscalefactor => 0.5);
161 scale_test($im, 'scale', 160, 48, "xscale 1.0 yscale 0.5",
162 xscalefactor => 1.0, yscalefactor => 0.5);
163 scale_test($im, 'scale', 160, 48, "xpixels 160 ypixels 48 type nonprop",
164 xpixels => 160, ypixels => 48, type => 'nonprop');
165 scale_test($im, 'scale', 160, 96, "xpixels 160 ypixels 96",
166 xpixels => 160, ypixels => 96);
167 scale_test($im, 'scale', 80, 96, "xpixels 80 ypixels 96 type nonprop",
168 xpixels => 80, ypixels => 96, type => 'nonprop');
169
15327bf5
TC
170 # scaleX
171 scale_test($im, 'scaleX', 80, 96, "defaults");
172 scale_test($im, 'scaleX', 40, 96, "0.25 scalefactor",
173 scalefactor => 0.25);
174 scale_test($im, 'scaleX', 120, 96, "pixels 120",
175 pixels => 120);
176
177 # scaleY
178 scale_test($im, 'scaleY', 160, 48, "defaults");
179 scale_test($im, 'scaleY', 160, 192, "2.0 scalefactor",
180 scalefactor => 2.0);
181 scale_test($im, 'scaleY', 160, 144, "pixels 144",
182 pixels => 144);
41c7d053
TC
183}
184
185sub scale_test {
15327bf5 186 my ($in, $method, $exp_width, $exp_height, $note, @parms) = @_;
41c7d053
TC
187
188 print "# $note: @parms\n";
658f724e
TC
189 for my $qtype (qw(normal preview mixing)) {
190 SKIP:
191 {
192 my $scaled = $in->$method(@parms, qtype => $qtype);
193 ok($scaled, "$method $note qtype $qtype")
194 or skip("failed to scale", 2);
195 is($scaled->getwidth, $exp_width, "check width");
196 is($scaled->getheight, $exp_height, "check height");
197 }
41c7d053
TC
198 }
199}