blib/lib/Image/Base/GD.pm

filter: Statementstmt 28.0% 63 / 225 Branchbran 14.1% 19 / 134 Conditioncond 3.8% 2 / 52 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 52.6% 10 / 19 Podpod 90.9% 10 / 11 totaltotal 22.2% 104 / 468 CC 88 SCAR 46.6
CC
88
Cov
20%
load
227.6
save
182.0
colour_to_index
110.0
CRAP 105.9
↑ Summary
1 # Copyright 2010, 2011, 2012, 2019, 2024 Kevin Ryde
2  
3 # This file is part of Image-Base-GD.
4 #
5 # Image-Base-GD 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-GD 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-GD. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::GD;
20 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 11 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 $VERSION = 17;
27  
28 11 ▶use Image::Base 1.12; # version 1.12 for ellipse() $fill
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 @ISA = ('Image::Base');
30  
31 # uncomment this to run the ### lines
32 #use Smart::Comments '###';
33  
34  
35 4 ▶sub new {
Subroutine new: called CC=6 SCAR=21.5
Pod documented
36 4 my ($class, %params) = @_;
37 ### Image-Base-GD new(): %params
38  
39 # $obj->new(...) means make a copy, with some extra settings
40 4 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 4 4
41 0 my $self = $class;
42 0 $class = ref $self;
43 0 ▶ if (! defined $params{'-gd'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"-gd"}) 0 0 0
44 0 $params{'-gd'} = $self->get('-gd')->clone;
45 }
46 # inherit everything else
47 0 %params = (%$self, %params);
48 ### copy params: \%params
49 }
50  
51 4 my $self = bless { -allocate_colours => 1,
52 -zlib_compression => -1,
53 -file_format => 'png' }, $class;
54 4 ▶ if (! defined $params{'-gd'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"-gd"}) 3 1 4
55 3 ▶ if (defined (my $filename = delete $params{'-file'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = delete $params{'-file'})) { } 1 2 3
56 1 $self->load ($filename);
57  
58 } else {
59 2 my $truecolor = !! delete $params{'-truecolor'};
60 2 my $width = delete $params{'-width'};
61 2 my $height = delete $params{'-height'};
62 2 require GD;
63 0 ▶ my $gd = $self->{'-gd'} = GD::Image->new ($width, $height, $truecolor)
Condition0%0/2
'GD::Image'->new($width, $height, $truecolor) || croak('Cannot create GD')
l!l
00
MC/DC0%0/1
'GD::Image'->new($width, $height, $truecolor) || croak('Cannot create GD')
'GD::Image'->new($width, $height, $truecolor)
Truth table0%0/2
A
A= 'GD::Image'->new($width, $height, $truecolor)
Aresult
11
00
64 || croak "Cannot create GD"; # undef if cannot create
65 0 $gd->alphaBlending(0);
66 }
67 }
68 1 $self->set (%params);
69 ### new made: $self
70 1 return $self;
71 }
72  
73 11 my %attr_to_get_method = (-width => 'width',
74 -height => 'height',
75 -ncolours => 'colorsTotal',
76  
77 # these not documented yet ...
78 -truecolor => 'isTrueColor',
79 -interlaced => 'interlaced');
80 0 ▶sub _get {
Subroutine _get: not called CC=2 SCAR=17.9
81 0 my ($self, $key) = @_;
82 ### Image-Base-GD _get(): $key
83  
84 0 ▶ if (my $method = $attr_to_get_method{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $attr_to_get_method{$key}) 0 0 0
85 0 return $self->{'-gd'}->$method;
86 }
87 0 return $self->SUPER::_get ($key);
88 }
89  
90 2 ▶sub set {
Subroutine set: called CC=8 SCAR=32.5
Pod documented
91 2 my ($self, %param) = @_;
92 ### Image-Base-GD set(): \%param
93  
94 2 foreach my $key ('-width', '-height', '-ncolours') {
95 6 ▶ if (exists $param{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$key}) 0 6 6
96 0 croak "Attribute $key is read-only";
97 }
98 }
99  
100 # these not documented yet ...
101 2 ▶ if (exists $param{'-interlaced'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"-interlaced"}) 0 2 2
102 0 $self->{'-gd'}->interlaced (delete $param{'-interlaced'});
103 }
104 2 ▶ if (exists $param{'-truecolor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"-truecolor"}) 0 2 2
105 0 my $gd = $self->{'-gd'};
106 0 ▶ if (delete $param{'-truecolor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $param{'-truecolor'}) { } 0 0 0
107 0 ▶ if (! $gd->isTrueColor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gd->isTrueColor) 0 0 0
108 0 die "How to turn palette into truecolor?";
109 }
110 } else {
111 0 ▶ if ($gd->isTrueColor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->isTrueColor) 0 0 0
112 0 $gd->trueColorToPalette;
113 }
114 }
115 }
116  
117 2 %$self = (%$self, %param);
118 }
119  
120 1 ▶sub load {
Subroutine load: called CC=24 SCAR=54.3
Pod documented
121 1 my ($self, $filename) = @_;
122 1 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 1 1
123 0 $filename = $self->{'-file'};
124 } else {
125 1 $self->set('-file', $filename);
126 }
127  
128 1 my $fh;
129 1 ▶ open $fh, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $filename or croak("Cannot open file ${filename}: $!") 1 0 1
130 or croak "Cannot open file $filename: $!";
131 1 ▶ binmode $fh
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or croak("Error setting binary mode: ", $!) 1 0 1
132 or croak 'Error setting binary mode: ',$!;
133  
134 1 my $filepos = tell($fh);
135 1 my $bytes = '';
136 1 read($fh,$bytes,9);
137 ### $bytes
138  
139 1 my $file_format;
140 my $method;
141 0 ▶ if ($bytes =~ /^\x89PNG/) { $file_format = 'png'; }
Branch: 8 branches
BranchTrueFalseTotal
if ($bytes =~ /^\x89PNG/) { } 0 1 1
elsif ($bytes =~ /^\xFF\xD8/) { } 0 1 1
elsif ($bytes =~ /^GIF8/) { } 0 1 1
elsif ($bytes =~ /^gd2\0/) { } 0 1 1
elsif ($bytes =~ m[^/\* XPM \*/]) { } 0 1 1
elsif ($bytes =~ /^#define /) { } 1 0 1
elsif ($bytes =~ /^\0\0/) { } 0 0 0
elsif ($bytes =~ /^\xFF[\xFF\xFE]/ or length $bytes >= 4 and do { () = unpack('nn', $bytes); -s $fh == 775 + $width * $height }) { } 0 0 0
142 0 elsif ($bytes =~ /^\xFF\xD8/) { $file_format = 'jpeg'; }
143 0 elsif ($bytes =~ /^GIF8/) { $file_format = 'gif'; }
144 0 elsif ($bytes =~ /^gd2\0/) { $file_format = 'gd2'; }
145 0 elsif ($bytes =~ m{^/\* XPM \*/}) { $file_format = 'xpm'; }
146 1 elsif ($bytes =~ m/^#define /) { $file_format = 'xbm';
147 1 $method = "_newFromXbm"; }
148 0 elsif ($bytes =~ m/^\0\0/) { $file_format = 'wbmp';
149 0 $method = "_newFromWBMP"; }
150  
151 # Image::WMF (as of 1.01) doesn't have a file reader to then extend perhaps.
152 # elsif ($bytes =~ m/^\327\315\306\232/) {
153 # require Image::WMF;
154 # my $class = 'Image::WMF';
155 # $file_format = 'wmf';
156 # $method = "newFromWMF"; }
157  
158 # GD::SVG (as of 0.33) doesn't have a file reader to then extend perhaps.
159 # elsif ($bytes =~ m/^<?xml/) {
160 # require GD::SVG;
161 # my $class = 'GD::SVG::Image';
162 # $file_format = 'svg';
163 # $method = "newFromSVG"; }
164  
165 ▶ elsif ($bytes =~ /^\xFF[\xFF\xFE]/
Condition0%0/6
$bytes =~ /^\xFF[\xFF\xFE]/ or length $bytes >= 4 and do { () = unpack("nn", $bytes); -s $fh == 775 + $width * $height }
l!l&&r!l&&!r
000
length $bytes >= 4 and do { () = unpack("nn", $bytes); -s $fh == 775 + $width * $height }
!ll&&!rl&&r
000
MC/DC0%0/3
$bytes =~ /^\xFF[\xFF\xFE]/ or length $bytes >= 4 and do { () = unpack("nn", $bytes); -s $fh == 775 + $width * $height }
$bytes =~ /^\xFF[\xFF\xFE]/ length $bytes >= 4 do { () = unpack("nn", $bytes); -s $fh == 775 + $width * $height }
Truth table0%0/4
A or B and C
A= $bytes =~ /^\xFF[\xFF\xFE]/B= length $bytes >= 4C= do { () = unpack("nn", $bytes); -s $fh == 775 + $width * $height }
ABCresult
1XX1
0111
00X0
0100
166 || (length($bytes) >= 4
167 && do {
168 0 my ($width, $height) = unpack 'nn', $bytes;
169 0 -s $fh == 4 + 3 + 256*3 + $width * $height
170 })) {
171 0 $file_format = 'gd';
172 } else {
173 0 croak "Unrecognised file format";
174 }
175 1 ▶ $method ||= "newFrom\u$file_format";
Condition50%1/2
$method ||= "newFrom\u$file_format"
l!l
10
MC/DC0%0/1
$method ||= "newFrom\u$file_format"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
176 ### $method
177  
178 1 my $fh_filename = $filename;
179 1 ▶ if ($file_format eq 'xpm' || ! seek($fh,$filepos,0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_format eq "xpm" or not seek($fh, $filepos, 0)) 0 1 1
Condition33%1/3
$file_format eq "xpm" or not seek($fh, $filepos, 0)
l!l&&r!l&&!r
001
MC/DC0%0/2
$file_format eq "xpm" or not seek($fh, $filepos, 0)
$file_format eq "xpm" not seek($fh, $filepos, 0)
Truth table33%1/3
A or not(B)
A= $file_format eq "xpm"B= not seek($fh, $filepos, 0)
ABresult
1X1
011
000
180 0 require File::Temp;
181 0 my $tempfh = File::Temp->new (UNLINK => 0);
182 0 ▶ binmode $tempfh or croak 'Error setting binary mode: ',$!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $tempfh or croak("Error setting binary mode: ", $!) 0 0 0
183  
184 0 my $rest = do { local $/; <$fh> }; # slurp
185 0 ▶ print $tempfh $bytes, $rest or croak 'Error writing temp file: ',$!;
Branch: 1 branch
BranchTrueFalseTotal
print $tempfh $bytes, $rest or croak("Error writing temp file: ", $!) 0 0 0
186 0 ▶ seek $tempfh, 0, 0 or croak "Error rewinding temp file: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $tempfh, 0, 0 or croak("Error rewinding temp file: $!") 0 0 0
187  
188 # require File::Copy;
189 # File::Copy::copy($fh,$tempfh)
190 # or croak "Error copying $filename: $!";
191 ### input size: -s $fh
192 ### copied size: -s $tempfh
193 ### tell fh: tell($fh)
194 ### tell temp: tell($tempfh)
195  
196 0 ▶ close $fh or croak "Error closing $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ${filename}: $!") 0 0 0
197  
198 0 $fh = $tempfh;
199 0 $fh_filename = $tempfh->filename;
200 }
201 ### tell: tell($fh)
202  
203 1 require GD;
204 0 my $gd;
205 0 ▶ if ($file_format eq 'xpm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_format eq 'xpm') { } 0 0 0
206 # newFromXpm() will only read a filename, not a handle
207 ### newFromXpm(): $fh_filename
208 0 $gd = GD::Image->newFromXpm($fh_filename);
209 } else {
210 0 $gd = GD::Image->$method($fh);
211 }
212 ### $gd
213  
214 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ${fh_filename}: $!") 0 0 0
215 or croak "Error closing $fh_filename: $!";
216  
217 0 ▶ if (! $gd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gd) 0 0 0
218 0 croak "Unrecognised data or error reading ",$filename;
219  
220 # undef $@;
221 # my $err = $@;
222 # newFromXpm() error message dodgy
223 # if (defined $err) {
224 # croak $err;
225 # } else {
226 # }
227 }
228  
229 0 $self->{'-gd'} = $gd;
230 0 $self->{'-file_format'} = $file_format;
231 0 $gd->alphaBlending(0);
232 }
233  
234 # check -file_format, don't call an arbitrary func/method through its name
235 11 my %file_format_save_method = (jpeg => 'jpeg',
236 gif => 'gif',
237 gd => 'gd',
238 gd2 => 'gd2',
239 png => 'png',
240 svg => 'svg', # experimental for GD::SVG::Image
241 wmf => 'wmf', # experimental for Image::WMF
242 );
243 11 my %text_mode = (svg => 1);
244  
245 0 ▶sub save {
Subroutine save: not called CC=13 SCAR=52.0
Pod documented
246 0 my ($self, $filename) = @_;
247 ### Image-Base-GD save(): @_
248 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
249 0 $self->set('-file', $filename);
250 } else {
251 0 $filename = $self->{'-file'};
252 }
253 ### $filename
254  
255 0 my $gd = $self->{'-gd'};
256 0 my $file_format;
257 0 ▶ if (defined ($file_format = $self->{'-file_format'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($file_format = $self->{'-file_format'})) { } 0 0 0
258 0 $file_format = lc($file_format);
259 } else {
260 0 $file_format = 'png'; # default
261 }
262  
263 0 my $data;
264 0 ▶ if ($file_format eq 'png') {
Branch: 4 branches
BranchTrueFalseTotal
if ($file_format eq 'png') { } 0 0 0
elsif ($file_format eq 'jpeg') { } 0 0 0
elsif ($file_format eq 'wbmp') { } 0 0 0
elsif (my $method = $file_format_save_method{$file_format}) { } 0 0 0
265 0 $data = $gd->png ($self->get('-zlib_compression'));
266 } elsif ($file_format eq 'jpeg') {
267 0 my $quality = $self->get('-quality_percent');
268 0 ▶ $data = $gd->jpeg (defined $quality ? $quality : -1);
Branch: 1 branch
BranchTrueFalseTotal
defined $quality ? : 0 0 0
269 } elsif ($file_format eq 'wbmp') {
270 # In libgd 2.0.36 gdImageWBMPCtx() the "foreground" index arg becomes
271 # WBMP_BLACK. In WAP world black is the foreground is it? In any case
272 # 'black' here makes save+load of a GD to wbmp come back the right way
273 # around.
274 # http://www.wapforum.org/what/technical/SPEC-WAESpec-19990524.pdf
275 ### wbmp fg: $self->colour_to_index('black')
276 0 $data = $gd->wbmp ($self->colour_to_index('black'));
277 } elsif (my $method = $file_format_save_method{$file_format}) {
278 0 $data = $gd->$method;
279 } else {
280 0 croak 'Cannot save file format ',$file_format;
281 }
282  
283 # or maybe File::Slurp::write_file($filename,{binmode=>':raw'})
284 0 my $fh;
285 (open $fh, '>', $filename
286 0 ▶ and ($text_mode{$file_format} || binmode($fh))
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $filename and $text_mode{$file_format} || binmode($fh) and print $fh $data and close $fh or croak("Error writing ${filename}: $!") 0 0 0
Condition0%0/12
open $fh, ">", $filename and $text_mode{$file_format} || binmode($fh) and print $fh $data and close $fh
!ll&&!rl&&r
000
open $fh, ">", $filename and $text_mode{$file_format} || binmode($fh) and print $fh $data
!ll&&!rl&&r
000
open $fh, ">", $filename and $text_mode{$file_format} || binmode($fh)
!ll&&!rl&&r
000
$text_mode{$file_format} or binmode $fh
l!l&&r!l&&!r
000
MC/DC0%0/5
open $fh, ">", $filename and $text_mode{$file_format} || binmode($fh) and print $fh $data and close $fh
open $fh, ">", $filename $text_mode{$file_format} binmode $fh print $fh $data close $fh
Truth table0%0/8
A and B or C and D and E
A= open $fh, ">", $filenameB= $text_mode{$file_format}C= binmode $fhD= print $fh $dataE= close $fh
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
287 and print $fh $data
288 and close $fh)
289 or croak "Error writing $filename: $!";
290 }
291  
292 #------------------------------------------------------------------------------
293  
294 0 ▶sub xy {
Subroutine xy: not called CC=4 SCAR=30.0
Pod documented
295 0 my ($self, $x, $y, $colour) = @_;
296 ### Image-Base-GD xy(): $x,$y,$colour
297  
298 0 my $gd = $self->{'-gd'};
299 0 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) { } 0 0 0
300 0 $gd->setPixel ($x, $y, $self->colour_to_index($colour));
301 ### setPixel: $self->colour_to_index($colour)
302 } else {
303 0 my $pixel = $gd->getPixel ($x, $y);
304 #### getPixel: $pixel
305 0 ▶ if ($pixel == $gd->transparent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel == $gd->transparent) 0 0 0
306 #### is transparent
307 0 return 'None';
308 }
309 0 ▶ if ($pixel >= 0x7F000000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pixel >= 2130706432) 0 0 0
310 #### pixel has fully-transparent alpha 0x7F
311 0 return 'None';
312 }
313 #### rgb: $gd->rgb($pixel)
314 0 return sprintf ('#%02X%02X%02X', $gd->rgb($pixel));
315 }
316 }
317  
318 0 ▶sub line {
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
319 0 my ($self, $x1, $y1, $x2, $y2, $colour) = @_;
320 ### Image-Base-GD line(): @_
321  
322 0 $self->{'-gd'}->line ($x1,$y1,$x2,$y2, $self->colour_to_index($colour));
323 }
324  
325 0 ▶sub rectangle {
Subroutine rectangle: not called CC=5 SCAR=34.0
Pod documented
326 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
327 ### Image-Base-GD rectangle(): @_[1..$#_]
328  
329 # ### index: $self->colour_to_index($colour)
330  
331 # libgd circa 2.0.35 gdImageFilledRectangle() has a bug where if the x1,x2
332 # range is all negative then it draws a pixels in the x=0 left edge. Or
333 # similarly if y1,y2 all negative then it draws in the y=0 top edge.
334 # Think this is a bug, the comments in the code suggest it's supposed to
335 # drawn nothing for all-negative. In any case avoid this in the interests
336 # of behaving like other Image-Base new style clipping 0,0,width,height.
337 #
338 0 ▶ if ($x2 < 0 || $y2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 < 0 or $y2 < 0) 0 0 0
Condition0%0/3
$x2 < 0 or $y2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0 $y2 < 0
Truth table0%0/3
A or B
A= $x2 < 0B= $y2 < 0
ABresult
1X1
011
000
339 ### all negative, workaround to drawn nothing ...
340 0 return;
341 }
342  
343 # libgd circa 2.0.35 has a bug where it draws a $y1==$y2 unfilled
344 # rectangle with dodgy sides like
345 #
346 # * *
347 # ********
348 # * *
349 #
350 # As a workaround send $y1==$y2 to filledRectangle() instead.
351 #
352 0 ▶ my $method = ($fill || $y1 == $y2
Branch: 1 branch
BranchTrueFalseTotal
$fill || $y1 == $y2 ? : 0 0 0
Condition0%0/3
$fill or $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/2
$fill or $y1 == $y2
$fill $y1 == $y2
Truth table0%0/3
A or B
A= $fillB= $y1 == $y2
ABresult
1X1
011
000
353 ? 'filledRectangle'
354 : 'rectangle');
355 0 $self->{'-gd'}->$method ($x1,$y1,$x2,$y2, $self->colour_to_index($colour));
356 }
357  
358 0 ▶sub ellipse {
Subroutine ellipse: not called CC=7 SCAR=40.3
Pod documented
359 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
360 ### Image-GD ellipse: "$x1, $y1, $x2, $y2, $colour, ".($fill||0)
361  
362 # If width $xw or height $yw is an odd number then GD draws the extra
363 # pixel on the higher value side, ie. the centre is the rounded-down
364 # position. Dunno if that should be relied on.
365 #
366 # some versions of libgd prior to 2.0.36 seem to draw nothing for
367 # filledEllipse() on an x1==x2 y1==y2 single-pixel ellipse. Try sending 1
368 # or 2 pixel wide or high to the base ellipse() and from there to
369 # filledRectangle() instead.
370 #
371 0 my $xw = $x2 - $x1;
372 0 my $yw = $y2 - $y1;
373 0 my $gd = $self->{'-gd'};
374 0 ▶ if ($gd->isa('GD::SVG::Image')
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->isa('GD::SVG::Image') or $xw > 1 and not $xw & 1 and $yw > 1 and not $yw & 1) { } 0 0 0
Condition0%0/12
$gd->isa("GD::SVG::Image") or $xw > 1 and not $xw & 1 and $yw > 1 and not $yw & 1
l!l&&r!l&&!r
000
$xw > 1 and not $xw & 1 and $yw > 1 and not $yw & 1
!ll&&!rl&&r
000
$xw > 1 and not $xw & 1 and $yw > 1
!ll&&!rl&&r
000
$xw > 1 and not $xw & 1
!ll&&!rl&&r
000
MC/DC0%0/5
$gd->isa("GD::SVG::Image") or $xw > 1 and not $xw & 1 and $yw > 1 and not $yw & 1
$gd->isa("GD::SVG::Image") $xw > 1 not $xw & 1 $yw > 1 not $yw & 1
Truth table0%0/6
A or B and not(C) and D and not(E)
A= $gd->isa("GD::SVG::Image")B= $xw > 1C= not $xw & 1D= $yw > 1E= not $yw & 1
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
375 || ($xw > 1 && ! ($xw & 1)
376 && $yw > 1 && ! ($yw & 1))) {
377 ### x centre: $x1 + $xw/2
378 ### y centre: $y1 + $yw/2
379 ### $xw+1
380 ### $yw+1
381 0 ▶ my $method = ($fill ? 'filledEllipse' : 'ellipse');
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
382 0 $gd->$method ($x1 + $xw/2, $y1 + $yw/2,
383 $xw+1, $yw+1,
384 $self->colour_to_index($colour));
385 } else {
386 ### use Image-Base by pixels ...
387 0 shift->SUPER::ellipse(@_);
388 }
389 }
390  
391 0 ▶sub diamond {
Subroutine diamond: not called CC=8 SCAR=42.8
Pod documented
392 0 my ($self, $x1, $y1, $x2, $y2, $colour, $fill) = @_;
393 ### Image-Base-GD diamond() ...
394  
395 0 my $gd = $self->{'-gd'};
396 0 ▶ if ($x1 == $x2 || $y1 == $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 == $x2 or $y1 == $y2) { } 0 0 0
Condition0%0/3
$x1 == $x2 or $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/2
$x1 == $x2 or $y1 == $y2
$x1 == $x2 $y1 == $y2
Truth table0%0/3
A or B
A= $x1 == $x2B= $y1 == $y2
ABresult
1X1
011
000
397 # as of libgd 2.0.36 a filledpolygon of 1x1 or Nx1 draws no pixels, go
398 # to rectangle in that case
399 0 $gd->filledRectangle ($x1,$y1,$x2,$y2,
400 $self->colour_to_index($colour));
401  
402 } else {
403 0 my $xh = ($x2 - $x1);
404 0 my $yh = ($y2 - $y1);
405 0 my $xeven = ($xh & 1);
406 0 my $yeven = ($yh & 1);
407 0 $xh = int($xh / 2);
408 0 $yh = int($yh / 2);
409 ### assert: $x1+$xh+$xeven == $x2-$xh
410 ### assert: $y1+$yh+$yeven == $y2-$yh
411  
412 0 my $poly = GD::Polygon->new;
413 0 $poly->addPt ($x1+$xh,$y1); # top centre
414  
415 # left
416 0 $poly->addPt ($x1,$y1+$yh);
417 0 ▶ if ($yeven) { $poly->addPt ($x1,$y2-$yh); }
Branch: 1 branch
BranchTrueFalseTotal
if ($yeven) 0 0 0
418  
419 # bottom
420 0 $poly->addPt ($x1+$xh,$y2);
421 0 ▶ if ($xeven) { $poly->addPt ($x2-$xh,$y2); }
Branch: 1 branch
BranchTrueFalseTotal
if ($xeven) 0 0 0
422  
423 # right
424 0 ▶ if ($yeven) { $poly->addPt ($x2,$y2-$yh); }
Branch: 1 branch
BranchTrueFalseTotal
if ($yeven) 0 0 0
425 0 $poly->addPt ($x2,$y1+$yh);
426  
427 # top again
428 0 ▶ if ($xeven) { $poly->addPt ($x2-$xh,$y1); }
Branch: 1 branch
BranchTrueFalseTotal
if ($xeven) 0 0 0
429  
430 ### $poly
431 0 ▶ my $method = ($fill ? 'filledPolygon' : 'openPolygon');
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
432 0 $gd->$method ($poly, $self->colour_to_index($colour));
433 }
434 }
435  
436 #------------------------------------------------------------------------------
437 # colours
438  
439 0 ▶sub add_colours {
Subroutine add_colours: not called CC=9 SCAR=45.0
Pod documented
440 0 my $self = shift;
441 ### add_colours: @_
442  
443 0 my $gd = $self->{'-gd'};
444 0 ▶ if ($gd->isTrueColor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->isTrueColor) 0 0 0
445 ### no allocation in truecolor
446 0 return;
447 }
448  
449 0 foreach my $colour (@_) {
450 ### $colour
451 0 ▶ if ($colour eq 'None') {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour eq 'None') { } 0 0 0
452 0 ▶ if ($gd->transparent() != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->transparent != -1) 0 0 0
453 ### transparent already: $gd->transparent()
454 0 next;
455 }
456 0 ▶ if ((my $index = $self->{'-gd'}->colorAllocateAlpha(0,0,0,127)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $index = $self->{"-gd"}->colorAllocateAlpha(0, 0, 0, 127)) != -1) 0 0 0
457 0 $gd->transparent ($index);
458 ### transparent now: $gd->transparent
459 0 next; # successful
460 }
461  
462 } else {
463 0 my @rgb = _colour_to_rgb255($colour);
464 0 ▶ if ($gd->can('colorExact') # not available in Image::WMF pre 1.03
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->can("colorExact") and $gd->colorExact(@rgb) != -1) 0 0 0
Condition0%0/3
$gd->can("colorExact") and $gd->colorExact(@rgb) != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$gd->can("colorExact") and $gd->colorExact(@rgb) != -1
$gd->can("colorExact") $gd->colorExact(@rgb) != -1
Truth table0%0/3
A and B
A= $gd->can("colorExact")B= $gd->colorExact(@rgb) != -1
ABresult
0X0
100
111
465 && $gd->colorExact(@rgb) != -1) {
466 ### already exists: $gd->colorExact(@rgb)
467 0 next;
468 }
469 0 ▶ if ($gd->colorAllocate(@rgb) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->colorAllocate(@rgb) != -1) 0 0 0
470 ### allocated
471 0 next;
472 }
473 }
474 0 croak "Cannot allocate colour: $colour";
475 }
476 }
477  
478 # not documented yet ...
479 0 ▶sub colour_to_index {
Subroutine colour_to_index: not called CC=10 SCAR=47.0
Pod undocumented
480 0 my ($self, $colour) = @_;
481 ### Image-Base-GD colour_to_index(): $colour
482 0 my $gd = $self->{'-gd'};
483 # while ($gd->isa('GD::Window')) {
484 # $gd = $gd->{im};
485 # }
486  
487 0 ▶ if ($colour eq 'None') {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour eq "None") 0 0 0
488 0 ▶ if ($gd->isTrueColor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->isTrueColor) 0 0 0
489 ### truecolor transparent: $gd->colorAllocateAlpha(0,0,0,127)
490 0 return $gd->colorAllocateAlpha(0,0,0,127);
491 }
492  
493 # Crib note: gdImageColorExactAlpha() doesn't take the single
494 # transparent() colour as equivalent to all transparents but instead
495 # looks for R,G,B to match as well as the alpha.
496 #
497 0 ▶ if ((my $index = $gd->transparent) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $index = $gd->transparent) != -1) 0 0 0
498 ### existing palette transparent: $index
499 0 return $index;
500 }
501 0 ▶ if (! $self->{'-allocate_colours'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"-allocate_colours"}) 0 0 0
502 0 croak "No transparent index set";
503 }
504 0 ▶ if ((my $index = $self->{'-gd'}->colorAllocate(0,0,0)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $index = $self->{"-gd"}->colorAllocate(0, 0, 0)) != -1) 0 0 0
505 0 $gd->transparent ($index);
506 ### transparent now: $gd->transparent
507 0 return $index;
508 }
509 0 croak "No colour cells free to create transparent";
510 }
511  
512 0 my @rgb = _colour_to_rgb255($colour);
513 ### @rgb
514 0 ▶ if ($self->{'-allocate_colours'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-allocate_colours"}) 0 0 0
515 0 ▶ if ($gd->can('colorExact') # not available in Image::WMF
Branch: 1 branch
BranchTrueFalseTotal
if ($gd->can("colorExact") and (my $index = $gd->colorExact(@rgb)) != -1) 0 0 0
Condition0%0/3
$gd->can("colorExact") and (my $index = $gd->colorExact(@rgb)) != -1
!ll&&!rl&&r
000
MC/DC0%0/2
$gd->can("colorExact") and (my $index = $gd->colorExact(@rgb)) != -1
$gd->can("colorExact") (my $index = $gd->colorExact(@rgb)) != -1
Truth table0%0/3
A and B
A= $gd->can("colorExact")B= (my $index = $gd->colorExact(@rgb)) != -1
ABresult
0X0
100
111
516 && (my $index = $gd->colorExact (@rgb)) != -1) {
517 ### existing exact: $index
518 0 return $index;
519 }
520 0 ▶ if ((my $index = $gd->colorAllocate (@rgb)) != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $index = $gd->colorAllocate(@rgb)) != -1) 0 0 0
521 ### allocate: $index
522 0 return $index;
523 }
524 }
525 ### closest: $gd->colorClosest(@rgb)
526 0 return $gd->colorClosest (@rgb);
527 }
528  
529 10 ▶sub _colour_to_rgb255 {
Subroutine _colour_to_rgb255: called CC=3 SCAR=14.8
530 10 my ($colour) = @_;
531  
532 # 1 to 4 digit hex
533 # Crib: [:xdigit:] matches some wide chars, but hex() as of perl 5.12.4
534 # doesn't accept them, so only 0-9A-F
535 10 ▶ if ($colour =~ /^#(([0-9A-F]{3}){1,4})$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colour =~ /^#(([0-9A-F]{3}){1,4})$/i) 10 0 10
536 10 my $len = length($1)/3; # of each group, so 1,2,3 or 4
537 10 return (map {hex(substr($_ x 2, 0, 2))} # first 2 chars of replicated
538 substr ($colour, 1, $len), # full size groups
539 substr ($colour, 1+$len, $len),
540 substr ($colour, -$len));
541 }
542  
543 0 require GD::Simple;
544 0 ▶ if (defined (my $aref = GD::Simple->color_names->{lc($colour)})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $aref = "GD::Simple"->color_names->{lc $colour})) 0 0 0
545 ### table: $aref
546 0 return @$aref;
547 }
548 0 croak "Unknown colour: $colour";
549 }
550  
551 11 1;
552 __END__
↑ Summary