API3/Compat/API2/Resource/XObject/Image/GIF.pm

filter: Statementstmt 13.0% 21 / 161 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 63.6% 7 / 11 Podpod 50.0% 2 / 4 totaltotal 12.2% 30 / 246 CC 32 SCAR 55.2
CC
32
Cov
9%
new
462.0
deGIF
72.0
unInterlace
30.0
CRAP 248.5
« API3/Compat/API2/UniWrap.pm ↑ Summary API3/Compat/API2/Basic/TTF/AATutils.pm »
1 #=======================================================================
2 # ____ ____ _____ _ ____ ___ ____
3 # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4 # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5 # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6 # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7 #
8 # A Perl Module Chain to faciliate the Creation and Modification
9 # of High-Quality "Portable Document Format (PDF)" Files.
10 #
11 # Copyright 1999-2005 Alfred Reibenschuh <areibens@cpan.org>.
12 #
13 #=======================================================================
14 #
15 # This library is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU Lesser General Public
17 # License as published by the Free Software Foundation; either
18 # version 2 of the License, or (at your option) any later version.
19 #
20 # This library is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # Lesser General Public License for more details.
24 #
25 # You should have received a copy of the GNU Lesser General Public
26 # License along with this library; if not, write to the
27 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
28 # Boston, MA 02111-1307, USA.
29 #
30 # $Id: GIF.pm,v 2.0 2005/11/16 02:18:23 areibens Exp $
31 #
32 #=======================================================================
33
34 package PDF::API3::Compat::API2::Resource::XObject::Image::GIF;
35
36 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
37
38 1 ▶ use PDF::API3::Compat::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶ use PDF::API3::Compat::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶ use PDF::API3::Compat::API2::Resource::XObject::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
41
42 1 ▶ use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
43
44 1 ▶ use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 @ISA = qw( PDF::API3::Compat::API2::Resource::XObject::Image );
46
47 1 ( $VERSION ) = sprintf '%i.%03i', split(/\./,('$Revision: 2.0 $' =~ /Revision: (\S+)\s/)[0]); # $Date: 2005/11/16 02:18:23 $
48
49 }
50 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 =item $res = PDF::API3::Compat::API2::Resource::XObject::Image::GIF->new $pdf, $file [, $name]
53
54 Returns a gif-image object.
55
56 =cut
57
58 # added from PDF::Create:
59 # PDF::Image::GIFImage - GIF image support
60 # Author: Michael Gross <mdgrosse@sbox.tugraz.at>
61 # modified for internal use. (c) 2004 fredo.
62 0 ▶sub unInterlace {
Subroutine unInterlace: not called CC=5 SCAR=34.0
Pod undocumented
63 0 my $self = shift;
64 0 my $data = $self->{' stream'};
65 0 my $row;
66 my @result;
67 0 my $width = $self->width;
68 0 my $height = $self->height;
69 0 my $idx = 0;
70
71 #Pass 1 - every 8th row, starting with row 0
72 0 $row = 0;
73 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
74 0 $result[$row] = substr($data, $idx*$width, $width);
75 0 $row+=8;
76 0 $idx++;
77 }
78
79 #Pass 2 - every 8th row, starting with row 4
80 0 $row = 4;
81 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
82 0 $result[$row] = substr($data, $idx*$width, $width);
83 0 $row+=8;
84 0 $idx++;
85 }
86
87 #Pass 3 - every 4th row, starting with row 2
88 0 $row = 2;
89 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
90 0 $result[$row] = substr($data, $idx*$width, $width);
91 0 $row+=4;
92 0 $idx++;
93 }
94
95 #Pass 4 - every 2th row, starting with row 1
96 0 $row = 1;
97 0 ▶ while ($row < $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row < $height) 0 0 0
98 0 $result[$row] = substr($data, $idx*$width, $width);
99 0 $row+=2;
100 0 $idx++;
101 }
102
103 0 $self->{' stream'}=join('', @result);
104 }
105
106 0 ▶sub deGIF {
Subroutine deGIF: not called CC=8 SCAR=42.8
Pod undocumented
107 0 my ($ibits,$stream)=@_;
108 0 my $bits=$ibits;
109 0 my $resetcode=1<<($ibits-1);
110 0 my $endcode=$resetcode+1;
111 0 my $nextcode=$endcode+1;
112 0 my $ptr=0;
113 0 my $maxptr=8*length($stream);
114 0 my $tag;
115 0 my $out='';
116 0 my $outptr=0;
117
118 # print STDERR "reset=$resetcode\nend=$endcode\nmax=$maxptr\n";
119
120 0 my @d=map { chr($_) } (0..$resetcode-1);
121
122 0 ▶ while(($ptr+$bits)<=$maxptr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr + $bits <= $maxptr) 0 0 0
123 0 $tag=0;
124 0 foreach my $off (reverse 0..$bits-1) {
125 0 $tag<<=1;
126 0 $tag|=vec($stream,$ptr+$off,1);
127 }
128 # foreach my $off (0..$bits-1) {
129 # $tag<<=1;
130 # $tag|=vec($stream,$ptr+$off,1);
131 # }
132 # print STDERR "ptr=$ptr,tag=$tag,bits=$bits,next=$nextcode\n";
133 # print STDERR "tag to large\n" if($tag>$nextcode);
134 0 $ptr+=$bits;
135 0 ▶ $bits++ if($nextcode == (1<<$bits));
Branch: 1 branch
BranchTrueFalseTotal
if $nextcode == 1 << $bits 0 0 0
136 0 ▶ if($tag==$resetcode) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tag == $resetcode) { } 0 0 0
elsif ($tag == $endcode) { } 0 0 0
elsif ($tag < $resetcode) { } 0 0 0
elsif ($tag > $endcode) { } 0 0 0
137 0 $bits=$ibits;
138 0 $nextcode=$endcode+1;
139 0 next;
140 } elsif($tag==$endcode) {
141 0 last;
142 } elsif($tag<$resetcode) {
143 0 $d[$nextcode]=$d[$tag];
144 0 $out.=$d[$nextcode];
145 0 $nextcode++;
146 } elsif($tag>$endcode) {
147 0 $d[$nextcode]=$d[$tag];
148 0 $d[$nextcode].=substr($d[$tag+1],0,1);
149 0 $out.=$d[$nextcode];
150 0 $nextcode++;
151 }
152 }
153 0 return($out);
154 }
155
156 0 ▶sub new {
Subroutine new: not called CC=21 SCAR=61.4
Pod documented
157 0 my ($class,$pdf,$file,$name,%opts) = @_;
158 0 my $self;
159 0 my $inter=0;
160
161 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
162
163 0 ▶ $self=$class->SUPER::new($pdf,$name || 'Gx'.pdfkey());
Condition0%0/2
$name || 'Gx' . pdfkey()
l!l
00
MC/DC0%0/1
$name || 'Gx' . pdfkey()
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
164 0 ▶ $pdf->new_obj($self) unless($self->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 0 0
165
166 0 $self->{' apipdf'}=$pdf;
167
168 0 my $fh = IO::File->new;
169 0 open($fh,$file);
170 0 binmode($fh,':raw');
171 0 my $buf;
172 0 $fh->read($buf,6); # signature
173 0 ▶ die "unknown image signature '$buf' -- not a gif." unless($buf=~/^GIF[0-9][0-9][a-b]/);
Branch: 1 branch
BranchTrueFalseTotal
unless $buf =~ /^GIF[0-9][0-9][a-b]/ 0 0 0
174
175 0 $fh->read($buf,7); # logical descr.
176 0 my($wg,$hg,$flags,$bgColorIndex,$aspect)=unpack('vvCCC',$buf);
177
178 0 ▶ if($flags&0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 0 0 0
179 0 my $colSize=2**(($flags&0x7)+1);
180 0 my $dict=PDFDict();
181 0 $pdf->new_obj($dict);
182 0 $self->colorspace(PDFArray(PDFName('Indexed'),PDFName('DeviceRGB'),PDFNum($colSize-1),$dict));
183 0 $fh->read($dict->{' stream'},3*$colSize); # color-table
184 }
185
186 0 ▶ while(!$fh->eof) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh->eof) 0 0 0
187 0 $fh->read($buf,1); # tag.
188 0 my $sep=unpack('C',$buf);
189 0 ▶ if($sep==0x2C){
Branch: 3 branches
BranchTrueFalseTotal
if ($sep == 44) { } 0 0 0
elsif ($sep == 59) { } 0 0 0
elsif ($sep == 33) { } 0 0 0
190 0 $fh->read($buf,9); # image-descr.
191 0 my ($left,$top,$w,$h,$flags)=unpack('vvvvC',$buf);
192
193 0 ▶ $self->width($w||$wg);
Condition0%0/3
$w || $wg
l!l&&r!l&&!r
000
MC/DC0%0/2
$w || $wg
$w $wg
Truth table0%0/3
A || B
A= $wB= $wg
ABresult
1X1
011
000
194 0 ▶ $self->height($h||$hg);
Condition0%0/3
$h || $hg
l!l&&r!l&&!r
000
MC/DC0%0/2
$h || $hg
$h $hg
Truth table0%0/3
A || B
A= $hB= $hg
ABresult
1X1
011
000
195 0 $self->bpc(8);
196
197 0 ▶ if($flags&0x80) { # local colormap
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 0 0 0
198 0 my $colSize=2**(($flags&0x7)+1);
199 0 my $dict=PDFDict();
200 0 $pdf->new_obj($dict);
201 0 $self->colorspace(PDFArray(PDFName('Indexed'),PDFName('DeviceRGB'),PDFNum($colSize-1),$dict));
202 0 $fh->read($dict->{' stream'},3*$colSize); # color-table
203 }
204 0 ▶ if($flags&0x40) { # need de-interlace
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 64) 0 0 0
205 0 $inter=1;
206 }
207
208 0 $fh->read($buf,1); # image-lzw-start (should be 9).
209 0 my ($sep)=unpack('C',$buf);
210
211 0 $fh->read($buf,1); # first chunk.
212 0 my ($len)=unpack('C',$buf);
213 0 my $stream='';
214 0 ▶ while($len>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
215 0 $fh->read($buf,$len);
216 0 $stream.=$buf;
217 0 $fh->read($buf,1);
218 0 $len=unpack('C',$buf);
219 }
220 0 $self->{' stream'}=deGIF($sep+1,$stream);
221 0 ▶ $self->unInterlace if($inter);
Branch: 1 branch
BranchTrueFalseTotal
if $inter 0 0 0
222 0 last;
223 } elsif($sep==0x3b) {
224 0 last;
225 } elsif($sep==0x21) {
226 # Graphic Control Extension
227 0 $fh->read($buf,1); # tag.
228 0 my $tag=unpack('C',$buf);
229 0 ▶ die "unsupported graphic control extension ($tag)" unless($tag==0xF9);
Branch: 1 branch
BranchTrueFalseTotal
unless $tag == 249 0 0 0
230 0 $fh->read($buf,1); # len.
231 0 my $len=unpack('C',$buf);
232 0 my $stream='';
233 0 ▶ while($len>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
234 0 $fh->read($buf,$len);
235 0 $stream.=$buf;
236 0 $fh->read($buf,1);
237 0 $len=unpack('C',$buf);
238 }
239 0 my ($cFlags,$delay,$transIndex)=unpack('CvC',$stream);
240 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
241 0 $self->{Mask}=PDFArray(PDFNum($transIndex),PDFNum($transIndex));
242 }
243 } else {
244 # extension
245 0 $fh->read($buf,1); # tag.
246 0 my $tag=unpack('C',$buf);
247 0 $fh->read($buf,1); # tag.
248 0 my $len=unpack('C',$buf);
249 0 ▶ while($len>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 0 0 0
250 0 $fh->read($buf,$len);
251 0 $fh->read($buf,1);
252 0 $len=unpack('C',$buf);
253 }
254 }
255 }
256 0 $fh->close;
257
258 0 $self->filters('FlateDecode');
259
260 0 return($self);
261 }
262
263 =item $res = PDF::API3::Compat::API2::Resource::XObject::Image::GIF->new_api $api, $file [, $name]
264
265 Returns a gif-image object. This method is different from 'new' that
266 it needs an PDF::API3::Compat::API2-object rather than a Text::PDF::File-object.
267
268 =cut
269
270 0 ▶sub new_api {
Subroutine new_api: not called CC=1 SCAR=6.9
Pod documented
271 0 my ($class,$api,@opts)=@_;
272
273 0 my $obj=$class->new($api->{pdf},@opts);
274 0 $obj->{' api'}=$api;
275
276 0 return($obj);
277 }
278
279 1 1;
280
281 __END__
« API3/Compat/API2/UniWrap.pm ↑ Summary API3/Compat/API2/Basic/TTF/AATutils.pm »