#![allow(clippy::while_let_loop)]
extern crate gif;
extern crate num_rational;
use std::clone::Clone;
use std::cmp::min;
use std::io::{self, Cursor, Read, Write};
use std::marker::PhantomData;
use std::mem;
use self::gif::{ColorOutput, SetParameter};
pub use self::gif::{DisposalMethod, Frame};
use animation;
use buffer::{ImageBuffer, Pixel};
use color;
use color::Rgba;
use image::{AnimationDecoder, ImageDecoder, ImageError, ImageResult};
use num_rational::Ratio;
pub struct Decoder<R: Read> {
reader: gif::Reader<R>,
}
impl<R: Read> Decoder<R> {
pub fn new(r: R) -> ImageResult<Decoder<R>> {
let mut decoder = gif::Decoder::new(r);
decoder.set(ColorOutput::RGBA);
Ok(Decoder {
reader: decoder.read_info()?,
})
}
}
pub struct GifReader<R>(Cursor<Vec<u8>>, PhantomData<R>);
impl<R> Read for GifReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
if self.0.position() == 0 && buf.is_empty() {
mem::swap(buf, self.0.get_mut());
Ok(buf.len())
} else {
self.0.read_to_end(buf)
}
}
}
impl<'a, R: 'a + Read> ImageDecoder<'a> for Decoder<R> {
type Reader = GifReader<R>;
fn dimensions(&self) -> (u64, u64) {
(u64::from(self.reader.width()), u64::from(self.reader.height()))
}
fn colortype(&self) -> color::ColorType {
color::ColorType::RGBA(8)
}
fn into_reader(self) -> ImageResult<Self::Reader> {
Ok(GifReader(Cursor::new(self.read_image()?), PhantomData))
}
fn read_image(mut self) -> ImageResult<Vec<u8>> {
let (f_width, f_height, left, top);
if let Some(frame) = self.reader.next_frame_info()? {
left = u32::from(frame.left);
top = u32::from(frame.top);
f_width = u32::from(frame.width);
f_height = u32::from(frame.height);
} else {
return Err(ImageError::ImageEnd);
}
let mut buf = vec![0; self.reader.buffer_size()];
self.reader.read_into_buffer(&mut buf)?;
let image_buffer_raw = ImageBuffer::from_raw(f_width, f_height, buf).ok_or_else(||
ImageError::UnsupportedError("Image dimensions are too large".into())
)?;
let (width, height) = (u32::from(self.reader.width()), u32::from(self.reader.height()));
let image_buffer = full_image_from_frame(width, height, image_buffer_raw, left, top);
Ok(image_buffer.into_raw())
}
}
struct GifFrameIterator<R: Read> {
reader: gif::Reader<R>,
width: u32,
height: u32,
non_disposed_frame: ImageBuffer<Rgba<u8>, Vec<u8>>,
}
impl<R: Read> GifFrameIterator<R> {
fn new(decoder: Decoder<R>) -> GifFrameIterator<R> {
let (width, height) = decoder.dimensions();
let (width, height) = (width as u32, height as u32);
let non_disposed_frame = ImageBuffer::from_pixel(width, height, Rgba([0, 0, 0, 0]));
GifFrameIterator {
reader: decoder.reader,
width,
height,
non_disposed_frame,
}
}
}
impl<R: Read> Iterator for GifFrameIterator<R> {
type Item = ImageResult<animation::Frame>;
fn next(&mut self) -> Option<ImageResult<animation::Frame>> {
let (left, top, delay, dispose, f_width, f_height);
match self.reader.next_frame_info() {
Ok(frame_info) => {
if let Some(frame) = frame_info {
left = u32::from(frame.left);
top = u32::from(frame.top);
f_width = u32::from(frame.width);
f_height = u32::from(frame.height);
delay = Ratio::new(frame.delay * 10, 1);
dispose = frame.dispose;
} else {
return None;
}
},
Err(err) => return Some(Err(err.into())),
}
let mut vec = vec![0; self.reader.buffer_size()];
if let Err(err) = self.reader.fill_buffer(&mut vec) {
return Some(Err(err.into()));
}
let image_buffer_raw = match ImageBuffer::from_raw(f_width, f_height, vec) {
Some(image_buffer_raw) => image_buffer_raw,
None => {
return Some(Err(ImageError::UnsupportedError(
"Image dimensions are too large".into(),
)))
}
};
let mut image_buffer =
full_image_from_frame(self.width, self.height, image_buffer_raw, left, top);
for (x, y, pixel) in image_buffer.enumerate_pixels_mut() {
let previous_img_buffer = &self.non_disposed_frame;
let adjusted_pixel: &mut Rgba<u8> = pixel;
let previous_pixel: &Rgba<u8> = previous_img_buffer.get_pixel(x, y);
let pixel_alpha = adjusted_pixel.channels()[3];
if pixel_alpha == 0 {
adjusted_pixel.blend(previous_pixel);
}
}
let frame = animation::Frame::from_parts(
image_buffer.clone(), 0, 0, delay
);
match dispose {
DisposalMethod::Any => {
}
DisposalMethod::Keep => {
self.non_disposed_frame = image_buffer;
}
DisposalMethod::Background => {
for y in top..min(top + f_height, self.height) {
for x in left..min(left + f_width, self.width) {
self.non_disposed_frame.put_pixel(x, y, Rgba([0, 0, 0, 0]));
}
}
}
DisposalMethod::Previous => {
}
};
Some(Ok(frame))
}
}
fn full_image_from_frame(
screen_width: u32,
screen_height: u32,
image: crate::RgbaImage,
left: u32,
top: u32,
) -> crate::RgbaImage {
if (left, top) == (0, 0) && (screen_width, screen_height) == (image.width(), image.height()) {
image
} else {
ImageBuffer::from_fn(screen_width, screen_height, |x, y| {
let x = x.wrapping_sub(left);
let y = y.wrapping_sub(top);
if x < image.width() && y < image.height() {
*image.get_pixel(x, y)
} else {
Rgba([0, 0, 0, 0])
}
})
}
}
impl<'a, R: Read + 'a> AnimationDecoder<'a> for Decoder<R> {
fn into_frames(self) -> animation::Frames<'a> {
animation::Frames::new(Box::new(GifFrameIterator::new(self)))
}
}
pub struct Encoder<W: Write> {
w: Option<W>,
gif_encoder: Option<gif::Encoder<W>>,
}
impl<W: Write> Encoder<W> {
pub fn new(w: W) -> Encoder<W> {
Encoder {
w: Some(w),
gif_encoder: None,
}
}
pub fn encode(&mut self, frame: &Frame) -> ImageResult<()> {
let result;
if let Some(ref mut encoder) = self.gif_encoder {
result = encoder.write_frame(frame).map_err(|err| err.into());
} else {
let writer = self.w.take().unwrap();
let mut encoder = gif::Encoder::new(writer, frame.width, frame.height, &[])?;
result = encoder.write_frame(&frame).map_err(|err| err.into());
self.gif_encoder = Some(encoder);
}
result
}
pub fn encode_frames<F>(&mut self, frames: F) -> ImageResult<()>
where
F: IntoIterator<Item = animation::Frame>
{
for img_frame in frames {
self.encode_single_frame(img_frame)?;
}
Ok(())
}
pub fn try_encode_frames<F>(&mut self, frames: F) -> ImageResult<()>
where
F: IntoIterator<Item = ImageResult<animation::Frame>>
{
for img_frame in frames {
self.encode_single_frame(img_frame?)?;
}
Ok(())
}
fn encode_single_frame(&mut self, img_frame: animation::Frame) -> ImageResult<()> {
let frame_delay = img_frame.delay().to_integer();
let rbga_frame = img_frame.into_buffer();
let mut frame = Frame::from_rgba(rbga_frame.width() as u16, rbga_frame.height() as u16, &mut rbga_frame.into_raw());
frame.delay = frame_delay;
self.encode(&frame)
}
}
impl From<gif::DecodingError> for ImageError {
fn from(err: gif::DecodingError) -> ImageError {
use self::gif::DecodingError::*;
match err {
Format(desc) | Internal(desc) => ImageError::FormatError(desc.into()),
Io(io_err) => ImageError::IoError(io_err),
}
}
}