use super::System;
#[derive(Clone)]
pub struct Image{
width: i32,
height: i32,
buffer: Vec<u8>
}
impl Image {
pub fn make(width: i32, height: i32, buffer: Vec<u8>) -> Self {
Self{width, height, buffer}
}
pub fn width(&self) -> i32 {
self.width
}
pub fn height(&self) -> i32 {
self.height
}
}
impl System {
pub fn draw_image(&mut self, img: &Image, x: i32, y: i32) {
let (width, height, scr_width, scr_height) = (img.width, img.height, self.width, self.height);
if (x < scr_width) && (x + width > 0) && (y < scr_height) && (y + height > 0) { let (u0, v0) = (0i32.max(-x), 0i32.max(-y));
let (u1, v1) = (width.min(scr_width - x), height.min(scr_height - y));
let (y0, y1) = ((y + v0) as usize, (y + v1) as usize);
let i2v_diff = v0 - (y0 as i32);
let img_buf = &img.buffer;
let row_offs_0 = ((x + u0) << 2) as usize;
let mut scr_rows: Vec<(usize, &mut [u8])> = self.buffer.chunks_mut((scr_width << 2) as usize)
.enumerate()
.collect();
let scr_sub_rows = &mut scr_rows[y0..y1]; scr_sub_rows.into_iter().for_each(|(i, vb_row)| {
let v = (*i as i32) + i2v_diff;
let mut img_offs = ((v * width + u0) << 2) as usize;
let mut row_offs = row_offs_0;
for _u in u0..u1 {
let a = img_buf[img_offs + 3] as i32;
let na = 0x100 - a;
for i in 0..3 { vb_row[row_offs + i] = (((img_buf[img_offs + i] as i32) * a + (vb_row[row_offs + i] as i32) * na) >> 8) as u8;
}
img_offs += 4;
row_offs += 4;
}
});
}
}
}