Skip to main content

nes_sim/
headless.rs

1use crate::api::VideoFrame;
2use crate::video::palette_index_to_rgb;
3use std::fs;
4use std::io;
5use std::path::Path;
6
7pub fn frame_to_ppm(frame: VideoFrame<'_>) -> Vec<u8> {
8    let mut ppm = Vec::with_capacity(16 + frame.pixels.len() * 3);
9    ppm.extend_from_slice(format!("P6\n{} {}\n255\n", frame.width, frame.height).as_bytes());
10    for &pixel in frame.pixels {
11        ppm.extend_from_slice(&palette_index_to_rgb(pixel));
12    }
13    ppm
14}
15
16pub fn write_frame_ppm<P: AsRef<Path>>(path: P, frame: VideoFrame<'_>) -> io::Result<()> {
17    fs::write(path, frame_to_ppm(frame))
18}
19
20pub fn stable_byte_hash(bytes: &[u8]) -> u64 {
21    let mut hash = 0xcbf29ce484222325u64;
22    for &byte in bytes {
23        hash ^= u64::from(byte);
24        hash = hash.wrapping_mul(0x100000001b3);
25    }
26    hash
27}