Skip to content

Commit

Permalink
Refactor to upgrade gif to v0.11
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed May 20, 2022
1 parent 21a8814 commit 89720f2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion imageflow_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ slotmap = "1"
base64 = "0.13"
hex = "0.4"

gif = "0.10"
gif = "0.11"
rgb = { version = "0.8", features = ["argb"] }
imagequant = "4"
lodepng = "3"
Expand Down
21 changes: 12 additions & 9 deletions imageflow_core/src/codecs/gif/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ mod bgra;
use self::bgra::BGRA8;
use self::screen::Screen;
use crate::gif::Frame;
use crate::gif::SetParameter;
use std::rc::Rc;
use lcms2_sys::cmsAllocProfileSequenceDescription;
use crate::io::IoProxyProxy;
use crate::io::IoProxyRef;
use crate::graphics::bitmaps::{BitmapKey, ColorSpace, BitmapCompositing};

pub struct GifDecoder{
reader: ::gif::Reader<IoProxy>,
reader: ::gif::Decoder<IoProxy>,
screen: Screen,
buffer: Option<Vec<u8>>,
last_frame: Option<Frame<'static>>,
Expand All @@ -33,12 +33,15 @@ pub struct GifDecoder{
impl GifDecoder {
pub fn create(c: &Context, io: IoProxy, io_id: i32) -> Result<GifDecoder> {

let mut decoder = ::gif::Decoder::new(io);

// Important:
decoder.set(::gif::ColorOutput::Indexed);
let mut options = ::gif::Decoder::<IoProxy>::build();
options.allow_unknown_blocks(true);
options.set_memory_limit(::gif::MemoryLimit(8000*8000));
options.set_color_output(::gif::ColorOutput::Indexed); // Important

let reader = decoder.read_info().map_err(|e| FlowError::from(e).at(here!()))?;

let reader = options.read_info(io)
.map_err(|e| FlowError::from(e).at(here!()))?;

let screen = Screen::new(&reader);

Expand Down Expand Up @@ -247,7 +250,7 @@ impl GifEncoder{
io_id,
io_ref: io_ref.clone(),
// Global color table??
encoder: ::gif::Encoder::new(IoProxyProxy(io_ref), bitmap.w() as u16, bitmap.h() as u16, &[]).map_err(|e| FlowError::from_encoder(e).at(here!()))?,
encoder: ::gif::Encoder::new(IoProxyProxy(io_ref), bitmap.w() as u16, bitmap.h() as u16, &[]).map_err(|e| FlowError::from(e).at(here!()))?,
frame_ix: 0
})
}
Expand Down Expand Up @@ -303,7 +306,7 @@ impl Encoder for GifEncoder{
// Only write before any frames
if let Some(r) = repeat {
// eprintln!("Writing repeat");
self.encoder.write_extension(::gif::ExtensionData::Repetitions(r)).map_err(|e| FlowError::from_encoder(e).at(here!()))?;
self.encoder.write_extension(::gif::ExtensionData::Repetitions(r)).map_err(|e| FlowError::from(e).at(here!()))?;
}else{
// eprintln!("Skipping repeat");
}
Expand All @@ -318,7 +321,7 @@ impl Encoder for GifEncoder{
// rect
// transparency??

self.encoder.write_frame(&f).map_err(|e| FlowError::from_encoder(e).at(here!()))?;
self.encoder.write_frame(&f).map_err(|e| FlowError::from(e).at(here!()))?;

self.frame_ix+=1;
Ok(
Expand Down
6 changes: 3 additions & 3 deletions imageflow_core/src/codecs/gif/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ impl Screen {
/// Initialize empty screen from GIF Reader.
/// Make sure Reader is set to use Indexed color.
/// `decoder.set(gif::ColorOutput::Indexed);`
pub fn new<T: io::Read>(reader: &gif::Reader<T>) -> Self {
pub fn new<T: io::Read>(reader: &gif::Decoder<T>) -> Self {
let pal = reader.global_palette().map(|palette_bytes| to_bgra(palette_bytes));

let pixels = reader.width() as usize * reader.height() as usize;
let bg_color = if let (Some(bg_index), Some(pal)) = (reader.bg_color(), pal.as_ref()) {
pal[bg_index]
pal[bg_index].clone()
} else {
BGRA8::default()
};
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Screen {
continue;
}
}
*dst = pal[src as usize];
*dst = pal[src as usize].clone();
}

Ok(())
Expand Down
13 changes: 12 additions & 1 deletion imageflow_core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,22 @@ impl From<::gif::DecodingError> for FlowError{
fn from(f: ::gif::DecodingError) -> Self {
match f {
::gif::DecodingError::Io(e) => FlowError::without_location(ErrorKind::DecodingIoError, format!("{:?}", e)),
::gif::DecodingError::Internal(msg) => FlowError::without_location(ErrorKind::InternalError,format!("Internal error in gif decoder: {:?}",msg)),
//::gif::DecodingError::Internal(msg) => FlowError::without_location(ErrorKind::InternalError,format!("Internal error in gif decoder: {:?}",msg)),
::gif::DecodingError::Format(msg) => FlowError::without_location(ErrorKind::GifDecodingError,format!("{:?}",msg))
}
}
}

impl From<::gif::EncodingError> for FlowError{
fn from(f: ::gif::EncodingError) -> Self {
match f {
::gif::EncodingError::Io(e) => FlowError::without_location(ErrorKind::EncodingIoError, format!("{:?}", e)),
::gif::EncodingError::Format(msg) => FlowError::without_location(ErrorKind::GifEncodingError,format!("{:?}",msg))
}
}
}


impl From<::imageflow_helpers::colors::ParseColorError> for FlowError{
fn from(f: ::imageflow_helpers::colors::ParseColorError) -> Self {
match f {
Expand Down

0 comments on commit 89720f2

Please sign in to comment.