diff options
author | Genny <gen@nyble.dev> | 2020-11-06 23:33:01 -0600 |
---|---|---|
committer | Genny <gen@nyble.dev> | 2020-11-06 23:33:01 -0600 |
commit | c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc (patch) | |
tree | 6ee047a5929fbd92a664c041b0596b53f58d59aa /src/components/image.rs | |
parent | 85b5d7de253f5f91dbca4047196e005856fd52de (diff) | |
download | gifed-c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc.tar.gz gifed-c54872f011c135bbbc963f4a2b6b1d8ee4fa92bc.zip |
Add gif components
Diffstat (limited to 'src/components/image.rs')
-rw-r--r-- | src/components/image.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/components/image.rs b/src/components/image.rs new file mode 100644 index 0000000..07f9555 --- /dev/null +++ b/src/components/image.rs @@ -0,0 +1,47 @@ +use crate::LZW; +use super::{ColorTable, ImageDescriptor}; + +pub struct Image { + pub image_descriptor: ImageDescriptor, + pub local_color_table: Option<ColorTable>, + pub indicies: Vec<u8> +} + +impl Image { + pub fn as_boxed_slice(&self, minimum_code_size: u8) -> Box<[u8]> { + let mut out = vec![]; + + let mut boxed: Box<[u8]> = (&self.image_descriptor).into(); + out.extend_from_slice(&*boxed); + + // Table based image data // + + // 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); + + lct.packed_len() + } else { + minimum_code_size + }; + + if mcs < 2 { + mcs = 2; // Must be true: 0 <= mcs <= 8 + } + + // First write out the MCS + out.push(mcs); + + let compressed = LZW::encode(mcs, &self.indicies); + + for chunk in compressed.chunks(255) { + out.push(chunk.len() as u8); + out.extend_from_slice(chunk); + } + // Data block length 0 to indicate an end + out.push(0x00); + + out.into_boxed_slice() + } +} \ No newline at end of file |