lib/Image/Info.pm

filter: Statementstmt 89.0% 98 / 110 Branchbran 76.2% 61 / 80 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 15 / 15 Podpod 100% 5 / 5 totaltotal 82.8% 188 / 227 CC 48 SCAR 21.4
CC
48
Cov
83%
_source
17.1
determine_file_format
14.0
_image_info_for_format
7.3
CRAP 8.5
« lib/Image/Info/TIFF.pm ↑ Summary lib/Image/Info/ICO.pm »
1  
2 #############################################################################
3 #
4 # ATTENTION! This file is autogenerated from dev/Info.pm.tmpl - DO NOT EDIT!
5 #
6 #############################################################################
7  
8 package Image::Info;
9  
10 # Copyright 1999-2004, Gisle Aas.
11 #
12 # This library is free software; you can redistribute it and/or
13 # modify it under the same terms as Perl v5.8.8 itself.
14 #
15 # Previously maintained by Tels - (c) 2006 - 2008.
16 # Currently maintained by Slaven Rezic - (c) 2008 - 2023.
17  
18 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 25 our $VERSION = '1.45';
21  
22 25 require Exporter;
23 25 *import = \&Exporter::import;
24  
25 25 our @EXPORT_OK = qw(image_info dim html_dim image_type determine_file_format);
26  
27 # already required and failed sub-modules are remembered here
28 25 my %mod_failure;
29  
30 sub image_info
31 85 ▶{
Subroutine image_info: called CC=4 SCAR=14.0
Pod documented
32 85 my $source = _source(shift);
33 85 ▶ return $source if ref $source eq 'HASH'; # Pass on errors
Branch: 1 branch
BranchTrueFalseTotal
if ref $source eq "HASH" 1 84 85
34  
35 # What sort of file is it?
36 84 my $head = _head($source);
37  
38 84 ▶ return $head if ref($head) eq 'HASH'; # error?
Branch: 1 branch
BranchTrueFalseTotal
if ref $head eq "HASH" 0 84 84
39  
40 84 ▶ my $format = determine_file_format($head)
Branch: 1 branch
BranchTrueFalseTotal
my $format = determine_file_format($head) or return {"error", "Unrecognized file format"} 84 0 84
41 or return { error => 'Unrecognized file format' };
42  
43 84 return _image_info_for_format($format, $source);
44 }
45  
46 # Note: this function is private, but may be used by Image::Info::*
47 # implementations (i.e. the WBMP implementation)
48 sub _image_info_for_format
49 85 ▶{
Subroutine _image_info_for_format: called CC=7 SCAR=19.8
50 85 my($format, $source) = @_;
51  
52 85 my $mod = "Image::Info::$format";
53 85 my $sub = "$mod\::process_file";
54 85 my $info = bless [], "Image::Info::Result";
55 85 eval {
56 85 ▶ unless (defined &$sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$sub) 34 51 85
57 # already required and failed?
58 34 ▶ if (my $fail = $mod_failure{$mod}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fail = $mod_failure{$mod}) 0 34 34
59 0 die $fail;
60 }
61 34 eval "require $mod";
62 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 34 34
63 0 $mod_failure{$mod} = $@;
64 0 die $@;
65 }
66 34 ▶ die "$mod did not define &$sub" unless defined &$sub;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$sub 0 34 34
67 }
68  
69 85 my %cnf = @_;
70 {
71 # call process_file()
72 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 85 &$sub($info, $source, \%cnf);
74 }
75 84 $info->clean_up;
76 };
77 85 ▶ return { error => $@ } if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 84 85
78 84 ▶ return wantarray ? @$info : $info->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 78 84
79 }
80  
81 sub image_type
82 2 ▶{
Subroutine image_type: called CC=4 SCAR=14.1
Pod documented
83 2 my $source = _source(shift);
84 2 ▶ return $source if ref $source eq 'HASH'; # Pass on errors
Branch: 1 branch
BranchTrueFalseTotal
if ref $source eq "HASH" 1 1 2
85  
86 # What sort of file is it?
87 1 ▶ my $head = _head($source) or return _os_err("Can't read head");
Branch: 1 branch
BranchTrueFalseTotal
my $head = _head($source) or return _os_err("Can't read head") 1 0 1
88 1 ▶ my $format = determine_file_format($head)
Branch: 1 branch
BranchTrueFalseTotal
my $format = determine_file_format($head) or return {"error", "Unrecognized file format"} 1 0 1
89 or return { error => "Unrecognized file format" };
90  
91 1 return { file_type => $format };
92 }
93  
94 # Note: this function is private, but may be used by Image::Info::*
95 # implementations (i.e. the WBMP implementation)
96 sub _source
97 88 ▶{
Subroutine _source: called CC=10 SCAR=28.4
98 88 my $source = shift;
99 88 ▶ if (!ref $source) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $source) { } 61 27 88
elsif (ref $source eq 'SCALAR') { } 27 0 27
100 61 my $fh;
101 61 ▶ if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.006) { } 0 61 61
102 0 require Symbol;
103 0 $fh = Symbol::gensym();
104 0 ▶ open($fh, $source) || return _os_err("Can't open $source");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $source or return _os_err("Can't open $source") 0 0 0
105 }
106 else {
107 61 ▶ open $fh, '<', $source
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $source or return _os_err("Can't open $source") 59 2 61
108 or return _os_err("Can't open $source");
109 }
110 59 ${*$fh} = $source; # keep filename in case somebody wants to know
111 59 binmode($fh);
112 59 $source = $fh;
113 }
114 elsif (ref($source) eq "SCALAR") {
115 # Earlier PerlIO::scalar versions may segfault or consume lots
116 # of memory for some invalid images, see
117 # RT #100847 and img/segfault.tif
118 27 ▶ if (eval { require PerlIO::scalar; PerlIO::scalar->VERSION(0.21) } ||
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require PerlIO::scalar; 'PerlIO::scalar'->VERSION(0.21) } } or not eval { do { require IO::Scalar; 1 } }) { } 27 0 27
Condition33%1/3
eval { do { require PerlIO::scalar; "PerlIO::scalar"->VERSION("0.21") } } or not eval { do { require IO::Scalar; 1 } }
l!l&&r!l&&!r
2700
MC/DC0%0/2
eval { do { require PerlIO::scalar; "PerlIO::scalar"->VERSION("0.21") } } or not eval { do { require IO::Scalar; 1 } }
eval { do { require PerlIO::scalar; "PerlIO::scalar"->VERSION("0.21") } } not eval { do { require IO::Scalar; 1 } }
Truth table33%1/3
A or not(B)
A= eval { do { require PerlIO::scalar; "PerlIO::scalar"->VERSION("0.21") } }B= not eval { do { require IO::Scalar; 1 } }
ABresult
1X1
011
000
119 0 !eval { require IO::Scalar; 1 }) {
120 27 ▶ open(my $s, "<", $source) or return _os_err("Can't open string");
Branch: 1 branch
BranchTrueFalseTotal
open my $s, "<", $source or return _os_err("Can't open string") 27 0 27
121 27 $source = $s;
122 }
123 else {
124 0 $source = IO::Scalar->new($source);
125 }
126 }
127 else {
128 0 ▶ seek($source, 0, 0) or return _os_err("Can't rewind");
Branch: 1 branch
BranchTrueFalseTotal
seek $source, 0, 0 or return _os_err("Can't rewind") 0 0 0
129 }
130  
131 86 $source;
132 }
133  
134 sub _head
135 85 ▶{
Subroutine _head: called CC=4 SCAR=14.6
136 85 my $source = shift;
137 85 my $head;
138  
139 # Originally was 32 bytes.
140 # In the meantime lowered to 11 bytes.
141 # But XBM probably need more because of a leading comment.
142 85 my $to_read = 64;
143 85 my $read = read($source, $head, $to_read);
144  
145 85 ▶ return _os_err("Couldn't read any bytes") if !$read;
Branch: 1 branch
BranchTrueFalseTotal
unless $read 0 85 85
146  
147 85 ▶ if (ref($source) eq "IO::String") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source eq 'IO::String') { } 0 85 85
148 # XXX workaround until we can trap seek() with a tied file handle
149 0 $source->setpos(0);
150 }
151 else {
152 85 ▶ seek($source, 0, 0) or return _os_err("Can't rewind");
Branch: 1 branch
BranchTrueFalseTotal
seek $source, 0, 0 or return _os_err("Can't rewind") 85 0 85
153 }
154 85 $head;
155 }
156  
157 sub _os_err
158 2 ▶{
Subroutine _os_err: called CC=1 SCAR=0.0
159 2 return { error => "$_[0]: $!",
160 Errno => $!+0,
161 };
162 }
163  
164 sub determine_file_format
165 86 ▶{
Subroutine determine_file_format: called CC=14 SCAR=26.4
Pod documented
166 86 local($_) = @_;
167 86 ▶ return "JPEG" if /^\xFF\xD8/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\xFF\xD8/ 21 65 86
168 65 ▶ return "PNG" if /^\x89PNG\x0d\x0a\x1a\x0a/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\x89PNG\x0d\x0a\x1a\x0a/ 8 57 65
169 57 ▶ return "GIF" if /^GIF8[79]a/;
Branch: 1 branch
BranchTrueFalseTotal
if /^GIF8[79]a/ 9 48 57
170 48 ▶ return "TIFF" if /^MM\x00\x2a/;
Branch: 1 branch
BranchTrueFalseTotal
if /^MM\x00\x2a/ 1 47 48
171 47 ▶ return "TIFF" if /^II\x2a\x00/;
Branch: 1 branch
BranchTrueFalseTotal
if /^II\x2a\x00/ 5 42 47
172 42 ▶ return "BMP" if /^BM/;
Branch: 1 branch
BranchTrueFalseTotal
if /^BM/ 4 38 42
173 38 ▶ return "ICO" if /^\000\000\001\000/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\000\000\001\000/ 3 35 38
174 35 ▶ return "PPM" if /^P[1-6]/;
Branch: 1 branch
BranchTrueFalseTotal
if /^P[1-6]/ 9 26 35
175 26 ▶ return "XPM" if /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if m((^/\* XPM \*/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)) 3 23 26
176 23 ▶ return "XBM" if /^(?:\/\*.*\*\/\n)?#define\s/;
Branch: 1 branch
BranchTrueFalseTotal
if m[^(?:/\*.*\*/\n)?#define\s] 5 18 23
177 18 ▶ return "AVIF" if /\A....ftypavif/s;
Branch: 1 branch
BranchTrueFalseTotal
if /\A....ftypavif/s 3 15 18
178 15 ▶ return "SVG" if /^(<\?xml|[\012\015\t ]*<svg\b)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(<\?xml|[\012\015\t ]*<svg\b)/ 5 10 15
179 10 ▶ return "WEBP" if /^RIFF.{4}WEBP/s;
Branch: 1 branch
BranchTrueFalseTotal
if /^RIFF.{4}WEBP/s 10 0 10
180 0 return undef;
181 }
182  
183 sub dim
184 18 ▶{
Subroutine dim: called CC=5 SCAR=16.3
Pod documented
185 18 ▶ my $img = shift || return;
Condition50%1/2
shift() || (return)
l!l
180
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
186 18 ▶ my $x = $img->{width} || return;
Condition100%2/2
$img->{'width'} || (return)
l!l
171
MC/DC100%1/1
$img->{'width'} || (return)
$img->{'width'}
Truth table100%2/2
A
A= $img->{'width'}
Aresult
11
00
187 17 ▶ my $y = $img->{height} || return;
Condition50%1/2
$img->{'height'} || (return)
l!l
170
MC/DC0%0/1
$img->{'height'} || (return)
$img->{'height'}
Truth table50%1/2
A
A= $img->{'height'}
Aresult
11
00
188 17 ▶ wantarray ? ($x, $y) : "${x}x$y";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 13 17
189 }
190  
191 sub html_dim
192 2 ▶{
Subroutine html_dim: called CC=2 SCAR=6.9
Pod documented
193 2 my($x, $y) = dim(@_);
194 2 ▶ return "" unless $x;
Branch: 1 branch
BranchTrueFalseTotal
unless $x 1 1 2
195 1 "width=\"$x\" height=\"$y\"";
196 }
197  
198 #############################################################################
199 package Image::Info::Result;
200  
201 sub push_info
202 2135 ▶{
Subroutine push_info: called CC=1 SCAR=0.0
203 2135 my($self, $n, $key) = splice(@_, 0, 3);
204 2135 push(@{$self->[$n]{$key}}, @_);
205 }
206  
207 sub replace_info
208 85 ▶{
Subroutine replace_info: called CC=1 SCAR=0.0
209 85 my($self, $n, $key) = splice(@_, 0, 3);
210 85 $self->[$n]{$key}[0] = $_[0];
211 }
212  
213 sub clean_up
214 84 ▶{
Subroutine clean_up: called CC=4 SCAR=13.9
215 84 my $self = shift;
216 84 for (@$self) {
217 108 for my $k (keys %$_) {
218 1875 my $a = $_->{$k};
219 1875 ▶ $_->{$k} = $a->[0] if @$a <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$a <= 1 1748 127 1875
220 }
221 }
222 }
223  
224 217 ▶sub get_info {
Subroutine get_info: called CC=3 SCAR=11.0
225 217 my($self, $n, $key, $delete) = @_;
226 217 ▶ my $v = $delete ? delete $self->[$n]{$key} : $self->[$n]{$key};
Branch: 1 branch
BranchTrueFalseTotal
$delete ? : 145 72 217
227 217 ▶ $v ||= [];
Condition100%2/2
$v ||= []
l!l
12493
MC/DC100%1/1
$v ||= []
$v
Truth table100%2/2
A
A= $v
Aresult
11
00
228 217 @$v;
229 }
230  
231 25 1;
232  
233 __END__
« lib/Image/Info/TIFF.pm ↑ Summary lib/Image/Info/ICO.pm »