3 require "t/testtools.pl";
10 Imager::init('log'=>'testout/t65crop.log');
12 my $img=Imager->new() || die "unable to create image object\n";
14 okx($img, "created image ph");
16 if (okx($img->open(file=>'testimg/scale.ppm',type=>'pnm'), "loaded source")) {
17 my $nimg = $img->crop(top=>10, left=>10, bottom=>25, right=>25);
18 okx($nimg, "got an image");
19 okx($nimg->write(file=>"testout/t65.ppm"), "save to file");
22 skipx(2, "couldn't load source image");
25 { # https://rt.cpan.org/Ticket/Display.html?id=7578
26 # make sure we get the right type of image on crop
27 my $src = Imager->new(xsize=>50, ysize=>50, channels=>2, bits=>16);
28 isx($src->getchannels, 2, "check src channels");
29 isx($src->bits, 16, "check src bits");
30 my $out = $src->crop(left=>10, right=>40, top=>10, bottom=>40);
31 isx($out->getchannels, 2, "check out channels");
32 isx($out->bits, 16, "check out bits");
34 { # https://rt.cpan.org/Ticket/Display.html?id=7578
35 print "# try it for paletted too\n";
36 my $src = Imager->new(xsize=>50, ysize=>50, channels=>3, type=>'paletted');
37 # make sure color index zero is defined so there's something to copy
38 $src->addcolors(colors=>[Imager::Color->new(0,0,0)]);
39 isx($src->type, 'paletted', "check source type");
40 my $out = $src->crop(left=>10, right=>40, top=>10, bottom=>40);
41 isx($out->type, 'paletted', 'check output type');
44 { # https://rt.cpan.org/Ticket/Display.html?id=7581
45 # crop() documentation says width/height takes precedence, but is unclear
46 # from looking at the existing code, setting width/height will go from
47 # the left of the image, even if left/top are provided, despite the
49 # Let's make sure that things happen as documented
50 my $src = test_oo_img();
51 # make sure we get what we want
52 isx($src->getwidth, 150, "src width");
53 isx($src->getheight, 150, "src height");
57 # - hash ref containing args to crop()
58 # - expected left, top, right, bottom values
59 # we call crop using the given arguments then call it using the
60 # hopefully stable left/top/right/bottom/arguments
61 # this is kind of lame, but I don't want to include a rewritten
67 { left=>10, top=>10, right=>70, bottom=>80 },
72 { width=>50, height=>50 },
77 { left=>20, width=>70, top=>30, height=>90 },
82 { right=>140, width=>50, bottom=>130, height=>60 },
87 { top=>40, bottom=>110 },
92 { left=>40, right=>110 },
117 { left=>100, right=>200 },
122 { bottom=>50, height=>70 },
127 { right=>30, width=>60 },
132 { top=>120, height=>60 },
136 for my $test (@tests) {
137 my ($desc, $args, $left, $top, $right, $bottom) = @$test;
138 my $out = $src->crop(%$args);
139 okx($out, "got output for $desc");
140 my $cmp = $src->crop(left=>$left, top=>$top, right=>$right, bottom=>$bottom);
141 okx($cmp, "got cmp for $desc");
142 # make sure they're the same
143 my $diff = Imager::i_img_diff($out->{IMG}, $cmp->{IMG});
144 isx($diff, 0, "difference should be 0 for $desc");
147 { # https://rt.cpan.org/Ticket/Display.html?id=7581
148 # previously we didn't check that the result had some pixels
150 my $src = test_oo_img();
151 okx(!$src->crop(left=>50, right=>50), "nothing across");
152 matchx($src->errstr, qr/resulting image would have no content/,
154 okx(!$src->crop(top=>60, bottom=>60), "nothing down");
155 matchx($src->errstr, qr/resulting image would have no content/,