lib/Image/Info/JPEG.pm

filter: Statementstmt 80.5% 116 / 144 Branchbran 66.6% 56 / 84 Conditioncond 48.2% 14 / 29 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 9 totaltotal 67.4% 201 / 298 CC 58 SCAR 25.6
CC
58
Cov
72%
process_app0_jfxx
20.0
process_chunk
19.2
process_app1_exif
15.9
CRAP 12.9
« lib/Image/Info/WEBP.pm ↑ Summary blib/lib/Image/Info/WBMP.pm »
1 package Image::Info::JPEG;
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 # maintained by Tels 2007 - 2008
9  
10 8 $VERSION = 0.06;
11  
12 =begin register
13  
14 MAGIC: /^\xFF\xD8/
15  
16 For JPEG files we extract information both from C<JFIF> and C<Exif>
17 application chunks.
18  
19 C<Exif> is the file format written by most digital cameras. This
20 encode things like timestamp, camera model, focal length, exposure
21 time, aperture, flash usage, GPS position, etc.
22  
23 The C<Exif> spec can be found at:
24 L<http://www.exif.org/specifications.html>.
25  
26 The C<color_type> element may have the following values: C<Gray>,
27 C<YCbCr>, and C<CMYK>. Note that detecting C<RGB> and C<YCCK>
28 currently does not work, but will hopefully in future.
29  
30 =end register
31  
32 =cut
33  
34 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 8 my %sof = (
37 0xC0 => "Baseline",
38 0xC1 => "Extended sequential",
39 0xC2 => "Progressive",
40 0xC3 => "Lossless",
41 0xC5 => "Differential sequential",
42 0xC6 => "Differential progressive",
43 0xC7 => "Differential lossless",
44 0xC9 => "Extended sequential, arithmetic coding",
45 0xCA => "Progressive, arithmetic coding",
46 0xCB => "Lossless, arithmetic coding",
47 0xCD => "Differential sequential, arithmetic coding",
48 0xCE => "Differential progressive, arithmetic coding",
49 0xCF => "Differential lossless, arithmetic coding",
50 );
51  
52 sub my_read
53 {
54 778 ▶ my($source, $len) = @_;
Subroutine my_read: called CC=3 SCAR=11.2
Pod undocumented
55 778 my $buf;
56 778 my $n = read($source, $buf, $len);
57 778 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 778 778
58 778 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 778 778
59 778 $buf;
60 }
61  
62 BEGIN {
63 8 ▶ my $f = ($] >= 5.008) ? <<'EOT' : <<'EOT';
Subroutine BEGIN: called CC=3 SCAR=11.2
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.008 ? : 8 0 8
64 sub with_io_string (&$) {
65 open(my $fh, "<", \$_[1]);
66 local $_ = $fh;
67 &{$_[0]};
68 }
69 EOT
70 sub with_io_string (&$) {
71 require IO::String;
72 local $_ = IO::String->new($_[1]);
73 &{$_[0]};
74 $_->close;
75 }
76 EOT
77  
78 #print $f;
79 8 ▶ eval $f;
Subroutine with_io_string: called CC=1 SCAR=0.0
Pod undocumented
80 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
81 }
82  
83 sub process_file
84 {
85 17 ▶ my($info, $fh, $cnf) = @_;
Subroutine process_file: called CC=1 SCAR=0.0
Pod undocumented
86 17 _process_file($info, $fh, 0);
87 }
88  
89 sub _process_file
90 {
91 28 ▶ my($info, $fh, $img_no) = @_;
Subroutine _process_file: called CC=11 SCAR=24.5
92  
93 28 my $soi = my_read($fh, 2);
94 28 ▶ unless ($soi eq "\xFF\xD8") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($soi eq "\377\330") 0 28 28
95 0 my $ofs = tell() - 2;
96 0 die "SOI missing in JPEG file at offset $ofs";
97 }
98  
99 28 $info->push_info($img_no, "file_media_type" => "image/jpeg");
100 28 $info->push_info($img_no, "file_ext" => "jpg");
101  
102 28 while (1) {
103 264 my($ff, $mark) = unpack("CC", my_read($fh, 2));
104 264 ▶ last if !defined $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff 0 264 264
105 264 ▶ if ($ff != 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ff != 255) 3 261 264
106 3 my $corrupt_bytes = 2;
107 3 while(1) {
108 9 my($ff) = unpack("C", my_read($fh,1));
109 9 ▶ return if !defined $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff 0 9 9
110 9 ▶ last if $ff == 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $ff == 255 3 6 9
111 6 $corrupt_bytes++;
112 }
113 3 $mark = unpack("C", my_read($fh,1));
114 3 $info->push_info($img_no, "Warn", sprintf("Corrupt JPEG data, $corrupt_bytes extraneous bytes before marker 0x%02x", $mark));
115 }
116 264 ▶ if ($mark == 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark == 255) 2 262 264
117 # JPEG markers can be padded with unlimited 0xFF's
118 2 for (;;) {
119 2 ($mark) = unpack("C", my_read($fh, 1));
120 2 ▶ last if $mark != 0xFF;
Branch: 1 branch
BranchTrueFalseTotal
if $mark != 255 2 0 2
121 }
122 }
123 264 ▶ last if $mark == 0xDA || $mark == 0xD9; # SOS/EOI
Branch: 1 branch
BranchTrueFalseTotal
if $mark == 218 or $mark == 217 28 236 264
Condition66%2/3
$mark == 218 or $mark == 217
l!l&&r!l&&!r
280236
MC/DC50%1/2
$mark == 218 or $mark == 217
$mark == 218 $mark == 217
Truth table66%2/3
A or B
A= $mark == 218B= $mark == 217
ABresult
1X1
011
000
124 236 my($len) = unpack("n", my_read($fh, 2));
125 236 ▶ last if $len < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 2 0 236 236
126 236 process_chunk($info, $img_no, $mark, my_read($fh, $len - 2));
127 }
128 }
129  
130 sub process_chunk
131 {
132 236 ▶ my($info, $img_no, $mark, $data) = @_;
Subroutine process_chunk: called CC=16 SCAR=29.6
Pod undocumented
133 #printf "MARK 0x%02X, len=%d\n", $mark, length($data);
134  
135 236 ▶ if ($mark == 0xFE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mark == 254) { } 3 233 236
elsif ($mark >= 224 and $mark <= 239) { } 43 190 233
elsif ($sof{$mark}) { } 28 162 190
136 3 $info->push_info($img_no, Comment => $data);
137 }
138 ▶ elsif ($mark >= 0xE0 && $mark <= 0xEF) {
Condition66%2/3
$mark >= 224 and $mark <= 239
!ll&&!rl&&r
190043
MC/DC50%1/2
$mark >= 224 and $mark <= 239
$mark >= 224 $mark <= 239
Truth table66%2/3
A and B
A= $mark >= 224B= $mark <= 239
ABresult
0X0
100
111
139 43 ▶ process_app($info, $mark, $data) if $img_no == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $img_no == 0 32 11 43
140 }
141 elsif ($sof{$mark}) {
142 28 my($precision, $height, $width, $num_comp) =
143 unpack("CnnC", substr($data, 0, 6, ""));
144 28 $info->push_info($img_no, "JPEG_Type", $sof{$mark});
145  
146 # fix bug #15167 by keeping the highest values
147 28 ▶ my $old_w = $info->get_info($img_no, "width") || -1;
Condition50%1/2
$info->get_info($img_no, 'width') || -1
l!l
028
MC/DC0%0/1
$info->get_info($img_no, 'width') || -1
$info->get_info($img_no, 'width')
Truth table50%1/2
A
A= $info->get_info($img_no, 'width')
Aresult
11
00
148 28 ▶ my $old_h = $info->get_info($img_no, "height") || -1;
Condition50%1/2
$info->get_info($img_no, 'height') || -1
l!l
028
MC/DC0%0/1
$info->get_info($img_no, 'height') || -1
$info->get_info($img_no, 'height')
Truth table50%1/2
A
A= $info->get_info($img_no, 'height')
Aresult
11
00
149  
150 28 ▶ $info->replace_info($img_no, "width", $width) if $old_w < $width;
Branch: 1 branch
BranchTrueFalseTotal
if $old_w < $width 28 0 28
151 28 ▶ $info->replace_info($img_no, "height", $height) if $old_h < $height;
Branch: 1 branch
BranchTrueFalseTotal
if $old_h < $height 28 0 28
152  
153 28 for (1..$num_comp) {
154 84 $info->push_info($img_no, "BitsPerSample", $precision);
155 }
156 28 $info->push_info($img_no, "SamplesPerPixel" => $num_comp);
157  
158 # XXX need to consider JFIF/Adobe markers to determine this...
159 28 ▶ if ($num_comp == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($num_comp == 1) { } 0 28 28
elsif ($num_comp == 3) { } 28 0 28
elsif ($num_comp == 4) { } 0 0 0
160 0 $info->push_info($img_no, "color_type" => "Gray");
161 }
162 elsif ($num_comp == 3) {
163 28 $info->push_info($img_no, "color_type" => "YCbCr"); # or RGB ?
164 }
165 elsif ($num_comp == 4) {
166 0 $info->push_info($img_no, "color_type" => "CMYK"); # or YCCK ?
167 }
168  
169 28 if (1) {
170 28 my %comp_id_lookup = ( 1 => "Y",
171 2 => "Cb",
172 3 => "Cr",
173 82 => "R",
174 71 => "G",
175 66 => "B" );
176 28 ▶ while (length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 84 28 112
177 84 my($comp_id, $hv, $qtable) =
178 unpack("CCC", substr($data, 0, 3, ""));
179 84 my $horiz_sf = $hv >> 4 & 0x0f;
180 84 my $vert_sf = $hv & 0x0f;
181 84 ▶ $comp_id = $comp_id_lookup{$comp_id} || $comp_id;
Condition33%1/3
$comp_id_lookup{$comp_id} || $comp_id
l!l&&r!l&&!r
8400
MC/DC0%0/2
$comp_id_lookup{$comp_id} || $comp_id
$comp_id_lookup{$comp_id} $comp_id
Truth table33%1/3
A || B
A= $comp_id_lookup{$comp_id}B= $comp_id
ABresult
1X1
011
000
182 84 $info->push_info($img_no, "ColorComponents", [$comp_id, $hv, $qtable]);
183 84 $info->push_info($img_no, "ColorComponentsDecoded",
184 { ComponentIdentifier => $comp_id,
185 HorizontalSamplingFactor => $horiz_sf,
186 VerticalSamplingFactor => $vert_sf,
187 QuantizationTableDesignator => $qtable } );
188 }
189 }
190 }
191 }
192  
193 sub process_app
194 {
195 32 ▶ my($info, $mark, $data) = @_;
Subroutine process_app: called CC=5 SCAR=16.6
Pod undocumented
196 32 my $app = $mark - 0xE0;
197 32 my $id = substr($data, 0, 5, "");
198 #$info->push_info(0, "Debug", "APP$app $id");
199 32 $id = "$app-$id";
200 32 ▶ if ($id eq "0-JFIF\0") {
Branch: 4 branches
BranchTrueFalseTotal
if ($id eq "0-JFIF\000") { } 17 15 32
elsif ($id eq "0-JFXX\000") { } 0 15 15
elsif ($id eq "1-Exif\000") { } 14 1 15
elsif ($id eq '14-Adobe') { } 0 1 1
201 17 process_app0_jfif($info, $data);
202 }
203 elsif ($id eq "0-JFXX\0") {
204 0 process_app0_jfxx($info, $data);
205 }
206 elsif ($id eq "1-Exif\0") {
207 14 process_app1_exif($info, $data);
208 }
209 elsif ($id eq "14-Adobe") {
210 0 process_app14_adobe($info, $data);
211 }
212 else {
213 1 $info->push_info(0, "App$id", $data);
214 #printf " %s\n", Data::Dump::dump($data);
215 }
216 }
217  
218 sub process_app0_jfif
219 {
220 17 ▶ my($info, $data) = @_;
Subroutine process_app0_jfif: called CC=8 SCAR=26.3
Pod undocumented
221 17 ▶ if (length $data < 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 9) 0 17 17
222 0 $info->push_info(0, "Debug", "Short JFIF chunk");
223 0 return;
224 }
225 17 my($ver_hi, $ver_lo, $unit, $x_density, $y_density, $x_thumb, $y_thumb) =
226 unpack("CC C nn CC", substr($data, 0, 9, ""));
227 17 $info->push_info(0, "JFIF_Version", sprintf("%d.%02d", $ver_hi, $ver_lo));
228  
229 17 ▶ my $res = $x_density != $y_density || !$unit
Branch: 1 branch
BranchTrueFalseTotal
$x_density != $y_density || !$unit ? : 0 17 17
Condition33%1/3
$x_density != $y_density or not $unit
l!l&&r!l&&!r
0017
MC/DC0%0/2
$x_density != $y_density or not $unit
$x_density != $y_density not $unit
Truth table33%1/3
A or not(B)
A= $x_density != $y_densityB= not $unit
ABresult
1X1
011
000
230 ? "$x_density/$y_density" : $x_density;
231  
232 17 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 17 0 17
233 $unit = { 0 => "pixels",
234 1 => "dpi",
235 2 => "dpcm"
236 17 ▶ }->{$unit} || "jfif-unit-$unit";
Condition50%1/2
{0, 'pixels', 1, 'dpi', 2, 'dpcm'}->{$unit} || "jfif-unit-$unit"
l!l
170
MC/DC0%0/1
{0, 'pixels', 1, 'dpi', 2, 'dpcm'}->{$unit} || "jfif-unit-$unit"
{0, 'pixels', 1, 'dpi', 2, 'dpcm'}->{$unit}
Truth table50%1/2
A
A= {0, 'pixels', 1, 'dpi', 2, 'dpcm'}->{$unit}
Aresult
11
00
237 17 $res .= " $unit";
238 }
239 17 $info->push_info(0, "resolution", $res);
240  
241 17 ▶ if ($x_thumb || $y_thumb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_thumb or $y_thumb) 0 17 17
Condition33%1/3
$x_thumb or $y_thumb
l!l&&r!l&&!r
0017
MC/DC0%0/2
$x_thumb or $y_thumb
$x_thumb $y_thumb
Truth table33%1/3
A or B
A= $x_thumbB= $y_thumb
ABresult
1X1
011
000
242 0 $info->push_info(1, "width", $x_thumb);
243 0 $info->push_info(1, "height", $y_thumb);
244 0 $info->push_info(1, "ByteCount", length($data));
245 }
246 }
247  
248 sub process_app0_jfxx
249 {
250 0 ▶ my($info, $data) = @_;
Subroutine process_app0_jfxx: not called CC=4 SCAR=30.0
Pod undocumented
251 0 my($code) = ord(substr($data, 0, 1, ""));
252 $info->push_info(1, "JFXX_ImageType",
253 { 0x10 => "JPEG thumbnail",
254 0x11 => "Bitmap thumbnail",
255 0x13 => "RGB thumbnail",
256 0 ▶ }->{$code} || "Unknown extension code $code");
Condition0%0/2
{16, 'JPEG thumbnail', 17, 'Bitmap thumbnail', 19, 'RGB thumbnail'}->{$code} || "Unknown extension code $code"
l!l
00
MC/DC0%0/1
{16, 'JPEG thumbnail', 17, 'Bitmap thumbnail', 19, 'RGB thumbnail'}->{$code} || "Unknown extension code $code"
{16, 'JPEG thumbnail', 17, 'Bitmap thumbnail', 19, 'RGB thumbnail'}->{$code}
Truth table0%0/2
A
A= {16, 'JPEG thumbnail', 17, 'Bitmap thumbnail', 19, 'RGB thumbnail'}->{$code}
Aresult
11
00
257  
258 0 ▶ if ($code == 0x10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 16) 0 0 0
259 0 eval {
260 with_io_string {
261 0 ▶ _process_file($info, $_, 1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 0 } $data;
263 };
264 0 ▶ $info->push_info(1, "error" => $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
265 }
266 }
267  
268 sub process_app1_exif
269 {
270 14 ▶ my($info, $data) = @_;
Subroutine process_app1_exif: called CC=15 SCAR=27.7
Pod undocumented
271 14 my $null = substr($data, 0, 1, "");
272 14 ▶ if ($null ne "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($null ne "\0") 0 14 14
273 0 $info->push_info(0, "Debug", "Exif chunk does not start with \\0");
274 0 return;
275 }
276  
277 14 require Image::TIFF;
278 14 my $t = eval { Image::TIFF->new(\$data) };
279 14 ▶ if (!$t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 0 14 14
280 0 $info->push_info(0, "Warn", "Cannot parse APP1 EXIF segment");
281 0 return;
282 }
283  
284  
285 14 for my $i (0 .. $t->num_ifds - 1) {
286 28 my $ifd = $t->ifd($i);
287  
288 # use Data::Dumper;
289 # print STDERR Dumper($ifd);
290  
291 28 for (@$ifd) {
292 # use Devel::Peek;
293 # print STDERR "# pushing info $i $_->[0] $_->[3]\n";
294 # print STDERR Devel::Peek::Dump($_->[3]),"\n" if $_->[0] =~ /Olympus-/;
295  
296 707 $info->push_info($i, $_->[0], $_->[3]);
297 }
298  
299 # If we find JPEGInterchangeFormat/JPEGInterchangeFormatLngth,
300 # then we should apply process_file kind of recursively to extract
301 # information of this (thumbnail) image file...
302 28 ▶ if (my($ipos) = $info->get_info($i, "JPEGInterchangeFormat", 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ipos) = $info->get_info($i, "JPEGInterchangeFormat", 1)) 12 16 28
303 12 my($ilen) = $info->get_info($i, "JPEGInterchangeFormatLength", 1);
304 12 ▶ if ($ilen)
Branch: 1 branch
BranchTrueFalseTotal
if ($ilen) 12 0 12
305 {
306 12 my $jdata = substr($data, $ipos, $ilen);
307 #$info->push_info($i, "JPEGImage" => $jdata);
308  
309 12 ▶ if ($jdata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jdata) 11 1 12
310 with_io_string {
311 11 ▶ _process_file($info, $_, $i);
Subroutine __ANON__: called CC=1 SCAR=0.0
312 11 } $jdata;
313 }
314 }
315 }
316  
317 # Turn XResolution/YResolution into 'resolution'
318 28 my($xres) = $info->get_info($i, "XResolution", 1);
319 28 my($yres) = $info->get_info($i, "YResolution", 1);
320  
321 # Samsung Digimax 200 is a totally confused camera that
322 # puts rational numbers with 0 as denominator and they
323 # also seem to not understand what resolution means.
324 28 for ($xres, $yres) {
325 56 ▶ $_ += 0 if ref($_) eq "Image::TIFF::Rational";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "Image::TIFF::Rational" 50 6 56
326 }
327  
328 28 my($unit) = $info->get_info($i, "ResolutionUnit", 1);
329 28 my $res = "1/1"; # default;
330 28 ▶ if ($xres && $yres) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xres and $yres) 26 2 28
Condition66%2/3
$xres and $yres
!ll&&!rl&&r
2026
MC/DC50%1/2
$xres and $yres
$xres $yres
Truth table66%2/3
A and B
A= $xresB= $yres
ABresult
0X0
100
111
331 26 ▶ $res = ($xres == $yres) ? $xres : "$xres/$yres";
Branch: 1 branch
BranchTrueFalseTotal
$xres == $yres ? : 25 1 26
332 }
333 28 ▶ $res .= " $unit" if $unit && $unit ne "pixels";
Branch: 1 branch
BranchTrueFalseTotal
if $unit and $unit ne "pixels" 26 2 28
Condition66%2/3
$unit and $unit ne "pixels"
!ll&&!rl&&r
2026
MC/DC50%1/2
$unit and $unit ne "pixels"
$unit $unit ne "pixels"
Truth table66%2/3
A and B
A= $unitB= $unit ne "pixels"
ABresult
0X0
100
111
334 28 $info->push_info($i, "resolution", $res);
335 }
336 }
337  
338 sub process_app14_adobe
339 {
340 0 ▶ my($info, $data) = @_;
Subroutine process_app14_adobe: not called CC=1 SCAR=6.9
Pod undocumented
341 0 my($version, $flags0, $flags1, $transform) = unpack("nnnC", $data);
342 0 $info->push_info(0, "AdobeTransformVersion" => $version);
343 0 $info->push_info(0, "AdobeTransformFlags" => [$flags0, $flags1]);
344 0 $info->push_info(0, "AdobeTransform" => $transform);
345 }
346  
347 8 1;
« lib/Image/Info/WEBP.pm ↑ Summary blib/lib/Image/Info/WBMP.pm »