/
pngdump.pl
341 lines (299 loc) · 7.82 KB
/
pngdump.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!perl
use strict;
use IO::Uncompress::Inflate qw(inflate);
use Getopt::Long;
my $dumpall = 0;
my $image = 0;
GetOptions(dumpall => \$dumpall,
image => \$image);
my $file = shift
or die "Usage: $0 filename\n";
open my $fh, "<", $file
or die "$0: cannot open '$file': $!\n";
binmode $fh;
my $head;
read($fh, $head, 8) == 8
or die "Cann't read header: $!\n";
my $offset = 0;
dump_data($offset, $head);
print " Header\n";
$offset += length $head;
unless ($head eq "\x89PNG\x0d\x0A\cZ\x0A") {
die "Header isn't a PNG header\n";
}
my $colour_type;
my $bits;
my $sline_len;
my $sline_left = 0;
my $row = 0;
while (my ($dlen, $data, $len, $type, $payload, $crc) = read_chunk($fh)) {
dump_data($offset, $data);
$offset += $dlen;
my $calc_crc = crc($type . $payload);
my $src_crc = unpack("N", $crc);
$type =~ s/([^ -\x7f])/sprintf("\\x%02x", ord $1)/ge;
print " Type: $type\n";
print " Length: $len\n";
printf " CRC: %x (calculated: %x)\n", $src_crc, $calc_crc;
if ($type eq 'IHDR') {
my ($w, $h, $d, $ct, $comp, $filter, $inter) =
unpack("NNCCCCC", $payload);
print <<EOS;
Width : $w
Height: $h
Depth: $d
Colour type: $ct
Filter: $filter
Interlace: $inter
EOS
$colour_type = $ct;
$bits = $d;
my $channels = $ct == 2 ? 3 : $ct == 4 ? 2 : $ct == 6 ? 4 : 1;
my $bitspp = $channels * $d;
$sline_len = int((($w * $bitspp) + 7) / 8);
++$sline_len; # filter byte
print " Line length: $sline_len\n";
}
elsif ($type eq 'sRGB') {
print " Rendering intent: ", ord($payload), "\n";
}
elsif ($type eq 'PLTE') {
my $index = 0;
while ($index * 3 < $len) {
my @rgb = unpack("CCC", substr($payload, $index * 3, 3));
print " $index: @rgb\n";
++$index;
}
}
elsif ($type eq 'tRNS') {
if ($colour_type == 0) {
my $g = unpack("n", $payload);
printf " Grey: %d (%x)\n", $g, $g;
}
elsif ($colour_type == 2) {
my @rgb = unpack("nnn", $payload);
printf " RGB: %d, %d, %d (%x, %x, %x)\n", @rgb, @rgb;
}
elsif ($colour_type == 3) {
my $index = 0;
for my $alpha (unpack("C*", $payload)) {
print " Index: $index: $alpha\n";
++$index;
}
}
else {
print " Unexpected tRNS for colour type $colour_type\n";
}
}
elsif ($type eq 'pHYs') {
my ($hres, $vres, $unit) = unpack("NNC", $payload);
my $unitname = $unit == 1 ? "metre" : "unknown";
print <<EOS;
hRes: $hres / $unitname
vRes: $vres / $unitname
Unit: $unit ($unitname)
EOS
}
elsif ($type eq 'tEXt') {
my ($key, $value) = split /\0/, $payload, 2;
print <<EOS;
Keyword: $key
Value: $value
EOS
do_more_text($key, $value);
}
elsif ($type eq 'zTXt') {
my ($key, $rest) = split /\0/, $payload, 2;
my $ctype = ord $rest;
my $ztxt = substr($rest, 1);
my $value = do_inflate($ztxt);
print <<EOS;
Keyword: $key
Value: $value
EOS
do_more_text($key, $value);
}
elsif ($type eq 'tIME') {
my @when = unpack("nCCCCC", $payload);
printf " Date: %d-%02d-%02d %02d:%02d:%02d\n", @when;
}
elsif ($type eq 'bKGD') {
if ($colour_type == 2 || $colour_type == 6) {
my @rgb = unpack("nnn", $payload);
printf " Background: rgb$bits(%d,%d,%d)\n", @rgb;
}
elsif ($colour_type == 0 || $colour_type == 4) {
my $g = unpack("n", $payload);
printf " Background: grey$bits(%d)\n", $g;
}
if ($colour_type == 3) {
my $index = unpack("C", $payload);
printf " Background: index(%d)\n", $index;
}
}
elsif ($type eq "IDAT" && $image) {
$sline_len
or die "IDAT before IHDR!?";
my $raw = do_inflate($payload);
if ($sline_left) {
print " Continuing $row:\n";
print " ", unpack("H*", substr($raw, 0, $sline_left, "")), "\n";
$sline_left = 0;
++$row;
}
while (length $raw >= $sline_len) {
my $row_data = substr($raw, 0, $sline_len, "");
my ($filter, $data) = unpack("CH*", $row_data);
print " Row $row, filter $filter\n";
print " $data\n";
++$row;
}
if (length $raw) {
$sline_left = $sline_len - length $raw;
my ($filter, $data) = unpack("CH*", $raw);
print " Row $row, filter $filter (partial)\n";
print " $data\n" if length $data;
}
}
$type eq "IEND"
and last;
}
sub do_more_text {
my ($key, $text) = @_;
if ($key eq 'Raw profile type xmp'
&& $text =~ s/^\s*xmp\s+\d+\s+//) {
print " XMP: ", pack("H*", join('', split ' ', $text)), "\n";
}
}
sub read_chunk {
my ($fh) = @_;
my $rlen;
read($fh, $rlen, 4)
or die "Cannot read chunk length\n";
my $len = unpack("N", $rlen);
my $type;
read($fh, $type, 4)
or die "Cannot read chunk type\n";
my $payload = "";
if ($rlen) {
read($fh, $payload, $len) == $len
or die "Cannot read payload\n";
}
my $crc;
read($fh, $crc, 4) == 4
or die "Cannot read CRC\n";
return ( $len + 12, $rlen . $type . $payload . $crc, $len, $type, $payload, $crc );
}
sub dump_data {
my ($offset, $data) = @_;
if (length $data > 28) {
if ($dumpall) {
for my $i (0 .. int((15 + length $data) / 16) - 1) {
my $row = substr($data, $i * 16, 16);
(my $clean = $row) =~ tr/ -~/./c;
printf("%08x: %-32s %s\n", $offset, unpack("H*", $row), $clean);
}
}
else {
printf "%08x: %s...\n", $offset, unpack("H*", substr($data, 0, 26));
}
}
else {
printf "%08x: %s\n", $offset, unpack("H*", $data), "\n";
}
}
#unsigned long crc_table[256];
my @crc_table;
#/* Flag: has the table been computed? Initially false. */
# int crc_table_computed = 0;
# /* Make the table for a fast CRC. */
# void make_crc_table(void)
# {
sub make_crc_table {
# unsigned long c;
# int n, k;
#
# for (n = 0; n < 256; n++) {
for my $n (0 .. 255) {
# c = (unsigned long) n;
my $c = $n;
# for (k = 0; k < 8; k++) {
for my $k (0 .. 7) {
# if (c & 1)
# c = 0xedb88320L ^ (c >> 1);
# else
# c = c >> 1;
if ($c & 1) {
$c = 0xedb88320 ^ ($c >> 1);
}
else {
$c = $c >> 1;
}
# }
}
# crc_table[n] = c;
$crc_table[$n] = $c;
# }
}
# crc_table_computed = 1;
# }
}
# /* Update a running CRC with the bytes buf[0..len-1]--the CRC
# should be initialized to all 1's, and the transmitted value
# is the 1's complement of the final running CRC (see the
# crc() routine below). */
# unsigned long update_crc(unsigned long crc, unsigned char *buf,
# int len)
# {
sub update_crc {
my ($crc, $data) = @_;
# unsigned long c = crc;
# int n;
# if (!crc_table_computed)
# make_crc_table();
@crc_table or make_crc_table();
# for (n = 0; n < len; n++) {
# c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
# }
for my $code (unpack("C*", $data)) {
$crc = $crc_table[($crc ^ $code) & 0xFF] ^ ($crc >> 8);
}
# return c;
# }
return $crc;
}
# /* Return the CRC of the bytes buf[0..len-1]. */
# unsigned long crc(unsigned char *buf, int len)
# {
# return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
# }
sub crc {
my $data = shift;
return update_crc(0xFFFFFFFF, $data) ^ 0xFFFFFFFF;
}
sub do_inflate {
my $z = shift;
my $out = '';
inflate(\$z, \$out);
return $out;
}
=head HEAD
pngdump.pl - dump the structure of a PNG image file.
=head1 SYNOPSIS
perl [-dumpall] [-image] pngdump.pl filename
=head1 DESCRIPTION
Dumps the structure of a PNG image file, listing chunk types, length,
CRC and optionally the entire content of each chunk.
Options:
=over
=item *
C<-dumpall> - dump the entire contents of each chunk rather than just
the leading bytes.
=item *
C<-image> - decompress the image data (IDAT chunk) and break the
result into rows, listing the filter and filtered (raw) data for each
row.
=back
Several chunk types are extracted and displayed in a more readable
form.
=cut