Skip to content

Commit

Permalink
Fix gif encoder to handle stride padding
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Aug 20, 2017
1 parent 8f3d6d7 commit 793c34e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
23 changes: 21 additions & 2 deletions imageflow_core/src/codecs/gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ impl Encoder for GifEncoder{
pix[0] = pix[2];
pix[2] = a;
}
f = ::gif::Frame::from_rgb(frame.w as u16, frame.h as u16, &mut pixels);
f = from_rgb_with_stride(frame.w as u16, frame.h as u16, &mut pixels, frame.stride as usize);
}else {
for pix in pixels.chunks_mut(4) {
let a = pix[0];
pix[0] = pix[2];
pix[2] = a;
}
f = ::gif::Frame::from_rgba(frame.w as u16, frame.h as u16, &mut pixels);
f = from_rgba_with_stride(frame.w as u16, frame.h as u16, &mut pixels, frame.stride as usize);
}

let mut encoder = ::gif::Encoder::new(io, frame.w as u16, frame.h as u16, &[]).unwrap();
Expand All @@ -159,3 +159,22 @@ impl Encoder for GifEncoder{
}
}


fn remove_padding(width: u16, pixels: &[u8], stride: usize) -> Vec<u8>{
pixels.chunks(stride).flat_map(|s| s[0..width as usize * 4].iter().map(|v| *v)).collect()
}
/// Creates a frame from pixels in RGBA format.
///
/// *Note: This method is not optimized for speed.*
pub fn from_rgba_with_stride(width: u16, height: u16, pixels: &mut [u8], stride: usize) -> ::gif::Frame<'static> {
let mut without_padding = remove_padding(width, pixels, stride);
::gif::Frame::from_rgba(width, height, &mut without_padding)
}

/// Creates a frame from pixels in RGB format.
///
/// *Note: This method is not optimized for speed.*
pub fn from_rgb_with_stride(width: u16, height: u16, pixels: &[u8], stride: usize) -> ::gif::Frame<'static> {
let mut without_padding = remove_padding(width, pixels, stride);
::gif::Frame::from_rgb(width, height, &mut without_padding)
}
35 changes: 35 additions & 0 deletions imageflow_core/tests/visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,41 @@ fn test_jpeg_rotation() {
}

}
//
//#[test]
//fn test_gif_ir4(){
// let matched = compare(Some(s::IoEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/waterhouse.jpg".to_owned())), 500,
// "Read".to_owned(), true, DEBUG_GRAPH, vec![
// s::Node::CommandString{
// kind: s::CommandStringKind::ImageResizer4,
// value: "width=200&height=200&format=gif".to_owned(),
// decode: Some(0),
// encode: None //Some(1)
// }
// ]
// );
// assert!(matched);
//
//}

#[test]
fn smoke_test_gif_ir4(){

let steps = vec![
s::Node::CommandString{
kind: s::CommandStringKind::ImageResizer4,
value: "width=200&height=200&format=gif".to_owned(),
decode: Some(0),
encode: Some(1)
}
];

smoke_test(Some(s::IoEnum::Url("https://s3-us-west-2.amazonaws.com/imageflow-resources/test_inputs/waterhouse.jpg".to_owned())),
Some(s::IoEnum::OutputBuffer),
DEBUG_GRAPH,
steps,
);
}


#[test]
Expand Down

0 comments on commit 793c34e

Please sign in to comment.