diff --git a/src/codecs/webp/extended.rs b/src/codecs/webp/extended.rs index b9fed0b9fc..c7114422d7 100644 --- a/src/codecs/webp/extended.rs +++ b/src/codecs/webp/extended.rs @@ -119,29 +119,35 @@ impl ExtendedImage { first_frame, } = &self.image.image { - let anim_frame_data = frames.get(self.index)?; - 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 anim_frame_data = frames.get(self.index)?; + 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, + ); + } } else { None }