From 67aa0e64c1011784b5222cf250538f840562b80d Mon Sep 17 00:00:00 2001 From: bankosegger Date: Wed, 18 Nov 2026 17:43:06 +0100 Subject: [PATCH] feat: replace the sprite with a full-width pixel strip that thins out The strip is four lines of 12-dot pixels across the paper: the first line is solid and each line below it has fewer pixels in random places. Co-Authored-By: Claude Sonnet 5.5 --- src/artwork.rs | 90 +++++++++++++++++++++++++++++++------------------- src/printer.rs | 75 +++++++++++++++++++++-------------------- 2 files changed, 95 insertions(+), 70 deletions(-) diff --git a/src/artwork.rs b/src/artwork.rs index 93f303b..3caac17 100644 --- a/src/artwork.rs +++ b/src/artwork.rs @@ -1,30 +1,36 @@ -//! A small random pixel-art sprite for the bottom of every printed slip. +//! A strip of random pixel art for the bottom of every printed slip. //! -//! The sprite is a few chunky cells high and mirrored left to right, which -//! makes random bits read as a little creature or emblem instead of noise. +//! The first line is solid; every line below it has fewer pixels, so the +//! strip dissolves towards the bottom edge of the slip. -pub const COLUMNS: usize = 8; +/// Lines of pixels in the strip. pub const ROWS: usize = 4; -const HALF: usize = COLUMNS / 2; -/// How many of the 16 cells of the left half may be filled: enough to read as -/// a shape, not so many that it turns into a block. -const FILLED: std::ops::RangeInclusive = 6..=11; +/// 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). +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]; -pub type Sprite = [[bool; COLUMNS]; ROWS]; - -/// The sprite for `seed`; the same seed always gives the same sprite. -pub fn sprite(seed: u64) -> Sprite { +/// 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; ROWS] { let mut state = seed; - loop { - let bits = next(&mut state) & 0xFFFF; - let row = |index: usize| (bits >> (index * HALF)) & 0xF; - if !FILLED.contains(&bits.count_ones()) || (0..ROWS).any(|index| row(index) == 0) { - continue; + 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 = (0..columns).collect(); + for index in 0..filled { + let other = index + (next(&mut state) % (columns - index) as u64) as usize; + order.swap(index, other); } - return std::array::from_fn(|y| { - std::array::from_fn(|x| row(y) >> x.min(COLUMNS - 1 - x) & 1 == 1) - }); - } + let mut line = vec![false; columns]; + for &column in &order[..filled] { + line[column] = true; + } + line + }) } /// SplitMix64: a few lines, well mixed, and good enough for decoration. @@ -41,25 +47,41 @@ mod tests { use super::*; #[test] - fn sprites_are_mirrored_filled_in_every_row_and_not_a_block() { - for seed in 0..2000 { - let sprite = sprite(seed); - let filled = sprite.iter().flatten().filter(|cell| **cell).count(); - assert!((12..=22).contains(&filled), "seed {seed}: {filled} cells"); - for row in sprite { - assert!(row.iter().any(|cell| *cell), "seed {seed}: empty row"); + fn the_first_line_is_solid_and_each_line_is_sparser() { + for columns in [8, 16, 48, 50] { + for seed in 0..200 { + let lines = pattern(seed, columns); + let counts: Vec = lines + .iter() + .map(|line| line.iter().filter(|cell| **cell).count()) + .collect(); + assert_eq!(counts[0], columns, "{columns} columns, seed {seed}"); assert!( - (0..COLUMNS).all(|x| row[x] == row[COLUMNS - 1 - x]), - "seed {seed}: not mirrored" + counts.windows(2).all(|pair| pair[0] > pair[1]), + "{columns} columns, seed {seed}: {counts:?}" ); + assert!(lines.iter().all(|line| line.len() == columns)); } } } #[test] - fn a_seed_gives_one_sprite_and_seeds_give_many() { - assert_eq!(sprite(7), sprite(7)); - let distinct: std::collections::HashSet = (0..1000).map(sprite).collect(); - assert!(distinct.len() > 200, "{} sprites", distinct.len()); + 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]; + for seed in 0..200 { + for (column, set) in pattern(seed, 48)[3].iter().enumerate() { + seen[column] |= *set; + } + } + assert!(seen.iter().all(|used| *used)); } } diff --git a/src/printer.rs b/src/printer.rs index e0be07c..aae1621 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -26,7 +26,7 @@ //! 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, then a small random pixel-art sprite (see `artwork`) closes +//! English only, then a strip of random pixel art (see `artwork`) closes //! the 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 @@ -55,7 +55,7 @@ use opencv::{ }; use crate::{ - artwork::{self, COLUMNS, ROWS}, + artwork::{self, CELLS_ACROSS, ROWS}, quotes::QUOTES, }; @@ -365,7 +365,7 @@ fn encode( ) -> Vec> { let mut job = Vec::new(); for copy in 0..copies { - let sprite = sprite_strip(bitmap.width_bytes * 8, seed.wrapping_add(copy as u64)); + let strip = pixel_strip(bitmap.width_bytes * 8, seed.wrapping_add(copy as u64)); job.push(header(head)); raster_bands(&mut job, bitmap, head); job.push(footer( @@ -373,7 +373,7 @@ fn encode( stamp, "ES[(first_quote + copy) % QUOTES.len()], )); - raster_bands(&mut job, &sprite, head); + raster_bands(&mut job, &strip, head); job.push(finish(head)); } job @@ -393,17 +393,18 @@ fn raster_bands(job: &mut Vec>, bitmap: &Bitmap, head: &Head) { } } -/// The random sprite centred on a full-width strip, `ROWS` cells high, with -/// cells that grow with the paper width (18 dots on 80 mm, 12 on 58 mm). -fn sprite_strip(dots: usize, seed: u64) -> Bitmap { - let cell = (dots / 32).max(1); - let left = (dots - COLUMNS * cell) / 2; +/// The random pixel strip across the whole paper, `ROWS` lines of pixels that +/// get sparser downwards. Pixels are 12 dots on 80 mm paper and 8 on 58 mm. +fn pixel_strip(dots: usize, seed: u64) -> Bitmap { + let cell = (dots / CELLS_ACROSS).max(1); + let columns = dots / cell; + let left = (dots - columns * cell) / 2; let stride = dots / 8; - let sprite = artwork::sprite(seed); + let lines = artwork::pattern(seed, columns); let mut data = vec![0u8; stride * ROWS * cell]; for y in 0..ROWS * cell { - for x in 0..COLUMNS * cell { - if sprite[y / cell][x / cell] { + for x in 0..columns * cell { + if lines[y / cell][x / cell] { let dot = left + x; data[y * stride + dot / 8] |= 0x80 >> (dot % 8); } @@ -474,7 +475,7 @@ fn first_quote() -> usize { % QUOTES.len() } -/// Seed for the sprites of one job, from the clock like the quote. +/// Seed for the pixel strips of one job, from the clock like the quote. fn clock_seed() -> u64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -494,7 +495,7 @@ fn footer(head: &Head, stamp: &str, quote: &[&str]) -> Vec { for line in quote { text_line(&mut write, line); } - write.extend([0x1B, b'd', 1]); // a blank line between the quote and the sprite + write.extend([0x1B, b'd', 1]); // a blank line between the quote and the pixel strip write } @@ -664,7 +665,7 @@ mod tests { }; let job = encode(&bitmap, &Head::default(), 2, STAMP, 0, 0); // Per copy: the caption, a full band, the leftover row, the footer, the - // sprite and the cut. + // pixel strip and the cut. assert_eq!(job.len(), 2 * 6); for copy in job.chunks(6) { assert_eq!( @@ -686,43 +687,45 @@ mod tests { } #[test] - fn closes_each_slip_with_a_centred_sprite_before_the_cut() { + fn closes_each_slip_with_a_fading_pixel_strip_before_the_cut() { for dots in [384, 576] { - let strip = sprite_strip(dots, 42); - let cell = dots / 32; + let strip = pixel_strip(dots, 42); + let cell = dots / CELLS_ACROSS; assert_eq!(strip.width_bytes, dots / 8); assert_eq!(strip.data.len(), dots / 8 * ROWS * cell); let lit = |row: usize, dot: usize| { strip.data[row * strip.width_bytes + dot / 8] & (0x80 >> (dot % 8)) != 0 }; - // Mirrored about the centre of the paper, nothing outside the sprite. - let margin = (dots - COLUMNS * cell) / 2; - for row in 0..ROWS * cell { - for dot in 0..dots { - assert_eq!( - lit(row, dot), - lit(row, dots - 1 - dot), - "{dots} {row} {dot}" - ); - if !(margin..dots - margin).contains(&dot) { - assert!(!lit(row, dot), "{dots} {row} {dot}"); + // The first line of pixels spans the paper; later ones thin out. + let counts: Vec = (0..ROWS) + .map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count()) + .collect(); + assert_eq!(counts[0], dots); + assert!( + counts.windows(2).all(|pair| pair[0] > pair[1]), + "{counts:?}" + ); + // Pixels are solid squares: every dot row of a line is the same. + for line in 0..ROWS { + for dot_row in 1..cell { + for dot in 0..dots { + assert_eq!(lit(line * cell, dot), lit(line * cell + dot_row, dot)); } } } - assert!(strip.data.iter().any(|byte| *byte != 0)); } - // After the footer come the sprite bands (72 rows at 576 dots), then the cut. + // After the footer comes the strip (48 rows at 576 dots), then the cut. let bitmap = Bitmap { width_bytes: 72, data: vec![0; 72], }; let job = encode(&bitmap, &Head::default(), 1, STAMP, 0, 5); - assert_eq!(job.len(), 6); // caption, photo, footer, 2 sprite bands, cut - assert!(job[3].starts_with(&[0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, BAND_ROWS as u8, 0])); - assert!(job[5].ends_with(&[0x1D, b'V', 66, 3])); - // Copies get different sprites. + assert_eq!(job.len(), 5); // caption, photo, footer, strip, cut + assert!(job[3].starts_with(&[0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, 48, 0])); + assert!(job[4].ends_with(&[0x1D, b'V', 66, 3])); + // Copies get different strips. let two = encode(&bitmap, &Head::default(), 2, STAMP, 0, 5); - assert_ne!(two[3], two[9]); + assert_ne!(two[3], two[8]); } #[test]