PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M11: Export ▸ Images reads CCITT parameters given in an array
Grzegorz Martens3204051c3b21
diff --git a/crates/create/src/extract.rs b/crates/create/src/extract.rsindex 16ecd5d8d..a32b42556 100644--- a/crates/create/src/extract.rs+++ b/crates/create/src/extract.rs@@ -1,7 +1,7 @@//! Export a PDF ▸ Image ▸ Export all images, and Edit ▸ Save image as: the images that pages//! use, as files. JPEG images are written unchanged; other images, and JPEG images with//! transparency or with CMYK samples a viewer would read inverted, are decoded and written as PNG with their soft mask, stencil mask or colour-//! key as alpha. Images PdfCraft can't decode yet (JPEG 2000, JBIG2, CCITT, separations) are+//! key as alpha. Images PdfCraft can't decode yet (JPEG 2000, JBIG2, separations) are//! reported, never silently left out.use std::collections::HashSet;@@ -110,6 +110,19 @@ fn decoded(doc: &Document, s: &Stream) -> Result<Vec<u8>, String> {}}+/// The `/DecodeParms` of a stream with one filter: a dictionary, or the one in an array (7.4.2).+fn decode_parms(doc: &Document, s: &Stream) -> Dict {+ let Some(p) = s.dict.get(b"DecodeParms").map(|p| doc.resolve(p)) else { return Dict::default() };+ let p = match &*p {+ Object::Array(a) => match a.first() {+ Some(first) => doc.resolve(first),+ None => return Dict::default(),+ },+ _ => p,+ };+ p.as_dict().cloned().unwrap_or_default()+}+/// [`decoded`] for a CCITT fax image, with its `/DecodeParms` (Table 11 defaults). `/Columns` has to/// be the image's width; pixels past it and rows past the image's height are dropped, so the output/// is bounded by the size the caller already checked. A stream that ends early gives fewer rows,@@ -151,7 +164,7 @@ fn ccitt(doc: &Document, s: &Stream) -> Result<Vec<u8>, String> {self.x = 0;}}- let p = s.dict.get(b"DecodeParms").map(|p| doc.resolve(p)).and_then(|p| p.as_dict().cloned()).unwrap_or_default();+ let p = decode_parms(doc, s);let flag = |key: &[u8], default: bool| match p.get(key).map(|v| doc.resolve(v)).as_deref() {Some(Object::Bool(b)) => *b,_ => default,diff --git a/crates/create/src/tests.rs b/crates/create/src/tests.rsindex 9716470ab..3f47d6d6d 100644--- a/crates/create/src/tests.rs+++ b/crates/create/src/tests.rs@@ -1517,8 +1517,8 @@ 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. One whose /Columns-/// aren't its width is skipped, not misread.+/// 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.#[test]fn ccitt_fax_images_export_as_their_picture() {use png::{BitDepth, ColorType};@@ -1558,6 +1558,15 @@ fn ccitt_fax_images_export_as_their_picture() {let out = extract_images(&doc, &[0], 0);assert_eq!(exported_white(&out.images[0].data), picture, "BlackIs1");+ let mut stream = with(false, i64::from(w), pdfcraft_filters::encode_g4(&bits, w, h).unwrap());+ let parms = stream.dict.get(b"DecodeParms").cloned().unwrap();+ stream.dict.set(b"DecodeParms".to_vec(), Object::Array(vec![parms]));+ stream.dict.set(b"Filter".to_vec(), Object::Array(vec![Object::name("CCITTFaxDecode")]));+ 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, "in arrays");+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);