use super::{MacMemoryBus, Presentation};
#[derive(Default)]
pub struct CompactPresentation {
pub width: u32,
pub height: u32,
pub scale: u32,
pub cells: Vec<u32>,
pub detail: Vec<u32>,
}
impl Presentation {
fn compact_sample(&self, x: usize, y: usize, sx: usize, sy: usize) -> u32 {
let lanes = self.bytes_per_pixel() as usize;
let mut rgb = [0u8; 3];
for lane in 0..lanes {
let bx = x * lanes + lane;
let cell = y * self.width as usize + bx;
let color = if self.text_cells[cell] {
let scale = self.scale as usize;
let offset = ((y * scale + sy) * self.width as usize * scale + bx * scale + sx) * 3;
[
self.pixels[offset],
self.pixels[offset + 1],
self.pixels[offset + 2],
]
} else {
self.palette_at(bx as u32)[self.guest_values[cell] as u8 as usize]
};
for channel in 0..3 {
rgb[channel] = rgb[channel].saturating_add(color[channel]);
}
}
(u32::from(rgb[0]) << 16) | (u32::from(rgb[1]) << 8) | u32::from(rgb[2])
}
}
impl MacMemoryBus {
pub fn compact_presentation(
&self,
guest: &[u32],
overlays: &[u32],
output: &mut CompactPresentation,
) -> bool {
let Some(p) = self.presentation.as_ref() else {
return false;
};
let width = p.logical_width();
let count = width as usize * p.height as usize;
if guest.len() != count
|| overlays.len() != count
|| overlays.iter().any(|pixel| pixel >> 24 != 255)
|| count
.checked_mul((p.scale * p.scale) as usize)
.is_none_or(|n| n >= 0x80000000)
{
return false;
}
output.width = width;
output.height = p.height;
output.scale = p.scale;
output.cells.resize(count, 0);
output.detail.clear();
if p.depth == 8 {
let palette = p.palette.map(|rgb| {
(u32::from(rgb[0]) << 16) | (u32::from(rgb[1]) << 8) | u32::from(rgb[2])
});
let scale = p.scale as usize;
let stride = width as usize * scale * 3;
for (cell, (((destination, &before), &after), (&text, &value))) in output
.cells
.iter_mut()
.zip(guest)
.zip(overlays)
.zip(p.text_cells.iter().zip(&p.guest_values))
.enumerate()
{
if before != after {
*destination = after & 0xffffff;
} else if !text {
*destination = palette[value as u8 as usize];
} else {
let x = cell % width as usize;
let y = cell / width as usize;
*destination = 0x80000000 | output.detail.len() as u32;
for sy in 0..scale {
let start = (y * scale + sy) * stride + x * scale * 3;
for rgb in p.pixels[start..start + scale * 3].chunks_exact(3) {
output.detail.push(
(u32::from(rgb[0]) << 16)
| (u32::from(rgb[1]) << 8)
| u32::from(rgb[2]),
);
}
}
}
}
return true;
}
let lanes = p.bytes_per_pixel() as usize;
for y in 0..p.height as usize {
for x in 0..width as usize {
let logical = y * width as usize + x;
let cell = y * p.width as usize + x * lanes;
if guest[logical] != overlays[logical] {
output.cells[logical] = overlays[logical] & 0xffffff;
} else if p.text_cells[cell..cell + lanes].iter().any(|&v| v) {
output.cells[logical] = 0x80000000 | output.detail.len() as u32;
for sy in 0..p.scale as usize {
for sx in 0..p.scale as usize {
output.detail.push(p.compact_sample(x, y, sx, sy));
}
}
} else {
output.cells[logical] = p.compact_sample(x, y, 0, 0);
}
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::MemoryBus;
#[test]
fn reused_compact_cells_are_overwritten_after_size_and_depth_changes() {
let mut bus = super::super::tests::bus();
let mut compact = CompactPresentation::default();
for (width, height, depth, ink) in [
(8, 8, 8u16, true),
(4, 3, 8, false),
(12, 9, 8, true),
(5, 4, 16, false),
(9, 6, 32, true),
] {
let lanes = u32::from(depth / 8);
bus.enable_outline_presentation(
(0x1000, width * lanes, width as u16, height as u16, depth),
std::array::from_fn(|i| [i as u8; 3]),
2,
);
if ink {
super::super::tests::paint_detail(&mut bus, 0x1000);
}
let guest = vec![0xff123456; (width * height) as usize];
let mut overlay = guest.clone();
*overlay.last_mut().unwrap() = 0xffabcdef;
assert!(bus.compact_presentation(&guest, &overlay, &mut compact));
assert_eq!(compact.cells.len(), guest.len());
if !ink {
assert!(compact.detail.is_empty());
}
let (w, h, expected) = bus.presented_argb(&guest, &overlay).unwrap();
let actual: Vec<_> = (0..h)
.flat_map(|y| (0..w).map(move |x| (x, y)))
.map(|(x, y)| {
let cell = compact.cells[((y / 2) * width + x / 2) as usize];
0xff000000 | if cell >> 31 == 0 {
cell
} else {
compact.detail[(cell & 0x7fffffff) as usize
+ ((y % 2) * 2 + x % 2) as usize]
}
})
.collect();
assert_eq!(actual, expected, "{width}x{height} depth={depth}");
}
}
#[test]
fn compact_transport_matches_full_retained_image_after_updates() {
for depth in [8u16, 16, 32] {
for scale in 2..=4 {
let mut bus = super::super::tests::bus();
let palette = std::array::from_fn(|i| [i as u8, (i * 7) as u8, (255 - i) as u8]);
bus.enable_outline_presentation(
(0x1000, u32::from(depth), 8, 8, depth),
palette,
scale,
);
let guest = vec![0xff123456; 64];
let mut overlays = guest.clone();
let mut compact = CompactPresentation::default();
for step in 0..4 {
match step {
0 => super::super::tests::paint_detail(&mut bus, 0x1000),
1 => overlays[0] = 0xffa71d6b,
2 => overlays[0] = guest[0],
_ => bus.write_byte(0x1000, 27),
}
assert!(bus.compact_presentation(&guest, &overlays, &mut compact));
let (w, h, expected) = bus.presented_argb(&guest, &overlays).unwrap();
let mut actual = Vec::new();
for y in 0..h {
for x in 0..w {
let cell = compact.cells[((y / scale) * 8 + x / scale) as usize];
actual.push(
0xff000000
| if cell >> 31 == 0 {
cell
} else {
compact.detail[(cell & 0x7fffffff) as usize
+ ((y % scale) * scale + x % scale) as usize]
},
);
}
}
assert_eq!(actual, expected, "depth={depth} scale={scale} step={step}");
}
overlays[0] = 0x7f123456;
assert!(!bus.compact_presentation(&guest, &overlays, &mut compact));
}
}
}
}