PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M11: an indexed image's palette is read once it matters, within 4 KiB; a colour key keeps its image
Grzegorz Martens6ad95f0aa481
diff --git a/crates/optimize/README.md b/crates/optimize/README.mdindex 184053c79..994ccbd9d 100644--- a/crates/optimize/README.md+++ b/crates/optimize/README.md@@ -63,10 +63,11 @@ let report = optimize_with_progress(&mut doc, &settings, &mut |stage| keep_goingmask (a 0, or a 1 with an inverting decode array), and the darker of an indexed image's twopalette entries. Only images whose dark sample is known so are averaged: `DeviceGray` (grey andRGB packed to one bit among them), image masks, and indexed images over `DeviceGray` or- `DeviceRGB`, whose palette is read (Rec. 601 luma for RGB), with no decode array or one of two- different numbers, and not drawn or listed where `/DefaultGray` or `/DefaultRGB` is or may be- in force (an indexed image referred to from beyond the page and form resources read, as an- annotation's appearance, may be). Not a separation or a DeviceN, whose tint transform isn't read, calibrated or ICC+ `DeviceRGB`, whose palette is read (Rec. 601 luma for RGB; a table of at most 4 KiB), with no+ decode array or one of two different numbers, and not drawn or listed where `/DefaultGray` or+ `/DefaultRGB` is or may be in force (an indexed image referred to from beyond the page and form+ resources read, as an annotation's appearance, may be). Not one with a colour key, which can+ hide the dark sample a tie would go to. Not a separation or a DeviceN, whose tint transform isn't read, calibrated or ICCgrey, or an indexed image over another base or whose two entries look alike. Not one with a soft mask, whose own size would have tofollow; not a halftone or a dither, most of whose new pixels would cover both black and white,so that taking the sample covering more would turn its tones solid (a 50% screen black, a 25%diff --git a/crates/optimize/src/images.rs b/crates/optimize/src/images.rsindex 843486f91..0f0e67c0d 100644--- a/crates/optimize/src/images.rs+++ b/crates/optimize/src/images.rs@@ -1,6 +1,7 @@//! Image resampling and recompression.use std::collections::{HashMap, HashSet};+use std::sync::Arc;use pdfcraft_content::{Matrix, Pieces};use pdfcraft_cos::{DecodeBudget, Dict, Document, ObjRef, Object, Stream};@@ -305,7 +306,12 @@ fn device_rgb(doc: &Document, r: ObjRef) -> bool {fn indexed(doc: &Document, r: ObjRef) -> bool {let Object::Stream(s) = &*doc.get(r) else { return false };let cs = s.dict.get(b"ColorSpace").map(|cs| doc.resolve(cs));- matches!(cs.as_deref(), Some(Object::Array(a)) if matches!(a.first().and_then(Object::as_name), Some(b"Indexed" | b"I")))+ matches!(cs.as_deref(), Some(Object::Array(a)) if matches!(family(doc, a).as_deref().and_then(Object::as_name), Some(b"Indexed")))+}++/// The first entry of colour space array `a`, its family, read through a reference.+fn family(doc: &Document, a: &[Object]) -> Option<Arc<Object>> {+ a.first().map(|f| doc.resolve(f))}/// Call `f` with every reference in `o`, a dictionary's or array's included, a stream's dictionary@@ -762,7 +768,7 @@ fn one_component(doc: &Document, cs: &Object) -> bool {let cs = doc.resolve(cs);match &*cs {Object::Name(n) => matches!(n.as_slice(), b"DeviceGray" | b"G"),- Object::Array(a) => match a.first().and_then(Object::as_name) {+ Object::Array(a) => match family(doc, a).as_deref().and_then(Object::as_name) {Some(b"CalGray" | b"Separation" | b"Indexed" | b"I") => true,Some(b"ICCBased") => a.get(1).is_some_and(|icc| matches!(&*doc.resolve(icc), Object::Stream(s) if s.dict.int(b"N") == Some(1))),Some(b"DeviceN") => a.get(1).is_some_and(|names| doc.resolve(names).as_array().is_some_and(|n| n.len() == 1)),@@ -838,7 +844,7 @@ fn one_bit(doc: &Document, s: &Stream, pack: bool, defaults: bool) -> Option<(Vepacked.set(b"BitsPerComponent".to_vec(), Object::Int(1));// Components a pixel, bits a component, and a pixel's samples as 1 or 0.let (n, bpc, white): (usize, usize, Pixel) = match &*doc.resolve(cs) {- Object::Array(a) if matches!(a.first().and_then(Object::as_name), Some(b"Indexed" | b"I")) => {+ Object::Array(a) if matches!(family(doc, a).as_deref().and_then(Object::as_name), Some(b"Indexed" | b"I")) => {let bpc = bits.filter(|b| matches!(b, 2 | 4 | 8))?;match decode.as_deref() {None => {}@@ -938,10 +944,18 @@ fn dark_is_one(doc: &Document, d: &Dict) -> Option<bool> {if matches!(d.get(b"ImageMask"), Some(Object::Bool(true))) {return Some(d1 < d0);}+ // A colour key can hide the dark sample, and a tie going to it would lose a thin rule of the+ // sample that shows.+ if matches!(d.get(b"Mask").map(|m| doc.resolve(m)).as_deref(), Some(Object::Array(_))) {+ return None;+ }match d.get(b"ColorSpace").map(|cs| doc.resolve(cs)).as_deref() {Some(Object::Name(n)) if matches!(n.as_slice(), b"DeviceGray" | b"G") => Some(d1 < d0),- Some(Object::Array(a)) if matches!(a.first().and_then(Object::as_name), Some(b"Indexed" | b"I")) => {- let hival = a.get(2).and_then(|v| doc.resolve(v).as_int()).filter(|h| (0..=255).contains(h))?;+ Some(Object::Array(a)) if matches!(family(doc, a).as_deref().and_then(Object::as_name), Some(b"Indexed")) => {+ let hival = a.get(2).and_then(|v| match &*doc.resolve(v) {+ Object::Int(h) if (0..=255).contains(h) => Some(*h),+ _ => None,+ })?;// `hival` is 0 to 255, and the rounded value is clipped to it: the cast can't wrap.let entry = |v: f64| v.round().clamp(0.0, hival as f64) as usize;let (l0, l1) = (lightness(doc, a, entry(d0))?, lightness(doc, a, entry(d1))?);@@ -951,21 +965,31 @@ fn dark_is_one(doc: &Document, d: &Dict) -> Option<bool> {}}+/// The most bytes a palette is read to (see [`lightness`]).+const MAX_PALETTE: usize = 4096;+/// How light entry `i` of the palette of indexed colour space `a` (`[/Indexed base hival lookup]`)/// is, from 0 (black) to 255,000 (white): its grey, or its luma (Rec. 601) over `DeviceRGB`. Only/// those two bases count, where the palette's values are the colour: under `/DefaultGray` or/// `/DefaultRGB` they aren't, and the caller asks for no polarity there; `None` for another base/// (calibrated, CMYK, an ICC profile, a resource name), and for an entry the table doesn't hold.fn lightness(doc: &Document, a: &[Object], i: usize) -> Option<u32> {- let n = match doc.resolve(a.get(1)?).as_name()? {+ // A device space is its name, or that name alone in an array.+ let base = doc.resolve(a.get(1)?);+ let base = match &*base {+ Object::Array(b) if b.len() == 1 => doc.resolve(b.first()?),+ _ => base,+ };+ let n = match base.as_name()? {b"DeviceGray" => 1,b"DeviceRGB" => 3,_ => return None,};- // At most 256 entries of three components: a stream decoding to more is no palette.+ // At most 256 entries of three components: more is no palette. Images share a table, and this+ // is read for each of them, so a large one is refused before it's copied or decoded.let table = match &*doc.resolve(a.get(3)?) {- Object::String(s) => s.bytes.clone(),- Object::Stream(s) => doc.with_direct_parms(s).decoded_within(4096).ok()?,+ Object::String(s) if s.bytes.len() <= MAX_PALETTE => s.bytes.clone(),+ Object::Stream(s) if s.raw.len() <= 4 * MAX_PALETTE => doc.with_direct_parms(s).decoded_within(MAX_PALETTE).ok()?,_ => return None,};let c: Vec<u32> = table.get(i.checked_mul(n)?..i.checked_mul(n)?.checked_add(n)?)?.iter().map(|v| u32::from(*v)).collect();@@ -1089,8 +1113,8 @@ fn mono(doc: &Document, s: &Stream, ppi: f64, defaults: bool, settings: &MonoSet} else {(w, h)};- let polarity = if defaults { None } else { dark_is_one(doc, packed.as_ref().unwrap_or(d)) };- let averaged = if (nw, nh) != (w, h) && polarity.is_some() {+ let polarity = if defaults || (nw, nh) == (w, h) { None } else { dark_is_one(doc, packed.as_ref().unwrap_or(d)) };+ let averaged = if polarity.is_some() {// The dark sample of the image as it ends up: packed RGB is grey, with grey's decode pair.let (out, mixed, local_tone) = downsample_bits_checked(&data, w, h, nw, nh, polarity?)?;// A halftone or dither: most new pixels cover both samples. Text covers both only alongdiff --git a/crates/optimize/tests/optimize.rs b/crates/optimize/tests/optimize.rsindex 34df39cb1..1db90f92b 100644--- a/crates/optimize/tests/optimize.rs+++ b/crates/optimize/tests/optimize.rs@@ -3566,6 +3566,73 @@ fn an_indexed_palette_is_read_by_the_luma_of_its_colours() {assert_eq!(optimized("NotIndexed").0, Some(1200));}+/// What is averaged, by how the image is spelled. A family or a base by reference, or a device base+/// alone in an array, is the same colour space. `/I`, an inline abbreviation, and a real `hival`+/// aren't one, and neither is a table too large to be a palette, which images share and which+/// would be read for each. A colour key can hide the dark sample a tie goes to, so a thin rule of+/// the other would be lost, in grey too; a stencil `/Mask` doesn't.+#[test]+fn an_indexed_image_is_averaged_only_in_a_spelling_it_can_read() {+ let (w, h) = (1200u32, 480u32);+ let stride = (w as usize).div_ceil(8);+ let mut data = vec![0xFF; stride * h as usize];+ for y in 0..h as usize {+ data[y * stride + 101 / 8] &= !(0x80 >> (101 % 8));+ }+ let table = || Object::String(pdfcraft_cos::PdfString::literal(vec![0, 0, 0, 255, 255, 255]));+ let indexed = |family: Object, base: Object, hival: Object, table: Object| Object::Array(vec![family, base, hival, table]);+ let rgb = || Object::name("DeviceRGB");+ let plain = || indexed(Object::name("Indexed"), rgb(), 1.into(), table());+ let width = |make: &dyn Fn(&mut Document) -> Vec<(&'static str, Object)>| {+ let mut doc = Document::new_empty();+ let mut entries = vec![("BitsPerComponent", Object::Int(1))];+ entries.extend(make(&mut doc));+ let image = mono_image(&mut doc, w, h, &data, None, &entries);+ page(&mut doc, &[("Im", image)], "q 144 0 0 57.6 20 600 cm /Im Do Q");+ optimize(&mut doc, &Settings::default()).unwrap();+ stream(&doc, image).dict.int(b"Width")+ };+ let averaged = Some(600);+ let kept = Some(1200);+ assert_eq!(width(&|_| vec![("ColorSpace", plain())]), averaged, "direct");+ assert_eq!(+ width(&|doc: &mut Document| {+ let family = doc.add(Object::name("Indexed"));+ vec![("ColorSpace", indexed(Object::Ref(family), rgb(), 1.into(), table()))]+ }),+ averaged,+ "the family by reference"+ );+ let base = Object::Array(vec![rgb()]);+ assert_eq!(width(&|_| vec![("ColorSpace", indexed(Object::name("Indexed"), base.clone(), 1.into(), table()))]), averaged, "the base in an array");+ assert_eq!(width(&|_| vec![("ColorSpace", indexed(Object::name("I"), rgb(), 1.into(), table()))]), kept, "an inline abbreviation");+ assert_eq!(width(&|_| vec![("ColorSpace", indexed(Object::name("Indexed"), rgb(), Object::Real(1.0), table()))]), kept, "a real hival");+ let long = Object::String(pdfcraft_cos::PdfString::literal([vec![0, 0, 0, 255, 255, 255], vec![0; 4097]].concat()));+ assert_eq!(width(&|_| vec![("ColorSpace", indexed(Object::name("Indexed"), rgb(), 1.into(), long.clone()))]), kept, "a string past a palette");+ let mut d = Dict::new();+ d.set(b"Filter".to_vec(), Object::name("ASCIIHexDecode"));+ let hex: Vec<u8> = b"000000FFFFFF".iter().copied().chain(std::iter::repeat_n(b' ', 16385)).chain(*b">").collect();+ assert_eq!(+ width(&|doc: &mut Document| {+ let large = doc.add(Object::Stream(pdfcraft_cos::Stream::from_raw(d.clone(), hex.clone())));+ vec![("ColorSpace", indexed(Object::name("Indexed"), rgb(), 1.into(), Object::Ref(large)))]+ }),+ kept,+ "a stream too large to be read for each image, whatever it decodes to"+ );+ let key = || Object::Array(vec![0.into(), 0.into()]);+ assert_eq!(width(&|_| vec![("ColorSpace", plain()), ("Mask", key())]), kept, "a colour key, indexed");+ assert_eq!(width(&|_| vec![("ColorSpace", Object::name("DeviceGray")), ("Mask", key())]), kept, "a colour key, grey");+ assert_eq!(+ width(&|doc: &mut Document| {+ let key = doc.add(key());+ vec![("ColorSpace", plain()), ("Mask", Object::Ref(key))]+ }),+ kept,+ "a colour key by reference"+ );+}+/// White margins must not hide a small screened photograph from the tone guard.#[test]fn mixed_text_and_small_halftone_regions_keep_their_tones_with_wide_white_margins() {diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.tomlindex 1e637d106..c62a1bacc 100644--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@ -7385,7 +7385,7 @@ status = "partial"commands = ["optimize.advanced"]tools = ["doc_optimize"]evidence = ["crates/optimize/tests/optimize.rs::images_are_measured_where_drawn_and_downsampled", "crates/optimize/tests/optimize.rs::settings_choose_what_happens", "crates/automation/tests/it/automation.rs::optimizing_through_tools", "crates/optimize/tests/optimize.rs::a_page_picture_printed_as_one_pixel_strips_is_merged_into_one_image", "crates/optimize/tests/optimize.rs::images_that_do_not_tile_a_picture_are_left_alone", "crates/optimize/tests/optimize.rs::a_deeply_nested_form_is_measured_at_its_largest_use", "crates/optimize/tests/optimize.rs::images_are_not_resampled_when_some_uses_could_not_be_measured", "crates/optimize/tests/optimize.rs::image_resolution_uses_the_pages_physical_unit", "crates/optimize/tests/optimize.rs::optimization_retains_an_image_below_threshold_on_a_scaled_page", "crates/optimize/tests/optimize.rs::shared_forms_are_measured_in_each_pages_physical_units", "crates/optimize/tests/optimize.rs::ambiguous_monochrome_text_is_recompressed_without_lossy_reduction", "crates/optimize/tests/optimize.rs::indexed_thin_rules_survive_averaging_for_both_palette_orders_and_decodes", "crates/optimize/src/images.rs::an_indexed_images_palette_says_which_sample_is_dark", "crates/optimize/src/images.rs::it_matches_the_slow_way", "crates/automation/tests/it/automation.rs::doc_optimize_averages_black_and_white_scans_down", "crates/ui-egui/tests/it/create.rs::the_pdf_optimizer_dialog_saves_an_optimized_copy"]-notes = "colour and grayscale, bicubic, effective resolution measured where drawn (every use, with page-local UserUnit scaling, through nested forms up to 12 deep; when a use can't be measured, nothing is resampled); image fragments stacked edge to edge are merged first (side-by-side tiles not yet); monochrome grey, image masks and indexed images (1-bit, CCITT fax, and eight-bit black-and-white grey and RGB packed to one bit) averaged by area to 300 ppi above 450 ppi by default, a tie to the dark sample (black in grey, painted in a mask, the darker palette entry over DeviceGray or DeviceRGB, after a decode array of two different numbers), then stored as Group 4 or 1-bit Flate (Monochrome Images ▸ Downsample to; doc_optimize mono); not separations or DeviceN (their tint transforms aren't read), calibrated or ICC grey, indexed images over another base, images drawn or listed where /DefaultGray or /DefaultRGB is in force, images with a soft mask, halftones and dithers (most of their new pixels would cover both black and white, and their tones turn solid), images with a square of four such new pixels (a small screened picture on a white page; ragged scanned text has them along its strokes too, and keeps its resolution), or images wider than a fax row (1,048,576 samples), and Acrobat's bicubic and subsampling choices for monochrome aren't offered"+notes = "colour and grayscale, bicubic, effective resolution measured where drawn (every use, with page-local UserUnit scaling, through nested forms up to 12 deep; when a use can't be measured, nothing is resampled); image fragments stacked edge to edge are merged first (side-by-side tiles not yet); monochrome grey, image masks and indexed images (1-bit, CCITT fax, and eight-bit black-and-white grey and RGB packed to one bit) averaged by area to 300 ppi above 450 ppi by default, a tie to the dark sample (black in grey, painted in a mask, the darker palette entry over DeviceGray or DeviceRGB, after a decode array of two different numbers), then stored as Group 4 or 1-bit Flate (Monochrome Images ▸ Downsample to; doc_optimize mono); not separations or DeviceN (their tint transforms aren't read), calibrated or ICC grey, indexed images over another base or with a table over 4 KiB, images drawn or listed where /DefaultGray or /DefaultRGB is or may be in force (an indexed image also used beyond the page and form resources read, as an annotation's appearance), images with a colour key, which can hide the dark sample, or a soft mask, halftones and dithers (most of their new pixels would cover both black and white, and their tones turn solid), images with a square of four such new pixels (a small screened picture on a white page; ragged scanned text has them along its strokes too, and keeps its resolution), or images wider than a fax row (1,048,576 samples), and Acrobat's bicubic and subsampling choices for monochrome aren't offered"[[feature]]id = "optimize.recompress-images"