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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::io::BufRead;
use std::borrow::Cow;
use std::str;
use byteorder::{ReadBytesExt, LittleEndian};
use types::{Result, Dimensions};
use traits::LoadableMetadata;
use utils::BufReadExt;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Version {
V87a,
V89a
}
impl Version {
fn from_bytes(b: &[u8]) -> Option<Version> {
match b {
b"87a" => Some(Version::V87a),
b"89a" => Some(Version::V89a),
_ => None
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Block {
ImageDescriptor(ImageDescriptor),
GraphicControlExtension(GraphicControlExtension),
PlainTextExtension(PlainTextExtension),
ApplicationExtension(ApplicationExtension),
CommentExtension(CommentExtension)
}
fn skip_blocks<R: ?Sized + BufRead, F>(r: &mut R, on_eof: F) -> Result<()>
where F: Fn() -> Cow<'static, str>
{
loop {
let n = try_if_eof!(r.read_u8(), on_eof()) as u64;
if n == 0 { return Ok(()); }
if try!(r.skip_exact(n)) != n {
return Err(unexpected_eof!(on_eof()));
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ColorTable {
pub size: u16,
pub sorted: bool,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ImageDescriptor {
pub left: u16,
pub top: u16,
pub width: u16,
pub height: u16,
pub local_color_table: Option<ColorTable>,
pub interlace: bool
}
impl ImageDescriptor {
fn load<R: ?Sized + BufRead>(index: usize, r: &mut R) -> Result<ImageDescriptor> {
let left = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading left offset of image block {}", index
);
let top = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading top offset of image block {}", index
);
let width = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading width of image block {}", index
);
let height = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading height of image block {}", index
);
let packed_flags = try_if_eof!(r.read_u8(), "when reading flags of image block {}", index);
let local_color_table = (0b10000000 & packed_flags) > 0;
let interlace = (0b01000000 & packed_flags) > 0;
let local_color_table_sorted = (0b00100000 & packed_flags) > 0;
let local_color_table_size_p = (0b00000111 & packed_flags) >> 0;
let local_color_table_size = if local_color_table {
1u16 << (local_color_table_size_p+1)
} else {
0
};
if local_color_table {
let skip_size = local_color_table_size as u64 * 3;
if try!(r.skip_exact(skip_size)) != skip_size {
return Err(unexpected_eof!("when reading color table of image block {}", index));
}
}
let _ = try_if_eof!(r.read_u8(), "when reading LZW minimum code size of image block {}", index);
try!(skip_blocks(r, || format!("when reading image data of image block {}", index).into()));
Ok(ImageDescriptor {
left: left,
top: top,
width: width,
height: height,
local_color_table: if local_color_table {
Some(ColorTable {
size: local_color_table_size,
sorted: local_color_table_sorted
})
} else { None },
interlace: interlace
})
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GraphicControlExtension {
pub disposal_method: DisposalMethod,
pub user_input: bool,
pub transparent_color_index: Option<u8>,
pub delay_time: u16
}
impl GraphicControlExtension {
#[inline]
pub fn delay_time_ms(&self) -> u32 {
self.delay_time as u32 * 10
}
fn load<R: ?Sized + BufRead>(index: usize, r: &mut R) -> Result<GraphicControlExtension> {
const NAME: &'static str = "graphics control extension block";
let block_size = try_if_eof!(r.read_u8(), "when reading block size of {} {}", NAME, index);
if block_size != 0x04 {
return Err(invalid_format!("invalid block size in {} {}: {}", NAME, index, block_size));
}
let packed_flags = try_if_eof!(r.read_u8(), "when reading flags of {} {}", NAME, index);
let disposal_method = (0b00011100 & packed_flags) >> 2;
let user_input = (0b00000010 & packed_flags) > 0;
let transparent_color = (0b00000001 & packed_flags) > 0;
let delay_time = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading delay time of {} {}", NAME, index
);
let transparent_color_index = try_if_eof!(
r.read_u8(),
"when reading transparent color index of {} {}", NAME, index
);
try!(skip_blocks(r, || format!("when reading block terminator of {} {}", NAME, index).into()));
Ok(GraphicControlExtension {
disposal_method: try!(
DisposalMethod::from_u8(disposal_method)
.ok_or(invalid_format!("invalid disposal method in {} {}: {}",
NAME, index, disposal_method))
),
user_input: user_input,
transparent_color_index: if transparent_color {
Some(transparent_color_index)
} else {
None
},
delay_time: delay_time
})
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum DisposalMethod {
None,
DoNotDispose,
RestoreToBackgroundColor,
RestoreToPrevious,
Unknown(u8)
}
impl DisposalMethod {
fn from_u8(n: u8) -> Option<DisposalMethod> {
match n {
0 => Some(DisposalMethod::None),
1 => Some(DisposalMethod::DoNotDispose),
2 => Some(DisposalMethod::RestoreToBackgroundColor),
3 => Some(DisposalMethod::RestoreToPrevious),
n if n < 8 => Some(DisposalMethod::Unknown(n)),
_ => None
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PlainTextExtension {
pub left: u16,
pub top: u16,
pub width: u16,
pub height: u16,
pub cell_width: u8,
pub cell_height: u8,
pub foreground_color_index: u8,
pub background_color_index: u8
}
impl PlainTextExtension {
fn load<R: ?Sized + BufRead>(index: usize, r: &mut R) -> Result<PlainTextExtension> {
const NAME: &'static str = "plain text extension block";
let block_size = try_if_eof!(r.read_u8(), "when reading block size of {} {}", NAME, index);
if block_size != 0x0C {
return Err(invalid_format!("invalid block size in {} {}: {}", NAME, index, block_size));
}
let left = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading left offset of {} {}", NAME, index
);
let top = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading top offset of {} {}", NAME, index
);
let width = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading width of {} {}", NAME, index
);
let height = try_if_eof!(
r.read_u16::<LittleEndian>(),
"when reading height of {} {}", NAME, index
);
let cell_width = try_if_eof!(
r.read_u8(),
"when reading character cell width of {} {}", NAME, index
);
let cell_height = try_if_eof!(
r.read_u8(),
"when reading character cell height of {} {}", NAME, index
);
let foreground_color_index = try_if_eof!(
r.read_u8(),
"when reading foreground color index of {} {}", NAME, index
);
let background_color_index = try_if_eof!(
r.read_u8(),
"when reading background color index of {} {}", NAME, index
);
try!(skip_blocks(r, || format!("when reading text data of {} {}", NAME, index).into()));
Ok(PlainTextExtension {
left: left,
top: top,
width: width,
height: height,
cell_width: cell_width,
cell_height: cell_height,
foreground_color_index: foreground_color_index,
background_color_index: background_color_index
})
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ApplicationExtension {
pub application_identifier: [u8; 8],
pub authentication_code: [u8; 3]
}
impl ApplicationExtension {
pub fn application_identifier_str(&self) -> Option<&str> {
str::from_utf8(&self.application_identifier).ok()
}
pub fn authentication_code_str(&self) -> Option<&str> {
str::from_utf8(&self.authentication_code).ok()
}
fn load<R: ?Sized + BufRead>(index: usize, r: &mut R) -> Result<ApplicationExtension> {
const NAME: &'static str = "application extension block";
let block_size = try_if_eof!(r.read_u8(), "when reading block size of {} {}", NAME, index);
if block_size != 0x0B {
return Err(invalid_format!("invalid block size in {} {}: {}", NAME, index, block_size));
}
let mut application_identifier = [0u8; 8];
try!(r.read_exact(&mut application_identifier)
.map_err(if_eof!(std, "while reading application identifier in {} {}", NAME, index)));
let mut authentication_code = [0u8; 3];
try!(r.read_exact(&mut authentication_code)
.map_err(if_eof!(std, "while reading authentication code in {} {}", NAME, index)));
try!(skip_blocks(r, || format!("when reading application data of {} {}", NAME, index).into()));
Ok(ApplicationExtension {
application_identifier: application_identifier,
authentication_code: authentication_code
})
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CommentExtension;
impl CommentExtension {
fn load<R: ?Sized + BufRead>(index: usize, r: &mut R) -> Result<CommentExtension> {
const NAME: &'static str = "comments extension block";
try!(skip_blocks(r, || format!("when reading comment data of {} {}", NAME, index).into()));
Ok(CommentExtension)
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Metadata {
pub version: Version,
pub dimensions: Dimensions,
pub global_color_table: Option<ColorTable>,
pub color_resolution: u16,
pub background_color_index: u8,
pub pixel_aspect_ratio: u8,
pub blocks: Vec<Block>
}
impl Metadata {
#[inline]
pub fn pixel_aspect_ratio_approx(&self) -> Option<f64> {
if self.pixel_aspect_ratio == 0 {
None
} else {
Some((self.pixel_aspect_ratio as f64 + 15.0)/64.0)
}
}
#[inline]
pub fn frames_number(&self) -> usize {
self.blocks.iter().filter(|b| match **b {
Block::ImageDescriptor(_) => true,
_ => false
}).count()
}
#[inline]
pub fn is_animated(&self) -> bool {
self.frames_number() > 1
}
}
impl LoadableMetadata for Metadata {
fn load<R: ?Sized + BufRead>(r: &mut R) -> Result<Metadata> {
let mut signature = [0u8; 6];
try!(r.read_exact(&mut signature).map_err(if_eof!(std, "when reading GIF signature")));
let version = try!(Version::from_bytes(&signature[3..])
.ok_or(invalid_format!("invalid GIF version: {:?}", &signature[3..])));
let width = try_if_eof!(r.read_u16::<LittleEndian>(), "when reading logical width");
let height = try_if_eof!(r.read_u16::<LittleEndian>(), "when reading logical height");
let packed_flags = try_if_eof!(r.read_u8(), "when reading global flags");
let global_color_table = (packed_flags & 0b10000000) > 0;
let color_resolution = (packed_flags & 0b01110000) >> 4;
let global_color_table_sorted = (packed_flags & 0b00001000) > 0;
let global_color_table_size_p = (packed_flags & 0b00000111) >> 0;
let global_color_table_size = if global_color_table {
1u16 << (global_color_table_size_p + 1)
} else {
0
};
let background_color_index = try_if_eof!(r.read_u8(), "when reading background color index");
let pixel_aspect_ratio = try_if_eof!(r.read_u8(), "when reading pixel aspect ration");
if global_color_table {
let skip_size = global_color_table_size as u64 * 3;
if try!(r.skip_exact(skip_size)) != skip_size {
return Err(unexpected_eof!("when reading global color table"));
}
}
let mut blocks = Vec::new();
let mut index = 0usize;
loop {
let separator = try_if_eof!(r.read_u8(), "when reading separator of block {}", index);
let block = match separator {
0x2c => Block::ImageDescriptor(try!(ImageDescriptor::load(index, r))),
0x21 => {
let label = try_if_eof!(r.read_u8(), "when reading label of block {}", index);
match label {
0x01 => Block::PlainTextExtension(try!(PlainTextExtension::load(index, r))),
0xf9 => Block::GraphicControlExtension(try!(GraphicControlExtension::load(index, r))),
0xfe => Block::CommentExtension(try!(CommentExtension::load(index, r))),
0xff => Block::ApplicationExtension(try!(ApplicationExtension::load(index, r))),
_ => return Err(invalid_format!("unknown extension type of block {}: 0x{:X}", index, label))
}
},
0x3b => break,
_ => return Err(invalid_format!("unknown block type of block {}: 0x{:X}", index, separator))
};
blocks.push(block);
index += 1;
}
Ok(Metadata {
version: version,
dimensions: (width, height).into(),
global_color_table: if global_color_table {
Some(ColorTable {
size: global_color_table_size,
sorted: global_color_table_sorted
})
} else {
None
},
color_resolution: 1u16 << (color_resolution + 1),
background_color_index: background_color_index,
pixel_aspect_ratio: pixel_aspect_ratio,
blocks: blocks
})
}
}