Skip to content

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

M11: redaction reads an image mask's /Decode given by reference

Grzegorz Martensbd5693679564
diff --git a/crates/redact/src/image.rs b/crates/redact/src/image.rs
index 6568e8015..a1bf022df 100644
--- a/crates/redact/src/image.rs
+++ b/crates/redact/src/image.rs
@@ -104,7 +104,10 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]
data.truncate(bytes);
// Image masks: a sample of 0 paints unless /Decode is [1 0]; cleared samples must not paint.
let clear_bit = if mask {
- let inverted = s.dict.get(b"Decode").and_then(Object::as_array).and_then(|d| d.first()).and_then(Object::as_f64) == Some(1.0);
+ // The array, and its first number, given by reference too.
+ let decode = s.dict.get(b"Decode").map(|d| doc.resolve(d));
+ let first = decode.as_deref().and_then(Object::as_array).and_then(|d| d.first()).map(|v| doc.resolve(v));
+ let inverted = first.as_deref().and_then(Object::as_f64) == Some(1.0);
!inverted
} else {
false
diff --git a/crates/redact/src/tests.rs b/crates/redact/src/tests.rs
index 9400391bb..c6885eaf1 100644
--- a/crates/redact/src/tests.rs
+++ b/crates/redact/src/tests.rs
@@ -579,7 +579,8 @@ fn an_image_whose_predictor_is_given_by_reference_has_the_covered_pixel_cleared(
/// A CCITT fax image (a scan the optimizer or Create PDF stored as Group 4) has its covered
/// pixels cleared in a copy, like any image PdfCraft can decode, instead of being removed whole:
-/// whether it sits behind another filter, with its parameters given by reference, or is a mask.
+/// whether it sits behind another filter, with its parameters given by reference, or is a mask,
+/// its decode array inverting it given directly, by reference or with its first number so.
#[test]
fn a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed() {
// 16 × 2, all 1 bits (white), or all 0 bits (a mask painting everywhere); the left half of
@@ -599,6 +600,20 @@ fn a_fax_image_has_its_covered_pixels_cleared_instead_of_being_removed() {
),
// A mask's 0 bits paint: cleared bits are set to 1, which paints nothing.
(format!("{image} /ImageMask true /Filter /CCITTFaxDecode /DecodeParms {parms}"), painted, vec![], [0xFF, 0x00, 0x00, 0x00]),
+ // With /Decode [1 0] a mask's 1 bits paint, and cleared bits are 0.
+ (format!("{image} /ImageMask true /Decode [1 0] /Filter /CCITTFaxDecode /DecodeParms {parms}"), g4.clone(), vec![], [0x00, 0xFF, 0xFF, 0xFF]),
+ (
+ format!("{image} /ImageMask true /Decode 8 0 R /Filter /CCITTFaxDecode /DecodeParms {parms}"),
+ g4.clone(),
+ vec![b"[1 0]".to_vec()],
+ [0x00, 0xFF, 0xFF, 0xFF],
+ ),
+ (
+ format!("{image} /ImageMask true /Decode [8 0 R 0] /Filter /CCITTFaxDecode /DecodeParms {parms}"),
+ g4.clone(),
+ vec![b"1".to_vec()],
+ [0x00, 0xFF, 0xFF, 0xFF],
+ ),
] {
let mut objs = vec![stream(&dict, data)];
objs.extend(extra);