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:
185
src/artwork.rs
185
src/artwork.rs
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,10 +26,10 @@
|
||||
//! Each photo is captioned with the event name and place above it, and a
|
||||
//! keepsake line, the capture date and time and one of `QUOTES` below it. The
|
||||
//! captions are in German with the English directly underneath, the quotes in
|
||||
//! English only. A strip of random pixel art (see `artwork`) frames the photo
|
||||
//! above and below, solid next to it and thinning out away from it. The
|
||||
//! timestamp is the system clock in local time, so the booth's clock has to be right for it to mean
|
||||
//! anything. Text goes out in the printer's own code page (PC437), since it
|
||||
//! English only. Christmas pixel art (see `artwork`) frames the photo: snow
|
||||
//! with small fir trees above it and snow with icicles below it, different on
|
||||
//! every slip. The timestamp is the system clock in local time, so the
|
||||
//! booth's clock has to be right for it to mean anything. Text goes out in the printer's own code page (PC437), since it
|
||||
//! prints UTF-8 bytes as separate glyphs; see `code_page`.
|
||||
//!
|
||||
//! Each photo is converted to gray, resized to the exact dot width, contrast
|
||||
@@ -365,15 +365,15 @@ fn encode(
|
||||
) -> Vec<Vec<u8>> {
|
||||
let mut job = Vec::new();
|
||||
for copy in 0..copies {
|
||||
// Two different strips frame the photo, each solid next to it.
|
||||
// Trees above the photo and icicles below it, new ones for every copy.
|
||||
let dots = bitmap.width_bytes * 8;
|
||||
let seed = seed.wrapping_add(2 * copy as u64);
|
||||
job.push(header(head));
|
||||
raster_bands(&mut job, &pixel_strip(dots, seed, true), head);
|
||||
raster_bands(&mut job, &pixel_strip(dots, seed, Side::Above), head);
|
||||
raster_bands(&mut job, bitmap, head);
|
||||
raster_bands(
|
||||
&mut job,
|
||||
&pixel_strip(dots, seed.wrapping_add(1), false),
|
||||
&pixel_strip(dots, seed.wrapping_add(1), Side::Below),
|
||||
head,
|
||||
);
|
||||
job.push(footer(
|
||||
@@ -400,23 +400,34 @@ fn raster_bands(job: &mut Vec<Vec<u8>>, bitmap: &Bitmap, head: &Head) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The random pixel strip across the whole paper: `ROWS` lines of pixels that
|
||||
/// get sparser downwards, or upwards when `solid_last`, so that the solid line
|
||||
/// can sit next to the photo on either side. Pixels are 12 dots on 80 mm paper
|
||||
/// and 8 on 58 mm.
|
||||
fn pixel_strip(dots: usize, seed: u64, solid_last: bool) -> Bitmap {
|
||||
/// Which side of the photo a pixel strip goes on.
|
||||
#[derive(Clone, Copy)]
|
||||
enum Side {
|
||||
/// Snow with trees standing on it, so the snow is the strip's bottom line.
|
||||
Above,
|
||||
/// Snow with icicles hanging from it, so the snow is the strip's top line.
|
||||
Below,
|
||||
}
|
||||
|
||||
/// The random Christmas pixel strip across the whole paper, `ROWS` lines of
|
||||
/// pixels with the solid snow next to the photo. Pixels are 12 dots on 80 mm
|
||||
/// paper and 8 on 58 mm.
|
||||
fn pixel_strip(dots: usize, seed: u64, side: Side) -> Bitmap {
|
||||
let cell = (dots / CELLS_ACROSS).max(1);
|
||||
let columns = dots / cell;
|
||||
let left = (dots - columns * cell) / 2;
|
||||
let stride = dots / 8;
|
||||
let lines = artwork::pattern(seed, columns);
|
||||
let lines = match side {
|
||||
Side::Above => artwork::trees(seed, columns),
|
||||
Side::Below => artwork::icicles(seed, columns),
|
||||
};
|
||||
let mut data = vec![0u8; stride * ROWS * cell];
|
||||
for y in 0..ROWS * cell {
|
||||
for x in 0..columns * cell {
|
||||
let line = if solid_last {
|
||||
ROWS - 1 - y / cell
|
||||
} else {
|
||||
y / cell
|
||||
// Lines are numbered from the photo outward.
|
||||
let line = match side {
|
||||
Side::Above => ROWS - 1 - y / cell,
|
||||
Side::Below => y / cell,
|
||||
};
|
||||
if lines[line][x / cell] {
|
||||
let dot = left + x;
|
||||
@@ -701,11 +712,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frames_the_photo_with_pixel_strips_that_thin_out_away_from_it() {
|
||||
fn frames_the_photo_with_snow_trees_and_icicles() {
|
||||
for dots in [384, 576] {
|
||||
let cell = dots / CELLS_ACROSS;
|
||||
for solid_last in [false, true] {
|
||||
let strip = pixel_strip(dots, 42, solid_last);
|
||||
for side in [Side::Above, Side::Below] {
|
||||
let strip = pixel_strip(dots, 42, side);
|
||||
assert_eq!(strip.width_bytes, dots / 8);
|
||||
assert_eq!(strip.data.len(), dots / 8 * ROWS * cell);
|
||||
let lit = |row: usize, dot: usize| {
|
||||
@@ -715,13 +726,14 @@ mod tests {
|
||||
let mut counts: Vec<usize> = (0..ROWS)
|
||||
.map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count())
|
||||
.collect();
|
||||
if solid_last {
|
||||
if matches!(side, Side::Above) {
|
||||
counts.reverse();
|
||||
}
|
||||
// The line next to the photo spans the paper; the others thin out.
|
||||
// The snow next to the photo spans the paper; what is on it
|
||||
// does not, and is there.
|
||||
assert_eq!(counts[0], dots);
|
||||
assert!(
|
||||
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
||||
counts[1..].iter().all(|count| (1..dots).contains(count)),
|
||||
"{counts:?}"
|
||||
);
|
||||
// Pixels are solid squares: every dot row of a line is the same.
|
||||
|
||||
Reference in New Issue
Block a user