diff options
-rw-r--r-- | gifed/src/block/imagedescriptor.rs | 4 | ||||
-rw-r--r-- | gifed/src/block/indexedimage.rs | 9 | ||||
-rw-r--r-- | gifed/src/block/mod.rs | 4 | ||||
-rw-r--r-- | gifed/src/block/palette.rs (renamed from gifed/src/block/colortable.rs) | 37 | ||||
-rw-r--r-- | gifed/src/block/screendescriptor.rs | 4 | ||||
-rw-r--r-- | gifed/src/colorimage.rs | 4 | ||||
-rw-r--r-- | gifed/src/gif.rs | 11 | ||||
-rw-r--r-- | gifed/src/reader/mod.rs | 10 | ||||
-rw-r--r-- | gifed/src/writer/gifbuilder.rs | 6 | ||||
-rw-r--r-- | gifed/src/writer/imagebuilder.rs | 6 |
10 files changed, 37 insertions, 58 deletions
diff --git a/gifed/src/block/imagedescriptor.rs b/gifed/src/block/imagedescriptor.rs index 76efe6c..6bc0792 100644 --- a/gifed/src/block/imagedescriptor.rs +++ b/gifed/src/block/imagedescriptor.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use super::{packed::ImagePacked, ColorTable}; +use super::{packed::ImagePacked, Palette}; pub struct ImageDescriptor { pub left: u16, @@ -13,7 +13,7 @@ pub struct ImageDescriptor { impl ImageDescriptor { /// This data structure **does not** contain the color table, only a flag to /// indicate if one is present and it's size. - pub fn set_color_table_metadata<T: AsRef<ColorTable>>(&mut self, table: Option<T>) { + pub fn set_color_table_metadata<T: AsRef<Palette>>(&mut self, table: Option<T>) { if let Some(table) = table { let table = table.as_ref(); self.packed.set_color_table(true); diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs index 730a02c..4e72ea0 100644 --- a/gifed/src/block/indexedimage.rs +++ b/gifed/src/block/indexedimage.rs @@ -2,12 +2,12 @@ use std::convert::TryFrom; use weezl::encode::Encoder; -use super::{ColorTable, ImageDescriptor}; +use super::{ImageDescriptor, Palette}; use crate::LZW; pub struct IndexedImage { pub image_descriptor: ImageDescriptor, - pub local_color_table: Option<ColorTable>, + pub local_color_table: Option<Palette>, pub indicies: Vec<u8>, } @@ -36,8 +36,7 @@ impl IndexedImage { // Get the mcs while we write out the color table let mut mcs = if let Some(lct) = &self.local_color_table { - boxed = lct.into(); - out.extend_from_slice(&*boxed); + out.extend_from_slice(&lct.as_bytes()); lct.packed_len() + 1 } else { @@ -70,7 +69,7 @@ impl IndexedImage { pub struct CompressedImage { pub image_descriptor: ImageDescriptor, - pub local_color_table: Option<ColorTable>, + pub local_color_table: Option<Palette>, pub lzw_minimum_code_size: u8, pub blocks: Vec<Vec<u8>>, } diff --git a/gifed/src/block/mod.rs b/gifed/src/block/mod.rs index 28f8c86..06f3d9a 100644 --- a/gifed/src/block/mod.rs +++ b/gifed/src/block/mod.rs @@ -1,15 +1,15 @@ -mod colortable; pub mod extension; mod imagedescriptor; mod indexedimage; pub mod packed; +mod palette; mod screendescriptor; mod version; -pub use colortable::ColorTable; pub use imagedescriptor::ImageDescriptor; pub use indexedimage::CompressedImage; pub use indexedimage::IndexedImage; +pub use palette::Palette; pub use screendescriptor::ScreenDescriptor; pub use version::Version; diff --git a/gifed/src/block/colortable.rs b/gifed/src/block/palette.rs index 0d076c9..f43a67c 100644 --- a/gifed/src/block/colortable.rs +++ b/gifed/src/block/palette.rs @@ -6,11 +6,11 @@ use std::{ }; #[derive(Clone, Debug)] -pub struct ColorTable { +pub struct Palette { table: Vec<Color>, } -impl ColorTable { +impl Palette { pub fn new() -> Self { Self { table: vec![] } } @@ -18,6 +18,9 @@ impl ColorTable { /// Returns the number of colors in the color table as used by the packed /// fields in the Logical Screen Descriptor and Image Descriptor. You can /// get the actual size with the [`len`](struct.ColorTable.html#method.len) method. + /// + /// This value is equal to `log2([Palette::len]) - 1`. In other words, 2^(n + 1) will + /// give you the same value as [Palette::len]. (where `n` is the value returned) pub fn packed_len(&self) -> u8 { ((self.table.len() as f32).log2().ceil() - 1f32) as u8 } @@ -55,7 +58,7 @@ impl ColorTable { } } -impl Deref for ColorTable { +impl Deref for Palette { type Target = [Color]; fn deref(&self) -> &Self::Target { @@ -63,32 +66,14 @@ impl Deref for ColorTable { } } -impl AsRef<ColorTable> for ColorTable { - fn as_ref(&self) -> &ColorTable { +impl AsRef<Palette> for Palette { + fn as_ref(&self) -> &Palette { self } } -impl From<&ColorTable> for Box<[u8]> { - fn from(table: &ColorTable) -> Self { - let mut vec = vec![]; - - for color in table.iter() { - vec.extend_from_slice(&[color.r, color.g, color.b]); - } - - let packed_len = 2usize.pow(table.packed_len() as u32 + 1); - let padding = (packed_len as usize - table.len()) * 3; - if padding > 0 { - vec.extend_from_slice(&vec![0; padding]); - } - - vec.into_boxed_slice() - } -} - //TODO: TryFrom Vec<u8> (must be multiple of 3 len) and From Vec<Color> -impl TryFrom<&[u8]> for ColorTable { +impl TryFrom<&[u8]> for Palette { type Error = (); fn try_from(value: &[u8]) -> Result<Self, Self::Error> { @@ -105,7 +90,7 @@ impl TryFrom<&[u8]> for ColorTable { } } -impl TryFrom<Vec<Color>> for ColorTable { +impl TryFrom<Vec<Color>> for Palette { type Error = EncodingError; fn try_from(value: Vec<Color>) -> Result<Self, Self::Error> { @@ -117,7 +102,7 @@ impl TryFrom<Vec<Color>> for ColorTable { } } -impl TryFrom<Vec<(u8, u8, u8)>> for ColorTable { +impl TryFrom<Vec<(u8, u8, u8)>> for Palette { type Error = EncodingError; fn try_from(value: Vec<(u8, u8, u8)>) -> Result<Self, Self::Error> { diff --git a/gifed/src/block/screendescriptor.rs b/gifed/src/block/screendescriptor.rs index 6b318b5..766ad66 100644 --- a/gifed/src/block/screendescriptor.rs +++ b/gifed/src/block/screendescriptor.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use super::{packed::ScreenPacked, ColorTable}; +use super::{packed::ScreenPacked, Palette}; pub struct ScreenDescriptor { pub width: u16, @@ -23,7 +23,7 @@ impl ScreenDescriptor { /// This data structure **does not** contain the color table, only a flag to /// indicate if one is present and it's size. - pub fn set_color_table_metadata<T: AsRef<ColorTable>>(&mut self, table: Option<T>) { + pub fn set_color_table_metadata<T: AsRef<Palette>>(&mut self, table: Option<T>) { if let Some(table) = table { let table = table.as_ref(); self.packed.set_color_table(true); diff --git a/gifed/src/colorimage.rs b/gifed/src/colorimage.rs index 8b817de..8c42768 100644 --- a/gifed/src/colorimage.rs +++ b/gifed/src/colorimage.rs @@ -3,7 +3,7 @@ use std::{ ops::{Deref, DerefMut, Index}, }; -use crate::{block::ColorTable, color::Rgb, gif::Image, reader::DecodingError, Color}; +use crate::{block::Palette, color::Rgb, gif::Image, reader::DecodingError, Color}; /// An RGBA, full color image pub struct RgbaImage { @@ -21,7 +21,7 @@ impl RgbaImage { top: u16, left: u16, indicies: &[u8], - table: &ColorTable, + table: &Palette, transindex: Option<u8>, ) -> Result<Self, DecodingError> { let mut data = vec![0; width as usize * height as usize * 4]; diff --git a/gifed/src/gif.rs b/gifed/src/gif.rs index c4fb66c..3fc8e50 100644 --- a/gifed/src/gif.rs +++ b/gifed/src/gif.rs @@ -1,11 +1,11 @@ -use std::{convert::TryInto, fs::File, io::Write, iter::Peekable, path::Path, time::Duration}; +use std::{convert::TryInto, fs::File, io::Write, path::Path, time::Duration}; use crate::{ block::{ encode_block, extension::{DisposalMethod, GraphicControl}, packed::ImagePacked, - Block, ColorTable, ScreenDescriptor, Version, + Block, Palette, ScreenDescriptor, Version, }, colorimage::{RgbImage, RgbaImage}, reader::DecodingError, @@ -15,7 +15,7 @@ use crate::{ pub struct Gif { pub header: Version, pub screen_descriptor: ScreenDescriptor, - pub global_color_table: Option<ColorTable>, + pub global_color_table: Option<Palette>, pub blocks: Vec<Block>, // Trailer at the end of this struct is 0x3B // } @@ -35,8 +35,7 @@ impl Gif { // While we output the color table, grab it's length to use when // outputting the image, or 0 if we don't have a GCT let mcs = if let Some(gct) = &self.global_color_table { - boxed = gct.into(); - out.extend_from_slice(&*boxed); + out.extend_from_slice(&gct.as_bytes()); gct.packed_len() } else { @@ -128,7 +127,7 @@ pub struct Image<'a> { pub left_offset: u16, pub top_offset: u16, pub packed: ImagePacked, - pub palette: &'a ColorTable, + pub palette: &'a Palette, pub indicies: &'a [u8], pub blocks: &'a [Block], } diff --git a/gifed/src/reader/mod.rs b/gifed/src/reader/mod.rs index e852d26..bd7d08a 100644 --- a/gifed/src/reader/mod.rs +++ b/gifed/src/reader/mod.rs @@ -11,8 +11,7 @@ use std::{ use crate::{ block::{ extension::{Application, GraphicControl}, - Block, ColorTable, CompressedImage, ImageDescriptor, IndexedImage, ScreenDescriptor, - Version, + Block, CompressedImage, ImageDescriptor, IndexedImage, Palette, ScreenDescriptor, Version, }, color, Gif, }; @@ -65,16 +64,13 @@ impl GifReader { }) } - fn read_color_table( - reader: &mut SmartReader, - size: usize, - ) -> Result<ColorTable, DecodingError> { + fn read_color_table(reader: &mut SmartReader, size: usize) -> Result<Palette, DecodingError> { let buffer = reader .take(size as usize) .ok_or(DecodingError::UnexpectedEof)?; // We get the size from the screen descriptor. This should never return Err - Ok(ColorTable::try_from(&buffer[..]).unwrap()) + Ok(Palette::try_from(&buffer[..]).unwrap()) } fn read_block(reader: &mut SmartReader) -> Result<Option<Block>, DecodingError> { diff --git a/gifed/src/writer/gifbuilder.rs b/gifed/src/writer/gifbuilder.rs index 2141fc7..f02862b 100644 --- a/gifed/src/writer/gifbuilder.rs +++ b/gifed/src/writer/gifbuilder.rs @@ -1,7 +1,7 @@ use std::convert::TryInto; use crate::block::packed::ScreenPacked; -use crate::block::{Block, ColorTable, LoopCount, ScreenDescriptor, Version}; +use crate::block::{Block, LoopCount, Palette, ScreenDescriptor, Version}; use crate::writer::ImageBuilder; use crate::{EncodingError, Gif}; @@ -10,7 +10,7 @@ pub struct GifBuilder { width: u16, height: u16, background_color_index: u8, - global_color_table: Option<ColorTable>, + global_color_table: Option<Palette>, blocks: Vec<Block>, error: Option<EncodingError>, } @@ -28,7 +28,7 @@ impl GifBuilder { } } - pub fn palette(mut self, palette: ColorTable) -> Self { + pub fn palette(mut self, palette: Palette) -> Self { self.global_color_table = Some(palette); self } diff --git a/gifed/src/writer/imagebuilder.rs b/gifed/src/writer/imagebuilder.rs index c2ffd1a..1f1d67c 100644 --- a/gifed/src/writer/imagebuilder.rs +++ b/gifed/src/writer/imagebuilder.rs @@ -2,7 +2,7 @@ use crate::{ block::{ extension::{DisposalMethod, GraphicControl}, packed::ImagePacked, - ColorTable, ImageDescriptor, IndexedImage, Version, + ImageDescriptor, IndexedImage, Palette, Version, }, EncodingError, }; @@ -12,7 +12,7 @@ pub struct ImageBuilder { top_offset: u16, width: u16, height: u16, - color_table: Option<ColorTable>, + color_table: Option<Palette>, delay: u16, disposal_method: DisposalMethod, @@ -42,7 +42,7 @@ impl ImageBuilder { self } - pub fn palette(mut self, table: ColorTable) -> Self { + pub fn palette(mut self, table: Palette) -> Self { self.color_table = Some(table); self } |