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
|
//! Both strips are a few lines of pixels. The first line, the snow next to the
|
||||||
//! strip dissolves towards the bottom edge of the slip.
|
//! 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;
|
pub const ROWS: usize = 4;
|
||||||
/// Pixels across the paper that the strip aims for; the printer picks the
|
/// Pixels across the paper that a strip aims for; the printer picks the pixel
|
||||||
/// pixel size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
|
/// size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
|
||||||
pub const CELLS_ACROSS: usize = 48;
|
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
|
pub type Lines = [Vec<bool>; ROWS];
|
||||||
/// strip. Every line has exactly `columns * DENSITY / 100` pixels set, in
|
|
||||||
/// random places.
|
/// Snow with small fir trees standing on it and a few snowflakes in between,
|
||||||
pub fn pattern(seed: u64, columns: usize) -> [Vec<bool>; ROWS] {
|
/// 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 state = seed;
|
||||||
std::array::from_fn(|row| {
|
let mut lines = snow(columns);
|
||||||
let filled = columns * DENSITY[row] / 100;
|
let mut left = (next(&mut state) % 4) as usize;
|
||||||
// Partial Fisher-Yates shuffle: the first `filled` entries are a
|
loop {
|
||||||
// uniform random choice of columns.
|
let base = if chance(&mut state, 50) { 5 } else { 3 };
|
||||||
let mut order: Vec<usize> = (0..columns).collect();
|
if left + base > columns {
|
||||||
for index in 0..filled {
|
break;
|
||||||
let other = index + (next(&mut state) % (columns - index) as u64) as usize;
|
|
||||||
order.swap(index, other);
|
|
||||||
}
|
}
|
||||||
let mut line = vec![false; columns];
|
for (line, cells) in lines.iter_mut().enumerate().skip(1) {
|
||||||
for &column in &order[..filled] {
|
let Some(width) = (base + 2).checked_sub(2 * line).filter(|width| *width > 0) else {
|
||||||
line[column] = true;
|
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.
|
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
|
||||||
@@ -46,42 +90,79 @@ fn next(state: &mut u64) -> u64 {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn the_first_line_is_solid_and_each_line_is_sparser() {
|
fn snow_is_solid_and_the_strips_have_the_right_size() {
|
||||||
for columns in [8, 16, 48, 50] {
|
for columns in WIDTHS {
|
||||||
for seed in 0..200 {
|
for seed in 0..200 {
|
||||||
let lines = pattern(seed, columns);
|
for lines in [trees(seed, columns), icicles(seed, columns)] {
|
||||||
let counts: Vec<usize> = lines
|
assert_eq!(count(&lines[0]), columns, "{columns} columns, seed {seed}");
|
||||||
.iter()
|
assert!(lines.iter().all(|line| line.len() == columns));
|
||||||
.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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_seed_gives_one_pattern_and_seeds_give_many() {
|
fn trees_have_a_base_and_thin_out_upwards() {
|
||||||
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];
|
|
||||||
for seed in 0..200 {
|
for seed in 0..200 {
|
||||||
for (column, set) in pattern(seed, 48)[3].iter().enumerate() {
|
let lines = trees(seed, 48);
|
||||||
seen[column] |= *set;
|
// 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
|
//! 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
|
//! 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
|
//! 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
|
//! English only. Christmas pixel art (see `artwork`) frames the photo: snow
|
||||||
//! above and below, solid next to it and thinning out away from it. The
|
//! with small fir trees above it and snow with icicles below it, different on
|
||||||
//! timestamp is the system clock in local time, so the booth's clock has to be right for it to mean
|
//! every slip. The timestamp is the system clock in local time, so the
|
||||||
//! anything. Text goes out in the printer's own code page (PC437), since it
|
//! 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`.
|
//! prints UTF-8 bytes as separate glyphs; see `code_page`.
|
||||||
//!
|
//!
|
||||||
//! Each photo is converted to gray, resized to the exact dot width, contrast
|
//! Each photo is converted to gray, resized to the exact dot width, contrast
|
||||||
@@ -365,15 +365,15 @@ fn encode(
|
|||||||
) -> Vec<Vec<u8>> {
|
) -> Vec<Vec<u8>> {
|
||||||
let mut job = Vec::new();
|
let mut job = Vec::new();
|
||||||
for copy in 0..copies {
|
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 dots = bitmap.width_bytes * 8;
|
||||||
let seed = seed.wrapping_add(2 * copy as u64);
|
let seed = seed.wrapping_add(2 * copy as u64);
|
||||||
job.push(header(head));
|
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, bitmap, head);
|
||||||
raster_bands(
|
raster_bands(
|
||||||
&mut job,
|
&mut job,
|
||||||
&pixel_strip(dots, seed.wrapping_add(1), false),
|
&pixel_strip(dots, seed.wrapping_add(1), Side::Below),
|
||||||
head,
|
head,
|
||||||
);
|
);
|
||||||
job.push(footer(
|
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
|
/// Which side of the photo a pixel strip goes on.
|
||||||
/// get sparser downwards, or upwards when `solid_last`, so that the solid line
|
#[derive(Clone, Copy)]
|
||||||
/// can sit next to the photo on either side. Pixels are 12 dots on 80 mm paper
|
enum Side {
|
||||||
/// and 8 on 58 mm.
|
/// Snow with trees standing on it, so the snow is the strip's bottom line.
|
||||||
fn pixel_strip(dots: usize, seed: u64, solid_last: bool) -> Bitmap {
|
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 cell = (dots / CELLS_ACROSS).max(1);
|
||||||
let columns = dots / cell;
|
let columns = dots / cell;
|
||||||
let left = (dots - columns * cell) / 2;
|
let left = (dots - columns * cell) / 2;
|
||||||
let stride = dots / 8;
|
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];
|
let mut data = vec![0u8; stride * ROWS * cell];
|
||||||
for y in 0..ROWS * cell {
|
for y in 0..ROWS * cell {
|
||||||
for x in 0..columns * cell {
|
for x in 0..columns * cell {
|
||||||
let line = if solid_last {
|
// Lines are numbered from the photo outward.
|
||||||
ROWS - 1 - y / cell
|
let line = match side {
|
||||||
} else {
|
Side::Above => ROWS - 1 - y / cell,
|
||||||
y / cell
|
Side::Below => y / cell,
|
||||||
};
|
};
|
||||||
if lines[line][x / cell] {
|
if lines[line][x / cell] {
|
||||||
let dot = left + x;
|
let dot = left + x;
|
||||||
@@ -701,11 +712,11 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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] {
|
for dots in [384, 576] {
|
||||||
let cell = dots / CELLS_ACROSS;
|
let cell = dots / CELLS_ACROSS;
|
||||||
for solid_last in [false, true] {
|
for side in [Side::Above, Side::Below] {
|
||||||
let strip = pixel_strip(dots, 42, solid_last);
|
let strip = pixel_strip(dots, 42, side);
|
||||||
assert_eq!(strip.width_bytes, dots / 8);
|
assert_eq!(strip.width_bytes, dots / 8);
|
||||||
assert_eq!(strip.data.len(), dots / 8 * ROWS * cell);
|
assert_eq!(strip.data.len(), dots / 8 * ROWS * cell);
|
||||||
let lit = |row: usize, dot: usize| {
|
let lit = |row: usize, dot: usize| {
|
||||||
@@ -715,13 +726,14 @@ mod tests {
|
|||||||
let mut counts: Vec<usize> = (0..ROWS)
|
let mut counts: Vec<usize> = (0..ROWS)
|
||||||
.map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count())
|
.map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count())
|
||||||
.collect();
|
.collect();
|
||||||
if solid_last {
|
if matches!(side, Side::Above) {
|
||||||
counts.reverse();
|
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_eq!(counts[0], dots);
|
||||||
assert!(
|
assert!(
|
||||||
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
counts[1..].iter().all(|count| (1..dots).contains(count)),
|
||||||
"{counts:?}"
|
"{counts:?}"
|
||||||
);
|
);
|
||||||
// Pixels are solid squares: every dot row of a line is the same.
|
// Pixels are solid squares: every dot row of a line is the same.
|
||||||
|
|||||||
Reference in New Issue
Block a user