PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M11: Export ▸ Images reads CCITT numbers given by reference, and decodes up to the image's height
Grzegorz Martens66b5ad2ab777
diff --git a/crates/create/src/extract.rs b/crates/create/src/extract.rsindex a32b42556..1053df2d7 100644--- a/crates/create/src/extract.rs+++ b/crates/create/src/extract.rs@@ -169,15 +169,17 @@ fn ccitt(doc: &Document, s: &Stream) -> Result<Vec<u8>, String> {Some(Object::Bool(b)) => *b,_ => default,};- let k = p.int(b"K").unwrap_or(0);+ let int = |key: &[u8]| p.get(key).and_then(|v| doc.resolve(v).as_int());+ let k = int(b"K").unwrap_or(0);// A row is the image's width: that was checked against the export's size limit, so a hostile// /Columns can't size the buffers.- let columns = u32::try_from(p.int(b"Columns").unwrap_or(1728)).ok().filter(|c| *c > 0).ok_or("the fax image has no columns")?;+ let columns = u32::try_from(int(b"Columns").unwrap_or(1728)).ok().filter(|c| *c > 0).ok_or("the fax image has no columns")?;if s.dict.int(b"Width") != Some(i64::from(columns)) {return Err("the fax image's columns aren't its width".into());}- let height = s.dict.int(b"Height").and_then(|h| u32::try_from(h).ok()).unwrap_or(0);- let rows = p.int(b"Rows").and_then(|r| u32::try_from(r).ok()).filter(|r| *r > 0).unwrap_or(height).min(height);+ // As many rows as the image has, whatever /Rows says: one below /Height would cut the picture+ // short, and rows past /Height aren't drawn.+ let rows = s.dict.int(b"Height").and_then(|h| u32::try_from(h).ok()).unwrap_or(0);let settings = hayro_ccitt::DecodeSettings {columns,rows,diff --git a/crates/create/src/tests.rs b/crates/create/src/tests.rsindex 3f47d6d6d..8ad8a54cd 100644--- a/crates/create/src/tests.rs+++ b/crates/create/src/tests.rs@@ -1517,8 +1517,9 @@ fn exported_white(png_data: &[u8]) -> Vec<bool> {}/// Export ▸ Images reads CCITT fax images: a page created from a 1-bit PNG (Group 4 now) exports as-/// the picture, as does the picture coded the other way round with /BlackIs1, and the picture with-/// its filter and parameters in arrays. One whose /Columns aren't its width is skipped, not misread.+/// the picture, as does the picture coded the other way round with /BlackIs1, the picture with its+/// filter and parameters in arrays, with /K and /Columns given by reference, and with a /Rows below+/// its height. One whose /Columns aren't its width is skipped, not misread.#[test]fn ccitt_fax_images_export_as_their_picture() {use png::{BitDepth, ColorType};@@ -1567,6 +1568,17 @@ fn ccitt_fax_images_export_as_their_picture() {assert!(out.skipped.is_empty(), "{:?}", out.skipped);assert_eq!(exported_white(&out.images[0].data), picture, "in arrays");+ let mut stream = with(false, i64::from(w), pdfcraft_filters::encode_g4(&bits, w, h).unwrap());+ let mut parms = stream.dict.get(b"DecodeParms").and_then(Object::as_dict).cloned().unwrap();+ parms.set(b"K".to_vec(), Object::Ref(doc.add(Object::Int(-1))));+ parms.set(b"Columns".to_vec(), Object::Ref(doc.add(Object::Int(i64::from(w)))));+ parms.set(b"Rows".to_vec(), Object::Int(1));+ stream.dict.set(b"DecodeParms".to_vec(), Object::Dict(parms));+ doc.set(r, Object::Stream(stream));+ let out = extract_images(&doc, &[0], 0);+ assert!(out.skipped.is_empty(), "{:?}", out.skipped);+ assert_eq!(exported_white(&out.images[0].data), picture, "/K and /Columns by reference, /Rows 1");+let stream = with(false, i64::from(w) + 8, pdfcraft_filters::encode_g4(&bits, w, h).unwrap());doc.set(r, Object::Stream(stream));let out = extract_images(&doc, &[0], 0);diff --git a/crates/optimize/src/images.rs b/crates/optimize/src/images.rsindex 33e60256e..7367f6d0d 100644--- a/crates/optimize/src/images.rs+++ b/crates/optimize/src/images.rs@@ -608,7 +608,8 @@ fn mono(doc: &Document, s: &Stream, compression: MonoCompression) -> Option<Stre/// `compression` asks (see [`mono`]), if that is smaller as stored. Lossless: a decode array maps/// 0 and 255 at eight bits as it maps 0 and 1 at one bit, so it stays. Left alone: indexed images/// (their samples are palette indices), a colour-key `/Mask` (its ranges are in eight-bit values),-/// and everything when `compression` is Retain.+/// and everything when `compression` is Retain: such an image is then a grey image like any other,+/// which the grey settings handle as they did before.fn bilevel_grey(doc: &Document, s: &Stream, compression: MonoCompression) -> Option<Stream> {if compression == MonoCompression::Retain {return None;