about summary refs log tree commit diff
path: root/src/block/indexedimage.rs
diff options
context:
space:
mode:
authorGenny <gen@nyble.dev>2021-03-10 01:26:24 -0600
committerGenny <gen@nyble.dev>2021-03-10 01:26:24 -0600
commit743888ae0c1038a92f4a0b64709a313eba70c887 (patch)
tree3b1f6e9fbe2524d3177a4ed12e3486a4fb009460 /src/block/indexedimage.rs
parenta368be4dfb3c1f75f6bcfdc297fe0372fb5f6092 (diff)
downloadgifed-743888ae0c1038a92f4a0b64709a313eba70c887.tar.gz
gifed-743888ae0c1038a92f4a0b64709a313eba70c887.zip
Refactor code and get ready for extension blocks
Diffstat (limited to 'src/block/indexedimage.rs')
-rw-r--r--src/block/indexedimage.rs45
1 files changed, 45 insertions, 0 deletions
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<ColorTable>,
+	pub indicies: Vec<u8>
+}
+
+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