#[derive(Clone, PartialEq, Eq)]
pub struct Image {
pub width: u32,
pub height: u32,
pub data: Vec<u8>,
}
impl Image {
#[must_use]
pub fn encode_png(&self) -> Vec<u8> {
assert_eq!(
self.data.len(),
self.width as usize * self.height as usize * 4,
"Image data must be width * height * 4 bytes"
);
let mut out = Vec::new();
let mut encoder = png::Encoder::new(&mut out, self.width, self.height);
encoder.set_color(png::ColorType::Rgba);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().expect("in-memory PNG header");
writer
.write_image_data(&self.data)
.expect("in-memory PNG data");
writer.finish().expect("in-memory PNG");
out
}
}
impl std::fmt::Debug for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Image({}x{})", self.width, self.height)
}
}
pub trait FrameSource {
fn fps(&self) -> u32;
fn size(&self) -> (u32, u32);
fn total_frames(&self) -> u32;
fn render(&mut self, frame: u32, size: (u32, u32)) -> Image;
}