Builder/Basic/PDF/Filter/RunLengthDecode.pm

filter: Statementstmt 79.2% 42 / 53 Branchbran 73.0% 19 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 75.8% 69 / 91 CC 15 SCAR 21.5
CC
15
Cov
76%
infilt
11.8
outfilt
7.6
BEGIN
1.0
CRAP 8.5
« Builder/Resource/XObject/Form/BarCode/code3of9.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/ean13.pm »
1 package PDF::Builder::Basic::PDF::Filter::RunLengthDecode;
2  
3 45 ▶use base 'PDF::Builder::Basic::PDF::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 45 our $VERSION = '3.028'; # VERSION
9 45 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10  
11 =head1 NAME
12  
13 PDF::Builder::Basic::PDF::Filter::RunLengthDecode - Compress and uncompress stream filters for Run-Length
14  
15 Inherits from L<PDF::Builder::Basic::PDF::Filter>
16  
17 =cut
18  
19 # Maintainer's Note: RunLengthDecode is described in the PDF 1.7 spec
20 # in section 7.4.5.
21  
22 2 ▶sub outfilt {
Subroutine outfilt: called CC=7 SCAR=20.3
Pod documented
23 2 my ($self, $input, $include_eod) = @_;
24  
25 2 my $output;
26  
27 2 ▶ while ($input ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($input ne "") 6 2 8
28 6 my ($unrepeated, $repeated);
29  
30 # Look for a repeated character (which can be repeated up to
31 # 127 times)
32 6 ▶ if ($input =~ m/^(.*?)((.)\3{1,127})(.*)$/so) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^(.*?)((.)\3{1,127})(.*)$/so) { } 6 0 6
33 6 $unrepeated = $1;
34 6 $repeated = $2;
35 6 $input = $4;
36 } else {
37 0 $unrepeated = $input;
38 0 $input = '';
39 }
40  
41 # Print any non-repeating bytes at the beginning of the input
42 # in chunks of up to 128 bytes, prefixed with a run-length (0
43 # to 127, signifying 1 to 128 bytes)
44 6 ▶ while (length($unrepeated) > 127) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $unrepeated > 127) 0 6 6
45 0 $output .= pack('C', 127) . substr($unrepeated, 0, 128);
46 0 substr($unrepeated, 0, 128) = '';
47 }
48 6 ▶ $output .= pack('C', length($unrepeated) - 1) . $unrepeated if length($unrepeated) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $unrepeated > 0 4 2 6
49  
50 # Then print the number of times the repeated byte was
51 # repeated (using the formula "257 - length" to give a result
52 # in the 129-255 range) followed by the byte to be repeated
53 6 ▶ if (length($repeated)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $repeated) 6 0 6
54 6 $output .= pack('C', 257 - length($repeated)) . substr($repeated, 0, 1);
55 }
56 }
57  
58 # A byte value of 128 signifies that we're done.
59 2 ▶ $output .= "\x80" if $include_eod;
Branch: 1 branch
BranchTrueFalseTotal
if $include_eod 1 1 2
60  
61 2 return $output;
62 }
63  
64 2 ▶sub infilt {
Subroutine infilt: called CC=9 SCAR=24.7
Pod documented
65 2 my ($self, $input, $is_terminated) = @_;
66  
67 2 my ($output, $length);
68  
69 # infilt may be called multiple times, and is expected to continue
70 # where it left off
71 2 ▶ if (exists $self->{'incache'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"incache"}) 0 2 2
72 0 $input = $self->{'incache'} . $input;
73 0 delete $self->{'incache'};
74 }
75  
76 2 ▶ while (length($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input) 11 1 12
77 # Read a length byte
78 11 $length = unpack("C", $input);
79  
80 # A "length" of 128 represents the end of the document
81 11 ▶ if ($length == 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 128) 1 10 11
82 1 return $output;
83 }
84  
85 # Any other length needs to be followed by at least one other byte
86 10 ▶ if (length($input) == 1 and not $is_terminated) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input == 1 and not $is_terminated) 0 10 10
Condition33%1/3
length $input == 1 and not $is_terminated
!ll&&!rl&&r
1000
MC/DC0%0/2
length $input == 1 and not $is_terminated
length $input == 1 not $is_terminated
Truth table33%1/3
A and not(B)
A= length $input == 1B= not $is_terminated
ABresult
0X0
100
111
87 0 die "Premature end to RunLengthEncoded data";
88 }
89  
90 # A length of 129-255 represents a repeated string
91 # (number of repeats = 257 - length)
92 10 ▶ if ($length > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 128) { } 6 4 10
93 6 ▶ if (length($input) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input == 1) 0 6 6
94 # Out of data. Defer until the next call.
95 0 $self->{'incache'} = $input;
96 0 return $output;
97 }
98 6 $output .= substr($input, 1, 1) x (257 - $length);
99 6 substr($input, 0, 2) = '';
100 }
101  
102 # Any other length (under 128) represents a non-repeated
103 # stream of bytes (with a length of 0 to 127 representing 1 to
104 # 128 bytes)
105 else {
106 4 ▶ if (length($input) < $length + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input < $length + 2) 0 4 4
107 # Insufficient data. Defer until the next call.
108 0 $self->{'incache'} = $input;
109 0 return $output;
110 }
111 4 $output .= substr($input, 1, $length + 1);
112 4 substr($input, 0, $length + 2) = '';
113 }
114 }
115  
116 1 return $output;
117 }
118  
119 45 1;
« Builder/Resource/XObject/Form/BarCode/code3of9.pm ↑ Summary Builder/Resource/XObject/Form/BarCode/ean13.pm »