lib/Image/Info/PNG.pm

filter: Statementstmt 93.6% 89 / 95 Branchbran 79.6% 51 / 64 Conditioncond 65.2% 15 / 23 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 81.6% 165 / 202 CC 41 SCAR 37.0
CC
41
Cov
85%
process_file
45.3
my_read
3.1
BEGIN
1.0
CRAP 40.4
« lib/Image/Info/GIF.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 3 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 $VERSION = 1.03;
24  
25 # Test for Compress::Zlib (for reading zTXt chunks)
26 3 my $have_zlib = 0;
27 3 eval {
28 3 require Compress::Zlib;
29 3 $have_zlib++;
30 };
31  
32 # Test for Encode (for reading iTXt chunks)
33 3 my $have_encode = 0;
34 3 eval {
35 3 require Encode;
36 3 $have_encode++;
37 };
38  
39 sub my_read
40 {
41 90 ▶ my($source, $len) = @_;
Subroutine my_read: called CC=3 SCAR=11.2
Pod undocumented
42 90 my $buf;
43 90 my $n = read($source, $buf, $len);
44 90 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 90 90
45 90 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 90 90
46 90 $buf;
47 }
48  
49  
50 sub process_file
51 {
52 8 ▶ my($info, $fh) = @_;
Subroutine process_file: called CC=39 SCAR=38.1
Pod undocumented
53  
54 8 my $signature = my_read($fh, 8);
55 8 ▶ die "Bad PNG signature"
Branch: 1 branch
BranchTrueFalseTotal
unless $signature eq "\211PNG\r\n\32\n" 0 8 8
56 unless $signature eq "\x89PNG\x0d\x0a\x1a\x0a";
57  
58 8 $info->push_info(0, "file_media_type" => "image/png");
59 8 $info->push_info(0, "file_ext" => "png");
60  
61 8 my @chunks;
62  
63 8 while (1) {
64 45 my($len, $type) = unpack("Na4", my_read($fh, 8));
65  
66 45 ▶ if (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) { } 37 8 45
67 37 my $last = $chunks[-1];
68 37 my $count = 1;
69 37 ▶ $count = $1 if $last =~ s/\s(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $last =~ s/\s(\d+)$// 2 35 37
70 37 ▶ if ($last eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last eq $type) { } 2 35 37
71 2 $count++;
72 2 $chunks[-1] = "$type $count";
73 }
74 else {
75 35 push(@chunks, $type);
76 }
77 }
78 else {
79 8 push(@chunks, $type);
80 }
81  
82 45 ▶ last if $type eq "IEND";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "IEND" 8 37 45
83 37 my $data = my_read($fh, $len + 4);
84 37 my $crc = unpack("N", substr($data, -4, 4, ""));
85 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
86 8 my($w, $h, $depth, $ctype, $compression, $filter, $interlace) =
87 unpack("NNCCCCC", $data);
88 $ctype = {
89 0 => "Gray",
90 2 => "RGB",
91 3 => "Indexed-RGB",
92 4 => "GrayA",
93 6 => "RGBA",
94 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
95  
96 8 ▶ $compression = "Deflate" if $compression == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $compression == 0 8 0 8
97 8 ▶ $filter = "Adaptive" if $filter == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $filter == 0 8 0 8
98 8 ▶ $interlace = "Adam7" if $interlace == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $interlace == 1 1 7 8
99  
100 8 $info->push_info(0, "width", $w);
101 8 $info->push_info(0, "height", $h);
102 8 $info->push_info(0, "SampleFormat", "U$depth");
103 8 $info->push_info(0, "color_type", $ctype);
104  
105 8 $info->push_info(0, "Compression", $compression);
106 8 $info->push_info(0, "PNG_Filter", $filter);
107 8 ▶ $info->push_info(0, "Interlace", $interlace)
Branch: 1 branch
BranchTrueFalseTotal
if $interlace 1 7 8
108 if $interlace;
109 }
110 elsif ($type eq "PLTE") {
111 3 my @table;
112 3 ▶ while (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 36 3 39
113 36 push(@table, sprintf("#%02x%02x%02x",
114 unpack("C3", substr($data, 0, 3, ""))));
115 }
116 3 $info->push_info(0, "ColorPalette" => \@table);
117 }
118 ▶ 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
119 0 $info->push_info(0, "Gamma", unpack("N", $data)/100_000);
120 }
121 ▶ 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
122 5 my $res;
123 5 my($res_x, $res_y, $unit) = unpack("NNC", $data);
124 5 if (0 && $unit == 1) {
125 # convert to dpi
126 $unit = "dpi";
127 for ($res_x, $res_y) {
128 $_ *= 0.0254;
129 }
130 }
131 5 ▶ $res = ($res_x == $res_y) ? $res_x : "$res_x/$res_y";
Branch: 1 branch
BranchTrueFalseTotal
$res_x == $res_y ? : 5 0 5
132 5 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 5 0 5
133 5 ▶ if ($unit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit == 1) { } 5 0 5
134 5 $res .= " dpm";
135 }
136 else {
137 0 $res .= " png-unit-$unit";
138 }
139 }
140 5 $info->push_info(0, "resolution" => $res)
141 }
142 ▶ 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
143 7 my($key, $val) = split(/\0/, $data, 2);
144 7 my($method,$ctext,$is_i);
145 7 ▶ if ($type eq "iTXt") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'iTXt') { } 3 4 7
elsif ($type eq 'zTXt') { } 3 1 4
146 3 ++$is_i;
147 3 (my $compressed, $method, my $lang, my $trans, $ctext)
148 = unpack "CaZ*Z*a*", $val;
149 3 ▶ unless ($compressed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($compressed) 1 2 3
150 1 undef $method;
151 1 $val = $ctext;
152 }
153 }
154 elsif ($type eq "zTXt") {
155 3 ($method,$ctext) = split(//, $val, 2);
156 }
157  
158 7 ▶ if (defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $method) 5 2 7
159 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
160 5 $val = Compress::Zlib::uncompress($ctext);
161 } else {
162 0 undef $val;
163 }
164 }
165  
166 7 ▶ if ($is_i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_i) 3 4 7
167 3 ▶ if ($have_encode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_encode) { } 3 0 3
168 3 $val = Encode::decode("UTF-8", $val);
169 } else {
170 0 undef $val;
171 }
172 }
173  
174 7 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 7 0 7
175 # XXX should make sure $key is not in conflict with any
176 # other key we might generate
177 7 $info->push_info(0, $key, $val);
178 } else {
179 0 $info->push_info(0, "Chunk-$type" => $data);
180 }
181 }
182 ▶ 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
183 4 $info->push_info(0, "LastModificationTime",
184 sprintf("%04d-%02d-%02d %02d:%02d:%02d",
185 unpack("nC5", $data)));
186 }
187 elsif ($type eq "sBIT") {
188 0 $info->push_info(0, "SignificantBits" => unpack("C*", $data));
189 }
190 elsif ($type eq "IDAT") {
191 # ignore
192 }
193 else {
194 2 $info->push_info(0, "Chunk-$type" => $data);
195 }
196 }
197  
198 8 $info->push_info(0, "PNG_Chunks", @chunks);
199  
200 8 ▶ unless ($info->get_info(0, "resolution")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->get_info(0, "resolution")) 3 5 8
201 3 $info->push_info(0, "resolution", "1/1");
202 }
203 }
204  
205 3 1;
« lib/Image/Info/GIF.pm ↑ Summary lib/Image/Info/BMP.pm »