lib/Imager/Filter/Autocrop.pm

filter: Statementstmt 91.6% 88 / 96 Branchbran 62.5% 40 / 64 Conditioncond 55.3% 31 / 56 MC/DCmcdc 32.2% 10 / 31 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 69.3% 179 / 258 CC 54 SCAR 36.6
CC
54
Cov
74%
_scan
68.8
autocrop
24.4
_outline
10.0
CRAP 38.7
« script/autocrop.pl ↑ Summary
1 package Imager::Filter::Autocrop;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Imager::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Imager::Filter::Autocrop - Automatic crop filter for Imager.
12  
13 =head1 VERSION
14  
15 Version 1.23
16  
17 =head1 SYNOPSIS
18  
19 use Imager;
20 use Imager::Filter::Autocrop;
21  
22 my $img = Imager->new();
23 $img->read(file => 'image.jpg');
24 $img->filter(type => 'autocrop') or die $img->errstr;
25  
26 =head1 DESCRIPTION
27  
28 This module extends C<Imager> functionality with the autocrop filter, similar to ImageMagick and GraphicsMagick "trim". It does
29 have a few additional features as well, such as support for the border or detection-only mode. The distribution also includes
30 a command-line script autocrop.pl.
31  
32 Note: If the image looks blank (whether genuinely or because of the 'fuzz' parameter), or if there is nothing to crop,
33 then the filter call will return false and the 'errstr' method will return appropriate message.
34  
35 =head1 NAME OVERRIDE
36  
37 You can change the name under which the filter is registered, by specifying it in 'use' directive. For example, to change the
38 name to 'trim' from the default 'autocrop':
39  
40 use Imager;
41 use Imager::Filter::Autocrop 'trim';
42  
43 $img->filter(type => 'trim') or die $img->errstr;
44  
45 =head1 PARAMETERS
46  
47 =over 12
48  
49 =item C<color>
50  
51 By default the color of the top left pixel is used for cropping. You can explicitly specify one though, by either
52 providing the '#RRGGBB' value or passing an object of C<Imager::Color> class or its subclass.
53  
54 # The following two calls are identical.
55 $img->filter(type => 'autocrop', color => "#FFFFFF");
56 $img->filter(type => 'autocrop', color => Imager::Color->new(255, 255, 255));
57  
58 =item C<fuzz>
59  
60 You can specify the deviation for the color value (for all RGB channels or the main channel if it is a greyscale image),
61 by using the 'fuzz' parameter. All image colors within the range would be treated as matching candidates for cropping.
62
63 $img->filter(type => 'autocrop', fuzz => 20);
64  
65 =item C<border>
66  
67 You can specify the border around the image for cropping. If the cropping area with the border is identical to the original
68 image height and width, then no actual cropping will be done.
69  
70 $img->filter(type => 'autocrop', border => 10);
71  
72 =item C<detect>
73  
74 Finally, you can just detect the cropping area by passing a hash reference as a 'detect' parameter. On success, your hash
75 will be set with left, right, top and bottom keys and appropriate values. Please note that if the 'border' parameter is used,
76 the detected area values will be adjusted appropriately.
77  
78 my %points = ();
79 $img->filter(type => 'autocrop', detect => \%points);
80  
81 =back
82  
83 =cut
84  
85 2 our $VERSION = '1.23';
86  
87 2 ▶sub autocrop {
Subroutine autocrop: called CC=13 SCAR=32.0
Pod undocumented
88 2 my (%params) = @_;
89 2 my ($img, $fuzz, $border, $color, $detect) = @params{qw<imager fuzz border color detect>};
90 2 ▶ $color||=$params{'colour'}; # Yes, we support British version too.
Condition33%1/3
$color ||= $params{"colour"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$color ||= $params{"colour"}
$color $params{"colour"}
Truth table33%1/3
A ||= B
A= $colorB= $params{"colour"}
ABresult
1X1
011
000
91 # Check if colour is given, otherwise read the corner pixel.
92 2 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) { } 0 2 2
93 0 ▶ $color = Imager::Color->new($color) unless UNIVERSAL::isa($color, 'Imager::Color');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($color, "Imager::Color") 0 0 0
94 } else {
95 2 $color = $img->getpixel(x => 0, y => 0);
96 }
97 2 ▶ die "AUTOCROP_ERROR_COLOR: Color is not set correctly\n" unless defined $color;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $color 0 2 2
98 2 my ($r, $g, $b) = $color->rgba;
99 2 my @range = ([ $r - $fuzz, $r + $fuzz ], [ $g - $fuzz, $g + $fuzz ], [ $b - $fuzz, $b + $fuzz ]);
100 2 my %original = (left => 0, right => $img->getwidth, top => 0, bottom => $img->getheight);
101 2 my $crop = _scan($img, \@range, \%original);
102 2 my $bordered = 0;
103 2 for (keys %original) {
104 8 ▶ if ($original{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original{$_}) { } 4 4 8
105 4 $crop->{$_}+=$border;
106 4 ▶ if ($crop->{$_} >= $original{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crop->{$_} >= $original{$_}) 0 4 4
107 0 $crop->{$_} = $original{$_};
108 0 $bordered++;
109 }
110 } else {
111 4 $crop->{$_}-=$border;
112 4 ▶ if ($crop->{$_} <= $original{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crop->{$_} <= $original{$_}) 0 4 4
113 0 $crop->{$_} = $original{$_};
114 0 $bordered++;
115 }
116 }
117 }
118 2 ▶ die "AUTOCROP_ERROR_NOCROP: Nothing to crop\n" if ($bordered == 4);
Branch: 1 branch
BranchTrueFalseTotal
if $bordered == 4 0 2 2
119 2 ▶ if ($detect and ref $detect eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($detect and ref $detect eq 'HASH') { } 2 0 2
Condition33%1/3
$detect and ref $detect eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
$detect and ref $detect eq "HASH"
$detect ref $detect eq "HASH"
Truth table33%1/3
A and B
A= $detectB= ref $detect eq "HASH"
ABresult
0X0
100
111
120 2 %{$detect} = %{$crop};
121 } else {
122 0 ▶ my $rv = $img->crop(%{$crop}) or die $img->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $rv = $img->crop(%{$crop;}) or die $img->errstr 0 0 0
123 0 $img->{IMG} = $rv->{IMG};
124 }
125 }
126  
127 2 ▶sub _scan {
Subroutine _scan: called CC=29 SCAR=42.3
128 2 my ($image, $range, $original) = @_;
129 2 my ($line, $top, $bottom, $left, $right) = (0, 0, 0, undef, undef);
130 2 my ($bpoint, $rpoint) = ($original->{bottom} - 1, $original->{right} - 1);
131 2 my ($outline, $pos, $rpos, @colors, @scanned);
132 2 ▶ my $channels = $image->getchannels < 3 ? 0 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$image->getchannels < 3 ? : 0 2 2
133 2 ▶ for ($line = $bpoint; $line >= 0; $line--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line >= 0) 38 0 38
134 38 ($outline, $pos, $rpos) = _outline($image, $line, $range, $channels, $rpoint);
135 38 ▶ last if $outline;
Branch: 1 branch
BranchTrueFalseTotal
if $outline 2 36 38
136 }
137 2 ▶ die "AUTOCROP_ERROR_BLANK: Image looks blank\n" unless $outline;
Branch: 1 branch
BranchTrueFalseTotal
unless $outline 0 2 2
138 2 $bottom = $line;
139 2 ▶ ($left, $right) = ($pos, $rpos) if $outline;
Branch: 1 branch
BranchTrueFalseTotal
if $outline 2 0 2
140 2 ▶ for ($line = 0; $line < $bottom; $line++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line < $bottom) 32 0 32
141 # NB - don't use left/right boundaries here
142 32 ($outline, $pos, $rpos) = _outline($image, $line, $range, $channels, $rpoint);
143 32 ▶ last if $outline;
Branch: 1 branch
BranchTrueFalseTotal
if $outline 2 30 32
144 }
145 2 $top = $line;
146 2 ▶ if ($outline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outline) 2 0 2
147 2 ▶ $left = $pos if (!defined $left or $pos < $left);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $left or $pos < $left 0 2 2
Condition33%1/3
not defined $left or $pos < $left
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $left or $pos < $left
not defined $left $pos < $left
Truth table33%1/3
not(A) or B
A= not defined $leftB= $pos < $left
ABresult
1X1
011
000
148 2 ▶ $right = $rpos if (!defined $right or $rpos > $right);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $right or $rpos > $right 0 2 2
Condition33%1/3
not defined $right or $rpos > $right
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $right or $rpos > $right
not defined $right $rpos > $right
Truth table33%1/3
not(A) or B
A= not defined $rightB= $rpos > $right
ABresult
1X1
011
000
149 }
150 2 ▶ unless (defined $left and defined $right and $left == 0 and $right == $rpoint) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $left and defined $right and $left == 0 and $right == $rpoint) 2 0 2
Condition33%3/9
defined $left and defined $right and $left == 0 and $right == $rpoint
!ll&&!rl&&r
200
defined $left and defined $right and $left == 0
!ll&&!rl&&r
020
defined $left and defined $right
!ll&&!rl&&r
002
MC/DC0%0/4
defined $left and defined $right and $left == 0 and $right == $rpoint
defined $left defined $right $left == 0 $right == $rpoint
Truth table20%1/5
A and B and C and D
A= defined $leftB= defined $rightC= $left == 0D= $right == $rpoint
ABCDresult
0XXX0
10XX0
110X0
11100
11111
151 2 ▶ for ($line = $top + 1; $line < $bottom; $line++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line < $bottom) 30 2 32
152 30 ($outline, $pos, $rpos) = _outline($image, $line, $range, $channels, $rpoint, $left, $right);
153 30 ▶ if ($outline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outline) 4 26 30
154 4 ▶ $left = $pos if (!defined $left or $pos < $left);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $left or $pos < $left 0 4 4
Condition33%1/3
not defined $left or $pos < $left
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $left or $pos < $left
not defined $left $pos < $left
Truth table33%1/3
not(A) or B
A= not defined $leftB= $pos < $left
ABresult
1X1
011
000
155 4 ▶ $right = $rpos if (!defined $right or $rpos > $right);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $right or $rpos > $right 0 4 4
Condition33%1/3
not defined $right or $rpos > $right
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $right or $rpos > $right
not defined $right $rpos > $right
Truth table33%1/3
not(A) or B
A= not defined $rightB= $rpos > $right
ABresult
1X1
011
000
156 4 ▶ last if (defined $left and defined $right and $left == 0 and $right == $rpoint);
Branch: 1 branch
BranchTrueFalseTotal
if defined $left and defined $right and $left == 0 and $right == $rpoint 0 4 4
Condition33%3/9
defined $left and defined $right and $left == 0 and $right == $rpoint
!ll&&!rl&&r
400
defined $left and defined $right and $left == 0
!ll&&!rl&&r
040
defined $left and defined $right
!ll&&!rl&&r
004
MC/DC0%0/4
defined $left and defined $right and $left == 0 and $right == $rpoint
defined $left defined $right $left == 0 $right == $rpoint
Truth table20%1/5
A and B and C and D
A= defined $leftB= defined $rightC= $left == 0D= $right == $rpoint
ABCDresult
0XXX0
10XX0
110X0
11100
11111
157 }
158 }
159 }
160 2 ▶ $right++ if defined $right;
Branch: 1 branch
BranchTrueFalseTotal
if defined $right 2 0 2
161 2 ▶ $bottom++ if defined $bottom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bottom 2 0 2
162 2 return { top => $top, bottom => $bottom, left => $left, right => $right };
163 }
164  
165 100 ▶sub _outline {
Subroutine _outline: called CC=10 SCAR=23.0
166 100 my ($image, $line, $range, $channels, $rpos, $left, $right) = @_;
167 100 my @colors = unpack "C*", $image->getscanline(y => $line);
168 100 my ($outline, $routline, $pos) = (0, 0, 0);
169 100 my @color;
170 100 ▶ while (@colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@colors) 6438 66 6504
171 6438 @color = splice @colors, 0, 4;
172 6438 $outline = _out_of_range(\@color, $range, $channels);
173 6438 ▶ last if ($outline or (defined $left and $pos >= $left));
Branch: 1 branch
BranchTrueFalseTotal
if $outline or defined $left and $pos >= $left 34 6404 6438
Condition100%6/6
$outline or defined $left and $pos >= $left
l!l&&r!l&&!r
6286404
defined $left and $pos >= $left
!ll&&!rl&&r
541499028
MC/DC100%3/3
$outline or defined $left and $pos >= $left
$outline defined $left $pos >= $left
Truth table100%4/4
A or B and C
A= $outlineB= defined $leftC= $pos >= $left
ABCresult
1XX1
0111
00X0
0100
174 6404 $pos++;
175 }
176 100 ▶ while (@colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@colors) 1156 66 1222
177 1156 @color = splice @colors, -4, 4;
178 1156 $routline = _out_of_range(\@color, $range, $channels);
179 1156 ▶ last if ($routline or (defined $right and $rpos <= $right));
Branch: 1 branch
BranchTrueFalseTotal
if $routline or defined $right and $rpos <= $right 34 1122 1156
Condition100%6/6
$routline or defined $right and $rpos <= $right
l!l&&r!l&&!r
8261122
defined $right and $rpos <= $right
!ll&&!rl&&r
13299026
MC/DC100%3/3
$routline or defined $right and $rpos <= $right
$routline defined $right $rpos <= $right
Truth table100%4/4
A or B and C
A= $routlineB= defined $rightC= $rpos <= $right
ABCresult
1XX1
0111
00X0
0100
180 1122 $rpos--;
181 }
182 100 ▶ return ($outline||$routline, $pos, $rpos);
Condition100%3/3
$outline || $routline
l!l&&r!l&&!r
6292
MC/DC100%2/2
$outline || $routline
$outline $routline
Truth table100%3/3
A || B
A= $outlineB= $routline
ABresult
1X1
011
000
183 }
184  
185 7594 ▶sub _out_of_range {
Subroutine _out_of_range: called CC=4 SCAR=13.9
186 7594 my ($scanned, $range, $channels) = @_;
187 7594 for (0 .. $channels) {
188 22754 ▶ if ($scanned->[$_] < $range->[$_]->[0] or $scanned->[$_] > $range->[$_]->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scanned->[$_] < $range->[$_][0] or $scanned->[$_] > $range->[$_][1]) 14 22740 22754
Condition66%2/3
$scanned->[$_] < $range->[$_][0] or $scanned->[$_] > $range->[$_][1]
l!l&&r!l&&!r
14022740
MC/DC50%1/2
$scanned->[$_] < $range->[$_][0] or $scanned->[$_] > $range->[$_][1]
$scanned->[$_] < $range->[$_][0] $scanned->[$_] > $range->[$_][1]
Truth table66%2/3
A or B
A= $scanned->[$_] < $range->[$_][0]B= $scanned->[$_] > $range->[$_][1]
ABresult
1X1
011
000
189 14 return 1;
190 }
191 }
192 7580 return 0;
193 }
194  
195 2 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
196 2 my ($self, $type) = @_;
197 2 ▶ Imager->register_filter(
Condition100%2/2
$type || 'autocrop'
l!l
11
MC/DC100%1/1
$type || 'autocrop'
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
198 type => $type||'autocrop',
199 callsub => \&autocrop,
200 callseq => [ 'image' ],
201 defaults => {
202 fuzz => 0,
203 border => 0,
204 });
205 }
206  
207 =head1 SEE ALSO
208  
209 L<http://search.cpan.org/perldoc?Imager>
210  
211 =head1 AUTHOR
212  
213 Alexander Yezhov, C<< <leader at cpan.org> >>
214 Domain Knowledge Ltd.
215 L<https://do-know.com/>
216  
217 =head1 BUGS
218  
219 Please report any bugs or feature requests to C<bug-imager-filter-autocrop at rt.cpan.org>, or through
220 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Imager-Filter-Autocrop>. I will be notified, and then you'll
221 automatically be notified of progress on your bug as I make changes.
222  
223  
224  
225 =head1 SUPPORT
226  
227 You can find documentation for this module with the perldoc command.
228  
229 perldoc Imager::Filter::Autocrop
230  
231  
232 You can also look for information at:
233  
234 =over 4
235  
236 =item * RT: CPAN's request tracker (report bugs here)
237  
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Imager-Filter-Autocrop>
239  
240 =item * AnnoCPAN: Annotated CPAN documentation
241  
242 L<http://annocpan.org/dist/Imager-Filter-Autocrop>
243  
244 =item * CPAN Ratings
245  
246 L<http://cpanratings.perl.org/d/Imager-Filter-Autocrop>
247  
248 =item * Search CPAN
249  
250 L<http://search.cpan.org/dist/Imager-Filter-Autocrop/>
251  
252 =back
253  
254  
255 =head1 LICENSE AND COPYRIGHT
256  
257 Copyright 2016 Alexander Yezhov.
258  
259 This program is free software; you can redistribute it and/or modify it
260 under the terms of the Artistic License (2.0). You may obtain a
261 copy of the full license at:
262  
263 L<http://www.perlfoundation.org/artistic_license_2_0>
264  
265 Any use, modification, and distribution of the Standard or Modified
266 Versions is governed by this Artistic License. By using, modifying or
267 distributing the Package, you accept this license. Do not use, modify,
268 or distribute the Package, if you do not accept this license.
269  
270 If your Modified Version has been derived from a Modified Version made
271 by someone other than you, you are nevertheless required to ensure that
272 your Modified Version complies with the requirements of this license.
273  
274 This license does not grant you the right to use any trademark, service
275 mark, tradename, or logo of the Copyright Holder.
276  
277 This license includes the non-exclusive, worldwide, free-of-charge
278 patent license to make, have made, use, offer to sell, sell, import and
279 otherwise transfer the Package with respect to any patent claims
280 licensable by the Copyright Holder that are necessarily infringed by the
281 Package. If you institute patent litigation (including a cross-claim or
282 counterclaim) against any party alleging that the Package constitutes
283 direct or contributory patent infringement, then this Artistic License
284 to you shall terminate on the date that such litigation is filed.
285  
286 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
287 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
288 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
289 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
290 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
291 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
292 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
293 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
294  
295  
296 =cut
297  
298 2 1;
« script/autocrop.pl ↑ Summary