use std::sync::Arc;
use crate::app::render_engine::{CapturedFrame, RenderCaptureTarget};
#[derive(Clone, Debug, PartialEq)]
pub struct SystemPicture {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
pub rgba: Vec<u8>,
}
pub(crate) type SystemPictures = Arc<dyn Fn() -> Vec<SystemPicture> + Send + Sync>;
pub(crate) fn lay_over(pixels: &mut [u8], width: u32, height: u32, origin: (u32, u32), pictures: &[SystemPicture]) {
for picture in pictures {
for row in 0..picture.height {
let y = i64::from(picture.y) + i64::from(row) - i64::from(origin.1);
if y < 0 || y >= i64::from(height) {
continue;
}
for column in 0..picture.width {
let x = i64::from(picture.x) + i64::from(column) - i64::from(origin.0);
if x < 0 || x >= i64::from(width) {
continue;
}
let from = ((row * picture.width + column) * 4) as usize;
let Some(source) = picture.rgba.get(from..from + 4) else {
continue;
};
let alpha = u32::from(source[3]);
if alpha == 0 {
continue;
}
let to = ((y as u32 * width + x as u32) * 4) as usize;
let under = &mut pixels[to..to + 4];
for channel in 0..4 {
let over = u32::from(source[channel]) + (u32::from(under[channel]) * (255 - alpha) + 127) / 255;
under[channel] = over.min(255) as u8;
}
}
}
}
}
pub(crate) fn laying_over(
target: RenderCaptureTarget,
origin: (u32, u32),
pictures: Vec<SystemPicture>,
) -> RenderCaptureTarget {
if pictures.is_empty() {
return target;
}
RenderCaptureTarget::Bytes(Arc::new(move |outcome: Result<CapturedFrame, String>| {
let outcome = outcome.map(|mut frame| {
lay_over(&mut frame.rgba, frame.width, frame.height, origin, &pictures);
frame
});
match (&target, outcome) {
(RenderCaptureTarget::Bytes(callback), outcome) => callback(outcome),
(RenderCaptureTarget::Path(path), Ok(frame)) => {
crate::app::frame_capture::save_capture_png(path, &frame.rgba, frame.width, frame.height);
}
(RenderCaptureTarget::Path(_), Err(reason)) => target.fail(reason),
}
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_picture_is_drawn_over_the_capture_where_it_is_in_the_window() {
let mut pixels = vec![100_u8; 4 * 2 * 4];
let red = [255, 0, 0, 255];
let half_white = [128, 128, 128, 128];
let clear = [0, 0, 0, 0];
let picture = SystemPicture {
x: 11,
y: 20,
width: 2,
height: 2,
rgba: [red, half_white, clear, red].concat(),
};
lay_over(&mut pixels, 4, 2, (10, 20), &[picture]);
let at = |x: usize, y: usize| pixels[(y * 4 + x) * 4..][..4].to_vec();
assert_eq!(at(0, 0), vec![100; 4], "outside the picture");
assert_eq!(at(1, 0), red.to_vec(), "opaque");
assert_eq!(at(2, 0), vec![178, 178, 178, 178], "half over gray");
assert_eq!(at(1, 1), vec![100; 4], "clear");
assert_eq!(at(2, 1), red.to_vec());
}
#[test]
fn a_picture_past_the_capture_is_cut_to_it() {
let mut pixels = vec![0_u8; 2 * 2 * 4];
let picture = SystemPicture {
x: -1,
y: 1,
width: 4,
height: 4,
rgba: [255_u8; 4 * 4 * 4].to_vec(),
};
lay_over(&mut pixels, 2, 2, (0, 0), &[picture]);
assert_eq!(&pixels[..8], &[0; 8], "the row above the picture");
assert_eq!(&pixels[8..], &[255; 8]);
}
}