48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace swf2ass;
|
|
|
|
class JPEGBitmapDefinition extends BitmapDefinition {
|
|
|
|
static function fromArray(array $element): BitmapDefinition {
|
|
|
|
$im = new \Imagick();
|
|
$bytes = $element["imageData"];
|
|
$alpha = $element["alphaData"] ?? null;
|
|
|
|
$isJPEG = substr($bytes, 0, 2) === "\xff\xd8" || substr($bytes, 0, 4) === "\xff\xd9\xff\xd8";
|
|
|
|
if ($isJPEG) { //Fix freaking broken JPEGs
|
|
if (substr($bytes, 0, 4) === "\xff\xd9\xff\xd8") { //Invalid header before marker
|
|
$bytes = substr($bytes, 4);
|
|
}
|
|
|
|
$size = strlen($bytes);
|
|
for ($i = 0; $i < $size; $i += 4) {
|
|
if (substr($bytes, $i, 4) === "\xff\xd9\xff\xd8") {
|
|
$bytes = substr($bytes, 0, $i) . substr($bytes, $i + 4);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
$im->readImageBlob($bytes);
|
|
|
|
$im->transformImageColorspace(\Imagick::COLORSPACE_SRGB);
|
|
$resolution = $im->getImageGeometry();
|
|
$size = new Vector2($resolution["width"], $resolution["height"]);
|
|
|
|
$pixels = array_fill(0, $size->y, array_fill(0, $size->x, new Color(0, 0, 0)));
|
|
|
|
$iterator = $im->getPixelIterator();
|
|
foreach ($iterator as $row => $p) {
|
|
foreach ($p as $col => $pixel) {
|
|
$c = $pixel->getColor(2);
|
|
$pixels[$row][$col] = new \swf2ass\Color($c["r"], $c["g"], $c["b"], $c["a"] ?? 255);
|
|
}
|
|
}
|
|
//TODO: add transparency
|
|
|
|
return new JPEGBitmapDefinition($element["characterId"], $size, $pixels);
|
|
}
|
|
} |