decode_jbig2 sits next to decode_ccitt, through hayro-jbig2 as Export ▸ Images used it, and shares CCITT's row sink. The JBIG2 page has to be the image's size: PdfCraft's renderer draws the whole page into the image (black box: a 4-row page behind /Height 2 fills the image box with all four rows), so the rows of a taller page aren't the image's. The decoder decodes a page whole, so the page is checked before it is decoded: that size, within the caller's bound as rows of one bit, and at most 2^28 pixels (MAX_JBIG2_PIXELS; the decoder keeps a bit a pixel, about 34 MB there). Document::decoded_image_within decodes a JBIG2 image as it does a fax image: the filters before it undone first, then the decoder with the /JBIG2Globals of the image's own /DecodeParms entry (/Crypt counts in the position; fax_params and the globals now share last_filter_parms). Globals have a limit of their own, MAX_JBIG2_GLOBALS (32 MiB, moved from Export), and globals that can't be read are an error rather than ignored. Export ▸ Images reads JBIG2 through it, so its own decoder, row sink and DecodeParms reader go, and hayro-jbig2 leaves pdfcraft-create. JBIG2 behind another filter now exports, and so copies (Copy Image uses the same function); a page taller than the image, which Export cut to the image's rows, now skips the image, as one of another width did. doc_export_all_images' description no longer lists JBIG2 inside other filters as skipped. The optimizer's monochrome step read images through decoded_image_within and relied on JBIG2 failing there: it now leaves JBIG2 images alone explicitly, as before (a resampled one would come out Flate under Retain), and the README and parity note list them as not yet handled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pdfcraft-optimize
Layer L4: the PDF Optimizer (execution plan M11.1; Acrobat's Reduce File Size and Optimize PDF ▸ Advanced optimization).
let report = optimize(&mut doc, &Settings::default())?; // Reduce File Size's choices
let report = optimize(&mut doc, &Preset::Mobile.settings())?; // smaller, for screens
// Or with progress: called before each image and the clean-up; `false` cancels.
let report = optimize_with_progress(&mut doc, &settings, &mut |stage| keep_going)?;
- Presets: Standard (Reduce File Size's choices: images above 225 ppi to 150 ppi, JPEG medium) and Mobile (above 144 ppi to 96 ppi, JPEG high), as in Acrobat's Settings menu. In both, black-and-white images are averaged down to 300 ppi above 450 ppi, and stored as CCITT Group 4 whenever that is smaller.
- Merge image fragments (
fragments, on by default): some PDF printers draw a page picture as thousands of images one pixel row high. Runs of upright 8-bit grey or RGB images in one colour space, sharing their left and right edges and stacked edge to edge (at least four, drawn with nothing else between them), become one lossless image at their finest resolution, which the image settings then resample; the strips are dropped by Clean Up. A real page printed this way went from 4.3 MB to 231 KB (Standard) and 161 KB (Mobile). Not merged yet: side-by-side tiles, rotated strips, masks. Content arrays use the shared space boundary policy, so trailing comments, split operands and graphics state keep their meaning after merging. - Images: the effective resolution of each image is measured where pages draw it (the CTM
through form XObjects, up to 12 deep; the smallest over all uses).
Page-local
/UserUnitscaling is included, so large-format pages are measured in physical inches. If a use can't be measured (forms nested deeper, or drawn too many times), no image is resampled. Colour and grayscale images above a threshold are resampled with a bicubic (Catmull-Rom) filter to a target resolution and stored as JPEG (Acrobat's quality steps) or ZIP; soft masks are resampled with their image. An image is replaced only if the result is smaller. Left alone by these colour and grey settings: CMYK and other colour spaces, image masks, decode arrays, more than 8 bits, JPEG 2000, JBIG2 and CCITT (black-and-white images, CCITT among them, are below). A JPEG is read with its colour transform (an Adobe marker's, else/ColorTransform, ISO 32000-2 §7.4.8), so one stored untransformed keeps its colours; one whose marker and/ColorTransformdisagree is left alone, since readers differ on which wins. Samples are read with/DecodeParmsgiven by reference too, the numbers inside included (Document::with_direct_parms), here and below: without them a predictor's tag bytes would shift every row of an image written back. - Monochrome images (
Settings::mono,MonoSettings::REDUCEby default: to 300 ppi above 450 ppi, CCITT Group 4). Black-and-white images are a 1-bit image with one colour component (grey, indexed, a separation) or an image mask, stored with no filter, with general-purpose ones or with CCITT fax after any of those (decoded byDocument::decoded_image_within, no further than its rows can need; byte-aligned Group 3 with end-of-line codes but none before the first row, not as T.4 sends them, is read as PdfCraft draws it, which at/K 0may be other rows than were coded, and an image stored again keeps them). Packed to one bit first, unless a colour-key/Maskis in the image's own values: eight-bit grey of only 0 and 255; eight-bitDeviceRGBof only black and white, which becomesDeviceGray(not calibrated RGB or RGB with a profile, nor one drawn or listed where/DefaultGrayor/DefaultRGBis in force, which would then draw it otherwise, nor one referred to from anywhere but the page and form resources the optimizer reads, where they may be: an annotation's appearance or a pattern drawing it, directly or through resources or a form it shares with a page, or another image's/Alternates; nor one whose soft mask has a/Matte); and an indexed image of 2, 4 or 8 bits that uses only palette entries 0 and 1, which keeps its palette, whatever its two colours. A decode array stays when it means the same at one bit (grey's, or RGB's if the same for each component, as grey's), and an indexed image's default one goes, as the default at one bit; otherwise the image is left alone. The grey or colour settings take a packed image instead when they make it smaller still, its soft mask counted (resampled with it there, kept as it is here), as they can a dithered scan by downsampling it to JPEG.-
Downsampling: one whose effective resolution is above
above_ppiis averaged down totarget_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). 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/DefaultGrayor/DefaultRGBis 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; Group 3, or Group 4 inside another filter such asASCII85Decode, is stored again as Group 4 alone when that is smaller. Damaged Group 3 with end-of-line codes is stored as the decoder recovers it, a bad row picked up again at the next code, as it reports no damage there; without them, damage stops the decoding and the image is left alone. With Retain, a resampled image keeps its kind of compression (CCITT, or Flate) and nothing else changes; an image of more bits is then left to the grey or colour settings. Every change is kept only when it is smaller as stored. -
Apart from the downsampling it is lossless: the samples keep their meaning, so the colour space, decode array and
/Maskstay, but for packed RGB, which becomes grey with one decode pair. Group 4 wins on scans, whose ragged edges defeat Flate; on clean computer-drawn 1-bit art Flate can be smaller. Not yet: JBIG2 images themselves (left as they are, neither downsampled nor stored again, thoughDocument::decoded_imagereads them), inline images, an image's own/SMaskor/Maskimage (only images drawn on a page are visited), and Group 4 for rows wider than 32,767 samples. -
Lossless JBIG2 uses bounded generic-region encoding or exact-copy symbol dictionaries when smaller. No lossy symbol substitution is used. Export reads embedded images and bounded shared globals.
-
- Discard objects: thumbnails, alternate images, document tags, print settings.
- Clean up: Flate for streams with no filter; for streams with an outer ASCII85 or ASCIIHex
layer, that layer is removed when the result is smaller (the inner filters keep their encoded
bytes; streams with
/DecodeParms, images and object streams keep their ASCII layer. LZW-compressed streams, content and images alike, are decoded and stored with Flate instead (Acrobat's "In streams that use LZW encoding, use Flate instead"): a predictor carries over to Flate withoutEarlyChange, filters applied before the LZW one go with it and those after it keep their bytes. A stream whose LZW data doesn't decode cleanly is left as it is. Not yet: inline images.
The engine runs Remove Hidden Information (pdfcraft-redact) for the user-data categories,
then this, then merges identical objects and writes a full, compressed save
(pdfcraft_engine::optimizer::OptimizeJob, which the UI runs on a worker thread with a
progress bar). JPEG decoding comes from zune-jpeg (the decoder behind image and the
renderer), JPEG encoding and resampling from the image crate.
Target PDF version: partial compatibility
The target selects the header version, clears the catalog /Version, controls object
streams (disabled for PDF 1.4), and checks encryption requirements. It does not
detect or convert content newer than the target: optional content, JPEG 2000 images,
16-bit images, OpenType fonts and PDF 2.0 features can remain. A PDF 1.4 header
therefore does not guarantee compatibility with PDF 1.4 readers or validators.
optimize.compatibility-level remains partial until content compatibility is checked.