mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-27 13:57:18 +00:00
* M11: JBIG2 images share a symbol dictionary, up to 16 at a time With Monochrome Images set to JBIG2, Optimize stored each image's letters in a symbol dictionary of its own, so a document of N pages carried its font's shapes N times. Now up to 16 images the run stores as JBIG2 share one dictionary in a /JBIG2Globals stream, holding the marks found in more than one of them; each image's text regions refer to it first, then to a dictionary of its own for the marks only it repeats. An image is stored that way when it comes out smaller, and a group only when what its images save is more than the globals take. - filters: encode_jbig2_shared codes a group: the globals are a symbol dictionary segment numbered 0 and associated with no page (7.2.6); each page numbers its segments from 1, as a segment refers only to lower numbers (7.2.5), and the shared symbols' IDs come first, as the dictionaries are referred to in that order (Table 31, SBSYMS). At most 16,383 shared marks, those in the most images, so a page keeps room for its own under the decoder's 32,767. - optimize: the run holds the 1-bit samples of the images it stores as JBIG2 (16 at most, 64 MiB at most) and stores them again sharing, weighing each with the longest reference the globals could have. Eight pages of 10 pt text rendered at 300 dpi take 41% less in Helvetica and 42% in Times than coded alone, 10% in Courier, and 46% in Helvetica at 600 dpi; groups of 2, 4, 16 and 32 save 19%, 33%, 45% and 47%. Redacting one image stores the others of its group again without the dictionary, so 16 keeps that to 15 images. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY * M11: test what the mutants found untested in shared JBIG2 dictionaries - filters: the group goes through encode_jbig2_shared, which refuses it for an image the encoder refuses; two marks in common are enough to share; past the 65,536 distinct marks a group holds, a mark first seen later isn't shared while those held count every image. - optimize: optimizing again keeps both the shared pages and the pages coded alone as they are; under Retain, averaged JBIG2 pages share nothing; a last group of two shares. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY * M11: only images redaction can store again share a JBIG2 dictionary Redacting one image of a group stores the others again without the dictionary, which can hold the shapes of marks under the area, and refuses the redaction for one it can't: it takes image masks and images of one component as its own reading counts them (no DeviceN, the family named directly), and at most 64 Mi pixels. Optimize took DeviceN of one colourant and up to 80 million pixels, so such an image in a group made any redaction of the others fail, where before it was coded alone and the same redaction worked (the Consumers review). Only an image mask or a DeviceGray image named directly, of at most 2^26 pixels, now joins a group; others are coded alone. An engine test optimizes three pages, two DeviceGray and one DeviceN, and redacts the first: the DeviceN page stays alone, the second is stored again, and no image names the dictionary after a save. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY * M11: optimize and redaction share one limit on the images redaction stores again The second Consumers review: Optimize's limit on images that share a JBIG2 dictionary copied redaction's private 64 Mi, with nothing tying the two, so a lower one in redaction would bring back refused redactions with no test going red. Both now read pdfcraft_cos::MAX_REDACTED_IMAGE_PIXELS. The engine test adds an image mask, the other kind that shares, so a redaction that stopped storing either kind again fails it. doc_optimize's description, the README, the parity note and ROADMAP say which images share (named directly). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY * M11: a JBIG2 group is coded before its samples would pass the limit CodeRabbit on #5910: hold added an image to the group and then checked SHARED_BYTES, so a group could pass the 64 MiB it is documented to stay within by its last image, which can itself be up to 64 MiB (an image that shares has at most 2^26 pixels, and as its rows are whole bytes, at most that many bytes; a 1-pixel-wide one takes them all). hold_in codes the group first when an image would take it past the limit, and after when it has 16 images; with room for 10 bytes, images of 4 go two to a group, where the old order made one of 12. CodeRabbit's follow-up too: the 18-image test decodes every image and compares it with its samples, across the boundary between the groups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY * optimize: count the globals' object framing; filters: test the shared coder's size limit - A JBIG2 group shares a dictionary only when it saves more than the globals' dictionary and data plus NEW_OBJECT (96 bytes: the object's framing, its xref entry, and an encrypted file's IV and padding), so a group that saves less than that no longer makes the file larger. - encode_jbig2_shared is Unsupported for an image taller than the decoder takes and codes one at the bound. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filters: drop the unused rows binding in encode_shared page() no longer takes the rows since #5511. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filters: the shared coder's at-bound test says what it returns Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
247 lines
12 KiB
Rust
247 lines
12 KiB
Rust
//! Clearing the pixels of an image under redaction regions. The image is decoded, every pixel
|
||
//! whose footprint intersects a region is set to zero (no ink for image masks), and the result is
|
||
//! written as a new image, Flate, or for a one-bit image of one component Group 4, or JBIG2 when it
|
||
//! was JBIG2, whichever is smallest; the original object is untouched (other pages may use it).
|
||
//! CCITT fax and JBIG2 images decode through `ImageBudget::decode`, but for Group 3 on
|
||
//! bytes, which PdfCraft's renderer may draw as other rows, and a JBIG2 page of other rows than the
|
||
//! image's. Images whose codec PdfCraft can't decode (DCT, JPX), and those, return `None`, and the
|
||
//! caller removes the whole image instead; that is fail-closed.
|
||
|
||
use pdfcraft_content::{Matrix, overlaps};
|
||
use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream};
|
||
|
||
/// Most pixels an image may have to be cleared in place (larger ones are removed), or to be stored
|
||
/// again without shared `/JBIG2Globals` (see [`without_globals`]).
|
||
const MAX_PIXELS: u64 = pdfcraft_cos::MAX_REDACTED_IMAGE_PIXELS;
|
||
|
||
fn components(doc: &Document, s: &Stream) -> Option<usize> {
|
||
let cs = doc.resolve(s.dict.get(b"ColorSpace")?);
|
||
let cs = &*cs;
|
||
let name = match cs {
|
||
Object::Name(n) => n.as_slice(),
|
||
Object::Array(a) => a.first()?.as_name()?,
|
||
_ => return None,
|
||
};
|
||
Some(match name {
|
||
b"DeviceGray" | b"G" | b"CalGray" | b"Indexed" | b"I" | b"Separation" => 1,
|
||
b"DeviceRGB" | b"RGB" | b"CalRGB" | b"Lab" => 3,
|
||
b"DeviceCMYK" | b"CMYK" => 4,
|
||
b"ICCBased" => {
|
||
// /N lives in the ICC profile stream, and shall be 1, 3 or 4 (ISO 32000-2 §8.6.5.5).
|
||
match &*doc.resolve(cs.as_array()?.get(1)?) {
|
||
Object::Stream(icc) => usize::try_from(icc.dict.int(b"N")?).ok().filter(|n| matches!(n, 1 | 3 | 4))?,
|
||
_ => return None,
|
||
}
|
||
}
|
||
_ => return None,
|
||
})
|
||
}
|
||
|
||
/// `Ok(Some(copy))` with the covered pixels cleared, `Ok(None)` when no pixel intersects,
|
||
/// `Err(())` when the image can't be cleared (the caller removes it).
|
||
#[allow(clippy::result_unit_err)]
|
||
pub(crate) fn clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]], budget: &mut ImageBudget) -> Result<Option<Stream>, ()> {
|
||
decode_and_clear(doc, s, ctm, rects, budget).ok_or(())
|
||
}
|
||
|
||
fn decode_and_clear(doc: &Document, s: &Stream, ctm: &Matrix, rects: &[[f64; 4]], budget: &mut ImageBudget) -> Option<Option<Stream>> {
|
||
// Separate masks can retain the original image data.
|
||
if s.dict.contains(b"SMask") || s.dict.contains(b"Mask") {
|
||
return None;
|
||
}
|
||
let supported = |n: &[u8]| {
|
||
matches!(
|
||
n,
|
||
b"FlateDecode" | b"Fl" | b"LZWDecode" | b"LZW" | b"ASCII85Decode" | b"A85" | b"ASCIIHexDecode" | b"AHx" | b"RunLengthDecode" | b"RL"
|
||
)
|
||
};
|
||
// A CCITT fax or JBIG2 image (its last filter) decodes through `ImageBudget::decode`,
|
||
// after the filters before it, so a scan stored as Group 4 or JBIG2 is cleared like any other
|
||
// image.
|
||
let fax = |f: &Object| f.as_name().is_some_and(|n| matches!(n, b"CCITTFaxDecode" | b"CCF"));
|
||
let jbig2 = |f: &Object| f.as_name() == Some(b"JBIG2Decode".as_slice());
|
||
let readable = |f: &Object| f.as_name().is_some_and(supported);
|
||
// `/Crypt` was undone when the object was loaded (ISO 32000-2 §7.4.10), and decoding leaves it
|
||
// out wherever it stands.
|
||
let crypt = |f: &Object| f.as_name() == Some(b"Crypt".as_slice());
|
||
let (ok, is_fax, is_jbig2) = match s.dict.get(b"Filter") {
|
||
None => (true, false, false),
|
||
Some(f @ Object::Name(_)) => (readable(f) || crypt(f) || fax(f) || jbig2(f), fax(f), jbig2(f)),
|
||
Some(Object::Array(a)) => {
|
||
let chain: Vec<&Object> = a.iter().filter(|f| !crypt(f)).collect();
|
||
match chain.split_last() {
|
||
Some((last, before)) if fax(last) || jbig2(last) => (before.iter().all(|f| readable(f)), fax(last), jbig2(last)),
|
||
_ => (chain.iter().all(|f| readable(f)), false, false),
|
||
}
|
||
}
|
||
_ => (false, false, false),
|
||
};
|
||
if !ok {
|
||
return None;
|
||
}
|
||
// Group 3 on bytes is removed whole for now. The decoder reads it by its end-of-line codes
|
||
// when the data starts with one, but PdfCraft's renderer draws such rows (the fill ending
|
||
// each code on a byte) as other rows, so the pixels a mark covers on screen needn't be the
|
||
// ones cleared.
|
||
if is_fax && doc.fax_params(s).is_none_or(|p| p.k >= 0 && p.encoded_byte_align) {
|
||
return None;
|
||
}
|
||
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)));
|
||
let (ncomp, bpc) = if mask { (1, 1) } else { (components(doc, s)?, s.dict.int(b"BitsPerComponent")? as usize) };
|
||
// JBIG2 codes one-bit images of one component (ISO 32000-2 §7.4.7).
|
||
if !matches!(bpc, 1 | 2 | 4 | 8 | 16) || (is_jbig2 && (ncomp, bpc) != (1, 1)) {
|
||
return None;
|
||
}
|
||
// Every size comes from the file: multiply checked, so an overflowing product refuses the image
|
||
// instead of sizing the writes below.
|
||
let bits_per_pixel = ncomp.checked_mul(bpc)?;
|
||
let row = w.checked_mul(bits_per_pixel)?.div_ceil(8);
|
||
let bytes = row.checked_mul(h)?;
|
||
let row_bits = row.checked_mul(8)?;
|
||
bytes.checked_mul(8)?;
|
||
// Decode no further than the raster can need: a filter stage may legitimately produce up to
|
||
// three bytes for each of it and a PNG predictor's byte per row (ASCIIHex text under Flate,
|
||
// two digits a byte broken into lines or spaced), as the optimizer allows. A fax image's
|
||
// code, which the filters before its decoder produce, takes at most about a byte per pixel
|
||
// plus each row's end. Data past that can only be a decompression bomb; the image is then
|
||
// removed whole.
|
||
let allowance = if is_fax {
|
||
w.checked_mul(h)?.checked_add(h.checked_mul(4)?)?.checked_add(4096)?
|
||
} else {
|
||
bytes.checked_add(h)?.checked_mul(3)?.checked_add(4096)?
|
||
};
|
||
// Through the document, which reads `/DecodeParms` given by reference (`Stream::decoded*`
|
||
// alone would skip such a predictor, and the cleared bytes would then miss the covered pixels),
|
||
// and refuses a JBIG2 page of another size than the image: the renderer draws the whole page
|
||
// into the image, so the rows cleared wouldn't be the ones a mark covers on screen.
|
||
let mut data = budget.decode(doc, s, allowance).ok()?;
|
||
if data.len() < bytes {
|
||
return None;
|
||
}
|
||
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 {
|
||
// 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
|
||
};
|
||
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);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if cleared == 0 {
|
||
return Some(None);
|
||
}
|
||
let mut dict = s.dict.clone();
|
||
dict.remove(b"Length");
|
||
dict.remove(b"DecodeParms");
|
||
dict.remove(b"Alternates");
|
||
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 the same samples, Flate, Group 4 or JBIG2 coded alone, whichever is smallest
|
||
/// (`smallest`), its other entries kept. `None` when it can't be decoded for certain (too large,
|
||
/// past what is left of `budget`, 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, budget: &mut ImageBudget) -> 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 = budget.decode(doc, s, allowance).ok()?;
|
||
if data.len() < bytes {
|
||
return None;
|
||
}
|
||
data.truncate(bytes);
|
||
let mut dict = s.dict.clone();
|
||
dict.remove(b"Length");
|
||
// The globals are named in the parameters; the copy is coded alone, as `clear`'s are.
|
||
dict.remove(b"DecodeParms");
|
||
dict.remove(b"Alternates");
|
||
Some(smallest(dict, &data, w, h, true, true))
|
||
}
|