From 86c03f64b20be199e0fd380b5a478fde79857819 Mon Sep 17 00:00:00 2001 From: Elise Amber Katze Date: Tue, 9 Jul 2024 22:08:24 +0200 Subject: [PATCH] feat: logs to a scene --- Cargo.lock | 30 +++++++++++++++++------------- Cargo.toml | 3 ++- src/main.rs | 14 ++++++++++++-- src/out.rs | 53 ++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2333e1..7e4ea5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,6 +199,15 @@ dependencies = [ "objc2-encode", ] +[[package]] +name = "bmp" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69985ff4f58085ac696454692d0b646a66ad1f9cc9be294c91dc51bb5df511ae" +dependencies = [ + "byteorder", +] + [[package]] name = "bumpalo" version = "3.16.0" @@ -513,6 +522,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "font8x8" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "875488b8711a968268c7cf5d139578713097ca4635a76044e8fe8eedf831d07e" + [[package]] name = "foreign-types" version = "0.3.2" @@ -651,18 +666,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df" -[[package]] -name = "image" -version = "0.25.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd54d660e773627692c524beaad361aca785a4f9f5730ce91f42aabe5bce3d11" -dependencies = [ - "bytemuck", - "byteorder", - "num-traits", - "png", -] - [[package]] name = "indexmap" version = "1.9.3" @@ -1373,9 +1376,10 @@ checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832" name = "rsrt" version = "0.1.0" dependencies = [ + "bmp", "criterion", "fastrand", - "image", + "font8x8", "paste", "rayon", "tracing", diff --git a/Cargo.toml b/Cargo.toml index 2c22fdb..6456c9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,9 @@ version = "0.1.0" edition = "2021" [dependencies] +bmp = "0.5.0" fastrand = "2.1.0" -image = { version = "0.25.1", default-features = false, features = ["png"] } +font8x8 = "0.3.1" paste = "1.0.15" rayon = "1.10.0" tracing = "0.1.40" diff --git a/src/main.rs b/src/main.rs index 72a5689..fce0970 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use rsrt::{cover, out}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +const LOGS: &str = include_str!("../logs.txt"); + fn main() { const WIDTH: usize = 1920; const HEIGHT: usize = 1080; @@ -10,7 +12,15 @@ fn main() { .with(tracing_subscriber::EnvFilter::from_default_env()) .init(); - let image = cover(WIDTH, HEIGHT, 500).render_multithread_rayon(); + let per_pixel = LOGS.len() / (WIDTH * HEIGHT); - out::write_image("./image.png", &image[..], WIDTH as u32, HEIGHT as u32).unwrap(); + if per_pixel == 0 { + panic!("Logs had to be more than {} bytes, found {} bytes", WIDTH * HEIGHT, LOGS.len()); + } + + println!("{} bytes found", LOGS.len()); + + let image = cover(WIDTH, HEIGHT, 200).render_multithread_rayon(); + + out::write_image("./image.png", &image[..], WIDTH as u32, HEIGHT as u32, LOGS).unwrap(); } diff --git a/src/out.rs b/src/out.rs index 5f50b29..1e83c39 100644 --- a/src/out.rs +++ b/src/out.rs @@ -1,5 +1,8 @@ use std::path::Path; +use bmp::Pixel; +use font8x8::{UnicodeFonts, BASIC_FONTS}; + use crate::{interval::Interval, vec3::Color}; fn linear_to_gamma(linear_component: f64) -> f64 { @@ -15,23 +18,47 @@ pub fn write_image>( image: &[Color], width: u32, height: u32, -) -> Result<(), image::ImageError> { - let mut out = image::ImageBuffer::new(width, height); + logs: &str, +) -> std::io::Result<()> { + let per_pixel = (logs.len() / (width as usize * height as usize)).min(1); + assert_ne!(per_pixel, 0); - for (x, y, pixel) in out.enumerate_pixels_mut() { - let index = (y * width) + x; + let mut out = bmp::Image::new(width * per_pixel as u32 * 8, height * per_pixel as u32 * 8); + + for y in 0..height { + for x in 0..width { + let index = (y as usize * width as usize) + x as usize; - let r = linear_to_gamma(image[index as usize].r()); - let g = linear_to_gamma(image[index as usize].g()); - let b = linear_to_gamma(image[index as usize].b()); + let r = linear_to_gamma(image[index as usize].r()); + let g = linear_to_gamma(image[index as usize].g()); + let b = linear_to_gamma(image[index as usize].b()); - const INTENSITY: Interval = Interval::new(0.000, 0.999); + const INTENSITY: Interval = Interval::new(0.000, 0.999); - *pixel = image::Rgb([ - (INTENSITY.clamp(r) * 256.0) as u8, - (INTENSITY.clamp(g) * 256.0) as u8, - (INTENSITY.clamp(b) * 256.0) as u8, - ]); + let r = (INTENSITY.clamp(r) * 256.0) as u8; + let g = (INTENSITY.clamp(g) * 256.0) as u8; + let b = (INTENSITY.clamp(b) * 256.0) as u8; + + for y_sub in 0..per_pixel { + for x_sub in 0..per_pixel { + let y_idx = (y as usize + y_sub) * width as usize; + let x_idx = x as usize + x_sub; + let letter = logs.chars().nth(y_idx + x_idx).unwrap(); + let letter_map = BASIC_FONTS.get(letter).unwrap(); + + for (char_row_idx, char_row) in letter_map.into_iter().enumerate() { + for bit in 0..8 { + let color = match char_row & 1 << bit { + 0 => Pixel::new(0, 0, 0), + _ => Pixel::new(r, g, b), + }; + + out.set_pixel((x_idx as u32 * 8) + bit as u32, ((y as u32 + y_sub as u32) * 8) + char_row_idx as u32, color); + } + } + } + } + } } out.save(path)?;