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
//! giffy is a simple GIF decoder.
//!
//! # Example
//!
//! ```no_run
//! use giffy;
//! use std::fs::File;
//!
//! let mut src = File::open("<gif path>").expect("File not found");
//! match giffy::load(&mut src) {
//!     Ok(gif) => {
//!         for frame in gif.image_frames {
//!             // do something with the frame
//!         }
//!     }
//!
//!     Err(e) => {
//!         eprintln!("Error: {}", e);
//!     }
//! }
//! ```

mod decompressor;
mod parser;
mod util;

use decompressor::Decompressor;
use parser::*;
use std::io::Read;

pub use util::Color;

/// This struct holds the width, height and the image frames of the GIF media.
#[derive(Debug, Clone)]
pub struct Gif {
    /// The width of the GIF media.
    pub width: u32,
    /// The height of the GIF media.
    pub height: u32,
    /// Individual image frames.
    pub image_frames: Vec<ImageFrame>,
}

/// This struct is used to hold the color information and the delay time of a frame.
#[derive(Debug, Clone)]
pub struct ImageFrame {
    /// The colors that make up the image frame. This is used for drawing the image frame.
    pub colors: Box<[Color]>,
    /// The amount of time this image frame should stay on screen before moving
    /// on to the next image frame.
    pub delay_time: u16,
}

/// Attempt to load a GIF from a given `src`.
///
/// # Errors
///
/// This function will return an error if the GIF src is not in a valid GIF format.
pub fn load<R>(src: &mut R) -> Result<Gif, String>
where
    R: Read,
{
    let mut parser = Parser::new(src);
    let result = parser.parse()?;

    let decoder = Decoder::new(&result);
    let frames = decoder.decode()?;

    Ok(Gif {
        image_frames: frames,
        width: result.logical_screen_descriptor.width as u32,
        height: result.logical_screen_descriptor.height as u32,
    })
}

struct Decoder<'a> {
    data: &'a ParseResult,
}

impl<'a> Decoder<'a> {
    fn new(input: &'a ParseResult) -> Self {
        Self { data: input }
    }

    fn decode(&self) -> Result<Vec<ImageFrame>, String> {
        let mut frames = vec![];

        for block in self.data.data_blocks.iter() {
            match block {
                DataType::ApplicationExtensionType(_) => {}
                DataType::CommentExtensionType(_) => {}
                DataType::PlainTextExtensionType(_) => {}
                DataType::TableBasedImageType(image) => {
                    let color_table = {
                        if image.local_color_table.is_some() {
                            image.local_color_table.as_ref().unwrap()
                        } else {
                            self.data
                                .logical_screen_descriptor
                                .global_color_table
                                .as_ref()
                                .expect("Global color table is missing!")
                        }
                    };

                    let (transparent_flag, transparent_color_index, disposal_method, delay_time) =
                        match image.graphic_control_extension {
                            Some(ref ext) => (
                                ext.transparent_color_index_available,
                                ext.transparent_color_index,
                                ext.disposal_method,
                                ext.delay_time,
                            ),
                            None => (false, 0, DisposalMethod::Unspecified, 0),
                        };

                    let mut decompressor = Decompressor::new(
                        &image.image_data.data_sub_blocks,
                        image.image_data.lzw_min_code_size,
                    );

                    let index_table = decompressor.decompress()?;

                    if frames.is_empty() {
                        frames.push(self.create_first_frame(
                            &index_table,
                            &color_table,
                            image.image_descriptor.interlace_flag,
                            delay_time,
                        ));
                    } else {
                        frames.push(self.create_frame(
                            &frames,
                            &image,
                            &index_table,
                            &color_table,
                            disposal_method,
                            transparent_flag,
                            transparent_color_index,
                            delay_time,
                        )?);
                    }
                }
            }
        }

        Ok(frames)
    }

    fn create_first_frame(
        &self,
        index_table: &[usize],
        color_table: &[Color],
        interlace_flag: bool,
        delay_time: u16,
    ) -> ImageFrame {
        let result = index_table
            .iter()
            .map(|i| Some(color_table[*i]))
            .collect::<Vec<_>>();

        let result = if interlace_flag {
            Self::deinterlace(
                result,
                self.data.logical_screen_descriptor.width as usize,
                self.data.logical_screen_descriptor.height as usize,
            )
        } else {
            result
        };

        ImageFrame {
            delay_time,
            colors: result
                .iter()
                .map(|e| e.expect("Missing color value"))
                .collect(),
        }
    }

    fn create_frame(
        &self,
        frames: &[ImageFrame],
        image: &TableBasedImage,
        index_table: &[usize],
        color_table: &[Color],
        disposal_method: DisposalMethod,
        transparent_flag: bool,
        transparent_color_index: u8,
        delay_time: u16,
    ) -> Result<ImageFrame, String> {
        let top = image.image_descriptor.top as usize;
        let height = image.image_descriptor.height as usize;
        let left = image.image_descriptor.left as usize;
        let width = image.image_descriptor.width as usize;
        let image_width = self.data.logical_screen_descriptor.width as usize;

        let result = if transparent_flag {
            index_table
                .iter()
                .map(|i| {
                    if *i == transparent_color_index as usize {
                        None
                    } else {
                        Some(color_table[*i])
                    }
                })
                .collect::<Vec<_>>()
        } else {
            index_table
                .iter()
                .map(|i| Some(color_table[*i]))
                .collect::<Vec<_>>()
        };

        let mut new_frame = match disposal_method {
            DisposalMethod::RestoreToBackgroundColor => ImageFrame {
                delay_time,
                colors: vec![
                    color_table[self.data.logical_screen_descriptor.background_color_index
                        as usize];
                    frames.last().unwrap().colors.len()
                ]
                .into_boxed_slice(),
            },
            DisposalMethod::DoNotDispose | DisposalMethod::Unspecified => {
                frames.last().unwrap().clone()
            }
            d @ _ => return Err(format!("Dispose method {:?} not supported", d)),
        };

        let result = if image.image_descriptor.interlace_flag {
            Self::deinterlace(result, width, height)
        } else {
            result
        };

        for y in 0..height {
            let offset = (top + y) * image_width + left;
            for x in 0..width {
                let c = result[y * width + x];
                if let Some(c) = c {
                    new_frame.colors[offset + x] = c;
                }
            }
        }

        Ok(new_frame)
    }

    // Refer to https://www.w3.org/Graphics/GIF/spec-gif89a.txt for details.
    fn deinterlace(input: Vec<Option<Color>>, width: usize, height: usize) -> Vec<Option<Color>> {
        let mut result = vec![None; width * height];

        let mut index = 0;
        let passes = [(0, 8), (4, 8), (2, 4), (1, 2)];

        for (start, step) in passes.iter() {
            'l: for y in (*start..height as usize).step_by(*step) {
                for x in 0..width as usize {
                    let index_dst = y * width as usize + x;
                    if index_dst >= result.len() {
                        break 'l;
                    }

                    result[index_dst] = input[index];
                    index += 1;
                }
            }
        }

        result
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    struct MockReader<'a> {
        data: &'a [u8],
        remaining: usize,
    }

    impl<'a> Read for MockReader<'a> {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            let mut count = 0;

            if self.remaining > 0 {
                let offset = self.data.len() - self.remaining;

                for i in 0..buf.len() {
                    buf[i] = self.data[offset + i];
                }

                self.remaining -= buf.len();
                count += buf.len();
            }

            Ok(count)
        }
    }

    #[test]
    fn test_sample_gif() {
        let input = vec![
            71, 73, 70, 56, 57, 97, 10, 0, 10, 0, 145, 0, 0, 255, 255, 255, 255, 0, 0, 0, 0, 255,
            0, 0, 0, 33, 249, 4, 0, 0, 0, 0, 0, 44, 0, 0, 0, 0, 10, 0, 10, 0, 0, 2, 22, 140, 45,
            153, 135, 42, 28, 220, 51, 160, 2, 117, 236, 149, 250, 168, 222, 96, 140, 4, 145, 76,
            1, 0, 59,
        ];

        let mut reader = MockReader {
            data: &input,
            remaining: input.len(),
        };

        let expected = vec![vec![
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 255, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(0, 0, 255),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
            Color(255, 0, 0),
        ]
        .into_boxed_slice()];

        let mut parser = Parser::new(&mut reader);
        let result = parser.parse().unwrap();

        let decoder = Decoder::new(&result);
        let actual = decoder.decode().unwrap();

        let mut v = vec![];
        for i in actual.iter() {
            v.push(i.colors.clone());
        }

        assert_eq!(expected, v);
    }
}