diff options
author | gennyble <gen@nyble.dev> | 2024-12-07 03:09:34 -0600 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-12-07 03:09:34 -0600 |
commit | f4cd5d7edc458535c8ab5245c5829a23fca4e5ef (patch) | |
tree | 5b29e12c6fcdc4591d0b257c8a9fbc58c4a7cf48 /src | |
parent | e29e66de1bbee0569a0707dd9bd603f221e6e970 (diff) | |
download | really-etches-f4cd5d7edc458535c8ab5245c5829a23fca4e5ef.tar.gz really-etches-f4cd5d7edc458535c8ab5245c5829a23fca4e5ef.zip |
Save etches as GIF
Diffstat (limited to 'src')
-rw-r--r-- | src/image.rs | 30 | ||||
-rw-r--r-- | src/main.rs | 8 |
2 files changed, 36 insertions, 2 deletions
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<u8> = self + .data + .clone() + .into_iter() + .map(|pix| palette.iter().position(|&x| x == pix).unwrap() as u8) + .collect(); + + let palette: Vec<gifed::Color> = 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 } diff --git a/src/main.rs b/src/main.rs index b1f22ab..30c374b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use tracing_subscriber::EnvFilter; use winit::{ application::ApplicationHandler, dpi::LogicalSize, - event::{DeviceEvent, KeyEvent, WindowEvent}, + event::{KeyEvent, WindowEvent}, event_loop::{ControlFlow, EventLoop}, keyboard::{Key, NamedKey}, window::Window, @@ -125,7 +125,11 @@ impl Etch { .set_file_name("etch.gif") .save_file(); - if let Some(path) = location {} + if let Some(path) = location { + tracing::info!("saving gif to {}", path.to_string_lossy()); + let gif = self.img.gif(); + gif.save(path).unwrap(); + } } } |