diff options
author | gennyble <gen@nyble.dev> | 2024-01-02 18:18:59 -0600 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-01-02 18:18:59 -0600 |
commit | 4d7093c266bf9870648b9aaa4b9dd36561a69dcc (patch) | |
tree | 1be757f76c5a1b85791e8c567f73ddce1eb842bc | |
parent | 46e170f9c8fe432469b91f64cde2ce5f64d49091 (diff) | |
download | gifed-4d7093c266bf9870648b9aaa4b9dd36561a69dcc.tar.gz gifed-4d7093c266bf9870648b9aaa4b9dd36561a69dcc.zip |
gif_build.rs renamed to videogif.rs
also checking frame's are the right size now
-rw-r--r-- | gifed/src/block/palette.rs | 5 | ||||
-rw-r--r-- | gifed/src/lib.rs | 4 | ||||
-rw-r--r-- | gifed/src/videogif.rs (renamed from gifed/src/gif_builder.rs) | 29 |
3 files changed, 27 insertions, 11 deletions
diff --git a/gifed/src/block/palette.rs b/gifed/src/block/palette.rs index 436e698..0dc1686 100644 --- a/gifed/src/block/palette.rs +++ b/gifed/src/block/palette.rs @@ -51,9 +51,8 @@ impl Palette { pub fn from_color(&self, color: Color) -> Option<u8> { self.table .iter() - .enumerate() - .find(|(_, c)| **c == color) - .map(|(i, _)| i as u8) + .position(|c| *c == color) + .map(|idx| idx as u8) } /// How many padding bytes we need to write. diff --git a/gifed/src/lib.rs b/gifed/src/lib.rs index 7d35dee..2ed3b9b 100644 --- a/gifed/src/lib.rs +++ b/gifed/src/lib.rs @@ -2,9 +2,9 @@ mod gif; mod lzw; pub mod block; -#[cfg(feature = "videoish")] -pub mod gif_builder; pub mod reader; +#[cfg(feature = "videoish")] +pub mod videogif; pub mod writer; pub use reader::DecodeError; diff --git a/gifed/src/gif_builder.rs b/gifed/src/videogif.rs index 53b1929..27ec798 100644 --- a/gifed/src/gif_builder.rs +++ b/gifed/src/videogif.rs @@ -30,8 +30,25 @@ impl VideoGif { self.framerate = Some(100 / framerate); } + /// Adds a frame to the gif. + /// + /// # Panic + /// Panics if the provided [Frame]'s length is not the same as the gif's + /// width * height. pub fn add_frame<F: Into<Frame>>(&mut self, frame: F) { - self.frames.push(frame.into()) + let frame = frame.into(); + let frame_area = frame.image_indices.len(); + let gif_area = self.width as usize * self.height as usize; + + if frame_area != gif_area { + //TODO: gen- Result instead of panic? + panic!( + "frame has a length of {frame_area} but VideoGif expected {gif_area} ({} * {})", + self.width, self.height + ); + } + + self.frames.push(frame) } #[rustfmt::skip] // it was doing things i did not like @@ -58,9 +75,9 @@ impl VideoGif { } pub struct Frame { - ///indices into the palette + /// indices into the palette image_indices: Vec<u8>, - ///in hundredths of a second + /// in hundredths of a second interval: Option<u16>, palette: Palette, } @@ -70,16 +87,16 @@ impl From<&[Color]> for Frame { let flat_rgba = flat.as_rgba(); let quant = NeuQuant::new(1, 256, flat_rgba.as_bytes()); - let mut indicies = vec![0; flat.len()]; + let mut indices = vec![0; flat.len()]; for (image_idx, px) in flat.iter().enumerate() { let color_idx = quant.index_of(&[px.r, px.g, px.b, 255]); - indicies[image_idx] = color_idx as u8; + indices[image_idx] = color_idx as u8; } let palette = Palette::try_from(quant.color_map_rgb().as_slice()).unwrap(); Self { - image_indices: indicies, + image_indices: indices, interval: None, palette, } |