diff options
author | Genny <gen@nyble.dev> | 2021-10-10 14:04:56 -0500 |
---|---|---|
committer | Genny <gen@nyble.dev> | 2021-10-10 14:04:56 -0500 |
commit | 9fc5f0b1259fff2b999347f9ea326d13c222a16e (patch) | |
tree | f8194e57c86caa3b6519e30a84a0d5919c9ad5a0 | |
parent | a4c967c5bd132d2bf9f2b151ee2ca1f4aa05f4a4 (diff) | |
download | gifed-9fc5f0b1259fff2b999347f9ea326d13c222a16e.tar.gz gifed-9fc5f0b1259fff2b999347f9ea326d13c222a16e.zip |
Fix builder api, add save method
-rw-r--r-- | src/gif.rs | 20 | ||||
-rw-r--r-- | src/writer/gifbuilder.rs | 42 |
2 files changed, 44 insertions, 18 deletions
diff --git a/src/gif.rs b/src/gif.rs index 36e64dd..5b58ed1 100644 --- a/src/gif.rs +++ b/src/gif.rs @@ -1,4 +1,9 @@ -use crate::{block::{extension::Extension, Block, ColorTable, ScreenDescriptor, Version}, writer::GifBuilder}; +use std::{fs::File, io::Write, path::Path}; + +use crate::{ + block::{extension::Extension, Block, ColorTable, ScreenDescriptor, Version}, + writer::GifBuilder, +}; pub struct Gif { pub header: Version, pub screen_descriptor: ScreenDescriptor, @@ -49,6 +54,10 @@ impl Gif { out } + + pub fn save<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> { + File::create(path.as_ref())?.write_all(&self.to_vec()) + } } /*struct FrameIterator { @@ -150,11 +159,9 @@ pub mod gif { .palette(colortable.try_into().unwrap()) .indicies(&indicies), ) - .unwrap() - .image(ImageBuilder::new(4, 4).indicies(&indicies)) - .unwrap(); + .image(ImageBuilder::new(4, 4).indicies(&indicies)); - let bytes = actual.build().to_vec(); + let bytes = actual.build().unwrap().to_vec(); assert_eq!(bytes, expected_out); } @@ -179,10 +186,9 @@ pub mod gif { .disposal_method(DisposalMethod::RestoreBackground) .delay(64), ) - .unwrap() .image(ImageBuilder::new(4, 4).indicies(&indicies)) - .unwrap() .build() + .unwrap() .to_vec(); std::fs::File::create("ah.gif") diff --git a/src/writer/gifbuilder.rs b/src/writer/gifbuilder.rs index ec3c304..1466f76 100644 --- a/src/writer/gifbuilder.rs +++ b/src/writer/gifbuilder.rs @@ -11,6 +11,7 @@ pub struct GifBuilder { background_color_index: u8, global_color_table: Option<ColorTable>, blocks: Vec<Block>, + error: Option<EncodingError>, } impl GifBuilder { @@ -22,6 +23,7 @@ impl GifBuilder { background_color_index: 0, global_color_table: None, blocks: vec![], + error: None, } } @@ -30,16 +32,24 @@ impl GifBuilder { self } - pub fn background_index(mut self, ind: u8) -> Result<Self, EncodingError> { + pub fn background_index(mut self, ind: u8) -> Self { + if self.error.is_some() { + return self; + } + if self.global_color_table.is_none() { - Err(EncodingError::NoColorTable) + self.error = Some(EncodingError::NoColorTable); } else { self.background_color_index = ind; - Ok(self) } + self } - pub fn image(mut self, ib: ImageBuilder) -> Result<Self, EncodingError> { + pub fn image(mut self, ib: ImageBuilder) -> Self { + if self.error.is_some() { + return self; + } + if ib.required_version() == Version::Gif89a { self.version = Version::Gif89a; } @@ -48,8 +58,12 @@ impl GifBuilder { self.blocks.push(Block::Extension(gce.into())); } - self.blocks.push(Block::IndexedImage(ib.build()?)); - Ok(self) + match ib.build() { + Ok(image) => self.blocks.push(Block::IndexedImage(image)), + Err(e) => self.error = Some(e), + } + + self } /*pub fn extension(mut self, ext: Extension) -> Self { @@ -57,11 +71,17 @@ impl GifBuilder { self }*/ - pub fn repeat(&mut self, count: u16) { - self.blocks.push(Block::Extension(Extension::Looping(count))) + pub fn repeat(mut self, count: u16) -> Self { + self.blocks + .push(Block::Extension(Extension::Looping(count))); + self } - pub fn build(self) -> Gif { + pub fn build(self) -> Result<Gif, EncodingError> { + if let Some(error) = self.error { + return Err(error); + } + let mut lsd = ScreenDescriptor { width: self.width, height: self.height, @@ -76,11 +96,11 @@ impl GifBuilder { lsd.set_color_table_size((gct.len() - 1) as u8); } - Gif { + Ok(Gif { header: self.version, screen_descriptor: lsd, global_color_table: self.global_color_table, blocks: self.blocks, - } + }) } } |