| 1 | package PDF::API2::Basic::PDF::Filter::FlateDecode; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 43 | ▶ | use base 'PDF::API2::Basic::PDF::Filter'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 43 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 43 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 43 | our $VERSION = '2.044'; # VERSION | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 43 | ▶ | use POSIX qw(ceil floor); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 43 | our $havezlib; | |||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | BEGIN { | ||||||||||||||||||||||||||||||||
| 15 | 43 | ▶ | eval { require Compress::Zlib }; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 43 | $havezlib = !$@; | |||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | sub new { | ||||||||||||||||||||||||||||||||
| 20 | 22 | ▶ | return unless $havezlib; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 22 | my ($class, $decode_parms) = @_; | |||||||||||||||||||||||||||||||
| 22 | 22 | my $self = { DecodeParms => $decode_parms }; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 22 | $self->{'outfilt'} = Compress::Zlib::deflateInit( | |||||||||||||||||||||||||||||||
| 25 | -Level => 9, | ||||||||||||||||||||||||||||||||
| 26 | -Bufsize => 32768, | ||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | 22 | $self->{'infilt'} = Compress::Zlib::inflateInit(); | |||||||||||||||||||||||||||||||
| 29 | 22 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | sub outfilt { | ||||||||||||||||||||||||||||||||
| 33 | 16 | ▶ | my ($self, $str, $is_end) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
outfilt: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 16 | my $result = $self->{'outfilt'}->deflate($str); | |||||||||||||||||||||||||||||||
| 36 | 16 | ▶ | $result .= $self->{'outfilt'}->flush() if $is_end; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 16 | return $result; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | sub infilt { | ||||||||||||||||||||||||||||||||
| 41 | 6 | ▶ | my ($self, $dat, $last) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
infilt: called CC=6 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 42 | 6 | my ($result, $status) = $self->{'infilt'}->inflate("$dat"); | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | if ($self->{'DecodeParms'} and $self->{'DecodeParms'}->{'Predictor'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"Predictor"}
MC/DC0%0/2
$self->{"DecodeParms"} and $self->{"DecodeParms"}{"Predictor"}
$self->{"DecodeParms"}
$self->{"DecodeParms"}{"Predictor"}
Truth table33%1/3
A and B
A= $self->{"DecodeParms"}B= $self->{"DecodeParms"}{"Predictor"}
| |||||||||||||||||||||||||||||||||
| 45 | 0 | my $predictor = $self->{'DecodeParms'}->{'Predictor'}->val(); | |||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ($predictor == 2) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 47 | 0 | die "The TIFF predictor logic has not been implemented"; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | ▶ | elsif ($predictor >= 10 and $predictor <= 15) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$predictor >= 10 and $predictor <= 15
MC/DC0%0/2
$predictor >= 10 and $predictor <= 15
$predictor >= 10
$predictor <= 15
Truth table0%0/3
A and B
A= $predictor >= 10B= $predictor <= 15
| |||||||||||||||||||||||||||||||||
| 50 | 0 | $result = $self->_depredict_png($result); | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||||||
| 53 | 0 | die "Invalid predictor: $predictor"; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 6 | return $result; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | sub _depredict_png { | ||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ($self, $stream) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_depredict_png: not called CC=20 SCAR=60.4
| |||||||||||||||||||||||||||||||||
| 62 | 0 | my $param = $self->{'DecodeParms'}; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | my $prev = ''; | |||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $stream = $self->{'_depredict_next'} . $stream if defined $self->{'_depredict_next'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $prev = $self->{'_depredict_prev'} if defined $self->{'_depredict_prev'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $alpha = $param->{'Alpha'} ? $param->{'Alpha'}->val() : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $bpc = $param->{'BitsPerComponent'} ? $param->{'BitsPerComponent'}->val() : 8; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my $colors = $param->{'Colors'} ? $param->{'Colors'}->val() : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my $columns = $param->{'Columns'} ? $param->{'Columns'}->val() : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my $height = $param->{'Height'} ? $param->{'Height'}->val() : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 0 | my $comp = $colors + $alpha; | |||||||||||||||||||||||||||||||
| 75 | 0 | my $bpp = ceil($bpc * $comp / 8); | |||||||||||||||||||||||||||||||
| 76 | 0 | my $scanline = 1 + ceil($bpp * $columns); | |||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | 0 | my $clearstream = ''; | |||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $lastrow = ($height || int(length($stream) / $scanline)) - 1; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$height || int length($stream) / $scanline
MC/DC0%0/2
$height || int length($stream) / $scanline
$height
int length($stream) / $scanline
Truth table0%0/3
A || B
A= $heightB= int length($stream) / $scanline
| |||||||||||||||||||||||||||||||||
| 80 | 0 | foreach my $n (0 .. $lastrow) { | |||||||||||||||||||||||||||||||
| 81 | 0 | my $line = substr($stream, $n * $scanline, $scanline); | |||||||||||||||||||||||||||||||
| 82 | 0 | my $filter = vec($line, 0, 8); | |||||||||||||||||||||||||||||||
| 83 | 0 | my $clear = ''; | |||||||||||||||||||||||||||||||
| 84 | 0 | $line = substr($line, 1); | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($filter == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||
| 86 | 0 | $clear = $line; | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | elsif ($filter == 1) { | ||||||||||||||||||||||||||||||||
| 89 | 0 | foreach my $x (0 .. length($line) - 1) { | |||||||||||||||||||||||||||||||
| 90 | 0 | vec($clear, $x, 8) = (vec($line, $x, 8) + vec($clear, $x - $bpp, 8)) % 256; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | elsif ($filter == 2) { | ||||||||||||||||||||||||||||||||
| 94 | 0 | foreach my $x (0 .. length($line) - 1) { | |||||||||||||||||||||||||||||||
| 95 | 0 | vec($clear, $x, 8) = (vec($line, $x, 8) + vec($prev, $x, 8)) % 256; | |||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | elsif ($filter == 3) { | ||||||||||||||||||||||||||||||||
| 99 | 0 | foreach my $x (0 .. length($line) - 1) { | |||||||||||||||||||||||||||||||
| 100 | 0 | vec($clear, $x, 8) = (vec($line, $x, 8) + floor((vec($clear, $x - $bpp, 8) + vec($prev, $x, 8)) / 2)) % 256; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | elsif ($filter == 4) { | ||||||||||||||||||||||||||||||||
| 104 | 0 | foreach my $x (0 .. length($line) - 1) { | |||||||||||||||||||||||||||||||
| 105 | 0 | vec($clear, $x, 8) = (vec($line, $x, 8) + _paeth_predictor(vec($clear, $x - $bpp, 8), vec($prev, $x, 8), vec($prev, $x - $bpp, 8))) % 256; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||||||
| 109 | 0 | die "Unexpected depredictor algorithm $filter requested on line $n (valid options are 0-4)"; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | 0 | $prev = $clear; | |||||||||||||||||||||||||||||||
| 112 | 0 | foreach my $x (0 .. ($columns * $comp) - 1) { | |||||||||||||||||||||||||||||||
| 113 | 0 | vec($clearstream, ($n * $columns * $comp) + $x, $bpc) = vec($clear, $x, $bpc); | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | 0 | $self->{'_depredict_next'} = substr($stream, ($lastrow + 1) * $scanline); | |||||||||||||||||||||||||||||||
| 117 | 0 | $self->{'_depredict_prev'} = $prev; | |||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | return $clearstream; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | sub _paeth_predictor { | ||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my ($a, $b, $c) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_paeth_predictor: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 124 | 0 | my $p = $a + $b - $c; | |||||||||||||||||||||||||||||||
| 125 | 0 | my $pa = abs($p - $a); | |||||||||||||||||||||||||||||||
| 126 | 0 | my $pb = abs($p - $b); | |||||||||||||||||||||||||||||||
| 127 | 0 | my $pc = abs($p - $c); | |||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | if ($pa <= $pb && $pa <= $pc) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$pa <= $pb and $pa <= $pc
MC/DC0%0/2
$pa <= $pb and $pa <= $pc
$pa <= $pb
$pa <= $pc
Truth table0%0/3
A and B
A= $pa <= $pbB= $pa <= $pc
| |||||||||||||||||||||||||||||||||
| 129 | 0 | return $a; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | elsif ($pb <= $pc) { | ||||||||||||||||||||||||||||||||
| 132 | 0 | return $b; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||
| 135 | 0 | return $c; | |||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 43 | 1; | |||||||||||||||||||||||||||||||