lib/Image/Info/JPEG.pm

filter: Statementstmt 82.1% 120 / 146 Branchbran 67.4% 58 / 86 Conditioncond 48.2% 14 / 29 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 9 totaltotal 68.5% 207 / 302 CC 59 SCAR 25.8
CC
59
Cov
74%
process_app0_jfxx
20.0
process_chunk
19.2
process_app1_exif
17.2
CRAP 13.2
« 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 10 $VERSION = 0.07;
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 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 10 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 880 ▶{
Subroutine my_read: called CC=3 SCAR=11.2
Pod undocumented
54 880 my($source, $len) = @_;
55 880 my $buf;
56 880 my $n = read($source, $buf, $len);
57 880 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 880 880
58 880 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 880 880
59 880 $buf;
60 }
61  
62 10 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.2
63 10 ▶ my $f = ($] >= 5.008) ? <<'EOT' : <<'EOT';
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.008 ? : 10 0 10
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 10 ▶ eval $f;
Subroutine with_io_string: called CC=1 SCAR=0.0
Pod undocumented
80 10 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 10 10
81 }
82  
83 sub process_file
84 21 ▶{
Subroutine process_file: called CC=1 SCAR=0.0
Pod undocumented
85 21 my($info, $fh, $cnf) = @_;
86 21 _process_file($info, $fh, 0);
87 }
88  
89 sub _process_file
90 33 ▶{
Subroutine _process_file: called CC=11 SCAR=24.1
91 33 my($info, $fh, $img_no) = @_;
92  
93 33 my $soi = my_read($fh, 2);
94 33 ▶ unless ($soi eq "\xFF\xD8") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($soi eq "\377\330") 1 32 33
95 1 my $ofs = tell() - 2;
96 1 die "SOI missing in JPEG file at offset $ofs";
97 }
98  
99 32 $info->push_info($img_no, "file_media_type" => "image/jpeg");
100 32 $info->push_info($img_no, "file_ext" => "jpg");
101  
102 32 while (1) {
103 299 my($ff, $mark) = unpack("CC", my_read($fh, 2));
104 299 ▶ last if !defined $ff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff 0 299 299
105 299 ▶ if ($ff != 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ff != 255) 3 296 299
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 299 ▶ if ($mark == 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark == 255) 2 297 299
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 299 ▶ last if $mark == 0xDA || $mark == 0xD9; # SOS/EOI
Branch: 1 branch
BranchTrueFalseTotal
if $mark == 218 or $mark == 217 32 267 299
Condition66%2/3
$mark == 218 or $mark == 217
l!l&&r!l&&!r
320267
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 267 my($len) = unpack("n", my_read($fh, 2));
125 267 ▶ last if $len < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 2 0 267 267
126 267 process_chunk($info, $img_no, $mark, my_read($fh, $len - 2));
127 }
128 }
129  
130 sub process_chunk
131 267 ▶{
Subroutine process_chunk: called CC=16 SCAR=29.6
Pod undocumented
132 267 my($info, $img_no, $mark, $data) = @_;
133 #printf "MARK 0x%02X, len=%d\n", $mark, length($data);
134  
135 267 ▶ if ($mark == 0xFE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($mark == 254) { } 3 264 267
elsif ($mark >= 224 and $mark <= 239) { } 52 212 264
elsif ($sof{$mark}) { } 32 180 212
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
212052
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 52 ▶ process_app($info, $mark, $data) if $img_no == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $img_no == 0 41 11 52
140 }
141 elsif ($sof{$mark}) {
142 32 my($precision, $height, $width, $num_comp) =
143 unpack("CnnC", substr($data, 0, 6, ""));
144 32 $info->push_info($img_no, "JPEG_Type", $sof{$mark});
145  
146 # fix bug #15167 by keeping the highest values
147 32 ▶ my $old_w = $info->get_info($img_no, "width") || -1;
Condition50%1/2
$info->get_info($img_no, 'width') || -1
l!l
032
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 32 ▶ my $old_h = $info->get_info($img_no, "height") || -1;
Condition50%1/2
$info->get_info($img_no, 'height') || -1
l!l
032
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 32 ▶ $info->replace_info($img_no, "width", $width) if $old_w < $width;
Branch: 1 branch
BranchTrueFalseTotal
if $old_w < $width 32 0 32
151 32 ▶ $info->replace_info($img_no, "height", $height) if $old_h < $height;
Branch: 1 branch
BranchTrueFalseTotal
if $old_h < $height 32 0 32
152  
153 32 for (1..$num_comp) {
154 96 $info->push_info($img_no, "BitsPerSample", $precision);
155 }
156 32 $info->push_info($img_no, "SamplesPerPixel" => $num_comp);
157  
158 # XXX need to consider JFIF/Adobe markers to determine this...
159 32 ▶ if ($num_comp == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($num_comp == 1) { } 0 32 32
elsif ($num_comp == 3) { } 32 0 32
elsif ($num_comp == 4) { } 0 0 0
160 0 $info->push_info($img_no, "color_type" => "Gray");
161 }
162 elsif ($num_comp == 3) {
163 32 $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 32 if (1) {
170 32 my %comp_id_lookup = ( 1 => "Y",
171 2 => "Cb",
172 3 => "Cr",
173 82 => "R",
174 71 => "G",
175 66 => "B" );
176 32 ▶ while (length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 96 32 128
177 96 my($comp_id, $hv, $qtable) =
178 unpack("CCC", substr($data, 0, 3, ""));
179 96 my $horiz_sf = $hv >> 4 & 0x0f;
180 96 my $vert_sf = $hv & 0x0f;
181 96 ▶ $comp_id = $comp_id_lookup{$comp_id} || $comp_id;
Condition33%1/3
$comp_id_lookup{$comp_id} || $comp_id
l!l&&r!l&&!r
9600
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 96 $info->push_info($img_no, "ColorComponents", [$comp_id, $hv, $qtable]);
183 96 $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 41 ▶{
Subroutine process_app: called CC=5 SCAR=16.6
Pod undocumented
195 41 my($info, $mark, $data) = @_;
196 41 my $app = $mark - 0xE0;
197 41 my $id = substr($data, 0, 5, "");
198 #$info->push_info(0, "Debug", "APP$app $id");
199 41 $id = "$app-$id";
200 41 ▶ if ($id eq "0-JFIF\0") {
Branch: 4 branches
BranchTrueFalseTotal
if ($id eq "0-JFIF\000") { } 21 20 41
elsif ($id eq "0-JFXX\000") { } 0 20 20
elsif ($id eq "1-Exif\000") { } 18 2 20
elsif ($id eq '14-Adobe') { } 0 2 2
201 21 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 18 process_app1_exif($info, $data);
208 }
209 elsif ($id eq "14-Adobe") {
210 0 process_app14_adobe($info, $data);
211 }
212 else {
213 2 $info->push_info(0, "App$id", $data);
214 #printf " %s\n", Data::Dump::dump($data);
215 }
216 }
217  
218 sub process_app0_jfif
219 21 ▶{
Subroutine process_app0_jfif: called CC=8 SCAR=26.3
Pod undocumented
220 21 my($info, $data) = @_;
221 21 ▶ if (length $data < 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 9) 0 21 21
222 0 $info->push_info(0, "Debug", "Short JFIF chunk");
223 0 return;
224 }
225 21 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 21 $info->push_info(0, "JFIF_Version", sprintf("%d.%02d", $ver_hi, $ver_lo));
228  
229 21 ▶ my $res = $x_density != $y_density || !$unit
Branch: 1 branch
BranchTrueFalseTotal
$x_density != $y_density || !$unit ? : 0 21 21
Condition33%1/3
$x_density != $y_density or not $unit
l!l&&r!l&&!r
0021
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 21 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 21 0 21
233 $unit = { 0 => "pixels",
234 1 => "dpi",
235 2 => "dpcm"
236 21 ▶ }->{$unit} || "jfif-unit-$unit";
Condition50%1/2
{0, 'pixels', 1, 'dpi', 2, 'dpcm'}->{$unit} || "jfif-unit-$unit"
l!l
210
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 21 $res .= " $unit";
238 }
239 21 $info->push_info(0, "resolution", $res);
240  
241 21 ▶ if ($x_thumb || $y_thumb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_thumb or $y_thumb) 0 21 21
Condition33%1/3
$x_thumb or $y_thumb
l!l&&r!l&&!r
0021
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 0 ▶{
Subroutine process_app0_jfxx: not called CC=4 SCAR=30.0
Pod undocumented
250 0 my($info, $data) = @_;
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 0 ▶ with_io_string {
Subroutine __ANON__: not called CC=1 SCAR=6.9
261 0 _process_file($info, $_, 1);
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 18 ▶{
Subroutine process_app1_exif: called CC=16 SCAR=28.4
Pod undocumented
270 18 my($info, $data) = @_;
271 18 my $null = substr($data, 0, 1, "");
272 18 ▶ if ($null ne "\0") {
Branch: 1 branch
BranchTrueFalseTotal
if ($null ne "\0") 0 18 18
273 0 $info->push_info(0, "Debug", "Exif chunk does not start with \\0");
274 0 return;
275 }
276  
277 18 require Image::TIFF;
278 18 my $t = eval { Image::TIFF->new(\$data) };
279 18 ▶ if (!$t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 0 18 18
280 0 $info->push_info(0, "Warn", "Cannot parse APP1 EXIF segment");
281 0 return;
282 }
283  
284  
285 18 for my $i (0 .. $t->num_ifds - 1) {
286 33 my $ifd = $t->ifd($i);
287  
288 # use Data::Dumper;
289 # print STDERR Dumper($ifd);
290  
291 33 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 809 $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 33 ▶ if (my($ipos) = $info->get_info($i, "JPEGInterchangeFormat", 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($ipos) = $info->get_info($i, "JPEGInterchangeFormat", 1)) 13 20 33
303 13 my($ilen) = $info->get_info($i, "JPEGInterchangeFormatLength", 1);
304 13 ▶ if ($ilen)
Branch: 1 branch
BranchTrueFalseTotal
if ($ilen) 13 0 13
305 {
306 13 my $jdata = substr($data, $ipos, $ilen);
307 #$info->push_info($i, "JPEGImage" => $jdata);
308  
309 13 ▶ if ($jdata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jdata) 12 1 13
310 12 eval {
311 12 ▶ with_io_string {
Subroutine __ANON__: called CC=1 SCAR=0.0
312 12 _process_file($info, $_, $i);
313 12 } $jdata;
314 };
315 12 ▶ $info->push_info($i, "error" => $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 11 12
316 }
317 }
318 }
319  
320 # Turn XResolution/YResolution into 'resolution'
321 33 my($xres) = $info->get_info($i, "XResolution", 1);
322 33 my($yres) = $info->get_info($i, "YResolution", 1);
323  
324 # Samsung Digimax 200 is a totally confused camera that
325 # puts rational numbers with 0 as denominator and they
326 # also seem to not understand what resolution means.
327 33 for ($xres, $yres) {
328 66 ▶ $_ += 0 if ref($_) eq "Image::TIFF::Rational";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "Image::TIFF::Rational" 62 4 66
329 }
330  
331 33 my($unit) = $info->get_info($i, "ResolutionUnit", 1);
332 33 my $res = "1/1"; # default;
333 33 ▶ if ($xres && $yres) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xres and $yres) 31 2 33
Condition66%2/3
$xres and $yres
!ll&&!rl&&r
2031
MC/DC50%1/2
$xres and $yres
$xres $yres
Truth table66%2/3
A and B
A= $xresB= $yres
ABresult
0X0
100
111
334 31 ▶ $res = ($xres == $yres) ? $xres : "$xres/$yres";
Branch: 1 branch
BranchTrueFalseTotal
$xres == $yres ? : 31 0 31
335 }
336 33 ▶ $res .= " $unit" if $unit && $unit ne "pixels";
Branch: 1 branch
BranchTrueFalseTotal
if $unit and $unit ne "pixels" 31 2 33
Condition66%2/3
$unit and $unit ne "pixels"
!ll&&!rl&&r
2031
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
337 33 $info->push_info($i, "resolution", $res);
338 }
339 }
340  
341 sub process_app14_adobe
342 0 ▶{
Subroutine process_app14_adobe: not called CC=1 SCAR=6.9
Pod undocumented
343 0 my($info, $data) = @_;
344 0 my($version, $flags0, $flags1, $transform) = unpack("nnnC", $data);
345 0 $info->push_info(0, "AdobeTransformVersion" => $version);
346 0 $info->push_info(0, "AdobeTransformFlags" => [$flags0, $flags1]);
347 0 $info->push_info(0, "AdobeTransform" => $transform);
348 }
349  
350 10 1;
« lib/Image/Info/WEBP.pm ↑ Summary blib/lib/Image/Info/WBMP.pm »