blib/lib/Image/Info/PNG.pm untested

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