From 827667a321d293904625ba6aaece3ccb87d283f1 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:34:00 +0100 Subject: [PATCH] refactor(codecs): read header sizes and write DIB PNGs with craft-image-codecs header_size reads every format's header through craft-image-codecs (probe_allocation_size: stored size with no limit; a GIF's logical screen through probe_size), no longer through the image crate's readers. Metafile DIBs are written as PNG by craft-image-codecs. --- crates/doc/src/pixels.rs | 28 ++++++++++------------------ crates/metafile/src/dib.rs | 9 +++------ 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/crates/doc/src/pixels.rs b/crates/doc/src/pixels.rs index b4659f8957..f589ca9eda 100644 --- a/crates/doc/src/pixels.rs +++ b/crates/doc/src/pixels.rs @@ -31,26 +31,18 @@ fn rgba_within(bytes: &[u8], max: u64) -> Option { Some(reader.decode().ok()?.into_rgba8()) } -/// An image file's stored size in pixels, read from its header without decoding any pixels: -/// PNG's IHDR and a JPEG's frame through craft-image-codecs, other formats through the image -/// crate's header readers (as before), with craft-image-codecs' header probe last. Not turned by -/// an EXIF orientation. `None` when no header gives a size. +/// An image file's stored size in pixels, read from its header by craft-image-codecs without +/// decoding any pixels: PNG's IHDR, a JPEG's frame, a TIFF's first full-resolution page, a +/// still or animated WebP's canvas, BMP's header ([`craft_image_codecs::probe_allocation_size`], +/// with no size limit), and a GIF's logical screen and the other formats' headers +/// ([`craft_image_codecs::probe_size`]). Not turned by an EXIF orientation. `None` when no header +/// gives a size. pub fn header_size(bytes: &[u8]) -> Option<(u32, u32)> { - if bytes.len() >= 24 && bytes.starts_with(b"\x89PNG\r\n\x1a\n") && &bytes[12..16] == b"IHDR" { - let w = u32::from_be_bytes(bytes[16..20].try_into().ok()?); - let h = u32::from_be_bytes(bytes[20..24].try_into().ok()?); - return Some((w, h)); + if craft_image_codecs::detect(bytes) == Some(craft_image_codecs::Format::Gif) { + // A GIF frame may be larger than the logical screen the image crate gave as its size. + return craft_image_codecs::probe_size(bytes).ok(); } - if bytes.starts_with(&[0xFF, 0xD8]) - && let Some(j) = craft_image_codecs::jpeg_info(bytes) - { - return Some((j.width, j.height)); - } - image::ImageReader::new(Cursor::new(bytes)) - .with_guessed_format() - .ok() - .and_then(|r| r.into_dimensions().ok()) - .or_else(|| craft_image_codecs::probe_size(bytes).ok()) + craft_image_codecs::probe_allocation_size(bytes).ok().or_else(|| craft_image_codecs::probe_size(bytes).ok()) } impl ImageBlob { diff --git a/crates/metafile/src/dib.rs b/crates/metafile/src/dib.rs index a1f1df8cc3..b104304d7f 100644 --- a/crates/metafile/src/dib.rs +++ b/crates/metafile/src/dib.rs @@ -1,8 +1,6 @@ //! Device-independent bitmaps (DIBs), the pixels inside image records: written bottom-up as 24-bit //! (opaque, composited over white) or 32-bit premultiplied BGRA (AlphaBlend's source). -use std::io::Cursor; - use vectorcraft_doc::resample::{Resample, resized}; /// Most pixels along a side of an image the writers store (larger ones are scaled down). @@ -54,10 +52,9 @@ impl Rgba { /// As PNG bytes. pub fn to_png(&self) -> Result, String> { - let img = image::RgbaImage::from_raw(self.width, self.height, self.pixels.clone()).ok_or("the image's pixels don't match its size")?; - let mut png = vec![]; - img.write_to(&mut Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| e.to_string())?; - Ok(png) + let img = craft_image_codecs::Image::from_u8(self.width, self.height, craft_image_codecs::ChannelLayout::Rgba, self.pixels.clone()) + .map_err(|_| "the image's pixels don't match its size")?; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).map_err(|e| e.to_string()) } }