Skip to main content

hypergraph_rs/raster/
png.rs

1//! Rust-side PNG pixel-plate emission (Task 14, ADR097 Tier-1).
2//!
3//! A **pixel** plate: one `Rgb` per pixel at arbitrary `(width_px,
4//! height_px)`, rasterized from the SAME `SceneGraph3D` the instruments
5//! build — a different tier from the Tier-0 braille `CellGrid`. Reuses the
6//! shared geometry core verbatim (reuse ledger, no duplicated math):
7//! near-plane clip + `project_wh` + `depth_sort` from `camera.rs` (Task 2),
8//! and the edge-function triangle fill + integer Bresenham + NaN-safe depth
9//! test + bounds-clamped writes from `grid.rs` (Task 3), targeting a pixel
10//! `SubBuffer` instead of the `cols*2 x rows*4` braille one.
11
12use super::grid::{draw_scene, SubBuffer};
13use super::{Camera, Rgb, SceneGraph3D};
14
15/// A full-resolution RGB8 pixel raster, row-major, `px.len() == w*h`.
16/// Every pixel carries an `Rgb` unconditionally — the Tier-1 analogue of
17/// the Tier-0 glyph floor (background pixels get the fixed clear color).
18#[derive(Debug, Clone, PartialEq)]
19pub struct PixelBuffer {
20    pub w: u32,
21    pub h: u32,
22    pub px: Vec<Rgb>,
23}
24
25/// Fixed clear/background color for pixels the z-test never wrote — pinned
26/// so the buffer stays fully defined (mirrors the Tier-0 glyph floor).
27const CLEAR: Rgb = Rgb(0, 0, 0);
28
29/// Rasterize `scene` under `cam` into a `width_px x height_px` `PixelBuffer`
30/// (Task 14) via the SAME `grid::draw_scene` core the Tier-0 braille
31/// `rasterize` calls — near-plane clip, painter's depth sort, triangle
32/// fill, integer Bresenham struts and `Node3` discs (review MAJOR 3), only
33/// the target resolution differs — then read the buffer out directly as
34/// `Rgb` per pixel (no braille composition — Tier-1 is raw pixels, and no
35/// glyph banner: a pixel plate has no text cells). Pure function of
36/// `(scene, cam, width_px, height_px)` — byte-exact golden-testable.
37pub fn render_pixels(
38    scene: &SceneGraph3D,
39    cam: &Camera,
40    width_px: u32,
41    height_px: u32,
42) -> PixelBuffer {
43    let mut buf = SubBuffer::new(width_px as usize, height_px as usize);
44    draw_scene(&mut buf, scene, cam, width_px as f64, height_px as f64);
45
46    PixelBuffer {
47        w: width_px,
48        h: height_px,
49        px: buf.to_rgb_plate(CLEAR),
50    }
51}
52
53/// Encode a `render_pixels` plate as PNG bytes (RGB8, 8-bit) via the `png`
54/// crate with PINNED encoder settings (`Compression::Fast` +
55/// `Filter::NoFilter`, frozen — never varied — for reproducibility). The
56/// pixel geometry (basic IEEE-754 arithmetic) is byte-exact cross-platform;
57/// the DEFLATE-compressed bytes returned here are only guaranteed
58/// byte-exact on the reference platform under this pinned `png` 0.18
59/// (CLAUDE.md §4 — decode-and-compare is the portable golden, see
60/// `test_png_decodes_back_to_exact_pixelbuffer`).
61pub fn render_png(scene: &SceneGraph3D, cam: &Camera, width_px: u32, height_px: u32) -> Vec<u8> {
62    let pb = render_pixels(scene, cam, width_px, height_px);
63
64    let mut bytes: Vec<u8> = Vec::new();
65    {
66        let mut encoder = png::Encoder::new(&mut bytes, pb.w, pb.h);
67        encoder.set_color(png::ColorType::Rgb);
68        encoder.set_depth(png::BitDepth::Eight);
69        encoder.set_compression(png::Compression::Fast);
70        encoder.set_filter(png::Filter::NoFilter);
71
72        let mut writer = encoder.write_header().expect("valid PNG header");
73        let mut data = Vec::with_capacity(pb.px.len() * 3);
74        for Rgb(r, g, b) in &pb.px {
75            data.push(*r);
76            data.push(*g);
77            data.push(*b);
78        }
79        writer
80            .write_image_data(&data)
81            .expect("in-memory PNG write cannot fail");
82    }
83
84    bytes
85}