use crate::memory::{MacMemoryBus, MemoryBus};
const BLACK_ARGB: u32 = 0xFF000000;
const WHITE_ARGB: u32 = 0xFFFFFFFF;
const COMPACT_MAC_VIEWPORT_WIDTH: usize = 512;
const COMPACT_MAC_VIEWPORT_HEIGHT: usize = 342;
const COMPACT_MAC_VIEWPORT_SIDE_PAD: usize = 1;
const LETTERBOX_SAMPLE_STRIDE: usize = 8;
const LETTERBOX_FLOOD_TOLERANCE: u8 = 4;
const LETTERBOX_MIN_FLOOD_RATIO_NUM: usize = 3;
const LETTERBOX_MIN_FLOOD_RATIO_DEN: usize = 4;
const LETTERBOX_EDGE_TRIM_LIMIT: usize = 32;
pub type RgbaPalette = [u32; 256];
pub type DisplayGamma = [[u8; 256]; 3];
pub(crate) fn standard_mac_8bpp_clut() -> [[u16; 3]; 256] {
let mut clut = [[0u16; 3]; 256];
for i in 0u32..=214 {
let r = 5 - (i / 36);
let g = 5 - ((i / 6) % 6);
let b = 5 - (i % 6);
clut[i as usize] = [
(r as u16) * 0x3333,
(g as u16) * 0x3333,
(b as u16) * 0x3333,
];
}
const RAMP: [u16; 10] = [
0xEEEE, 0xDDDD, 0xBBBB, 0xAAAA, 0x8888, 0x7777, 0x5555, 0x4444, 0x2222, 0x1111,
];
for (offset, component) in RAMP.into_iter().enumerate() {
clut[215 + offset] = [component, 0, 0];
clut[225 + offset] = [0, component, 0];
clut[235 + offset] = [0, 0, component];
clut[245 + offset] = [component, component, component];
}
clut[255] = [0, 0, 0];
clut
}
pub(crate) const fn classic_depth_mode(pixel_size: u16) -> Option<u16> {
match pixel_size {
1 => Some(0x0080),
2 => Some(0x0081),
4 => Some(0x0082),
8 => Some(0x0083),
16 => Some(0x0084),
32 => Some(0x0085),
_ => None,
}
}
pub(crate) const fn classic_pixel_size(depth_or_mode: u16) -> Option<u16> {
match depth_or_mode {
1 | 2 | 4 | 8 | 16 | 32 => Some(depth_or_mode),
0x0080 => Some(1),
0x0081 => Some(2),
0x0082 => Some(4),
0x0083 => Some(8),
0x0084 => Some(16),
0x0085 => Some(32),
_ => None,
}
}
pub fn default_display_gamma() -> DisplayGamma {
[MAC_ROM_GAMMA_LUT; 3]
}
pub fn linear_display_gamma() -> DisplayGamma {
let identity = std::array::from_fn(|index| index as u8);
[identity; 3]
}
const UNUSED_RGBA_PALETTE: RgbaPalette = [0; 256];
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CursorImage {
Mono {
data: [u8; 32],
mask: [u8; 32],
hot_v: i16,
hot_h: i16,
},
Color {
width: u16,
height: u16,
pixels_argb: Vec<u32>,
mask: [u8; 32],
hot_v: i16,
hot_h: i16,
mono_data: [u8; 32],
mono_mask: [u8; 32],
},
}
impl CursorImage {
pub fn mono(data: [u8; 32], mask: [u8; 32], hot_v: i16, hot_h: i16) -> Self {
Self::Mono {
data,
mask,
hot_v,
hot_h,
}
}
pub fn mono_parts(&self) -> ([u8; 32], [u8; 32], i16, i16) {
match self {
Self::Mono {
data,
mask,
hot_v,
hot_h,
} => (*data, *mask, *hot_v, *hot_h),
Self::Color {
mono_data,
mono_mask,
hot_v,
hot_h,
..
} => (*mono_data, *mono_mask, *hot_v, *hot_h),
}
}
}
pub(crate) fn default_arrow_cursor() -> ([u8; 32], [u8; 32], i16, i16) {
#[rustfmt::skip]
let data = [
0x00, 0x00, 0x40, 0x00, 0x60, 0x00, 0x70, 0x00,
0x78, 0x00, 0x7C, 0x00, 0x7E, 0x00, 0x7F, 0x00,
0x7F, 0x80, 0x7C, 0x00, 0x6C, 0x00, 0x46, 0x00,
0x06, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
];
#[rustfmt::skip]
let mask = [
0xC0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF8, 0x00,
0xFC, 0x00, 0xFE, 0x00, 0xFF, 0x00, 0xFF, 0x80,
0xFF, 0xC0, 0xFF, 0xE0, 0xFE, 0x00, 0xEF, 0x00,
0xCF, 0x00, 0x07, 0x80, 0x07, 0x80, 0x03, 0x80,
];
(data, mask, 1, 1)
}
pub(crate) fn default_arrow_cursor_image() -> CursorImage {
let (data, mask, hot_v, hot_h) = default_arrow_cursor();
CursorImage::mono(data, mask, hot_v, hot_h)
}
pub fn outline_output_scale(logical: (u32, u32), drawable: (u32, u32)) -> u32 {
let (w, h) = logical;
let (dw, dh) = drawable;
if w == 0 || h == 0 {
return 1;
}
let scale = if u64::from(dw) * u64::from(h) <= u64::from(dh) * u64::from(w) {
dw.div_ceil(w)
} else {
dh.div_ceil(h)
};
scale.clamp(1, 4)
}
#[inline]
pub(crate) fn coverage_quotient(numerator: u64, divisor: u64, reciprocal: u64) -> u64 {
let estimate = ((u128::from(numerator) * u128::from(reciprocal)) >> 64) as u64;
estimate + u64::from(numerator - estimate * divisor >= divisor)
}
pub fn resize_argb_coverage(
source: &[u32],
source_size: (u32, u32),
size: (u32, u32),
output: &mut Vec<u32>,
) {
let (sw, sh) = source_size;
let (dw, dh) = size;
assert!(sw > 0 && sh > 0 && dw > 0 && dh > 0);
assert_eq!(source.len(), (sw as usize) * sh as usize);
output.resize(dw as usize * dh as usize, 0);
if source_size == size {
output.copy_from_slice(source);
return;
}
fn axis_coverage(source: u32, destination: u32) -> Vec<Vec<(usize, u64)>> {
(0..destination)
.map(|position| {
if source <= destination {
let cell = ((u64::from(position) * 2 + 1) * u64::from(source)
/ (u64::from(destination) * 2))
.min(u64::from(source - 1));
vec![(cell as usize, 1)]
} else {
let left = u64::from(position) * u64::from(source);
let right = u64::from(position + 1) * u64::from(source);
let unit = u64::from(destination);
(left / unit..right.div_ceil(unit))
.map(|cell| {
let weight = right.min((cell + 1) * unit) - left.max(cell * unit);
(cell as usize, weight)
})
.collect()
}
})
.collect()
}
let horizontal = axis_coverage(sw, dw);
let vertical = axis_coverage(sh, dh);
let total = u64::from(if sw > dw { sw } else { 1 }) * u64::from(if sh > dh { sh } else { 1 });
let reciprocal = u64::MAX / total;
for (y, rows) in vertical.iter().enumerate() {
for (x, columns) in horizontal.iter().enumerate() {
let mut sum = [0u64; 4];
for &(sy, wy) in rows {
for &(sx, wx) in columns {
let weight = wy * wx;
let pixel = source[sy * sw as usize + sx];
for (channel, sum) in sum.iter_mut().enumerate() {
*sum += u64::from((pixel >> (channel * 8)) & 255) * weight;
}
}
}
output[y * dw as usize + x] =
sum.iter().enumerate().fold(0, |pixel, (channel, sum)| {
let value = coverage_quotient(sum + total / 2, total, reciprocal);
pixel | ((value as u32) << (channel * 8))
});
}
}
}
pub fn render_screen(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
) -> Vec<u8> {
render_screen_with_gamma(bus, screen_mode, device_clut, &default_display_gamma())
}
pub fn render_screen_with_gamma(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
) -> Vec<u8> {
let (_, _, scrn_w, scrn_h, _) = screen_mode;
let mut pixels = Vec::with_capacity(scrn_w as usize * scrn_h as usize * 4);
render_screen_into_with_gamma(bus, screen_mode, device_clut, device_gamma, &mut pixels);
pixels
}
pub fn render_screen_into(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
pixels: &mut Vec<u8>,
) {
render_screen_into_with_gamma(
bus,
screen_mode,
device_clut,
&default_display_gamma(),
pixels,
);
}
pub fn render_screen_into_with_gamma(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
pixels: &mut Vec<u8>,
) {
if matches!(screen_mode.4, 1 | 2 | 4 | 8) {
let palette = rgba_palette_from_clut_with_gamma(device_clut, device_gamma);
render_screen_with_rgba_palette_into(bus, screen_mode, &palette, pixels);
} else {
render_screen_with_rgba_palette_into(bus, screen_mode, &UNUSED_RGBA_PALETTE, pixels);
}
}
pub fn rgba_palette_from_clut(device_clut: &[[u16; 3]; 256]) -> RgbaPalette {
rgba_palette_from_clut_with_gamma(device_clut, &default_display_gamma())
}
pub fn rgba_palette_from_clut_with_gamma(
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
) -> RgbaPalette {
let mut palette = [0u32; 256];
for (index, slot) in palette.iter_mut().enumerate() {
let [r, g, b] = clut_to_rgba8_with_gamma(device_clut, device_gamma, index as u8);
*slot = rgba_word(r, g, b);
}
palette
}
pub fn render_screen_with_rgba_palette_into(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
palette: &RgbaPalette,
pixels: &mut Vec<u8>,
) {
let (scrn_base, row_bytes, scrn_w, scrn_h, pixel_size) = screen_mode;
let w = scrn_w as u32;
let h = scrn_h as u32;
pixels.resize((w * h * 4) as usize, 0);
let Some(framebuffer_len) = validated_screen_framebuffer_len(bus, screen_mode) else {
fill_black_rgba(pixels);
return;
};
if w == 0 || h == 0 {
return;
}
let fb = bus.ram_slice(scrn_base, framebuffer_len);
match pixel_size {
32 => render_32bpp_rgba_rows(fb, row_bytes, w, h, pixels),
16 => render_16bpp_rgba_rows(fb, row_bytes, w, h, pixels),
8 => render_8bpp_rgba_rows(fb, row_bytes, w, h, palette, pixels),
4 => render_4bpp_rgba_rows(fb, row_bytes, w, h, palette, pixels),
2 => render_2bpp_rgba_rows(fb, row_bytes, w, h, palette, pixels),
_ => render_1bpp_rgba_rows(fb, row_bytes, w, h, palette, pixels),
}
normalize_centered_compact_mac_viewport_margins_rgba(pixels, w as usize, h as usize);
}
fn validated_screen_framebuffer_len(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
) -> Option<u32> {
let (base, row_bytes, width, height, pixel_size) = screen_mode;
if width == 0 || height == 0 {
return Some(0);
}
let length = packed_screen_framebuffer_len(row_bytes, width, height, pixel_size)?;
let end = u64::from(base).checked_add(u64::from(length))?;
(end <= u64::from(bus.ram_size())).then_some(length)
}
fn packed_screen_framebuffer_len(
row_bytes: u32,
width: u16,
height: u16,
pixel_size: u16,
) -> Option<u32> {
if width == 0 || height == 0 {
return Some(0);
}
let bits_per_row = u32::from(width).checked_mul(u32::from(pixel_size))?;
let minimum_row_bytes = match pixel_size {
1 | 2 | 4 | 8 | 16 | 32 => bits_per_row.checked_add(7)? / 8,
_ => return None,
};
if row_bytes < minimum_row_bytes {
return None;
}
row_bytes.checked_mul(u32::from(height))
}
fn fill_black_rgba(pixels: &mut [u8]) {
for pixel in pixels.chunks_exact_mut(4) {
pixel.copy_from_slice(&[0, 0, 0, 0xFF]);
}
}
fn normalize_centered_compact_mac_viewport_margins_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
) -> bool {
if width <= COMPACT_MAC_VIEWPORT_WIDTH + COMPACT_MAC_VIEWPORT_SIDE_PAD * 2
|| height <= COMPACT_MAC_VIEWPORT_HEIGHT
|| pixels.len() < width.saturating_mul(height).saturating_mul(4)
{
return false;
}
let content_left =
((width - COMPACT_MAC_VIEWPORT_WIDTH) / 2).saturating_sub(COMPACT_MAC_VIEWPORT_SIDE_PAD);
let content_top = (height - COMPACT_MAC_VIEWPORT_HEIGHT) / 2;
let content_right =
(content_left + COMPACT_MAC_VIEWPORT_WIDTH + COMPACT_MAC_VIEWPORT_SIDE_PAD * 2).min(width);
let content_bottom = (content_top + COMPACT_MAC_VIEWPORT_HEIGHT).min(height);
if content_left == 0 || content_top == 0 || content_right >= width || content_bottom >= height {
return false;
}
let Some(flood) = first_nonblack_outside_sample(
pixels,
width,
height,
content_left,
content_top,
content_right,
content_bottom,
) else {
return false;
};
let mut outside_samples = 0usize;
let mut outside_flood_samples = 0usize;
let mut inside_samples = 0usize;
let mut inside_non_flood_samples = 0usize;
for y in (0..height).step_by(LETTERBOX_SAMPLE_STRIDE) {
for x in (0..width).step_by(LETTERBOX_SAMPLE_STRIDE) {
let rgb = rgba_pixel_rgb(pixels, width, x, y);
let inside =
x >= content_left && x < content_right && y >= content_top && y < content_bottom;
if inside {
inside_samples += 1;
if !rgb_near(rgb, flood, LETTERBOX_FLOOD_TOLERANCE) {
inside_non_flood_samples += 1;
}
} else {
outside_samples += 1;
if rgb_near(rgb, flood, LETTERBOX_FLOOD_TOLERANCE) {
outside_flood_samples += 1;
}
}
}
}
if outside_samples == 0
|| outside_flood_samples * LETTERBOX_MIN_FLOOD_RATIO_DEN
< outside_samples * LETTERBOX_MIN_FLOOD_RATIO_NUM
{
return false;
}
if inside_samples == 0 || inside_non_flood_samples < inside_samples / 4 {
return false;
}
let (content_left, content_top, content_right, content_bottom) = expand_content_edges_rgba(
pixels,
width,
height,
flood,
content_left,
content_top,
content_right,
content_bottom,
);
let (content_left, content_top, content_right, content_bottom) = trim_flood_edges_rgba(
pixels,
width,
flood,
content_left,
content_top,
content_right,
content_bottom,
);
black_outside_rect_rgba(
pixels,
width,
height,
content_left,
content_top,
content_right,
content_bottom,
);
true
}
#[allow(clippy::too_many_arguments)]
fn expand_content_edges_rgba(
pixels: &[u8],
width: usize,
height: usize,
flood: [u8; 3],
mut left: usize,
mut top: usize,
mut right: usize,
mut bottom: usize,
) -> (usize, usize, usize, usize) {
let min_top = top.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT);
while top > min_top
&& top > 0
&& !row_or_column_is_mostly_flood(pixels, width, flood, left, top - 1, right, top)
{
top -= 1;
}
let max_bottom = bottom.saturating_add(LETTERBOX_EDGE_TRIM_LIMIT).min(height);
while bottom < max_bottom
&& !row_or_column_is_mostly_flood(pixels, width, flood, left, bottom, right, bottom + 1)
{
bottom += 1;
}
let min_left = left.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT);
while left > min_left
&& left > 0
&& !row_or_column_is_mostly_flood(pixels, width, flood, left - 1, top, left, bottom)
{
left -= 1;
}
let max_right = right.saturating_add(LETTERBOX_EDGE_TRIM_LIMIT).min(width);
while right < max_right
&& !row_or_column_is_mostly_flood(pixels, width, flood, right, top, right + 1, bottom)
{
right += 1;
}
(left, top, right, bottom)
}
fn trim_flood_edges_rgba(
pixels: &[u8],
width: usize,
flood: [u8; 3],
mut left: usize,
mut top: usize,
mut right: usize,
mut bottom: usize,
) -> (usize, usize, usize, usize) {
let min_width = COMPACT_MAC_VIEWPORT_WIDTH.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT);
let min_height = COMPACT_MAC_VIEWPORT_HEIGHT.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT);
let max_top = top.saturating_add(LETTERBOX_EDGE_TRIM_LIMIT).min(bottom);
while top < max_top
&& bottom.saturating_sub(top) > min_height
&& row_or_column_is_mostly_flood(pixels, width, flood, left, top, right, top + 1)
{
top += 1;
}
let min_bottom = bottom.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT).max(top);
while bottom > min_bottom
&& bottom.saturating_sub(top) > min_height
&& row_or_column_is_mostly_flood(pixels, width, flood, left, bottom - 1, right, bottom)
{
bottom -= 1;
}
let max_left = left.saturating_add(LETTERBOX_EDGE_TRIM_LIMIT).min(right);
while left < max_left
&& right.saturating_sub(left) > min_width
&& row_or_column_is_mostly_flood(pixels, width, flood, left, top, left + 1, bottom)
{
left += 1;
}
let min_right = right.saturating_sub(LETTERBOX_EDGE_TRIM_LIMIT).max(left);
while right > min_right
&& right.saturating_sub(left) > min_width
&& row_or_column_is_mostly_flood(pixels, width, flood, right - 1, top, right, bottom)
{
right -= 1;
}
(left, top, right, bottom)
}
fn row_or_column_is_mostly_flood(
pixels: &[u8],
width: usize,
flood: [u8; 3],
left: usize,
top: usize,
right: usize,
bottom: usize,
) -> bool {
let mut total = 0usize;
let mut matches = 0usize;
for y in (top..bottom).step_by(LETTERBOX_SAMPLE_STRIDE) {
for x in (left..right).step_by(LETTERBOX_SAMPLE_STRIDE) {
total += 1;
if rgb_near(
rgba_pixel_rgb(pixels, width, x, y),
flood,
LETTERBOX_FLOOD_TOLERANCE,
) {
matches += 1;
}
}
}
total > 0 && matches * 8 >= total * 7
}
fn first_nonblack_outside_sample(
pixels: &[u8],
width: usize,
height: usize,
content_left: usize,
content_top: usize,
content_right: usize,
content_bottom: usize,
) -> Option<[u8; 3]> {
let probes = [
(0, 0),
(width / 2, 0),
(0, height / 2),
(width.saturating_sub(1), height / 2),
(width / 2, height.saturating_sub(1)),
];
for (x, y) in probes {
let inside =
x >= content_left && x < content_right && y >= content_top && y < content_bottom;
if inside {
continue;
}
let rgb = rgba_pixel_rgb(pixels, width, x, y);
if !rgb_is_near_black(rgb) {
return Some(rgb);
}
}
None
}
#[inline]
fn rgba_pixel_rgb(pixels: &[u8], width: usize, x: usize, y: usize) -> [u8; 3] {
let idx = (y * width + x) * 4;
[pixels[idx], pixels[idx + 1], pixels[idx + 2]]
}
#[inline]
fn rgb_is_near_black(rgb: [u8; 3]) -> bool {
rgb[0] <= 8 && rgb[1] <= 8 && rgb[2] <= 8
}
#[inline]
fn rgb_near(a: [u8; 3], b: [u8; 3], tolerance: u8) -> bool {
a[0].abs_diff(b[0]) <= tolerance
&& a[1].abs_diff(b[1]) <= tolerance
&& a[2].abs_diff(b[2]) <= tolerance
}
fn black_outside_rect_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
left: usize,
top: usize,
right: usize,
bottom: usize,
) {
for y in 0..height {
let row = y * width * 4;
if y < top || y >= bottom {
for px in pixels[row..row + width * 4].chunks_exact_mut(4) {
px.copy_from_slice(&[0, 0, 0, 0xFF]);
}
continue;
}
for px in pixels[row..row + left * 4].chunks_exact_mut(4) {
px.copy_from_slice(&[0, 0, 0, 0xFF]);
}
for px in pixels[row + right * 4..row + width * 4].chunks_exact_mut(4) {
px.copy_from_slice(&[0, 0, 0, 0xFF]);
}
}
}
#[inline]
fn render_8bpp_rgba_rows(
fb: &[u8],
row_bytes: u32,
w: u32,
h: u32,
palette: &RgbaPalette,
pixels: &mut [u8],
) {
let dst = pixels.as_mut_ptr().cast::<u32>();
let w = w as usize;
let row_bytes = row_bytes as usize;
for gy in 0..h as usize {
let src_row = &fb[gy * row_bytes..];
let dst_row = unsafe { dst.add(gy * w) };
for gx in 0..w {
let rgba = palette[src_row[gx] as usize];
unsafe {
std::ptr::write_unaligned(dst_row.add(gx), rgba);
}
}
}
}
fn render_32bpp_rgba_rows(fb: &[u8], row_bytes: u32, w: u32, h: u32, pixels: &mut [u8]) {
for y in 0..h as usize {
for x in 0..w as usize {
let src = y * row_bytes as usize + x * 4;
let dst = (y * w as usize + x) * 4;
pixels[dst..dst + 4].copy_from_slice(&[fb[src + 1], fb[src + 2], fb[src + 3], 0xff]);
}
}
}
fn render_16bpp_rgba_rows(fb: &[u8], row_bytes: u32, w: u32, h: u32, pixels: &mut [u8]) {
for gy in 0..h as usize {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w as usize * 4..(gy + 1) * w as usize * 4];
for gx in 0..w as usize {
let src = row_start + gx * 2;
let [r, g, b] = rgb555_to_rgb888(u16::from_be_bytes([fb[src], fb[src + 1]]));
let dst = gx * 4;
dst_row[dst..dst + 4].copy_from_slice(&[r, g, b, 0xff]);
}
}
}
#[inline]
fn render_4bpp_rgba_rows(
fb: &[u8],
row_bytes: u32,
w: u32,
h: u32,
palette: &RgbaPalette,
pixels: &mut [u8],
) {
let dst = pixels.as_mut_ptr().cast::<u32>();
let w = w as usize;
let row_bytes = row_bytes as usize;
for gy in 0..h as usize {
let src_row = &fb[gy * row_bytes..];
let dst_row = unsafe { dst.add(gy * w) };
for gx in 0..w {
let packed = src_row[gx / 2];
let index = if gx & 1 == 0 {
packed >> 4
} else {
packed & 0x0F
};
unsafe {
std::ptr::write_unaligned(dst_row.add(gx), palette[index as usize]);
}
}
}
}
#[inline]
fn render_2bpp_rgba_rows(
fb: &[u8],
row_bytes: u32,
w: u32,
h: u32,
palette: &RgbaPalette,
pixels: &mut [u8],
) {
let dst = pixels.as_mut_ptr().cast::<u32>();
let w = w as usize;
let row_bytes = row_bytes as usize;
for gy in 0..h as usize {
let src_row = &fb[gy * row_bytes..];
let dst_row = unsafe { dst.add(gy * w) };
for gx in 0..w {
let packed = src_row[gx / 4];
let shift = 6 - 2 * (gx & 3);
let index = (packed >> shift) & 0x03;
unsafe {
std::ptr::write_unaligned(dst_row.add(gx), palette[index as usize]);
}
}
}
}
#[inline]
fn render_1bpp_rgba_rows(
fb: &[u8],
row_bytes: u32,
w: u32,
h: u32,
palette: &RgbaPalette,
pixels: &mut [u8],
) {
let dst = pixels.as_mut_ptr().cast::<u32>();
let w = w as usize;
let row_bytes = row_bytes as usize;
for gy in 0..h as usize {
let src_row = &fb[gy * row_bytes..];
let dst_row = unsafe { dst.add(gy * w) };
for gx in 0..w {
let byte = src_row[gx / 8];
let bit = 7 - (gx & 7);
let index = (byte >> bit) & 1;
unsafe {
std::ptr::write_unaligned(dst_row.add(gx), palette[index as usize]);
}
}
}
}
pub fn screen_pixel_rgb(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
x: u32,
y: u32,
) -> Option<[u8; 3]> {
screen_pixel_rgb_with_gamma(
bus,
screen_mode,
device_clut,
&default_display_gamma(),
x,
y,
)
}
pub fn screen_pixel_rgb_with_gamma(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
x: u32,
y: u32,
) -> Option<[u8; 3]> {
let (scrn_base, row_bytes, scrn_w, scrn_h, pixel_size) = screen_mode;
let w = scrn_w as u32;
let h = scrn_h as u32;
if x >= w || y >= h || row_bytes == 0 {
return None;
}
validated_screen_framebuffer_len(bus, screen_mode)?;
match pixel_size {
32 => {
let addr = scrn_base + y * row_bytes + x * 4;
Some([bus.read_byte(addr + 1), bus.read_byte(addr + 2), bus.read_byte(addr + 3)])
}
16 => {
let addr = scrn_base + y * row_bytes + x * 2;
Some(rgb555_to_rgb888(bus.read_word(addr)))
}
8 => {
let addr = scrn_base + y * row_bytes + x;
let pixel = bus.read_byte(addr);
Some(clut_to_rgba8_with_gamma(device_clut, device_gamma, pixel))
}
4 => {
let addr = scrn_base + y * row_bytes + x / 2;
let packed = bus.read_byte(addr);
let pixel = if x & 1 == 0 {
packed >> 4
} else {
packed & 0x0F
};
Some(clut_to_rgba8_with_gamma(device_clut, device_gamma, pixel))
}
2 => {
let addr = scrn_base + y * row_bytes + x / 4;
let packed = bus.read_byte(addr);
let shift = 6 - 2 * (x & 3);
let pixel = (packed >> shift) & 0x03;
Some(clut_to_rgba8_with_gamma(device_clut, device_gamma, pixel))
}
1 => {
let addr = scrn_base + y * row_bytes + x / 8;
let byte = bus.read_byte(addr);
let bit = 7 - (x % 8);
let pixel = (byte >> bit) & 1;
Some(clut_to_rgba8_with_gamma(device_clut, device_gamma, pixel))
}
_ => None,
}
}
#[derive(Clone)]
pub struct PackedScreenFrame {
pub screen_mode: (u32, u32, u16, u16, u16),
pub pixels: Vec<u8>,
pub palette: [u32; 256],
}
impl Default for PackedScreenFrame {
fn default() -> Self {
Self {
screen_mode: (0, 0, 0, 0, 0),
pixels: Vec::new(),
palette: [BLACK_ARGB; 256],
}
}
}
impl PackedScreenFrame {
pub fn capture(
&mut self,
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
) {
self.screen_mode = screen_mode;
self.palette = argb_palette_from_clut_with_gamma(device_clut, device_gamma);
self.pixels.clear();
if let Some(length) =
validated_screen_framebuffer_len(bus, screen_mode).filter(|&length| length != 0)
{
self.pixels
.extend_from_slice(bus.ram_slice(screen_mode.0, length));
}
}
pub fn render_argb(&self, output: &mut Vec<u32>) {
let (_, stride, width, height, depth) = self.screen_mode;
render_packed_screen_argb(
&self.pixels,
(stride, width, height, depth),
&self.palette,
output,
);
}
}
pub fn render_screen_argb(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
pixels: &mut Vec<u32>,
) {
render_screen_argb_with_gamma(
bus,
screen_mode,
device_clut,
&default_display_gamma(),
pixels,
);
}
pub fn render_screen_argb_with_gamma(
bus: &MacMemoryBus,
screen_mode: (u32, u32, u16, u16, u16),
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
pixels: &mut Vec<u32>,
) {
let (base, row_bytes, width, height, depth) = screen_mode;
let framebuffer = validated_screen_framebuffer_len(bus, screen_mode)
.filter(|&length| length != 0)
.map(|length| bus.ram_slice(base, length))
.unwrap_or(&[]);
let palette = argb_palette_from_clut_with_gamma(device_clut, device_gamma);
render_packed_screen_argb(
framebuffer,
(row_bytes, width, height, depth),
&palette,
pixels,
);
}
pub fn render_packed_screen_argb(
fb: &[u8],
layout: (u32, u16, u16, u16),
palette: &[u32; 256],
pixels: &mut Vec<u32>,
) {
let (row_bytes, scrn_w, scrn_h, pixel_size) = layout;
let w = usize::from(scrn_w);
let h = usize::from(scrn_h);
pixels.resize(w.saturating_mul(h), BLACK_ARGB);
pixels.fill(BLACK_ARGB);
let Some(length) = packed_screen_framebuffer_len(row_bytes, scrn_w, scrn_h, pixel_size) else {
return;
};
if w == 0 || h == 0 || fb.len() < length as usize {
return;
}
match pixel_size {
32 => {
for y in 0..h {
for x in 0..w {
let src = y * row_bytes as usize + x * 4;
pixels[y * w + x] = 0xff000000 | (u32::from_be_bytes(fb[src..src + 4].try_into().unwrap()) & 0x00ffffff);
}
}
}
16 => {
for gy in 0..h {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w..(gy + 1) * w];
for gx in 0..w {
let src = row_start + gx * 2;
let [r, g, b] = rgb555_to_rgb888(u16::from_be_bytes([fb[src], fb[src + 1]]));
dst_row[gx] =
0xff00_0000 | (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b);
}
}
}
8 => {
for gy in 0..h {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w..(gy + 1) * w];
for gx in 0..w {
dst_row[gx] = palette[fb[row_start + gx] as usize];
}
}
}
4 => {
for gy in 0..h {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w..(gy + 1) * w];
for gx in 0..w {
let packed = fb[row_start + gx / 2];
let index = if gx & 1 == 0 {
packed >> 4
} else {
packed & 0x0F
};
dst_row[gx] = palette[index as usize];
}
}
}
2 => {
for gy in 0..h {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w..(gy + 1) * w];
for gx in 0..w {
let packed = fb[row_start + gx / 4];
let shift = 6 - 2 * (gx & 3);
let index = (packed >> shift) & 0x03;
dst_row[gx] = palette[index as usize];
}
}
}
_ => {
for gy in 0..h {
let row_start = gy * row_bytes as usize;
let dst_row = &mut pixels[gy * w..(gy + 1) * w];
for gx in 0..w {
let byte = fb[row_start + (gx / 8)];
let bit = 7 - (gx % 8);
let index = (byte >> bit) & 1;
dst_row[gx] = palette[index as usize];
}
}
}
}
}
pub fn argb_palette_from_clut(device_clut: &[[u16; 3]; 256]) -> [u32; 256] {
argb_palette_from_clut_with_gamma(device_clut, &default_display_gamma())
}
pub fn argb_palette_from_clut_with_gamma(
device_clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
) -> [u32; 256] {
let mut palette = [0u32; 256];
for (dst, rgb) in palette.iter_mut().zip(device_clut.iter()) {
let [r, g, b] = *rgb;
*dst = 0xFF000000
| (u32::from(clut_component_to_u8_with_gamma(r, &device_gamma[0])) << 16)
| (u32::from(clut_component_to_u8_with_gamma(g, &device_gamma[1])) << 8)
| u32::from(clut_component_to_u8_with_gamma(b, &device_gamma[2]));
}
palette
}
pub fn render_cursor(
pixels: &mut [u8],
width: u32,
height: u32,
cursor: &CursorImage,
mouse_pos: (i16, i16),
) {
match cursor {
CursorImage::Mono {
data,
mask,
hot_v,
hot_h,
} => render_mono_cursor(pixels, width, height, data, mask, *hot_v, *hot_h, mouse_pos),
CursorImage::Color {
width: cursor_w,
height: cursor_h,
pixels_argb,
mask,
hot_v,
hot_h,
..
} => render_color_cursor_rgba(
pixels,
width,
height,
*cursor_w,
*cursor_h,
pixels_argb,
mask,
*hot_v,
*hot_h,
mouse_pos,
),
}
}
fn render_mono_cursor(
pixels: &mut [u8],
width: u32,
height: u32,
data: &[u8; 32],
mask: &[u8; 32],
hot_v: i16,
hot_h: i16,
mouse_pos: (i16, i16),
) {
let (mouse_v, mouse_h) = mouse_pos;
let cx = mouse_h as i32 - hot_h as i32;
let cy = mouse_v as i32 - hot_v as i32;
for row in 0..16i32 {
let data_word =
((data[(row * 2) as usize] as u16) << 8) | data[(row * 2 + 1) as usize] as u16;
let mask_word =
((mask[(row * 2) as usize] as u16) << 8) | mask[(row * 2 + 1) as usize] as u16;
for col in 0..16i32 {
let bit = 15 - col;
if (mask_word >> bit) & 1 == 0 {
continue;
}
let gx = cx + col;
let gy = cy + row;
if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 {
continue;
}
let data_bit = (data_word >> bit) & 1;
let idx = ((gy as u32 * width + gx as u32) * 4) as usize;
if data_bit == 1 {
pixels[idx] = 0;
pixels[idx + 1] = 0;
pixels[idx + 2] = 0;
} else {
pixels[idx] = 255;
pixels[idx + 1] = 255;
pixels[idx + 2] = 255;
}
pixels[idx + 3] = 255;
}
}
}
pub fn render_cursor_argb(
pixels: &mut [u32],
width: u32,
height: u32,
cursor: &CursorImage,
mouse_pos: (i16, i16),
) {
match cursor {
CursorImage::Mono {
data,
mask,
hot_v,
hot_h,
} => render_mono_cursor_argb(pixels, width, height, data, mask, *hot_v, *hot_h, mouse_pos),
CursorImage::Color {
width: cursor_w,
height: cursor_h,
pixels_argb,
mask,
hot_v,
hot_h,
..
} => render_color_cursor_argb(
pixels,
width,
height,
*cursor_w,
*cursor_h,
pixels_argb,
mask,
*hot_v,
*hot_h,
mouse_pos,
),
}
}
fn render_mono_cursor_argb(
pixels: &mut [u32],
width: u32,
height: u32,
data: &[u8; 32],
mask: &[u8; 32],
hot_v: i16,
hot_h: i16,
mouse_pos: (i16, i16),
) {
let (mouse_v, mouse_h) = mouse_pos;
let cx = mouse_h as i32 - hot_h as i32;
let cy = mouse_v as i32 - hot_v as i32;
for row in 0..16i32 {
let data_word =
((data[(row * 2) as usize] as u16) << 8) | data[(row * 2 + 1) as usize] as u16;
let mask_word =
((mask[(row * 2) as usize] as u16) << 8) | mask[(row * 2 + 1) as usize] as u16;
for col in 0..16i32 {
let bit = 15 - col;
if (mask_word >> bit) & 1 == 0 {
continue;
}
let gx = cx + col;
let gy = cy + row;
if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 {
continue;
}
let data_bit = (data_word >> bit) & 1;
let idx = gy as usize * width as usize + gx as usize;
pixels[idx] = if data_bit == 1 {
BLACK_ARGB
} else {
WHITE_ARGB
};
}
}
}
fn color_cursor_mask_bit(mask: &[u8; 32], row: u16, col: u16) -> bool {
if row >= 16 || col >= 16 {
return false;
}
let row = row as usize;
let bit = 15 - col;
let word = ((mask[row * 2] as u16) << 8) | mask[row * 2 + 1] as u16;
((word >> bit) & 1) != 0
}
fn render_color_cursor_rgba(
pixels: &mut [u8],
width: u32,
height: u32,
cursor_w: u16,
cursor_h: u16,
pixels_argb: &[u32],
mask: &[u8; 32],
hot_v: i16,
hot_h: i16,
mouse_pos: (i16, i16),
) {
let (mouse_v, mouse_h) = mouse_pos;
let cx = mouse_h as i32 - hot_h as i32;
let cy = mouse_v as i32 - hot_v as i32;
for row in 0..cursor_h {
for col in 0..cursor_w {
let gx = cx + col as i32;
let gy = cy + row as i32;
if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 {
continue;
}
let Some(&argb) = pixels_argb.get(row as usize * cursor_w as usize + col as usize)
else {
continue;
};
let idx = ((gy as u32 * width + gx as u32) * 4) as usize;
if color_cursor_mask_bit(mask, row, col) {
pixels[idx] = ((argb >> 16) & 0xFF) as u8;
pixels[idx + 1] = ((argb >> 8) & 0xFF) as u8;
pixels[idx + 2] = (argb & 0xFF) as u8;
pixels[idx + 3] = 0xFF;
} else if argb == BLACK_ARGB {
pixels[idx] = !pixels[idx];
pixels[idx + 1] = !pixels[idx + 1];
pixels[idx + 2] = !pixels[idx + 2];
pixels[idx + 3] = 0xFF;
}
}
}
}
fn render_color_cursor_argb(
pixels: &mut [u32],
width: u32,
height: u32,
cursor_w: u16,
cursor_h: u16,
pixels_argb: &[u32],
mask: &[u8; 32],
hot_v: i16,
hot_h: i16,
mouse_pos: (i16, i16),
) {
let (mouse_v, mouse_h) = mouse_pos;
let cx = mouse_h as i32 - hot_h as i32;
let cy = mouse_v as i32 - hot_v as i32;
for row in 0..cursor_h {
for col in 0..cursor_w {
let gx = cx + col as i32;
let gy = cy + row as i32;
if gx < 0 || gy < 0 || gx >= width as i32 || gy >= height as i32 {
continue;
}
let Some(&argb) = pixels_argb.get(row as usize * cursor_w as usize + col as usize)
else {
continue;
};
let idx = gy as usize * width as usize + gx as usize;
if color_cursor_mask_bit(mask, row, col) {
pixels[idx] = argb | 0xFF00_0000;
} else if argb == BLACK_ARGB {
pixels[idx] ^= 0x00FF_FFFF;
}
}
}
}
pub fn render_debug_overlay_argb(pixels: &mut [u32], width: u32, height: u32, lines: &[String]) {
if pixels.is_empty() || width == 0 || height == 0 || lines.is_empty() {
return;
}
let width = width as usize;
let height = height as usize;
if pixels.len() < width.saturating_mul(height) {
return;
}
let margin = 6usize;
let pad = 4usize;
let char_w = 6usize;
let line_h = 8usize;
if width <= margin * 2 || height <= margin * 2 {
return;
}
let max_chars = ((width - margin * 2).saturating_sub(pad * 2) / char_w).max(1);
let max_lines = ((height - margin * 2).saturating_sub(pad * 2) / line_h).max(1);
let visible_lines = lines.len().min(max_lines);
let longest = lines
.iter()
.take(visible_lines)
.map(|line| line.chars().count().min(max_chars))
.max()
.unwrap_or(0);
if longest == 0 {
return;
}
let panel_x = margin;
let panel_y = margin;
let panel_w = (longest * char_w + pad * 2).min(width - panel_x);
let panel_h = (visible_lines * line_h + pad * 2).min(height - panel_y);
fill_rect_argb(
pixels, width, height, panel_x, panel_y, panel_w, panel_h, 0xFF101010,
);
stroke_rect_argb(
pixels, width, height, panel_x, panel_y, panel_w, panel_h, 0xFF4A9C63,
);
let text_x = panel_x + pad;
let mut text_y = panel_y + pad;
for line in lines.iter().take(visible_lines) {
draw_debug_text_argb(
pixels, width, height, text_x, text_y, line, max_chars, 0xFFE8F0EA,
);
text_y += line_h;
}
}
pub fn render_debug_overlay_rgba(pixels: &mut [u8], width: u32, height: u32, lines: &[String]) {
if pixels.is_empty() || width == 0 || height == 0 || lines.is_empty() {
return;
}
let width = width as usize;
let height = height as usize;
if pixels.len() < width.saturating_mul(height).saturating_mul(4) {
return;
}
let margin = 6usize;
let pad = 4usize;
let char_w = 6usize;
let line_h = 8usize;
if width <= margin * 2 || height <= margin * 2 {
return;
}
let max_chars = ((width - margin * 2).saturating_sub(pad * 2) / char_w).max(1);
let max_lines = ((height - margin * 2).saturating_sub(pad * 2) / line_h).max(1);
let visible_lines = lines.len().min(max_lines);
let longest = lines
.iter()
.take(visible_lines)
.map(|line| line.chars().count().min(max_chars))
.max()
.unwrap_or(0);
if longest == 0 {
return;
}
let panel_x = margin;
let panel_y = margin;
let panel_w = (longest * char_w + pad * 2).min(width - panel_x);
let panel_h = (visible_lines * line_h + pad * 2).min(height - panel_y);
fill_rect_rgba(
pixels,
width,
height,
panel_x,
panel_y,
panel_w,
panel_h,
[0x10, 0x10, 0x10, 0xFF],
);
stroke_rect_rgba(
pixels,
width,
height,
panel_x,
panel_y,
panel_w,
panel_h,
[0x4A, 0x9C, 0x63, 0xFF],
);
let text_x = panel_x + pad;
let mut text_y = panel_y + pad;
for line in lines.iter().take(visible_lines) {
draw_debug_text_rgba(
pixels,
width,
height,
text_x,
text_y,
line,
max_chars,
[0xE8, 0xF0, 0xEA, 0xFF],
);
text_y += line_h;
}
}
fn fill_rect_argb(
pixels: &mut [u32],
width: usize,
height: usize,
x: usize,
y: usize,
rect_w: usize,
rect_h: usize,
color: u32,
) {
let right = x.saturating_add(rect_w).min(width);
let bottom = y.saturating_add(rect_h).min(height);
for row in y..bottom {
let start = row * width + x.min(width);
let end = row * width + right;
pixels[start..end].fill(color);
}
}
fn fill_rect_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
x: usize,
y: usize,
rect_w: usize,
rect_h: usize,
color: [u8; 4],
) {
let right = x.saturating_add(rect_w).min(width);
let bottom = y.saturating_add(rect_h).min(height);
for row in y..bottom {
for col in x.min(width)..right {
let idx = (row * width + col) * 4;
pixels[idx..idx + 4].copy_from_slice(&color);
}
}
}
fn stroke_rect_argb(
pixels: &mut [u32],
width: usize,
height: usize,
x: usize,
y: usize,
rect_w: usize,
rect_h: usize,
color: u32,
) {
if rect_w == 0 || rect_h == 0 || x >= width || y >= height {
return;
}
let right = x.saturating_add(rect_w).min(width).saturating_sub(1);
let bottom = y.saturating_add(rect_h).min(height).saturating_sub(1);
for col in x..=right {
pixels[y * width + col] = color;
pixels[bottom * width + col] = color;
}
for row in y..=bottom {
pixels[row * width + x] = color;
pixels[row * width + right] = color;
}
}
fn stroke_rect_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
x: usize,
y: usize,
rect_w: usize,
rect_h: usize,
color: [u8; 4],
) {
if rect_w == 0 || rect_h == 0 || x >= width || y >= height {
return;
}
let right = x.saturating_add(rect_w).min(width).saturating_sub(1);
let bottom = y.saturating_add(rect_h).min(height).saturating_sub(1);
for col in x..=right {
let top_idx = (y * width + col) * 4;
let bottom_idx = (bottom * width + col) * 4;
pixels[top_idx..top_idx + 4].copy_from_slice(&color);
pixels[bottom_idx..bottom_idx + 4].copy_from_slice(&color);
}
for row in y..=bottom {
let left_idx = (row * width + x) * 4;
let right_idx = (row * width + right) * 4;
pixels[left_idx..left_idx + 4].copy_from_slice(&color);
pixels[right_idx..right_idx + 4].copy_from_slice(&color);
}
}
fn draw_debug_text_argb(
pixels: &mut [u32],
width: usize,
height: usize,
x: usize,
y: usize,
text: &str,
max_chars: usize,
color: u32,
) {
let mut cursor_x = x;
for ch in text.chars().take(max_chars) {
draw_debug_char_argb(pixels, width, height, cursor_x, y, ch, color);
cursor_x += 6;
}
}
fn draw_debug_text_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
x: usize,
y: usize,
text: &str,
max_chars: usize,
color: [u8; 4],
) {
let mut cursor_x = x;
for ch in text.chars().take(max_chars) {
draw_debug_char_rgba(pixels, width, height, cursor_x, y, ch, color);
cursor_x += 6;
}
}
fn debug_glyph_pixels(ch: char, mut paint: impl FnMut(i32, i32)) {
use crate::quickdraw::fonts::{get_font_face_or_default, style::FONT_MONACO};
let face = get_font_face_or_default(FONT_MONACO, 9);
let code = if ch.is_ascii() && !ch.is_ascii_control() {
ch as usize
} else {
b'?' as usize
};
let glyph = &face.glyphs[code - 32];
for row in 0..glyph.height as usize {
for col in 0..glyph.width as usize {
if face.data[glyph.data_offset + row * glyph.width as usize + col] != 0 {
paint(
col as i32 + glyph.origin_x as i32,
row as i32 + glyph.origin_y as i32 + 7,
);
}
}
}
}
fn draw_debug_char_argb(
pixels: &mut [u32],
width: usize,
height: usize,
x: usize,
y: usize,
ch: char,
color: u32,
) {
debug_glyph_pixels(ch, |dx, dy| {
let (gx, gy) = (x as i32 + dx, y as i32 + dy);
if gx >= 0 && gy >= 0 && gx < width as i32 && gy < height as i32 {
pixels[gy as usize * width + gx as usize] = color;
}
});
}
fn draw_debug_char_rgba(
pixels: &mut [u8],
width: usize,
height: usize,
x: usize,
y: usize,
ch: char,
color: [u8; 4],
) {
debug_glyph_pixels(ch, |dx, dy| {
let (gx, gy) = (x as i32 + dx, y as i32 + dy);
if gx >= 0 && gy >= 0 && gx < width as i32 && gy < height as i32 {
let idx = (gy as usize * width + gx as usize) * 4;
pixels[idx..idx + 4].copy_from_slice(&color);
}
});
}
pub fn clut_to_argb(clut: &[[u16; 3]; 256], index: u8) -> u32 {
let [r8, g8, b8] = clut_to_rgba8(clut, index);
0xFF000000 | (u32::from(r8) << 16) | (u32::from(g8) << 8) | u32::from(b8)
}
fn clut_to_rgba8(clut: &[[u16; 3]; 256], index: u8) -> [u8; 3] {
clut_to_rgba8_with_gamma(clut, &default_display_gamma(), index)
}
fn clut_to_rgba8_with_gamma(
clut: &[[u16; 3]; 256],
device_gamma: &DisplayGamma,
index: u8,
) -> [u8; 3] {
let [r, g, b] = clut[index as usize];
[
clut_component_to_u8_with_gamma(r, &device_gamma[0]),
clut_component_to_u8_with_gamma(g, &device_gamma[1]),
clut_component_to_u8_with_gamma(b, &device_gamma[2]),
]
}
#[inline]
fn rgba_word(r: u8, g: u8, b: u8) -> u32 {
u32::from_le_bytes([r, g, b, 0xFF])
}
fn rgb555_to_rgb888(pixel: u16) -> [u8; 3] {
let expand = |value: u16| -> u8 {
let five_bit = value & 0x1f;
((five_bit << 3) | (five_bit >> 2)) as u8
};
[expand(pixel >> 10), expand(pixel >> 5), expand(pixel)]
}
#[cfg(test)]
fn clut_component_to_u8(component: u16) -> u8 {
clut_component_to_u8_with_gamma(component, &MAC_ROM_GAMMA_LUT)
}
fn clut_component_to_u8_with_gamma(component: u16, gamma: &[u8; 256]) -> u8 {
gamma[(component >> 8) as usize]
}
const MAC_ROM_GAMMA_LUT: [u8; 256] = [
0x00, 0x05, 0x09, 0x0B, 0x0E, 0x10, 0x13, 0x15, 0x17, 0x19, 0x1B, 0x1D, 0x1E, 0x20, 0x22, 0x24,
0x25, 0x27, 0x28, 0x2A, 0x2C, 0x2D, 0x2F, 0x30, 0x31, 0x33, 0x34, 0x36, 0x37, 0x38, 0x3A, 0x3B,
0x3C, 0x3E, 0x3F, 0x40, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5E, 0x5F, 0x60, 0x61,
0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71,
0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81,
0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8C, 0x8D, 0x8E, 0x8F,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9B, 0x9C, 0x9D,
0x9E, 0x9F, 0xA0, 0xA1, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB,
0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC0, 0xC1, 0xC2, 0xC3, 0xC3, 0xC4,
0xC5, 0xC6, 0xC7, 0xC7, 0xC8, 0xC9, 0xCA, 0xCA, 0xCB, 0xCC, 0xCD, 0xCD, 0xCE, 0xCF, 0xD0, 0xD0,
0xD1, 0xD2, 0xD3, 0xD3, 0xD4, 0xD5, 0xD6, 0xD6, 0xD7, 0xD8, 0xD9, 0xD9, 0xDA, 0xDB, 0xDC, 0xDC,
0xDD, 0xDE, 0xDF, 0xDF, 0xE0, 0xE1, 0xE1, 0xE2, 0xE3, 0xE4, 0xE4, 0xE5, 0xE6, 0xE7, 0xE7, 0xE8,
0xE9, 0xE9, 0xEA, 0xEB, 0xEC, 0xEC, 0xED, 0xEE, 0xEE, 0xEF, 0xF0, 0xF1, 0xF1, 0xF2, 0xF3, 0xF3,
0xF4, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFC, 0xFC, 0xFD, 0xFE, 0xFF,
];
#[cfg(test)]
mod tests {
#[test]
fn output_scale_covers_aspect_fitted_drawable() {
for (drawable, expected) in [
((800, 600), 1),
((1600, 1200), 2),
((1200, 900), 2),
((2400, 1200), 2),
((2400, 1800), 3),
((4000, 3000), 4),
((0, 0), 1),
] {
assert_eq!(super::outline_output_scale((800, 600), drawable), expected);
}
assert_eq!(super::outline_output_scale((0, 0), (800, 600)), 1);
}
#[test]
fn coverage_reciprocal_matches_integer_division_at_boundaries() {
for divisor in [
1u64,
2,
3,
5,
7,
255,
256,
1_920_000,
u32::MAX as u64,
1u64 << 63,
u64::MAX,
] {
for numerator in [
0,
1,
divisor - 1,
divisor,
divisor.saturating_add(1),
divisor.saturating_mul(255),
u64::MAX - 1,
u64::MAX,
] {
assert_eq!(
super::coverage_quotient(numerator, divisor, u64::MAX / divisor),
numerator / divisor,
);
}
}
let mut seed = 0x123456789abcdef0u64;
for _ in 0..100_000 {
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
let numerator = seed;
seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
let divisor = seed.max(1);
assert_eq!(
super::coverage_quotient(numerator, divisor, u64::MAX / divisor),
numerator / divisor,
);
}
}
#[test]
fn coverage_resize_preserves_all_channels_with_mixed_axis_scales() {
let mut output = Vec::new();
let source = [
0x10203040, 0x50607080, 0x90a0b0c0, 0xc0b0a090, 0x80706050, 0x40302010,
];
super::resize_argb_coverage(&source, (3, 2), (2, 3), &mut output);
assert_eq!(
output,
[0x25354555, 0x7b8b9bab, 0xab9b8b7b, 0x55453525, 0xab9b8b7b, 0x55453525,]
);
let transposed = [
source[0], source[3], source[1], source[4], source[2], source[5],
];
super::resize_argb_coverage(&transposed, (2, 3), (3, 2), &mut output);
assert_eq!(
output,
[0x25354555, 0xab9b8b7b, 0xab9b8b7b, 0x7b8b9bab, 0x55453525, 0x55453525,]
);
super::resize_argb_coverage(
&[0x00000000, 0x11223344, 0xaabbccdd, 0xffffffff],
(2, 2),
(1, 1),
&mut output,
);
assert_eq!(output, [0x6f778088]);
}
#[test]
fn software_minification_preserves_fractional_stroke_coverage() {
let source: Vec<u32> = (0..64)
.map(|i| if i % 2 == 0 { 0xff000000 } else { 0xffffffff })
.collect();
let mut output = Vec::new();
for (width, expected) in [
(2, vec![128, 128]),
(3, vec![96, 128, 159]),
(5, vec![96, 96, 128, 159, 159]),
] {
super::resize_argb_coverage(&source, (8, 8), (width, 3), &mut output);
for (i, pixel) in output.iter().enumerate() {
assert_eq!((pixel & 255) as u8, expected[i % width as usize]);
assert_eq!(pixel >> 24, 255);
}
}
super::resize_argb_coverage(&source, (8, 8), (16, 16), &mut output);
for y in 0..16 {
for x in 0..16 {
assert_eq!(output[y * 16 + x], source[(y / 2) * 8 + x / 2]);
}
}
}
use super::{
argb_palette_from_clut, argb_palette_from_clut_with_gamma, classic_depth_mode,
classic_pixel_size, clut_component_to_u8, clut_to_argb,
normalize_centered_compact_mac_viewport_margins_rgba, render_cursor, render_cursor_argb,
render_screen_argb, render_screen_into, render_screen_with_rgba_palette_into,
rgba_palette_from_clut, screen_pixel_rgb, screen_pixel_rgb_with_gamma, CursorImage,
};
use crate::memory::{MacMemoryBus, MemoryBus};
#[test]
fn classic_video_depth_modes_round_trip_every_supported_pixel_size() {
for (pixel_size, mode) in [
(1, 0x0080),
(2, 0x0081),
(4, 0x0082),
(8, 0x0083),
(16, 0x0084),
(32, 0x0085),
] {
assert_eq!(classic_depth_mode(pixel_size), Some(mode));
assert_eq!(classic_pixel_size(pixel_size), Some(pixel_size));
assert_eq!(classic_pixel_size(mode), Some(pixel_size));
}
assert_eq!(classic_depth_mode(3), None);
assert_eq!(classic_pixel_size(0x0086), None);
}
#[test]
fn clut_component_applies_the_modeled_device_default() {
assert_eq!(clut_component_to_u8(0x0000), 0x00);
assert_eq!(clut_component_to_u8(0x0A0A), 0x1B);
assert_eq!(clut_component_to_u8(0xFFFF), 0xFF);
assert_eq!(clut_component_to_u8(0x4444), 0x66);
assert_eq!(clut_component_to_u8(0x6666), 0x87);
assert_eq!(clut_component_to_u8(0x7373), 0x93);
assert_eq!(clut_component_to_u8(0xAAAA), 0xC0);
}
#[test]
fn clut_to_argb_applies_gamma() {
let mut clut = [[0u16; 3]; 256];
clut[7] = [0x4444, 0x8888, 0xCCCC];
let argb = clut_to_argb(&clut, 7);
assert_eq!(argb, 0xFF66A5DA);
assert_eq!(argb_palette_from_clut(&clut)[7], argb);
}
#[test]
fn argb_palette_applies_each_installed_gamma_channel() {
let mut clut = [[0u16; 3]; 256];
clut[7] = [0x1212, 0x3434, 0x5656];
let mut gamma = [[0u8; 256]; 3];
gamma[0][0x12] = 0xA1;
gamma[1][0x34] = 0xB2;
gamma[2][0x56] = 0xC3;
let palette = argb_palette_from_clut_with_gamma(&clut, &gamma);
assert_eq!(palette[7], 0xFFA1B2C3);
}
#[test]
fn render_screen_into_8bpp_writes_rgba_bytes() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0);
bus.write_byte(base + 1, 1);
bus.write_byte(base + 2, 2);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0x0000, 0x0000, 0x0000];
clut[1] = [0xFFFF, 0x0000, 0x0000];
clut[2] = [0x0000, 0xFFFF, 0x0000];
let mut pixels = Vec::new();
render_screen_into(&bus, (base, 4, 3, 1, 8), &clut, &mut pixels);
assert_eq!(
pixels,
vec![
0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF,
]
);
}
#[test]
fn render_screen_with_precomputed_palette_matches_clut_path() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0);
bus.write_byte(base + 1, 1);
bus.write_byte(base + 2, 2);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0x0000, 0x0000, 0x0000];
clut[1] = [0xFFFF, 0x0000, 0x0000];
clut[2] = [0x0000, 0xFFFF, 0x0000];
let mut direct = Vec::new();
render_screen_into(&bus, (base, 4, 3, 1, 8), &clut, &mut direct);
let palette = rgba_palette_from_clut(&clut);
let mut precomputed = Vec::new();
render_screen_with_rgba_palette_into(&bus, (base, 4, 3, 1, 8), &palette, &mut precomputed);
assert_eq!(precomputed, direct);
}
#[test]
fn render_screen_reuses_rgba_storage_without_retaining_old_pixels() {
let mut bus = MacMemoryBus::new(1024);
for base in [128, 136, 144, 152, 160] {
bus.write_byte(base, 0xA5);
bus.write_byte(base + 1, 0x3C);
}
bus.write_word(160, 0x7FFF);
let palette = std::array::from_fn(|index| {
super::rgba_word(index as u8, index.wrapping_mul(3) as u8, 0x5A)
});
let screen_modes = [
(128, 1, 8, 1, 1),
(136, 1, 4, 1, 2),
(144, 1, 2, 1, 4),
(152, 1, 2, 1, 8),
(160, 2, 1, 1, 16),
];
for screen_mode in screen_modes {
let mut fresh = Vec::new();
render_screen_with_rgba_palette_into(&bus, screen_mode, &palette, &mut fresh);
let mut reused = vec![0x13; fresh.len()];
render_screen_with_rgba_palette_into(&bus, screen_mode, &palette, &mut reused);
assert_eq!(reused, fresh, "pixel depth {}", screen_mode.4);
}
}
#[test]
fn render_screen_into_4bpp_decodes_high_then_low_nibbles() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0x12);
bus.write_byte(base + 1, 0x30);
let mut clut = [[0u16; 3]; 256];
clut[1] = [0xFFFF, 0x0000, 0x0000];
clut[2] = [0x0000, 0xFFFF, 0x0000];
clut[3] = [0x0000, 0x0000, 0xFFFF];
let mut rgba = Vec::new();
render_screen_into(&bus, (base, 2, 4, 1, 4), &clut, &mut rgba);
assert_eq!(
rgba,
vec![
0xFF, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF,
]
);
let mut argb = Vec::new();
render_screen_argb(&bus, (base, 2, 4, 1, 4), &clut, &mut argb);
assert_eq!(
argb,
vec![0xFFFF_0000, 0xFF00_FF00, 0xFF00_00FF, 0xFF00_0000]
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 2, 4, 1, 4), &clut, 0, 0),
Some([255, 0, 0])
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 2, 4, 1, 4), &clut, 1, 0),
Some([0, 255, 0])
);
}
#[test]
fn render_screen_into_2bpp_decodes_high_to_low_fields() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0b00_01_10_11);
bus.write_byte(base + 1, 0xff);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0xffff, 0xffff, 0xffff];
clut[1] = [0xffff, 0x0000, 0x0000];
clut[2] = [0x0000, 0xffff, 0x0000];
clut[3] = [0x0000, 0x0000, 0xffff];
let mut rgba = Vec::new();
render_screen_into(&bus, (base, 2, 4, 1, 2), &clut, &mut rgba);
assert_eq!(
rgba,
vec![
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff,
]
);
let mut argb = Vec::new();
render_screen_argb(&bus, (base, 2, 4, 1, 2), &clut, &mut argb);
assert_eq!(
argb,
vec![0xffff_ffff, 0xffff_0000, 0xff00_ff00, 0xff00_00ff]
);
for (x, expected) in [
[0xff, 0xff, 0xff],
[0xff, 0x00, 0x00],
[0x00, 0xff, 0x00],
[0x00, 0x00, 0xff],
]
.into_iter()
.enumerate()
{
assert_eq!(
screen_pixel_rgb(&bus, (base, 2, 4, 1, 2), &clut, x as u32, 0),
Some(expected)
);
}
}
#[test]
fn malformed_two_bit_screen_modes_render_black_without_reading_past_ram() {
let bus = MacMemoryBus::new(64);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0xFFFF, 0x0000, 0x0000];
let malformed_modes = [
(60, 1, 5, 1, 2), (63, 2, 4, 2, 2), ];
for screen_mode in malformed_modes {
let pixel_count = usize::from(screen_mode.2) * usize::from(screen_mode.3);
let mut rgba = vec![0xE7; pixel_count * 4];
render_screen_into(&bus, screen_mode, &clut, &mut rgba);
assert_eq!(rgba, vec![0, 0, 0, 0xFF].repeat(pixel_count));
let mut argb = vec![0xFFFF_00FF; pixel_count];
render_screen_argb(&bus, screen_mode, &clut, &mut argb);
assert_eq!(argb, vec![0xFF00_0000; pixel_count]);
assert_eq!(screen_pixel_rgb(&bus, screen_mode, &clut, 0, 0), None);
}
}
#[test]
fn compact_viewport_normalizer_blacks_uniform_outer_flood() {
let width = 800usize;
let height = 600usize;
let mut pixels = vec![0u8; width * height * 4];
for px in pixels.chunks_exact_mut(4) {
px.copy_from_slice(&[255, 106, 37, 0xFF]);
}
let left = 143usize;
let top = 129usize;
let right = 657usize;
let bottom = 471usize;
for y in top + 9..bottom {
for x in left..right {
let idx = (y * width + x) * 4;
pixels[idx..idx + 4].copy_from_slice(&[
(x & 0xFF) as u8,
245,
(y & 0xFF) as u8,
0xFF,
]);
}
}
assert!(normalize_centered_compact_mac_viewport_margins_rgba(
&mut pixels,
width,
height
));
assert_eq!(&pixels[0..4], &[0, 0, 0, 0xFF]);
let trimmed_top_idx = ((top + 1) * width + 400) * 4;
assert_eq!(
&pixels[trimmed_top_idx..trimmed_top_idx + 4],
&[0, 0, 0, 0xFF]
);
let inside_idx = (300 * width + 400) * 4;
assert_eq!(
&pixels[inside_idx..inside_idx + 4],
&[(400 & 0xFF) as u8, 245, (300 & 0xFF) as u8, 0xFF]
);
let lower_margin_idx = (500 * width + 400) * 4;
assert_eq!(
&pixels[lower_margin_idx..lower_margin_idx + 4],
&[0, 0, 0, 0xFF]
);
}
#[test]
fn compact_viewport_normalizer_keeps_content_wider_than_a_compact_mac_screen() {
let width = 800usize;
let height = 600usize;
let mut pixels = vec![0u8; width * height * 4];
for px in pixels.chunks_exact_mut(4) {
px.copy_from_slice(&[255, 255, 222, 0xFF]);
}
let (left, top, right, bottom) = (128usize, 134usize, 672usize, 466usize);
for y in top..bottom {
for x in left..right {
let idx = (y * width + x) * 4;
pixels[idx..idx + 4].copy_from_slice(&[
(x & 0xFF) as u8,
200,
(y & 0xFF) as u8,
0xFF,
]);
}
}
assert!(normalize_centered_compact_mac_viewport_margins_rgba(
&mut pixels,
width,
height
));
for x in [left, left + 8, right - 9, right - 1] {
let idx = (300 * width + x) * 4;
assert_eq!(
&pixels[idx..idx + 4],
&[(x & 0xFF) as u8, 200, (300 & 0xFF) as u8, 0xFF],
"column {x} is inside the 544-wide viewport and must not be blacked out"
);
}
let outside_idx = (300 * width + (left - 1)) * 4;
assert_eq!(&pixels[outside_idx..outside_idx + 4], &[0, 0, 0, 0xFF]);
}
#[test]
fn compact_viewport_normalizer_preserves_detailed_outer_pixels() {
let width = 800usize;
let height = 600usize;
let mut pixels = vec![0u8; width * height * 4];
for y in 0..height {
for x in 0..width {
let idx = (y * width + x) * 4;
pixels[idx..idx + 4].copy_from_slice(&[
(x & 0xFF) as u8,
(y & 0xFF) as u8,
((x + y) & 0xFF) as u8,
0xFF,
]);
}
}
let before = pixels.clone();
assert!(!normalize_centered_compact_mac_viewport_margins_rgba(
&mut pixels,
width,
height
));
assert_eq!(pixels, before);
}
#[test]
fn screen_pixel_rgb_samples_8bpp_with_gamma() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0);
bus.write_byte(base + 1, 7);
let mut clut = [[0u16; 3]; 256];
clut[7] = [0x4444, 0x8888, 0xCCCC];
assert_eq!(
screen_pixel_rgb(&bus, (base, 4, 2, 1, 8), &clut, 1, 0),
Some([0x66, 0xA5, 0xDA])
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 4, 2, 1, 8), &clut, 2, 0),
None
);
let mut gamma = [[0u8; 256]; 3];
gamma[0][0x44] = 0x11;
gamma[1][0x88] = 0x22;
gamma[2][0xCC] = 0x33;
assert_eq!(
screen_pixel_rgb_with_gamma(&bus, (base, 4, 2, 1, 8), &clut, &gamma, 1, 0),
Some([0x11, 0x22, 0x33])
);
}
#[test]
fn render_screen_into_16bpp_decodes_rgb555_pixels() {
let mut bus = MacMemoryBus::new(4096);
let base = 0x100;
bus.write_word(base, 0x7c00);
bus.write_word(base + 2, 0x03e0);
let mut pixels = Vec::new();
render_screen_into(&bus, (base, 4, 2, 1, 16), &[[0; 3]; 256], &mut pixels);
assert_eq!(&pixels[0..4], &[0xff, 0x00, 0x00, 0xff]);
assert_eq!(&pixels[4..8], &[0x00, 0xff, 0x00, 0xff]);
}
#[test]
fn screen_pixel_rgb_samples_16bpp() {
let mut bus = MacMemoryBus::new(4096);
let base = 0x100;
bus.write_word(base, 0x001f);
assert_eq!(
screen_pixel_rgb(&bus, (base, 2, 1, 1, 16), &[[0; 3]; 256], 0, 0),
Some([0x00, 0x00, 0xff])
);
}
#[test]
fn one_bit_host_rendering_uses_swapped_custom_palette() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0b1010_0000);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0x0000, 0x0000, 0x0000];
clut[1] = [0xFFFF, 0x0000, 0x0000];
let mut pixels = Vec::new();
render_screen_into(&bus, (base, 1, 4, 1, 1), &clut, &mut pixels);
assert_eq!(
pixels,
vec![
0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF,
]
);
let mut argb = Vec::new();
render_screen_argb(&bus, (base, 1, 4, 1, 1), &clut, &mut argb);
assert_eq!(
argb,
vec![0xFFFF_0000, 0xFF00_0000, 0xFFFF_0000, 0xFF00_0000]
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 1, 4, 1, 1), &clut, 0, 0),
Some([255, 0, 0])
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 1, 4, 1, 1), &clut, 1, 0),
Some([0, 0, 0])
);
}
#[test]
fn render_color_cursor_replaces_masked_pixels_and_inverts_black_outside_mask() {
let mut mask = [0u8; 32];
mask[0] = 0x80;
let cursor = CursorImage::Color {
width: 2,
height: 2,
pixels_argb: vec![0xFFFF_0000, 0xFF00_0000, 0xFFFF_FFFF, 0xFF00_FF00],
mask,
hot_v: 0,
hot_h: 0,
mono_data: [0; 32],
mono_mask: mask,
};
let mut rgba = vec![
0x40, 0x50, 0x60, 0xFF, 0x40, 0x50, 0x60, 0xFF, 0x40, 0x50, 0x60, 0xFF, 0x40, 0x50,
0x60, 0xFF,
];
render_cursor(&mut rgba, 2, 2, &cursor, (0, 0));
assert_eq!(&rgba[0..4], &[0xFF, 0x00, 0x00, 0xFF]);
assert_eq!(&rgba[4..8], &[0xBF, 0xAF, 0x9F, 0xFF]);
assert_eq!(&rgba[8..12], &[0x40, 0x50, 0x60, 0xFF]);
assert_eq!(&rgba[12..16], &[0x40, 0x50, 0x60, 0xFF]);
let mut argb = vec![0xFF40_5060; 4];
render_cursor_argb(&mut argb, 2, 2, &cursor, (0, 0));
assert_eq!(argb[0], 0xFFFF_0000);
assert_eq!(argb[1], 0xFFBF_AF9F);
assert_eq!(argb[2], 0xFF40_5060);
assert_eq!(argb[3], 0xFF40_5060);
}
#[test]
fn screen_pixel_rgb_samples_1bpp() {
let mut bus = MacMemoryBus::new(1024);
let base = 128;
bus.write_byte(base, 0b1010_0000);
let mut clut = [[0u16; 3]; 256];
clut[0] = [0xFFFF, 0xFFFF, 0xFFFF];
clut[1] = [0x0000, 0x0000, 0x0000];
assert_eq!(
screen_pixel_rgb(&bus, (base, 1, 4, 1, 1), &clut, 0, 0),
Some([0, 0, 0])
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 1, 4, 1, 1), &clut, 1, 0),
Some([255, 255, 255])
);
assert_eq!(
screen_pixel_rgb(&bus, (base, 1, 4, 1, 1), &clut, 4, 0),
None
);
}
}
#[cfg(test)]
mod owned_frame_tests {
use super::*;
#[test]
fn packed_rows_preserve_bit_order_stride_and_rgb555_endian() {
let palette = std::array::from_fn(|index| 0xff000000 | index as u32);
for (depth, stride, bytes, indices) in [
(
1,
2,
vec![0xa0, 0xee, 0x50, 0xdd],
vec![1, 0, 1, 0, 0, 1, 0, 1],
),
(
2,
2,
vec![0x1b, 0xee, 0xe4, 0xdd],
vec![0, 1, 2, 3, 3, 2, 1, 0],
),
(
4,
3,
vec![0x12, 0x34, 0xee, 0x56, 0x78, 0xdd],
(1..=8).collect(),
),
(
8,
5,
vec![1, 2, 3, 4, 0xee, 5, 6, 7, 8, 0xdd],
(1..=8).collect(),
),
] {
let mut output = Vec::new();
render_packed_screen_argb(&bytes, (stride, 4, 2, depth), &palette, &mut output);
assert_eq!(
output,
indices.into_iter().map(|i| palette[i]).collect::<Vec<_>>(),
"depth={depth}"
);
}
let mut output = Vec::new();
render_packed_screen_argb(
&[0x7c, 0, 0x03, 0xe0, 0, 0x1f, 0x7f, 0xff, 0xee, 0xee],
(10, 4, 1, 16),
&palette,
&mut output,
);
assert_eq!(output, [0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffffff]);
render_packed_screen_argb(&[0x6f], (1, 3, 1, 2), &palette, &mut output);
assert_eq!(output, [palette[1], palette[2], palette[3]]);
}
#[test]
fn rgb32_owned_rows_preserve_channels_stride_and_ignore_unused_byte() {
let bytes = [
0x7f, 0x12, 0x34, 0x56, 0x80, 0xab, 0xcd, 0xef, 0xee, 0xee, 0xee, 0xee, 0xff, 0xff, 0, 0,
0x33, 0, 0x80, 0xff, 0xdd, 0xdd, 0xdd, 0xdd,
];
let mut output = Vec::new();
render_packed_screen_argb(&bytes, (12, 2, 2, 32), &[0; 256], &mut output);
assert_eq!(output, [0xff123456, 0xffabcdef, 0xffff0000, 0xff0080ff]);
render_packed_screen_argb(&bytes[..19], (12, 2, 2, 32), &[0; 256], &mut output);
assert_eq!(output, [BLACK_ARGB; 4]);
}
#[test]
fn malformed_owned_layouts_keep_black_fallback() {
let mut output = vec![0xffffffff; 9];
for (bytes, layout) in [
(vec![1; 5], (4, 4, 2, 8)),
(vec![0xff; 2], (1, 9, 1, 1)),
(vec![0xff; 16], (16, 4, 1, 24)),
] {
render_packed_screen_argb(&bytes, layout, &[0xffffffff; 256], &mut output);
assert_eq!(
output,
vec![BLACK_ARGB; usize::from(layout.1) * usize::from(layout.2)]
);
}
render_packed_screen_argb(&[], (0, 0, 4, 8), &[0; 256], &mut output);
assert!(output.is_empty());
}
#[test]
fn captured_pixels_and_palette_are_independent_of_later_guest_changes() {
let mut bus = MacMemoryBus::new(1024 * 1024);
let base = 0x1000;
bus.write_byte(base, 1);
bus.write_byte(base + 1, 2);
let mut clut = [[0; 3]; 256];
clut[1] = [65535, 0, 0];
clut[2] = [0, 65535, 0];
let mut frame = PackedScreenFrame::default();
frame.capture(&bus, (base, 2, 2, 1, 8), &clut, &linear_display_gamma());
bus.write_byte(base, 0);
clut[1] = [0, 0, 65535];
let mut output = Vec::new();
frame.render_argb(&mut output);
assert_eq!(output, [0xffff0000, 0xff00ff00]);
bus.write_byte(base, 1);
frame.capture(&bus, (base, 2, 2, 1, 8), &clut, &linear_display_gamma());
frame.render_argb(&mut output);
assert_eq!(output, [0xff0000ff, 0xff00ff00]);
}
}