From 3fa02146d092934b8e67bccc837d615ff15ac542 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 1 Dec 2026 20:46:08 +0100 Subject: [PATCH] Bound forced PDF raster resampling for extreme dimensions --- crates/pdf/src/images.rs | 18 ++++++++++++------ docs/mcp.md | 7 +++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/pdf/src/images.rs b/crates/pdf/src/images.rs index 72d748a595..911e69bd37 100644 --- a/crates/pdf/src/images.rs +++ b/crates/pdf/src/images.rs @@ -192,6 +192,10 @@ fn lossless( /// out. Open and Place hold larger images than this ([`vectorcraft_doc::MAX_RASTER_ALLOC`]). pub(crate) const MAX_EMBED_PIXELS: u64 = 1 << 27; +/// Keep forced resampling within JPEG's dimension range. This also keeps an extreme aspect +/// ratio from making the resampler allocate hundreds of millions of column accumulators. +const MAX_SCALED_EDGE: u32 = u16::MAX as u32; + /// [`MAX_EMBED_PIXELS`], lowered by tests that need an image past it. #[cfg(not(test))] pub(crate) fn max_embed_pixels() -> u64 { @@ -215,14 +219,14 @@ pub(crate) fn oversize(bytes: &[u8]) -> Option<(u32, u32)> { (u64::from(w) * u64::from(h) > max_embed_pixels()).then_some((w, h)) } -/// The largest size of at most `max` pixels with the proportions of `w` × `h`, at least a pixel a -/// side: `w` × `h` itself when it has no more. +/// The largest size within the pixel and edge limits with the proportions of `w` × `h`, at least +/// a pixel a side. Images already under the pixel limit keep their dimensions. fn fit_size((w, h): (u32, u32), max: u64) -> (u32, u32) { let pixels = u64::from(w) * u64::from(h); if pixels <= max { return (w, h); } - let k = (max as f64 / pixels as f64).sqrt(); + let k = (max as f64 / pixels as f64).sqrt().min(f64::from(MAX_SCALED_EDGE) / f64::from(w.max(h))); let side = |n: u32| ((f64::from(n) * k).floor() as u32).clamp(1, n); let (mut nw, mut nh) = (side(w), side(h)); // A side held at a pixel leaves the other too long: cut to what fits. @@ -670,9 +674,9 @@ pub(crate) fn recode( // Pixels that can't be decoded are kept as they are, with a warning when they were to be // converted. let keep = if source.is_some() { Recoded { warning: Some(UNDECODED), ..keep } } else { keep }; - // What the header declares is decoded within the image allowance, not whatever it asks for. - // Grey pixels aren't converted. - if decoder.set_limits(limits()).is_err() { + // The oversized path applies Open and Place's limits in decoded_oversize. The other + // path uses this decoder's limits. Grey pixels aren't converted. + if !oversize && decoder.set_limits(limits()).is_err() { return Recoded { unconverted: convert.is_some() && !grey, ..keep }; } let (mut kind, pixels) = if grey { @@ -1001,6 +1005,8 @@ mod tests { assert_eq!(fit_size((30_000, 7), 1000), (1000, 1)); assert_eq!(fit_size((7, 30_000), 1000), (1, 1000)); assert_eq!(fit_size((600, 600), 360_000), (600, 600)); + assert_eq!(fit_size((MAX_EMBED_PIXELS as u32 + 1, 1), MAX_EMBED_PIXELS), (MAX_SCALED_EDGE, 1)); + assert_eq!(fit_size((1, MAX_EMBED_PIXELS as u32 + 1), MAX_EMBED_PIXELS), (1, MAX_SCALED_EDGE)); } /// The inks of a CMYK image with alpha, resampled, keep the amounts of the opaque pixels: the diff --git a/docs/mcp.md b/docs/mcp.md index 4d49bc923c..0d756c4c2c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -1660,10 +1660,9 @@ Images follow the `compression` settings of their kind (`color`, `gray`, or `mon `abovePpi` as placed they are resampled (`downsample`: `average`, `subsample` or `bicubic`) to `ppi`, and compressed with `zip`, `jpeg` (at `quality`; images with transparency stay lossless) or `auto` (JPEGs stay JPEG, the others lossless). `none`, `jpeg2000`, CCITT and `runLength` are written as ZIP, with a warning when an image needs them. -An image of more than 2^27 pixels (134 million) is not written as it is: it is scaled down to the most pixels that -fit, in its proportions and each pixel the mean of those it covers, before those settings apply, and a warning gives -both sizes. One that can't be scaled down (its pixels are beyond what Open and Place decode, or it is a CMYK image) is -left out, and the warning gives its size. +PDF export scales an ordinary image over 2^27 pixels (134 million) proportionally to fit that limit and a maximum +edge of 65,535 pixels, before applying compression settings. It averages the pixels and reports both dimensions. +An oversized image beyond the decoder's allowance or in CMYK may be left out, with a warning that gives its size. The pieces of an image bent by a Warp effect are resampled once, for the widest and the highest size any piece places the image at, and all paint that one image. A 16-bit PNG that is not resampled, recompressed or converted keeps its 16 bits a sample in PDF 1.5 and later files,