- check that the result of fileno($fh) is defined rather than simply
[imager.git] / t / t40scale.t
CommitLineData
1501d9b3
TC
1#!perl -w
2use strict;
de470892 3use Test::More tests => 230;
02d1d628 4
01edbe4f 5BEGIN { use_ok(Imager=>':all') }
2757bad0 6use Imager::Test qw(is_image is_color4);
02d1d628 7
874c55db 8#require "t/testtools.pl";
1501d9b3 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
874c55db
TC
185{ # check proper alpha handling
186 my $im = Imager->new(xsize => 40, ysize => 40, channels => 4);
187 $im->box(filled => 1, color => 'C0C0C0');
188 my $rot = $im->rotate(degrees => -4)
189 or die;
190 $rot = $rot->to_rgb16;
191 my $sc = $rot->scale(qtype => 'mixing', xpixels => 40);
192 my $out = Imager->new(xsize => $sc->getwidth, ysize => $sc->getheight);
193 $out->box(filled => 1, color => 'C0C0C0');
194 my $cmp = $out->copy;
195 $out->rubthrough(src => $sc);
196 is_image($out, $cmp, "check we get the right image after scaling");
2757bad0
TC
197
198 # we now set alpha=0 pixels to zero on scaling
199 is_color4($sc->getpixel('x' => 39, 'y' => 39), 0, 0, 0, 0,
200 "check we set alpha=0 pixels to zero on scaling");
874c55db
TC
201}
202
2a836a06
TC
203{ # scale_calculate
204 my $im = Imager->new(xsize => 100, ysize => 120);
205 is_deeply([ $im->scale_calculate(scalefactor => 0.5) ],
206 [ 0.5, 0.5, 50, 60 ],
207 "simple scale_calculate");
208 is_deeply([ Imager->scale_calculate(scalefactor => 0.5) ],
209 [], "failed scale_calculate");
210 is_deeply([ Imager->scale_calculate(width => 120, height => 150,
211 xpixels => 240) ],
212 [ 2.0, 2.0, 240, 300 ],
213 "class method scale_factor");
214}
215
de470892
TC
216{ # passing a reference for scaling parameters should fail
217 # RT #35172
218 my $im = Imager->new(xsize => 100, ysize => 100);
219 ok(!$im->scale(xpixels => {}), "can't use a reference as a size");
220 cmp_ok($im->errstr, '=~', "xpixels parameter cannot be a reference",
221 "check error message");
222}
223
41c7d053 224sub scale_test {
15327bf5 225 my ($in, $method, $exp_width, $exp_height, $note, @parms) = @_;
41c7d053
TC
226
227 print "# $note: @parms\n";
658f724e
TC
228 for my $qtype (qw(normal preview mixing)) {
229 SKIP:
230 {
231 my $scaled = $in->$method(@parms, qtype => $qtype);
232 ok($scaled, "$method $note qtype $qtype")
233 or skip("failed to scale", 2);
234 is($scaled->getwidth, $exp_width, "check width");
235 is($scaled->getheight, $exp_height, "check height");
236 }
41c7d053
TC
237 }
238}