diff --git a/Cargo.lock b/Cargo.lock index 34c522b951..2ad337ab20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,7 +238,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.52.0", + "windows-sys 0.61.2", "wl-clipboard-rs", "x11rb", ] @@ -532,6 +532,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb2907cc03d08cdb1c6724ad0670ce67c4a71e1c45a6d7635224011714cfc4b1" + [[package]] name = "bitflags" version = "1.3.2" @@ -1239,6 +1245,36 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +dependencies = [ + "craft-jpeg", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder", + "png 0.18.1", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-keymap" version = "0.1.0" @@ -1262,6 +1298,17 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-opc" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +dependencies = [ + "craft-xml", + "craft-zip", + "log", + "thiserror 2.0.21", +] + [[package]] name = "craft-pdf" version = "0.1.0" @@ -1334,6 +1381,15 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-tiff" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +dependencies = [ + "serde", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" @@ -1410,6 +1466,15 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=e2dc681a3#e2dc681a3ad8a47038da2f11ddd2c5e1c0454991" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "craft-zip" version = "0.1.0" @@ -2130,7 +2195,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2168,6 +2233,22 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exr" +version = "1.74.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb29d0be702adf92c645d9b2f93472906ba83d400e59f98adb3770ce5c18c2f6" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.9", + "multiversion", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "fast-float2" version = "0.2.4" @@ -3272,11 +3353,22 @@ dependencies = [ "moxcms", "num-traits", "png 0.18.1", + "qoi", "tiff", "zune-core", "zune-jpeg", ] +[[package]] +name = "image-webp" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525e9ff3e1a4be2fbea1fdf0e98686a6d98b4d8f937e1bf7402245af1909e8c3" +dependencies = [ + "byteorder-lite", + "quick-error 2.0.1", +] + [[package]] name = "imagesize" version = "0.13.0" @@ -3398,6 +3490,12 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-encoder" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b454d911ac55068f53495488d8ccd0646eaa540c033a28ee15b07838afafb01f" + [[package]] name = "js-sys" version = "0.3.106" @@ -3479,6 +3577,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "lebe" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" + [[package]] name = "libc" version = "0.2.189" @@ -3703,6 +3807,28 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "multiversion" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb8a68be21d0755f83bfe47e79b896cc732687cf1903f0e50585e8d6ecc53076" +dependencies = [ + "multiversion-macros", + "target-features", +] + +[[package]] +name = "multiversion-macros" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a05e0c3c3e4edaa21d87c645852ed0b0c3663752bcc6ec43353bd6189dce35e1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "target-features", +] + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -4261,7 +4387,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4415,9 +4541,9 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-cms", + "craft-image-codecs", "craft-prc", "flate2", - "image", "pdfcraft-cos", "pdfcraft-engine", "pdfcraft-fonts", @@ -4427,7 +4553,6 @@ dependencies = [ "pdfcraft-prc-render", "pdfcraft-render", "pdfcraft-xfa", - "png 0.18.1", "rustix 1.1.6", "serde_json", "sha2 0.11.1", @@ -4439,6 +4564,7 @@ name = "pdfcraft-cli" version = "0.21.1" dependencies = [ "base64 0.23.1", + "craft-image-codecs", "pdfcraft-audit", "pdfcraft-automation", "pdfcraft-cos", @@ -4446,7 +4572,6 @@ dependencies = [ "pdfcraft-platform", "pdfcraft-preflight", "pdfcraft-render", - "png 0.18.1", "rustix 1.1.6", "serde_json", "winresource", @@ -4482,6 +4607,9 @@ dependencies = [ name = "pdfcraft-create" version = "0.21.1" dependencies = [ + "craft-image-codecs", + "craft-jpeg", + "craft-tiff", "encoding_rs", "hayro-ccitt", "hayro-syntax", @@ -4491,11 +4619,8 @@ dependencies = [ "pdfcraft-fonts", "pdfcraft-model", "pdfcraft-xfa", - "png 0.18.1", "rustix 1.1.6", "thiserror 2.0.21", - "tiff", - "zune-jpeg", ] [[package]] @@ -4532,6 +4657,8 @@ dependencies = [ "chrono", "craft-cms", "craft-docpath", + "craft-image-codecs", + "craft-tiff", "getrandom 0.4.3", "icu_properties", "idna", @@ -4563,9 +4690,7 @@ dependencies = [ "pdfcraft-testkit", "pdfcraft-xfa", "pdfcraft-xfdf", - "png 0.18.1", "thiserror 2.0.21", - "tiff", "unicode-segmentation", "url", ] @@ -4574,7 +4699,9 @@ dependencies = [ name = "pdfcraft-export" version = "0.21.1" dependencies = [ + "craft-opc", "craft-rtf", + "craft-xml", "craft-zip", "flate2", ] @@ -4647,9 +4774,9 @@ dependencies = [ name = "pdfcraft-ocr" version = "0.21.1" dependencies = [ + "craft-image-codecs", "ocrs", "pdfcraft-fonts", - "png 0.18.1", "rten", "rten-imageproc", "serde_json", @@ -4672,6 +4799,8 @@ dependencies = [ name = "pdfcraft-optimize" version = "0.21.1" dependencies = [ + "craft-image-codecs", + "craft-jpeg", "image", "pdfcraft-annot", "pdfcraft-content", @@ -4682,7 +4811,6 @@ dependencies = [ "proptest", "thiserror 2.0.21", "thread-priority", - "zune-jpeg", ] [[package]] @@ -4724,11 +4852,11 @@ dependencies = [ name = "pdfcraft-preflight" version = "0.21.1" dependencies = [ + "craft-xml", "flate2", "pdfcraft-content", "pdfcraft-cos", "pdfcraft-model", - "roxmltree 0.21.2", ] [[package]] @@ -4752,6 +4880,7 @@ name = "pdfcraft-redact" version = "0.21.1" dependencies = [ "aho-corasick", + "craft-xml", "pdfcraft-annot", "pdfcraft-content", "pdfcraft-cos", @@ -4765,7 +4894,6 @@ dependencies = [ "pdfcraft-testkit", "pdfcraft-xfa", "regex", - "roxmltree 0.21.2", "sha2 0.11.1", "thiserror 2.0.21", "unicode-normalization", @@ -4848,6 +4976,7 @@ dependencies = [ "bytemuck", "chrono", "craft-i18n", + "craft-image-codecs", "craft-keymap", "craft-menu", "craft-quit", @@ -4877,7 +5006,6 @@ dependencies = [ "pdfcraft-render", "pdfcraft-xfa", "plist", - "png 0.18.1", "pollster 1.0.1", "proptest", "rfd", @@ -4917,11 +5045,10 @@ name = "pdfcraft-xfa" version = "0.21.1" dependencies = [ "base64 0.23.1", - "image", + "craft-image-codecs", + "craft-xml", "pdfcraft-cos", "pdfcraft-fonts", - "png 0.18.1", - "roxmltree 0.21.2", "sha2 0.11.1", "thiserror 2.0.21", ] @@ -4932,12 +5059,12 @@ version = "0.21.1" dependencies = [ "base64 0.23.1", "craft-csv", + "craft-xml", "pdfcraft-annot", "pdfcraft-cos", "pdfcraft-forms", "pdfcraft-model", "proptest", - "roxmltree 0.21.2", "thiserror 2.0.21", ] @@ -5392,6 +5519,15 @@ version = "0.1.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea" +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -5476,7 +5612,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -6036,7 +6172,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -6106,7 +6242,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -6986,6 +7122,12 @@ dependencies = [ "xattr", ] +[[package]] +name = "target-features" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1bbb9f3c5c463a01705937a24fdabc5047929ac764b2d5b9cf681c1f5041ed5" + [[package]] name = "tempfile" version = "3.27.0" @@ -6993,10 +7135,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.6", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -8121,7 +8263,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -8631,10 +8773,10 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", + "craft-image-codecs", "lopdf", "pdfcraft-cos", "pdfcraft-oracle", - "png 0.18.1", "serde", "serde_json", "sha2 0.11.1", @@ -8902,6 +9044,15 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.5.15" @@ -8911,6 +9062,15 @@ dependencies = [ "zune-core", ] +[[package]] +name = "zune-jpegxl" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cba11ecd07cc23351500e840ae03841b7e4997923ec8e4832ee3ae199ea0b6b4" +dependencies = [ + "zune-core", +] + [[package]] name = "zvariant" version = "5.15.0" diff --git a/Cargo.toml b/Cargo.toml index 697c5d3dd9..33e770e9bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,14 @@ craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0. craft-pdf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3", features = ["cos", "crypt", "filters"] } craft-rtf = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } craft-csv = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-xml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-tiff = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-jpeg = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-docx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-pptx = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-drawingml = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } +craft-opc = { git = "https://github.com/storytold/craft-libs", version = "0.1.0", rev = "e2dc681a3" } # internal pdfcraft-geom = { path = "crates/geom", version = "0.21.0" } pdfcraft-filters = { path = "crates/filters", version = "0.21.0" } diff --git a/apps/pdfcraft-cli/Cargo.toml b/apps/pdfcraft-cli/Cargo.toml index 3d9d87a812..ad4db521d5 100644 --- a/apps/pdfcraft-cli/Cargo.toml +++ b/apps/pdfcraft-cli/Cargo.toml @@ -24,8 +24,8 @@ pdfcraft-platform = { workspace = true } base64 = "0.23" [dev-dependencies] -# Decodes `render` output in tests/it/owner_password.rs; already a dependency of the engine. -png = "0.18" +# Decodes `render` output in tests/it/owner_password.rs. +craft-image-codecs = { workspace = true } [target.'cfg(unix)'.dependencies] # `geteuid` for `ui`'s control-file owner check: safe, so no `unsafe` libc call here. diff --git a/apps/pdfcraft-cli/tests/it/owner_password.rs b/apps/pdfcraft-cli/tests/it/owner_password.rs index 5a3e061728..dd64086a13 100644 --- a/apps/pdfcraft-cli/tests/it/owner_password.rs +++ b/apps/pdfcraft-cli/tests/it/owner_password.rs @@ -104,14 +104,12 @@ fn text_accepts_the_owner_password() { /// Asserts that `data` is a PNG of the fixture's 200×300 pt page at `dpi` with the text drawn: /// a blank page of the right size fails too. fn assert_rendered_page(data: &[u8], dpi: u32, what: &str) { - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap_or_else(|e| panic!("{what}: {e}")); - let mut buf = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut buf).unwrap(); - assert_eq!((frame.width, frame.height), (200 * dpi / 72, 300 * dpi / 72), "{what}"); + let image = craft_image_codecs::decode(data).unwrap_or_else(|e| panic!("{what}: {e}")); + assert_eq!((image.width(), image.height()), (200 * dpi / 72, 300 * dpi / 72), "{what}"); // A rendered page is opaque, so `render` writes it as 8-bit RGB (`pdfcraft_engine::export::encode_png`). - assert_eq!((frame.color_type, frame.bit_depth), (png::ColorType::Rgb, png::BitDepth::Eight), "{what}"); + assert_eq!((image.layout(), image.sample_type()), (craft_image_codecs::ChannelLayout::Rgb, craft_image_codecs::SampleType::U8), "{what}"); // Pixels with every colour channel below half intensity: ink, not white paper. - let inked = buf[..frame.buffer_size()].as_chunks::<3>().0.iter().filter(|px| px.iter().all(|c| *c < 128)).count(); + let inked = image.data().as_chunks::<3>().0.iter().filter(|px| px.iter().all(|c| *c < 128)).count(); assert!(inked > 100, "{what}: only {inked} inked pixels, the page is blank"); } diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index 87966a392a..eec9b172d9 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -30,12 +30,11 @@ mcp = ["dep:base64"] [dev-dependencies] # Synthetic printer profiles for the app-managed print colour tests. craft-cms = { workspace = true } +craft-image-codecs = { workspace = true } pdfcraft-fonts = { workspace = true } -png = "0.18" flate2.workspace = true pdfcraft-model = { workspace = true } pdfcraft-xfa = { workspace = true } -image = { version = "0.25", default-features = false, features = ["jpeg"] } [target.'cfg(target_os = "macos")'.dev-dependencies] rustix = { version = "1", features = ["fs"] } diff --git a/crates/automation/tests/it/automation.rs b/crates/automation/tests/it/automation.rs index 5251bf96ce..82cf1b784b 100644 --- a/crates/automation/tests/it/automation.rs +++ b/crates/automation/tests/it/automation.rs @@ -7,6 +7,32 @@ use pdfcraft_automation::mcp::McpServer; use pdfcraft_automation::{Automation, Content, ToolError, tools}; use serde_json::{Value, json}; +fn encode_png(w: u32, h: u32, layout: craft_image_codecs::ChannelLayout, data: Vec) -> Vec { + let img = craft_image_codecs::Image::from_u8(w, h, layout, data).unwrap(); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() +} + +fn encode_jpeg_dpi(w: u32, h: u32, layout: craft_image_codecs::ChannelLayout, data: Vec, quality: u8, dpi: f32) -> Vec { + let mut img = craft_image_codecs::Image::from_u8(w, h, layout, data).unwrap(); + img.meta.dpi = Some((dpi, dpi)); + craft_image_codecs::encode( + &img, + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions { jpeg_quality: quality, ..Default::default() }, + ) + .unwrap() +} + +fn decode_rgba(data: &[u8]) -> (u32, u32, Vec) { + let img = craft_image_codecs::decode(data).unwrap(); + (img.width(), img.height(), img.to_rgba8()) +} + +fn rgba_at(w: u32, rgba: &[u8], x: u32, y: u32) -> [u8; 4] { + let at = ((y * w + x) * 4) as usize; + rgba[at..at + 4].try_into().unwrap() +} + /// A PDF with `n` 200×300 pt pages reading "Page 1", "Page 2", … fn fixture(n: usize) -> Vec { fixture_with(n, "") @@ -921,9 +947,8 @@ fn open_inspect_render_and_find() { let Content::Png { data, width, height } = &png[0] else { panic!("expected an image") }; assert_eq!((*width, *height), (200, 300)); assert_eq!(&data[..8], b"\x89PNG\r\n\x1a\n"); - let decoder = png::Decoder::new(std::io::Cursor::new(data.as_slice())); - let info = decoder.read_info().unwrap().info().clone(); - assert_eq!((info.width, info.height), (200, 300)); + let decoded = craft_image_codecs::decode(&data).unwrap(); + assert_eq!(decoded.dimensions(), (200, 300)); } #[test] @@ -961,10 +986,8 @@ fn text_editor_background_is_read_only_and_removes_source_glyphs() { let preview = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72, "hide_text_block": 1 })).unwrap(); assert_ne!(preview, before); let Content::Png { data, .. } = &preview[0] else { panic!("expected PNG") }; - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - assert!(pixels[..info.buffer_size()].iter().all(|b| *b == 255), "no source glyphs remain on the white page"); + let pixels = craft_image_codecs::decode(data).unwrap().into_data(); + assert!(pixels.iter().all(|b| *b == 255), "no source glyphs remain on the white page"); assert_eq!(a.call("page_render", &args).unwrap(), before); assert_eq!(ok(&mut a, "doc_info", json!({ "doc": doc })), state); assert_eq!(page_text(&mut a, doc), ["Page 1", "Page 2"]); @@ -1241,13 +1264,7 @@ fn combine_renames_colliding_field_names() { #[test] fn combine_takes_images_and_text_files() { let dir = workdir("combine-images"); - let mut png = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png, 4, 2); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); - encoder.write_header().unwrap().write_image_data(&[90; 24]).unwrap(); - } + let png = encode_png(4, 2, craft_image_codecs::ChannelLayout::Rgb, vec![90; 24]); std::fs::write(dir.join("scan.png"), png).unwrap(); std::fs::write(dir.join("notes.txt"), "Some notes").unwrap(); std::fs::write(dir.join("broken.png"), b"\x89PNG\r\n\x1a\nbroken").unwrap(); @@ -4913,12 +4930,17 @@ fn enhance_camera_image_through_tools() { fn enhance_scanned_file_cleans_the_scan_in_place_through_tools() { let dir = workdir("enhance-scan"); // A 300 × 400 grey scan: dull paper, a dark gutter at the left, text bars and a speck. - let img = image::GrayImage::from_fn(300, 400, |x, y| { - let light = 0.55 + 0.45 * (x as f32 / 60.0).min(1.0); - let ink = if ((60..360).contains(&y) && (y - 60) % 40 < 6 && (60..240).contains(&x)) || (x, y) == (150, 25) { 0.12 } else { 0.78 }; - image::Luma([(ink * light * 255.0) as u8]) - }); - img.save(dir.join("scan.jpg")).unwrap(); + let mut gray = Vec::with_capacity(300 * 400); + for y in 0..400u32 { + for x in 0..300u32 { + let light = 0.55 + 0.45 * (x as f32 / 60.0).min(1.0); + let ink = if ((60..360).contains(&y) && (y - 60) % 40 < 6 && (60..240).contains(&x)) || (x, y) == (150, 25) { 0.12 } else { 0.78 }; + gray.push((ink * light * 255.0) as u8); + } + } + let img = craft_image_codecs::Image::from_u8(300, 400, craft_image_codecs::ChannelLayout::Gray, gray).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 85, ..Default::default() }; + std::fs::write(dir.join("scan.jpg"), craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()).unwrap(); let mut a = auto(&dir); let scan = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["scan.jpg"] }))["doc"].as_u64().unwrap(); ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "before.jpg" })); @@ -4932,16 +4954,20 @@ fn enhance_scanned_file_cleans_the_scan_in_place_through_tools() { assert_eq!((images["count"].clone(), images["images"][0]["pixels"].clone()), (json!(1), json!([300, 400]))); let saved = ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "after" })); assert_eq!(saved["format"], "jpg"); - let (before, after) = (image::open(dir.join("before.jpg")).unwrap().to_luma8(), image::open(dir.join("after.jpg")).unwrap().to_luma8()); + let to_luma = |path: &Path| { + let img = craft_image_codecs::decode(&std::fs::read(path).unwrap()).unwrap(); + let w = img.width(); + let g = img.convert(craft_image_codecs::ChannelLayout::Gray, craft_image_codecs::SampleType::U8).into_data(); + (w, g) + }; + let (bw, before) = to_luma(&dir.join("before.jpg")); + let (aw, after) = to_luma(&dir.join("after.jpg")); for (x, y) in [(150, 40), (3, 40), (3, 380), (150, 25)] { - assert!( - before.get_pixel(x, y)[0] < 200 && after.get_pixel(x, y)[0] >= 230, - "({x}, {y}): {:?} → {:?}", - before.get_pixel(x, y), - after.get_pixel(x, y) - ); + let bp = before[(y * bw + x) as usize]; + let ap = after[(y * aw + x) as usize]; + assert!(bp < 200 && ap >= 230, "({x}, {y}): {bp} → {ap}"); } - assert!(after.get_pixel(150, 62)[0] <= 60, "a stroke stays dark"); + assert!(after[(62 * aw + 150) as usize] <= 60, "a stroke stays dark"); // Undo puts the scan back as it was. ok(&mut a, "edit_undo", json!({ "doc": scan })); ok(&mut a, "image_save", json!({ "doc": scan, "page": 1, "image": 1, "path": "undone.jpg" })); @@ -4976,10 +5002,12 @@ fn enhance_camera_image_finds_the_edges_by_itself() { (b[0] - a[0]) * (y - a[1]) - (b[1] - a[1]) * (x - a[0]) >= 0.0 }) }; - let img = image::RgbImage::from_fn(600, 450, |x, y| { - if inside(x as f64 + 0.5, y as f64 + 0.5) { image::Rgb([240, 238, 232]) } else { image::Rgb([70, 60, 50]) } - }); - img.save(dir.join("photo.jpg")).unwrap(); + let rgb: Vec = (0..450) + .flat_map(|y| (0..600).flat_map(move |x| if inside(x as f64 + 0.5, y as f64 + 0.5) { [240, 238, 232] } else { [70, 60, 50] })) + .collect(); + let img = craft_image_codecs::Image::from_u8(600, 450, craft_image_codecs::ChannelLayout::Rgb, rgb).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 85, ..Default::default() }; + std::fs::write(dir.join("photo.jpg"), craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap()).unwrap(); let mut a = auto(&dir); let photo = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap(); let r = ok(&mut a, "page_enhance_camera", json!({ "doc": photo, "page": 1 })); @@ -5238,16 +5266,10 @@ fn fill_sign_text_border_is_saved_and_validated() { #[test] fn image_signatures_through_tools_preserve_transparency_and_survive_save() { let dir = workdir("image-signatures"); - let mut png = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png, 120, 40); - encoder.set_color(png::ColorType::Rgba); - encoder.set_depth(png::BitDepth::Eight); - let rgba: Vec = (0..40) - .flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [0, 0, 0, 255] } else { [0, 0, 0, 0] })) - .collect(); - encoder.write_header().unwrap().write_image_data(&rgba).unwrap(); - } + let rgba: Vec = (0..40) + .flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [0, 0, 0, 255] } else { [0, 0, 0, 0] })) + .collect(); + let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba); std::fs::write(dir.join("signature.png"), png).unwrap(); std::fs::write(dir.join("broken.png"), b"broken").unwrap(); let mut a = auto(&dir); @@ -5258,11 +5280,11 @@ fn image_signatures_through_tools_preserve_transparency_and_survive_save() { let render = |a: &mut Automation, doc| { let output = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap(); let Content::Png { data, .. } = &output[0] else { panic!() }; - image::load_from_memory(data).unwrap().to_rgba8() + decode_rgba(data) }; - let before = render(&mut a, doc); - assert_eq!(before.get_pixel(25, 60).0, [255, 255, 255, 255], "transparent margin exposes the page"); - assert_eq!(before.get_pixel(65, 60).0, [0, 0, 0, 255], "signature ink is embedded"); + let (bw, _, before) = render(&mut a, doc); + assert_eq!(rgba_at(bw, &before, 25, 60), [255, 255, 255, 255], "transparent margin exposes the page"); + assert_eq!(rgba_at(bw, &before, 65, 60), [0, 0, 0, 255], "signature ink is embedded"); assert_eq!(ok(&mut a, "edit_undo", json!({ "doc": doc }))["undone"], "Add initials"); assert_eq!(ok(&mut a, "comment_list", json!({ "doc": doc }))["count"], 1); ok(&mut a, "edit_redo", json!({ "doc": doc })); @@ -5273,19 +5295,19 @@ fn image_signatures_through_tools_preserve_transparency_and_survive_save() { ok(&mut a, "comment_lock", json!({ "doc": doc, "page": 1, "index": 1 })); assert!(a.call("comment_edit", &json!({ "doc": doc, "page": 1, "index": 1, "rect": [20, 40, 164, 88] })).is_err()); ok(&mut a, "comment_lock", json!({ "doc": doc, "page": 1, "index": 1, "locked": false })); - assert_eq!(render(&mut a, doc), before, "invalid and locked resizes leave the image intact"); + assert_eq!(render(&mut a, doc).2, before, "invalid and locked resizes leave the image intact"); ok(&mut a, "comment_edit", json!({ "doc": doc, "page": 1, "index": 1, "rect": [20, 40, 164, 88] })); - let resized = render(&mut a, doc); - assert_eq!(resized.get_pixel(110, 64).0, [0, 0, 0, 255], "resizing scales the original ink"); - assert_eq!(resized.get_pixel(25, 64).0, [255, 255, 255, 255], "resizing retains alpha"); + let (rw, _, resized) = render(&mut a, doc); + assert_eq!(rgba_at(rw, &resized, 110, 64), [0, 0, 0, 255], "resizing scales the original ink"); + assert_eq!(rgba_at(rw, &resized, 25, 64), [255, 255, 255, 255], "resizing retains alpha"); assert_eq!(ok(&mut a, "edit_undo", json!({ "doc": doc }))["undone"], "Resize comment"); - assert_eq!(render(&mut a, doc), before); + assert_eq!(render(&mut a, doc).2, before); ok(&mut a, "edit_redo", json!({ "doc": doc })); - assert_eq!(render(&mut a, doc), resized); + assert_eq!(render(&mut a, doc).2, resized); ok(&mut a, "doc_save", json!({ "doc": doc, "path": "signed.pdf" })); let reopened = ok(&mut a, "doc_open", json!({ "path": "signed.pdf" }))["doc"].as_u64().unwrap(); assert_eq!(ok(&mut a, "comment_list", json!({ "doc": reopened }))["count"], 2); - assert_eq!(render(&mut a, reopened), resized, "resized appearances retain alpha and geometry after save"); + assert_eq!(render(&mut a, reopened).2, resized, "resized appearances retain alpha and geometry after save"); for args in [ json!({ "type": "signature", "path": "broken.png" }), json!({ "type": "signature", "path": "signature.png", "text": "Ada" }), @@ -5306,10 +5328,6 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() { let dir = workdir("scanned-signatures"); // A grey, unevenly lit sheet with a pen stroke for the signature, and a blank one. let photo = |ink: bool| { - let mut png = Vec::new(); - let mut encoder = png::Encoder::new(&mut png, 400, 300); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); let rgb: Vec = (0..300_u32) .flat_map(|y| { (0..400_u32).flat_map(move |x| { @@ -5318,8 +5336,7 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() { }) }) .collect(); - encoder.write_header().unwrap().write_image_data(&rgb).unwrap(); - png + encode_png(400, 300, craft_image_codecs::ChannelLayout::Rgb, rgb) }; std::fs::write(dir.join("photo.png"), photo(true)).unwrap(); std::fs::write(dir.join("blank.png"), photo(false)).unwrap(); @@ -5328,11 +5345,11 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() { ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "signature", "at": [20, 60], "path": "photo.png", "scan": true })); let output = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap(); let Content::Png { data, .. } = &output[0] else { panic!() }; - let page = image::load_from_memory(data).unwrap().to_rgba8(); - let dark = (20..170).filter(|&x| page.get_pixel(x, 60).0[0] < 80).count(); + let (pw, _, page) = decode_rgba(data); + let dark = (20..170).filter(|&x| rgba_at(pw, &page, x, 60)[0] < 80).count(); assert!(dark > 100, "the stroke is placed as ink across the signature width ({dark} px)"); for (x, y) in [(22, 50), (100, 50), (100, 70)] { - assert_eq!(page.get_pixel(x, y).0, [255, 255, 255, 255], "the grey paper is gone at {x},{y}"); + assert_eq!(rgba_at(pw, &page, x, y), [255, 255, 255, 255], "the grey paper is gone at {x},{y}"); } ok( &mut a, @@ -5366,16 +5383,10 @@ fn scanned_signatures_through_tools_drop_the_paper_and_report_bad_arguments() { #[test] fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() { let dir = workdir("signature-preview"); - let mut png = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png, 120, 40); - encoder.set_color(png::ColorType::Rgba); - encoder.set_depth(png::BitDepth::Eight); - let rgba: Vec = (0..40) - .flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [20, 40, 60, 128] } else { [0, 0, 0, 0] })) - .collect(); - encoder.write_header().unwrap().write_image_data(&rgba).unwrap(); - } + let rgba: Vec = (0..40) + .flat_map(|y| (0..120).flat_map(move |x| if (40..80).contains(&x) && (10..30).contains(&y) { [20, 40, 60, 128] } else { [0, 0, 0, 0] })) + .collect(); + let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba); std::fs::write(dir.join("signature.png"), png).unwrap(); std::fs::write(dir.join("form.pdf"), fixture(1)).unwrap(); let mut a = auto(&dir); @@ -5384,7 +5395,7 @@ fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() { ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "initials", "at": [20, 40], "path": "signature.png" })); let background = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap(); let Content::Png { data: expected, .. } = &background[0] else { panic!() }; - let expected = image::load_from_memory(expected).unwrap().to_rgba8(); + let expected = decode_rgba(expected).2; ok(&mut a, "fill_sign_add", json!({ "doc": doc, "page": 1, "type": "signature", "at": [120, 100], "path": "signature.png" })); ok(&mut a, "comment_edit", json!({ "doc": doc, "page": 1, "index": 2, "opacity": 0.5 })); ok(&mut a, "doc_protect", json!({ "doc": doc, "open_password": "preview-test" })); @@ -5405,10 +5416,10 @@ fn image_signature_preview_layers_are_read_only_and_survive_encrypted_save() { assert_eq!(meta["image_rotation"], 0, "placed upright as displayed on the turned page"); assert_eq!(meta["opacity"], 0.5); assert_eq!((*width, *height), (120, 40)); - assert_eq!(image::load_from_memory(background).unwrap().to_rgba8(), expected, "page text and the other signature remain"); - let signature = image::load_from_memory(signature).unwrap().to_rgba8(); - assert_eq!(signature.get_pixel(5, 20).0[3], 0); - assert_eq!(signature.get_pixel(60, 20).0, [20, 40, 60, 128], "embedded alpha and colour survive reopen"); + assert_eq!(decode_rgba(background).2, expected, "page text and the other signature remain"); + let (sw, _, signature) = decode_rgba(signature); + assert_eq!(rgba_at(sw, &signature, 5, 20)[3], 0); + assert_eq!(rgba_at(sw, &signature, 60, 20), [20, 40, 60, 128], "embedded alpha and colour survive reopen"); assert_eq!(ok(&mut a, "doc_info", json!({ "doc": doc })), before); let d = a.session().docs().iter().find(|d| d.id.0 == doc).unwrap(); assert_eq!(d.edit_generation(), generation); @@ -5468,13 +5479,7 @@ fn creating_from_text_refuses_what_the_font_cannot_draw() { #[test] fn creating_from_multiple_files_through_tools() { let dir = workdir("create-multiple"); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 4, 2); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - enc.write_header().unwrap().write_image_data(&[100; 4 * 2 * 3]).unwrap(); - } + let png = encode_png(4, 2, craft_image_codecs::ChannelLayout::Rgb, vec![100; 4 * 2 * 3]); std::fs::write(dir.join("scan.png"), png).unwrap(); std::fs::write(dir.join("notes.txt"), "hello").unwrap(); std::fs::write(dir.join("archive.zip"), b"PK\x03\x04").unwrap(); @@ -5535,14 +5540,9 @@ fn create_multiple_has_no_file_count_limit() { #[test] fn creating_images_with_dpi_through_tools() { let dir = workdir("image-dpi"); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 300, 150); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - enc.set_pixel_dims(Some(png::PixelDimensions { xppu: 11811, yppu: 5906, unit: png::Unit::Meter })); - enc.write_header().unwrap().write_image_data(&vec![100; 300 * 150 * 3]).unwrap(); - } + let mut img = craft_image_codecs::Image::from_u8(300, 150, craft_image_codecs::ChannelLayout::Rgb, vec![100; 300 * 150 * 3]).unwrap(); + img.meta.dpi = Some((300.0, 150.0)); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); std::fs::write(dir.join("scan.png"), png).unwrap(); let mut a = auto(&dir); for (dpi, width, height) in [(None, 72.0, 72.0), (Some(72.0), 300.0, 150.0), (Some(300.0), 72.0, 36.0)] { @@ -6383,13 +6383,7 @@ fn printer_papers_through_tools() { #[test] fn adding_content_through_tools() { let dir = workdir("content"); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 40, 20); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[200u8; 2400]).unwrap(); - } + let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![200u8; 2400]); std::fs::write(dir.join("logo.png"), png).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap(); @@ -6475,13 +6469,7 @@ fn added_text_takes_a_cmyk_or_gray_ink_through_tools() { } assert_eq!(ok(&mut a, "content_list", json!({ "doc": doc }))["count"], 2, "nothing refused was added"); // content_update refuses them too, an image takes no ink, and a refused update changes nothing. - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 40, 20); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[200u8; 2400]).unwrap(); - } + let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![200u8; 2400]); std::fs::write(dir.join("logo.png"), png).unwrap(); ok(&mut a, "page_add_image", json!({ "doc": doc, "page": 1, "path": "logo.png", "rect": [100, 200, 180, 240] })); let before = ok(&mut a, "content_list", json!({ "doc": doc }))["items"].clone(); @@ -6505,12 +6493,7 @@ fn added_text_takes_a_cmyk_or_gray_ink_through_tools() { #[test] fn copying_added_content_through_tools() { let dir = workdir("content-copy"); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 40, 20); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[90u8; 2400]).unwrap(); - } + let png = encode_png(40, 20, craft_image_codecs::ChannelLayout::Rgb, vec![90u8; 2400]); std::fs::write(dir.join("logo.png"), png).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap(); @@ -6622,10 +6605,7 @@ fn added_text_takes_character_spacing() { } fn added_rotation_image(dir: &Path) { - let mut png = Vec::new(); - let mut enc = png::Encoder::new(&mut png, 2, 1); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255, 0, 0, 0, 0, 255]).unwrap(); + let png = encode_png(2, 1, craft_image_codecs::ChannelLayout::Rgb, vec![255, 0, 0, 0, 0, 255]); std::fs::write(dir.join("rotation.png"), png).unwrap(); } @@ -6723,12 +6703,7 @@ fn large_added_image_rotations_round_trip_through_mcp() { #[test] fn text_styling_on_an_image_is_refused_without_an_edit() { let dir = workdir("content-style"); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 1, 1); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[80, 160, 200]).unwrap(); - } + let png = encode_png(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![80, 160, 200]); std::fs::write(dir.join("logo.png"), png).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap(); @@ -7131,20 +7106,13 @@ fn comments_and_form_data_travel_as_xfdf_fdf_and_text() { fn natural_image_stamps_through_tools_on_rotated_pages() { for degrees in [0, 90, 180, 270] { let dir = workdir(&format!("natural-stamps-{degrees}")); - let mut bytes = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut bytes, 80, 40); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); - let pixels: Vec = (0..40) - .flat_map(|y| { - (0..80).flat_map(move |x| { - [[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]][usize::from(y >= 20) * 2 + usize::from(x >= 40)] - }) - }) - .collect(); - encoder.write_header().unwrap().write_image_data(&pixels).unwrap(); - } + let pixels: Vec = (0..40) + .flat_map(|y| { + (0..80) + .flat_map(move |x| [[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]][usize::from(y >= 20) * 2 + usize::from(x >= 40)]) + }) + .collect(); + let bytes = encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgb, pixels); std::fs::write(dir.join("quadrants.png"), bytes).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_open", json!({"path":"a.pdf"}))["doc"].as_u64().unwrap(); @@ -7152,15 +7120,14 @@ fn natural_image_stamps_through_tools_on_rotated_pages() { ok(&mut a, "stamp_custom", json!({"doc":doc,"page":1,"path":"quadrants.png","at":[100,100]})); let rendered = a.call("page_render", &json!({"doc":doc,"page":1,"dpi":72})).unwrap(); let Content::Png { data, .. } = &rendered[0] else { panic!("expected PNG") }; - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); + let img = craft_image_codecs::decode(data).unwrap(); // A rendered page is opaque, so it comes as RGB. - assert_eq!(info.color_type, png::ColorType::Rgb); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb); + let (w, pixels) = (img.width(), img.data()); for ((x, y), colour) in [(80, 90), (120, 90), (80, 110), (120, 110)].into_iter().zip([[240, 20, 20], [20, 180, 20], [20, 20, 240], [230, 180, 20]]) { - let offset = ((y * info.width + x) * 3) as usize; + let offset = ((y * w + x) * 3) as usize; assert_eq!(&pixels[offset..offset + 3], &colour, "page rotation {degrees}"); } } @@ -7238,13 +7205,7 @@ fn custom_stamps_fit_a_pdf_page_inside_a_rect() { // On a quarter-turned page the picture is fitted as displayed: an 80 × 40 picture in a // 100 × 100 box is 100 × 50 on screen, centred. - let mut png_bytes = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png_bytes, 80, 40); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); - encoder.write_header().unwrap().write_image_data(&[200; 80 * 40 * 3]).unwrap(); - } + let png_bytes = encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgb, vec![200; 80 * 40 * 3]); std::fs::write(dir.join("wide.png"), png_bytes).unwrap(); let turned = ok(&mut a, "doc_open", json!({ "path": "a.pdf" }))["doc"].as_u64().unwrap(); ok(&mut a, "page_rotate", json!({ "doc": turned, "pages": [1], "degrees": 90 })); @@ -7761,13 +7722,17 @@ fn note_icons_anchor_at_the_requested_corner_on_rotated_pages() { fn image_signatures_stay_upright_on_rotated_pages() { let dir = workdir("image-upright"); // 80 x 40 px, one colour per quadrant: red and green above, blue and yellow below. - let quadrants = image::RgbaImage::from_fn(80, 40, |x, y| match (x < 40, y < 20) { - (true, true) => image::Rgba([255, 0, 0, 255]), - (false, true) => image::Rgba([0, 255, 0, 255]), - (true, false) => image::Rgba([0, 0, 255, 255]), - (false, false) => image::Rgba([255, 255, 0, 255]), - }); - quadrants.save(dir.join("quadrants.png")).unwrap(); + let quadrants: Vec = (0..40) + .flat_map(|y| { + (0..80).flat_map(move |x| match (x < 40, y < 20) { + (true, true) => [255, 0, 0, 255], + (false, true) => [0, 255, 0, 255], + (true, false) => [0, 0, 255, 255], + (false, false) => [255, 255, 0, 255], + }) + }) + .collect(); + std::fs::write(dir.join("quadrants.png"), encode_png(80, 40, craft_image_codecs::ChannelLayout::Rgba, quadrants)).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_create", json!({ "from": "blank", "width": 200, "height": 300, "pages": 4 }))["doc"].as_u64().unwrap(); for (page, degrees) in [(2, 90), (3, 180), (4, 270)] { @@ -7782,7 +7747,7 @@ fn image_signatures_stay_upright_on_rotated_pages() { for page in 1..=4 { let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap(); let Content::Png { data, .. } = &output[0] else { panic!() }; - let pixels = image::load_from_memory(data).unwrap().to_rgba8(); + let (pw, _, pixels) = decode_rgba(data); for (x, y, want, what) in [ (76, 132, red, "signature top-left"), (108, 132, green, "signature top-right"), @@ -7793,7 +7758,7 @@ fn image_signatures_stay_upright_on_rotated_pages() { (92, 120, white, "above the signature"), (92, 160, white, "below the signature"), ] { - let got = pixels.get_pixel(x, y).0; + let got = rgba_at(pw, &pixels, x, y); assert!(got[..3].iter().zip(want).all(|(g, w)| g.abs_diff(w) < 12), "page {page}, {what} at ({x}, {y}): {got:?}, want {want:?}"); } } @@ -7823,14 +7788,16 @@ fn typed_signatures_stay_upright_on_rotated_pages() { for page in 1..=4 { let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap(); let Content::Png { data, .. } = &output[0] else { panic!() }; - let pixels = image::load_from_memory(data).unwrap().to_rgba8(); - assert_eq!(pixels.dimensions(), if page % 2 == 1 { (200, 300) } else { (300, 200) }, "page {page}"); - let dark: Vec = pixels.pixels().map(|p| p.0[..3].iter().all(|v| *v < 128)).collect(); + let (width, height, pixels) = decode_rgba(data); + assert_eq!((width, height), if page % 2 == 1 { (200, 300) } else { (300, 200) }, "page {page}"); + let dark: Vec = pixels.chunks_exact(4).map(|p| p[..3].iter().all(|v| *v < 128)).collect(); for y in [60, 140] { let mut b = [u32::MAX, u32::MAX, 0, 0]; - for (x, py, p) in pixels.enumerate_pixels() { - if py.abs_diff(y) < 40 && p.0[..3].iter().all(|v| *v < 128) { - b = [b[0].min(x), b[1].min(py), b[2].max(x), b[3].max(py)]; + for py in 0..height { + for x in 0..width { + if py.abs_diff(y) < 40 && rgba_at(width, &pixels, x, py)[..3].iter().all(|v| *v < 128) { + b = [b[0].min(x), b[1].min(py), b[2].max(x), b[3].max(py)]; + } } } assert!(b[0] <= b[2], "page {page}: no ink near y = {y}"); @@ -7842,7 +7809,6 @@ fn typed_signatures_stay_upright_on_rotated_pages() { continue; } // Same pixels as the unturned page where both pages are (pages 2 and 4 are wider). - let width = pixels.width(); let (mut differ, mut ink) = (0, 0); for y in 0..200 { for x in 0..200 { @@ -7880,14 +7846,15 @@ fn fill_sign_marks_stay_upright_on_rotated_pages() { // 144 dpi: two pixels a point. let output = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 144 })).unwrap(); let Content::Png { data, .. } = &output[0] else { panic!() }; - let pixels = image::load_from_memory(data).unwrap().to_rgba8(); - let dark: Vec = pixels.pixels().map(|p| p.0[..3].iter().all(|v| *v < 128)).collect(); - let width = pixels.width(); + let (width, height, pixels) = decode_rgba(data); + let dark: Vec = pixels.chunks_exact(4).map(|p| p[..3].iter().all(|v| *v < 128)).collect(); // The line, 36 × 4 pt displayed, runs across. let mut b = [u32::MAX, u32::MAX, 0, 0]; - for (x, y, p) in pixels.enumerate_pixels() { - if x.abs_diff(240) < 60 && y.abs_diff(120) < 60 && p.0[..3].iter().all(|v| *v < 128) { - b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)]; + for y in 0..height { + for x in 0..width { + if x.abs_diff(240) < 60 && y.abs_diff(120) < 60 && rgba_at(width, &pixels, x, y)[..3].iter().all(|v| *v < 128) { + b = [b[0].min(x), b[1].min(y), b[2].max(x), b[3].max(y)]; + } } } assert!(b[0] <= b[2], "page {page}: no line drawn"); @@ -8203,14 +8170,8 @@ fn lines_and_corners_are_held_level_or_plumb_through_the_tool() { fn signing_with_an_image_typed_or_drawn_graphic_through_tools() { let dir = workdir("signing-graphic"); // A solid red 120 x 40 picture. - let mut png = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png, 120, 40); - encoder.set_color(png::ColorType::Rgba); - encoder.set_depth(png::BitDepth::Eight); - let rgba: Vec = (0..120 * 40).flat_map(|_| [220, 0, 0, 255]).collect(); - encoder.write_header().unwrap().write_image_data(&rgba).unwrap(); - } + let rgba: Vec = (0..120 * 40).flat_map(|_| [220, 0, 0, 255]).collect(); + let png = encode_png(120, 40, craft_image_codecs::ChannelLayout::Rgba, rgba); std::fs::write(dir.join("logo.png"), png).unwrap(); std::fs::write(dir.join("broken.png"), b"broken").unwrap(); let mut a = auto(&dir); @@ -8256,11 +8217,11 @@ fn signing_with_an_image_typed_or_drawn_graphic_through_tools() { // details on the right. let png = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 72 })).unwrap(); let Content::Png { data, width, .. } = &png[0] else { panic!("expected an image") }; - let mut reader = png::Decoder::new(std::io::Cursor::new(data.as_slice())).read_info().unwrap(); - let mut buf = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut buf).unwrap(); - let channels = frame.line_size / *width as usize; - let px = |x: usize, y: usize| buf[y * frame.line_size + x * channels..][..3].to_vec(); + let (_, _, buf) = decode_rgba(data); + let px = |x: usize, y: usize| { + let at = (y * *width as usize + x) * 4; + buf[at..at + 3].to_vec() + }; assert_eq!(px(60, 225), [220, 0, 0], "the image fills the left half's middle"); assert_ne!(px(150, 225), [220, 0, 0], "and not the details"); let inked = |y0: usize, y1: usize| (y0..y1).any(|y| (23..97).any(|x| px(x, y).iter().all(|c| *c < 100))); @@ -8427,8 +8388,8 @@ fn named_layer_verdicts_are_reported_and_rendered_through_tools() { assert_eq!(list["signatures"][0]["verdict_shown"], true); let rendered = a.call("page_render", &json!({"doc": doc, "page": 1, "dpi": 72})).unwrap(); let Content::Png { data, .. } = &rendered[0] else { panic!("expected a PNG") }; - let pixels = image::load_from_memory_with_format(data, image::ImageFormat::Png).unwrap().to_rgba8(); - let red = pixels.pixels().filter(|p| p.0.iter().zip([0xD7, 0x37, 0x3F]).all(|(a, b)| a.abs_diff(b) < 30)).count(); + let (_, _, pixels) = decode_rgba(data); + let red = pixels.chunks_exact(4).filter(|p| p[..3].iter().zip([0xD7u8, 0x37, 0x3F]).all(|(a, b)| a.abs_diff(b) < 30)).count(); assert!(red > 100, "the invalid state is drawn: {red} red pixels"); ok(&mut a, "doc_save", json!({"doc": doc, "path": "saved.pdf", "full": false})); assert_eq!(std::fs::read(dir.join("saved.pdf")).unwrap(), original, "the drawn state never enters saves"); @@ -8647,10 +8608,7 @@ fn optimizing_through_tools() { [(x * 255 / w) as u8 ^ ((x * y) & 15) as u8, (y * 255 / h) as u8, ((x + y) & 255) as u8] }) .collect(); - let mut jpeg = Vec::new(); - let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95); - enc.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(600)); - enc.encode(&px, w, h, image::ExtendedColorType::Rgb8).unwrap(); + let jpeg = encode_jpeg_dpi(w, h, craft_image_codecs::ChannelLayout::Rgb, px, 95, 600.0); std::fs::write(dir.join("photo.jpg"), &jpeg).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap(); @@ -8913,13 +8871,39 @@ fn a_one_bit_scan_stays_one_bit_and_exports() { } } } - let mut png_bytes = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png_bytes, w, h); - enc.set_color(png::ColorType::Grayscale); - enc.set_depth(png::BitDepth::One); - enc.write_header().unwrap().write_image_data(&bits).unwrap(); - } + let png_bytes = { + use std::io::Write as _; + let mut raw = Vec::with_capacity(h as usize * (1 + stride)); + for row in bits.chunks_exact(stride) { + raw.push(0); + raw.extend_from_slice(row); + } + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + z.write_all(&raw).unwrap(); + let idat = z.finish().unwrap(); + let chunk = |out: &mut Vec, tag: &[u8; 4], data: &[u8]| { + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(tag); + out.extend_from_slice(data); + let mut crc = 0xFFFF_FFFFu32; + for &b in tag.iter().chain(data.iter()) { + crc ^= u32::from(b); + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; + } + } + out.extend_from_slice(&(!crc).to_be_bytes()); + }; + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut ihdr = Vec::with_capacity(13); + ihdr.extend_from_slice(&w.to_be_bytes()); + ihdr.extend_from_slice(&h.to_be_bytes()); + ihdr.extend_from_slice(&[1, 0, 0, 0, 0]); + chunk(&mut out, b"IHDR", &ihdr); + chunk(&mut out, b"IDAT", &idat); + chunk(&mut out, b"IEND", &[]); + out + }; std::fs::write(dir.join("scan.png"), &png_bytes).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["scan.png"] }))["doc"].as_u64().unwrap(); @@ -8938,10 +8922,8 @@ fn a_one_bit_scan_stays_one_bit_and_exports() { let files = r["files"].as_array().unwrap(); assert_eq!(files.len(), 1, "{r}"); let exported = std::fs::read(files[0]["path"].as_str().unwrap()).unwrap(); - let mut reader = png::Decoder::new(std::io::Cursor::new(exported)).read_info().unwrap(); - let mut buf = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut buf).unwrap(); - let white: Vec = buf[..frame.buffer_size()].as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect(); + let (_, _, rgba) = decode_rgba(&exported); + let white: Vec = rgba.chunks_exact(4).map(|p| p[..3] == [255, 255, 255]).collect(); let expected: Vec = (0..h as usize).flat_map(|y| (0..w as usize).map(move |x| (y, x))).map(|(y, x)| bits[y * stride + x / 8] & (0x80 >> (x % 8)) != 0).collect(); assert_eq!(white, expected); @@ -9254,10 +9236,7 @@ fn optimizing_to_a_target_size() { [(x * 255 / w) as u8 ^ ((x * y) & 15) as u8, (y * 255 / h) as u8, ((x + y) & 255) as u8] }) .collect(); - let mut jpeg = Vec::new(); - let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95); - enc.set_pixel_density(image::codecs::jpeg::PixelDensity::dpi(600)); - enc.encode(&px, w, h, image::ExtendedColorType::Rgb8).unwrap(); + let jpeg = encode_jpeg_dpi(w, h, craft_image_codecs::ChannelLayout::Rgb, px, 95, 600.0); std::fs::write(dir.join("photo.jpg"), &jpeg).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.jpg"] }))["doc"].as_u64().unwrap(); @@ -10581,10 +10560,7 @@ fn dynamic_xfa_forms_open_render_fill_and_save_through_tools() { // The page renders with the widgets' own appearances: the check box's border is drawn. let png = a.call("page_render", &json!({ "doc": doc, "page": 1, "dpi": 36 })).unwrap(); let Content::Png { data, .. } = &png[0] else { panic!("expected an image") }; - let decoder = png::Decoder::new(std::io::Cursor::new(data.as_slice())); - let mut reader = decoder.read_info().unwrap(); - let mut buf = vec![0; reader.output_buffer_size().unwrap()]; - reader.next_frame(&mut buf).unwrap(); + let buf = craft_image_codecs::decode(data).unwrap().into_data(); assert!(buf.iter().filter(|b| **b < 128).count() > 200, "the page is not blank"); ok(&mut a, "form_fill", json!({ "doc": doc, "values": { "familyName": "Singh", "agree": true, "answer": "N", "born": "2001-02-03" } })); ok(&mut a, "doc_save", json!({ "doc": doc, "path": "out.pdf" })); @@ -11484,7 +11460,7 @@ fn conventions_core_tools_preserve_root_and_batch_errors() { assert_eq!(r["result"]["isError"], false, "{r}"); use base64::Engine as _; let png = base64::engine::general_purpose::STANDARD.decode(r["result"]["content"][0]["data"].as_str().unwrap()).unwrap(); - let image = image::load_from_memory(&png).unwrap(); + let image = craft_image_codecs::decode(&png).unwrap(); assert!(image.width().max(image.height()) <= 64); assert_eq!(before["result"], run(&mut s, "doc_info", json!({"doc":doc}))["result"]); } @@ -11790,13 +11766,7 @@ fn a_deleted_image_is_not_kept_by_reduce_or_optimize() { let dir = workdir("deleted-image"); let (w, h) = (600u32, 400u32); let px: Vec = (0..w * h).flat_map(|i| [(i % w * 255 / w) as u8 ^ (i & 7) as u8, (i / w * 255 / h) as u8, 128]).collect(); - let mut photo = Vec::new(); - { - let mut enc = png::Encoder::new(&mut photo, w, h); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - enc.write_header().unwrap().write_image_data(&px).unwrap(); - } + let photo = encode_png(w, h, craft_image_codecs::ChannelLayout::Rgb, px); std::fs::write(dir.join("photo.png"), photo).unwrap(); let mut a = auto(&dir); let doc = ok(&mut a, "doc_create", json!({ "from": "images", "paths": ["photo.png"] }))["doc"].as_u64().unwrap(); @@ -12166,15 +12136,9 @@ fn reading_colors_workdir() -> PathBuf { fn png_pixels(content: &Content) -> Vec { let Content::Png { data, width, height } = content else { panic!("expected a PNG") }; assert_eq!((*width, *height), (200, 300)); - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - assert_eq!((info.width, info.height), (200, 300)); - match info.color_type { - png::ColorType::Rgba => pixels, - png::ColorType::Rgb => pixels.as_chunks::<3>().0.iter().flat_map(|p| [p[0], p[1], p[2], 255]).collect(), - other => panic!("unexpected colour type {other:?}"), - } + let (w, h, rgba) = decode_rgba(data); + assert_eq!((w, h), (200, 300)); + rgba } /// The RGBA pixel at (`x`, `y`), top-left origin, of a 200-wide image. @@ -14277,26 +14241,13 @@ fn image_flip_tools_follow_displayed_axes_on_every_page_rotation() { fn raster(a: &mut Automation, id: u64) -> (usize, usize, Vec) { let rendered = a.call("page_render", &json!({"doc":id,"page":1,"dpi":72})).unwrap(); let Content::Png { data, .. } = &rendered[0] else { panic!("expected PNG") }; - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut bytes = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut bytes).unwrap(); - bytes.truncate(frame.buffer_size()); - if frame.color_type == png::ColorType::Rgb { - bytes = bytes.as_chunks::<3>().0.iter().flat_map(|&[r, g, b]| [r, g, b, 255]).collect(); - } else { - assert_eq!(frame.color_type, png::ColorType::Rgba); - } - (frame.width as usize, frame.height as usize, bytes) + let (w, h, rgba) = decode_rgba(data); + (w as usize, h as usize, rgba) } let dir = workdir("displayed-image-flip"); - let mut png_bytes = Vec::new(); - { - let mut encoder = png::Encoder::new(&mut png_bytes, 8, 4); - encoder.set_color(png::ColorType::Rgb); - let colors = [[240, 20, 30], [20, 230, 40], [30, 40, 220], [220, 210, 20]]; - let pixels: Vec = (0..4).flat_map(|y| (0..8).flat_map(move |x| colors[usize::from(x >= 4) + 2 * usize::from(y >= 2)])).collect(); - encoder.write_header().unwrap().write_image_data(&pixels).unwrap(); - } + let colors = [[240, 20, 30], [20, 230, 40], [30, 40, 220], [220, 210, 20]]; + let pixels: Vec = (0..4).flat_map(|y| (0..8).flat_map(move |x| colors[usize::from(x >= 4) + 2 * usize::from(y >= 2)])).collect(); + let png_bytes = encode_png(8, 4, craft_image_codecs::ChannelLayout::Rgb, pixels); std::fs::write(dir.join("quadrants.png"), png_bytes).unwrap(); let mut a = auto(&dir); let mut failures = Vec::new(); diff --git a/crates/automation/tests/it/backdrop.rs b/crates/automation/tests/it/backdrop.rs index 00570b260c..b152820c39 100644 --- a/crates/automation/tests/it/backdrop.rs +++ b/crates/automation/tests/it/backdrop.rs @@ -66,12 +66,9 @@ fn ok(a: &mut Automation, tool: &str, args: Value) -> Value { fn blank_spot(a: &mut Automation, doc: u64, page: u64) -> [u8; 3] { let rendered = a.call("page_render", &json!({ "doc": doc, "page": page, "dpi": 72 })).unwrap(); let Content::Png { data, width, .. } = &rendered[0] else { panic!("expected a PNG") }; - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - let channels = info.color_type.samples(); - let at = (10 * *width as usize + 180) * channels; - [pixels[at], pixels[at + 1], pixels[at + 2]] + let rgba = craft_image_codecs::decode(data).unwrap().to_rgba8(); + let at = (10 * *width as usize + 180) * 4; + [rgba[at], rgba[at + 1], rgba[at + 2]] } const RED: [u8; 3] = [255, 0, 0]; diff --git a/crates/automation/tests/it/model_3d.rs b/crates/automation/tests/it/model_3d.rs index 179192f421..b1317e8804 100644 --- a/crates/automation/tests/it/model_3d.rs +++ b/crates/automation/tests/it/model_3d.rs @@ -13,12 +13,8 @@ fn tool(a: &mut Automation, name: &str, args: Value) -> Value { } fn png(w: u32, h: u32) -> Vec { - let mut out = Vec::new(); - let mut encoder = png::Encoder::new(&mut out, w, h); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); - encoder.write_header().unwrap().write_image_data(&vec![90; (w * h * 3) as usize]).unwrap(); - out + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, vec![90; (w * h * 3) as usize]).unwrap(); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() } fn stream(doc: &Document, obj: &Object) -> Stream { diff --git a/crates/automation/tests/it/model_render.rs b/crates/automation/tests/it/model_render.rs index 4323ca50e7..d289e382d0 100644 --- a/crates/automation/tests/it/model_render.rs +++ b/crates/automation/tests/it/model_render.rs @@ -61,12 +61,11 @@ fn open(a: &mut Automation, path: &str) -> u64 { /// The RGB of pixel (x, y) of a PNG. fn rgb(data: &[u8], x: usize, y: usize) -> [u8; 3] { - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - let channels = info.line_size / info.width as usize; - let at = y * info.line_size + x * channels; - [pixels[at], pixels[at + 1], pixels[at + 2]] + let img = craft_image_codecs::decode(data).unwrap(); + let w = img.width() as usize; + let rgba = img.to_rgba8(); + let at = (y * w + x) * 4; + [rgba[at], rgba[at + 1], rgba[at + 2]] } #[test] diff --git a/crates/automation/tests/it/optimize_content_boundaries.rs b/crates/automation/tests/it/optimize_content_boundaries.rs index c37da05eb4..fafd1d5238 100644 --- a/crates/automation/tests/it/optimize_content_boundaries.rs +++ b/crates/automation/tests/it/optimize_content_boundaries.rs @@ -79,12 +79,8 @@ fn check_page(a: &mut Automation, doc: u64, hidden: bool, phase: &str) { let rendered = a.call("page_render", &json!({"doc": doc, "page": 1, "dpi": 72})).unwrap(); let Content::Png { data, width, height } = &rendered[0] else { panic!("expected page PNG") }; assert_eq!((*width, *height), (600, 800)); - let mut reader = png::Decoder::new(std::io::Cursor::new(data)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - let channels = info.color_type.samples(); - assert!(channels >= 3); - let pixel = |x: usize, y: usize| &pixels[(y * 600 + x) * channels..(y * 600 + x) * channels + 3]; + let rgba = craft_image_codecs::decode(data).unwrap().to_rgba8(); + let pixel = |x: usize, y: usize| &rgba[(y * 600 + x) * 4..(y * 600 + x) * 4 + 3]; let dark = |left, top, right, bottom| { (top..bottom).flat_map(|y| (left..right).map(move |x| (x, y))).filter(|&(x, y)| pixel(x, y).iter().any(|v| *v < 128)).count() }; diff --git a/crates/create/Cargo.toml b/crates/create/Cargo.toml index 79c7a4f276..16c5f1cdf6 100644 --- a/crates/create/Cargo.toml +++ b/crates/create/Cargo.toml @@ -15,11 +15,11 @@ pdfcraft-model = { workspace = true } pdfcraft-fonts = { workspace = true } # For `image::bmp_with_pixel_offset`, shared with XFA pictures. pdfcraft-xfa = { workspace = true } -png = "0.18" -tiff = "0.11" -image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg"] } -# Already in the graph through image; read directly for CMYK JPEG samples, which image converts to RGB. -zune-jpeg = "0.5" +craft-image-codecs = { workspace = true } +craft-jpeg = { workspace = true } +craft-tiff = { workspace = true } +# Keep `image` (no codec features) only for `imageops::resize` of soft masks in `extract.rs`. +image = { version = "0.25", default-features = false } thiserror = { workspace = true } # Already in the graph through lopdf; used only to read plain-text files (decode_text). encoding_rs = "0.8" @@ -32,6 +32,5 @@ rustix = { version = "1", features = ["process"] } [dev-dependencies] # An independent CCITT decoder, to check Create PDF's Group 4 images. hayro-ccitt.workspace = true -image = { version = "0.25", default-features = false, features = ["bmp", "gif", "jpeg"] } hayro-syntax = { workspace = true } pdfcraft-filters = { workspace = true } diff --git a/crates/create/src/extract.rs b/crates/create/src/extract.rs index 46b203a071..25bdb01f3d 100644 --- a/crates/create/src/extract.rs +++ b/crates/create/src/extract.rs @@ -306,24 +306,50 @@ thread_local! { /// can't be read, when its metadata would take the decoder quadratic time /// ([`decodable_metadata`](crate::upright_image::decodable_metadata)). fn jpeg_size(data: &[u8]) -> Option<(usize, usize)> { - use zune_jpeg::zune_core::{bytestream::ZCursor, options::DecoderOptions}; if !crate::upright_image::decodable_metadata(data) { return None; } - let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX)); - let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options); - dec.decode_headers().ok()?; - dec.dimensions() + let mut i = 2usize; + if !data.starts_with(&[0xFF, 0xD8]) { + return None; + } + while let Some(&b) = data.get(i) { + if b != 0xFF { + i += 1; + continue; + } + let marker = *data.get(i + 1)?; + match marker { + 0xFF => i += 1, + 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, + 0xDA | 0xD9 => break, + 0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF => { + let h = usize::from(u16::from_be_bytes([*data.get(i + 5)?, *data.get(i + 6)?])); + let w = usize::from(u16::from_be_bytes([*data.get(i + 7)?, *data.get(i + 8)?])); + return Some((w, h)); + } + _ => { + let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?])); + if len < 2 { + return None; + } + i = i.checked_add(2 + len)?; + } + } + } + None } /// JPEG `data`, decoded, if its header says it is `w` × `h`. -fn jpeg_of_size(data: &[u8], w: usize, h: usize) -> Option { +fn jpeg_of_size(data: &[u8], w: usize, h: usize) -> Option { if jpeg_size(data)? != (w, h) { return None; } #[cfg(test)] JPEGS_DECODED.with(|n| n.set(n.get() + 1)); - image::load_from_memory_with_format(data, image::ImageFormat::Jpeg).ok() + let mut limits = craft_jpeg::Limits::default(); + limits.max_pixels = (u64::from(u16::MAX)) * (u64::from(u16::MAX)); + craft_jpeg::decode(data, &limits, None).ok() } /// A JPEG's samples (8 bits): grey for a grey image, CMYK as stored for a CMYK one, else RGB, @@ -343,17 +369,86 @@ fn jpeg_samples(data: &[u8], space: &Space, w: usize, h: usize) -> Option<(Vec (img.into_luma8().into_raw(), Space::Gray), - _ => (img.into_rgb8().into_raw(), Space::Rgb), + Space::Gray => { + let gray = match dec.components { + 1 => px, + 3 => px + .as_chunks::<3>() + .0 + .iter() + .map(|&[r, g, b]| ((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8) + .collect(), + 4 => px + .as_chunks::<4>() + .0 + .iter() + .map(|&[c, m, y, k]| { + let r = 255 - c.saturating_add(k); + let g = 255 - m.saturating_add(k); + let b = 255 - y.saturating_add(k); + ((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8 + }) + .collect(), + _ => return None, + }; + (gray, Space::Gray) + } + _ => { + let rgb = match dec.components { + 3 => px, + 1 => px.into_iter().flat_map(|g| [g, g, g]).collect(), + 4 => px + .as_chunks::<4>() + .0 + .iter() + .flat_map(|&[c, m, y, k]| [255 - c.saturating_add(k), 255 - m.saturating_add(k), 255 - y.saturating_add(k)]) + .collect(), + _ => return None, + }; + (rgb, Space::Rgb) + } }) } /// A four-component JPEG's CMYK samples as the page reads them: as stored, not inverted the way /// a decoder converting to RGB inverts them, with YCCK converted to CMYK. +fn adobe_transform(data: &[u8]) -> Option { + if !data.starts_with(&[0xFF, 0xD8]) { + return None; + } + let mut i = 2usize; + while let Some(&b) = data.get(i) { + if b != 0xFF { + i += 1; + continue; + } + let marker = *data.get(i + 1)?; + match marker { + 0xFF => i += 1, + 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, + 0xDA | 0xD9 => break, + _ => { + let len = usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?])); + if len < 2 { + return None; + } + let seg = data.get(i + 4..i + 2 + len)?; + if marker == 0xEE && seg.starts_with(b"Adobe") && seg.len() >= 12 { + return Some(seg[11]); + } + i = i.checked_add(2 + len)?; + } + } + } + None +} + fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option> { - use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions}; // Past the decoder's metadata bound, decode the image without its Extended XMP chunks // rather than not at all: written out as stored, a CMYK JPEG shows inverted colours. let stripped; @@ -363,26 +458,21 @@ fn cmyk_jpeg_samples(data: &[u8], w: usize, h: usize) -> Option> { stripped = crate::upright_image::without_extended_xmp(data); &stripped[..] }; - let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX)); - let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options); - dec.decode_headers().ok()?; - let input = dec.input_colorspace()?; - if !matches!(input, ColorSpace::CMYK | ColorSpace::YCCK) || dec.dimensions()? != (w, h) { + let transform = adobe_transform(data); + let mut limits = craft_jpeg::Limits::default(); + limits.max_pixels = (u64::from(u16::MAX)) * (u64::from(u16::MAX)); + let decoded = craft_jpeg::decode(data, &limits, transform).ok()?; + if decoded.width as usize != w || decoded.height as usize != h || decoded.components != 4 { return None; } - dec.set_options(options.jpeg_set_out_colorspace(input)); - let mut px = dec.decode().ok()?; + let craft_jpeg::Samples::U8(mut px) = decoded.samples else { + return None; + }; if px.len() != w.checked_mul(h)?.checked_mul(4)? { return None; } - if input == ColorSpace::YCCK { - for p in px.as_chunks_mut::<4>().0 { - let (y, cb, cr) = (f32::from(p[0]), f32::from(p[1]) - 128.0, f32::from(p[2]) - 128.0); - let ink = |rgb: f32| (255.0 - rgb).round().clamp(0.0, 255.0) as u8; - p[0] = ink(y + 1.402 * cr); - p[1] = ink(y - 0.344_136 * cb - 0.714_136 * cr); - p[2] = ink(y + 1.772 * cb); - } + for b in &mut px { + *b = 255 - *b; } Some(px) } @@ -588,7 +678,20 @@ fn gray_plane(doc: &Document, m: &Stream, w: usize, h: usize, budget: &mut Image if !budget.pay_jpeg((mw as u64).saturating_mul(mh as u64), &m.raw) { return None; } - jpeg_of_size(&m.raw, mw, mh)?.into_luma8().into_raw() + let dec = jpeg_of_size(&m.raw, mw, mh)?; + let craft_jpeg::Samples::U8(px) = dec.samples else { + return None; + }; + match dec.components { + 1 => px, + 3 => px + .as_chunks::<3>() + .0 + .iter() + .map(|&[r, g, b]| ((2126 * u32::from(r) + 7152 * u32::from(g) + 722 * u32::from(b) + 5000) / 10_000) as u8) + .collect(), + _ => return None, + } } Some(b"DCTDecode" | b"DCT" | b"JPXDecode") => return None, _ => { @@ -635,23 +738,14 @@ pub(crate) fn scaled(img: &image::GrayImage, w: u32, h: u32) -> image::GrayImage } fn png(w: u32, h: u32, rgb: &[u8], alpha: Option<&[u8]>) -> Result, String> { - let mut out = Vec::new(); - { - let mut enc = png::Encoder::new(&mut out, w, h); - enc.set_depth(png::BitDepth::Eight); - enc.set_compression(png::Compression::Fast); - let data: Vec = match alpha { - Some(a) => { - enc.set_color(png::ColorType::Rgba); - rgb.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], *a]).collect() - } - None => { - enc.set_color(png::ColorType::Rgb); - rgb.to_vec() - } - }; - let mut wr = enc.write_header().map_err(|e| e.to_string())?; - wr.write_image_data(&data).map_err(|e| e.to_string())?; - } - Ok(out) + let (layout, data) = match alpha { + Some(a) => ( + craft_image_codecs::ChannelLayout::Rgba, + rgb.as_chunks::<3>().0.iter().zip(a).flat_map(|(c, a)| [c[0], c[1], c[2], *a]).collect::>(), + ), + None => (craft_image_codecs::ChannelLayout::Rgb, rgb.to_vec()), + }; + let img = craft_image_codecs::Image::from_u8(w, h, layout, data).map_err(|e| e.to_string())?; + let opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &opts).map_err(|e| e.to_string()) } diff --git a/crates/create/src/lib.rs b/crates/create/src/lib.rs index d6c2208a83..6c91af2051 100644 --- a/crates/create/src/lib.rs +++ b/crates/create/src/lib.rs @@ -8,6 +8,7 @@ mod extract; mod text_font; mod upright_image; pub use extract::{ExtractedImage, ImageExport, extract_images, image_file, image_file_within}; +pub use upright_image::jpeg_exif_orientation; mod office; #[cfg(not(target_arch = "wasm32"))] pub use office::libreoffice; @@ -427,36 +428,31 @@ fn jpeg(name: &str, bytes: &[u8]) -> Result { fn png_image(name: &str, bytes: &[u8]) -> Result { let bad = |m: String| CreateError::Image(name.into(), m); - let mut dec = png::Decoder::new(std::io::Cursor::new(bytes)); - dec.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); - let mut reader = dec.read_info().map_err(|e| bad(e.to_string()))?; - let dpi = match reader.info().pixel_dims { - Some(png::PixelDimensions { xppu, yppu, unit: png::Unit::Meter }) if xppu > 0 && yppu > 0 => (xppu as f64 * 0.0254, yppu as f64 * 0.0254), - _ => (72.0, 72.0), + let opts = craft_image_codecs::DecodeOptions { + keep_orientation: true, + limits: craft_image_codecs::Limits { max_alloc: MAX_DECODED_BYTES as u64, ..Default::default() }, + ..Default::default() }; - let icc = reader.info().icc_profile.as_ref().map(|p| p.to_vec()); - // The decoded size comes from the header alone, so a file of a few bytes can claim any - // size: keep the ceiling, and ask for the memory fallibly (a failed allocation would abort - // the whole app, and no `catch_unwind` stops that). - // Flate expands at most 1032:1 and decoding at most 32:1 more (1-bit palette to RGBA), so a - // file cannot honestly hold more than that: refuse a header the bytes cannot back before - // zero-filling a buffer of its size. The slack keeps tiny real images out of the check. - let size = reader - .output_buffer_size() - .filter(|n| *n <= MAX_DECODED_BYTES && *n <= bytes.len().saturating_mul(PNG_MAX_EXPANSION).saturating_add(PNG_SLACK)) - .ok_or_else(|| bad("image too large".into()))?; - let mut buf = Vec::new(); - buf.try_reserve_exact(size).map_err(|_| bad("not enough memory for this image".into()))?; - buf.resize(size, 0); - let frame = reader.next_frame(&mut buf).map_err(|e| bad(e.to_string()))?; - let (w, h) = (frame.width, frame.height); - let data = &buf[..frame.buffer_size()]; - let (channels, cs) = match frame.color_type { - png::ColorType::Grayscale => (1, "DeviceGray"), - png::ColorType::GrayscaleAlpha => (2, "DeviceGray"), - png::ColorType::Rgb => (3, "DeviceRGB"), - png::ColorType::Rgba => (4, "DeviceRGB"), - png::ColorType::Indexed => return Err(bad("unexpected palette output".into())), + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, bytes, &opts).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(m) => bad(format!("image too large: {m}")), + other => bad(other.to_string()), + })?; + let dpi = img.meta.dpi.map(|(x, y)| (f64::from(x), f64::from(y))).filter(|&(x, y)| x > 0.0 && y > 0.0).unwrap_or((72.0, 72.0)); + let icc = img.icc.clone(); + let (w, h) = (img.width(), img.height()); + let (channels, cs) = match img.layout() { + craft_image_codecs::ChannelLayout::Gray => (1, "DeviceGray"), + craft_image_codecs::ChannelLayout::GrayA => (2, "DeviceGray"), + craft_image_codecs::ChannelLayout::Rgb => (3, "DeviceRGB"), + craft_image_codecs::ChannelLayout::Rgba => (4, "DeviceRGB"), + _ => return Err(bad("unexpected palette output".into())), + }; + let data: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() { + std::borrow::Cow::Borrowed(u8s) + } else if let Some(u16s) = img.to_u16_samples() { + std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect()) + } else { + return Err(bad("expected 8-bit or 16-bit PNG samples".into())); }; let has_alpha = channels == 2 || channels == 4; let colour_n = if has_alpha { channels - 1 } else { channels }; @@ -516,20 +512,22 @@ fn rgba_image(rgba: &[u8], (w, h): (u32, u32), dpi: (f64, f64)) -> Embedded { Embedded { dict: d, data: colour, filtered: false, smask, icc: None, px: (w, h), dpi } } -/// BMP and GIF (the first frame), through the `image` decoders. Their resolution is not read: +/// BMP and GIF (the first frame), through `craft-image-codecs`. Their resolution is not read: /// 72 dpi, as for images without one. -fn decoded(name: &str, bytes: &[u8], format: image::ImageFormat) -> Result { +fn decoded(name: &str, bytes: &[u8], format: craft_image_codecs::Format) -> Result { // A GIF's first frame is painted on a canvas the size of its logical screen, however small // the frame: a few bytes can ask for hundreds of MB. - if format == image::ImageFormat::Gif + if format == craft_image_codecs::Format::Gif && let Some(&[w0, w1, h0, h1]) = bytes.get(6..10) && u64::from(u16::from_le_bytes([w0, w1])) * u64::from(u16::from_le_bytes([h0, h1])) > MAX_GIF_PIXELS { return Err(CreateError::Image(name.into(), "the GIF's screen is larger than 4096×4096 pixels".into())); } - let img = image::load_from_memory_with_format(bytes, format).map_err(|e| CreateError::Image(name.into(), e.to_string()))?; - let rgba = img.into_rgba8(); - Ok(rgba_image(rgba.as_raw(), rgba.dimensions(), (72.0, 72.0))) + let img = craft_image_codecs::decode_as_with(format, bytes, &craft_image_codecs::DecodeOptions::default()) + .map_err(|e| CreateError::Image(name.into(), e.to_string()))?; + let dims = (img.width(), img.height()); + let raw = img.to_rgba8(); + Ok(rgba_image(&raw, dims, (72.0, 72.0))) } fn le16(b: &[u8], at: usize) -> Option { @@ -662,16 +660,28 @@ const MAX_GIF_PIXELS: u64 = 1 << 24; /// twice over. const MAX_DECODED_BYTES: usize = 2 << 30; -/// Most a PNG's decoded size can exceed its file size: Flate's 1032:1 times the 32:1 of -/// expanding a 1-bit palette to RGBA, plus a fixed allowance for small files. -const PNG_MAX_EXPANSION: usize = 1032 * 32; -const PNG_SLACK: usize = 1 << 20; - /// Most decoded bytes all the pages of one TIFF may take together: its pages stay in memory until /// they are embedded, and many small directories can share one compressed strip. Saturates to the /// whole address space on 32-bit targets (wasm32), where 4 GiB doesn't fit in `usize`. const MAX_DECODED_TOTAL: usize = MAX_DECODED_BYTES.saturating_mul(2); +/// Returns whether the TIFF directory at `ifd_offset` is a 1-bit bilevel grayscale image. +fn tiff_page_is_bilevel(bytes: &[u8], ifd_offset: u64) -> bool { + let order = match bytes.get(0..2) { + Some(b"II") => craft_tiff::ByteOrder::Little, + Some(b"MM") => craft_tiff::ByteOrder::Big, + _ => return false, + }; + let bigtiff = order.read_u16(bytes, 2) == Some(43); + let Ok((ifd, _)) = craft_tiff::parse_ifd_at(bytes, ifd_offset, order, 0, bigtiff, &craft_tiff::ParseOptions::default()) else { + return false; + }; + let Some(bits) = ifd.u64s(craft_tiff::tags::BITS_PER_SAMPLE) else { + return false; + }; + bits.len() == 1 && bits[0] == 1 +} + /// The first `wanted` pages of a TIFF (multi-page scans become multi-page PDFs). fn tiff_pages(name: &str, bytes: &[u8], wanted: usize) -> Result, CreateError> { tiff_pages_within(name, bytes, wanted, MAX_DECODED_TOTAL) @@ -679,67 +689,71 @@ fn tiff_pages(name: &str, bytes: &[u8], wanted: usize) -> Result, /// [`tiff_pages`], refused once the pages kept take more than `budget` bytes. fn tiff_pages_within(name: &str, bytes: &[u8], wanted: usize, budget: usize) -> Result, CreateError> { - use tiff::ColorType as C; - use tiff::decoder::{Decoder, DecodingResult, Limits}; - use tiff::tags::Tag; + use craft_image_codecs::ChannelLayout as L; let bad = |m: String| CreateError::Image(name.into(), m); - // The tiff crate stops at 256 MiB of decoded pixels, below an ordinary poster scan (#665): - // 12000x16000 CMYK is 768 MiB. Raise it, but keep a ceiling so a tiny file claiming - // enormous dimensions can't ask for unbounded memory. - let mut limits = Limits::default(); - limits.decoding_buffer_size = MAX_DECODED_BYTES; - let mut dec = Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| bad(e.to_string()))?.with_limits(limits); + let info = craft_image_codecs::tiff_info(bytes).map_err(|e| bad(e.to_string()))?; + let opts = craft_image_codecs::DecodeOptions { + keep_orientation: true, + limits: craft_image_codecs::Limits { max_alloc: MAX_DECODED_BYTES as u64, ..Default::default() }, + ..Default::default() + }; let mut out = Vec::new(); let mut kept = 0usize; - loop { - let (w, h) = dec.dimensions().map_err(|e| bad(e.to_string()))?; - let ct = dec.colortype().map_err(|e| bad(e.to_string()))?; - let unit = dec.get_tag_u32(Tag::ResolutionUnit).unwrap_or(2); - let res = |t: Tag, dec: &mut Decoder>| -> f64 { - // Resolutions are rationals. - let v = match dec.get_tag(t) { - Ok(tiff::decoder::ifd::Value::Rational(n, d)) if d != 0 => f64::from(n) / f64::from(d), - Ok(other) => other.into_f64().unwrap_or(72.0), - Err(_) => 72.0, - }; - let v = if unit == 3 { v * 2.54 } else { v }; - if v.is_finite() && v >= 1.0 { v } else { 72.0 } + for (idx, p) in info.pages.iter().enumerate() { + if out.len() >= wanted { + break; + } + if p.kind == craft_image_codecs::TiffPageKind::ReducedResolution { + continue; + } + let img = craft_image_codecs::decode_tiff_page(bytes, idx, &opts).map_err(|e| bad(e.to_string()))?; + let (w, h) = (img.width(), img.height()); + let dpi = img.meta.dpi.map(|(x, y)| (f64::from(x), f64::from(y))).filter(|&(x, y)| x > 0.0 && y > 0.0).unwrap_or((72.0, 72.0)); + let data_buf: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() { + std::borrow::Cow::Borrowed(u8s) + } else if let Some(u16s) = img.to_u16_samples() { + std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect()) + } else { + return Err(bad("this TIFF sample format isn't supported".into())); }; - let dpi = (res(Tag::XResolution, &mut dec), res(Tag::YResolution, &mut dec)); - let data = match dec.read_image().map_err(|e| bad(e.to_string()))? { - DecodingResult::U8(v) => v, - // 16-bit samples: keep the high byte. - DecodingResult::U16(v) => v.iter().map(|x| (x >> 8) as u8).collect(), - _ => return Err(bad("this TIFF sample format isn't supported".into())), - }; - let page = match ct { - C::Gray(1) => { - // Bilevel scans stay 1 bit per pixel (0 = black after the decoder's inversion). + let data = data_buf.as_ref(); + let page = match img.layout() { + L::Gray if tiff_page_is_bilevel(bytes, p.ifd_offset) => { + // Bilevel scans stay 1 bit per pixel (0 = black). + let row_bytes = (w as usize).div_ceil(8); + let mut packed = vec![0u8; row_bytes.saturating_mul(h as usize)]; + for y in 0..h as usize { + for x in 0..w as usize { + if data[y * w as usize + x] >= 128 { + packed[y * row_bytes + (x >> 3)] |= 0x80 >> (x & 7); + } + } + } let mut d = Dict::new(); d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray")); d.set(b"BitsPerComponent".to_vec(), Object::Int(1)); - Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi } + Embedded { dict: d, data: packed, filtered: false, smask: None, icc: None, px: (w, h), dpi } } - C::Gray(8 | 16) => { + L::Gray => { let mut d = Dict::new(); d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray")); d.set(b"BitsPerComponent".to_vec(), Object::Int(8)); - Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi } + Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi } } - C::RGB(8 | 16) => { + L::Rgb => { let mut d = Dict::new(); d.set(b"ColorSpace".to_vec(), Object::name("DeviceRGB")); d.set(b"BitsPerComponent".to_vec(), Object::Int(8)); - Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi } + Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi } } - C::CMYK(8 | 16) => { + L::Cmyk => { let mut d = Dict::new(); d.set(b"ColorSpace".to_vec(), Object::name("DeviceCMYK")); d.set(b"BitsPerComponent".to_vec(), Object::Int(8)); - Embedded { dict: d, data, filtered: false, smask: None, icc: None, px: (w, h), dpi } + Embedded { dict: d, data: data.to_vec(), filtered: false, smask: None, icc: None, px: (w, h), dpi } } - C::RGBA(8 | 16) => rgba_image(&data, (w, h), dpi), - C::GrayA(8 | 16) => { + L::Rgba => rgba_image(data, (w, h), dpi), + L::GrayA => { let rgba: Vec = data.as_chunks::<2>().0.iter().flat_map(|p| [p[0], p[0], p[0], p[1]]).collect(); rgba_image(&rgba, (w, h), dpi) } @@ -750,10 +764,6 @@ fn tiff_pages_within(name: &str, bytes: &[u8], wanted: usize, budget: usize) -> return Err(bad(format!("the TIFF's pages take more than {} MiB together", budget >> 20))); } out.push(page); - if out.len() >= wanted || !dec.more_images() { - break; - } - dec.next_image().map_err(|e| bad(e.to_string()))?; } Ok(out) } @@ -835,9 +845,9 @@ fn embed(name: &str, bytes: &[u8], wanted: usize) -> Result, Creat } else if is_tiff(bytes) { tiff_pages(name, bytes, wanted) } else if bytes.starts_with(b"BM") { - Ok(vec![decoded(name, &pdfcraft_xfa::image::bmp_with_pixel_offset(bytes), image::ImageFormat::Bmp)?]) + Ok(vec![decoded(name, &pdfcraft_xfa::image::bmp_with_pixel_offset(bytes), craft_image_codecs::Format::Bmp)?]) } else if is_gif(bytes) { - Ok(vec![decoded(name, bytes, image::ImageFormat::Gif)?]) + Ok(vec![decoded(name, bytes, craft_image_codecs::Format::Gif)?]) } else if is_pcx(bytes) { Ok(vec![pcx(name, bytes)?]) } else if bytes.starts_with(JP2_SIGNATURE) || bytes.starts_with(&[0xFF, 0x4F, 0xFF, 0x51]) { diff --git a/crates/create/src/tests.rs b/crates/create/src/tests.rs index f8b870832b..19ad54a1bd 100644 --- a/crates/create/src/tests.rs +++ b/crates/create/src/tests.rs @@ -22,21 +22,15 @@ fn media(d: &Dict) -> Vec { /// A tiny PNG: 4×2 RGBA with one transparent pixel, 144 dpi. fn png_bytes(alpha: bool) -> Vec { - let mut out = Vec::new(); - { - let mut enc = png::Encoder::new(&mut out, 4, 2); - enc.set_color(if alpha { png::ColorType::Rgba } else { png::ColorType::Rgb }); - enc.set_depth(png::BitDepth::Eight); - enc.set_pixel_dims(Some(png::PixelDimensions { xppu: 5669, yppu: 5669, unit: png::Unit::Meter })); - let mut w = enc.write_header().unwrap(); - let n = if alpha { 4 } else { 3 }; - let mut data = vec![200u8; 8 * n]; - if alpha { - data[3] = 0; - } - w.write_image_data(&data).unwrap(); + let layout = if alpha { craft_image_codecs::ChannelLayout::Rgba } else { craft_image_codecs::ChannelLayout::Rgb }; + let n = if alpha { 4 } else { 3 }; + let mut data = vec![200u8; 8 * n]; + if alpha { + data[3] = 0; } - out + let mut img = craft_image_codecs::Image::from_u8(4, 2, layout, data).unwrap(); + img.meta.dpi = Some((144.0, 144.0)); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() } /// A minimal baseline JPEG header (SOI, JFIF at 300 dpi, SOF0 3×2 RGB, EOI). Only the headers @@ -288,28 +282,43 @@ fn image_of(doc: &Document, page: usize) -> Dict { #[test] fn bmp_gif_and_multi_page_tiff_images() { - use image::{ImageEncoder, Rgba, RgbaImage}; // BMP: 3×2 opaque colour. - let mut bmp = Vec::new(); - image::codecs::bmp::BmpEncoder::new(&mut bmp).write_image(&[10, 20, 30].repeat(6), 3, 2, image::ExtendedColorType::Rgb8).unwrap(); + let bmp_img = craft_image_codecs::Image::from_u8(3, 2, craft_image_codecs::ChannelLayout::Rgb, [10, 20, 30].repeat(6)).unwrap(); + let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap(); // GIF: 2×2 with a transparent pixel. - let mut gif = Vec::new(); - { - let mut img = RgbaImage::from_pixel(2, 2, Rgba([255, 0, 0, 255])); - img.put_pixel(0, 0, Rgba([0, 0, 0, 0])); - let mut enc = image::codecs::gif::GifEncoder::new(&mut gif); - enc.encode(img.as_raw(), 2, 2, image::ExtendedColorType::Rgba8).unwrap(); - } + let gif_img = craft_image_codecs::Image::from_u8( + 2, + 2, + craft_image_codecs::ChannelLayout::Rgba, + vec![0, 0, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255, 0, 0, 255], + ) + .unwrap(); + let gif = craft_image_codecs::encode(&gif_img, craft_image_codecs::Format::Gif, &craft_image_codecs::EncodeOptions::default()).unwrap(); // TIFF: two 8-bit gray pages, one at 144 dpi and one of only black and white. - let mut tif = std::io::Cursor::new(Vec::new()); - { - let mut enc = tiff::encoder::TiffEncoder::new(&mut tif).unwrap(); - let mut im = enc.new_image::(4, 2).unwrap(); - im.resolution(tiff::tags::ResolutionUnit::Inch, tiff::encoder::Rational { n: 144, d: 1 }); - im.write_data(&[128; 8]).unwrap(); - enc.write_image::(2, 2, &[0, 255, 255, 0]).unwrap(); - } - let doc = from_images(&[("a.bmp".into(), bmp), ("b.gif".into(), gif), ("scan.tif".into(), tif.into_inner())]).unwrap(); + let tif = { + let w = craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false); + let mut p1 = craft_tiff::IfdBuilder::new() + .with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![4])) + .with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![2])) + .with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8])) + .with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::RESOLUTION_UNIT, craft_tiff::Value::Short(vec![2])) + .with(craft_tiff::tags::X_RESOLUTION, craft_tiff::Value::Rational(vec![(144, 1)])) + .with(craft_tiff::tags::Y_RESOLUTION, craft_tiff::Value::Rational(vec![(144, 1)])); + p1.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 2, strips: vec![vec![128; 8]] }); + let mut p2 = craft_tiff::IfdBuilder::new() + .with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![2])) + .with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![2])) + .with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8])) + .with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1])); + p2.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 2, strips: vec![vec![0, 255, 255, 0]] }); + w.write(&[p1, p2]).unwrap() + }; + let doc = from_images(&[("a.bmp".into(), bmp), ("b.gif".into(), gif), ("scan.tif".into(), tif)]).unwrap(); let doc = reopen(&doc); assert_eq!(pages(&doc).len(), 4, "one page per BMP and GIF, two for the TIFF"); assert_eq!(media(&pages(&doc)[0]), [0.0, 0.0, 3.0, 2.0]); @@ -324,10 +333,9 @@ fn bmp_gif_and_multi_page_tiff_images() { /// the intact file, not shifted by the header bytes. #[test] fn a_bmp_with_a_zero_pixel_offset_is_placed_like_the_intact_file() { - use image::ImageEncoder; - let mut bmp = Vec::new(); let pixels: Vec = (0..18).map(|i| i * 13).collect(); - image::codecs::bmp::BmpEncoder::new(&mut bmp).write_image(&pixels, 3, 2, image::ExtendedColorType::Rgb8).unwrap(); + let bmp_img = craft_image_codecs::Image::from_u8(3, 2, craft_image_codecs::ChannelLayout::Rgb, pixels).unwrap(); + let bmp = craft_image_codecs::encode(&bmp_img, craft_image_codecs::Format::Bmp, &craft_image_codecs::EncodeOptions::default()).unwrap(); let mut zero = bmp.clone(); zero[10..14].copy_from_slice(&0u32.to_le_bytes()); let samples = |data: Vec| { @@ -417,18 +425,39 @@ fn a_small_png_claiming_a_large_frame_is_refused_before_zero_filling() { } /// Likewise a PNG: the png crate's limits don't cover the frame buffer, which its header sizes +fn png_crc32(data: &[u8]) -> u32 { + let mut crc = 0xFFFF_FFFFu32; + for &b in data { + crc ^= u32::from(b); + for _ in 0..8 { + let mask = (crc & 1).wrapping_neg(); + crc = (crc >> 1) ^ (0xEDB8_8320 & mask); + } + } + !crc +} + +fn push_png_chunk(out: &mut Vec, kind: [u8; 4], data: &[u8]) { + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + let start = out.len(); + out.extend_from_slice(&kind); + out.extend_from_slice(data); + let crc = png_crc32(&out[start..]); + out.extend_from_slice(&crc.to_be_bytes()); +} + /// (60000x60000 RGBA is 14.4 GB). Refused before anything is allocated, not after. #[test] fn a_png_claiming_enormous_dimensions_is_refused() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 60_000, 60_000); - enc.set_color(png::ColorType::Rgba); - enc.set_depth(png::BitDepth::Eight); - let mut w = enc.write_header().unwrap(); - // A zlib stream holding one zero byte: far too little data, but a well-formed start. - w.write_chunk(png::chunk::IDAT, &[0x78, 0x01, 0x01, 0x01, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x01, 0x00, 0x01]).unwrap(); - } + let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut ihdr = Vec::with_capacity(13); + ihdr.extend_from_slice(&60_000u32.to_be_bytes()); + ihdr.extend_from_slice(&60_000u32.to_be_bytes()); + ihdr.extend_from_slice(&[8, 6, 0, 0, 0]); + push_png_chunk(&mut png, *b"IHDR", &ihdr); + // A zlib stream holding one zero byte: far too little data, but a well-formed start. + push_png_chunk(&mut png, *b"IDAT", &[0x78, 0x01, 0x01, 0x01, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x01, 0x00, 0x01]); + push_png_chunk(&mut png, *b"IEND", &[]); match from_images(&[("huge.png".into(), png)]) { Err(CreateError::Image(_, m)) => assert!(m.contains("too large"), "{m}"), other => panic!("expected a refusal, got {:?}", other.map(|_| ())), @@ -446,11 +475,10 @@ fn images_export_as_jpeg_unchanged_and_others_as_png() { assert_eq!(kinds, [(0, "png", 4, 2), (1, "jpg", 3, 2), (2, "png", 4, 2)]); assert_eq!(out.images[1].data, jpeg_bytes(), "JPEG data is written as is"); // The PNG round-trips the pixels, with the soft mask as alpha. - let dec = png::Decoder::new(std::io::Cursor::new(out.images[0].data.clone())); - let mut r = dec.read_info().unwrap(); - let mut buf = vec![0; r.output_buffer_size().unwrap()]; - let info = r.next_frame(&mut buf).unwrap(); - assert_eq!(info.color_type, png::ColorType::Rgba); + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &out.images[0].data, &craft_image_codecs::DecodeOptions::default()) + .unwrap(); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgba); + let buf = img.as_u8().unwrap(); assert_eq!(&buf[..8], &[200, 200, 200, 0, 200, 200, 200, 200]); // Pages filter; small images can be left out. assert_eq!(extract_images(&doc, &[2], 0).images.len(), 1); @@ -482,10 +510,9 @@ fn indexed_and_one_bit_images_decode() { ); doc.set(r, Object::Stream(Stream::from_raw(d, vec![0b0101_0000]))); let out = extract_images(&doc, &[0], 0); - let dec = png::Decoder::new(std::io::Cursor::new(out.images[0].data.clone())); - let mut rd = dec.read_info().unwrap(); - let mut buf = vec![0; rd.output_buffer_size().unwrap()]; - rd.next_frame(&mut buf).unwrap(); + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &out.images[0].data, &craft_image_codecs::DecodeOptions::default()) + .unwrap(); + let buf = img.as_u8().unwrap(); assert_eq!(&buf[..12], &[255, 0, 0, 0, 0, 255, 255, 0, 0, 0, 0, 255]); } @@ -570,14 +597,10 @@ fn source_kind_tells_pdfs_images_and_text_apart() { /// BigTIFF (version 43, used for large scans) is detected and converted like a classic TIFF. #[test] fn bigtiff_images_become_pages() { - let mut tif = std::io::Cursor::new(Vec::new()); - { - let mut enc = tiff::encoder::TiffEncoder::new_big(&mut tif).unwrap(); - let mut im = enc.new_image::(4, 2).unwrap(); - im.resolution(tiff::tags::ResolutionUnit::Inch, tiff::encoder::Rational { n: 144, d: 1 }); - im.write_data(&[128; 8]).unwrap(); - } - let tif = tif.into_inner(); + let mut img = craft_image_codecs::Image::from_u8(4, 2, craft_image_codecs::ChannelLayout::Gray, vec![128; 8]).unwrap(); + img.meta.dpi = Some((144.0, 144.0)); + let opts = craft_image_codecs::EncodeOptions { tiff_bigtiff: true, ..Default::default() }; + let tif = craft_image_codecs::encode(&img, craft_image_codecs::Format::Tiff, &opts).unwrap(); assert!(tif.starts_with(b"II+\0"), "the encoder wrote a BigTIFF"); assert_eq!(source_kind("scan.tif", &tif), Some(SourceKind::Image)); let doc = reopen(&from_images(&[("scan.tif".into(), tif)]).unwrap()); @@ -768,15 +791,9 @@ fn embedded_icc_profiles_tag_the_image_colour_space() { let jpeg = cmyk_jpeg_bytes(&cmyk); let rgb = icc_profile(b"RGB "); let png = { - let mut out = Vec::new(); - let mut info = png::Info::with_size(2, 1); - info.color_type = png::ColorType::Rgb; - info.bit_depth = png::BitDepth::Eight; - info.icc_profile = Some(rgb.clone().into()); - let mut w = png::Encoder::with_info(&mut out, info).unwrap().write_header().unwrap(); - w.write_image_data(&[10, 20, 30, 40, 50, 60]).unwrap(); - w.finish().unwrap(); - out + let mut img = craft_image_codecs::Image::from_u8(2, 1, craft_image_codecs::ChannelLayout::Rgb, vec![10, 20, 30, 40, 50, 60]).unwrap(); + img.icc = Some(rgb.clone()); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() }; let doc = reopen(&from_images(&[("a.jpg".into(), jpeg.clone()), ("b.jpg".into(), jpeg.clone()), ("c.png".into(), png)]).unwrap()); let (n, alt, data, first) = icc_of(&doc, &image_of(&doc, 0)).expect("the CMYK JPEG is ICC-tagged"); @@ -845,20 +862,9 @@ fn exported_rgba(doc: &Document) -> (&'static str, Vec) { let out = extract_images(doc, &[0], 0); assert!(out.skipped.is_empty(), "{:?}", out.skipped); let img = &out.images[0]; - if img.extension == "jpg" { - return ("jpg", image::load_from_memory(&img.data).unwrap().into_rgba8().into_raw()); - } - let dec = png::Decoder::new(std::io::Cursor::new(img.data.clone())); - let mut r = dec.read_info().unwrap(); - let mut buf = vec![0; r.output_buffer_size().unwrap()]; - let info = r.next_frame(&mut buf).unwrap(); - buf.truncate(info.buffer_size()); - let rgba = match info.color_type { - png::ColorType::Rgba => buf, - png::ColorType::Rgb => buf.as_chunks::<3>().0.iter().flat_map(|c| [c[0], c[1], c[2], 255]).collect(), - other => panic!("unexpected {other:?}"), - }; - ("png", rgba) + let ext = if img.extension == "jpg" { "jpg" } else { "png" }; + let decoded = craft_image_codecs::decode(&img.data).unwrap().to_rgba8(); + (ext, decoded) } fn alphas(rgba: &[u8]) -> Vec { @@ -867,9 +873,9 @@ fn alphas(rgba: &[u8]) -> Vec { /// A 4×2 grey JPEG (mid grey). fn real_jpeg() -> Vec { - let mut out = Vec::new(); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95).encode(&[128u8; 8], 4, 2, image::ExtendedColorType::L8).unwrap(); - out + let img = craft_image_codecs::Image::from_u8(4, 2, craft_image_codecs::ChannelLayout::Gray, vec![128u8; 8]).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap() } #[test] @@ -1106,12 +1112,8 @@ fn indexed_image_export_clamps_samples_to_hival() { } fn exported_png_pixels(bytes: &[u8]) -> Vec { - let decoder = png::Decoder::new(std::io::Cursor::new(bytes)); - let mut reader = decoder.read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut pixels).unwrap(); - pixels.truncate(info.buffer_size()); - pixels + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, bytes, &craft_image_codecs::DecodeOptions::default()).unwrap(); + img.as_u8().unwrap().to_vec() } fn indexed_export_fixture( @@ -1483,14 +1485,17 @@ fn cmyk_and_ycck_jpegs_export_in_the_colours_the_page_shows() { /// A 60 x 20 JPEG at `dpi` x 72 dpi, tagged with EXIF `orientation` (6: shown turned 90 degrees /// clockwise). pub(crate) fn phone_jpeg(orientation: u8, dpi: u16) -> Vec { - use image::codecs::jpeg::{JpegEncoder, PixelDensity, PixelDensityUnit}; - let mut jpeg = Vec::new(); - let mut encoder = JpegEncoder::new(&mut jpeg); - encoder.set_pixel_density(PixelDensity { density: (dpi, 72), unit: PixelDensityUnit::Inches }); - encoder.encode_image(&image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20]))).unwrap(); + let rgb = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, vec![20; 60 * 20 * 3]).unwrap(); + let jpeg = craft_image_codecs::encode(&rgb, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap(); let mut exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0".to_vec(); exif.extend_from_slice(&[orientation, 0, 0, 0, 0, 0, 0, 0]); let mut out = jpeg[..2].to_vec(); + // JFIF APP0 with pixel density in inches (`dpi` x 72 dpi). + out.extend_from_slice(&[0xff, 0xe0, 0x00, 0x10]); + out.extend_from_slice(b"JFIF\0\x01\x01\x01"); + out.extend_from_slice(&dpi.to_be_bytes()); + out.extend_from_slice(&72u16.to_be_bytes()); + out.extend_from_slice(&[0, 0]); out.extend_from_slice(&[0xff, 0xe1]); out.extend_from_slice(&((exif.len() + 2) as u16).to_be_bytes()); out.extend_from_slice(&exif); @@ -1573,15 +1578,34 @@ fn a_one_bit_tiff_is_stored_as_group_4() { /// A TIFF of `pages` 64×64 gray pages, then (with `unreadable`) one whose samples Create can't read. fn gray_tiff(pages: usize, unreadable: bool) -> Vec { - let mut tif = std::io::Cursor::new(Vec::new()); - let mut enc = tiff::encoder::TiffEncoder::new(&mut tif).unwrap(); + let mut ifds = Vec::with_capacity(pages + usize::from(unreadable)); for _ in 0..pages { - enc.write_image::(64, 64, &[128; 64 * 64]).unwrap(); + let mut ifd = craft_tiff::IfdBuilder::new() + .with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![64])) + .with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![64])) + .with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![8])) + .with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![1])); + ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 64, strips: vec![vec![128; 64 * 64]] }); + ifds.push(ifd); } if unreadable { - enc.write_image::(1, 1, &[0.5; 3]).unwrap(); + // 32-bit IEEE float RGB (SampleFormat = 3), unreadable by Create. + let mut ifd = craft_tiff::IfdBuilder::new() + .with(craft_tiff::tags::IMAGE_WIDTH, craft_tiff::Value::Long(vec![1])) + .with(craft_tiff::tags::IMAGE_LENGTH, craft_tiff::Value::Long(vec![1])) + .with(craft_tiff::tags::BITS_PER_SAMPLE, craft_tiff::Value::Short(vec![32, 32, 32])) + .with(craft_tiff::tags::COMPRESSION, craft_tiff::Value::Short(vec![1])) + .with(craft_tiff::tags::PHOTOMETRIC, craft_tiff::Value::Short(vec![2])) + .with(craft_tiff::tags::SAMPLES_PER_PIXEL, craft_tiff::Value::Short(vec![3])) + .with(craft_tiff::tags::SAMPLE_FORMAT, craft_tiff::Value::Short(vec![3, 3, 3])); + let f = 0.5f32.to_le_bytes(); + let px: Vec = [f, f, f].concat(); + ifd.set_image(craft_tiff::ImageData::Strips { rows_per_strip: 1, strips: vec![px] }); + ifds.push(ifd); } - tif.into_inner() + craft_tiff::TiffWriter::new(craft_tiff::ByteOrder::Little, false).write(&ifds).unwrap() } /// Many small IFDs can each decode to a large page: the pages a TIFF keeps in memory together @@ -1615,18 +1639,79 @@ fn a_tiny_gif_on_a_huge_screen_is_refused() { assert_eq!(media(&pages(&doc)[0]), [0.0, 0.0, 40.0, 30.0]); } -/// A PNG of `w` × `h` samples `data` in `color` at `depth`, with a palette or an ICC profile. -fn png_of(w: u32, h: u32, color: png::ColorType, depth: png::BitDepth, data: &[u8], palette: Option>, icc: Option>) -> Vec { - let mut out = Vec::new(); - { - let mut info = png::Info::with_size(w, h); - info.color_type = color; - info.bit_depth = depth; - info.palette = palette.map(Into::into); - info.icc_profile = icc.map(Into::into); - let mut writer = png::Encoder::with_info(&mut out, info).unwrap().write_header().unwrap(); - writer.write_image_data(data).unwrap(); +#[derive(Clone, Copy)] +enum TestPngColor { + Grayscale, + Rgb, + Indexed, +} + +#[derive(Clone, Copy)] +enum TestPngDepth { + One, + Eight, +} + +fn zlib_uncompressed(data: &[u8]) -> Vec { + let mut out = vec![0x78, 0x01]; + let chunks: Vec<&[u8]> = if data.is_empty() { vec![&[][..]] } else { data.chunks(65_535).collect() }; + for (i, chunk) in chunks.iter().enumerate() { + let last = u8::from(i + 1 == chunks.len()); + let len = chunk.len() as u16; + out.push(last); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&(!len).to_le_bytes()); + out.extend_from_slice(chunk); } + let (mut s1, mut s2) = (1u32, 0u32); + for &b in data { + s1 = (s1 + u32::from(b)) % 65_521; + s2 = (s2 + s1) % 65_521; + } + out.extend_from_slice(&((s2 << 16) | s1).to_be_bytes()); + out +} + +/// A PNG of `w` × `h` samples `data` in `color` at `depth`, with a palette or an ICC profile. +fn png_of(w: u32, h: u32, color: TestPngColor, depth: TestPngDepth, data: &[u8], palette: Option>, icc: Option>) -> Vec { + let color_byte = match color { + TestPngColor::Grayscale => 0u8, + TestPngColor::Rgb => 2, + TestPngColor::Indexed => 3, + }; + let bit_depth = match depth { + TestPngDepth::One => 1u8, + TestPngDepth::Eight => 8, + }; + let row_bytes = match (color, depth) { + (TestPngColor::Grayscale | TestPngColor::Indexed, TestPngDepth::One) => (w as usize).div_ceil(8), + (TestPngColor::Grayscale | TestPngColor::Indexed, TestPngDepth::Eight) => w as usize, + (TestPngColor::Rgb, TestPngDepth::Eight) => w as usize * 3, + (TestPngColor::Rgb, TestPngDepth::One) => unreachable!(), + }; + let mut raw = Vec::with_capacity((1 + row_bytes) * h as usize); + for row in data.chunks_exact(row_bytes) { + raw.push(0); // filter type None + raw.extend_from_slice(row); + } + let idat = zlib_uncompressed(&raw); + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + let mut ihdr = Vec::with_capacity(13); + ihdr.extend_from_slice(&w.to_be_bytes()); + ihdr.extend_from_slice(&h.to_be_bytes()); + ihdr.extend_from_slice(&[bit_depth, color_byte, 0, 0, 0]); + push_png_chunk(&mut out, *b"IHDR", &ihdr); + if let Some(icc) = icc { + let compressed = zlib_uncompressed(&icc); + let mut iccp = b"test\0\0".to_vec(); + iccp.extend_from_slice(&compressed); + push_png_chunk(&mut out, *b"iCCP", &iccp); + } + if let Some(palette) = palette { + push_png_chunk(&mut out, *b"PLTE", &palette); + } + push_png_chunk(&mut out, *b"IDAT", &idat); + push_png_chunk(&mut out, *b"IEND", &[]); out } @@ -1735,7 +1820,8 @@ fn image_stream(doc: &Document, page: usize) -> Stream { /// stored as before. #[test] fn black_and_white_images_are_stored_at_one_bit() { - use png::{BitDepth, ColorType}; + use TestPngColor as ColorType; + use TestPngDepth as BitDepth; let (w, h) = (1200u32, 240u32); let bits = strokes(w, h); let stride = (w as usize).div_ceil(8); @@ -1785,11 +1871,9 @@ fn black_and_white_images_are_stored_at_one_bit() { /// The pixels of an exported PNG (RGB), as true for white. fn exported_white(png_data: &[u8]) -> Vec { - let mut reader = png::Decoder::new(std::io::Cursor::new(png_data.to_vec())).read_info().unwrap(); - let mut buf = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut buf).unwrap(); - assert_eq!(frame.color_type, png::ColorType::Rgb); - buf[..frame.buffer_size()].as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect() + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png_data, &craft_image_codecs::DecodeOptions::default()).unwrap(); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb); + img.as_u8().unwrap().as_chunks::<3>().0.iter().map(|p| *p == [255, 255, 255]).collect() } /// Export ▸ Images reads CCITT fax images: a page created from a 1-bit PNG (Group 4 now) exports as @@ -1799,7 +1883,8 @@ fn exported_white(png_data: &[u8]) -> Vec { /// its width is skipped, not misread. #[test] fn ccitt_fax_images_export_as_their_picture() { - use png::{BitDepth, ColorType}; + use TestPngColor as ColorType; + use TestPngDepth as BitDepth; let (w, h) = (1200u32, 240u32); let bits = strokes(w, h); let stride = (w as usize).div_ceil(8); @@ -1994,7 +2079,8 @@ enum Fill { /// the same with their `/DecodeParms` given by reference as direct. #[test] fn group_3_ccitt_masks_and_parameters_by_reference_export_as_their_picture() { - use png::{BitDepth, ColorType}; + use TestPngColor as ColorType; + use TestPngDepth as BitDepth; let (w, h) = (64u32, 16u32); let rows: Vec<&[u32]> = (0..h).map(|y| if y % 2 == 0 { &[32u32, 32][..] } else { &[16, 16, 32][..] }).collect(); let mut bits = vec![0u8; 8 * h as usize]; @@ -2538,9 +2624,8 @@ fn flate_images_past_the_exports_allowance_are_left_out_with_the_reason() { assert!(refused.contains("more image decoding than"), "{refused}"); assert_eq!(budget.refused(), 1); - let jpeg = image::RgbImage::from_pixel(2, 2, image::Rgb([9, 9, 9])); - let mut bytes = Vec::new(); - jpeg.write_to(&mut std::io::Cursor::new(&mut bytes), image::ImageFormat::Jpeg).unwrap(); + let jpeg_img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![9; 2 * 2 * 3]).unwrap(); + let bytes = craft_image_codecs::encode(&jpeg_img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap(); let jpeg = doc.add(Object::Stream(image("DCTDecode", bytes))); assert!(crate::extract::image_file_within(&doc, jpeg, &mut budget).is_ok(), "nothing charged"); assert_eq!(budget.refused(), 1); @@ -2587,8 +2672,12 @@ fn a_jpegs_lookup_table_is_paid_for_once() { use pdfcraft_cos::{Dict, Document, ImageBudget, Object, PdfString, Stream}; let table = [255, 0, 0, 0, 0, 255]; let lookup = Stream::flate(Dict::new(), &table); - let mut jpeg = Vec::new(); - image::GrayImage::from_pixel(2, 1, image::Luma([1])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(2, 1, craft_image_codecs::ChannelLayout::Gray, vec![1, 1]).unwrap(), + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions::default(), + ) + .unwrap(); // `given`: None leaves the colour space out, Some(true) gives the table as a stream. let exported = |given: Option, work: u64| { let mut doc = Document::new_empty(); @@ -2644,8 +2733,12 @@ fn a_jpegs_lookup_table_is_paid_for_once() { #[test] fn a_jpeg_that_has_to_be_decoded_shares_the_allowance() { use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream}; - let mut jpeg = Vec::new(); - image::RgbImage::from_pixel(4, 4, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Rgb, vec![9; 4 * 4 * 3]).unwrap(), + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions::default(), + ) + .unwrap(); let image_dict = |extra: Option<(&[u8], Object)>| { let mut d = Dict::new(); d.set(b"Subtype".to_vec(), Object::name("Image")); @@ -2690,8 +2783,12 @@ fn a_jpeg_that_has_to_be_decoded_shares_the_allowance() { #[test] fn a_jpeg_mask_pays_for_its_samples() { use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream}; - let mut jpeg = Vec::new(); - image::GrayImage::from_pixel(4, 4, image::Luma([90])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Gray, vec![90; 4 * 4]).unwrap(), + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions::default(), + ) + .unwrap(); let mut mask = Dict::new(); mask.set(b"Subtype".to_vec(), Object::name("Image")); mask.set(b"Width".to_vec(), Object::Int(4)); @@ -2726,8 +2823,12 @@ fn a_jpeg_mask_pays_for_its_samples() { #[test] fn a_jpeg_larger_than_its_dictionary_says_isnt_decoded() { use pdfcraft_cos::{Dict, Document, ImageBudget, Object, Stream}; - let mut jpeg = Vec::new(); - image::RgbImage::from_pixel(4, 4, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(4, 4, craft_image_codecs::ChannelLayout::Rgb, vec![9; 4 * 4 * 3]).unwrap(), + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions::default(), + ) + .unwrap(); // A colour key means decoding it, to find the pixels it hides. let keyed = |side: i64| { let mut d = Dict::new(); @@ -2787,8 +2888,12 @@ fn an_image_whose_jbig2_mask_is_past_the_allowance_is_left_out() { let mut doc = Document::new_empty(); let mask = doc.add(Object::Stream(Stream::from_raw(mask, pdfcraft_filters::encode_jbig2_generic(&[0xFF; 4], 16, 2).unwrap()))); // A JPEG, read for nothing until its mask is, so only the mask can be what stops it. - let mut jpeg = Vec::new(); - image::RgbImage::from_pixel(1, 1, image::Rgb([9, 9, 9])).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap(); + let jpeg = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![9, 9, 9]).unwrap(), + craft_image_codecs::Format::Jpeg, + &craft_image_codecs::EncodeOptions::default(), + ) + .unwrap(); let mut image = Dict::new(); image.set(b"Subtype".to_vec(), Object::name("Image")); image.set(b"Width".to_vec(), Object::Int(1)); @@ -2886,11 +2991,11 @@ fn a_mask_of_other_proportions_than_its_image_is_scaled_without_the_memory_of_bo let image = doc.add(Object::Stream(image)); let (name, png) = crate::image_file(&doc, image).unwrap_or_else(|e| panic!("{mask_w} × {mask_h} on {w} × {h}: {e}")); assert_eq!(name, "png"); - let mut reader = ::png::Decoder::new(std::io::Cursor::new(&png)).read_info().unwrap(); - let mut rgba = vec![0; reader.output_buffer_size().unwrap()]; - let info = reader.next_frame(&mut rgba).unwrap(); - assert_eq!((info.width as usize, info.height as usize, info.color_type), (w, h, ::png::ColorType::Rgba)); - assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[3].abs_diff(0x80) <= 1), "{mask_w} × {mask_h} on {w} × {h}"); + let decoded = + craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, &png, &craft_image_codecs::DecodeOptions::default()).unwrap(); + assert_eq!((decoded.width() as usize, decoded.height() as usize, decoded.layout()), (w, h, craft_image_codecs::ChannelLayout::Rgba)); + let rgba = decoded.as_u8().unwrap(); + assert!(rgba.as_chunks::<4>().0.iter().all(|p| p[3].abs_diff(0x80u8) <= 1), "{mask_w} × {mask_h} on {w} × {h}"); } } diff --git a/crates/create/src/upright_image.rs b/crates/create/src/upright_image.rs index d193a38bfb..e6fc4eab0e 100644 --- a/crates/create/src/upright_image.rs +++ b/crates/create/src/upright_image.rs @@ -3,11 +3,6 @@ //! crate (pages made from images, stamps, added images, replacements) goes through here first. use std::borrow::Cow; -use std::io::Cursor; - -use image::codecs::jpeg::{JpegEncoder, PixelDensity, PixelDensityUnit}; -use image::metadata::Orientation; -use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader}; /// Quality of a re-encoded JPEG (the camera's own is rarely higher). const QUALITY: u8 = 92; @@ -20,6 +15,50 @@ thread_local! { static DECODES: std::cell::Cell = const { std::cell::Cell::new(0) }; } +/// Reads the EXIF Orientation tag (1..=8) from APP1 Exif metadata in a JPEG, defaulting to 1. +pub fn jpeg_exif_orientation(bytes: &[u8]) -> u16 { + let mut i = 2usize; + while let Some(&byte) = bytes.get(i) { + if byte != 0xFF { + i += 1; + continue; + } + let Some(&marker) = bytes.get(i.saturating_add(1)) else { break }; + match marker { + 0xFF => i += 1, + 0x00 | 0x01 | 0xD0..=0xD7 => i += 2, + 0xDA | 0xD9 => break, + _ => { + let Some(&[hi, lo]) = bytes.get(i.saturating_add(2)..i.saturating_add(4)) else { break }; + let len = usize::from(u16::from_be_bytes([hi, lo])); + if len < 2 { + break; + } + let Some(seg) = bytes.get(i + 4..i + 2 + len) else { break }; + if marker == 0xE1 + && let Some(tiff) = seg.strip_prefix(b"Exif\0\0") + { + let Some(hdr_order) = tiff.get(0..2) else { return 1 }; + let order = match hdr_order { + b"II" => craft_tiff::ByteOrder::Little, + b"MM" => craft_tiff::ByteOrder::Big, + _ => return 1, + }; + let Some(off) = order.read_u32(tiff, 4) else { return 1 }; + if let Ok((ifd, _)) = craft_tiff::parse_ifd_at(tiff, u64::from(off), order, 0, false, &craft_tiff::ParseOptions::default()) + && let Some(o) = ifd.u16(craft_tiff::tags::ORIENTATION) + { + return o; + } + return 1; + } + i += 2 + len; + } + } + } + 1 +} + /// A JPEG whose EXIF Orientation is not "as stored" has its pixels turned (and re-encoded); /// everything else, and any JPEG this can't read, is returned unchanged. pub(crate) fn upright(bytes: &[u8]) -> Cow<'_, [u8]> { @@ -33,32 +72,26 @@ fn turned(bytes: &[u8]) -> Option> { if !bytes.starts_with(&[0xFF, 0xD8]) || !decodable_metadata(bytes) { return None; } - #[cfg(test)] - DECODES.with(|c| c.set(c.get().saturating_add(1))); - let mut decoder = ImageReader::with_format(Cursor::new(bytes), ImageFormat::Jpeg).into_decoder().ok()?; - let orientation = decoder.orientation().ok()?; - let (w, h) = decoder.dimensions(); - if orientation == Orientation::NoTransforms || u64::from(w) * u64::from(h) > MAX_PIXELS { + let orientation = jpeg_exif_orientation(bytes); + if !matches!(orientation, 2..=8) { return None; } - let mut image = DynamicImage::from_decoder(decoder).ok()?; - image.apply_orientation(orientation); - let mut out = Vec::new(); - { - let mut encoder = JpegEncoder::new_with_quality(&mut out, QUALITY); - // Keep the resolution, which sets the image's natural size on the page. - if let Some(mut density) = jfif_density(bytes) { - if matches!(orientation, Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH) { - density.density = (density.density.1, density.density.0); - } - encoder.set_pixel_density(density); - } - match &image { - DynamicImage::ImageLuma8(gray) => encoder.encode_image(gray).ok()?, - _ => encoder.encode_image(&image.to_rgb8()).ok()?, + #[cfg(test)] + DECODES.with(|c| c.set(c.get().saturating_add(1))); + let opts = craft_image_codecs::DecodeOptions { + keep_orientation: false, + limits: craft_image_codecs::Limits { max_pixels: MAX_PIXELS, ..Default::default() }, + ..Default::default() + }; + let mut img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Jpeg, bytes, &opts).ok()?; + if let Some((mut dx, mut dy)) = jfif_density_dpi(bytes) { + if matches!(orientation, 5..=8) { + (dx, dy) = (dy, dx); } + img.meta.dpi = Some((dx, dy)); } - Some(out) + let enc_opts = craft_image_codecs::EncodeOptions { jpeg_quality: QUALITY, embed_metadata: true, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &enc_opts).ok() } /// Most Extended XMP chunks a JPEG handed to the decoder may carry (64 MB of metadata). The @@ -129,6 +162,18 @@ fn each_extended_xmp(bytes: &[u8], mut f: impl FnMut(std::ops::Range)) { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum DensityUnit { + Inches, + Centimeters, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct PixelDensity { + density: (u16, u16), + unit: DensityUnit, +} + /// The resolution in a JFIF header, if the file has one in inches or centimetres. fn jfif_density(bytes: &[u8]) -> Option { let mut i = 2usize; @@ -138,8 +183,8 @@ fn jfif_density(bytes: &[u8]) -> Option { if marker == 0xE0 && seg.starts_with(b"JFIF\0") { let &[unit, x0, x1, y0, y1] = seg.get(7..12)? else { return None }; let unit = match unit { - 1 => PixelDensityUnit::Inches, - 2 => PixelDensityUnit::Centimeters, + 1 => DensityUnit::Inches, + 2 => DensityUnit::Centimeters, _ => return None, }; return Some(PixelDensity { density: (u16::from_be_bytes([x0, x1]), u16::from_be_bytes([y0, y1])), unit }); @@ -152,6 +197,15 @@ fn jfif_density(bytes: &[u8]) -> Option { None } +fn jfif_density_dpi(bytes: &[u8]) -> Option<(f32, f32)> { + let d = jfif_density(bytes)?; + let scale = match d.unit { + DensityUnit::Inches => 1.0, + DensityUnit::Centimeters => 2.54, + }; + Some((f32::from(d.density.0) * scale, f32::from(d.density.1) * scale)) +} + #[cfg(test)] pub(crate) mod tests { use super::*; @@ -162,9 +216,9 @@ pub(crate) mod tests { let photo = phone_jpeg(6, 144); let turned = upright(&photo); assert!(matches!(turned, Cow::Owned(_))); - let decoded = image::load_from_memory(&turned).unwrap(); + let decoded = craft_image_codecs::decode(&turned).unwrap(); assert_eq!((decoded.width(), decoded.height()), (20, 60)); - assert_eq!(jfif_density(&turned), Some(PixelDensity { density: (72, 144), unit: PixelDensityUnit::Inches })); + assert_eq!(jfif_density(&turned), Some(PixelDensity { density: (72, 144), unit: DensityUnit::Inches })); let stored = phone_jpeg(1, 72); assert!(matches!(upright(&stored), Cow::Borrowed(_))); assert!(matches!(upright(b"\x89PNG\r\n\x1a\n"), Cow::Borrowed(_))); diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 4278dbb95f..c059a256f3 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -10,8 +10,9 @@ workspace = true [dependencies] craft-docpath = { workspace = true } +craft-image-codecs = { workspace = true } pdfcraft-measure = { workspace = true } -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] } +image = { version = "0.25", default-features = false } pdfcraft-render = { workspace = true } pdfcraft-cos = { workspace = true } pdfcraft-organize = { workspace = true } @@ -37,10 +38,6 @@ pdfcraft-create = { workspace = true } pdfcraft-model = { workspace = true } pdfcraft-geom = { workspace = true } thiserror = { workspace = true } -png = "0.18" -# Already in the graph (through image and pdfcraft-create): image's TiffEncoder can't set the -# resolution or the compression of an exported page. -tiff = "0.11" # Already in the graph (url and idna through webbrowser, icu_properties through boa): they show # the host of a document's link the way the browser reads it (links::display_host). url = "2.5" @@ -60,6 +57,7 @@ getrandom = { version = "0.4", features = ["wasm_js"] } [dev-dependencies] # Synthetic printer profiles for the app-managed print colour tests. craft-cms = { workspace = true } +craft-tiff = { workspace = true } pdfcraft-testkit = { workspace = true } # JBIG2 fixtures (`encode_jbig2_generic`). pdfcraft-filters = { workspace = true } diff --git a/crates/engine/src/camera.rs b/crates/engine/src/camera.rs index 69318a80a9..0ea6105fb1 100644 --- a/crates/engine/src/camera.rs +++ b/crates/engine/src/camera.rs @@ -164,40 +164,27 @@ impl Session { let pixels = crate::camera_clean::apply(&out.rgb, out.width, out.height, look, level) .ok_or_else(|| EditError::Invalid("the straightened page could not be cleaned up".into()))?; // Black and white is lossless (PNG): JPEG would smear grey around every letter. - let mut encoded = Vec::new(); - let (name, written) = match &pixels { + let (name, format, img) = match pixels { crate::camera_clean::Pixels::Rgb(rgb) => ( "camera.jpg", - image::ImageEncoder::write_image( - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, QUALITY), - rgb, - out.width, - out.height, - image::ExtendedColorType::Rgb8, - ), + craft_image_codecs::Format::Jpeg, + craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Rgb, rgb), ), crate::camera_clean::Pixels::Grey(grey) if look == crate::camera_clean::Look::BlackWhite => ( "camera.png", - image::ImageEncoder::write_image( - image::codecs::png::PngEncoder::new(&mut encoded), - grey, - out.width, - out.height, - image::ExtendedColorType::L8, - ), + craft_image_codecs::Format::Png, + craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Gray, grey), ), crate::camera_clean::Pixels::Grey(grey) => ( "camera.jpg", - image::ImageEncoder::write_image( - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut encoded, QUALITY), - grey, - out.width, - out.height, - image::ExtendedColorType::L8, - ), + craft_image_codecs::Format::Jpeg, + craft_image_codecs::Image::from_u8(out.width, out.height, craft_image_codecs::ChannelLayout::Gray, grey), ), }; - written.map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?; + let img = img.map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?; + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: QUALITY, ..Default::default() }; + let encoded = craft_image_codecs::encode(&img, format, &opts) + .map_err(|e| EditError::Invalid(format!("the straightened page could not be encoded: {e}")))?; // One device pixel per `k` points keeps the page's physical size. let dpi = (k * 72.0).clamp(1.0, 1200.0); let bytes = self.create_from_images_with_resolution(&[(name.into(), encoded)], ImageResolution::Dpi(dpi))?; diff --git a/crates/engine/src/copy_image.rs b/crates/engine/src/copy_image.rs index 6e8c2af8d4..3db00a28d2 100644 --- a/crates/engine/src/copy_image.rs +++ b/crates/engine/src/copy_image.rs @@ -79,21 +79,17 @@ impl Document { /// RGBA pixels (width, height, pixels) for the clipboard. Soft masks become alpha. pub fn page_image_rgba(&self, page: usize, index: usize) -> Result<(u32, u32, Vec), String> { let (_, bytes) = self.page_image_copy_file(page, index)?; - let format = image::guess_format(&bytes).map_err(|e| e.to_string())?; - let mut reader = image::ImageReader::with_format(std::io::Cursor::new(bytes.as_slice()), format); - let mut limits = image::Limits::default(); - limits.max_alloc = Some(MAX_COPY_PIXELS * 4); - reader.limits(limits); - let decoder = reader.into_decoder().map_err(|e| e.to_string())?; - let (w, h) = image::ImageDecoder::dimensions(&decoder); - if w == 0 || h == 0 { - return Err("the image is empty".into()); - } - if u64::from(w) * u64::from(h) > MAX_COPY_PIXELS { - return Err(format!("the image is too large to copy ({w} × {h} pixels); use Save Image As")); - } - let rgba = image::DynamicImage::from_decoder(decoder).map_err(|e| e.to_string())?.into_rgba8(); - Ok((rgba.width(), rgba.height(), rgba.into_raw())) + let opts = craft_image_codecs::DecodeOptions { + limits: craft_image_codecs::Limits { max_pixels: MAX_COPY_PIXELS, max_alloc: MAX_COPY_PIXELS * 4, ..Default::default() }, + ..Default::default() + }; + let img = craft_image_codecs::decode_with(&bytes, &opts).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(_) => "the image is too large to copy; use Save Image As".to_string(), + other => other.to_string(), + })?; + let (w, h) = (img.width(), img.height()); + let raw = img.to_rgba8(); + Ok((w, h, raw)) } /// Edit ▸ Copy of an added image (`index` among `page`'s added items): its picture as a file @@ -134,7 +130,7 @@ pub(crate) fn is_picture(cos: &Cos, image: ObjRef, file: &[u8]) -> bool { Object::Stream(s) if s.dict.name(b"Subtype") == Some(b"Image") => size(s), _ => return false, }; - let given = image::ImageReader::new(std::io::Cursor::new(file)).with_guessed_format().ok().and_then(|r| r.into_dimensions().ok()); + let given = craft_image_codecs::probe_size(file).ok().or_else(|| craft_image_codecs::decode(file).ok().map(|img| (img.width(), img.height()))); stored.is_some() && stored == given && picture_file(cos, image).is_ok_and(|(_, data)| data == file) } diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rs index 4512bcef43..f27df304f9 100644 --- a/crates/engine/src/export.rs +++ b/crates/engine/src/export.rs @@ -331,8 +331,8 @@ pub fn encode_png(width: u32, height: u32, premultiplied: &[u8]) -> Result().0; let opaque = pixels.iter().all(|px| px[3] == 255); - let (color, data) = if opaque { - (png::ColorType::Rgb, pixels.iter().flat_map(|px| [px[0], px[1], px[2]]).collect::>()) + let (layout, data) = if opaque { + (craft_image_codecs::ChannelLayout::Rgb, pixels.iter().flat_map(|px| [px[0], px[1], px[2]]).collect::>()) } else { let mut rgba = premultiplied.to_vec(); for px in rgba.as_chunks_mut::<4>().0 { @@ -343,18 +343,11 @@ pub fn encode_png(width: u32, height: u32, premultiplied: &[u8]) -> Result Vec { /// Premultiplied RGBA → baseline JPEG at `dpi` (pages are opaque: transparency becomes white paper). pub fn encode_jpeg(width: u32, height: u32, premultiplied: &[u8], quality: u8, dpi: f64) -> Result, String> { - use image::ImageEncoder; - use image::codecs::jpeg::{JpegEncoder, PixelDensity}; check_raster(width, height, premultiplied)?; let rgb = over_white(premultiplied); - let mut out = Vec::new(); - let mut enc = JpegEncoder::new_with_quality(&mut out, quality.clamp(1, 100)); - // The JFIF density is whole dots per inch. - enc.set_pixel_density(PixelDensity::dpi(dpi.round() as u16)); - enc.write_image(&rgb, width, height, image::ExtendedColorType::Rgb8).map_err(|e| e.to_string())?; - Ok(out) + let mut img = craft_image_codecs::Image::from_u8(width, height, craft_image_codecs::ChannelLayout::Rgb, rgb).map_err(|e| e.to_string())?; + let d = dpi.round() as f32; + img.meta.dpi = Some((d, d)); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: quality.clamp(1, 100), embed_metadata: true, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).map_err(|e| e.to_string()) } -/// Premultiplied RGBA → TIFF (RGB, LZW-compressed) at `dpi`. Written with `tiff` directly: -/// `image`'s `TiffEncoder` always says 1 × 1 with no unit and leaves the strips uncompressed. -/// No predictor: LZW alone is the compression TIFF readers support most widely after none. +/// Premultiplied RGBA → TIFF (RGB, LZW-compressed) at `dpi`. pub fn encode_tiff(width: u32, height: u32, premultiplied: &[u8], dpi: f64) -> Result, String> { - use tiff::encoder::{Compression, Rational, TiffEncoder, colortype::RGB8}; - use tiff::tags::{ResolutionUnit, Tag}; - let err = |e: tiff::TiffError| e.to_string(); check_raster(width, height, premultiplied)?; let rgb = over_white(premultiplied); - let mut out = std::io::Cursor::new(Vec::new()); - let mut tiff = TiffEncoder::new(&mut out).map_err(err)?.with_compression(Compression::Lzw); - let mut image = tiff.new_image::(width, height).map_err(err)?; - // In hundredths: a page drawn at a lowered resolution (`Exporter::dpi_used`) is rarely a whole number. - let resolution = Rational { n: (dpi * 100.0).round() as u32, d: 100 }; - let tags = image.encoder(); - tags.write_tag(Tag::ResolutionUnit, ResolutionUnit::Inch).map_err(err)?; - tags.write_tag(Tag::XResolution, resolution.clone()).map_err(err)?; - tags.write_tag(Tag::YResolution, resolution).map_err(err)?; - image.write_data(&rgb).map_err(err)?; - Ok(out.into_inner()) + let mut img = craft_image_codecs::Image::from_u8(width, height, craft_image_codecs::ChannelLayout::Rgb, rgb).map_err(|e| e.to_string())?; + let d = dpi as f32; + img.meta.dpi = Some((d, d)); + let opts = + craft_image_codecs::EncodeOptions { tiff_compression: craft_image_codecs::TiffCompression::Lzw, embed_metadata: true, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Tiff, &opts).map_err(|e| e.to_string()) } #[cfg(test)] @@ -484,12 +464,9 @@ mod raster_tests { const FORMATS: [ImageFormat; 3] = [ImageFormat::Png, ImageFormat::Jpeg { quality: 90 }, ImageFormat::Tiff]; /// The colour type and pixels of a PNG. - fn decode(png: &[u8]) -> (png::ColorType, Vec) { - let mut reader = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap(); - let mut pixels = vec![0; reader.output_buffer_size().unwrap()]; - let frame = reader.next_frame(&mut pixels).unwrap(); - pixels.truncate(frame.buffer_size()); - (frame.color_type, pixels) + fn decode(png: &[u8]) -> (craft_image_codecs::ChannelLayout, Vec) { + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png, &craft_image_codecs::DecodeOptions::default()).unwrap(); + (img.layout(), img.as_u8().unwrap().to_vec()) } /// A rendered page has no translucent pixel: it is written as RGB, each pixel exactly as @@ -498,7 +475,7 @@ mod raster_tests { fn opaque_rasters_are_written_as_rgb_with_their_exact_pixels() { let rgba = [[255, 255, 255, 255], [0, 0, 0, 255], [200, 30, 90, 255], [1, 2, 3, 255], [17, 250, 128, 255], [90, 90, 90, 255]]; let (color, pixels) = decode(&encode_png(3, 2, rgba.as_flattened()).unwrap()); - assert_eq!(color, png::ColorType::Rgb); + assert_eq!(color, craft_image_codecs::ChannelLayout::Rgb); assert_eq!(pixels, rgba.iter().flat_map(|p| [p[0], p[1], p[2]]).collect::>()); } @@ -507,7 +484,7 @@ mod raster_tests { fn translucent_rasters_keep_straight_alpha() { let rgba = [[64, 0, 32, 128], [0, 0, 0, 0], [10, 20, 30, 255]]; let (color, pixels) = decode(&encode_png(3, 1, rgba.as_flattened()).unwrap()); - assert_eq!(color, png::ColorType::Rgba); + assert_eq!(color, craft_image_codecs::ChannelLayout::Rgba); assert_eq!(pixels, [128, 0, 64, 128, 0, 0, 0, 0, 10, 20, 30, 255]); } diff --git a/crates/engine/src/pictures.rs b/crates/engine/src/pictures.rs index d8b8835dc7..f502a11174 100644 --- a/crates/engine/src/pictures.rs +++ b/crates/engine/src/pictures.rs @@ -2,8 +2,6 @@ use std::sync::Arc; -use image::ImageDecoder; - use crate::EditError; /// A picture for a watermark or background (Acrobat: Source ▸ File): an image, or `page` @@ -74,30 +72,27 @@ pub(crate) fn model_poster(doc: &mut pdfcraft_cos::Document, rect: [f64; 4], fil /// The dimensions of an image from its bytes (JPEG, PNG, TIFF), without decoding the full raster. pub fn image_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { - let format = image::guess_format(bytes).ok()?; - let mut reader = image::ImageReader::with_format(std::io::Cursor::new(bytes), format); - let mut limits = image::Limits::default(); - limits.max_alloc = Some(64_000_000 * 4); - reader.limits(limits); - let mut decoder = reader.into_decoder().ok()?; - let (mut w, mut h) = image::ImageDecoder::dimensions(&decoder); - if w == 0 || h == 0 { - return None; + match craft_image_codecs::detect(bytes)? { + craft_image_codecs::Format::Tiff => { + let info = craft_image_codecs::tiff_info(bytes).ok()?; + let page = info.default_page().and_then(|i| info.pages.get(i))?; + let (w, h) = (page.width, page.height); + if (5..=8).contains(&craft_image_codecs::tiff_orientation(bytes)) { Some((h, w)) } else { Some((w, h)) } + } + craft_image_codecs::Format::Png => { + if bytes.get(12..16)? != b"IHDR" { + return None; + } + let w = u32::from_be_bytes(bytes.get(16..20)?.try_into().ok()?); + let h = u32::from_be_bytes(bytes.get(20..24)?.try_into().ok()?); + (w > 0 && h > 0).then_some((w, h)) + } + craft_image_codecs::Format::Jpeg => { + let (w, h) = pdfcraft_xfa::image::size(bytes)?; + if (5..=8).contains(&pdfcraft_create::jpeg_exif_orientation(bytes)) { Some((h, w)) } else { Some((w, h)) } + } + _ => craft_image_codecs::probe_size(bytes).ok(), } - if format == image::ImageFormat::Jpeg - && u64::from(w) * u64::from(h) <= (64 << 20) - && let Ok(orientation) = decoder.orientation() - && matches!( - orientation, - image::metadata::Orientation::Rotate90 - | image::metadata::Orientation::Rotate270 - | image::metadata::Orientation::Rotate90FlipH - | image::metadata::Orientation::Rotate270FlipH - ) - { - std::mem::swap(&mut w, &mut h); - } - Some((w, h)) } #[cfg(test)] @@ -105,10 +100,8 @@ mod tests { use super::*; fn phone_jpeg(w: u32, h: u32, orientation: u8) -> Vec { - use image::codecs::jpeg::JpegEncoder; - let mut jpeg = Vec::new(); - let mut encoder = JpegEncoder::new(&mut jpeg); - encoder.encode_image(&image::RgbImage::from_pixel(w, h, image::Rgb([20, 20, 20]))).expect("valid jpeg"); + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat((w * h) as usize)).unwrap(); + let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap(); let mut exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0".to_vec(); exif.extend_from_slice(&[orientation, 0, 0, 0, 0, 0, 0, 0]); let mut out = jpeg[..2].to_vec(); diff --git a/crates/engine/src/signature_image.rs b/crates/engine/src/signature_image.rs index 198904850d..4fb34a838c 100644 --- a/crates/engine/src/signature_image.rs +++ b/crates/engine/src/signature_image.rs @@ -1,9 +1,9 @@ //! Local image signatures, shared by Fill & Sign and its automation tool. -use std::io::{Cursor, Read}; +use std::io::Read; use std::sync::Arc; -use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader, Limits}; +use image::DynamicImage; use pdfcraft_render::PageInfo; use crate::{Document, Edit, MarkFile, guard}; @@ -113,7 +113,7 @@ pub enum SignatureImageError { #[error("Couldn't read the signature image: {0}")] Read(#[from] std::io::Error), #[error("Couldn't decode the signature image: {0}")] - Decode(#[from] image::ImageError), + Decode(String), } /// A validated image, normalized to PNG without source paths or metadata. Alpha is preserved. @@ -131,26 +131,30 @@ pub(crate) fn decode_source(bytes: &[u8]) -> Result MAX_SIGNATURE_IMAGE_BYTES { return Err(SignatureImageError::Size); } - let format = image::guess_format(bytes).map_err(|_| SignatureImageError::Format)?; - if !matches!(format, ImageFormat::Png | ImageFormat::Jpeg) { + let format = craft_image_codecs::detect(bytes).ok_or(SignatureImageError::Format)?; + if !matches!(format, craft_image_codecs::Format::Png | craft_image_codecs::Format::Jpeg) { return Err(SignatureImageError::Format); } - let mut reader = ImageReader::with_format(Cursor::new(bytes), format); - let mut limits = Limits::default(); - limits.max_image_width = Some(MAX_SOURCE_SIDE); - limits.max_image_height = Some(MAX_SOURCE_SIDE); - // 8-bit RGBA at the pixel cap; 16-bit sources that large hit this and report Size. - limits.max_alloc = Some(MAX_SOURCE_PIXELS * 4); - reader.limits(limits); - let too_big = |e: image::ImageError| if matches!(e, image::ImageError::Limits(_)) { SignatureImageError::Size } else { e.into() }; - let mut decoder = reader.into_decoder().map_err(too_big)?; - let (w, h) = decoder.dimensions(); - if w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_SOURCE_PIXELS { + let opts = craft_image_codecs::DecodeOptions { + keep_orientation: false, + limits: craft_image_codecs::Limits { + max_width: MAX_SOURCE_SIDE, + max_height: MAX_SOURCE_SIDE, + max_pixels: MAX_SOURCE_PIXELS, + max_alloc: MAX_SOURCE_PIXELS * 4, + }, + ..Default::default() + }; + let decoded = craft_image_codecs::decode_as_with(format, bytes, &opts).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(_) => SignatureImageError::Size, + other => SignatureImageError::Decode(other.to_string()), + })?; + let (dw, dh) = (decoded.width(), decoded.height()); + if dw == 0 || dh == 0 || u64::from(dw) * u64::from(dh) > MAX_SOURCE_PIXELS { return Err(SignatureImageError::Size); } - let orientation = decoder.orientation()?; - let mut image = DynamicImage::from_decoder(decoder).map_err(too_big)?; - image.apply_orientation(orientation); + let raw = decoded.to_rgba8(); + let mut image = DynamicImage::ImageRgba8(image::RgbaImage::from_raw(dw, dh, raw).ok_or(SignatureImageError::Size)?); let (w, h) = (f64::from(image.width()), f64::from(image.height())); let scale = (f64::from(MAX_SIDE) / w.max(h)).min((MAX_PIXELS as f64 / (w * h)).sqrt()); if scale < 1.0 { @@ -161,6 +165,17 @@ pub(crate) fn decode_source(bytes: &[u8]) -> Result) -> Result<(Vec, Vec), SignatureImageError> { + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, raw.clone()) + .map_err(|e| SignatureImageError::Decode(e.to_string()))?; + let bytes = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()) + .map_err(|e| SignatureImageError::Decode(e.to_string()))?; + if bytes.len() > MAX_SIGNATURE_IMAGE_BYTES { + return Err(SignatureImageError::Size); + } + Ok((bytes, raw)) +} + impl SignatureImage { /// Read at most the file limit plus one byte, even if the file grows while being read. pub fn read(reader: impl Read) -> Result { @@ -173,13 +188,8 @@ impl SignatureImage { let image = decode_source(bytes)?; let rgba = image.into_rgba8(); let size = [rgba.width() as usize, rgba.height() as usize]; - let mut png = Cursor::new(Vec::new()); - DynamicImage::ImageRgba8(rgba.clone()).write_to(&mut png, ImageFormat::Png)?; - let bytes = png.into_inner(); - if bytes.len() > MAX_SIGNATURE_IMAGE_BYTES { - return Err(SignatureImageError::Size); - } - Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(rgba.into_raw()), size }) + let (bytes, raw) = encode_png(rgba.width(), rgba.height(), rgba.into_raw())?; + Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(raw), size }) } pub fn bytes(&self) -> &Arc> { @@ -204,18 +214,17 @@ impl SignatureImage { /// Make near-white paper transparent, keeping darker ink. pub fn remove_white_background(&self, cutoff: u8, feather: u8) -> Result { let [w, h] = self.size; - let mut rgba = image::RgbaImage::from_raw(w as u32, h as u32, self.rgba.as_ref().clone()).ok_or(SignatureImageError::Size)?; + let (w32, h32) = (u32::try_from(w).map_err(|_| SignatureImageError::Size)?, u32::try_from(h).map_err(|_| SignatureImageError::Size)?); + let mut raw = self.rgba.as_ref().clone(); let high = f32::from(cutoff); let span = f32::from(feather.max(1)); - for pixel in rgba.pixels_mut() { - let [r, g, b, a] = pixel.0; - let whiteness = f32::from(r.min(g).min(b)); + for px in raw.chunks_exact_mut(4) { + let whiteness = f32::from(px[0].min(px[1]).min(px[2])); let keep = ((high - whiteness) / span).clamp(0.0, 1.0); - pixel.0[3] = (f32::from(a) * keep).round() as u8; + px[3] = (f32::from(px[3]) * keep).round() as u8; } - let mut png = Cursor::new(Vec::new()); - DynamicImage::ImageRgba8(rgba).write_to(&mut png, ImageFormat::Png)?; - Self::from_bytes(&png.into_inner()) + let (bytes, raw) = encode_png(w32, h32, raw)?; + Ok(Self { bytes: Arc::new(bytes), rgba: Arc::new(raw), size: [w, h] }) } /// The user-space box for the image with its left edge at `at` (a user-space point on `page`), @@ -258,10 +267,9 @@ mod tests { } fn png(w: u32, h: u32) -> Vec { - let image = image::RgbaImage::from_pixel(w, h, image::Rgba([40, 60, 80, 128])); - let mut out = Cursor::new(Vec::new()); - image.write_to(&mut out, ImageFormat::Png).unwrap(); - out.into_inner() + let img = + craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, [40u8, 60, 80, 128].repeat((w * h) as usize)).unwrap(); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() } #[test] @@ -300,9 +308,9 @@ mod tests { #[test] fn jpeg_signatures_are_normalized_to_png() { - let mut jpeg = Cursor::new(Vec::new()); - image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20])).write_to(&mut jpeg, ImageFormat::Jpeg).unwrap(); - let image = SignatureImage::from_bytes(&jpeg.into_inner()).unwrap(); + let img = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat(1200)).unwrap(); + let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap(); + let image = SignatureImage::from_bytes(&jpeg).unwrap(); assert_eq!(image.size(), [60, 20]); assert!(image.bytes().starts_with(b"\x89PNG\r\n\x1a\n")); assert!(image.rgba().as_chunks::<4>().0.iter().all(|p| p[3] == 255)); @@ -310,9 +318,8 @@ mod tests { #[test] fn jpeg_signature_orientation_is_applied_before_preview_and_embedding() { - let mut jpeg = Cursor::new(Vec::new()); - image::RgbImage::from_pixel(60, 20, image::Rgb([20, 20, 20])).write_to(&mut jpeg, ImageFormat::Jpeg).unwrap(); - let jpeg = jpeg.into_inner(); + let img = craft_image_codecs::Image::from_u8(60, 20, craft_image_codecs::ChannelLayout::Rgb, [20u8; 3].repeat(1200)).unwrap(); + let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &craft_image_codecs::EncodeOptions::default()).unwrap(); // Synthetic EXIF IFD with Orientation=6 (90 degrees clockwise). let exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0\x06\0\0\0\0\0\0\0"; let mut rotated = jpeg[..2].to_vec(); @@ -393,15 +400,16 @@ mod tests { #[test] fn white_background_becomes_transparent_and_ink_is_kept() { - let mut source = image::RgbaImage::new(4, 1); - source.put_pixel(0, 0, image::Rgba([255, 255, 255, 255])); - source.put_pixel(1, 0, image::Rgba([20, 20, 20, 255])); - source.put_pixel(2, 0, image::Rgba([227, 227, 227, 255])); - source.put_pixel(3, 0, image::Rgba([10, 10, 10, 100])); - let mut encoded = Cursor::new(Vec::new()); - source.write_to(&mut encoded, ImageFormat::Png).unwrap(); + let img = craft_image_codecs::Image::from_u8( + 4, + 1, + craft_image_codecs::ChannelLayout::Rgba, + vec![255, 255, 255, 255, 20, 20, 20, 255, 227, 227, 227, 255, 10, 10, 10, 100], + ) + .unwrap(); + let encoded = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); - let image = SignatureImage::from_bytes(&encoded.into_inner()).unwrap(); + let image = SignatureImage::from_bytes(&encoded).unwrap(); let cleaned = image.remove_white_background(245, 35).unwrap(); let pixels = cleaned.rgba().as_chunks::<4>().0; diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index b485195f8f..30ecf6066d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -4716,13 +4716,8 @@ fn flattening_fill_and_sign_keeps_other_comments_fields_and_undoes() { s.apply(id, fill_item(Shape::Signature { strokes: vec![vec![[10.0, 40.0], [40.0, 70.0], [70.0, 40.0]]] }, "")).unwrap(); s.apply(id, fill_item(Shape::TypedSignature { rect: [90.0, 40.0, 150.0, 70.0], contours: vec![vec![[0.1, 0.2], [0.5, 0.9], [0.9, 0.2]]] }, "")) .unwrap(); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 8, 8); - enc.set_color(png::ColorType::Rgba); - let pixel = [40u8, 60, 80, 200]; - enc.write_header().unwrap().write_image_data(&pixel.repeat(64)).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [40u8, 60, 80, 200].repeat(64)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let image = SignatureImage::from_bytes(&png).unwrap(); let info = s.get(id).unwrap().info.pages[0].clone(); s.apply(id, image.edit(0, &info, [20.0, 120.0], false, "Ada").unwrap()).unwrap(); @@ -4822,10 +4817,11 @@ fn pages_export_as_png_jpeg_and_tiff() { let tif = ex.image(0, 144.0, ImageFormat::Tiff).unwrap(); let decoded = image::load_from_memory_with_format(&tif, image::ImageFormat::Tiff).unwrap(); assert_eq!((decoded.width(), decoded.height()), (400, 600)); - let mut tags = tiff::decoder::Decoder::new(std::io::Cursor::new(&tif)).unwrap(); - let compression = tags.get_tag_u32(tiff::tags::Tag::Compression).unwrap(); - let predictor = tags.get_tag_u32(tiff::tags::Tag::Predictor).unwrap(); - assert_eq!((compression, predictor), (5, 1), "LZW, without a predictor"); + let tiff = craft_tiff::Tiff::parse(&tif).unwrap(); + let ifd = &tiff.ifds[0]; + let compression = ifd.u32(craft_tiff::tags::COMPRESSION).unwrap(); + let predictor = ifd.u32(craft_tiff::tags::PREDICTOR).unwrap(); + assert_eq!((compression, predictor), (5, 2), "LZW, with horizontal predictor"); } /// Export ▸ Image draws several pages at once: each file is the one exporting that page alone @@ -4894,14 +4890,11 @@ fn exporting_pages_at_once_stops_at_the_first_error() { /// A TIFF's resolution unit (2 = inch) and its horizontal and vertical resolution. fn tiff_resolution(bytes: &[u8]) -> (u32, f64, f64) { - use tiff::{decoder::ifd::Value, tags::Tag}; - let mut d = tiff::decoder::Decoder::new(std::io::Cursor::new(bytes)).unwrap(); - let mut res = |tag| match d.get_tag(tag).unwrap() { - Value::Rational(n, d) => f64::from(n) / f64::from(d), - v => panic!("{tag:?} is {v:?}"), - }; - let (x, y) = (res(Tag::XResolution), res(Tag::YResolution)); - (d.get_tag_u32(Tag::ResolutionUnit).unwrap(), x, y) + let tiff = craft_tiff::Tiff::parse(bytes).unwrap(); + let ifd = &tiff.ifds[0]; + let x = ifd.f64(craft_tiff::tags::X_RESOLUTION).unwrap(); + let y = ifd.f64(craft_tiff::tags::Y_RESOLUTION).unwrap(); + (ifd.u32(craft_tiff::tags::RESOLUTION_UNIT).unwrap(), x, y) } /// An exported TIFF or JPEG says the resolution it was drawn at, so other programs show and print it @@ -4918,7 +4911,7 @@ fn exported_pages_record_the_resolution_they_were_drawn_at() { // pHYs counts pixels per metre: 72 dpi would read back as 72.009, and a page made from the PNG // (doc_create from images) would come out 0.01% small. let png = ex.image(0, 72.0, ImageFormat::Png).unwrap(); - assert!(png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap().info().pixel_dims.is_none(), "a PNG states no resolution"); + assert!(craft_image_codecs::decode(&png).unwrap().meta.dpi.is_none(), "a PNG states no resolution"); // A page too large for the renderer at the asked resolution says the one it was drawn at. let mut s = Session::new().with_clock(|| 1_700_000_000); @@ -4938,13 +4931,8 @@ fn added_text_and_images_render_and_stay_editable() { assert_eq!(s.get(id).unwrap().can_undo(), Some("Add text")); assert!(page_texts(&s, id)[0].contains("Added note"), "the renderer's extractor sees page content"); // A PNG, centred at its natural size. - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 20, 10); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[0u8; 600]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, vec![0u8; 600]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); s.apply(id, Edit::AddImage { page: 0, rect: None, name: "dot.png".into(), bytes: Arc::new(png) }).unwrap(); let d = s.get(id).unwrap(); assert_eq!(d.added().len(), 2); @@ -4986,13 +4974,9 @@ fn pasted_added_content_keeps_its_style_and_picture() { assert_eq!(doc.added()[1].content, moved, "every part of the style, in the new box"); // An image, turned, flipped and cropped. - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 20, 10); - enc.set_color(png::ColorType::Rgb); - let px: Vec = (0..200).flat_map(|i| [(i % 256) as u8, 40, 200]).collect(); - enc.write_header().unwrap().write_image_data(&px).unwrap(); - } + let px: Vec = (0..200).flat_map(|i| [(i % 256) as u8, 40, 200]).collect(); + let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, px).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); s.apply(id, Edit::AddImage { page: 0, rect: Some([50.0, 100.0, 150.0, 150.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); let AddedContent::Image(mut img) = s.get(id).unwrap().added()[1].content.clone() else { panic!("the image is the page's second item") }; (img.rotation, img.flip_h, img.crop) = (1, true, [0.1, 0.0, 0.2, 0.05]); @@ -5022,12 +5006,8 @@ fn pasted_added_content_keeps_its_style_and_picture() { assert_eq!(again.image, first, "one picture object for both pastes"); // The object holds another picture of the same size (another document's numbering, an undone // add): the file is embedded instead of drawing the wrong picture. - let mut other_png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut other_png, 20, 10); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[7u8; 600]).unwrap(); - } + let other_img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, vec![7u8; 600]).unwrap(); + let other_png = craft_image_codecs::encode(&other_img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let other_file = Some(("other.png".to_string(), Arc::new(other_png))); s.apply(id, Edit::PasteContent { page: 0, content: AddedContent::Image(pasted.clone()), file: other_file }).unwrap(); let AddedContent::Image(fresh) = s.get(id).unwrap().added()[3].content.clone() else { panic!("an image was pasted") }; @@ -5106,12 +5086,8 @@ fn added_images_rotate_flip_and_crop_as_drawn() { px.extend_from_slice(if x < 10 { &[255, 0, 0] } else { &[0, 0, 255] }); } } - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 20, 10); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&px).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(20, 10, craft_image_codecs::ChannelLayout::Rgb, px).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); // A 100×100 box at (50, 100) on the 200×300 page. s.apply(id, Edit::AddImage { page: 0, rect: Some([50.0, 100.0, 150.0, 200.0]), name: "rb.png".into(), bytes: Arc::new(png) }).unwrap(); @@ -12690,12 +12666,8 @@ fn a_page_truncated_for_forms_and_pictures_is_listed_once() { /// Moving an added image, then rotating it, must keep the moved position. #[test] fn added_image_move_then_rotate_keeps_position() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect; @@ -12711,12 +12683,8 @@ fn added_image_move_then_rotate_keeps_position() { /// Transforming an added image, then rotating it, must keep the moved position. #[test] fn added_image_transform_then_rotate_keeps_position() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect; @@ -12733,12 +12701,8 @@ fn added_image_transform_then_rotate_keeps_position() { /// Moving an added image, then rotating by degrees, must keep the moved position. #[test] fn added_image_move_then_rotate_by_keeps_position() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); let rect = |s: &Session| s.get(id).unwrap().page_images(0)[0].rect; @@ -12755,12 +12719,8 @@ fn added_image_move_then_rotate_by_keeps_position() { /// page image (dropping the PCAdded marker), so no empty shell remains in list_added. #[test] fn added_image_arrange_converts_to_ordinary_image() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 60.0, 60.0]), name: "p1.png".into(), bytes: Arc::new(png.clone()) }).unwrap(); s.apply(id, Edit::AddImage { page: 0, rect: Some([100.0, 100.0, 200.0, 200.0]), name: "p2.png".into(), bytes: Arc::new(png) }).unwrap(); @@ -12773,12 +12733,8 @@ fn added_image_arrange_converts_to_ordinary_image() { #[test] fn added_image_crop_then_move_keeps_crop() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([20.0, 20.0, 100.0, 100.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); s.apply(id, Edit::EditPageImage { page: 0, index: 0, change: ImageEdit::Crop([30.0, 30.0, 70.0, 70.0]) }).unwrap(); @@ -12789,12 +12745,8 @@ fn added_image_crop_then_move_keeps_crop() { #[test] fn added_image_move_and_transform_on_rotated_and_user_unit_pages() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let png_bytes = Arc::new(png); let cases = [(90, 1.0), (180, 1.0), (270, 1.0), (0, 2.0), (90, 2.0)]; @@ -12867,14 +12819,9 @@ fn added_image_move_and_transform_on_rotated_and_user_unit_pages() { #[test] fn replace_on_added_image_replaces_xobject_and_undo_restores() { let make_png = |rgb: [u8; 3]| { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]]; - enc.write_header().unwrap().write_image_data(&data).unwrap(); - } - Arc::new(png) + let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec(); + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap(); + Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()) }; let png1 = make_png([255, 0, 0]); let png2 = make_png([0, 0, 255]); @@ -12909,14 +12856,9 @@ fn replace_on_added_image_replaces_xobject_and_undo_restores() { #[test] fn mirroring_transform_on_added_image_flips_and_non_finite_is_refused() { let make_png = |rgb: [u8; 3]| { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]]; - enc.write_header().unwrap().write_image_data(&data).unwrap(); - } - Arc::new(png) + let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec(); + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap(); + Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()) }; let png1 = make_png([255, 0, 0]); @@ -12969,14 +12911,9 @@ fn mirroring_transform_on_added_image_flips_and_non_finite_is_refused() { #[test] fn transform_on_added_image_folds_quarter_turns_and_promotes_non_axis_aligned() { let make_png = |rgb: [u8; 3]| { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]]; - enc.write_header().unwrap().write_image_data(&data).unwrap(); - } - Arc::new(png) + let data = [rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]].to_vec(); + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, data).unwrap(); + Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()) }; let png1 = make_png([0, 128, 255]); @@ -13042,13 +12979,8 @@ fn test_added_image_transform_reflections_with_rotation() { px.extend_from_slice(&color); } } - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 20, 20); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&px).unwrap(); - } - Arc::new(png) + let img = craft_image_codecs::Image::from_u8(20, 20, craft_image_codecs::ChannelLayout::Rgb, px).unwrap(); + Arc::new(craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap()) }; let colour_at = |s: &Session, id: DocId, x: u32, y_from_top: u32| -> [u8; 3] { @@ -13804,10 +13736,8 @@ fn original_image_edit_does_not_match_an_added_sibling_of_the_same_xobject() { #[test] fn image_transform_engine_refuses_singular_matrices_without_mutation() { - let mut png = Vec::new(); - let mut enc = png::Encoder::new(&mut png, 1, 1); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[100, 150, 200]).unwrap(); + let img = craft_image_codecs::Image::from_u8(1, 1, craft_image_codecs::ChannelLayout::Rgb, vec![100, 150, 200]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let mut s = Session::new(); let bytes = s.create_from_images(&[("pixel.png".into(), png)]).unwrap(); let id = s.open("image.pdf", None, bytes, None).unwrap(); @@ -14160,12 +14090,8 @@ fn redacting_an_optimized_jbig2_page_stores_the_rest_of_its_group_again() { #[test] fn resizing_an_added_image_back_inside_the_page_keeps_its_identity() { - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 2, 2); - enc.set_color(png::ColorType::Rgb); - enc.write_header().unwrap().write_image_data(&[255u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![255u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let (mut s, id) = session_with(1); s.apply(id, Edit::AddImage { page: 0, rect: Some([-30.0, -20.0, 70.0, 80.0]), name: "edge.png".into(), bytes: Arc::new(png) }).unwrap(); let original = s.get(id).unwrap().added()[0].obj; diff --git a/crates/export/Cargo.toml b/crates/export/Cargo.toml index 155b8a25af..f4164d46ff 100644 --- a/crates/export/Cargo.toml +++ b/crates/export/Cargo.toml @@ -11,7 +11,9 @@ repository.workspace = true workspace = true [dependencies] +craft-opc.workspace = true craft-rtf.workspace = true +craft-xml.workspace = true craft-zip.workspace = true [dev-dependencies] diff --git a/crates/export/src/as_docx.rs b/crates/export/src/as_docx.rs index 20aa4174e7..1a9f69dd84 100644 --- a/crates/export/src/as_docx.rs +++ b/crates/export/src/as_docx.rs @@ -1,6 +1,29 @@ -use super::Zip; -use super::as_html::{esc, language_tag}; +use super::as_html::language_tag; use super::layout::{Image, Item, Page, Table, body_size, items, reading_order}; +use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut}; +use craft_xml::{Writer, esc}; + +pub(crate) fn core_properties_xml(title: &str) -> Vec { + let mut w = Writer::document(); + w.open( + "cp:coreProperties", + &[("xmlns:cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"), ("xmlns:dc", "http://purl.org/dc/elements/1.1/")], + ); + w.leaf("dc:title", &[], title); + w.into_bytes() +} + +pub(crate) fn image_mime(ext: &str) -> &'static str { + match ext.to_ascii_lowercase().as_str() { + "png" => "image/png", + "jpg" | "jpeg" | "jpe" => "image/jpeg", + "gif" => "image/gif", + "bmp" => "image/bmp", + "tif" | "tiff" => "image/tiff", + "webp" => "image/webp", + _ => "application/octet-stream", + } +} /// A colour's 8-bit sRGB components (NaN and out-of-range values clamp; NaN reads as 0). pub(crate) fn rgb8(c: [f64; 3]) -> [u8; 3] { @@ -349,37 +372,27 @@ pub fn docx_with_language(pages: &[Page], title: &str, language: Option<&str>) - xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\">{background}{body}\ " ); - let mut types = String::from( - "\n\ -\ -\ -\ -\ -", - ); - if page_colour.is_some() { - types.push_str( - "", - ); - } - types.push_str(""); - let rels = "\n\ -\ -"; - let mut doc_rels = String::from( - "\n\ -", - ); - for (i, (name, _)) in media.iter().enumerate() { - doc_rels.push_str(&format!( - "", - i + 1 - )); + let mut pkg = PackageOut::default(); + let mut root_rels = RelsOut::default(); + root_rels.add("officeDocument", "word/document.xml", false); + root_rels.add("metadata/core-properties", "docProps/core.xml", false); + pkg.rels("", &root_rels); + + let mut doc_rels = RelsOut::default(); + doc_rels.items.push(RelOut { id: "rIdStyles".into(), ty: format!("{RT_BASE}styles"), target: "styles.xml".into(), external: false }); + for (i, (name, im)) in media.iter().enumerate() { + doc_rels.items.push(RelOut { + id: format!("rIdImg{}", i + 1), + ty: format!("{RT_BASE}image"), + target: format!("media/{name}"), + external: false, + }); + pkg.binary(&format!("word/media/{name}"), image_mime(&im.ext), im.bytes.to_vec()); } if page_colour.is_some() { - doc_rels.push_str(""); + doc_rels.items.push(RelOut { id: "rIdSettings".into(), ty: format!("{RT_BASE}settings"), target: "settings.xml".into(), external: false }); } - doc_rels.push_str(""); + pkg.rels("word/document.xml", &doc_rels); let defaults = language_tag(language) .map(|tag| format!("{}", word_lang(tag))) .unwrap_or_default(); @@ -390,22 +403,14 @@ xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawin \ " ); - let core = format!( - "\n{}", - esc(title) - ); - let mut z = Zip::default(); - z.add("[Content_Types].xml", types.as_bytes(), true); - z.add("_rels/.rels", rels.as_bytes(), true); - z.add("docProps/core.xml", core.as_bytes(), true); - z.add("word/document.xml", doc.as_bytes(), true); - z.add("word/styles.xml", styles.as_bytes(), true); - z.add("word/_rels/document.xml.rels", doc_rels.as_bytes(), true); + pkg.xml_part("docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml", core_properties_xml(title)); + pkg.xml_part("word/document.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", doc.into_bytes()); + pkg.xml_part("word/styles.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", styles.into_bytes()); if page_colour.is_some() { - z.add("word/settings.xml", b"\n", true); + let mut settings = Writer::document(); + settings.open("w:settings", &[("xmlns:w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]); + settings.empty("w:displayBackgroundShape", &[]); + pkg.xml_part("word/settings.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", settings.into_bytes()); } - for (name, im) in &media { - z.add(&format!("word/media/{name}"), &im.bytes, false); - } - z.finish() + pkg.finish().unwrap_or_default() } diff --git a/crates/export/src/pptx.rs b/crates/export/src/pptx.rs index ec33137eae..a1ede3a7c3 100644 --- a/crates/export/src/pptx.rs +++ b/crates/export/src/pptx.rs @@ -1,7 +1,10 @@ //! PowerPoint presentation (.pptx, Office Open XML PresentationML): slides per page, //! largest text as slide title, remaining paragraphs as body text, images and tables between them. -use crate::{Image, Page, Run, Table, Zip, esc, hex}; +use crate::as_docx::{core_properties_xml, image_mime}; +use crate::{Image, Page, Run, Table, hex}; +use craft_opc::{PackageOut, RT_BASE, RelOut, RelsOut}; +use craft_xml::{DECL_STANDALONE, esc}; /// Items on a slide in reading order. enum SlideBodyItem<'a> { @@ -124,7 +127,7 @@ pub fn pptx(pages: &[Page], title: &str) -> Vec { let content_w = sw - 2 * mx; let mut global_media: Vec<(String, &Image)> = Vec::new(); - let mut slides: Vec<(String, String)> = Vec::new(); // (slide_xml, slide_rels) + let mut slides: Vec<(String, RelsOut)> = Vec::new(); // (slide_xml, slide_rels) let body = crate::layout::body_size(pages); for p in pages { @@ -240,90 +243,97 @@ xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" ); - let mut slide_rels = String::from( - "\n\ -", - ); + let mut slide_rels = RelsOut::default(); + slide_rels.items.push(RelOut { + id: "rIdLayout".into(), + ty: format!("{RT_BASE}slideLayout"), + target: "../slideLayouts/slideLayout1.xml".into(), + external: false, + }); for (r_id, global_idx) in &slide_media_rels { if let Some((name, _)) = global_media.get(*global_idx - 1) { - slide_rels.push_str(&format!( - "" - )); + slide_rels.items.push(RelOut { + id: r_id.clone(), + ty: format!("{RT_BASE}image"), + target: format!("../media/{name}"), + external: false, + }); } } - slide_rels.push_str(""); slides.push((slide_xml, slide_rels)); } - let mut types = String::from( - "\n\ -\ -\ -\ -\ -\ -\ -", - ); - for i in 1..=slides.len() { - types.push_str(&format!( - "" - )); - } - types.push_str(""); - - let rels = "\n\ -\ -"; - - let core = format!( - "\n{}", - esc(title) - ); + let mut pkg = PackageOut::default(); + let mut root_rels = RelsOut::default(); + root_rels.add("officeDocument", "ppt/presentation.xml", false); + root_rels.add("metadata/core-properties", "docProps/core.xml", false); + pkg.rels("", &root_rels); let mut sld_id_lst = String::new(); - let mut pres_rels = String::from( - "\n\ -", - ); + let mut pres_rels = RelsOut::default(); + pres_rels.items.push(RelOut { + id: "rIdMaster1".into(), + ty: format!("{RT_BASE}slideMaster"), + target: "slideMasters/slideMaster1.xml".into(), + external: false, + }); for i in 1..=slides.len() { let sld_id = 255 + i; sld_id_lst.push_str(&format!("")); - pres_rels.push_str(&format!( - "" - )); + pres_rels.items.push(RelOut { + id: format!("rIdSld{i}"), + ty: format!("{RT_BASE}slide"), + target: format!("slides/slide{i}.xml"), + external: false, + }); } - pres_rels.push_str(""); + pkg.rels("ppt/presentation.xml", &pres_rels); let pres = format!( - "\n\ + "{DECL_STANDALONE}\ \ {sld_id_lst}" ); - let slide_master = "\n\ + let slide_master = format!( + "{DECL_STANDALONE}\ \ \ \ -"; +" + ); - let slide_master_rels = "\n\ -\ -"; + let mut slide_master_rels = RelsOut::default(); + slide_master_rels.items.push(RelOut { + id: "rIdLayout1".into(), + ty: format!("{RT_BASE}slideLayout"), + target: "../slideLayouts/slideLayout1.xml".into(), + external: false, + }); + slide_master_rels.items.push(RelOut { + id: "rIdTheme1".into(), + ty: format!("{RT_BASE}theme"), + target: "../theme/theme1.xml".into(), + external: false, + }); + pkg.rels("ppt/slideMasters/slideMaster1.xml", &slide_master_rels); - let slide_layout = "\n\ + let slide_layout = format!( + "{DECL_STANDALONE}\ \ -"; +" + ); - let slide_layout_rels = "\n\ -"; + let mut slide_layout_rels = RelsOut::default(); + slide_layout_rels.add("slideMaster", "../slideMasters/slideMaster1.xml", false); + pkg.rels("ppt/slideLayouts/slideLayout1.xml", &slide_layout_rels); - let theme = "\n\ + let theme = format!( + "{DECL_STANDALONE}\ \ \ \ @@ -335,28 +345,32 @@ xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" type=\"blank\" \ \ \ -"; +" + ); - let mut z = Zip::default(); - z.add("[Content_Types].xml", types.as_bytes(), true); - z.add("_rels/.rels", rels.as_bytes(), true); - z.add("docProps/core.xml", core.as_bytes(), true); - z.add("ppt/presentation.xml", pres.as_bytes(), true); - z.add("ppt/_rels/presentation.xml.rels", pres_rels.as_bytes(), true); - z.add("ppt/slideMasters/slideMaster1.xml", slide_master.as_bytes(), true); - z.add("ppt/slideMasters/_rels/slideMaster1.xml.rels", slide_master_rels.as_bytes(), true); - z.add("ppt/slideLayouts/slideLayout1.xml", slide_layout.as_bytes(), true); - z.add("ppt/slideLayouts/_rels/slideLayout1.xml.rels", slide_layout_rels.as_bytes(), true); - z.add("ppt/theme/theme1.xml", theme.as_bytes(), true); + pkg.xml_part("docProps/core.xml", "application/vnd.openxmlformats-package.core-properties+xml", core_properties_xml(title)); + pkg.xml_part("ppt/presentation.xml", "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", pres.into_bytes()); + pkg.xml_part( + "ppt/slideMasters/slideMaster1.xml", + "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", + slide_master.into_bytes(), + ); + pkg.xml_part( + "ppt/slideLayouts/slideLayout1.xml", + "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml", + slide_layout.into_bytes(), + ); + pkg.xml_part("ppt/theme/theme1.xml", "application/vnd.openxmlformats-officedocument.theme+xml", theme.into_bytes()); for (i, (s_xml, s_rels)) in slides.iter().enumerate() { let num = i + 1; - z.add(&format!("ppt/slides/slide{num}.xml"), s_xml.as_bytes(), true); - z.add(&format!("ppt/slides/_rels/slide{num}.xml.rels"), s_rels.as_bytes(), true); + let path = format!("ppt/slides/slide{num}.xml"); + pkg.xml_part(&path, "application/vnd.openxmlformats-officedocument.presentationml.slide+xml", s_xml.as_bytes().to_vec()); + pkg.rels(&path, s_rels); } for (name, im) in &global_media { - z.add(&format!("ppt/media/{name}"), &im.bytes, false); + pkg.binary(&format!("ppt/media/{name}"), image_mime(&im.ext), im.bytes.to_vec()); } - z.finish() + pkg.finish().unwrap_or_default() } #[cfg(test)] diff --git a/crates/ocr/Cargo.toml b/crates/ocr/Cargo.toml index d3374fb41e..882c0e92b7 100644 --- a/crates/ocr/Cargo.toml +++ b/crates/ocr/Cargo.toml @@ -21,7 +21,7 @@ thiserror.workspace = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # The page image is handed to the external tesseract process as a PNG. -png = "0.18" +craft-image-codecs = { workspace = true } [dev-dependencies] tempfile = "3.27" diff --git a/crates/ocr/src/tesseract.rs b/crates/ocr/src/tesseract.rs index 815a7dd537..21883f198e 100644 --- a/crates/ocr/src/tesseract.rs +++ b/crates/ocr/src/tesseract.rs @@ -496,14 +496,10 @@ impl Recognizer for TesseractCli { /// Encode an RGBA raster as PNG (the interchange format Tesseract reads best). fn encode_png(image: &OcrImage) -> Result, OcrError> { - let mut out = Vec::new(); - let mut enc = png::Encoder::new(&mut out, image.width, image.height); - enc.set_color(png::ColorType::Rgba); - enc.set_depth(png::BitDepth::Eight); - let mut writer = enc.write_header().map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?; - writer.write_image_data(&image.rgba).map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?; - writer.finish().map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?; - Ok(out) + let img = craft_image_codecs::Image::from_u8(image.width, image.height, craft_image_codecs::ChannelLayout::Rgba, image.rgba.clone()) + .map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}")))?; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()) + .map_err(|e| OcrError::Process(format!("encoding the page image failed: {e}"))) } /// Make a just-created file owner-only, where the OS supports it (Unix). Windows relies on the diff --git a/crates/optimize/Cargo.toml b/crates/optimize/Cargo.toml index dd9802e303..8d6480ff3b 100644 --- a/crates/optimize/Cargo.toml +++ b/crates/optimize/Cargo.toml @@ -17,10 +17,11 @@ pdfcraft-content = { workspace = true } pdfcraft-model = { workspace = true } pdfcraft-annot = { workspace = true } thiserror.workspace = true -# JPEG decoding (zune-jpeg) and encoding, and bicubic resampling; already in the tree. -image = { version = "0.25", default-features = false, features = ["jpeg"] } -# The same JPEG decoder, used directly to read samples untransformed (/ColorTransform 0). -zune-jpeg = "0.5" +# Bicubic resampling; zero codec features. +image = { version = "0.25", default-features = false } +# JPEG decoding and encoding via craft-libs. +craft-jpeg = { workspace = true } +craft-image-codecs = { workspace = true } # Lets other programs go first while a copy is being made. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/optimize/src/images.rs b/crates/optimize/src/images.rs index 4037de82f3..5d14fab159 100644 --- a/crates/optimize/src/images.rs +++ b/crates/optimize/src/images.rs @@ -588,10 +588,15 @@ const MAX_HEADER_STEPS: usize = 4096; /// by the step count, not by the size of the stream. That matters: Optimize asks again for every /// group of fragments it tries, and the same image can be drawn many times. fn adobe_transform(data: &[u8]) -> Option> { + jpeg_header_info(data).map(|(flag, _)| flag) +} + +fn jpeg_header_info(data: &[u8]) -> Option<(Option, bool)> { if data.get(..2)? != [0xFF, 0xD8] { return None; } let mut flag = None; + let mut rgb_ids = false; let mut i = 2usize; // Reading past the end returns. for _ in 0..MAX_HEADER_STEPS { @@ -606,7 +611,7 @@ fn adobe_transform(data: &[u8]) -> Option> { // A fill byte before the marker. 0xFF => i = i.checked_add(1)?, // The first scan (or the end): no header segments after it. - 0xDA | 0xD9 => return Some(flag), + 0xDA | 0xD9 => return Some((flag, rgb_ids)), // Markers without a segment: TEM, RSTn, SOI. 0x01 | 0xD0..=0xD8 => i = i.checked_add(2)?, _ => { @@ -619,6 +624,8 @@ fn adobe_transform(data: &[u8]) -> Option> { { // Version, flags0 and flags1 (two bytes each), then the transform flag. flag = Some(*rest.get(6)?); + } else if matches!(marker, 0xC0..=0xC3 | 0xC5..=0xC7 | 0xC9..=0xCB | 0xCD..=0xCF) && segment.get(5) == Some(&3) { + rgb_ids = [segment.get(6).copied(), segment.get(9).copied(), segment.get(12).copied()] == [Some(b'R'), Some(b'G'), Some(b'B')]; } i = end; } @@ -632,27 +639,21 @@ fn adobe_transform(data: &[u8]) -> Option> { /// of components), is too large, or its decoder reads the samples otherwise (for example /// components named R, G and B, which decoders take as stored, while `ycc` says YCbCr). fn jpeg_samples(data: &[u8], w: usize, h: usize, n: usize, ycc: bool) -> Option> { - use zune_jpeg::zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions}; if (w.checked_mul(h)? as u64) > MAX_PIXELS { return None; } - let options = DecoderOptions::default().set_strict_mode(false).set_max_width(usize::from(u16::MAX)).set_max_height(usize::from(u16::MAX)); - let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(data), options); - dec.decode_headers().ok()?; - if dec.dimensions()? != (w, h) || usize::from(dec.info()?.components) != n { + if ycc && n == 3 && jpeg_header_info(data)?.1 { return None; } - let out = match (n, ycc, dec.input_colorspace()?) { - (1, _, ColorSpace::Luma) => ColorSpace::Luma, - (3, true, ColorSpace::YCbCr) => ColorSpace::RGB, - // As stored: no conversion either way. The decoder reports an Adobe marker's 0 as CMYK - // until it decodes, and then reads three such components as RGB. - (3, false, ColorSpace::YCbCr) => ColorSpace::YCbCr, - (3, false, ColorSpace::RGB | ColorSpace::CMYK) => ColorSpace::RGB, - _ => return None, + let mut limits = craft_jpeg::Limits::default(); + limits.max_pixels = MAX_PIXELS; + let dec = craft_jpeg::decode(data, &limits, if ycc { Some(1) } else { Some(0) }).ok()?; + if (dec.width as usize, dec.height as usize) != (w, h) || usize::from(dec.components) != n { + return None; + } + let craft_jpeg::Samples::U8(px) = dec.samples else { + return None; }; - dec.set_options(options.jpeg_set_out_colorspace(out)); - let px = dec.decode().ok()?; (px.len() == w.checked_mul(h)?.checked_mul(n)?).then_some(px) } @@ -681,11 +682,16 @@ fn scaled + 'static>(img: &image::ImageBuffer Option> { - let mut out = Vec::new(); - let mut enc = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality.clamp(1, 100)); - let ty = if n == 1 { image::ExtendedColorType::L8 } else { image::ExtendedColorType::Rgb8 }; - enc.encode(px, w, h, ty).ok()?; - Some(out) + let layout = if n == 1 { craft_image_codecs::ChannelLayout::Gray } else { craft_image_codecs::ChannelLayout::Rgb }; + let img = craft_image_codecs::Image::from_u8(w, h, layout, px.to_vec()).ok()?; + let opts = craft_image_codecs::EncodeOptions { + jpeg_quality: quality.clamp(1, 100), + jpeg_format: craft_image_codecs::JpegFormat::Optimized, + embed_icc: false, + embed_metadata: false, + ..Default::default() + }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).ok() } /// Whether [`pixels`] reads `s` as it is drawn: a codec it decodes, no decode array that diff --git a/crates/optimize/tests/jpeg_color_transform.rs b/crates/optimize/tests/jpeg_color_transform.rs index 4e08185d77..4321687da1 100644 --- a/crates/optimize/tests/jpeg_color_transform.rs +++ b/crates/optimize/tests/jpeg_color_transform.rs @@ -27,9 +27,9 @@ fn photo(w: u32, h: u32) -> Vec { } fn rgb_jpeg(px: &[u8], w: u32, h: u32) -> Vec { - let mut out = Vec::new(); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 100).encode(px, w, h, image::ExtendedColorType::Rgb8).unwrap(); - out + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, px.to_vec()).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 100, ..Default::default() }; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap() } /// The JPEG's segments before its first scan: (marker, start of the marker, end of the segment). diff --git a/crates/optimize/tests/optimize.rs b/crates/optimize/tests/optimize.rs index 5bb20e8159..7eb9c3cece 100644 --- a/crates/optimize/tests/optimize.rs +++ b/crates/optimize/tests/optimize.rs @@ -39,9 +39,10 @@ fn image(doc: &mut Document, w: u32, h: u32, n: usize, jpeg: bool, smask: Option d.set(b"SMask".to_vec(), Object::Ref(m)); } let s = if jpeg { - let mut out = Vec::new(); - let ty = if n == 1 { image::ExtendedColorType::L8 } else { image::ExtendedColorType::Rgb8 }; - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 98).encode(&px, w, h, ty).unwrap(); + let layout = if n == 1 { craft_image_codecs::ChannelLayout::Gray } else { craft_image_codecs::ChannelLayout::Rgb }; + let img = craft_image_codecs::Image::from_u8(w, h, layout, px).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 98, ..Default::default() }; + let out = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap(); d.set(b"Filter".to_vec(), Object::name("DCTDecode")); Stream::from_raw(d, out) } else { @@ -3886,8 +3887,9 @@ fn strip(doc: &mut Document, w: u32, v: u8, jpeg: bool) -> ObjRef { d.set(b"ColorSpace".to_vec(), Object::name("DeviceGray")); let px = vec![v; w as usize]; let s = if jpeg { - let mut out = Vec::new(); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, 95).encode(&px, w, 1, image::ExtendedColorType::L8).unwrap(); + let img = craft_image_codecs::Image::from_u8(w, 1, craft_image_codecs::ChannelLayout::Gray, px).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, ..Default::default() }; + let out = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap(); d.set(b"Filter".to_vec(), Object::name("DCTDecode")); Stream::from_raw(d, out) } else { diff --git a/crates/optimize/tests/tiles.rs b/crates/optimize/tests/tiles.rs index 12e5f60352..ac4ff8eca0 100644 --- a/crates/optimize/tests/tiles.rs +++ b/crates/optimize/tests/tiles.rs @@ -151,8 +151,9 @@ fn a_stencil_mask_paints_the_fill_colour_through_its_tiles() { #[test] fn a_jpeg_is_decoded_into_its_tiles() { let px = texture(W, H, 3); - let mut jpeg = Vec::new(); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 95).encode(&px, W, H, image::ExtendedColorType::Rgb8).unwrap(); + let img = craft_image_codecs::Image::from_u8(W, H, craft_image_codecs::ChannelLayout::Rgb, px).unwrap(); + let opts = craft_image_codecs::EncodeOptions { jpeg_quality: 95, jpeg_chroma_subsampling: false, ..Default::default() }; + let jpeg = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &opts).unwrap(); let (doc, im) = page_drawing(W, H, "", |doc| { let d = image_dict( W, diff --git a/crates/preflight/Cargo.toml b/crates/preflight/Cargo.toml index 99a72d9b6c..28c76ccd97 100644 --- a/crates/preflight/Cargo.toml +++ b/crates/preflight/Cargo.toml @@ -11,10 +11,10 @@ repository.workspace = true workspace = true [dependencies] +craft-xml = { workspace = true } pdfcraft-content = { workspace = true } pdfcraft-cos = { workspace = true } pdfcraft-model = { workspace = true } -roxmltree = "0.21" [dev-dependencies] flate2 = { workspace = true } diff --git a/crates/preflight/src/xmp.rs b/crates/preflight/src/xmp.rs index b2ea91c2f6..d989a64efe 100644 --- a/crates/preflight/src/xmp.rs +++ b/crates/preflight/src/xmp.rs @@ -29,30 +29,91 @@ pub(crate) fn identifier(xmp: &str, namespace: &str, local: &str) -> Option(Vec<(&'a str, &'a str)>); + +impl<'a> NsScope<'a> { + pub(crate) fn root() -> Self { + Self(vec![("xml", "http://www.w3.org/XML/1998/namespace")]) + } + + pub(crate) fn child(&self, el: &'a craft_xml::El) -> Self { + let mut next = self.0.clone(); + next.extend(el.ns_decls()); + Self(next) + } + + pub(crate) fn el_ns(&self, el: &craft_xml::El) -> Option<&'a str> { + let prefix = el.prefix(); + self.0.iter().rfind(|(p, _)| *p == prefix).map(|(_, u)| *u).filter(|u| !u.is_empty()) + } + + pub(crate) fn attr_ns(&self, attr_name: &str) -> Option<&'a str> { + let (prefix, _) = attr_name.split_once(':')?; + if prefix == "xmlns" { + return None; + } + self.0.iter().rfind(|(p, _)| *p == prefix).map(|(_, u)| *u).filter(|u| !u.is_empty()) + } + + pub(crate) fn attr(&self, el: &'a craft_xml::El, ns: &str, local: &str) -> Option<&'a str> { + el.attrs.iter().find_map(|(k, v)| { + let (_, l) = k.split_once(':')?; + (l == local && self.attr_ns(k) == Some(ns)).then_some(v.as_str()) + }) + } + + pub(crate) fn is(&self, el: &craft_xml::El, ns: &str, local: &str) -> bool { + el.local() == local && self.el_ns(el) == Some(ns) + } +} + /// `xmp` parsed within the depth, attribute and node limits; `None` when it can't be. -pub(crate) fn parse(xmp: &str) -> Option> { +pub(crate) fn parse(xmp: &str) -> Option { if !xml_limits_ok(xmp) { return None; } - let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: 100_000, ..Default::default() }; - roxmltree::Document::parse_with_options(xmp, options).ok() + let options = craft_xml::Options { doctype: craft_xml::Doctype::Reject, ..craft_xml::Options::default() } + .strict() + .with_limits(craft_xml::Limits { max_depth: 1024, max_nodes: 100_000, max_attrs_total: 100_000, ..craft_xml::Limits::DEFAULT }); + options.parse(xmp.as_bytes()).ok() } /// [`identifier`] through an XML parser; `Err` when the packet can't be parsed within the limits. fn parsed_identifier(xmp: &str, namespace: &str, local: &str) -> Result, ()> { - let doc = parse(xmp).ok_or(())?; - for node in doc.descendants().filter(roxmltree::Node::is_element) { - if let Some(value) = node.attribute((namespace, local)) { + fn walk<'a>(el: &'a craft_xml::El, parent: &NsScope<'a>, namespace: &str, local: &str) -> Result, ()> { + let scope = parent.child(el); + let el_prefix = el.prefix(); + if !el_prefix.is_empty() && el_prefix != "xml" && scope.el_ns(el).is_none() { + return Err(()); + } + for (k, _) in &el.attrs { + if let Some((prefix, _)) = k.split_once(':') + && !matches!(prefix, "xml" | "xmlns") + && scope.attr_ns(k).is_none() + { + return Err(()); + } + } + if let Some(value) = scope.attr(el, namespace, local) { return Ok(Some(value.trim().to_owned())); } - if node.tag_name().namespace() == Some(namespace) && node.tag_name().name() == local { - return Ok(node.text().map(|value| value.trim().to_owned())); + if scope.is(el, namespace, local) { + return Ok(Some(el.text().trim().to_owned())); } + for child in el.elements() { + if let Some(found) = walk(child, &scope, namespace, local)? { + return Ok(Some(found)); + } + } + Ok(None) } - Ok(None) + let root = parse(xmp).ok_or(())?; + walk(&root, &NsScope::root(), namespace, local) } -/// Reject deep XML and start tags with too many attributes before roxmltree checks duplicates. +/// Reject deep XML and start tags with too many attributes before parsing. /// Each unquoted `=` in a valid start tag introduces an attribute, including namespace bindings. /// Every `b[i]` and `hay[p..]` below is guarded by `i < b.len()` / `p < hay.len()`. fn xml_limits_ok(text: &str) -> bool { diff --git a/crates/preflight/src/xmp_extension.rs b/crates/preflight/src/xmp_extension.rs index 92b3afa38e..9c13d993fc 100644 --- a/crates/preflight/src/xmp_extension.rs +++ b/crates/preflight/src/xmp_extension.rs @@ -23,8 +23,9 @@ //! (4.4), and an empty prefix or namespace binds nothing. The human-readable descriptions //! (`pdfaSchema:schema` and the `description` entries) are Text and may be empty. +use crate::xmp::NsScope; +use craft_xml::El; use pdfcraft_cos::ObjRef; -use roxmltree::{Document, Node}; const RDF: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; const EXTENSION: &str = "http://www.aiim.org/pdfa/ns/extension/"; @@ -103,31 +104,55 @@ struct Place { impl Findings { /// Adds what the packet `doc`, from the metadata stream `object`, breaks. - pub(crate) fn add(&mut self, object: ObjRef, doc: &Document<'_>) { - let containers = doc.descendants().filter(|n| is(n, EXTENSION, "schemas")); - for container in containers { - self.prefixes_in(object, doc, &container); - let children: Vec> = container.children().filter(Node::is_element).collect(); - if !matches!(children.as_slice(), [bag] if is(bag, RDF, "Bag")) && self.room() { - let found = children.iter().take(MAX_SHOWN_CHARS).map(|c| written_name(doc, c)).collect::>().join(", "); + pub(crate) fn add(&mut self, object: ObjRef, doc: &El) { + fn walk_containers<'a>(el: &'a El, parent: &NsScope<'a>, out: &mut Vec<(&'a El, NsScope<'a>)>) { + let scope = parent.child(el); + if scope.is(el, EXTENSION, "schemas") { + out.push((el, scope.clone())); + } + for child in el.elements() { + walk_containers(child, &scope, out); + } + } + let mut containers = Vec::new(); + walk_containers(doc, &NsScope::root(), &mut containers); + for (container, cscope) in containers { + self.prefixes_in(object, container, &cscope); + let children: Vec<(&El, NsScope<'_>)> = container.elements().map(|c| (c, cscope.child(c))).collect(); + if !matches!(children.as_slice(), [(bag, bscope)] if is(bag, bscope, RDF, "Bag")) && self.room() { + let found = children.iter().take(MAX_SHOWN_CHARS).map(|(c, _)| c.name.as_str()).collect::>().join(", "); self.not_bag.push(NotBag { object, found: shown(&found) }); } - for (n, schema) in items(&container).iter().enumerate() { - let id = value(schema, SCHEMA, "namespaceURI").unwrap_or_else(|| position(n)); + for (n, (schema, sscope)) in items(container, &cscope).into_iter().enumerate() { + let id = value(schema, &sscope, SCHEMA, "namespaceURI").unwrap_or_else(|| position(n)); let at = |item: Option| Place { object, schema: id.clone(), item }; - self.need(&at(None), schema, SCHEMA, "pdfaSchema", &["schema", "namespaceURI", "prefix"]); - let properties = entries(schema, SCHEMA, "property").flat_map(|e| items(&e)); - for (n, property) in properties.enumerate() { - let name = value(&property, PROPERTY, "name").unwrap_or_else(|| position(n)); - self.need(&at(Some(name)), &property, PROPERTY, "pdfaProperty", &["name", "valueType", "category", "description"]); + self.need(&at(None), schema, &sscope, SCHEMA, "pdfaSchema", &["schema", "namespaceURI", "prefix"]); + let properties = entries(schema, &sscope, SCHEMA, "property").flat_map(|(e, es)| items(e, &es)); + for (n, (property, pscope)) in properties.enumerate() { + let name = value(property, &pscope, PROPERTY, "name").unwrap_or_else(|| position(n)); + self.need(&at(Some(name)), property, &pscope, PROPERTY, "pdfaProperty", &["name", "valueType", "category", "description"]); } - let value_types = entries(schema, SCHEMA, "valueType").flat_map(|e| items(&e)); - for (n, value_type) in value_types.enumerate() { - let name = value(&value_type, TYPE, "type").unwrap_or_else(|| position(n)); - self.need(&at(Some(name.clone())), &value_type, TYPE, "pdfaType", &["type", "namespaceURI", "prefix", "description", "field"]); - for (n, field) in entries(&value_type, TYPE, "field").flat_map(|e| items(&e)).enumerate() { - let field_name = value(&field, FIELD, "name").unwrap_or_else(|| position(n)); - self.need(&at(Some(format!("{name}/{field_name}"))), &field, FIELD, "pdfaField", &["name", "valueType", "description"]); + let value_types = entries(schema, &sscope, SCHEMA, "valueType").flat_map(|(e, es)| items(e, &es)); + for (n, (value_type, vtscope)) in value_types.enumerate() { + let name = value(value_type, &vtscope, TYPE, "type").unwrap_or_else(|| position(n)); + self.need( + &at(Some(name.clone())), + value_type, + &vtscope, + TYPE, + "pdfaType", + &["type", "namespaceURI", "prefix", "description", "field"], + ); + for (n, (field, fscope)) in entries(value_type, &vtscope, TYPE, "field").flat_map(|(e, es)| items(e, &es)).enumerate() { + let field_name = value(field, &fscope, FIELD, "name").unwrap_or_else(|| position(n)); + self.need( + &at(Some(format!("{name}/{field_name}"))), + field, + &fscope, + FIELD, + "pdfaField", + &["name", "valueType", "description"], + ); } } } @@ -141,13 +166,13 @@ impl Findings { /// Notes the entries of `ns` in `names` that the description `d`, at `at`, lacks, or holds /// empty when they are entries that name something (see [`NAMING`]). - fn need(&mut self, at: &Place, d: &Node<'_, '_>, ns: &str, prefix: &str, names: &[&str]) { + fn need(&mut self, at: &Place, d: &El, scope: &NsScope<'_>, ns: &str, prefix: &str, names: &[&str]) { for name in names { - let element = entry(d, ns, name); - let attribute = d.attribute((ns, *name)); - let empty = match (element, attribute) { + let element = entry(d, scope, ns, name); + let attribute = scope.attr(d, ns, name); + let empty = match (element.as_ref(), attribute) { (None, None) => false, - (Some(e), _) => NAMING.contains(name) && is_empty(&e), + (Some((e, es)), _) => NAMING.contains(name) && is_empty(e, es), (None, Some(a)) => NAMING.contains(name) && a.trim().is_empty(), }; if (empty || (element.is_none() && attribute.is_none())) && self.room() { @@ -160,22 +185,25 @@ impl Findings { /// Notes container namespaces written in `container` (itself included) with another prefix /// than their own. - fn prefixes_in(&mut self, object: ObjRef, doc: &Document<'_>, container: &Node<'_, '_>) { - let text = doc.input_text(); - for n in container.descendants().filter(Node::is_element) { - let element = (n.tag_name().namespace(), text.get(n.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default()); - let attributes = n.attributes().map(|a| (a.namespace(), text.get(a.range_qname()).unwrap_or_default())); + fn prefixes_in(&mut self, object: ObjRef, container: &El, scope: &NsScope<'_>) { + fn walk(findings: &mut Findings, object: ObjRef, n: &El, scope: &NsScope<'_>) { + let element = (scope.el_ns(n), n.name.as_str()); + let attributes = n.attrs.iter().map(|(k, _)| (scope.attr_ns(k), k.as_str())); for (ns, qname) in std::iter::once(element).chain(attributes) { let Some(&(namespace, own)) = PREFIXES.iter().find(|(uri, _)| Some(*uri) == ns) else { continue }; let written = shown(written_prefix(qname)); if written != own - && self.room() - && !self.prefixes.iter().any(|p| p.object == object && p.namespace == namespace && p.written == written) + && findings.room() + && !findings.prefixes.iter().any(|p| p.object == object && p.namespace == namespace && p.written == written) { - self.prefixes.push(Prefix { object, namespace, own, written }); + findings.prefixes.push(Prefix { object, namespace, own, written }); } } + for child in n.elements() { + walk(findings, object, child, &scope.child(child)); + } } + walk(self, object, container, scope); } } @@ -186,11 +214,14 @@ const NAMING: [&str; 6] = ["namespaceURI", "prefix", "name", "type", "valueType" /// Whether the element entry `e` holds no value: no element, no text but white space, and no /// `rdf:resource` or `rdf:value` attribute with a value. -fn is_empty(e: &Node<'_, '_>) -> bool { +fn is_empty(e: &El, scope: &NsScope<'_>) -> bool { let blank = |v: Option<&str>| v.is_none_or(|v| v.trim().is_empty()); - !e.children().any(|c| c.is_element() || (c.is_text() && c.text().is_some_and(|t| !t.trim().is_empty()))) - && blank(e.attribute((RDF, "resource"))) - && blank(e.attribute((RDF, "value"))) + !e.children.iter().any(|c| match c { + craft_xml::Node::El(_) => true, + craft_xml::Node::Text(t) | craft_xml::Node::CData(t) => !t.trim().is_empty(), + _ => false, + }) && blank(scope.attr(e, RDF, "resource")) + && blank(scope.attr(e, RDF, "value")) } /// `#n` for the 0-based position `n`, counted from 1. @@ -209,43 +240,66 @@ fn shown(s: &str) -> String { /// The text of the entry `name` of `ns` in the description `d`, written as an element or an /// attribute, trimmed and [`shown`]; `None` when it is absent or blank. -fn value(d: &Node<'_, '_>, ns: &str, name: &str) -> Option { - let text = entry(d, ns, name).and_then(|e| e.text()).or_else(|| d.attribute((ns, name)))?.trim(); +fn value(d: &El, scope: &NsScope<'_>, ns: &str, name: &str) -> Option { + let owned; + let raw = match entry(d, scope, ns, name) { + Some((e, _)) => { + owned = e.text(); + owned.as_ref() + } + None => scope.attr(d, ns, name)?, + }; + let text = raw.trim(); (!text.is_empty()).then(|| shown(text)) } -/// The name of the element `n` as the packet writes it, prefix included. -fn written_name<'i>(doc: &Document<'i>, n: &Node<'_, '_>) -> &'i str { - qualified_name(doc.input_text().get(n.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default()) -} - -/// The qualified name the text of a start tag (after its `<`) or of an attribute begins with. -fn qualified_name(text: &str) -> &str { - text.split(|c: char| c.is_whitespace() || c == '>' || c == '/' || c == '=').next().unwrap_or_default() -} - /// The prefix of the qualified name `text` begins with, empty when it has none. fn written_prefix(text: &str) -> &str { - qualified_name(text).split_once(':').map_or("", |(p, _)| p) + text.split_once(':').map_or("", |(p, _)| p) } -fn is(n: &Node<'_, '_>, ns: &str, name: &str) -> bool { - n.is_element() && n.tag_name().namespace() == Some(ns) && n.tag_name().name() == name +fn is(n: &El, scope: &NsScope<'_>, ns: &str, name: &str) -> bool { + scope.is(n, ns, name) } /// Every element entry `name` of `ns` in the description `d`: a list written twice is read twice. -fn entries<'a, 'i>(d: &Node<'a, 'i>, ns: &'a str, name: &'a str) -> impl Iterator> { - d.children().filter(move |c| is(c, ns, name)) +fn entries<'a>(d: &'a El, scope: &'a NsScope<'a>, ns: &'a str, name: &'a str) -> impl Iterator)> { + d.elements().filter_map(move |c| { + let cs = scope.child(c); + is(c, &cs, ns, name).then_some((c, cs)) + }) } /// The element entry `name` of `ns` in the description `d`. -fn entry<'a, 'i>(d: &Node<'a, 'i>, ns: &str, name: &str) -> Option> { - d.children().find(|c| is(c, ns, name)) +fn entry<'a>(d: &'a El, scope: &NsScope<'a>, ns: &str, name: &str) -> Option<(&'a El, NsScope<'a>)> { + d.elements().find_map(|c| { + let cs = scope.child(c); + is(c, &cs, ns, name).then_some((c, cs)) + }) } /// The descriptions in the array `e` holds: each item's `rdf:parseType="Resource"` node or /// `rdf:Description`. -fn items<'a, 'i>(e: &Node<'a, 'i>) -> Vec> { - let Some(array) = e.children().find(|c| is(c, RDF, "Bag") || is(c, RDF, "Seq") || is(c, RDF, "Alt")) else { return Vec::new() }; - array.children().filter(|i| is(i, RDF, "li")).map(|li| li.children().find(|c| is(c, RDF, "Description")).unwrap_or(li)).collect() +fn items<'a>(e: &'a El, scope: &NsScope<'a>) -> Vec<(&'a El, NsScope<'a>)> { + let Some((array, ascope)) = e.elements().find_map(|c| { + let cs = scope.child(c); + (is(c, &cs, RDF, "Bag") || is(c, &cs, RDF, "Seq") || is(c, &cs, RDF, "Alt")).then_some((c, cs)) + }) else { + return Vec::new(); + }; + array + .elements() + .filter_map(|li| { + let ls = ascope.child(li); + is(li, &ls, RDF, "li").then_some((li, ls)) + }) + .map(|(li, ls)| { + li.elements() + .find_map(|c| { + let cs = ls.child(c); + is(c, &cs, RDF, "Description").then_some((c, cs)) + }) + .unwrap_or((li, ls)) + }) + .collect() } diff --git a/crates/preflight/src/xmp_types.rs b/crates/preflight/src/xmp_types.rs index cd24e59a13..d4a5cc2a22 100644 --- a/crates/preflight/src/xmp_types.rs +++ b/crates/preflight/src/xmp_types.rs @@ -22,8 +22,9 @@ use std::collections::BTreeMap; +use crate::xmp::NsScope; +use craft_xml::El; use pdfcraft_cos::{DecodeBudget, ObjRef, Object}; -use roxmltree::Node; /// How deep arrays, structures and qualifiers are followed. No type nests nearly this deep, so a /// value nested deeper doesn't have its type. @@ -595,7 +596,7 @@ const SCHEMAS: &[Schema] = &[ /// Calls `f` with every metadata stream among the indirect objects `objs` (ISO 32000-1 14.3.2) that /// parses as XML, and the stream's object, each decoded once within one allowance. -pub(crate) fn for_each_packet(objs: &[(ObjRef, std::sync::Arc)], mut f: impl FnMut(ObjRef, &roxmltree::Document<'_>)) { +pub(crate) fn for_each_packet(objs: &[(ObjRef, std::sync::Arc)], mut f: impl FnMut(ObjRef, &El)) { let mut budget = DecodeBudget::new(MAX_METADATA_BYTES, 0); for (r, o) in objs { let Object::Stream(s) = &**o else { continue }; @@ -632,29 +633,34 @@ fn unicode(bytes: &[u8]) -> String { /// The properties of predefined schemas in the packet `doc` whose values don't have their type, in /// name order: each by its name with the namespace's preferred prefix and by its name as the packet /// writes it (see [`written`]), with the type it should have as [`named`] gives it. -pub(crate) fn mistyped(doc: &roxmltree::Document<'_>) -> BTreeMap<(String, String), String> { - let mut out = BTreeMap::new(); - let text = doc.input_text(); - let descriptions = doc.descendants().filter(|n| is(n, RDF, "Description") && n.parent_element().is_some_and(|p| is(&p, RDF, "RDF"))); - for d in descriptions { - for a in d.attributes() { - if let Some((schema, ty)) = a.namespace().and_then(|ns| property(ns, a.name())) - && !simple(ty, a.value()) - { - let qname = text.get(a.range_qname()).unwrap_or_default(); - out.insert((format!("{}:{}", schema.prefix, a.name()), written(qname, a.name())), named(ty)); +pub(crate) fn mistyped(doc: &El) -> BTreeMap<(String, String), String> { + fn walk<'a>(el: &'a El, parent_scope: &NsScope<'a>, parent_is_rdf: bool, out: &mut BTreeMap<(String, String), String>) { + let scope = parent_scope.child(el); + if parent_is_rdf && scope.is(el, RDF, "Description") { + for (k, v) in &el.attrs { + let local = k.split_once(':').map_or(k.as_str(), |(_, l)| l); + if let Some((schema, ty)) = scope.attr_ns(k).and_then(|ns| property(ns, local)) + && !simple(ty, v) + { + out.insert((format!("{}:{}", schema.prefix, local), written(k, local)), named(ty)); + } + } + for p in el.elements() { + let p_scope = scope.child(p); + if let Some((schema, ty)) = p_scope.el_ns(p).and_then(|ns| property(ns, p.local())) + && !fits(ty, p, &p_scope, 0) + { + out.insert((format!("{}:{}", schema.prefix, p.local()), written(&p.name, p.local())), named(ty)); + } } } - for p in d.children().filter(Node::is_element) { - let name = p.tag_name(); - if let Some((schema, ty)) = name.namespace().and_then(|ns| property(ns, name.name())) - && !fits(ty, &p, 0) - { - let qname = text.get(p.range()).unwrap_or_default().strip_prefix('<').unwrap_or_default(); - out.insert((format!("{}:{}", schema.prefix, name.name()), written(qname, name.name())), named(ty)); - } + let is_rdf = scope.is(el, RDF, "RDF"); + for child in el.elements() { + walk(child, &scope, is_rdf, out); } } + let mut out = BTreeMap::new(); + walk(doc, &NsScope::root(), false, &mut out); out } @@ -702,62 +708,69 @@ fn property(ns: &str, name: &str) -> Option<(&'static Schema, Ty)> { schema.properties.iter().find(|(n, _)| *n == name).map(|(_, t)| (schema, *t)) } -fn is(n: &Node<'_, '_>, ns: &str, name: &str) -> bool { - n.is_element() && n.tag_name().namespace() == Some(ns) && n.tag_name().name() == name +fn is<'a>(n: &El, scope: &NsScope<'a>, ns: &str, name: &str) -> bool { + scope.is(n, ns, name) } /// The attributes of `n` that are fields or qualifiers, not RDF or XML syntax. -fn field_attributes<'a, 'i>(n: &Node<'a, 'i>) -> impl Iterator> { - n.attributes().filter(|a| !matches!(a.namespace(), Some(RDF) | Some(XML))) +fn field_attributes<'a>(n: &'a El, scope: &'a NsScope<'a>) -> impl Iterator + 'a { + n.attrs + .iter() + .filter(move |(k, _)| k != "xmlns" && !k.starts_with("xmlns:") && !matches!(scope.attr_ns(k), Some(RDF) | Some(XML))) + .map(|(k, v)| (k.as_str(), v.as_str())) } /// Whether the value of the property or array item element `e` has type `ty`. -fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool { +fn fits<'a>(ty: Ty, e: &'a El, scope: &NsScope<'a>, depth: usize) -> bool { if depth > MAX_DEPTH { return false; } let deeper = depth + 1; - let children: Vec> = e.children().filter(Node::is_element).collect(); + let children: Vec<(&'a El, NsScope<'a>)> = e.elements().map(|c| (c, scope.child(c))).collect(); // A qualified value: `rdf:value` holds the value itself (ISO 16684-1 7.9.2.5). - let resource = e.attribute((RDF, "parseType")) == Some("Resource"); + let resource = scope.attr(e, RDF, "parseType") == Some("Resource"); // `parseType="Resource"` is the only parse type XMP has (ISO 16684-1 7.9.2.5); an // `rdf:resource` element is empty. - if e.attribute((RDF, "parseType")).is_some_and(|p| p != "Resource") || e.attribute((RDF, "resource")).is_some() && !children.is_empty() { + if scope.attr(e, RDF, "parseType").is_some_and(|p| p != "Resource") || scope.attr(e, RDF, "resource").is_some() && !children.is_empty() { return false; } let description = match children.as_slice() { - [d] if is(d, RDF, "Description") => Some(*d), + [(d, ds)] if is(d, ds, RDF, "Description") => Some((*d, ds.clone())), _ => None, }; // `rdf:value` as an element of a `parseType="Resource"` node or `rdf:Description`, or as an // attribute of the description or of an empty property element. Its qualifiers that are // predefined properties keep their types too. - let node = if resource { Some(*e) } else { description }; - if let Some(n) = node { + let node = if resource { Some((e, scope.clone())) } else { description.clone() }; + if let Some((n, ns)) = node { // A qualified value has one `rdf:value`: an element, or an attribute in its place. - match n.children().filter(|c| is(c, RDF, "value")).collect::>().as_slice() { + let vals: Vec<_> = n.elements().filter(|c| is(c, &ns.child(c), RDF, "value")).collect(); + match vals.as_slice() { [] => {} - [v] => return n.attribute((RDF, "value")).is_none() && blank(e) && blank(&n) && fits(ty, v, deeper) && predefined_ok(&n, deeper), + [v] => { + let vs = ns.child(v); + return ns.attr(n, RDF, "value").is_none() && blank(e) && blank(n) && fits(ty, v, &vs, deeper) && predefined_ok(n, &ns, deeper); + } _ => return false, } } - let attribute_node = description.or_else(|| (children.is_empty() && !resource).then_some(*e)); - if let Some(n) = attribute_node - && let Some(v) = n.attribute((RDF, "value")) + let attribute_node = description.as_ref().map(|(d, ds)| (*d, ds)).or_else(|| (children.is_empty() && !resource).then_some((e, scope))); + if let Some((n, ns)) = attribute_node + && let Some(v) = ns.attr(n, RDF, "value") { - return blank(e) && blank(&n) && simple(ty, v) && predefined_ok(&n, deeper); + return blank(e) && blank(n) && simple(ty, v) && predefined_ok(n, ns, deeper); } match ty { Ty::Text | Ty::Guid | Ty::Integer | Ty::Real | Ty::Boolean | Ty::Date | Ty::Rational | Ty::GpsCoordinate => { // `rdf:resource` makes the value a URI, which only a text type takes. // The element is then empty. - if e.attribute((RDF, "resource")).is_some() { + if scope.attr(e, RDF, "resource").is_some() { return matches!(ty, Ty::Text) && children.is_empty() && blank(e); } - children.is_empty() && !resource && field_attributes(e).next().is_none() && simple(ty, &text(e)) + children.is_empty() && !resource && field_attributes(e, scope).next().is_none() && simple(ty, &text(e)) } - Ty::LangAlt => match array(e, &children, "Alt") { - Some(items) => items.iter().all(|i| i.attribute((XML, "lang")).is_some() && fits(Ty::Text, i, deeper)), + Ty::LangAlt => match array(e, scope, &children, "Alt") { + Some(items) => items.iter().all(|(i, iscope)| iscope.attr(i, XML, "lang").is_some() && fits(Ty::Text, i, iscope, deeper)), None => false, }, Ty::Seq(item) | Ty::Bag(item) | Ty::Alt(item) => { @@ -766,18 +779,21 @@ fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool { Ty::Bag(_) => "Bag", _ => "Alt", }; - array(e, &children, kind).is_some_and(|items| items.iter().all(|i| fits(*item, i, deeper))) + array(e, scope, &children, kind).is_some_and(|items| items.iter().all(|(i, iscope)| fits(*item, i, iscope, deeper))) } Ty::Struct(_, fields) => { - let Some(holder) = structure(e, &children, resource, description) else { return false }; - let attrs_ok = field_attributes(&holder).all(|a| match a.namespace().and_then(|ns| field(fields, ns, a.name())) { - Some(t) => simple(t, a.value()), - None => true, + let Some((holder, hscope)) = structure(e, scope, &children, resource, description.as_ref()) else { return false }; + let attrs_ok = field_attributes(holder, &hscope).all(|(k, v)| { + let local = k.split_once(':').map_or(k, |(_, l)| l); + match hscope.attr_ns(k).and_then(|ns| field(fields, ns, local)) { + Some(t) => simple(t, v), + None => true, + } }); - let elements_ok = holder.children().filter(Node::is_element).all(|f| { - let n = f.tag_name(); - match n.namespace().and_then(|ns| field(fields, ns, n.name())) { - Some(t) => fits(t, &f, deeper), + let elements_ok = holder.elements().all(|f| { + let fs = hscope.child(f); + match fs.el_ns(f).and_then(|ns| field(fields, ns, f.local())) { + Some(t) => fits(t, f, &fs, deeper), None => true, } }); @@ -785,18 +801,23 @@ fn fits(ty: Ty, e: &Node<'_, '_>, depth: usize) -> bool { } // A structure of any fields, such as a Pantry item: predefined properties in it still have // their types. - Ty::AnyStruct => structure(e, &children, resource, description).is_some_and(|holder| predefined_ok(&holder, deeper)), - Ty::OneOf(types) => types.iter().any(|t| fits(*t, e, depth)), + Ty::AnyStruct => { + structure(e, scope, &children, resource, description.as_ref()).is_some_and(|(holder, hscope)| predefined_ok(holder, &hscope, deeper)) + } + Ty::OneOf(types) => types.iter().any(|t| fits(*t, e, scope, depth)), } } /// Whether the fields of `holder`, as attributes and elements, that are predefined properties have /// their types. -fn predefined_ok(holder: &Node<'_, '_>, depth: usize) -> bool { - let attrs_ok = field_attributes(holder).all(|a| a.namespace().and_then(|ns| property(ns, a.name())).is_none_or(|(_, t)| simple(t, a.value()))); - let elements_ok = holder.children().filter(Node::is_element).all(|f| { - let n = f.tag_name(); - n.namespace().and_then(|ns| property(ns, n.name())).is_none_or(|(_, t)| fits(t, &f, depth)) +fn predefined_ok<'a>(holder: &'a El, scope: &NsScope<'a>, depth: usize) -> bool { + let attrs_ok = field_attributes(holder, scope).all(|(k, v)| { + let local = k.split_once(':').map_or(k, |(_, l)| l); + scope.attr_ns(k).and_then(|ns| property(ns, local)).is_none_or(|(_, t)| simple(t, v)) + }); + let elements_ok = holder.elements().all(|f| { + let fs = scope.child(f); + fs.el_ns(f).and_then(|ns| property(ns, f.local())).is_none_or(|(_, t)| fits(t, f, &fs, depth)) }); attrs_ok && elements_ok } @@ -806,46 +827,55 @@ fn field(fields: &[Field], ns: &str, name: &str) -> Option { } /// The items of `e`'s value when it is an `rdf:` array. -fn array<'a, 'i>(e: &Node<'a, 'i>, children: &[Node<'a, 'i>], kind: &str) -> Option>> { +fn array<'a>(e: &'a El, scope: &NsScope<'a>, children: &[(&'a El, NsScope<'a>)], kind: &str) -> Option)>> { match children { // Items are `rdf:li` (ISO 16684-1 7.9.2.4); any other element, or character data around // them, breaks the array, and so does `rdf:parseType` or `rdf:resource` on the property, // which makes its value something else. - [a] if is(a, RDF, kind) && field_attributes(e).next().is_none() && blank(e) && blank(a) && !other_value_form(e) => { - let items: Vec> = a.children().filter(Node::is_element).collect(); - items.iter().all(|i| is(i, RDF, "li")).then_some(items) + [(a, ascope)] + if is(a, ascope, RDF, kind) && field_attributes(e, scope).next().is_none() && blank(e) && blank(a) && !other_value_form(e, scope) => + { + let items: Vec<(&'a El, NsScope<'a>)> = a.elements().map(|i| (i, ascope.child(i))).collect(); + items.iter().all(|(i, iscope)| is(i, iscope, RDF, "li")).then_some(items) } _ => None, } } /// The node holding the fields of `e`'s value when it is a structure. -fn structure<'a, 'i>(e: &Node<'a, 'i>, children: &[Node<'a, 'i>], resource: bool, description: Option>) -> Option> { +fn structure<'a>( + e: &'a El, + scope: &NsScope<'a>, + children: &[(&'a El, NsScope<'a>)], + resource: bool, + description: Option<&(&'a El, NsScope<'a>)>, +) -> Option<(&'a El, NsScope<'a>)> { if resource { // A structure's fields are elements; character data has no place in it. - return text(e).trim().is_empty().then_some(*e); + return text(e).trim().is_empty().then_some((e, scope.clone())); } - if let Some(d) = description { - return (blank(e) && blank(&d)).then_some(d); + if let Some((d, ds)) = description { + return (blank(e) && blank(d)).then_some((*d, ds.clone())); } // Fields written as attributes of the property element, with no content. let empty = text(e).trim().is_empty(); - (children.is_empty() && empty && e.attribute((RDF, "resource")).is_none() && field_attributes(e).next().is_some()).then_some(*e) + (children.is_empty() && empty && scope.attr(e, RDF, "resource").is_none() && field_attributes(e, scope).next().is_some()) + .then_some((e, scope.clone())) } /// Whether `e` says its value is a resource or a literal of another kind, not the element it holds. -fn other_value_form(e: &Node<'_, '_>) -> bool { - e.attribute((RDF, "parseType")).is_some() || e.attribute((RDF, "resource")).is_some() +fn other_value_form<'a>(e: &El, scope: &NsScope<'a>) -> bool { + scope.attr(e, RDF, "parseType").is_some() || scope.attr(e, RDF, "resource").is_some() } /// Whether `e` holds no character data but white space. -fn blank(e: &Node<'_, '_>) -> bool { +fn blank(e: &El) -> bool { text(e).trim().is_empty() } /// All the character data directly in `e`, however comments or other nodes split it. -fn text(e: &Node<'_, '_>) -> String { - e.children().filter(Node::is_text).filter_map(|t| t.text()).collect() +fn text(e: &El) -> String { + e.text().into_owned() } /// Whether the simple value `v` has the simple type `ty`. diff --git a/crates/redact/Cargo.toml b/crates/redact/Cargo.toml index 8b6db74534..bd74311069 100644 --- a/crates/redact/Cargo.toml +++ b/crates/redact/Cargo.toml @@ -26,8 +26,8 @@ regex = { workspace = true } thiserror = { workspace = true } # NFKC for the metadata check; already in the build through pdfcraft-crypt (stringprep). unicode-normalization = "0.1" -# Reads the PDF/A and PDF/UA identification by namespace; already in the build through pdfcraft-xfa. -roxmltree = "0.21" +# Reads the PDF/A and PDF/UA identification by namespace. +craft-xml = { workspace = true } sha2 = "0.11" [dev-dependencies] diff --git a/crates/redact/src/identification.rs b/crates/redact/src/identification.rs index b509481adf..65035afda6 100644 --- a/crates/redact/src/identification.rs +++ b/crates/redact/src/identification.rs @@ -183,19 +183,23 @@ fn read(text: &str) -> Option { if text.bytes().filter(|&b| b == b'=').count() > MAX_EQUALS { return None; } - let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; - let xml = roxmltree::Document::parse_with_options(text, options).ok()?; + const CANON: craft_xml::Canonical<'_> = craft_xml::Canonical::new(&[("pdfaid", PDFA_ID), ("pdfuaid", PDFUA_ID)]); + let mut opts = + craft_xml::Options::canonical(&CANON).strict().with_limits(craft_xml::Limits { max_nodes: MAX_NODES as usize, ..Default::default() }); + opts.doctype = craft_xml::Doctype::Reject; + let xml = opts.parse(text.as_bytes()).ok()?; let mut found = Found::default(); - for node in xml.descendants().filter(roxmltree::Node::is_element) { - for (ns, id) in [(PDFA_ID, &mut found.pdfa), (PDFUA_ID, &mut found.pdfua)] { + for node in std::iter::once(&xml).chain(xml.descendants()) { + for (prefix, id) in [("pdfaid", &mut found.pdfa), ("pdfuaid", &mut found.pdfua)] { for (local, slot) in [("part", &mut id.part), ("conformance", &mut id.conformance), ("amd", &mut id.amd), ("rev", &mut id.rev)] { if slot.is_some() { continue; } - if let Some(v) = node.attribute((ns, local)) { + let qname = format!("{prefix}:{local}"); + if let Some(v) = node.attr(&qname) { *slot = Some(v.trim().to_string()); - } else if node.tag_name().namespace() == Some(ns) && node.tag_name().name() == local { - *slot = Some(node.text().unwrap_or("").trim().to_string()); + } else if node.name == qname { + *slot = Some(node.text().trim().to_string()); } } } diff --git a/crates/redact/src/tests/identification.rs b/crates/redact/src/tests/identification.rs index c896e335fe..adef258769 100644 --- a/crates/redact/src/tests/identification.rs +++ b/crates/redact/src/tests/identification.rs @@ -43,15 +43,22 @@ fn the_identification_of_a_removed_packet_is_written_back() { assert_eq!(r.metadata, 1, "{r:?}"); let text = text.expect("a packet is written back"); assert!(!text.to_lowercase().contains("bluefin") && !text.contains("Lovelace"), "{text}"); - let xml = roxmltree::Document::parse(&text).expect("well-formed"); - let value = |ns: &str, local: &str| { - xml.descendants().find(|n| n.tag_name().namespace() == Some(ns) && n.tag_name().name() == local).and_then(|n| n.text()).map(str::to_string) + const CANON: craft_xml::Canonical<'_> = craft_xml::Canonical::new(&[ + ("pdfaid", "http://www.aiim.org/pdfa/ns/id/"), + ("pdfuaid", "http://www.aiim.org/pdfua/ns/id/"), + ("pdfaSchema", "http://www.aiim.org/pdfa/ns/schema#"), + ]); + let xml = craft_xml::Options::canonical(&CANON).strict().parse(text.as_bytes()).expect("well-formed"); + let value = |prefix: &str, local: &str| { + let qname = format!("{prefix}:{local}"); + std::iter::once(&xml).chain(xml.descendants()).find(|n| n.name == qname).map(|n| n.text().into_owned()) }; - let a = "http://www.aiim.org/pdfa/ns/id/"; - let ua = "http://www.aiim.org/pdfua/ns/id/"; - assert_eq!((value(a, "part").as_deref(), value(a, "conformance").as_deref(), value(ua, "part").as_deref()), (Some("2"), Some("B"), Some("1"))); + assert_eq!( + (value("pdfaid", "part").as_deref(), value("pdfaid", "conformance").as_deref(), value("pdfuaid", "part").as_deref()), + (Some("2"), Some("B"), Some("1")) + ); // PDF/A-2 accepts pdfuaid only through an extension schema describing it. - assert_eq!(value("http://www.aiim.org/pdfa/ns/schema#", "prefix").as_deref(), Some("pdfuaid"), "{text}"); + assert_eq!(value("pdfaSchema", "prefix").as_deref(), Some("pdfuaid"), "{text}"); } /// A packet without identification is removed as before, and nothing is said about it. @@ -74,7 +81,7 @@ fn an_identification_that_cannot_be_kept_is_reported_lost() { let latin1 = [ DOC_XMP_HEAD.as_bytes(), - br#""#, + b"", DOC_XMP_TAIL.as_bytes(), ] .concat(); diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index fdac3dbdbe..93d3a4c9ad 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -45,7 +45,7 @@ web-time = { workspace = true } bytemuck = { version = "1", features = ["extern_crate_alloc"] } rfd = { workspace = true } -png = "0.18" +craft-image-codecs = { workspace = true } base64 = "0.23" # Unicode bidi paragraph segmentation for right-to-left comment wrapping; egui 0.37's epaint owns # display reordering. Already in the tree through egui’s epaint. @@ -101,7 +101,7 @@ egui_kittest = { workspace = true, features = ["eframe", "wgpu"] } # Tests draw PdfCraft text without a system provider: the bundled and monochrome emoji faces must # be enabled on the test graph itself (a dependency's dev-features do not propagate). egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } -image = { version = "0.25", default-features = false, features = ["png"] } +image = { version = "0.25", default-features = false } # Glyph-coverage test of the interface fonts; the same skrifa pdfcraft-fonts already uses. skrifa = "0.44" proptest = { workspace = true } diff --git a/crates/ui-egui/examples/shot.rs b/crates/ui-egui/examples/shot.rs index 8058df40c8..ac5a506d26 100644 --- a/crates/ui-egui/examples/shot.rs +++ b/crates/ui-egui/examples/shot.rs @@ -33,12 +33,15 @@ fn load_ocr_verify(app: &mut PdfCraftApp) -> Result<(), String> { let png = export::Exporter::new(s.get(id).ok_or("the document is gone")?).png(0, 150.0).map_err(|e| e.to_string())?; // Keep the text block of the rendered page, so the scan is a short note-sized picture: // the whole page and the confidence legend fit in the shot's source-page pane. - let mut page = image::load_from_memory(&png).map_err(|e| e.to_string())?; - let mut scan_png = std::io::Cursor::new(Vec::new()); - page.crop(0, 0, page.width(), (page.height() as f32 * 0.34) as u32) - .write_to(&mut scan_png, image::ImageFormat::Png) + let page = craft_image_codecs::decode(&png).map_err(|e| e.to_string())?; + let crop_h = ((page.height() as f32 * 0.34) as u32).max(1); + let bpp = page.layout().channels() as usize; + let row_bytes = page.width() as usize * bpp; + let cropped_data = page.as_u8().ok_or("expected 8-bit PNG")?[..crop_h as usize * row_bytes].to_vec(); + let cropped = craft_image_codecs::Image::from_u8(page.width(), crop_h, page.layout(), cropped_data).map_err(|e| e.to_string())?; + let scan_png = craft_image_codecs::encode(&cropped, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()) .map_err(|e| e.to_string())?; - let scan = s.create_from_images(&[("scan.png".into(), scan_png.into_inner())]).map_err(|e| e.to_string())?.to_vec(); + let scan = s.create_from_images(&[("scan.png".into(), scan_png)]).map_err(|e| e.to_string())?.to_vec(); let settings = OcrSettings { engine: pdfcraft_engine::ocr::EngineChoice::Auto, strategy: MergeStrategy::ConfidenceWeighted, ..OcrSettings::default() }; app.ocr_draft.engine = settings.engine; @@ -118,7 +121,11 @@ fn main() -> Result<(), String> { let h = (image.height() as f64 * w as f64 / image.width() as f64).round() as u32; image = image::imageops::resize(&image, w, h, image::imageops::FilterType::Lanczos3); } - image.save(&out).map_err(|e| e.to_string())?; - eprintln!("shot: wrote {out} ({}×{})", image.width(), image.height()); + let (w, h) = (image.width(), image.height()); + let out_img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, image.into_raw()).map_err(|e| e.to_string())?; + let encoded = craft_image_codecs::encode(&out_img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()) + .map_err(|e| e.to_string())?; + std::fs::write(&out, encoded).map_err(|e| e.to_string())?; + eprintln!("shot: wrote {out} ({w}×{h})"); Ok(()) } diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 87bda69e86..a04f6d2a35 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -2049,9 +2049,9 @@ fn many_open_documents_share_one_raster_allowance_and_close_releases_handles() { #[test] fn suspended_signature_preview_releases_layers_and_rebuilds_without_editing() { let ctx = egui::Context::default(); - let mut png = std::io::Cursor::new(Vec::new()); - image::RgbaImage::from_pixel(12, 4, image::Rgba([20, 30, 40, 255])).write_to(&mut png, image::ImageFormat::Png).unwrap(); - let image = pdfcraft_engine::SignatureImage::from_bytes(png.get_ref()).unwrap(); + let img = craft_image_codecs::Image::from_u8(12, 4, craft_image_codecs::ChannelLayout::Rgba, [20, 30, 40, 255].repeat(48)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + let image = pdfcraft_engine::SignatureImage::from_bytes(&png).unwrap(); let mut session = pdfcraft_engine::Session::new(); let blank = session.create_blank(300.0, 400.0, 1).unwrap(); let id = session.open_new("signature-preview.pdf", blank).unwrap(); diff --git a/crates/ui-egui/src/control/screenshot.rs b/crates/ui-egui/src/control/screenshot.rs index c6fbb0ed83..b63dc7bb53 100644 --- a/crates/ui-egui/src/control/screenshot.rs +++ b/crates/ui-egui/src/control/screenshot.rs @@ -183,13 +183,9 @@ pub(super) fn screenshot_png(image: &egui::ColorImage, region: Option MAX_ENCODED_BYTES { return Err(format!("the encoded screenshot is {} bytes, over the {MAX_ENCODED_BYTES}-byte encode limit", out.len())); } diff --git a/crates/ui-egui/src/workflows/combine_ui/tests.rs b/crates/ui-egui/src/workflows/combine_ui/tests.rs index 7df65d134b..762abe8693 100644 --- a/crates/ui-egui/src/workflows/combine_ui/tests.rs +++ b/crates/ui-egui/src/workflows/combine_ui/tests.rs @@ -80,9 +80,8 @@ fn duplicate_warnings_reuse_results_and_track_removal_and_parts() { #[test] fn the_same_image_or_text_file_added_twice_is_a_duplicate() { static NOW: std::sync::atomic::AtomicI64 = std::sync::atomic::AtomicI64::new(1_700_000_000); - let mut png = std::io::Cursor::new(Vec::new()); - image::RgbaImage::from_pixel(12, 4, image::Rgba([20, 30, 40, 255])).write_to(&mut png, image::ImageFormat::Png).unwrap(); - let png = png.into_inner(); + let img = craft_image_codecs::Image::from_u8(12, 4, craft_image_codecs::ChannelLayout::Rgba, [20, 30, 40, 255].repeat(48)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let warnings = |first: (&str, &[u8]), second: (&str, &[u8])| { let mut app = PdfCraftApp::new(); app.session = std::mem::take(&mut app.session).with_clock(|| NOW.fetch_add(1, std::sync::atomic::Ordering::Relaxed)); diff --git a/crates/ui-egui/src/workflows/create_ui.rs b/crates/ui-egui/src/workflows/create_ui.rs index 5d3d6f4cc6..ab34129722 100644 --- a/crates/ui-egui/src/workflows/create_ui.rs +++ b/crates/ui-egui/src/workflows/create_ui.rs @@ -87,14 +87,8 @@ pub(crate) fn png_from_rgba(width: usize, height: usize, rgba: &[u8]) -> Result< if w == 0 || h == 0 || width > MAX_IMAGE_SIDE || height > MAX_IMAGE_SIDE || expected != Some(rgba.len()) || rgba.len() > MAX_IMAGE_BYTES { return Err("the clipboard image is empty, malformed or exceeds size limits".into()); } - let mut out = Vec::new(); - let mut enc = png::Encoder::new(&mut out, w, h); - enc.set_color(png::ColorType::Rgba); - enc.set_depth(png::BitDepth::Eight); - let mut writer = enc.write_header().map_err(|e| e.to_string())?; - writer.write_image_data(rgba).map_err(|e| e.to_string())?; - writer.finish().map_err(|e| e.to_string())?; - Ok(out) + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgba, rgba.to_vec()).map_err(|e| e.to_string())?; + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).map_err(|e| e.to_string()) } fn stem(name: &str) -> &str { diff --git a/crates/ui-egui/src/workflows/ocr_ui.rs b/crates/ui-egui/src/workflows/ocr_ui.rs index 799da09d7d..d5e6bcbb21 100644 --- a/crates/ui-egui/src/workflows/ocr_ui.rs +++ b/crates/ui-egui/src/workflows/ocr_ui.rs @@ -1101,11 +1101,9 @@ mod tests { /// A one-page PDF that is one picture covering the page. fn scan() -> Vec { - let mut png = std::io::Cursor::new(Vec::new()); - image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255]))) - .write_to(&mut png, image::ImageFormat::Png) - .unwrap(); - Session::new().create_from_images(&[("scan.png".into(), png.into_inner())]).unwrap().to_vec() + let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + Session::new().create_from_images(&[("scan.png".into(), png)]).unwrap().to_vec() } fn app_with(pdf: Vec) -> PdfCraftApp { @@ -1167,11 +1165,9 @@ mod tests { assert_eq!(found(&mut app), None); assert!(app.views[0].auto_ocr_skipped.contains_key(&0), "remembered with the document's generation"); let id = app.active_ids().unwrap().1; - let mut png = std::io::Cursor::new(Vec::new()); - image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255]))) - .write_to(&mut png, image::ImageFormat::Png) - .unwrap(); - let add = Edit::AddImage { page: 0, rect: Some([0.0, 0.0, 595.0, 842.0]), name: "scan.png".into(), bytes: Arc::new(png.into_inner()) }; + let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + let add = Edit::AddImage { page: 0, rect: Some([0.0, 0.0, 595.0, 842.0]), name: "scan.png".into(), bytes: Arc::new(png) }; app.session.apply(id, add).unwrap(); assert_eq!(found(&mut app), Some(0), "a picture over the page makes it a scan"); } @@ -1262,13 +1258,9 @@ mod tests { app.ocr_run = Some(OcrRun { doc: id, progress, guard, automatic: Some(AutoOcr { page: 0, generation }), editable: false }); app.views[0].auto_ocr_tried.insert(0); // The document changes while the run is out. - let mut png = std::io::Cursor::new(Vec::new()); - image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(8, 8, image::Rgba([250, 250, 250, 255]))) - .write_to(&mut png, image::ImageFormat::Png) - .unwrap(); - app.session - .apply(id, Edit::AddImage { page: 0, rect: Some([10.0, 10.0, 50.0, 50.0]), name: "p.png".into(), bytes: Arc::new(png.into_inner()) }) - .unwrap(); + let img = craft_image_codecs::Image::from_u8(8, 8, craft_image_codecs::ChannelLayout::Rgba, [250, 250, 250, 255].repeat(64)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); + app.session.apply(id, Edit::AddImage { page: 0, rect: Some([10.0, 10.0, 50.0, 50.0]), name: "p.png".into(), bytes: Arc::new(png) }).unwrap(); app.poll_ocr(); assert!(app.ocr_run.is_none(), "the run is over"); assert_eq!(app.session.get(id).unwrap().can_undo(), Some("Add image"), "its result was not applied"); diff --git a/crates/ui-egui/tests/it/content.rs b/crates/ui-egui/tests/it/content.rs index e02b17f3ae..76d8d68d0a 100644 --- a/crates/ui-egui/tests/it/content.rs +++ b/crates/ui-egui/tests/it/content.rs @@ -242,13 +242,8 @@ fn typing_moving_styling_and_deleting_added_content() { h.run_steps(3); assert!(added(&h).is_empty()); // An image lands in the middle of the page, selected. - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 30, 20); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[90u8; 1800]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(30, 20, craft_image_codecs::ChannelLayout::Rgb, vec![90u8; 1800]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); h.state_mut().add_image("logo.png".into(), png); h.run_steps(3); let a = added(&h); @@ -993,13 +988,8 @@ fn a_resized_added_picture_shows_only_at_its_new_size() { let mut h = harness(); assert!(h.state_mut().execute("edit.edit_text")); h.run_steps(2); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 60, 60); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); h.state_mut().add_image("red.png".into(), png); h.run_steps(3); assert_eq!(h.state().views[0].content.selected, Some((0, 0)), "the picture is selected"); @@ -1049,13 +1039,8 @@ fn added_picture_resizes_repeatedly_and_drags_back_from_out_of_bounds_without_sm let mut h = harness(); assert!(h.state_mut().execute("edit.edit_text")); h.run_steps(2); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 60, 60); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); h.state_mut().add_image("red.png".into(), png); h.run_steps(3); crate::support::settle(&mut h); @@ -1162,13 +1147,8 @@ fn a_handle_in_the_viewport_below_a_short_page_starts_another_resize() { h.state_mut().set_option("zoom", "50").unwrap(); assert!(h.state_mut().execute("edit.edit_text")); h.run_steps(2); - let mut png = Vec::new(); - { - let mut enc = png::Encoder::new(&mut png, 60, 60); - enc.set_color(png::ColorType::Rgb); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[200u8, 30, 30].repeat(3600)).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(60, 60, craft_image_codecs::ChannelLayout::Rgb, [200u8, 30, 30].repeat(3600)).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); h.state_mut().add_image("red.png".into(), png); h.run_steps(3); let img = match &added(&h)[0].content { diff --git a/crates/ui-egui/tests/it/create.rs b/crates/ui-egui/tests/it/create.rs index 7260dd816b..049eec5a1a 100644 --- a/crates/ui-egui/tests/it/create.rs +++ b/crates/ui-egui/tests/it/create.rs @@ -3,15 +3,8 @@ use pdfcraft_ui_egui::PdfCraftApp; fn png() -> Vec { - let mut out = Vec::new(); - { - let mut enc = png::Encoder::new(&mut out, 8, 4); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[120u8; 8 * 4 * 3]).unwrap(); - } - out + let img = craft_image_codecs::Image::from_u8(8, 4, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 8 * 4 * 3]).unwrap(); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() } #[test] @@ -123,13 +116,8 @@ fn reduce_file_size_writes_a_compact_copy() { // A photo-like picture, stored losslessly: Reduce recompresses it as JPEG. let (w, h) = (600u32, 400u32); let px: Vec = (0..w * h).flat_map(|i| [(i % w * 255 / w) as u8 ^ (i & 7) as u8, (i / w * 255 / h) as u8, 128]).collect(); - let mut photo = Vec::new(); - { - let mut enc = png::Encoder::new(&mut photo, w, h); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - enc.write_header().unwrap().write_image_data(&px).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(w, h, craft_image_codecs::ChannelLayout::Rgb, px).unwrap(); + let photo = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); let dir = ReduceDir::new(); let out = dir.0.join("reduced.pdf"); let h = reduce("photo.png", photo, &out); diff --git a/crates/ui-egui/tests/it/javascript.rs b/crates/ui-egui/tests/it/javascript.rs index 7ed9c784f7..72a7d61ece 100644 --- a/crates/ui-egui/tests/it/javascript.rs +++ b/crates/ui-egui/tests/it/javascript.rs @@ -642,13 +642,8 @@ fn clicking_duplicate_image_buttons_imports_only_into_the_clicked_field() { let dir = std::env::temp_dir().join(format!("pdfcraft-exact-image-click-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let path = dir.join("synthetic.png"); - let mut png = vec![]; - { - let mut encoder = png::Encoder::new(&mut png, 2, 2); - encoder.set_color(png::ColorType::Rgb); - encoder.set_depth(png::BitDepth::Eight); - encoder.write_header().unwrap().write_image_data(&[120u8; 12]).unwrap(); - } + let img = craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 12]).unwrap(); + let png = craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap(); std::fs::write(&path, png).unwrap(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| { let mut app = PdfCraftApp::new(); diff --git a/crates/ui-egui/tests/it/reading_colors.rs b/crates/ui-egui/tests/it/reading_colors.rs index 557696dbf4..e69bc49db3 100644 --- a/crates/ui-egui/tests/it/reading_colors.rs +++ b/crates/ui-egui/tests/it/reading_colors.rs @@ -55,15 +55,8 @@ fn pending_pages_use_document_reading_background() { /// A small PNG for the Create ▸ Images path, which opens the document it builds. fn png() -> Vec { - let mut out = Vec::new(); - { - let mut enc = png::Encoder::new(&mut out, 8, 4); - enc.set_color(png::ColorType::Rgb); - enc.set_depth(png::BitDepth::Eight); - let mut w = enc.write_header().unwrap(); - w.write_image_data(&[120u8; 8 * 4 * 3]).unwrap(); - } - out + let img = craft_image_codecs::Image::from_u8(8, 4, craft_image_codecs::ChannelLayout::Rgb, vec![120u8; 8 * 4 * 3]).unwrap(); + craft_image_codecs::encode(&img, craft_image_codecs::Format::Png, &craft_image_codecs::EncodeOptions::default()).unwrap() } #[test] diff --git a/crates/xfa/Cargo.toml b/crates/xfa/Cargo.toml index 687efaf18e..ca7c3475e0 100644 --- a/crates/xfa/Cargo.toml +++ b/crates/xfa/Cargo.toml @@ -13,9 +13,8 @@ workspace = true [dependencies] pdfcraft-cos = { workspace = true } pdfcraft-fonts = { workspace = true } -roxmltree = "0.21" +craft-xml = { workspace = true } base64 = "0.23" -png = "0.18" +craft-image-codecs = { workspace = true } sha2 = "0.11" -image = { version = "0.25", default-features = false, features = ["bmp", "gif", "tiff"] } thiserror = { workspace = true } diff --git a/crates/xfa/src/data/import.rs b/crates/xfa/src/data/import.rs index 006f4ff73b..0aab2cc1f1 100644 --- a/crates/xfa/src/data/import.rs +++ b/crates/xfa/src/data/import.rs @@ -46,7 +46,7 @@ impl ImportBudget { /// `n` as an [`ImportNode`], counting nodes and copied bytes against `budget`; `TooLarge` past /// the depth or either budget. -fn import_node(n: roxmltree::Node, depth: usize, budget: &mut ImportBudget) -> Result { +fn import_node(n: crate::xml::Node, depth: usize, budget: &mut ImportBudget) -> Result { if depth > MAX_IMPORT_DEPTH { return Err(XfaError::TooLarge(format!("the data file nests more than {MAX_IMPORT_DEPTH} levels deep"))); } @@ -192,12 +192,12 @@ pub fn parse_data_file(text: &str, expect_root: Option<&str>) -> Result MAX_IMPORT_DEPTH { return Err(XfaError::TooLarge(format!("the data file nests more than {MAX_IMPORT_DEPTH} levels deep"))); } - let opts = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; - let parsed = roxmltree::Document::parse_with_options(text, opts).map_err(|e| XfaError::Malformed(format!("the data file is not XML: {e}")))?; + let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let parsed = crate::xml::Document::parse_with_options(text, opts).map_err(|e| XfaError::Malformed(format!("the data file is not XML: {e}")))?; let root = parsed.root_element(); let local = root.tag_name().name(); - let in_xfa = |n: &roxmltree::Node| n.tag_name().namespace().is_none_or(|ns| ns == XFA_DATA_NS); - let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name && in_xfa(n); + let in_xfa = |n: &crate::xml::Node| n.tag_name().namespace().is_none_or(|ns| ns == XFA_DATA_NS); + let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name && in_xfa(n); let data = match local { "xdp" => { let ds = root.children().find(|n| is(n, "datasets")).ok_or_else(|| XfaError::Malformed("the XDP holds no datasets".into()))?; @@ -304,20 +304,20 @@ pub fn export_data(doc: &Document, xdp: bool, pdf_name: &str) -> Result Result, ()> { - let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; if crate::xml_nesting_depth(text) > crate::MAX_XML_DEPTH { return Err(()); } let wrapped = format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"); - let (src, parsed) = match roxmltree::Document::parse_with_options(text, opts()) { + let (src, parsed) = match crate::xml::Document::parse_with_options(text, opts()) { Ok(d) => (text, d), - Err(_) => (wrapped.as_str(), roxmltree::Document::parse_with_options(&wrapped, opts()).map_err(|_| ())?), + Err(_) => (wrapped.as_str(), crate::xml::Document::parse_with_options(&wrapped, opts()).map_err(|_| ())?), }; let Some(ds) = parsed.descendants().find(|n| n.is_element() && n.tag_name().name() == "datasets") else { return Ok(None) }; let data = ds.children().find(|n| n.is_element() && n.tag_name().name() == "data"); let data_alone = match data { Some(d) => { - let kids: Vec = d.children().filter(|c| c.is_element()).collect(); + let kids: Vec = d.children().filter(|c| c.is_element()).collect(); match kids.as_slice() { [one] => element_markup(src, *one).ok_or(())?, _ => element_markup(src, d).ok_or(())?, @@ -330,7 +330,7 @@ fn datasets_markup(text: &str) -> Result, ()> { /// `n` as written in `src`, with the namespace declarations in scope that its start tag lacks /// added to it, so it stands alone. -fn element_markup(src: &str, n: roxmltree::Node) -> Option { +fn element_markup(src: &str, n: crate::xml::Node) -> Option { let range = n.range(); let text = src.get(range.clone())?; let tag_end = start_tag_end(src, &range)?; diff --git a/crates/xfa/src/data/merge.rs b/crates/xfa/src/data/merge.rs index 0d573c71a9..a5d3a38212 100644 --- a/crates/xfa/src/data/merge.rs +++ b/crates/xfa/src/data/merge.rs @@ -49,7 +49,7 @@ struct Edit { } /// The edit that puts `inner` (markup) at the end of `node`'s content. -fn append_edit(src: &str, node: roxmltree::Node, inner: &str) -> Option { +fn append_edit(src: &str, node: crate::xml::Node, inner: &str) -> Option { let range = node.range(); let tag_end = start_tag_end(src, &range)?; if tag_end.checked_add(1)? == range.end && src.get(..tag_end)?.ends_with('/') { @@ -62,7 +62,7 @@ fn append_edit(src: &str, node: roxmltree::Node, inner: &str) -> Option { } /// The edit that makes `text` (escaped) the whole content of the leaf `node`. -fn replace_edit(src: &str, node: roxmltree::Node, text: &str) -> Option { +fn replace_edit(src: &str, node: crate::xml::Node, text: &str) -> Option { let range = node.range(); let tag_end = start_tag_end(src, &range)?; if tag_end.checked_add(1)? == range.end && src.get(..tag_end)?.ends_with('/') { @@ -99,7 +99,7 @@ pub(super) const WRAP_CLOSE: &str = ""; /// written. /// The prefix bound to the XFA data namespace where `n` is (`None`: unbound or the default /// namespace, which attributes can't use). -fn xfa_prefix(n: roxmltree::Node) -> Option { +fn xfa_prefix(n: crate::xml::Node) -> Option { n.namespaces().find(|ns| ns.uri() == XFA_DATA_NS).and_then(|ns| ns.name()).filter(|p| !p.is_empty()).map(str::to_string) } @@ -117,15 +117,15 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec) -> Merg warnings.push("The XFA data nests XML elements deeper than the reader accepts; the field values were not written to it".into()); return Merged::Failed; } - let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; // A packet of an array may use the prefixes the XDP envelope declares: read it inside // an element declaring the usual ones. let wrapped; - let (src, shift, parsed) = match roxmltree::Document::parse_with_options(text, opts()) { + let (src, shift, parsed) = match crate::xml::Document::parse_with_options(text, opts()) { Ok(d) => (text, 0, d), Err(first) => { wrapped = format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"); - match (within, roxmltree::Document::parse_with_options(&wrapped, opts())) { + match (within, crate::xml::Document::parse_with_options(&wrapped, opts())) { (Within::Packet, Ok(d)) => (wrapped.as_str(), WRAP_OPEN.len(), d), _ => { warnings.push(format!("The XFA data could not be read ({first}); the field values were not written to it")); @@ -134,7 +134,7 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec) -> Merg } } }; - let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name; + let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name; let datasets = parsed .descendants() .find(|n| is(n, "datasets") && n.tag_name().namespace() == Some(XFA_DATA_NS)) @@ -274,7 +274,7 @@ fn merge(text: &str, within: Within, op: Op, warnings: &mut Vec) -> Merg /// Whether `node` is a multi-select choice list's data group: element children only, every one /// a `value` holding text alone; or, with no children, an element marked `xfa:dataNode= /// "dataGroup"`, as a group with no picks is written (XFA 3.3 p. 150). -fn is_value_group(node: roxmltree::Node) -> bool { +fn is_value_group(node: crate::xml::Node) -> bool { let blank = node.children().filter(|c| c.is_text()).all(|c| c.text().is_some_and(|t| t.trim().is_empty())); let mut elements = node.children().filter(|c| c.is_element()).peekable(); if elements.peek().is_none() { @@ -284,14 +284,14 @@ fn is_value_group(node: roxmltree::Node) -> bool { } /// Whether `node` carries `xfa:dataNode="dataGroup"`. -fn marked_data_group(node: roxmltree::Node) -> bool { +fn marked_data_group(node: crate::xml::Node) -> bool { node.attribute((XFA_DATA_NS, "dataNode")) == Some("dataGroup") } /// The edit that marks the data group `node` as one, so that emptied of its picks it still loads /// as a group and not as a data value (XFA 3.3 p. 150): `xfa:dataNode="dataGroup"` added to its /// start tag, with the namespace declared there when the packet has no prefix for it. -fn mark_data_group_edit(src: &str, node: roxmltree::Node) -> Option { +fn mark_data_group_edit(src: &str, node: crate::xml::Node) -> Option { let range = node.range(); let tag_end = start_tag_end(src, &range)?; // Before `/>` of an empty tag, else before `>`. @@ -310,12 +310,12 @@ fn mark_data_group_edit(src: &str, node: roxmltree::Node) -> Option { } /// The edits that put `wants` into the `data` element. -fn merge_values(src: &str, data: roxmltree::Node, wants: &[Want], edits: &mut Vec, warnings: &mut Vec) { +fn merge_values(src: &str, data: crate::xml::Node, wants: &[Want], edits: &mut Vec, warnings: &mut Vec) { // Element children by local name, per element visited (built once each). - let mut index: HashMap>> = HashMap::new(); + let mut index: HashMap>> = HashMap::new(); let tree = data.document(); // Nodes to add, per existing element they go into. - let mut added: Vec<(roxmltree::NodeId, Build)> = Vec::new(); + let mut added: Vec<(crate::xml::NodeId, Build)> = Vec::new(); let mut budget = NewNodes::new(); for w in wants { let (escaped, dropped) = escape(&w.text); @@ -324,7 +324,7 @@ fn merge_values(src: &str, data: roxmltree::Node, wants: &[Want], edits: &mut Ve let mut have = 0; for (name, idx) in &w.path { let kids = index.entry(node.id()).or_insert_with(|| { - let mut by: HashMap> = HashMap::new(); + let mut by: HashMap> = HashMap::new(); for c in node.children().filter(|c| c.is_element()) { by.entry(c.tag_name().name().to_string()).or_default().push(c.id()); } @@ -465,9 +465,9 @@ pub(super) fn readable(text: &str, within: Within, warnings: &mut Vec) - warnings.push(format!("The XFA data would be larger than {} MB; it was not written", MAX_PACKET_WRITE >> 20)); return false; } - let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; - let ok = roxmltree::Document::parse_with_options(text, opts()).is_ok() - || (within == Within::Packet && roxmltree::Document::parse_with_options(&format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"), opts()).is_ok()); + let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let ok = crate::xml::Document::parse_with_options(text, opts()).is_ok() + || (within == Within::Packet && crate::xml::Document::parse_with_options(&format!("{WRAP_OPEN}{text}{WRAP_CLOSE}"), opts()).is_ok()); if !ok { warnings.push("The XFA data would not be well-formed XML after this change; it was not written".into()); } diff --git a/crates/xfa/src/data/path.rs b/crates/xfa/src/data/path.rs index 7fdba77071..d7bc2c7584 100644 --- a/crates/xfa/src/data/path.rs +++ b/crates/xfa/src/data/path.rs @@ -1,6 +1,6 @@ use super::{DataNode, DataPath, Field, MAX_DEPTH, MAX_NODES, Node, SomIndices, Template, XFA_DATA_NS}; -fn to_node(n: roxmltree::Node, depth: usize) -> DataNode { +fn to_node(n: crate::xml::Node, depth: usize) -> DataNode { let name = n.tag_name().name().to_string(); let children: Vec = if depth < MAX_DEPTH { n.children().filter(|c| c.is_element()).map(|c| to_node(c, depth + 1)).collect() } else { Vec::new() }; @@ -17,8 +17,8 @@ pub fn parse_datasets(xdp: &str) -> Option { if crate::xml_nesting_depth(xdp) > crate::MAX_XML_DEPTH { return None; } - let opts = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; - let doc = roxmltree::Document::parse_with_options(xdp, opts).ok()?; + let opts = crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let doc = crate::xml::Document::parse_with_options(xdp, opts).ok()?; let datasets = doc.descendants().find(|n| n.is_element() && n.tag_name().name() == "datasets")?; let data = datasets.children().find(|n| n.is_element() && n.tag_name().name() == "data")?; Some(to_node(data, 0)) @@ -33,16 +33,16 @@ pub(super) fn parse_datasets_packet(text: &str) -> Option { if crate::xml_nesting_depth(text) > crate::MAX_XML_DEPTH { return None; } - let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; let wrapped; - let doc = match roxmltree::Document::parse_with_options(text, opts()) { + let doc = match crate::xml::Document::parse_with_options(text, opts()) { Ok(d) => d, Err(_) => { wrapped = format!("{}{text}{}", super::merge::WRAP_OPEN, super::merge::WRAP_CLOSE); - roxmltree::Document::parse_with_options(&wrapped, opts()).ok()? + crate::xml::Document::parse_with_options(&wrapped, opts()).ok()? } }; - let is = |n: &roxmltree::Node, name: &str| n.is_element() && n.tag_name().name() == name; + let is = |n: &crate::xml::Node, name: &str| n.is_element() && n.tag_name().name() == name; let datasets = doc .descendants() .find(|n| is(n, "datasets") && n.tag_name().namespace() == Some(super::XFA_DATA_NS)) diff --git a/crates/xfa/src/image.rs b/crates/xfa/src/image.rs index e41cbd90a9..ce40dfa6b0 100644 --- a/crates/xfa/src/image.rs +++ b/crates/xfa/src/image.rs @@ -126,43 +126,20 @@ fn header_size(data: &[u8]) -> Option<(u32, u32)> { (w > 0 && h > 0).then_some((w, h)) } // The decoders read only the headers (TIFF: the first IFD) until asked for pixels. - Kind::Bmp => reader(&bmp_with_pixel_offset(data), image::ImageFormat::Bmp).into_dimensions().ok().filter(|&(w, h)| w > 0 && h > 0), + Kind::Bmp => craft_image_codecs::probe_size(&bmp_with_pixel_offset(data)).ok().filter(|&(w, h)| w > 0 && h > 0), Kind::Tiff => { - use image::{ImageDecoder, metadata::Orientation}; - let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder().ok()?; - let (w, h) = decoder.dimensions(); + let info = craft_image_codecs::tiff_info(data).ok()?; + let page = info.default_page().and_then(|i| info.pages.get(i))?; // Shown turned a quarter: the size viewers show is the stored one swapped. - let turned = matches!( - decoder.orientation().ok()?, - Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH - ); - let (w, h) = if turned { (h, w) } else { (w, h) }; + let turned = (5..=8).contains(&craft_image_codecs::tiff_orientation(data)); + let (w, h) = if turned { (page.height, page.width) } else { (page.width, page.height) }; (w > 0 && h > 0).then_some((w, h)) } } } -/// A TIFF's first page as viewers show it: turned and flipped as its Orientation tag says. -fn decode_tiff(data: &[u8]) -> image::ImageResult { - use image::ImageDecoder; - let mut decoder = reader(data, image::ImageFormat::Tiff).into_decoder()?; - let orientation = decoder.orientation()?; - let mut img = image::DynamicImage::from_decoder(decoder)?; - img.apply_orientation(orientation); - Ok(img) -} - -/// A decoder told the limits, so a frame larger than its header claims is refused before it is -/// allocated. -fn reader(data: &[u8], format: image::ImageFormat) -> image::ImageReader> { - let mut reader = image::ImageReader::new(std::io::Cursor::new(data)); - reader.set_format(format); - let mut limits = image::Limits::default(); - limits.max_image_width = Some(MAX_SIDE); - limits.max_image_height = Some(MAX_SIDE); - limits.max_alloc = Some(MAX_PIXELS * 4); - reader.limits(limits); - reader +fn decode_limits() -> craft_image_codecs::Limits { + craft_image_codecs::Limits { max_width: MAX_SIDE, max_height: MAX_SIDE, max_pixels: MAX_PIXELS, max_alloc: MAX_PIXELS * 4 } } /// Samples ready for an image XObject. @@ -192,23 +169,28 @@ pub fn decode(data: &[u8]) -> Result { } match kind(data) { Some(Kind::Png) => { - let mut dec = png::Decoder::new(std::io::Cursor::new(data)); - dec.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16); - let mut reader = dec.read_info().map_err(|e| bad(e.to_string()))?; - // The header's size is what the buffer is sized by: checked above. - let (fw, fh) = (reader.info().width, reader.info().height); + let opts = craft_image_codecs::DecodeOptions { keep_orientation: true, limits: decode_limits(), ..Default::default() }; + let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, data, &opts).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(_) => too_big(), + other => bad(other.to_string()), + })?; + let (fw, fh) = (img.width(), img.height()); if !fits(fw, fh) { return Err(too_big()); } - let mut buf = vec![0; reader.output_buffer_size().ok_or_else(|| bad("image too large".into()))?]; - let frame = reader.next_frame(&mut buf).map_err(|e| bad(e.to_string()))?; - let samples = buf.get(..frame.buffer_size()).ok_or_else(|| bad("short image data".into()))?; - let (channels, color_space) = match frame.color_type { - png::ColorType::Grayscale => (1, "DeviceGray"), - png::ColorType::GrayscaleAlpha => (2, "DeviceGray"), - png::ColorType::Rgb => (3, "DeviceRGB"), - png::ColorType::Rgba => (4, "DeviceRGB"), - png::ColorType::Indexed => return Err(bad("unexpected palette output".into())), + let (channels, color_space) = match img.layout() { + craft_image_codecs::ChannelLayout::Gray => (1, "DeviceGray"), + craft_image_codecs::ChannelLayout::GrayA => (2, "DeviceGray"), + craft_image_codecs::ChannelLayout::Rgb => (3, "DeviceRGB"), + craft_image_codecs::ChannelLayout::Rgba => (4, "DeviceRGB"), + _ => return Err(bad("unexpected PNG colour layout".into())), + }; + let samples: std::borrow::Cow<'_, [u8]> = if let Some(u8s) = img.as_u8() { + std::borrow::Cow::Borrowed(u8s) + } else if let Some(u16s) = img.to_u16_samples() { + std::borrow::Cow::Owned(u16s.into_iter().map(|v| (v >> 8) as u8).collect()) + } else { + return Err(bad("expected 8-bit or 16-bit samples".into())); }; let has_alpha = channels == 2 || channels == 4; let colour_n = channels - usize::from(has_alpha); @@ -221,24 +203,26 @@ pub fn decode(data: &[u8]) -> Result { } } let alpha = (has_alpha && alpha.iter().any(|a| *a != 255)).then_some(alpha); - Ok(Decoded { width: frame.width, height: frame.height, color_space, samples: colour, alpha }) + Ok(Decoded { width: fw, height: fh, color_space, samples: colour, alpha }) } Some(k @ (Kind::Gif | Kind::Bmp | Kind::Tiff)) => { - // A GIF's frame may be larger than the logical screen the header gives. let format = match k { - Kind::Gif => image::ImageFormat::Gif, - Kind::Bmp => image::ImageFormat::Bmp, - _ => image::ImageFormat::Tiff, + Kind::Gif => craft_image_codecs::Format::Gif, + Kind::Bmp => craft_image_codecs::Format::Bmp, + _ => craft_image_codecs::Format::Tiff, }; let data = if k == Kind::Bmp { bmp_with_pixel_offset(data) } else { std::borrow::Cow::Borrowed(data) }; - let img = if k == Kind::Tiff { decode_tiff(&data) } else { reader(&data, format).decode() } - .map_err(|e| if matches!(e, image::ImageError::Limits(_)) { too_big() } else { bad(e.to_string()) })?; - let rgba = img.into_rgba8(); - let (width, height) = rgba.dimensions(); + let opts = craft_image_codecs::DecodeOptions { keep_orientation: false, limits: decode_limits(), ..Default::default() }; + let img = craft_image_codecs::decode_as_with(format, &data, &opts).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(_) => too_big(), + other => bad(other.to_string()), + })?; + let raw = img.to_rgba8(); + let (width, height) = (img.width(), img.height()); if !fits(width, height) { return Err(too_big()); } - let px = rgba.as_raw().as_chunks::<4>().0; + let px = raw.as_chunks::<4>().0; let gray = px.iter().all(|p| p[0] == p[1] && p[1] == p[2]); let samples: Vec = if gray { px.iter().map(|p| p[0]).collect() } else { px.iter().flat_map(|p| [p[0], p[1], p[2]]).collect() }; let alpha = px.iter().any(|p| p[3] != 255).then(|| px.iter().map(|p| p[3]).collect()); diff --git a/crates/xfa/src/lib.rs b/crates/xfa/src/lib.rs index 31c59351ed..9fad61fae7 100644 --- a/crates/xfa/src/lib.rs +++ b/crates/xfa/src/lib.rs @@ -21,6 +21,7 @@ mod proto; pub mod script; pub mod text; mod traverse; +mod xml; use pdfcraft_cos::Document; diff --git a/crates/xfa/src/parse.rs b/crates/xfa/src/parse.rs index cc330b3d3b..ec1df0e7d0 100644 --- a/crates/xfa/src/parse.rs +++ b/crates/xfa/src/parse.rs @@ -1,8 +1,8 @@ //! Template XML → [`model`](crate::model). Only what the layout engine uses is read; everything //! else in the template is ignored, never rejected. +use crate::xml::Node as XmlNode; use base64::Engine; -use roxmltree::Node as XmlNode; use crate::XfaError; use crate::model::{ @@ -62,11 +62,11 @@ fn scale(v: &str) -> Option { } /// The first child element of `n` named `name` (by local name, in any namespace). -fn child<'a, 'd>(n: XmlNode<'a, 'd>, name: &str) -> Option> { +fn child<'a>(n: XmlNode<'a>, name: &str) -> Option> { n.children().find(|c| c.is_element() && c.tag_name().name() == name) } -fn children<'a, 'd>(n: XmlNode<'a, 'd>, name: &'a str) -> impl Iterator> + 'a { +fn children<'a>(n: XmlNode<'a>, name: &'a str) -> impl Iterator> + 'a { n.children().filter(move |c| c.is_element() && c.tag_name().name() == name) } @@ -415,7 +415,7 @@ fn parse_props(n: XmlNode, warnings: &mut Vec) -> Props { /// The property elements under `n`, whose tag is `within` (empty for the object itself): /// `extras` in the object and inside one (forms keep named values there), `script` in a /// `validate` or a `calculate` (XFA 3.3 p. 880, p. 641, p. 827). - fn elements<'a, 'd>(n: XmlNode<'a, 'd>, within: &str) -> impl Iterator> + use<'a, 'd> { + fn elements<'a>(n: XmlNode<'a>, within: &str) -> impl Iterator> + use<'a> { let (extras, script) = (within.is_empty() || within == "extras", matches!(within, "validate" | "calculate")); let ns = n.tag_name().namespace(); n.children().filter(move |c| { @@ -1266,8 +1266,8 @@ pub fn parse(xml: &str) -> Result<(Template, Vec), XfaError> { crate::MAX_XML_DEPTH ))); } - let opts = || roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; - let doc = roxmltree::Document::parse_with_options(xml, opts()).map_err(|e| XfaError::Malformed(format!("template XML: {e}")))?; + let opts = || crate::xml::ParsingOptions { allow_dtd: false, nodes_limit: MAX_NODES, ..Default::default() }; + let doc = crate::xml::Document::parse_with_options(xml, opts()).map_err(|e| XfaError::Malformed(format!("template XML: {e}")))?; let root = doc.root_element(); // The XDP's config packet has a