32 lines
1 KiB
Python
32 lines
1 KiB
Python
"""Export a ZX Spectrum image as a .SNA snapshot (+ a standard .SCR alongside)."""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
from . import snapshot
|
|
|
|
_EXTS = (".sna", ".scr", ".z80")
|
|
|
|
|
|
def export_sna(conv, output_path, source_path=None, display="forever",
|
|
seconds=0, video="pal"):
|
|
if not output_path.lower().endswith(_EXTS):
|
|
output_path += ".sna"
|
|
screen = bytes(conv.data)
|
|
border = _dominant_paper(screen)
|
|
sna = snapshot.build_sna(screen, border=border)
|
|
with open(output_path, "wb") as f:
|
|
f.write(sna)
|
|
# also drop the standard raw-screen .scr next to it (interchange format)
|
|
scr_path = os.path.splitext(output_path)[0] + ".scr"
|
|
with open(scr_path, "wb") as f:
|
|
f.write(snapshot.build_scr(screen))
|
|
return output_path
|
|
|
|
|
|
def _dominant_paper(screen: bytes) -> int:
|
|
"""Most common paper colour across the 768 attribute cells -> border colour."""
|
|
counts = [0] * 8
|
|
for a in screen[6144:6912]:
|
|
counts[(a >> 3) & 7] += 1
|
|
return max(range(8), key=lambda c: counts[c])
|