blib/lib/Imager/TrimColorList.pm

filter: Statementstmt 72.1% 70 / 97 Branchbran 63.6% 28 / 44 Conditioncond 64.0% 16 / 25 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 85.7% 12 / 14 Podpod 100% 6 / 6 totaltotal 67.3% 136 / 202 CC 35 SCAR 26.8
CC
35
Cov
69%
add
25.1
describe
20.0
auto
14.7
CRAP 14.5
« blib/lib/Imager/Transform.pm ↑ Summary blib/lib/Imager/Expr.pm »
1 package Imager::TrimColorList;
2 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 79 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 79 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 79 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 79 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 79 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 79 our $VERSION = "1.001";
10  
11 101 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
12 101 my $class = shift;
13  
14 101 my $self = $class->_new;
15  
16 101 for my $entry (@_) {
17 30 ▶ $self->add($entry)
Branch: 1 branch
BranchTrueFalseTotal
$self->add($entry) or return 30 0 30
18 or return;
19 }
20  
21 101 $self;
22 }
23  
24 18 ▶sub _add_anycolor {
Subroutine _add_anycolor: called CC=2 SCAR=6.9
25 18 my ($self, $c1, $c2) = @_;
26  
27 18 ▶ if ($c1->isa("Imager::Color")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1->isa('Imager::Color')) { } 15 3 18
28 15 return $self->add_color($c1, $c2);
29 }
30 else {
31 3 return $self->add_fcolor($c1, $c2);
32 }
33 }
34  
35 168 ▶sub _clamp_255 {
Subroutine _clamp_255: called CC=3 SCAR=11.0
36 168 my $x = shift;
37 168 ▶ if ($x < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x < 0) { } 47 121 168
elsif ($x > 255) { } 34 87 121
38 47 return 0;
39 }
40 elsif ($x > 255) {
41 34 return 255;
42 }
43 else {
44 87 return int($x);
45 }
46 }
47  
48 40 ▶sub add {
Subroutine add: called CC=18 SCAR=32.2
Pod documented
49 40 my ($self, $entry) = @_;
50  
51 40 ▶ if (ref $entry && Scalar::Util::blessed($entry)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $entry and builtin::blessed($entry)) { } 2 38 40
elsif (ref $entry and builtin::reftype($entry) eq 'ARRAY') { } 27 11 38
Condition100%3/3
ref $entry and builtin::blessed($entry)
!ll&&!rl&&r
11272
MC/DC100%2/2
ref $entry and builtin::blessed($entry)
ref $entry builtin::blessed($entry)
Truth table100%3/3
A and B
A= ref $entryB= builtin::blessed($entry)
ABresult
0X0
100
111
52 2 ▶ if ($entry->isa("Imager::Color") || $entry->isa("Imager::Color::Float")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->isa('Imager::Color') or $entry->isa('Imager::Color::Float')) { } 2 0 2
Condition66%2/3
$entry->isa("Imager::Color") or $entry->isa("Imager::Color::Float")
l!l&&r!l&&!r
110
MC/DC0%0/2
$entry->isa("Imager::Color") or $entry->isa("Imager::Color::Float")
$entry->isa("Imager::Color") $entry->isa("Imager::Color::Float")
Truth table66%2/3
A or B
A= $entry->isa("Imager::Color")B= $entry->isa("Imager::Color::Float")
ABresult
1X1
011
000
53 2 return $self->_add_anycolor($entry, $entry);
54 }
55 else {
56 0 Imager->_set_error("bad non-array color range entry");
57 0 return;
58 }
59 }
60 ▶ elsif (ref $entry && Scalar::Util::reftype($entry) eq "ARRAY") {
Condition66%2/3
ref $entry and builtin::reftype($entry) eq "ARRAY"
!ll&&!rl&&r
11027
MC/DC50%1/2
ref $entry and builtin::reftype($entry) eq "ARRAY"
ref $entry builtin::reftype($entry) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $entryB= builtin::reftype($entry) eq "ARRAY"
ABresult
0X0
100
111
61 27 ▶ if (@$entry == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$entry == 1) { } 3 24 27
elsif (@$entry == 2) { } 24 0 24
62 3 ▶ if (my $c = Imager::_color($entry->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = Imager::_color($entry->[0])) { } 3 0 3
63 3 ▶ if ($c->isa("Imager::Color") || $c->isa("Imager::Color::Float")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->isa("Imager::Color") or $c->isa("Imager::Color::Float")) 3 0 3
Condition66%2/3
$c->isa("Imager::Color") or $c->isa("Imager::Color::Float")
l!l&&r!l&&!r
210
MC/DC0%0/2
$c->isa("Imager::Color") or $c->isa("Imager::Color::Float")
$c->isa("Imager::Color") $c->isa("Imager::Color::Float")
Truth table66%2/3
A or B
A= $c->isa("Imager::Color")B= $c->isa("Imager::Color::Float")
ABresult
1X1
011
000
64 3 return $self->_add_anycolor($c, $c);
65 }
66 }
67 else {
68 # error set by _color()
69 0 return;
70 }
71 }
72 elsif (@$entry == 2) {
73 # first must be a color (or convertible to a color)
74 24 ▶ if (my $c1 = Imager::_color($entry->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c1 = Imager::_color($entry->[0])) { } 24 0 24
75 24 ▶ if (Scalar::Util::looks_like_number($entry->[1]) && $entry->[1] >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (&Scalar::Util::looks_like_number($entry->[1]) and $entry->[1] >= 0) { } 22 2 24
elsif (my $c2 = Imager::_color($entry->[1])) { } 2 0 2
Condition66%2/3
&Scalar::Util::looks_like_number($entry->[1]) and $entry->[1] >= 0
!ll&&!rl&&r
2022
MC/DC50%1/2
&Scalar::Util::looks_like_number($entry->[1]) and $entry->[1] >= 0
&Scalar::Util::looks_like_number($entry->[1]) $entry->[1] >= 0
Truth table66%2/3
A and B
A= &Scalar::Util::looks_like_number($entry->[1])B= $entry->[1] >= 0
ABresult
0X0
100
111
76 # convert to range
77 22 my $f = $entry->[1];
78 22 ▶ if ($c1->isa("Imager::Color")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c1->isa('Imager::Color')) { } 21 1 22
79 84 return $self->add_color(Imager::Color->new(map { _clamp_255(POSIX::ceil($_ - ( 255 * $f ))) } $c1->rgba),
80 21 Imager::Color->new(map { _clamp_255($_ + ( 255 * $f )) } $c1->rgba));
81 }
82 else {
83 4 return $self->add_fcolor(Imager::Color::Float->new(map { $_ - $f } $c1->rgba),
84 1 Imager::Color::Float->new(map { $_ + $f } $c1->rgba));
85 }
86 }
87 elsif (my $c2 = Imager::_color($entry->[1])) {
88 2 return $self->_add_anycolor($c1, $c2);
89 }
90 else {
91 # error set by _color()
92 0 return;
93 }
94 }
95 else {
96 0 return;
97 }
98 }
99 else {
100 0 Imager->_set_error("array entry for color range must be 1 or 2 elements");
101 0 return;
102 }
103 }
104 else {
105 # try as a color entry
106 11 ▶ if (my $c1 = Imager::_color($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c1 = Imager::_color($entry)) { } 11 0 11
107 11 return $self->_add_anycolor($c1, $c1);
108 }
109 else {
110 0 return;
111 }
112 }
113 }
114  
115 1 ▶sub all {
Subroutine all: called CC=2 SCAR=7.0
Pod documented
116 1 my ($self) = @_;
117  
118 1 my $count = $self->count;
119 1 my @result;
120 1 for my $i (0 .. $count-1) {
121 0 push @result, $self->get($i);
122 }
123  
124 1 return @result;
125 }
126  
127 0 ▶sub describe {
Subroutine describe: not called CC=4 SCAR=30.0
Pod documented
128 0 my ($self) = @_;
129  
130 0 my $out = <<EOS;
131 Imager::TrimColorList->new(
132 EOS
133 0 ▶ if ($self->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count) { } 0 0 0
134 0 for my $i (0.. $self->count()-1) {
135 0 my $entry = $self->get($i);
136 0 ▶ if ($entry->[0]->isa("Imager::Color")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->[0]->isa('Imager::Color')) { } 0 0 0
137 0 $out .= sprintf("[ Imager::Color->new(%d, %d, %d), Imager::Color->new(%d, %d, %d) ],\n",
138 ($entry->[0]->rgba)[0 .. 2], ($entry->[1]->rgba)[0 .. 2]);
139 }
140 else {
141 0 $out .= sprintf("[ Imager::Color::Float->new(%g, %g, %g), Imager::Color::Float->new(%g, %g, %g) ],\n",
142 ($entry->[0]->rgba)[0 .. 2], ($entry->[1]->rgba)[0 .. 2]);
143 }
144 }
145 }
146 else {
147 0 chomp $out;
148 }
149 0 $out .= ")\n";
150  
151 0 return $out;
152 }
153  
154 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
155 0 my ($self) = @_;
156  
157 0 return Imager::TrimColorList->new($self->all);
158 }
159  
160 5 ▶sub auto {
Subroutine auto: called CC=9 SCAR=26.8
Pod documented
161 5 my ($self, %hsh) = @_;
162  
163 5 ▶ my $name = delete $hsh{name} || "auto";
Condition50%1/2
delete $hsh{'name'} || 'auto'
l!l
50
MC/DC0%0/1
delete $hsh{'name'} || 'auto'
delete $hsh{'name'}
Truth table50%1/2
A
A= delete $hsh{'name'}
Aresult
11
00
164 5 ▶ my $auto = delete $hsh{auto} || "1";
Condition50%1/2
delete $hsh{'auto'} || '1'
l!l
50
MC/DC0%0/1
delete $hsh{'auto'} || '1'
delete $hsh{'auto'}
Truth table50%1/2
A
A= delete $hsh{'auto'}
Aresult
11
00
165 5 my $image = delete $hsh{image};
166 5 my $tolerance = delete $hsh{tolerance};
167  
168 5 ▶ defined $tolerance or $tolerance = 0.01;
Branch: 1 branch
BranchTrueFalseTotal
defined $tolerance or $tolerance = "0.01" 5 0 5
169  
170 5 ▶ unless ($image && $image->{IMG}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image and $image->{"IMG"}) 0 5 5
Condition33%1/3
$image and $image->{"IMG"}
!ll&&!rl&&r
005
MC/DC0%0/2
$image and $image->{"IMG"}
$image $image->{"IMG"}
Truth table33%1/3
A and B
A= $imageB= $image->{"IMG"}
ABresult
0X0
100
111
171 0 Imager->_set_error("$name: no image supplied");
172 0 return;
173 }
174  
175 5 ▶ if ($auto eq "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto eq 1) 0 5 5
176 0 $auto = "centre";
177 }
178 5 ▶ if ($auto eq "center" || $auto eq "centre") {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto eq 'center' or $auto eq 'centre') { } 5 0 5
Condition66%2/3
$auto eq "center" or $auto eq "centre"
l!l&&r!l&&!r
320
MC/DC0%0/2
$auto eq "center" or $auto eq "centre"
$auto eq "center" $auto eq "centre"
Truth table66%2/3
A or B
A= $auto eq "center"B= $auto eq "centre"
ABresult
1X1
011
000
179 5 my ($w, $h) = ( $image->getwidth(), $image->getheight() );
180 5 return Imager::TrimColorList->new
181 (
182 [ $image->getpixel(x => $w / 2, y => 0 ), $tolerance ],
183 [ $image->getpixel(x => $w / 2, y => $h - 1), $tolerance ],
184 [ $image->getpixel(x => 0, y => $h / 2), $tolerance ],
185 [ $image->getpixel(x => $w - 1, y => $h / 2), $tolerance ],
186 );
187 }
188 else {
189 0 Imager->_set_error("$name: auto must be '1' or 'center'");
190 0 return;
191 }
192 }
193  
194 79 1;
195  
196 =head1 NAME
197  
198 Imager::TrimColorList - represent a list of color ranges for Imager's trim() method.
199  
200 =head1 SYNOPSIS
201  
202 use Imager::TrimColorList;
203  
204 # empty list
205 my $tcl = Imager::TrimColorList->new;
206  
207 # add an entry in a variety of forms
208 $tcl->add($entry);
209  
210 # add an explicit color object entry
211 $tcl->add_color($c1, $c2);
212  
213 # add an explicit floating color object entry
214 $tcl->add_fcolor($cf1, $cf2);
215  
216 # number of entries
217 my $count = $tcl->count;
218  
219 # fetch an entry
220 my $entry = $tcl->get($index);
221  
222 # fetch all entries
223 my @all = $tcl->all;
224  
225 # make a list and populate it
226 my $tcl = Imager::TrimColorList->new($entry1, $entry2, ...);
227  
228 # dump contents of the list as a string
229 print $tcl->describe;
230  
231 =head1 DESCRIPTION
232  
233 An Imager::TrimColorList represents a list of color ranges to supply
234 to the trim() method.
235  
236 Each range can be either an 8-bit color range, ie. L<Imager::Color>
237 objects, or a floating point color range, ie. L<Imager::Color::Float>
238 objects, these can be mixed freely in a single list but each range
239 must be 8-bit or floating point.
240  
241 You can supply an entry in a small variety of forms:
242  
243 =over
244  
245 =item *
246  
247 a simple color object of either type, or something convertible to a
248 color object such as a color name such as C<"red">, a hex color such
249 as C<"#FFF">. Any of the forms that Imager::Color supports should
250 work here I<except> for the array form. This becomes a range of only
251 that color.
252  
253 $tcl->add("#000");
254 $tcl->add(Imager::Color->new(0, 0, 0));
255 $tcl->add(Imager::Color::Float->new(0, 0, 0));
256  
257 =item *
258  
259 an arrayref containing a single color object, or something convertible
260 to a color object. This becomes a range of only that color.
261  
262 $tcl->add([ "#000" ]);
263 $tcl->add([ Imager::Color->new(0, 0, 0) ]);
264 $tcl->add([ Imager::Color::Float->new(0, 0, 0) ]);
265  
266 =item *
267  
268 an arrayref containing two color objects of the same type, ie. both
269 Imager::Color objects or convertible to Imager::Color objects, or two
270 Imager::Color::Float objects. This becomes a range between those two
271 colors inclusive.
272  
273 $tcl->add([ "#000", "#002" ]);
274 $tcl->add([ Imager::Color->new(0, 0, 0), Imager::Color->new(0, 0, 2) ]);
275 $tcl->add([ Imager::Color::Float->new(0, 0, 0), Imager::Color::Float->new(0, 0, 2/255) ]);
276  
277 =item *
278  
279 an arrayref containing a color object of either type and a number
280 representing the variance within the color space on either side of the
281 specified color to include.
282  
283 $tcl->add([ "#000", 0.01 ])
284 $tcl->add([ Imager::Color->new(0, 0, 0), 0.01 ]);
285 $tcl->add([ Imager::Color::Float->new(0, 0, 0), 0.01 ]);
286  
287 A range specified this way with an 8-bit color clips at the top and
288 bottom of the sample ranges, so the example 8-bit range above goes
289 from (0, 0, 0) to (2, 2, 2) inclusive, while the floating point range
290 isn't clipped and results in the floating color range (-0.01, -0.01,
291 -0.01) to (0.01, 0.01, 0.01) inclusive.
292  
293 =back
294  
295 =head1 METHODS
296  
297 =over
298  
299 =item new()
300  
301 =item new($entry1, ...)
302  
303 Class method. Create a new Imager::TrimColorList object and
304 optionally add some color ranges to it.
305  
306 Returns an optionally populated Imager::TrimColorList object, or an
307 empty list (or undef) or failure.
308  
309 =item add($entry)
310  
311 Add a single range entry. Note that this accepts a single value which
312 can be a color or convertible to a color, or a reference to an array
313 as described above.
314  
315 Returns a true value on success and a false value on failure.
316  
317 =item add_color($color1, $color2)
318  
319 Add a single 8-bit color range going from the C<$color1> object to the
320 C<$color2> object inclusive. Both parameters must be Image::Color
321 objects or an exception is thrown.
322  
323 =item add_fcolor($fcolor1, $fcolor2)
324  
325 Add a single floating point color range going from the C<$fcolor1>
326 object to the C<$fcolor2> object inclusive. Both parameters must be
327 Image::Color::Float objects or an exception is thrown.
328  
329 =item count()
330  
331 Fetch the number of color ranges stored in the object.
332  
333 =item get($index)
334  
335 Fetch the color range at the given index. This returns a reference to
336 an array containing either two Imager::Color objects or two
337 Imager::Color::Float objects.
338  
339 Returns undef if C<$index> is out of range and does not set C<<
340 Imager->errstr >>.
341  
342 =item all()
343  
344 Fetch all ranges from the object.
345  
346 =item describe()
347  
348 Return a string describing the color range as code that can create the
349 object.
350  
351 =item clone()
352  
353 Duplicate the object.
354  
355 =item auto()
356  
357 Automatically produce a trim color list based on an input image.
358  
359 This is used to implement 'auto' for image trim() and trim_rect()
360 methods.
361  
362 Parameters:
363  
364 =over
365  
366 =item *
367  
368 C<image> - the image to base the color list on. Required.
369  
370 =item *
371  
372 C<auto> - the mechanism used to produce the color list, one of:
373  
374 =over
375  
376 =item *
377  
378 C<1> - a "best" mechanism is selected, this is currently the C<center>
379 method, but it subject to change.
380  
381 =item *
382  
383 C<center>, C<centre> - the pixels at the center of each side of the
384 image are used.
385  
386 =back
387  
388 Default: C<1>.
389  
390 =item *
391  
392 C<tolerance> - used to control the range of pixel colors to be
393 accepted as part of the color list. Default: 0.01.
394  
395 =item *
396  
397 C<name> - used internally to attribute errors back to the original
398 method. Default: C<auto>.
399  
400 =back
401  
402 =back
403  
404 If any method returns an error you can fetch a diagnostic from C<<
405 Imager->errstr >>.
406  
407 =head1 THREADS
408  
409 Imager::TrimColorList objects are properly duplicated when new perl
410 threads are created.
411  
412 =head1 AUTHOR
413  
414 Tony Cook <tony@develop-help.com>
415  
416 =head1 HISTORY
417  
418 Originally the range handling for this was going to be embedded in the
419 trim() method, but this meant that every call that used color ranges
420 would pay some cost as the range list was checked for names (vs color
421 objects) and non-standard forms such as single colors and the color
422 plus variance.
423  
424 The object allows a simple test for the trim() C<colors> parameter
425 that doesn't pay that cost, and still allows a caller to use the
426 explicit convention.
427  
428 =cut
« blib/lib/Imager/Transform.pm ↑ Summary blib/lib/Imager/Expr.pm »