PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
Merge branch 'main' into feat/redact-bilevel-codec
FredApps54d0ea7c5928
diff --cc crates/redact/README.mdindex 0a33c4e20,cbc7e8bfd..e590d0a03--- a/crates/redact/README.md+++ b/crates/redact/README.md@@@ -21,10 -21,11 +21,13 @@@ clear_marks(&mut doc, None)?(8/16-bit and 1/2/4-bit images, image masks; Flate/LZW/RL/A85/AHx, and CCITT fax or JBIG2 asthe last filter, decoded through `Document::decoded_image_within`, a JBIG2 image with its`/JBIG2Globals`; `/DecodeParms` given by reference, numbers inside included, count). The copy- is Flate, and refers to no globals. Globals hold the shapes of every image that shares them (a- scan's pages, often), those of marks under the area included, so every other image that uses- the globals of a removed or replaced image is decoded and stored again in place as Flate without- them (`Report::images_restored`), and they leave the file on the full rewrite; one that can't be+ is Flate, or for a one-bit image of one component (a scan as a rule) CCITT Group 4, or JBIG2- when it was JBIG2, whichever is smallest as stored, coded from the cleared samples; it refers to no globals: when no other image uses them, they leave the file with the- original on the full rewrite; when one does, they stay as they are, and may hold the shapes of- marks under the area (not where they were). Codecs redaction doesn't decode (DCT, JPX) are++ when it was JBIG2, whichever is smallest as stored, coded from the cleared samples; it refers to++ no globals. Globals hold the shapes of every image that shares them (a scan's pages, often),++ those of marks under the area included, so every other image that uses the globals of a removed++ or replaced image is decoded and stored again in place as Flate without them++ (`Report::images_restored`), and they leave the file on the full rewrite; one that can't be+ decoded for certain refuses the redaction (`RedactError::SharedJbig2Globals`). Codecs redaction doesn't decode (DCT, JPX) areremoved whole (fail-closed), and so is a JBIG2 image whose page isn't its size (the rendererdraws the whole page into the image, so the rows cleared wouldn't be the ones shown) or of morethan one bit or component, and Group 3 on bytes (`/K` 0 or more, `/EncodedByteAlign`): PdfCraft'sdiff --cc crates/redact/src/image.rsindex e9606c5e3,8c58d9c42..802c7a47e--- a/crates/redact/src/image.rs+++ b/crates/redact/src/image.rs@@@ -166,39 -165,36 +166,70 @@@ fn decode_and_clear(doc: &Document, s:dict.remove(b"Length");dict.remove(b"DecodeParms");dict.remove(b"Alternates");- Some(Some(Stream::flate(dict, &data)))+ Some(Some(smallest(dict, &data, w, h, ncomp == 1 && bpc == 1, is_jbig2)))+}++/// The cleared samples `data` of a `w` × `h` image as a stream with `dict`: Flate, or, for one bit+/// of one component (`bilevel`, a scan as a rule, which Flate stores several times larger), CCITT+/// Group 4, or JBIG2 when the image was JBIG2 (`jbig2`; JBIG2 is PDF 1.4, which such a file already+/// is), whichever is smallest as stored, dictionary included (a tiny image can lose by Group 4's+/// parameters). Both decode to `data`, cleared before it is coded, so nothing of the original's+/// code is carried over; Group 4 or JBIG2 that can't code the rows (too wide) is left out.+fn smallest(dict: Dict, data: &[u8], w: usize, h: usize, bilevel: bool, jbig2: bool) -> Stream {+ let flate = Stream::flate(dict.clone(), data);+ let Some((columns, rows)) = u32::try_from(w).ok().zip(u32::try_from(h).ok()).filter(|_| bilevel) else { return flate };+ let g4 = pdfcraft_filters::encode_g4(data, columns, rows).ok().map(|coded| {+ let mut dict = dict.clone();+ let mut parms = Dict::new();+ parms.set(b"K".to_vec(), Object::Int(-1));+ parms.set(b"Columns".to_vec(), Object::Int(i64::from(columns)));+ parms.set(b"Rows".to_vec(), Object::Int(i64::from(rows)));+ dict.set(b"Filter".to_vec(), Object::name("CCITTFaxDecode"));+ dict.set(b"DecodeParms".to_vec(), Object::Dict(parms));+ Stream::from_raw(dict, coded)+ });+ let jbig = jbig2.then(|| pdfcraft_filters::encode_jbig2(data, columns, rows).ok()).flatten().map(|coded| {+ let mut dict = dict.clone();+ dict.set(b"Filter".to_vec(), Object::name("JBIG2Decode"));+ Stream::from_raw(dict, coded)+ });+ [g4, jbig].into_iter().flatten().fold(flate, |best, s| if stored_len(&s) < stored_len(&best) { s } else { best })+}++/// The bytes stream `s` takes in a file: its data and its dictionary as written.+fn stored_len(s: &Stream) -> usize {+ let mut dict = Vec::new();+ pdfcraft_cos::serialize(&Object::Dict(s.dict.clone()), &mut dict);+ dict.len().saturating_add(s.raw.len())}++ /// JBIG2 image `s` stored again without its `/JBIG2Globals`: decoded through them, as it is drawn,+ /// and written as a Flate image of the same samples, its other entries kept. `None` when it can't+ /// be decoded for certain (too large, data that doesn't decode, a JBIG2 page of another size than+ /// the image, or not a one-bit image of one component). Redaction does this to every image that+ /// shares a redacted image's globals, since their symbol dictionary holds the shapes of marks drawn+ /// under the area too.+ pub(crate) fn without_globals(doc: &Document, s: &Stream) -> Option<Stream> {+ let (w, h) = (s.dict.int(b"Width")?, s.dict.int(b"Height")?);+ if w <= 0 || h <= 0 || (w as u64) * (h as u64) > MAX_PIXELS {+ return None;+ }+ let (w, h) = (w as usize, h as usize);+ let mask = matches!(s.dict.get(b"ImageMask"), Some(Object::Bool(true)));+ // JBIG2 codes one-bit images of one component (ISO 32000-2 §7.4.7).+ if !mask && (components(doc, s)?, s.dict.int(b"BitsPerComponent")?) != (1, 1) {+ return None;+ }+ let bytes = w.div_ceil(8).checked_mul(h)?;+ // As for `clear`: the filters before the JBIG2 decoder may give up to three bytes for each one+ // of the raster and a byte a row.+ let allowance = bytes.checked_add(h)?.checked_mul(3)?.checked_add(4096)?;+ let mut data = doc.decoded_image_within(s, allowance).ok()?;+ if data.len() < bytes {+ return None;+ }+ data.truncate(bytes);+ let mut dict = s.dict.clone();+ dict.remove(b"Length");+ Some(Stream::flate(dict, &data))+ }diff --cc parity/acrobat-features.tomlindex e87195a4d,d892eb75f..ebafe530b--- a/parity/acrobat-features.toml+++ b/parity/acrobat-features.toml@@@ -5804,8 -5824,8 +5824,8 @@@ milestone = "M8status = "shipped"commands = ["redact.apply"]tools = ["redact_apply"]- evidence = ["crates/redact/src/tests.rs::images_are_removed_or_have_their_pixels_cleared", "crates/redact/src/tests.rs::redaction_clears_pixels_intersected_without_covering_their_centres", "crates/redact/src/tests.rs::a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::group_3_on_bytes_is_removed_whole_and_other_fax_images_cleared", "crates/redact/src/tests.rs::an_image_whose_predictor_is_given_by_reference_has_the_covered_pixel_cleared", "crates/redact/src/tests.rs::a_jbig2_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::a_jbig2_image_that_cannot_be_cleared_for_certain_is_removed_whole", "crates/redact/src/tests.rs::a_redacted_jbig2_scan_keeps_neither_its_code_nor_its_globals", "crates/redact/src/tests.rs::a_cleared_black_and_white_scan_is_stored_as_group_4"]- notes = "intersected pixel footprints are cleared conservatively in a copy, CCITT fax and JBIG2 images (scans stored as Group 4 or JBIG2, with their /JBIG2Globals) included; DCT/JPX images, Group 3 on bytes (which the bootstrap renderer may draw as other rows than the decoder reads), and a JBIG2 image whose page isn't its size, removed whole. The copy is Flate, or for a one-bit image of one component Group 4, or JBIG2 when it was JBIG2, whichever is smallest as stored, and refers to no globals; globals another image still uses stay, and may hold the shapes of marks under the area (not where they were)"-evidence = ["crates/redact/src/tests.rs::images_are_removed_or_have_their_pixels_cleared", "crates/redact/src/tests.rs::redaction_clears_pixels_intersected_without_covering_their_centres", "crates/redact/src/tests.rs::a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::group_3_on_bytes_is_removed_whole_and_other_fax_images_cleared", "crates/redact/src/tests.rs::an_image_whose_predictor_is_given_by_reference_has_the_covered_pixel_cleared", "crates/redact/src/tests.rs::a_jbig2_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::a_jbig2_image_that_cannot_be_cleared_for_certain_is_removed_whole", "crates/redact/src/tests.rs::a_redacted_jbig2_scan_keeps_neither_its_code_nor_its_globals", "crates/redact/src/tests.rs::globals_a_redacted_jbig2_image_shares_with_another_page_leave_the_file"]-notes = "intersected pixel footprints are cleared conservatively in a copy, CCITT fax and JBIG2 images (scans stored as Group 4 or JBIG2, with their /JBIG2Globals) included; DCT/JPX images, Group 3 on bytes (which the bootstrap renderer may draw as other rows than the decoder reads), and a JBIG2 image whose page isn't its size, removed whole. The copy is Flate (larger than a Group 4 or JBIG2 scan) and refers to no globals; every other image using a redacted image's globals is stored again as Flate without them, so they leave the file (one that can't be decoded refuses the redaction)"++evidence = ["crates/redact/src/tests.rs::images_are_removed_or_have_their_pixels_cleared", "crates/redact/src/tests.rs::redaction_clears_pixels_intersected_without_covering_their_centres", "crates/redact/src/tests.rs::a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::group_3_on_bytes_is_removed_whole_and_other_fax_images_cleared", "crates/redact/src/tests.rs::an_image_whose_predictor_is_given_by_reference_has_the_covered_pixel_cleared", "crates/redact/src/tests.rs::a_jbig2_image_has_its_covered_pixels_cleared_instead_of_being_removed", "crates/redact/src/tests.rs::a_jbig2_image_that_cannot_be_cleared_for_certain_is_removed_whole", "crates/redact/src/tests.rs::a_redacted_jbig2_scan_keeps_neither_its_code_nor_its_globals", "crates/redact/src/tests.rs::a_cleared_black_and_white_scan_is_stored_as_group_4", "crates/redact/src/tests.rs::globals_a_redacted_jbig2_image_shares_with_another_page_leave_the_file"]++notes = "intersected pixel footprints are cleared conservatively in a copy, CCITT fax and JBIG2 images (scans stored as Group 4 or JBIG2, with their /JBIG2Globals) included; DCT/JPX images, Group 3 on bytes (which the bootstrap renderer may draw as other rows than the decoder reads), and a JBIG2 image whose page isn't its size, removed whole. The copy is Flate, or for a one-bit image of one component Group 4, or JBIG2 when it was JBIG2, whichever is smallest as stored, and refers to no globals; every other image using a redacted image's globals is stored again as Flate without them, so they leave the file (one that can't be decoded refuses the redaction)"[[feature]]id = "protect.redact-vectors"