3 use Test::More tests => 64;
5 use Imager::Test qw(test_image);
9 -d "testout" or mkdir "testout";
11 Imager::init('log'=>'testout/t65crop.log');
13 my $img=Imager->new() || die "unable to create image object\n";
15 ok($img, "created image ph");
19 skip("couldn't load source image", 2)
20 unless ok($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "loaded source");
21 my $nimg = $img->crop(top=>10, left=>10, bottom=>25, right=>25);
22 ok($nimg, "got an image");
23 ok($nimg->write(file=>"testout/t65.ppm"), "save to file");
26 { # https://rt.cpan.org/Ticket/Display.html?id=7578
27 # make sure we get the right type of image on crop
28 my $src = Imager->new(xsize=>50, ysize=>50, channels=>2, bits=>16);
29 is($src->getchannels, 2, "check src channels");
30 is($src->bits, 16, "check src bits");
31 my $out = $src->crop(left=>10, right=>40, top=>10, bottom=>40);
32 is($out->getchannels, 2, "check out channels");
33 is($out->bits, 16, "check out bits");
35 { # https://rt.cpan.org/Ticket/Display.html?id=7578
36 print "# try it for paletted too\n";
37 my $src = Imager->new(xsize=>50, ysize=>50, channels=>3, type=>'paletted');
38 # make sure color index zero is defined so there's something to copy
39 $src->addcolors(colors=>[Imager::Color->new(0,0,0)]);
40 is($src->type, 'paletted', "check source type");
41 my $out = $src->crop(left=>10, right=>40, top=>10, bottom=>40);
42 is($out->type, 'paletted', 'check output type');
45 { # https://rt.cpan.org/Ticket/Display.html?id=7581
46 # crop() documentation says width/height takes precedence, but is unclear
47 # from looking at the existing code, setting width/height will go from
48 # the left of the image, even if left/top are provided, despite the
50 # Let's make sure that things happen as documented
51 my $src = test_image();
52 # make sure we get what we want
53 is($src->getwidth, 150, "src width");
54 is($src->getheight, 150, "src height");
58 # - hash ref containing args to crop()
59 # - expected left, top, right, bottom values
60 # we call crop using the given arguments then call it using the
61 # hopefully stable left/top/right/bottom/arguments
62 # this is kind of lame, but I don't want to include a rewritten
68 { left=>10, top=>10, right=>70, bottom=>80 },
73 { width=>50, height=>50 },
78 { left=>20, width=>70, top=>30, height=>90 },
83 { right=>140, width=>50, bottom=>130, height=>60 },
88 { top=>40, bottom=>110 },
93 { left=>40, right=>110 },
118 { left=>100, right=>200 },
123 { bottom=>50, height=>70 },
128 { right=>30, width=>60 },
133 { top=>120, height=>60 },
137 for my $test (@tests) {
138 my ($desc, $args, $left, $top, $right, $bottom) = @$test;
139 my $out = $src->crop(%$args);
140 ok($out, "got output for $desc");
141 my $cmp = $src->crop(left=>$left, top=>$top, right=>$right, bottom=>$bottom);
142 ok($cmp, "got cmp for $desc");
143 # make sure they're the same
144 my $diff = Imager::i_img_diff($out->{IMG}, $cmp->{IMG});
145 is($diff, 0, "difference should be 0 for $desc");
148 { # https://rt.cpan.org/Ticket/Display.html?id=7581
149 # previously we didn't check that the result had some pixels
151 my $src = test_image();
152 ok(!$src->crop(left=>50, right=>50), "nothing across");
153 cmp_ok($src->errstr, '=~', qr/resulting image would have no content/,
155 ok(!$src->crop(top=>60, bottom=>60), "nothing down");
156 cmp_ok($src->errstr, '=~', qr/resulting image would have no content/,
160 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
162 local $SIG{__WARN__} =
165 my $printed = $warning;
167 $printed =~ s/\n/\n\#/g;
168 print "# ",$printed, "\n";
170 my $img = Imager->new(xsize=>10, ysize=>10);
172 cmp_ok($warning, '=~', 'void', "correct warning");
173 cmp_ok($warning, '=~', 't65crop\\.t', "correct file");
177 my $src = test_image();
178 ok(!$src->crop( top=>1000, bottom=>1500, left=>0, right=>100 ),
179 "outside of image" );
180 cmp_ok($src->errstr, '=~', qr/outside of the image/, "and message");
181 ok(!$src->crop( top=>100, bottom=>1500, left=>1000, right=>1500 ),
182 "outside of image" );
183 cmp_ok($src->errstr, '=~', qr/outside of the image/, "and message");