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.
This commit is contained in:
FredApps
2026-11-15 22:34:00 +01:00
parent 48a8f2a464
commit 827667a321
2 changed files with 13 additions and 24 deletions
+10 -18
View File
@@ -31,26 +31,18 @@ fn rgba_within(bytes: &[u8], max: u64) -> Option<image::RgbaImage> {
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 {
+3 -6
View File
@@ -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<Vec<u8>, 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())
}
}