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
extern crate gif;
use std::io::{Read, Write};
pub use self::gif::Frame;
use self::gif::{SetParameter, ColorOutput};
use image::{ImageError, ImageResult, DecodingResult, ImageDecoder};
use color;
enum Either<T, U> {
Left(T),
Right(U)
}
pub struct Decoder<R: Read> {
inner: Option<Either<gif::Decoder<R>, gif::Reader<R>>>
}
impl<R: Read> Decoder<R> {
pub fn new(r: R) -> Decoder<R> {
let mut decoder = gif::Decoder::new(r);
decoder.set(ColorOutput::RGBA);
Decoder {
inner: Some(Either::Left(decoder))
}
}
fn get_reader(&mut self) -> Result<&mut gif::Reader<R>, gif::DecodingError> {
let inner = self.inner.take().unwrap();
self.inner = Some(match inner {
Either::Left(decoder) => {
let reader = try!(decoder.read_info());
Either::Right(reader)
},
Either::Right(reader) => Either::Right(reader)
});
match self.inner {
Some(Either::Right(ref mut reader)) => Ok(reader),
_ => unreachable!()
}
}
}
impl<R: Read> ImageDecoder for Decoder<R> {
fn dimensions(&mut self) -> ImageResult<(u32, u32)> {
let reader = try!(self.get_reader());
Ok((reader.width() as u32, reader.height() as u32))
}
fn colortype(&mut self) -> ImageResult<color::ColorType> {
Ok(color::ColorType::RGBA(8))
}
fn row_len(&mut self) -> ImageResult<usize> {
let reader = try!(self.get_reader());
Ok(reader.line_length())
}
fn read_scanline(&mut self, buf: &mut [u8]) -> ImageResult<u32> {
let reader = try!(self.get_reader());
let len = reader.line_length();
try!(reader.fill_buffer(&mut buf[..len]));
Ok(len as u32)
}
fn read_image(&mut self) -> ImageResult<DecodingResult> {
let reader = try!(self.get_reader());
if let Some(_) = try!(reader.next_frame_info()) {
let mut buf = vec![0; reader.buffer_size()];
try!(reader.fill_buffer(&mut buf));
Ok(DecodingResult::U8(buf))
} else {
Err(ImageError::ImageEnd)
}
}
}
pub struct Encoder<W: Write> {
w: W,
}
impl<W: Write> Encoder<W> {
pub fn new(w: W) -> Encoder<W> {
Encoder {
w: w
}
}
pub fn encode(self, frame: Frame) -> ImageResult<()> {
let mut encoder = try!(
gif::Encoder::new(self.w, frame.width, frame.height, &[])
);
encoder.write_frame(&frame).map_err(|err| err.into())
}
}
impl From<gif::DecodingError> for ImageError {
fn from(err: gif::DecodingError) -> ImageError {
use self::gif::DecodingError::*;
match err {
Format(desc) => ImageError::FormatError(desc.into()),
Internal(desc) => ImageError::FormatError(desc.into()),
Io(io_err) => ImageError::IoError(io_err),
}
}
}