mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 20:19:40 +00:00
fix(codecs): read image sizes from headers for PNG, JPEG and TIFF again
craft_image_codecs::probe_size only reads WebP, EXR and the GIF/BMP/TGA family; for PNG, JPEG and TIFF it returns Unsupported. The cutover used it for header sizes, so DXF export left every PNG out (cad tests), plugin objects lost PNG pixels, and EPS image-mask and SVG import fell back to decoding whole images to learn their size. vectorcraft_doc::header_size reads PNG's IHDR and a JPEG's frame through craft-image-codecs and other formats through the image crate's header readers, as main does, without decoding pixels. The SVG filter's JPEG size comes from jpeg_info instead of a full decode.
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -31,6 +31,28 @@ 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.
|
||||
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<u8>) -> Self {
|
||||
|
||||
@@ -633,11 +633,7 @@ fn raster_image_sized(data: Arc<Vec<u8>>) -> 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,
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user