feat: frame the photo with pixel strips above and below

The solid line of each strip sits next to the photo and the pixels thin
out away from it; the strip at the bottom of the slip is gone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bankosegger
2026-11-18 17:44:46 +01:00
parent 67aa0e64c1
commit 4a3149bab3

View File

@@ -26,9 +26,9 @@
//! 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 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
//! 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
//! prints UTF-8 bytes as separate glyphs; see `code_page`.
//!
@@ -365,15 +365,22 @@ fn encode(
) -> Vec<Vec<u8>> {
let mut job = Vec::new();
for copy in 0..copies {
let strip = pixel_strip(bitmap.width_bytes * 8, seed.wrapping_add(copy as u64));
// Two different strips frame the photo, each solid next to it.
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, bitmap, head);
raster_bands(
&mut job,
&pixel_strip(dots, seed.wrapping_add(1), false),
head,
);
job.push(footer(
head,
stamp,
&QUOTES[(first_quote + copy) % QUOTES.len()],
));
raster_bands(&mut job, &strip, head);
job.push(finish(head));
}
job
@@ -393,9 +400,11 @@ 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. Pixels are 12 dots on 80 mm paper and 8 on 58 mm.
fn pixel_strip(dots: usize, seed: u64) -> Bitmap {
/// 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 {
let cell = (dots / CELLS_ACROSS).max(1);
let columns = dots / cell;
let left = (dots - columns * cell) / 2;
@@ -404,7 +413,12 @@ fn pixel_strip(dots: usize, seed: u64) -> Bitmap {
let mut data = vec![0u8; stride * ROWS * cell];
for y in 0..ROWS * cell {
for x in 0..columns * cell {
if lines[y / cell][x / cell] {
let line = if solid_last {
ROWS - 1 - y / cell
} else {
y / cell
};
if lines[line][x / cell] {
let dot = left + x;
data[y * stride + dot / 8] |= 0x80 >> (dot % 8);
}
@@ -495,7 +509,6 @@ fn footer(head: &Head, stamp: &str, quote: &[&str]) -> Vec<u8> {
for line in quote {
text_line(&mut write, line);
}
write.extend([0x1B, b'd', 1]); // a blank line between the quote and the pixel strip
write
}
@@ -622,7 +635,8 @@ mod tests {
};
let head = Head::from_values(Some("85"), Some("quality"));
let job = encode(&bitmap, &head, 1, STAMP, 0, 0);
for band in &job[1..3] {
// Strip, the two photo bands and the other strip.
for band in &job[1..5] {
assert_eq!(
&band[..12],
[
@@ -664,17 +678,17 @@ mod tests {
data: vec![0xAA; 2 * (BAND_ROWS + 1)],
};
let job = encode(&bitmap, &Head::default(), 2, STAMP, 0, 0);
// Per copy: the caption, a full band, the leftover row, the footer, the
// pixel strip and the cut.
assert_eq!(job.len(), 2 * 6);
for copy in job.chunks(6) {
// Per copy: the caption, a pixel strip, the photo's full band and
// leftover row, a pixel strip, the footer and the cut.
assert_eq!(job.len(), 2 * 7);
for copy in job.chunks(7) {
assert_eq!(
&copy[1][..10],
&copy[2][..10],
[0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, BAND_ROWS as u8, 0]
);
assert_eq!(copy[1].len(), 10 + 2 * BAND_ROWS);
assert_eq!(copy[2].len(), 10 + 2 * BAND_ROWS);
assert_eq!(
copy[2],
copy[3],
[
vec![0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, 1, 0],
vec![0xAA; 2]
@@ -687,45 +701,57 @@ mod tests {
}
#[test]
fn closes_each_slip_with_a_fading_pixel_strip_before_the_cut() {
fn frames_the_photo_with_pixel_strips_that_thin_out_away_from_it() {
for dots in [384, 576] {
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
};
// The first line of pixels spans the paper; later ones thin out.
let counts: Vec<usize> = (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));
for solid_last in [false, true] {
let strip = pixel_strip(dots, 42, solid_last);
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
};
// Dots lit in each line of pixels, from top to bottom.
let mut counts: Vec<usize> = (0..ROWS)
.map(|line| (0..dots).filter(|dot| lit(line * cell, *dot)).count())
.collect();
if solid_last {
counts.reverse();
}
// The line next to the photo spans the paper; the others thin out.
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));
}
}
}
}
}
// After the footer comes the strip (48 rows at 576 dots), then the cut.
// Caption, strip, photo, strip, footer, cut: the strips are 48 rows at
// 576 dots and enclose the photo's single row.
let bitmap = Bitmap {
width_bytes: 72,
data: vec![0; 72],
data: vec![0xFF; 72],
};
let job = encode(&bitmap, &Head::default(), 1, STAMP, 0, 5);
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.
assert_eq!(job.len(), 6);
let raster = |rows: u8| [0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, rows, 0];
assert!(job[1].starts_with(&raster(48)) && job[3].starts_with(&raster(48)));
assert_eq!(&job[2][..10], raster(1));
assert!(job[5].ends_with(&[0x1D, b'V', 66, 3]));
// Nothing is printed after the quote but the feed and the cut.
assert!(job[4].windows(2).all(|pair| pair != [0x1D, b'v']));
// The two strips differ, and so do those of the next copy.
let two = encode(&bitmap, &Head::default(), 2, STAMP, 0, 5);
assert_ne!(two[3], two[8]);
assert_ne!(two[1], two[3]);
assert_ne!(two[1], two[7]);
}
#[test]
@@ -737,12 +763,12 @@ mod tests {
let last = QUOTES.len() - 1;
let job = encode(&bitmap, &Head::default(), 2, STAMP, last, 0);
assert!(contains_line(&job[0], EVENT_TITLE) && contains_line(&job[0], EVENT_PLACE));
assert!(contains_line(&job[2], KEEPSAKE_DE) && contains_line(&job[2], KEEPSAKE_EN));
assert!(contains_line(&job[2], STAMP));
assert!(contains_line(&job[4], KEEPSAKE_DE) && contains_line(&job[4], KEEPSAKE_EN));
assert!(contains_line(&job[4], STAMP));
// Consecutive copies get consecutive quotes, wrapping round the list.
assert!(QUOTES[last].iter().all(|line| contains_line(&job[2], line)));
assert!(QUOTES[0].iter().all(|line| contains_line(&job[7], line)));
assert!(job[2].ends_with(&footer(&Head::default(), STAMP, QUOTES[last])[1..]));
assert!(QUOTES[last].iter().all(|line| contains_line(&job[4], line)));
assert!(QUOTES[0].iter().all(|line| contains_line(&job[10], line)));
assert!(job[4].ends_with(&footer(&Head::default(), STAMP, QUOTES[last])[1..]));
// The real stamp is as wide as the one used here.
let now = Local::now().format(STAMP_FORMAT).to_string();
assert!(now.is_ascii() && now.len() == STAMP.len(), "{now}");