diff --git a/crates/webp/src/anim.rs b/crates/webp/src/anim.rs index 3c6df1a64..d407819e9 100644 --- a/crates/webp/src/anim.rs +++ b/crates/webp/src/anim.rs @@ -56,48 +56,8 @@ pub fn animation(width: u32, height: u32, frames: &[AnimFrame], opts: &AnimOptio if frames.is_empty() { return Err(Error::Encode("an animation needs a frame")); } - let mut anmf = Vec::new(); - let mut any_alpha = opts.alpha; - for f in frames { - if f.width == 0 || f.height == 0 || f.x % 2 != 0 || f.y % 2 != 0 { - return Err(Error::Encode("a frame is empty or not at an even position")); - } - if f.x.checked_add(f.width).is_none_or(|r| r > width) || f.y.checked_add(f.height).is_none_or(|b| b > height) { - return Err(Error::Encode("a frame reaches past the canvas")); - } - if f.duration_ms >= 1 << 24 { - return Err(Error::Encode("a frame duration is 2^24 ms or more")); - } - let mut body = Vec::new(); - u24(&mut body, f.x / 2); - u24(&mut body, f.y / 2); - u24(&mut body, f.width - 1); - u24(&mut body, f.height - 1); - u24(&mut body, f.duration_ms); - // Bit 1: do not blend; bit 0: dispose to the background. - body.push(u8::from(!f.blend) << 1 | u8::from(f.dispose_to_background)); - match f.payload { - Payload::Lossy { vp8, alpha } => { - if f.width > MAX_VP8_DIMENSION || f.height > MAX_VP8_DIMENSION { - return Err(Error::Encode("a lossy frame is at most 16383 pixels a side")); - } - if let Some(a) = alpha { - if a.len() as u64 != u64::from(f.width) * u64::from(f.height) { - return Err(Error::Encode("an alpha plane does not match its frame")); - } - any_alpha = true; - let mut alph = Vec::with_capacity(a.len() + 1); - // Header byte: no pre-processing, no filtering, no compression. - alph.push(0); - alph.extend_from_slice(a); - chunk(&mut body, b"ALPH", &alph)?; - } - chunk(&mut body, b"VP8 ", vp8)?; - } - Payload::Lossless { vp8l } => chunk(&mut body, b"VP8L", vp8l)?, - } - chunk(&mut anmf, b"ANMF", &body)?; - } + // Determine the header flag before writing, without retaining another full animation. + let any_alpha = opts.alpha || frames.iter().any(|f| matches!(f.payload, Payload::Lossy { alpha: Some(_), .. })); let mut body = b"WEBP".to_vec(); let flags = [(opts.icc.is_some(), 0x20u8), (any_alpha, 0x10), (opts.exif.is_some(), 0x08), (opts.xmp.is_some(), 0x04), (true, 0x02)] .iter() @@ -113,7 +73,46 @@ pub fn animation(width: u32, height: u32, frames: &[AnimFrame], opts: &AnimOptio let mut anim = opts.background_bgra.to_vec(); anim.extend_from_slice(&opts.loop_count.to_le_bytes()); chunk(&mut body, b"ANIM", &anim)?; - body.extend_from_slice(&anmf); + + for f in frames { + if f.width == 0 || f.height == 0 || f.x % 2 != 0 || f.y % 2 != 0 { + return Err(Error::Encode("a frame is empty or not at an even position")); + } + if f.x.checked_add(f.width).is_none_or(|r| r > width) || f.y.checked_add(f.height).is_none_or(|b| b > height) { + return Err(Error::Encode("a frame reaches past the canvas")); + } + if f.duration_ms >= 1 << 24 { + return Err(Error::Encode("a frame duration is 2^24 ms or more")); + } + let mut frame_body = Vec::new(); + u24(&mut frame_body, f.x / 2); + u24(&mut frame_body, f.y / 2); + u24(&mut frame_body, f.width - 1); + u24(&mut frame_body, f.height - 1); + u24(&mut frame_body, f.duration_ms); + // Bit 1: do not blend; bit 0: dispose to the background. + frame_body.push(u8::from(!f.blend) << 1 | u8::from(f.dispose_to_background)); + match f.payload { + Payload::Lossy { vp8, alpha } => { + if f.width > MAX_VP8_DIMENSION || f.height > MAX_VP8_DIMENSION { + return Err(Error::Encode("a lossy frame is at most 16383 pixels a side")); + } + if let Some(a) = alpha { + if a.len() as u64 != u64::from(f.width) * u64::from(f.height) { + return Err(Error::Encode("an alpha plane does not match its frame")); + } + let mut alph = Vec::with_capacity(a.len() + 1); + // Header byte: no pre-processing, no filtering, no compression. + alph.push(0); + alph.extend_from_slice(a); + chunk(&mut frame_body, b"ALPH", &alph)?; + } + chunk(&mut frame_body, b"VP8 ", vp8)?; + } + Payload::Lossless { vp8l } => chunk(&mut frame_body, b"VP8L", vp8l)?, + } + chunk(&mut body, b"ANMF", &frame_body)?; + } if let Some(exif) = opts.exif { chunk(&mut body, b"EXIF", exif)?; } diff --git a/crates/webp/src/lib.rs b/crates/webp/src/lib.rs index 82f0a2003..a24dfc656 100644 --- a/crates/webp/src/lib.rs +++ b/crates/webp/src/lib.rs @@ -17,6 +17,7 @@ //! Input is treated as hostile: every read is bounds-checked, chunk sizes are checked against the //! file, and the number of frames recorded is capped by the caller. +pub mod anim; #[cfg(feature = "frames")] pub mod frames; #[cfg(test)] diff --git a/crates/webp/tests/animation_public.rs b/crates/webp/tests/animation_public.rs new file mode 100644 index 000000000..9106f1a79 --- /dev/null +++ b/crates/webp/tests/animation_public.rs @@ -0,0 +1,6 @@ +//! Guard the public module wiring: the implementation must be usable by consumers. +#[test] +fn public_animation_api_refuses_empty_movies() { + let result = craft_webp::anim::animation(1, 1, &[], &craft_webp::anim::AnimOptions::default()); + assert!(result.is_err()); +}