| 1 | package Image::APNG ; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Image::Magick ; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | our $VERSION = '1.0.0' ; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Image::APNG - Generate Animated PNG (APNG) files from individual PNG images | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Image::APNG; | ||
| 16 | |||
| 17 | my $frames = | ||
| 18 | [ | ||
| 19 | ['frame1.png', 100], | ||
| 20 | ['frame2.png', 150], | ||
| 21 | ['frame3.png', 100] | ||
| 22 | ] ; | ||
| 23 | |||
| 24 | my $options = | ||
| 25 | { | ||
| 26 | loop_count => 0, | ||
| 27 | normalize_resolution => 1, | ||
| 28 | background_color => [255, 255, 255, 0] | ||
| 29 | } ; | ||
| 30 | |||
| 31 | my $result = Image::APNG::generate($frames, $options) ; | ||
| 32 | |||
| 33 | if ($result->{status} == 0) | ||
| 34 | { | ||
| 35 | open my $fh, '>', 'output.png' ; | ||
| 36 | binmode $fh ; | ||
| 37 | print $fh $result->{data} ; | ||
| 38 | close $fh ; | ||
| 39 | } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | print "Errors: " . join("\n", @{$result->{errors}}) ; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | Generates APNG files from a list of PNG images with individual frame delays. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | #---------------------------------------------------------------------------------------------- | ||
| 52 | |||
| 53 | sub generate | ||
| 54 | { | ||
| 55 | my ($frames, $options) = @_ ; | ||
| 56 | |||
| 57 | $options //= {} ; | ||
| 58 | my $errors = [] ; | ||
| 59 | |||
| 60 | my $default_options = | ||
| 61 | { | ||
| 62 | optimize_palette => 0, | ||
| 63 | normalize_resolution => 0, | ||
| 64 | target_resolution => undef, | ||
| 65 | background_color => [0, 0, 0, 0], | ||
| 66 | loop_count => 0, | ||
| 67 | disposal_method => 1, | ||
| 68 | blend_operation => 1 | ||
| 69 | } ; | ||
| 70 | |||
| 71 | $options = {%$default_options, %$options} ; | ||
| 72 | return {status => 1, errors => ['No frames provided'], data => undef} unless $frames && @$frames ; | ||
| 73 | |||
| 74 | my $loaded_frames = load_frames($frames, $errors, $options) ; | ||
| 75 | return {status => 1, errors => $errors, data => undef} unless @$loaded_frames ; | ||
| 76 | |||
| 77 | $loaded_frames = normalize_frames($loaded_frames, $options, $errors) if $options->{normalize_resolution} ; | ||
| 78 | $loaded_frames = optimize_palettes($loaded_frames, $errors) if $options->{optimize_palette} ; | ||
| 79 | |||
| 80 | return | ||
| 81 | { | ||
| 82 | status => @$errors ? 1 : 0, | ||
| 83 | errors => $errors, | ||
| 84 | data => assemble_apng($loaded_frames, $options, $errors), | ||
| 85 | } ; | ||
| 86 | } | ||
| 87 | |||
| 88 | #---------------------------------------------------------------------------------------------- | ||
| 89 | |||
| 90 | sub load_frames | ||
| 91 | { | ||
| 92 | my ($frames, $errors, $options) = @_ ; | ||
| 93 | |||
| 94 | my ($loaded, $previous_valid) = ([]) ; | ||
| 95 | |||
| 96 | for my $frame_data (@$frames) | ||
| 97 | { | ||
| 98 | my ($filename, $delay_ms) = @$frame_data ; | ||
| 99 | |||
| 100 | my $image = Image::Magick->new() ; | ||
| 101 | my $status = $image->Read($filename) ; | ||
| 102 | |||
| 103 | if ($status) | ||
| 104 | { | ||
| 105 | push @$errors, "Failed to load $filename: $status" ; | ||
| 106 | |||
| 107 | if ($previous_valid) | ||
| 108 | { | ||
| 109 | my $blank = $previous_valid->Clone() ; | ||
| 110 | $blank->Quantize(colorspace => 'Transparent') ; | ||
| 111 | |||
| 112 | push @$loaded, | ||
| 113 | { | ||
| 114 | image => $blank, | ||
| 115 | delay => $delay_ms, | ||
| 116 | width => $previous_valid->Get('width'), | ||
| 117 | height => $previous_valid->Get('height') | ||
| 118 | } ; | ||
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | push @$errors, "Cannot create blank frame: no previous valid frame" ; | ||
| 123 | } | ||
| 124 | |||
| 125 | next ; | ||
| 126 | } | ||
| 127 | |||
| 128 | my $width = $image->Get('width') ; | ||
| 129 | my $height = $image->Get('height') ; | ||
| 130 | |||
| 131 | push @$loaded, | ||
| 132 | { | ||
| 133 | image => $image, | ||
| 134 | delay => $delay_ms, | ||
| 135 | width => $width, | ||
| 136 | height => $height | ||
| 137 | } ; | ||
| 138 | |||
| 139 | $previous_valid = $image ; | ||
| 140 | } | ||
| 141 | |||
| 142 | return $loaded ; | ||
| 143 | } | ||
| 144 | |||
| 145 | #---------------------------------------------------------------------------------------------- | ||
| 146 | |||
| 147 | sub normalize_frames | ||
| 148 | { | ||
| 149 | my ($frames, $options, $errors) = @_ ; | ||
| 150 | |||
| 151 | my ($max_width, $max_height) ; | ||
| 152 | |||
| 153 | if ($options->{target_resolution}) | ||
| 154 | { | ||
| 155 | ($max_width, $max_height) = @{$options->{target_resolution}} ; | ||
| 156 | } | ||
| 157 | else | ||
| 158 | { | ||
| 159 | $max_width = 0 ; | ||
| 160 | $max_height = 0 ; | ||
| 161 | |||
| 162 | for my $frame (@$frames) | ||
| 163 | { | ||
| 164 | $max_width = $frame->{width} if $frame->{width} > $max_width ; | ||
| 165 | $max_height = $frame->{height} if $frame->{height} > $max_height ; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | my $bg_color = $options->{background_color} ; | ||
| 170 | my $bg_string = sprintf | ||
| 171 | ( | ||
| 172 | 'rgba(%d,%d,%d,%f)', | ||
| 173 | $bg_color->[0], | ||
| 174 | $bg_color->[1], | ||
| 175 | $bg_color->[2], | ||
| 176 | $bg_color->[3] / 255.0 | ||
| 177 | ) ; | ||
| 178 | |||
| 179 | for my $frame (@$frames) | ||
| 180 | { | ||
| 181 | next if $frame->{width} == $max_width && $frame->{height} == $max_height ; | ||
| 182 | |||
| 183 | my $canvas = Image::Magick->new(size => "${max_width}x${max_height}") ; | ||
| 184 | $canvas->Read("xc:$bg_string") ; | ||
| 185 | |||
| 186 | my $x_offset = int(($max_width - $frame->{width}) / 2) ; | ||
| 187 | my $y_offset = int(($max_height - $frame->{height}) / 2) ; | ||
| 188 | |||
| 189 | $canvas->Composite | ||
| 190 | ( | ||
| 191 | image => $frame->{image}, | ||
| 192 | x => $x_offset, | ||
| 193 | y => $y_offset, | ||
| 194 | compose => 'Over' | ||
| 195 | ) ; | ||
| 196 | |||
| 197 | $frame->{image} = $canvas ; | ||
| 198 | $frame->{width} = $max_width ; | ||
| 199 | $frame->{height} = $max_height ; | ||
| 200 | } | ||
| 201 | |||
| 202 | return $frames ; | ||
| 203 | } | ||
| 204 | |||
| 205 | #---------------------------------------------------------------------------------------------- | ||
| 206 | |||
| 207 | sub optimize_palettes | ||
| 208 | { | ||
| 209 | my ($frames, $errors) = @_ ; | ||
| 210 | |||
| 211 | for my $frame (@$frames) | ||
| 212 | { | ||
| 213 | my $img = $frame->{image} ; | ||
| 214 | |||
| 215 | my $status = $img->Quantize | ||
| 216 | ( | ||
| 217 | colors => 256, | ||
| 218 | colorspace => 'RGB', | ||
| 219 | dither => 'True', | ||
| 220 | treedepth => 8, | ||
| 221 | ) ; | ||
| 222 | |||
| 223 | push @$errors, "Palette optimization failed: $status" if $status ; | ||
| 224 | |||
| 225 | $img->Set(type => 'Palette') ; | ||
| 226 | } | ||
| 227 | |||
| 228 | return $frames ; | ||
| 229 | } | ||
| 230 | |||
| 231 | #---------------------------------------------------------------------------------------------- | ||
| 232 | |||
| 233 | sub assemble_apng | ||
| 234 | { | ||
| 235 | my ($frames, $options, $errors) = @_ ; | ||
| 236 | |||
| 237 | my $first_frame = $frames->[0] ; | ||
| 238 | my $width = $first_frame->{width} ; | ||
| 239 | my $height = $first_frame->{height} ; | ||
| 240 | |||
| 241 | my $png_signature = pack('C8', 137, 80, 78, 71, 13, 10, 26, 10) ; | ||
| 242 | |||
| 243 | my $ihdr = create_ihdr($width, $height, $first_frame->{image}) ; | ||
| 244 | my $actl = create_actl(scalar @$frames, $options->{loop_count}) ; | ||
| 245 | |||
| 246 | my $sequence = 0 ; | ||
| 247 | my $chunks = '' ; | ||
| 248 | |||
| 249 | for my $i (0 .. $#$frames) | ||
| 250 | { | ||
| 251 | my $frame = $frames->[$i] ; | ||
| 252 | my $fctl = create_fctl | ||
| 253 | ( | ||
| 254 | $sequence++, | ||
| 255 | $frame->{width}, | ||
| 256 | $frame->{height}, | ||
| 257 | 0, | ||
| 258 | 0, | ||
| 259 | $frame->{delay}, | ||
| 260 | 1000, | ||
| 261 | $options->{disposal_method}, | ||
| 262 | $options->{blend_operation}, | ||
| 263 | ) ; | ||
| 264 | |||
| 265 | $chunks .= $fctl ; | ||
| 266 | |||
| 267 | my $frame_data = get_compressed_image_data($frame->{image}) ; | ||
| 268 | |||
| 269 | if ($i == 0) | ||
| 270 | { | ||
| 271 | $chunks .= create_idat($frame_data) ; | ||
| 272 | } | ||
| 273 | else | ||
| 274 | { | ||
| 275 | $chunks .= create_fdat($sequence++, $frame_data) ; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | my $iend = create_iend() ; | ||
| 280 | |||
| 281 | return $png_signature . $ihdr . $actl . $chunks . $iend ; | ||
| 282 | } | ||
| 283 | |||
| 284 | #---------------------------------------------------------------------------------------------- | ||
| 285 | |||
| 286 | sub create_ihdr | ||
| 287 | { | ||
| 288 | my ($width, $height, $image) = @_ ; | ||
| 289 | |||
| 290 | my $bit_depth = $image->Get('depth') || 8 ; | ||
| 291 | my $color_type = get_color_type($image) ; | ||
| 292 | |||
| 293 | my $data = pack | ||
| 294 | ('N2C5', | ||
| 295 | $width, | ||
| 296 | $height, | ||
| 297 | $bit_depth, | ||
| 298 | $color_type, | ||
| 299 | 0, | ||
| 300 | 0, | ||
| 301 | 0, | ||
| 302 | ) ; | ||
| 303 | |||
| 304 | return create_chunk('IHDR', $data) ; | ||
| 305 | } | ||
| 306 | |||
| 307 | #---------------------------------------------------------------------------------------------- | ||
| 308 | |||
| 309 | sub get_color_type | ||
| 310 | { | ||
| 311 | my ($image) = @_ ; | ||
| 312 | |||
| 313 | my $type = $image->Get('type') ; | ||
| 314 | my $matte = $image->Get('matte') ; | ||
| 315 | |||
| 316 | return 6 if $matte ; | ||
| 317 | return 3 if $type eq 'Palette' ; | ||
| 318 | return 2 if $type eq 'TrueColor' ; | ||
| 319 | return 0 if $type eq 'Grayscale' ; | ||
| 320 | return 4 if $type eq 'GrayscaleMatte' ; | ||
| 321 | |||
| 322 | return 6 ; | ||
| 323 | } | ||
| 324 | |||
| 325 | #---------------------------------------------------------------------------------------------- | ||
| 326 | |||
| 327 | sub create_actl | ||
| 328 | { | ||
| 329 | my ($num_frames, $num_plays) = @_ ; | ||
| 330 | |||
| 331 | my $data = pack('N2', $num_frames, $num_plays) ; | ||
| 332 | return create_chunk('acTL', $data) ; | ||
| 333 | } | ||
| 334 | |||
| 335 | #---------------------------------------------------------------------------------------------- | ||
| 336 | |||
| 337 | sub create_fctl | ||
| 338 | { | ||
| 339 | my ($sequence, $width, $height, $x_offset, $y_offset, $delay_num, $delay_den, $dispose_op, $blend_op) = @_ ; | ||
| 340 | |||
| 341 | my $data = pack | ||
| 342 | ('N5n2C2', | ||
| 343 | $sequence, | ||
| 344 | $width, | ||
| 345 | $height, | ||
| 346 | $x_offset, | ||
| 347 | $y_offset, | ||
| 348 | $delay_num, | ||
| 349 | $delay_den, | ||
| 350 | $dispose_op, | ||
| 351 | $blend_op, | ||
| 352 | ) ; | ||
| 353 | |||
| 354 | return create_chunk('fcTL', $data) ; | ||
| 355 | } | ||
| 356 | |||
| 357 | #---------------------------------------------------------------------------------------------- | ||
| 358 | |||
| 359 | sub create_idat | ||
| 360 | { | ||
| 361 | my ($compressed_data) = @_ ; | ||
| 362 | |||
| 363 | return create_chunk('IDAT', $compressed_data) ; | ||
| 364 | } | ||
| 365 | |||
| 366 | #---------------------------------------------------------------------------------------------- | ||
| 367 | |||
| 368 | sub create_fdat | ||
| 369 | { | ||
| 370 | my ($sequence, $compressed_data) = @_ ; | ||
| 371 | |||
| 372 | my $data = pack('N', $sequence) . $compressed_data ; | ||
| 373 | return create_chunk('fdAT', $data) ; | ||
| 374 | } | ||
| 375 | |||
| 376 | #---------------------------------------------------------------------------------------------- | ||
| 377 | |||
| 378 | sub create_iend | ||
| 379 | { | ||
| 380 | return create_chunk('IEND', '') ; | ||
| 381 | } | ||
| 382 | |||
| 383 | #---------------------------------------------------------------------------------------------- | ||
| 384 | |||
| 385 | sub create_chunk | ||
| 386 | { | ||
| 387 | my ($type, $data) = @_ ; | ||
| 388 | |||
| 389 | my $length = length($data) ; | ||
| 390 | my $crc = calculate_crc($type . $data) ; | ||
| 391 | |||
| 392 | return pack('N', $length) . $type . $data . pack('N', $crc) ; | ||
| 393 | } | ||
| 394 | |||
| 395 | #---------------------------------------------------------------------------------------------- | ||
| 396 | |||
| 397 | sub calculate_crc | ||
| 398 | { | ||
| 399 | my ($data) = @_ ; | ||
| 400 | |||
| 401 | my $crc = 0xFFFFFFFF ; | ||
| 402 | my @crc_table ; | ||
| 403 | |||
| 404 | unless (@crc_table) | ||
| 405 | { | ||
| 406 | for my $n (0 .. 255) | ||
| 407 | { | ||
| 408 | my $c = $n ; | ||
| 409 | for my $k (0 .. 7) | ||
| 410 | { | ||
| 411 | if ($c & 1) | ||
| 412 | { | ||
| 413 | $c = 0xEDB88320 ^ ($c >> 1) ; | ||
| 414 | } | ||
| 415 | else | ||
| 416 | { | ||
| 417 | $c = $c >> 1 ; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | $crc_table[$n] = $c ; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | for my $byte (unpack('C*', $data)) | ||
| 425 | { | ||
| 426 | $crc = $crc_table[($crc ^ $byte) & 0xFF] ^ ($crc >> 8) ; | ||
| 427 | } | ||
| 428 | |||
| 429 | return $crc ^ 0xFFFFFFFF ; | ||
| 430 | } | ||
| 431 | |||
| 432 | #---------------------------------------------------------------------------------------------- | ||
| 433 | |||
| 434 | sub get_compressed_image_data | ||
| 435 | { | ||
| 436 | my ($image) = @_ ; | ||
| 437 | |||
| 438 | my $temp_file = "/tmp/apng_temp_$$.png" ; | ||
| 439 | $image->Write(filename => $temp_file, compression => 9) ; | ||
| 440 | |||
| 441 | open my $fh, '<', $temp_file or die "Cannot open temp file: $!" ; | ||
| 442 | binmode $fh ; | ||
| 443 | |||
| 444 | my $png_data = do { local $/; <$fh> } ; | ||
| 445 | |||
| 446 | close $fh ; | ||
| 447 | unlink $temp_file ; | ||
| 448 | |||
| 449 | my $idat_data = '' ; | ||
| 450 | my $pos = 8 ; | ||
| 451 | |||
| 452 | while ($pos < length($png_data)) | ||
| 453 | { | ||
| 454 | my $chunk_length = unpack('N', substr($png_data, $pos, 4)) ; | ||
| 455 | my $chunk_type = substr($png_data, $pos + 4, 4) ; | ||
| 456 | my $chunk_data = substr($png_data, $pos + 8, $chunk_length) ; | ||
| 457 | |||
| 458 | $idat_data .= $chunk_data if $chunk_type eq 'IDAT' ; | ||
| 459 | |||
| 460 | $pos += 12 + $chunk_length ; | ||
| 461 | } | ||
| 462 | |||
| 463 | return $idat_data ; | ||
| 464 | } | ||
| 465 | |||
| 466 | #---------------------------------------------------------------------------------------------- | ||
| 467 | |||
| 468 | 1; | ||
| 469 | |||
| 470 | =head1 OPTIONS | ||
| 471 | |||
| 472 | =over 4 | ||
| 473 | |||
| 474 | =item optimize_palette | ||
| 475 | |||
| 476 | Boolean. Convert frames to 8-bit indexed PNG. Default: 0 | ||
| 477 | |||
| 478 | =item normalize_resolution | ||
| 479 | |||
| 480 | Boolean. Make all frames the same resolution. Default: 0 | ||
| 481 | |||
| 482 | =item target_resolution | ||
| 483 | |||
| 484 | Array reference [width, height]. Override automatic resolution. Default: undef | ||
| 485 | |||
| 486 | =item background_color | ||
| 487 | |||
| 488 | Array reference [R, G, B, A] (0-255). Background for smaller frames. Default: [0, 0, 0, 0] | ||
| 489 | |||
| 490 | =item loop_count | ||
| 491 | |||
| 492 | Integer. Animation loops (0 = infinite). Default: 0 | ||
| 493 | |||
| 494 | =item disposal_method | ||
| 495 | |||
| 496 | Integer (0-2). Frame disposal method. Default: 1 | ||
| 497 | |||
| 498 | =item blend_operation | ||
| 499 | |||
| 500 | Integer (0-1). Frame blending operation. Default: 1 | ||
| 501 | |||
| 502 | =back | ||
| 503 | |||
| 504 | =head1 RETURN VALUE | ||
| 505 | |||
| 506 | Hash reference with keys: | ||
| 507 | |||
| 508 | =over 4 | ||
| 509 | |||
| 510 | =item status | ||
| 511 | |||
| 512 | 0 for success, 1 for error | ||
| 513 | |||
| 514 | =item errors | ||
| 515 | |||
| 516 | Array reference of error messages | ||
| 517 | |||
| 518 | =item data | ||
| 519 | |||
| 520 | Binary APNG data (undef on error) | ||
| 521 | |||
| 522 | =back | ||
| 523 | |||
| 524 | =head1 AUTHOR | ||
| 525 | |||
| 526 | Generated for APNG creation | ||
| 527 | |||
| 528 | =cut | ||