"""Reports from format 2, the single-screen cart before the framework. Kept for those results.""" import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) import build def parse(b): n = struct.unpack_from(">H", b, 8)[0] return {"hw": struct.unpack_from(">H", b, 6)[0], "records": [struct.unpack_from(">HHI", b, 16 + 8 * i) for i in range(n)]} def from_png(path): import numpy as np from PIL import Image a = np.asarray(Image.open(path).convert("L")) top, left = (a.shape[0] - 224) // 2, (a.shape[1] - 320) // 2 glyphs = {} for ch, spec in build.GLYPHS.items(): cell = np.zeros((8, 8), bool) for x, c in enumerate(int(h, 16) for h in spec.split()): for y in range(8): cell[y, x + 1] = c >> y & 1 glyphs[cell.tobytes()] = ch text = ["".join(glyphs.get((a[top + r * 8:top + r * 8 + 8, left + c * 8:left + c * 8 + 8] > 128).tobytes(), "?") for c in range(40)).rstrip() for r in range(28)] recs = [] for line in text[3:]: for part in (line[:13], line[13:26], line[26:]): f = part.split() if len(f) == 2: recs.append((int(f[0][:2], 16), int(f[0][2], 16), int(f[1], 16))) return {"hw": int(text[0].split("HW")[1].split()[0], 16), "records": recs} def load(path): p = Path(path) if p.suffix.lower() == ".png": return from_png(p) b = p.read_bytes() for layout in (b, b[1::2], b[::2]): if layout[:4] == b"MDPB": return parse(layout) raise SystemExit(f"{path}: no report")