From 07250580e250c0eeb7465790a39823826101b046 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:07:56 +0100 Subject: [PATCH] test(export): share the GIF render and decode setup between the GIF tests Split the byte-parity check against the former gif 0.13 encoder into its own test, with the reference encode in a helper. --- crates/export/tests/it/roundtrip.rs | 94 +++++++++++++++-------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/crates/export/tests/it/roundtrip.rs b/crates/export/tests/it/roundtrip.rs index 1abed1983..224cbfc43 100644 --- a/crates/export/tests/it/roundtrip.rs +++ b/crates/export/tests/it/roundtrip.rs @@ -543,16 +543,40 @@ fn exr_sequence_values_at_or_below_one_are_unchanged() { assert!((c[0] - 1.0).abs() < 1e-4 && (c[1] - lin(0.4)).abs() < 1e-4 && c[2].abs() < 1e-6, "{channels:?}: {c:?}"); } } -#[test] -fn gif_roundtrip() { +/// Render the test project as a GIF with `om` into `out_dir(name)`, and open it for RGBA decoding. +fn render_gif(name: &str, om: &OutputModule) -> (PathBuf, gif::Decoder) { let (p, cid) = project(None); - let d = out_dir("gif"); - let om = OutputModule::for_format(OutputFormat::Gif); - let path = d.join("anim.gif"); - run(&p, cid, &NoFootage, &om, &path); + let path = out_dir(name).join("anim.gif"); + run(&p, cid, &NoFootage, om, &path); let mut opts = gif::DecodeOptions::new(); opts.set_color_output(gif::ColorOutput::RGBA); - let mut dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header"); + let dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header"); + (path, dec) +} + +/// The test project's frames, rendered as PNGs, re-encoded through the former app-local gif 0.13 +/// path (`Frame::from_rgba_speed` at speed 10, 1/10 s per frame, looping). +fn reference_gif() -> Vec { + let (p, cid) = project(None); + let png_pattern = out_dir("gif-byte-parity").join("frame_[#####].png"); + run(&p, cid, &NoFootage, &OutputModule::for_format(OutputFormat::PngSequence), &png_pattern); + let png_pattern = png_pattern.to_string_lossy(); + let mut expected = Vec::new(); + let mut encoder = gif::Encoder::new(&mut expected, W as u16, H as u16, &[]).expect("reference GIF encoder"); + encoder.set_repeat(gif::Repeat::Infinite).expect("reference GIF repeat"); + for frame_number in 0..FRAMES { + let mut rgba = image::open(sequence_path(&png_pattern, frame_number as i64)).expect("reference PNG").to_rgba8().into_raw(); + let mut frame = gif::Frame::from_rgba_speed(W as u16, H as u16, &mut rgba, 10); + frame.delay = 10; + encoder.write_frame(&frame).expect("reference GIF frame"); + } + drop(encoder); + expected +} + +#[test] +fn gif_roundtrip() { + let (path, mut dec) = render_gif("gif", &OutputModule::for_format(OutputFormat::Gif)); assert_eq!((dec.width() as u32, dec.height() as u32), (W, H)); assert_eq!(dec.repeat(), gif::Repeat::Infinite); let mut k = 0; @@ -567,52 +591,30 @@ fn gif_roundtrip() { } assert_eq!(k, FRAMES); assert_eq!(total_delay, 120, "1.2 s in centiseconds"); - - // Re-encode the same rendered RGB pixels through the former app-local gif 0.13 path. The - // shared writer must preserve the complete output bytes, including quantization and LZW data. - let reference_dir = out_dir("gif-byte-parity"); - let png_pattern = reference_dir.join("frame_[#####].png"); - let png_module = OutputModule::for_format(OutputFormat::PngSequence); - run(&p, cid, &NoFootage, &png_module, &png_pattern); - let png_pattern = png_pattern.to_string_lossy(); - let mut expected = Vec::new(); - { - let mut encoder = gif::Encoder::new(&mut expected, W as u16, H as u16, &[]).expect("reference GIF encoder"); - encoder.set_repeat(gif::Repeat::Infinite).expect("reference GIF repeat"); - for frame_number in 0..FRAMES { - let png_path = sequence_path(&png_pattern, frame_number as i64); - let mut rgba = image::open(png_path).expect("reference PNG").to_rgba8().into_raw(); - let mut frame = gif::Frame::from_rgba_speed(W as u16, H as u16, &mut rgba, 10); - frame.delay = 10; - encoder.write_frame(&frame).expect("reference GIF frame"); - } - } - let actual = std::fs::read(&path).expect("GIF bytes"); - assert_eq!(actual.len(), expected.len(), "GIF byte length differs: actual {}, reference {}", actual.len(), expected.len()); - let difference = actual.iter().zip(&expected).position(|(a, b)| a != b); - assert!( - difference.is_none(), - "GIF bytes differ at offset {:?}: actual {:#04x}, reference {:#04x}", - difference, - difference.map_or(0, |i| actual[i]), - difference.map_or(0, |i| expected[i]) - ); if let Some(s) = ffprobe(&path) { assert!(s.starts_with("gif|64|48"), "{s}"); } } #[test] -fn gif_rgba_keeps_background_disposal_and_non_looping() { - let (p, cid) = project(None); - let d = out_dir("gif-rgba"); - let om = OutputModule { channels: Channels::Rgba, gif_loop: false, ..OutputModule::for_format(OutputFormat::Gif) }; - let path = d.join("anim.gif"); - run(&p, cid, &NoFootage, &om, &path); +fn gif_matches_the_former_encoder_byte_for_byte() { + // The shared writer must keep the complete output, quantization and LZW data included. + let (path, _) = render_gif("gif-parity", &OutputModule::for_format(OutputFormat::Gif)); + let actual = std::fs::read(&path).expect("GIF bytes"); + let expected = reference_gif(); + let difference = actual.iter().zip(&expected).position(|(a, b)| a != b); + assert!( + actual.len() == expected.len() && difference.is_none(), + "GIF bytes differ: {} vs {} bytes, first difference at {difference:?}", + actual.len(), + expected.len() + ); +} - let mut opts = gif::DecodeOptions::new(); - opts.set_color_output(gif::ColorOutput::RGBA); - let mut dec = opts.read_info(std::fs::File::open(&path).expect("open")).expect("gif header"); +#[test] +fn gif_rgba_keeps_background_disposal_and_non_looping() { + let om = OutputModule { channels: Channels::Rgba, gif_loop: false, ..OutputModule::for_format(OutputFormat::Gif) }; + let (_, mut dec) = render_gif("gif-rgba", &om); assert_eq!(dec.repeat(), gif::Repeat::Finite(0)); let first = dec.read_next_frame().expect("frame").expect("first frame"); assert_eq!(first.dispose, gif::DisposalMethod::Background);