start a jpeg dump tool
Tony Cook
5 years ago
0 | #!perl | |
1 | use strict; | |
2 | use Getopt::Long; | |
3 | ||
4 | my $dumpall = 0; | |
5 | my $exiffile; | |
6 | GetOptions(dumpall => \$dumpall, | |
7 | "e|exif=s" => \$exiffile); | |
8 | ||
9 | my $file = shift | |
10 | or die "Usage: $0 filename\n"; | |
11 | ||
12 | open my $fh, "<", $file | |
13 | or die "$0: cannot open '$file': $!\n"; | |
14 | ||
15 | binmode $fh; | |
16 | ||
17 | ||
18 | while (!eof($fh)) { | |
19 | my $chead; | |
20 | unless (read($fh, $chead, 2) == 2) { | |
21 | eof $fh or die "Failed to read start of chunk: $!"; | |
22 | last; | |
23 | } | |
24 | if ($chead eq "\xFF\xD8") { | |
25 | print "Start of image\n"; | |
26 | } | |
27 | elsif ($chead =~ /^\xFF[\xE0-\xEF]$/) { | |
28 | # APP0-APP15 | |
29 | my $clen; | |
30 | unless (read($fh, $clen, 2) == 2) { | |
31 | die "Couldn't read length for APPn\n"; | |
32 | } | |
33 | my $len = unpack("S>", $clen); | |
34 | my $appdata; | |
35 | unless (read($fh, $appdata, $len-2) == $len-2) { | |
36 | print "length ", length $appdata, " expected $len\n"; | |
37 | die "Couldn't read application data for APPn\n"; | |
38 | } | |
39 | if ($chead eq "\xFF\xE0") { | |
40 | # APP0 | |
41 | my $type = substr($appdata, 0, 5, ''); | |
42 | print "APP0 ", $type =~ tr/\0//dr, "\n"; | |
43 | if ($type eq "JFIF\0") { | |
44 | my ($version, $units, $xdens, $ydens, $tx, $ty, $rest) = | |
45 | unpack("S>CS>S>CCa*", $appdata); | |
46 | printf " Version: %x\n", $version; | |
47 | print " Units: $units\n"; | |
48 | print " Density: $xdens x $ydens\n"; | |
49 | print " Thumbnail: $tx x $ty\n"; | |
50 | } | |
51 | else { | |
52 | # more to do | |
53 | } | |
54 | } | |
55 | elsif ($chead eq "\xFF\xE1") { | |
56 | # APP1 | |
57 | if ($appdata =~ s/^Exif\0.//) { | |
58 | print " EXIF data\n"; | |
59 | if ($exiffile) { | |
60 | open my $eh, ">", $exiffile | |
61 | or die "Cannot create $exiffile: $!\n"; | |
62 | binmode $eh; | |
63 | print $eh $appdata; | |
64 | close $eh | |
65 | or die "Cannot close $exiffile: $!\n"; | |
66 | } | |
67 | } | |
68 | } | |
69 | } | |
70 | else { | |
71 | die "I don't know how to handle ", unpack("H*", $chead), "\n"; | |
72 | } | |
73 | } | |
74 | ||
75 | =head HEAD | |
76 | ||
77 | jpegdump.pl - dump the structure of a JPEG image file. | |
78 | ||
79 | =head1 SYNOPSIS | |
80 | ||
81 | perl jpegdump.pl [-dumpall] [-exif=exifdata] filename | |
82 | ||
83 | =head1 DESCRIPTION | |
84 | ||
85 | Dumps the structure of a JPEG image file. | |
86 | ||
87 | Options: | |
88 | ||
89 | =over | |
90 | ||
91 | =item * | |
92 | ||
93 | C<-dumpall> - dump the entire contents of each chunk rather than just | |
94 | the leading bytes. Currently unimplemented. | |
95 | ||
96 | =item * | |
97 | ||
98 | C<< -exif I<filename> >> - extract the EXIF blob to a file. | |
99 | ||
100 | =back | |
101 | ||
102 | This is incomplete, I mostly wrote it to extract the EXIF blob, but I | |
103 | expect I'll finish it at some point. | |
104 | ||
105 | =cut |