"""Just enough PNG to write frames and to read MAME's snapshots, with the stdlib only. Writing: 8-bit RGB, no filtering. Reading: 8-bit RGB or RGBA, non-interlaced, all five filter types (PNG specification, section 9 "Filtering"). """ from __future__ import annotations import struct import zlib from pathlib import Path SIGNATURE = b"\x89PNG\r\n\x1a\n" def _chunk(kind: bytes, data: bytes) -> bytes: body = kind + data return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body)) def write_png(path: Path, rows: list[list[int]]) -> None: """Write rows of RGB888 ints as an 8-bit RGB PNG.""" height, width = len(rows), len(rows[0]) raw = bytearray() for row in rows: raw.append(0) # filter type 0: none for pixel in row: raw += bytes(((pixel >> 16) & 0xFF, (pixel >> 8) & 0xFF, pixel & 0xFF)) header = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) path.write_bytes( SIGNATURE + _chunk(b"IHDR", header) + _chunk(b"IDAT", zlib.compress(bytes(raw), 9)) + _chunk(b"IEND", b"") ) def read_png(path: Path) -> list[list[int]]: """Read an 8-bit RGB or RGBA PNG as rows of RGB888 ints (alpha dropped).""" data = path.read_bytes() if not data.startswith(SIGNATURE): raise ValueError(f"{path}: not a PNG") offset, idat = len(SIGNATURE), bytearray() width = height = channels = 0 while offset < len(data): (length,) = struct.unpack(">I", data[offset : offset + 4]) kind = data[offset + 4 : offset + 8] body = data[offset + 8 : offset + 8 + length] offset += 12 + length if kind == b"IHDR": width, height, depth, colour, _, _, interlace = struct.unpack(">IIBBBBB", body) if depth != 8 or colour not in (2, 6) or interlace: raise ValueError(f"{path}: only 8-bit non-interlaced RGB/RGBA is supported") channels = 3 if colour == 2 else 4 elif kind == b"IDAT": idat += body elif kind == b"IEND": break raw = zlib.decompress(bytes(idat)) stride = width * channels rows: list[list[int]] = [] previous = bytearray(stride) position = 0 for _ in range(height): kind = raw[position] line = bytearray(raw[position + 1 : position + 1 + stride]) position += 1 + stride for i in range(stride): left = line[i - channels] if i >= channels else 0 up = previous[i] upper_left = previous[i - channels] if i >= channels else 0 if kind == 1: line[i] = (line[i] + left) & 0xFF elif kind == 2: line[i] = (line[i] + up) & 0xFF elif kind == 3: line[i] = (line[i] + ((left + up) >> 1)) & 0xFF elif kind == 4: estimate = left + up - upper_left pa, pb, pc = abs(estimate - left), abs(estimate - up), abs(estimate - upper_left) predictor = left if pa <= pb and pa <= pc else (up if pb <= pc else upper_left) line[i] = (line[i] + predictor) & 0xFF rows.append( [line[x] << 16 | line[x + 1] << 8 | line[x + 2] for x in range(0, stride, channels)] ) previous = line return rows