diff options
Diffstat (limited to 'src/writer')
-rw-r--r-- | src/writer/gifbuilder.rs | 115 | ||||
-rw-r--r-- | src/writer/imagebuilder.rs | 112 |
2 files changed, 114 insertions, 113 deletions
diff --git a/src/writer/gifbuilder.rs b/src/writer/gifbuilder.rs index 7e5138a..d16be56 100644 --- a/src/writer/gifbuilder.rs +++ b/src/writer/gifbuilder.rs @@ -1,73 +1,74 @@ -use crate::block::{Block, ColorTable, ScreenDescriptor, Version, extension::Extension}; +use crate::block::{extension::Extension, Block, ColorTable, ScreenDescriptor, Version}; use crate::writer::ImageBuilder; use crate::Gif; pub struct GifBuilder { - version: Version, - width: u16, - height: u16, - background_color_index: u8, - global_color_table: Option<ColorTable>, - blocks: Vec<Block> + version: Version, + width: u16, + height: u16, + background_color_index: u8, + global_color_table: Option<ColorTable>, + blocks: Vec<Block>, } impl GifBuilder { - pub fn new(version: Version, width: u16, height: u16) -> Self { - Self { - version, - width, - height, - background_color_index: 0, - global_color_table: None, - blocks: vec![] - } - } + pub fn new(version: Version, width: u16, height: u16) -> Self { + Self { + version, + width, + height, + background_color_index: 0, + global_color_table: None, + blocks: vec![], + } + } - pub fn global_color_table(mut self, table: ColorTable) -> Self { - self.global_color_table = Some(table); + pub fn global_color_table(mut self, table: ColorTable) -> Self { + self.global_color_table = Some(table); - self - } + self + } - pub fn background_color_index(mut self, ind: u8) -> Self { - if self.global_color_table.is_none() { - //TODO: Throw error or let it go by, who knows - panic!("Setting background color index with noGCT!"); - } + pub fn background_color_index(mut self, ind: u8) -> Self { + if self.global_color_table.is_none() { + //TODO: Throw error or let it go by, who knows + panic!("Setting background color index with noGCT!"); + } - self.background_color_index = ind; - self - } + self.background_color_index = ind; + self + } - pub fn image(mut self, ib: ImageBuilder) -> Self { - self.blocks.push(Block::IndexedImage(ib.build())); - self - } + pub fn image(mut self, ib: ImageBuilder) -> Self { + self.blocks.push(Block::IndexedImage(ib.build())); + self + } - pub fn extension(mut self, ext: Extension) -> Self { - self.blocks.push(Block::Extension(ext)); - self - } + pub fn extension(mut self, ext: Extension) -> Self { + self.blocks.push(Block::Extension(ext)); + self + } - pub fn build(self) -> Gif { - let mut lsd = ScreenDescriptor { - width: self.width, - height: self.height, - packed: 0, // Set later - background_color_index: self.background_color_index, - pixel_aspect_ratio: 0 //TODO: Allow configuring - }; + pub fn build(self) -> Gif { + let mut lsd = ScreenDescriptor { + width: self.width, + height: self.height, + packed: 0, // Set later + background_color_index: self.background_color_index, + pixel_aspect_ratio: 0, //TODO: Allow configuring + }; - if let Some(gct) = &self.global_color_table { - lsd.color_table_present(true); - lsd.color_table_size(gct.len() as u8); - } + if let Some(gct) = &self.global_color_table { + println!("build {}", gct.len()); + lsd.set_color_table_present(true); + lsd.set_color_table_size((gct.len() - 1) as u8); + } - Gif { - header: self.version, - screen_descriptor: lsd, - global_color_table: self.global_color_table, - blocks: self.blocks - } - } -} \ No newline at end of file + Gif { + header: self.version, + screen_descriptor: lsd, + global_color_table: self.global_color_table, + blocks: self.blocks, + } + } +} diff --git a/src/writer/imagebuilder.rs b/src/writer/imagebuilder.rs index f2156ac..d38687e 100644 --- a/src/writer/imagebuilder.rs +++ b/src/writer/imagebuilder.rs @@ -1,71 +1,71 @@ -use crate::block::{ColorTable, IndexedImage, ImageDescriptor}; +use crate::block::{ColorTable, ImageDescriptor, IndexedImage}; pub struct ImageBuilder { - left_offset: u16, - top_offset: u16, - width: u16, - height: u16, - color_table: Option<ColorTable>, - indicies: Vec<u8> + left_offset: u16, + top_offset: u16, + width: u16, + height: u16, + color_table: Option<ColorTable>, + indicies: Vec<u8>, } impl ImageBuilder { - pub fn new(width: u16, height: u16) -> Self { - Self { - left_offset: 0, - top_offset: 0, - width, - height, - color_table: None, - indicies: vec![] - } - } + pub fn new(width: u16, height: u16) -> Self { + Self { + left_offset: 0, + top_offset: 0, + width, + height, + color_table: None, + indicies: vec![], + } + } - pub fn offsets(mut self, left_offset: u16, top_offset: u16) -> Self { - self.left_offset = left_offset; - self.top_offset = top_offset; - self - } + pub fn offsets(mut self, left_offset: u16, top_offset: u16) -> Self { + self.left_offset = left_offset; + self.top_offset = top_offset; + self + } - pub fn left_offset(mut self, offset: u16) -> Self { - self.left_offset = offset; - self - } + pub fn left_offset(mut self, offset: u16) -> Self { + self.left_offset = offset; + self + } - pub fn top_offset(mut self, offset: u16) -> Self { - self.top_offset = offset; - self - } + pub fn top_offset(mut self, offset: u16) -> Self { + self.top_offset = offset; + self + } - pub fn color_table(mut self, table: ColorTable) -> Self { - self.color_table = Some(table); + pub fn color_table(mut self, table: ColorTable) -> Self { + self.color_table = Some(table); - self - } + self + } - pub fn indicies(mut self, vec: Vec<u8>) -> Self { - self.indicies = vec; - self - } + pub fn indicies(mut self, vec: Vec<u8>) -> Self { + self.indicies = vec; + self + } - pub fn build(self) -> IndexedImage { - let mut imgdesc = ImageDescriptor { - left: self.left_offset, - top: self.top_offset, - width: self.width, - height: self.height, - packed: 0 // Set later - }; + pub fn build(self) -> IndexedImage { + let mut imgdesc = ImageDescriptor { + left: self.left_offset, + top: self.top_offset, + width: self.width, + height: self.height, + packed: 0, // Set later + }; - if let Some(lct) = &self.color_table { - imgdesc.color_table_present(true); - imgdesc.color_table_size(lct.packed_len()); - } + if let Some(lct) = &self.color_table { + imgdesc.set_color_table_present(true); + imgdesc.set_color_table_size(lct.packed_len()); + } - IndexedImage { - image_descriptor: imgdesc, - local_color_table: self.color_table, - indicies: self.indicies - } - } + IndexedImage { + image_descriptor: imgdesc, + local_color_table: self.color_table, + indicies: self.indicies, + } + } } |