lib/Image/Info/PNG.pm

filter: Statementstmt 93.4% 86 / 92 Branchbran 79.6% 51 / 64 Conditioncond 65.2% 15 / 23 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 3 / 3 Podpod 0.0% 0 / 2 totaltotal 81.3% 161 / 198 CC 41 SCAR 37.2
CC
41
Cov
85%
process_file
45.3
my_read
3.1
BEGIN
1.0
CRAP 41.3
« lib/Image/TIFF.pm ↑ Summary lib/Image/Info/BMP.pm »
1 package Image::Info::PNG;
2  
3 # Copyright 1999-2000, Gisle Aas.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 =begin register
9  
10 MAGIC: /^\x89PNG\x0d\x0a\x1a\x0a/
11  
12 Information from IHDR, PLTE, gAMA, pHYs, tEXt, tIME chunks are
13 extracted. The sequence of chunks are also given by the C<PNG_Chunks>
14 key.
15  
16 =end register
17  
18 =cut
19  
20 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 our $VERSION = 1.04;
23  
24 # Test for Compress::Zlib (for reading zTXt chunks)
25 3 my $have_zlib = 0;
26 3 eval {
27 3 require Compress::Zlib;
28 3 $have_zlib++;
29 };
30  
31 # Test for Encode (for reading iTXt chunks)
32 3 my $have_encode = 0;
33 3 eval {
34 3 require Encode;
35 3 $have_encode++;
36 };
37  
38 sub my_read
39 90 ▶{
Subroutine my_read: called CC=3 SCAR=11.2
Pod undocumented
40 90 my($source, $len) = @_;
41 90 my $buf;
42 90 my $n = read($source, $buf, $len);
43 90 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 90 90
44 90 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 90 90
45 90 $buf;
46 }
47  
48  
49 sub process_file
50 8 ▶{
Subroutine process_file: called CC=39 SCAR=38.1
Pod undocumented
51 8 my($info, $fh) = @_;
52  
53 8 my $signature = my_read($fh, 8);
54 8 ▶ die "Bad PNG signature"
Branch: 1 branch
BranchTrueFalseTotal
unless $signature eq "\211PNG\r\n\32\n" 0 8 8
55 unless $signature eq "\x89PNG\x0d\x0a\x1a\x0a";
56  
57 8 $info->push_info(0, "file_media_type" => "image/png");
58 8 $info->push_info(0, "file_ext" => "png");
59  
60 8 my @chunks;
61  
62 8 while (1) {
63 45 my($len, $type) = unpack("Na4", my_read($fh, 8));
64  
65 45 ▶ if (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) { } 37 8 45
66 37 my $last = $chunks[-1];
67 37 my $count = 1;
68 37 ▶ $count = $1 if $last =~ s/\s(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $last =~ s/\s(\d+)$// 2 35 37
69 37 ▶ if ($last eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $type) { } 2 35 37
70 2 $count++;
71 2 $chunks[-1] = "$type $count";
72 }
73 else {
74 35 push(@chunks, $type);
75 }
76 }
77 else {
78 8 push(@chunks, $type);
79 }
80  
81 45 ▶ last if $type eq "IEND";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "IEND" 8 37 45
82 37 my $data = my_read($fh, $len + 4);
83 37 my $crc = unpack("N", substr($data, -4, 4, ""));
84 37 ▶ if ($type eq "IHDR" && $len == 13) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'IHDR' and $len == 13) { } 8 29 37
elsif ($type eq 'PLTE') { } 3 26 29
elsif ($type eq 'gAMA' and $len == 4) { } 0 26 26
elsif ($type eq 'pHYs' and $len == 9) { } 5 21 26
elsif ($type eq 'tEXt' or $type eq 'zTXt' or $type eq 'iTXt') { } 7 14 21
elsif ($type eq 'tIME' and $len == 7) { } 4 10 14
elsif ($type eq 'sBIT') { } 0 10 10
elsif ($type eq 'IDAT') { } 8 2 10
Condition66%2/3
$type eq "IHDR" and $len == 13
!ll&&!rl&&r
2908
MC/DC50%1/2
$type eq "IHDR" and $len == 13
$type eq "IHDR" $len == 13
Truth table66%2/3
A and B
A= $type eq "IHDR"B= $len == 13
ABresult
0X0
100
111
85 8 my($w, $h, $depth, $ctype, $compression, $filter, $interlace) =
86 unpack("NNCCCCC", $data);
87 $ctype = {
88 0 => "Gray",
89 2 => "RGB",
90 3 => "Indexed-RGB",
91 4 => "GrayA",
92 6 => "RGBA",
93 8 ▶ }->{$ctype} || "PNG-$ctype";
Condition50%1/2
{0, 'Gray', 2, 'RGB', 3, 'Indexed-RGB', 4, 'GrayA', 6, 'RGBA'}->{$ctype} || "PNG-$ctype"
l!l
80
MC/DC0%0/1
{0, 'Gray', 2, 'RGB', 3, 'Indexed-RGB', 4, 'GrayA', 6, 'RGBA'}->{$ctype} || "PNG-$ctype"
{0, 'Gray', 2, 'RGB', 3, 'Indexed-RGB', 4, 'GrayA', 6, 'RGBA'}->{$ctype}
Truth table50%1/2
A
A= {0, 'Gray', 2, 'RGB', 3, 'Indexed-RGB', 4, 'GrayA', 6, 'RGBA'}->{$ctype}
Aresult
11
00
94  
95 8 ▶ $compression = "Deflate" if $compression == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $compression == 0 8 0 8
96 8 ▶ $filter = "Adaptive" if $filter == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $filter == 0 8 0 8
97 8 ▶ $interlace = "Adam7" if $interlace == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $interlace == 1 1 7 8
98  
99 8 $info->push_info(0, "width", $w);
100 8 $info->push_info(0, "height", $h);
101 8 $info->push_info(0, "SampleFormat", "U$depth");
102 8 $info->push_info(0, "color_type", $ctype);
103  
104 8 $info->push_info(0, "Compression", $compression);
105 8 $info->push_info(0, "PNG_Filter", $filter);
106 8 ▶ $info->push_info(0, "Interlace", $interlace)
Branch: 1 branch
BranchTrueFalseTotal
if $interlace 1 7 8
107 if $interlace;
108 }
109 elsif ($type eq "PLTE") {
110 3 my @table;
111 3 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 36 3 39
112 36 push(@table, sprintf("#%02x%02x%02x",
113 unpack("C3", substr($data, 0, 3, ""))));
114 }
115 3 $info->push_info(0, "ColorPalette" => \@table);
116 }
117 ▶ elsif ($type eq "gAMA" && $len == 4) {
Condition33%1/3
$type eq "gAMA" and $len == 4
!ll&&!rl&&r
2600
MC/DC0%0/2
$type eq "gAMA" and $len == 4
$type eq "gAMA" $len == 4
Truth table33%1/3
A and B
A= $type eq "gAMA"B= $len == 4
ABresult
0X0
100
111
118 0 $info->push_info(0, "Gamma", unpack("N", $data)/100_000);
119 }
120 ▶ elsif ($type eq "pHYs" && $len == 9) {
Condition66%2/3
$type eq "pHYs" and $len == 9
!ll&&!rl&&r
2105
MC/DC50%1/2
$type eq "pHYs" and $len == 9
$type eq "pHYs" $len == 9
Truth table66%2/3
A and B
A= $type eq "pHYs"B= $len == 9
ABresult
0X0
100
111
121 5 my $res;
122 5 my($res_x, $res_y, $unit) = unpack("NNC", $data);
123 5 if (0 && $unit == 1) {
124 # convert to dpi
125 $unit = "dpi";
126 for ($res_x, $res_y) {
127 $_ *= 0.0254;
128 }
129 }
130 5 ▶ $res = ($res_x == $res_y) ? $res_x : "$res_x/$res_y";
Branch: 1 branch
BranchTrueFalseTotal
$res_x == $res_y ? : 5 0 5
131 5 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 5 0 5
132 5 ▶ if ($unit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit == 1) { } 5 0 5
133 5 $res .= " dpm";
134 }
135 else {
136 0 $res .= " png-unit-$unit";
137 }
138 }
139 5 $info->push_info(0, "resolution" => $res)
140 }
141 ▶ elsif ($type eq "tEXt" || $type eq "zTXt" || $type eq "iTXt") {
Condition100%6/6
$type eq "tEXt" or $type eq "zTXt" or $type eq "iTXt"
l!l&&r!l&&!r
4314
$type eq "tEXt" or $type eq "zTXt"
l!l&&r!l&&!r
1317
MC/DC100%3/3
$type eq "tEXt" or $type eq "zTXt" or $type eq "iTXt"
$type eq "tEXt" $type eq "zTXt" $type eq "iTXt"
Truth table100%4/4
A or B or C
A= $type eq "tEXt"B= $type eq "zTXt"C= $type eq "iTXt"
ABCresult
1XX1
01X1
0011
0000
142 7 my($key, $val) = split(/\0/, $data, 2);
143 7 my($method,$ctext,$is_i);
144 7 ▶ if ($type eq "iTXt") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'iTXt') { } 3 4 7
elsif ($type eq 'zTXt') { } 3 1 4
145 3 ++$is_i;
146 3 (my $compressed, $method, my $lang, my $trans, $ctext)
147 = unpack "CaZ*Z*a*", $val;
148 3 ▶ unless ($compressed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compressed) 1 2 3
149 1 undef $method;
150 1 $val = $ctext;
151 }
152 }
153 elsif ($type eq "zTXt") {
154 3 ($method,$ctext) = split(//, $val, 2);
155 }
156  
157 7 ▶ if (defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) 5 2 7
158 5 ▶ if ($have_zlib && $method eq "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_zlib and $method eq "\000") { } 5 0 5
Condition33%1/3
$have_zlib and $method eq "\0"
!ll&&!rl&&r
005
MC/DC0%0/2
$have_zlib and $method eq "\0"
$have_zlib $method eq "\0"
Truth table33%1/3
A and B
A= $have_zlibB= $method eq "\0"
ABresult
0X0
100
111
159 5 $val = Compress::Zlib::uncompress($ctext);
160 } else {
161 0 undef $val;
162 }
163 }
164  
165 7 ▶ if ($is_i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_i) 3 4 7
166 3 ▶ if ($have_encode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_encode) { } 3 0 3
167 3 $val = Encode::decode("UTF-8", $val);
168 } else {
169 0 undef $val;
170 }
171 }
172  
173 7 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 7 0 7
174 # XXX should make sure $key is not in conflict with any
175 # other key we might generate
176 7 $info->push_info(0, $key, $val);
177 } else {
178 0 $info->push_info(0, "Chunk-$type" => $data);
179 }
180 }
181 ▶ elsif ($type eq "tIME" && $len == 7) {
Condition66%2/3
$type eq "tIME" and $len == 7
!ll&&!rl&&r
1004
MC/DC50%1/2
$type eq "tIME" and $len == 7
$type eq "tIME" $len == 7
Truth table66%2/3
A and B
A= $type eq "tIME"B= $len == 7
ABresult
0X0
100
111
182 4 $info->push_info(0, "LastModificationTime",
183 sprintf("%04d-%02d-%02d %02d:%02d:%02d",
184 unpack("nC5", $data)));
185 }
186 elsif ($type eq "sBIT") {
187 0 $info->push_info(0, "SignificantBits" => unpack("C*", $data));
188 }
189 elsif ($type eq "IDAT") {
190 # ignore
191 }
192 else {
193 2 $info->push_info(0, "Chunk-$type" => $data);
194 }
195 }
196  
197 8 $info->push_info(0, "PNG_Chunks", @chunks);
198  
199 8 ▶ unless ($info->get_info(0, "resolution")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->get_info(0, "resolution")) 3 5 8
200 3 $info->push_info(0, "resolution", "1/1");
201 }
202 }
203  
204 3 1;
« lib/Image/TIFF.pm ↑ Summary lib/Image/Info/BMP.pm »