// SPDX-License-Identifier: MIT //! Saved pictures: the board's frame as the player sees it, written as PNG //! (`picture.py`). //! //! The LCD is 160 x 102 (landscape). Some carts are played with the console turned on //! its side; the cart (not the hardware) says so in its rotation field (LNX header byte //! 58: 0 none, 1 left, 2 right; or the cart table: docs/cartridge.md). The board never //! rotates anything (start-here.md "Output"); a host that shows or saves the picture //! turns it so the game reads upright. //! //! Direction. Looked at on the reference's own pictures, 2026-11-14 (its //! docs/validation.md "Saved pictures follow the cart's rotation"): Klax (table: right) //! reads upright after a quarter turn clockwise and upside down after a quarter turn //! anticlockwise; Centipede (Proto) (table: left) the other way round. No document //! defines the field's direction (HYPOTHESIS for carts whose text cannot be looked at: //! the same rule). //! //! Differences forced by Rust: the PNG's compressed stream is flate2's at level 9, not //! zlib's, so the file's bytes may differ from the reference's; its pixels do not. use std::io::Write; use std::path::Path; pub const WIDTH: usize = 160; pub const HEIGHT: usize = 102; /// Turn an RGB picture for the cart's rotation; returns (rgb, width, height). /// /// `right`: a quarter turn clockwise; `left`: a quarter turn anticlockwise; /// `none`: unchanged (see the module text for the evidence). pub fn rotate(rgb: &[u8], rotation: &str, width: usize, height: usize) -> (Vec, usize, usize) { if rotation != "left" && rotation != "right" { return (rgb.to_vec(), width, height); } let mut out = vec![0u8; rgb.len()]; for y in 0..height { for x in 0..width { let (nx, ny) = if rotation == "right" { // clockwise: (x, y) -> (height - 1 - y, x) (height - 1 - y, x) } else { // anticlockwise: (x, y) -> (y, width - 1 - x) (y, width - 1 - x) }; let src = 3 * (y * width + x); let dst = 3 * (ny * height + nx); out[dst..dst + 3].copy_from_slice(&rgb[src..src + 3]); } } (out, height, width) } /// Nearest-neighbour enlargement (for looking at a picture, not for judging). pub fn scale(rgb: &[u8], width: usize, height: usize, factor: usize) -> Vec { let mut out = Vec::with_capacity(rgb.len() * factor * factor); for y in 0..height { let mut row = Vec::with_capacity(width * 3 * factor); for x in 0..width { for _ in 0..factor { row.extend_from_slice(&rgb[3 * (y * width + x)..3 * (y * width + x) + 3]); } } for _ in 0..factor { out.extend_from_slice(&row); } } out } /// An 8-bit RGB PNG (the format's own chunks). pub fn png_bytes(rgb: &[u8], width: usize, height: usize) -> Vec { let mut rows = Vec::with_capacity((width * 3 + 1) * height); for y in 0..height { rows.push(0); rows.extend_from_slice(&rgb[y * width * 3..(y + 1) * width * 3]); } fn chunk(kind: &[u8], data: &[u8]) -> Vec { let mut body = kind.to_vec(); body.extend_from_slice(data); let mut out = (data.len() as u32).to_be_bytes().to_vec(); out.extend_from_slice(&body); out.extend_from_slice(&crc32fast::hash(&body).to_be_bytes()); out } let mut header = Vec::new(); header.extend((width as u32).to_be_bytes()); header.extend((height as u32).to_be_bytes()); header.extend([8, 2, 0, 0, 0]); let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::new(9)); encoder.write_all(&rows).unwrap(); let compressed = encoder.finish().unwrap(); let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); png.extend(chunk(b"IHDR", &header)); png.extend(chunk(b"IDAT", &compressed)); png.extend(chunk(b"IEND", b"")); png } pub fn write_png(path: &Path, rgb: &[u8], width: usize, height: usize) -> std::io::Result<()> { std::fs::write(path, png_bytes(rgb, width, height)) } /// Save the frame as the player sees it: turned for the cart's rotation. pub fn save(path: &Path, rgb: &[u8], rotation: &str) -> std::io::Result<()> { let (turned, width, height) = rotate(rgb, rotation, WIDTH, HEIGHT); write_png(path, &turned, width, height) }