diff options
author | Devon Sawatsky <novedevo@gmail.com> | 2023-12-24 00:26:44 -0800 |
---|---|---|
committer | Devon Sawatsky <novedevo@gmail.com> | 2023-12-24 00:26:44 -0800 |
commit | 2230e8d37c16e864821db23636f2bad5c14d8d3c (patch) | |
tree | 95780a49dd4f0a2b1b498b441f163b82167d52fe | |
parent | d5385e8bee605c51273f84a3983aa5f55e24f777 (diff) | |
download | gifed-2230e8d37c16e864821db23636f2bad5c14d8d3c.tar.gz gifed-2230e8d37c16e864821db23636f2bad5c14d8d3c.zip |
add optional rgb feature for improved interoperability with other crates
-rw-r--r-- | gifed/Cargo.toml | 7 | ||||
-rw-r--r-- | gifed/src/block/palette.rs | 6 | ||||
-rw-r--r-- | gifed/src/gif_builder.rs | 14 | ||||
-rw-r--r-- | gifed/src/lib.rs | 9 |
4 files changed, 25 insertions, 11 deletions
diff --git a/gifed/Cargo.toml b/gifed/Cargo.toml index 528f670..9c9743c 100644 --- a/gifed/Cargo.toml +++ b/gifed/Cargo.toml @@ -9,11 +9,16 @@ repository = "https://github.com/genuinebyte/gifed" [dependencies] bitvec = "1.0.1" -colorsquash = { git = "https://github.com/gennyble/colorsquash", version = "0.1.0", optional = true } +colorsquash = { git = "https://github.com/novedevo/colorsquash", version = "0.2.0", optional = true } +rgb = {version="0.8.37", optional=true} weezl = "0.1.5" [features] weezl-encode = [] +default = [ + "colorsquash", + "rgb" +] [dev-dependencies] rand = "0.8.5" diff --git a/gifed/src/block/palette.rs b/gifed/src/block/palette.rs index 3cbcaaa..1c660df 100644 --- a/gifed/src/block/palette.rs +++ b/gifed/src/block/palette.rs @@ -48,9 +48,9 @@ impl Palette { self.table.get(index as usize).copied() } - pub fn from_color<C: AsRef<Color>>(&self, color: C) -> Option<u8> { + pub fn from_color(&self, color: Color) -> Option<u8> { for (i, &c) in self.table.iter().enumerate() { - if c == *color.as_ref() { + if c == color { return Some(i as u8); } } @@ -201,7 +201,7 @@ mod test { fn test_n_with_padding_range(real_count_low: u8, real_count_high: u8, next_padstop: usize) { for x in real_count_low..=real_count_high { - test_n_with_padding(x as usize, (next_padstop as usize - x as usize) * 3) + test_n_with_padding(x as usize, (next_padstop - x as usize) * 3) } } diff --git a/gifed/src/gif_builder.rs b/gifed/src/gif_builder.rs index e701f6e..6951961 100644 --- a/gifed/src/gif_builder.rs +++ b/gifed/src/gif_builder.rs @@ -1,10 +1,10 @@ use std::convert::TryFrom; #[cfg(feature = "colorsquash")] -use colorsquash::{Squasher, SquasherBuilder}; +use colorsquash::Squasher; use crate::{ - block::{packed::ScreenPacked, Palette, ScreenDescriptor, Version}, + block::{Palette, ScreenDescriptor, Version}, writer::ImageBuilder, Color, Gif, }; @@ -111,14 +111,18 @@ impl From<Vec<Vec<Color>>> for Frame { impl Frame { #[cfg(feature = "colorsquash")] pub fn optimize_palette(&mut self) { + #[cfg(feature = "rgb")] + let image_bytes = self.image.clone().into_iter().flatten().collect::<Vec<_>>(); + #[cfg(not(feature = "rgb"))] let image_bytes = self .image .iter() .flat_map(|row| row.iter().flat_map(|color| [color.r, color.g, color.b])) .collect::<Vec<_>>(); - let squasher = SquasherBuilder::default() - .max_colors(255u8) - .build(image_bytes.as_slice()); + #[cfg(feature = "rgb")] + let squasher = Squasher::new(255u8, image_bytes.as_slice()); + #[cfg(not(feature = "rgb"))] + let squasher = Squasher::new_raw(255u8, image_bytes.as_slice()); let pal = Palette::try_from(squasher.palette_bytes().as_slice()).unwrap(); self.set_palette(pal) } diff --git a/gifed/src/lib.rs b/gifed/src/lib.rs index cc1e69f..9e4458f 100644 --- a/gifed/src/lib.rs +++ b/gifed/src/lib.rs @@ -1,4 +1,3 @@ -mod color; mod gif; mod lzw; @@ -10,10 +9,16 @@ pub mod writer; pub use reader::DecodeError; pub use writer::EncodeError; -pub use color::Color; pub use gif::{Gif, Image}; pub use lzw::LZW; +#[cfg(feature = "rgb")] +pub type Color = rgb::RGB8; +#[cfg(not(feature = "rgb"))] +mod color; +#[cfg(not(feature = "rgb"))] +pub use color::Color; + /// Perform the algorithm to get the length of a color table from /// the value of the packed field. The max value here is 256 pub(crate) fn packed_to_color_table_length(packed: u8) -> usize { |