Add animation extractor script

main
Christian Seiler 1 week ago
parent debce36336
commit 4f36b72f5c
  1. 58
      php/extract_animation.php

@ -0,0 +1,58 @@
<?php
include_once("monoformat_schema.php");
include_once("monoformat_structured.php");
if (count($argv) != 4) {
fwrite(STDERR, "Usage: $argv[0] binfile index outprefix\n");
exit(1);
}
$binfile = $argv[1];
$index = (int) $argv[2];
$outprefix = $argv[3];
$file = monoformat\parseFile(file_get_contents($binfile));
$animations = [];
foreach ($file->sections as $section) {
for ($i = 0; $i < $section->elementCount(); ++$i) {
$element = $section->elementAt($i);
if ($element->elementType() == monoformat\ElementType::Animation) {
array_push($animations, $element);
}
}
}
if ($index < 0 || $index >= count($animations)) {
$n = count($animations);
$nm1 = $n - 1;
fwrite(STDERR, "$argv[0]: the animation file contains $n animations, index $index is invalid (must be 0 .. $nm1)\n");
exit(2);
}
$animation = $animations[$index];
$height = $animation->height();
$width = $animation->width();
$nFrames = $animation->numberOfFrames();
for ($f = 0; $f < $nFrames; ++$f) {
$outfile = sprintf("%s_%04d.png", $outprefix, $f);
print("Writing $outfile...\n");
$input = $animation->image($f);
$image = imagecreatetruecolor($width, $height);
$bg = imagecolorallocate($image, 0, 0, 0);
$fg = imagecolorallocate($image, 255, 255, 255);
for ($y = 0; $y < $height; ++$y) {
for ($x = 0; $x < $width; ++$x) {
$i = $y * $width + $x;
if ($input[$i]) {
imagesetpixel($image, $x, $y, $fg);
} else {
imagesetpixel($image, $x, $y, $bg);
}
}
}
imagepng($image, $outfile);
}
?>
Loading…
Cancel
Save