Files
winf-photobooth-app/src/artwork.rs
bankosegger 34f5914bb1 feat: draw snowy fir trees above the photo and icicles below it
Replaces the random pixel noise with Christmas scenery: snow next to the
photo, small trees and flakes on top, icicles hanging underneath.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-18 17:48:16 +01:00

169 lines
5.7 KiB
Rust

//! Christmas pixel art that frames the photo on every printed slip.
//!
//! Both strips are a few lines of pixels. The first line, the snow next to the
//! photo, is solid; the pixels above it thin out away from the photo. Lines
//! are ordered from the photo outward, so index 0 is always the solid one.
/// Lines of pixels in a strip.
pub const ROWS: usize = 4;
/// Pixels across the paper that a strip aims for; the printer picks the pixel
/// size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
pub const CELLS_ACROSS: usize = 48;
pub type Lines = [Vec<bool>; ROWS];
/// Snow with small fir trees standing on it and a few snowflakes in between,
/// for the strip above the photo.
///
/// Trees are 3 or 5 pixels wide at the base and narrow by two pixels per line
/// to a single tip, so they are 2 or 3 lines high.
pub fn trees(seed: u64, columns: usize) -> Lines {
let mut state = seed;
let mut lines = snow(columns);
let mut left = (next(&mut state) % 4) as usize;
loop {
let base = if chance(&mut state, 50) { 5 } else { 3 };
if left + base > columns {
break;
}
for (line, cells) in lines.iter_mut().enumerate().skip(1) {
let Some(width) = (base + 2).checked_sub(2 * line).filter(|width| *width > 0) else {
continue;
};
let start = left + (base - width) / 2;
cells[start..start + width].fill(true);
}
left += base + 1 + (next(&mut state) % 4) as usize;
}
for cells in lines.iter_mut().skip(1) {
for cell in cells.iter_mut().filter(|cell| !**cell) {
*cell = chance(&mut state, 4);
}
}
lines
}
/// Snow with icicles hanging from it, for the strip below the photo.
///
/// Icicles are one pixel wide and 1 to 3 pixels long, never side by side, so
/// the longer ones are rarer and the strip thins out downwards.
pub fn icicles(seed: u64, columns: usize) -> Lines {
let mut state = seed;
let mut lines = snow(columns);
let mut hanging = false;
for column in 0..columns {
if std::mem::take(&mut hanging) || !chance(&mut state, 55) {
continue;
}
let length = match next(&mut state) % 10 {
0..=4 => 1,
5..=7 => 2,
_ => 3,
};
for cells in &mut lines[1..=length] {
cells[column] = true;
}
hanging = true;
}
lines
}
/// The solid first line and nothing above it yet.
fn snow(columns: usize) -> Lines {
std::array::from_fn(|line| vec![line == 0; columns])
}
fn chance(state: &mut u64, percent: u64) -> bool {
next(state) % 100 < percent
}
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
fn next(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut mixed = *state;
mixed = (mixed ^ (mixed >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
mixed = (mixed ^ (mixed >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
mixed ^ (mixed >> 31)
}
#[cfg(test)]
mod tests {
use super::*;
const WIDTHS: [usize; 4] = [8, 16, 48, 50];
fn count(cells: &[bool]) -> usize {
cells.iter().filter(|cell| **cell).count()
}
/// Longest run of set pixels in one line.
fn longest_run(cells: &[bool]) -> usize {
cells
.split(|cell| !cell)
.map(<[bool]>::len)
.max()
.unwrap_or(0)
}
#[test]
fn snow_is_solid_and_the_strips_have_the_right_size() {
for columns in WIDTHS {
for seed in 0..200 {
for lines in [trees(seed, columns), icicles(seed, columns)] {
assert_eq!(count(&lines[0]), columns, "{columns} columns, seed {seed}");
assert!(lines.iter().all(|line| line.len() == columns));
}
}
}
}
#[test]
fn trees_have_a_base_and_thin_out_upwards() {
for seed in 0..200 {
let lines = trees(seed, 48);
// Every strip has trees: runs of at least three pixels on line 1.
assert!(longest_run(&lines[1]) >= 3, "seed {seed}");
// Each tree line is sparser than the one below it, on the whole.
assert!(count(&lines[1]) < 48, "seed {seed}");
assert!(count(&lines[1]) > count(&lines[3]), "seed {seed}");
}
}
#[test]
fn icicles_hang_from_the_snow_apart_and_thin_out_downwards() {
let mut longest = 0;
for columns in WIDTHS {
for seed in 0..200 {
let lines = icicles(seed, columns);
for column in 0..columns {
for line in 1..ROWS {
// Hanging, not floating.
assert!(!lines[line][column] || lines[line - 1][column]);
}
if column > 0 {
assert!(!(lines[1][column] && lines[1][column - 1]));
}
}
assert!(count(&lines[1]) >= count(&lines[2]));
assert!(count(&lines[2]) >= count(&lines[3]));
longest = longest.max(count(&lines[3]));
}
}
assert!(longest > 0, "no icicle ever reaches the third line");
}
#[test]
fn a_seed_gives_one_picture_and_seeds_give_many() {
assert_eq!(trees(7, 48), trees(7, 48));
assert_eq!(icicles(7, 48), icicles(7, 48));
let distinct = |picture: fn(u64, usize) -> Lines| {
(0..1000)
.map(|seed| picture(seed, 48))
.collect::<std::collections::HashSet<_>>()
.len()
};
assert_eq!(distinct(trees), 1000);
assert_eq!(distinct(icicles), 1000);
}
}