| 1 | package Imager::File::APNG; | ||
| 2 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use Imager; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Imager::File::PNG; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | our $VERSION = "0.001"; | ||
| 9 | |||
| 10 | my $png_header = "\x89PNG\x0d\x0A\cZ\x0A"; | ||
| 11 | |||
| 12 | # looks behind the curtain | ||
| 13 | # I expect I'll give Imager::File::PNG a separate API, so don't do | ||
| 14 | # this because I'll break your code | ||
| 15 | |||
| 16 | sub do_write_png { | ||
| 17 | my ($im, $io, %hsh) = @_; | ||
| 18 | |||
| 19 | $im->_set_opts(\%hsh, "i_", $im); | ||
| 20 | $im->_set_opts(\%hsh, "png_", $im); | ||
| 21 | |||
| 22 | unless (Imager::File::PNG::i_writepng_wiol($im->{IMG}, $io)) { | ||
| 23 | $im->_set_error(Imager->_error_as_msg); | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | return $im; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub do_read_png { | ||
| 30 | my ($data, $frame) = @_; | ||
| 31 | |||
| 32 | open my $fh, ">", "foo.png" or die; | ||
| 33 | binmode $fh; | ||
| 34 | print $fh $data; | ||
| 35 | close $fh; | ||
| 36 | |||
| 37 | my $io = Imager::IO->new_buffer($data); | ||
| 38 | my $im = Imager->new; | ||
| 39 | unless ($im->{IMG} = Imager::File::PNG::i_readpng_wiol($io, 0)) { | ||
| 40 | Imager->_set_error("APNG: " . Imager->_error_as_msg . " decoding frame $frame"); | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | return $im; | ||
| 44 | } | ||
| 45 | |||
| 46 | # single image APNG is just PNG | ||
| 47 | sub write_apng { | ||
| 48 | my ($im, $io, %hsh) = @_; | ||
| 49 | |||
| 50 | return return do_write_png($im, $io, %hsh); | ||
| 51 | }; | ||
| 52 | |||
| 53 | sub _make_actl { | ||
| 54 | my ($num_frames, $num_plays) = @_; | ||
| 55 | |||
| 56 | return _make_chunk("acTL", pack("NN", $num_frames, $num_plays)); | ||
| 57 | } | ||
| 58 | |||
| 59 | my %dispose_names = | ||
| 60 | ( | ||
| 61 | 0 => "none", | ||
| 62 | 1 => "background", | ||
| 63 | 2 => "previous", | ||
| 64 | ); | ||
| 65 | |||
| 66 | my %dispose_nums = reverse(%dispose_names); | ||
| 67 | |||
| 68 | my %blend_names = | ||
| 69 | ( | ||
| 70 | 0 => "source", | ||
| 71 | 1 => "over", | ||
| 72 | ); | ||
| 73 | |||
| 74 | my %blend_nums = reverse(%blend_names); | ||
| 75 | |||
| 76 | sub _make_fctl { | ||
| 77 | my ($seq, $im, $frame) = @_; | ||
| 78 | |||
| 79 | my $xoff = $im->tags(name => "apng_xoffset") || 0; | ||
| 80 | my $yoff = $im->tags(name => "apng_yoffset") || 0; | ||
| 81 | my $delay = $im->tags(name => "apng_delay"); | ||
| 82 | my $delay_num = $im->tags(name => "apng_delay_num"); | ||
| 83 | my $delay_den = $im->tags(name => "apng_delay_den"); | ||
| 84 | |||
| 85 | if (defined $delay) { | ||
| 86 | if ($delay > 65) { | ||
| 87 | # some seconds | ||
| 88 | $delay_num = int($delay); | ||
| 89 | $delay_den = 1; | ||
| 90 | } | ||
| 91 | elsif ($delay < 0) { | ||
| 92 | Imager->_set_error("APNG: apng_delay value $delay page $frame must be non-negative"); | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | # FIXME: try to find a better rational estimate | ||
| 97 | $delay_num = int($delay * 1000); | ||
| 98 | $delay_den = 1000; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | elsif (defined $delay_den) { | ||
| 102 | unless (defined $delay_num) { | ||
| 103 | $delay_num = 1; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $delay_den = 60; | ||
| 108 | unless (defined $delay_num) { | ||
| 109 | $delay_num = 1; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | if ($delay_num < 0 || $delay_num > 65535) { | ||
| 113 | Imager->_set_error("APNG: apng_delay_num value $delay_num page $frame out of range 0 .. 65535"); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | if ($delay_den < 0 || $delay_den > 65535) { | ||
| 117 | Imager->_set_error("APNG: apng_delay_den value $delay_den page $frame out of range 0 .. 65535"); | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | my $dispose = $im->tags(name => "apng_dispose") || 0; | ||
| 122 | my $blend = $im->tags(name => "apng_blend") || 0; | ||
| 123 | if ($dispose =~ /[^0-9.]/) { | ||
| 124 | if (exists $dispose_nums{$dispose}) { | ||
| 125 | $dispose = $dispose_nums{$dispose}; | ||
| 126 | } | ||
| 127 | else { | ||
| 128 | Imager->_set_error("APNG: unknown value '$dispose' page $frame for apng_dispose"); | ||
| 129 | return | ||
| 130 | } | ||
| 131 | } | ||
| 132 | if ($blend =~ /[^0-9.]/) { | ||
| 133 | if (exists $blend_nums{$blend}) { | ||
| 134 | $blend = $blend_nums{$blend}; | ||
| 135 | } | ||
| 136 | else { | ||
| 137 | Imager->_set_error("APNG: unknown value '$blend' page $frame for apng_blend"); | ||
| 138 | return | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | return _make_chunk("fcTL", pack("NNNNNnnCC", $seq, $im->getwidth, $im->getheight, | ||
| 143 | $xoff, $yoff, $delay_num, $delay_den, $dispose, $blend)); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _make_fdat { | ||
| 147 | my ($seq, $payload) = @_; | ||
| 148 | |||
| 149 | return _make_chunk("fdAT", pack("N", $seq) . $payload); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub write_multi_apng { | ||
| 153 | my ($class, $io, $opts, @ims) = @_; | ||
| 154 | |||
| 155 | if (@ims == 1) { | ||
| 156 | return $ims[0]->write(%$opts, type => "png", io => $io); | ||
| 157 | } | ||
| 158 | |||
| 159 | Imager->_set_opts($opts, "apng_", @ims); | ||
| 160 | |||
| 161 | # APNG files contain only a single image type and bit depth, if | ||
| 162 | # they're all the same use that, otherwise we need to convert them | ||
| 163 | # to the same | ||
| 164 | my $type = $ims[0]->type; | ||
| 165 | my $bits = $ims[0]->bits; | ||
| 166 | my $chans = $ims[0]->colorchannels; | ||
| 167 | my $need_alpha = 0; | ||
| 168 | for my $im (@ims) { | ||
| 169 | if ($type eq 'palette' && $im->type eq 'direct') { | ||
| 170 | # FIXME: look for a common palette | ||
| 171 | $type = 'direct'; | ||
| 172 | } | ||
| 173 | if ($im->bits eq 'double' || ($bits ne 'double' && $im->bits > $bits)) { | ||
| 174 | $bits = $im->bits; | ||
| 175 | } | ||
| 176 | $need_alpha = 1 if $im->alphachannel; | ||
| 177 | if ($im->colorchannels > $chans) { | ||
| 178 | $chans = $im->colorchannels; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | if ($type eq 'palette' && $chans == 3) { | ||
| 183 | # FIXME - we shouldn't get here yet | ||
| 184 | } | ||
| 185 | else { | ||
| 186 | $type = 'direct'; | ||
| 187 | if ($bits eq 'double' || $bits > 8) { | ||
| 188 | $bits = 16; | ||
| 189 | } | ||
| 190 | # chans is fine | ||
| 191 | } | ||
| 192 | |||
| 193 | # Which is the first visible frame in the animation? The first image | ||
| 194 | # can be hidden from the animation. The first image still needs to be | ||
| 195 | # the same type as the other images, since they share the type fields of the | ||
| 196 | # IHDR (and the PLTE, tRNS). | ||
| 197 | my $first_frame = 0; | ||
| 198 | if ($ims[0]->tags(name => "apng_hidden")) { | ||
| 199 | ++$first_frame; | ||
| 200 | } | ||
| 201 | my $cwidth = $ims[0]->getwidth; | ||
| 202 | my $cheight = $ims[0]->getheight; | ||
| 203 | for my $frame (0 .. $#ims) { | ||
| 204 | my $im = $ims[$frame]; | ||
| 205 | my $xoff = $im->tags(name => "apng_xoffset") || 0; | ||
| 206 | my $yoff = $im->tags(name => "apng_yoffset") || 0; | ||
| 207 | if ($xoff < 0) { | ||
| 208 | Imager->_set_error("APNG: apng_xoffset must be non-negative for page $frame"); | ||
| 209 | return; | ||
| 210 | } | ||
| 211 | if ($yoff < 0) { | ||
| 212 | Imager->_set_error("APNG: apng_yoffset must be non-negative for page $frame"); | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | my $fwidth = $im->getwidth; | ||
| 216 | my $fheight = $im->getheight; | ||
| 217 | if ($xoff + $fwidth > $cwidth || $yoff + $fheight > $cheight) { | ||
| 218 | Imager->_set_error("APNG: Page $frame (${fwidth}x${fheight}\@${xoff}x$yoff) is outside the canvas defined by page $first_frame (${cwidth}x$cheight)"); | ||
| 219 | return; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | my @workims; | ||
| 224 | my $ihdr; | ||
| 225 | my $seq = 0; | ||
| 226 | for my $frame (0 .. $#ims) { | ||
| 227 | my $im = $ims[$frame]; | ||
| 228 | my $orig = $im; | ||
| 229 | |||
| 230 | # convert each frame to a common form | ||
| 231 | if ($bits > 8 && $im->bits == 8) { | ||
| 232 | $im = $im->to_rgb16; | ||
| 233 | } | ||
| 234 | elsif ($im->type eq "paletted") { | ||
| 235 | $im = $im->to_rgb8; | ||
| 236 | } | ||
| 237 | if ($im->colorchannels != $chans) { | ||
| 238 | $im = $im->convert(preset => "rgb"); | ||
| 239 | } | ||
| 240 | if ($need_alpha && !$im->alphachannel) { | ||
| 241 | $im = $im->convert(preset => "addalpha"); | ||
| 242 | } | ||
| 243 | my $dio = Imager::IO->new_bufchain; | ||
| 244 | do_write_png($im, $dio, %$opts) | ||
| 245 | or return; | ||
| 246 | my $data = Imager::io_slurp($dio); | ||
| 247 | undef $dio; | ||
| 248 | my $rio = Imager::IO->new_buffer($data); | ||
| 249 | my $dparsed = _parse_apng_parts($rio, 0); | ||
| 250 | undef $rio; | ||
| 251 | unless ($dparsed) { | ||
| 252 | Imager->_set_error("APNG: Failed to parse temp PNG for frame $frame"); | ||
| 253 | return; | ||
| 254 | } | ||
| 255 | my $writeme; | ||
| 256 | if ($frame == 0) { | ||
| 257 | $ihdr = _make_ihdr(%{$dparsed->{ihdr}}, w => $im->getwidth, h => $im->getheight); | ||
| 258 | $writeme = join "", $png_header, $ihdr, @{$dparsed->{headers}}; | ||
| 259 | my $num_plays = $orig->tags(name => "apng_num_plays") || 0; | ||
| 260 | $writeme .= _make_actl(scalar(@ims) - $first_frame, $num_plays); | ||
| 261 | if ($first_frame == 0) { | ||
| 262 | my $fctl = _make_fctl($seq++, $orig, $frame) | ||
| 263 | or return; | ||
| 264 | $writeme .= $fctl; | ||
| 265 | } | ||
| 266 | $writeme .= join "", @{$dparsed->{frames}[0]{idat}}; | ||
| 267 | } | ||
| 268 | else { | ||
| 269 | # validate that we're making the same type of image | ||
| 270 | my $myihdr = _make_ihdr(%{$dparsed->{ihdr}}, w => $ims[0]->getwidth, h => $ims[0]->getheight); | ||
| 271 | if ($ihdr ne $myihdr) { | ||
| 272 | Imager->_set_error("APNG: Internal error: IHDR mismatch " | ||
| 273 | . unpack("H*", $ihdr) . " vs " . unpack("H*", $myihdr)); | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | my $fctl = _make_fctl($seq++, $orig, $frame) | ||
| 277 | or return; | ||
| 278 | $writeme .= $fctl; | ||
| 279 | for my $idat (@{$dparsed->{frames}[0]{idat_payloads}}) { | ||
| 280 | $writeme .= _make_fdat($seq++, $idat); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | if ($io->write($writeme) != length $writeme) { | ||
| 284 | Imager->_set_error("APNG: Write failed: $!"); | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | } | ||
| 288 | my $iend = _make_chunk("IEND", ""); | ||
| 289 | if ($io->write($iend) != length $iend) { | ||
| 290 | Imager->_set_error("APNG: Write failed: $!"); | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | if ($io->close) { | ||
| 294 | Imager->_set_error("APNG: Close failed: $!"); | ||
| 295 | return; | ||
| 296 | } | ||
| 297 | |||
| 298 | return 1; | ||
| 299 | } | ||
| 300 | |||
| 301 | #unsigned long crc_table[256]; | ||
| 302 | my @crc_table; | ||
| 303 | |||
| 304 | #/* Flag: has the table been computed? Initially false. */ | ||
| 305 | # int crc_table_computed = 0; | ||
| 306 | |||
| 307 | # /* Make the table for a fast CRC. */ | ||
| 308 | # void make_crc_table(void) | ||
| 309 | # { | ||
| 310 | sub _make_crc_table { | ||
| 311 | # unsigned long c; | ||
| 312 | # int n, k; | ||
| 313 | # | ||
| 314 | # for (n = 0; n < 256; n++) { | ||
| 315 | for my $n (0 .. 255) { | ||
| 316 | # c = (unsigned long) n; | ||
| 317 | my $c = $n; | ||
| 318 | # for (k = 0; k < 8; k++) { | ||
| 319 | for my $k (0 .. 7) { | ||
| 320 | # if (c & 1) | ||
| 321 | # c = 0xedb88320L ^ (c >> 1); | ||
| 322 | # else | ||
| 323 | # c = c >> 1; | ||
| 324 | if ($c & 1) { | ||
| 325 | $c = 0xedb88320 ^ ($c >> 1); | ||
| 326 | } | ||
| 327 | else { | ||
| 328 | $c = $c >> 1; | ||
| 329 | } | ||
| 330 | # } | ||
| 331 | } | ||
| 332 | # crc_table[n] = c; | ||
| 333 | $crc_table[$n] = $c; | ||
| 334 | # } | ||
| 335 | } | ||
| 336 | # crc_table_computed = 1; | ||
| 337 | # } | ||
| 338 | } | ||
| 339 | |||
| 340 | # /* Update a running CRC with the bytes buf[0..len-1]--the CRC | ||
| 341 | # should be initialized to all 1's, and the transmitted value | ||
| 342 | # is the 1's complement of the final running CRC (see the | ||
| 343 | # crc() routine below). */ | ||
| 344 | |||
| 345 | # unsigned long update_crc(unsigned long crc, unsigned char *buf, | ||
| 346 | # int len) | ||
| 347 | # { | ||
| 348 | sub _update_crc { | ||
| 349 | my ($crc, $data) = @_; | ||
| 350 | # unsigned long c = crc; | ||
| 351 | # int n; | ||
| 352 | |||
| 353 | # for (n = 0; n < len; n++) { | ||
| 354 | # c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8); | ||
| 355 | # } | ||
| 356 | for my $code (unpack("C*", $data)) { | ||
| 357 | $crc = $crc_table[($crc ^ $code) & 0xFF] ^ ($crc >> 8); | ||
| 358 | } | ||
| 359 | # return c; | ||
| 360 | # } | ||
| 361 | return $crc; | ||
| 362 | } | ||
| 363 | |||
| 364 | # /* Return the CRC of the bytes buf[0..len-1]. */ | ||
| 365 | # unsigned long crc(unsigned char *buf, int len) | ||
| 366 | # { | ||
| 367 | # return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL; | ||
| 368 | # } | ||
| 369 | |||
| 370 | sub _crc { | ||
| 371 | my $data = shift; | ||
| 372 | |||
| 373 | return _update_crc(0xFFFFFFFF, $data) ^ 0xFFFFFFFF; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub _read_chunk { | ||
| 377 | my ($io) = @_; | ||
| 378 | |||
| 379 | my $rlen; | ||
| 380 | unless ($io->read($rlen, 4) == 4) { | ||
| 381 | Imager->_set_error("APNG: Cannot read chunk length"); | ||
| 382 | return; | ||
| 383 | } | ||
| 384 | my $len = unpack("N", $rlen); | ||
| 385 | my $type; | ||
| 386 | unless ($io->read($type, 4) == 4){ | ||
| 387 | Imager->_set_error("APNG: Cannot read chunk type"); | ||
| 388 | return; | ||
| 389 | } | ||
| 390 | my $payload = ""; | ||
| 391 | if ($len) { | ||
| 392 | unless ($io->read($payload, $len) == $len) { | ||
| 393 | Imager->_set_error("Cannot read payload"); | ||
| 394 | return; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | my $crc; | ||
| 398 | unless ($io->read($crc, 4) == 4) { | ||
| 399 | Imager->_set_error("APNG: Cannot read CRC"); | ||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | return ( $len + 12, $rlen . $type . $payload . $crc, $len, $type, $payload, $crc ); | ||
| 404 | } | ||
| 405 | |||
| 406 | sub _parse_apng_parts { | ||
| 407 | my ($io, $strict) = @_; | ||
| 408 | |||
| 409 | my $head; | ||
| 410 | unless($io->read($head, 8) == 8 && $head eq $png_header) { | ||
| 411 | Imager->_set_error("Invalid PNG header"); | ||
| 412 | return; | ||
| 413 | } | ||
| 414 | my @frames; | ||
| 415 | my $ihdr; | ||
| 416 | my @headers; | ||
| 417 | my $end; | ||
| 418 | my $actl; | ||
| 419 | my $sequence = -1; | ||
| 420 | my $frame = -1; | ||
| 421 | while (my ($dlen, $data, $len, $type, $payload, $crc) = _read_chunk($io)) { | ||
| 422 | # FIXME: crc checks? | ||
| 423 | # FIXME: ordering checks | ||
| 424 | # chunks we need to duplicate from image to image | ||
| 425 | if ($type eq 'IHDR') { | ||
| 426 | my ($w, $h, $d, $ct, $comp, $filter, $inter) = | ||
| 427 | unpack("NNCCCCC", $payload); | ||
| 428 | $ihdr = | ||
| 429 | { | ||
| 430 | w => $w, | ||
| 431 | h => $h, | ||
| 432 | d => $d, | ||
| 433 | type => $ct, | ||
| 434 | comp => $comp, | ||
| 435 | filter => $filter, | ||
| 436 | interlace => $inter, | ||
| 437 | data => $data, | ||
| 438 | }; | ||
| 439 | } | ||
| 440 | elsif ($type =~ /\A(PLTE|tRNS|sRGB|cHRM|gAMA|iCCP|sBIT|pHYs|tIME)\z/) { | ||
| 441 | push @headers, $data; | ||
| 442 | } | ||
| 443 | elsif ($type eq 'IEND') { | ||
| 444 | $end = $data; | ||
| 445 | } | ||
| 446 | elsif ($type eq 'acTL') { | ||
| 447 | unless ($len == 8) { | ||
| 448 | Imager->_set_error("APNG: Invalid aCTL chunk"); | ||
| 449 | return; | ||
| 450 | } | ||
| 451 | my ($num_frames, $num_plays) = unpack("NN", $payload); | ||
| 452 | $actl = { frames => $num_frames, plays => $num_plays }; | ||
| 453 | } | ||
| 454 | elsif ($type eq 'fcTL') { | ||
| 455 | ++$frame; | ||
| 456 | my ($seq, $width, $height, $xoff, $yoff, $delay_num, $delay_den, $dispose_op, $blend_op) | ||
| 457 | = unpack("NNNNNnnCC", $payload); | ||
| 458 | if ($frames[$frame]{fctl}) { | ||
| 459 | Imager->_set_error("APNG: Duplicate fcTL chunk for frame $seq"); | ||
| 460 | return; | ||
| 461 | } | ||
| 462 | if ($strict && $seq != $sequence) { | ||
| 463 | Imager->_set_error("APNG: out of sequence fcTL chunk, got $seq, expected ". | ||
| 464 | ($sequence+1) . " (strict)"); | ||
| 465 | return; | ||
| 466 | } | ||
| 467 | $frames[$frame]{fctl} = | ||
| 468 | { | ||
| 469 | frame => $frame, | ||
| 470 | sequence => $seq, | ||
| 471 | width => $width, | ||
| 472 | height => $height, | ||
| 473 | xoff => $xoff, | ||
| 474 | yoff => $yoff, | ||
| 475 | delay_num => $delay_num, | ||
| 476 | delay_den => $delay_den, | ||
| 477 | dispose => $dispose_op, | ||
| 478 | blend => $blend_op, | ||
| 479 | }; | ||
| 480 | $sequence = $seq; | ||
| 481 | } | ||
| 482 | elsif ($type eq 'IDAT') { | ||
| 483 | $frame = 0; | ||
| 484 | push @{$frames[$frame]{idat}}, $data; | ||
| 485 | push @{$frames[$frame]{idat_payloads}}, $payload; | ||
| 486 | } | ||
| 487 | elsif ($type eq 'fdAT') { | ||
| 488 | my $seq = unpack("N", $payload); | ||
| 489 | if ($strict && $seq != $sequence+1) { | ||
| 490 | Imager->_set_error("APNG: invalid fdAT sequence got $seq, expected $sequence (strict)"); | ||
| 491 | return; | ||
| 492 | } | ||
| 493 | ++$sequence; | ||
| 494 | # we need to rebuild the IDAT from scratch anyway | ||
| 495 | push @{$frames[$frame]{fdat}}, substr($payload, 4); | ||
| 496 | } | ||
| 497 | # what to do about text chunks? | ||
| 498 | |||
| 499 | last if $type eq 'IEND'; | ||
| 500 | } | ||
| 501 | return +{ | ||
| 502 | ihdr => $ihdr, | ||
| 503 | headers => \@headers, | ||
| 504 | end => $end, | ||
| 505 | actl => $actl, | ||
| 506 | frames => \@frames, | ||
| 507 | }; | ||
| 508 | } | ||
| 509 | |||
| 510 | sub _from_idat { | ||
| 511 | my ($parsed) = @_; | ||
| 512 | |||
| 513 | my $data = join "", $png_header, | ||
| 514 | $parsed->{ihdr}{data}, | ||
| 515 | @{$parsed->{headers}}, | ||
| 516 | @{$parsed->{frames}[0]{idat}}, | ||
| 517 | $parsed->{end}; | ||
| 518 | my $im = do_read_png($data, 0) | ||
| 519 | or return; | ||
| 520 | |||
| 521 | return $im; | ||
| 522 | } | ||
| 523 | |||
| 524 | sub _make_chunk { | ||
| 525 | my ($type, $payload) = @_; | ||
| 526 | |||
| 527 | my $base = $type . $payload; | ||
| 528 | my $sizeb = pack("N", length $payload); | ||
| 529 | my $crc = _crc($type . $payload); | ||
| 530 | |||
| 531 | return $sizeb . $type . $payload . pack("N", $crc); | ||
| 532 | } | ||
| 533 | |||
| 534 | sub _make_idat { | ||
| 535 | my ($payload) = @_; | ||
| 536 | |||
| 537 | return _make_chunk("IDAT", $payload); | ||
| 538 | } | ||
| 539 | |||
| 540 | sub _make_ihdr { | ||
| 541 | my (%args) = @_; | ||
| 542 | |||
| 543 | return _make_chunk("IHDR", pack("NNCCCCC", @args{qw/w h d type comp filter interlace/})); | ||
| 544 | } | ||
| 545 | |||
| 546 | sub _from_fdat { | ||
| 547 | my ($parsed, $frame) = @_; | ||
| 548 | |||
| 549 | my $fctl = $parsed->{frames}[$frame]{fctl}; | ||
| 550 | unless ($parsed->{frames}[$frame]{fdat}) { | ||
| 551 | Imager->_set_error("APNG: No fdAT found for frame $frame"); | ||
| 552 | return; | ||
| 553 | } | ||
| 554 | my $data = join "", $png_header, | ||
| 555 | _make_ihdr(%{$parsed->{ihdr}}, w => $fctl->{width}, h => $fctl->{height}), | ||
| 556 | @{$parsed->{headers}}, | ||
| 557 | ( map { _make_idat($_) } @{$parsed->{frames}[$frame]{fdat}} ), | ||
| 558 | $parsed->{end}; | ||
| 559 | |||
| 560 | my $im = do_read_png($data, $frame) | ||
| 561 | or return; | ||
| 562 | |||
| 563 | return $im; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub _from_frame { | ||
| 567 | my ($parsed, $frame) = @_; | ||
| 568 | |||
| 569 | my $im; | ||
| 570 | if ($frame == 0 && $parsed->{frames}[0]{idat}) { | ||
| 571 | $im = _from_idat($parsed) | ||
| 572 | or return; | ||
| 573 | } | ||
| 574 | else { | ||
| 575 | $im = _from_fdat($parsed, $frame) | ||
| 576 | or return; | ||
| 577 | } | ||
| 578 | my $fctl = $parsed->{frames}[$frame]{fctl}; | ||
| 579 | if ($fctl) { | ||
| 580 | $im->settag(name => "apng_hidden", value => 0); | ||
| 581 | $im->settag(name => "apng_xoffset", value => $fctl->{xoff}); | ||
| 582 | $im->settag(name => "apng_yoffset", value => $fctl->{yoff}); | ||
| 583 | $im->settag(name => "apng_delay_num", value => $fctl->{delay_num}); | ||
| 584 | $im->settag(name => "apng_delay_den", value => $fctl->{delay_den}); | ||
| 585 | $im->settag(name => "apng_delay", value => sprintf("%.4g", $fctl->{delay_num} / ($fctl->{delay_den} || 100))); | ||
| 586 | $im->settag(name => "apng_dispose", value => $fctl->{dispose}); | ||
| 587 | $im->settag(name => "apng_blend", value => $fctl->{blend}); | ||
| 588 | } | ||
| 589 | else { | ||
| 590 | $im->settag(name => "apng_hidden", value => 1); | ||
| 591 | } | ||
| 592 | $im->settag(name => "apng_num_plays", value => $parsed->{actl}{plays}); | ||
| 593 | |||
| 594 | return $im; | ||
| 595 | } | ||
| 596 | |||
| 597 | sub read_apng { | ||
| 598 | my ($im, $io, %hsh) = @_; | ||
| 599 | |||
| 600 | my $strict = $hsh{apng_strict} || 0; | ||
| 601 | |||
| 602 | my $page = $hsh{page}; | ||
| 603 | defined $page or $page = 0; | ||
| 604 | |||
| 605 | my $parsed = _parse_apng_parts($io, $strict); | ||
| 606 | unless ($parsed) { | ||
| 607 | $im->_set_error(Imager->errstr); | ||
| 608 | return; | ||
| 609 | } | ||
| 610 | |||
| 611 | if ($page < 0 || $page > $#{$parsed->{frames}}) { | ||
| 612 | my $im2 = _from_frame($parsed, $page); | ||
| 613 | unless ($im2) { | ||
| 614 | $im->_set_error(Imager->errstr); | ||
| 615 | return; | ||
| 616 | } | ||
| 617 | $im->{IMG} = $im2->{IMG}; | ||
| 618 | } | ||
| 619 | else { | ||
| 620 | $im->_set_error("APNG: page out of range"); | ||
| 621 | return; | ||
| 622 | } | ||
| 623 | |||
| 624 | return $im; | ||
| 625 | } | ||
| 626 | |||
| 627 | sub read_multi_apng { | ||
| 628 | my ($io, %hsh) = @_; | ||
| 629 | |||
| 630 | my $strict = $hsh{apng_strict} || 0; | ||
| 631 | |||
| 632 | my $parsed = _parse_apng_parts($io, $strict) | ||
| 633 | or return; | ||
| 634 | |||
| 635 | my @imgs; | ||
| 636 | |||
| 637 | for my $frame (0 .. $#{$parsed->{frames}}) { | ||
| 638 | my $im = _from_frame($parsed, $frame) | ||
| 639 | or return; | ||
| 640 | push @imgs, $im; | ||
| 641 | } | ||
| 642 | |||
| 643 | return @imgs; | ||
| 644 | } | ||
| 645 | |||
| 646 | _make_crc_table(); | ||
| 647 | |||
| 648 | Imager->register_reader | ||
| 649 | ( | ||
| 650 | type => 'apng', | ||
| 651 | single => \&read_apng, | ||
| 652 | multiple => \&read_multi_apng, | ||
| 653 | ); | ||
| 654 | |||
| 655 | # override the PNG reader | ||
| 656 | Imager->register_reader | ||
| 657 | ( | ||
| 658 | type => 'png', | ||
| 659 | single => \&read_apng, | ||
| 660 | multiple => \&read_multi_apng, | ||
| 661 | ); | ||
| 662 | |||
| 663 | Imager->register_writer | ||
| 664 | ( | ||
| 665 | type=>'apng', | ||
| 666 | single => \&write_apng, | ||
| 667 | multiple => \&write_multi_apng, | ||
| 668 | ); | ||
| 669 | |||
| 670 | 1; | ||
| 671 | |||
| 672 | |||
| 673 | __END__ | ||