redact: one clearing walk for raw samples and JPEG samples

Both paths cleared the covered pixels with the same row/pixel box walk written twice.
`covered_pixels` is now the one place that decides which samples a region reaches —
main's conservative rotated/skewed box test, unchanged — and each path supplies what
clearing a sample means: bit surgery for raw data, zeroed components for a JPEG.

The two gray-scan tests also share one fixture (`marked_gray_scan`), which leaves only
the `/Decode` they declare as their difference.
This commit is contained in:
skylartaylor
2026-11-15 08:23:16 +01:00
parent 1ef4f2755b
commit 3e6dfcd06b
2 changed files with 57 additions and 66 deletions
+46 -54
View File
@@ -42,6 +42,34 @@ pub(crate) fn components(doc: &Document, s: &Stream) -> Option<usize> {
})
}
/// Run `clear` for every sample whose conservative box a region reaches, row by row, and say how
/// many there were. Image space has its origin at the bottom left and row 0 is at the top. The box
/// may be larger than the pixel for a rotated or skewed image, and that conservative over-clear is
/// what keeps a touched sample out of the file: raw samples and JPEG samples are cleared by the same
/// walk so the two cannot drift apart.
fn covered_pixels(ctm: &Matrix, rects: &[[f64; 4]], w: usize, h: usize, mut clear: impl FnMut(usize, usize) -> Option<()>) -> Option<usize> {
let mut cleared = 0usize;
for j in 0..h {
let v0 = 1.0 - (j + 1) as f64 / h as f64;
let v1 = 1.0 - j as f64 / h as f64;
let row_box = ctm.bbox([0.0, v0, 1.0, v1]);
if !rects.iter().any(|r| overlaps(*r, row_box, 0.0)) {
continue;
}
for i in 0..w {
let u0 = i as f64 / w as f64;
let u1 = (i + 1) as f64 / w as f64;
let pixel_box = ctm.bbox([u0, v0, u1, v1]);
if !rects.iter().any(|r| overlaps(*r, pixel_box, 0.0)) {
continue;
}
cleared += 1;
clear(i, j)?;
}
}
Some(cleared)
}
/// Whether `s`'s whole filter is a plain `/DCTDecode` — the JPEG a scanned page is made of. A DCT
/// image behind another filter is left to the fail-closed path.
fn plain_dct(s: &Stream) -> bool {
@@ -154,38 +182,20 @@ fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]]
} else {
false
};
let mut cleared = 0usize;
for j in 0..h {
// Image space has its origin at the bottom left; row 0 is at the top.
let v0 = 1.0 - (j + 1) as f64 / h as f64;
let v1 = 1.0 - j as f64 / h as f64;
let row_box = ctm.bbox([0.0, v0, 1.0, v1]);
if !rects.iter().any(|r| overlaps(*r, row_box, 0.0)) {
continue;
}
for i in 0..w {
let u0 = i as f64 / w as f64;
let u1 = (i + 1) as f64 / w as f64;
// The bounding box may cover extra pixels for a rotated or skewed image; that
// conservative over-clear prevents a touched sample surviving in the file.
let pixel_box = ctm.bbox([u0, v0, u1, v1]);
if !rects.iter().any(|r| overlaps(*r, pixel_box, 0.0)) {
continue;
}
cleared += 1;
// `bytes * 8` fits, and every bit index is below it, so this arithmetic can't overflow.
let start = j * row_bits + i * bits_per_pixel;
for bit in start..start + bits_per_pixel {
let (byte, shift) = (bit / 8, 7 - bit % 8);
let b = data.get_mut(byte)?;
if clear_bit {
*b |= 1 << shift;
} else {
*b &= !(1 << shift);
}
let cleared = covered_pixels(ctm, rects, w, h, |i, j| {
// `bytes * 8` fits, and every bit index is below it, so this arithmetic can't overflow.
let start = j * row_bits + i * bits_per_pixel;
for bit in start..start + bits_per_pixel {
let (byte, shift) = (bit / 8, 7 - bit % 8);
let b = data.get_mut(byte)?;
if clear_bit {
*b |= 1 << shift;
} else {
*b &= !(1 << shift);
}
}
}
Some(())
})?;
if cleared == 0 {
return Some(None);
}
@@ -247,30 +257,12 @@ fn dct_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]], w: us
if data.len() != w.checked_mul(h)?.checked_mul(ncomp)? {
return None;
}
let mut cleared = 0usize;
for j in 0..h {
// Image space has its origin at the bottom left; row 0 is at the top.
let v0 = 1.0 - (j + 1) as f64 / h as f64;
let v1 = 1.0 - j as f64 / h as f64;
let row_box = ctm.bbox([0.0, v0, 1.0, v1]);
if !rects.iter().any(|r| overlaps(*r, row_box, 0.0)) {
continue;
}
for i in 0..w {
let u0 = i as f64 / w as f64;
let u1 = (i + 1) as f64 / w as f64;
// As for raw samples: the box can be larger than the pixel for a rotated or skewed
// image, and that conservative over-clear keeps a touched sample out of the file.
let pixel_box = ctm.bbox([u0, v0, u1, v1]);
if !rects.iter().any(|r| overlaps(*r, pixel_box, 0.0)) {
continue;
}
cleared += 1;
let at = (j.checked_mul(w)?).checked_add(i)?.checked_mul(ncomp)?;
let sample = data.get_mut(at..at.checked_add(ncomp)?)?;
sample.fill(0);
}
}
let cleared = covered_pixels(ctm, rects, w, h, |i, j| {
let at = (j.checked_mul(w)?).checked_add(i)?.checked_mul(ncomp)?;
let sample = data.get_mut(at..at.checked_add(ncomp)?)?;
sample.fill(0);
Some(())
})?;
if cleared == 0 {
return Some(None);
}
+11 -12
View File
@@ -7354,17 +7354,23 @@ fn a_rotated_scan_is_still_cleared_under_the_mark() {
/// A grayscale scan with an explicit identity `/Decode [0 1]` is cleared in place too — the
/// component count of the `/Decode` check must be the image's own (one), not three, or every
/// explicit-declaration gray scan would be removed whole and the page blanked again.
#[test]
fn a_gray_scan_with_an_identity_decode_is_cleared_not_removed() {
/// A 20×20 gray scan drawn 100×100 at (10,10), marked over its middle, and the report applying the
/// mark gave: the one shape the gray cases differ in is the `/Decode` they declare.
fn marked_gray_scan(decode: &str) -> Report {
let mut gray = vec![200u8; 20 * 20];
gray[..20].fill(20);
let img = stream(
"/Type /XObject /Subtype /Image /Width 20 /Height 20 /ColorSpace /DeviceGray /BitsPerComponent 8 /Decode [0 1] /Filter /DCTDecode",
&format!("/Type /XObject /Subtype /Image /Width 20 /Height 20 /ColorSpace /DeviceGray /BitsPerComponent 8 {decode} /Filter /DCTDecode"),
jpeg_gray(&gray, 20, 20),
);
let mut doc = one_page(b"q 100 0 0 100 10 10 cm /Im0 Do Q", "/XObject << /Im0 7 0 R >>", vec![img]);
mark(&mut doc, 0, &[[40.0, 40.0, 60.0, 60.0]], "");
let r = apply(&mut doc, None).unwrap();
apply(&mut doc, None).unwrap()
}
#[test]
fn a_gray_scan_with_an_identity_decode_is_cleared_not_removed() {
let r = marked_gray_scan("/Decode [0 1]");
assert_eq!((r.images_removed, r.images_cleared), (0, 1), "the gray scan is cleared, not removed: {r:?}");
}
@@ -7372,15 +7378,8 @@ fn a_gray_scan_with_an_identity_decode_is_cleared_not_removed() {
/// the inverted mapping, so the whole image goes instead.
#[test]
fn a_gray_scan_with_an_inverted_decode_still_fails_closed() {
let img = stream(
"/Type /XObject /Subtype /Image /Width 20 /Height 20 /ColorSpace /DeviceGray /BitsPerComponent 8 /Decode [1 0] /Filter /DCTDecode",
jpeg_gray(&vec![200u8; 20 * 20], 20, 20),
);
let mut doc = one_page(b"q 100 0 0 100 10 10 cm /Im0 Do Q", "/XObject << /Im0 7 0 R >>", vec![img]);
mark(&mut doc, 0, &[[40.0, 40.0, 60.0, 60.0]], "");
let r = apply(&mut doc, None).unwrap();
let r = marked_gray_scan("/Decode [1 0]");
assert_eq!(r.images_removed, 1, "the inverted scan is removed whole: {r:?}");
assert!(!content(&doc, 0).contains("/Im0"));
}
/// A CMYK JPEG still fails closed: its samples would be written back in a space its