about summary refs log tree commit diff
path: root/src/writer/gifbuilder.rs
blob: 6ae55d585f59abc6a896387b8a011c4b1e7da683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::components::{ColorTable, Gif, LogicalScreenDescriptor, Version};
use super::ImageBuilder;

pub struct GifBuilder {
	version: Version,
	width: u16,
	height: u16,
	global_color_table: Option<ColorTable>,
	background_color_index: u8,
	imagebuilders: Vec<ImageBuilder>
}

impl GifBuilder {
	pub fn new(version: Version, width: u16, height: u16) -> Self {
		Self {
			version,
			width,
			height,
			global_color_table: None,
			background_color_index: 0,
			imagebuilders: vec![]
		}
	}

	pub fn global_color_table(mut self, table: ColorTable) -> Self {
		self.global_color_table = Some(table);

		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!");
		}

		self.background_color_index = ind;
		self
	}

	pub fn image(mut self, ib: ImageBuilder) -> Self {
		self.imagebuilders.push(ib);
		self
	}

	pub fn build(self) -> Gif {
		let mut lsd = LogicalScreenDescriptor {
			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);
		}

		let mut images = vec![];
		for builder in self.imagebuilders.into_iter() {
			images.push(builder.build());
		}

		Gif {
			header: self.version,
			logical_screen_descriptor: lsd,
			global_color_table: self.global_color_table,
			images
		}
	}
}