-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebP Extended animation format needs to be rendered into frames… #1925
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,10 +93,10 @@ impl ExtendedImage { | |
|
||
pub(crate) fn color_type(&self) -> ColorType { | ||
match &self.image { | ||
ExtendedImageData::Animation { frames, .. } => &frames[0].image, | ||
ExtendedImageData::Static(image) => image, | ||
// animation frames are always rendered to RGBA8 (see into_frames() function below) | ||
ExtendedImageData::Animation { .. } => ColorType::Rgba8, | ||
ExtendedImageData::Static(image) => image.color_type(), | ||
} | ||
.color_type() | ||
} | ||
|
||
pub(crate) fn into_frames<'a>(self) -> Frames<'a> { | ||
|
@@ -147,6 +147,54 @@ impl ExtendedImage { | |
Frames::new(Box::new(frame_iter)) | ||
} | ||
|
||
pub(crate) fn as_frames<'a>(&'a self) -> Frames<'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
struct FrameIterator<'a> { | ||
image: &'a ExtendedImage, | ||
index: usize, | ||
canvas: RgbaImage, | ||
} | ||
|
||
impl<'a> Iterator for FrameIterator<'a> { | ||
type Item = ImageResult<Frame>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if let ExtendedImageData::Animation { frames, anim_info } = &self.image.image { | ||
let frame = frames.get(self.index); | ||
match frame { | ||
Some(anim_image) => { | ||
self.index += 1; | ||
ExtendedImage::draw_subimage( | ||
&mut self.canvas, | ||
anim_image, | ||
anim_info.background_color, | ||
) | ||
} | ||
None => None, | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
let width = self.info.canvas_width; | ||
let height = self.info.canvas_height; | ||
let background_color = | ||
if let ExtendedImageData::Animation { ref anim_info, .. } = self.image { | ||
anim_info.background_color | ||
} else { | ||
Rgba([0, 0, 0, 0]) | ||
}; | ||
|
||
let frame_iter = FrameIterator { | ||
image: &self, | ||
index: 0, | ||
canvas: RgbaImage::from_pixel(width, height, background_color), | ||
}; | ||
|
||
Frames::new(Box::new(frame_iter)) | ||
} | ||
|
||
pub(crate) fn read_extended_chunks<R: Read>( | ||
reader: &mut R, | ||
mut info: WebPExtendedInfo, | ||
|
@@ -324,19 +372,27 @@ impl ExtendedImage { | |
pub(crate) fn fill_buf(&self, buf: &mut [u8]) { | ||
match &self.image { | ||
// will always have at least one frame | ||
ExtendedImageData::Animation { frames, .. } => &frames[0].image, | ||
ExtendedImageData::Static(image) => image, | ||
ExtendedImageData::Animation { .. } => { | ||
let frame = self.as_frames().nth(0).unwrap().ok().unwrap(); | ||
buf.copy_from_slice(frame.buffer()); | ||
} | ||
ExtendedImageData::Static(image) => image.fill_buf(buf), | ||
} | ||
.fill_buf(buf); | ||
} | ||
|
||
pub(crate) fn get_buf_size(&self) -> usize { | ||
match &self.image { | ||
// will always have at least one frame | ||
ExtendedImageData::Animation { frames, .. } => &frames[0].image, | ||
ExtendedImageData::Static(image) => image, | ||
ExtendedImageData::Animation { .. } => self | ||
.as_frames() | ||
.nth(0) | ||
.unwrap() | ||
.ok() | ||
.unwrap() | ||
.buffer() | ||
.len(), | ||
ExtendedImageData::Static(image) => image.get_buf_size(), | ||
} | ||
.get_buf_size() | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not get rid of this assert. The spec for read_image guarantees a panic if this condition doesn't hold.
Theoretically the values of
{vp8_frame, lossless_frame, extended}.get_buf_size()
should match, but there could be bugsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why doesn't the spec make use of ImageResult instead? Much cleaner.
Also the bug I'm trying to fix happily passed this check and then panicked at
copy_from_slice()
in thefill_buf()
function. So didn't do much good there.