FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
PNG Sequence writes RGB files when Include Alpha Channel is off (fixes #1863)
Marco Arnulfoe8d20a39e74d
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rsindex ebea2550e..613e29135 100644--- a/crates/export/src/lib.rs+++ b/crates/export/src/lib.rs@@ -1785,12 +1785,11 @@ impl Write for SharedBuf {/// Encode straight sRGB RGBA8 pixels as a PNG file (thumbnails, previews).pub fn encode_png(rgba: Vec<u8>, w: u32, h: u32) -> Result<Vec<u8>> {- encode_still(Format::PngSequence, rgba, w, h, false)+ encode_still(Format::PngSequence, rgba, w, h, true)}-/// Encode one still of an image sequence (also Export Frame). PNG always carries the alpha-/// channel it is given; `alpha` keeps it in TIFF, Targa and OpenEXR too, which are otherwise written-/// as RGB.+/// Encode one still of an image sequence (also Export Frame). `alpha` keeps the alpha channel in+/// PNG, TIFF, Targa and OpenEXR, which are otherwise written as RGB./// JPEG uses the default export quality ([`encode_still_with_quality`] picks another).pub fn encode_still(format: Format, rgba: Vec<u8>, w: u32, h: u32, alpha: bool) -> Result<Vec<u8>> {encode_still_with_quality(format, rgba, w, h, alpha, ExportSettings::default().quality)@@ -1820,9 +1819,13 @@ pub fn encode_still_with(format: Format, rgba: Vec<u8>, w: u32, h: u32, alpha: bFormat::TgaSequence => return raster::tga(&rgba, w, h, alpha),Format::DpxSequence => return raster::dpx(&rgba, w, h),Format::ExrSequence => return raster::exr(&rgba, w, h, alpha, exr),- Format::PngSequence => {+ Format::PngSequence if alpha => {image::ImageEncoder::write_image(image::codecs::png::PngEncoder::new(&mut out), &rgba, w, h, image::ExtendedColorType::Rgba8).map_err(enc)?}+ Format::PngSequence => {+ let rgb = raster::rgb(&rgba, w, h)?;+ image::ImageEncoder::write_image(image::codecs::png::PngEncoder::new(&mut out), &rgb, w, h, image::ExtendedColorType::Rgb8).map_err(enc)?+ }Format::TiffSequence | Format::BmpSequence => {let img = image::RgbaImage::from_raw(w, h, rgba).ok_or_else(|| ExportError::Encode("frame size".into()))?;let f = if format == Format::TiffSequence { image::ImageFormat::Tiff } else { image::ImageFormat::Bmp };diff --git a/crates/export/src/settings.rs b/crates/export/src/settings.rsindex ab954adc9..84ca5bacd 100644--- a/crates/export/src/settings.rs+++ b/crates/export/src/settings.rs@@ -624,7 +624,7 @@ impl ExportSettings {px * fps * bpp}Format::Mjpeg | Format::JpegSequence => px * 8.0 * (0.4 + self.quality as f64 / 100.0 * 2.0) * fps / 8.0,- Format::PngSequence => px * 4.0 * 0.45 * 8.0 * fps,+ Format::PngSequence => px * if self.keeps_alpha() { 4.0 } else { 3.0 } * 0.45 * 8.0 * fps,Format::BmpSequence => px * 3.0 * 8.0 * fps,Format::TiffSequence => px * if self.keeps_alpha() { 4.0 } else { 3.0 } * 8.0 * fps,Format::TgaSequence => px * if self.keeps_alpha() { 4.0 } else { 3.0 } * 8.0 * fps,diff --git a/crates/export/src/settings_tests.rs b/crates/export/src/settings_tests.rsindex 22be2d317..cfd028bf8 100644--- a/crates/export/src/settings_tests.rs+++ b/crates/export/src/settings_tests.rs@@ -238,7 +238,8 @@ fn openexr_sequences_compress() {}}-/// `alpha` keeps straight alpha in PNG and TIFF sequences; off (the default) flattens over black (#160).+/// `alpha` keeps straight alpha in PNG and TIFF sequences; off (the default) flattens over black (#160)+/// and writes RGB files without an alpha channel (#1863).#[test]fn image_sequences_keep_alpha_when_asked() {let (p, seq, m) = matte([0.0, 0.0, 1.0, 0.5], 64, 36, None);@@ -248,16 +249,36 @@ fn image_sequences_keep_alpha_when_asked() {let mut s = ExportSettings { format: fmt, path: dir.path(&format!("a.{ext}")), alpha, ..Default::default() };s.range = Some(TimeRange::new(Tick::ZERO, FrameRate::FPS_24.tick_of(2)));export(&p, seq, &s, &m, &Progress::default()).unwrap();- let img = image::open(dir.0.join(format!("a000.{ext}"))).unwrap();+ let file = dir.0.join(format!("a000.{ext}"));+ if fmt == Format::PngSequence {+ let colour_type = std::fs::read(&file).unwrap()[25];+ assert_eq!(colour_type, if alpha { 6 } else { 2 }, "PNG colour type, alpha={alpha}");+ }+ let img = image::open(&file).unwrap();let px = img.to_rgba8().get_pixel(32, 18).0;+ assert_eq!(img.color().has_alpha(), alpha, "{fmt:?} alpha channel written, alpha={alpha}");if alpha {- assert!(img.color().has_alpha(), "{fmt:?} must be written with an alpha channel");assert!((100..=160).contains(&px[3]) && px[2] > 200 && px[0] < 15, "{fmt:?} straight alpha: {px:?}");} else {assert!(px[3] == 255 && px[2] > 100 && px[2] < 240 && px[0] < 15, "{fmt:?} flattened over black: {px:?}");}}}+ // the PNG size estimate counts the channels actually written, as TIFF's does+ let rate = FrameRate::FPS_24;+ let png = |alpha| ExportSettings { format: Format::PngSequence, alpha, ..Default::default() }.estimate_bytes(320, 180, rate, 48_000, rate.tick_of(1));+ assert_eq!(png(false) * 4, png(true) * 3, "PNG estimate: {} RGB vs {} RGBA", png(false), png(true));+}++/// Thumbnails (`encode_png`) and an 8-bit PNG Export Frame keep the alpha they are given: only the+/// sequence's `alpha` switch drops it (#1863).+#[test]+fn png_stills_keep_their_alpha_channel() {+ let half = filmcraft_render::Image::filled(4, 2, [0.0, 0.0, 0.5, 0.5]);+ let thumb = encode_png(half.to_rgba8(), 4, 2).unwrap();+ assert_eq!(thumb[25], 6, "encode_png colour type");+ let frame = still::encode(&half, still::StillFormat::Png, 8).unwrap();+ assert_eq!(frame[25], 6, "Export Frame PNG colour type");}#[test]diff --git a/crates/export/src/still.rs b/crates/export/src/still.rsindex c60c2159f..2af712461 100644--- a/crates/export/src/still.rs+++ b/crates/export/src/still.rs@@ -77,8 +77,8 @@ pub fn encode(image: &Image, format: StillFormat, depth: u8) -> Result<Vec<u8>>StillFormat::Tiff => Format::TiffSequence,_ => Format::BmpSequence,};- // 8-bit TIFF stays RGB, as Export Frame wrote it before; 16-bit output keeps alpha.- return crate::encode_still(f, rgba, w, h, false);+ // 8-bit TIFF and BMP stay RGB, as Export Frame wrote them before; PNG and 16-bit output keep alpha.+ return crate::encode_still(f, rgba, w, h, format == StillFormat::Png);}let capacity = samples.checked_mul(2).ok_or_else(bad)?;let mut bytes = Vec::with_capacity(capacity);