blib/lib/Image/Base/Imager.pm

filter: Statementstmt 95.2% 121 / 127 Branchbran 87.1% 54 / 62 Conditioncond 87.5% 7 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 95.0% 19 / 20 Podpod 83.3% 10 / 12 totaltotal 91.4% 214 / 234 CC 38 SCAR 17.7
CC
38
Cov
92%
set
11.0
new
10.1
diamond
7.0
CRAP 5.9
↑ Summary
1 # Copyright 2010, 2011, 2012, 2019 Kevin Ryde
2  
3 # This file is part of Image-Base-Imager.
4 #
5 # Image-Base-Imager is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Imager is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Imager. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19  
20 # cf Imager::Draw -- drawing operations
21 # Seems to auto-clip to width,height.
22  
23 package Image::Base::Imager;
24 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # maybe Imager 0.39 of Nov 2001 for oop style tags, or something post 0.20
29 # for the oopery, but don't think need to force that here (just list in the
30 # Makefile.PL PREREQ_PM)
31 3 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 3 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 ▶use Image::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 @ISA = ('Image::Base');
37  
38 3 $VERSION = 14;
39  
40 # uncomment this to run the ### lines
41 # use Smart::Comments '###';
42  
43  
44 # As of Imager 0.79 there's nothing to set the Zlib compression level for a
45 # -zlib_compression attribute.
46 #
47 # An -allow_partial could set allow_partial=> on read().
48 #
49  
50 26 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
Pod documented
51 26 my ($class, %params) = @_;
52 ### Image-Base-Imager new(): %params
53  
54 # $obj->new(...) means make a copy, with some extra settings
55 26 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 1 25 26
56 1 my $self = $class;
57 1 $class = ref $class;
58 1 ▶ if (! defined $params{'-imager'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"-imager"}) 1 0 1
59 1 $params{'-imager'} = $self->get('-imager')->copy;
60 }
61 # inherit everything else
62 1 %params = (%$self, %params);
63 ### copy params: \%params
64 }
65  
66 26 my $want_load = 1;
67 26 ▶ if (! defined $params{'-imager'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"-imager"}) 22 4 26
68 22 my $width = delete $params{'-width'};
69 22 my $height = delete $params{'-height'};
70 22 my $filename = $params{'-file'};
71 22 ▶ if (! defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 19 3 22
72 # default 1x1 image since xsize=>undef,ysize=>undef is a 0x0
73 # nothingness where settag() won't store -file_format
74 19 ▶ if (! defined $width) { $width = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $width) 4 15 19
75 19 ▶ if (! defined $height) { $height = 1; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $height) 4 15 19
76 }
77 22 ▶ $params{'-imager'} = Imager->new (xsize => $width,
Condition100%2/2
'Imager'->new('xsize', $width, 'ysize', $height, 'file', $filename) || croak('Cannot create image: ', 'Imager'->errstr)
l!l
211
MC/DC100%1/1
'Imager'->new('xsize', $width, 'ysize', $height, 'file', $filename) || croak('Cannot create image: ', 'Imager'->errstr)
'Imager'->new('xsize', $width, 'ysize', $height, 'file', $filename)
Truth table100%2/2
A
A= 'Imager'->new('xsize', $width, 'ysize', $height, 'file', $filename)
Aresult
11
00
78 ysize => $height,
79 file => $filename)
80 || croak "Cannot create image: ",Imager->errstr;
81 # set -file as filename, but have already loaded
82 21 $want_load = 0;
83 }
84 25 my $self = bless {}, $class;
85 25 $self->set (%params);
86  
87 25 ▶ if ($want_load && defined $params{'-file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_load and defined $params{"-file"}) 0 25 25
Condition66%2/3
$want_load and defined $params{"-file"}
!ll&&!rl&&r
2140
MC/DC0%0/2
$want_load and defined $params{"-file"}
$want_load defined $params{"-file"}
Truth table66%2/3
A and B
A= $want_loadB= defined $params{"-file"}
ABresult
0X0
100
111
88 0 $self->load;
89 }
90  
91 ### new made: $self
92 25 return $self;
93 }
94  
95  
96 3 my %attr_to_tag = (-hotx => 'cur_hotspotx', # get and set
97 -hoty => 'cur_hotspoty',
98 );
99 3 my %attr_to_get_method = (-width => 'getwidth',
100 -height => 'getheight',
101 -ncolours => 'colorcount',
102 -file_format => \&_imager_get_file_format,
103 );
104 1033 ▶sub _get {
Subroutine _get: called CC=3 SCAR=11.0
105 1033 my ($self, $key) = @_;
106 ### Image-Base-Imager _get(): $key
107  
108 1033 ▶ if (my $tag = $attr_to_tag{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $attr_to_tag{$key}) 6 1027 1033
109 ### $tag
110 ### is: [$self->{'-imager'}->tags(name=>$tag)]
111 6 return scalar(($self->{'-imager'}->tags(name=>$tag))[0]);
112 }
113 1027 ▶ if (my $method = $attr_to_get_method{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $attr_to_get_method{$key}) 1024 3 1027
114 ### $method
115 ### is: $self->{'-imager'}->$method()
116 1024 return $self->{'-imager'}->$method();
117 }
118 3 return $self->SUPER::_get ($key);
119 }
120 19 ▶sub _imager_get_file_format {
Subroutine _imager_get_file_format: called CC=1 SCAR=0.0
121 19 my ($i) = @_;
122 ### _imager_get_file_format() from tags: [$i->tags]
123 # tags() returns a list of the values
124 19 return scalar(($i->tags (name => 'i_format'))[0]);
125 }
126  
127 3 my %attr_to_img_set = (-width => 'xsize',
128 -height => 'ysize',
129 );
130 45 ▶sub set {
Subroutine set: called CC=11 SCAR=24.0
Pod documented
131 45 my ($self, %param) = @_;
132 ### Image-Base-Imager set(): \%param
133  
134 45 foreach my $key ('-ncolours') {
135 45 ▶ if (exists $param{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$key}) 0 45 45
136 0 croak "Attribute $key is read-only";
137 }
138 }
139  
140 # apply this first
141 45 ▶ if (my $i = delete $param{'-imager'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = delete $param{"-imager"}) 25 20 45
142 25 $self->{'-imager'} = $i;
143 }
144  
145 45 my $i = $self->{'-imager'};
146 45 ▶ if (exists $param{'-file_format'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"-file_format"}) 11 34 45
147 11 my $format = delete $param{'-file_format'};
148 11 ▶ if (defined $format) { $format = lc($format); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) 10 1 11
149 ### apply -file_format with settag() i_format: $format
150 11 $i->settag (name => 'i_format', value => $format);
151 ### tags now: [$i->tags]
152 }
153 45 foreach my $key (keys %param) {
154 32 ▶ if (my $tag = $attr_to_tag{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $attr_to_tag{$key}) 6 26 32
155 ### settag: $tag
156 6 $i->settag (name => $tag, value => delete $param{$key});
157 ### tags now: [$i->tags]
158 }
159 }
160  
161 45 my @set;
162 45 foreach my $key (keys %param) {
163 26 ▶ if (my $attribute = $attr_to_img_set{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute = $attr_to_img_set{$key}) 4 22 26
164 4 push @set, $attribute, delete $param{$key};
165 }
166 }
167 45 ▶ if (@set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@set) 2 43 45
168 ### @set
169 2 $i->img_set(@set);
170 }
171  
172 45 %$self = (%$self, %param);
173 }
174  
175 2 ▶sub load {
Subroutine load: called CC=3 SCAR=11.2
Pod documented
176 2 my ($self, $filename) = @_;
177 ### Image-Base-Imager load(): @_
178 2 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 2 2
179 0 $filename = $self->get('-file');
180 } else {
181 2 $self->set('-file', $filename);
182 }
183 2 my $i = $self->{'-imager'};
184 2 ▶ $i->read (file => $filename)
Branch: 1 branch
BranchTrueFalseTotal
$i->read("file", $filename) or croak("Cannot load: ", $i->errstr) 1 1 2
185 or croak "Cannot load: ",$i->errstr;
186 ### $i
187 ### size: $i->getwidth.'x'.$i->getheight
188 ### tags: [$i->tags]
189 }
190  
191 # not yet documented ...
192 1 ▶sub load_fh {
Subroutine load_fh: called CC=2 SCAR=7.1
Pod undocumented
193 1 my ($self, $fh) = @_;
194 1 my $i = $self->{'-imager'};
195 1 ▶ $i->read (fh => $fh)
Branch: 1 branch
BranchTrueFalseTotal
$i->read("fh", $fh) or croak("Cannot load: ", $i->errstr) 1 0 1
196 or croak "Cannot load: ",$i->errstr;
197 }
198  
199 12 ▶sub save {
Subroutine save: called CC=3 SCAR=11.1
Pod documented
200 12 my ($self, $filename) = @_;
201 ### Image-Base-Imager save(): @_
202 12 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 12 0 12
203 12 $self->set('-file', $filename);
204 } else {
205 0 $filename = $self->get('-file');
206 }
207 12 my $i = $self->{'-imager'};
208 12 my $type = _imager_get_file_format($i);
209 12 my $quality = $self->{'-quality_percent'};
210 ### file: $filename
211 ### type: $type
212  
213 # think it's ok to pass undef as $quality, and that the options can be
214 # passed even when not saving to the respective formats
215 12 ▶ $i->write (file => $filename,
Branch: 1 branch
BranchTrueFalseTotal
$i->write("file", $filename, "type", $type, "jpegquality", $quality, "tiff_jpegquality", $quality) or croak("Cannot save: ", $i->errstr) 10 2 12
216 type => $type,
217 jpegquality => $quality,
218 tiff_jpegquality => $quality)
219 or croak "Cannot save: ",$i->errstr;
220 }
221  
222 # not yet documented ...
223 1 ▶sub save_fh {
Subroutine save_fh: called CC=2 SCAR=7.1
Pod undocumented
224 1 my ($self, $fh) = @_;
225 1 my $i = $self->{'-imager'};
226 1 ▶ $i->write (fh => $fh,
Branch: 1 branch
BranchTrueFalseTotal
$i->write("fh", $fh, "type", _imager_get_file_format($i)) or croak("Cannot save: ", $i->errstr) 1 0 1
227 type => _imager_get_file_format($i))
228 or croak "Cannot save: ",$i->errstr;
229 }
230  
231 2722 ▶sub xy {
Subroutine xy: called CC=3 SCAR=11.0
Pod documented
232 2722 my ($self, $x, $y, $colour) = @_;
233 ### Image-Base-Imager xy: $x,$y,$colour
234 2722 my $i = $self->{'-imager'};
235 2722 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) { } 42 2680 2722
236 42 $i->setpixel (x => $x, y => $y, color => $colour);
237  
238 } else {
239 2680 my $cobj = $i->getpixel (x => $x, y => $y);
240 2680 ▶ if (! defined $cobj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cobj) 6 2674 2680
241 # getpixel() returns undef if x,y outside image size
242 6 return undef;
243 }
244 2674 my @rgba = $cobj->rgba;
245 ### @rgba
246 # if ($a == 0) {
247 # return 'None';
248 # }
249 2674 return sprintf ('#%02X%02X%02X', @rgba[0,1,2]);
250 }
251 }
252 40 ▶sub line {
Subroutine line: called CC=1 SCAR=0.0
Pod documented
253 40 my ($self, $x1, $y1, $x2, $y2, $colour) = @_;
254 ### Image-Base-Imager line: @_
255 40 $self->{'-imager'}->line (x1 => $x1,
256 y1 => $y1,
257 x2 => $x2,
258 y2 => $y2,
259 color => $colour);
260 }
261 170 ▶sub rectangle {
Subroutine rectangle: called CC=1 SCAR=0.0
Pod documented
262 170 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
263 ### Image-Base-Imager rectangle: @_
264  
265 170 $self->{'-imager'}->box (xmin => $x1,
266 ymin => $y1,
267 xmax => $x2,
268 ymax => $y2,
269 color => $colour,
270 filled => $fill);
271 }
272  
273 26 ▶sub ellipse {
Subroutine ellipse: called CC=3 SCAR=11.0
Pod documented
274 26 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
275 ### Image-Base-Imager ellipse: "$x1, $y1, $x2, $y2, $colour"
276  
277 26 my $diam = $x2-$x1;
278 26 ▶ if (! ($diam & 1) && $y2-$y1 == $diam) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $diam & 1 and $y2 - $y1 == $diam) { } 6 20 26
Condition100%3/3
not $diam & 1 and $y2 - $y1 == $diam
!ll&&!rl&&r
8126
MC/DC100%2/2
not $diam & 1 and $y2 - $y1 == $diam
not $diam & 1 $y2 - $y1 == $diam
Truth table100%3/3
not(A) and B
A= not $diam & 1B= $y2 - $y1 == $diam
ABresult
0X0
100
111
279 ### use circle
280 6 $self->{'-imager'}->circle (x => ($x2+$x1)/2,
281 y => ($y2+$y1)/2,
282 r => $diam/2,
283 color => $colour,
284 filled => $fill);
285 } else {
286 ### use superclass ellipse
287 20 shift->SUPER::ellipse (@_);
288 }
289 }
290  
291 26 ▶sub diamond {
Subroutine diamond: called CC=7 SCAR=19.5
Pod documented
292 26 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
293 ### Image-Base-Imager diamond() ...
294  
295 # $imager->polygon() for filled poly is always anti-alias, but don't want
296 # that, or not by default, so polyline() for unfilled and Image::Base for
297 # filled
298  
299 26 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) { } 13 13 26
300 13 shift->SUPER::diamond(@_);
301  
302 } else {
303 # 0 1 2 3 4
304 # x1=0, x2=4 -> xh=2
305 #
306 # 0 1 2 3 4 5
307 # x1=0, x2=5 -> xh=2
308 #
309 13 my $xh = ($x2 - $x1);
310 13 my $yh = ($y2 - $y1);
311 13 my $xeven = ($xh & 1);
312 13 my $yeven = ($yh & 1);
313 13 $xh = int($xh / 2);
314 13 $yh = int($yh / 2);
315 ### assert: $x1+$xh == $x2-$xh || $x1+$xh+1 == $x2-$xh
316 ### assert: $y1+$yh == $y2-$yh || $y1+$yh+1 == $y2-$yh
317  
318 13 ▶ $self->{'-imager'}->polyline (points => [ [$x1+$xh,$y1], # top centre
Branch: 5 branches
BranchTrueFalseTotal
$yeven ? : 6 7 13
$xeven ? : 4 9 13
$yeven ? : 6 7 13
$xeven ? : 4 9 13
$fill ? : 0 13 13
319  
320 # left
321 [$x1,$y1+$yh],
322 ($yeven ? [$x1,$y2-$yh] : ()),
323  
324 # bottom
325 [$x1+$xh,$y2],
326 ($xeven ? [$x2-$xh,$y2] : ()),
327  
328 # right
329 ($yeven ? [$x2,$y2-$yh] : ()),
330 [$x2,$y1+$yh],
331  
332 ($xeven ? [$x2-$xh,$y1] : ()),
333 ($fill ? () : [$x1+$xh,$y1]),
334 ],
335 color => $colour);
336 }
337 }
338  
339 #------------------------------------------------------------------------------
340 # colours
341  
342 0 ▶sub add_colours {
Subroutine add_colours: not called CC=1 SCAR=6.9
Pod documented
343 0 my $self = shift;
344 ### add_colours: @_
345 0 $self->{'-imager'}->addcolors (colors => \@_);
346 }
347  
348  
349 # sub _validate_file_format {
350 # my ($format) = @_;
351 # if (! defined $format) {
352 # return; # undef is ok
353 # }
354 #
355 # # in Imager 0.80 'cur' works but isn't in the types lists
356 # my $lform = lc($format);
357 # foreach my $f ('cur', Imager->read_types, Imager->write_types) {
358 # if ($lform eq $f) {
359 # return;
360 # }
361 # }
362 #
363 # croak 'Unrecognised -file_format: ',$format;
364 # }
365  
366 3 1;
367 __END__
↑ Summary