Builder/Basic/PDF/Filter/LZWDecode.pm

filter: Statementstmt 95.1% 178 / 187 Branchbran 70.7% 58 / 82 Conditioncond 53.3% 8 / 15 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 15 / 15 Podpod 75.0% 3 / 4 totaltotal 84.6% 265 / 313 CC 53 SCAR 22.1
CC
53
Cov
86%
infilt
17.1
_predict
10.5
_depredict
9.5
CRAP 9.1
« Builder/Basic/PDF/Filter/ASCII85Decode.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/code3of9.pm »
1 package PDF::Builder::Basic::PDF::Filter::LZWDecode;
2  
3 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 45 ▶use base 'PDF::Builder::Basic::PDF::Filter::FlateDecode';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 45 our $VERSION = '3.028'; # VERSION
10 45 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
11  
12 =head1 NAME
13  
14 PDF::Builder::Basic::PDF::Filter::LZWDecode - Compress and uncompress stream filters for Lempel-Ziv-Welch
15  
16 Inherits from L<PDF::Builder::Basic::PDF::Filter::FlateDecode>
17  
18 =cut
19  
20 # extensively extended from PDF::API2 version, for TIFF support
21  
22 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 10 my ($class, $decode_parms) = @_;
24  
25 10 my $self = { 'DecodeParms' => $decode_parms };
26  
27 10 bless $self, $class;
28 10 $self->_reset_code();
29 10 return $self;
30 }
31  
32 6 ▶sub infilt {
Subroutine infilt: called CC=15 SCAR=28.4
Pod documented
33 6 my ($self, $data, $is_last) = @_;
34  
35 6 my ($code, $result);
36 6 my $partial_code = $self->{'partial_code'};
37 6 my $partial_bits = $self->{'partial_bits'};
38  
39 6 my $early_change = 1;
40 6 ▶ if ($self->{'DecodeParms'} and $self->{'DecodeParms'}->{'EarlyChange'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DecodeParms"} and $self->{"DecodeParms"}{"EarlyChange"}) 0 6 6
Condition66%2/3
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"EarlyChange"}
!ll&&!rl&&r
420
MC/DC0%0/2
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"EarlyChange"}
$self->{"DecodeParms"} $self->{"DecodeParms"}{"EarlyChange"}
Truth table66%2/3
A and B
A= $self->{"DecodeParms"}B= $self->{"DecodeParms"}{"EarlyChange"}
ABresult
0X0
100
111
41 0 $early_change = $self->{'DecodeParms'}->{'EarlyChange'}->val();
42 }
43 6 $self->{'table'} = [ map { chr } 0 .. $self->{'clear_table'} - 1 ];
44  
45 6 ▶ while ($data ne q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 29547 0 29547
46 ($code, $partial_code, $partial_bits) =
47 $self->read_dat(\$data, $partial_code, $partial_bits,
48 29547 $self->{'code_length'});
49 29547 ▶ last unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 29547 29547
50  
51 29547 ▶ unless ($early_change) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($early_change) 0 29547 29547
52 0 ▶ if ($self->{'next_code'} == (1 << $self->{'code_length'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12) 0 0 0
Condition0%0/3
$self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12
$self->{"next_code"} == 1 << $self->{"code_length"} $self->{"code_length"} < 12
Truth table0%0/3
A and B
A= $self->{"next_code"} == 1 << $self->{"code_length"}B= $self->{"code_length"} < 12
ABresult
0X0
100
111
53 and $self->{'code_length'} < 12) {
54 0 $self->{'code_length'}++;
55 }
56 }
57  
58 29547 ▶ if ($code == $self->{'clear_table'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code == $self->{'clear_table'}) { } 13 29534 29547
elsif ($code == $self->{'eod_marker'}) { } 6 29528 29534
elsif ($code > $self->{'eod_marker'}) { } 1060 28468 29528
59 13 $self->{'code_length'} = $self->{'initial_code_length'};
60 13 $self->{'next_code'} = $self->{'eod_marker'} + 1;
61 13 next;
62 } elsif ($code == $self->{'eod_marker'}) {
63 6 last;
64 } elsif ($code > $self->{'eod_marker'}) {
65 1060 $self->{'table'}[$self->{'next_code'}] = $self->{'table'}[$code];
66 $self->{'table'}[$self->{'next_code'}] .=
67 1060 substr($self->{'table'}[$code + 1], 0, 1);
68 1060 $result .= $self->{'table'}[$self->{'next_code'}];
69 1060 $self->{'next_code'}++;
70 } else {
71 28468 $self->{'table'}[$self->{'next_code'}] = $self->{'table'}[$code];
72 28468 $result .= $self->{'table'}[$self->{'next_code'}];
73 28468 $self->{'next_code'}++;
74 }
75  
76 29528 ▶ if ($early_change) {
Branch: 1 branch
BranchTrueFalseTotal
if ($early_change) 29528 0 29528
77 29528 ▶ if ($self->{'next_code'} == (1 << $self->{'code_length'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12) 25 29503 29528
Condition100%3/3
$self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12
!ll&&!rl&&r
29496725
MC/DC100%2/2
$self->{"next_code"} == 1 << $self->{"code_length"} and $self->{"code_length"} < 12
$self->{"next_code"} == 1 << $self->{"code_length"} $self->{"code_length"} < 12
Truth table100%3/3
A and B
A= $self->{"next_code"} == 1 << $self->{"code_length"}B= $self->{"code_length"} < 12
ABresult
0X0
100
111
78 and $self->{'code_length'} < 12) {
79 25 $self->{'code_length'}++;
80 }
81 }
82 }
83 6 $self->{'partial_code'} = $partial_code;
84 6 $self->{'partial_bits'} = $partial_bits;
85  
86 6 ▶ if ($self->_predictor_type() == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_predictor_type == 2) 2 4 6
87 2 return $self->_depredict($result);
88 }
89  
90 4 return $result;
91 }
92  
93 6 ▶sub outfilt {
Subroutine outfilt: called CC=6 SCAR=17.9
Pod documented
94 6 my ($self, $str, $is_end) = @_;
95 6 my $max_code = 32767;
96 6 my $bytes_in = 0;
97 6 my $checkpoint = 0;
98 6 my $last_ratio = 0;
99 6 my $seen = q{};
100 6 $self->{'buf'} = q{};
101 6 $self->{'buf_pos'} = 0;
102 6 $self->_write_code($self->{'clear_table'});
103
104 6 ▶ if ($self->_predictor_type() == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_predictor_type == 2) 2 4 6
105 2 $str = $self->_predict($str);
106 }
107
108 6 for my $i (0 .. length($str)) {
109 31085 my $char = substr($str, $i, 1);
110 31085 $bytes_in += 1;
111
112 31085 ▶ if (exists $self->{'table'}{ $seen . $char }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"table"}{$seen . $char}) 1563 29522 31085
113 1563 $seen .= $char;
114 1563 next;
115 }
116
117 29522 $self->_write_code($self->{'table'}{$seen});
118
119 29522 $self->_new_code($seen . $char);
120
121 29522 $seen = $char;
122
123 29522 ▶ if ($self->{'at_max_code'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"at_max_code"}) 7 29515 29522
124 7 $self->_write_code($self->{'clear_table'});
125 7 $self->_reset_code();
126
127 7 undef $checkpoint;
128 7 undef $last_ratio;
129 }
130 }
131 6 $self->_write_code($self->{'table'}{$seen}); #last bit of input
132 6 $self->_write_code($self->{'eod_marker'});
133 6 my $padding = length($self->{'buf'}) % 8;
134 6 ▶ if ($padding > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($padding > 0) 5 1 6
135 5 $padding = 8 - $padding;
136 5 $self->{'buf'} .= '0' x $padding;
137 }
138 6 return pack 'B*', $self->{'buf'};
139 }
140
141 17 ▶sub _reset_code {
Subroutine _reset_code: called CC=1 SCAR=0.0
142 17 my $self = shift;
143
144 17 $self->{'initial_code_length'} = 9;
145 17 $self->{'max_code_length'} = 12;
146 17 $self->{'code_length'} = $self->{'initial_code_length'};
147 17 $self->{'clear_table'} = 256;
148 17 $self->{'eod_marker'} = $self->{'clear_table'} + 1;
149 17 $self->{'next_code'} = $self->{'eod_marker'} + 1;
150 17 $self->{'next_increase'} = 2**$self->{'code_length'};
151 17 $self->{'at_max_code'} = 0;
152 17 $self->{'table'} = { map { chr $_ => $_ } 0 .. $self->{'clear_table'} - 1 };
153 17 return;
154 }
155
156 29522 ▶sub _new_code {
Subroutine _new_code: called CC=4 SCAR=13.9
157 29522 my ($self, $word) = @_;
158
159 29522 ▶ if ($self->{'at_max_code'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"at_max_code"} == 0) 29522 0 29522
160 29522 $self->{'table'}{$word} = $self->{'next_code'};
161 29522 $self->{'next_code'} += 1;
162 }
163
164 29522 ▶ if ($self->{'next_code'} >= $self->{'next_increase'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"next_code"} >= $self->{"next_increase"}) 32 29490 29522
165 32 ▶ if ($self->{'code_length'} < $self->{'max_code_length'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'code_length'} < $self->{'max_code_length'}) { } 25 7 32
166 25 $self->{'code_length'} += 1;
167 25 $self->{'next_increase'} *= 2;
168 } else {
169 7 $self->{'at_max_code'} = 1;
170 }
171 }
172 29522 return;
173 }
174
175 29547 ▶sub _write_code {
Subroutine _write_code: called CC=5 SCAR=16.7
176 29547 my ($self, $code) = @_;
177
178 0 ▶ if (not defined $code) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 0 29547 29547
179
180 29547 ▶ if ($code > (2**$self->{'code_length'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code > 2 ** $self->{"code_length"}) 0 29547 29547
181 0 croak
182 "Code $code too large for current code length $self->{'code_length'}";
183 }
184
185 29547 for my $bit (reverse 0 .. ($self->{'code_length'} - 1)) {
186 331015 ▶ if (($code >> $bit) & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >> $bit & 1) { } 119491 211524 331015
187 119491 $self->{'buf'} .= '1';
188 } else {
189 211524 $self->{'buf'} .= '0';
190 }
191 }
192
193 29547 $self->{'buf_pos'} += $self->{'code_length'};
194 29547 return;
195 }
196
197 29547 ▶sub read_dat {
Subroutine read_dat: called CC=5 SCAR=16.1
Pod undocumented
198 29547 my ($self, $data_ref, $partial_code, $partial_bits, $code_length) = @_;
199  
200 29547 ▶ if (not defined $partial_bits) { $partial_bits = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $partial_bits) 6 29541 29547
201 29547 ▶ if (not defined $partial_code) { $partial_code = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $partial_code) 6 29541 29547
202  
203 29547 ▶ while ($partial_bits < $code_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($partial_bits < $code_length) 41380 29547 70927
204 41380 ▶ return (undef, $partial_code, $partial_bits) unless length($$data_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless length $$data_ref 0 41380 41380
205 41380 $partial_code = ($partial_code << 8) + unpack('C', $$data_ref);
206 41380 substr($$data_ref, 0, 1, q{});
207 41380 $partial_bits += 8;
208 }
209  
210 29547 my $code = $partial_code >> ($partial_bits - $code_length);
211 29547 $partial_code &= (1 << ($partial_bits - $code_length)) - 1;
212 29547 $partial_bits -= $code_length;
213  
214 29547 return ($code, $partial_code, $partial_bits);
215 }
216  
217 12 ▶sub _predictor_type {
Subroutine _predictor_type: called CC=6 SCAR=21.2
218 12 my ($self) = @_;
219 12 ▶ if ($self->{'DecodeParms'} and $self->{'DecodeParms'}->{'Predictor'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DecodeParms"} and $self->{"DecodeParms"}{"Predictor"}) 4 8 12
Condition66%2/3
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"Predictor"}
!ll&&!rl&&r
804
MC/DC50%1/2
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"Predictor"}
$self->{"DecodeParms"} $self->{"DecodeParms"}{"Predictor"}
Truth table66%2/3
A and B
A= $self->{"DecodeParms"}B= $self->{"DecodeParms"}{"Predictor"}
ABresult
0X0
100
111
220 4 my $predictor = $self->{'DecodeParms'}->{'Predictor'}->val();
221 4 ▶ if ($predictor == 1 or $predictor == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($predictor == 1 or $predictor == 2) { } 4 0 4
elsif ($predictor == 3) { } 0 0 0
Condition33%1/3
$predictor == 1 or $predictor == 2
l!l&&r!l&&!r
040
MC/DC0%0/2
$predictor == 1 or $predictor == 2
$predictor == 1 $predictor == 2
Truth table33%1/3
A or B
A= $predictor == 1B= $predictor == 2
ABresult
1X1
011
000
222 4 return $predictor;
223 } elsif ($predictor == 3) {
224 0 croak 'Floating point TIFF predictor not yet supported';
225 } else {
226 0 croak "Invalid predictor: $predictor";
227 }
228 }
229 8 return 1;
230 }
231
232 2 ▶sub _depredict {
Subroutine _depredict: called CC=9 SCAR=22.5
233 2 my ($self, $data) = @_;
234 2 my $param = $self->{'DecodeParms'};
235 2 ▶ my $alpha = $param->{'Alpha'} ? $param->{'Alpha'}->val() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Alpha'} ? : 0 2 2
236 my $bpc =
237 2 ▶ $param->{'BitsPerComponent'} ? $param->{'BitsPerComponent'}->val() : 8;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'BitsPerComponent'} ? : 0 2 2
238 2 ▶ my $colors = $param->{'Colors'} ? $param->{'Colors'}->val() : 1;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Colors'} ? : 1 1 2
239 2 ▶ my $columns = $param->{'Columns'} ? $param->{'Columns'}->val() : 1;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Columns'} ? : 2 0 2
240 2 ▶ my $rows = $param->{'Rows'} ? $param->{'Rows'}->val() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Rows'} ? : 2 0 2
241
242 2 my $comp = $colors + $alpha;
243 2 my $bpp = ceil($bpc * $comp / 8);
244 2 my $max = 256;
245 2 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) 2 0 2
246 2 my @data = unpack('C*', $data);
247 2 for my $j (0 .. $rows - 1) {
248 4 my $count = $bpp * ($j * $columns + 1);
249 4 for my $i ($bpp .. $columns * $bpp - 1) {
250 72 $data[$count] =
251 ($data[$count] + $data[$count - $bpp]) % $max;
252 72 $count++;
253 }
254 }
255 2 $data = pack('C*', @data);
256 2 return $data;
257 }
258 0 return $data;
259 }
260
261 3 ▶sub _predict {
Subroutine _predict: called CC=10 SCAR=23.5
262 3 my ($self, $data) = @_;
263 3 my $param = $self->{'DecodeParms'};
264 3 ▶ my $alpha = $param->{'Alpha'} ? $param->{'Alpha'}->val() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Alpha'} ? : 0 3 3
265 my $bpc =
266 3 ▶ $param->{'BitsPerComponent'} ? $param->{'BitsPerComponent'}->val() : 8;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'BitsPerComponent'} ? : 0 3 3
267 3 ▶ my $colors = $param->{'Colors'} ? $param->{'Colors'}->val() : 1;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Colors'} ? : 1 2 3
268 3 ▶ my $columns = $param->{'Columns'} ? $param->{'Columns'}->val() : 1;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Columns'} ? : 3 0 3
269 3 ▶ my $rows = $param->{'Rows'} ? $param->{'Rows'}->val() : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'Rows'} ? : 3 0 3
270
271 3 my $comp = $colors + $alpha;
272 3 my $bpp = ceil($bpc * $comp / 8);
273 3 my $max = 256;
274 3 ▶ if ($bpc == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc == 8) 3 0 3
275 3 my @data = unpack('C*', $data);
276 3 for my $j (0 .. $rows - 1) {
277 6 my $count = $bpp * $columns * ($j + 1) - 1;
278 6 for my $i ($bpp .. $columns * $bpp - 1) {
279 78 $data[$count] -= $data[$count - $bpp];
280 78 ▶ if ($data[$count] < 0) { $data[$count] += $max; }
Branch: 1 branch
BranchTrueFalseTotal
if ($data[$count] < 0) 7 71 78
281 78 $count--;
282 }
283 }
284 3 $data = pack('C*', @data);
285 3 return $data;
286 }
287 0 return $data;
288 }
289
290 45 1;
« Builder/Basic/PDF/Filter/ASCII85Decode.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/code3of9.pm »