IT
games/alis / src / Alis / fn / image2.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/image.c (MIT, Olivier Huguenot, Vadim Kindl).// Part 2: draw functions (chunky 8bpp blitters for every platform format), RRQ / Transarctica maps,// helpers, and image_line_palettes() which host.php / sys.php expect.// Blitters write into Img::$wdraw (an offset into Alis::$mem) with a pitch of Host::$pixelbuf->w./** Small fill (byte loop: mem_set() rewrites the whole memory string, too slow per row). */function image_fill(int $p, int $c, int $n): void{    if ($n <= 0) {        return;    }    if ($n >= 512) {        mem_set($p, $c, $n);        return;    }    $m = &Alis::$mem;    $ch = chr($c & 0xff);    for ($i = 0; $i < $n; $i++) {        $m[$p + $i] = $ch;    }}// ---------------------------------------------------------------------------------------------// Draw functions// ---------------------------------------------------------------------------------------------function draw_pixel(int $x0, int $y0): void{    Alis::$mem[Img::$logic + s16($x0) + s16($y0) * Alis::$platform->width] = chr(Img::$inkcolor & 0xff);}function draw_line(int $x0, int $y0, int $x1, int $y1): void{    $x0 = s16($x0);    $y0 = s16($y0);    $x1 = s16($x1);    $y1 = s16($y1);    $dx = abs($x1 - $x0);    $sx = $x0 < $x1 ? 1 : -1;    $dy = -abs($y1 - $y0);    $sy = $y0 < $y1 ? 1 : -1;    $error = $dx + $dy;    while (true) {        draw_pixel($x0, $y0);        if ($x0 == $x1 && $y0 == $y1) {            break;        }        $e2 = 2 * $error;        if ($e2 >= $dy) {            if ($x0 == $x1) {                break;            }            $error += $dy;            $x0 = s16($x0 + $sx);        }        if ($e2 <= $dx) {            if ($y0 == $y1) {                break;            }            $error += $dx;            $y0 = s16($y0 + $sy);        }    }}// draw_box stays compiled for all targets — it only calls draw_linefunction draw_box(int $x1, int $y1, int $x2, int $y2): void{    $x1 = s16($x1);    $y1 = s16($y1);    $x2 = s16($x2);    $y2 = s16($y2);    if (Alis::$platform->kind == C::EPlatformMac) {        mac_update_pos($x1, $y1);        mac_update_pos($x2, $y2);    }    dirty_add($x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1);    draw_line($x1, $y1, $x2, $y1);    draw_line($x2, $y1, $x2, $y2);    draw_line($x2, $y2, $x1, $y2);    draw_line($x1, $y2, $x1, $y1);}function draw_boxf(int $x1, int $y1, int $x2, int $y2): void{    $x1 = s16($x1);    $y1 = s16($y1);    $x2 = s16($x2);    $y2 = s16($y2);    if (Alis::$platform->kind == C::EPlatformMac) {        mac_update_pos($x1, $y1);        mac_update_pos($x2, $y2);    }    $tmpx = min($x1, $x2);    $x2 = max($x1, $x2);    $x1 = $tmpx;    if (!boxf_clip($x1, $y1, $x2, $y2)) {        return;    }    $tmpx = s16($x2 - $x1);    dirty_add($x1, $y1, $x2 - $x1 + 1, $y2 - $y1 + 1);    $w = Alis::$platform->width;    for ($y = $y1; $y <= $y2; $y++) {        image_fill(Img::$logic + $x1 + $y * $w, Img::$inkcolor, $tmpx);    }}function draw_mac_rect(sRect $pos, sRect $bmp, int $color): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $color &= 0xff;    $tgt = Img::$wdraw + $pos->x1 + (($bmp->y1 + $pos->y1) * $pw);    if ($color == 15) {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $m[$tgt + $w] = chr(($w + $h) % 2);            }        }    } else {        $color = $color ? 0 : 1;        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $tgt += $pw) {            image_fill($tgt + $bmp->x1, $color, $bmp->x2 - $bmp->x1);        }    }}function draw_rect(sRect $pos, sRect $bmp, int $color): void{    $pw = Host::$pixelbuf->w;    $tgt = Img::$wdraw + $pos->x1 + (($bmp->y1 + $pos->y1) * $pw);    for ($h = $bmp->y1; $h < $bmp->y2; $h++, $tgt += $pw) {        image_fill($tgt + $bmp->x1, $color, $bmp->x2 - $bmp->x1);    }}function draw_mac_mono_0(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $masks = ImageVars::$masks;    $rots = ImageVars::$rots;    $width = s16($width);    $swadd = ($width >> 2) & 0xffff;    $src = $at + $bmp->y1 * $swadd;    $tgt = Img::$wdraw + $pos->x1 + (($bmp->y1 + $pos->y1) * $pw);    if ($flip) {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = 3 - $w % 4;                $color = (ord($m[$src + (($width - ($w + 1)) >> 2)]) & $masks[$index]) >> $rots[$index];                if (!($color & 2)) {                    $m[$tgt + $w] = ($color & 1) ? "\0" : "\1";                }            }        }    } else {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = $w % 4;                $color = (ord($m[$src + ($w >> 2)]) & $masks[$index]) >> $rots[$index];                if (!($color & 2)) {                    $m[$tgt + $w] = ($color & 1) ? "\0" : "\1";                }            }        }    }}function draw_mac_mono_2(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $masks = ImageVars::$masks;    $rots = ImageVars::$rots;    $width = s16($width);    $swadd = ($width >> 2) & 0xffff;    $src = $at + $bmp->y1 * $swadd;    $tgt = Img::$wdraw + $pos->x1 + (($bmp->y1 + $pos->y1) * $pw);    if ($flip) {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = 3 - $w % 4;                $m[$tgt + $w] = (((ord($m[$src + (($width - ($w + 1)) >> 2)]) & $masks[$index]) >> $rots[$index]) & 1) ? "\0" : "\1";            }        }    } else {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = $w % 4;                $m[$tgt + $w] = (((ord($m[$src + ($w >> 2)]) & $masks[$index]) >> $rots[$index]) & 1) ? "\0" : "\1";            }        }    }}function draw_dos_cga_0(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $masks = ImageVars::$masks;    $rots = ImageVars::$rots;    $width = s16($width);    $swadd = ($width >> 1) & 0xffff;    $src = $at + $bmp->y1 * $swadd;    $tgt = Img::$wdraw + $pos->x1 + (($bmp->y1 + $pos->y1) * $pw);    if ($flip) {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $wh = s16((($width - ($w + 1)) >> 2) << 1);                $index = 3 - $w % 4;                if (!((ord($m[$src + $wh]) & $masks[$index]) >> $rots[$index])) {                    $m[$tgt + $w] = chr((ord($m[$src + $wh + 1]) & $masks[$index]) >> $rots[$index]);                }            }        }    } else {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $wh = s16(($w >> 2) << 1);                $index = $w % 4;                if (!((ord($m[$src + $wh]) & $masks[$index]) >> $rots[$index])) {                    $m[$tgt + $w] = chr((ord($m[$src + $wh + 1]) & $masks[$index]) >> $rots[$index]);                }            }        }    }}function draw_dos_cga_2(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $masks = ImageVars::$masks;    $rots = ImageVars::$rots;    $width = s16($width);    $swadd = ($width >> 2) & 0xffff;    $src = $at + $bmp->y1 * $swadd;    $tgt = Img::$wdraw + $pos->x1 + (($pos->y1 + $bmp->y1) * $pw);    if ($flip) {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = 3 - $w % 4;                $m[$tgt + $w] = chr((ord($m[$src + (($width - ($w + 1)) >> 2)]) & $masks[$index]) >> $rots[$index]);            }        }    } else {        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $swadd, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2; $w++) {                $index = $w % 4;                $m[$tgt + $w] = chr((ord($m[$src + ($w >> 2)]) & $masks[$index]) >> $rots[$index]);            }        }    }}/** * The 4-bit chunky blitter shared by draw_st_4bit_0/2 and draw_4to8bit_0/2 (the C bodies are identical * apart from the pal_offset added to each pixel, 0 for the ST ones, and the colour-0 transparency of * the _0 variants). Mutates bmp like the C. */function image_draw_nibbles(int $at, sRect $pos, sRect $bmp, int $width, int $flip, int $pal_offset, bool $opaque): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $pal_offset &= 0xff;    $width = s16($width);    $swadd = ($width >> 1) & 0xffff;    $src = $at + $bmp->y1 * $swadd;    $tgt = Img::$wdraw + ($pos->y1 + $bmp->y1) * $pw;    $y1 = $bmp->y1;    $y2 = $bmp->y2;    if ($flip) {        if ($bmp->x1 % 2 == 1) {            $sptr = $swadd - 1 - ($bmp->x1 >> 1) + $src;            $tptr = $bmp->x1 + $pos->x1 + $tgt;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $pw) {                $color = ord($m[$sptr]) >> 4;                if ($opaque || $color) {                    $m[$tptr] = chr(($pal_offset + $color) & 0xff);                }            }            $bmp->x1 = s16($bmp->x1 + 1);        }        if (($bmp->x2 - 1) % 2 == 0) {            $sptr = $swadd - 1 - ($bmp->x2 >> 1) + $src;            $tptr = ($bmp->x2 + $pos->x1 - 1) + $tgt;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $pw) {                $color = ord($m[$sptr]) & 0b00001111;                if ($opaque || $color) {                    $m[$tptr] = chr(($pal_offset + $color) & 0xff);                }            }            $bmp->x2 = s16($bmp->x2 - 1);        }        if ($bmp->x1 != $bmp->x2) {            $sptr = $swadd - 1 + $src;            $tptr = ($bmp->x1 + $pos->x1) + $tgt;            $twadd = ($pw - ($bmp->x2 - $bmp->x1)) & 0xffff;            $bmp->x1 >>= 1;            $bmp->x2 >>= 1;            $x1 = $bmp->x1;            $x2 = $bmp->x2;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $twadd) {                for ($w = $x1; $w < $x2; $w++, $tptr += 2) {                    $s = ord($m[$sptr - $w]);                    $color = $s & 0b00001111;                    if ($opaque || $color) {                        $m[$tptr] = chr(($pal_offset + $color) & 0xff);                    }                    $color = $s >> 4;                    if ($opaque || $color) {                        $m[$tptr + 1] = chr(($pal_offset + $color) & 0xff);                    }                }            }        }    } else {        if ($bmp->x1 % 2 == 1) {            $sptr = ($bmp->x1 >> 1) + $src;            $tptr = ($bmp->x1 + $pos->x1) + $tgt;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $pw) {                $color = ord($m[$sptr]) & 0b00001111;                if ($opaque || $color) {                    $m[$tptr] = chr(($pal_offset + $color) & 0xff);                }            }            $bmp->x1 = s16($bmp->x1 + 1);        }        if (($bmp->x2 - 1) % 2 == 0) {            $bmp->x2 = s16($bmp->x2 - 1);            $sptr = ($bmp->x2 >> 1) + $src;            $tptr = ($bmp->x2 + $pos->x1) + $tgt;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $pw) {                $color = ord($m[$sptr]) >> 4;                if ($opaque || $color) {                    $m[$tptr] = chr(($pal_offset + $color) & 0xff);                }            }        }        if ($bmp->x1 != $bmp->x2) {            $sptr = $src;            $tptr = ($bmp->x1 + $pos->x1) + $tgt;            $twadd = ($pw - ($bmp->x2 - $bmp->x1)) & 0xffff;            $bmp->x1 >>= 1;            $bmp->x2 >>= 1;            $x1 = $bmp->x1;            $x2 = $bmp->x2;            for ($h = $y1; $h < $y2; $h++, $sptr += $swadd, $tptr += $twadd) {                for ($w = $x1; $w < $x2; $w++, $tptr += 2) {                    $s = ord($m[$sptr + $w]);                    $color = $s >> 4;                    if ($opaque || $color) {                        $m[$tptr] = chr(($pal_offset + $color) & 0xff);                    }                    $color = $s & 0b00001111;                    if ($opaque || $color) {                        $m[$tptr + 1] = chr(($pal_offset + $color) & 0xff);                    }                }            }        }    }}function draw_st_4bit_0(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    image_draw_nibbles($at, $pos, $bmp, $width, $flip, 0, false);}function draw_st_4bit_2(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    image_draw_nibbles($at, $pos, $bmp, $width, $flip, 0, true);}/** 5 bit-planes -> chunky colour of bit b (the C expression repeated in draw_ami_5bit_*). */function image_ami_px(int $c0, int $c1, int $c2, int $c3, int $c4, int $b): int{    return (($c0 >> $b) & 1) | ((($c1 >> $b) & 1) << 1) | ((($c2 >> $b) & 1) << 2) | ((($c3 >> $b) & 1) << 3) | ((($c4 >> $b) & 1) << 4);}/** Shared body of draw_ami_5bit_0 (colour 0 transparent) and draw_ami_5bit_2 (opaque). Mutates bmp. */function image_draw_ami5(int $at, sRect $pos, sRect $bmp, int $width, int $height, int $flip, bool $opaque): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $width = s16($width);    $height = s16($height);    $planesize = shr32(s32($width * $height), 3);    $swadd = $width & 0xffff;    $src = ($bmp->y1 * $swadd) & 0xffff;   // u16 src    $tgt = Img::$wdraw + $pos->x1 + (($pos->y1 + $bmp->y1) * $pw);    if ($flip) {        $x1 = s16(($bmp->x1 >> 3) << 3);        if ($x1 != $bmp->x1) {            $fr = s16($bmp->x1 - $x1);            $to = s16(($x1 + 8 > $bmp->x2) ? $bmp->x2 - $x1 : 8);            $tptr = $tgt;            $w = $bmp->x1;            $wh = s16($width - ($w + 1));            for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tptr += $pw, $w = $bmp->x1) {                $idx = ($wh + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = $fr; $b < $to; $b++, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tptr + $w] = chr($color);                    }                }            }            $bmp->x1 = s16($x1 + 8);        }        $x1 = s16(($bmp->x2 >> 3) << 3);        if ($x1 != $bmp->x2 && $bmp->x1 < $bmp->x2) {            $fr = 0;            $to = s16($bmp->x2 - $x1);            $src = ($bmp->y1 * $swadd) & 0xffff;            $tptr = $tgt;            $w = $x1;            $wh = s16($width - ($w + 1));            for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tptr += $pw, $w = $x1) {                $idx = ($wh + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = $fr; $b < $to; $b++, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tptr + $w] = chr($color);                    }                }            }            $bmp->x2 = $x1;        }        $src = ($bmp->y1 * $swadd) & 0xffff;        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2;) {                $wh = s16($width - ($w + 1));                $idx = ($wh + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = 0; $b < 8; $b++, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tgt + $w] = chr($color);                    }                }            }        }    } else {        $x1 = s16(($bmp->x1 >> 3) << 3);        if ($x1 != $bmp->x1) {            $fr = s16(7 - ($bmp->x1 - $x1));            $to = s16(($x1 + 8 > $bmp->x2) ? -1 + ($x1 + 8 - $bmp->x2) : -1);            $tptr = $tgt;            $w = $bmp->x1;            for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tptr += $pw, $w = $bmp->x1) {                $idx = ($x1 + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = $fr; $b > $to; $b--, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tptr + $w] = chr($color);                    }                }            }            $bmp->x1 = s16($x1 + 8);        }        $x1 = s16(($bmp->x2 >> 3) << 3);        if ($x1 != $bmp->x2 && $bmp->x1 < $bmp->x2) {            $fr = 7;            $to = s16(-1 + ($x1 + 8 - $bmp->x2));            $src = ($bmp->y1 * $swadd) & 0xffff;            $tptr = $tgt;            $w = $x1;            for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tptr += $pw, $w = $x1) {                $idx = ($x1 + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = $fr; $b > $to; $b--, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tptr + $w] = chr($color);                    }                }            }            $bmp->x2 = $x1;        }        $src = ($bmp->y1 * $swadd) & 0xffff;        for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src = ($src + $swadd) & 0xffff, $tgt += $pw) {            for ($w = $bmp->x1; $w < $bmp->x2;) {                $idx = ($w + $src) >> 3;                $c0 = ord($m[$at + $idx]);                $c1 = ord($m[$at + ($idx += $planesize)]);                $c2 = ord($m[$at + ($idx += $planesize)]);                $c3 = ord($m[$at + ($idx += $planesize)]);                $c4 = ord($m[$at + ($idx += $planesize)]);                for ($b = 7; $b > -1; $b--, $w++) {                    $color = image_ami_px($c0, $c1, $c2, $c3, $c4, $b);                    if ($opaque || $color) {                        $m[$tgt + $w] = chr($color);                    }                }            }        }    }}function draw_ami_5bit_0(int $at, sRect $pos, sRect $bmp, int $width, int $height, int $flip): void{    image_draw_ami5($at, $pos, $bmp, $width, $height, $flip, false);}function draw_ami_5bit_2(int $at, sRect $pos, sRect $bmp, int $width, int $height, int $flip): void{    image_draw_ami5($at, $pos, $bmp, $width, $height, $flip, true);}function draw_4to8bit_0(int $at, sRect $pos, sRect $bmp, int $width, int $flip, int $pal_offset): void{    image_draw_nibbles($at, $pos, $bmp, $width, $flip, $pal_offset, false);}function draw_4to8bit_2(int $at, sRect $pos, sRect $bmp, int $width, int $flip, int $pal_offset): void{    image_draw_nibbles($at, $pos, $bmp, $width, $flip, $pal_offset, true);}/** Shared body of draw_8bit_0 (colour 0 transparent) and draw_8bit_2 (opaque), one row string at a time. */function image_draw_8bit(int $at, sRect $pos, sRect $bmp, int $width, int $flip, bool $opaque): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $width = s16($width);    $x1 = $bmp->x1;    $x2 = $bmp->x2;    $wadd = ($pw - ($x2 - $x1)) & 0xffff;    $src = ($flip ? $width - 1 : 0) + $at + 2 + $bmp->y1 * $width;    $tgt = Img::$wdraw + ($x1 + $pos->x1) + ($pos->y1 + $bmp->y1) * $pw;    $n = $x2 - $x1;    for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $width, $tgt += $wadd) {        if ($n <= 0) {            continue;        }        // flip: *(src - w) for w = x1 .. x2-1; else *(src + w)        $row = $flip ? strrev(substr($m, $src - ($x2 - 1), $n)) : substr($m, $src + $x1, $n);        if ($opaque) {            for ($i = 0; $i < $n; $i++) {                $m[$tgt + $i] = $row[$i];            }        } else {            for ($i = 0; $i < $n; $i++) {                $c = $row[$i];                if ($c !== "\0") {                    $m[$tgt + $i] = $c;                }            }        }        $tgt += $n;    }}function draw_8bit_0(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    image_draw_8bit($at, $pos, $bmp, $width, $flip, false);}function draw_8bit_2(int $at, sRect $pos, sRect $bmp, int $width, int $flip): void{    image_draw_8bit($at, $pos, $bmp, $width, $flip, true);}/** v3 DOS 0x12974 (unflipped, colour 0 transparent): every non-zero pixel is drawn in $colour. */function draw_8bit_tinted(int $at, sRect $pos, sRect $bmp, int $width, int $colour): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $width = s16($width);    $x1 = $bmp->x1;    $n = $bmp->x2 - $x1;    if ($n <= 0) {        return;    }    $c = chr($colour & 0xff);    $src = $at + 2 + $bmp->y1 * $width + $x1;    $tgt = Img::$wdraw + ($x1 + $pos->x1) + ($pos->y1 + $bmp->y1) * $pw;    for ($h = $bmp->y1; $h < $bmp->y2; $h++, $src += $width, $tgt += $pw) {        $row = substr($m, $src, $n);        for ($i = 0; $i < $n; $i++) {            if ($row[$i] !== "\0") {                $m[$tgt + $i] = $c;            }        }    }}function draw_fli_video(int $bitmap): void{    // empty in the C (FLI video is played elsewhere)}function mapchar(int $a2, int $a4, int $d3w, int $d4w, int $d5w): void{    // empty in the C}function chartpalti16(int $src): int{    return (ImageVars::$chartvcol0 + (xread8($src + 1) >> 4)) & 0xff;}function chartpalti8(int $src): int{    return (ImageVars::$chartvcol0 + (xread8($src + 1) >> 5)) & 0xff;}function chartpalti6(int $src): int{    return (ImageVars::$chartvcol0 + intdiv(xread8($src + 1), 43)) & 0xff;}function chartpalti4(int $src): int{    return (ImageVars::$chartvcol0 + (xread8($src + 1) >> 6)) & 0xff;}function chartpalti(int $src): int{    $tmp = xread8($src + 1) * (ImageVars::$chartvncol & 0xffff);    return (ImageVars::$chartvcol0 + s8($tmp >> 0x10)) & 0xff;}function chartpbyte(int $src): int{    return xread8($src + 1);}function chartpdeco(int $src): int{    return xread8($src);}// DOS chart pixel: writes to tgt, skipped for missing textures (as the original).// plot_x is the screen X; xdc (texture/dither phase) is not advanced across fully-masked// column groups, so the planar path must not plot at xdc.function chartpixel(int $tgt, int $src, int $type_table_base, int $xdc, int $ydc, int $plot_x): void{    $xdc &= 0xffff;    $ydc &= 0xffff;    // Read terrain type from high byte of 16-bit cell    $cell = xread16($src) & 0xffff;    $type = ($cell >> 8) & 0x3F;    // Index into type table (32 bytes per entry)    $type_entry = $type_table_base + $type * 32;    // Follow redirect if flag set    if (s8(xread8($type_entry + 0x14)) < 0) {        $src = s32($src + xread32($type_entry + 0x10));        $cell = xread16($src) & 0xffff;        $type = ($cell >> 8) & 0x3F;        $type_entry = $type_table_base + $type * 32;    }    // Check texture exists - skip pixel entirely if not (asm: JZ to exit without MOV [EDI])    if (xread8($type_entry + 0x08) == 0 || xread32($type_entry + 0x04) == 0) {        return;    }    // Resolve texture pointer    $tex_ptr = xread32($type_entry + 0x04);    $texture = s32($tex_ptr + xread32($tex_ptr));    // Compute texture coordinates    $y_wrapped = (($ydc & ((xread16($texture + 4) & 0xffff) - (xread16($type_entry + 2) & 0xffff))) + (xread16($type_entry + 2) & 0xffff)) & 0xffff;    $y_scaled = ($y_wrapped * ((xread16($texture + 2) & 0xffff) + 1)) & 0xffff;    $x_wrapped = $xdc & (xread16($texture + 2) & 0xffff);    $tex_pixel = xread8($texture + 8 + $y_scaled + $x_wrapped);    // Compute shade from terrain cell data    $height_nibble = ($cell & 0xFF) >> 4;    $normal_x2 = (($cell >> 14) & 3) << 1;    $shade = s16(((-s8($normal_x2) + s8(Alis::$basedark)) >> 1) + $height_nibble);    if ($shade < 0) {        $shade = 0;    } elseif ($shade > 15) {        $shade = 15;    }    // 2D palette lookup via darkness table, write directly to framebuffer    Alis::$mem[$tgt] = chr(xread8(Alis::$ptrdark + (($shade & 0xffff) << 8) + $tex_pixel));}function chartptra(int $src): int{    // Compute VRAM offset from terrain type (lower 6 bits, scaled)    $vram_offset = s16((s8(xread8($src)) & 0x3f) * 0x20 - 0xc00);    // Follow terrain indirection if redirect flag is set    if (s8(xread8(Alis::$script->vram_org + 0x1 + $vram_offset)) < 0) {        $src = s32($src + xread16(Alis::$script->vram_org + 0x10 + $vram_offset));        $vram_offset = s16(((xread16($src + 1) & 0xffff) & 0x3f) * 0x20 - 0xc00);    }    // Compute shade level from source data and base darkness (clamped to 0-15)    // NOTE: shade is computed but unused in the final lookup — matches original code    $shade = s16(((s8(xread8($src + 1)) >> 4) & 0xffff) + (Alis::$basedark - ((s8(xread8($src)) >> 6) & 0xffff)));    if ($shade < 0) {        $shade = 0;    } elseif (0xf < $shade) {        $shade = 0xf;    }    // Look up color from darkness/dithering table    $dark_idx = s16(s8(xread8(Alis::$script->vram_org + 0x8 + $vram_offset)) * 2);    $result = ((ImageVars::$xdeschart ^ ImageVars::$ydeschart) & 1) == 0 ? s8(xread8(Alis::$ptrdark + $dark_idx)) : s8(xread8(Alis::$ptrdark + 1 + $dark_idx));    return $result & 0xff;}function draw_requiem_map(Sprite $sprite, int $bitmap): void{    $pw = Host::$pixelbuf->w;    $mapdata = s32($bitmap + 0x3d8);    if (xread8($bitmap - 0x28) != 0x9) {        if ((xread8($bitmap - 0x28) != 0xa) && (xread32(Alis::$atent + xread16($bitmap - 0x24)) != 0)) {            // PORT: mapchar() is empty in the C, so this tile walk has no visible effect (kept as is).            // Resolve tile data base address from entity's script            $script_addr = xread32(xread32(Alis::$atent + xread16($bitmap - 0x24)) - 0x14);            $tile_hdr = s32(xread32($script_addr + 0xe) + $script_addr);            $tile_base = s32(xread32($tile_hdr) + $tile_hdr);            $tileh = xread16($bitmap - 0x18) & 0xffff;            $tilew = xread16($bitmap - 0x1c) & 0xffff;            // Compute starting tile row and Y pixel offset            $vert_offset = (xread16($bitmap - 6) + (Img::$blocy1 - $sprite->newy)) & 0xffff;            $start_row = cdiv($vert_offset, $tileh) & 0xffff;            $tile_y = s16(Img::$blocy1 - cmod($vert_offset, $tileh));            // Compute map pointer and tile grid dimensions            $map_ptr = s32($mapdata + s16($start_row + xread16($bitmap + 0x3d4) * cdiv((xread16($bitmap - 0xa) + (Img::$blocx1 - $sprite->newx)) & 0xffff, $tilew)));            $tile_rows = s16(cdiv((xread16($bitmap - 6) + (Img::$blocy2 - $sprite->newy)) & 0xffff, $tileh) - $start_row);            $tile_cols = cdiv((Img::$blocx2 - Img::$blocx1) & 0xffff, $tilew);            $row_stride = xread16($bitmap + 0x3d4);            // Half-tile offset adjustment for staggered grids            if ((xread8($bitmap - 0x26) & 2) != 0) {                $tile_y = s16((((xread16($bitmap - 0x18) - 1) & 0xffff) >> 1) + $tile_y);                if ($start_row != 0) {                    $tile_rows = s16($tile_rows + 1);                    $map_ptr = s32($map_ptr - 1);                    $tile_y = s16($tile_y - xread16($bitmap - 0x18));                }                $tile_rows = s16($tile_rows + 1);            }            $start_x = Img::$blocx1 & 0xffff;            $col_counter = $tile_cols;            $row_counter = $tile_rows;            $cur_x = s32(Img::$blocx1);            // Draw tile grid row by row, column by column            do {                do {                    $mb = xread8($map_ptr);                    if ($mb != 0 && ($tile_idx = ((xread16($bitmap - 0x22) + $mb) - 1) & 0xffff) <= (xread16($bitmap - 0x20) & 0xffff)) {                        $tile_entry = s32($tile_base + s16($tile_idx * 4));                        mapchar($mapdata, s32(xread32($tile_entry) + $tile_entry), $tile_rows, s16($cur_x), $tile_y);                    }                    $map_ptr = s32($map_ptr + xread16($bitmap + 0x3d4));                    $cur_x = (xread16($bitmap - 0x1c) + s16($cur_x)) & 0xffff;                    $col_rem = (s16($col_counter) - 1) & 0xffff;                    $col_counter = $col_rem;                } while ($col_rem != 0xffff);                $tile_y = s16(xread16($bitmap - 0x18) + $tile_y);                $cur_x = $start_x & 0xffff;                $map_ptr = s32($map_ptr + s16(1 - (s16($tile_cols) + 1) * $row_stride));                $row_counter = s16($row_counter - 1);                $col_counter = $tile_cols;            } while ($row_counter != -1);        }    } else {        // Direct pixel-level map rendering (type 9)        $base_col = cdiv(xread16($mapdata - 0x3e2) & 0xffff, xread16($mapdata - 0x3f4) & 0xffff);        $pixel_x = Img::$blocx1 - $sprite->newx;        $base_row = cdiv(xread16($mapdata - 0x3e0) & 0xffff, xread16($mapdata - 0x3f2) & 0xffff);        $pixel_y = (xread16($mapdata - 0x3d4) + $sprite->newy) - Img::$blocy2;        $entity_data = xread32($mapdata - 0x3ba);        if ($entity_data == 0) {            return;        }        if (Alis::$platform->is_little_endian) {            // DOS path: bitmap mask-based rendering with textured terrain            // Bitmap pointer (mask data from mapdata)            $col_groups_bm = $base_col + ($pixel_x >> 4);            $adj_row_bm = $base_row + $pixel_y;            $bitmap_stride = xread16($mapdata - 0x3c4) & 0xffff;            $bitmap_base = $mapdata;            if (s8(xread8($mapdata - 1)) < 0) {                $bitmap_base = xread32(xread32($mapdata));            }            $bitmap_src = s32($bitmap_base + mul32($col_groups_bm, $bitmap_stride) + $adj_row_bm * 2);            // Entity source pointer (terrain cell data from entity_data)            $x_log_diff = s16(xread16($mapdata - 0x3c0) - xread16($entity_data - 0x3c0));            $y_log_diff = s16(xread16($mapdata - 0x3be) - xread16($entity_data - 0x3be));            $col_groups_ent = $col_groups_bm;            if ($x_log_diff > 0) {                $col_groups_ent = shl32($col_groups_ent, $x_log_diff);            }            $adj_row_ent = $adj_row_bm;            if ($y_log_diff > 0) {                $adj_row_ent = shl32($adj_row_ent, $y_log_diff);            }            $entity_stride = xread16($entity_data - 0x3c4) & 0xffff;            $entity_base = $entity_data;            if (s8(xread8($entity_data - 1)) < 0) {                $entity_base = xread32(xread32($entity_data));            }            $entity_src = s32($entity_base + mul32($col_groups_ent, $entity_stride) + $adj_row_ent * 2);            // Per-pixel and per-group strides            $x_stride_diff = s16(xread16($mapdata - 0x3c0) - 4 - xread16($entity_data - 0x3c0));            $pixel_stride = $x_stride_diff > 0 ? shl32($entity_stride, $x_stride_diff) : $entity_stride;            $group_stride = shl32($pixel_stride, 4);            $row_advance = $y_log_diff > 0 ? shl32(2, $y_log_diff) : 2;            // Type table base for terrain lookups            $type_table_base = s32($entity_data - 0xC00);            // Dimensions and screen setup            // Align X to column group boundary (bitmap mask handles edge clipping)            $sub_pixel_x = s16($pixel_x & 0xf);            $last_cg = s16($base_col + ((Img::$blocx2 - $sprite->newx) >> 4));            $col_groups_count = s16($last_cg - $col_groups_bm + 1);            $height = s16(Img::$blocy2 - Img::$blocy1 + 1);            ImageVars::$xdeschart = (Img::$blocx1 - Img::$wlogx1 - $sub_pixel_x) & 0xffff;            ImageVars::$ydeschart = (Img::$blocy2 - Img::$wlogy1) & 0xffff;            $tgt = Img::$logic + ImageVars::$ydeschart * $pw + ImageVars::$xdeschart;            // Screen X; diverges from xdeschart on fully-masked column groups (see chartpixel).            $sx = ImageVars::$xdeschart;            // Render row-major, bottom-to-top            while ($height > 0) {                $tgt_save = $tgt;                $entity_src_save = $entity_src;                $bitmap_src_save = $bitmap_src;                $xdeschart_save = ImageVars::$xdeschart;                $sx_save = $sx;                for ($cg = 0; $cg < $col_groups_count; $cg++) {                    // NOTE: set 0xffff to draw whole map                    $mask = ~xread16($bitmap_src) & 0xffff;                    $bitmap_src = s32($bitmap_src + $bitmap_stride);                    if ($mask == 0) {                        $tgt += 16;                        $sx += 16;               // destination moves even though xdeschart does not                        $entity_src = s32($entity_src + $group_stride);                    } else {                        for ($bit = 15; $bit >= 0; $bit--) {                            if ($mask & (1 << $bit)) {                                chartpixel($tgt, $entity_src, $type_table_base, ImageVars::$xdeschart, ImageVars::$ydeschart, $sx);                            }                            $tgt++;                            $sx++;                            ImageVars::$xdeschart = (ImageVars::$xdeschart + 1) & 0xffff;                            $entity_src = s32($entity_src + $pixel_stride);                        }                    }                }                $tgt = $tgt_save;                $entity_src = $entity_src_save;                $bitmap_src = $bitmap_src_save;                ImageVars::$xdeschart = $xdeschart_save;                $sx = $sx_save;                ImageVars::$ydeschart = (ImageVars::$ydeschart - 1) & 0xffff;                $bitmap_src = s32($bitmap_src + 2);                $entity_src = s32($entity_src + $row_advance);                $tgt -= $pw;                $height = s16($height - 1);            }        } else {            // Bitmap mask pointer setup            $col_groups_bm = $base_col + ($pixel_x >> 4);            $adj_row_bm = $base_row + $pixel_y;            $bitmap_stride = xread16($mapdata - 0x3c4) & 0xffff;            $bitmap_base = $mapdata;            if (s8(xread8($mapdata - 1)) < 0) {                $bitmap_base = xread32(xread32($mapdata));            }            $bitmap_src = s32($bitmap_base + mul32($col_groups_bm, $bitmap_stride) + $adj_row_bm * 2);            // Entity source setup            $src = $entity_data;            if (s8(xread8($entity_data - 1)) < 0) {                $src = xread32(xread32($entity_data));            }            $x_log_diff = s16(xread16($mapdata - 0x3c0) - xread16($entity_data - 0x3c0));            $y_log_diff = s16(xread16($mapdata - 0x3be) - xread16($entity_data - 0x3be));            $src_stride = xread16($entity_data - 0x3c4);            $x_stride_diff = s16($x_log_diff - 4);            $pixel_stride = $x_stride_diff > 0 ? shl32($src_stride, $x_stride_diff) : $src_stride;            $group_stride = shl32($pixel_stride, 4);            $row_advance = $y_log_diff > 0 ? shl32(2, $y_log_diff) : 2;            $col_groups_ent = $col_groups_bm;            if ($x_log_diff > 0) {                $col_groups_ent = shl32($col_groups_ent, $x_log_diff);            }            $adj_row_ent = $adj_row_bm;            if ($y_log_diff > 0) {                $adj_row_ent = shl32($adj_row_ent, $y_log_diff);            }            $entity_src = s32($src + mul32($col_groups_ent, $src_stride) + $adj_row_ent * 2);            // Dimensions            $sub_pixel_x = s16($pixel_x & 0xf);            $last_cg = s16($base_col + ((Img::$blocx2 - $sprite->newx) >> 4));            $col_groups_count = s16($last_cg - $col_groups_bm + 1);            $height = s16(Img::$blocy2 - Img::$blocy1 + 1);            // chartproc selection (matching Falcon destomap)            $type = xread16($mapdata - 0x3b6);            switch ($type) {                case 1:                    ImageVars::$chartvcol0 = xread16($mapdata - 0x3b4) & 0xff;                    ImageVars::$chartvncol = xread16($mapdata - 0x3b2) & 0xffff;                    switch (ImageVars::$chartvncol) {                        case 0x10:                            ImageVars::$chartproc = chartpalti16(...);                            break;                        case 0x8:                            ImageVars::$chartproc = chartpalti8(...);                            break;                        case 0x6:                            ImageVars::$chartproc = chartpalti6(...);                            break;                        case 0x4:                            ImageVars::$chartproc = chartpalti4(...);                            break;                        default:                            ImageVars::$chartproc = chartpalti(...);                            break;                    }                    break;                case 2:                    ImageVars::$chartproc = chartpdeco(...);                    break;                default:                    ImageVars::$chartproc = chartpbyte(...);                    break;            }            ImageVars::$xdeschart = (Img::$blocx1 - Img::$wlogx1 - $sub_pixel_x) & 0xffff;            ImageVars::$ydeschart = (Img::$blocy2 - Img::$wlogy1) & 0xffff;            $tgt = Img::$logic + ImageVars::$ydeschart * $pw + ImageVars::$xdeschart;            $chartproc = ImageVars::$chartproc;            // Render row-major, bottom-to-top (matching Falcon destomap)            while ($height > 0) {                $tgt_save = $tgt;                $entity_src_save = $entity_src;                $bitmap_src_save = $bitmap_src;                $xdeschart_save = ImageVars::$xdeschart;                for ($cg = 0; $cg < $col_groups_count; $cg++) {                    // Mask bit=1 → hidden, bit=0 → revealed; invert so 1=draw                    $mask = ~xread16($bitmap_src) & 0xffff;                    $bitmap_src = s32($bitmap_src + $bitmap_stride);                    if ($mask == 0) {                        // Fully masked — skip 16 pixels                        $tgt += 16;                        ImageVars::$xdeschart = (ImageVars::$xdeschart + 16) & 0xffff;                        $entity_src = s32($entity_src + $group_stride);                    } else {                        for ($bit = 15; $bit >= 0; $bit--) {                            if ($mask & (1 << $bit)) {                                Alis::$mem[$tgt] = chr($chartproc($entity_src) & 0xff);                            }                            $tgt++;                            ImageVars::$xdeschart = (ImageVars::$xdeschart + 1) & 0xffff;                            $entity_src = s32($entity_src + $pixel_stride);                        }                    }                }                $tgt = $tgt_save;                $entity_src = $entity_src_save;                $bitmap_src = $bitmap_src_save;                ImageVars::$xdeschart = $xdeschart_save;                ImageVars::$ydeschart = (ImageVars::$ydeschart - 1) & 0xffff;                $bitmap_src = s32($bitmap_src + 2);                $entity_src = s32($entity_src + $row_advance);                $tgt -= $pw;                $height = s16($height - 1);            }        }    }}function draw_transarctica_map(Sprite $sprite, int $mapaddr, sRect $lim): void{    $m = &Alis::$mem;    $pw = Host::$pixelbuf->w;    $vram = xread32(xread16($mapaddr - 0x24) + Alis::$atent);    if ($vram != 0) {        $tileoffset = xread16($mapaddr + 0x24) & 0xffff;        $tileadd = xread16($mapaddr - 0x22) & 0xffff;        $tilecount = xread16($mapaddr - 0x20) & 0xffff;        $tilex = xread16($mapaddr - 0x1c) & 0xffff;        $tiley = xread16($mapaddr - 0x18) & 0xffff;        $addr = get_0x14_script_org_offset($vram);        $vram = s32(xread32($addr + 0xe) + $addr);        $addr = s32(xread32($vram) + $vram);        $tempy = ((Img::$blocy1 - $sprite->newy) + xread16($mapaddr - 6)) & 0xffff;        $yc = cdiv($tempy, $tiley);        $yo = s16(Img::$blocy1 - cmod($tempy, $tiley));        $tileaddr = $mapaddr + 0x28;        $t1 = s16(Img::$blocx1 - $sprite->newx);        $t2 = xread16($mapaddr - 10);        $tileaddr = s32($tileaddr + ($tileoffset * cdiv(($t1 + $t2) & 0xffff, $tilex) + $yc));        $mapheight = s16(cdiv((Img::$blocy2 - $sprite->newy) + xread16($mapaddr - 6), $tiley) - $yc);        $mapwidth = s16(cdiv(Img::$blocx2 - Img::$blocx1, $tilex));        if ((xread8($mapaddr - 0x26) & 2) != 0) {            $yo = s16($yo + ((($tiley - 1) & 0xffff) >> 1));            if ($yc != 0) {                $mapheight = s16($mapheight + 1);                $tileaddr = s32($tileaddr - 1);                $yo = s16($yo - $tiley);            }            $mapheight = s16($mapheight + 1);        }        $addy = Alis::$platform->kind == C::EPlatformPC ? 0 : 1;        if (Alis::$platform->bpp == 8) {            $color = 63;            for ($h = $lim->y1; $h < $lim->y2; $h++) {                image_fill(Img::$logic + $lim->x1 + ($h * $pw), $color, $lim->x2 - $lim->x1);            }        }        for ($mh = $mapheight; $mh >= 0; $mh--) {            for ($mw = $mapwidth; $mw >= 0; $mw--) {                $tb = xread8($tileaddr);                if ($tb != 0 && ($tileidx = (($tb + $tileadd) - 1) & 0xffff) <= $tilecount) {                    $vram = s32($addr + s16($tileidx * 4));                    $img = s32(xread32($vram) + $vram);                    $bitmap = $img;                    $width = s16(s16(read16($bitmap + 2)) + 1);                    $height = s16(s16(read16($bitmap + 4)) + 1);                    $at = $bitmap + 6;                    $posx1 = s16(($mapwidth - $mw) * $tilex);                    $posy1 = s16((($mapheight - $mh) - $addy) * $tiley + cdiv((($tiley - 1) - $height), 2));                    $flip = 0;                    $color = 0;                    $clear = 0;                    $palidx = 0;                    $bmpx1 = 0;                    $bmpx2 = $width;                    $bmpy1 = 0;                    $bmpy2 = $height;                    switch (xread8($bitmap)) {                        case 0x01:                            // rectangle                            $color = xread8($bitmap + 1);                            for ($h = $bmpy1; $h < $bmpy1 + $bmpy2; $h++) {                                $tgt = Img::$logic + ($bmpx1 + $posx1) + (($posy1 + $h) * $pw);                                for ($w = $bmpx1; $w < $bmpx1 + $bmpx2; $w++, $tgt++) {                                    if (($w + $posx1) < $lim->x1) {                                        continue;                                    }                                    if (($w + $posx1) >= $lim->x2) {                                        continue;                                    }                                    if (($h + $posy1) < $lim->y1) {                                        continue;                                    }                                    if (($h + $posy1) >= $lim->y2) {                                        continue;                                    }                                    $m[$tgt] = chr($color);                                }                            }                            break;                        case 0x00:                        case 0x02:                            // ST image                            $clear = xread8($bitmap) == 0 ? 0 : 0xff;                            $half = cdiv($width, 2);                            for ($h = $bmpy1; $h < $bmpy1 + $bmpy2; $h++) {                                $tgt = Img::$logic + ($bmpx1 + $posx1) + (($posy1 + $h) * $pw);                                for ($w = $bmpx1; $w < $bmpx1 + $bmpx2; $w++, $tgt++) {                                    $wh = s16(cdiv($flip ? ($width - ($w + 1)) : $w, 2));                                    $color = ord($m[$at + $wh + $h * $half]);                                    $color = $w % 2 == $flip ? (($color & 0b11110000) >> 4) : ($color & 0b00001111);                                    if ($color != $clear) {                                        if (($w + $posx1) < $lim->x1) {                                            continue;                                        }                                        if (($w + $posx1) >= $lim->x2) {                                            continue;                                        }                                        if (($h + $posy1) < $lim->y1) {                                            continue;                                        }                                        if (($h + $posy1) >= $lim->y2) {                                            continue;                                        }                                        $m[$tgt] = chr($color);                                    }                                }                            }                            break;                        case 0x10:                        case 0x12:                            // 4 bit image                            $palidx = xread8($bitmap + 6);                            $clear = xread8($bitmap) == 0x10 ? xread8($bitmap + 7) : 0xff;                            $at = $bitmap + 8;                            $half = cdiv($width, 2);                            for ($h = $bmpy1; $h < $bmpy1 + $bmpy2; $h++) {                                $tgt = Img::$logic + ($bmpx1 + $posx1) + (($posy1 + $h) * $pw);                                for ($w = $bmpx1; $w < $bmpx1 + $bmpx2; $w++, $tgt++) {                                    $wh = s16(cdiv($flip ? ($width - ($w + 1)) : $w, 2));                                    $color = ord($m[$at + $wh + $h * $half]);                                    $color = $w % 2 == $flip ? (($color & 0b11110000) >> 4) : ($color & 0b00001111);                                    if ($color != $clear) {                                        if (($w + $posx1) < $lim->x1) {                                            continue;                                        }                                        if (($w + $posx1) >= $lim->x2) {                                            continue;                                        }                                        if (($h + $posy1) < $lim->y1) {                                            continue;                                        }                                        if (($h + $posy1) >= $lim->y2) {                                            continue;                                        }                                        $m[$tgt] = chr(($palidx + $color) & 0xff);                                    }                                }                            }                            break;                        case 0x14:                        case 0x16:                            // 8 bit image                            $palidx = xread8($bitmap + 6); // NOTE: not realy sure what it is, but definetly not palette index                            $clear = xread8($bitmap) == 0x14 ? xread8($bitmap + 7) : 0xff;                            $at = $bitmap + 8;                            for ($h = $bmpy1; $h < $bmpy1 + $bmpy2; $h++) {                                $tgt = Img::$logic + ($bmpx1 + $posx1) + (($posy1 + $h) * $pw);                                for ($w = $bmpx1; $w < $bmpx1 + $bmpx2; $w++, $tgt++) {                                    $color = ord($m[$at + ($flip ? $width - ($w + 1) : $w) + $h * $width]);                                    if ($color != $clear) {                                        if (($w + $posx1) < $lim->x1) {                                            continue;                                        }                                        if (($w + $posx1) >= $lim->x2) {                                            continue;                                        }                                        if (($h + $posy1) < $lim->y1) {                                            continue;                                        }                                        if (($h + $posy1) >= $lim->y2) {                                            continue;                                        }                                        $m[$tgt] = chr(($color + $palidx) & 0xff);                                    }                                }                            }                            break;                    }                }                $tileaddr = s32($tileaddr + $tileoffset);            }            $tileaddr = s32($tileaddr + s16(1 - (s16($mapwidth) + 1) * $tileoffset));        }    }}// ---------------------------------------------------------------------------------------------// Helper functions// ---------------------------------------------------------------------------------------------function mac_update_pos(int &$x, int &$y): void{    // C: *x *= 1.5 (s16, truncation toward zero)    $x = s16((int) ($x * 1.5));    $y = s16((int) ($y * 1.5));}// ---------------------------------------------------------------------------------------------// Host-layer helpers (called by fn/sys.php; not in image.c)// ---------------------------------------------------------------------------------------------/** * The per-line palette list of image.firstpal (built by linepal()), walked like sys_render() of * sys_sdl2.c: [[first line, palette offset], ...]. The first entry applies from line 0. * @return array<int, array{int, int}> */function image_line_palettes(): array{    $fp = Img::$firstpal;    $stride = ImageConst::LINEPAL_STRIDE;    $list = [[0, ($fp[2] ?? 0) & 0xffff]];    for ($k = $stride; isset($fp[$k]) && $fp[$k] != 0xff; $k += $stride) {        $list[] = [$fp[$k + 1] ?? 0, ($fp[$k + 2] ?? 0) & 0xffff];    }    return $list;}// image_mouse_cursor() (sys_dirty_mouse decoding) is in host.php.