Builder/Resource/XObject/Image/PNG.pm

filter: Statementstmt 59.5% 153 / 257 Branchbran 54.8% 45 / 82 Conditioncond 34.6% 9 / 26 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 100% 13 / 13 Podpod 50.0% 2 / 4 totaltotal 56.1% 224 / 399 CC 60 SCAR 54.8
CC
60
Cov
57%
new
370.4
unprocess
12.9
PaethPredictor
4.0
CRAP 239.2
« Builder/UniWrap.pm ↑ Summary Builder/Resource/XObject/Image/GIF.pm »
1 package PDF::Builder::Resource::XObject::Image::PNG;
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 Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 PDF::Builder::Resource::XObject::Image::PNG - Support routines for PNG image
22 library (using pure Perl code)
23  
24 Inherits from L<PDF::Builder::Resource::XObject::Image>
25  
26 =head1 METHODS
27  
28 =head2 new
29  
30 $res = PDF::Builder::Resource::XObject::Image::PNG->new($pdf, $file, %opts)
31  
32 =over
33  
34 Returns a PNG-image object. C<$pdf> is the PDF object being added to, C<$file>
35 is the input PNG file, and the optional C<$name> of the new parent image object
36 defaults to PxAAA.
37  
38 If the Image::PNG::Libpng package is installed, the PNG_IPL library will be
39 used instead of the PNG library. In such a case, use of the PNG library may be
40 forced via the C<nouseIPL> flag (see Builder documentation for C<image_png()>).
41  
42 B<opts:>
43  
44 =over
45  
46 =item 'notrans' => 1
47  
48 No transparency -- ignore tRNS chunk if provided, ignore Alpha channel
49 if provided.
50  
51 =item 'name' => 'string'
52  
53 This is the name you can give for the PNG image object. The default is Pxnnnn.
54  
55 =back
56  
57 Remember that you need to invoke the image_png method from Builder.pm in
58 order to use this functionality.
59  
60 =back
61  
62 =head2 Supported PNG types
63  
64 (0) Gray scale of depth 1, 2, 4, or 8 bits per pixel (2, 4, 16, or 256
65 gray levels). 16 bpp is not currently supported (a PNG with 16 bpp
66 is a fatal error). Full transparency (of one 8-bit gray value) via
67 the tRNS chunk is allowed, unless the notrans option specifies
68 that it be ignored.
69  
70 (2) RGB 24-bit truecolor with 8 bits per sample (16.7 million colors).
71 16 bps is not currently supported (a PNG with 16 bps is a fatal
72 error). Full transparency (of one 3x8-bit RGB color value) via the
73 tRNS chunk is allowed, unless the notrans option specifies that it
74 be ignored.
75  
76 (3) Palette color with 1, 2, 4, or 8 bits per pixel (2, 4, 16, or 256
77 color table/palette entries). 16 bpp is not currently supported by
78 PNG or PDF. Partial transparency (8-bit Alpha) for each palette
79 entry via the tRNS chunk is allowed, unless the notrans option
80 specifies that it be ignored (all entries fully opaque).
81  
82 (4) Gray scale of depth 8 bits per pixel plus 8-bit Alpha channel (256
83 gray levels and 256 levels of transparency). 16 bpp is not
84 currently supported (a PNG with 16 bpp is a fatal error). The Alpha
85 channel is ignored if the notrans option is given. The tRNS chunk
86 is not permitted.
87  
88 (6) RGB 24-bit truecolor with 8 bits per sample (16.7 million colors)
89 plus 8-bit Alpha channel (256 levels of transparency). 16 bps is not
90 currently supported (a PNG with 16 bps is a fatal error). The Alpha
91 channel is ignored if the notrans option is given. The tRNS chunk
92 is not permitted.
93  
94 In all cases, 16 bits per sample are not implemented. A fatal error will be
95 returned if a PNG image with 16-bps data is supplied. The code is assuming
96 standard "network" bit ordering (Big Endian). Interlaced (progressive) display
97 images are not supported. Use the PNG_IPL version if you need to support 16 bps
98 or interlaced images.
99  
100 The transparency chunk (tRNS) will specify one gray level entry or one RGB
101 entry to be treated as transparent (Alpha = 0). For palette color, up to
102 256 palette entry 8-bit Alpha values are specified (256 levels of transparency,
103 from 0 = transparent to 255 = opaque).
104  
105 Only a limited number of chunks are handled: IHDR, IDAT (internally), PLTE,
106 tRNS, and IEND (internally). All other chunks are ignored at this time. Certain
107 filters and compressions applied to data will be handled, but there may be
108 unsupported methods.
109  
110 =cut
111  
112 # TBD: gAMA (gamma) chunk, perhaps some others?
113  
114 5 ▶sub new {
Subroutine new: called CC=46 SCAR=59.1
Pod documented
115 5 my ($class, $pdf, $file, %opts) = @_;
116 # copy dashed option names to preferred undashed names
117 0 ▶ if (defined $opts{'-nouseIPL'} && !defined $opts{'nouseIPL'}) { $opts{'nouseIPL'} = delete($opts{'-nouseIPL'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-nouseIPL"} and not defined $opts{"nouseIPL"}) 0 5 5
Condition33%1/3
defined $opts{"-nouseIPL"} and not defined $opts{"nouseIPL"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $opts{"-nouseIPL"} and not defined $opts{"nouseIPL"}
defined $opts{"-nouseIPL"} not defined $opts{"nouseIPL"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-nouseIPL"}B= not defined $opts{"nouseIPL"}
ABresult
0X0
100
111
118 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 5 5
Condition33%1/3
defined $opts{"-notrans"} and not defined $opts{"notrans"}
!ll&&!rl&&r
500
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
119 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 5 5
Condition33%1/3
defined $opts{"-name"} and not defined $opts{"name"}
!ll&&!rl&&r
500
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
120 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 5 5
Condition33%1/3
defined $opts{"-compress"} and not defined $opts{"compress"}
!ll&&!rl&&r
500
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
121  
122 5 my ($name, $compress);
123 0 ▶ if (exists $opts{'name'}) { $name = $opts{'name'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"name"}) 0 5 5
124 #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; }
125  
126 5 my $self;
127  
128 5 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 5 5
129  
130 5 ▶ $self = $class->SUPER::new($pdf, $name || 'Px'.pdfkey());
Condition50%1/2
$name || 'Px' . pdfkey()
l!l
05
MC/DC0%0/1
$name || 'Px' . pdfkey()
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
131 5 ▶ $pdf->new_obj($self) unless $self->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_obj($pdf) 0 5 5
132  
133 5 $self->{' apipdf'} = $pdf;
134 5 weaken $self->{' apipdf'};
135  
136 5 my $fh = IO::File->new();
137 5 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 1 4 5
138 1 $fh = $file;
139 } else {
140 4 ▶ open $fh, '<', $file or die "$!: $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $file or die "$!: $file" 3 1 4
141 }
142 4 binmode($fh, ':raw');
143  
144 4 my ($buf, $l, $crc, $w,$h, $bpc, $cs, $cm, $fm, $im, $palette, $trns);
145 4 seek($fh, 8, 0);
146 4 $self->{' stream'} = '';
147 4 $self->{' nofilt'} = 1;
148 4 ▶ while (!eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 18 0 18
149 18 read($fh, $buf, 4);
150 18 $l = unpack('N', $buf);
151 18 read($fh, $buf, 4);
152 18 ▶ if ($buf eq 'IHDR') {
Branch: 5 branches
BranchTrueFalseTotal
if ($buf eq 'IHDR') { } 4 14 18
elsif ($buf eq 'PLTE') { } 2 12 14
elsif ($buf eq 'IDAT') { } 4 8 12
elsif ($buf eq 'tRNS') { } 0 8 8
elsif ($buf eq 'IEND') { } 4 4 8
153 4 read($fh, $buf, $l);
154 4 ($w, $h, $bpc, $cs, $cm, $fm, $im) = unpack('NNCCCCC', $buf);
155 4 ▶ die "Unsupported Compression($cm) Method" if $cm;
Branch: 1 branch
BranchTrueFalseTotal
if $cm 0 4 4
156 4 ▶ die "Unsupported Interlace($im) Method" if $im;
Branch: 1 branch
BranchTrueFalseTotal
if $im 0 4 4
157 4 ▶ die "Unsupported Filter($fm) Method" if $fm;
Branch: 1 branch
BranchTrueFalseTotal
if $fm 0 4 4
158 } elsif ($buf eq 'PLTE') {
159 2 read($fh, $buf, $l);
160 2 $palette = $buf;
161 } elsif ($buf eq 'IDAT') {
162 4 read($fh, $buf, $l);
163 4 $self->{' stream'} .= $buf;
164 } elsif ($buf eq 'tRNS') {
165 0 read($fh, $buf, $l);
166 0 $trns = $buf;
167 } elsif ($buf eq 'IEND') {
168 4 last;
169 } else {
170 # skip ahead
171 4 seek($fh, $l, 1);
172 }
173 14 read($fh, $buf, 4);
174 14 $crc = $buf;
175 }
176 4 close($fh);
177  
178 4 $self->width($w);
179 4 $self->height($h);
180  
181 4 ▶ if ($cs == 0){ # greyscale (1,2,4,8 bps, 16 not supported here)
Branch: 5 branches
BranchTrueFalseTotal
if ($cs == 0) { } 0 4 4
elsif ($cs == 2) { } 0 4 4
elsif ($cs == 3) { } 2 2 4
elsif ($cs == 4) { } 0 2 2
elsif ($cs == 6) { } 2 0 2
182 # transparency via tRNS chunk allowed
183 # scanline = ceil(bpc * comp / 8)+1
184 0 ▶ if ($bpc > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc > 8) { } 0 0 0
185 0 die ">8 bits of greylevel in PNG is not supported.";
186 } else {
187 0 $self->filters('FlateDecode');
188 0 $self->colorspace('DeviceGray');
189 0 $self->bits_per_component($bpc);
190 0 my $dict = PDFDict();
191 0 $self->{'DecodeParms'} = PDFArray($dict);
192 0 $dict->{'Predictor'} = PDFNum(15);
193 0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
194 0 $dict->{'Colors'} = PDFNum(1);
195 0 $dict->{'Columns'} = PDFNum($w);
196 0 ▶ if (defined $trns && !$opts{'notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trns and not $opts{"notrans"}) 0 0 0
Condition0%0/3
defined $trns and not $opts{"notrans"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $trns and not $opts{"notrans"}
defined $trns not $opts{"notrans"}
Truth table0%0/3
A and not(B)
A= defined $trnsB= not $opts{"notrans"}
ABresult
0X0
100
111
197 0 my $m = mMax(unpack('n*', $trns));
198 0 my $n = mMin(unpack('n*', $trns));
199 0 $self->{'Mask'} = PDFArray(PDFNum($n), PDFNum($m));
200 }
201 }
202 } elsif ($cs == 2) { # RGB 8 bps (16 not supported here)
203 # transparency via tRNS chunk allowed
204 0 ▶ if ($bpc > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc > 8) { } 0 0 0
205 0 die ">8 bits of RGB in PNG is not supported.";
206 } else {
207 0 $self->filters('FlateDecode');
208 0 $self->colorspace('DeviceRGB');
209 0 $self->bits_per_component($bpc);
210 0 my $dict = PDFDict();
211 0 $self->{'DecodeParms'} = PDFArray($dict);
212 0 $dict->{'Predictor'} = PDFNum(15);
213 0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
214 0 $dict->{'Colors'} = PDFNum(3);
215 0 $dict->{'Columns'} = PDFNum($w);
216 0 ▶ if (defined $trns && !$opts{'notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trns and not $opts{"notrans"}) 0 0 0
Condition0%0/3
defined $trns and not $opts{"notrans"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $trns and not $opts{"notrans"}
defined $trns not $opts{"notrans"}
Truth table0%0/3
A and not(B)
A= defined $trnsB= not $opts{"notrans"}
ABresult
0X0
100
111
217 0 my @v = unpack('n*', $trns);
218 0 my (@cr,@cg,@cb, $m, $n);
219 0 ▶ while (scalar @v > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @v > 0) 0 0 0
220 0 push(@cr, shift(@v));
221 0 push(@cg, shift(@v));
222 0 push(@cb, shift(@v));
223 }
224 0 @v = ();
225 0 $m = mMax(@cr);
226 0 $n = mMin(@cr);
227 0 push @v, $n,$m;
228 0 $m = mMax(@cg);
229 0 $n = mMin(@cg);
230 0 push @v, $n,$m;
231 0 $m = mMax(@cb);
232 0 $n = mMin(@cb);
233 0 push @v, $n,$m;
234 0 $self->{'Mask'} = PDFArray(map { PDFNum($_) } @v);
235 }
236 }
237 } elsif ($cs == 3) { # palette 1,2,4,8 bpp depth (is 16 legal?)
238 # transparency via tRNS chunk allowed
239 2 ▶ if ($bpc > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc > 8) { } 0 2 2
240 0 die ">8 bits of palette in PNG is not supported.";
241 } else {
242 2 my $dict = PDFDict();
243 2 $pdf->new_obj($dict);
244 2 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
245 2 $dict->{' stream'} = $palette;
246 2 $palette = "";
247 2 $self->filters('FlateDecode');
248 2 $self->colorspace(PDFArray(PDFName('Indexed'), PDFName('DeviceRGB'), PDFNum(int(length($dict->{' stream'})/3)-1), $dict));
249 2 $self->bits_per_component($bpc);
250 2 $dict = PDFDict();
251 2 $self->{'DecodeParms'} = PDFArray($dict);
252 2 $dict->{'Predictor'} = PDFNum(15);
253 2 $dict->{'BitsPerComponent'} = PDFNum($bpc);
254 2 $dict->{'Colors'} = PDFNum(1);
255 2 $dict->{'Columns'} = PDFNum($w);
256 2 ▶ if (defined $trns && !$opts{'notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $trns and not $opts{"notrans"}) 0 2 2
Condition33%1/3
defined $trns and not $opts{"notrans"}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $trns and not $opts{"notrans"}
defined $trns not $opts{"notrans"}
Truth table33%1/3
A and not(B)
A= defined $trnsB= not $opts{"notrans"}
ABresult
0X0
100
111
257 0 $trns .= "\xFF" x 256; # pad out with opaque entries to
258 # ensure at least 256 entries available
259 0 $dict = PDFDict();
260 0 $pdf->new_obj($dict);
261 0 $dict->{'Type'} = PDFName('XObject');
262 0 $dict->{'Subtype'} = PDFName('Image');
263 0 $dict->{'Width'} = PDFNum($w);
264 0 $dict->{'Height'} = PDFNum($h);
265 0 $dict->{'ColorSpace'} = PDFName('DeviceGray');
266 0 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
267 # $dict->{'Filter'} = PDFArray(PDFName('ASCIIHexDecode'));
268 0 $dict->{'BitsPerComponent'} = PDFNum(8);
269 0 $self->{'SMask'} = $dict;
270 # length of row (scanline) in bytes, plus 1
271 0 my $scanline = 1 + ceil($bpc * $w/8);
272 # bytes per pixel (always 1)
273 0 my $bpp = ceil($bpc/8);
274 # uncompressed and unfiltered image data (stream of 1,2,4, or
275 # 8 bit indices into palette)
276 0 my $clearstream = unprocess($bpc, $bpp, 1, $w,$h, $scanline, \$self->{' stream'});
277 0 foreach my $n (0 .. ($h*$w)-1) {
278 # dict->stream initially empty. fill with Alpha value for
279 # each pixel, indexed by pixel value
280 0 vec($dict->{' stream'}, $n, 8) = # each Alpha 8 bits
281 vec($trns, # the table of Alphas corresponding to palette
282 vec($clearstream, $n, $bpc), #1-8 bit index to palette
283 8); # Alpha is 8 bits
284 # print STDERR vec($trns,vec($clearstream,$n,$bpc),8)."=".vec($clearstream,$n,$bpc).",";
285 }
286 # print STDERR "\n";
287 }
288 }
289 } elsif ($cs == 4) { # greyscale+alpha 8 bps (16 not supported here)
290 # transparency via tRNS chunk NOT allowed
291 0 ▶ if ($bpc > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc > 8) { } 0 0 0
292 0 die ">8 bits of greylevel+alpha in PNG is not supported.";
293 } else {
294 0 $self->filters('FlateDecode');
295 0 $self->colorspace('DeviceGray');
296 0 $self->bits_per_component($bpc);
297 0 my $dict = PDFDict();
298 0 $self->{'DecodeParms'} = PDFArray($dict);
299 # $dict->{'Predictor'} = PDFNum(15);
300 0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
301 0 $dict->{'Colors'} = PDFNum(1);
302 0 $dict->{'Columns'} = PDFNum($w);
303  
304 0 $dict = PDFDict();
305 0 ▶ unless ($opts{'notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"notrans"}) 0 0 0
306 0 $pdf->new_obj($dict);
307 0 $dict->{'Type'} = PDFName('XObject');
308 0 $dict->{'Subtype'} = PDFName('Image');
309 0 $dict->{'Width'} = PDFNum($w);
310 0 $dict->{'Height'} = PDFNum($h);
311 0 $dict->{'ColorSpace'} = PDFName('DeviceGray');
312 0 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
313 0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
314 0 $self->{'SMask'} = $dict;
315 }
316 # as with cs=3, create SMask of Alpha entry for each pixel. this
317 # time, separating Alpha from grayscale and putting in dict->stream
318 0 my $scanline = 1 + ceil($bpc*2 * $w/8);
319 0 my $bpp = ceil($bpc*2 / 8);
320 0 my $clearstream = unprocess($bpc, $bpp, 2, $w,$h, $scanline, \$self->{' stream'});
321 0 delete $self->{' nofilt'};
322 #delete $self->{' stream'};
323 0 $dict->{' stream'} = '';
324 0 $self->{' stream'} = '';
325 # dict->stream is the outer dict if notrans, and the Alpha data
326 # moved to it is simply unused
327 # dict->stream is the inner dict (created if !notrans), and the
328 # Alpha data moved to it becomes the SMask
329 # rebuild self->stream from the gray data in clearstream
330 0 foreach my $n (0 .. $h*$w-1) {
331 0 vec($dict->{' stream'}, $n, $bpc) = vec($clearstream, $n*2+1, $bpc);
332 0 vec($self->{' stream'}, $n, $bpc) = vec($clearstream, $n*2, $bpc);
333 }
334 }
335 } elsif ($cs == 6) { # RGB+alpha 8 bps (16 not supported here)
336 # transparency via tRNS chunk NOT allowed
337 2 ▶ if ($bpc > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bpc > 8) { } 0 2 2
338 0 die ">8 bits of RGB+alpha in PNG is not supported.";
339 } else {
340 2 $self->filters('FlateDecode');
341 2 $self->colorspace('DeviceRGB');
342 2 $self->bits_per_component($bpc);
343 2 my $dict = PDFDict();
344 2 $self->{'DecodeParms'} = PDFArray($dict);
345 # $dict->{'Predictor'} = PDFNum(15);
346 2 $dict->{'BitsPerComponent'} = PDFNum($bpc);
347 2 $dict->{'Colors'} = PDFNum(3);
348 2 $dict->{'Columns'} = PDFNum($w);
349  
350 2 $dict = PDFDict();
351 2 ▶ unless ($opts{'notrans'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"notrans"}) 2 0 2
352 2 $pdf->new_obj($dict);
353 2 $dict->{'Type'} = PDFName('XObject');
354 2 $dict->{'Subtype'} = PDFName('Image');
355 2 $dict->{'Width'} = PDFNum($w);
356 2 $dict->{'Height'} = PDFNum($h);
357 2 $dict->{'ColorSpace'} = PDFName('DeviceGray');
358 2 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
359 2 $dict->{'BitsPerComponent'} = PDFNum($bpc);
360 2 $self->{'SMask'} = $dict;
361 }
362 # bytes per pixel (4 samples) and length of row scanline in bytes
363 2 my $scanline = 1 + ceil($bpc*4 * $w/8);
364 2 my $bpp = ceil($bpc*4 /8);
365 # unpacked, uncompressed, unfiltered image data
366 2 my $clearstream = unprocess($bpc, $bpp, 4, $w,$h, $scanline, \$self->{' stream'});
367 2 delete $self->{' nofilt'};
368 #delete $self->{' stream'};
369 2 $dict->{' stream'} = '';
370 2 $self->{' stream'} = '';
371 # as with cs=4, create SMask of Alpha entry for each pixel. this
372 # time, separating Alpha from RGB triplet and put in dict->stream
373 # dict->stream is the outer dict if notrans, and the Alpha data
374 # moved to it is simply unused
375 # dict->stream is the inner dict (created if !notrans), and the
376 # Alpha data moved to it becomes the SMask
377 # rebuild self->stream from the RGB data in clearstream 1/3 smaller
378 2 foreach my $n (0 .. ($h*$w)-1) {
379 # pull out Alpha data bpc bits into new dict SMask
380 218120 vec($dict->{' stream'}, $n, $bpc) = vec($clearstream, $n*4+3, $bpc);
381 # transfer RGB triplet into self->stream
382 218120 vec($self->{' stream'}, $n*3, $bpc) = vec($clearstream, $n*4, $bpc);
383 218120 vec($self->{' stream'}, $n*3+1, $bpc) = vec($clearstream, $n*4+1, $bpc);
384 218120 vec($self->{' stream'}, $n*3+2, $bpc) = vec($clearstream, $n*4+2, $bpc);
385 }
386 }
387 } else {
388 0 die "unsupported PNG-color type (cs=$cs).";
389 }
390  
391 4 return($self);
392 }
393  
394 =head2 usesLib
395  
396 $mode = $png->usesLib()
397  
398 =over
399  
400 Returns 1 if Image::PNG::Libpng installed and used, 0 if not installed, or -1
401 if installed but not used (nouseIPL option given to C<image_png>).
402  
403 B<Caution:> this method can only be used I<after> the image object has been
404 created. It can't tell you whether Image::PNG::Libpng is available in
405 advance of actually using it, in case you want to use some functionality
406 available only in PNG_IPL. See the L<PDF::Builder> LA_IPL() call if you
407 need to know in advance.
408  
409 =back
410  
411 =cut
412  
413 3 ▶sub usesLib {
Subroutine usesLib: called CC=1 SCAR=0.0
Pod documented
414 3 my ($self) = shift;
415 # should be 0 for Image::PNG::Libpng not installed, or -1 for is installed,
416 # but not using it
417 3 return $self->{'usesIPL'}->val();
418 }
419  
420 94240 ▶sub PaethPredictor {
Subroutine PaethPredictor: called CC=4 SCAR=13.9
Pod undocumented
421 94240 my ($a, $b, $c) = @_;
422 94240 my $p = $a + $b - $c;
423 94240 my $pa = abs($p - $a);
424 94240 my $pb = abs($p - $b);
425 94240 my $pc = abs($p - $c);
426 94240 ▶ if (($pa <= $pb) && ($pa <= $pc)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pa <= $pb and $pa <= $pc) { } 82304 11936 94240
elsif ($pb <= $pc) { } 11154 782 11936
Condition100%3/3
$pa <= $pb and $pa <= $pc
!ll&&!rl&&r
1119674082304
MC/DC100%2/2
$pa <= $pb and $pa <= $pc
$pa <= $pb $pa <= $pc
Truth table100%3/3
A and B
A= $pa <= $pbB= $pa <= $pc
ABresult
0X0
100
111
427 82304 return $a;
428 } elsif ($pb <= $pc) {
429 11154 return $b;
430 } else {
431 782 return $c;
432 }
433 }
434  
435 2 ▶sub unprocess {
Subroutine unprocess: called CC=12 SCAR=25.5
Pod undocumented
436 2 my ($bpc, $bpp, $comp, $width,$height, $scanline, $sstream) = @_;
437  
438 2 my $stream = uncompress($$sstream);
439 2 my $prev = '';
440 2 my $clearstream = '';
441 2 foreach my $n (0 .. $height-1) {
442 # print STDERR "line $n:";
443 574 my $line = substr($stream, $n*$scanline, $scanline);
444 574 my $filter = vec($line, 0, 8);
445 574 my $clear = '';
446 574 $line = substr($line, 1);
447 # print STDERR " filter=$filter";
448 574 ▶ if ($filter == 0) {
Branch: 5 branches
BranchTrueFalseTotal
if ($filter == 0) { } 0 574 574
elsif ($filter == 1) { } 18 556 574
elsif ($filter == 2) { } 494 62 556
elsif ($filter == 3) { } 0 62 62
elsif ($filter == 4) { } 62 0 62
449 0 $clear = $line;
450 } elsif ($filter == 1) {
451 18 foreach my $x (0 .. length($line)-1) {
452 27360 vec($clear, $x, 8) = (vec($line, $x, 8) + vec($clear, $x-$bpp, 8))%256;
453 }
454 } elsif ($filter == 2) {
455 494 foreach my $x (0 .. length($line)-1) {
456 750880 vec($clear, $x, 8) = (vec($line, $x, 8) + vec($prev, $x, 8))%256;
457 }
458 } elsif ($filter == 3) {
459 0 foreach my $x (0 .. length($line)-1) {
460 0 vec($clear, $x, 8) = (vec($line, $x, 8) + floor((vec($clear, $x-$bpp, 8) + vec($prev, $x, 8))/2))%256;
461 }
462 } elsif ($filter == 4) {
463 62 foreach my $x (0 .. length($line)-1) {
464 94240 vec($clear, $x, 8) = (vec($line, $x, 8) + PaethPredictor(vec($clear, $x-$bpp, 8), vec($prev, $x, 8), vec($prev, $x-$bpp, 8)))%256;
465 }
466 }
467 574 $prev = $clear;
468 574 foreach my $x (0 .. ($width*$comp)-1) {
469 872480 vec($clearstream, ($n*$width*$comp)+$x, $bpc) = vec($clear, $x, $bpc);
470 # print STDERR "".vec($clear,$x,$bpc).",";
471 }
472 # print STDERR "\n";
473 }
474 2 return $clearstream;
475 }
476  
477 2 1;
478  
479 __END__
« Builder/UniWrap.pm ↑ Summary Builder/Resource/XObject/Image/GIF.pm »