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>
This commit is contained in:
bankosegger
2026-11-18 17:48:16 +01:00
parent 4a3149bab3
commit 34f5914bb1
2 changed files with 168 additions and 75 deletions

View File

@@ -1,36 +1,80 @@
//! A strip of random pixel art for the bottom of every printed slip.
//! Christmas pixel art that frames the photo on every printed slip.
//!
//! The first line is solid; every line below it has fewer pixels, so the
//! strip dissolves towards the bottom edge of the 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 the strip.
/// Lines of pixels in a strip.
pub const ROWS: usize = 4;
/// Pixels across the paper that the strip aims for; the printer picks the
/// pixel size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
/// 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;
/// Percent of the pixels that are set in each line, from the solid one down.
const DENSITY: [usize; ROWS] = [100, 65, 35, 12];
/// The pixels of each line for `seed`; the same seed always gives the same
/// strip. Every line has exactly `columns * DENSITY / 100` pixels set, in
/// random places.
pub fn pattern(seed: u64, columns: usize) -> [Vec<bool>; ROWS] {
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;
std::array::from_fn(|row| {
let filled = columns * DENSITY[row] / 100;
// Partial Fisher-Yates shuffle: the first `filled` entries are a
// uniform random choice of columns.
let mut order: Vec<usize> = (0..columns).collect();
for index in 0..filled {
let other = index + (next(&mut state) % (columns - index) as u64) as usize;
order.swap(index, other);
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;
}
let mut line = vec![false; columns];
for &column in &order[..filled] {
line[column] = true;
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);
}
line
})
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.
@@ -46,42 +90,79 @@ fn next(state: &mut u64) -> u64 {
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 the_first_line_is_solid_and_each_line_is_sparser() {
for columns in [8, 16, 48, 50] {
fn snow_is_solid_and_the_strips_have_the_right_size() {
for columns in WIDTHS {
for seed in 0..200 {
let lines = pattern(seed, columns);
let counts: Vec<usize> = lines
.iter()
.map(|line| line.iter().filter(|cell| **cell).count())
.collect();
assert_eq!(counts[0], columns, "{columns} columns, seed {seed}");
assert!(
counts.windows(2).all(|pair| pair[0] > pair[1]),
"{columns} columns, seed {seed}: {counts:?}"
);
assert!(lines.iter().all(|line| line.len() == columns));
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 a_seed_gives_one_pattern_and_seeds_give_many() {
assert_eq!(pattern(7, 48), pattern(7, 48));
let distinct: std::collections::HashSet<_> =
(0..1000).map(|seed| pattern(seed, 48)).collect();
assert_eq!(distinct.len(), 1000);
}
#[test]
fn sparse_pixels_land_everywhere_across_the_strip() {
// Not clumped on one side: over many seeds each column gets used.
let mut seen = [false; 48];
fn trees_have_a_base_and_thin_out_upwards() {
for seed in 0..200 {
for (column, set) in pattern(seed, 48)[3].iter().enumerate() {
seen[column] |= *set;
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!(seen.iter().all(|used| *used));
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);
}
}