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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
use std::{fs::File, io::Write, path::Path};
use crate::{
block::{extension::Extension, Block, ColorTable, ScreenDescriptor, Version},
colorimage,
writer::GifBuilder,
ColorImage,
};
pub struct Gif {
pub header: Version,
pub screen_descriptor: ScreenDescriptor,
pub global_color_table: Option<ColorTable>,
pub blocks: Vec<Block>, // Trailer at the end of this struct is 0x3B //
}
impl Gif {
pub fn builder(width: u16, height: u16) -> GifBuilder {
GifBuilder::new(width, height)
}
pub fn to_vec(&self) -> Vec<u8> {
let mut out = vec![];
out.extend_from_slice((&self.header).into());
let mut boxed: Box<[u8]> = (&self.screen_descriptor).into();
out.extend_from_slice(&*boxed);
// While we output the color table, grab it's length to use when
// outputting the image, or 0 if we don't have a GCT
let mcs = if let Some(gct) = &self.global_color_table {
boxed = gct.into();
out.extend_from_slice(&*boxed);
gct.packed_len()
} else {
0
};
for block in self.blocks.iter() {
match block {
Block::IndexedImage(image) => {
boxed = image.as_boxed_slice(mcs);
out.extend_from_slice(&*boxed);
}
//Block::BlockedImage(_) => unreachable!(),
Block::Extension(ext) => {
boxed = ext.into();
out.extend_from_slice(&*boxed);
}
}
}
// Write Trailer
out.push(0x3B);
out
}
pub fn save<P: AsRef<Path>>(&self, path: P) -> std::io::Result<()> {
File::create(path.as_ref())?.write_all(&self.to_vec())
}
pub fn images<'a>(&'a self) -> ImageIterator<'a> {
ImageIterator {
gif: self,
veciter: self.blocks.iter(),
}
}
}
pub struct ImageIterator<'a> {
gif: &'a Gif,
veciter: std::slice::Iter<'a, Block>,
}
impl<'a> Iterator for ImageIterator<'a> {
type Item = Image<'a>;
fn next(&mut self) -> Option<Self::Item> {
let mut transparent = None;
let img = loop {
match self.veciter.next() {
Some(block) => match block {
Block::IndexedImage(img) => break img,
Block::Extension(Extension::GraphicControl(gce)) => {
if gce.is_transparent() {
transparent = Some(gce.transparency_index());
} else {
transparent = None;
}
}
_ => (),
},
None => return None,
}
};
if img.image_descriptor.color_table_present() {
Some(Image {
width: img.image_descriptor.width,
height: img.image_descriptor.height,
left_offset: img.image_descriptor.left,
top_offset: img.image_descriptor.top,
palette: &img.local_color_table.as_ref().unwrap(),
transparent_index: transparent,
indicies: &img.indicies,
})
} else {
Some(Image {
width: img.image_descriptor.width,
height: img.image_descriptor.height,
left_offset: img.image_descriptor.left,
top_offset: img.image_descriptor.top,
palette: self.gif.global_color_table.as_ref().unwrap(),
transparent_index: transparent,
indicies: &img.indicies,
})
}
}
}
pub struct Image<'a> {
pub(crate) width: u16,
pub(crate) height: u16,
left_offset: u16,
top_offset: u16,
pub(crate) palette: &'a ColorTable,
pub(crate) transparent_index: Option<u8>,
pub(crate) indicies: &'a [u8],
}
impl<'a> Image<'a> {
pub fn width(&self) -> u16 {
self.width
}
pub fn height(&self) -> u16 {
self.height
}
pub fn position(&self) -> (u16, u16) {
(self.left_offset, self.top_offset)
}
pub fn palette(&self) -> &ColorTable {
self.palette
}
pub fn transparent_index(&self) -> Option<u8> {
self.transparent_index
}
pub fn indicies(&self) -> &[u8] {
self.indicies
}
}
#[cfg(test)]
pub mod gif {
use std::convert::TryInto;
use std::io::Write;
use crate::block::extension::DisposalMethod;
use crate::writer::{GifBuilder, ImageBuilder};
use crate::Color;
#[test]
fn to_vec_gif87a() {
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 actual = GifBuilder::new(4, 4)
.palette(gct.try_into().unwrap())
.image(
ImageBuilder::new(4, 4)
.palette(colortable.try_into().unwrap())
.indicies(&indicies),
)
.image(ImageBuilder::new(4, 4).indicies(&indicies));
let bytes = actual.build().unwrap().to_vec();
assert_eq!(bytes, expected_out);
}
#[test]
fn to_vec_gif89a() {
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![
71, 73, 70, 56, 57, 97, 4, 0, 4, 0, 128, 0, 0, 1, 2, 3, 253, 254, 255, 33, 249, 4, 8,
64, 0, 0, 0, 44, 0, 0, 0, 0, 4, 0, 4, 0, 128, 0, 0, 0, 128, 0, 255, 2, 5, 132, 29, 129,
122, 80, 0, 44, 0, 0, 0, 0, 4, 0, 4, 0, 0, 2, 5, 132, 29, 129, 122, 80, 0, 59,
];
let actual_out = GifBuilder::new(4, 4)
.palette(gct.try_into().unwrap())
.image(
ImageBuilder::new(4, 4)
.palette(colortable.try_into().unwrap())
.indicies(&indicies)
.disposal_method(DisposalMethod::RestoreBackground)
.delay(64),
)
.image(ImageBuilder::new(4, 4).indicies(&indicies))
.build()
.unwrap()
.to_vec();
std::fs::File::create("ah.gif")
.unwrap()
.write_all(&actual_out)
.unwrap();
std::fs::File::create("ah_hand.gif")
.unwrap()
.write_all(&expected_out)
.unwrap();
assert_eq!(actual_out, expected_out);
}
}
|