From e95c99e7bcba0f70bae630596355c3ebd372fff6 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:10:01 -0500 Subject: [PATCH] Support common raster output formats by path --- crates/raster-codecs/src/common.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index 868e375d1..fd8c1be60 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -73,16 +73,26 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro Ok(out.into_inner()) } -/// Encode pixels according to a destination filename's extension (`png`, `jpg`, or `jpeg`). +/// Encode pixels according to a destination filename's extension. pub fn encode_for_path(path: impl AsRef, pixels: &RgbaImage) -> Result, CodecError> { let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default(); match extension.to_ascii_lowercase().as_str() { "png" => encode_png(pixels), "jpg" | "jpeg" => encode_jpeg(pixels, 90), + "bmp" => encode_dynamic(pixels, image::ImageFormat::Bmp), + "gif" => encode_dynamic(pixels, image::ImageFormat::Gif), + "tif" | "tiff" => encode_dynamic(pixels, image::ImageFormat::Tiff), + "webp" => encode_dynamic(pixels, image::ImageFormat::WebP), other => Err(CodecError::unsupported("image", format!("cannot encode files with the .{other} extension"))), } } +fn encode_dynamic(pixels: &RgbaImage, format: image::ImageFormat) -> Result, CodecError> { + let mut out = Cursor::new(Vec::new()); + image::DynamicImage::ImageRgba8(pixels.clone()).write_to(&mut out, format).map_err(|e| CodecError::encode("image", e))?; + Ok(out.into_inner()) +} + fn is_heif(bytes: &[u8]) -> bool { bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) }