about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGenny <gen@nyble.dev>2022-11-25 17:58:32 -0600
committerGenny <gen@nyble.dev>2022-11-25 17:58:32 -0600
commitd19e7b983ec034004cb27ab647634474ada10a18 (patch)
treec0ec08bf03bc26fc0f9a91e8cf29ff5bd9cb0e6e
parent66d77dddef9928e52dfc7a99405ef1f8d934cc6a (diff)
downloadgifed-d19e7b983ec034004cb27ab647634474ada10a18.tar.gz
gifed-d19e7b983ec034004cb27ab647634474ada10a18.zip
Add decompress
-rw-r--r--gifed/src/block/indexedimage.rs24
-rw-r--r--gifed/src/writer/gifbuilder.rs5
-rw-r--r--giftool/src/main.rs2
3 files changed, 28 insertions, 3 deletions
diff --git a/gifed/src/block/indexedimage.rs b/gifed/src/block/indexedimage.rs
index d27d463..64b251e 100644
--- a/gifed/src/block/indexedimage.rs
+++ b/gifed/src/block/indexedimage.rs
@@ -1,6 +1,6 @@
 use weezl::encode::Encoder;
 
-use crate::EncodeError;
+use crate::{reader::DecodeError, EncodeError};
 
 use super::{ImageDescriptor, Palette};
 
@@ -66,6 +66,7 @@ impl IndexedImage {
 	}
 }
 
+#[derive(Clone, Debug)]
 pub struct CompressedImage {
 	pub image_descriptor: ImageDescriptor,
 	pub local_color_table: Option<Palette>,
@@ -90,4 +91,25 @@ impl CompressedImage {
 
 		ret
 	}
+
+	pub fn decompress(self) -> Result<IndexedImage, DecodeError> {
+		let CompressedImage {
+			image_descriptor,
+			local_color_table,
+			lzw_code_size,
+			blocks,
+		} = self;
+
+		let data: Vec<u8> = blocks.into_iter().map(<_>::into_iter).flatten().collect();
+
+		//TODO: remove unwrap
+		let mut decompressor = weezl::decode::Decoder::new(weezl::BitOrder::Lsb, lzw_code_size);
+		let indicies = decompressor.decode(&data).unwrap();
+
+		Ok(IndexedImage {
+			image_descriptor,
+			local_color_table,
+			indicies,
+		})
+	}
 }
diff --git a/gifed/src/writer/gifbuilder.rs b/gifed/src/writer/gifbuilder.rs
index 4ea88ec..1d3ff1a 100644
--- a/gifed/src/writer/gifbuilder.rs
+++ b/gifed/src/writer/gifbuilder.rs
@@ -48,6 +48,11 @@ impl GifBuilder {
 		self
 	}
 
+	pub fn block(mut self, block: Block) -> Self {
+		self.blocks.push(BuildBlock::Block(block));
+		self
+	}
+
 	pub fn repeat(mut self, count: LoopCount) -> Self {
 		self.blocks
 			.push(BuildBlock::Block(Block::LoopingExtension(count)));
diff --git a/giftool/src/main.rs b/giftool/src/main.rs
index dcfe113..1482185 100644
--- a/giftool/src/main.rs
+++ b/giftool/src/main.rs
@@ -1,7 +1,5 @@
 use std::{fs::File, ops::Deref, path::PathBuf};
 
-use gifed::{reader::GifReader, Gif};
-
 fn main() {
 	let subcommand = std::env::args().nth(1);