From 743888ae0c1038a92f4a0b64709a313eba70c887 Mon Sep 17 00:00:00 2001 From: Genny Date: Wed, 10 Mar 2021 01:26:24 -0600 Subject: Refactor code and get ready for extension blocks --- src/block/block.rs | 8 ++------ src/block/image.rs | 47 ----------------------------------------------- src/block/indexedimage.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/block/mod.rs | 4 ++-- 4 files changed, 49 insertions(+), 55 deletions(-) delete mode 100644 src/block/image.rs create mode 100644 src/block/indexedimage.rs (limited to 'src/block') diff --git a/src/block/block.rs b/src/block/block.rs index 95ff12e..329361f 100644 --- a/src/block/block.rs +++ b/src/block/block.rs @@ -1,9 +1,5 @@ -use crate::block::Version; - -use super::ScreenDescriptor; - +use super::IndexedImage; pub enum Block { - Version(Version), - LogicalScreenDescriptor(ScreenDescriptor) + IndexedImage(IndexedImage) } \ No newline at end of file diff --git a/src/block/image.rs b/src/block/image.rs deleted file mode 100644 index 07f9555..0000000 --- a/src/block/image.rs +++ /dev/null @@ -1,47 +0,0 @@ -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 diff --git a/src/block/indexedimage.rs b/src/block/indexedimage.rs new file mode 100644 index 0000000..ae2da06 --- /dev/null +++ b/src/block/indexedimage.rs @@ -0,0 +1,45 @@ +use crate::LZW; +use super::{ColorTable, ImageDescriptor}; + +pub struct IndexedImage { + pub image_descriptor: ImageDescriptor, + pub local_color_table: Option, + pub indicies: Vec +} + +impl IndexedImage { + 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); + + // 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 diff --git a/src/block/mod.rs b/src/block/mod.rs index 049b39a..a0c2454 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -1,13 +1,13 @@ mod block; mod colortable; -mod image; +mod indexedimage; mod imagedescriptor; mod screendescriptor; mod version; pub use block::Block; pub use colortable::ColorTable; -pub use image::Image; +pub use indexedimage::IndexedImage; pub use imagedescriptor::ImageDescriptor; pub use screendescriptor::ScreenDescriptor; pub use version::Version; \ No newline at end of file -- cgit 1.4.1-3-g733a5