From f4cd5d7edc458535c8ab5245c5829a23fca4e5ef Mon Sep 17 00:00:00 2001 From: gennyble Date: Sat, 7 Dec 2024 03:09:34 -0600 Subject: Save etches as GIF --- src/image.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/image.rs') diff --git a/src/image.rs b/src/image.rs index 6f03db5..c1e67db 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,3 +1,5 @@ +use gifed::{block::Palette, videogif::Frame, writer::ImageBuilder, Gif}; + use crate::Vec2; #[allow(dead_code)] @@ -30,6 +32,34 @@ impl Image { &mut self.data } + pub fn gif(&self) -> Gif { + let mut palette = self.data.clone(); + palette.sort(); + palette.dedup(); + + let indicies: Vec = self + .data + .clone() + .into_iter() + .map(|pix| palette.iter().position(|&x| x == pix).unwrap() as u8) + .collect(); + + let palette: Vec = palette + .into_iter() + .map(|c| { + let b = c.to_be_bytes(); + gifed::Color::from([b[3], b[2], b[1]]) + }) + .collect(); + + let img = ImageBuilder::new(self.width as u16, self.height as u16).build(indicies).unwrap(); + let mut gif = Gif::new(self.width as u16, self.height as u16); + gif.set_palette(Some(Palette::try_from(palette).unwrap())); + gif.push(img); + + gif + } + pub fn width(&self) -> u32 { self.width } -- cgit 1.4.1-3-g733a5