diff --git a/crates/pdf/src/import/device.rs b/crates/pdf/src/import/device.rs index 3fcefd7b8e..45ca7f7081 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,39 @@ 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), + // Gray samples are stored as RGB: as a gray PNG, the image would become a + // grayscale image that a fill color colorizes. + (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..470432aa8f 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) } } @@ -323,10 +323,13 @@ pub struct TrimPages { pub bleed: [f64; 4], } -/// 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. +/// The shared reader's options: files of any size are accepted. Ordinary stream decoding 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, past the shared reader's defaults. 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..1cb23b2768 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 GiB and 2^30 pixels (31404 × 31403 pixels are 2.76 GiB in RGB and +/// 3.67 GiB in CMYK), otherwise 1 GiB and 2^28 pixels (an A0 sheet scanned at 300 ppi is 399 MiB +/// in RGB and 532 MiB 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; @@ -779,31 +791,42 @@ mod tests { /// A one-page PDF drawing one flat 8-bit DeviceRGB image of `w` × `h` pixels, Flate-compressed. fn one_image_pdf(w: usize, h: usize) -> Vec { + image_pdf(&format!("/Width {w} /Height {h} /ColorSpace /DeviceRGB"), &flat(&[200, 180, 150].repeat(w), h), None) + } + + /// `row` repeated `h` times, Flate-compressed as it is written. + fn flat(row: &[u8], h: usize) -> Vec { let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); - let row: Vec = [200, 180, 150].repeat(w); for _ in 0..h { - std::io::Write::write_all(&mut e, &row).unwrap(); + std::io::Write::write_all(&mut e, row).unwrap(); } - let image = e.finish().unwrap(); + e.finish().unwrap() + } + + /// A one-page PDF drawing one 8-bit image with the size and color space of `keys` and the + /// Flate-compressed samples `data`; with `smask`, a soft mask with those keys and data. + fn image_pdf(keys: &str, data: &[u8], smask: Option<(&str, &[u8])>) -> Vec { + let stream = |keys: &str, data: &[u8]| { + let dict = + format!("<< /Type /XObject /Subtype /Image {keys} /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", data.len()); + [dict.as_bytes(), data, b"\nendstream"].concat() + }; let content = b"q 595 0 0 842 0 0 cm /Im0 Do Q"; let mut out = b"%PDF-1.6\n".to_vec(); let mut offsets = vec![]; - let objects: Vec> = vec![ + let mut objects: Vec> = vec![ b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(), b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(), b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Contents 4 0 R /Resources << /XObject << /Im0 5 0 R >> >> >>".to_vec(), [format!("<< /Length {} >>\nstream\n", content.len()).as_bytes(), content, b"\nendstream"].concat(), - [ - format!( - "<< /Type /XObject /Subtype /Image /Width {w} /Height {h} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", - image.len() - ) - .as_bytes(), - &image, - b"\nendstream", - ] - .concat(), ]; + match smask { + Some((mask_keys, mask_data)) => { + objects.push(stream(&format!("{keys} /SMask 6 0 R"), data)); + objects.push(stream(mask_keys, mask_data)); + } + None => objects.push(stream(keys, data)), + } for (i, o) in objects.iter().enumerate() { offsets.push(out.len()); out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes()); @@ -828,6 +851,65 @@ 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)); + 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 declaring more is held to this build's limit, which a refusal of it names. + 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); + } + + /// The only image of `doc`, as stored: its width, height and channel layout. + fn stored_image(doc: &vectorcraft_doc::Document) -> (u32, u32, image::ExtendedColorType) { + use image::ImageDecoder as _; + let blobs: Vec<_> = doc.images.values().collect(); + let [blob] = blobs[..] else { panic!("one image expected, found {}", blobs.len()) }; + 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()) + } + + #[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(); + // Opaque: stored as RGB, without an alpha channel. + assert_eq!(stored_image(&doc), (side as u32, side as u32, image::ExtendedColorType::Rgb8)); + } + + #[test] + fn opaque_images_are_stored_without_an_alpha_channel_and_masked_ones_with_one() { + let rgb = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceRGB", &flat(&[200, 180, 150].repeat(4), 2), None); + assert_eq!(stored_image(&crate::import(&rgb).unwrap()), (4, 2, image::ExtendedColorType::Rgb8)); + let gray = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceGray", &flat(&[120; 4], 2), None); + assert_eq!(stored_image(&crate::import(&gray).unwrap()), (4, 2, image::ExtendedColorType::Rgb8)); + let mask = ("/Width 4 /Height 2 /ColorSpace /DeviceGray", &flat(&[0, 85, 170, 255], 2)[..]); + let masked = image_pdf("/Width 4 /Height 2 /ColorSpace /DeviceRGB", &flat(&[200, 180, 150].repeat(4), 2), Some(mask)); + assert_eq!(stored_image(&crate::import(&masked).unwrap()), (4, 2, image::ExtendedColorType::Rgba8)); + } + + #[test] + fn an_image_of_more_pixels_than_the_limit_is_left_out_with_a_warning() { + let (_, pixels) = image_limits(LARGE_IMAGES); + // One more row than a square of the limit's pixels; its data isn't looked at. + let side = 1u64 << (pixels.ilog2() / 2); + let pdf = image_pdf(&format!("/Width {side} /Height {} /ColorSpace /DeviceGray", side + 1), &flat(&[0; 16], 1), None); + let report = crate::import_with_report(&pdf, &crate::ImportOptions::default()).unwrap(); + assert!(report.document.images.is_empty()); + assert!(report.warnings.iter().any(|w| w.contains("an image could not be decoded and was skipped")), "{:?}", report.warnings); + } + #[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 40fe97f27b..a2d6776e0a 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -2025,6 +2025,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur A `.ai` saved without PDF compatibility (its PDF part only a placeholder page) is placed and pasted from its editing data, as it opens: File › Place places the art of the layers that show on the first artboard (`file.place`'s `page` places another one), and Paste of it as PDF data (`clipboard.importPdf`) takes every object. `document.open` with `pages` naming every page of such a file opens it the same way (`crates/engine/src/cmd/fileio/tests_aiimport.rs`). Explicitly requested editing data keeps the full single artboard when its PDF page is capped at 14,400 pt (#6865; `crates/eps/src/import/tests_native.rs`). Scores and estimates unchanged. A `.ai`, or a PDF saved with Illustrator's editing data, whose PDF part can't be read (a stream the import refuses) opens from its editing data, with the PDF part's error as its first warning; File › Place places its first artboard the same way, and Paste of it as PDF data with `editingData: true` takes every object (part of #4366; `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 GiB with up to 2^30 pixels in a 64-bit desktop build (31404 × 31403 RGB pixels are 2.76 GiB), and up to 1 GiB 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 left out with a warning (#4366). An opaque image is kept as RGB, without an alpha channel (`crates/pdf/src/streams.rs`). - EPS and DXF in and out, EMF/WMF in and out; PNG (with PNG-8), JPEG, WebP, GIF, TIFF, BMP, Targa and layered PSD export; grayscale TIFFs with an alpha channel or other extra channels place, relink and open, the alpha as the image's transparency (#1466 in part; CCITT Group 3 and planar files with extra channels are not read); PSD and PSB files open and place, linked or embedded, as their merged image in every colour mode and depth (#918). EPS import runs a PostScript Level 3 interpreter written from the PostScript Language Reference: resource categories and `resourceforall`, local and global VM (`currentglobal`/`setglobal`/`gcheck`), `clipsave`/`cliprestore`, subarrays and substrings that share storage, the standard dictionaries in `systemdict`, `dictstack`, `internaldict`, executable filters and strings, `load` by any key, `//name` immediate names, `token`, `execform`, user paths, `cshow`, `rootfont`, `cachestatus`, colour rendering and halftone queries, file `status` and data read with `flushfile`, and `pdfmark` marks dropped (Discord feedback, #466, #469, #474). Masked images (`ImageType 3` in its three interleavings) and reusable streams are read, and an image's several data sources are read in turn, as the reference defines (procedures that read one file get their own rows: such images came in banded, #498). `restore` puts dictionaries back as they were at the `save`, so programs that count their saves in a dictionary read to their end (part of #505). Tiling patterns come in as pattern swatches, uncoloured ones too; Type 3 fonts' glyphs as their outlines, spaced by `ashow`/`widthshow`/`xshow`/`kshow`; mesh and function-based shadings (types 1, 4–7) as gradient meshes; sampled functions (type 0) and Flate/LZW predictors are read. It is tested with programs written from the specification in the ways common generators use these features; Inkscape/cairo EPS files open as vectors. When a file's PostScript still can't be read, it opens as its TIFF preview, its Windows metafile preview (as vectors) or its EPSI preview, and the warning (in the status bar, `document.open`/`app.open`/MCP `open_file` replies and `vectorcraft-cli info`) names the error, the operator that raised it and the procedures it ran in. Legacy PostScript `.ai` files (Illustrator 3–8, written with the prolog that defines their operators) keep their groups and nested groups (`u` … `U`), between and inside clipping groups; EPS and EMF/WMF import nest clips (and groups) at most 128 deep, so a hostile file can't exhaust the stack. DXF TEXT justified Fit or Aligned spans its two points (stretched, or scaled as a whole). PDF export writes type as real, searchable text in embedded subset fonts by default, as Illustrator's PDF presets do (Convert Text to Outlines is an option; printer's marks stay shapes), so text-heavy PDFs stay small (#733). DXF export writes each fill as its outlines followed by its hatch, so laser and cutter software, which skips hatches, gets filled art and type too (#546). - JPEG XL (`.jxl`) images open and place, linked or embedded, as their first frame, decoded with jxl-oxide and stored as PNG: 16-bit and floating-point samples become 8 bits, alpha is kept, and an image past the pixel allowance is refused from its header (part of #2306; `crates/engine/src/cmd/fileio/tests_jxlread.rs`). - The Open and Place dialogs list PostScript `.ps` files and `.epsf` files. A `.ps` file opens and places through the EPS reader (its first page), also when its header is only `%!`, as the `ps` format, which exports don't write (Print to PostScript does); an `.epsf` file opens and places as EPS. Copied in a file manager, these files paste as EPS, and `clipboard.importEps` also reads PostScript whose header is only `%!` (`crates/engine/src/cmd/fileio/tests_epsimport.rs`).