1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#!perl -w
use strict;
use Imager;
use Test::More;
use Imager::Test qw(is_image);
{
my $im8 = Imager->new(channels => 4, xsize => 200, ysize => 400);
$im8->box(filled => 1, xmin => 11, ymin => 23, xmax => 161, ymax => 257, color => "#FFF") or die;
my $im16 = $im8->to_rgb16;
for my $test ( [ $im8, 8 ], [ $im16, 16 ]) {
my ($im, $bits) = @$test;
{
my @rect = $im->trim_rect();
note "@rect";
is_deeply(\@rect, [ 11, 23, 38, 142 ], "check alpha trim ($bits)");
}
my $imr = $im->rotate(right => 90)
or die;
is($imr->bits, $bits, "rotated has expected bits ($bits)");
{
my @rect = $imr->trim_rect();
note "@rect";
is_deeply(\@rect, [ 142, 11, 23, 38 ], "check alpha trim ($bits)");
}
}
}
{
my $im = Imager->new(channels => 3, xsize => 200, ysize => 300);
$im->box(filled => 1, color => "#F00");
$im->box(filled => 1, color => "#00F",
xmin => 31, ymin => 42, xmax => 116, ymax => 251);
#$im->write(file => "trim2.png") or die;
{
my @rect = $im->trim_rect(colors => [ "#F00" ]);
note "@rect";
is_deeply(\@rect, [ 31, 42, 83, 48 ], "check simple color trim");
my $trimmed = $im->trim(colors => [ "#F00" ]);
ok($trimmed, "got a simple color trimmed image");
is_image($trimmed, $im->crop(left => 31, top => 42, right => 117, bottom => 252),
"check trimmed image is as expected");
}
{
# intrude into the border a little
$im->box(filled => 1, color => "#000", xmin => 16, xmax => 18, ymin => 8, ymax => 10);
my @rect = $im->trim_rect(colors => [ "#F00" ]);
is_deeply(\@rect, [ 16, 8, 83, 48 ],
"check simple color trim with intrusion");
my $trimmed = $im->trim(colors => [ "#F00" ]);
ok($trimmed, "got a simple color trimmed image");
is_image($trimmed, $im->crop(left => 16, top => 8, right => 117, bottom => 252),
"check simple with intrusion trimmed image is as expected");
}
}
{
my $im = Imager->new(channels => 3, xsize => 20, ysize => 20);
$im->box(filled => 1, color => "#FFF");
$im->box(filled => 1, color => "#F00", xmax => 0);
$im->box(filled => 1, color => "#0F0", ymax => 1);
$im->box(filled => 1, color => "#00F", xmin => 17);
$im->box(filled => 1, color => "#FF0", ymin => 16);
my @rect = $im->trim_rect(colors => [ "#F00", "#0F0", "#00F", "#FF0" ]);
note "@rect";
is_deeply(\@rect, [ 1, 2, 3, 4 ], "check multi-color trim");
}
{
my $im = Imager->new(channels => 4, xsize => 20, ysize => 20);
$im->box(filled => 1, xmax => 9, color => "#F00");
$im->box(filled => 1, xmin => 10, color => "#0F0");
$im->box(filled => 1, color => "#FFF", xmin => 1, ymin => 2, xmax => 16, ymax => 15);
my $tcl = Imager::TrimColorList->new("#F00", "#0F0");
my @rect = $im->trim_rect(colors => $tcl);
note "@rect";
is_deeply(\@rect, [ 1, 2, 3, 4 ], "check trim via TrimColorList");
}
{
my $im = Imager->new(channels =>4, xsize => 20, ysize => 20);
$im->box(filled => 1, color => "#FFF");
$im->box(filled => 1, color => "#F00", xmax => 0);
$im->box(filled => 1, color => "#0F0", ymax => 1);
$im->box(filled => 1, color => "#00F", xmin => 17);
$im->box(filled => 1, color => "#FF0", ymin => 16);
my @rect = $im->trim_rect(auto => "center")
or diag $im->errstr;
note "@rect";
is_deeply(\@rect, [ 1, 2, 3, 4 ], "check auto=center trim");
}
{
my $im = Imager->new(channels => 3, xsize => 20, ysize => 20);
$im->box(filled => 1, color => "#FFF");
# off centre cross
$im->box(filled => 1, xmin => 8, xmax => 11, ymin => 1, ymax => 17, color => "#000");
$im->box(filled => 1, xmin => 3, xmax => 16, ymin => 10, ymax => 11, color => "#000");
my @rect = $im->trim_rect(auto => "centre");
is_deeply(\@rect, [ 3, 1, 3, 2 ], "trim_rect 3-channel cross");
is_image($im->trim(auto => "center"),
$im->crop(left => 3, top => 1, right => 17, bottom => 18),
"trim 3-channel cross");
}
{
my $im = Imager->new(channels => 4, xsize => 20, ysize => 20);
# off centre cross, working by transparency instead
$im->box(filled => 1, xmin => 8, xmax => 11, ymin => 1, ymax => 17, color => "#F00");
$im->box(filled => 1, xmin => 3, xmax => 16, ymin => 10, ymax => 11, color => "#F00");
my @rect = $im->trim_rect(auto => "centre");
is_deeply(\@rect, [ 3, 1, 3, 2 ], "trim_rect 4-channel cross");
is_image($im->trim(auto => "center"),
$im->crop(left => 3, top => 1, right => 17, bottom => 18),
"trim 4-channel cross");
}
done_testing();
|