diff options
author | Genny <gen@nyble.dev> | 2023-02-20 09:07:17 -0600 |
---|---|---|
committer | Genny <gen@nyble.dev> | 2023-02-20 09:07:17 -0600 |
commit | b3c5fa87301399792e91edde1ac821d112174851 (patch) | |
tree | d17381fe7baad91e245e161eb42aa5e169b2eec4 | |
parent | a9a60e13c8ea8dbf436eb66aab88b77fa0ad0cf2 (diff) | |
download | gifed-b3c5fa87301399792e91edde1ac821d112174851.tar.gz gifed-b3c5fa87301399792e91edde1ac821d112174851.zip |
correctly write palette padding
-rw-r--r-- | gifed/src/block/palette.rs | 17 | ||||
-rw-r--r-- | gifed/src/writer/mod.rs | 2 |
2 files changed, 18 insertions, 1 deletions
diff --git a/gifed/src/block/palette.rs b/gifed/src/block/palette.rs index f2f4de3..d5cc696 100644 --- a/gifed/src/block/palette.rs +++ b/gifed/src/block/palette.rs @@ -15,6 +15,7 @@ impl Palette { Self { table: vec![] } } + //FIXME: gen- Second paragraph is incorrect /// 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. @@ -29,11 +30,17 @@ impl Palette { self.packed_len() + 1 } - /// Returns the number of items in the table + /// Returns the number of colours in the pallette pub fn len(&self) -> usize { self.table.len() } + /// Returns the number of items that the decoder *thinks* is in the palette. + /// This is 2^(n + 1) where n = [Palette::packed_len] + pub fn computed_len(&self) -> usize { + 2usize.pow(self.packed_len() as u32 + 1) + } + /// Pushes a color on to the end of the table pub fn push(&mut self, color: Color) { self.table.push(color); @@ -52,6 +59,14 @@ impl Palette { None } + /// How many padding bytes we need to write. + /// We need to pad the colour table because the size must be a power of two. + //TODO: gen- better docs + pub fn padding(&self) -> usize { + let comp = self.computed_len(); + (comp as usize - self.len()) * 3 + } + pub fn as_bytes(&self) -> Vec<u8> { let mut bytes = Vec::with_capacity(self.table.len() * 3); for color in &self.table { diff --git a/gifed/src/writer/mod.rs b/gifed/src/writer/mod.rs index 394d85f..493514b 100644 --- a/gifed/src/writer/mod.rs +++ b/gifed/src/writer/mod.rs @@ -47,7 +47,9 @@ impl<W: Write> Writer<W> { this.write_all(&screen_descriptor.as_bytes())?; if let Some(palette) = this.global_palette.as_ref() { + let padding: Vec<u8> = std::iter::repeat(0u8).take(palette.padding()).collect(); this.write_all(&palette.as_bytes())?; + this.write_all(&padding)?; } Ok(this) |