image2ass improvements
This commit is contained in:
parent
5909f4790e
commit
087d992842
398
image2ass.php
398
image2ass.php
|
@ -2,342 +2,34 @@
|
|||
|
||||
require_once __DIR__ . "/vendor/autoload.php";
|
||||
|
||||
const SWFMILL_BINARY = "swfmill";
|
||||
const ADVANCED_MOTION_INTERPOLATION = true;
|
||||
function outputFrame($frame, $endFrame, $frameDurationMs) {
|
||||
foreach ($frame[2]->commands as $path) {
|
||||
/** @var \swf2ass\DrawPath $path */
|
||||
$line = new \swf2ass\ass\ASSLine();
|
||||
$line->objectId = 0;
|
||||
$line->layer = [$frame[0]];
|
||||
$line->start = $frame[1];
|
||||
$line->end = $endFrame;
|
||||
$line->style = "f";
|
||||
$line->name = $frame[3];
|
||||
$shape = $path->commands;
|
||||
|
||||
|
||||
function normalizePath($path) {
|
||||
$newPath = [];
|
||||
$lastCursor = new \swf2ass\Vector2(0, 0);
|
||||
foreach ($path as $e) {
|
||||
/** @var \swf2ass\LineRecord|\swf2ass\MoveRecord $e */
|
||||
|
||||
if (!$e->start->equals($lastCursor)) {
|
||||
$newPath[] = new \swf2ass\MoveRecord($e->start, $lastCursor);
|
||||
$line->tags = [
|
||||
new \swf2ass\ass\borderTag(0),
|
||||
new \swf2ass\ass\shadowTag(0),
|
||||
\swf2ass\ass\fillColorTag::fromStyleRecord($path->style)
|
||||
];
|
||||
if($shape->edges[0] instanceof \swf2ass\MoveRecord){
|
||||
$shape = (new \swf2ass\MatrixTransform(null, null, $shape->edges[0]->coord->multiply(-1)))->applyToShape($shape);
|
||||
}
|
||||
$newPath[] = $e;
|
||||
$lastCursor = $e->coord;
|
||||
}
|
||||
|
||||
return $newPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \swf2ass\LineRecord[] $path
|
||||
*/
|
||||
function cleanupPath(array $path) {
|
||||
$path = array_values($path);
|
||||
/** @var \swf2ass\LineRecord[] $newPath */
|
||||
$newPath = [];
|
||||
$currentPath = null;
|
||||
|
||||
$cornerMap = [];
|
||||
foreach ($path as $i => $p) {
|
||||
if (!isset($cornerMap[$p->start->x . "_" . $p->start->y])) {
|
||||
$cornerMap[$p->start->x . "_" . $p->start->y] = [];
|
||||
}
|
||||
$cornerMap[$p->start->x . "_" . $p->start->y][$i] = $p;
|
||||
if (!isset($cornerMap[$p->coord->x . "_" . $p->coord->y])) {
|
||||
$cornerMap[$p->coord->x . "_" . $p->coord->y] = [];
|
||||
}
|
||||
$cornerMap[$p->coord->x . "_" . $p->coord->y][$i] = $p;
|
||||
}
|
||||
|
||||
$index = 0;
|
||||
while (count($path) > 0) {
|
||||
$currentPath = $path[$index++] ?? null;
|
||||
if ($currentPath !== null) {
|
||||
$dupes = 0;
|
||||
foreach ($cornerMap[$currentPath->start->x . "_" . $currentPath->start->y] as $i => $p) {
|
||||
if ((($currentPath->start->equals($p->start) and $currentPath->coord->equals($p->coord)) or ($currentPath->start->equals($p->coord) and $currentPath->coord->equals($p->start)))) {
|
||||
unset($path[$i]);
|
||||
unset($cornerMap[$k1 = $p->start->x . "_" . $p->start->y][$i]);
|
||||
unset($cornerMap[$k2 = $p->coord->x . "_" . $p->coord->y][$i]);
|
||||
if ($currentPath !== $p) {
|
||||
++$dupes;
|
||||
}
|
||||
|
||||
if (count($cornerMap[$k1]) === 0) {
|
||||
unset($cornerMap[$k1]);
|
||||
}
|
||||
if (count($cornerMap[$k2]) === 0) {
|
||||
unset($cornerMap[$k2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($dupes === 0) {
|
||||
$newPath[] = $currentPath;
|
||||
}
|
||||
|
||||
//fwrite(STDERR, "\rdup " . count($newPath) . "/" . count($path) . " ");
|
||||
}
|
||||
}
|
||||
//fwrite(STDERR, "\n");
|
||||
|
||||
$cornerMap = [];
|
||||
foreach ($newPath as $i => $p) {
|
||||
if (!isset($cornerMap[$p->start->x . "_" . $p->start->y])) {
|
||||
$cornerMap[$p->start->x . "_" . $p->start->y] = [];
|
||||
}
|
||||
$cornerMap[$p->start->x . "_" . $p->start->y][$i] = $p;
|
||||
if (!isset($cornerMap[$p->coord->x . "_" . $p->coord->y])) {
|
||||
$cornerMap[$p->coord->x . "_" . $p->coord->y] = [];
|
||||
}
|
||||
$cornerMap[$p->coord->x . "_" . $p->coord->y][$i] = $p;
|
||||
}
|
||||
|
||||
$sortedPath = [];
|
||||
$startPath = null;
|
||||
$currentVector = null;
|
||||
$index = 0;
|
||||
while (count($newPath) > 0) {
|
||||
$currentPath = $newPath[$index++] ?? null;
|
||||
if ($currentPath !== null) {
|
||||
$startPath = $currentPath;
|
||||
$currentVector = $currentPath->coord->sub($currentPath->start);
|
||||
$sortedPath[] = $startPath;
|
||||
unset($newPath[$index - 1]);
|
||||
unset($cornerMap[$startPath->start->x . "_" . $startPath->start->y][$index - 1]);
|
||||
unset($cornerMap[$startPath->coord->x . "_" . $startPath->coord->y][$index - 1]);
|
||||
|
||||
while (($nextPath = findNextCorner($currentPath->coord, $newPath, $cornerMap)) !== null) {
|
||||
$nextVector = $nextPath->coord->sub($nextPath->start);
|
||||
if ($nextVector->equals($currentVector)) { //Enlongate
|
||||
$currentPath->coord = $nextPath->coord;
|
||||
continue;
|
||||
}
|
||||
$sortedPath[] = $nextPath;
|
||||
|
||||
if ($nextPath->coord->equals($nextPath->start)) { //Reached the end!
|
||||
$startPath = null;
|
||||
break;
|
||||
}
|
||||
$currentPath = $nextPath;
|
||||
$currentVector = $nextVector;
|
||||
}
|
||||
//fwrite(STDERR, "\rsort " . count($sortedPath) . "/" . count($newPath) . " ");
|
||||
}
|
||||
|
||||
}
|
||||
//fwrite(STDERR, "\n");
|
||||
|
||||
return $sortedPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \swf2ass\Vector2 $corner
|
||||
* @param \swf2ass\LineRecord[] $path
|
||||
*
|
||||
* @return ?\swf2ass\LineRecord
|
||||
*/
|
||||
function findNextCorner(\swf2ass\Vector2 $corner, array &$path, array &$cornerMap): ?\swf2ass\LineRecord {
|
||||
foreach ($cornerMap[$corner->x . "_" . $corner->y] as $i => $p) {
|
||||
unset($path[$i]);
|
||||
unset($cornerMap[$p->start->x . "_" . $p->start->y][$i]);
|
||||
unset($cornerMap[$p->coord->x . "_" . $p->coord->y][$i]);
|
||||
if ($p->start->equals($corner)) {
|
||||
return $p;
|
||||
} else if ($p->coord->equals($corner)) {
|
||||
return $p->reverse();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function offsetPath($path, \swf2ass\Vector2 $offset): array {
|
||||
$newPath = [];
|
||||
foreach ($path as $e) {
|
||||
|
||||
if ($e instanceof \swf2ass\LineRecord) {
|
||||
$newPath[] = new \swf2ass\LineRecord($e->coord->add($offset), $e->start->add($offset));
|
||||
} else if ($e instanceof \swf2ass\MoveRecord) {
|
||||
$newPath[] = new \swf2ass\MoveRecord($e->coord->add($offset), $e->start->add($offset));
|
||||
}
|
||||
|
||||
$newPath[] = $e;
|
||||
}
|
||||
|
||||
return $newPath;
|
||||
}
|
||||
|
||||
function getBoundingBox($path) {
|
||||
$bb = new \swf2ass\Rectangle(new \swf2ass\Vector2(PHP_INT_MAX, PHP_INT_MAX), new \swf2ass\Vector2(PHP_INT_MIN, PHP_INT_MIN));
|
||||
/*if(count($path) > 0){
|
||||
$first = reset($path);
|
||||
if($first instanceof MoveRecord or $first instanceof LineRecord){
|
||||
$bb->topLeft = $first->coord;
|
||||
$bb->bottomRight = $first->coord;
|
||||
}
|
||||
}*/
|
||||
foreach ($path as $i => $e) {
|
||||
/** @var \swf2ass\LineRecord|\swf2ass\MoveRecord $e */
|
||||
if ($i === 0 and $e instanceof \swf2ass\MoveRecord) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$bb->inBounds($e->coord)) {
|
||||
$bb->topLeft->x = min($e->coord->x, $bb->topLeft->x);
|
||||
$bb->topLeft->y = min($e->coord->y, $bb->topLeft->y);
|
||||
$bb->bottomRight->x = max($e->coord->x, $bb->bottomRight->x);
|
||||
$bb->bottomRight->y = max($e->coord->y, $bb->bottomRight->y);
|
||||
}
|
||||
if (!$bb->inBounds($e->start)) {
|
||||
$bb->topLeft->x = min($e->start->x, $bb->topLeft->x);
|
||||
$bb->topLeft->y = min($e->start->y, $bb->topLeft->y);
|
||||
$bb->bottomRight->x = max($e->start->x, $bb->bottomRight->x);
|
||||
$bb->bottomRight->y = max($e->start->y, $bb->bottomRight->y);
|
||||
}
|
||||
}
|
||||
|
||||
return $bb;
|
||||
}
|
||||
|
||||
function calculateAreaOfPath($path) {
|
||||
$totalArea = 0;
|
||||
/** @var \swf2ass\Vector2[][] $subPolygons */
|
||||
$subPolygons = [];
|
||||
/** @var \swf2ass\Vector2[] $currentPolygon */
|
||||
$currentPolygon = [];
|
||||
foreach ($path as $n) {
|
||||
if ($n instanceof \swf2ass\MoveRecord) {
|
||||
if (count($currentPolygon) > 0) {
|
||||
$subPolygons[] = $currentPolygon;
|
||||
$currentPolygon = [];
|
||||
}
|
||||
} else if ($n instanceof \swf2ass\LineRecord) {
|
||||
if (count($currentPolygon) === 0) {
|
||||
$currentPolygon[] = $n->start;
|
||||
}
|
||||
$currentPolygon[] = $n->coord;
|
||||
}
|
||||
}
|
||||
if (count($currentPolygon) > 0) {
|
||||
$subPolygons[] = $currentPolygon;
|
||||
$currentPolygon = [];
|
||||
}
|
||||
|
||||
foreach ($subPolygons as $polygon) {
|
||||
$NumPoints = count($polygon);
|
||||
|
||||
if ($polygon[$NumPoints - 1]->equals($polygon[0])) {
|
||||
$NumPoints--;
|
||||
} else {
|
||||
//Add the first point at the end of the array.
|
||||
$polygon[$NumPoints] = $polygon[0];
|
||||
}
|
||||
|
||||
if ($NumPoints < 3) {
|
||||
break;
|
||||
} else {
|
||||
$area = 0;
|
||||
$lastPoint = $polygon[$NumPoints - 1];
|
||||
foreach ($polygon as $point) {
|
||||
$area += ($lastPoint->x * $point->y - $lastPoint->y * $point->x);
|
||||
$lastPoint = $point;
|
||||
}
|
||||
$totalArea += ($area / 2.0);
|
||||
}
|
||||
}
|
||||
return $totalArea;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \swf2ass\Color[][] $pixelMatrix
|
||||
* @param int $resX
|
||||
* @param int $resY
|
||||
* @param bool $doFullFrameOptimizations
|
||||
* @param $size
|
||||
* @return array
|
||||
*/
|
||||
function renderFrame(array $pixelMatrix, int $resX, int $resY, bool $doFullFrameOptimizations, &$size): array {
|
||||
$size = 0;
|
||||
$colors = [];
|
||||
$paths = [];
|
||||
foreach ($pixelMatrix as $y => $row) {
|
||||
foreach ($row as $x => $color) {
|
||||
$k = $color->toString(true);
|
||||
if (!isset($paths[$k])) {
|
||||
$colors[$k] = $color;
|
||||
$paths[$k] = [];
|
||||
}
|
||||
foreach ((new \swf2ass\Square(new \swf2ass\Vector2($x, $y), 1))->draw() as $p) {
|
||||
$paths[$k][] = $p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$optimizedPaths = [];
|
||||
|
||||
foreach ($paths as $k => $path) {
|
||||
$path = cleanupPath($path);
|
||||
$path = normalizePath($path);
|
||||
$optimizedPaths[$k] = $path;
|
||||
}
|
||||
|
||||
if ($doFullFrameOptimizations) {
|
||||
uasort($optimizedPaths, function ($a, $b) {
|
||||
return calculateAreaOfPath($a) - calculateAreaOfPath($b);
|
||||
});
|
||||
|
||||
//Largest path
|
||||
$k0 = array_key_first($optimizedPaths);
|
||||
$path = $optimizedPaths[$k0];
|
||||
$optimizedPaths[$k0] = [];
|
||||
foreach ((new \swf2ass\Rectangle(new \swf2ass\Vector2(0, 0), new \swf2ass\Vector2($resX, $resY)))->draw() as $p) {
|
||||
$optimizedPaths[$k0][] = $p;
|
||||
}
|
||||
}
|
||||
|
||||
$assLines = [];
|
||||
|
||||
foreach ($optimizedPaths as $k => $path) {
|
||||
$pos = new \swf2ass\Vector2(0, 0);
|
||||
$bb = getBoundingBox($path);
|
||||
$pos = $bb->topLeft;
|
||||
/*$first = reset($path);
|
||||
if($first instanceof MoveRecord){
|
||||
$pos = $first->coord;
|
||||
unset($path[0]);// = new MoveRecord(new Coordinate(0, 0), $pos);
|
||||
}*/
|
||||
|
||||
if (count($path) <= 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$assLine = "{\\an7\\bord0\\shad0\\pos({$pos->x},{$pos->y})";
|
||||
$assLine .= $colors[$k]->toString(true) . "\\p1}";
|
||||
|
||||
foreach ($path as $edge) {
|
||||
if ($edge instanceof \swf2ass\MoveRecord) {
|
||||
$coords = $edge->coord->sub($pos);
|
||||
$assLine .= "m " . $coords->x . " " . $coords->y . " ";
|
||||
} else if ($edge instanceof \swf2ass\LineRecord) {
|
||||
$coords = $edge->coord->sub($pos);
|
||||
$assLine .= "l " . $coords->x . " " . $coords->y . " ";
|
||||
}
|
||||
}
|
||||
$assLine .= "{\\p0}";
|
||||
|
||||
$assLines[] = $assLine;
|
||||
$size += strlen($assLine);
|
||||
}
|
||||
|
||||
return $assLines;
|
||||
}
|
||||
|
||||
function outputFrame($frame, $endTime) {
|
||||
$assHeader = "Dialogue: " . $frame[0] . "," . \swf2ass\Utils::timeToStamp($frame[1]) . "," . \swf2ass\Utils::timeToStamp($endTime) . ",f," . $frame[3] . ",0,0,0,,";
|
||||
foreach ($frame[2] as $line) {
|
||||
echo $assHeader . $line . PHP_EOL;
|
||||
$line->tags[] = new \swf2ass\ass\positionTag($shape->edges[0]->getStart()->divide(-\swf2ass\Constants::TWIP_SIZE), $shape->edges[0]->getStart()->divide(-\swf2ass\Constants::TWIP_SIZE), $endFrame, $endFrame);
|
||||
$line->tags[] = new \swf2ass\ass\drawTag($shape, 1);
|
||||
echo $line->encode($frameDurationMs) . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
$fps = 30000 / 1001;//30;
|
||||
$colorN = 2;//64;
|
||||
$colorN = 64;
|
||||
$pframeColorDistance = 16;
|
||||
|
||||
if (is_dir($argv[1])) {
|
||||
|
@ -351,7 +43,7 @@ sort($frames);
|
|||
$frameBuffer = null;
|
||||
$quantizedFrameBuffer = $frameBuffer;
|
||||
|
||||
$dynamicPalette = false;
|
||||
$dynamicPalette = true;
|
||||
$palette = [];
|
||||
for ($i = 0; $i < $colorN; ++$i) {
|
||||
$palette[] = new \swf2ass\Color((int)round($i * (255 / ($colorN - 1))), (int)round($i * (255 / ($colorN - 1))), (int)round($i * (255 / ($colorN - 1))));
|
||||
|
@ -411,8 +103,8 @@ foreach ($frames as $frame) {
|
|||
$quantizedPframe[$y] = [];
|
||||
for ($x = 0; $x < $resX; ++$x) {
|
||||
$pixel = $im->getImagePixelColor($x, $y);
|
||||
$c = $pixel->getColor(true);
|
||||
$color = new \swf2ass\Color((int)round($c["r"] * 255), (int)round($c["g"] * 255), (int)round($c["b"] * 255), isset($c["a"]) ? (int)round((1 - $c["a"]) * 255) : null);
|
||||
$c = $pixel->getColor(2);
|
||||
$color = new \swf2ass\Color($c["r"], $c["g"], $c["b"], $c["a"] ?? 255);
|
||||
|
||||
$iframe[$y][$x] = $color;
|
||||
if ($frameBuffer !== null and $frameBuffer[$y][$x]->distance($color) > $pframeColorDistance) {
|
||||
|
@ -430,8 +122,8 @@ foreach ($frames as $frame) {
|
|||
foreach ($iframe as $y => $row) {
|
||||
foreach ($row as $x => $color) {
|
||||
$pixel = $im->getImagePixelColor($x, $y);
|
||||
$c = $pixel->getColor(true);
|
||||
$color = new \swf2ass\Color((int)round($c["r"] * 255), (int)round($c["g"] * 255), (int)round($c["b"] * 255), isset($c["a"]) ? (int)round((1 - $c["a"]) * 255) : null);
|
||||
$c = $pixel->getColor(2);
|
||||
$color = new \swf2ass\Color($c["r"], $c["g"], $c["b"], $c["a"] ?? 255);
|
||||
|
||||
|
||||
if (!$dynamicPalette) {
|
||||
|
@ -450,8 +142,8 @@ foreach ($frames as $frame) {
|
|||
$headerFps = $fps * 2;
|
||||
echo <<<ASSHEADER
|
||||
[Script Info]
|
||||
; Script generated by Aegisub 3.2.2
|
||||
; http://www.aegisub.org/
|
||||
; Script generated by image2ass from swf2ass
|
||||
; https://git.gammaspectra.live/WeebDataHoarder/swf2ass/
|
||||
ScriptType: v4.00+
|
||||
WrapStyle: 0
|
||||
ScaledBorderAndShadow: yes
|
||||
|
@ -476,34 +168,42 @@ ASSHEADER;
|
|||
fwrite(STDERR, "=== frame $frameNumber\n");
|
||||
|
||||
$iframeSize = "---";
|
||||
$pframeRender = renderFrame($quantizedPframe, $resX, $resY, false, $pframeSize);
|
||||
if ($frameBuffer === null or $pframeSize > 0) {
|
||||
$iframeRender = renderFrame($quantizedIframe, $resX, $resY, true, $iframeSize);
|
||||
|
||||
$pframeRender = (new \swf2ass\BitmapConverter($quantizedPframe))->render(false);
|
||||
$pframePathCount = array_reduce($pframeRender->commands, function (int $item, \swf2ass\DrawPath $path){
|
||||
return $item + count($path->commands->edges);
|
||||
}, 0);
|
||||
|
||||
if ($frameBuffer === null or $pframePathCount > 0) {
|
||||
$iframeRender = (new \swf2ass\BitmapConverter($quantizedIframe))->render(true);
|
||||
$iframePathCount = array_reduce($iframeRender->commands, function (int $item, \swf2ass\DrawPath $path){
|
||||
return $item + count($path->commands->edges);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
if ($frameBuffer === null or ($pframeSize > 0 and $iframeSize <= $pframeSize)) {
|
||||
fwrite(STDERR, " | IS IFRAME $frameNumber |$iframeSize|$pframeSize\n");
|
||||
if ($frameBuffer === null or ($pframePathCount > 0 and $iframePathCount <= $pframePathCount)) {
|
||||
fwrite(STDERR, " | IS IFRAME $frameNumber |$iframePathCount|$pframePathCount\n");
|
||||
$frameBuffer = $iframe;
|
||||
$quantizedFrameBuffer = $quantizedIframe;
|
||||
foreach ($currentFrames as $f) {
|
||||
outputFrame($f, $frameNumber * (1 / $fps));
|
||||
outputFrame($f, $frameNumber, (1 / $fps) * 1000);
|
||||
}
|
||||
$currentFrames = [];
|
||||
|
||||
$currentFrames[] = [$frameNumber, $frameNumber * (1 / $fps), $iframeRender, "i"];
|
||||
} else if ($pframeSize > 0) {
|
||||
fwrite(STDERR, " | IS PFRAME $frameNumber |$iframeSize|$pframeSize\n");
|
||||
$currentFrames[] = [$frameNumber, $frameNumber * (1 / $fps), $pframeRender, "p"];
|
||||
$currentFrames[] = [$frameNumber, $frameNumber, $iframeRender, "i"];
|
||||
} else if ($pframePathCount > 0) {
|
||||
fwrite(STDERR, " | IS PFRAME $frameNumber |$iframePathCount|$pframePathCount\n");
|
||||
$currentFrames[] = [$frameNumber, $frameNumber, $pframeRender, "p"];
|
||||
} else {
|
||||
|
||||
fwrite(STDERR, " | IS DROP $frameNumber |$iframeSize|$pframeSize\n");
|
||||
fwrite(STDERR, " | IS DROP $frameNumber |$iframePathCount|$pframePathCount\n");
|
||||
}
|
||||
|
||||
$lastFrameNumber = $frameNumber;
|
||||
}
|
||||
}
|
||||
foreach ($currentFrames as $f) {
|
||||
outputFrame($f, $endFrame * (1 / $fps));
|
||||
outputFrame($f, $endFrame, (1 / $fps) * 1000);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue