lib/Image/Info/TIFF.pm

filter: Statementstmt 88.2% 113 / 128 Branchbran 74.0% 37 / 50 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 84.8% 168 / 198 CC 29 SCAR 23.8
CC
29
Cov
84%
_process_ifds
20.2
_hostbyteorder
4.9
_readrational
3.0
CRAP 10.8
« blib/lib/Image/Info/WBMP.pm ↑ Summary lib/Image/Info.pm »
1 package Image::Info::TIFF;
2  
3 5 $VERSION = 0.05;
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Image::TIFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 my @types = (
11 [ "ERROR INVALID TYPE", "?", 0],
12 [ "BYTE", "C", 1],
13 [ "ASCII", "A", 1],
14 [ "SHORT", "S", 2],
15 [ "LONG", "L", 4],
16 [ "RATIONAL", "N2", 8],
17 [ "SBYTE", "c", 1],
18 [ "UNDEFINED", "a", 1],
19 [ "SSHORT", "s", 2],
20 [ "SLONG", "l", 4],
21 [ "SRATIONAL", "N2", 8],
22 [ "FLOAT", "f", 4],
23 [ "DOUBLE", "d", 8],
24 );
25  
26 sub _hostbyteorder {
27 1023 ▶ my $hbo = $Config{byteorder};
Subroutine _hostbyteorder: called CC=3 SCAR=16.0
28 # we only care about the order, not the length (for 64 bit, it might
29 # be 12345678)
30 1023 ▶ if ($hbo =~ /^1234/) { return '1234' }
Branch: 1 branch
BranchTrueFalseTotal
if ($hbo =~ /^1234/) 1023 0 1023
31 0 ▶ if ($hbo =~ /4321$/) { return '4321' }
Branch: 1 branch
BranchTrueFalseTotal
if ($hbo =~ /4321$/) 0 0 0
32 0 die "Unexpected host byteorder: $hbo";
33 }
34  
35 sub _read
36 {
37 # read bytes, and move the file pointer forward
38 891 ▶ my($source, $len) = @_;
Subroutine _read: called CC=3 SCAR=11.0
39 891 my $buf;
40 891 my $n = read($source, $buf, $len);
41 891 ▶ die "read failed: $!" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 891 891
42 891 ▶ die "short read ($len/$n) at pos " . tell($source) unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 1 890 891
43 890 $buf;
44 }
45  
46 sub _readbytes
47 {
48 # read bytes, but make the file pointer stand still
49 42 ▶ my ($fh,$offset,$len) = @_;
Subroutine _readbytes: called CC=2 SCAR=7.0
50 42 my $curoffset = tell($fh);
51 42 my $buf;
52 42 seek($fh,$offset,0);
53 42 my $n = read($fh,$buf,$len);
54 42 ▶ confess("short read($n/$len)") unless $n == $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == $len 0 42 42
55 # back to before.
56 42 seek($fh,$curoffset,0);
57 42 return $buf;
58 }
59  
60 sub _readrational
61 {
62 22 ▶ my ($fh,$offset,$byteorder,$count,$ar,$signed) = @_;
Subroutine _readrational: called CC=3 SCAR=11.1
63 22 my $curoffset = tell($fh);
64 22 my $buf;
65 22 seek($fh,$offset,0);
66 22 ▶ while ($count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 22 22 44
67 22 my $num;
68 my $denom;
69 22 ▶ if ($signed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed) { } 0 22 22
70 0 $num = unpack("l",_read_order($fh,4,$byteorder));
71 0 $denom = unpack("l",_read_order($fh,4,$byteorder));
72 } else {
73 22 $num = unpack("L",_read_order($fh,4,$byteorder));
74 22 $denom = unpack("L",_read_order($fh,4,$byteorder));
75 }
76 22 push(@{$ar},new Image::TIFF::Rational($num,$denom));
77 22 $count--;
78 }
79 # back to before.
80 22 seek($fh,$curoffset,0);
81 }
82  
83 sub _read_order
84 {
85 885 ▶ my($source, $len,$byteorder) = @_;
Subroutine _read_order: called CC=3 SCAR=11.0
86  
87 885 my $buf = _read($source,$len);
88 # maybe reverse the read data?
89 884 ▶ if ($byteorder ne _hostbyteorder()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byteorder ne _hostbyteorder()) 83 801 884
90 83 my @bytes = unpack("C$len",$buf);
91 83 my @newbytes;
92 # swap bytes
93 83 ▶ for (my $i = $len-1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 254 83 337
94 254 push(@newbytes,$bytes[$i]);
95 }
96 83 $buf = pack("C$len",@newbytes);
97 }
98 884 $buf;
99 }
100  
101 5 my %order = (
102 "MM\x00\x2a" => '4321',
103 "II\x2a\x00" => '1234',
104 );
105  
106 sub process_file
107 {
108 6 ▶ my($info, $fh) = @_;
Subroutine process_file: called CC=3 SCAR=11.0
Pod documented
109  
110 6 my $soi = _read($fh, 4);
111 6 ▶ die "TIFF: SOI missing" unless (defined($order{$soi}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $order{$soi} 0 6 6
112 # XXX: should put this info in all pages?
113 6 $info->push_info(0, "file_media_type" => "image/tiff");
114 6 $info->push_info(0, "file_ext" => "tif");
115  
116 6 my $byteorder = $order{$soi};
117 6 my $ifdoff = unpack("L",_read_order($fh,4,$byteorder));
118 6 my $page = 0;
119 6 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$ifdoff and do { do { $ifdoff = _process_ifds($info, $fh, $page, 0, $byteorder, $ifdoff); $page++ } } 6 5 11
120 # print "TIFF Directory at $ifdoff\n";
121 12 $ifdoff = _process_ifds($info,$fh,$page,0,$byteorder,$ifdoff);
122 11 $page++;
123 } while ($ifdoff);
124 }
125  
126 sub _process_ifds {
127 12 ▶ my($info, $fh, $page, $tagsseen, $byteorder, $ifdoffset) = @_;
Subroutine _process_ifds: called CC=18 SCAR=30.0
128 12 my $curpos = tell($fh);
129 12 seek($fh,$ifdoffset,0);
130  
131 12 my $n = unpack("S",_read_order($fh, 2, $byteorder)); ## Number of entries
132 11 my $i = 1;
133 11 ▶ while ($n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 203 11 214
134 # process one IFD entry
135 203 my $tag = unpack("S",_read_order($fh,2,$byteorder));
136 203 my $fieldtype = unpack("S",_read_order($fh,2,$byteorder));
137 203 ▶ unless ($types[$fieldtype]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($types[$fieldtype]) 0 203 203
138 0 my $warnmsg = "Unrecognised fieldtype $fieldtype, ignoring following entries";
139 0 warn "$warnmsg\n";
140 0 $info->push_info($page, "Warn" => $warnmsg);
141 0 return 0;
142 }
143 203 my ($typename, $typepack, $typelen) = @{$types[$fieldtype]};
144 203 my $count = unpack("L",_read_order($fh,4,$byteorder));
145 203 my $value_offset_orig = _read_order($fh,4,$byteorder);
146 203 my $value_offset = unpack("L", $value_offset_orig);
147 203 my $val;
148 ## The 4 bytes of $value_offset may actually contains the value itself,
149 ## if it fits into 4 bytes.
150 203 my $len = $typelen * $count;
151 203 ▶ if ($len <= 4) {
Branch: 4 branches
BranchTrueFalseTotal
if ($len <= 4) { } 139 64 203
elsif ($fieldtype == 2) { } 31 33 64
elsif ($fieldtype == 5) { } 22 11 33
elsif ($fieldtype == 10) { } 0 11 11
152 139 ▶ if (($byteorder ne _hostbyteorder()) && ($len != 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byteorder ne _hostbyteorder() and $len != 4) 12 127 139
Condition100%3/3
$byteorder ne _hostbyteorder() and $len != 4
!ll&&!rl&&r
125212
MC/DC100%2/2
$byteorder ne _hostbyteorder() and $len != 4
$byteorder ne _hostbyteorder() $len != 4
Truth table100%3/3
A and B
A= $byteorder ne _hostbyteorder()B= $len != 4
ABresult
0X0
100
111
153 12 my @bytes = unpack("C4", $value_offset_orig);
154 12 ▶ for (my $i=0; $i < 4 - $len; $i++) { shift @bytes; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4 - $len) 24 12 36
155 12 $value_offset_orig = pack("C$len", @bytes);
156 }
157 139 @$val = unpack($typepack x $count, $value_offset_orig);
158 } elsif ($fieldtype == 2) {
159 ## ASCII text. The last byte is a NUL, which we don't need
160 ## to include in the Perl string, so read one less than the count.
161 31 @$val = _readbytes($fh, $value_offset, $count - 1);
162 } elsif ($fieldtype == 5) {
163 ## Unsigned Rational
164 22 $val = [];
165 22 _readrational($fh,$value_offset,$byteorder,$count,$val,0);
166 } elsif ($fieldtype == 10) {
167 ## Signed Rational
168 0 $val = [];
169 0 _readrational($fh,$value_offset,$byteorder,$count,$val,1);
170 } else {
171 ## Just read $count thingies from the offset
172 11 @$val = unpack($typepack x $count, _readbytes($fh, $value_offset, $typelen * $count));
173 }
174 #look up tag
175 203 my $tn = Image::TIFF->exif_tagname($tag);
176 203 foreach my $v (@$val) {
177 221 ▶ if (ref($tn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tn) 66 155 221
178 66 $v = $$tn{$v};
179 66 $tn = $$tn{__TAG__};
180 }
181 }
182 203 ▶ if ($tn eq "NewSubfileType") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tn eq 'NewSubfileType') { } 0 203 203
183 # start new page if necessary
184 0 ▶ if ($tagsseen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagsseen) 0 0 0
185 0 $page++;
186 0 $tagsseen = 0;
187 }
188 } else {
189 203 $tagsseen = 1;
190 }
191 203 my $vval;
192 ## If only one value, use direct
193 203 ▶ if (@$val <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$val <= 1) { } 192 11 203
194 192 ▶ $val = $val->[0] || '';
Condition50%1/2
$val->[0] || ''
l!l
1920
MC/DC0%0/1
$val->[0] || ''
$val->[0]
Truth table50%1/2
A
A= $val->[0]
Aresult
11
00
195 192 $vval = $val;
196 } else {
197 11 $vval = '(' . join(',',@$val) . ')';
198 }
199 # print "$page/$i:$value_offset:$tag ($tn), fieldtype: $fieldtype, count: $count = $vval\n";
200 203 ▶ if ($tn eq "ExifOffset") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tn eq "ExifOffset") 0 203 203
201 # parse ExifSubIFD
202 # print "ExifSubIFD at $value_offset\n";
203 0 _process_ifds($info,$fh,$page,$tagsseen,$byteorder,$value_offset);
204 }
205 203 $info->push_info($page, $tn => $val);
206 203 $n--;
207 203 $i++;
208 }
209 11 my $ifdoff = unpack("L",_read_order($fh,4,$byteorder));
210 #print "next dir at $ifdoff\n";
211 11 seek($fh,$curpos,0);
212 11 ▶ return $ifdoff if $ifdoff;
Branch: 1 branch
BranchTrueFalseTotal
if $ifdoff 6 5 11
213 5 0;
214 }
215 5 1;
216  
217 __END__
« blib/lib/Image/Info/WBMP.pm ↑ Summary lib/Image/Info.pm »