Builder/Resource/XObject/Image/GIF.pm

filter: Statementstmt 53.1% 111 / 209 Branchbran 35.0% 28 / 80 Conditioncond 30.7% 8 / 26 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 90.0% 9 / 10 Podpod 33.3% 1 / 3 totaltotal 45.5% 157 / 345 CC 51 SCAR 54.2
CC
51
Cov
47%
new
341.5
unInterlace
30.0
deGIF
10.0
CRAP 226.1
« Builder/Resource/XObject/Image/PNG.pm ↑ Summary Builder/Basic/PDF/Filter/FlateDecode.pm »
1 package PDF::Builder::Resource::XObject::Image::GIF;
2  
3 2 ▶use base 'PDF::Builder::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 = '3.028'; # VERSION
9 2 our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10  
11 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use PDF::Builder::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 =head1 NAME
17  
18 PDF::Builder::Resource::XObject::Image::GIF - Support routines for GIF image library
19  
20 Inherits from L<PDF::Builder::Resource::XObject::Image>
21  
22 =head2 History
23  
24 GIF89a Specification: https://www.w3.org/Graphics/GIF/spec-gif89a.txt
25  
26 A fairly thorough description of the GIF format may be found in
27 L<http://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html>.
28  
29 Code originally from PDF::Create, PDF::Image::GIFImage - GIF image support
30 Author: Michael Gross <mdgrosse@sboxtugraz.at>
31  
32 =head1 Supported Formats
33  
34 GIF87a and GIF89a headers are supported. The Image block (x2C) is supported.
35  
36 The Graphic Control Extension block (x21 + xF9) is supported for transparency
37 control. Animation is not supported.
38  
39 The Comment Extension block (x21 + xFE), Plain Text Extension block (x21 + x01),
40 and Application Extension block (x21 + xFF) are read, but ignored. Any other
41 block or Extension block will be flagged as an error.
42  
43 If given, Local Color Tables are read and used, supposedly permitting more
44 than 256 colors to be used overall in the image (despite the 8 bit color table
45 depth).
46  
47 =head2 Options
48  
49 =over
50  
51 =item notrans
52  
53 When defined and not 0, C<notrans> suppresses the use of transparency if such
54 is defined in the GIF file.
55  
56 =item name => 'string'
57  
58 This is the name you can give for the GIF image object. The default is Gxnnnn.
59  
60 =item multi
61  
62 When defined and not 0, C<multi> continues processing past the end of the
63 first Image Block. The old behavior, which is now the default, is to stop
64 processing at the end of the first Image Block.
65  
66 =back
67  
68 =cut
69  
70 # modified for internal use. (c) 2004 fredo.
71 0 ▶sub unInterlace {
Subroutine unInterlace: not called CC=5 SCAR=34.0
Pod undocumented
72 0 my $self = shift;
73  
74 0 my $data = $self->{' stream'};
75 0 my $row;
76 my @result;
77 0 my $width = $self->width();
78 0 my $height = $self->height();
79 0 my $idx = 0;
80  
81 # Pass 1 - every 8th row, starting with row 0
82 0 $row = 0;
83 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
84 0 $result[$row] = substr($data, $idx*$width, $width);
85 0 $row += 8;
86 0 $idx++;
87 }
88  
89 # Pass 2 - every 8th row, starting with row 4
90 0 $row = 4;
91 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
92 0 $result[$row] = substr($data, $idx*$width, $width);
93 0 $row += 8;
94 0 $idx++;
95 }
96  
97 # Pass 3 - every 4th row, starting with row 2
98 0 $row = 2;
99 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
100 0 $result[$row] = substr($data, $idx*$width, $width);
101 0 $row += 4;
102 0 $idx++;
103 }
104  
105 # Pass 4 - every 2nd row, starting with row 1
106 0 $row = 1;
107 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
108 0 $result[$row] = substr($data, $idx*$width, $width);
109 0 $row += 2;
110 0 $idx++;
111 }
112  
113 0 return $self->{' stream'} = join('', @result);
114 }
115  
116 2 ▶sub deGIF {
Subroutine deGIF: called CC=9 SCAR=23.1
Pod undocumented
117 2 my ($ibits, $stream) = @_;
118  
119 2 my $bits = $ibits;
120 2 my $resetcode = 1 << ($ibits-1);
121 2 my $endcode = $resetcode+1;
122 2 my $nextcode = $endcode+1;
123 2 my $ptr = 0;
124 2 my $maxptr = 8*length($stream);
125 2 my $tag;
126 2 my $out = '';
127 2 my $outptr = 0;
128  
129 # print STDERR "reset=$resetcode\nend=$endcode\nmax=$maxptr\n";
130  
131 2 my @d = map { chr($_) } (0 .. $resetcode-1);
132  
133 2 ▶ while ($ptr+$bits <= $maxptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr + $bits <= $maxptr) 6 0 6
134 6 $tag = 0;
135 6 foreach my $off (reverse 0 .. $bits-1) {
136 18 $tag <<= 1;
137 18 $tag |= vec($stream, $ptr+$off, 1);
138 }
139 # foreach my $off (0 .. $bits-1) {
140 # $tag <<= 1;
141 # $tag |= vec($stream, $ptr+$off, 1);
142 # }
143 # print STDERR "ptr=$ptr,tag=$tag,bits=$bits,next=$nextcode\n";
144 # print STDERR "tag too large\n" if($tag>$nextcode);
145 6 $ptr += $bits;
146 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
147 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
148 2 $bits = $ibits;
149 2 $nextcode = $endcode+1;
150 2 next;
151 } elsif ($tag == $endcode) {
152 2 last;
153 } elsif ($tag < $resetcode) {
154 2 $d[$nextcode] = $d[$tag];
155 2 $out .= $d[$nextcode];
156 2 $nextcode++;
157 } elsif ($tag > $endcode) {
158 0 $d[$nextcode] = $d[$tag];
159 0 $d[$nextcode] .= substr($d[$tag+1], 0, 1);
160 0 $out .= $d[$nextcode];
161 0 $nextcode++;
162 }
163 }
164 2 return $out;
165 }
166  
167 =head1 METHODS
168  
169 =head2 new
170  
171 PDF::Builder::Resource::XObject::Image::GIF->new()
172  
173 =over
174  
175 Create an image object from a GIF input file.
176 Remember that this should be invoked via the Builder.pm method!
177  
178 =back
179  
180 =cut
181  
182 3 ▶sub new {
Subroutine new: called CC=39 SCAR=58.3
Pod documented
183 3 my ($class, $pdf, $file, %opts) = @_;
184 # copy dashed option names to preferred undashed names
185 0 ▶ if (defined $opts{'-notrans'} && !defined $opts{'notrans'}) { $opts{'notrans'} = delete($opts{'-notrans'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-notrans"} and not defined $opts{"notrans"}) 0 3 3
Condition33%1/3
defined $opts{"-notrans"} and not defined $opts{"notrans"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-notrans"} and not defined $opts{"notrans"}
defined $opts{"-notrans"} not defined $opts{"notrans"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-notrans"}B= not defined $opts{"notrans"}
ABresult
0X0
100
111
186 0 ▶ if (defined $opts{'-name'} && !defined $opts{'name'}) { $opts{'name'} = delete($opts{'-name'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-name"} and not defined $opts{"name"}) 0 3 3
Condition33%1/3
defined $opts{"-name"} and not defined $opts{"name"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-name"} and not defined $opts{"name"}
defined $opts{"-name"} not defined $opts{"name"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-name"}B= not defined $opts{"name"}
ABresult
0X0
100
111
187 0 ▶ if (defined $opts{'-multi'} && !defined $opts{'multi'}) { $opts{'multi'} = delete($opts{'-multi'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-multi"} and not defined $opts{"multi"}) 0 3 3
Condition33%1/3
defined $opts{"-multi"} and not defined $opts{"multi"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-multi"} and not defined $opts{"multi"}
defined $opts{"-multi"} not defined $opts{"multi"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-multi"}B= not defined $opts{"multi"}
ABresult
0X0
100
111
188 0 ▶ if (defined $opts{'-compress'} && !defined $opts{'compress'}) { $opts{'compress'} = delete($opts{'-compress'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-compress"} and not defined $opts{"compress"}) 0 3 3
Condition33%1/3
defined $opts{"-compress"} and not defined $opts{"compress"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-compress"} and not defined $opts{"compress"}
defined $opts{"-compress"} not defined $opts{"compress"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-compress"}B= not defined $opts{"compress"}
ABresult
0X0
100
111
189  
190 3 my ($name, $compress);
191 0 ▶ if (exists $opts{'name'}) { $name = $opts{'name'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"name"}) 0 3 3
192 #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; }
193  
194 3 my $self;
195  
196 3 my $interlaced = 0;
197  
198 3 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 3 3
199  
200 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
201 3 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 3 3
202  
203 3 $self->{' apipdf'} = $pdf;
204 3 weaken $self->{' apipdf'};
205  
206 3 my $fh = IO::File->new();
207 3 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 2 3
208 1 $fh = $file;
209 } else {
210 2 ▶ open $fh, "<", $file or die "$!: $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "$!: $file" 1 1 2
211 }
212 2 binmode $fh, ':raw';
213 2 my $buf;
214 2 $fh->seek(0, 0);
215  
216 # start reading in the GIF file
217 # GIF Header
218 # 6 bytes "GIF87a" or "GIF89a"
219 2 $fh->read($buf, 6); # signature
220 2 ▶ unless ($buf =~ /^GIF[0-9][0-9][a-b]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /^GIF[0-9][0-9][a-b]/) 0 2 2
221 # TBD b? is anything other than 87a and 89a valid?
222 # PDF::API2 allows a-z, not just a-b
223 0 die "unknown image signature '$buf' -- not a GIF."
224 }
225  
226 # 4 bytes logical screen width and height (2 x 16 bit LSB first)
227 # 1 byte flags, 1 byte background color index, 1 byte pixel aspect ratio
228 2 $fh->read($buf, 7); # logical screen descriptor
229 2 my($wg, $hg, $flags, $bgColorIndex, $aspect) = unpack('vvCCC', $buf);
230  
231 # flags numbered left to right 0-7:
232 # bit 0 = 1 (x80) Global Color Table Flag (GCTF)
233 # bits 1-3 = color resolution
234 # bit 4 = 1 (x08) sort flag for Global Color Table
235 # bits 5-7 = size of Global Color Table 2**(n+1), $colSize
236 2 ▶ if ($flags & 0x80) { # GCTF is set?
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 2 0 2
237 2 my $colSize = 2**(($flags & 0x7)+1); # 2 - 256 entries
238 2 my $dict = PDFDict();
239 2 $pdf->new_obj($dict);
240 2 $self->colorspace(PDFArray(PDFName('Indexed'),
241 PDFName('DeviceRGB'),
242 PDFNum($colSize-1),
243 $dict));
244 2 $fh->read($dict->{' stream'}, 3*$colSize); # Global Color Table
245 }
246  
247 # further content in file is blocks and trailer
248 2 ▶ while (!$fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->eof) 2 0 2
249 2 $fh->read($buf, 1); # 1 byte block tag (type)
250 2 my $sep = unpack('C', $buf);
251  
252 2 ▶ if ($sep == 0x2C) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sep == 44) { } 2 0 2
elsif ($sep == 59) { } 0 0 0
elsif ($sep == 33) { } 0 0 0
253 # x2C = image block (separator, equals ASCII comma ',')
254 2 $fh->read($buf, 9); # image descriptor
255 # image left (16 bits), image top (16 bits LSB first)
256 # image width (16 bits), image height (16 bits LSB first)
257 # flags (1 byte):
258 # bit 0 = 1 (x80) Local Color Table Flag (LCTF)
259 # bit 1 = 1 (x40) interlaced
260 # bit 2 = 1 (x20) sort flag
261 # bits 3-4 = reserved
262 # bits 5-7 = size of Local Color Table 2**(n+1) if LCTF=1
263 2 my ($left, $top, $w,$h, $flags) = unpack('vvvvC', $buf);
264  
265 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
266 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
267 2 $self->bits_per_component(8);
268  
269 2 ▶ if ($flags & 0x80) { # Local Color Table (LCTF = 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 0 2 2
270 0 my $colSize = 2**(($flags & 0x7)+1);
271 0 my $dict = PDFDict();
272 0 $pdf->new_obj($dict);
273 0 $self->colorspace(PDFArray(PDFName('Indexed'),
274 PDFName('DeviceRGB'),
275 PDFNum($colSize-1),
276 $dict));
277 0 $fh->read($dict->{' stream'}, 3*$colSize); # Local Color Table
278 }
279 2 ▶ if ($flags & 0x40) { # need de-interlace
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 64) 0 2 2
280 0 $interlaced = 1; # default to 0 earlier
281 }
282  
283 # LZW Minimum Code Size
284 2 $fh->read($buf, 1); # image-lzw-start (should be 9).
285 2 my ($sep) = unpack('C', $buf);
286  
287 # read one or more blocks. first byte is length.
288 # if 0, done (Block Terminator)
289 2 $fh->read($buf, 1); # first chunk.
290 2 my ($len) = unpack('C', $buf);
291 2 my $stream = '';
292 2 ▶ while ($len > 0) { # loop through blocks as long as non-0 length
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 2 2 4
293 2 $fh->read($buf, $len);
294 2 $stream .= $buf;
295 2 $fh->read($buf, 1);
296 2 $len = unpack('C', $buf);
297 }
298 2 $self->{' stream'} = deGIF($sep+1, $stream);
299 2 ▶ $self->unInterlace() if $interlaced;
Branch: 1 branch
BranchTrueFalseTotal
if $interlaced 0 2 2
300 # old (and current default) behavior is to quit processing at the
301 # end of the first Image Block. This means that any other blocks,
302 # including the Trailer, will not be processed.
303 2 ▶ if (!$opts{'multi'}) { last; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"multi"}) 2 0 2
304  
305 } elsif ($sep == 0x3b) {
306 # trailer (EOF) equals ASCII semicolon (;)
307 0 last;
308  
309 } elsif ($sep == 0x21) {
310 # Extension block (x21 + subtag) = ASCII '!'
311 0 $fh->read($buf, 1); # tag.
312 0 my $tag = unpack('C', $buf);
313  
314 0 ▶ if ($tag == 0xF9) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag == 249) { } 0 0 0
elsif ($tag == 254) { } 0 0 0
elsif ($tag == 1) { } 0 0 0
elsif ($tag == 255) { } 0 0 0
315 # xF9 graphic control extension block
316 0 $fh->read($buf, 1); # len. should be 04
317 0 my $len = unpack('C', $buf);
318 0 my $stream = '';
319 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
320 0 $fh->read($buf, $len);
321 0 $stream .= $buf;
322 0 $fh->read($buf, 1);
323 0 $len = unpack('C', $buf);
324 }
325 0 my ($cFlags, $delay, $transIndex) = unpack('CvC', $stream);
326 0 ▶ if (($cFlags & 0x01) && !$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
327 0 $self->{'Mask'} = PDFArray(PDFNum($transIndex),
328 PDFNum($transIndex));
329 }
330  
331 } elsif ($tag == 0xFE) {
332 # xFE comment extension block
333 # read comment data block(s) until 0 length
334 # currently just discard comment ($stream)
335 0 $fh->read($buf, 1); # len.
336 0 my $len = unpack('C', $buf);
337 0 my $stream = '';
338 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
339 0 $fh->read($buf, $len);
340 0 $stream .= $buf;
341 0 $fh->read($buf, 1);
342 0 $len = unpack('C', $buf);
343 }
344  
345 } elsif ($tag == 0x01) {
346 # x01 plain text extension block
347 0 $fh->read($buf, 13); # len.
348 0 my ($blkSize,$tgL,$tgT,$tgW,$tgH,$ccW,$ccH,$tFci,$tBci) =
349 unpack('CvvvvCCCC', $buf);
350  
351 # read plain text data block(s) until 0 length
352 # currently just discard comment ($stream)
353 0 $fh->read($buf, 1); # len.
354 0 my $len = unpack('C', $buf);
355 0 my $stream = '';
356 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
357 0 $fh->read($buf, $len);
358 0 $stream .= $buf;
359 0 $fh->read($buf, 1);
360 0 $len = unpack('C', $buf);
361 }
362  
363 } elsif ($tag == 0xFF) {
364 # xFF application extension block
365 0 $fh->read($buf, 1);
366 0 my $blkSize = unpack('C', $buf);
367 0 $fh->read($buf, 8);
368 0 my $appID = unpack('C8', $buf);
369  
370 0 $fh->read($buf, 1); # len.
371 0 my $len = unpack('C', $buf);
372 0 my $stream = '';
373 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
374 0 $fh->read($buf, $len);
375 0 $stream .= $buf;
376 0 $fh->read($buf, 1);
377 0 $len = unpack('C', $buf);
378 }
379  
380 } else {
381 0 print "unsupported extension block (".
382 sprintf("0x%02X",$tag).") ignored!\n";
383 }
384  
385 } else {
386 # other extensions and blocks (ignored)
387 0 print "unsupported extension or block (".
388 sprintf("0x%02X",$sep).") ignored.\n";
389  
390 0 $fh->read($buf, 1); # tag.
391 0 my $tag = unpack('C', $buf);
392 0 $fh->read($buf, 1); # tag.
393 0 my $len = unpack('C', $buf);
394 0 ▶ while ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
395 0 $fh->read($buf, $len);
396 0 $fh->read($buf, 1);
397 0 $len = unpack('C', $buf);
398 }
399 }
400 }
401 2 $fh->close();
402  
403 2 $self->filters('FlateDecode');
404  
405 2 return $self;
406 }
407  
408 2 1;
« Builder/Resource/XObject/Image/PNG.pm ↑ Summary Builder/Basic/PDF/Filter/FlateDecode.pm »