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 --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 4 +++- src/image.rs | 30 ++++++++++++++++++++++++++++++ src/main.rs | 8 ++++++-- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf3ddf5..250f1fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -418,6 +418,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "combine" version = "4.6.7" @@ -875,6 +881,8 @@ version = "0.3.0" source = "git+https://git.nyble.dev/multimedia/gifed#7a3a4b5f86b17015e174737fde6c4867682b17f8" dependencies = [ "bitvec", + "color_quant", + "rgb", "weezl", ] @@ -1860,6 +1868,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rgb" +version = "0.8.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" +dependencies = [ + "bytemuck", +] + [[package]] name = "rustix" version = "0.38.41" diff --git a/Cargo.toml b/Cargo.toml index f5c2a32..b50e2a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -gifed = { git = "https://git.nyble.dev/multimedia/gifed" } +gifed = { git = "https://git.nyble.dev/multimedia/gifed", features = [ + "videoish", +] } gilrs = "0.11.0" neam = { git = "https://github.com/gennyble/neam", rev = "5efc1761866283537fd254a8fb3582f50631cf0f" } rfd = "0.15.1" 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 } 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(); + } } } -- cgit 1.4.1-3-g733a5