about summary refs log tree commit diff
path: root/src/writer/gifbuilder.rs
blob: ff98ba7e6c06bb3beb39d5ed43914ff88db43a85 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::common::{Color, Version};
use super::OutVec;
use super::ImageBuilder;

pub struct GifBuilder {
	version: Version,
	width: u16,
	height: u16,
	global_color_table: Option<Vec<Color>>,
	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, vec: Vec<Color>) -> Self {
		if vec.len() == 0 || vec.len() > 256 {
			//TODO: Throw error instead of panic
			panic!("GCT has to be less than or 256 colors in size, and at least one");
		}

		self.global_color_table = Some(vec);

		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 write_to_vec(&self) -> Vec<u8> {
		let mut out = OutVec::new();

		self
			.write_header(&mut out)
			.write_logical_screen_descriptor(&mut out)
			.write_global_color_table(&mut out)
			.write_images(&mut out)
			.write_trailer(&mut out);

		out.vec()
	}

	fn write_header(&self, out: &mut OutVec) -> &Self {
		out.push_slice(b"GIF");

		//TODO: Automatically detect version
		match self.version {
			Version::Gif87a => out.push_slice(b"87a"),
			Version::Gif89a => out.push_slice(b"89a")
		};

		self
	}

	fn write_logical_screen_descriptor(&self, out: &mut OutVec) -> &Self {
		out.push_u16(self.width).push_u16(self.height);

		let mut packed: u8 = 0;

		if let Some(gct) = &self.global_color_table {
			packed |= 0b1000_0000; // Set the GCT flag

			// GCT size is calulated by raising two to this number plus one,
			// so we have to work backwards.
			let size = (gct.len() as f32).log2().ceil() - 1f32;
			
			packed |= size as u8;
		}
		//TODO: Color Resolution and Sort Flag fields in packed.
		out.push_u8(packed);

		out.push_u8(self.background_color_index)
			.push_u8(0x00); //TOOD: Allow setting pixel aspect ratio

		self
	}

	fn write_global_color_table(&self, out: &mut OutVec) -> &Self {
		if let Some(gct) = &self.global_color_table {
			out.push_colors(&gct);
		}

		self
	}

	fn write_images(&self, out: &mut OutVec) -> &Self {
		//TODO: Deduplicate color table size code
		let mcs = if let Some(gct) = &self.global_color_table {
			((gct.len() as f32).log2().ceil() - 1f32) as u8
		} else {
			0
		};

		for ib in &self.imagebuilders {
			let image = ib.write_to_vec(mcs);
			out.push_slice(&image);
		}

		self
	}

	fn write_trailer(&self, out: &mut OutVec) {
		/*
			"This block is a single-field block indicating the end of the GIF Data Stream. 
			It contains the fixed value 0x3B."
		*/
		out.push_u8(0x3B);
	}
}

#[cfg(test)]
pub mod gifbuilder_test {
	use super::*;

	#[test]
	pub fn writer_header() {
		let mut out = OutVec::new();
		GifBuilder::new(Version::Gif87a, 0, 0).write_header(&mut out);

		assert_eq!(out.vec(), b"GIF87a");
	}

	#[test]
	pub fn write_logical_screen_descriptor() {
		let mut out = OutVec::new();
		GifBuilder::new(Version::Gif87a, 4, 4).write_logical_screen_descriptor(&mut out);

		assert_eq!(out.vec(), vec![0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00]);

		let mut out = OutVec::new();
		let gct = vec![
			Color::new(0, 0, 0), Color::new(0, 0, 0), Color::new(0, 0, 0),
			Color::new(0, 0, 0), Color::new(0, 0, 0)
		];
		GifBuilder::new(Version::Gif87a, 4, 4).global_color_table(gct).write_logical_screen_descriptor(&mut out);

		assert_eq!(out.vec(), vec![0x04, 0x00, 0x04, 0x00, 0b1000_0010, 0x00, 0x00]);
	}

	#[test]
	pub fn write_global_color_table() {
		let mut out = OutVec::new();
		let gct = vec![
			Color::new(1, 2, 3), Color::new(253, 254, 255)
		];
		GifBuilder::new(Version::Gif87a, 4, 4).global_color_table(gct).write_global_color_table(&mut out);

		assert_eq!(out.vec(), vec![1, 2, 3, 253, 254, 255]);
	}

	#[test]
	fn write_images() {
		let gct = vec![
			Color::new(1, 2, 3), Color::new(253, 254, 255)
		];
		let colortable = vec![
			Color::new(0, 0, 0), Color::new(128, 0, 255)
		];
		let indicies = vec![0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0];

		let expected_out = vec![
			0x2C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0b1000_0000, // Image Descriptor 1
			0, 0, 0, 128, 0, 255, // Color Table
			0x02, 0x05, 0x84, 0x1D, 0x81, 0x7A, 0x50, 0x00, // Image Data 1
			0x2C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0b0000_0000, // Image Descriptor 2
			0x02, 0x05, 0x84, 0x1D, 0x81, 0x7A, 0x50, 0x00 // Image Data 2
		];

		let mut out = OutVec::new();
		GifBuilder::new(Version::Gif87a, 4, 4)
			.global_color_table(gct)
			.image(ImageBuilder::new(4, 4)
				.color_table(colortable)
				.indicies(indicies.clone())
			).image(ImageBuilder::new(4, 4)
				.indicies(indicies)
			).write_images(&mut out);

		assert_eq!(out.vec(), expected_out);
	}

	#[test]
	fn write_to_vec() {
		let gct = vec![
			Color::new(1, 2, 3), Color::new(253, 254, 255)
		];
		let colortable = vec![
			Color::new(0, 0, 0), Color::new(128, 0, 255)
		];
		let indicies = vec![0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0];

		let expected_out = vec![
			0x47, 0x49, 0x46, 0x38, 0x37, 0x61, // Version - GIF87a
			0x04, 0x00, 0x04, 0x00, 0b1000_0000, 0x00, 0x00, // Logical Screen Descriptor
			1, 2, 3, 253, 254, 255, // Global Color Table
			0x2C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0b1000_0000, // Image Descriptor 1
			0, 0, 0, 128, 0, 255, // Color Table
			0x02, 0x05, 0x84, 0x1D, 0x81, 0x7A, 0x50, 0x00, // Image Data 1
			0x2C, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0b0000_0000, // Image Descriptor 2
			0x02, 0x05, 0x84, 0x1D, 0x81, 0x7A, 0x50, 0x00, // Image Data 2
			0x3B // Trailer
		];

		let mut out = OutVec::new();
		let actual_out = GifBuilder::new(Version::Gif87a, 4, 4)
			.global_color_table(gct)
			.image(ImageBuilder::new(4, 4)
				.color_table(colortable)
				.indicies(indicies.clone())
			).image(ImageBuilder::new(4, 4)
				.indicies(indicies)
			).write_to_vec();

		assert_eq!(actual_out, expected_out);
	}
}