API2/Resource/XObject/Image/TIFF.pm

filter: Statementstmt 51.7% 103 / 199 Branchbran 42.0% 21 / 50 Conditioncond 40.0% 8 / 20 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 82.3% 14 / 17 Podpod 25.0% 2 / 8 totaltotal 48.8% 150 / 307 SCAR 56.2
CC
42
Cov
49%
read_tiff
72.7
handle_ccitt
56.0
handle_flate
12.0
CRAP 275.0
« API2/Basic/PDF/Filter/FlateDecode.pm ↑ Summary API2/Resource/CIDFont/TrueType.pm »
1 package PDF::API2::Resource::XObject::Image::TIFF;
2  
3 2 ▶use base 'PDF::API2::Resource::XObject::Image';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '2.044'; # VERSION
11  
12 2 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use PDF::API2::Resource::XObject::Image::TIFF::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 PDF::API2::Resource::XObject::Image::TIFF - TIFF image support
22  
23 =head1 METHODS
24  
25 =over
26  
27 =item $res = PDF::API2::Resource::XObject::Image::TIFF->new $pdf, $file [, $name]
28  
29 Returns a tiff-image object.
30  
31 =cut
32  
33 sub new {
34 4 ▶ my ($class, $pdf, $file, $name) = @_;
Subroutine new: called CC=4 SCAR=14.1
Pod documented
35 4 my $self;
36  
37 4 my $tif = PDF::API2::Resource::XObject::Image::TIFF::File->new($file);
38  
39 # in case of problematic things
40 # proxy to other modules
41  
42 3 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 3 3
43  
44 3 ▶ $self = $class->SUPER::new($pdf, $name || 'Ix' . pdfkey());
Condition50%1/2
$name || 'Ix' . pdfkey()
l!l
03
MC/DC0%0/1
$name || 'Ix' . pdfkey()
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
45 3 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 3 3
46  
47 3 $self->{' apipdf'} = $pdf;
48 3 weaken $self->{' apipdf'};
49  
50 3 $self->read_tiff($pdf, $tif);
51  
52 3 $tif->close();
53  
54 3 return $self;
55 }
56  
57 sub deLZW {
58 1 ▶ my ($ibits, $stream) = @_;
Subroutine deLZW: called CC=8 SCAR=21.4
Pod undocumented
59 1 my $bits = $ibits;
60 1 my $resetcode = 1 << ($ibits - 1);
61 1 my $endcode = $resetcode + 1;
62 1 my $nextcode = $endcode + 1;
63 1 my $ptr = 0;
64 1 $stream = unpack('B*', $stream);
65 1 my $maxptr = length($stream);
66 1 my $tag;
67 1 my $out = '';
68 1 my $outptr = 0;
69  
70 # print STDERR "reset=$resetcode\nend=$endcode\nmax=$maxptr\n";
71  
72 1 my @d = map { chr($_) } (0 .. $resetcode - 1);
73  
74 1 ▶ while (($ptr + $bits) <= $maxptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr + $bits <= $maxptr) 3 0 3
75 3 $tag=0;
76 3 foreach my $off (reverse 1 .. $bits) {
77 27 $tag <<= 1;
78 27 $tag |= substr($stream, $ptr + $bits - $off, 1);
79 }
80 # print STDERR "ptr=$ptr,tag=$tag,bits=$bits,next=$nextcode\n";
81 # print STDERR "tag to large\n" if($tag>$nextcode);
82 3 $ptr += $bits;
83 3 ▶ if ($tag == $resetcode) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag == $resetcode) { } 1 2 3
elsif ($tag == $endcode) { } 1 1 2
elsif ($tag < $resetcode) { } 1 0 1
elsif ($tag > $endcode) { } 0 0 0
84 1 $bits = $ibits;
85 1 $nextcode = $endcode + 1;
86 1 next;
87 }
88 elsif ($tag == $endcode) {
89 1 last;
90 }
91 elsif ($tag < $resetcode) {
92 1 $d[$nextcode] = $d[$tag];
93 1 $out .= $d[$nextcode];
94 1 $nextcode++;
95 }
96 elsif ($tag > $endcode) {
97 0 $d[$nextcode] = $d[$tag];
98 0 $d[$nextcode] .= substr($d[$tag + 1], 0, 1);
99 0 $out .= $d[$nextcode];
100 0 $nextcode++;
101 }
102 1 ▶ $bits++ if $nextcode == (1 << $bits);
Branch: 1 branch
BranchTrueFalseTotal
if $nextcode == 1 << $bits 0 1 1
103 }
104 1 return $out;
105 }
106  
107 sub handle_generic {
108 2 ▶ my ($self, $pdf, $tif) = @_;
Subroutine handle_generic: called CC=4 SCAR=20.3
Pod undocumented
109  
110 2 ▶ if ($tif->{'filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tif->{'filter'}) { } 0 2 2
111 # should we die here?
112 # die "unknown tiff-compression";
113 0 $self->filters($tif->{filter});
114 0 $self->{' nofilt'} = 1;
115 }
116 else {
117 2 $self->filters('FlateDecode');
118 }
119  
120 2 ▶ if (ref($tif->{'imageOffset'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tif->{'imageOffset'}) { } 0 2 2
121 0 $self->{' stream'} = '';
122 0 my $d = scalar @{$tif->{'imageOffset'}};
123 0 foreach (1..$d) {
124 0 my $buf;
125 0 $tif->{'fh'}->seek(shift(@{$tif->{'imageOffset'}}), 0);
126 0 $tif->{'fh'}->read($buf, shift(@{$tif->{'imageLength'}}));
127 0 $self->{' stream'} .= $buf;
128 }
129 }
130 else {
131 2 $tif->{'fh'}->seek($tif->{'imageOffset'}, 0);
132 2 $tif->{'fh'}->read($self->{' stream'}, $tif->{'imageLength'});
133 }
134  
135 2 return $self;
136 }
137  
138 sub handle_flate {
139 0 ▶ my ($self, $pdf, $tif) = @_;
Subroutine handle_flate: not called CC=3 SCAR=24.8
Pod undocumented
140 0 $self->filters('FlateDecode');
141  
142 0 ▶ if (ref($tif->{'imageOffset'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tif->{'imageOffset'}) { } 0 0 0
143 0 $self->{' stream'} = '';
144 0 my $d = scalar @{$tif->{'imageOffset'}};
145 0 foreach (1 .. $d) {
146 0 my $buf;
147 0 $tif->{'fh'}->seek(shift(@{$tif->{'imageOffset'}}),0);
148 0 $tif->{'fh'}->read($buf, shift(@{$tif->{'imageLength'}}));
149 0 $buf=uncompress($buf);
150 0 $self->{' stream'} .= $buf;
151 }
152 }
153 else {
154 0 $tif->{'fh'}->seek($tif->{'imageOffset'}, 0);
155 0 $tif->{'fh'}->read($self->{' stream'}, $tif->{'imageLength'});
156 0 $self->{' stream'} = uncompress($self->{' stream'});
157 }
158  
159 0 return $self;
160 }
161  
162 sub handle_lzw {
163 1 ▶ my ($self, $pdf, $tif) = @_;
Subroutine handle_lzw: called CC=5 SCAR=22.1
Pod undocumented
164 1 $self->filters('FlateDecode');
165 1 my $imageWidth = $tif->{'imageWidth'};
166 1 my $mod = $imageWidth % 8;
167 1 ▶ if ($mod > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod > 0) 1 0 1
168 1 $imageWidth += 8 - $mod;
169 }
170 1 my $max_raw_strip = $imageWidth * $tif->{'bitsPerSample'} * $tif->{'RowsPerStrip'} / 8;
171  
172 1 ▶ if (ref($tif->{'imageOffset'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tif->{'imageOffset'}) { } 0 1 1
173 0 $self->{' stream'}='';
174 0 my $d = scalar @{$tif->{'imageOffset'}};
175 0 foreach (1 .. $d) {
176 0 my $buf;
177 0 $tif->{'fh'}->seek(shift(@{$tif->{imageOffset}}), 0);
178 0 $tif->{'fh'}->read($buf, shift(@{$tif->{'imageLength'}}));
179 0 $buf = deLZW(9, $buf);
180 0 ▶ if (length($buf) > $max_raw_strip) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > $max_raw_strip) 0 0 0
181 0 $buf = substr($buf, 0, $max_raw_strip);
182 }
183 0 $self->{' stream'} .= $buf;
184 }
185 }
186 else {
187 1 $tif->{'fh'}->seek($tif->{'imageOffset'}, 0);
188 1 $tif->{'fh'}->read($self->{' stream'}, $tif->{'imageLength'});
189 1 $self->{' stream'} = deLZW(9, $self->{' stream'});
190 }
191  
192 1 return $self;
193 }
194  
195 sub handle_ccitt {
196 0 ▶ my ($self, $pdf, $tif) = @_;
Subroutine handle_ccitt: not called CC=7 SCAR=40.3
Pod undocumented
197  
198 0 $self->{' nofilt'} = 1;
199 0 $self->{'Filter'} = PDFName('CCITTFaxDecode');
200 0 $self->{'DecodeParms'} = PDFDict();
201 0 ▶ $self->{'DecodeParms'}->{'K'} = (($tif->{'ccitt'} == 4 || ($tif->{'g3Options'} & 0x1)) ? PDFNum(-1) : PDFNum(0));
Branch: 1 branch
BranchTrueFalseTotal
$tif->{'ccitt'} == 4 || $tif->{'g3Options'} & 1 ? : 0 0 0
Condition0%0/3
$tif->{"ccitt"} == 4 or $tif->{"g3Options"} & 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$tif->{"ccitt"} == 4 or $tif->{"g3Options"} & 1
$tif->{"ccitt"} == 4 $tif->{"g3Options"} & 1
Truth table0%0/3
A or B
A= $tif->{"ccitt"} == 4B= $tif->{"g3Options"} & 1
ABresult
1X1
011
000
202 0 $self->{'DecodeParms'}->{'Columns'} = PDFNum($tif->{'imageWidth'});
203 0 $self->{'DecodeParms'}->{'Rows'} = PDFNum($tif->{'imageHeight'});
204 0 ▶ $self->{'DecodeParms'}->{'Blackls1'} = PDFBool($tif->{'whiteIsZero'} == 1 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tif->{'whiteIsZero'} == 1 ? : 0 0 0
205 0 ▶ if (defined($tif->{'g3Options'}) && ($tif->{'g3Options'} & 0x4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tif->{"g3Options"} and $tif->{"g3Options"} & 4) 0 0 0
Condition0%0/3
defined $tif->{"g3Options"} and $tif->{"g3Options"} & 4
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tif->{"g3Options"} and $tif->{"g3Options"} & 4
defined $tif->{"g3Options"} $tif->{"g3Options"} & 4
Truth table0%0/3
A and B
A= defined $tif->{"g3Options"}B= $tif->{"g3Options"} & 4
ABresult
0X0
100
111
206 0 $self->{'DecodeParms'}->{'EndOfLine'} = PDFBool(1);
207 0 $self->{'DecodeParms'}->{'EncodedByteAlign'} = PDFBool(1);
208 }
209 # $self->{'DecodeParms'} = PDFArray($self->{'DecodeParms'});
210 0 $self->{'DecodeParms'}->{'DamagedRowsBeforeError'} = PDFNum(100);
211  
212 0 ▶ if (ref($tif->{'imageOffset'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tif->{'imageOffset'}) { } 0 0 0
213 0 die "chunked ccitt g4 tif not supported.";
214 }
215 else {
216 0 $tif->{'fh'}->seek($tif->{'imageOffset'}, 0);
217 0 $tif->{'fh'}->read($self->{' stream'}, $tif->{'imageLength'});
218 }
219  
220 0 return $self;
221 }
222  
223 sub read_tiff {
224 3 ▶ my ($self, $pdf, $tif) = @_;
Subroutine read_tiff: called CC=17 SCAR=42.9
Pod undocumented
225  
226 3 $self->width($tif->{'imageWidth'});
227 3 $self->height($tif->{'imageHeight'});
228 3 ▶ if ($tif->{'colorSpace'} eq 'Indexed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tif->{'colorSpace'} eq 'Indexed') { } 0 3 3
229 0 my $dict = PDFDict();
230 0 $pdf->new_obj($dict);
231 0 $self->colorspace(PDFArray(PDFName($tif->{'colorSpace'}), PDFName('DeviceRGB'), PDFNum(255), $dict));
232 0 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
233 0 $tif->{'fh'}->seek($tif->{'colorMapOffset'}, 0);
234 0 my $colormap;
235 my $straight;
236 0 $tif->{'fh'}->read($colormap, $tif->{'colorMapLength'});
237 0 $dict->{' stream'} = '';
238 0 $straight .= pack('C', ($_ / 256)) for unpack($tif->{'short'} . '*', $colormap);
239 0 foreach my $c (0 .. (($tif->{'colorMapSamples'} / 3) - 1)) {
240 0 $dict->{' stream'} .= substr($straight, $c, 1);
241 0 $dict->{' stream'} .= substr($straight, $c + ($tif->{'colorMapSamples'} / 3), 1);
242 0 $dict->{' stream'} .= substr($straight, $c + ($tif->{'colorMapSamples'} / 3) * 2, 1);
243 }
244 }
245 else {
246 3 $self->colorspace($tif->{'colorSpace'});
247 }
248  
249 3 $self->{'Interpolate'} = PDFBool(1);
250 3 $self->bpc($tif->{'bitsPerSample'});
251  
252 3 ▶ if ($tif->{'whiteIsZero'} == 1 && $tif->{'filter'} ne 'CCITTFaxDecode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tif->{"whiteIsZero"} == 1 and $tif->{"filter"} ne "CCITTFaxDecode") 1 2 3
Condition66%2/3
$tif->{"whiteIsZero"} == 1 and $tif->{"filter"} ne "CCITTFaxDecode"
!ll&&!rl&&r
201
MC/DC50%1/2
$tif->{"whiteIsZero"} == 1 and $tif->{"filter"} ne "CCITTFaxDecode"
$tif->{"whiteIsZero"} == 1 $tif->{"filter"} ne "CCITTFaxDecode"
Truth table66%2/3
A and B
A= $tif->{"whiteIsZero"} == 1B= $tif->{"filter"} ne "CCITTFaxDecode"
ABresult
0X0
100
111
253 1 $self->{'Decode'} = PDFArray(PDFNum(1), PDFNum(0));
254 }
255  
256 # check filters and handle seperately
257 3 ▶ if (defined $tif->{'filter'} and $tif->{'filter'} eq 'CCITTFaxDecode') {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $tif->{'filter'} and $tif->{'filter'} eq 'CCITTFaxDecode') { } 0 3 3
elsif (defined $tif->{'filter'} and $tif->{'filter'} eq 'LZWDecode') { } 1 2 3
elsif (defined $tif->{'filter'} and $tif->{'filter'} eq 'FlateDecode') { } 0 2 2
Condition66%2/3
defined $tif->{"filter"} and $tif->{"filter"} eq "CCITTFaxDecode"
!ll&&!rl&&r
210
MC/DC0%0/2
defined $tif->{"filter"} and $tif->{"filter"} eq "CCITTFaxDecode"
defined $tif->{"filter"} $tif->{"filter"} eq "CCITTFaxDecode"
Truth table66%2/3
A and B
A= defined $tif->{"filter"}B= $tif->{"filter"} eq "CCITTFaxDecode"
ABresult
0X0
100
111
258 0 $self->handle_ccitt($pdf, $tif);
259 }
260 ▶ elsif (defined $tif->{'filter'} and $tif->{'filter'} eq 'LZWDecode') {
Condition66%2/3
defined $tif->{"filter"} and $tif->{"filter"} eq "LZWDecode"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $tif->{"filter"} and $tif->{"filter"} eq "LZWDecode"
defined $tif->{"filter"} $tif->{"filter"} eq "LZWDecode"
Truth table66%2/3
A and B
A= defined $tif->{"filter"}B= $tif->{"filter"} eq "LZWDecode"
ABresult
0X0
100
111
261 1 $self->handle_lzw($pdf, $tif);
262 }
263 ▶ elsif (defined $tif->{'filter'} and $tif->{filter} eq 'FlateDecode') {
Condition33%1/3
defined $tif->{"filter"} and $tif->{"filter"} eq "FlateDecode"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $tif->{"filter"} and $tif->{"filter"} eq "FlateDecode"
defined $tif->{"filter"} $tif->{"filter"} eq "FlateDecode"
Truth table33%1/3
A and B
A= defined $tif->{"filter"}B= $tif->{"filter"} eq "FlateDecode"
ABresult
0X0
100
111
264 0 $self->handle_flate($pdf, $tif);
265 }
266 else {
267 2 $self->handle_generic($pdf, $tif);
268 }
269  
270 3 ▶ if ($tif->{'fillOrder'} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tif->{"fillOrder"} == 2) 0 3 3
271 0 my @bl = ();
272 0 foreach my $n (0 .. 255) {
273 0 my $b = $n;
274 0 my $f = 0;
275 0 foreach (0 .. 7) {
276 0 my $bit = 0;
277 0 ▶ if ($b & 0x1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b & 1) 0 0 0
278 0 $bit = 1;
279 }
280 0 $b >>= 1;
281 0 $f <<= 1;
282 0 $f |= $bit;
283 }
284 0 $bl[$n] = $f;
285 }
286 0 my $l = length($self->{' stream'}) - 1;
287 0 foreach my $n (0 .. $l) {
288 0 vec($self->{' stream'}, $n, 8) = $bl[vec($self->{' stream'}, $n, 8)];
289 }
290 }
291 3 $self->{' tiff'} = $tif;
292  
293 3 return $self;
294 }
295  
296 =item $value = $tif->tiffTag $tag
297  
298 returns the value of the internal tiff-tag.
299  
300 B<Useful Tags:>
301  
302 imageDescription, imageId (strings)
303 xRes, yRes (dpi; pixel/cm if resUnit==3)
304 resUnit
305  
306 =cut
307  
308 sub tiffTag {
309 0 ▶ my ($self, $tag) = @_;
Subroutine tiffTag: not called CC=1 SCAR=6.9
Pod documented
310 0 return $self->{' tiff'}->{$tag};
311 }
312  
313 =back
314  
315 =cut
316  
317 2 1;
« API2/Basic/PDF/Filter/FlateDecode.pm ↑ Summary API2/Resource/CIDFont/TrueType.pm »