diff --git a/crates/cad/src/scene.rs b/crates/cad/src/scene.rs index 27a8cc2cce..660e8275d7 100644 --- a/crates/cad/src/scene.rs +++ b/crates/cad/src/scene.rs @@ -1405,7 +1405,7 @@ impl<'a> Scene<'a> { use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image as CodecImage, SampleType}; let blob = self.doc.image_blob(im).filter(|b| !b.bytes.is_empty()).ok_or_else(|| "an image has no pixels and was left out".to_string())?; // The pixels it converts to (RGBA, 4 bytes each) are read from its header before decoding. - let (w, h) = craft_image_codecs::probe_size(&blob.bytes).map_err(|_| UNDECODED.to_string())?; + let (w, h) = vectorcraft_doc::header_size(&blob.bytes).ok_or_else(|| UNDECODED.to_string())?; if u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS { return Err("an image is too large for DXF and was left out".to_string()); } diff --git a/crates/doc/Cargo.toml b/crates/doc/Cargo.toml index 3a5626054b..e1ca2ba838 100644 --- a/crates/doc/Cargo.toml +++ b/crates/doc/Cargo.toml @@ -17,6 +17,8 @@ vectorcraft-geom = { workspace = true } vectorcraft-color = { workspace = true } kurbo = { workspace = true } image = { workspace = true } +# Image headers (PNG and JPEG sizes, `pixels::header_size`). +craft-image-codecs = { workspace = true } tiff = { workspace = true } # 1-bit TIFFs compressed with CCITT Group 3 (Compression 2 and 3). hayro-ccitt = { workspace = true } diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 7b7ef4fee7..410a45c47d 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -52,7 +52,7 @@ pub mod overprint; pub mod pattern; pub mod perspective; mod pixels; -pub use pixels::{GraySource, MAX_RASTER_ALLOC, PixelPick}; +pub use pixels::{GraySource, MAX_RASTER_ALLOC, PixelPick, header_size}; pub mod placed_document; pub mod plugin_data; pub mod ppi; diff --git a/crates/doc/src/pixels.rs b/crates/doc/src/pixels.rs index c451fea7be..b4659f8957 100644 --- a/crates/doc/src/pixels.rs +++ b/crates/doc/src/pixels.rs @@ -31,6 +31,28 @@ 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. +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 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()) +} + impl ImageBlob { /// A PNG blob of `png` bytes. pub fn png(png: Vec) -> Self { diff --git a/crates/effects/src/svg_filter.rs b/crates/effects/src/svg_filter.rs index 47a7d081ca..6cb0c45e65 100644 --- a/crates/effects/src/svg_filter.rs +++ b/crates/effects/src/svg_filter.rs @@ -633,11 +633,7 @@ fn raster_image_sized(data: Arc>) -> Option<(usvg::ImageKind, u64)> { } (u32::from_be_bytes(data[16..20].try_into().ok()?), u32::from_be_bytes(data[20..24].try_into().ok()?)) } - craft_image_codecs::Format::Jpeg => { - let mut opts = craft_image_codecs::DecodeOptions::default(); - opts.limits.max_pixels = MAX_IMAGE_PIXELS; - craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, &data, &opts).ok()?.dimensions() - } + craft_image_codecs::Format::Jpeg => craft_image_codecs::jpeg_info(&data).map(|j| (j.width, j.height))?, craft_image_codecs::Format::Gif | craft_image_codecs::Format::WebP => craft_image_codecs::probe_size(&data).ok()?, _ => return None, }; diff --git a/crates/eps/src/legacy_ai.rs b/crates/eps/src/legacy_ai.rs index 6fd7a6bfd3..d304ed27df 100644 --- a/crates/eps/src/legacy_ai.rs +++ b/crates/eps/src/legacy_ai.rs @@ -177,12 +177,8 @@ pub struct LegacyAiOutput { pub fn writes_image_mask(doc: &Document, n: &Node) -> bool { let NodeKind::Image(im) = &n.kind else { return false }; // The size from the file's header, before any pixels are decoded. - let fits = |bytes: &[u8]| { - craft_image_codecs::probe_size(bytes) - .ok() - .or_else(|| craft_image_codecs::decode(bytes).ok().map(|i| i.dimensions())) - .is_some_and(|(w, h)| w > 0 && h > 0 && u64::from(w) * u64::from(h) <= MAX_IMAGE_PIXELS) - }; + let fits = + |bytes: &[u8]| vectorcraft_doc::header_size(bytes).is_some_and(|(w, h)| w > 0 && h > 0 && u64::from(w) * u64::from(h) <= MAX_IMAGE_PIXELS); matches!(n.extras().colorize.as_deref(), Some(Paint::Solid { .. })) && !n.extras().mask.as_ref().is_some_and(|m| !m.disabled) && doc diff --git a/crates/plugins/src/objects.rs b/crates/plugins/src/objects.rs index fa14ed1bee..08689b7235 100644 --- a/crates/plugins/src/objects.rs +++ b/crates/plugins/src/objects.rs @@ -459,7 +459,7 @@ fn color_mode(b: &ImageBlob) -> &'static str { if b.gray_source().is_some() { return "gray"; } - let readable = craft_image_codecs::probe_size(&b.bytes).is_ok() || b.header_size().is_some(); + let readable = vectorcraft_doc::header_size(&b.bytes).is_some() || b.header_size().is_some(); if readable { "rgb" } else { "unknown" } } @@ -480,7 +480,7 @@ fn rgba_pixels(b: &ImageBlob, w: u32, h: u32, max: usize, charged: &mut usize) - return None; } let cmyk = b.is_cmyk(); - let size = if cmyk { b.header_size()? } else { craft_image_codecs::probe_size(&b.bytes).ok().or_else(|| b.header_size())? }; + let size = if cmyk { b.header_size()? } else { vectorcraft_doc::header_size(&b.bytes).or_else(|| b.header_size())? }; if size != (w, h) { return None; } diff --git a/crates/svg/src/import/importer.rs b/crates/svg/src/import/importer.rs index 683c5dd867..7f0220aaf2 100644 --- a/crates/svg/src/import/importer.rs +++ b/crates/svg/src/import/importer.rs @@ -767,10 +767,7 @@ impl Importer { usvg::ImageKind::SVG(tree) => return self.svg_image(tree, acc), }; let size = i.size(); - let (pw, ph) = craft_image_codecs::probe_size(bytes) - .ok() - .or_else(|| craft_image_codecs::decode(bytes).ok().map(|i| i.dimensions())) - .unwrap_or((size.width().round().max(1.0) as u32, size.height().round().max(1.0) as u32)); + let (pw, ph) = vectorcraft_doc::header_size(bytes).unwrap_or((size.width().round().max(1.0) as u32, size.height().round().max(1.0) as u32)); // A turned CMYK image is stored as the CMYK TIFF of its inks, the size of the PNG usvg got. let cmyk = self.files.stored(i.kind()); let (bytes, mime) = match &cmyk {