feat/print-pixel-art #4
@@ -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
|
//! The first line is solid; every line below it has fewer pixels, so the
|
||||||
//! makes random bits read as a little creature or emblem instead of noise.
|
//! 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;
|
pub const ROWS: usize = 4;
|
||||||
const HALF: usize = COLUMNS / 2;
|
/// Pixels across the paper that the strip aims for; the printer picks the
|
||||||
/// How many of the 16 cells of the left half may be filled: enough to read as
|
/// pixel size that comes closest (12 dots on 80 mm paper, 8 on 58 mm).
|
||||||
/// a shape, not so many that it turns into a block.
|
pub const CELLS_ACROSS: usize = 48;
|
||||||
const FILLED: std::ops::RangeInclusive<u32> = 6..=11;
|
/// 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 pixels of each line for `seed`; the same seed always gives the same
|
||||||
|
/// strip. Every line has exactly `columns * DENSITY / 100` pixels set, in
|
||||||
/// The sprite for `seed`; the same seed always gives the same sprite.
|
/// random places.
|
||||||
pub fn sprite(seed: u64) -> Sprite {
|
pub fn pattern(seed: u64, columns: usize) -> [Vec<bool>; ROWS] {
|
||||||
let mut state = seed;
|
let mut state = seed;
|
||||||
loop {
|
std::array::from_fn(|row| {
|
||||||
let bits = next(&mut state) & 0xFFFF;
|
let filled = columns * DENSITY[row] / 100;
|
||||||
let row = |index: usize| (bits >> (index * HALF)) & 0xF;
|
// Partial Fisher-Yates shuffle: the first `filled` entries are a
|
||||||
if !FILLED.contains(&bits.count_ones()) || (0..ROWS).any(|index| row(index) == 0) {
|
// uniform random choice of columns.
|
||||||
continue;
|
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);
|
||||||
}
|
}
|
||||||
return std::array::from_fn(|y| {
|
let mut line = vec![false; columns];
|
||||||
std::array::from_fn(|x| row(y) >> x.min(COLUMNS - 1 - x) & 1 == 1)
|
for &column in &order[..filled] {
|
||||||
});
|
line[column] = true;
|
||||||
}
|
}
|
||||||
|
line
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
|
/// SplitMix64: a few lines, well mixed, and good enough for decoration.
|
||||||
@@ -41,25 +47,41 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sprites_are_mirrored_filled_in_every_row_and_not_a_block() {
|
fn the_first_line_is_solid_and_each_line_is_sparser() {
|
||||||
for seed in 0..2000 {
|
for columns in [8, 16, 48, 50] {
|
||||||
let sprite = sprite(seed);
|
for seed in 0..200 {
|
||||||
let filled = sprite.iter().flatten().filter(|cell| **cell).count();
|
let lines = pattern(seed, columns);
|
||||||
assert!((12..=22).contains(&filled), "seed {seed}: {filled} cells");
|
let counts: Vec<usize> = lines
|
||||||
for row in sprite {
|
.iter()
|
||||||
assert!(row.iter().any(|cell| *cell), "seed {seed}: empty row");
|
.map(|line| line.iter().filter(|cell| **cell).count())
|
||||||
|
.collect();
|
||||||
|
assert_eq!(counts[0], columns, "{columns} columns, seed {seed}");
|
||||||
assert!(
|
assert!(
|
||||||
(0..COLUMNS).all(|x| row[x] == row[COLUMNS - 1 - x]),
|
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
||||||
"seed {seed}: not mirrored"
|
"{columns} columns, seed {seed}: {counts:?}"
|
||||||
);
|
);
|
||||||
|
assert!(lines.iter().all(|line| line.len() == columns));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn a_seed_gives_one_sprite_and_seeds_give_many() {
|
fn a_seed_gives_one_pattern_and_seeds_give_many() {
|
||||||
assert_eq!(sprite(7), sprite(7));
|
assert_eq!(pattern(7, 48), pattern(7, 48));
|
||||||
let distinct: std::collections::HashSet<Sprite> = (0..1000).map(sprite).collect();
|
let distinct: std::collections::HashSet<_> =
|
||||||
assert!(distinct.len() > 200, "{} sprites", distinct.len());
|
(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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
//! 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, 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
|
//! the slip. The timestamp is the system
|
||||||
//! clock in local time, so the booth's clock has to be right for it to mean
|
//! 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
|
//! anything. Text goes out in the printer's own code page (PC437), since it
|
||||||
@@ -55,7 +55,7 @@ use opencv::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
artwork::{self, COLUMNS, ROWS},
|
artwork::{self, CELLS_ACROSS, ROWS},
|
||||||
quotes::QUOTES,
|
quotes::QUOTES,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -365,7 +365,7 @@ 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 {
|
||||||
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));
|
job.push(header(head));
|
||||||
raster_bands(&mut job, bitmap, head);
|
raster_bands(&mut job, bitmap, head);
|
||||||
job.push(footer(
|
job.push(footer(
|
||||||
@@ -373,7 +373,7 @@ fn encode(
|
|||||||
stamp,
|
stamp,
|
||||||
"ES[(first_quote + copy) % QUOTES.len()],
|
"ES[(first_quote + copy) % QUOTES.len()],
|
||||||
));
|
));
|
||||||
raster_bands(&mut job, &sprite, head);
|
raster_bands(&mut job, &strip, head);
|
||||||
job.push(finish(head));
|
job.push(finish(head));
|
||||||
}
|
}
|
||||||
job
|
job
|
||||||
@@ -393,17 +393,18 @@ fn raster_bands(job: &mut Vec<Vec<u8>>, bitmap: &Bitmap, head: &Head) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The random sprite centred on a full-width strip, `ROWS` cells high, with
|
/// The random pixel strip across the whole paper, `ROWS` lines of pixels that
|
||||||
/// cells that grow with the paper width (18 dots on 80 mm, 12 on 58 mm).
|
/// get sparser downwards. Pixels are 12 dots on 80 mm paper and 8 on 58 mm.
|
||||||
fn sprite_strip(dots: usize, seed: u64) -> Bitmap {
|
fn pixel_strip(dots: usize, seed: u64) -> Bitmap {
|
||||||
let cell = (dots / 32).max(1);
|
let cell = (dots / CELLS_ACROSS).max(1);
|
||||||
let left = (dots - COLUMNS * cell) / 2;
|
let columns = dots / cell;
|
||||||
|
let left = (dots - columns * cell) / 2;
|
||||||
let stride = dots / 8;
|
let stride = dots / 8;
|
||||||
let sprite = artwork::sprite(seed);
|
let lines = artwork::pattern(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 {
|
||||||
if sprite[y / cell][x / cell] {
|
if lines[y / cell][x / cell] {
|
||||||
let dot = left + x;
|
let dot = left + x;
|
||||||
data[y * stride + dot / 8] |= 0x80 >> (dot % 8);
|
data[y * stride + dot / 8] |= 0x80 >> (dot % 8);
|
||||||
}
|
}
|
||||||
@@ -474,7 +475,7 @@ fn first_quote() -> usize {
|
|||||||
% QUOTES.len()
|
% 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 {
|
fn clock_seed() -> u64 {
|
||||||
std::time::SystemTime::now()
|
std::time::SystemTime::now()
|
||||||
.duration_since(std::time::UNIX_EPOCH)
|
.duration_since(std::time::UNIX_EPOCH)
|
||||||
@@ -494,7 +495,7 @@ fn footer(head: &Head, stamp: &str, quote: &[&str]) -> Vec<u8> {
|
|||||||
for line in quote {
|
for line in quote {
|
||||||
text_line(&mut write, line);
|
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
|
write
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,7 +665,7 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let job = encode(&bitmap, &Head::default(), 2, STAMP, 0, 0);
|
let job = encode(&bitmap, &Head::default(), 2, STAMP, 0, 0);
|
||||||
// Per copy: the caption, a full band, the leftover row, the footer, the
|
// 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);
|
assert_eq!(job.len(), 2 * 6);
|
||||||
for copy in job.chunks(6) {
|
for copy in job.chunks(6) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -686,43 +687,45 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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] {
|
for dots in [384, 576] {
|
||||||
let strip = sprite_strip(dots, 42);
|
let strip = pixel_strip(dots, 42);
|
||||||
let cell = dots / 32;
|
let cell = dots / CELLS_ACROSS;
|
||||||
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| {
|
||||||
strip.data[row * strip.width_bytes + dot / 8] & (0x80 >> (dot % 8)) != 0
|
strip.data[row * strip.width_bytes + dot / 8] & (0x80 >> (dot % 8)) != 0
|
||||||
};
|
};
|
||||||
// Mirrored about the centre of the paper, nothing outside the sprite.
|
// The first line of pixels spans the paper; later ones thin out.
|
||||||
let margin = (dots - COLUMNS * cell) / 2;
|
let counts: Vec<usize> = (0..ROWS)
|
||||||
for row in 0..ROWS * cell {
|
.map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count())
|
||||||
for dot in 0..dots {
|
.collect();
|
||||||
assert_eq!(
|
assert_eq!(counts[0], dots);
|
||||||
lit(row, dot),
|
assert!(
|
||||||
lit(row, dots - 1 - dot),
|
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
||||||
"{dots} {row} {dot}"
|
"{counts:?}"
|
||||||
);
|
);
|
||||||
if !(margin..dots - margin).contains(&dot) {
|
// Pixels are solid squares: every dot row of a line is the same.
|
||||||
assert!(!lit(row, dot), "{dots} {row} {dot}");
|
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 {
|
let bitmap = Bitmap {
|
||||||
width_bytes: 72,
|
width_bytes: 72,
|
||||||
data: vec![0; 72],
|
data: vec![0; 72],
|
||||||
};
|
};
|
||||||
let job = encode(&bitmap, &Head::default(), 1, STAMP, 0, 5);
|
let job = encode(&bitmap, &Head::default(), 1, STAMP, 0, 5);
|
||||||
assert_eq!(job.len(), 6); // caption, photo, footer, 2 sprite bands, cut
|
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, BAND_ROWS as u8, 0]));
|
assert!(job[3].starts_with(&[0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, 48, 0]));
|
||||||
assert!(job[5].ends_with(&[0x1D, b'V', 66, 3]));
|
assert!(job[4].ends_with(&[0x1D, b'V', 66, 3]));
|
||||||
// Copies get different sprites.
|
// Copies get different strips.
|
||||||
let two = encode(&bitmap, &Head::default(), 2, STAMP, 0, 5);
|
let two = encode(&bitmap, &Head::default(), 2, STAMP, 0, 5);
|
||||||
assert_ne!(two[3], two[9]);
|
assert_ne!(two[3], two[8]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user