From d19e7b983ec034004cb27ab647634474ada10a18 Mon Sep 17 00:00:00 2001 From: Genny Date: Fri, 25 Nov 2022 17:58:32 -0600 Subject: Add decompress --- gifed/src/block/indexedimage.rs | 24 +++++++++++++++++++++++- gifed/src/writer/gifbuilder.rs | 5 +++++ giftool/src/main.rs | 2 -- 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, @@ -90,4 +91,25 @@ impl CompressedImage { ret } + + pub fn decompress(self) -> Result { + let CompressedImage { + image_descriptor, + local_color_table, + lzw_code_size, + blocks, + } = self; + + let data: Vec = 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); -- cgit 1.4.1-3-g733a5