diff options
Diffstat (limited to 'gifed/src/block')
-rw-r--r-- | gifed/src/block/indexedimage.rs | 8 | ||||
-rw-r--r-- | gifed/src/block/mod.rs | 10 | ||||
-rw-r--r-- | gifed/src/block/palette.rs | 4 | ||||
-rw-r--r-- | gifed/src/block/screendescriptor.rs | 16 | ||||
-rw-r--r-- | gifed/src/block/version.rs | 6 |
5 files changed, 32 insertions, 12 deletions
diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs index 25eadbe..d27d463 100644 --- a/gifed/src/block/indexedimage.rs +++ b/gifed/src/block/indexedimage.rs @@ -33,9 +33,13 @@ impl IndexedImage { /// LZW Minimum Code Size here. It is equal to the value of [Palette::packed_len], but /// must also be at least 2. pub fn compress(self, lzw_code_size: Option<u8>) -> Result<CompressedImage, EncodeError> { - //TODO: gen- The old code had a +1 here. Why? + // gen- The old code had a +1 here. Why? + // In the spec, under the section for the Logical Screen Descriptor, it + // mentions that the size in the packed field is calculated with + // 2 ^ (packed + 1) and the code size is supposed to be the "number + // of color bits", which I guess is the exponent? let mcs = match self.local_color_table.as_ref() { - Some(palette) => palette.packed_len(), + Some(palette) => palette.lzw_code_size(), None => match lzw_code_size { None => return Err(EncodeError::InvalidCodeSize { lzw_code_size: 0 }), Some(mcs) => mcs, diff --git a/gifed/src/block/mod.rs b/gifed/src/block/mod.rs index 99075cf..5eb8a78 100644 --- a/gifed/src/block/mod.rs +++ b/gifed/src/block/mod.rs @@ -38,7 +38,15 @@ pub enum LoopCount { Number(u16), } -pub(crate) fn encode_block(mcs: u8, block: &Block) -> Vec<u8> { +impl LoopCount { + /// Set a fixed loop count. A value of 0 means forever, which you should + /// probably use [LoopCount::Forever] for. + pub fn count(count: u16) -> Self { + Self::Number(count) + } +} + +pub(crate) fn encode_block(block: &Block) -> Vec<u8> { match block { Block::CompressedImage(img) => img.as_bytes(), Block::GraphicControlExtension(_) => encode_extension(block), diff --git a/gifed/src/block/palette.rs b/gifed/src/block/palette.rs index 8f58b40..f2f4de3 100644 --- a/gifed/src/block/palette.rs +++ b/gifed/src/block/palette.rs @@ -25,6 +25,10 @@ impl Palette { ((self.table.len() as f32).log2().ceil() - 1f32) as u8 } + pub fn lzw_code_size(&self) -> u8 { + self.packed_len() + 1 + } + /// Returns the number of items in the table pub fn len(&self) -> usize { self.table.len() diff --git a/gifed/src/block/screendescriptor.rs b/gifed/src/block/screendescriptor.rs index 766ad66..aaeea53 100644 --- a/gifed/src/block/screendescriptor.rs +++ b/gifed/src/block/screendescriptor.rs @@ -42,18 +42,16 @@ impl ScreenDescriptor { pub fn color_table_len(&self) -> usize { crate::packed_to_color_table_length(self.packed.color_table_size()) } -} -impl From<&ScreenDescriptor> for Box<[u8]> { - fn from(lsd: &ScreenDescriptor) -> Self { + pub fn as_bytes(&self) -> Vec<u8> { let mut vec = vec![]; - vec.extend_from_slice(&lsd.width.to_le_bytes()); - vec.extend_from_slice(&lsd.height.to_le_bytes()); - vec.push(lsd.packed.raw); - vec.push(lsd.background_color_index); - vec.push(lsd.pixel_aspect_ratio); + vec.extend_from_slice(&self.width.to_le_bytes()); + vec.extend_from_slice(&self.height.to_le_bytes()); + vec.push(self.packed.raw); + vec.push(self.background_color_index); + vec.push(self.pixel_aspect_ratio); - vec.into_boxed_slice() + vec } } diff --git a/gifed/src/block/version.rs b/gifed/src/block/version.rs index 0171ad4..c26ebb5 100644 --- a/gifed/src/block/version.rs +++ b/gifed/src/block/version.rs @@ -6,6 +6,12 @@ pub enum Version { Gif89a, } +impl Version { + pub fn as_bytes(&self) -> &[u8] { + self.into() + } +} + impl From<&Version> for &[u8] { fn from(version: &Version) -> Self { match version { |