Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/codecs/webp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,35 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for WebPDecoder<R> {
}

fn read_image(self, buf: &mut [u8]) -> ImageResult<()> {
assert_eq!(u64::try_from(buf.len()), Ok(self.total_bytes()));
Copy link
Contributor

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 bugs

Copy link
Author

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 the fill_buf() function. So didn't do much good there.


match &self.image {
WebPImage::Lossy(vp8_frame) => {
assert_eq!(
buf.len(),
vp8_frame.get_buf_size(),
"Buffer size mismatch, got {} but need {}",
buf.len(),
vp8_frame.get_buf_size()
);
vp8_frame.fill_rgb(buf);
}
WebPImage::Lossless(lossless_frame) => {
assert_eq!(
buf.len(),
lossless_frame.get_buf_size(),
"Buffer size mismatch, got {} but need {}",
buf.len(),
lossless_frame.get_buf_size()
);
lossless_frame.fill_rgba(buf);
}
WebPImage::Extended(extended) => {
assert_eq!(
buf.len(),
extended.get_buf_size(),
"Buffer size mismatch, got {} but need {}",
buf.len(),
extended.get_buf_size()
);
extended.fill_buf(buf);
}
}
Expand Down
74 changes: 65 additions & 9 deletions src/codecs/webp/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -147,6 +147,54 @@ impl ExtendedImage {
Frames::new(Box::new(frame_iter))
}

pub(crate) fn as_frames<'a>(&'a self) -> Frames<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the as_frames API. That's very useful. However, does the code have to be duplicated with into_frames, or could into_frames cust call as_frames?

Copy link
Contributor

@sophie-h sophie-h May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fintelia I think having as_frames for #1869 would be good. Should it maybe take a &mut self just to be safe?

EDIT: It's not public API here, so irrelevant.

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,
Expand Down Expand Up @@ -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()
}
}

Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.