API2/Resource/XObject/Image/GIF.pm

filter: Statementstmt 66.2% 106 / 160 Branchbran 37.9% 22 / 58 Conditioncond 17.3% 4 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 90.9% 10 / 11 Podpod 33.3% 1 / 3 totaltotal 52.9% 143 / 270 SCAR 51.9
CC
39
Cov
55%
new
104.5
unInterlace
30.0
deGIF
10.0
CRAP 179.7
« API2/Annotation.pm ↑ Summary API2/Resource/XObject/Image/TIFF/File.pm »
1 package PDF::API2::Resource::XObject::Image::GIF;
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 our $VERSION = '2.044'; # VERSION
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # GIF89a Specification:
17 # https://www.w3.org/Graphics/GIF/spec-gif89a.txt
18  
19 # Originally from PDF::Create
20 # PDF::Image::GIFImage - GIF image support
21 # Author: Michael Gross <mdgrosse@sbox.tugraz.at>
22 sub unInterlace {
23 0 ▶ my $self = shift;
Subroutine unInterlace: not called CC=5 SCAR=34.0
Pod undocumented
24 0 my $data = $self->{' stream'};
25 0 my $row;
26 my @result;
27 0 my $width = $self->width();
28 0 my $height = $self->height();
29 0 my $idx = 0;
30  
31 # Pass 1 - every 8th row, starting with row 0
32 0 $row = 0;
33 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
34 0 $result[$row] = substr($data, $idx * $width, $width);
35 0 $row += 8;
36 0 $idx++;
37 }
38  
39 # Pass 2 - every 8th row, starting with row 4
40 0 $row = 4;
41 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
42 0 $result[$row] = substr($data, $idx * $width, $width);
43 0 $row += 8;
44 0 $idx++;
45 }
46  
47 # Pass 3 - every 4th row, starting with row 2
48 0 $row = 2;
49 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
50 0 $result[$row] = substr($data, $idx * $width, $width);
51 0 $row += 4;
52 0 $idx++;
53 }
54  
55 # Pass 4 - every 2th row, starting with row 1
56 0 $row = 1;
57 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
58 0 $result[$row] = substr($data, $idx * $width, $width);
59 0 $row += 2;
60 0 $idx++;
61 }
62  
63 0 $self->{' stream'} = join('', @result);
64 }
65  
66 sub deGIF {
67 2 ▶ my ($ibits, $stream) = @_;
Subroutine deGIF: called CC=9 SCAR=23.1
Pod undocumented
68 2 my $bits = $ibits;
69 2 my $resetcode = 1 << ($ibits - 1);
70 2 my $endcode = $resetcode + 1;
71 2 my $nextcode = $endcode + 1;
72 2 my $ptr = 0;
73 2 my $maxptr = 8 * length($stream);
74 2 my $tag;
75 2 my $out = '';
76 2 my $outptr = 0;
77  
78 2 my @d = map { chr($_) } (0 .. ($resetcode - 1));
79  
80 2 ▶ while (($ptr + $bits) <= $maxptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr + $bits <= $maxptr) 6 0 6
81 6 $tag = 0;
82 6 foreach my $off (reverse 0 .. ($bits - 1)) {
83 18 $tag <<= 1;
84 18 $tag |= vec($stream, $ptr + $off, 1);
85 }
86 # foreach my $off (0..$bits-1) {
87 # $tag<<=1;
88 # $tag|=vec($stream,$ptr+$off,1);
89 # }
90 # print STDERR "ptr=$ptr,tag=$tag,bits=$bits,next=$nextcode\n";
91 # print STDERR "tag to large\n" if($tag>$nextcode);
92 6 $ptr += $bits;
93 6 ▶ $bits++ if $nextcode == 1 << $bits and $bits < 12;
Branch: 1 branch
BranchTrueFalseTotal
if $nextcode == 1 << $bits and $bits < 12 0 6 6
Condition33%1/3
$nextcode == 1 << $bits and $bits < 12
!ll&&!rl&&r
600
MC/DC0%0/2
$nextcode == 1 << $bits and $bits < 12
$nextcode == 1 << $bits $bits < 12
Truth table33%1/3
A and B
A= $nextcode == 1 << $bitsB= $bits < 12
ABresult
0X0
100
111
94 6 ▶ if ($tag==$resetcode) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag == $resetcode) { } 2 4 6
elsif ($tag == $endcode) { } 2 2 4
elsif ($tag < $resetcode) { } 2 0 2
elsif ($tag > $endcode) { } 0 0 0
95 2 $bits = $ibits;
96 2 $nextcode = $endcode + 1;
97 2 next;
98 }
99 elsif ($tag == $endcode) {
100 2 last;
101 }
102 elsif ($tag < $resetcode) {
103 2 $d[$nextcode] = $d[$tag];
104 2 $out .= $d[$nextcode];
105 2 $nextcode++;
106 }
107 elsif ($tag > $endcode) {
108 0 $d[$nextcode] = $d[$tag];
109 0 $d[$nextcode] .= substr($d[$tag + 1], 0, 1);
110 0 $out .= $d[$nextcode];
111 0 $nextcode++;
112 }
113 }
114 2 return $out;
115 }
116  
117 sub new {
118 3 ▶ my ($class, $pdf, $file, $name, %opts) = @_;
Subroutine new: called CC=27 SCAR=46.5
Pod documented
119 3 my $self;
120 3 my $interlaced = 0;
121  
122 3 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 3 3
123  
124 3 ▶ $self = $class->SUPER::new($pdf, $name || 'Gx' . pdfkey());
Condition50%1/2
$name || 'Gx' . pdfkey()
l!l
03
MC/DC0%0/1
$name || 'Gx' . pdfkey()
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
125 3 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 3 3
126  
127 3 $self->{' apipdf'} = $pdf;
128 3 weaken $self->{' apipdf'};
129  
130 3 my $fh = IO::File->new();
131 3 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 2 3
132 1 $fh = $file;
133 }
134 else {
135 2 ▶ open $fh, '<', $file or die "$!: $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "$!: $file" 1 1 2
136 }
137 2 binmode $fh, ':raw';
138 2 my $buf;
139 2 $fh->seek(0, 0);
140 2 $fh->read($buf, 6); # signature
141 2 ▶ unless ($buf =~ /^GIF[0-9][0-9][a-z]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /^GIF[0-9][0-9][a-z]/) 0 2 2
142 0 die "Unknown image signature '$buf' -- not a GIF";
143 }
144  
145 2 $fh->read($buf, 7); # logical screen descriptor
146 2 my ($wg, $hg, $flags, $bgColorIndex, $aspect) = unpack('vvCCC', $buf);
147  
148 2 ▶ if ($flags & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 2 0 2
149 2 my $colSize = 2 ** (($flags & 0x7) + 1);
150 2 my $dict = PDFDict();
151 2 $pdf->new_obj($dict);
152 2 $self->colorspace(PDFArray(PDFName('Indexed'),
153 PDFName('DeviceRGB'),
154 PDFNum($colSize-1),
155 $dict));
156 2 $fh->read($dict->{' stream'}, 3 * $colSize); # color table
157 }
158  
159 2 ▶ until ($fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->eof) 2 0 2
160 2 $fh->read($buf, 1); # tag.
161 2 my $sep = unpack('C', $buf);
162 2 ▶ if ($sep == 0x2C) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sep == 44) { } 2 0 2
elsif ($sep == 59) { } 0 0 0
163 2 $fh->read($buf, 9); # image descriptor
164 2 my ($left, $top, $w, $h, $flags) = unpack('vvvvC', $buf);
165  
166 2 ▶ $self->width($w || $wg);
Condition33%1/3
$w || $wg
l!l&&r!l&&!r
200
MC/DC0%0/2
$w || $wg
$w $wg
Truth table33%1/3
A || B
A= $wB= $wg
ABresult
1X1
011
000
167 2 ▶ $self->height($h || $hg);
Condition33%1/3
$h || $hg
l!l&&r!l&&!r
200
MC/DC0%0/2
$h || $hg
$h $hg
Truth table33%1/3
A || B
A= $hB= $hg
ABresult
1X1
011
000
168 2 $self->bpc(8);
169  
170 2 ▶ if ($flags & 0x80) { # local color table
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 0 2 2
171 0 my $colSize = 2 ** (($flags & 0x7) + 1);
172 0 my $dict = PDFDict();
173 0 $pdf->new_obj($dict);
174 0 $self->colorspace(PDFArray(PDFName('Indexed'),
175 PDFName('DeviceRGB'),
176 PDFNum($colSize-1),
177 $dict));
178 0 $fh->read($dict->{' stream'}, 3 * $colSize); # color table
179 }
180 2 ▶ if ($flags & 0x40) { # whether image is interlaced
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 64) 0 2 2
181 0 $interlaced = 1;
182 }
183  
184 2 $fh->read($buf, 1); # image-lzw-start (should be 9).
185 2 my ($sep) = unpack('C', $buf);
186  
187 2 $fh->read($buf, 1); # first chunk.
188 2 my ($len) = unpack('C', $buf);
189 2 my $stream = '';
190 2 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 2 2 4
191 2 $fh->read($buf, $len);
192 2 $stream .= $buf;
193 2 $fh->read($buf, 1);
194 2 $len = unpack('C', $buf);
195 }
196 2 $self->{' stream'} = deGIF($sep + 1, $stream);
197 2 ▶ $self->unInterlace() if $interlaced;
Branch: 1 branch
BranchTrueFalseTotal
if $interlaced 0 2 2
198 2 last;
199 }
200 elsif ($sep == 0x3b) { # trailer
201 0 last;
202 }
203 else { # extension
204 0 $fh->read($buf, 1); # tag
205 0 my $tag = unpack('C', $buf);
206 0 $fh->read($buf, 1); # length
207 0 my $len = unpack('C', $buf);
208 0 my $stream = '';
209 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
210 0 $fh->read($buf, $len);
211 0 $stream .= $buf;
212 0 $fh->read($buf, 1);
213 0 $len = unpack('C', $buf);
214 }
215  
216 # Graphic Control Extension
217 0 ▶ if ($sep == 0x21 and $tag == 0xF9) {
Branch: 4 branches
BranchTrueFalseTotal
if ($sep == 33 and $tag == 249) { } 0 0 0
elsif ($sep == 33 and $tag == 254) { } 0 0 0
elsif ($sep == 33 and $tag == 1) { } 0 0 0
elsif ($sep == 33) { } 0 0 0
Condition0%0/3
$sep == 33 and $tag == 249
!ll&&!rl&&r
000
MC/DC0%0/2
$sep == 33 and $tag == 249
$sep == 33 $tag == 249
Truth table0%0/3
A and B
A= $sep == 33B= $tag == 249
ABresult
0X0
100
111
218 0 my ($cFlags, $delay, $transIndex) = unpack('CvC', $stream);
219 0 ▶ if (($cFlags & 0x01) and not $opts{'-notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cFlags & 1 and not $opts{"-notrans"}) 0 0 0
Condition0%0/3
$cFlags & 1 and not $opts{"-notrans"}
!ll&&!rl&&r
000
MC/DC0%0/2
$cFlags & 1 and not $opts{"-notrans"}
$cFlags & 1 not $opts{"-notrans"}
Truth table0%0/3
A and not(B)
A= $cFlags & 1B= not $opts{"-notrans"}
ABresult
0X0
100
111
220 0 $self->{'Mask'} = PDFArray(PDFNum($transIndex),
221 PDFNum($transIndex));
222 }
223 }
224  
225 # Comment Extension
226 ▶ elsif ($sep == 0x21 and $tag == 0xFE) {
Condition0%0/3
$sep == 33 and $tag == 254
!ll&&!rl&&r
000
MC/DC0%0/2
$sep == 33 and $tag == 254
$sep == 33 $tag == 254
Truth table0%0/3
A and B
A= $sep == 33B= $tag == 254
ABresult
0X0
100
111
227 # NOOP: ignore
228 }
229  
230 # Plain Text Extension
231 ▶ elsif ($sep == 0x21 and $tag == 0x01) {
Condition0%0/3
$sep == 33 and $tag == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$sep == 33 and $tag == 1
$sep == 33 $tag == 1
Truth table0%0/3
A and B
A= $sep == 33B= $tag == 1
ABresult
0X0
100
111
232 # NOOP: ignore
233 }
234  
235 elsif ($sep == 0x21) {
236 0 carp "Ignoring unsupported GIF extension $tag";
237 }
238 }
239 }
240 2 $fh->close();
241  
242 2 $self->filters('FlateDecode');
243  
244 2 return $self;
245 }
246  
247 2 1;
« API2/Annotation.pm ↑ Summary API2/Resource/XObject/Image/TIFF/File.pm »