From 977024a4e91ddb3d1066b40f2e86b16ebbfca3d4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 20:01:26 -0800 Subject: [PATCH] PDF import: 64-bit desktop builds open images up to 4 GB and 2^30 pixels A PDF, or a `.ai` read through its PDF part, was refused when one image would decode to more than 1 GB, and the shared reader skipped images of more than 2^28 pixels. On a 64-bit desktop build both limits are now 4 GB and 2^30 pixels (`image_limits`), which `MAX_IMAGE_DECODED` and `open_options()` share; 32-bit and web builds keep 1 GB and 2^28 pixels. An image past the limit is refused with the same message. An opaque image is encoded as an RGB PNG from its decoded samples instead of building an alpha channel and an RGBA copy first. Docs: docs/target-app-parity.md. Part of #4366. --- crates/pdf/src/import/device.rs | 30 ++++++++++++------- crates/pdf/src/import/state.rs | 5 ++++ crates/pdf/src/pages.rs | 9 ++++-- crates/pdf/src/streams.rs | 53 +++++++++++++++++++++++++++++++-- docs/target-app-parity.md | 1 + 5 files changed, 83 insertions(+), 15 deletions(-) diff --git a/crates/pdf/src/import/device.rs b/crates/pdf/src/import/device.rs index 3fcefd7b8e..2a2df28e37 100644 --- a/crates/pdf/src/import/device.rs +++ b/crates/pdf/src/import/device.rs @@ -1,6 +1,6 @@ use super::entry::{INVISIBLE_TEXT_NOTE, MAX_NESTED, OUTLINED_TEXT_NOTE, UNSORTED, WHITE_KEY}; use super::state::{ - Builder, Decoded, Frame, FrameKind, GlyphRun, blend, fill_rule, gray_png, line_width, mean_scale, resize_alpha, rgba_png, same_outline, + Builder, Decoded, Frame, FrameKind, GlyphRun, blend, fill_rule, gray_png, line_width, mean_scale, resize_alpha, rgb_png, rgba_png, same_outline, stroke_layer, stroke_outline, }; use super::{ @@ -304,27 +304,37 @@ impl<'a> Device<'a> for Builder<'_> { self.add_image(key, || None, xf); return; } - let mut decoded: Option = None; + // RGB samples, or RGBA when the image has an alpha channel or is drawn white. + let mut decoded: Option<(Decoded, bool)> = None; r.with_rgba( |img, alpha| { let (w, h, sf) = (img.width(), img.height(), img.scale_factors()); - let a = alpha.map(|a| resize_alpha(&a, w, h)).unwrap_or_else(|| vec![255; (w * h) as usize]); - let rgba: Vec = match img { - _ if white => a.into_iter().flat_map(|a| [255, 255, 255, a]).collect(), - ImageData::Rgb(d) => d.data.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], a]).collect(), - ImageData::Luma(d) => d.data.iter().zip(a).flat_map(|(g, a)| [*g, *g, *g, a]).collect(), + let px = match (img, alpha) { + // An opaque image's RGB samples are encoded as they are, with no copy. + (ImageData::Rgb(d), None) if !white => (d.data, false), + (ImageData::Luma(d), None) if !white => (d.data.iter().flat_map(|g| [*g; 3]).collect(), false), + (img, alpha) => { + let a = alpha.map(|a| resize_alpha(&a, w, h)).unwrap_or_else(|| vec![255; (w * h) as usize]); + let rgba: Vec = match img { + _ if white => a.into_iter().flat_map(|a| [255, 255, 255, a]).collect(), + ImageData::Rgb(d) => d.data.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], a]).collect(), + ImageData::Luma(d) => d.data.iter().zip(a).flat_map(|(g, a)| [*g, *g, *g, a]).collect(), + }; + (rgba, true) + } }; - decoded = Some((rgba, w, h, sf)); + decoded = Some(((px.0, w, h, sf), px.1)); }, None, ); - let Some((rgba, w, h, sf)) = decoded else { + let Some(((px, w, h, sf), alpha)) = decoded else { self.warn("an image could not be decoded and was skipped"); return; }; self.raster_scales.insert(key, sf); let xf = transform * Affine::scale_non_uniform(sf.0 as f64, sf.1 as f64); - self.add_image(key, || rgba_png(rgba, w, h).map(|png| (ImageBlob::new("image/png", png), w, h)), xf); + let png = move || if alpha { rgba_png(px, w, h) } else { rgb_png(px, w, h) }; + self.add_image(key, || png().map(|png| (ImageBlob::new("image/png", png), w, h)), xf); } Image::Stencil(s) => { let white = self.white_images; diff --git a/crates/pdf/src/import/state.rs b/crates/pdf/src/import/state.rs index 1eeb8a5539..ba0c23a808 100644 --- a/crates/pdf/src/import/state.rs +++ b/crates/pdf/src/import/state.rs @@ -340,6 +340,11 @@ pub(super) fn rgba_png(rgba: Vec, w: u32, h: u32) -> Option> { craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok() } +pub(super) fn rgb_png(rgb: Vec, w: u32, h: u32) -> Option> { + let img = craft_image_codecs::Image::from_raw(w, h, craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8, rgb).ok()?; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok() +} + pub(super) fn gray_png(gray: Vec, w: u32, h: u32) -> Option> { let img = craft_image_codecs::Image::from_raw(w, h, craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8, gray).ok()?; craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).ok() diff --git a/crates/pdf/src/pages.rs b/crates/pdf/src/pages.rs index 64e6217148..12a7405d6f 100644 --- a/crates/pdf/src/pages.rs +++ b/crates/pdf/src/pages.rs @@ -215,7 +215,7 @@ fn stream_kind(pdf: &Pdf, id: ObjectIdentifier, dict: &Dict<'_>) -> String { } /// `bytes` for a message, in whole megabytes, or gigabytes when it is a whole number of them. -fn decoded_size(bytes: usize) -> String { +pub(crate) fn decoded_size(bytes: usize) -> String { const GB: usize = 1 << 30; if bytes >= GB && bytes.is_multiple_of(GB) { format!("{} GB", bytes / GB) } else { format!("{} MB", bytes >> 20) } } @@ -324,9 +324,12 @@ pub struct TrimPages { } /// Preserve the application's existing accepted file sizes. Ordinary stream decoding still -/// uses the shared reader's immutable per-document limits, and its input copy is fallible. +/// uses the shared reader's immutable per-document limits, and its input copy is fallible. One +/// image may decode to the bytes and pixels of [`crate::streams::image_limits`] for this build. pub(crate) fn open_options() -> craft_pdf::OpenOptions { - craft_pdf::OpenOptions { max_input_bytes: usize::MAX, ..Default::default() } + let (bytes, pixels) = crate::streams::image_limits(crate::streams::LARGE_IMAGES); + let decode_limits = craft_pdf::syntax::DecodeLimits::default().with_max_decoded_image(bytes).with_max_image_pixels(pixels); + craft_pdf::OpenOptions { max_input_bytes: usize::MAX, decode_limits } } pub(crate) fn open_with_options(bytes: &[u8], password: Option<&str>, options: &craft_pdf::OpenOptions) -> Result { diff --git a/crates/pdf/src/streams.rs b/crates/pdf/src/streams.rs index 3f99b8b82a..0b20eb7866 100644 --- a/crates/pdf/src/streams.rs +++ b/crates/pdf/src/streams.rs @@ -15,9 +15,21 @@ use craft_pdf::syntax::object::{Array, Dict, FromBytes, MaybeRef, Name, Object, /// million, 4 bytes each). pub(crate) const MAX_DECODED: usize = 256 << 20; +/// Whether this build reads the larger images of [`image_limits`]: a 64-bit build that isn't for +/// the web. +pub(crate) const LARGE_IMAGES: bool = cfg!(all(target_pointer_width = "64", not(target_family = "wasm"))); + +/// The most bytes one image XObject may decode to and the most pixels it may have, for a build +/// that reads `large` images: 4 GB and 2^30 pixels (31404 × 31403 pixels are 2.96 GB in RGB and +/// 3.94 GB in CMYK), otherwise 1 GB and 2^28 pixels (an A0 sheet scanned at 300 ppi is 418 MB in +/// RGB and 557 MB in CMYK). The shared reader takes the same two figures ([`crate::pages::open_options`]). +pub(crate) const fn image_limits(large: bool) -> (u64, u64) { + if large { (1 << 32, 1 << 30) } else { (1 << 30, 1 << 28) } +} + /// The most an image XObject may decode to when its own size asks for more than -/// [`MAX_DECODED`]: 1 GB (an A0 sheet scanned at 300 ppi is 418 MB in RGB, 557 MB in CMYK). -const MAX_IMAGE_DECODED: usize = 1 << 30; +/// [`MAX_DECODED`]: the bytes of [`image_limits`] for this build. +const MAX_IMAGE_DECODED: usize = image_limits(LARGE_IMAGES).0 as usize; /// The longest row a predictor may have, in bytes. const MAX_ROW: usize = 1 << 24; @@ -828,6 +840,43 @@ mod tests { assert_eq!(info.map(|i| i.pages.len()).unwrap(), 1); } + #[test] + fn a_64_bit_desktop_build_reads_larger_images_and_others_keep_their_limits() { + assert_eq!(image_limits(false), (1 << 30, 1 << 28)); + assert_eq!(image_limits(true), (1 << 32, 1 << 30)); + assert_eq!(LARGE_IMAGES, cfg!(all(target_pointer_width = "64", not(target_family = "wasm")))); + let (bytes, pixels) = image_limits(LARGE_IMAGES); + assert_eq!(MAX_IMAGE_DECODED as u64, bytes); + let limits = crate::pages::open_options().decode_limits; + assert_eq!((limits.max_decoded_image(), limits.max_image_pixels()), (bytes, pixels)); + // An image past the limit is refused with the same message, naming this build's limit. + let d = dict("<< /Subtype /Image /Width 70000 /Height 70000 /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode >>"); + assert_eq!(budget(&d), MAX_IMAGE_DECODED); + let most = if LARGE_IMAGES { "4 GB" } else { "1 GB" }; + assert_eq!(crate::pages::decoded_size(MAX_IMAGE_DECODED), most); + } + + #[test] + fn an_image_past_1_gb_and_2_to_the_28_pixels_opens_on_a_64_bit_desktop_build() { + if !LARGE_IMAGES { + return; + } + // 18919 × 18919 RGB pixels: 358 million pixels, 1.07 GB decoded (#4366). + let side = 18919; + let doc = crate::import(&one_image_pdf(side, side)).unwrap(); + let blobs: Vec<_> = doc.images.values().collect(); + let [blob] = blobs[..] else { panic!("one image expected, found {}", blobs.len()) }; + // Opaque: stored as RGB, without an alpha channel. + let (w, h, layout) = { + use image::ImageDecoder as _; + let decoder = image::ImageReader::new(std::io::Cursor::new(blob.bytes.as_slice())).with_guessed_format().unwrap().into_decoder().unwrap(); + let (w, h) = decoder.dimensions(); + (w, h, decoder.original_color_type()) + }; + assert_eq!((w, h), (side as u32, side as u32)); + assert_eq!(layout, image::ExtendedColorType::Rgb8); + } + #[test] fn object_and_cross_reference_streams_are_looked_at_before_the_file_is_read() { let stream = |dict: &str, data: &[u8]| { diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index a4992647b2..e683c240f4 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1962,6 +1962,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur When the editing data of a `.ai` or EPS is for a page of another size than the file's, the warning that its layers weren't read gives both sizes in points, and the scale of the editing data's page to the file's page (1:10 for a tenth of its size) when their sides are in proportion (#2034; `crates/eps/src/import/tests_native.rs`). A `.ai` or EPS opened from its editing data keeps its layers when it embeds an image of more than 2^25 pixels (part of #1479). The image comes in at full size up to a target size (2^26 to 2^28 pixels on the desktop, by installed memory; 2^27 on the web), a 1-bit gray image as a 1-bit PNG. A larger image is averaged down by the smallest whole step that fits, a 1-bit one into shades of gray, with a note giving both sizes. An image of more than 2^31 pixels or 512 MiB of samples is an object the reader can't read, so its box comes in as the page's art; without page art, it is left out with a note (`crates/eps/src/import/tests_native.rs`). A `.ai`, or a PDF saved with Illustrator's editing data, whose editing data can't be decompressed opens from its PDF part with a warning that its layers weren't read and why, and an export or Save As to its own path asks first (`acknowledgeLoss`), as for a file whose layers the reader can't read (`crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). + A PDF, or a `.ai` read through its PDF part, opens an image that decodes to up to 4 GB with up to 2^30 pixels in a 64-bit desktop build (31404 × 31403 RGB pixels are 2.96 GB), and up to 1 GB with 2^28 pixels in 32-bit and web builds. A file with an image that would decode to more is refused with the size it would decode past, and an image with more pixels is not drawn (#4366). An opaque image is kept as RGB, without an alpha channel. Measured on Linux x86_64: a synthetic 31404 × 31403 RGB image opened in 15 s, peaking at 5.9 GiB (`crates/pdf/src/streams.rs`). A `.ai` or EPS opened from its editing data with one object on a shown layer that the reader can't read (an unknown operator inside a group, a pattern fill whose pattern it doesn't have, a symbol instance, a placed file without a usable preview) keeps its layers and groups: in that object's place, its layer gets a group of the page's objects inside the object's box, named after it, with a warning that lists what came in as the page's art and an export or Save As to its own path asking first (`acknowledgeLoss`). The file still opens from its page or PDF part when no box can be found, when live art or another unread object shares the page art inside it, when the object spans two artboards or lies on an artboard that has no page, when no page art lies inside its box, when that page art is inside a page group with a clip, mask, transparency or appearance, when the object holds a frame of threaded type, when the page art uses a pattern, image or swatch that the page lacks or that differs from the editing data's, or when the page art doesn't carry the object's transparency (#4806; `crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). A `.ai` or EPS opened from its editing data with one object on a shown layer that the reader can't read (an unknown operator inside a group, a pattern fill whose pattern it doesn't have, a symbol instance, a placed file without a usable preview) keeps its layers and groups: in that object's place, its layer gets a group of the page's objects inside the object's box, named after it, with a warning that lists what came in as the page's art and an export or Save As to its own path asking first (`acknowledgeLoss`). The file still opens from its page or PDF part when no box can be found, when live art or another unread object shares the page art inside it, when the object spans two artboards, or when the page art doesn't carry the object's transparency (#4806; `crates/eps/src/import/tests_native.rs`, `crates/engine/src/cmd/fileio/tests_aiimport.rs`). In a `.ai` opened from its editing data, an envelope made with a warp, a mesh or a top object opens as a live envelope of its contents, with its warp style, bend and distortions, its mesh points and handles or its top object's outline, its Envelope Options, its Fidelity and whether its contents are being edited. When Distort Appearance is on and the size the file saves for the contents matches their bounds with strokes, the contents map from that stroked box, until an edit in Edit Contents changes how far the strokes reach; VectorCraft's new envelopes map from the geometric box, as do contents with no width or no height, such as a single straight line. An envelope that can't be read, or that VectorCraft draws differently from the file's drawn art, keeps that drawn art, with a note. An envelope of type opens live, its type laid out from the text document as its contents, where VectorCraft's own envelope of that type draws as the drawn art the file saves (compared as for the other envelopes, seen on white and on black, so white type counts, and only where the layers with it are compared with the page and draw as it, the envelope weighed as the art it draws; where they don't, even with the drawn art of every live envelope put back, and are used all the same, trusted, with the objects that differ coming in as the page's art, or opened alone, it stays live where, in the pixels it and its drawn art ink (drawn together without the rest of the layers), the layers with it differ from the page in no more than its own check allows and the envelopes that stay differ there in no more than half of what the page check allows, and nothing above it clips, masks or moves it); elsewhere, and where the type's font isn't available, a text or visibility variable binds the type, or the envelope is in a symbol's or a pattern's art, it keeps its drawn art, with its type hidden and a note that says the type isn't editable (#3179; `crates/eps/src/import/ai/tests.rs`, `crates/eps/src/import/tests_native.rs`). A warp of type whose own warp, VectorCraft's, doesn't draw as its drawn art is tried again as a mesh envelope on the grid the file saves for it: its type stays editable and keeps the file's shape, but the warp's settings (style, bend) are lost, as the document has no place for them on a mesh. Type with strokes in an envelope with Distort Appearance on is also tried mapped from its glyphs grown by as far as its strokes paint along a side (half the weight of a centred stroke), without what miter joins add at corners, where the size the file saves for it doesn't already give that box; after those tries (and before a grid moved onto its drawn art), it is also tried mapped from the box of what it paints, where that is another box, as users' files map some such type: its glyph outlines with their fills and strokes baked as the envelope bakes them, so geometry effects on them count (a stroke a Transform effect moves paints where it is moved to), each measured by its visual bounds (exact for miter joins, a tip counting where the miter limit keeps it, and for round joins; an upper bound for bevel joins, miters the limit cuts, width profiles and square-capped dashes), and a contour of zero length, which paints nothing, left out; effects on the type object itself are left out, as VectorCraft applies them after the envelope bends the type. The drawn-art check decides which one opens. An envelope's grid reads its way round from all its rows and columns, so a mesh bent far round reads the way it runs. While an envelope's contents are edited, its mesh is kept in its dictionary, and in some files that mesh lies away from the drawn art by a move, the contents with the drawn art (as if the mesh hadn't followed the envelope when it moved): a mesh or top object saved in Edit Contents whose mesh draws the contents as large as the drawn art but elsewhere is tried again, after every other way, with its mesh moved onto that art, and opens there where it then draws as it. The drawn art of these files strokes type in an envelope along its bent outlines at the stroke's own weight, Distort Appearance on or off, and VectorCraft draws it so (#6644): an envelope of stroked type whose grid squeezes or stretches it opens live where it draws as that art.