use super::paint::Ink;
#[cfg(test)]
mod tests;
pub(crate) struct PatCopyRows<'s> {
scratch: &'s mut Vec<u8>,
aligned: [u8; 8],
slot: [u8; 8],
built: u8,
row_len: usize,
fg: u32,
bg: u32,
bytes_per_pixel: usize,
}
impl<'s> PatCopyRows<'s> {
pub(crate) fn new(
scratch: &'s mut Vec<u8>,
ink: &Ink,
left: i32,
width: usize,
bytes_per_pixel: usize,
) -> Self {
let pattern = ink.pattern;
let rotate = (left - i32::from(ink.pat_origin.0)).rem_euclid(8) as u32;
let mut aligned = [0u8; 8];
let mut slot = [0u8; 8];
let mut slots = 0u8;
for row in 0..8 {
aligned[row] = pattern[row].rotate_left(rotate);
slot[row] = match (0..row).find(|&earlier| aligned[earlier] == aligned[row]) {
Some(earlier) => slot[earlier],
None => {
slots += 1;
slots - 1
}
};
}
let row_len = width * bytes_per_pixel;
scratch.clear();
scratch.resize(row_len * usize::from(slots), 0);
Self {
scratch,
aligned,
slot,
built: 0,
row_len,
fg: ink.fg,
bg: ink.bg,
bytes_per_pixel,
}
}
#[inline]
pub(crate) fn row(&mut self, row: usize) -> &[u8] {
let slot = usize::from(self.slot[row]);
let start = slot * self.row_len;
let out = &mut self.scratch[start..start + self.row_len];
if self.built & (1 << slot) == 0 {
expand_pat_copy_row(
self.aligned[row],
self.fg,
self.bg,
self.bytes_per_pixel,
out,
);
self.built |= 1 << slot;
}
out
}
}
fn expand_pat_copy_row(bits: u8, fg: u32, bg: u32, bytes_per_pixel: usize, out: &mut [u8]) {
let mut tile = [0u8; 16];
for (pixel, chunk) in tile.chunks_exact_mut(bytes_per_pixel).take(8).enumerate() {
let value = if bits & (0x80 >> pixel) != 0 { fg } else { bg };
chunk.copy_from_slice(&value.to_be_bytes()[4 - bytes_per_pixel..]);
}
let tile = &tile[..8 * bytes_per_pixel];
for chunk in out.chunks_mut(tile.len()) {
chunk.copy_from_slice(&tile[..chunk.len()]);
}
}