Skip to content

Commit

Permalink
webp: Skip invalid frames in iterator
Browse files Browse the repository at this point in the history
Since the iterator cannot return decoding errors
and just stops with an invalid frame, just
use the next valid frame instead. That should
also be more robust for partially corrupted images.
  • Loading branch information
sophie-h committed Nov 18, 2023
1 parent ec857a1 commit ad94efe
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions src/codecs/webp/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,39 @@ impl ExtendedImage {
first_frame,
} = &self.image.image
{
let frame = frames.get(self.index);
match frame {
Some(anim_frame_data) => {
let anim_frame;
let frame;

if self.index == 0 {
// Use already decoded first frame
anim_frame = first_frame;
} else {
frame = read_anim_frame(
&mut Cursor::new(anim_frame_data),
self.image.info.canvas_width,
self.image.info.canvas_height,
)
.ok()?;
anim_frame = &frame;
};

self.index += 1;
ExtendedImage::draw_subimage(
&mut self.canvas,
anim_frame,
anim_info.background_color,
)
// Loop until a valid frame is found to skip invalid frames
loop {
let frame = frames.get(self.index);
match frame {
Some(anim_frame_data) => {
self.index += 1;
let anim_frame;
let frame;

if self.index == 1 {
// Use already decoded first frame
anim_frame = first_frame;
} else {
frame = read_anim_frame(
&mut Cursor::new(anim_frame_data),
self.image.info.canvas_width,
self.image.info.canvas_height,
);
match &frame {
Ok(frame) => anim_frame = frame,
// Try next frame
Err(_) => continue,
}
};

return ExtendedImage::draw_subimage(
&mut self.canvas,
anim_frame,
anim_info.background_color,
);
}
None => return None,
}
None => None,
}
} else {
None
Expand Down

0 comments on commit ad94efe

Please sign in to comment.