Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M11: the docs say which black-and-white images monochrome downsampling averages since #5426

Grzegorz Martensc0e3c6bec5c5
diff --git a/crates/optimize/README.md b/crates/optimize/README.md
index 9f96b1721..4ac11e5a8 100644
--- a/crates/optimize/README.md
+++ b/crates/optimize/README.md
@@ -60,13 +60,18 @@ let report = optimize_with_progress(&mut doc, &settings, &mut |stage| keep_going
- **Downsampling:** one whose effective resolution is above `above_ppi` is averaged down to
`target_ppi`: each new pixel takes the sample covering more of its area, and the dark one on a
tie, so a dark line half a new pixel wide stays: black in grey and the painted part of an image
- mask (a 0, or a 1 with an inverting decode array), full ink in a separation (a 1, or a 0), and
- entry 0 of an indexed image, whatever colour its palette gives it (the palette isn't read yet).
- Not one with a soft mask, whose own size would have to follow; 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% one white); and not one wider than
- a fax row (`pdfcraft_filters::MAX_CCITT_COLUMNS`), as the averaging keeps a counter for each
- column. On a 600 ppi text page, Group 4 at 300 ppi is half the size.
+ mask (a 0, or a 1 with an inverting decode array). Only images whose dark sample is known so
+ are averaged: `DeviceGray` (grey and RGB packed to one bit among them) and image masks, 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. Not a separation, a DeviceN, calibrated or ICC grey, or an
+ indexed image, whose palette isn't read. Not one with a soft mask, whose own size would have to
+ follow; 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%
+ one white), nor one with a square of four new pixels that each would, as a small screened
+ picture on a white page has: ragged scanned text has such squares along its strokes too, and
+ is then left at its resolution. And not one wider than a fax row
+ (`pdfcraft_filters::MAX_CCITT_COLUMNS`), as the averaging keeps a counter for each column. On a
+ 600 ppi page of text that is averaged, Group 4 at 300 ppi is about half the size.
- **Compression:** CCITT Group 4 (`pdfcraft_filters::encode_g4`), or 1-bit Flate when that is
smaller, which only a resampled image or one packed from more bits may be: a 1-bit image that
is only stored again becomes Group 4 or stays. Group 4 alone is only decoded to be resampled;
diff --git a/crates/optimize/src/images.rs b/crates/optimize/src/images.rs
index 933806197..36fb2f6b0 100644
--- a/crates/optimize/src/images.rs
+++ b/crates/optimize/src/images.rs
@@ -1000,9 +1000,12 @@ fn downsample_bits_checked(data: &[u8], w: u32, h: u32, nw: u32, nh: u32, dark_i
/// `/DefaultGray` or `/DefaultRGB` is or may be in force. Also says whether it was resampled.
///
/// - **Resampled** ([`downsample_bits_checked`]) when its resolution is above `above_ppi`, to `target_ppi`,
+/// if its dark sample is known ([`dark_is_one`]: grey or an image mask, not under `defaults`),
/// unless it has a soft mask, whose own size would then have to follow, or is a halftone or a
/// dither: most of its new pixels would cover both black and white, and taking the sample that
-/// covers more would turn its tones solid (a 50% screen black, a 25% one white).
+/// covers more would turn its tones solid (a 50% screen black, a 25% one white). So would a
+/// square of four new pixels that each cover both, as a small screened picture on a white page
+/// has (and ragged scanned text, which is then left at its resolution too).
/// - **Stored** as CCITT Group 4 with `CcittG4`, or as 1-bit Flate when that is smaller, which only
/// a resampled image or one packed from more bits may be: a 1-bit image that is only stored again
/// becomes Group 4 or stays. Group 4 alone is only decoded to be resampled; Group 3 is stored
diff --git a/crates/optimize/src/lib.rs b/crates/optimize/src/lib.rs
index 76e30da7a..6e8988fd1 100644
--- a/crates/optimize/src/lib.rs
+++ b/crates/optimize/src/lib.rs
@@ -13,7 +13,8 @@
//! and `/ColorTransform` disagree). Black-and-white images (1-bit, image masks and CCITT fax,
//! Group 3 among them; and eight-bit grey and RGB of only black and white, and indexed images
//! that use only palette entries 0 and 1, packed to one bit) above a threshold are averaged down to a
-//! target resolution, halftones and dithers aside, and are stored as CCITT Group 4 or lossless JBIG2, or 1-bit
+//! target resolution when grey or image masks, halftones and dithers aside (see the README), and
+//! are stored as CCITT Group 4 or lossless JBIG2, or 1-bit
//! Flate once resampled or packed, when that is smaller.
//! - **Discard objects:** page thumbnails, alternate images, document tags (structure tree),
//! print settings.
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 1404247cd..ed1f162ba 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/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 (1-bit, CCITT fax, and eight-bit black-and-white grey and RGB, and indexed images that use only palette entries 0 and 1) averaged by area to 300 ppi above 450 ppi by default, a tie to the dark sample (black in grey, painted in a mask, ink in a separation, after the decode array; an indexed image's entry 0, its palette not read yet), then stored as Group 4 or 1-bit Flate (Monochrome Images ▸ Downsample to; doc_optimize mono); not monochrome images with a soft mask, halftones and dithers (most of their new pixels would cover both black and white, and their tones turn solid), 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 images and image masks (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, 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, DeviceN, calibrated or ICC grey, or indexed images (their dark sample isn't read), 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"
[[feature]]
id = "optimize.recompress-images"