Debian Codegraph @

t/300-transform/210-trim.t

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