Merge branch 'lane-image-webpanim': animated WebP
Browse files at this commit6 changed files +382 -5
- crates/ec-image/src/lib.rs +3 -0
- crates/ec-image/src/webp/mod.rs +168 -3
- crates/ec-image/tests/differential.rs +106 -2
- crates/ec-image/tests/fuzz.rs +28 -0
- lanes/image-webpanim-r1.report.md +47 -0
- scripts/gen-still-fixtures.sh +30 -0
crates/ec-image/src/lib.rsmodified+3 -0
| @@ -392,6 +392,9 @@ pub struct AnimationFrame { | ||
| 392 | 392 | pub fn decode_animation(data: &[u8]) -> Result<Vec<AnimationFrame>> { |
| 393 | 393 | match ImageFormat::guess(data) { |
| 394 | 394 | Some(ImageFormat::Gif) => gif::decode_animation(data, Limits::default()), |
| 395 | + Some(ImageFormat::WebP) if webp::is_animated(data) => { | |
| 396 | + webp::decode_frames(data, Limits::default()) | |
| 397 | + } | |
| 395 | 398 | _ => Ok(vec![AnimationFrame { |
| 396 | 399 | image: decode(data)?, |
| 397 | 400 | delay_num: 0, |
crates/ec-image/src/webp/mod.rsmodified+168 -3
| @@ -1,9 +1,11 @@ | ||
| 1 | 1 | //! WebP: the RIFF container, its lossy (`VP8 `) and lossless (`VP8L`) payloads, |
| 2 | 2 | //! and the separate alpha plane (`ALPH`) an extended file may carry. |
| 3 | 3 | //! |
| 4 | -//! Animation is refused by name. A still decoder that quietly handed back the | |
| 5 | -//! first frame of an animation would make "this file plays" and "this file | |
| 6 | -//! decoded" indistinguishable to its caller. | |
| 4 | +//! `decode` returns one still picture and refuses an animation by name: a still | |
| 5 | +//! decoder that quietly handed back the first frame would make "this file | |
| 6 | +//! plays" and "this file decoded" indistinguishable to its caller. The | |
| 7 | +//! sequence is `decode_frames`, which composites the `ANMF` frames onto the | |
| 8 | +//! canvas. | |
| 7 | 9 | |
| 8 | 10 | pub mod vp8; |
| 9 | 11 | mod vp8_tables; |
| @@ -46,6 +48,23 @@ fn chunks(data: &[u8]) -> Result<Vec<Chunk<'_>>> { | ||
| 46 | 48 | Ok(out) |
| 47 | 49 | } |
| 48 | 50 | |
| 51 | +/// Whether the file is an animation rather than a still. | |
| 52 | +/// | |
| 53 | +/// The question is answerable from the container alone -- the VP8X animation | |
| 54 | +/// flag, or an `ANIM`/`ANMF` chunk -- so it is answered even for a file whose | |
| 55 | +/// frames fail to decode. Bytes that are not a readable WebP are not an | |
| 56 | +/// animation. | |
| 57 | +pub fn is_animated(data: &[u8]) -> bool { | |
| 58 | + let Ok(chunks) = chunks(data) else { | |
| 59 | + return false; | |
| 60 | + }; | |
| 61 | + chunks.iter().any(|chunk| match &chunk.tag { | |
| 62 | + b"VP8X" => chunk.data.first().is_some_and(|flags| flags & 0x02 != 0), | |
| 63 | + b"ANIM" | b"ANMF" => true, | |
| 64 | + _ => false, | |
| 65 | + }) | |
| 66 | +} | |
| 67 | + | |
| 49 | 68 | /// Dimensions from the container's headers alone. |
| 50 | 69 | pub fn info(data: &[u8]) -> Result<Info> { |
| 51 | 70 | let chunks = chunks(data)?; |
| @@ -131,6 +150,21 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | ||
| 131 | 150 | } |
| 132 | 151 | |
| 133 | 152 | let (tag, body) = image.ok_or_else(|| Error::corrupt("WebP: no VP8 or VP8L chunk"))?; |
| 153 | + decode_payload(tag, body, alpha, meta, limits) | |
| 154 | +} | |
| 155 | + | |
| 156 | +/// Decode one image payload -- a `VP8 ` or `VP8L` body, with its optional | |
| 157 | +/// `ALPH` plane -- into pixels. | |
| 158 | +/// | |
| 159 | +/// A still file and one frame of an animation differ only in where these | |
| 160 | +/// chunks were found, so both paths land here. | |
| 161 | +fn decode_payload( | |
| 162 | + tag: &[u8; 4], | |
| 163 | + body: &[u8], | |
| 164 | + alpha: Option<&[u8]>, | |
| 165 | + meta: Metadata, | |
| 166 | + limits: Limits, | |
| 167 | +) -> Result<Image> { | |
| 134 | 168 | if tag == b"VP8L" { |
| 135 | 169 | let argb = vp8l::decode(body, limits)?; |
| 136 | 170 | let mut rgba = Vec::with_capacity(argb.pixels.len() * 4); |
| @@ -193,6 +227,137 @@ pub fn decode(data: &[u8], limits: Limits) -> Result<Image> { | ||
| 193 | 227 | }) |
| 194 | 228 | } |
| 195 | 229 | |
| 230 | +/// Decode an animated WebP into composited canvas frames. | |
| 231 | +/// | |
| 232 | +/// Every frame comes back as the whole canvas in RGBA, the way a player wants | |
| 233 | +/// it: the frame rectangles, the two blending methods and the dispose-to- | |
| 234 | +/// background rule are applied here rather than left to the caller. | |
| 235 | +pub fn decode_frames(data: &[u8], limits: Limits) -> Result<Vec<crate::AnimationFrame>> { | |
| 236 | + let chunks = chunks(data)?; | |
| 237 | + let (canvas_width, canvas_height) = { | |
| 238 | + let info = info(data)?; | |
| 239 | + (info.width, info.height) | |
| 240 | + }; | |
| 241 | + limits.check(canvas_width, canvas_height)?; | |
| 242 | + | |
| 243 | + let mut frames: Vec<crate::AnimationFrame> = Vec::new(); | |
| 244 | + // The canvas starts fully transparent. The `ANIM` background colour is | |
| 245 | + // advice to a viewer about what to show behind the animation, not a colour | |
| 246 | + // the frames are composited onto -- libwebp's own animation decoder | |
| 247 | + // ignores it the same way. | |
| 248 | + let mut canvas = vec![0u8; canvas_width as usize * canvas_height as usize * 4]; | |
| 249 | + let mut dispose: Option<(u32, u32, u32, u32)> = None; | |
| 250 | + | |
| 251 | + for chunk in &chunks { | |
| 252 | + if &chunk.tag != b"ANMF" { | |
| 253 | + continue; | |
| 254 | + } | |
| 255 | + let d = chunk.data; | |
| 256 | + if d.len() < 16 { | |
| 257 | + return Err(Error::corrupt("WebP: short ANMF header")); | |
| 258 | + } | |
| 259 | + let u24 = |at: usize| u32::from_le_bytes([d[at], d[at + 1], d[at + 2], 0]); | |
| 260 | + let (x, y) = (u24(0) * 2, u24(3) * 2); | |
| 261 | + let (width, height) = (u24(6) + 1, u24(9) + 1); | |
| 262 | + let duration = u24(12); | |
| 263 | + let blend_over = d[15] & 0x02 == 0; | |
| 264 | + let dispose_to_background = d[15] & 0x01 != 0; | |
| 265 | + if x + width > canvas_width || y + height > canvas_height { | |
| 266 | + return Err(Error::corrupt(format!( | |
| 267 | + "WebP: frame {width}x{height}+{x}+{y} runs off a {canvas_width}x{canvas_height} canvas" | |
| 268 | + ))); | |
| 269 | + } | |
| 270 | + | |
| 271 | + // The previous frame's disposal happens before this one is drawn, so a | |
| 272 | + // caller that keeps every frame still sees each one whole. | |
| 273 | + if let Some((x, y, width, height)) = dispose.take() { | |
| 274 | + for row in y..y + height { | |
| 275 | + let at = (row as usize * canvas_width as usize + x as usize) * 4; | |
| 276 | + canvas[at..at + width as usize * 4].fill(0); | |
| 277 | + } | |
| 278 | + } | |
| 279 | + | |
| 280 | + let mut alpha: Option<&[u8]> = None; | |
| 281 | + let mut payload: Option<(&[u8; 4], &[u8])> = None; | |
| 282 | + let mut at = 16usize; | |
| 283 | + while at + 8 <= d.len() { | |
| 284 | + let tag = [d[at], d[at + 1], d[at + 2], d[at + 3]]; | |
| 285 | + let size = u32::from_le_bytes([d[at + 4], d[at + 5], d[at + 6], d[at + 7]]) as usize; | |
| 286 | + let body = d | |
| 287 | + .get(at + 8..at + 8 + size) | |
| 288 | + .ok_or_else(|| Error::corrupt("WebP: ANMF sub-chunk runs past the frame"))?; | |
| 289 | + match &tag { | |
| 290 | + b"ALPH" => alpha = Some(body), | |
| 291 | + // Only the first image chunk of a frame is the frame; the tag | |
| 292 | + // is kept as a static so the borrow of `d` does not outlive | |
| 293 | + // this iteration. | |
| 294 | + b"VP8 " if payload.is_none() => payload = Some((b"VP8 ", body)), | |
| 295 | + b"VP8L" if payload.is_none() => payload = Some((b"VP8L", body)), | |
| 296 | + _ => {} | |
| 297 | + } | |
| 298 | + at += 8 + size + (size & 1); | |
| 299 | + } | |
| 300 | + let (tag, body) = | |
| 301 | + payload.ok_or_else(|| Error::corrupt("WebP: ANMF without an image chunk"))?; | |
| 302 | + let sub = decode_payload(tag, body, alpha, Metadata::default(), limits)?; | |
| 303 | + if sub.width != width || sub.height != height { | |
| 304 | + return Err(Error::corrupt(format!( | |
| 305 | + "WebP: ANMF says {width}x{height}, the payload decodes {}x{}", | |
| 306 | + sub.width, sub.height | |
| 307 | + ))); | |
| 308 | + } | |
| 309 | + let pixels = sub.to_rgba8(); | |
| 310 | + | |
| 311 | + for row in 0..height as usize { | |
| 312 | + for col in 0..width as usize { | |
| 313 | + let src = (row * width as usize + col) * 4; | |
| 314 | + let dst = ((row + y as usize) * canvas_width as usize + col + x as usize) * 4; | |
| 315 | + let src = &pixels[src..src + 4]; | |
| 316 | + if !blend_over || src[3] == 255 { | |
| 317 | + canvas[dst..dst + 4].copy_from_slice(src); | |
| 318 | + continue; | |
| 319 | + } | |
| 320 | + if src[3] == 0 { | |
| 321 | + continue; | |
| 322 | + } | |
| 323 | + // Source-over in eight-bit alpha, rounded rather than | |
| 324 | + // truncated: a run of blended frames drifts visibly otherwise. | |
| 325 | + let sa = u32::from(src[3]); | |
| 326 | + let da = u32::from(canvas[dst + 3]); | |
| 327 | + let out_a = sa + da * (255 - sa) / 255; | |
| 328 | + for c in 0..3 { | |
| 329 | + let s = u32::from(src[c]) * sa; | |
| 330 | + let dcol = u32::from(canvas[dst + c]) * da * (255 - sa) / 255; | |
| 331 | + canvas[dst + c] = (s + dcol + out_a / 2) | |
| 332 | + .checked_div(out_a) | |
| 333 | + .unwrap_or(0) | |
| 334 | + .min(255) as u8; | |
| 335 | + } | |
| 336 | + canvas[dst + 3] = out_a as u8; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + if dispose_to_background { | |
| 341 | + dispose = Some((x, y, width, height)); | |
| 342 | + } | |
| 343 | + frames.push(crate::AnimationFrame { | |
| 344 | + image: Image { | |
| 345 | + width: canvas_width, | |
| 346 | + height: canvas_height, | |
| 347 | + pixels: Pixels::Rgba8(canvas.clone()), | |
| 348 | + meta: Metadata::default(), | |
| 349 | + }, | |
| 350 | + delay_num: duration, | |
| 351 | + delay_den: 1000, | |
| 352 | + }); | |
| 353 | + } | |
| 354 | + | |
| 355 | + if frames.is_empty() { | |
| 356 | + return Err(Error::corrupt("WebP: an animation with no ANMF frame")); | |
| 357 | + } | |
| 358 | + Ok(frames) | |
| 359 | +} | |
| 360 | + | |
| 196 | 361 | /// BT.601 studio-swing YCbCr to RGB, in 16-bit fixed point. |
| 197 | 362 | fn yuv_to_rgb(y: u8, u: u8, v: u8) -> [u8; 3] { |
| 198 | 363 | let y = (i32::from(y) - 16) * 76284; |
crates/ec-image/tests/differential.rsmodified+106 -2
| @@ -317,8 +317,8 @@ fn every_fixture_decodes() { | ||
| 317 | 317 | for extension in ["png", "jpg", "webp"] { |
| 318 | 318 | for path in corpus(extension) { |
| 319 | 319 | let name = path.file_name().unwrap().to_string_lossy().to_string(); |
| 320 | - if name.contains("animated") { | |
| 321 | - continue; // refused by name; `an_animated_webp_is_refused_by_name` covers it | |
| 320 | + if name.contains("animated") || name.starts_with("anim-") { | |
| 321 | + continue; // an animation is not a still; the animation tests cover it | |
| 322 | 322 | } |
| 323 | 323 | let bytes = std::fs::read(&path).unwrap(); |
| 324 | 324 | count += 1; |
| @@ -436,3 +436,107 @@ fn a_still_of_any_format_is_one_frame_of_animation() { | ||
| 436 | 436 | ec_image::decode(&bytes).unwrap().to_rgba8() |
| 437 | 437 | ); |
| 438 | 438 | } |
| 439 | + | |
| 440 | +#[test] | |
| 441 | +fn an_animated_webp_composites_frame_for_frame_like_the_incumbent() { | |
| 442 | + one_animated_webp("animated.webp"); | |
| 443 | +} | |
| 444 | + | |
| 445 | +fn one_animated_webp(fixture: &str) { | |
| 446 | + use image::AnimationDecoder; | |
| 447 | + | |
| 448 | + let path = fixtures().join(fixture); | |
| 449 | + let Ok(bytes) = std::fs::read(&path) else { | |
| 450 | + skip(fixture); | |
| 451 | + return; | |
| 452 | + }; | |
| 453 | + assert!( | |
| 454 | + ec_image::webp::is_animated(&bytes), | |
| 455 | + "the fixture is not an animation" | |
| 456 | + ); | |
| 457 | + let ours = ec_image::decode_animation(&bytes).expect("our animation decode"); | |
| 458 | + let theirs: Vec<image::Frame> = | |
| 459 | + image::codecs::webp::WebPDecoder::new(std::io::Cursor::new(&bytes)) | |
| 460 | + .expect("incumbent decoder") | |
| 461 | + .into_frames() | |
| 462 | + .collect::<Result<Vec<_>, _>>() | |
| 463 | + .expect("incumbent frames"); | |
| 464 | + | |
| 465 | + assert_eq!(ours.len(), theirs.len(), "frame count"); | |
| 466 | + assert!(ours.len() > 1, "the fixture is not an animation"); | |
| 467 | + for (i, (ours, theirs)) in ours.iter().zip(&theirs).enumerate() { | |
| 468 | + let (num, den) = theirs.delay().numer_denom_ms(); | |
| 469 | + let ours_ms = f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den); | |
| 470 | + let theirs_ms = f64::from(num) / f64::from(den); | |
| 471 | + assert!( | |
| 472 | + (ours_ms - theirs_ms).abs() < 0.5, | |
| 473 | + "frame {i}: {ours_ms} ms against the incumbent's {theirs_ms} ms" | |
| 474 | + ); | |
| 475 | + assert_eq!( | |
| 476 | + (ours.image.width, ours.image.height), | |
| 477 | + theirs.buffer().dimensions(), | |
| 478 | + "frame {i} is not the whole canvas" | |
| 479 | + ); | |
| 480 | + // The frames are lossy VP8, so the payload is only as exact as the two | |
| 481 | + // VP8 decoders agree; the bar here is on the compositing, which is | |
| 482 | + // exact arithmetic over whatever the payload decoded to. | |
| 483 | + let a = ours.image.to_rgba8(); | |
| 484 | + let b = theirs.buffer().as_raw(); | |
| 485 | + let max = a | |
| 486 | + .iter() | |
| 487 | + .zip(b) | |
| 488 | + .map(|(&x, &y)| x.abs_diff(y)) | |
| 489 | + .max() | |
| 490 | + .unwrap_or(0); | |
| 491 | + let psnr = psnr(&a, b); | |
| 492 | + eprintln!("{fixture} frame {i}: {ours_ms:.0} ms, max delta {max}, {psnr:.1} dB"); | |
| 493 | + assert!( | |
| 494 | + max <= 5 && psnr > 48.0, | |
| 495 | + "{fixture} frame {i}: max delta {max}, {psnr:.1} dB" | |
| 496 | + ); | |
| 497 | + } | |
| 498 | +} | |
| 499 | + | |
| 500 | +/// Alpha blending and dispose-to-background, against libwebp itself. | |
| 501 | +/// | |
| 502 | +/// The `image` crate is not the oracle here: it ignores the ANMF | |
| 503 | +/// dispose-to-background flag, so the frame after a disposal keeps pixels | |
| 504 | +/// libwebp clears -- 6912 of 6912 pixels differ on this fixture, and libwebp | |
| 505 | +/// agrees with us on every one. The goldens are libwebp's own composited | |
| 506 | +/// frames, written by `scripts/gen-still-fixtures.sh`. | |
| 507 | +#[test] | |
| 508 | +fn webp_disposal_and_blending_match_libwebp() { | |
| 509 | + let path = fixtures().join("anim-alpha.webp"); | |
| 510 | + let Ok(bytes) = std::fs::read(&path) else { | |
| 511 | + skip("anim-alpha.webp"); | |
| 512 | + return; | |
| 513 | + }; | |
| 514 | + let goldens: Vec<PathBuf> = (0..) | |
| 515 | + .map(|i| fixtures().join(format!("anim-alpha-f{i}.png"))) | |
| 516 | + .take_while(|p| p.exists()) | |
| 517 | + .collect(); | |
| 518 | + if goldens.is_empty() { | |
| 519 | + skip("anim-alpha-f0.png (libwebp goldens; Pillow not installed?)"); | |
| 520 | + return; | |
| 521 | + } | |
| 522 | + | |
| 523 | + let ours = ec_image::decode_animation(&bytes).expect("our animation decode"); | |
| 524 | + assert_eq!(ours.len(), goldens.len(), "frame count against libwebp"); | |
| 525 | + for (i, (ours, golden)) in ours.iter().zip(&goldens).enumerate() { | |
| 526 | + let want = ec_image::open(golden).expect("golden"); | |
| 527 | + assert_eq!( | |
| 528 | + (ours.image.width, ours.image.height), | |
| 529 | + (want.width, want.height), | |
| 530 | + "frame {i} is not the whole canvas" | |
| 531 | + ); | |
| 532 | + assert_eq!(ours.image.to_rgba8(), want.to_rgba8(), "frame {i} samples"); | |
| 533 | + eprintln!( | |
| 534 | + "frame {i}: {} ms, exact against libwebp", | |
| 535 | + f64::from(ours.delay_num) * 1000.0 / f64::from(ours.delay_den) | |
| 536 | + ); | |
| 537 | + } | |
| 538 | + // The delays come from the same headers, but a player that gets them wrong | |
| 539 | + // plays the animation at the wrong speed however exact the pixels are. | |
| 540 | + let delays: Vec<u32> = ours.iter().map(|f| f.delay_num).collect(); | |
| 541 | + assert_eq!(delays, vec![60, 90, 30], "per-frame durations in ms"); | |
| 542 | +} |
crates/ec-image/tests/fuzz.rsmodified+28 -0
| @@ -169,6 +169,34 @@ fn a_gif_animation_survives_mutation() { | ||
| 169 | 169 | eprintln!("gif-animation: {ROUNDS} mutations, {decoded} still decoded"); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | +/// The WebP animation path parses its own ANMF headers and composites onto a | |
| 173 | +/// canvas sized by a different chunk, so a mutation can make the two disagree. | |
| 174 | +#[test] | |
| 175 | +fn an_animated_webp_survives_mutation() { | |
| 176 | + let Ok(seed) = std::fs::read(fixtures().join("anim-alpha.webp")) else { | |
| 177 | + eprintln!("skipped: fixtures/stills not generated; run scripts/gen-still-fixtures.sh"); | |
| 178 | + return; | |
| 179 | + }; | |
| 180 | + let mut rng = Rng(0x2545_f491_4f6c_dd1d ^ 0x5eed); | |
| 181 | + let mut decoded = 0usize; | |
| 182 | + for round in 0..ROUNDS { | |
| 183 | + let head = if round % 2 == 0 { Some(48) } else { None }; | |
| 184 | + let data = mutate(&mut rng, &seed, head); | |
| 185 | + if let Ok(frames) = ec_image::decode_animation(&data) { | |
| 186 | + decoded += 1; | |
| 187 | + for frame in &frames { | |
| 188 | + let image = &frame.image; | |
| 189 | + assert_eq!( | |
| 190 | + image.to_rgba8().len(), | |
| 191 | + (image.width as usize) * (image.height as usize) * 4, | |
| 192 | + "webp-animation round {round}: buffer does not match the dimensions" | |
| 193 | + ); | |
| 194 | + } | |
| 195 | + } | |
| 196 | + } | |
| 197 | + eprintln!("webp-animation: {ROUNDS} mutations, {decoded} still decoded"); | |
| 198 | +} | |
| 199 | + | |
| 172 | 200 | #[test] |
| 173 | 201 | fn arbitrary_bytes_behind_a_signature_are_refused_not_believed() { |
| 174 | 202 | let signatures: [&[u8]; 4] = [ |
lanes/image-webpanim-r1.report.mdadded+47 -0
| @@ -0,0 +1,47 @@ | ||
| 1 | +# lane-image-webpanim r1 — animated WebP | |
| 2 | + | |
| 3 | +## What landed | |
| 4 | + | |
| 5 | +`webp::is_animated`, answered from the container alone (the VP8X animation flag | |
| 6 | +or an `ANIM`/`ANMF` chunk), and `webp::decode_frames`, which walks the `ANMF` | |
| 7 | +chunks and composites them onto the canvas: frame rectangles, both blending | |
| 8 | +methods, dispose-to-background applied before the next frame is drawn, and the | |
| 9 | +duration of each frame in milliseconds. `decode_animation` routes an animated | |
| 10 | +WebP here; `decode` still refuses one by name. | |
| 11 | + | |
| 12 | +The still and animated paths share the payload decoder — a `VP8 ` or `VP8L` | |
| 13 | +body with its optional `ALPH` plane — which was factored out of `decode` as | |
| 14 | +`decode_payload`. The canvas starts fully transparent; the `ANIM` background | |
| 15 | +colour is advice to a viewer, not a colour the frames are composited onto, | |
| 16 | +which is also what libwebp does. | |
| 17 | + | |
| 18 | +## The oracle is libwebp, not the incumbent | |
| 19 | + | |
| 20 | +`image` 0.25.10 **ignores the ANMF dispose-to-background flag**. On the alpha | |
| 21 | +fixture its third frame keeps pixels libwebp clears: all 6912 pixels of the | |
| 22 | +canvas differ, and libwebp agrees with us on every one. So the fixture that | |
| 23 | +exercises blending and disposal is gated against libwebp's own composited | |
| 24 | +frames (written by `scripts/gen-still-fixtures.sh` through Pillow), and the | |
| 25 | +incumbent stays the oracle only for the opaque fixture, where the two agree. | |
| 26 | + | |
| 27 | +ffmpeg cannot arbitrate: its WebP decoder refuses an animation outright | |
| 28 | +(`image data not found`). | |
| 29 | + | |
| 30 | +## Gates | |
| 31 | + | |
| 32 | +| Gate | Result | | |
| 33 | +| --- | --- | | |
| 34 | +| `webp_disposal_and_blending_match_libwebp` | 3/3 frames **pixel-exact**, delays 60/90/30 ms exact | | |
| 35 | +| `an_animated_webp_composites_frame_for_frame_like_the_incumbent` | 3/3 frames, max delta 3, ≥52.7 dB (lossy VP8 payload) | | |
| 36 | +| `an_animated_webp_is_refused_by_name` | still `decode` still refuses an animation | | |
| 37 | +| `an_animated_webp_survives_mutation` | 10 000 mutations of the animation path, no panic, no hang | | |
| 38 | +| `cargo test -p ec-image` | 25 + 13 + 9 + 2, 0 failed | | |
| 39 | +| clippy | no new lint from this lane (the 1.98 toolchain fires new lints repo-wide; that is a separate sweep) | | |
| 40 | + | |
| 41 | +## New fixtures | |
| 42 | + | |
| 43 | +`anim-alpha.webp` — three lossless frames carrying alpha, flags `0x2` (no | |
| 44 | +blend), `0x3` (no blend + dispose to background) and `0x0` (blend over what the | |
| 45 | +disposal left). The pre-existing `animated.webp` is opaque and disposes | |
| 46 | +nothing, so it left both of those paths untested. | |
| 47 | +`anim-alpha-f{0,1,2}.png` — libwebp's composited frames, the goldens above. |
scripts/gen-still-fixtures.shmodified+30 -0
| @@ -92,6 +92,36 @@ ffmpeg -loglevel error -y -i "$out/source-flat.png" -c:v libwebp -lossless 1 \ | ||
| 92 | 92 | # writes this one: its output really does carry ANIM/ANMF chunks. |
| 93 | 93 | magick -delay 10 "$out/source.png" "$out/source-flat.png" "$out/source-odd.png" \ |
| 94 | 94 | -loop 0 "$out/animated.webp" |
| 95 | +# A second animation whose frames carry alpha and ask for dispose-to-background: | |
| 96 | +# the first one is opaque and disposes nothing, so it leaves the blending and | |
| 97 | +# the disposal rectangle untested. Lossless, so the frames are exact and the | |
| 98 | +# only thing the comparison can catch is the compositing. | |
| 99 | +magick "$out/source-alpha.png" -resize 96x72! "$out/anim-a.png" | |
| 100 | +magick "$out/source-alpha.png" -resize 96x72! -channel A -evaluate multiply 0.5 \ | |
| 101 | + +channel "$out/anim-b.png" | |
| 102 | +magick "$out/source-flat.png" -resize 48x36! -background none -extent 96x72 \ | |
| 103 | + "$out/anim-c.png" | |
| 104 | +magick -dispose Background -delay 6 "$out/anim-a.png" -delay 9 "$out/anim-b.png" \ | |
| 105 | + -delay 3 "$out/anim-c.png" -loop 0 -define webp:lossless=true \ | |
| 106 | + "$out/anim-alpha.webp" | |
| 107 | +rm -f "$out/anim-a.png" "$out/anim-b.png" "$out/anim-c.png" | |
| 108 | +# libwebp's own composited frames, through Pillow, as the reference for that | |
| 109 | +# animation: the `image` crate ignores the dispose-to-background flag, so it | |
| 110 | +# cannot be the oracle for the frame that follows a disposal. | |
| 111 | +if python3 -c "import PIL" 2>/dev/null; then | |
| 112 | + python3 - "$out" <<'PILLOW' | |
| 113 | +import sys | |
| 114 | +from PIL import Image | |
| 115 | + | |
| 116 | +out = sys.argv[1] | |
| 117 | +animation = Image.open(f"{out}/anim-alpha.webp") | |
| 118 | +for i in range(animation.n_frames): | |
| 119 | + animation.seek(i) | |
| 120 | + animation.convert("RGBA").save(f"{out}/anim-alpha-f{i}.png") | |
| 121 | +PILLOW | |
| 122 | +else | |
| 123 | + echo "skipped anim-alpha-f*.png: python3 Pillow is not installed" >&2 | |
| 124 | +fi | |
| 95 | 125 | |
| 96 | 126 | # --- GIF: still, interlaced, transparent, odd-size, animated --------------- |
| 97 | 127 | # GIF is palette-only, so every source is quantised first; the differential bar |