You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.4 KiB
105 lines
3.4 KiB
<?php
|
|
|
|
$width = 120;
|
|
$height = 60;
|
|
$PASSWORD = "Ky/6lG3kbA>/TM?C(V@S";
|
|
|
|
// --- Helpers ---
|
|
function setPixel(int $x, int $y, int $width, &$bitmap) {
|
|
if ($x < 0 || $y < 0) return;
|
|
$index = $y * $width + $x;
|
|
$byteIdx = intdiv($index, 8);
|
|
$bitPos = $index % 8;
|
|
$bitmap[$byteIdx] |= (1 << $bitPos);
|
|
}
|
|
|
|
function createImageBitmap(array $pixelsOn, int $width, int $height): array {
|
|
$totalBits = $width * $height;
|
|
$bitmapSize = intdiv($totalBits + 7, 8);
|
|
$bitmap = array_fill(0, $bitmapSize, 0);
|
|
foreach ($pixelsOn as [$x, $y]) {
|
|
setPixel($x, $y, $width, $bitmap);
|
|
}
|
|
return $bitmap;
|
|
}
|
|
|
|
function packBitmap(array $bitmap): string {
|
|
return implode('', array_map(fn($b) => pack('C', $b), $bitmap));
|
|
}
|
|
|
|
function createImageObject(int $width, int $height, array $bitmap, int $xOffset = 0, int $yOffset = 0): string {
|
|
$payload = pack('C', $width);
|
|
$payload .= pack('C', $height);
|
|
$payload .= packBitmap($bitmap);
|
|
$header = pack('C*', 0x01, $xOffset, $yOffset); // Type = 1
|
|
return $header . $payload;
|
|
}
|
|
|
|
function createScrollObject(int $xOffset, int $yOffset, int $width, int $height, int $contentWidth, array $bitmap, int $speed = 0, bool $directionRight = false, bool $wrap = true): string {
|
|
$CW = pack('v', $contentWidth);
|
|
$UF = (($wrap ? 1 : 0) << 7) | (($directionRight ? 1 : 0) << 6) | ($speed & 0x3F);
|
|
$payload = pack('C', $width);
|
|
$payload .= pack('C', $height);
|
|
$payload .= $CW;
|
|
$payload .= pack('C', $UF);
|
|
$payload .= packBitmap($bitmap);
|
|
$header = pack('C*', 0x03, $xOffset, $yOffset);
|
|
return $header . $payload;
|
|
}
|
|
|
|
function createBlob(array $objects, string $fileName) {
|
|
$blob = '';
|
|
$blob .= pack('C*', 0x42, 0x4E, 0x17, 0xEE);
|
|
$blob .= pack('C', count($objects));
|
|
foreach ($objects as $obj) {
|
|
$blob .= $obj;
|
|
}
|
|
file_put_contents($fileName, $blob);
|
|
echo "Blob written to {$fileName} (" . strlen($blob) . " bytes)\n";
|
|
}
|
|
|
|
// --- Validation ---
|
|
if (!isset($_POST['password'])) die("password missing.");
|
|
if (htmlspecialchars($_POST['password']) !== htmlspecialchars($PASSWORD)) die("wrong password.");
|
|
|
|
// --- Determine mode ---
|
|
$mode = $_POST['mode'] ?? 'image';
|
|
$objects = [];
|
|
$fileName = 'out/' . $_POST['fileName'] . '.bin' ?? 'out/' .'output.bin';
|
|
|
|
mkdir('out/');
|
|
// --- Handle Image Mode ---
|
|
if ($mode === 'image') {
|
|
$pixelsOn = json_decode($_POST['array_output'] ?? '[]', true);
|
|
if (!is_array($pixelsOn)) die("invalid or missing array_output for image mode.");
|
|
$bitmap = createImageBitmap($pixelsOn, $width, $height);
|
|
$objects[] = createImageObject($width, $height, $bitmap, 0, 0);
|
|
}
|
|
|
|
// --- Handle Text Mode (scrolling text) ---
|
|
elseif ($mode === 'text') {
|
|
$scrolls = json_decode($_POST['scrolls'] ?? '[]', true);
|
|
if (!is_array($scrolls) || empty($scrolls)) die("invalid or missing scrolls for text mode.");
|
|
|
|
foreach ($scrolls as $scroll) {
|
|
$pixelsOn = $scroll['pixelsOn'] ?? [];
|
|
$bitmap = createImageBitmap($pixelsOn, $scroll['contentWidth'], $scroll['height']);
|
|
$objects[] = createScrollObject(
|
|
$scroll['x'] ?? 0,
|
|
$scroll['y'] ?? 0,
|
|
$scroll['width'] ?? $width,
|
|
$scroll['height'] ?? 8,
|
|
$scroll['contentWidth'] ?? 60,
|
|
$bitmap,
|
|
$scroll['speed'] ?? 0,
|
|
$scroll['directionRight'] ?? false,
|
|
$scroll['wrap'] ?? true
|
|
);
|
|
}
|
|
}
|
|
|
|
else {
|
|
die("unknown mode: {$mode}");
|
|
}
|
|
|
|
createBlob($objects, $fileName);
|
|
|