From a368be4dfb3c1f75f6bcfdc297fe0372fb5f6092 Mon Sep 17 00:00:00 2001 From: Genny Date: Mon, 8 Mar 2021 21:02:50 -0600 Subject: Rename a few things --- src/block/image.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/block/image.rs (limited to 'src/block/image.rs') diff --git a/src/block/image.rs b/src/block/image.rs new file mode 100644 index 0000000..07f9555 --- /dev/null +++ b/src/block/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, + pub indicies: Vec +} + +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 -- cgit 1.4.1-3-g733a5