use super::*;
use alloc::boxed::Box;
pub struct Image {
pub magic_number: u32,
pub width: u16,
pub height: u16,
pub pixels: Box<[Color]>,
pub binary: &'static [u8],
}
impl Image {
pub fn from_raw(binary_raw: &'static [u8]) -> Option<Self> {
let magic_number: u32 = u32::from_le_bytes(
binary_raw[..4].try_into().expect("Slice trop courte pour magic number")
);
if magic_number == utils::EIF1_MAGIC_NUMBER {
return Some(Image::from_raw_eif1(binary_raw));
};
None
}
pub fn from_raw_eif1(binary_raw: &'static [u8]) -> Self {
let magic_number: u32 = unsafe { *(binary_raw.as_ptr() as *const u32) };
let width: u16 = unsafe { *(binary_raw.as_ptr().add(4) as *const u16) };
let height: u16 = unsafe { *(binary_raw.as_ptr().add(6) as *const u16) };
let surface_size = (width as usize) * (height as usize);
let binary: &'static [u8] = &binary_raw[8..];
let pixels: Box<[Color]> = (0..surface_size)
.map(|i| {
let idx = i * 2;
let rgb565 = u16::from_le_bytes([binary[idx], binary[idx + 1]]);
Color { rgb565 }
})
.collect();
Image {
magic_number,
width,
height,
pixels,
binary,
}
}
pub fn get_pixels(&self) -> &[Color] {
&self.pixels
}
pub fn for_coordinates(&self, x: u16, y: u16) -> Rect {
Rect {
x,
y,
width: self.width,
height: self.height,
}
}
}