| 1 | package PDF::Builder::Resource::XObject::Image::PNG_IPL; | ||
| 2 | |||
| 3 | use base 'PDF::Builder::Resource::XObject::Image'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '3.028'; # VERSION | ||
| 9 | our $LAST_UPDATE = '3.027'; # manually update whenever code is changed | ||
| 10 | |||
| 11 | use Compress::Zlib; | ||
| 12 | use POSIX qw(ceil floor); | ||
| 13 | |||
| 14 | use IO::File; | ||
| 15 | use PDF::Builder::Util; | ||
| 16 | use PDF::Builder::Basic::PDF::Utils; | ||
| 17 | use Image::PNG::Libpng ':all'; # have already confirmed that this exists | ||
| 18 | use Image::PNG::Const ':all'; | ||
| 19 | use Scalar::Util qw(weaken); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | PDF::Builder::Resource::XObject::Image::PNG_IPL - Support routines for PNG | ||
| 24 | image library (using Image::PNG::Libpng) | ||
| 25 | |||
| 26 | Inherits from L<PDF::Builder::Resource::XObject::Image> | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 new | ||
| 31 | |||
| 32 | $res = PDF::Builder::Resource::XObject::Image::PNG_IPL->new($pdf, $file, %opts) | ||
| 33 | |||
| 34 | =over | ||
| 35 | |||
| 36 | Returns a PNG-image object. C<$pdf> is the PDF object being added to, C<$file> | ||
| 37 | is the input PNG file, and the optional C<$name> of the new parent image object | ||
| 38 | defaults to PxAAA. | ||
| 39 | |||
| 40 | If the Image::PNG::Libpng package is installed, and its use is not suppressed | ||
| 41 | via the C<nouseIPL> flag (see Builder documentation for C<image_png>), the | ||
| 42 | PNG_IPL library will be used. Otherwise, the PNG library will be used instead. | ||
| 43 | |||
| 44 | B<opts:> | ||
| 45 | |||
| 46 | =over | ||
| 47 | |||
| 48 | =item 'notrans' => 1 | ||
| 49 | |||
| 50 | No transparency -- ignore tRNS chunk if provided, ignore Alpha channel | ||
| 51 | if provided. | ||
| 52 | |||
| 53 | =item 'force8bps' => 1 | ||
| 54 | |||
| 55 | If the PNG source is 16bps, tell the libpng library to strip down all | ||
| 56 | channels to 8bps, permitting use on PDF 1.4 output. | ||
| 57 | |||
| 58 | =item 'name' => 'string' | ||
| 59 | |||
| 60 | This is the name you can give for the PNG image object. The default is Pxnnnn. | ||
| 61 | |||
| 62 | =back | ||
| 63 | |||
| 64 | Remember that you need to use Builder.pm's image_png to process PNG images. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =head2 Supported PNG types | ||
| 69 | |||
| 70 | (0) Gray scale of depth 1, 2, 4, 8, or 16 bits per pixel (2, 4, 16, 256, | ||
| 71 | or 65536 gray levels). Full transparency (of one 16-bit gray value) | ||
| 72 | via the tRNS chunk is allowed, unless the notrans option specifies | ||
| 73 | that it be ignored. | ||
| 74 | |||
| 75 | (2) RGB truecolor with 8 or 16 bits per sample (3 samples: 16.7 million | ||
| 76 | or 281.5 trillion colors). Full transparency (of one 3x16-bit RGB | ||
| 77 | color value) via the tRNS chunk is allowed, unless the notrans | ||
| 78 | option specifies that it be ignored. | ||
| 79 | |||
| 80 | (3) Palette color with 1, 2, 4, or 8 bits per pixel (2, 4, 16, or 256 | ||
| 81 | color table/palette entries). 16 bpp is not currently supported by | ||
| 82 | PNG or PDF. Partial transparency (8-bit Alpha) for each palette | ||
| 83 | entry via the tRNS chunk is allowed, unless the notrans option | ||
| 84 | specifies that it be ignored (all entries fully opaque). | ||
| 85 | |||
| 86 | (4) Gray scale of depth 8 or 16 bits per pixel plus equal-sized Alpha | ||
| 87 | channel (256 or 65536 gray levels and 256 or 65536 levels of | ||
| 88 | transparency). The Alpha channel is ignored if the notrans | ||
| 89 | option is given. The tRNS chunk is not permitted. | ||
| 90 | |||
| 91 | (5) B<RESERVED> for grayscale via palette + Alpha channel | ||
| 92 | |||
| 93 | (6) RGB truecolor with 8 or 16 bits per sample, with equal-sized | ||
| 94 | Alpha channel (256 or 65536 levels of transparency). The Alpha | ||
| 95 | channel is ignored if the notrans option is given. The tRNS | ||
| 96 | chunk is not permitted. | ||
| 97 | |||
| 98 | (7) B<RESERVED> for truecolor via palette + Alpha channel | ||
| 99 | |||
| 100 | In all cases, 16 bits per sample forces PDF 1.5 (or higher) output, unless | ||
| 101 | you give the C<force8bps> option, to "strip" 16 bit samples to 8 bits, and | ||
| 102 | permit PDF 1.4-compatible output. | ||
| 103 | The libpng.a library is assuming standard "network" bit and | ||
| 104 | byte ordering (Big Endian), although flags might be added to change this. | ||
| 105 | |||
| 106 | The transparency chunk (tRNS) will specify one gray level entry or one RGB | ||
| 107 | entry to be treated as transparent (Alpha = 0). For palette color, up to | ||
| 108 | 256 palette entry 8-bit Alpha values are specified (256 levels of transparency, | ||
| 109 | from 0 = transparent to 255 = opaque). | ||
| 110 | |||
| 111 | Only a limited number of chunks are handled: IHDR, IDAT (internally), PLTE, | ||
| 112 | tRNS, and IEND (internally). All other chunks are ignored at this time. Filters | ||
| 113 | and compression applied to data is handled internally by libpng.a -- there may | ||
| 114 | be unsupported methods. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | # TBD: gAMA (gamma) chunk, perhaps some others? | ||
| 119 | |||
| 120 | sub new { | ||
| 121 | my ($class, $pdf, $file, %opts) = @_; | ||
| 122 | # copy dashed option names to preferred undashed names | ||
| 123 | if (defined $opts{'-nouseIPL'} && !defined $opts{'nouseIPL'}) { $opts{'nouseIPL'} = delete($opts{'-nouseIPL'}); } | ||
| 124 | if (defined $opts{'-name'} && !defined $opts{'name'}) { $opts{'name'} = delete($opts{'-name'}); } | ||
| 125 | if (defined $opts{'-compress'} && !defined $opts{'compress'}) { $opts{'compress'} = delete($opts{'-compress'}); } | ||
| 126 | if (defined $opts{'-notrans'} && !defined $opts{'notrans'}) { $opts{'notrans'} = delete($opts{'-notrans'}); } | ||
| 127 | if (defined $opts{'-force8bps'} && !defined $opts{'force8bps'}) { $opts{'force8bps'} = delete($opts{'-force8bps'}); } | ||
| 128 | |||
| 129 | my ($name, $compress); | ||
| 130 | if (exists $opts{'name'}) { $name = $opts{'name'}; } | ||
| 131 | #if (exists $opts{'compress'}) { $compress = $opts{'compress'}; } | ||
| 132 | |||
| 133 | my $self; | ||
| 134 | |||
| 135 | $class = ref($class) if ref($class); | ||
| 136 | |||
| 137 | $self = $class->SUPER::new($pdf, $name || 'Px'.pdfkey()); | ||
| 138 | $pdf->new_obj($self) unless $self->is_obj($pdf); | ||
| 139 | |||
| 140 | $self->{' apipdf'} = $pdf; | ||
| 141 | weaken $self->{' apipdf'}; | ||
| 142 | |||
| 143 | my $fh = IO::File->new(); | ||
| 144 | my $png = create_read_struct(); | ||
| 145 | if (ref($file)) { | ||
| 146 | $fh = $file; | ||
| 147 | } else { | ||
| 148 | open $fh, '<', $file or die "$!: $file"; | ||
| 149 | } | ||
| 150 | if (ref($fh) ne 'SCALAR') { | ||
| 151 | binmode($fh, ':raw'); | ||
| 152 | } | ||
| 153 | |||
| 154 | my ($w,$h, $bpc, $cs, $im, $palette, $trns); | ||
| 155 | $self->{' stream'} = ''; | ||
| 156 | $self->{' nofilt'} = 1; | ||
| 157 | |||
| 158 | # slurp whole PNG file into $png structure | ||
| 159 | my $xform = PNG_TRANSFORM_IDENTITY; # default bit flag (0) | ||
| 160 | my $transparency = 1; # default YES, allow transparency/Alpha | ||
| 161 | if ($opts{'notrans'}) { | ||
| 162 | $transparency = 0; | ||
| 163 | $xform |= PNG_TRANSFORM_STRIP_ALPHA; | ||
| 164 | # this appears to turn cs=4 into cs=0, and cs=6 into cs=2 | ||
| 165 | } | ||
| 166 | if ($opts{'force8bps'}) { | ||
| 167 | $xform |= PNG_TRANSFORM_STRIP_16; | ||
| 168 | # this reduces 16bps channels to 8bps | ||
| 169 | } | ||
| 170 | |||
| 171 | # IO::String form "filehandle" from SVG doesn't play well with IPL | ||
| 172 | # (wants C FILE *fh) | ||
| 173 | if (ref($fh) eq 'IO::String') { | ||
| 174 | # ref to GLOB (IO::String creation) | ||
| 175 | $png = read_from_scalar(${$fh->string_ref()}); | ||
| 176 | $png->set_transforms($xform); | ||
| 177 | } elsif (ref($fh) eq 'IO::File' || ref($fh) eq 'GLOB') { | ||
| 178 | # an opened filename | ||
| 179 | $png->init_io($fh); | ||
| 180 | $png->read_png($xform); | ||
| 181 | } elsif (ref($fh) eq 'SCALAR') { | ||
| 182 | # scalar ref with actual data | ||
| 183 | $png = read_from_scalar($$fh); | ||
| 184 | $png->set_transforms($xform); | ||
| 185 | } else { | ||
| 186 | die("unhandled input to LibPNG: ".ref($fh)); | ||
| 187 | } | ||
| 188 | if (ref($fh) ne 'SCALAR') { | ||
| 189 | close($fh); | ||
| 190 | } | ||
| 191 | |||
| 192 | # what chunks are available? | ||
| 193 | my $valid = $png->get_valid(); | ||
| 194 | # IHDR one must exist and be first | ||
| 195 | # PLTE one must exist if ColorType 3, and be before IDATs | ||
| 196 | # tRNS is optional, must be after PLTE and before IDAT, not allowed if | ||
| 197 | # Alpha channel is given | ||
| 198 | # IDAT one or more must exist and be consecutive, obtain with get_rows() | ||
| 199 | # any other chunk is just ignored for now | ||
| 200 | # TBD: gAMA (gamma) consider doing, if implemented in PDF | ||
| 201 | # IEND one must exist and be last (internally handled) | ||
| 202 | |||
| 203 | # header (IHDR) | ||
| 204 | # there had BETTER be a header chunk! | ||
| 205 | my $IHDR = $png->get_IHDR(); # hash ref for header data | ||
| 206 | $w = $IHDR->{'width'}; | ||
| 207 | $h = $IHDR->{'height'}; | ||
| 208 | |||
| 209 | $bpc = $IHDR->{'bit_depth'}; # bits-per-channel | ||
| 210 | if ($bpc > 8) { | ||
| 211 | $PDF::Builder::global_pdf->verCheckOutput(1.5, "image sample depth > 8 bits"); | ||
| 212 | # if don't want to allow > 8 bits, can use force8bps | ||
| 213 | # if later PDFs allow other depths > 8, give them their own test | ||
| 214 | } | ||
| 215 | |||
| 216 | $im = $IHDR->{'interlace_method'}; | ||
| 217 | # im = 0 : PNG_INTERLACE_NONE | ||
| 218 | # im = 1 : PNG_INTERLACE_ADAM7 | ||
| 219 | #if ($IHDR->{'interlace_method'} != PNG_INTERLACE_NONE) { | ||
| 220 | # die "Unsupported interlace method $im (must be NONE)\n"; | ||
| 221 | #} | ||
| 222 | # we don't care what the original interlacing was, as the data should be | ||
| 223 | # arranged in the non-interlaced order by the time we see it. | ||
| 224 | |||
| 225 | $cs = $IHDR->{'color_type'}; | ||
| 226 | #print "\ncs (color type) = $cs\n"; # if Alpha stripped, is cs-4 | ||
| 227 | # cs = 0 : PNG_COLOR_TYPE_GRAY | ||
| 228 | # cs = 1 : reserved for grayscale via palette | ||
| 229 | # cs = 2 : PNG_COLOR_TYPE_RGB (truecolor) | ||
| 230 | # cs = 3 : PNG_COLOR_TYPE_PALETTE (truecolor + palette) | ||
| 231 | # cs = 4 : PNG_COLOR_TYPE_GRAY_ALPHA | ||
| 232 | # cs = 5 : reserved for grayscale via palette + Alpha channel | ||
| 233 | # cs = 6 : PNG_COLOR_TYPE_RGB_ALPHA | ||
| 234 | # cs = 7 : reserved for truecolor via palette + Alpha channel | ||
| 235 | |||
| 236 | # compression method ($cm), filter ($fm) method not returned. | ||
| 237 | # supposedly they were never implemented, and should always be 0. | ||
| 238 | |||
| 239 | # palette (PLTE) if given and valid | ||
| 240 | if ($valid->{'PLTE'}) { # should only see for palette color type (3)! | ||
| 241 | my $palette_ref = $png->get_PLTE(); | ||
| 242 | # should be an arrayref of hashes for 'red', 'green', 'blue' | ||
| 243 | # is 3 bytes (red, green, blue) one set per palette entry (1..256) | ||
| 244 | # convert into format PDF understands | ||
| 245 | # old code: count of bytes read into $palette as string | ||
| 246 | my @pal_array = (); | ||
| 247 | for (my $i=0; $i<@{$palette_ref}; $i++) { | ||
| 248 | push @pal_array, $palette_ref->[$i]->{'red'}; | ||
| 249 | push @pal_array, $palette_ref->[$i]->{'green'}; | ||
| 250 | push @pal_array, $palette_ref->[$i]->{'blue'}; | ||
| 251 | } | ||
| 252 | # there should be 3N bytes to pack, N=2,4,16, or 256 entries | ||
| 253 | $palette = pack('C*', @pal_array); | ||
| 254 | if (length($palette)/3 != int(length($palette)/3)) { | ||
| 255 | die "Palette read (length ".length($palette).") that was not 3N bytes long.\n"; | ||
| 256 | } | ||
| 257 | if (length($palette)/3 > 1<<$bpc) { | ||
| 258 | warn "Palette read with ".(length($palette)/3)." entries, when maximum of ".(1<<$bpc)." were expected.\n"; | ||
| 259 | } | ||
| 260 | #print "palette is ".length($palette)." bytes long, expect ".(3*2**$bpc)."\n"; | ||
| 261 | } | ||
| 262 | |||
| 263 | # transparency chunk (tRNS) if given and valid | ||
| 264 | # for cs=0 (gray) or cs=2 (RGB) it is the entry to be made transparent | ||
| 265 | # 16-bit or 48-bit value(s), regardless of $bpc | ||
| 266 | # for cs=3 (palette), it is 8-bit Alpha for each palette entry | ||
| 267 | # Alpha: 0 = transparent, 2**bpc -1 (e.g., 255) is opaque | ||
| 268 | my $tRNS_available = 0; | ||
| 269 | if ($valid->{'tRNS'}) { | ||
| 270 | $tRNS_available = 1; | ||
| 271 | $trns = $png->get_tRNS(); | ||
| 272 | #print "tRNS chunk found, is ".length($trns)." bytes long\n"; | ||
| 273 | # convert into format PDF understands | ||
| 274 | # Gray (cs=0) 16-bit gray value to replace by transparent pixel | ||
| 275 | # expects string to unpack, get min and max values into Mask | ||
| 276 | # according to PNG spec, it should be a single value | ||
| 277 | # according to Libpng, $trns->{gray} is value to use (hash) | ||
| 278 | # RGB (cs=2) expects a 3 element array (red, green, blue entries) | ||
| 279 | # get min and max of each primary color. PNG only supplies 3 16-bit | ||
| 280 | # values (red, green, blue) to indicate one truecolor to make | ||
| 281 | # transparent (hash $trns->red, ->green, ->blue) | ||
| 282 | # Palette (cs=3) expects an array of 8-bit entries, one per palette | ||
| 283 | # entry, giving the Alpha value 0=transparent to 255=opaque for that | ||
| 284 | # entry. Unlike Gray and RGB, we are NOT selecting one pixel value | ||
| 285 | # to be fully transparent and the rest opaque. $trns is an array ref. | ||
| 286 | } | ||
| 287 | |||
| 288 | # transfer over the unpacked (uncompressed, unfiltered) data rows (IDAT) | ||
| 289 | # to self->{' stream'}. stream is already initialized to empty | ||
| 290 | # note that this loop is eliminated for RGBA and GA, as they | ||
| 291 | # are read directly from $png via split_alpha | ||
| 292 | if ($cs != PNG_COLOR_TYPE_GRAY_ALPHA && | ||
| 293 | $cs != PNG_COLOR_TYPE_RGB_ALPHA) { | ||
| 294 | my $rows = $png->get_rows(); | ||
| 295 | for (my $row = 0; $row < @{$rows}; $row++) { | ||
| 296 | $self->{' stream'} .= $rows->[$row]; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | |||
| 300 | $self->width($w); | ||
| 301 | $self->height($h); | ||
| 302 | |||
| 303 | # ColorType fields (cs value) | ||
| 304 | # bit 0: 0 = actual value given, 1 (1) = palette index given | ||
| 305 | # bit 1: 0 = grayscale, 1 (2) = truecolor RGB | ||
| 306 | # bit 2: 0 = no Alpha channel, tRNS chunk allowed | ||
| 307 | # 1 (4) = Alpha channel given, tRNS chunk forbidden | ||
| 308 | |||
| 309 | if ($cs == PNG_COLOR_TYPE_GRAY) { | ||
| 310 | # cs=0 grayscale 1,2,4,8,16 bps, no Alpha, optional tRNS | ||
| 311 | # $png->get_channels() should return 1 (1 sample per pixel) | ||
| 312 | $self->colorspace('DeviceGray'); | ||
| 313 | $self->bits_per_component($bpc); | ||
| 314 | my $dict = PDFDict(); | ||
| 315 | $self->{'DecodeParms'} = PDFArray($dict); | ||
| 316 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 317 | $dict->{'Colors'} = PDFNum(1); # samples per pixel (channels) | ||
| 318 | $dict->{'Columns'} = PDFNum($w); | ||
| 319 | if ($tRNS_available && $transparency) { | ||
| 320 | # only need to set the Mask | ||
| 321 | my $m = $trns->{'gray'}; | ||
| 322 | # should be one 16-bit value in tRNS chunk, corresponding to the | ||
| 323 | # precise gray value to render transparent (Alpha = 0) | ||
| 324 | $self->{'Mask'} = PDFArray(PDFNum($m), PDFNum($m)); | ||
| 325 | } | ||
| 326 | # compress all but short streams | ||
| 327 | if (length($self->{' stream'}) > 32) { | ||
| 328 | $self->{' stream'} = Compress::Zlib::compress($self->{' stream'}); | ||
| 329 | $self->filters('FlateDecode'); # tell reader it's compressed... | ||
| 330 | $self->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 331 | } else { | ||
| 332 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 333 | # takes up 25 bytes all by itself | ||
| 334 | delete $self->{'Filter'}; | ||
| 335 | $self->{' nofilt'} = 1; | ||
| 336 | } | ||
| 337 | |||
| 338 | # cs=1 reserved for grayscale given by palette (1 channel) -- | ||
| 339 | # not in current PNG spec. you can emulate by cs=3 with R=G=B values, | ||
| 340 | # but this wastes 2/3 of the space used for the palette | ||
| 341 | |||
| 342 | } elsif ($cs == PNG_COLOR_TYPE_RGB) { | ||
| 343 | # cs=2 RGB 8 or 16 bps, no Alpha, optional tRNS | ||
| 344 | # $png->get_channels() should return 3 (3 samples per pixel) | ||
| 345 | $self->colorspace('DeviceRGB'); | ||
| 346 | $self->bits_per_component($bpc); | ||
| 347 | my $dict = PDFDict(); | ||
| 348 | $self->{'DecodeParms'} = PDFArray($dict); | ||
| 349 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 350 | $dict->{'Colors'} = PDFNum(3); | ||
| 351 | $dict->{'Columns'} = PDFNum($w); | ||
| 352 | if ($tRNS_available && $transparency) { | ||
| 353 | # only need to set the Mask | ||
| 354 | # old code unpacked 16-bit ints into an array, pulled out 3 at | ||
| 355 | # a time into r,g,b arrays, got the max and min values, and | ||
| 356 | # created @v array to map to an array, as below. according to | ||
| 357 | # PNG spec, should be only 3 entries in the first place! | ||
| 358 | # rgb 16-bit values, together form one truecolor entry to make transparent | ||
| 359 | my @v = (); | ||
| 360 | my $m = $trns->{'red'}; | ||
| 361 | push @v, $m,$m; | ||
| 362 | $m = $trns->{'green'}; | ||
| 363 | push @v, $m,$m; | ||
| 364 | $m = $trns->{'blue'}; | ||
| 365 | push @v, $m,$m; | ||
| 366 | $self->{'Mask'} = PDFArray(map { PDFNum($_) } @v); | ||
| 367 | } | ||
| 368 | # compress all but short streams | ||
| 369 | if (length($self->{' stream'}) > 32) { | ||
| 370 | $self->{' stream'} = Compress::Zlib::compress($self->{' stream'}); | ||
| 371 | $self->filters('FlateDecode'); # tell reader it's compressed... | ||
| 372 | $self->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 373 | } else { | ||
| 374 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 375 | # takes up 25 bytes all by itself | ||
| 376 | delete $self->{'Filter'}; | ||
| 377 | $self->{' nofilt'} = 1; | ||
| 378 | } | ||
| 379 | |||
| 380 | } elsif ($cs == PNG_COLOR_TYPE_PALETTE) { | ||
| 381 | # cs=3 palette 1,2,4,8 bpp depth, no Alpha, optional tRNS | ||
| 382 | # $png->get_channels() should return 1 (1 sample per pixel) | ||
| 383 | # should not be 16 bpp, as palette limited to 256 entries | ||
| 384 | if ($bpc > 8) { | ||
| 385 | die "maximum 256 palette entries, 2**$bpc requested\n"; | ||
| 386 | } | ||
| 387 | # other cs values ignore any palette that happens to be there | ||
| 388 | if ($tRNS_available && $transparency) { | ||
| 389 | # $trns is an arrayref containing up to 256 8-bit Alpha entries, | ||
| 390 | # one per palette entry. if necessary, pad out with xFF entries | ||
| 391 | # (opaque) to make enough entries (to match the palette size... | ||
| 392 | # assuming that no pixels request an out-of-range index value). | ||
| 393 | # x00 is transparent. | ||
| 394 | # TBD: first pad out the palette to 1<<$bpc entries, just to be | ||
| 395 | # absolutely certain no out-of-range indices? 0 0 0 bytes? | ||
| 396 | # or, check all image indices used to make sure $palette (and | ||
| 397 | # thus $trns), is long enough (don't trust image maker)? | ||
| 398 | # $palette SHOULD be 3x bytes in length | ||
| 399 | for (my $i=@{$trns}; $i<(length($palette)/3); $i++) { | ||
| 400 | $trns->[$i] = 255; | ||
| 401 | } | ||
| 402 | } | ||
| 403 | my $dict = PDFDict(); | ||
| 404 | $pdf->new_obj($dict); | ||
| 405 | # note that it is legal for palette to be short (too few entries for | ||
| 406 | # its bps value) so long as none of the missing entries are used | ||
| 407 | # compressing palettes seems to cause problems, and such binary data | ||
| 408 | # often doesn't compress well anyway | ||
| 409 | $dict->{' stream'} = $palette; | ||
| 410 | delete $dict->{'Filter'}; | ||
| 411 | $dict->{' nofilt'} = 1; | ||
| 412 | $palette = ""; # why does this need to be destroyed? to save space? | ||
| 413 | $self->colorspace(PDFArray( | ||
| 414 | PDFName('Indexed'), | ||
| 415 | PDFName('DeviceRGB'), | ||
| 416 | PDFNum(int(length($dict->{' stream'})/3)-1), | ||
| 417 | $dict) ); | ||
| 418 | $self->bits_per_component($bpc); | ||
| 419 | $dict = PDFDict(); | ||
| 420 | $self->{'DecodeParms'} = PDFArray($dict); | ||
| 421 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 422 | $dict->{'Colors'} = PDFNum(1); # one palette entry number per pixel | ||
| 423 | $dict->{'Columns'} = PDFNum($w); | ||
| 424 | if ($tRNS_available && $transparency) { | ||
| 425 | $dict = PDFDict(); | ||
| 426 | $pdf->new_obj($dict); | ||
| 427 | $dict->{'Type'} = PDFName('XObject'); | ||
| 428 | $dict->{'Subtype'} = PDFName('Image'); | ||
| 429 | $dict->{'Width'} = PDFNum($w); | ||
| 430 | $dict->{'Height'} = PDFNum($h); | ||
| 431 | $dict->{'ColorSpace'} = PDFName('DeviceGray'); | ||
| 432 | $dict->{'BitsPerComponent'} = PDFNum(8); | ||
| 433 | $self->{'SMask'} = $dict; | ||
| 434 | # now to build an "image" used as an SMask, which is the (8-bit) | ||
| 435 | # Alpha value to be used for each pixel. get the palette (and thus | ||
| 436 | # $trns) index from the uncompressed/unfiltered image data and | ||
| 437 | # look up the Alpha to stick in each byte of the SMask. | ||
| 438 | foreach my $n (0 .. $h*$w-1) { | ||
| 439 | # dict->stream initially empty. fill with Alpha value for | ||
| 440 | # each pixel, indexed by pixel value | ||
| 441 | vec($dict->{' stream'}, $n, 8) = # each Alpha 8 bits | ||
| 442 | $trns->[vec($self->{' stream'}, $n, $bpc)]; | ||
| 443 | # n-th pixel is palette index 1-8 bit integer | ||
| 444 | # $trns[index for n-th pixel] is Alpha to use | ||
| 445 | } | ||
| 446 | # compress all but short streams | ||
| 447 | if (length($dict->{' stream'}) > 32) { | ||
| 448 | $dict->{' stream'} = Compress::Zlib::compress($dict->{' stream'}); | ||
| 449 | $dict->filters('FlateDecode'); # tell reader it's compressed... | ||
| 450 | $dict->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 451 | } else { | ||
| 452 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 453 | # takes up 25 bytes all by itself | ||
| 454 | delete $dict->{'Filter'}; | ||
| 455 | $dict->{' nofilt'} = 1; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | # compress all but short streams | ||
| 459 | if (length($self->{' stream'}) > 32) { | ||
| 460 | $self->{' stream'} = Compress::Zlib::compress($self->{' stream'}); | ||
| 461 | $self->filters('FlateDecode'); # tell reader it's compressed... | ||
| 462 | $self->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 463 | } else { | ||
| 464 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 465 | # takes up 25 bytes all by itself | ||
| 466 | delete $self->{'Filter'}; | ||
| 467 | $self->{' nofilt'} = 1; | ||
| 468 | } | ||
| 469 | |||
| 470 | } elsif ($cs == PNG_COLOR_TYPE_GRAY_ALPHA) { | ||
| 471 | # cs=4 grayscale+Alpha 8 or 16 bps, NO tRNS | ||
| 472 | # $png->get_channels() should return 2 (2 samples per pixel) | ||
| 473 | $self->filters('FlateDecode'); | ||
| 474 | $self->colorspace('DeviceGray'); | ||
| 475 | $self->bits_per_component($bpc); | ||
| 476 | my $dict = PDFDict(); | ||
| 477 | $self->{'DecodeParms'} = PDFArray($dict); | ||
| 478 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 479 | $dict->{'Colors'} = PDFNum(1); # not 2 for Alpha b/c only gray part | ||
| 480 | $dict->{'Columns'} = PDFNum($w); | ||
| 481 | |||
| 482 | $dict = PDFDict(); | ||
| 483 | if ($transparency) { # will be in cs=0 if stripped Alpha for notrans | ||
| 484 | $pdf->new_obj($dict); | ||
| 485 | $dict->{'Type'} = PDFName('XObject'); | ||
| 486 | $dict->{'Subtype'} = PDFName('Image'); | ||
| 487 | $dict->{'Width'} = PDFNum($w); | ||
| 488 | $dict->{'Height'} = PDFNum($h); | ||
| 489 | $dict->{'ColorSpace'} = PDFName('DeviceGray'); | ||
| 490 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 491 | $self->{'SMask'} = $dict; | ||
| 492 | # basically, move all the first half of each pair of samples | ||
| 493 | # (1 or 2 bytes) to self->stream, and the second half (1 or | ||
| 494 | # 2 bytes) into dict->stream as the Alpha SMask. delete | ||
| 495 | # leftover self->stream. | ||
| 496 | delete $self->{' nofilt'}; | ||
| 497 | $dict->{' stream'} = ''; | ||
| 498 | $self->{' stream'} = ''; | ||
| 499 | # high-speed splitting out of alpha channel | ||
| 500 | my $split = $png->split_alpha(); | ||
| 501 | $self->{' stream'} = $split->{'data'}; | ||
| 502 | $dict->{' stream'} = $split->{'alpha'}; | ||
| 503 | } | ||
| 504 | # compress all but short streams | ||
| 505 | if (length($self->{' stream'}) > 32) { | ||
| 506 | $self->{' stream'} = Compress::Zlib::compress($self->{' stream'}); | ||
| 507 | $self->filters('FlateDecode'); # tell reader it's compressed... | ||
| 508 | $self->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 509 | } else { | ||
| 510 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 511 | # takes up 25 bytes all by itself | ||
| 512 | delete $self->{'Filter'}; | ||
| 513 | $self->{' nofilt'} = 1; | ||
| 514 | } | ||
| 515 | if (length($dict->{' stream'}) > 32) { | ||
| 516 | $dict->{' stream'} = Compress::Zlib::compress($dict->{' stream'}); | ||
| 517 | $dict->filters('FlateDecode'); # tell reader it's compressed... | ||
| 518 | $dict->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 519 | } else { | ||
| 520 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 521 | # takes up 25 bytes all by itself | ||
| 522 | delete $dict->{'Filter'}; | ||
| 523 | $dict->{' nofilt'} = 1; | ||
| 524 | } | ||
| 525 | # if notrans, Alpha channel should have been stripped off | ||
| 526 | # and we are told it is cs = 0 (visit other section, not this one) | ||
| 527 | |||
| 528 | # cs=5 reserved for grayscale given by palette+Alpha (2 channels) -- | ||
| 529 | # not in current PNG spec. you can emulate by cs=3 with R=G=B values, | ||
| 530 | # but this wastes 2/3 of the space used, and using a tRNS chunk to | ||
| 531 | # specify an 8-bit Alpha for each palette entry. note that this is | ||
| 532 | # a constant Alpha per palette entry, NOT an Alpha per pixel! | ||
| 533 | |||
| 534 | } elsif ($cs == PNG_COLOR_TYPE_RGB_ALPHA) { | ||
| 535 | # about 50 times slower than cs=2! (withOUT notrans) due to Alpha move | ||
| 536 | # cs=6 RGB+Alpha 8 or 16 bps, NO tRNS | ||
| 537 | # $png->get_channels() should return 4 (4 samples per pixel) | ||
| 538 | $self->filters('FlateDecode'); | ||
| 539 | $self->colorspace('DeviceRGB'); | ||
| 540 | $self->bits_per_component($bpc); | ||
| 541 | my $dict = PDFDict(); | ||
| 542 | $self->{'DecodeParms'} = PDFArray($dict); | ||
| 543 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 544 | $dict->{'Colors'} = PDFNum(3); # not 4 for Alpha b/c Alpha removed | ||
| 545 | $dict->{'Columns'} = PDFNum($w); | ||
| 546 | |||
| 547 | $dict = PDFDict(); | ||
| 548 | if ($transparency) { # will be in cs=2 if stripped Alpha for notrans | ||
| 549 | $pdf->new_obj($dict); | ||
| 550 | $dict->{'Type'} = PDFName('XObject'); | ||
| 551 | $dict->{'Subtype'} = PDFName('Image'); | ||
| 552 | $dict->{'Width'} = PDFNum($w); | ||
| 553 | $dict->{'Height'} = PDFNum($h); | ||
| 554 | $dict->{'ColorSpace'} = PDFName('DeviceGray'); | ||
| 555 | $dict->{'Filter'} = PDFArray(PDFName('FlateDecode')); | ||
| 556 | $dict->{'BitsPerComponent'} = PDFNum($bpc); | ||
| 557 | $self->{'SMask'} = $dict; | ||
| 558 | # basically, move the last quarter of each quartet of samples | ||
| 559 | # (1 or 2 bytes) to dict->stream as the Alpha SMask, and the | ||
| 560 | # first 3/4 (3 * 1 or 2 bytes) into self->stream as the image. | ||
| 561 | # delete leftover self->stream. | ||
| 562 | delete $self->{' nofilt'}; | ||
| 563 | $dict->{' stream'} = ''; | ||
| 564 | $self->{' stream'} = ''; | ||
| 565 | # high-speed splitting out of alpha channel | ||
| 566 | my $split = $png->split_alpha(); | ||
| 567 | $self->{' stream'} = $split->{'data'}; | ||
| 568 | $dict->{' stream'} = $split->{'alpha'}; | ||
| 569 | } | ||
| 570 | # compress all but short streams | ||
| 571 | if (length($self->{' stream'}) > 32) { | ||
| 572 | $self->{' stream'} = Compress::Zlib::compress($self->{' stream'}); | ||
| 573 | $self->filters('FlateDecode'); # tell reader it's compressed... | ||
| 574 | $self->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 575 | } else { | ||
| 576 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 577 | # takes up 25 bytes all by itself | ||
| 578 | delete $self->{'Filter'}; | ||
| 579 | $self->{' nofilt'} = 1; | ||
| 580 | } | ||
| 581 | if (length($dict->{' stream'}) > 32) { | ||
| 582 | $dict->{' stream'} = Compress::Zlib::compress($dict->{' stream'}); | ||
| 583 | $dict->filters('FlateDecode'); # tell reader it's compressed... | ||
| 584 | $dict->{' nofilt'} = 1; # ...but writer not to compress on the fly | ||
| 585 | } else { | ||
| 586 | # too short to bother compressing. '/Filter [ /FlateDecode ] ' | ||
| 587 | # takes up 25 bytes all by itself | ||
| 588 | delete $dict->{'Filter'}; | ||
| 589 | $dict->{' nofilt'} = 1; | ||
| 590 | } | ||
| 591 | # if notrans, Alpha channel should have been stripped off | ||
| 592 | # and we are told it is cs = 2 (visit other section, not this one) | ||
| 593 | |||
| 594 | # cs=7 reserved for RGB given by palette+Alpha (2 channels) -- | ||
| 595 | # not in current PNG spec. the closest you can emulate this is cs=3 | ||
| 596 | # plus tRNS chunk for an 8-bit Alpha per palette entry. note that this | ||
| 597 | # is an Alpha per palette index, and NOT an Alpha per pixel! | ||
| 598 | |||
| 599 | } else { | ||
| 600 | die "unsupported PNG-color type (cs=$cs)."; | ||
| 601 | } | ||
| 602 | |||
| 603 | return($self); | ||
| 604 | } | ||
| 605 | |||
| 606 | =head2 usesLib | ||
| 607 | |||
| 608 | $mode = $png->usesLib() | ||
| 609 | |||
| 610 | =over | ||
| 611 | |||
| 612 | Returns 1 if Image::PNG::Libpng installed and used, 0 if not installed, or -1 | ||
| 613 | if installed but not used (nouseIPL option given to C<image_png>). | ||
| 614 | |||
| 615 | B<Caution:> this method can only be used I<after> the image object has been | ||
| 616 | created. It can't tell you whether Image::PNG::Libpng is available in | ||
| 617 | advance of actually using it, in case you want to use some functionality | ||
| 618 | available only in PNG_IPL. See the L<PDF::Builder> LA_IPL() call if you | ||
| 619 | need to know in advance. | ||
| 620 | |||
| 621 | =back | ||
| 622 | |||
| 623 | =cut | ||
| 624 | |||
| 625 | sub usesLib { | ||
| 626 | my ($self) = @_; | ||
| 627 | # should be 0 for Image::PNG::Libpng not installed, or -1 for is installed, | ||
| 628 | # but not using it | ||
| 629 | return $self->{'usesIPL'}->val(); | ||
| 630 | } | ||
| 631 | |||
| 632 | 1; | ||
| 633 | |||
| 634 | __END__ |