File Coverage

blib/lib/PDF/Builder/Resource/XObject/Image/PNG.pm
Criterion Covered Total %
statement 150 254 59.0
branch 44 78 56.4
condition 9 27 33.3
subroutine 13 13 100.0
pod 2 4 50.0
total 218 376 57.9


line stmt bran cond sub pod time code
1             package PDF::Builder::Resource::XObject::Image::PNG;
2              
3 2     2   1135 use base 'PDF::Builder::Resource::XObject::Image';
  2         5  
  2         772  
4              
5 2     2   13 use strict;
  2         4  
  2         40  
6 2     2   7 use warnings;
  2         4  
  2         175  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10              
11 2     2   11 use Compress::Zlib;
  2         3  
  2         745  
12 2     2   14 use POSIX qw(ceil floor);
  2         5  
  2         17  
13              
14 2     2   207 use IO::File;
  2         3  
  2         431  
15 2     2   13 use PDF::Builder::Util;
  2         4  
  2         307  
16 2     2   14 use PDF::Builder::Basic::PDF::Utils;
  2         4  
  2         171  
17 2     2   11 use Scalar::Util qw(weaken);
  2         14  
  2         5801  
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             sub new {
115 5     5 1 22 my ($class, $pdf, $file, %opts) = @_;
116             # copy dashed option names to preferred undashed names
117 5 50 33     24 if (defined $opts{'-nouseIPL'} && !defined $opts{'nouseIPL'}) { $opts{'nouseIPL'} = delete($opts{'-nouseIPL'}); }
  0         0  
118 5 50 33     21 if (defined $opts{'-notrans'} && !defined $opts{'notrans'}) { $opts{'notrans'} = delete($opts{'-notrans'}); }
  0         0  
119 5 50 33     20 if (defined $opts{'-name'} && !defined $opts{'name'}) { $opts{'name'} = delete($opts{'-name'}); }
  0         0  
120 5 50 33     24 if (defined $opts{'-compress'} && !defined $opts{'compress'}) { $opts{'compress'} = delete($opts{'-compress'}); }
  0         0  
121              
122 5         10 my ($name, $compress);
123 5 50       17 if (exists $opts{'name'}) { $name = $opts{'name'}; }
  0         0  
124             #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; }
125              
126 5         8 my $self;
127              
128 5 50       16 $class = ref($class) if ref($class);
129              
130 5   33     51 $self = $class->SUPER::new($pdf, $name || 'Px'.pdfkey());
131 5 50       18 $pdf->new_obj($self) unless $self->is_obj($pdf);
132              
133 5         17 $self->{' apipdf'} = $pdf;
134 5         11 weaken $self->{' apipdf'};
135              
136 5         38 my $fh = IO::File->new();
137 5 100       341 if (ref($file)) {
138 1         3 $fh = $file;
139             } else {
140 4 100       293 open $fh, '<', $file or die "$!: $file";
141             }
142 4         32 binmode($fh, ':raw');
143              
144 4         12 my ($buf, $l, $crc, $w,$h, $bpc, $cs, $cm, $fm, $im, $palette, $trns);
145 4         33 seek($fh, 8, 0);
146 4         15 $self->{' stream'} = '';
147 4         13 $self->{' nofilt'} = 1;
148 4         152 while (!eof($fh)) {
149 18         40 read($fh, $buf, 4);
150 18         41 $l = unpack('N', $buf);
151 18         35 read($fh, $buf, 4);
152 18 100       73 if ($buf eq 'IHDR') {
    100          
    100          
    50          
    100          
153 4         9 read($fh, $buf, $l);
154 4         20 ($w, $h, $bpc, $cs, $cm, $fm, $im) = unpack('NNCCCCC', $buf);
155 4 50       15 die "Unsupported Compression($cm) Method" if $cm;
156 4 50       10 die "Unsupported Interlace($im) Method" if $im;
157 4 50       11 die "Unsupported Filter($fm) Method" if $fm;
158             } elsif ($buf eq 'PLTE') {
159 2         4 read($fh, $buf, $l);
160 2         4 $palette = $buf;
161             } elsif ($buf eq 'IDAT') {
162 4         136 read($fh, $buf, $l);
163 4         58 $self->{' stream'} .= $buf;
164             } elsif ($buf eq 'tRNS') {
165 0         0 read($fh, $buf, $l);
166 0         0 $trns = $buf;
167             } elsif ($buf eq 'IEND') {
168 4         8 last;
169             } else {
170             # skip ahead
171 4         31 seek($fh, $l, 1);
172             }
173 14         47 read($fh, $buf, 4);
174 14         38 $crc = $buf;
175             }
176 4         99 close($fh);
177              
178 4         39 $self->width($w);
179 4         22 $self->height($h);
180              
181 4 50       26 if ($cs == 0){ # greyscale (1,2,4,8 bps, 16 not supported here)
    50          
    100          
    50          
    50          
182             # transparency via tRNS chunk allowed
183             # scanline = ceil(bpc * comp / 8)+1
184 0 0       0 if ($bpc > 8) {
185 0         0 die ">8 bits of greylevel in PNG is not supported.";
186             } else {
187 0         0 $self->filters('FlateDecode');
188 0         0 $self->colorspace('DeviceGray');
189 0         0 $self->bits_per_component($bpc);
190 0         0 my $dict = PDFDict();
191 0         0 $self->{'DecodeParms'} = PDFArray($dict);
192 0         0 $dict->{'Predictor'} = PDFNum(15);
193 0         0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
194 0         0 $dict->{'Colors'} = PDFNum(1);
195 0         0 $dict->{'Columns'} = PDFNum($w);
196 0 0 0     0 if (defined $trns && !$opts{'notrans'}) {
197 0         0 my $m = mMax(unpack('n*', $trns));
198 0         0 my $n = mMin(unpack('n*', $trns));
199 0         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 0       0 if ($bpc > 8) {
205 0         0 die ">8 bits of RGB in PNG is not supported.";
206             } else {
207 0         0 $self->filters('FlateDecode');
208 0         0 $self->colorspace('DeviceRGB');
209 0         0 $self->bits_per_component($bpc);
210 0         0 my $dict = PDFDict();
211 0         0 $self->{'DecodeParms'} = PDFArray($dict);
212 0         0 $dict->{'Predictor'} = PDFNum(15);
213 0         0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
214 0         0 $dict->{'Colors'} = PDFNum(3);
215 0         0 $dict->{'Columns'} = PDFNum($w);
216 0 0 0     0 if (defined $trns && !$opts{'notrans'}) {
217 0         0 my @v = unpack('n*', $trns);
218 0         0 my (@cr,@cg,@cb, $m, $n);
219 0         0 while (scalar @v > 0) {
220 0         0 push(@cr, shift(@v));
221 0         0 push(@cg, shift(@v));
222 0         0 push(@cb, shift(@v));
223             }
224 0         0 @v = ();
225 0         0 $m = mMax(@cr);
226 0         0 $n = mMin(@cr);
227 0         0 push @v, $n,$m;
228 0         0 $m = mMax(@cg);
229 0         0 $n = mMin(@cg);
230 0         0 push @v, $n,$m;
231 0         0 $m = mMax(@cb);
232 0         0 $n = mMin(@cb);
233 0         0 push @v, $n,$m;
234 0         0 $self->{'Mask'} = PDFArray(map { PDFNum($_) } @v);
  0         0  
235             }
236             }
237             } elsif ($cs == 3) { # palette 1,2,4,8 bpp depth (is 16 legal?)
238             # transparency via tRNS chunk allowed
239 2 50       6 if ($bpc > 8) {
240 0         0 die ">8 bits of palette in PNG is not supported.";
241             } else {
242 2         7 my $dict = PDFDict();
243 2         13 $pdf->new_obj($dict);
244 2         7 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
245 2         15 $dict->{' stream'} = $palette;
246 2         16 $palette = "";
247 2         21 $self->filters('FlateDecode');
248 2         7 $self->colorspace(PDFArray(PDFName('Indexed'), PDFName('DeviceRGB'), PDFNum(int(length($dict->{' stream'})/3)-1), $dict));
249 2         12 $self->bits_per_component($bpc);
250 2         5 $dict = PDFDict();
251 2         6 $self->{'DecodeParms'} = PDFArray($dict);
252 2         6 $dict->{'Predictor'} = PDFNum(15);
253 2         5 $dict->{'BitsPerComponent'} = PDFNum($bpc);
254 2         7 $dict->{'Colors'} = PDFNum(1);
255 2         6 $dict->{'Columns'} = PDFNum($w);
256 2 50 33     10 if (defined $trns && !$opts{'notrans'}) {
257 0         0 $trns .= "\xFF" x 256; # pad out with opaque entries to
258             # ensure at least 256 entries available
259 0         0 $dict = PDFDict();
260 0         0 $pdf->new_obj($dict);
261 0         0 $dict->{'Type'} = PDFName('XObject');
262 0         0 $dict->{'Subtype'} = PDFName('Image');
263 0         0 $dict->{'Width'} = PDFNum($w);
264 0         0 $dict->{'Height'} = PDFNum($h);
265 0         0 $dict->{'ColorSpace'} = PDFName('DeviceGray');
266 0         0 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
267             # $dict->{'Filter'} = PDFArray(PDFName('ASCIIHexDecode'));
268 0         0 $dict->{'BitsPerComponent'} = PDFNum(8);
269 0         0 $self->{'SMask'} = $dict;
270             # length of row (scanline) in bytes, plus 1
271 0         0 my $scanline = 1 + ceil($bpc * $w/8);
272             # bytes per pixel (always 1)
273 0         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         0 my $clearstream = unprocess($bpc, $bpp, 1, $w,$h, $scanline, \$self->{' stream'});
277 0         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         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 0       0 if ($bpc > 8) {
292 0         0 die ">8 bits of greylevel+alpha in PNG is not supported.";
293             } else {
294 0         0 $self->filters('FlateDecode');
295 0         0 $self->colorspace('DeviceGray');
296 0         0 $self->bits_per_component($bpc);
297 0         0 my $dict = PDFDict();
298 0         0 $self->{'DecodeParms'} = PDFArray($dict);
299             # $dict->{'Predictor'} = PDFNum(15);
300 0         0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
301 0         0 $dict->{'Colors'} = PDFNum(1);
302 0         0 $dict->{'Columns'} = PDFNum($w);
303              
304 0         0 $dict = PDFDict();
305 0 0       0 unless ($opts{'notrans'}) {
306 0         0 $pdf->new_obj($dict);
307 0         0 $dict->{'Type'} = PDFName('XObject');
308 0         0 $dict->{'Subtype'} = PDFName('Image');
309 0         0 $dict->{'Width'} = PDFNum($w);
310 0         0 $dict->{'Height'} = PDFNum($h);
311 0         0 $dict->{'ColorSpace'} = PDFName('DeviceGray');
312 0         0 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
313 0         0 $dict->{'BitsPerComponent'} = PDFNum($bpc);
314 0         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         0 my $scanline = 1 + ceil($bpc*2 * $w/8);
319 0         0 my $bpp = ceil($bpc*2 / 8);
320 0         0 my $clearstream = unprocess($bpc, $bpp, 2, $w,$h, $scanline, \$self->{' stream'});
321 0         0 delete $self->{' nofilt'};
322             #delete $self->{' stream'};
323 0         0 $dict->{' stream'} = '';
324 0         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         0 foreach my $n (0 .. $h*$w-1) {
331 0         0 vec($dict->{' stream'}, $n, $bpc) = vec($clearstream, $n*2+1, $bpc);
332 0         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 50       9 if ($bpc > 8) {
338 0         0 die ">8 bits of RGB+alpha in PNG is not supported.";
339             } else {
340 2         12 $self->filters('FlateDecode');
341 2         11 $self->colorspace('DeviceRGB');
342 2         10 $self->bits_per_component($bpc);
343 2         8 my $dict = PDFDict();
344 2         8 $self->{'DecodeParms'} = PDFArray($dict);
345             # $dict->{'Predictor'} = PDFNum(15);
346 2         9 $dict->{'BitsPerComponent'} = PDFNum($bpc);
347 2         6 $dict->{'Colors'} = PDFNum(3);
348 2         6 $dict->{'Columns'} = PDFNum($w);
349              
350 2         6 $dict = PDFDict();
351 2 50       9 unless ($opts{'notrans'}) {
352 2         12 $pdf->new_obj($dict);
353 2         9 $dict->{'Type'} = PDFName('XObject');
354 2         7 $dict->{'Subtype'} = PDFName('Image');
355 2         7 $dict->{'Width'} = PDFNum($w);
356 2         7 $dict->{'Height'} = PDFNum($h);
357 2         7 $dict->{'ColorSpace'} = PDFName('DeviceGray');
358 2         5 $dict->{'Filter'} = PDFArray(PDFName('FlateDecode'));
359 2         8 $dict->{'BitsPerComponent'} = PDFNum($bpc);
360 2         7 $self->{'SMask'} = $dict;
361             }
362             # bytes per pixel (4 samples) and length of row scanline in bytes
363 2         142 my $scanline = 1 + ceil($bpc*4 * $w/8);
364 2         10 my $bpp = ceil($bpc*4 /8);
365             # unpacked, uncompressed, unfiltered image data
366 2         55 my $clearstream = unprocess($bpc, $bpp, 4, $w,$h, $scanline, \$self->{' stream'});
367 2         21 delete $self->{' nofilt'};
368             #delete $self->{' stream'};
369 2         13 $dict->{' stream'} = '';
370 2         8 $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         13 foreach my $n (0 .. ($h*$w)-1) {
379             # pull out Alpha data bpc bits into new dict SMask
380 218120         475021 vec($dict->{' stream'}, $n, $bpc) = vec($clearstream, $n*4+3, $bpc);
381             # transfer RGB triplet into self->stream
382 218120         493164 vec($self->{' stream'}, $n*3, $bpc) = vec($clearstream, $n*4, $bpc);
383 218120         486118 vec($self->{' stream'}, $n*3+1, $bpc) = vec($clearstream, $n*4+1, $bpc);
384 218120         551396 vec($self->{' stream'}, $n*3+2, $bpc) = vec($clearstream, $n*4+2, $bpc);
385             }
386             }
387             } else {
388 0         0 die "unsupported PNG-color type (cs=$cs).";
389             }
390              
391 4         51 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             sub usesLib {
414 3     3 1 31 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         16 return $self->{'usesIPL'}->val();
418             }
419              
420             sub PaethPredictor {
421 94240     94240 0 257295 my ($a, $b, $c) = @_;
422 94240         160812 my $p = $a + $b - $c;
423 94240         147435 my $pa = abs($p - $a);
424 94240         149295 my $pb = abs($p - $b);
425 94240         151030 my $pc = abs($p - $c);
426 94240 100 100     290802 if (($pa <= $pb) && ($pa <= $pc)) {
    100          
427 82304         295659 return $a;
428             } elsif ($pb <= $pc) {
429 11154         36868 return $b;
430             } else {
431 782         2879 return $c;
432             }
433             }
434              
435             sub unprocess {
436 2     2 0 8 my ($bpc, $bpp, $comp, $width,$height, $scanline, $sstream) = @_;
437              
438 2         16 my $stream = uncompress($$sstream);
439 2         11114 my $prev = '';
440 2         8 my $clearstream = '';
441 2         11 foreach my $n (0 .. $height-1) {
442             # print STDERR "line $n:";
443 574         3784 my $line = substr($stream, $n*$scanline, $scanline);
444 574         1736 my $filter = vec($line, 0, 8);
445 574         1595 my $clear = '';
446 574         1720 $line = substr($line, 1);
447             # print STDERR " filter=$filter";
448 574 50       4240 if ($filter == 0) {
    100          
    100          
    50          
    50          
449 0         0 $clear = $line;
450             } elsif ($filter == 1) {
451 18         61 foreach my $x (0 .. length($line)-1) {
452 27360         73283 vec($clear, $x, 8) = (vec($line, $x, 8) + vec($clear, $x-$bpp, 8))%256;
453             }
454             } elsif ($filter == 2) {
455 494         1852 foreach my $x (0 .. length($line)-1) {
456 750880         1971803 vec($clear, $x, 8) = (vec($line, $x, 8) + vec($prev, $x, 8))%256;
457             }
458             } elsif ($filter == 3) {
459 0         0 foreach my $x (0 .. length($line)-1) {
460 0         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         239 foreach my $x (0 .. length($line)-1) {
464 94240         277145 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         1720 $prev = $clear;
468 574         2170 foreach my $x (0 .. ($width*$comp)-1) {
469 872480         2146136 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         1493 return $clearstream;
475             }
476              
477             1;
478              
479             __END__
480              
481             RFC 2083
482             PNG: Portable Network Graphics
483             January 1997
484              
485              
486             4.1.3. IDAT Image data
487              
488             The IDAT chunk contains the actual image data. To create this
489             data:
490              
491             * Begin with image scanlines represented as described in
492             Image layout (Section 2.3); the layout and total size of
493             this raw data are determined by the fields of IHDR.
494             * Filter the image data according to the filtering method
495             specified by the IHDR chunk. (Note that with filter
496             method 0, the only one currently defined, this implies
497             prepending a filter type byte to each scanline.)
498             * Compress the filtered data using the compression method
499             specified by the IHDR chunk.
500              
501             The IDAT chunk contains the output datastream of the compression
502             algorithm.
503              
504             To read the image data, reverse this process.
505              
506             There can be multiple IDAT chunks; if so, they must appear
507             consecutively with no other intervening chunks. The compressed
508             datastream is then the concatenation of the contents of all the
509             IDAT chunks. The encoder can divide the compressed datastream
510             into IDAT chunks however it wishes. (Multiple IDAT chunks are
511             allowed so that encoders can work in a fixed amount of memory;
512             typically the chunk size will correspond to the encoder's buffer
513             size.) It is important to emphasize that IDAT chunk boundaries
514             have no semantic significance and can occur at any point in the
515             compressed datastream. A PNG file in which each IDAT chunk
516             contains only one data byte is legal, though remarkably wasteful
517             of space. (For that matter, zero-length IDAT chunks are legal,
518             though even more wasteful.)
519              
520              
521             4.2.9. tRNS Transparency
522              
523             The tRNS chunk specifies that the image uses simple
524             transparency: either alpha values associated with palette
525             entries (for indexed-color images) or a single transparent
526             color (for grayscale and truecolor images). Although simple
527             transparency is not as elegant as the full alpha channel, it
528             requires less storage space and is sufficient for many common
529             cases.
530              
531             For color type 3 (indexed color), the tRNS chunk contains a
532             series of one-byte alpha values, corresponding to entries in
533             the PLTE chunk:
534              
535             Alpha for palette index 0: 1 byte
536             Alpha for palette index 1: 1 byte
537             ... etc ...
538              
539             Each entry indicates that pixels of the corresponding palette
540             index must be treated as having the specified alpha value.
541             Alpha values have the same interpretation as in an 8-bit full
542             alpha channel: 0 is fully transparent, 255 is fully opaque,
543             regardless of image bit depth. The tRNS chunk must not contain
544             more alpha values than there are palette entries, but tRNS can
545             contain fewer values than there are palette entries. In this
546             case, the alpha value for all remaining palette entries is
547             assumed to be 255. In the common case in which only palette
548             index 0 need be made transparent, only a one-byte tRNS chunk is
549             needed.
550              
551             For color type 0 (grayscale), the tRNS chunk contains a single
552             gray level value, stored in the format:
553              
554             Gray: 2 bytes, range 0 .. (2^bitdepth)-1
555              
556             (For consistency, 2 bytes are used regardless of the image bit
557             depth.) Pixels of the specified gray level are to be treated as
558             transparent (equivalent to alpha value 0); all other pixels are
559             to be treated as fully opaque (alpha value (2^bitdepth)-1).
560              
561             For color type 2 (truecolor), the tRNS chunk contains a single
562             RGB color value, stored in the format:
563              
564             Red: 2 bytes, range 0 .. (2^bitdepth)-1
565             Green: 2 bytes, range 0 .. (2^bitdepth)-1
566             Blue: 2 bytes, range 0 .. (2^bitdepth)-1
567              
568             (For consistency, 2 bytes per sample are used regardless of the
569             image bit depth.) Pixels of the specified color value are to be
570             treated as transparent (equivalent to alpha value 0); all other
571             pixels are to be treated as fully opaque (alpha value
572             2^bitdepth)-1).
573              
574             tRNS is prohibited for color types 4 and 6, since a full alpha
575             channel is already present in those cases.
576              
577             Note: when dealing with 16-bit grayscale or truecolor data, it
578             is important to compare both bytes of the sample values to
579             determine whether a pixel is transparent. Although decoders
580             may drop the low-order byte of the samples for display, this
581             must not occur until after the data has been tested for
582             transparency. For example, if the grayscale level 0x0001 is
583             specified to be transparent, it would be incorrect to compare
584             only the high-order byte and decide that 0x0002 is also
585             transparent.
586              
587             When present, the tRNS chunk must precede the first IDAT chunk,
588             and must follow the PLTE chunk, if any.
589              
590              
591             6. Filter Algorithms
592              
593             This chapter describes the filter algorithms that can be applied
594             before compression. The purpose of these filters is to prepare the
595             image data for optimum compression.
596              
597              
598             6.1. Filter types
599              
600             PNG filter method 0 defines five basic filter types:
601              
602             Type Name
603              
604             0 None
605             1 Sub
606             2 Up
607             3 Average
608             4 Paeth
609              
610             (Note that filter method 0 in IHDR specifies exactly this set of
611             five filter types. If the set of filter types is ever extended, a
612             different filter method number will be assigned to the extended
613             set, so that decoders need not decompress the data to discover
614             that it contains unsupported filter types.)
615              
616             The encoder can choose which of these filter algorithms to apply
617             on a scanline-by-scanline basis. In the image data sent to the
618             compression step, each scanline is preceded by a filter type byte
619             that specifies the filter algorithm used for that scanline.
620              
621             Filtering algorithms are applied to bytes, not to pixels,
622             regardless of the bit depth or color type of the image. The
623             filtering algorithms work on the byte sequence formed by a
624             scanline that has been represented as described in Image layout
625             (Section 2.3). If the image includes an alpha channel, the alpha
626             data is filtered in the same way as the image data.
627              
628             When the image is interlaced, each pass of the interlace pattern
629             is treated as an independent image for filtering purposes. The
630             filters work on the byte sequences formed by the pixels actually
631             transmitted during a pass, and the "previous scanline" is the one
632             previously transmitted in the same pass, not the one adjacent in
633             the complete image. Note that the subimage transmitted in any one
634             pass is always rectangular, but is of smaller width and/or height
635             than the complete image. Filtering is not applied when this
636             subimage is empty.
637              
638             For all filters, the bytes "to the left of" the first pixel in a
639             scanline must be treated as being zero. For filters that refer to
640             the prior scanline, the entire prior scanline must be treated as
641             being zeroes for the first scanline of an image (or of a pass of
642             an interlaced image).
643              
644             To reverse the effect of a filter, the decoder must use the
645             decoded values of the prior pixel on the same line, the pixel
646             immediately above the current pixel on the prior line, and the
647             pixel just to the left of the pixel above. This implies that at
648             least one scanline's worth of image data will have to be stored by
649             the decoder at all times. Even though some filter types do not
650             refer to the prior scanline, the decoder will always need to store
651             each scanline as it is decoded, since the next scanline might use
652             a filter that refers to it.
653              
654             PNG imposes no restriction on which filter types can be applied to
655             an image. However, the filters are not equally effective on all
656             types of data. See Recommendations for Encoders: Filter selection
657             (Section 9.6).
658              
659             See also Rationale: Filtering (Section 12.9).
660              
661              
662              
663             6.2. Filter type 0: None
664              
665             With the None filter, the scanline is transmitted unmodified; it
666             is only necessary to insert a filter type byte before the data.
667              
668              
669             6.3. Filter type 1: Sub
670              
671             The Sub filter transmits the difference between each byte and the
672             value of the corresponding byte of the prior pixel.
673              
674             To compute the Sub filter, apply the following formula to each
675             byte of the scanline:
676              
677             Sub(x) = Raw(x) - Raw(x-bpp)
678              
679             where x ranges from zero to the number of bytes representing the
680             scanline minus one, Raw(x) refers to the raw data byte at that
681             byte position in the scanline, and bpp is defined as the number of
682             bytes per complete pixel, rounding up to one. For example, for
683             color type 2 with a bit depth of 16, bpp is equal to 6 (three
684             samples, two bytes per sample); for color type 0 with a bit depth
685             of 2, bpp is equal to 1 (rounding up); for color type 4 with a bit
686             depth of 16, bpp is equal to 4 (two-byte grayscale sample, plus
687             two-byte alpha sample).
688              
689             Note this computation is done for each byte, regardless of bit
690             depth. In a 16-bit image, each MSB is predicted from the
691             preceding MSB and each LSB from the preceding LSB, because of the
692             way that bpp is defined.
693              
694             Unsigned arithmetic modulo 256 is used, so that both the inputs
695             and outputs fit into bytes. The sequence of Sub values is
696             transmitted as the filtered scanline.
697              
698             For all x < 0, assume Raw(x) = 0.
699              
700             To reverse the effect of the Sub filter after decompression,
701             output the following value:
702              
703             Sub(x) + Raw(x-bpp)
704              
705             (computed mod 256), where Raw refers to the bytes already decoded.
706              
707              
708             6.4. Filter type 2: Up
709              
710             The Up filter is just like the Sub filter except that the pixel
711             immediately above the current pixel, rather than just to its left,
712             is used as the predictor.
713              
714             To compute the Up filter, apply the following formula to each byte
715             of the scanline:
716              
717             Up(x) = Raw(x) - Prior(x)
718              
719             where x ranges from zero to the number of bytes representing the
720             scanline minus one, Raw(x) refers to the raw data byte at that
721             byte position in the scanline, and Prior(x) refers to the
722             unfiltered bytes of the prior scanline.
723              
724             Note this is done for each byte, regardless of bit depth.
725             Unsigned arithmetic modulo 256 is used, so that both the inputs
726             and outputs fit into bytes. The sequence of Up values is
727             transmitted as the filtered scanline.
728              
729             On the first scanline of an image (or of a pass of an interlaced
730             image), assume Prior(x) = 0 for all x.
731              
732             To reverse the effect of the Up filter after decompression, output
733             the following value:
734              
735             Up(x) + Prior(x)
736              
737             (computed mod 256), where Prior refers to the decoded bytes of the
738             prior scanline.
739              
740              
741             6.5. Filter type 3: Average
742              
743             The Average filter uses the average of the two neighboring pixels
744             (left and above) to predict the value of a pixel.
745              
746             To compute the Average filter, apply the following formula to each
747             byte of the scanline:
748              
749             Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2)
750              
751             where x ranges from zero to the number of bytes representing the
752             scanline minus one, Raw(x) refers to the raw data byte at that
753             byte position in the scanline, Prior(x) refers to the unfiltered
754             bytes of the prior scanline, and bpp is defined as for the Sub
755             filter.
756              
757             Note this is done for each byte, regardless of bit depth. The
758             sequence of Average values is transmitted as the filtered
759             scanline.
760              
761             The subtraction of the predicted value from the raw byte must be
762             done modulo 256, so that both the inputs and outputs fit into
763             bytes. However, the sum Raw(x-bpp)+Prior(x) must be formed
764             without overflow (using at least nine-bit arithmetic). floor()
765             indicates that the result of the division is rounded to the next
766             lower integer if fractional; in other words, it is an integer
767             division or right shift operation.
768              
769             For all x < 0, assume Raw(x) = 0. On the first scanline of an
770             image (or of a pass of an interlaced image), assume Prior(x) = 0
771             for all x.
772              
773             To reverse the effect of the Average filter after decompression,
774             output the following value:
775              
776             Average(x) + floor((Raw(x-bpp)+Prior(x))/2)
777              
778             where the result is computed mod 256, but the prediction is
779             calculated in the same way as for encoding. Raw refers to the
780             bytes already decoded, and Prior refers to the decoded bytes of
781             the prior scanline.
782              
783              
784             6.6. Filter type 4: Paeth
785              
786             The Paeth filter computes a simple linear function of the three
787             neighboring pixels (left, above, upper left), then chooses as
788             predictor the neighboring pixel closest to the computed value.
789             This technique is due to Alan W. Paeth [PAETH].
790              
791             To compute the Paeth filter, apply the following formula to each
792             byte of the scanline:
793              
794             Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
795              
796             where x ranges from zero to the number of bytes representing the
797             scanline minus one, Raw(x) refers to the raw data byte at that
798             byte position in the scanline, Prior(x) refers to the unfiltered
799             bytes of the prior scanline, and bpp is defined as for the Sub
800             filter.
801              
802             Note this is done for each byte, regardless of bit depth.
803             Unsigned arithmetic modulo 256 is used, so that both the inputs
804             and outputs fit into bytes. The sequence of Paeth values is
805             transmitted as the filtered scanline.
806              
807             The PaethPredictor function is defined by the following
808             pseudocode:
809              
810             function PaethPredictor (a, b, c)
811             begin
812             ; a = left, b = above, c = upper left
813             p := a + b - c ; initial estimate
814             pa := abs(p - a) ; distances to a, b, c
815             pb := abs(p - b)
816             pc := abs(p - c)
817             ; return nearest of a,b,c,
818             ; breaking ties in order a,b,c.
819             if pa <= pb AND pa <= pc then return a
820             else if pb <= pc then return b
821             else return c
822             end
823              
824             The calculations within the PaethPredictor function must be
825             performed exactly, without overflow. Arithmetic modulo 256 is to
826             be used only for the final step of subtracting the function result
827             from the target byte value.
828              
829             Note that the order in which ties are broken is critical and must
830             not be altered. The tie break order is: pixel to the left, pixel
831             above, pixel to the upper left. (This order differs from that
832             given in Paeth's article.)
833              
834             For all x < 0, assume Raw(x) = 0 and Prior(x) = 0. On the first
835             scanline of an image (or of a pass of an interlaced image), assume
836             Prior(x) = 0 for all x.
837              
838             To reverse the effect of the Paeth filter after decompression,
839             output the following value:
840              
841             Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
842              
843             (computed mod 256), where Raw and Prior refer to bytes already
844             decoded. Exactly the same PaethPredictor function is used by both
845             encoder and decoder.