"""Rebuild a save from photos of the ROM's QR pages (START on the results list). python tools/qrdecode.py OUT.sav PHOTO_OR_DIR [...] Each code holds an 8-byte head ("MQ", page, pages, save offset, length) and that many bytes of the save. Pages may come in any order, repeated or from several photos; the result is a 32 KB save with the head, the boot and reset logs and the run the codes carried, the rest zero, which tools/decode.py and tools/timing.py read like any other save. Needs zxing-cpp (pip install zxing-cpp) and Pillow. """ import struct import sys from pathlib import Path import zxingcpp from PIL import Image, ImageOps SAVE_BYTES = 32768 def codes(path): img = ImageOps.exif_transpose(Image.open(path)).convert("L") found = list(zxingcpp.read_barcodes(img)) if not found: # a large photo of a TV: try it smaller, where the moire is gone w, h = img.size for scale in (2, 3, 4): found = list(zxingcpp.read_barcodes(img.resize((w // scale, h // scale)))) if found: break for r in found: b = r.bytes if len(b) >= 8 and b[:2] == b"MQ": page, pages, off, n = b[2], b[3], *struct.unpack(">HH", b[4:8]) if len(b) == 8 + n: yield page, pages, off, b[8:] def main(): out, inputs = Path(sys.argv[1]), sys.argv[2:] files = [] for p in map(Path, inputs): files += sorted(x for x in p.iterdir() if x.suffix.lower() in (".jpg", ".jpeg", ".png", ".heic")) if p.is_dir() else [p] save, seen, total = bytearray(SAVE_BYTES), {}, None for f in files: got = list(codes(f)) for page, pages, off, data in got: if page in seen and seen[page] != (off, data): print(f"{f.name}: page {page + 1} differs from an earlier read; keeping the first") continue seen[page] = (off, data) total = pages save[off:off + len(data)] = data print(f"{f.name}: " + (", ".join(f"page {p + 1} of {n}" for p, n, _, _ in got) if got else "no code read")) if total is None: sys.exit("no codes read") missing = [p + 1 for p in range(total) if p not in seen] out.write_bytes(save) print(f"{len(seen)} of {total} pages -> {out}" + (f"; missing {missing}" if missing else "")) if __name__ == "__main__": main()