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:
@@ -26,9 +26,9 @@
|
|||||||
//! 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 strip of random pixel art (see `artwork`) closes
|
//! English only. A strip of random pixel art (see `artwork`) frames the photo
|
||||||
//! the slip. The timestamp is the system
|
//! above and below, solid next to it and thinning out away from it. The
|
||||||
//! clock in local time, so the booth's clock has to be right for it to mean
|
//! 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
|
//! 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`.
|
||||||
//!
|
//!
|
||||||
@@ -365,15 +365,22 @@ 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 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));
|
job.push(header(head));
|
||||||
|
raster_bands(&mut job, &pixel_strip(dots, seed, true), head);
|
||||||
raster_bands(&mut job, bitmap, head);
|
raster_bands(&mut job, bitmap, head);
|
||||||
|
raster_bands(
|
||||||
|
&mut job,
|
||||||
|
&pixel_strip(dots, seed.wrapping_add(1), false),
|
||||||
|
head,
|
||||||
|
);
|
||||||
job.push(footer(
|
job.push(footer(
|
||||||
head,
|
head,
|
||||||
stamp,
|
stamp,
|
||||||
"ES[(first_quote + copy) % QUOTES.len()],
|
"ES[(first_quote + copy) % QUOTES.len()],
|
||||||
));
|
));
|
||||||
raster_bands(&mut job, &strip, head);
|
|
||||||
job.push(finish(head));
|
job.push(finish(head));
|
||||||
}
|
}
|
||||||
job
|
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
|
/// 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.
|
/// get sparser downwards, or upwards when `solid_last`, so that the solid line
|
||||||
fn pixel_strip(dots: usize, seed: u64) -> Bitmap {
|
/// 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 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;
|
||||||
@@ -404,7 +413,12 @@ fn pixel_strip(dots: usize, seed: u64) -> Bitmap {
|
|||||||
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 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;
|
let dot = left + x;
|
||||||
data[y * stride + dot / 8] |= 0x80 >> (dot % 8);
|
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 {
|
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 pixel strip
|
|
||||||
write
|
write
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -622,7 +635,8 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let head = Head::from_values(Some("85"), Some("quality"));
|
let head = Head::from_values(Some("85"), Some("quality"));
|
||||||
let job = encode(&bitmap, &head, 1, STAMP, 0, 0);
|
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!(
|
assert_eq!(
|
||||||
&band[..12],
|
&band[..12],
|
||||||
[
|
[
|
||||||
@@ -664,17 +678,17 @@ mod tests {
|
|||||||
data: vec![0xAA; 2 * (BAND_ROWS + 1)],
|
data: vec![0xAA; 2 * (BAND_ROWS + 1)],
|
||||||
};
|
};
|
||||||
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 pixel strip, the photo's full band and
|
||||||
// pixel strip and the cut.
|
// leftover row, a pixel strip, the footer and the cut.
|
||||||
assert_eq!(job.len(), 2 * 6);
|
assert_eq!(job.len(), 2 * 7);
|
||||||
for copy in job.chunks(6) {
|
for copy in job.chunks(7) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
©[1][..10],
|
©[2][..10],
|
||||||
[0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, BAND_ROWS as u8, 0]
|
[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!(
|
assert_eq!(
|
||||||
copy[2],
|
copy[3],
|
||||||
[
|
[
|
||||||
vec![0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, 1, 0],
|
vec![0x1B, b'@', 0x1D, b'v', b'0', 0, 2, 0, 1, 0],
|
||||||
vec![0xAA; 2]
|
vec![0xAA; 2]
|
||||||
@@ -687,19 +701,24 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[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] {
|
for dots in [384, 576] {
|
||||||
let strip = pixel_strip(dots, 42);
|
|
||||||
let cell = dots / CELLS_ACROSS;
|
let cell = dots / CELLS_ACROSS;
|
||||||
|
for solid_last in [false, true] {
|
||||||
|
let strip = pixel_strip(dots, 42, solid_last);
|
||||||
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
|
||||||
};
|
};
|
||||||
// The first line of pixels spans the paper; later ones thin out.
|
// Dots lit in each line of pixels, from top to bottom.
|
||||||
let 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 {
|
||||||
|
counts.reverse();
|
||||||
|
}
|
||||||
|
// The line next to the photo spans the paper; the others thin out.
|
||||||
assert_eq!(counts[0], dots);
|
assert_eq!(counts[0], dots);
|
||||||
assert!(
|
assert!(
|
||||||
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
counts.windows(2).all(|pair| pair[0] > pair[1]),
|
||||||
@@ -714,18 +733,25 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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 {
|
let bitmap = Bitmap {
|
||||||
width_bytes: 72,
|
width_bytes: 72,
|
||||||
data: vec![0; 72],
|
data: vec![0xFF; 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(), 5); // caption, photo, footer, strip, cut
|
assert_eq!(job.len(), 6);
|
||||||
assert!(job[3].starts_with(&[0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, 48, 0]));
|
let raster = |rows: u8| [0x1B, b'@', 0x1D, b'v', b'0', 0, 72, 0, rows, 0];
|
||||||
assert!(job[4].ends_with(&[0x1D, b'V', 66, 3]));
|
assert!(job[1].starts_with(&raster(48)) && job[3].starts_with(&raster(48)));
|
||||||
// Copies get different strips.
|
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);
|
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]
|
#[test]
|
||||||
@@ -737,12 +763,12 @@ mod tests {
|
|||||||
let last = QUOTES.len() - 1;
|
let last = QUOTES.len() - 1;
|
||||||
let job = encode(&bitmap, &Head::default(), 2, STAMP, last, 0);
|
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[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[4], KEEPSAKE_DE) && contains_line(&job[4], KEEPSAKE_EN));
|
||||||
assert!(contains_line(&job[2], STAMP));
|
assert!(contains_line(&job[4], STAMP));
|
||||||
// Consecutive copies get consecutive quotes, wrapping round the list.
|
// Consecutive copies get consecutive quotes, wrapping round the list.
|
||||||
assert!(QUOTES[last].iter().all(|line| contains_line(&job[2], line)));
|
assert!(QUOTES[last].iter().all(|line| contains_line(&job[4], line)));
|
||||||
assert!(QUOTES[0].iter().all(|line| contains_line(&job[7], line)));
|
assert!(QUOTES[0].iter().all(|line| contains_line(&job[10], line)));
|
||||||
assert!(job[2].ends_with(&footer(&Head::default(), STAMP, QUOTES[last])[1..]));
|
assert!(job[4].ends_with(&footer(&Head::default(), STAMP, QUOTES[last])[1..]));
|
||||||
// The real stamp is as wide as the one used here.
|
// The real stamp is as wide as the one used here.
|
||||||
let now = Local::now().format(STAMP_FORMAT).to_string();
|
let now = Local::now().format(STAMP_FORMAT).to_string();
|
||||||
assert!(now.is_ascii() && now.len() == STAMP.len(), "{now}");
|
assert!(now.is_ascii() && now.len() == STAMP.len(), "{now}");
|
||||||
|
|||||||
Reference in New Issue
Block a user