From 9430f2807e1c5f1cf852bad369feb10774759965 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:17:04 -0500 Subject: [PATCH 01/15] Add shared image codec facade --- ATTRIBUTION.md | 1 + Cargo.lock | 404 +++- Cargo.toml | 1 + NOTICE | 8 + crates/image-codecs/Cargo.toml | 45 + crates/image-codecs/README.md | 323 +++ crates/image-codecs/fuzz/.gitignore | 5 + crates/image-codecs/fuzz/Cargo.toml | 78 + .../fuzz/fuzz_targets/decode_any.rs | 18 + .../fuzz/fuzz_targets/decode_exr.rs | 12 + .../fuzz/fuzz_targets/decode_gif.rs | 13 + .../fuzz/fuzz_targets/decode_heif.rs | 15 + .../fuzz/fuzz_targets/decode_jpeg.rs | 12 + .../fuzz/fuzz_targets/decode_png.rs | 12 + .../fuzz/fuzz_targets/decode_tiff.rs | 20 + .../fuzz/fuzz_targets/decode_webp.rs | 13 + crates/image-codecs/src/cicp.rs | 182 ++ crates/image-codecs/src/codecs.rs | 25 + crates/image-codecs/src/codecs/dds.rs | 556 +++++ crates/image-codecs/src/codecs/dds_bc.rs | 368 +++ crates/image-codecs/src/codecs/deep_exr.rs | 786 ++++++ crates/image-codecs/src/codecs/dicom.rs | 517 ++++ crates/image-codecs/src/codecs/dpx.rs | 886 +++++++ crates/image-codecs/src/codecs/exr.rs | 441 ++++ .../src/codecs/exr_cryptomatte.rs | 266 ++ crates/image-codecs/src/codecs/fits.rs | 855 +++++++ crates/image-codecs/src/codecs/gif_frames.rs | 82 + crates/image-codecs/src/codecs/gif_palette.rs | 126 + crates/image-codecs/src/codecs/heif.rs | 126 + crates/image-codecs/src/codecs/iff.rs | 544 +++++ crates/image-codecs/src/codecs/jpeg.rs | 931 +++++++ crates/image-codecs/src/codecs/jxl.rs | 159 ++ crates/image-codecs/src/codecs/pcx.rs | 139 ++ crates/image-codecs/src/codecs/png.rs | 2139 +++++++++++++++++ crates/image-codecs/src/codecs/pnm.rs | 355 +++ crates/image-codecs/src/codecs/tga.rs | 175 ++ crates/image-codecs/src/codecs/tiff.rs | 220 ++ crates/image-codecs/src/codecs/tiff/bands.rs | 509 ++++ crates/image-codecs/src/codecs/tiff/decode.rs | 932 +++++++ crates/image-codecs/src/codecs/tiff/encode.rs | 659 +++++ crates/image-codecs/src/codecs/tiff/tags.rs | 99 + crates/image-codecs/src/codecs/tiff/ycbcr.rs | 222 ++ crates/image-codecs/src/codecs/tiff_exif.rs | 238 ++ crates/image-codecs/src/codecs/tiff_ifd.rs | 502 ++++ crates/image-codecs/src/codecs/via_image.rs | 455 ++++ crates/image-codecs/src/codecs/vp8.rs | 848 +++++++ .../src/codecs/vp8/bool_encoder.rs | 273 +++ crates/image-codecs/src/codecs/vp8/predict.rs | 382 +++ crates/image-codecs/src/codecs/vp8/tables.rs | 593 +++++ .../image-codecs/src/codecs/vp8/transform.rs | 183 ++ crates/image-codecs/src/codecs/webp.rs | 254 ++ crates/image-codecs/src/codecs/xpm.rs | 226 ++ .../image-codecs/src/codecs/xpm/x11_colors.rs | 666 +++++ crates/image-codecs/src/error.rs | 31 + crates/image-codecs/src/exif_color.rs | 138 ++ crates/image-codecs/src/exr_stream.rs | 294 +++ crates/image-codecs/src/fidelity.rs | 282 +++ crates/image-codecs/src/format.rs | 425 ++++ crates/image-codecs/src/image.rs | 1028 ++++++++ crates/image-codecs/src/lab.rs | 56 + crates/image-codecs/src/lib.rs | 421 ++++ crates/image-codecs/src/options.rs | 364 +++ crates/image-codecs/src/orientation.rs | 476 ++++ crates/image-codecs/src/resolution.rs | 575 +++++ crates/image-codecs/src/source_settings.rs | 355 +++ crates/image-codecs/src/web.rs | 525 ++++ crates/image-codecs/tests/it/camera_raw.rs | 134 ++ crates/image-codecs/tests/it/common/mod.rs | 122 + .../image-codecs/tests/it/common/tiffgen.rs | 327 +++ crates/image-codecs/tests/it/dds.rs | 410 ++++ crates/image-codecs/tests/it/dds_write.rs | 375 +++ .../image-codecs/tests/it/decode_warnings.rs | 511 ++++ crates/image-codecs/tests/it/deep_exr.rs | 992 ++++++++ crates/image-codecs/tests/it/detect_caps.rs | 309 +++ crates/image-codecs/tests/it/dpx.rs | 521 ++++ .../image-codecs/tests/it/exr_cryptomatte.rs | 271 +++ crates/image-codecs/tests/it/exr_multipart.rs | 334 +++ crates/image-codecs/tests/it/exr_stream.rs | 161 ++ crates/image-codecs/tests/it/fidelity.rs | 243 ++ crates/image-codecs/tests/it/gif_frames.rs | 168 ++ crates/image-codecs/tests/it/heif.rs | 194 ++ crates/image-codecs/tests/it/limits.rs | 670 ++++++ crates/image-codecs/tests/it/main.rs | 39 + crates/image-codecs/tests/it/malformed.rs | 270 +++ crates/image-codecs/tests/it/metadata.rs | 340 +++ crates/image-codecs/tests/it/orientation.rs | 670 ++++++ crates/image-codecs/tests/it/pcx.rs | 137 ++ crates/image-codecs/tests/it/png_jpeg.rs | 646 +++++ crates/image-codecs/tests/it/pnm_headers.rs | 82 + crates/image-codecs/tests/it/resolution.rs | 546 +++++ crates/image-codecs/tests/it/roundtrip.rs | 348 +++ .../image-codecs/tests/it/source_settings.rs | 111 + crates/image-codecs/tests/it/tga.rs | 91 + crates/image-codecs/tests/it/tiff_bands.rs | 367 +++ crates/image-codecs/tests/it/tiff_bench.rs | 124 + crates/image-codecs/tests/it/tiff_exif.rs | 262 ++ crates/image-codecs/tests/it/tiff_lab.rs | 109 + crates/image-codecs/tests/it/tiff_pages.rs | 841 +++++++ .../image-codecs/tests/it/tiff_photoshop.rs | 436 ++++ crates/image-codecs/tests/it/tiff_pyramid.rs | 184 ++ .../image-codecs/tests/it/tiff_regressions.rs | 176 ++ crates/image-codecs/tests/it/tiff_ycbcr.rs | 259 ++ crates/image-codecs/tests/it/webp_frames.rs | 166 ++ crates/image-codecs/tests/it/webp_lossy.rs | 140 ++ crates/image-codecs/tests/it/xpm.rs | 123 + crates/image-codecs/tests/jxl_export.rs | 110 + crates/image-codecs/tests/pnm_peak_memory.rs | 36 + crates/raster-codecs/Cargo.toml | 3 +- crates/raster-codecs/README.md | 6 +- crates/raster-codecs/src/common.rs | 75 +- 110 files changed, 35260 insertions(+), 48 deletions(-) create mode 100644 crates/image-codecs/Cargo.toml create mode 100644 crates/image-codecs/README.md create mode 100644 crates/image-codecs/fuzz/.gitignore create mode 100644 crates/image-codecs/fuzz/Cargo.toml create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_any.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_png.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs create mode 100644 crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs create mode 100644 crates/image-codecs/src/cicp.rs create mode 100644 crates/image-codecs/src/codecs.rs create mode 100644 crates/image-codecs/src/codecs/dds.rs create mode 100644 crates/image-codecs/src/codecs/dds_bc.rs create mode 100644 crates/image-codecs/src/codecs/deep_exr.rs create mode 100644 crates/image-codecs/src/codecs/dicom.rs create mode 100644 crates/image-codecs/src/codecs/dpx.rs create mode 100644 crates/image-codecs/src/codecs/exr.rs create mode 100644 crates/image-codecs/src/codecs/exr_cryptomatte.rs create mode 100644 crates/image-codecs/src/codecs/fits.rs create mode 100644 crates/image-codecs/src/codecs/gif_frames.rs create mode 100644 crates/image-codecs/src/codecs/gif_palette.rs create mode 100644 crates/image-codecs/src/codecs/heif.rs create mode 100644 crates/image-codecs/src/codecs/iff.rs create mode 100644 crates/image-codecs/src/codecs/jpeg.rs create mode 100644 crates/image-codecs/src/codecs/jxl.rs create mode 100644 crates/image-codecs/src/codecs/pcx.rs create mode 100644 crates/image-codecs/src/codecs/png.rs create mode 100644 crates/image-codecs/src/codecs/pnm.rs create mode 100644 crates/image-codecs/src/codecs/tga.rs create mode 100644 crates/image-codecs/src/codecs/tiff.rs create mode 100644 crates/image-codecs/src/codecs/tiff/bands.rs create mode 100644 crates/image-codecs/src/codecs/tiff/decode.rs create mode 100644 crates/image-codecs/src/codecs/tiff/encode.rs create mode 100644 crates/image-codecs/src/codecs/tiff/tags.rs create mode 100644 crates/image-codecs/src/codecs/tiff/ycbcr.rs create mode 100644 crates/image-codecs/src/codecs/tiff_exif.rs create mode 100644 crates/image-codecs/src/codecs/tiff_ifd.rs create mode 100644 crates/image-codecs/src/codecs/via_image.rs create mode 100644 crates/image-codecs/src/codecs/vp8.rs create mode 100644 crates/image-codecs/src/codecs/vp8/bool_encoder.rs create mode 100644 crates/image-codecs/src/codecs/vp8/predict.rs create mode 100644 crates/image-codecs/src/codecs/vp8/tables.rs create mode 100644 crates/image-codecs/src/codecs/vp8/transform.rs create mode 100644 crates/image-codecs/src/codecs/webp.rs create mode 100644 crates/image-codecs/src/codecs/xpm.rs create mode 100644 crates/image-codecs/src/codecs/xpm/x11_colors.rs create mode 100644 crates/image-codecs/src/error.rs create mode 100644 crates/image-codecs/src/exif_color.rs create mode 100644 crates/image-codecs/src/exr_stream.rs create mode 100644 crates/image-codecs/src/fidelity.rs create mode 100644 crates/image-codecs/src/format.rs create mode 100644 crates/image-codecs/src/image.rs create mode 100644 crates/image-codecs/src/lab.rs create mode 100644 crates/image-codecs/src/lib.rs create mode 100644 crates/image-codecs/src/options.rs create mode 100644 crates/image-codecs/src/orientation.rs create mode 100644 crates/image-codecs/src/resolution.rs create mode 100644 crates/image-codecs/src/source_settings.rs create mode 100644 crates/image-codecs/src/web.rs create mode 100644 crates/image-codecs/tests/it/camera_raw.rs create mode 100644 crates/image-codecs/tests/it/common/mod.rs create mode 100644 crates/image-codecs/tests/it/common/tiffgen.rs create mode 100644 crates/image-codecs/tests/it/dds.rs create mode 100644 crates/image-codecs/tests/it/dds_write.rs create mode 100644 crates/image-codecs/tests/it/decode_warnings.rs create mode 100644 crates/image-codecs/tests/it/deep_exr.rs create mode 100644 crates/image-codecs/tests/it/detect_caps.rs create mode 100644 crates/image-codecs/tests/it/dpx.rs create mode 100644 crates/image-codecs/tests/it/exr_cryptomatte.rs create mode 100644 crates/image-codecs/tests/it/exr_multipart.rs create mode 100644 crates/image-codecs/tests/it/exr_stream.rs create mode 100644 crates/image-codecs/tests/it/fidelity.rs create mode 100644 crates/image-codecs/tests/it/gif_frames.rs create mode 100644 crates/image-codecs/tests/it/heif.rs create mode 100644 crates/image-codecs/tests/it/limits.rs create mode 100644 crates/image-codecs/tests/it/main.rs create mode 100644 crates/image-codecs/tests/it/malformed.rs create mode 100644 crates/image-codecs/tests/it/metadata.rs create mode 100644 crates/image-codecs/tests/it/orientation.rs create mode 100644 crates/image-codecs/tests/it/pcx.rs create mode 100644 crates/image-codecs/tests/it/png_jpeg.rs create mode 100644 crates/image-codecs/tests/it/pnm_headers.rs create mode 100644 crates/image-codecs/tests/it/resolution.rs create mode 100644 crates/image-codecs/tests/it/roundtrip.rs create mode 100644 crates/image-codecs/tests/it/source_settings.rs create mode 100644 crates/image-codecs/tests/it/tga.rs create mode 100644 crates/image-codecs/tests/it/tiff_bands.rs create mode 100644 crates/image-codecs/tests/it/tiff_bench.rs create mode 100644 crates/image-codecs/tests/it/tiff_exif.rs create mode 100644 crates/image-codecs/tests/it/tiff_lab.rs create mode 100644 crates/image-codecs/tests/it/tiff_pages.rs create mode 100644 crates/image-codecs/tests/it/tiff_photoshop.rs create mode 100644 crates/image-codecs/tests/it/tiff_pyramid.rs create mode 100644 crates/image-codecs/tests/it/tiff_regressions.rs create mode 100644 crates/image-codecs/tests/it/tiff_ycbcr.rs create mode 100644 crates/image-codecs/tests/it/webp_frames.rs create mode 100644 crates/image-codecs/tests/it/webp_lossy.rs create mode 100644 crates/image-codecs/tests/it/xpm.rs create mode 100644 crates/image-codecs/tests/jxl_export.rs create mode 100644 crates/image-codecs/tests/pnm_peak_memory.rs diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 965b348e4..19d38973d 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -82,6 +82,7 @@ and the exact transfer revision are recorded in each vendored crate's SOURCE.jso | `hayro-interpret 0.7.0 / src/font/generated/metrics.rs` | Adobe (Core 14 AFM metrics, PDF specification encodings); tables by The Hayro Authors | https://github.com/storytold/pdfcraft/blob/7437bde378f43856eada40af25b983812e615e8a/vendor/hayro-interpret/src/font/generated/metrics.rs | MIT OR Apache-2.0 | `2ec9e608573eda459de1e1d9bda3bb494315e36bee885acc88912a11fe0caa41` | Glyph widths and encodings for non-embedded standard fonts. Non-visual dependency data; existing embedding unchanged | | `crates/audio/src/ac3/golden_frames.bin` | FilmCraft contributors (original work: 12 AC-3 and E-AC-3 syncframes that ffmpeg's `ac3` and `eac3` encoders made from tones and seeded noise synthesised by ffmpeg's lavfi generators; no third-party or recorded audio) | [FilmCraft `crates/ac3`](https://github.com/storytold/filmcraft/tree/1c9c2e82/crates/ac3/src) | MIT OR Apache-2.0 | | `crates/audio/tests/fixtures/` | SoundCraft and craft-libs contributors (original work: 0.1 s test tones from ffmpeg's `sine` generator and hand-built silent MPEG frames; no third-party or recorded audio; commands in `crates/audio/tests/fixtures/README.md`) | [SoundCraft `crates/audio-io/tests/fixtures`](https://github.com/storytold/soundcraft/tree/e86d09a/crates/audio-io/tests/fixtures); `tone.mov`, `tone.mka`, `tone.opus`, `tone.ac3` and `tone.eac3` made in this repository | MIT OR Apache-2.0 | +| `crates/image-codecs/src/codecs/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | , via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/5000240c6/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) | | `crates/raster-codecs/src/xpm/x11_colors.rs` | X.Org Foundation and X11 contributors (X11 `rgb.txt` colour names and values, normalised into a lookup table) | , via [PhotoCraft `crates/codecs`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/codecs/src/codecs/xpm) | MIT (X.Org app/rgb) | | `crates/psd/tests/fixtures/atn/*.atn` (seven synthetic action sets, encoded field by field; `crates/psd/tests/fixtures/atn/README.md`) | PhotoCraft contributors | [PhotoCraft `crates/psd/tests/fixtures/atn`](https://github.com/storytold/photocraft/tree/63b641b55e54bf8f5c83efd977fdb16a2c255431/crates/psd/tests/fixtures/atn) | MIT OR Apache-2.0 | | `crates/lottie/tests/fixtures/` | EffectCraft contributors (original work: four hand-written Lottie JSON documents, written from the public Lottie format description; not exported from any Adobe product or third-party file) | [EffectCraft `crates/lottie/tests/fixtures`](https://github.com/storytold/effectcraft/tree/e18f48fb/crates/lottie/tests/fixtures) | MIT OR Apache-2.0 | diff --git a/Cargo.lock b/Cargo.lock index 4b671fb2e..c5ff17d87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,24 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aligned" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685" +dependencies = [ + "as-slice", +] + +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + [[package]] name = "alloc-no-stdlib" version = "2.0.4" @@ -180,6 +198,23 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "argon2" version = "0.5.3" @@ -214,6 +249,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -366,6 +410,49 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "av-scenechange" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394" +dependencies = [ + "aligned", + "anyhow", + "arg_enum_proc_macro", + "arrayvec", + "log", + "num-rational", + "num-traits", + "pastey", + "rayon", + "thiserror 2.0.21", + "v_frame", + "y4m", +] + +[[package]] +name = "av1-grain" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom", + "num-rational", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7178fe5f7d460b13895ebb9dcb28a3a6216d2df2574a0806cb51b555d297f38" +dependencies = [ + "arrayvec", +] + [[package]] name = "aws-lc-rs" version = "1.18.1" @@ -493,6 +580,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "bitstream-io" +version = "4.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f" +dependencies = [ + "no_std_io2", +] + [[package]] name = "bitvec" version = "1.1.1" @@ -703,6 +799,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "built" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9" + [[package]] name = "bumpalo" version = "3.20.3" @@ -1628,6 +1730,30 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +dependencies = [ + "craft-heif", + "craft-jpeg", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder 0.6.1", + "jxl-oxide", + "png 0.18.1", + "proptest", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + [[package]] name = "craft-jmap" version = "0.1.0" @@ -2106,6 +2232,7 @@ dependencies = [ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ + "craft-image-codecs", "image", "rayon", "thiserror 2.0.21", @@ -3135,7 +3262,7 @@ dependencies = [ "bitflags 2.13.2", "emath", "epaint", - "itertools", + "itertools 0.15.0", "log", "nohash-hasher", "profiling", @@ -3192,7 +3319,7 @@ dependencies = [ "ahash", "egui", "enum-map", - "itertools", + "itertools 0.15.0", "log", "profiling", "resvg", @@ -3421,6 +3548,26 @@ name = "epaint_default_fonts" version = "0.37.0" source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -3477,6 +3624,7 @@ dependencies = [ "lebe", "miniz_oxide 0.8.9", "multiversion", + "rayon-core", "smallvec", "zune-inflate", ] @@ -4696,6 +4844,9 @@ dependencies = [ "moxcms", "num-traits", "png 0.18.1", + "qoi", + "ravif", + "rgb", "tiff", "zune-core", "zune-jpeg", @@ -4729,6 +4880,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" +[[package]] +name = "imgref" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" + [[package]] name = "indexmap" version = "2.14.2" @@ -4758,6 +4915,17 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "io-extras" version = "0.19.0" @@ -4805,6 +4973,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.15.0" @@ -5069,6 +5246,8 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25f15eb830aa77a7f21148d72e153562a26bfe570139bd4922eab1908dd499d3" dependencies = [ + "rayon", + "rayon-core", "tracing", ] @@ -5229,6 +5408,16 @@ version = "0.2.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + [[package]] name = "libloading" version = "0.8.9" @@ -5316,6 +5505,15 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "loop9" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fae87c125b03c1d2c0150c90365d7d6bcc53fb73a9acaef207d2d065860f062" +dependencies = [ + "imgref", +] + [[package]] name = "lru-slab" version = "0.1.3" @@ -5328,6 +5526,15 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", +] + [[package]] name = "md-5" version = "0.10.6" @@ -5543,6 +5750,12 @@ dependencies = [ "jni-sys 0.3.1", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nix" version = "0.31.3" @@ -5555,6 +5768,15 @@ dependencies = [ "libc", ] +[[package]] +name = "no_std_io2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" +dependencies = [ + "memchr", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -5570,6 +5792,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -5579,6 +5807,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint-dig" version = "0.8.6" @@ -5611,6 +5849,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "num-integer" version = "0.1.47" @@ -5630,6 +5879,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -6187,6 +6447,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" + [[package]] name = "pbkdf2" version = "0.13.0" @@ -6713,6 +6985,19 @@ name = "profiling" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" +dependencies = [ + "quote", + "syn 2.0.119", +] [[package]] name = "proptest" @@ -6740,6 +7025,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" @@ -6861,10 +7155,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" dependencies = [ "libc", - "rand_chacha", + "rand_chacha 0.3.1", "rand_core 0.6.4", ] +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + [[package]] name = "rand" version = "0.10.3" @@ -6886,6 +7190,16 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -6895,6 +7209,15 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "rand_core" version = "0.10.1" @@ -6925,6 +7248,55 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" +[[package]] +name = "rav1e" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b" +dependencies = [ + "aligned-vec", + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-scenechange", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.14.0", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "paste", + "profiling", + "rand 0.9.5", + "rand_chacha 0.9.0", + "simd_helpers", + "thiserror 2.0.21", + "v_frame", + "wasm-bindgen", +] + +[[package]] +name = "ravif" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e52310197d971b0f5be7fe6b57530dcd27beb35c1b013f29d66c1ad73fbbcc45" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error 2.0.1", + "rav1e", + "rgb", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -7962,6 +8334,15 @@ dependencies = [ "simdutf8", ] +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "simdutf8" version = "0.1.5" @@ -9176,6 +9557,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "v_frame" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + [[package]] name = "vello_common" version = "0.0.8" @@ -10198,6 +10590,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" +[[package]] +name = "y4m" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" + [[package]] name = "yeslogic-fontconfig-sys" version = "6.0.1" diff --git a/Cargo.toml b/Cargo.toml index 629bab64c..53395992e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,6 +100,7 @@ craft-prepress = { path = "crates/prepress" } craft-print = { path = "crates/print" } craft-proof = { path = "crates/proof" } craft-raster-codecs = { path = "crates/raster-codecs" } +craft-image-codecs = { path = "crates/image-codecs" } craft-psd = { path = "crates/psd" } craft-pst = { path = "crates/pst" } craft-quit = { path = "crates/quit" } diff --git a/NOTICE b/NOTICE index c6583cc70..10e0fcc19 100644 --- a/NOTICE +++ b/NOTICE @@ -89,6 +89,14 @@ The optional PDF writer bundles four CC0 ICC profiles by Clinton Ingram. See ATT Source provenance +crates/image-codecs (craft-image-codecs) is moved from PhotoCraft's standalone `photocraft-codecs` +crate, including its bounded decoders, encoders, format registry, image model, metadata handling, +orientation, fidelity warnings, and tests, from revision 5000240c6: +https://github.com/storytold/photocraft +Copyright (c) 2026 ArtCraft Team and the PhotoCraft contributors. The format implementations are +clean-room work based on their public specifications; dependency licences and X.Org's `rgb.txt` +attribution are listed in ATTRIBUTION.md. + crates/raw (craft-raw) is moved from LightCraft, with behaviour ported from two other readers: - LightCraft, crates/raw (src/lib.rs, dng.rs, dngwrite.rs, tiffraw.rs, unpack.rs, ljpeg.rs, jxl.rs, opcodes.rs, profile.rs, gaintable.rs, semantic.rs, preview.rs, vendor/crw.rs, vendor/hasselblad.rs, diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml new file mode 100644 index 000000000..30f7f7046 --- /dev/null +++ b/crates/image-codecs/Cargo.toml @@ -0,0 +1,45 @@ +[package] +name = "craft-image-codecs" +description = "Bounded, depth- and metadata-preserving codecs for flat raster image formats" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true + +[features] +corpus = [] +default = [] +avif = ["image/avif"] +heif = ["dep:craft-heif"] + +[dependencies] +thiserror = { workspace = true } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff", "gif", "bmp", "tga", "ico", "qoi", "hdr"] } +png = "0.18" +tiff = { version = "0.11", default-features = false, features = ["deflate", "fax", "jpeg", "lzw"] } +exr = { version = "1.73", default-features = false } +half = "2" +serde_json = { workspace = true } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } +image-webp = "0.2" +zune-jpeg = "0.5" +zune-core = "0.5" +jpeg-encoder = "0.6" +craft-jpeg = { workspace = true } +zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } +craft-heif = { workspace = true, optional = true } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +rayon = { workspace = true } +exr = { version = "1.73", default-features = false, features = ["rayon"] } + +[dev-dependencies] +proptest = { workspace = true } +image = { version = "0.25", default-features = false, features = ["png", "jpeg", "tiff"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] +jxl-oxide = { version = "=0.12.6", default-features = false, features = ["rayon"] } diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md new file mode 100644 index 000000000..0fdcd6ae4 --- /dev/null +++ b/crates/image-codecs/README.md @@ -0,0 +1,323 @@ +# craft-image-codecs + +Codecs for flat raster image formats, written in pure Rust. This crate defines its own `Image` +type and shares its baseline and extended JPEG decoders through `craft-jpeg`, and its optional +HEIF decoder through `craft-heif`. It builds for `wasm32-unknown-unknown` and does no filesystem +I/O: decoders take `&[u8]` and encoders return `Vec`. + +It follows the "Avoiding GIMP's hole" rules in `docs/architecture.md` §1.1: + +* **Symmetric.** Every format we write, we can also read. The only exceptions are listed in + `ASYMMETRIC_EXCEPTIONS`, each with a reason. The test suite checks this. +* **Depth-preserving.** An image can hold U8, U16, F16 or F32 samples, and each format keeps + what it can hold. +* **Metadata-preserving.** ICC, EXIF, XMP, DPI and text are kept wherever the format supports + them. +* **Honest.** `caps(format)` says what each format holds, and + `fidelity_warnings(&image, format)` lists what an export will lose before you write it. Both + functions use the same encode plan as `encode`, so a warning appears exactly when the file + changes. On the way in, `Image::warnings` lists what the decoded image doesn't show: frames or + pages left out, or data that ended early. + +```rust +use craft_image_codecs::*; + +let img = decode(&bytes)?; // detect + decode (default Limits) +for w in fidelity_warnings(&img, Format::Jpeg) { // e.g. "16-bit will be reduced to 8-bit" + println!("{w}"); +} +let out = encode(&img, Format::Tiff, &EncodeOptions::default())?; +``` + +```toml +craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "" } +``` + +## Capability matrix (default build) + +| Format | Read | Write | Depths (native) | Layouts (native) | Alpha | ICC | EXIF | XMP | DPI | Text | Lossy write | Backend | +|---|---|---|---|---|---|---|---|---|---|---|---|---| +| PNG | yes | yes | U8, U16 | Gray, GrayA, RGB, RGBA | yes | yes (iCCP) | yes (eXIf) | yes (iTXt `XML:com.adobe.xmp`) | yes (pHYs) | yes (tEXt/zTXt/iTXt) | no | `png` | +| JPEG | yes | yes | U8 | Gray, RGB, CMYK | no | yes (multi-segment APP2) | yes (APP1) | yes (APP1) | yes (JFIF) | no | yes | `zune-jpeg` / `jpeg-encoder` | +| TIFF | yes | yes | U8, U16, F32 | all six (CMYK, CMYK+A included) | yes | yes (tag 34675) | no | yes (tag 700) | yes | yes (Description, Make, Model, Software, DateTime, Artist, Copyright) | no | `tiff` | +| WebP | yes | yes | U8 | RGB, RGBA | yes | yes | yes | yes | no | no | yes (VP8, when `webp_lossless` is off) | `image-webp` / built-in VP8 | +| GIF | yes | yes | U8 | RGBA | 1-bit | no | no | no | no | no | yes (256-colour palette) | `image` | +| BMP | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| TGA | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | no | `image` (read) / built in (RLE write, one scanline per packet) | +| ICO | yes | yes | U8 | RGBA (at most 256×256) | yes | no | no | no | no | no | no | `image` | +| DDS | yes | yes | U8 | Gray, GrayA, RGB, RGBA | yes | no | no | no | no | no | yes (BC1–BC3, when `EncodeOptions::dds` asks for one) | built in | +| Netpbm (PBM/PGM/PPM/PAM/PFM) | yes | yes | U8, U16, F32 (float: PFM gray/RGB only) | all six (PAM `CMYK`/`CMYK_ALPHA`) | yes (PAM) | no | no | no | no | no | no | built in | +| QOI | yes | yes | U8 | RGB, RGBA | yes | no | no | no | no | no | no | `image` | +| OpenEXR | yes | yes | F16, F32 | Y, YA, RGB, RGBA | yes | no | no | no | no | no | no | `exr` | +| Radiance HDR | yes | yes | F32 | RGB | no | no | no | no | no | no | yes (RGBE) | `image` | +| FITS | yes | yes | U8, U16, F32 | Gray, RGB | no | no | no | no | no | yes (header cards) | no | built in | +| JPEG XL (.jxl) | **no** (see #2868) | yes | U8, U16 | Gray, RGB, RGBA (GrayA expands to RGBA) | yes | no (bare codestream) | no | no | no | no | no | `zune-jpegxl` | +| DPX | yes | yes | U8, U16 (10/12-bit code values read and written exactly through U16) | Gray, RGB, RGBA | yes | no | no | no | no | DPX header fields only (creator, project, copyright, creation time, transfer, colorimetric, frame position, frame rate, time code) | no | built in | +| Cineon | yes | yes | U16 (10-bit code values read and written exactly through U16) | Gray, RGB | no | no | no | no | no | no | no | built in | +| DICOM | yes | **no** | U8, U16 | Gray, RGB | no | no | no | no | no | no | n/a | built in | +| Amiga IFF (ILBM/PBM) | yes | yes (24-bit ILBM) | U8 | RGB, RGBA | yes (1-bit mask plane on write) | no | no | no | no | no | no | built in | +| AVIF | **no** | only with feature `avif` | U8 | RGB, RGBA | yes | no | no | no | no | no | yes | `image` + `ravif` | +| HEIF/HEIC | with feature `heif` | **no** | U8, U16 (10/12-bit decodes to U16) | RGB, RGBA | yes (auxiliary alpha) | yes (`colr` prof) | yes | yes | no | no | n/a | `heic-rs` | + +"Native" means the data is stored and read back without conversion. Anything else is converted by +the encode plan, and `fidelity_warnings` reports the conversion when it loses information: + +* A float image written to an integer format goes to U16 where the format supports it. Values + outside 0..1 raise `RangeClipped`. +* Integer data written to a float-only format (EXR, HDR) becomes F32 without loss. +* CMYK is converted to RGB with a naive formula that is not colour-managed. `CmykConverted` is + raised and the ICC profile is dropped, because a CMYK profile does not describe RGB data. +* Gray written to an RGB-only format is expanded to RGB without loss, so no warning is raised. + +`Image::convert` applies the same ICC rule: it keeps the profile only when the colour model stays +the same. + +Palette BMP and PNG pixels decode as RGB(A) in `Image`; `bmp_palette` and `png_palette` expose the +embedded table (and a PNG's transparent entry) so `photocraft-io` can open them in Indexed Color mode. +`bmp_palette` returns the colours the header declares, not the decoder's 256-entry padding. +`encode_bmp_indexed` writes the inverse: indices plus a table as an 8-bit palette BMP. + +## Documented asymmetries and limitations + +* **JPEG XL export.** Writes lossless 8/16-bit bare codestreams with `zune-jpegxl`. + Native layouts are Gray, RGB and RGBA. GrayA is expanded to RGBA at the + original depth. For 16-bit RGBA (including expanded GrayA), we correct + zune-jpegxl 0.5.2's extra-channel header, which otherwise describes the + 16-bit alpha samples as 8-bit. This preserves alpha precision; the independent + jxl-oxide decoder test checks the actual gray and alpha samples. The expansion + is not a native GrayA layout. No ICC, EXIF, XMP or DPI container boxes are written; the + document pipeline converts tagged images to sRGB and reports metadata loss. + Reading remains in the separate #2868 PR. The `ASYMMETRIC_EXCEPTIONS` + entry stays until import is merged. + +* **DDS.** The top mip level of a 2D texture: uncompressed pixels described by channel masks + (8, 16, 24 or 32 bits, RGB or luminance, with or without alpha) or BC1/BC2/BC3 (DXT1/DXT3/DXT5), + with the legacy header or the DX10 one. Uncompressed row padding follows the header's + `DDSD_PITCH` stride; without that flag, rows are tightly packed. Everything opens as U8; the + other mip levels are ignored. Cubemaps, texture arrays, volume textures, premultiplied DXT2/DXT4 and other formats + (BC4–BC7, float) are refused with `CodecError::Unsupported`. Writing (`EncodeOptions::dds`) makes + a 2D texture with the legacy header: uncompressed by default (BGRA, BGRX, or luminance with or + without alpha, so it reads back exactly), or BC1/BC2/BC3. The block encoder is our own, from + Microsoft's public block-compression documentation: RGB565 endpoints fitted along each block's + principal axis and refined by least squares, indices picked against the palette the reader + decodes; BC1 turns texels under half opacity into its transparent black, and BC3 picks + whichever alpha mode fits. `mipmaps` adds the full chain down to 1×1, each level a box filter + of the one above (the last column and row of an odd-sized level average three pixels, so none + is dropped). Cubemaps, arrays, BC4–BC7 and sRGB (DX10-only) formats aren't written. + +* **FITS.** The image in the primary HDU, or the first `IMAGE` extension, is read; `NAXIS3 = 3` + opens as RGB and any other plane count opens the first plane with `DecodeWarning::MorePages`. + Rows follow `ROWORDER` (bottom-up by default, as Siril and DS9 do). Physical values that fit + stay exact as U8/U16 (a camera's `BITPIX = 16`, `BZERO = 32768`); the rest become F32, taken as + normalised when at most 2 and scaled by 65535 (or the maximum) otherwise. Header cards are + kept as text (`HIERARCH` for long names). Tile-compressed (fpack) files are refused with + `CodecError::Unsupported`; Bayer mosaics open undebayered. + +* **DPX.** SMPTE 268M frames in either byte order (`SDPX`, `XPDS`): the first image element, + descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned and unencoded, at 8, 10, 12 or 16 bits. + 10-bit data may be packed (0) or filled three to a word (1, method A; 2, method B), 12-bit data + packed (0) or one to a 16-bit word (1, method A). 10- and 12-bit code values open as U16 + `round(c × 65535 / (2^b − 1))`, so a frame written back at its depth keeps them exactly. No + colour transform is applied: `DecodeWarning::DpxEncoding` names the transfer characteristic and + colorimetric specification, so a log frame is reported as log. More elements than the first + give `DecodeWarning::MoreElements`; the image orientation is applied on import. Writing is + big-endian, one unencoded element, at `EncodeOptions::dpx_bits` (8/10/12/16; by default 8 for + U8 and 16 otherwise), packing 1 for 10 and 12 bits; gray with alpha is written as luma. + `FidelityWarning::BitsReduced` reports a 10- or 12-bit save (and every Cineon save) that drops + precision: always for float samples, for U16 ones unless they are already code values at that + depth. + Other descriptors, signed or run-length-encoded data and other bit depths return + `CodecError::Unsupported` naming the field. + +* **Cineon.** Kodak's 10-bit logarithmic film-scan format (the one DPX grew from), + read from the same Kodak header family: the generic 1024-byte header, big-endian, one (Gray) + or three (RGB) channels, three 10-bit samples packed into each 32-bit word, lines on a word + boundary. The 10-bit code values open as U16 `round(c × 65535 / 1023)`; the file's black/white + reference points are a log-conversion choice and are not applied. A depth other than 10 bits or + a channel count other than 1/3 returns `CodecError::Unsupported`; truncation errors rather than + panicking. Writing follows the Cineon 4.5 layout: a 1024-byte generic header (version `V4.5`, + one or three channels designated as universal-metric gray or R, G, B, 10 bits, data 0–1023 over + densities 0–2.048) and a 1024-byte film header left undefined, then the samples three to a + big-endian 32-bit word, left justified (packing 5), lines on a word boundary. Samples are written + as `round(v × 1023 / 65535)`, so a frame opened from Cineon keeps its code values; any other + image takes the encode plan (alpha dropped, CMYK and Lab to RGB) and is quantized to 10 bits. + +* **DICOM (read-only).** Single-frame medical images over the uncompressed transfer syntaxes + (implicit VR little-endian, explicit VR little/big-endian): MONOCHROME1/2 gray (with the + 1 = white inversion) and RGB/YBR_FULL, 8- and 16-bit allocated samples, interleaved or planar, + scaled from `BitsStored`/`HighBit` and mapped up when `PixelRepresentation` is signed. + Undefined-length sequences outside the pixel data are skipped, as clinical files contain them. + Compressed transfer syntaxes, sequences over the pixel data and other photometric + interpretations return `Unsupported`/`Malformed`. There is no DICOM writer + (`ASYMMETRIC_EXCEPTIONS`). + +* **Amiga IFF.** `FORM ... ILBM` stores interleaved 1–8 bitplanes; + `FORM ... PBM ` stores 8-bit chunky palette indices with even-padded rows. Both support + stored and ByteRun1 compression, a `CMAP` palette (grey ramp when absent, black for missing + entries), and transparent-color masking. ILBM also supports a separate mask plane and + 24-bitplane RGB ("deep" ILBM: 8 planes each of red, green and blue, least significant bit + first, no palette). HAM/EHB viewport modes, PBM mask planes, a transparent color in a + 24-bitplane file, other bit depths and compressions return `CodecError::Unsupported`. + Writing produces a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, at most + 65535 × 65535 pixels. An image with alpha gets a mask plane (masking 1): alpha of 128 or more + is opaque, and `AlphaBinarized` is raised when any pixel is not fully opaque. Gray is + expanded to RGB, and 16-bit and float images are reduced to 8 bits. + +* **Camera raw files** (DNG, CR2, NEF, ARW… which are TIFF-structured) are recognised and refused + with `CodecError::Unsupported`: they are sensor data, not flat images. `photocraft-raw` decodes + and develops them, and `photocraft-io` routes them there. + +* **HEIF/HEIC (read-only, feature `heif`).** Decoding lives in the optional `craft-heif` crate + behind the non-default `heif` feature (official PhotoCraft builds enable it; distributors choose). + Without it, HEIF is still detected and decoding returns `CodecError::Unsupported` ("HEIC/HEIF + support isn't included in this build"). iPhone and Mac photos decode with `heic-rs` (pure Rust, no `unsafe`): + single pictures and grid-tiled photos, 8 to 12 bits, auxiliary alpha, ICC, EXIF and XMP. The + container's `irot`/`imir`/`clap` are applied and EXIF Orientation is rewritten to 1 (HEIF's EXIF + tag only repeats the container's, and applying both would turn the photo twice). Grayscale + decodes to RGB. Image sequences, overlays and identity derivations, multilayer HEVC and some + 4:2:2/4:4:4 streams return `CodecError::Unsupported` or `Malformed`, never wrong pixels. Writing + needs an HEVC encoder and every mature one is C, so HEIF is listed in `ASYMMETRIC_EXCEPTIONS`. +* **AVIF.** Encoding uses `ravif`, which is pure Rust. Decoding + needs `dav1d`, which is C. AVIF is therefore read-unsupported, and write support is gated + behind the non-default `avif` feature. In a default build it is neither readable nor writable, + so the symmetric guarantee holds. It is listed in `ASYMMETRIC_EXCEPTIONS`. +* **Lossy WebP.** Lossless (VP8L, via `image-webp`) is the default. `webp_lossless: false` + writes a lossy file with our own clean-room VP8 key-frame encoder (`codecs::vp8`, from + RFC 6386; `webp_quality` 0–100 on libwebp's scale sets the quantizer). Alpha travels losslessly + in an `ALPH` chunk and ICC/EXIF/XMP in their own chunks of the extended (`VP8X`) container. The + encoder makes no rate-distortion decisions yet, and a side longer than 16383 px (the VP8 frame + header's limit) is refused with `CodecError::Encode`. We read both lossy and lossless files. +* **Animation and multi-page files** (APNG, animated GIF/WebP, multi-page TIFF): only the first + frame or page is decoded, and a single frame is written. `FormatCaps::animation` marks + containers that can hold more frames. The decoded image then carries a + `DecodeWarning::MoreFrames` / `MorePages` in `Image::warnings`, with the total when the file + states it (frames and TIFF directories are counted without decoding them; reduced-resolution + TIFF directories and transparency masks are not pages). Any TIFF page can still be opened + with `decode_tiff_page` (and `photocraft_io::import_tiff_page`). Every frame of a GIF comes + from `decode_gif_frames`: each frame composited onto the logical screen with the disposal + methods applied, as RGBA8 with its delay in milliseconds, within `Limits` and + `MAX_GIF_FRAMES`. Every frame of an animated WebP comes from `decode_webp_frames` the same + way, composited onto its canvas, with its duration in milliseconds and the file's ICC profile. + The video-layer commands use both (`photocraft_io::import_gif_frames`, + `photocraft_io::import_webp_frames`). +* **EXIF in TIFF** is stored as a sub-IFD rather than a blob, so it is not preserved yet. + `caps.exif = false` for TIFF, and a warning is raised. +* **Orientation** (EXIF tag 274 in JPEG, PNG `eXIf` and WebP; the decoded page's tag in TIFF and + BigTIFF) is applied on + decode, like Photoshop: the pixels come back upright and the EXIF/XMP orientation is rewritten + to 1 (`DecodeOptions::keep_orientation` opts out). Encoders always write Orientation = 1 + (`upright_exif`, `upright_xmp`, which change only the tag's value, kept in its own type), so upright pixels + are never rotated twice. Parsing is strict: an IFD0 inside the 8-byte header or cut off before + its next-IFD pointer, an Orientation that is not exactly one SHORT or LONG, or an out-of-range value + reads as 1; with duplicate entries the first wins. `Limits` are checked again on the upright + size (5–8 swap width and height), and the rotated buffer is allocated fallibly. + `Image::oriented` turns any layout and depth in parallel bands (about 10 ms for 24 MP RGB8 in + release). +* **Resolution** (#1691, module `resolution`): a file without a resolution of its own format + (no PNG `pHYs`, a WebP, HEIF, …) takes it from its XMP `tiff:XResolution` / `YResolution` / + `ResolutionUnit`, else from EXIF IFD0 (RATIONAL X/YResolution, unit 2 inch — the default when + absent — or 3 cm; unit 1 or anything malformed counts as no resolution). A JPEG uses, like + Photoshop, the first usable of: APP13 Photoshop ResolutionInfo (0x03ED), XMP, EXIF, and only + then the JFIF density (cameras write EXIF only, many tools write a default JFIF 72). On export + the EXIF and XMP resolution are rewritten to the image's (`export_exif`, `export_xmp`): the + RATIONAL values in place, ResolutionUnit set to 2, an entry that can't be rewritten removed + from IFD0 without moving any other data. IFD1 (the thumbnail's) is left as it is. +* **JPEG** + * 8-bit only. Neither decoder backend supports 12-bit. + * A file cut off inside its image data decodes leniently (a baseline JPEG's missing rows come + out grey) but never silently: the image carries `DecodeWarning::Truncated`. A file that ends + before any scan data is an error. + * CMYK is always written 4:4:4 (subsampled CMYK is not portable) as Adobe-inverted CMYK with an + APP14 marker. + * Text is not written because there is no COM-segment support. + * EXIF and XMP must each fit in one APP1 segment (65 533 bytes with its header). Metadata that + doesn't fit is dropped and the export still succeeds, with a + `MetadataTooLarge` warning ("EXIF (… bytes) is too large for the format; it will be dropped"). + Before dropping XMP, the encoder first removes the layered-document properties + (`DocumentAncestors`, `TextLayers`) that mean nothing in a flat JPEG. Extended XMP is not + written. +* **TIFF decode** + * Classic TIFF and BigTIFF (version 43, 8-byte offsets), both byte orders, strips or tiles, + planar configuration 1 or 2. + * Supports 1, 2 and 4-bit gray, 8/16/32/64-bit integers (32 and 64-bit are reduced to U16), + F16, F32 and F64 (converted to F32), WhiteIsZero (#691: the tones are no longer inverted), + palette images (opened as RGB) and associated alpha (made straight). + * Extra samples: the first alpha sample (ExtraSamples 1 or 2, wherever it sits) is the image's + alpha. Every other extra sample, such as a spot colour or a further alpha channel + (ExtraSamples 0), comes back in `Metadata::extra_samples`: one plane per sample in the + image's sample type, with its ExtraSamples value as `kind` (a reserved value above 2 reads as + 0). At most `MAX_EXTRA_SAMPLES` (16) are kept, and they count against `Limits::max_alloc` with + the image: when the image fits but its extra samples don't, it opens without them and carries + `DecodeWarning::ExtraSamplesDropped`. + * Every directory is reachable: `tiff_info` lists the main IFD chain and its SubIFDs (tag 330) + with each page's size, kind (page, reduced-resolution copy, mask) and storage, walked with a + seen-set, a depth limit and a 10 000-directory cap; `decode_tiff_page` opens any of them. + `decode` opens the first full-resolution page, like Photoshop (thumbnails are skipped). + * Banded decoding: the image is allocated once at its final size and each strip or row of + tiles is decoded straight into it, in parallel on native targets, with at most one + strip/tile-sized scratch buffer per worker (30 MP RGB8: Deflate 345 → 32 ms, LZW 428 → 43 + ms in release). Image data cut off by the end of the file decodes with + `DecodeWarning::Truncated`; a file far too small for its declared size (more than any + compression could expand) is refused before allocating. + * YCbCr with three 8- or 16-bit samples decodes to RGB. YCbCr with other sample counts or + bit depths returns `Unsupported`. JPEG-compressed YCbCr uses the full-range JFIF conversion. + * 8/16-bit CIELab and ICCLab decode to the Lab channel layout. + * Signed integer samples and unsupported compression methods return `Unsupported`. +* **TIFF encode** writes classic TIFF, or BigTIFF when `EncodeOptions::tiff_bigtiff` is set or + the file could pass 4 GiB. `Metadata::extra_samples` (up to 16) are written after the colour + and alpha samples, with their `kind` in the ExtraSamples tag (a reserved `kind` above 2 is + written as 0). Each plane must be exactly + `width × height × sample.bytes()` bytes; a plane of any other length, and more than 16 planes, + are encode errors rather than silently truncated or reinterpreted. +* **EXR** + * Multi-part files (a Maya/Arnold render writes one part per AOV) open the part that + looks most like a colour image — RGBA beats RGB beats YA beats a single channel, so a + depth-only part no longer fails the file — with `DecodeWarning::MoreParts` naming how + many parts exist. `exr_info` lists every part (name, view, size, channels, deep/tiled) + without decoding pixels, and `decode_exr_part` decodes a chosen one. Deep parts stay + with the deep decoder: a file that has any is refused here with a pointer to it. + * Reads at full resolution, from the data window. + * Channel names are matched by suffix, so `layer.R` counts as `R`. + * Subsampled channels are unsupported. + * Cryptomatte layers (specification 1.2, as written by Arnold, V-Ray, Redshift, Mantra/Karma + and Cycles) are listed and decoded structurally: `cryptomatte_layers` reads the header + attributes, channels and embedded manifest without pixels, `decode_cryptomatte` returns the + per-pixel (ID, coverage) pairs, and `cryptomatte_id`/`cryptomatte_key`/`cryptomatte_preview_color` + hash names to IDs the way the reference implementation does (official test vectors in the + test suite). + * Deep data (scanlines or single-level tiles, compressed NONE, RLE or ZIPS) opens as a flat + image: each pixel's samples are composited into one, with + `DecodeWarning::DeepFlattened` because per-pixel depth is not kept. Deep ZIP (16-line + blocks) and deep mip-map or rip-map tiles return `Unsupported`. + * Only lossless compressions are offered for writing: None, RLE, ZIP1, ZIP16 and PIZ. +* **Netpbm writing** picks the subtype from the image: float gives PFM, gray gives P5, RGB gives + P6, and anything with alpha or CMYK gives P7 (PAM). Writing a `.pbm` file therefore produces + whichever of these fits the data. +* **Colour conversions** (CMYK↔RGB, RGB→gray) are naive. Colour-managed conversion belongs in + `photocraft-color`. + +## Limits (decompression bombs) + +`DecodeOptions { limits: Limits { max_width, max_height, max_pixels, max_alloc }, .. }` guards +decoding. Header dimensions are checked before the pixel buffer is allocated, and the budget is +also passed to the underlying decoders. A violation returns `CodecError::LimitExceeded`. The +defaults are 262144 px per side, 2^30 pixels and 8 GiB (2 GiB on 32-bit targets such as wasm). +PNG also bounds the aggregate decoded text and XMP (UTF-8 keywords and values) by `max_alloc`, +including chunks after IDAT. This budget is separate from pixels, not a total process-memory cap. + +## Tests + +`cargo test -p craft-image-codecs` covers: + +* the full round-trip matrix (every writable format × 4 sample types × 6 layouts), exact for + lossless formats and above a PSNR threshold for lossy ones; +* metadata preservation per format, checked against `caps`; +* magic-number detection, including negatives; +* fidelity warnings, and a cross-check that the warnings match the actual round-trip result; +* the symmetric-capability guarantee; +* malformed input: truncation, bit flips and proptest random bytes; +* Adam7 PNG against progressive PNG and against the `image` crate as an oracle; +* limit enforcement. + +If `corpus/pngsuite/*.png` exists at the repo root, every file in it is compared against the +`image` crate's decoder. Without it, that test is skipped. diff --git a/crates/image-codecs/fuzz/.gitignore b/crates/image-codecs/fuzz/.gitignore new file mode 100644 index 000000000..ab0eaa1a4 --- /dev/null +++ b/crates/image-codecs/fuzz/.gitignore @@ -0,0 +1,5 @@ +target/ +corpus/ +artifacts/ +coverage/ +Cargo.lock diff --git a/crates/image-codecs/fuzz/Cargo.toml b/crates/image-codecs/fuzz/Cargo.toml new file mode 100644 index 000000000..3257a3953 --- /dev/null +++ b/crates/image-codecs/fuzz/Cargo.toml @@ -0,0 +1,78 @@ +[package] +name = "craft-image-codecs-fuzz" +version = "0.0.0" +publish = false +edition = "2024" +license = "MIT OR Apache-2.0" + +[package.metadata] +cargo-fuzz = true + +# Standalone workspace so the main workspace's `crates/*` glob and lints do +# not apply (cargo-fuzz needs nightly + sanitizer flags). +[workspace] + +[dependencies] +libfuzzer-sys = "0.4" +craft-image-codecs = { path = "..", features = ["heif"] } +# Called directly too: the codec turns its panics into errors (see decode_heif.rs). +heic-rs = "=0.1.4" + +[profile.release] +debug = 1 + +[[bin]] +name = "decode_any" +path = "fuzz_targets/decode_any.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_png" +path = "fuzz_targets/decode_png.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_jpeg" +path = "fuzz_targets/decode_jpeg.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_tiff" +path = "fuzz_targets/decode_tiff.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_exr" +path = "fuzz_targets/decode_exr.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_heif" +path = "fuzz_targets/decode_heif.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_gif" +path = "fuzz_targets/decode_gif.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "decode_webp" +path = "fuzz_targets/decode_webp.rs" +test = false +doc = false +bench = false diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs new file mode 100644 index 000000000..aa261efaf --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_any.rs @@ -0,0 +1,18 @@ +#![no_main] +//! Detect + decode arbitrary bytes, then re-encode what decoded (symmetry +//! path) — neither may panic. + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_with, encode, DecodeOptions, EncodeOptions, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + if let Ok(img) = decode_with(data, &opts) + && let Some(f) = craft_image_codecs::detect(data) + { + let _ = encode(&img, f, &EncodeOptions::default()); + } +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs new file mode 100644 index 000000000..1e6abbf81 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_exr.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::OpenExr, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs new file mode 100644 index 000000000..4e0c556c7 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_gif.rs @@ -0,0 +1,13 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_gif_frames, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Gif, data, &opts); + let _ = decode_gif_frames(data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs new file mode 100644 index 000000000..447c52a0d --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_heif.rs @@ -0,0 +1,15 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +// heic-rs is young and parses untrusted HEIF/HEVC: run with `-timeout=10` so a hang is reported, +// not just a panic. The codec turns a heic-rs panic into an error, so heic-rs is also called +// directly, where its panics still reach the fuzzer. Both orientation paths are covered. +fuzz_target!(|data: &[u8]| { + let keep_orientation = data.first().is_some_and(|b| b & 1 == 1); + let raw = heic_rs::DecodeOptions::default().with_max_pixels(Some(1 << 22)).with_transforms(!keep_orientation).with_alpha(true); + let _ = heic_rs::decode(data, &raw); + let limits = Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }; + let _ = decode_as_with(Format::Heif, data, &DecodeOptions { limits, keep_orientation }); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs new file mode 100644 index 000000000..96616edc8 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_jpeg.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Jpeg, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs new file mode 100644 index 000000000..54b23f399 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_png.rs @@ -0,0 +1,12 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Png, data, &opts); +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs new file mode 100644 index 000000000..277dac778 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_tiff.rs @@ -0,0 +1,20 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_tiff_page, exif_orientation, tiff_info, tiff_orientation, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::Tiff, data, &opts); + let _ = exif_orientation(data); + let _ = tiff_orientation(data); + // Every directory of the chain and its SubIFDs (BigTIFF too), not only the default page. + if let Ok(info) = tiff_info(data) { + for page in 0..info.pages.len().min(16) { + let _ = decode_tiff_page(data, page, &opts); + } + } +}); diff --git a/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs b/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs new file mode 100644 index 000000000..139777a81 --- /dev/null +++ b/crates/image-codecs/fuzz/fuzz_targets/decode_webp.rs @@ -0,0 +1,13 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use craft_image_codecs::{decode_as_with, decode_webp_frames, DecodeOptions, Format, Limits}; + +fuzz_target!(|data: &[u8]| { + let opts = DecodeOptions { + limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 256 << 20 }, + ..Default::default() + }; + let _ = decode_as_with(Format::WebP, data, &opts); + let _ = decode_webp_frames(data, &opts); +}); diff --git a/crates/image-codecs/src/cicp.rs b/crates/image-codecs/src/cicp.rs new file mode 100644 index 000000000..fc148837c --- /dev/null +++ b/crates/image-codecs/src/cicp.rs @@ -0,0 +1,182 @@ +//! ITU-T H.273 colour descriptions (CICP: coding-independent code points), as HEIF `colr` +//! `nclx` boxes and video streams declare them, and the HDR transfer functions of ITU-R BT.2100. +//! +//! PQ and HLG can't be written as an ICC tone curve, so an image using them is decoded here to +//! linear light (32-bit float) instead: diffuse white (BT.2408's 203 cd/m²) becomes 1.0 and the +//! highlights above it are kept as values over 1.0, as Photoshop holds HDR in a 32-bit document. +//! The caller tags the result with a linear profile on the declared primaries. + +use crate::error::CodecError; +use crate::image::{Image, SampleType}; + +/// A colour description: the four H.273 code points as written in the file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Cicp { + /// Colour primaries: 1 BT.709, 9 BT.2020, 12 P3-D65… + pub primaries: u16, + /// Transfer characteristics: 13 sRGB, 1/6/14/15 BT.709, 8 linear, 16 PQ, 18 HLG… + pub transfer: u16, + /// Matrix coefficients. + pub matrix: u16, + /// Full-range samples rather than studio swing. + pub full_range: bool, +} + +impl Cicp { + /// SMPTE ST 2084 / BT.2100 perceptual quantizer. + pub const TRANSFER_PQ: u16 = 16; + /// ARIB STD-B67 / BT.2100 hybrid log-gamma. + pub const TRANSFER_HLG: u16 = 18; + + /// PQ or HLG: samples that [`linearize`] decodes to linear light. + pub fn is_hdr(self) -> bool { + matches!(self.transfer, Cicp::TRANSFER_PQ | Cicp::TRANSFER_HLG) + } +} + +/// The luminance that becomes 1.0: BT.2408's HDR reference (diffuse) white. +pub const REFERENCE_WHITE_NITS: f32 = 203.0; +/// The nominal display peak HLG is rendered for (BT.2100's reference display). +pub const HLG_PEAK_NITS: f32 = 1000.0; +/// PQ's absolute peak. +pub const PQ_PEAK_NITS: f32 = 10_000.0; + +/// BT.2100 HLG inverse OETF: non-linear signal `E'` (0..1) to normalised scene light `E` (0..1). +pub fn hlg_inverse_oetf(e: f32) -> f32 { + const A: f32 = 0.178_832_77; + const B: f32 = 1.0 - 4.0 * A; + const C: f32 = 0.559_910_7; + let e = if e.is_finite() { e.clamp(0.0, 1.0) } else { 0.0 }; + if e <= 0.5 { e * e / 3.0 } else { (((e - C) / A).exp() + B) / 12.0 } +} + +/// BT.2100 HLG signal to display light relative to [`REFERENCE_WHITE_NITS`]: the inverse OETF, +/// then the OOTF for a [`HLG_PEAK_NITS`] display (system gamma 1.2, zero black level), whose +/// luminance uses the weights of the `luma` primaries. +pub fn hlg_to_linear(rgb: [f32; 3], luma: [f32; 3]) -> [f32; 3] { + let s = rgb.map(hlg_inverse_oetf); + let ys = luma[0] * s[0] + luma[1] * s[1] + luma[2] * s[2]; + // γ = 1.2 + 0.42·log10(Lw / 1000) is exactly 1.2 at 1000 cd/m². + let gain = if ys > 0.0 { HLG_PEAK_NITS * ys.powf(0.2) / REFERENCE_WHITE_NITS } else { 0.0 }; + s.map(|v| v * gain) +} + +/// SMPTE ST 2084 (PQ) signal `E'` (0..1) to display light relative to [`REFERENCE_WHITE_NITS`]. +pub fn pq_to_linear(e: f32) -> f32 { + const M1: f32 = 2610.0 / 16384.0; + const M2: f32 = 2523.0 / 4096.0 * 128.0; + const C1: f32 = 3424.0 / 4096.0; + const C2: f32 = 2413.0 / 4096.0 * 32.0; + const C3: f32 = 2392.0 / 4096.0 * 32.0; + let e = if e.is_finite() { e.clamp(0.0, 1.0) } else { 0.0 }; + let p = e.powf(1.0 / M2); + let y = ((p - C1).max(0.0) / (C2 - C3 * p)).powf(1.0 / M1); + y * PQ_PEAK_NITS / REFERENCE_WHITE_NITS +} + +/// Relative luminance weights of the RGB primaries `code` (BT.2020's for anything else, the +/// space BT.2100 defines HLG in). +fn luma_weights(code: u16) -> [f32; 3] { + match code { + 1 => [0.2126, 0.7152, 0.0722], + 12 => [0.2290, 0.6917, 0.0793], + _ => [0.2627, 0.6780, 0.0593], + } +} + +/// `img` (RGB or RGBA, decoded with `cicp`'s PQ or HLG transfer) as linear light in 32-bit float, +/// alpha unchanged. Its ICC profile is dropped: whatever it said described the encoded samples, +/// not linear light. Anything else comes back unchanged. +pub fn linearize(img: Image, cicp: Cicp) -> Result { + let layout = img.layout(); + if !cicp.is_hdr() || !layout.is_rgb() { + return Ok(img); + } + let (w, h) = img.dimensions(); + let ch = layout.channels(); + let mut v = img.to_normalized(); + let luma = luma_weights(cicp.primaries); + for px in v.chunks_exact_mut(ch) { + let [r, g, b, ..] = *px else { continue }; + let out = if cicp.transfer == Cicp::TRANSFER_HLG { hlg_to_linear([r, g, b], luma) } else { [r, g, b].map(pq_to_linear) }; + px[..3].copy_from_slice(&out); + } + let mut out = Image::from_raw(w, h, layout, SampleType::F32, v.iter().flat_map(|x| x.to_ne_bytes()).collect())?; + out.meta = img.meta; + out.warnings = img.warnings; + out.cicp = Some(cicp); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::image::ChannelLayout; + + const BT2020: [f32; 3] = [0.2627, 0.6780, 0.0593]; + + fn close(a: f32, b: f32, tol: f32) -> bool { + (a - b).abs() <= tol + } + + #[test] + fn hlg_inverse_oetf_matches_bt2100() { + assert_eq!(hlg_inverse_oetf(0.0), 0.0); + // The two segments meet at E' = 0.5, E = 1/12. + assert!(close(hlg_inverse_oetf(0.5), 1.0 / 12.0, 1e-6)); + assert!(close(hlg_inverse_oetf(0.500_001), 1.0 / 12.0, 1e-5)); + assert!(close(hlg_inverse_oetf(1.0), 1.0, 1e-4)); + // BT.2408's HLG reference white, 75 % signal, is E ≈ 0.265. + assert!(close(hlg_inverse_oetf(0.75), 0.2650, 1e-3)); + for bad in [f32::NAN, f32::INFINITY, -1.0] { + assert!(hlg_inverse_oetf(bad).is_finite()); + } + } + + #[test] + fn hlg_reference_white_is_one_and_highlights_are_kept() { + // BT.2408: a 75 % HLG grey on a 1000 cd/m² display is 203 cd/m², our 1.0. + let white = hlg_to_linear([0.75; 3], BT2020); + assert!(white.iter().all(|&v| close(v, 1.0, 0.005)), "{white:?}"); + // Peak signal is the display peak: 1000 / 203. + let peak = hlg_to_linear([1.0; 3], BT2020); + assert!(peak.iter().all(|&v| close(v, 1000.0 / 203.0, 0.01)), "{peak:?}"); + // 50 % grey: 1000 · (1/12)^1.2 ≈ 50.7 cd/m². + let mid = hlg_to_linear([0.5; 3], BT2020); + assert!(mid.iter().all(|&v| close(v * 203.0, 50.71, 0.1)), "{mid:?}"); + assert_eq!(hlg_to_linear([0.0; 3], BT2020), [0.0; 3]); + // A saturated colour keeps its hue: only red lit stays only red. + let red = hlg_to_linear([0.75, 0.0, 0.0], BT2020); + assert!(red[0] > 0.0 && red[1] == 0.0 && red[2] == 0.0, "{red:?}"); + } + + #[test] + fn pq_matches_st2084() { + assert_eq!(pq_to_linear(0.0), 0.0); + assert!(close(pq_to_linear(1.0), 10_000.0 / 203.0, 0.05)); + // 203 cd/m² is PQ signal ≈ 0.5807 (BT.2408). + assert!(close(pq_to_linear(0.5807), 1.0, 0.005), "{}", pq_to_linear(0.5807)); + assert!(pq_to_linear(f32::NAN).is_finite()); + } + + #[test] + fn linearize_decodes_hlg_to_float_and_keeps_alpha() { + let hlg = Cicp { primaries: 9, transfer: Cicp::TRANSFER_HLG, matrix: 9, full_range: true }; + let three_quarters = (0.75f32 * 65535.0).round() as u16; + let img = Image::from_u16(2, 1, ChannelLayout::Rgba, &[three_quarters, three_quarters, three_quarters, 65535, 0, 0, 0, 32768]) + .unwrap() + .with_icc(Some(b"encoded-profile".to_vec())); + let out = linearize(img, hlg).unwrap(); + assert_eq!((out.sample_type(), out.layout(), out.icc.as_deref(), out.cicp), (SampleType::F32, ChannelLayout::Rgba, None, Some(hlg))); + let v = out.to_normalized(); + assert!(v[..3].iter().all(|&x| close(x, 1.0, 0.005)), "{v:?}"); + assert!(close(v[3], 1.0, 1e-6) && v[4..7] == [0.0; 3] && close(v[7], 0.5, 1e-3), "{v:?}"); + } + + #[test] + fn linearize_leaves_sdr_untouched() { + let srgb = Cicp { primaries: 1, transfer: 13, matrix: 6, full_range: false }; + let img = Image::from_u16(1, 1, ChannelLayout::Rgb, &[1, 2, 3]).unwrap(); + assert_eq!(linearize(img.clone(), srgb).unwrap(), img); + } +} diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs new file mode 100644 index 000000000..4c4c22388 --- /dev/null +++ b/crates/image-codecs/src/codecs.rs @@ -0,0 +1,25 @@ +pub(crate) mod dds; +pub(crate) mod dds_bc; +pub(crate) mod deep_exr; +pub(crate) mod dicom; +pub(crate) mod dpx; +pub(crate) mod exr; +pub(crate) mod exr_cryptomatte; +pub(crate) mod fits; +pub(crate) mod gif_frames; +pub(crate) mod gif_palette; +pub(crate) mod heif; +pub(crate) mod iff; +pub(crate) mod jpeg; +pub(crate) mod jxl; +pub(crate) mod pcx; +pub(crate) mod png; +pub(crate) mod pnm; +pub(crate) mod tga; +pub(crate) mod tiff; +pub(crate) mod tiff_exif; +pub(crate) mod tiff_ifd; +pub(crate) mod via_image; +pub(crate) mod vp8; +pub(crate) mod webp; +pub(crate) mod xpm; diff --git a/crates/image-codecs/src/codecs/dds.rs b/crates/image-codecs/src/codecs/dds.rs new file mode 100644 index 000000000..1d220e6ac --- /dev/null +++ b/crates/image-codecs/src/codecs/dds.rs @@ -0,0 +1,556 @@ +//! DirectDraw Surface (DDS) textures, after Microsoft's public DDS documentation ("DDS", +//! "DDS_HEADER", "DDS_PIXELFORMAT", "DDS_HEADER_DXT10"). +//! +//! Reading: the top mip level of a 2D texture, either uncompressed (8 to 32 bits per pixel +//! described by channel masks, RGB or luminance) or block-compressed BC1/BC2/BC3 +//! (DXT1/DXT3/DXT5), with or without the DX10 header. Cubemaps, texture arrays and volume +//! textures are refused rather than flattened to one face or slice. +//! +//! Writing: a 2D texture, uncompressed (BGRA or BGRX, luminance with or without alpha) or +//! BC1/BC2/BC3, optionally with its full mip chain. The legacy header describes all of these, +//! so no DX10 header is written; formats only DXGI can name (BC4–BC7, sRGB) aren't written. + +use std::borrow::Cow; + +use super::dds_bc::{self, Block, Mask}; +use crate::SampleType; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::{DdsCompression, EncodeOptions, Limits}; + +/// `DDS ` plus the 124-byte DDS_HEADER. +const HEADER: usize = 128; +/// DDS_HEADER_DXT10, present when the FourCC is `DX10`. +const DX10_HEADER: usize = 20; + +const DDSD_DEPTH: u32 = 0x80_0000; +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; +const DDSCAPS2_CUBEMAP: u32 = 0x200; +const DDSCAPS2_CUBEMAP_FACES: u32 = 0xfc00; +const DDSCAPS2_VOLUME: u32 = 0x20_0000; +const DIMENSION_TEXTURE2D: u32 = 3; +const MISC_TEXTURECUBE: u32 = 0x4; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Dds, reason) +} +fn unsupported(reason: impl Into) -> CodecError { + CodecError::unsupported(Format::Dds, reason) +} +fn le32(b: &[u8], at: usize) -> Result { + let bytes = b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).ok_or_else(|| bad("truncated header"))?; + Ok(u32::from_le_bytes(bytes)) +} + +/// Header-declared extra subresources cannot survive a single-image rewrite, even without presence flags. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let depth = le32(bytes, 24)?; + let mips = le32(bytes, 28)?; + let faces = (le32(bytes, 112)? & DDSCAPS2_CUBEMAP_FACES).count_ones(); + let dx10 = le32(bytes, 80)? & DDPF_FOURCC != 0 && bytes.get(84..88) == Some(b"DX10"); + let (cube, array) = if dx10 { (le32(bytes, 136)? & MISC_TEXTURECUBE != 0, le32(bytes, 140)?) } else { (false, 0) }; + Ok(depth > 1 || mips > 1 || faces > 1 || cube || array > 1) +} + +/// A FourCC as text when it is printable, else as the number legacy writers store there. +fn fourcc_name(fourcc: u32) -> String { + let bytes = fourcc.to_le_bytes(); + if bytes.iter().all(|c| c.is_ascii_graphic() || *c == b' ') { bytes.iter().map(|&c| char::from(c)).collect() } else { fourcc.to_string() } +} + +/// One channel of an uncompressed pixel: where it sits and its largest value. +#[derive(Clone, Copy)] +struct Field { + shift: u32, + max: u32, +} + +impl Field { + /// `None` for an empty mask; an error for one with gaps or wider than the pixel. + fn new(mask: u32, bits: u32) -> Result, CodecError> { + if mask == 0 { + return Ok(None); + } + let shift = mask.trailing_zeros(); + let max = mask >> shift; + if max & max.wrapping_add(1) != 0 || (bits < 32 && mask >> bits != 0) { + return Err(bad(format!("channel mask {mask:#x} is not a contiguous field of a {bits}-bit pixel"))); + } + Ok(Some(Field { shift, max })) + } + + fn get(self, pixel: u32) -> u8 { + let v = u64::from((pixel >> self.shift) & self.max); + let max = u64::from(self.max); + ((v * 255 + max / 2) / max) as u8 + } +} + +enum Encoding { + /// Uncompressed: bytes per pixel, then red (or luminance), green, blue and alpha. + Masks { + bytes: usize, + gray: bool, + fields: [Option; 4], + }, + Bc1, + Bc2, + Bc3, +} + +impl Encoding { + fn masks(bits: u32, masks: [u32; 4], gray: bool) -> Result { + if !matches!(bits, 8 | 16 | 24 | 32) { + return Err(bad(format!("{bits} bits per pixel is not 8, 16, 24 or 32"))); + } + let mut fields = [None; 4]; + for (field, mask) in fields.iter_mut().zip(masks) { + *field = Field::new(mask, bits)?; + } + let colour = if gray { fields[0].is_some() } else { fields[..3].iter().any(Option::is_some) }; + if !colour { + return Err(bad("the pixel format has no colour channel masks")); + } + Ok(Encoding::Masks { bytes: bits as usize / 8, gray, fields }) + } + + fn layout(&self) -> ChannelLayout { + match self { + Encoding::Masks { gray: true, fields, .. } if fields[3].is_some() => ChannelLayout::GrayA, + Encoding::Masks { gray: true, .. } => ChannelLayout::Gray, + Encoding::Masks { fields, .. } if fields[3].is_none() => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } + } + + /// Stored row stride and byte size of the top mip level (block rows for BC). + fn level_layout(&self, width: u32, height: u32, pitch: Option) -> Result<(usize, usize), CodecError> { + let overflow = || bad("pixel data size overflow"); + let (w, h) = (usize::try_from(width).map_err(|_| overflow())?, usize::try_from(height).map_err(|_| overflow())?); + let (row_bytes, rows) = match self { + Encoding::Masks { bytes, .. } => { + let packed = w.checked_mul(*bytes).ok_or_else(overflow)?; + let row_bytes = match pitch { + Some(pitch) => usize::try_from(pitch).map_err(|_| overflow())?, + None => packed, + }; + if row_bytes < packed { + return Err(bad(format!("row pitch {row_bytes} is smaller than the {packed}-byte pixel row"))); + } + (row_bytes, h) + } + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { + let block = if matches!(self, Encoding::Bc1) { 8 } else { 16 }; + (w.div_ceil(4).checked_mul(block).ok_or_else(overflow)?, h.div_ceil(4)) + } + }; + let size = row_bytes.checked_mul(rows).ok_or_else(overflow)?; + Ok((row_bytes, size)) + } +} + +/// The legacy pixel format: a FourCC, or channel masks. +fn legacy_encoding(b: &[u8]) -> Result { + let (flags, fourcc, bits) = (le32(b, 80)?, le32(b, 84)?, le32(b, 88)?); + let alpha = if flags & DDPF_ALPHAPIXELS != 0 { le32(b, 104)? } else { 0 }; + if flags & DDPF_FOURCC != 0 { + return match &fourcc.to_le_bytes() { + b"DXT1" => Ok(Encoding::Bc1), + b"DXT3" => Ok(Encoding::Bc2), + b"DXT5" => Ok(Encoding::Bc3), + b"DXT2" | b"DXT4" => Err(unsupported("premultiplied-alpha DXT2/DXT4 textures are not supported")), + _ => Err(unsupported(format!("compression `{}` is not supported (DXT1, DXT3 and DXT5 are)", fourcc_name(fourcc)))), + }; + } + if flags & DDPF_RGB != 0 { + return Encoding::masks(bits, [le32(b, 92)?, le32(b, 96)?, le32(b, 100)?, alpha], false); + } + if flags & DDPF_LUMINANCE != 0 { + return Encoding::masks(bits, [le32(b, 92)?, 0, 0, alpha], true); + } + Err(unsupported(format!("pixel format flags {flags:#x} are not RGB, luminance or a supported compression"))) +} + +/// The DX10 header's DXGI format, refusing anything but a single 2D texture. +fn dx10_encoding(b: &[u8]) -> Result { + let (dxgi, dimension, misc, array) = (le32(b, HEADER)?, le32(b, HEADER + 4)?, le32(b, HEADER + 8)?, le32(b, HEADER + 12)?); + if misc & MISC_TEXTURECUBE != 0 { + return Err(unsupported("cubemaps are not supported")); + } + if dimension != DIMENSION_TEXTURE2D { + return Err(unsupported(format!("only 2D textures are supported (resource dimension {dimension})"))); + } + if array > 1 { + return Err(unsupported(format!("texture arrays are not supported ({array} textures)"))); + } + const RGBA: [u32; 4] = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; + const BGRA: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; + const BGRX: [u32; 4] = [0xff_0000, 0xff00, 0xff, 0]; + match dxgi { + 27..=29 => Encoding::masks(32, RGBA, false), + 87 | 90 | 91 => Encoding::masks(32, BGRA, false), + 88 | 92 | 93 => Encoding::masks(32, BGRX, false), + 70..=72 => Ok(Encoding::Bc1), + 73..=75 => Ok(Encoding::Bc2), + 76..=78 => Ok(Encoding::Bc3), + _ => Err(unsupported(format!("DXGI format {dxgi} is not supported (BC1, BC2, BC3 and 8-bit RGBA or BGRA are)"))), + } +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !b.starts_with(b"DDS ") { + return Err(bad("missing the `DDS ` signature")); + } + if le32(b, 4)? != 124 { + return Err(bad("header size is not 124")); + } + let (flags, height, width, depth, caps2) = (le32(b, 8)?, le32(b, 12)?, le32(b, 16)?, le32(b, 24)?, le32(b, 112)?); + if caps2 & DDSCAPS2_CUBEMAP != 0 { + return Err(unsupported("cubemaps are not supported")); + } + if caps2 & DDSCAPS2_VOLUME != 0 || (flags & DDSD_DEPTH != 0 && depth > 1) { + return Err(unsupported("volume textures are not supported")); + } + let dx10 = le32(b, 80)? & DDPF_FOURCC != 0 && &le32(b, 84)?.to_le_bytes() == b"DX10"; + let (encoding, start) = if dx10 { (dx10_encoding(b)?, HEADER + DX10_HEADER) } else { (legacy_encoding(b)?, HEADER) }; + let layout = encoding.layout(); + limits.check(width, height, layout, SampleType::U8)?; + // The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set. + let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None }; + let (row_pitch, size) = encoding.level_layout(width, height, pitch)?; + let data = b.get(start..).and_then(|d| d.get(..size)).ok_or_else(|| bad(format!("truncated pixel data: {size} bytes needed")))?; + let (w, h) = (width as usize, height as usize); + let channels = layout.channels(); + let row_size = w.checked_mul(channels).ok_or_else(|| bad("pixel size overflow"))?; + let total = row_size.checked_mul(h).ok_or_else(|| bad("pixel size overflow"))?; + let mut pixels = vec![0u8; total]; + match encoding { + Encoding::Masks { bytes, gray, fields } => { + for (src_row, dst_row) in data.chunks_exact(row_pitch).zip(pixels.chunks_exact_mut(row_size)) { + // Stop at the output width, leaving any stored row padding unread. + for (src, dst) in src_row.chunks_exact(bytes).zip(dst_row.chunks_exact_mut(channels)) { + let pixel = src.iter().rev().fold(0u32, |v, &byte| v << 8 | u32::from(byte)); + let channel = |i: usize| fields[i].map_or(0, |f| f.get(pixel)); + let values = if gray { [channel(0), channel(3), 0, 0] } else { [channel(0), channel(1), channel(2), channel(3)] }; + dst.copy_from_slice(&values[..channels]); + } + } + } + Encoding::Bc1 | Encoding::Bc2 | Encoding::Bc3 => { + let block_size = if matches!(encoding, Encoding::Bc1) { 8 } else { 16 }; + let blocks_x = w.div_ceil(4); + for (i, block) in data.chunks_exact(block_size).enumerate() { + let texels = match encoding { + Encoding::Bc1 => bc1(block, true), + Encoding::Bc2 => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc2_alpha(block)), + _ => with_alpha(bc1(block.get(8..).unwrap_or_default(), false), bc3_alpha(block)), + }; + let (bx, by) = ((i % blocks_x) * 4, (i / blocks_x) * 4); + for (t, texel) in texels.iter().enumerate() { + let (x, y) = (bx + t % 4, by + t / 4); + if x < w && y < h { + let at = (y * w + x) * 4; + if let Some(dst) = pixels.get_mut(at..at + 4) { + dst.copy_from_slice(texel); + } + } + } + } + } + } + Image::from_u8(width, height, layout, pixels) +} + +/// A BC1 colour block. BC2 and BC3 colour blocks always have four colours; only BC1's +/// `c0 <= c1` order selects three colours and transparent black. +fn bc1(block: &[u8], one_bit_alpha: bool) -> [[u8; 4]; 16] { + let byte = |i: usize| block.get(i).copied().unwrap_or(0); + let (c0, c1) = (u16::from_le_bytes([byte(0), byte(1)]), u16::from_le_bytes([byte(2), byte(3)])); + let palette = dds_bc::colour_palette(c0, c1, c0 > c1 || !one_bit_alpha); + let indices = u32::from_le_bytes([byte(4), byte(5), byte(6), byte(7)]); + std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) +} + +/// BC2: four explicit bits of alpha per texel. +fn bc2_alpha(block: &[u8]) -> [u8; 16] { + let bits = u64::from_le_bytes(std::array::from_fn(|i| block.get(i).copied().unwrap_or(0))); + std::array::from_fn(|t| ((bits >> (4 * t)) & 0xf) as u8 * 17) +} + +/// BC3: two alpha endpoints and a 3-bit index per texel into eight values, or into six plus +/// 0 and 255 when `a0 <= a1`. +fn bc3_alpha(block: &[u8]) -> [u8; 16] { + let byte = |i: usize| block.get(i).copied().unwrap_or(0); + let palette = dds_bc::alpha_palette(byte(0), byte(1)); + let bits = u64::from_le_bytes([byte(2), byte(3), byte(4), byte(5), byte(6), byte(7), 0, 0]); + std::array::from_fn(|t| palette[((bits >> (3 * t)) & 7) as usize]) +} + +fn with_alpha(mut texels: [[u8; 4]; 16], alpha: [u8; 16]) -> [[u8; 4]; 16] { + for (texel, a) in texels.iter_mut().zip(alpha) { + texel[3] = a; + } + texels +} + +const DDSD_CAPS: u32 = 0x1; +const DDSD_HEIGHT: u32 = 0x2; +const DDSD_WIDTH: u32 = 0x4; +const DDSD_PITCH: u32 = 0x8; +const DDSD_PIXELFORMAT: u32 = 0x1000; +const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; +const DDSD_LINEARSIZE: u32 = 0x8_0000; +const DDSCAPS_COMPLEX: u32 = 0x8; +const DDSCAPS_TEXTURE: u32 = 0x1000; +const DDSCAPS_MIPMAP: u32 = 0x40_0000; + +fn encode_err(reason: impl std::fmt::Display) -> CodecError { + CodecError::encode(Format::Dds, reason) +} + +/// How a written texture stores its pixels. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Store { + /// 8-bit luminance. + Luminance, + /// 8-bit luminance, then 8-bit alpha. + LuminanceAlpha, + /// Blue, green, red and an unused byte (written as 255, so a reader that takes it for + /// alpha still sees an opaque texture). + Bgrx, + Bgra, + Bc1, + Bc2, + Bc3, +} + +impl Store { + /// The storage for an image of `layout`, as [`crate::fidelity::plan`] chose it. + fn new(compression: DdsCompression, layout: ChannelLayout) -> Result { + Ok(match (compression, layout) { + (DdsCompression::None, ChannelLayout::Gray) => Store::Luminance, + (DdsCompression::None, ChannelLayout::GrayA) => Store::LuminanceAlpha, + (DdsCompression::None, ChannelLayout::Rgb) => Store::Bgrx, + (DdsCompression::None, ChannelLayout::Rgba) => Store::Bgra, + (DdsCompression::Bc1, ChannelLayout::Rgba) => Store::Bc1, + (DdsCompression::Bc2, ChannelLayout::Rgba) => Store::Bc2, + (DdsCompression::Bc3, ChannelLayout::Rgba) => Store::Bc3, + (_, layout) => return Err(encode_err(format!("{layout:?} pixels can't be written as `{}`", compression.name()))), + }) + } + + /// Bytes per pixel, or per 4×4 block for a block compression. + fn unit(self) -> usize { + match self { + Store::Luminance => 1, + Store::LuminanceAlpha => 2, + Store::Bgrx | Store::Bgra => 4, + Store::Bc1 => 8, + Store::Bc2 | Store::Bc3 => 16, + } + } + + fn is_block(self) -> bool { + matches!(self, Store::Bc1 | Store::Bc2 | Store::Bc3) + } + + /// Bytes of one row of pixels (or of 4×4 blocks) of a level `width` wide. + fn row_bytes(self, width: usize) -> Option { + if self.is_block() { width.div_ceil(4).checked_mul(self.unit()) } else { width.checked_mul(self.unit()) } + } + + fn level_bytes(self, width: usize, height: usize) -> Option { + let rows = if self.is_block() { height.div_ceil(4) } else { height }; + self.row_bytes(width)?.checked_mul(rows) + } + + /// DDS_PIXELFORMAT: flags, FourCC, bits per pixel and the red, green, blue and alpha masks. + fn pixel_format(self) -> (u32, [u8; 4], u32, [u32; 4]) { + match self { + Store::Luminance => (DDPF_LUMINANCE, [0; 4], 8, [0xff, 0, 0, 0]), + Store::LuminanceAlpha => (DDPF_LUMINANCE | DDPF_ALPHAPIXELS, [0; 4], 16, [0xff, 0, 0, 0xff00]), + Store::Bgrx => (DDPF_RGB, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0]), + Store::Bgra => (DDPF_RGB | DDPF_ALPHAPIXELS, [0; 4], 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]), + Store::Bc1 => (DDPF_FOURCC, *b"DXT1", 0, [0; 4]), + Store::Bc2 => (DDPF_FOURCC, *b"DXT3", 0, [0; 4]), + Store::Bc3 => (DDPF_FOURCC, *b"DXT5", 0, [0; 4]), + } + } +} + +/// Levels in a full mip chain: the larger side halves down to 1. +fn mip_levels(width: u32, height: u32) -> u32 { + 32 - width.max(height).max(1).leading_zeros() +} + +fn half((w, h): (usize, usize)) -> (usize, usize) { + ((w / 2).max(1), (h / 2).max(1)) +} + +pub(crate) fn encode(image: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = image.converted(plan.layout, SampleType::U8); + let pixels = img.as_u8().ok_or_else(|| encode_err("the image was not converted to 8 bits per channel"))?; + let store = Store::new(opts.dds.compression, img.layout())?; + let (width, height) = img.dimensions(); + let levels = if opts.dds.mipmaps { mip_levels(width, height) } else { 1 }; + let top = (width as usize, height as usize); + // Every level's size, checked before anything is allocated. + let mut sizes = Vec::new(); + let mut size = top; + for _ in 0..levels { + sizes.push(store.level_bytes(size.0, size.1).ok_or_else(|| encode_err("texture size overflow"))?); + size = half(size); + } + let total = sizes.iter().try_fold(HEADER, |total, &s| total.checked_add(s)).ok_or_else(|| encode_err("texture size overflow"))?; + let header = header(width, height, store, levels, sizes.first().copied().unwrap_or(0))?; + let mut out = crate::options::alloc_zeroed::(total)?; + out.get_mut(..HEADER).ok_or_else(|| encode_err("output buffer too small"))?.copy_from_slice(&header); + + let channels = img.layout().channels(); + let mut level = Cow::Borrowed(pixels); + let (mut size, mut at) = (top, HEADER); + for (i, &bytes) in sizes.iter().enumerate() { + let dst = out.get_mut(at..at + bytes).ok_or_else(|| encode_err("output buffer too small"))?; + write_level(store, &level, size, dst)?; + at += bytes; + if i + 1 < sizes.len() { + let next = half(size); + level = Cow::Owned(downsample(&level, size, next, channels)?); + size = next; + } + } + Ok(out) +} + +/// The `DDS ` signature and DDS_HEADER. `top` is the top level's size in bytes. +fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Result<[u8; HEADER], CodecError> { + let mut b = [0u8; HEADER]; + let mut put = |at: usize, v: u32| { + if let Some(d) = b.get_mut(at..at + 4) { + d.copy_from_slice(&v.to_le_bytes()); + } + }; + // Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size"). + let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) }; + let pitch_or_size = + pitch_or_size.and_then(|v| u32::try_from(v).ok()).ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; + let mipmapped = levels > 1; + put(4, 124); + put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 }); + put(12, height); + put(16, width); + put(20, pitch_or_size); + put(28, levels); + let (flags, fourcc, bits, masks) = store.pixel_format(); + put(76, 32); + put(80, flags); + put(84, u32::from_le_bytes(fourcc)); + put(88, bits); + for (i, mask) in masks.into_iter().enumerate() { + put(92 + 4 * i, mask); + } + put(108, DDSCAPS_TEXTURE | if mipmapped { DDSCAPS_COMPLEX | DDSCAPS_MIPMAP } else { 0 }); + b[..4].copy_from_slice(b"DDS "); + Ok(b) +} + +/// Runs `f` on each `len`-byte row of `out` with its index, in parallel on native targets. +fn rows(out: &mut [u8], len: usize, f: impl Fn(usize, &mut [u8]) + Send + Sync) { + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(len.max(1)).enumerate().for_each(|(i, row)| f(i, row)); + } + #[cfg(target_arch = "wasm32")] + for (i, row) in out.chunks_mut(len.max(1)).enumerate() { + f(i, row); + } +} + +/// Writes one level of `width` × `height` pixels (in the image's layout) into `dst`. +fn write_level(store: Store, pixels: &[u8], (width, height): (usize, usize), dst: &mut [u8]) -> Result<(), CodecError> { + let row_bytes = store.row_bytes(width).ok_or_else(|| encode_err("texture size overflow"))?; + match store { + Store::Luminance | Store::LuminanceAlpha => { + // The image's gray (and alpha) samples, in the same order. + let src = pixels.get(..dst.len()).ok_or_else(|| encode_err("the level is smaller than its size"))?; + dst.copy_from_slice(src); + } + Store::Bgrx | Store::Bgra => { + let channels = if store == Store::Bgra { 4 } else { 3 }; + rows(dst, row_bytes, |y, row| { + let src = pixels.get(y * width * channels..).unwrap_or_default(); + for (d, s) in row.as_chunks_mut::<4>().0.iter_mut().zip(src.chunks_exact(channels)) { + let byte = |i: usize| s.get(i).copied().unwrap_or(255); + *d = [byte(2), byte(1), byte(0), byte(3)]; + } + }); + } + Store::Bc1 | Store::Bc2 | Store::Bc3 => { + let (texels, _) = pixels.as_chunks::<4>(); + rows(dst, row_bytes, |by, row| { + for (bx, d) in row.chunks_exact_mut(store.unit()).enumerate() { + let (block, inside) = gather(texels, (width, height), bx, by); + let encoded: &[u8] = match store { + Store::Bc1 => &dds_bc::encode_bc1(&block, inside), + Store::Bc2 => &dds_bc::encode_bc2(&block, inside), + _ => &dds_bc::encode_bc3(&block, inside), + }; + for (d, s) in d.iter_mut().zip(encoded) { + *d = *s; + } + } + }); + } + } + Ok(()) +} + +/// Block (`bx`, `by`)'s texels, and which of them lie inside the image. +fn gather(texels: &[[u8; 4]], (width, height): (usize, usize), bx: usize, by: usize) -> (Block, Mask) { + let mut block = [[0u8; 4]; 16]; + let mut inside: Mask = 0; + for (t, texel) in block.iter_mut().enumerate() { + let (x, y) = (bx * 4 + t % 4, by * 4 + t / 4); + if x < width + && y < height + && let Some(p) = texels.get(y * width + x) + { + *texel = *p; + inside |= 1 << t; + } + } + (block, inside) +} + +/// The next mip level: each pixel the average of the 2×2 pixels it covers, or of 2×3, 3×2 or +/// 3×3 along the last column and row of an odd-sized level, so no pixel is dropped. +fn downsample(src: &[u8], (w, h): (usize, usize), (nw, nh): (usize, usize), channels: usize) -> Result, CodecError> { + let len = nw.checked_mul(nh).and_then(|n| n.checked_mul(channels)).ok_or_else(|| encode_err("mip level size overflow"))?; + let mut dst = crate::options::alloc_zeroed::(len)?; + let span = |i: usize, n: usize, size: usize| 2 * i..=if i + 1 == n { size.saturating_sub(1) } else { 2 * i + 1 }; + rows(&mut dst, nw * channels, |y, row| { + let ys = span(y, nh, h); + for (x, px) in row.chunks_exact_mut(channels.max(1)).enumerate() { + let xs = span(x, nw, w); + for (c, v) in px.iter_mut().enumerate() { + let (mut sum, mut n) = (0u32, 0u32); + for sy in ys.clone() { + for sx in xs.clone() { + if let Some(&s) = src.get((sy * w + sx) * channels + c) { + sum += u32::from(s); + n += 1; + } + } + } + *v = ((sum + n / 2) / n.max(1)) as u8; + } + } + }); + Ok(dst) +} diff --git a/crates/image-codecs/src/codecs/dds_bc.rs b/crates/image-codecs/src/codecs/dds_bc.rs new file mode 100644 index 000000000..bbd55b4bf --- /dev/null +++ b/crates/image-codecs/src/codecs/dds_bc.rs @@ -0,0 +1,368 @@ +//! BC1, BC2 and BC3 (DXT1, DXT3, DXT5) blocks, after Microsoft's public block compression +//! documentation ("Block Compression (Direct3D 10)" and "Texture Block Compression in Direct3D +//! 11"): the palettes the DDS reader decodes with, and the block encoder the DDS writer uses. +//! +//! The encoder fits a block's colours with two RGB565 endpoints along their principal axis, +//! refines them by least squares, and scores every candidate against the exact palette the +//! reader rebuilds, so the indices it picks are the best ones for what a decoder shows. + +/// The 16 texels of a 4×4 block, row by row, as RGBA. +pub(crate) type Block = [[u8; 4]; 16]; + +/// Bit `t` set for each texel `t` of a block that counts: edge blocks of a texture whose size is +/// not a multiple of four have texels outside the image. +pub(crate) type Mask = u16; + +pub(crate) fn rgb565(c: u16) -> [u8; 3] { + let scale = |v: u16, max: u16| ((u32::from(v) * 255 + u32::from(max) / 2) / u32::from(max)) as u8; + [scale(c >> 11, 31), scale((c >> 5) & 63, 63), scale(c & 31, 31)] +} + +/// The colours a colour block's indices select. Four-colour mode (BC1 with `c0 > c1`, and every +/// BC2 and BC3 block) adds two colours at thirds; three-colour mode adds the midpoint and +/// transparent black. +pub(crate) fn colour_palette(c0: u16, c1: u16, four: bool) -> [[u8; 4]; 4] { + let (a, b) = (rgb565(c0), rgb565(c1)); + let mix = |wa: u32, wb: u32| { + let d = wa + wb; + let at = |i: usize| ((wa * u32::from(a[i]) + wb * u32::from(b[i]) + d / 2) / d) as u8; + [at(0), at(1), at(2), 255] + }; + if four { + [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(2, 1), mix(1, 2)] + } else { + [[a[0], a[1], a[2], 255], [b[0], b[1], b[2], 255], mix(1, 1), [0; 4]] + } +} + +/// A BC3 alpha block's eight values: six interpolated at sevenths when `a0 > a1`, else four +/// interpolated at fifths followed by 0 and 255. +pub(crate) fn alpha_palette(a0: u8, a1: u8) -> [u8; 8] { + let (a0, a1) = (u32::from(a0), u32::from(a1)); + let mut palette = [a0, a1, 0, 0, 0, 0, 0, 255]; + if a0 > a1 { + for (k, v) in palette.iter_mut().enumerate().skip(2) { + let k = k as u32; + *v = ((8 - k) * a0 + (k - 1) * a1 + 3) / 7; + } + } else { + for (k, v) in palette.iter_mut().enumerate().take(6).skip(2) { + let k = k as u32; + *v = ((6 - k) * a0 + (k - 1) * a1 + 2) / 5; + } + } + palette.map(|v| v as u8) +} + +/// A BC1 block. Texels under half opacity become BC1's transparent black, which needs the +/// three-colour mode; an opaque block takes whichever mode fits its colours better. +pub(crate) fn encode_bc1(block: &Block, inside: Mask) -> [u8; 8] { + let transparent = select(block, inside, |t| t[3] < 128); + colour_block(block, inside & !transparent, transparent, true) +} + +/// A BC2 block: four bits of alpha per texel, then a four-colour block. +pub(crate) fn encode_bc2(block: &Block, inside: Mask) -> [u8; 16] { + let bits = block.iter().enumerate().fold(0u64, |bits, (t, texel)| bits | ((u64::from(texel[3]) * 15 + 127) / 255) << (4 * t)); + join(bits.to_le_bytes(), colour_block(block, inside, 0, false)) +} + +/// A BC3 block: an interpolated alpha block, then a four-colour block. +pub(crate) fn encode_bc3(block: &Block, inside: Mask) -> [u8; 16] { + join(alpha_block(block, inside), colour_block(block, inside, 0, false)) +} + +fn join(a: [u8; 8], b: [u8; 8]) -> [u8; 16] { + std::array::from_fn(|i| if i < 8 { a.get(i) } else { b.get(i - 8) }.copied().unwrap_or(0)) +} + +/// The texels of `inside` for which `f` holds. +fn select(block: &Block, inside: Mask, f: impl Fn(&[u8; 4]) -> bool) -> Mask { + block.iter().enumerate().filter(|(t, texel)| inside >> t & 1 != 0 && f(texel)).fold(0, |m, (t, _)| m | 1 << t) +} + +fn rgb_of(texel: &[u8; 4]) -> [f32; 3] { + [f32::from(texel[0]), f32::from(texel[1]), f32::from(texel[2])] +} + +/// RGB565 of a colour, rounding each channel to the nearest level. +fn to565(c: [f32; 3]) -> u16 { + let q = |v: f32, max: f32| (v.clamp(0.0, 255.0) * max / 255.0).round() as u16; + q(c[0], 31.0) << 11 | q(c[1], 63.0) << 5 | q(c[2], 31.0) +} + +fn distance(a: &[u8; 4], b: &[u8; 4]) -> u32 { + (0..3).map(|i| u32::from(a[i].abs_diff(b[i])).pow(2)).sum() +} + +/// A colour block (endpoints, then 2-bit indices) for the `opaque` texels' colours, with the +/// `transparent` ones on BC1's transparent index. `bc1` allows the three-colour mode; BC2 and +/// BC3 always decode four colours. +fn colour_block(block: &Block, opaque: Mask, transparent: Mask, bc1: bool) -> [u8; 8] { + let colours: Vec<[f32; 3]> = block.iter().enumerate().filter(|(t, _)| opaque >> t & 1 != 0).map(|(_, texel)| rgb_of(texel)).collect(); + if colours.is_empty() { + // Nothing visible: every texel on the transparent index (or index 0 of black). + let indices = if transparent != 0 { u32::MAX } else { 0 }; + return pack(0, 0, indices); + } + let (lo, hi) = principal_endpoints(&colours); + let mut candidates = vec![(to565(lo), to565(hi))]; + let modes: &[bool] = if transparent != 0 { + &[false] + } else if bc1 { + &[true, false] + } else { + &[true] + }; + let mut best: Option<(u32, [u8; 8])> = None; + // Each refinement starts from the best block so far: its indices give the least-squares + // endpoints, which become one more candidate. + for round in 0..3 { + for &(e0, e1) in &candidates { + for &four in modes { + let (bytes, error) = fit(block, opaque, transparent, e0, e1, four); + if best.is_none_or(|(e, _)| error < e) { + best = Some((error, bytes)); + } + } + } + let Some((error, bytes)) = best else { break }; + if error == 0 || round == 2 { + break; + } + candidates = least_squares(block, opaque, &bytes).into_iter().collect(); + if candidates.is_empty() { + break; + } + } + best.map_or([0; 8], |(_, bytes)| bytes) +} + +/// Orders endpoints `e0`, `e1` for the mode (`c0 > c1` for four colours, `c0 <= c1` for three) +/// and picks each texel's nearest palette colour. Returns the block and its squared error. +fn fit(block: &Block, opaque: Mask, transparent: Mask, e0: u16, e1: u16, four: bool) -> ([u8; 8], u32) { + let (c0, c1) = if four == (e0 >= e1) { (e0, e1) } else { (e1, e0) }; + // Equal endpoints can't be in four-colour order; the palette is then one colour either way. + let palette = colour_palette(c0, c1, four && c0 > c1); + let choices = if four && c0 > c1 { 4 } else { 3 }; + let mut indices = 0u32; + let mut error = 0u32; + for (t, texel) in block.iter().enumerate() { + let index = if transparent >> t & 1 != 0 { + 3 + } else if opaque >> t & 1 != 0 { + let (i, d) = palette.iter().take(choices).enumerate().map(|(i, p)| (i, distance(texel, p))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); + error += d; + i as u32 + } else { + 0 + }; + indices |= index << (2 * t); + } + (pack(c0, c1, indices), error) +} + +fn pack(c0: u16, c1: u16, indices: u32) -> [u8; 8] { + let (c0, c1, i) = (c0.to_le_bytes(), c1.to_le_bytes(), indices.to_le_bytes()); + [c0[0], c0[1], c1[0], c1[1], i[0], i[1], i[2], i[3]] +} + +/// The ends of the colours' spread along their principal axis (the covariance's dominant +/// eigenvector, by power iteration), clamped to the RGB cube. +fn principal_endpoints(colours: &[[f32; 3]]) -> ([f32; 3], [f32; 3]) { + let n = colours.len().max(1) as f32; + let mean = [0, 1, 2].map(|i| colours.iter().map(|c| c[i]).sum::() / n); + let mut cov = [[0f32; 3]; 3]; + for c in colours { + let d = [c[0] - mean[0], c[1] - mean[1], c[2] - mean[2]]; + for (i, row) in cov.iter_mut().enumerate() { + for (j, v) in row.iter_mut().enumerate() { + *v += d[i] * d[j]; + } + } + } + let times = |a: [f32; 3]| [0, 1, 2].map(|i| cov[i][0] * a[0] + cov[i][1] * a[1] + cov[i][2] * a[2]); + let iterate = |start: usize| { + let mut axis = [0f32; 3]; + axis[start] = 1.0; + for _ in 0..8 { + let next = times(axis); + let len = next.iter().map(|v| v * v).sum::().sqrt(); + if !len.is_normal() { + break; + } + axis = next.map(|v| v / len); + } + axis + }; + // From each channel's axis, keeping the one the colours spread along most: a channel that + // varies on its own is already an eigenvector, so a single start there would never leave it + // even when the others vary more together. + let spread = |a: [f32; 3]| times(a).iter().zip(a).map(|(x, y)| x * y).sum::(); + let axis = [0, 1, 2].map(iterate).into_iter().max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or([1.0, 0.0, 0.0]); + let along = |c: &[f32; 3]| (c[0] - mean[0]) * axis[0] + (c[1] - mean[1]) * axis[1] + (c[2] - mean[2]) * axis[2]; + let (lo, hi) = colours.iter().map(along).fold((f32::MAX, f32::MIN), |(lo, hi), t| (lo.min(t), hi.max(t))); + let at = |t: f32| [0, 1, 2].map(|i| (mean[i] + t * axis[i]).clamp(0.0, 255.0)); + if lo > hi { (mean, mean) } else { (at(lo), at(hi)) } +} + +/// The endpoints that minimise the squared error for the indices `bytes` already chose: each +/// texel's index fixes its weights between the endpoints, giving 2×2 normal equations per +/// channel. `None` when the indices don't constrain both endpoints. +fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u16)> { + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); + let four = c0 > c1; + // How much of c0 each index holds. + let weight = |i: u32| match (four, i) { + (_, 0) => Some(1.0f32), + (_, 1) => Some(0.0), + (true, 2) => Some(2.0 / 3.0), + (true, _) => Some(1.0 / 3.0), + (false, 2) => Some(0.5), + (false, _) => None, + }; + let (mut aa, mut ab, mut bb) = (0f32, 0f32, 0f32); + let (mut ax, mut bx) = ([0f32; 3], [0f32; 3]); + for (t, texel) in block.iter().enumerate() { + if opaque >> t & 1 == 0 { + continue; + } + let Some(a) = weight(indices >> (2 * t) & 3) else { continue }; + let b = 1.0 - a; + let c = rgb_of(texel); + aa += a * a; + ab += a * b; + bb += b * b; + for ((x0, x1), v) in ax.iter_mut().zip(bx.iter_mut()).zip(c) { + *x0 += a * v; + *x1 += b * v; + } + } + let det = aa * bb - ab * ab; + if det.abs() < 1e-3 { + return None; + } + let e0 = [0, 1, 2].map(|i| (ax[i] * bb - bx[i] * ab) / det); + let e1 = [0, 1, 2].map(|i| (bx[i] * aa - ax[i] * ab) / det); + Some((to565(e0), to565(e1))) +} + +/// A BC3 alpha block: whichever of the two modes (eight values between the extremes, or six +/// between the extremes other than 0 and 255, plus exact 0 and 255) fits better. +fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] { + let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]); + let range = |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); + let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0)); + // Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too. + let eight = alpha_fit(block, inside, hi, lo); + let (mid_lo, mid_hi) = range(&mut alphas().filter(|a| !matches!(a, 0 | 255))).unwrap_or((0, 0)); + let six = alpha_fit(block, inside, mid_lo, mid_hi); + if six.1 < eight.1 { six.0 } else { eight.0 } +} + +fn alpha_fit(block: &Block, inside: Mask, a0: u8, a1: u8) -> ([u8; 8], u32) { + let palette = alpha_palette(a0, a1); + let mut bits = 0u64; + let mut error = 0u32; + for (t, texel) in block.iter().enumerate() { + if inside >> t & 1 == 0 { + continue; + } + let (i, d) = palette.iter().enumerate().map(|(i, &p)| (i, u32::from(p.abs_diff(texel[3])).pow(2))).min_by_key(|&(_, d)| d).unwrap_or((0, 0)); + error += d; + bits |= (i as u64) << (3 * t); + } + let b = bits.to_le_bytes(); + ([a0, a1, b[0], b[1], b[2], b[3], b[4], b[5]], error) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn decode_colour(bytes: &[u8; 8], bc1: bool) -> Block { + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + let palette = colour_palette(c0, c1, c0 > c1 || !bc1); + let indices = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]); + std::array::from_fn(|t| palette[(indices >> (2 * t)) as usize & 3]) + } + + #[test] + fn solid_blocks_land_on_the_nearest_565_colour() { + for colour in [[0, 0, 0, 255], [255, 255, 255, 255], [12, 200, 77, 255]] { + let block = [colour; 16]; + let out = decode_colour(&encode_bc1(&block, u16::MAX), true); + for texel in out { + assert!(texel.iter().zip(colour).all(|(a, b)| a.abs_diff(b) <= 4), "{texel:?} for {colour:?}"); + } + } + } + + #[test] + fn two_colour_blocks_are_exact_when_the_colours_are_565() { + let (a, b) = ([255, 0, 0, 255], [0, 0, 255, 255]); + let block: Block = std::array::from_fn(|t| if t % 3 == 0 { a } else { b }); + for bc1 in [true, false] { + let bytes = if bc1 { encode_bc1(&block, u16::MAX) } else { colour_block(&block, u16::MAX, 0, false) }; + assert_eq!(decode_colour(&bytes, bc1), block); + } + } + + #[test] + fn bc1_punches_through_texels_under_half_alpha() { + let block: Block = std::array::from_fn(|t| if t < 8 { [200, 100, 50, 255] } else { [9, 9, 9, 127] }); + let bytes = encode_bc1(&block, u16::MAX); + let (c0, c1) = (u16::from_le_bytes([bytes[0], bytes[1]]), u16::from_le_bytes([bytes[2], bytes[3]])); + assert!(c0 <= c1, "three-colour mode"); + let out = decode_colour(&bytes, true); + assert!(out[..8].iter().all(|t| t[3] == 255) && out[8..].iter().all(|t| *t == [0; 4]), "{out:?}"); + } + + #[test] + fn four_colour_blocks_keep_c0_above_c1() { + // A ramp needs all four colours; BC2 and BC3 blocks must not rely on the order, but + // older decoders read c0 <= c1 as three colours, so the encoder never writes that. + let block: Block = std::array::from_fn(|t| [(t * 16) as u8, 0, 0, 255]); + let bytes = colour_block(&block, u16::MAX, 0, false); + assert!(u16::from_le_bytes([bytes[0], bytes[1]]) > u16::from_le_bytes([bytes[2], bytes[3]])); + } + + #[test] + fn alpha_blocks_pick_the_mode_that_keeps_0_and_255() { + // 0, 255 and a cluster in between: the six-value mode stores 0 and 255 exactly. + let block: Block = std::array::from_fn(|t| [0, 0, 0, [0, 255, 100, 110][t % 4]]); + let bytes = alpha_block(&block, u16::MAX); + assert!(bytes[0] <= bytes[1], "six-value mode"); + let palette = alpha_palette(bytes[0], bytes[1]); + let bits = u64::from_le_bytes([bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], 0, 0]); + for (t, texel) in block.iter().enumerate() { + let got = palette[(bits >> (3 * t)) as usize & 7]; + assert!(got.abs_diff(texel[3]) <= 2, "texel {t}: {got} for {}", texel[3]); + } + } + + #[test] + fn the_principal_axis_is_found_when_one_channel_varies_on_its_own() { + // Red alternates on its own (variance 64); green and blue alternate together (variance 49 + // each, 98 along their shared axis), uncorrelated with red. Starting on red alone would + // keep red's axis and flatten green and blue; the block must do better than its bounding box. + let block: Block = std::array::from_fn(|t| { + let gb = if (t / 2) % 2 == 0 { 121 } else { 135 }; + [if t % 2 == 0 { 120 } else { 136 }, gb, gb, 255] + }); + let error = |bytes: &[u8; 8]| decode_colour(bytes, false).iter().zip(&block).map(|(a, b)| distance(a, b)).sum::(); + let (_, bounding_box) = fit(&block, u16::MAX, 0, to565([120.0, 121.0, 121.0]), to565([136.0, 135.0, 135.0]), true); + let ours = error(&colour_block(&block, u16::MAX, 0, false)); + assert!(ours < bounding_box, "{ours} vs the bounding box's {bounding_box}"); + } + + #[test] + fn texels_outside_the_image_are_ignored() { + // Only the top-left texel is inside: its colour alone decides the endpoints. + let mut block: Block = [[0, 255, 0, 255]; 16]; + block[0] = [255, 0, 255, 255]; + let out = decode_colour(&encode_bc1(&block, 1), true); + assert_eq!(out[0], [255, 0, 255, 255]); + } +} diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs new file mode 100644 index 000000000..0652112bf --- /dev/null +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -0,0 +1,786 @@ +//! Deep OpenEXR (`deepscanline` / `deeptile`): a variable-length list of samples per pixel, +//! each with its own colour, alpha and depth. The `exr` crate rejects deep headers, so the +//! chunk stream is walked directly from the bytes, per the OpenEXR file-layout specification: +//! a deep chunk carries its coordinates, the packed size of the sample-count table, the +//! packed and unpacked sizes of the sample data, then the compressed count table (cumulative +//! within each row of the block) and the compressed sample data (row by row, each row channel +//! by channel). The layout was checked against files written by the OpenEXR library. Deep data permits only NONE, RLE, ZIPS and ZIP +//! compression; we support the first three (one scan line per block, which keeps the count +//! table unambiguous) and decline ZIP with `unsupported`. +//! +//! [`decode_deep`] returns the structured samples ([`DeepImage`]); [`flatten`] composites +//! them into a flat `Image` — samples sorted by Z, front-to-back `over` with premultiplied +//! colour, un-premultiplied into straight alpha (HDR values are kept, not clamped) — because the flat document model has no +//! per-pixel depth. + +use std::io::{Cursor, Read, Seek, SeekFrom}; + +use exr::meta::attribute::{LevelMode, SampleType as ExrSample}; +use exr::meta::{BlockDescription, MetaData}; +use exr::prelude::Compression; +use half::f16; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, DecodeWarning, DeepChannel, DeepImage, Image, SampleType}; +use crate::options::{Limits, alloc_zeroed}; + +const F: Format = Format::OpenExr; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +fn unsup(reason: impl Into) -> CodecError { + CodecError::unsupported(F, reason) +} + +// --- checked little-endian reads ------------------------------------------------------------- +// Every length and offset comes from the file, so every read is bounds-checked and a short +// read is a malformed file, never a panic. + +fn read_exact<'a>(cur: &mut Cursor<&'a [u8]>, n: usize, what: &str) -> Result<&'a [u8], CodecError> { + // Borrow packed payloads from the input. Copying them here allocates before count-table + // admission and needlessly duplicates the table even when the second pass just skips it. + let truncated = || err(format!("truncated file while reading {what}")); + let start = usize::try_from(cur.position()).map_err(|_| truncated())?; + let end = start.checked_add(n).ok_or_else(truncated)?; + let bytes = *cur.get_ref(); + let v = bytes.get(start..end).ok_or_else(truncated)?; + cur.set_position(end as u64); + Ok(v) +} + +fn i32(cur: &mut Cursor<&[u8]>) -> Result { + let b = read_exact(cur, 4, "an i32")?; + Ok(i32::from_le_bytes([b.first().copied().unwrap_or(0), b.get(1).copied().unwrap_or(0), b.get(2).copied().unwrap_or(0), b.get(3).copied().unwrap_or(0)])) +} + +fn u64(cur: &mut Cursor<&[u8]>) -> Result { + let b = read_exact(cur, 8, "a u64")?; + let a: [u8; 8] = b.try_into().unwrap_or([0; 8]); + Ok(u64::from_le_bytes(a)) +} + +fn seek_to(cur: &mut Cursor<&[u8]>, pos: u64) -> Result<(), CodecError> { + if pos > cur.get_ref().len() as u64 { + return Err(err(format!("chunk offset {pos} points past the end of the file"))); + } + cur.seek(SeekFrom::Start(pos)).map_err(err)?; + Ok(()) +} + +// --- decompression ---------------------------------------------------------------------------- + +/// Undo the ZIP predictor: each byte was stored as its difference to the previous byte plus +/// 128 (so a zero difference is 0x80), modulo 256. Applies to the sample-count table and the +/// sample data alike. +fn differences_to_samples(buf: &mut [u8]) { + if let Some(&first) = buf.first() { + let mut prev = i16::from(first); + for b in buf.iter_mut().skip(1) { + let s = (prev + i16::from(*b) - 128) as u8; + prev = i16::from(s); + *b = s; + } + } +} + +/// Undo the ZIP half-split: the stream is `[first half][second half]` with the second half +/// holding every second byte of the original; weave them back together. The scratch copy is +/// allocated fallibly, so a buffer that passed `max_alloc` but not the allocator is an error. +fn interleave_byte_blocks(buf: &mut [u8]) -> Result<(), CodecError> { + let half = buf.len().div_ceil(2); + let mut out = alloc_zeroed::(buf.len())?; + let mut k = 0usize; + for i in 0..buf.len() / 2 { + let (a, b) = (buf.get(i).copied(), buf.get(half + i).copied()); + if let (Some(a), Some(b), Some(pair)) = (a, b, out.get_mut(k..k + 2)) { + if let Some(byte) = pair.first_mut() { + *byte = a; + } + if let Some(byte) = pair.get_mut(1) { + *byte = b; + } + } + k += 2; + } + if buf.len() % 2 == 1 && half > 0 { + let last = buf.get(half - 1).copied(); + if let (Some(last), Some(slot)) = (last, out.get_mut(k)) { + *slot = last; + } + } + buf.copy_from_slice(&out); + Ok(()) +} + +/// Make room in `out` for `extra` more bytes, `limit` in all. Grows by doubling like `Vec`, +/// but never reserves past `limit`: callers admit exactly that size against `max_alloc`, and +/// plain doubling reserved up to twice as much. Fallible, so a failed allocation is an error. +fn reserve_up_to(out: &mut Vec, extra: usize, limit: usize, what: &str) -> Result<(), CodecError> { + let need = out.len().saturating_add(extra); + if need > limit { + return Err(err(format!("{what}: data expands past {limit} bytes"))); + } + if need > out.capacity() { + let target = out.capacity().saturating_mul(2).max(need).min(limit); + out.try_reserve_exact(target.saturating_sub(out.len())).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; + } + Ok(()) +} + +/// OpenEXR RLE tokens: a negative byte `n` means `-n` literal bytes follow, a non-negative +/// byte `n` means the next byte repeats `n + 1` times (as `ImfRle.cpp` and the `exr` crate). +fn rle_decode(src: &[u8], expected: usize, what: &str) -> Result, CodecError> { + let mut out = Vec::new(); + reserve_up_to(&mut out, expected.min(1 << 20), expected, what)?; + let mut i = 0; + while i < src.len() { + let count = src[i] as i8; + i += 1; + let n = if count < 0 { (-(count as i32)) as usize } else { count as usize + 1 }; + if out.len().saturating_add(n) > expected { + return Err(err(format!("{what}: RLE data expands past {expected} bytes"))); + } + reserve_up_to(&mut out, n, expected, what)?; + if count < 0 { + let end = i.saturating_add(n); + let run = src.get(i..end.min(src.len())).ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?; + if run.len() != n { + return Err(err(format!("{what}: RLE run points past the compressed data"))); + } + out.extend_from_slice(run); + i = end; + } else { + let b = src.get(i).copied().ok_or_else(|| err(format!("{what}: RLE run points past the compressed data")))?; + i += 1; + out.resize(out.len() + n, b); + } + } + if out.len() != expected { + return Err(err(format!("{what}: RLE data expands to {} bytes, expected {expected}", out.len()))); + } + Ok(out) +} + +/// Decompress `what` to exactly `expected` bytes. Like flat blocks, a payload that already +/// has the uncompressed length is stored raw whatever the declared compression. +fn decompress(compression: Compression, packed: &[u8], expected: usize, what: &str) -> Result, CodecError> { + if packed.len() == expected { + let mut out = Vec::new(); + reserve_up_to(&mut out, expected, expected, what)?; + out.extend_from_slice(packed); + return Ok(out); + } + match compression { + Compression::Uncompressed => Err(err(format!("{what}: expected {expected} uncompressed bytes, found {}", packed.len()))), + Compression::RLE => { + // Like ZIPS, RLE runs over the predicted, byte-split stream. + let mut out = rle_decode(packed, expected, what)?; + differences_to_samples(&mut out); + interleave_byte_blocks(&mut out)?; + Ok(out) + } + Compression::ZIP1 => { + // Inflate straight into `out`, grown with `reserve_up_to` rather than by + // `read_to_end`, whose doubling reserves past `expected`. Once `expected` bytes are + // in, a one-byte read checks that the stream ends there. + let malformed = || err(format!("{what}: zlib data is malformed")); + let mut zlib = flate2::bufread::ZlibDecoder::new(packed); + let mut out = Vec::new(); + let mut filled = 0; + while filled < expected { + if filled == out.len() { + let grow = out.len().max(1 << 13).min(expected - filled); + reserve_up_to(&mut out, grow, expected, what)?; + out.resize(filled + grow, 0); + } + let n = zlib.read(out.get_mut(filled..).unwrap_or_default()).map_err(|_| malformed())?; + if n == 0 { + break; + } + filled += n; + } + out.truncate(filled); + if filled == expected && zlib.read(&mut [0u8; 1]).map_err(|_| malformed())? != 0 { + return Err(err(format!("{what}: zlib data expands past {expected} bytes"))); + } + if out.len() != expected { + return Err(err(format!("{what}: zlib data expands to {} bytes, expected {expected}", out.len()))); + } + differences_to_samples(&mut out); + interleave_byte_blocks(&mut out)?; + Ok(out) + } + other => Err(unsup(format!("deep data with {other:?} compression (NONE, RLE and ZIPS are supported)"))), + } +} + +// --- the deep chunk stream --------------------------------------------------------------------- + +/// One parsed chunk: where its pixels go and its cumulative sample counts, plus where to +/// find the compressed sample data in the second pass. +struct DeepChunk { + /// Global pixel indices of the block's pixels, in block order (row-major within the block). + pixels: Vec, + /// Pixels per row of the block (the data window width for a scan line, the clipped tile + /// width for a tile). + row_w: usize, + /// Cumulative sample count table of the block: `len == pixels.len() + 1`. + prefix: Vec, + /// File offset of the chunk start. + pos: u64, + /// Size of the compressed sample data in the file. + packed_data: u64, + /// The decompressed sample-data size the file declares (validated against the table). + unpacked_data: u64, +} + +fn sample_bytes(t: ExrSample) -> usize { + match t { + ExrSample::U32 | ExrSample::F32 => 4, + ExrSample::F16 => 2, + } +} + +fn our_sample_type(t: ExrSample) -> SampleType { + match t { + ExrSample::F16 => SampleType::F16, + ExrSample::U32 | ExrSample::F32 => SampleType::F32, + } +} + +/// Read the deep samples of the first deep part. `meta` must come from an unvalidated +/// [`MetaData::read_from_buffered`] of the same bytes (deep headers fail validation). +pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Result { + let header_index = meta.headers.iter().position(|h| h.deep).ok_or_else(|| unsup("the file has no deep part"))?; + let header = meta.headers.get(header_index).ok_or_else(|| unsup("the file has no deep part"))?; + let compression = header.compression; + if !matches!(compression, Compression::Uncompressed | Compression::RLE | Compression::ZIP1) { + return Err(unsup(format!("deep data with {compression:?} compression (NONE, RLE and ZIPS are supported)"))); + } + + let dw = header.data_window(); + let (w, h) = (dw.size.0, dw.size.1); + if w == 0 || h == 0 { + return Err(err("the data window is empty")); + } + if w > u32::MAX as usize || h > u32::MAX as usize { + return Err(err("the data window is too large")); + } + let y0 = dw.position.1; + let npixels = w.checked_mul(h).ok_or_else(|| err("the data window is too large"))?; + limits.check_bytes(w as u32, h as u32, (header.channels.list.len().max(1) as u64) * 4)?; + + // The display window is the full stage the renderer composed onto (a render pass's data + // window is often its crop). Open on the stage when it plausibly holds the data region: + // sane size for the limits, and the data region inside it. Otherwise fall back to the + // data window alone, as before. + let display = header.shared_attributes.display_window; + let display_size = (display.size.0.max(1) as u32, display.size.1.max(1) as u32); + let (stage, origin) = match display_size { + (sw, sh) + if sw <= 1 << 15 + && sh <= 1 << 15 + && limits.check_bytes(sw, sh, 4).is_ok() + && dw.position.0 >= display.position.0 + && dw.position.1 >= display.position.1 + && dw.position.0.saturating_add(w as i32) <= display.position.0.saturating_add(sw as i32) + && dw.position.1.saturating_add(h as i32) <= display.position.1.saturating_add(sh as i32) => + { + (Some((sw, sh)), (dw.position.0 - display.position.0, dw.position.1 - display.position.1)) + } + _ => (None, (0, 0)), + }; + + let exr_types: Vec = header.channels.list.iter().map(|c| c.sample_type).collect(); + let bytes_per_sample: usize = exr_types.iter().map(|t| sample_bytes(*t)).sum(); + let multipart = meta.requirements.has_multiple_layers; + + // The offset tables follow the headers: `chunk_count` u64 per header, in header order. + // Our part's table starts after the tables of all earlier parts. + let mut table_pos = header_end(bytes)?; + for h_ in meta.headers.iter().take(header_index) { + table_pos = table_pos.saturating_add((h_.chunk_count.min(1 << 24) as u64).saturating_mul(8)); + } + let mut cur = Cursor::new(bytes); + seek_to(&mut cur, table_pos)?; + let mut offsets = Vec::with_capacity(header.chunk_count.min(1 << 20)); + for _ in 0..header.chunk_count { + offsets.push(u64(&mut cur)?); + } + let tables_end = cur.position(); + + // Block geometry. + let (tiles_x, tile_w, tile_h, slots) = match &header.blocks { + BlockDescription::ScanLines => (0usize, w, 1usize, h), // one line per block for NONE/RLE/ZIPS + BlockDescription::Tiles(t) => { + if t.level_mode != LevelMode::Singular { + return Err(unsup("deep mip-map or rip-map tiles (single-level tiles are supported)")); + } + let (tw, th) = (t.tile_size.0.max(1), t.tile_size.1.max(1)); + let tx = w.saturating_add(tw - 1) / tw; + let ty = h.saturating_add(th - 1) / th; + (tx, tw, th, tx.checked_mul(ty).ok_or_else(|| err("tile count overflows"))?) + } + }; + + let mut counts = vec![0u32; npixels]; + let mut seen = vec![false; slots]; + let mut chunks: Vec = Vec::with_capacity(offsets.len()); + + for &off in &offsets { + if off < tables_end || off >= bytes.len() as u64 { + return Err(err(format!("chunk offset {off} is outside the chunk area of the file"))); + } + seek_to(&mut cur, off)?; + if multipart { + let part = i32(&mut cur)?; + if part < 0 || part as usize != header_index { + return Err(err(format!("chunk declares part {part}, expected {header_index}"))); + } + } + // Block coordinates → the block's pixel bounds and its slot. The per-pixel index is + // built only after the chunk's declared payload sizes are validated below, so a + // truncated or lying chunk cannot drive a large index allocation first. + let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = match &header.blocks { + BlockDescription::ScanLines => { + let y = i32(&mut cur)?; + let row = match y.checked_sub(y0) { + Some(d) if d >= 0 && (d as usize) < h => d as usize, + _ => return Err(err(format!("scan line block y={y} is outside the data window"))), + }; + (row..row + 1, 0..w, row, w, true) + } + BlockDescription::Tiles(_) => { + let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + if (lx, ly) != (0, 0) { + return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + } + if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { + return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + } + let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); + let rows = (by + tile_h).min(h); + let cols = (bx + tile_w).min(w); + let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; + (by..rows, bx..cols, slot, cols - bx, false) + } + }; + let block_len = block_rows.len().saturating_mul(block_cols.len()); + if seen.get(slot).copied().unwrap_or(true) { + return Err(err(format!("two chunks cover block {slot}"))); + } + if let Some(s) = seen.get_mut(slot) { + *s = true; + } + + let packed_table = u64(&mut cur)?; + let packed_data = u64(&mut cur)?; + let unpacked_data = u64(&mut cur)?; + let remaining = (bytes.len() as u64).saturating_sub(cur.position()); + if packed_table > remaining || packed_data > remaining.saturating_sub(packed_table) { + return Err(err("deep chunk sizes point past the end of the file")); + } + // The chunk's payload is present; now materialize the block's global pixel index. For + // scan lines this is one row; for tiles the clipped tile rectangle. + let block_pixels: Vec = if scanline_row { + let row = block_rows.start; + (0..w).map(|x| row * w + x).collect() + } else { + let mut px = Vec::with_capacity(block_len); + for yy in block_rows.clone() { + for xx in block_cols.clone() { + px.push(yy.saturating_mul(w).saturating_add(xx)); + } + } + px + }; + let what = "the sample-count table"; + let table_bytes = read_exact(&mut cur, packed_table as usize, what)?; + let expected_table = block_pixels.len().checked_mul(4).ok_or_else(|| err("count table overflows"))?; + // A compressed clipped table also needs its scratch copy. The data-window check only + // budgets four bytes per channel per pixel, insufficient for a one-channel image. + // An equal-sized payload takes decompress's raw path, which allocates no scratch. + let buffers = if table_bytes.len() == expected_table { 1 } else { 2 }; + let clipped_need = (expected_table as u64).saturating_mul(buffers); + if clipped_need > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!( + "{what} needs {clipped_need} bytes including decompression scratch, exceeding max_alloc {}", + limits.max_alloc + ))); + } + // An edge tile's table may still be stored at the full tile size (its pixels first, in + // rows of the clipped width, then unused entries), as the OpenEXR library writes it. + // That size comes from the header's tile size alone, which nothing ties to the data + // window, so it is tried only when `max_alloc` has room for the table and for the copy + // that undoes its byte split. Otherwise only the clipped size is read. + let full_table = tile_w.checked_mul(tile_h).and_then(|n| n.checked_mul(4)); + let raw = match full_table { + // Scan lines and interior tiles: the full size is the clipped size. + Some(full) if full <= expected_table => decompress(compression, table_bytes, expected_table, what)?, + Some(full) if (full as u64).saturating_mul(2) <= limits.max_alloc => { + decompress(compression, table_bytes, full, what).or_else(|_| decompress(compression, table_bytes, expected_table, what))? + } + _ => decompress(compression, table_bytes, expected_table, what).map_err(|_| { + // Four bytes per pixel of the tile, twice: the table and its scratch copy. + let need = (tile_w as u64).saturating_mul(tile_h as u64).saturating_mul(8); + CodecError::LimitExceeded(format!( + "{what} of a {tile_w}x{tile_h} deep tile does not decode at the clipped size, and at the full tile size it needs {need} bytes with its scratch copy, exceeding max_alloc {}", + limits.max_alloc + )) + })?, + }; + + // The table is cumulative within each row of the block (a tile restarts at every row); + // `prefix` is cumulative over the whole block, and each pixel's own count is the difference. + let mut prefix = Vec::with_capacity(block_pixels.len() + 1); + prefix.push(0u32); + let (mut row_base, mut prev) = (0u32, 0u32); + for (i, p) in block_pixels.iter().enumerate() { + let b: [u8; 4] = raw.get(i * 4..i * 4 + 4).and_then(|s| s.try_into().ok()).ok_or_else(|| err("the count table is short"))?; + let v = u32::from_le_bytes(b); + if row_w > 0 && i % row_w == 0 { + row_base = row_base.checked_add(prev).ok_or_else(|| err("the block's sample count overflows"))?; + prev = 0; + } + if v < prev { + return Err(err("the sample-count table decreases (malformed cumulative counts)")); + } + if let Some(c) = counts.get_mut(*p) { + *c = v - prev; + } + prev = v; + prefix.push(row_base.checked_add(v).ok_or_else(|| err("the block's sample count overflows"))?); + } + let total = prefix.last().copied().unwrap_or(0) as u64; + let expected_data = total.checked_mul(bytes_per_sample as u64).ok_or_else(|| err("the sample data size overflows"))?; + if expected_data != unpacked_data { + return Err(err(format!("the chunk declares {unpacked_data} bytes of sample data, the count table implies {expected_data}"))); + } + chunks.push(DeepChunk { pixels: block_pixels, row_w, prefix, pos: off, packed_data, unpacked_data }); + } + if seen.iter().any(|s| !s) { + let missing = seen.iter().filter(|s| !**s).count(); + return Err(err(format!("{missing} of {slots} chunks are missing from the offset table"))); + } + + // Global cumulative counts in raster order. + let mut deep_counts = Vec::with_capacity(npixels + 1); + deep_counts.push(0u64); + let mut acc = 0u64; + for &c in &counts { + acc = acc.checked_add(c as u64).ok_or_else(|| err("the total sample count overflows"))?; + deep_counts.push(acc); + } + let total_samples = acc; + let sample_bytes_total = total_samples.checked_mul(exr_types.len() as u64 * 4).ok_or_else(|| err("the sample buffer size overflows"))?; + if sample_bytes_total > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("deep samples need {sample_bytes_total} bytes, exceeding max_alloc {}", limits.max_alloc))); + } + + let mut channels: Vec = header + .channels + .list + .iter() + .map(|c| DeepChannel { name: c.name.to_string(), sample: our_sample_type(c.sample_type), samples: vec![0f32; total_samples as usize] }) + .collect(); + + // Second pass: the compressed sample data, scattered per pixel (a block's channel data + // is contiguous in the file but the pixels' samples interleave with other blocks'). + for chunk in &chunks { + seek_to(&mut cur, chunk.pos)?; + if multipart { + let _part = i32(&mut cur)?; + } + match &header.blocks { + BlockDescription::ScanLines => { + let _y = i32(&mut cur)?; + } + BlockDescription::Tiles(_) => { + let _coords = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + } + } + let packed_table = u64(&mut cur)?; + let _packed_data = u64(&mut cur)?; + let _unpacked_data = u64(&mut cur)?; + // The count table sits between the sizes and the sample data; skip its bytes. + read_exact(&mut cur, packed_table as usize, "the sample-count table")?; + let data_bytes = read_exact(&mut cur, chunk.packed_data as usize, "the deep sample data")?; + let raw = decompress(compression, data_bytes, chunk.unpacked_data as usize, "the deep sample data")?; + // Row by row (a scan line block is one row), and within a row channel by channel, as + // flat tiles are laid out: each pixel's samples are contiguous within its channel. + let mut in_off = 0usize; + let row_w = chunk.row_w.max(1); + for (r, row) in chunk.pixels.chunks(row_w).enumerate() { + let i0 = r * row_w; + let row_from = chunk.prefix.get(i0).copied().unwrap_or(0) as usize; + let row_to = chunk.prefix.get(i0 + row.len()).copied().unwrap_or(0) as usize; + let row_total = row_to.saturating_sub(row_from); + for (ci, t) in exr_types.iter().enumerate() { + let sb = sample_bytes(*t); + let ch_bytes = row_total.checked_mul(sb).ok_or_else(|| err("channel size overflows"))?; + let end = in_off.checked_add(ch_bytes).ok_or_else(|| err("channel size overflows"))?; + let src = raw.get(in_off..end).ok_or_else(|| err("the sample data is short"))?; + in_off = end; + let out = channels.get_mut(ci).ok_or_else(|| err("channel index out of range"))?; + for (j, p) in row.iter().enumerate() { + let from = (chunk.prefix.get(i0 + j).copied().unwrap_or(0) as usize).saturating_sub(row_from); + let n = (chunk.prefix.get(i0 + j + 1).copied().unwrap_or(0) as usize).saturating_sub(row_from).saturating_sub(from); + let to = deep_counts.get(*p).copied().unwrap_or(0) as usize; + for k in 0..n { + let base = (from + k) * sb; + let v = match t { + ExrSample::F16 => src.get(base..base + 2).map(|b| f16::from_le_bytes([b[0], b[1]]).to_f32()), + ExrSample::F32 => src.get(base..base + 4).map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])), + ExrSample::U32 => src.get(base..base + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as f32), + }; + let (Some(v), Some(slot)) = (v, out.samples.get_mut(to + k)) else { + return Err(err("the sample data is short")); + }; + *slot = v; + } + } + } + } + } + + Ok(DeepImage { width: w as u32, height: h as u32, stage, origin, channels, counts: deep_counts }) +} + +/// Where the headers end and the offset tables begin. [`MetaData::read_from_buffered`] +/// consumes exactly the version field and the headers, so re-reading with a fresh cursor +/// and taking its end position yields it. +fn header_end(bytes: &[u8]) -> Result { + let mut cur = Cursor::new(bytes); + MetaData::read_from_buffered(&mut cur, false).map_err(err)?; + Ok(cur.position()) +} + +/// Composite the deep samples into a flat image: per pixel, samples sorted by Z (stable; the +/// file is assumed messy), front-to-back `over` with premultiplied colour, un-premultiplied +/// into straight alpha at the end. Without any alpha channel the front-most sample wins, +/// opaque. Z, ZBack and other deep channels influence the flat result only through the +/// ordering; volume samples (with ZBack) are approximated as point samples. +pub(crate) fn flatten(deep: &DeepImage, limits: &Limits) -> Result { + let npx = deep.width as usize * deep.height as usize; + let has = |n: &str| deep.channel(n).is_some(); + let rgb = has("R") && has("G") && has("B"); + let gray = !rgb && (has("Y") || deep.channels.len() == 1); + let layout = if rgb || gray { + if has("A") || has("AR") || has("AG") || has("AB") { + if rgb { ChannelLayout::Rgba } else { ChannelLayout::GrayA } + } else if rgb { + ChannelLayout::Rgb + } else { + ChannelLayout::Gray + } + } else { + return Err(CodecError::unsupported(F, "no R/G/B or Y channels in the deep part")); + }; + let nc = layout.channels(); + // Admit the flattened output at its true size: nc f32 samples per pixel. The deep decode + // admitted the data window at four bytes per pixel, before the flattened channel layout + // was known, so re-check here with the actual layout and f32 sample size. + limits.check_bytes(deep.width, deep.height, nc as u64 * 4)?; + let samples = |n: &str| -> &[f32] { deep.channel(n).map(|c| c.samples.as_slice()).unwrap_or(&[]) }; + let (r, g, b) = (samples("R"), samples("G"), samples("B")); + let (ar, ag, ab) = (samples("AR"), samples("AG"), samples("AB")); + let (a, z) = (samples("A"), samples("Z")); + let y = samples("Y"); + let lum = if y.is_empty() { deep.channels.first().map(|c| c.samples.as_slice()).unwrap_or(&[]) } else { y }; + + let mut out = vec![0f32; npx * nc]; + let mut order: Vec = Vec::new(); + let mut max_samples = 0u32; + for p in 0..npx { + let range = deep.sample_range(p); + let (s0, n) = (range.start as usize, (range.end - range.start) as usize); + max_samples = max_samples.max(u32::try_from(n).unwrap_or(u32::MAX)); + if n == 0 { + continue; // no samples: transparent black + } + order.clear(); + order.extend(0u32..n as u32); + if !z.is_empty() { + // Stable sort by Z; a NaN depth (invalid) sorts last, deterministically. + order.sort_by(|&i, &j| { + let zi = z.get(s0 + i as usize).copied().unwrap_or(f32::NAN); + let zj = z.get(s0 + j as usize).copied().unwrap_or(f32::NAN); + zi.total_cmp(&zj) + }); + } + let at = |ch: &[f32], i: u32| ch.get(s0 + i as usize).copied().unwrap_or(0.0); + // Per-colour-channel alpha: AR/AG/AB when present, else A, else opaque. + let alpha = |i: u32| -> [f32; 3] { + let s = at(a, i); + [ + if !a.is_empty() && ar.is_empty() { + s + } else if !ar.is_empty() { + at(ar, i) + } else { + 1.0 + }, + if !a.is_empty() && ag.is_empty() { + s + } else if !ag.is_empty() { + at(ag, i) + } else { + 1.0 + }, + if !a.is_empty() && ab.is_empty() { + s + } else if !ab.is_empty() { + at(ab, i) + } else { + 1.0 + }, + ] + }; + let mut cacc = [0f32; 3]; // premultiplied colour + let mut aacc = [0f32; 3]; // accumulated alpha + for &i in &order { + let ai = alpha(i); + let src = if rgb { [at(r, i), at(g, i), at(b, i)] } else { [at(lum, i); 3] }; + let mut covered = true; + for k in 0..3 { + let t = (1.0 - aacc[k]).max(0.0); + cacc[k] += src[k] * t; + aacc[k] += t * ai[k]; + covered &= aacc[k] >= 1.0; + } + if covered { + break; // everything behind is hidden + } + } + let a_out = if a.is_empty() && ar.is_empty() && ag.is_empty() && ab.is_empty() { 1.0 } else { (aacc[0] + aacc[1] + aacc[2]) / 3.0 }; + let a_clamped = a_out.clamp(0.0, 1.0); + // Un-premultiply into straight alpha (the deep colours are premultiplied). + let straight = |c: f32| if a_clamped > 1e-6 { c / a_clamped } else { 0.0 }; + let base = p * nc; + // Written by layout: RGB(A), gray(+A) — a fixed RGBA quadruple would put a luminance + // copy into a GrayA image's alpha slot. + // A fixed array plus a length: this runs once per pixel, so no allocation. + let (vals, len) = match (rgb, nc) { + (true, 4) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), a_clamped], 4), + (true, _) => ([straight(cacc[0]), straight(cacc[1]), straight(cacc[2]), 0.0], 3), + (false, 2) => ([straight(cacc[0]), a_clamped, 0.0, 0.0], 2), + (false, _) => ([straight(cacc[0]), 0.0, 0.0, 0.0], 1), + }; + for (k, v) in vals.into_iter().take(len).enumerate() { + if let Some(slot) = out.get_mut(base + k) { + *slot = v; + } + } + } + // Open on the full display-window stage when the file declares one: the renderer's data + // window is its crop, and the composition lives on the whole stage. The composed region + // pads to the stage at its offset, transparent elsewhere (limits validated the size). + let mut img = match deep.stage { + Some((sw, sh)) => { + // The display stage can be larger than the data window; admit it at the real f32 + // layout size before allocating, since the earlier deep-decode check used four + // bytes per pixel and did not know the flattened channel count. + limits.check_bytes(sw, sh, nc as u64 * 4)?; + let mut padded = vec![0f32; sw as usize * sh as usize * nc]; + let row_len = deep.width as usize * nc; + let dx = deep.origin.0.max(0) as usize; + for row in 0..deep.height as usize { + let src = row * row_len; + let dst = (row + deep.origin.1.max(0) as usize) * sw as usize + dx; + if let (Some(d), Some(r)) = (padded.get_mut(dst * nc..dst * nc + row_len), out.get(src..src + row_len)) { + d.copy_from_slice(r); + } + } + Image::from_f32(sw, sh, layout, &padded)? + } + None => Image::from_f32(deep.width, deep.height, layout, &out)?, + }; + if let Some((sw, sh)) = deep.stage { + img.warnings.push(DecodeWarning::DisplayWindowStage { stage: (sw, sh), origin: deep.origin }); + } + img.warnings.push(DecodeWarning::DeepFlattened { format: F, max_samples_per_pixel: max_samples }); + Ok(img) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write as _; + + #[test] + fn packed_reads_borrow_input_and_reject_bad_sizes_before_allocation() { + let bytes = [1u8, 2, 3, 4]; + let mut cur = Cursor::new(bytes.as_slice()); + let prefix = read_exact(&mut cur, 3, "x").expect("three input bytes"); + assert_eq!(prefix, &[1, 2, 3]); + assert_eq!(prefix.as_ptr(), bytes.as_ptr()); + assert_eq!(cur.position(), 3); + assert!(matches!(read_exact(&mut cur, usize::MAX, "x"), Err(CodecError::Malformed { .. }))); + assert_eq!(cur.position(), 3); + assert_eq!(read_exact(&mut cur, 1, "x").expect("last byte"), &[4]); + assert!(matches!(read_exact(&mut cur, 1, "x"), Err(CodecError::Malformed { .. }))); + cur.set_position(u64::MAX); + assert!(matches!(read_exact(&mut cur, 1, "x"), Err(CodecError::Malformed { .. }))); + } + + /// What the writer stores before compressing: the byte split, then the +128 predictor. + fn predicted(raw: &[u8]) -> Vec { + let half = raw.len().div_ceil(2); + let mut out = vec![0u8; raw.len()]; + for (i, &v) in raw.iter().enumerate() { + out[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = v; + } + let mut prev = 0i16; + for (i, b) in out.iter_mut().enumerate() { + let v = i16::from(*b); + if i > 0 { + *b = (v - prev + 128) as u8; + } + prev = v; + } + out + } + + /// #4958: callers admit `expected` bytes for a decompressed buffer (twice that for a count + /// table, with the copy that undoes its byte split), so decompression must not reserve + /// more. `Vec`'s doubling gave a 4 MiB ZIPS table 8 MiB of capacity and a 3 MiB RLE table + /// 4 MiB, so the "admitted" table and its copy took up to three times the table size. + #[test] + fn decompressed_buffers_reserve_no_more_than_expected() { + for n in [1usize << 20, 2 << 20, (3 << 20) + 1, 4 << 20, 5 << 20] { + let raw = vec![0u8; n]; + let p = predicted(&raw); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + z.write_all(&p).expect("write to a Vec"); + let zips = z.finish().expect("finish a Vec"); + assert!(decompress(Compression::ZIP1, &zips[..zips.len() - 1], n, "the table").is_err()); + // RLE: the first byte as a literal, then runs of up to 128 copies of the next. + let mut rle = vec![0xFF, p[0]]; + for run in p[1..].chunks(128) { + rle.extend_from_slice(&[(run.len() - 1) as u8, run[0]]); + } + for (compression, packed) in [(Compression::ZIP1, &zips), (Compression::RLE, &rle)] { + let out = decompress(compression, packed, n, "the table").expect("decompresses"); + assert_eq!(out, raw, "{compression:?}, {n} bytes"); + assert!(out.capacity() <= n, "{compression:?}: a {n} byte buffer reserved {} bytes", out.capacity()); + } + // One byte short, the same data expands past the expected size and is refused. + for (compression, packed) in [(Compression::ZIP1, &zips), (Compression::RLE, &rle)] { + assert!(decompress(compression, packed, n - 1, "the table").is_err(), "{compression:?}, {n} bytes"); + } + } + } + + /// A size past the limit is malformed data, and a reservation the allocator refuses is an + /// error rather than an abort. + #[test] + fn reservations_past_the_limit_or_the_allocator_are_errors() { + let mut v = Vec::new(); + assert!(matches!(reserve_up_to(&mut v, 2, 1, "x"), Err(CodecError::Malformed { .. }))); + assert!(matches!(reserve_up_to(&mut v, isize::MAX as usize + 1, usize::MAX, "x"), Err(CodecError::LimitExceeded(_)))); + assert_eq!(v.capacity(), 0); + } +} diff --git a/crates/image-codecs/src/codecs/dicom.rs b/crates/image-codecs/src/codecs/dicom.rs new file mode 100644 index 000000000..ea2674dae --- /dev/null +++ b/crates/image-codecs/src/codecs/dicom.rs @@ -0,0 +1,517 @@ +//! DICOM (medical imaging) reader, uncompressed transfer syntaxes only. +//! +//! Supports what a viewer needs from a single-frame image: MONOCHROME1/2 (gray) and RGB or +//! YBR_FULL, 8- and 16-bit samples, interleaved or planar, both byte orders and explicit or +//! implicit VR. Undefined-length sequences outside the pixel data are skipped (clinical files +//! contain them); compressed transfer syntaxes, encapsulated pixel data and other photometric +//! interpretations are refused with a clear error. Read-only in this crate. + +use std::collections::HashMap; + +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Dicom; +const PREAMBLE: usize = 132; // 128 zero bytes + "DICM" +const UNDEFINED: u32 = 0xFFFF_FFFF; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +/// A DICOM file begins with a 128-byte preamble and the `DICM` magic. +pub(crate) fn is_dicom(b: &[u8]) -> bool { + b.get(128..132) == Some(b"DICM") +} + +#[derive(Clone, Copy)] +struct Endian { + big: bool, +} + +/// The transfer syntax: byte order and whether value representations are explicit. +#[derive(Clone, Copy)] +struct Syntax { + endian: Endian, + explicit: bool, +} + +/// The value representations whose length field is 2 reserved bytes + `u32` in explicit VR. +fn long_vr(vr: &[u8]) -> bool { + matches!(vr, b"OB" | b"OW" | b"OF" | b"SQ" | b"UT" | b"UN" | b"OD" | b"OL" | b"OV" | b"UC" | b"UR") +} + +/// A cursor over the byte stream. +struct Cur<'a> { + b: &'a [u8], + pos: usize, + big: bool, +} + +impl<'a> Cur<'a> { + fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let s = self.b.get(self.pos..self.pos.checked_add(n)?)?; + self.pos += n; + Some(s) + } + fn u16(&mut self) -> Option { + let c = self.take(2)?; + Some(if self.big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) + } + fn u32(&mut self) -> Option { + let c = self.take(4)?; + Some(if self.big { u32::from_be_bytes([c[0], c[1], c[2], c[3]]) } else { u32::from_le_bytes([c[0], c[1], c[2], c[3]]) }) + } + /// Skip `len` bytes, refusing to run past the buffer. + fn skip(&mut self, len: usize) -> Result<(), CodecError> { + self.pos = self.pos.checked_add(len).filter(|p| *p <= self.b.len()).ok_or_else(|| bad("a value runs past the end of the file"))?; + Ok(()) + } +} + +/// Read one element's tag and value length, leaving the cursor at the value. Item and delimiter +/// tags (group `FFFE`) carry a `u32` length with no VR even under explicit VR. +fn read_header(c: &mut Cur, syn: &Syntax) -> Result<(u16, u16, [u8; 2], u32), CodecError> { + let group = c.u16().ok_or_else(|| bad("truncated element tag"))?; + let elem = c.u16().ok_or_else(|| bad("truncated element tag"))?; + let (vr, len) = if group == 0xFFFE { + ([0, 0], c.u32().ok_or_else(|| bad("truncated item length"))?) + } else if syn.explicit { + let vr = c.take(2).ok_or_else(|| bad("truncated VR"))?; + let v = [vr[0], vr[1]]; + let len = if long_vr(&v) { + c.take(2).ok_or_else(|| bad("truncated VR"))?; // reserved + c.u32().ok_or_else(|| bad("truncated length"))? + } else { + u32::from(c.u16().ok_or_else(|| bad("truncated length"))?) + }; + (v, len) + } else { + ([0, 0], c.u32().ok_or_else(|| bad("truncated length"))?) + }; + Ok((group, elem, vr, len)) +} + +/// Skip an undefined-length sequence, ending just after its `FFFE,E0DD` delimiter. Nesting is +/// tracked through undefined-length items and nested sequences; everything else is skipped by +/// its length. Returns the position after the delimiter. +fn skip_undefined(c: &mut Cur, syn: &Syntax) -> Result<(), CodecError> { + let mut depth = 1u32; + while depth > 0 { + let (group, elem, _, len) = read_header(c, syn)?; + // Item (E00D) and sequence (E0DD) delimiters close one undefined length; they carry no + // content of their own. + if group == 0xFFFE && matches!(elem, 0xE00D | 0xE0DD) { + depth -= 1; + continue; + } + if len == UNDEFINED { + depth += 1; + } else { + c.skip(len as usize)?; + } + } + Ok(()) +} + +/// Walk the elements from `start`, calling `f` with each `(group, element, value)`. An +/// undefined-length sequence outside the pixel data is skipped; encapsulated (undefined-length) +/// pixel data is refused. Returns the number of bytes walked. +fn elements(b: &[u8], start: usize, syn: &Syntax, mut f: impl FnMut(u16, u16, &Elem) -> Result<(), CodecError>) -> Result { + let mut c = Cur { b, pos: start, big: syn.endian.big }; + loop { + if c.pos >= b.len() { + return Ok(c.pos - start); + } + let (group, elem, vr, len) = read_header(&mut c, syn)?; + if len == UNDEFINED { + if (group, elem) == (0x7FE0, 0x0010) { + return Err(CodecError::unsupported(F, "encapsulated (compressed) pixel data")); + } + skip_undefined(&mut c, syn)?; + continue; + } + let len = len as usize; + let start = c.pos; + if b.len() < start.saturating_add(len) { + return Err(bad("an element value runs past the end of the file")); + } + f(group, elem, &Elem { vr, start, len })?; + c.pos = start + len; + } +} + +/// A parsed element: its VR and the byte range of its value. +struct Elem { + vr: [u8; 2], + start: usize, + len: usize, +} + +fn u16_val(b: &[u8], e: &Elem, big: bool) -> Option { + let c = b.get(e.start..e.start.checked_add(2)?)?; + Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }) +} + +/// The transfer syntax (byte order and explicit/implicit VR) and the data-set start. The file +/// meta group is always explicit VR little-endian; the data set follows its `(0002,0010)` +/// TransferSyntaxUID. +fn transfer_syntax(b: &[u8]) -> Result<(Syntax, usize), CodecError> { + let meta = Syntax { endian: Endian { big: false }, explicit: true }; + let mut ts: Option = None; + let mut data_start = b.len(); + let mut c = Cur { b, pos: PREAMBLE, big: false }; + while c.pos + 8 <= b.len() { + let save = c.pos; + let (group, elem, _, len) = read_header(&mut c, &meta)?; + if group != 0x0002 { + // A non-meta group begins the data set. + data_start = save; + break; + } + if len == UNDEFINED { + return Err(CodecError::unsupported(F, "undefined-length meta element")); + } + let start = c.pos; + if b.len() < start.saturating_add(len as usize) { + return Err(bad("a meta element runs past the end of the file")); + } + if elem == 0x0010 { + ts = Some(String::from_utf8_lossy(&b[start..start + len as usize]).trim_end_matches(['\0', ' ']).to_string()); + } + c.pos = start + len as usize; + } + let syn = match ts.as_deref() { + Some("1.2.840.10008.1.2") => Syntax { endian: Endian { big: false }, explicit: false }, + Some("1.2.840.10008.1.2.1") => Syntax { endian: Endian { big: false }, explicit: true }, + Some("1.2.840.10008.1.2.2") => Syntax { endian: Endian { big: true }, explicit: true }, + Some(other) => return Err(CodecError::unsupported(F, format!("transfer syntax {other} (only uncompressed is read)"))), + // No meta: the historical default is implicit VR little-endian. + None => Syntax { endian: Endian { big: false }, explicit: false }, + }; + Ok((syn, data_start)) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !is_dicom(b) { + return Err(bad("not a DICOM file (no DICM magic)")); + } + let (syn, start) = transfer_syntax(b)?; + let mut tags: HashMap<(u16, u16), Elem> = HashMap::new(); + elements(b, start, &syn, |g, e, el| { + // Later duplicates must not silently override the first (DICOM forbids duplicates). + tags.entry((g, e)).or_insert_with(|| Elem { vr: el.vr, start: el.start, len: el.len }); + Ok(()) + })?; + let big = syn.endian.big; + let get_u16 = |tag: (u16, u16)| tags.get(&tag).and_then(|e| u16_val(b, e, big)); + let rows = u32::from(get_u16((0x0028, 0x0010)).ok_or_else(|| bad("missing Rows (0028,0010)"))?); + let cols = u32::from(get_u16((0x0028, 0x0011)).ok_or_else(|| bad("missing Columns (0028,0011)"))?); + let samples = u32::from(get_u16((0x0028, 0x0002)).unwrap_or(1)); + let bits = get_u16((0x0028, 0x0100)).ok_or_else(|| bad("missing BitsAllocated (0028,0100)"))?; + let bits_stored = get_u16((0x0028, 0x0101)).unwrap_or(bits); + let high_bit = get_u16((0x0028, 0x0102)).unwrap_or(bits_stored.saturating_sub(1)); + if u32::from(high_bit) >= u32::from(bits) { + return Err(bad("HighBit is outside the allocated bits")); + } + let signed = get_u16((0x0028, 0x0103)).unwrap_or(0) == 1; + let planar = get_u16((0x0028, 0x0006)).unwrap_or(0); + let photometric = tags + .get(&(0x0028, 0x0004)) + .and_then(|e| b.get(e.start..e.start.checked_add(e.len)?)) + .map(|v| String::from_utf8_lossy(v).trim_end_matches(['\0', ' ']).to_string()) + .ok_or_else(|| bad("missing PhotometricInterpretation (0028,0004)"))?; + let pixel = tags.get(&(0x7FE0, 0x0010)).ok_or_else(|| CodecError::unsupported(F, "no pixel data (7FE0,0010)"))?; + if !matches!(bits, 8 | 16) || !matches!(bits_stored, 1..=16) { + return Err(CodecError::unsupported(F, format!("{bits}-bit allocated / {bits_stored}-bit stored samples"))); + } + let mono = matches!(photometric.as_str(), "MONOCHROME1" | "MONOCHROME2"); + let rgb = photometric == "RGB"; + let ybr = photometric == "YBR_FULL"; + if !(mono || rgb || ybr) { + return Err(CodecError::unsupported(F, format!("photometric interpretation {photometric}"))); + } + // Mono stores one sample; RGB and YBR_FULL store three. Any other count would index past the + // pixel data, so it is refused rather than misread. + let want_samples = if mono { 1 } else { 3 }; + if samples != want_samples { + return Err(CodecError::unsupported(F, format!("{samples} samples per pixel with {photometric}"))); + } + if ybr && bits != 8 { + return Err(CodecError::unsupported(F, format!("{bits}-bit {photometric} (8-bit only)"))); + } + let layout = if mono { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + let out_sample = if bits == 8 { SampleType::U8 } else { SampleType::U16 }; + limits.check(cols, rows, layout, out_sample)?; + + let pixels = (cols as usize).checked_mul(rows as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let n = pixels.checked_mul(samples as usize).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let bytes_per = if bits == 16 { 2usize } else { 1 }; + let need = n.checked_mul(bytes_per).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?; + let data = b + .get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?) + .ok_or_else(|| bad("pixel data is truncated"))?; + let out_channels = layout.channels(); + let out_len = pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let mut out = Vec::new(); + out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + // Raw sample of channel `ch` at pixel `p`, honouring the planar configuration. + let sample_at = |p: usize, ch: usize| -> Option { + let idx = if planar == 1 { ch.checked_mul(pixels)?.checked_add(p)? } else { p.checked_mul(samples as usize)?.checked_add(ch)? }; + let off = idx.checked_mul(bytes_per)?; + if bits == 16 { + let c = data.get(off..off.checked_add(2)?)?; + Some(if big { u16::from_be_bytes([c[0], c[1]]) } else { u16::from_le_bytes([c[0], c[1]]) }.into()) + } else { + Some(u32::from(*data.get(off)?)) + } + }; + let stored_max = f64::from((1u32 << bits_stored) - 1); + // Extract the `bits_stored` samples (the least significant one at `high_bit`) and scale to + // [0, 1]; signed values are shifted up by half a range first. `high_bit` is 1-based here. + let shift = (u32::from(high_bit) + 1).saturating_sub(u32::from(bits_stored)); + let mask = (1u32 << bits_stored) - 1; + let half = 1u32 << (bits_stored - 1); + let norm = move |raw: u32| -> f64 { + let v = (raw >> shift) & mask; + // Signed: flip the sign bit to map [-2^(b-1), 2^(b-1)-1] onto [0, 2^b-1]. + let v = if signed { v ^ half } else { v }; + (f64::from(v) / stored_max).clamp(0.0, 1.0) + }; + let q = |x: f64| -> u16 { (x * 65535.0).round() as u16 }; + let push = |out: &mut Vec, x: f64| { + if bits == 8 { + out.push((x * 255.0).round() as u8); + } else { + out.extend_from_slice(&q(x).to_ne_bytes()); + } + }; + for p in 0..pixels { + let s = |ch: usize| sample_at(p, ch).ok_or_else(|| bad("pixel data is shorter than the image")); + if mono { + let x = norm(s(0)?); + push(&mut out, if photometric == "MONOCHROME1" { 1.0 - x } else { x }); + } else if rgb { + for ch in 0..3 { + push(&mut out, norm(s(ch)?)); + } + } else { + // YBR_FULL (8-bit): BT.601 full-range to RGB. + let y = f64::from(s(0)?); + let cb = f64::from(s(1)?) - 128.0; + let cr = f64::from(s(2)?) - 128.0; + let r = (y + 1.402 * cr).clamp(0.0, 255.0); + let g = (y - 0.344_136 * cb - 0.714_136 * cr).clamp(0.0, 255.0); + let bl = (y + 1.772 * cb).clamp(0.0, 255.0); + for v in [r, g, bl] { + push(&mut out, v / 255.0); + } + } + } + Image::from_raw(cols, rows, layout, out_sample, out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Build the file meta group (explicit VR little-endian), including the given extra + /// `(element, ascii)` pairs before the TransferSyntaxUID. + fn meta(b: &mut Vec, ts: &str, extra: &[(u16, &str)]) { + let ui = |e: u16, v: &str, b: &mut Vec| { + let mut s = v.as_bytes().to_vec(); + if s.len() % 2 == 1 { + s.push(0); + } + b.extend_from_slice(&0x0002u16.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + b.extend_from_slice(b"UI"); + b.extend_from_slice(&(s.len() as u16).to_le_bytes()); + b.extend_from_slice(&s); + }; + for (e, v) in extra { + ui(*e, v, b); + } + ui(0x0010, ts, b); + } + + fn us(b: &mut Vec, g: u16, e: u16, v: u16, explicit: bool) { + b.extend_from_slice(&g.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + if explicit { + b.extend_from_slice(b"US"); + b.extend_from_slice(&2u16.to_le_bytes()); + } else { + b.extend_from_slice(&2u32.to_le_bytes()); + } + b.extend_from_slice(&v.to_le_bytes()); + } + + fn cs(b: &mut Vec, g: u16, e: u16, v: &str, explicit: bool) { + let mut s = v.as_bytes().to_vec(); + if s.len() % 2 == 1 { + s.push(0); + } + b.extend_from_slice(&g.to_le_bytes()); + b.extend_from_slice(&e.to_le_bytes()); + if explicit { + b.extend_from_slice(b"CS"); + b.extend_from_slice(&(s.len() as u16).to_le_bytes()); + } else { + b.extend_from_slice(&(s.len() as u32).to_le_bytes()); + } + b.extend_from_slice(&s); + } + + /// A DICOM with the given transfer syntax UID and data-set encoding. + #[allow(clippy::too_many_arguments)] + fn synth(ts: &str, explicit: bool, big: bool, planar: u16, rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec { + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + // One extra meta element before the syntax, so a reader that only looks at the last + // group-0002 element takes the wrong one. + meta(&mut b, ts, &[(0x0002, "1.2.840.10008.5.1.4.1.1.7")]); + let _ = big; + us(&mut b, 0x0028, 0x0010, rows, explicit); + us(&mut b, 0x0028, 0x0011, cols, explicit); + us(&mut b, 0x0028, 0x0002, samples, explicit); + us(&mut b, 0x0028, 0x0006, planar, explicit); + cs(&mut b, 0x0028, 0x0004, photometric, explicit); + us(&mut b, 0x0028, 0x0100, bits, explicit); + us(&mut b, 0x0028, 0x0101, bits, explicit); + us(&mut b, 0x0028, 0x0102, bits - 1, explicit); + us(&mut b, 0x0028, 0x0103, 0, explicit); + b.extend_from_slice(&0x7FE0u16.to_le_bytes()); + b.extend_from_slice(&0x0010u16.to_le_bytes()); + if explicit { + b.extend_from_slice(b"OB"); + b.extend_from_slice(&0u16.to_le_bytes()); + } + b.extend_from_slice(&(pixels.len() as u32).to_le_bytes()); + b.extend_from_slice(pixels); + b + } + + #[test] + fn decodes_8bit_monochrome() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (2, 2, ChannelLayout::Gray, SampleType::U8)); + assert_eq!(img.data(), &[0, 51, 102, 255]); + } + + #[test] + fn decodes_16bit_monochrome_and_scales() { + let px: Vec = [0u16, 32768, 65535].iter().flat_map(|v| v.to_le_bytes()).collect(); + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 3, 1, 1, 16, "MONOCHROME2", &px); + let img = decode(&b, &Limits::default()).unwrap(); + let v = img.to_u16_samples().unwrap(); + assert_eq!(v[0], 0); + assert!((i32::from(v[1]) - 32768).abs() <= 1); + assert_eq!(v[2], 65535); + } + + #[test] + fn monochrome1_is_inverted() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 1, 1, 8, "MONOCHROME1", &[0, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[255, 0]); + } + + #[test] + fn decodes_rgb_interleaved_and_planar() { + // 2 pixels: (10,20,30) and (40,50,60). + let interleaved = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 2, 3, 8, "RGB", &[10, 20, 30, 40, 50, 60]); + let img = decode(&interleaved, &Limits::default()).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[10u8, 20, 30, 40, 50, 60][..])); + // Planar 0: R plane, then G, then B. + let planar = synth("1.2.840.10008.1.2.1", true, false, 1, 1, 2, 3, 8, "RGB", &[10, 40, 20, 50, 30, 60]); + let img = decode(&planar, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20, 30, 40, 50, 60]); + } + + #[test] + fn decodes_ybr_full_to_rgb() { + // White (Y=128, Cb=Cr=128) stays neutral grey. + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 3, 8, "YBR_FULL", &[128, 128, 128]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgb); + let d = img.data(); + assert!(d[0].abs_diff(d[1]) <= 1 && d[1].abs_diff(d[2]) <= 1, "{d:?}"); + } + + #[test] + fn decodes_implicit_vr() { + let b = synth("1.2.840.10008.1.2", false, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 51, 102, 255]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[0, 51, 102, 255]); + } + + #[test] + fn skips_an_undefined_length_sequence_outside_the_pixel_data() { + // Insert an undefined-length SQ (0008,2218) with one item, then a defined-length one, + // before the pixel data: the reader must skip both and still decode the image. + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + meta(&mut b, "1.2.840.10008.1.2.1", &[]); + for (g, e, v) in [ + (0x0028, 0x0010, 1u16), + (0x0028, 0x0011, 1), + (0x0028, 0x0002, 1), + (0x0028, 0x0100, 8), + (0x0028, 0x0101, 8), + (0x0028, 0x0102, 7), + (0x0028, 0x0103, 0), + ] { + us(&mut b, g, e, v, true); + } + cs(&mut b, 0x0028, 0x0004, "MONOCHROME2", true); + // Undefined-length sequence (0008,2211), one undefined-length item containing a nested + // defined-length sequence, then the delimiters. + b.extend_from_slice(&[0x08, 0x00, 0x11, 0x22, b'S', b'Q', 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]); + b.extend_from_slice(&[0xFE, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF]); // item, undefined + b.extend_from_slice(&[0x08, 0x00, 0x55, 0x22, b'S', b'Q', 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 1, 2, 3, 4]); // nested, defined + b.extend_from_slice(&[0xFE, 0xFF, 0x0D, 0xE0, 0x00, 0x00, 0x00, 0x00]); // item delimitation + b.extend_from_slice(&[0xFE, 0xFF, 0xDD, 0xE0, 0x00, 0x00, 0x00, 0x00]); // sequence delimitation + // Pixel data (defined length). + b.extend_from_slice(&0x7FE0u16.to_le_bytes()); + b.extend_from_slice(&0x0010u16.to_le_bytes()); + b.extend_from_slice(b"OB"); + b.extend_from_slice(&0u16.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.push(200); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[200]); + } + + #[test] + fn truncated_and_bad_is_error_not_panic() { + let b = synth("1.2.840.10008.1.2.1", true, false, 0, 2, 2, 1, 8, "MONOCHROME2", &[0, 1, 2, 3]); + for cut in [0, 4, 128, 132, 140, b.len() - 1] { + assert!(decode(&b[..cut.min(b.len())], &Limits::default()).is_err(), "cut {cut}"); + } + assert!(decode(b"not dicom", &Limits::default()).is_err()); + } + + #[test] + fn high_bit_outside_the_allocated_bits_is_an_error_not_a_panic() { + let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[7]); + let tag = [0x28, 0x00, 0x02, 0x01, b'U', b'S', 0x02, 0x00]; + let at = b.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len(); + b[at..at + 2].copy_from_slice(&200u16.to_le_bytes()); + assert!(decode(&b, &Limits::default()).is_err()); + } + + #[test] + fn rejects_compressed_transfer_syntax() { + let mut b = synth("1.2.840.10008.1.2.1", true, false, 0, 1, 1, 1, 8, "MONOCHROME2", &[0]); + let uncompressed = b"1.2.840.10008.1.2.1\0"; + let compressed = b"1.2.840.10008.1.2.4\0"; // same length, a compressed family + let at = b.windows(uncompressed.len()).position(|w| w == uncompressed).expect("transfer syntax in the fixture"); + b[at..at + compressed.len()].copy_from_slice(compressed); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs new file mode 100644 index 000000000..86e4938b0 --- /dev/null +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -0,0 +1,886 @@ +//! DPX (SMPTE 268M Digital Picture Exchange), implemented here from the published layout: the +//! 1664-byte generic header (file and image information, image source), the 384-byte industry +//! header (film and television), then the image data of each element. +//! +//! Reading: +//! +//! * Both byte orders: `SDPX` (big-endian) and `XPDS` (little-endian). +//! * The first image element, descriptor 6 (luma), 50 (RGB) or 51 (RGBA), unsigned, without +//! run-length encoding, at 8, 10, 12 or 16 bits. 10-bit samples are packed into 32-bit words +//! (packing 0), or three to a word with the padding bits at the bottom (1, method A) or the +//! top (2, method B); 12-bit samples are packed (0) or one to a 16-bit word in its top bits +//! (1, method A). Lines start on a 32-bit word boundary. +//! * 8- and 16-bit code values become U8 and U16 samples unchanged; 10- and 12-bit ones become U16 +//! as `round(c × 65535 / (2^b − 1))`, so they come back exactly when written at their depth. +//! * No colour transform: the transfer characteristic and colorimetric specification are reported +//! ([`DecodeWarning::DpxEncoding`]), so a log frame is labelled log, not shown as display-referred. +//! * The image orientation is applied on import (the caller turns the image upright). +//! * Header fields a pipeline relies on travel as text metadata under the [`KEYS`] names, and a +//! DPX export writes them back. +//! +//! Writing: one element, big-endian, unencoded, descriptor 6, 50 or 51; 8 and 16 bits with +//! packing 0, 10 and 12 bits with packing 1 (method A). The bit depth follows the image +//! (8 → 8, 16 → 16) unless [`EncodeOptions::dpx_bits`] picks one. +//! +//! Kodak Cineon (`.cin`), the format DPX grew from, is read and written here too (see its section). + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{DpxBits, EncodeOptions, Limits}; + +const F: Format = Format::Dpx; +/// Generic plus industry header: where our image data starts. +const HEADER_LEN: usize = 2048; +const GENERIC_LEN: u32 = 1664; +const INDUSTRY_LEN: u32 = 384; +/// "Undefined" for 32-bit fields (also the bit pattern of an undefined float). +const UNDEF32: u32 = u32::MAX; +const UNDEF8: u8 = u8::MAX; +/// Offset of the first image element and the size of one. +const ELEMENT0: usize = 780; +const ELEMENT_LEN: usize = 72; +const MAX_ELEMENTS: u16 = 8; + +// Header field offsets (generic file header, image information, film and TV headers). +const CREATION_TIME: (usize, usize) = (136, 24); +const CREATOR: (usize, usize) = (160, 100); +const PROJECT: (usize, usize) = (260, 200); +const COPYRIGHT: (usize, usize) = (460, 200); +const ORIENTATION: usize = 768; +const FRAME_POSITION: usize = 1712; +const FRAME_RATE: usize = 1724; +const TIME_CODE: usize = 1920; + +/// Text metadata keys of the header fields kept through a DPX → document → DPX round trip. +pub(crate) const KEY_CREATOR: &str = "DPX:Creator"; +pub(crate) const KEY_PROJECT: &str = "DPX:Project"; +pub(crate) const KEY_COPYRIGHT: &str = "DPX:Copyright"; +pub(crate) const KEY_CREATION_TIME: &str = "DPX:CreationTime"; +pub(crate) const KEY_TRANSFER: &str = "DPX:Transfer"; +pub(crate) const KEY_COLORIMETRIC: &str = "DPX:Colorimetric"; +pub(crate) const KEY_FRAME_POSITION: &str = "DPX:FramePosition"; +pub(crate) const KEY_FRAME_RATE: &str = "DPX:FrameRate"; +pub(crate) const KEY_TIME_CODE: &str = "DPX:TimeCode"; +pub(crate) const KEYS: [&str; 9] = + [KEY_CREATOR, KEY_PROJECT, KEY_COPYRIGHT, KEY_CREATION_TIME, KEY_TRANSFER, KEY_COLORIMETRIC, KEY_FRAME_POSITION, KEY_FRAME_RATE, KEY_TIME_CODE]; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +fn unsupported(msg: impl Into) -> CodecError { + CodecError::unsupported(F, msg) +} + +/// The file's byte order. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Order { + Big, + Little, +} + +impl Order { + fn bytes(b: &[u8], at: usize) -> Option<[u8; N]> { + b.get(at..at.checked_add(N)?)?.try_into().ok() + } + + fn u16(self, b: &[u8], at: usize) -> Option { + let s = Self::bytes::<2>(b, at)?; + Some(if self == Order::Big { u16::from_be_bytes(s) } else { u16::from_le_bytes(s) }) + } + + fn u32(self, b: &[u8], at: usize) -> Option { + let s = Self::bytes::<4>(b, at)?; + Some(if self == Order::Big { u32::from_be_bytes(s) } else { u32::from_le_bytes(s) }) + } + + /// A 32-bit field that may be undefined (all ones). + fn defined_u32(self, b: &[u8], at: usize) -> Option { + self.u32(b, at).filter(|&v| v != UNDEF32) + } + + fn f32(self, b: &[u8], at: usize) -> Option { + self.defined_u32(b, at).map(f32::from_bits).filter(|v| v.is_finite()) + } +} + +/// The byte order from the magic number, `None` when it is not DPX. +fn order(b: &[u8]) -> Option { + match b.get(..4)? { + b"SDPX" => Some(Order::Big), + b"XPDS" => Some(Order::Little), + _ => None, + } +} + +/// An ASCII header string: up to the first NUL, printable characters only, trimmed. `None` when +/// empty or undefined (all ones). +fn string(b: &[u8], (at, len): (usize, usize)) -> Option { + let raw = b.get(at..at.checked_add(len)?)?; + let end = raw.iter().position(|&c| c == 0).unwrap_or(raw.len()); + let s: String = raw.get(..end)?.iter().filter(|c| (32..=126).contains(*c)).map(|&c| c as char).collect(); + Some(s.trim().to_string()).filter(|s| !s.is_empty()) +} + +/// The descriptive fields of image element 0 that decoding needs. +#[derive(Debug, Clone, Copy)] +struct Element { + signed: bool, + descriptor: u8, + transfer: u8, + colorimetric: u8, + bits: u8, + packing: u16, + encoding: u16, + data_offset: usize, + eol_padding: usize, +} + +struct Header { + order: Order, + width: u32, + height: u32, + elements: u16, + element: Element, +} + +/// The decoder opens only element zero; protect the other declared elements from flat saves. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + Ok(parse(bytes)?.elements > 1) +} + +fn parse(b: &[u8]) -> Result { + let o = order(b).ok_or_else(|| err("missing SDPX/XPDS magic number"))?; + let missing = |what: &str| err(format!("the header ends before {what}")); + let image_offset = o.u32(b, 4).ok_or_else(|| missing("the image data offset"))?; + o.u16(b, ORIENTATION).ok_or_else(|| missing("the orientation"))?; + let elements = o.u16(b, ORIENTATION + 2).ok_or_else(|| missing("the element count"))?; + if !(1..=MAX_ELEMENTS).contains(&elements) { + return Err(err(format!("invalid number of image elements {elements}"))); + } + let width = o.u32(b, 772).ok_or_else(|| missing("the line length"))?; + let height = o.u32(b, 776).ok_or_else(|| missing("the line count"))?; + let e = ELEMENT0; + let field = |at: usize| o.u32(b, e + at).ok_or_else(|| missing("image element 1")); + let byte = |at: usize| b.get(e + at).copied().ok_or_else(|| missing("image element 1")); + let half = |at: usize| o.u16(b, e + at).ok_or_else(|| missing("image element 1")); + let data_offset = match field(28)? { + 0 | UNDEF32 => image_offset, + v => v, + }; + let eol_padding = match field(32)? { + UNDEF32 => 0, + v => v, + }; + let element = Element { + signed: field(0)? == 1, + descriptor: byte(20)?, + transfer: byte(21)?, + colorimetric: byte(22)?, + bits: byte(23)?, + packing: half(24)?, + encoding: half(26)?, + data_offset: usize::try_from(data_offset).map_err(|_| err("image data offset out of range"))?, + eol_padding: usize::try_from(eol_padding).map_err(|_| err("line padding out of range"))?, + }; + Ok(Header { order: o, width, height, elements, element }) +} + +/// The EXIF orientation (1–8) that turns the stored lines upright: the DPX orientation codes 0–7 +/// in SMPTE 268M order (left-to-right top-to-bottom, mirrored, flipped, rotated, transposed…). +/// Anything else, undefined included, is taken as the default (1). +pub(crate) fn orientation(b: &[u8]) -> u16 { + let Some(o) = order(b).and_then(|o| o.u16(b, ORIENTATION)) else { return 1 }; + match o { + 1 => 2, + 2 => 4, + 3 => 3, + 4 => 5, + 5 => 6, + 6 => 8, + 7 => 7, + _ => 1, + } +} + +/// Bytes in one stored line of `samples` samples, padded to a 32-bit word. +fn line_bytes(samples: usize, bits: u8, packing: u16) -> Option { + let words = match (bits, packing) { + // Three samples to a word, or one to a 16-bit half word. + (10, 1 | 2) => samples.div_ceil(3), + (12, 1 | 2) => samples.div_ceil(2), + _ => samples.checked_mul(usize::from(bits))?.div_ceil(32), + }; + words.checked_mul(4) +} + +/// Code values of one stored line. +fn unpack_line(line: &[u8], o: Order, bits: u8, packing: u16, out: &mut [u16]) -> Result<(), CodecError> { + let short = || err("truncated image data"); + match (bits, packing) { + (8, _) => { + let src = line.get(..out.len()).ok_or_else(short)?; + for (v, &c) in out.iter_mut().zip(src) { + *v = u16::from(c); + } + } + (16, _) => { + for (i, v) in out.iter_mut().enumerate() { + *v = o.u16(line, i * 2).ok_or_else(short)?; + } + } + (10, 1 | 2) => { + let base = if packing == 1 { 22 } else { 20 }; + for (i, v) in out.iter_mut().enumerate() { + let word = o.u32(line, i / 3 * 4).ok_or_else(short)?; + *v = ((word >> (base - 10 * (i % 3) as u32)) & 0x3FF) as u16; + } + } + (12, 1 | 2) => { + let shift = if packing == 1 { 4 } else { 0 }; + for (i, v) in out.iter_mut().enumerate() { + *v = (o.u16(line, i * 2).ok_or_else(short)? >> shift) & 0xFFF; + } + } + // Packing 0: one bit stream through the line's 32-bit words, high bits first. + _ => { + let mask = (1u64 << bits) - 1; + let (mut acc, mut have, mut word) = (0u64, 0u32, 0usize); + for v in out.iter_mut() { + while have < u32::from(bits) { + acc = (acc << 32) | u64::from(o.u32(line, word * 4).ok_or_else(short)?); + word += 1; + have += 32; + } + have -= u32::from(bits); + *v = ((acc >> have) & mask) as u16; + } + } + } + Ok(()) +} + +/// A 10- or 12-bit code value as a 16-bit sample: `round(c × 65535 / max)`. +fn widen(c: u16, max: u64) -> u16 { + ((u64::from(c) * 2 * 65535 + max) / (2 * max)).min(65535) as u16 +} + +/// The inverse of [`widen`]: `round(v × max / 65535)`. +fn narrow(v: u16, max: u64) -> u16 { + ((u64::from(v) * 2 * max + 65535) / (2 * 65535)) as u16 +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let h = parse(bytes)?; + let e = h.element; + let layout = match e.descriptor { + 6 => ChannelLayout::Gray, + 50 => ChannelLayout::Rgb, + 51 => ChannelLayout::Rgba, + d => return Err(unsupported(format!("descriptor {d} is not supported (only 6 luma, 50 RGB and 51 RGBA)"))), + }; + if e.signed { + return Err(unsupported("data sign 1 (signed samples) is not supported")); + } + if e.encoding != 0 { + return Err(unsupported(format!("encoding {} (run-length encoded data) is not supported", e.encoding))); + } + let packing_ok = match e.bits { + 8 | 16 => e.packing == 0, + 10 => e.packing <= 2, + 12 => e.packing <= 1, + b => return Err(unsupported(format!("bit size {b} is not supported (only 8, 10, 12 and 16)"))), + }; + if !packing_ok { + return Err(unsupported(format!("packing {} is not supported at {} bits", e.packing, e.bits))); + } + let sample = if e.bits == 8 { SampleType::U8 } else { SampleType::U16 }; + limits.check(h.width, h.height, layout, sample)?; + + let nc = layout.channels(); + let per_line = (h.width as usize).checked_mul(nc).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + let height = h.height as usize; + let data = bytes.get(e.data_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?; + let too_large = || CodecError::LimitExceeded("image too large".into()); + let mut line_len = line_bytes(per_line, e.bits, e.packing).ok_or_else(too_large)?; + let mut stride = line_len.checked_add(e.eol_padding).ok_or_else(too_large)?; + // Lines run `stride` apart; the last one needs no padding after it. + let span = |stride: usize, line_len: usize| stride.checked_mul(height - 1)?.checked_add(line_len); + // Some writers pack 8- and 16-bit lines without padding them to a word; take that layout + // when the file is too short for padded lines but holds the unpadded ones. + let tight = per_line * usize::from(e.bits / 8); + if matches!(e.bits, 8 | 16) && e.eol_padding == 0 && line_len != tight && span(stride, line_len).is_none_or(|need| data.len() < need) { + (line_len, stride) = (tight, tight); + } + if span(stride, line_len).ok_or_else(too_large)? > data.len() { + return Err(err("truncated image data")); + } + + let mut codes = vec![0u16; per_line.checked_mul(height).ok_or_else(too_large)?]; + for (y, row) in codes.chunks_exact_mut(per_line.max(1)).enumerate() { + let start = y * stride; + let line = data.get(start..).ok_or_else(|| err("truncated image data"))?; + unpack_line(line, h.order, e.bits, e.packing, row)?; + } + + let mut img = match e.bits { + 8 => Image::from_u8(h.width, h.height, layout, codes.iter().map(|&c| c as u8).collect())?, + 16 => Image::from_u16(h.width, h.height, layout, &codes)?, + b => { + let max = (1u64 << b) - 1; + let wide: Vec = codes.iter().map(|&c| widen(c, max)).collect(); + Image::from_u16(h.width, h.height, layout, &wide)? + } + }; + if h.elements > 1 { + img.warnings.push(DecodeWarning::MoreElements { total: u32::from(h.elements) }); + } + if e.transfer != UNDEF8 || e.colorimetric != UNDEF8 { + img.warnings.push(DecodeWarning::DpxEncoding { transfer: e.transfer, colorimetric: e.colorimetric }); + } + img.meta.text = header_text(bytes, h.order, e); + Ok(img) +} + +// ---------------------------------------------------------------- Cineon (Kodak .cin) +// +// Cineon is the 10-bit film-scan format DPX grew from (same Kodak header family, big-endian). +// The generic header is 1024 bytes: the file information fields, then at offset 192 the image +// orientation (1 byte), the channel count (1 byte) and up to eight 28-byte channel descriptors +// (the first carries the bits per pixel and the line/image dimensions). Image data is three +// 10-bit samples packed into each 32-bit word, lines on a word boundary — the same layout as +// DPX's 10-bit packing method A, so the unpacking and packing are shared. + +/// The Cineon magic number (`802A5FD7` hex), big-endian. +pub(crate) fn is_cineon(b: &[u8]) -> bool { + b.get(..4) == Some(&[0x80, 0x2A, 0x5F, 0xD7]) +} + +/// The EXIF orientation (1–8) for a Cineon header's orientation code (the same 0–7 order as DPX). +pub(crate) fn cineon_orientation(b: &[u8]) -> u16 { + let Some(code) = b.get(192).copied() else { return 1 }; + match code { + 1 => 2, + 2 => 4, + 3 => 3, + 4 => 5, + 5 => 6, + 6 => 8, + 7 => 7, + _ => 1, + } +} + +/// Decode a Cineon (`.cin`) frame: 10-bit logarithmic film scans, big-endian, one (gray) or +/// three (RGB) channels. The 10-bit code values become U16 as `round(c × 65535 / 1023)`; the +/// file's black/white reference points are not applied (a log conversion is a display choice). +pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result { + if !is_cineon(bytes) { + return Err(err("missing the Cineon magic number (802A5FD7)")); + } + let o = Order::Big; + let missing = |what: &str| err(format!("the header ends before {what}")); + let image_offset = usize::try_from(o.u32(bytes, 4).ok_or_else(|| missing("the image data offset"))?).map_err(|_| err("image data offset out of range"))?; + let channels = *bytes.get(193).ok_or_else(|| missing("the channel count"))?; + let bits = *bytes.get(198).ok_or_else(|| missing("the bits per pixel"))?; + if bits != 10 { + return Err(unsupported(format!("{bits}-bit samples (Cineon frames are 10-bit)"))); + } + let layout = match channels { + 1 => ChannelLayout::Gray, + 3 => ChannelLayout::Rgb, + n => return Err(unsupported(format!("{n} channels (Cineon frames have 1 or 3)"))), + }; + let width = o.u32(bytes, 200).ok_or_else(|| missing("the pixels per line"))?; + let height = o.u32(bytes, 204).ok_or_else(|| missing("the lines per image"))?; + limits.check(width, height, layout, SampleType::U16)?; + + let (w, h) = (width as usize, height as usize); + let per_line = w.checked_mul(layout.channels()).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + // Three 10-bit samples per 32-bit word, lines on a word boundary (DPX packing 1). + let line_len = line_bytes(per_line, 10, 1).ok_or_else(|| CodecError::LimitExceeded("line too long".into()))?; + let data = bytes.get(image_offset..).ok_or_else(|| err("the image data offset is past the end of the file"))?; + let need = line_len.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + if data.len() < need { + return Err(err("truncated image data")); + } + let mut codes = vec![0u16; per_line.checked_mul(h).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?]; + for (y, row) in codes.chunks_exact_mut(per_line).enumerate() { + let start = y * line_len; + let line = data.get(start..start + line_len).ok_or_else(|| err("truncated image data"))?; + unpack_line(line, o, 10, 1, row)?; + } + let wide: Vec = codes.iter().map(|&c| widen(c, 1023)).collect(); + Image::from_u16(width, height, layout, &wide) +} + +/// Cineon's generic plus film header: where our image data starts. +const CINEON_HEADER_LEN: usize = 2048; +const CINEON_GENERIC_LEN: u32 = 1024; +/// Offset of the first of the eight 28-byte channel descriptors. +const CINEON_CHANNEL0: usize = 196; + +/// Encode a Cineon 4.5 frame: the generic header, an undefined film header, then the samples as +/// 10-bit code values `round(v × 1023 / 65535)`, three to a big-endian word, left justified +/// (packing 5), lines on a word boundary. The plan has already made the image U16 gray or RGB. +pub(crate) fn encode_cineon(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(Format::Cineon, msg); + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + // Channel designators: 0 for black and white, 1–3 for red, green and blue. + let designators: &[u8] = match img.layout() { + ChannelLayout::Gray => &[0], + ChannelLayout::Rgb => &[1, 2, 3], + l => return Err(enc(format!("Cineon cannot store {l:?}"))), + }; + let samples = img.to_u16_samples().ok_or_else(|| enc(format!("cannot write {:?} samples at 10 bits", img.sample_type())))?; + let codes: Vec = samples.into_iter().map(|v| narrow(v, 1023)).collect(); + let per_line = (w as usize).checked_mul(designators.len()).ok_or_else(|| enc("image too large".into()))?; + if codes.len() != per_line.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? { + return Err(enc("the image has the wrong number of samples".into())); + } + let data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?; + let total = + CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; + + let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] }; + // File information. + wr.u32(0, 0x802A_5FD7); + wr.u32(4, CINEON_HEADER_LEN as u32); + wr.u32(8, CINEON_GENERIC_LEN); + wr.u32(12, CINEON_HEADER_LEN as u32 - CINEON_GENERIC_LEN); + wr.u32(16, 0); + wr.u32(20, total); + wr.string((24, 8), "V4.5"); + // Image information: default orientation, then each channel's descriptor; the unused ones, + // the white point and the primaries are undefined. + wr.u8(192, 0); + wr.u8(193, designators.len() as u8); + wr.undefined(CINEON_CHANNEL0, 420); + for (i, &designator) in designators.iter().enumerate() { + let c = CINEON_CHANNEL0 + 28 * i; + wr.u8(c, 0); + wr.u8(c + 1, designator); + wr.u8(c + 2, 10); + wr.u8(c + 3, 0); + wr.u32(c + 4, w); + wr.u32(c + 8, h); + // Code values 0–1023 stand for printing densities 0–2.048, the Cineon convention. All + // four are 32-bit floats here (DPX stores the data values as integers). + wr.u32(c + 12, 0f32.to_bits()); + wr.u32(c + 16, 0f32.to_bits()); + wr.u32(c + 20, 1023f32.to_bits()); + wr.u32(c + 24, 2.048f32.to_bits()); + } + // Pixel-interleaved, unsigned, positive, no line or channel padding. + wr.u8(681, 5); + // Image origination: pixel pitches and gamma unknown. + wr.undefined(972, 984); + // Film header: manufacturer, type, perforation offset, prefix, count, frame position and rate + // unknown; its strings stay empty. + wr.undefined(1024, 1036); + wr.undefined(1068, 1076); + + let mut out = wr.h; + out.try_reserve_exact(data_len).map_err(|_| enc("out of memory".into()))?; + for row in codes.chunks_exact(per_line.max(1)) { + pack_line(&mut out, row, 10); + } + Ok(out) +} + +/// The kept header fields as text metadata, in [`KEYS`] order; undefined ones are left out. +fn header_text(b: &[u8], o: Order, e: Element) -> Vec<(String, String)> { + let mut text = Vec::new(); + let mut put = |k: &str, v: Option| { + if let Some(v) = v { + text.push((k.to_string(), v)); + } + }; + put(KEY_CREATOR, string(b, CREATOR)); + put(KEY_PROJECT, string(b, PROJECT)); + put(KEY_COPYRIGHT, string(b, COPYRIGHT)); + put(KEY_CREATION_TIME, string(b, CREATION_TIME)); + put(KEY_TRANSFER, (e.transfer != UNDEF8).then(|| e.transfer.to_string())); + put(KEY_COLORIMETRIC, (e.colorimetric != UNDEF8).then(|| e.colorimetric.to_string())); + // The industry header is there when the generic header says so and the file holds it. + let industry = o.u32(b, 28).is_some_and(|l| l != 0 && l != UNDEF32) && b.len() >= HEADER_LEN; + if industry { + put(KEY_FRAME_POSITION, o.defined_u32(b, FRAME_POSITION).map(|v| v.to_string())); + put(KEY_FRAME_RATE, o.f32(b, FRAME_RATE).map(|v| v.to_string())); + put(KEY_TIME_CODE, o.defined_u32(b, TIME_CODE).map(format_time_code)); + } + text +} + +/// A SMPTE time code (BCD `HHMMSSFF`) as `HH:MM:SS:FF`, or `0x…` when it is not valid BCD. +fn format_time_code(v: u32) -> String { + let d: [u32; 8] = std::array::from_fn(|i| (v >> ((7 - i) * 4)) & 0xF); + if d.iter().all(|&d| d <= 9) { format!("{}{}:{}{}:{}{}:{}{}", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7]) } else { format!("{v:#010x}") } +} + +/// The inverse of [`format_time_code`]; `None` for anything else. +fn parse_time_code(s: &str) -> Option { + if let Some(hex) = s.strip_prefix("0x") { + return u32::from_str_radix(hex, 16).ok(); + } + let parts: Vec<&str> = s.split(':').collect(); + if parts.len() != 4 || parts.iter().any(|p| p.len() != 2 || !p.bytes().all(|c| c.is_ascii_digit())) { + return None; + } + parts.iter().flat_map(|p| p.bytes()).try_fold(0u32, |acc, c| Some((acc << 4) | u32::from(c - b'0'))) +} + +/// Bits written for `sample` when the options pick none: 8 for U8, 16 otherwise. +pub(crate) fn bits_for(opt: Option, sample: SampleType) -> u8 { + match opt { + Some(b) => b.bits(), + None if sample == SampleType::U8 => 8, + None => 16, + } +} + +/// The bits `format` stores below its U16 plan: DPX at 10 or 12 bits, Cineon's 10. `None` when +/// the stored depth is the plan's sample type. +pub(crate) fn packed_bits(format: Format, opts: &EncodeOptions) -> Option { + match format { + Format::Dpx => opts.dpx_bits.filter(|b| matches!(b, DpxBits::Bits10 | DpxBits::Bits12)).map(DpxBits::bits), + Format::Cineon => Some(10), + _ => None, + } +} + +/// `true` when every sample of a U16 image is already a `bits`-bit code value, so writing it at +/// `bits` and reading it back gives the same samples (a frame opened from such a file). +pub(crate) fn keeps_u16_samples(img: &Image, bits: u8) -> bool { + if img.sample_type() != SampleType::U16 || !(1..16).contains(&bits) { + return false; + } + let max = (1u64 << bits) - 1; + img.data().as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).all(|v| widen(narrow(v, max), max) == v) +} + +/// The plan's sample type for the chosen bits: U8 for 8 bits, U16 for 10, 12 and 16. +pub(crate) fn plan_sample(opt: Option, src: SampleType) -> SampleType { + match opt { + Some(DpxBits::Bits8) => SampleType::U8, + Some(_) => SampleType::U16, + None if src == SampleType::U8 => SampleType::U8, + None => SampleType::U16, + } +} + +struct Writer { + h: Vec, +} + +impl Writer { + fn u8(&mut self, at: usize, v: u8) { + if let Some(b) = self.h.get_mut(at) { + *b = v; + } + } + + fn u16(&mut self, at: usize, v: u16) { + if let Some(s) = self.h.get_mut(at..at + 2) { + s.copy_from_slice(&v.to_be_bytes()); + } + } + + fn u32(&mut self, at: usize, v: u32) { + if let Some(s) = self.h.get_mut(at..at + 4) { + s.copy_from_slice(&v.to_be_bytes()); + } + } + + /// Marks a run of numeric fields undefined (all ones). + fn undefined(&mut self, from: usize, to: usize) { + if let Some(s) = self.h.get_mut(from..to) { + s.fill(0xFF); + } + } + + /// An ASCII string field, cut to leave room for a terminating NUL. + fn string(&mut self, (at, len): (usize, usize), v: &str) { + let ascii: Vec = v.chars().map(|c| if (' '..='~').contains(&c) { c as u8 } else { b'?' }).take(len.saturating_sub(1)).collect(); + if let Some(s) = self.h.get_mut(at..at + ascii.len()) { + s.copy_from_slice(&ascii); + } + } +} + +fn text<'a>(img: &'a Image, key: &str) -> Option<&'a str> { + img.meta.text.iter().find(|(k, _)| k == key).map(|(_, v)| v.trim()).filter(|v| !v.is_empty()) +} + +/// Appends one line's code values, padded to a 32-bit word. +fn pack_line(out: &mut Vec, codes: &[u16], bits: u8) { + let start = out.len(); + match bits { + 8 => out.extend(codes.iter().map(|&c| c as u8)), + 10 => { + for chunk in codes.chunks(3) { + let word = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c & 0x3FF) << (22 - 10 * i as u32))); + out.extend_from_slice(&word.to_be_bytes()); + } + } + 12 => out.extend(codes.iter().flat_map(|&c| ((c & 0xFFF) << 4).to_be_bytes())), + _ => out.extend(codes.iter().flat_map(|&c| c.to_be_bytes())), + } + let written = out.len() - start; + out.resize(start + written.div_ceil(4) * 4, 0); +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let (descriptor, layout) = match img.layout() { + ChannelLayout::Gray => (6u8, ChannelLayout::Gray), + ChannelLayout::Rgb => (50, ChannelLayout::Rgb), + ChannelLayout::Rgba => (51, ChannelLayout::Rgba), + l => return Err(CodecError::encode(F, format!("DPX cannot store {l:?}"))), + }; + let bits = bits_for(opts.dpx_bits, img.sample_type()); + let codes: Vec = match (img.sample_type(), bits) { + (SampleType::U8, 8) => img.data().iter().map(|&v| u16::from(v)).collect(), + (SampleType::U16, 16) => img.to_u16_samples().unwrap_or_default(), + (SampleType::U16, 10 | 12) => { + let max = (1u64 << bits) - 1; + img.to_u16_samples().unwrap_or_default().into_iter().map(|v| narrow(v, max)).collect() + } + (s, b) => return Err(CodecError::encode(F, format!("cannot write {s:?} samples at {b} bits"))), + }; + let per_line = w as usize * layout.channels(); + if codes.len() != per_line * h as usize { + return Err(CodecError::encode(F, "the image has the wrong number of samples")); + } + let data_len = line_bytes(per_line, bits, if matches!(bits, 10 | 12) { 1 } else { 0 }) + .and_then(|l| l.checked_mul(h as usize)) + .ok_or_else(|| CodecError::encode(F, "image too large"))?; + let total = u32::try_from(HEADER_LEN + data_len).map_err(|_| CodecError::encode(F, "image too large for a DPX file (4 GiB)"))?; + + let mut wr = Writer { h: vec![0; HEADER_LEN] }; + // Generic file header. + wr.h[..4].copy_from_slice(b"SDPX"); + wr.u32(4, HEADER_LEN as u32); + wr.string((8, 8), "V2.0"); + wr.u32(16, total); + wr.u32(20, 1); + wr.u32(24, GENERIC_LEN); + wr.u32(28, INDUSTRY_LEN); + wr.u32(32, 0); + let keep = |key| if opts.embed_metadata { text(&img, key) } else { None }; + wr.string(CREATION_TIME, keep(KEY_CREATION_TIME).unwrap_or("")); + wr.string(CREATOR, keep(KEY_CREATOR).unwrap_or("PhotoCraft")); + wr.string(PROJECT, keep(KEY_PROJECT).unwrap_or("")); + wr.string(COPYRIGHT, keep(KEY_COPYRIGHT).unwrap_or("")); + wr.u32(660, UNDEF32); + // Image information: default orientation, one element. + wr.u16(ORIENTATION, 0); + wr.u16(ORIENTATION + 2, 1); + wr.u32(772, w); + wr.u32(776, h); + let e = ELEMENT0; + wr.u32(e, 0); + wr.u32(e + 4, 0); + wr.u32(e + 8, UNDEF32); + wr.u32(e + 12, ((1u32 << bits) - 1).min(65535)); + wr.u32(e + 16, UNDEF32); + wr.u8(e + 20, descriptor); + // No claim about the encoding unless the document came with one. + let code = |key| keep(key).and_then(|v| v.parse::().ok()).unwrap_or(UNDEF8); + wr.u8(e + 21, code(KEY_TRANSFER)); + wr.u8(e + 22, code(KEY_COLORIMETRIC)); + wr.u8(e + 23, bits); + wr.u16(e + 24, if matches!(bits, 10 | 12) { 1 } else { 0 }); + wr.u16(e + 26, 0); + wr.u32(e + 28, HEADER_LEN as u32); + wr.u32(e + 32, 0); + wr.u32(e + 36, 0); + // The seven unused elements, the image source header's numbers and the industry header's + // numbers are undefined; their strings stay empty. + wr.undefined(e + ELEMENT_LEN, e + ELEMENT_LEN * usize::from(MAX_ELEMENTS)); + wr.undefined(1408, 1432); + wr.undefined(1620, 1644); + wr.undefined(FRAME_POSITION, 1732); + wr.undefined(TIME_CODE, 1972); + if let Some(v) = keep(KEY_FRAME_POSITION).and_then(|v| v.parse::().ok()) { + wr.u32(FRAME_POSITION, v); + } + if let Some(v) = keep(KEY_FRAME_RATE).and_then(|v| v.parse::().ok()).filter(|v| v.is_finite()) { + wr.u32(FRAME_RATE, v.to_bits()); + } + if let Some(v) = keep(KEY_TIME_CODE).and_then(parse_time_code) { + wr.u32(TIME_CODE, v); + } + + let mut out = wr.h; + out.try_reserve_exact(data_len).map_err(|_| CodecError::encode(F, "out of memory"))?; + for row in codes.chunks_exact(per_line.max(1)) { + pack_line(&mut out, row, bits); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A minimal header for `w`×`h` with element 0 set; image data at 2048. + fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec { + let mut b = vec![0u8; HEADER_LEN]; + let put32 = |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put16 = |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" }); + put32(&mut b, 4, HEADER_LEN as u32); + put32(&mut b, 28, INDUSTRY_LEN); + put16(&mut b, ORIENTATION, 0); + put16(&mut b, ORIENTATION + 2, 1); + put32(&mut b, 772, w); + put32(&mut b, 776, h); + b[ELEMENT0 + 20] = descriptor; + b[ELEMENT0 + 21] = UNDEF8; + b[ELEMENT0 + 22] = UNDEF8; + b[ELEMENT0 + 23] = bits; + put16(&mut b, ELEMENT0 + 24, packing); + put32(&mut b, ELEMENT0 + 28, HEADER_LEN as u32); + b[FRAME_POSITION..1732].fill(0xFF); + b[TIME_CODE..1972].fill(0xFF); + b + } + + fn words(o: Order, ws: &[u32]) -> Vec { + ws.iter().flat_map(|w| if o == Order::Big { w.to_be_bytes() } else { w.to_le_bytes() }).collect() + } + + #[test] + fn ten_bit_method_a_and_b_unpack_three_to_a_word() { + for o in [Order::Big, Order::Little] { + for (packing, base) in [(1u16, 22u32), (2, 20)] { + let mut b = header(o, 1, 1, 50, 10, packing); + b.extend(words(o, &[(1023 << base) | (512 << (base - 10)) | (1 << (base - 20))])); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(512, 1023), widen(1, 1023)], "{o:?} packing {packing}"); + } + } + } + + #[test] + fn ten_bit_packing_0_is_one_bit_stream_per_line() { + // Four luma samples: 40 bits over two words, the line padded to the second word. + let samples = [1u64, 2, 1022, 1023]; + let stream = samples.iter().fold(0u64, |acc, &s| (acc << 10) | s) << 24; + for o in [Order::Big, Order::Little] { + let mut b = header(o, 4, 2, 6, 10, 0); + let line = words(o, &[(stream >> 32) as u32, stream as u32]); + b.extend(&line); + b.extend(&line); + let img = decode(&b, &Limits::default()).unwrap(); + let want: Vec = samples.iter().map(|&s| widen(s as u16, 1023)).collect(); + assert_eq!(img.to_u16_samples().unwrap(), [want.clone(), want].concat(), "{o:?}"); + } + } + + #[test] + fn twelve_bit_method_a_keeps_the_top_bits() { + let mut b = header(Order::Big, 2, 1, 6, 12, 1); + b.extend_from_slice(&[0xFF, 0xF0, 0x00, 0x10]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_u16_samples().unwrap(), vec![65535, widen(1, 4095)]); + } + + #[test] + fn widen_and_narrow_round_trip_every_code() { + for bits in [10u8, 12] { + let max = (1u64 << bits) - 1; + for c in 0..=max as u16 { + assert_eq!(narrow(widen(c, max), max), c, "{bits}-bit {c}"); + } + } + assert_eq!(widen(512, 1023), ((512.0f64 * 65535.0 / 1023.0).round()) as u16); + } + + #[test] + fn orientation_codes_map_to_exif() { + let mut b = header(Order::Big, 1, 1, 6, 8, 0); + for (dpx, exif) in [(0u16, 1u16), (1, 2), (2, 4), (3, 3), (4, 5), (5, 6), (6, 8), (7, 7), (0xFFFF, 1)] { + b[ORIENTATION..ORIENTATION + 2].copy_from_slice(&dpx.to_be_bytes()); + assert_eq!(orientation(&b), exif, "DPX {dpx}"); + } + } + + #[test] + fn time_codes_round_trip() { + assert_eq!(format_time_code(0x0102_0304), "01:02:03:04"); + assert_eq!(parse_time_code("01:02:03:04"), Some(0x0102_0304)); + assert_eq!(parse_time_code(&format_time_code(0xABCD_0000)), Some(0xABCD_0000)); + assert_eq!(parse_time_code("1:2:3:4"), None); + } + + #[test] + fn eight_bit_lines_without_word_padding_still_open() { + // 1 × 2 RGB: 3 bytes a line, 6 in all; padded lines would need 8. + let mut b = header(Order::Big, 1, 2, 50, 8, 0); + b.extend_from_slice(&[1, 2, 3, 4, 5, 6]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[1, 2, 3, 4, 5, 6]); + } + + /// A minimal Cineon: a 1024-byte generic header, then `w × h` 10-bit samples packed three + /// to a 32-bit word (RGB in word order, or gray three pixels per word). + fn cineon(w: u32, h: u32, channels: u8, px: &[[u16; 3]]) -> Vec { + let mut b = vec![0u8; 1024]; + b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); // magic (802A5FD7) + b[4..8].copy_from_slice(&1024u32.to_be_bytes()); // image data offset + b[192] = 0; // orientation: left-to-right, top-to-bottom + b[193] = channels; + b[198] = 10; // bits per pixel (channel 1) + b[200..204].copy_from_slice(&w.to_be_bytes()); // pixels per line + b[204..208].copy_from_slice(&h.to_be_bytes()); // lines per image + let word = |a: u16, c: u16, e: u16| ((u32::from(a) << 22) | (u32::from(c) << 12) | (u32::from(e) << 2)).to_be_bytes(); + if channels == 3 { + for p in px { + b.extend_from_slice(&word(p[0], p[1], p[2])); + } + } else { + let g = |i: usize| px.get(i).map_or(0, |p| p[0]); + for i in (0..px.len()).step_by(3) { + b.extend_from_slice(&word(g(i), g(i + 1), g(i + 2))); + } + } + b + } + + #[test] + fn cineon_reads_ten_bit_rgb() { + let px = [[0, 512, 1023], [1023, 0, 256]]; + let b = cineon(2, 1, 3, &px); + assert!(is_cineon(&b)); + assert_eq!(order(&b), None, "Cineon is not DPX"); + let img = decode_cineon(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!( + img.to_u16_samples().unwrap(), + vec![widen(0, 1023), widen(512, 1023), widen(1023, 1023), widen(1023, 1023), widen(0, 1023), widen(256, 1023)] + ); + } + + #[test] + fn cineon_reads_grayscale_and_rejects_other_depths() { + let px = [[10, 0, 0], [20, 0, 0], [30, 0, 0], [40, 0, 0]]; + let b = cineon(4, 1, 1, &px); + let img = decode_cineon(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.to_u16_samples().unwrap(), vec![widen(10, 1023), widen(20, 1023), widen(30, 1023), widen(40, 1023)]); + // A depth other than 10 is unsupported, and truncation errors rather than panicking. + let mut bad = cineon(1, 1, 3, &[[0, 0, 0]]); + bad[198] = 8; + assert!(matches!(decode_cineon(&bad, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let full = cineon(4, 4, 3, &[[0; 3]; 16]); + for cut in [0, 4, 200, 1024, full.len() - 1] { + assert!(decode_cineon(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}"); + } + } +} diff --git a/crates/image-codecs/src/codecs/exr.rs b/crates/image-codecs/src/codecs/exr.rs new file mode 100644 index 000000000..0d68d20ca --- /dev/null +++ b/crates/image-codecs/src/codecs/exr.rs @@ -0,0 +1,441 @@ +//! OpenEXR via the pure-Rust `exr` crate: F16/F32, Y/YA/RGB/RGBA channels, +//! lossless compression. Multi-part files (a Maya/Arnold render writes one part +//! per AOV) open the part that looks most like a colour image, with a warning +//! naming how many parts exist; [`info`] lists every part and [`decode_part`] +//! decodes a chosen one. Pixel data is taken from the data window. + +use std::io::Cursor; + +use exr::meta::MetaData; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, read, +}; +use half::f16; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, ExrChannelInfo, ExrPartInfo, Image, SampleType, base_channel_name, exr_color_channels}; +use crate::options::{EncodeOptions, ExrCompression, Limits}; + +const F: Format = Format::OpenExr; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +/// An `exr`-crate error, with its `NotSupported` kind kept distinct from malformed data +/// (the same split the TIFF codec makes), so unsupported features say so. +fn map_exr(e: exr::error::Error) -> CodecError { + match e { + exr::error::Error::NotSupported(message) => CodecError::unsupported(F, format!("not supported: {message}")), + e => err(e), + } +} + +/// Refuses headers whose `preview` attributes declare more pixels than their value holds. +/// +/// The `exr` crate sizes a preview's pixel buffer from its declared dimensions (up to 4 MiB at +/// once, zero-filled) before it finds the attribute too short, and in the non-pedantic mode +/// every reader here uses it then skips the attribute and goes on: thousands of tiny malformed +/// previews would each cost that allocation. This walks the raw headers (name, type, size, +/// value per attribute; an empty name ends a header, an empty header ends a multi-part list) +/// and checks each preview against its own size, so the crate only reads consistent ones. +/// Anything else odd is left to the crate to report. +pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> { + if b.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01][..]) { + return Ok(()); + } + let Some(flags) = b.get(4..8).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes) else { return Ok(()) }; + let multipart = flags & 0x1000 != 0; + let cstr = |at: usize| -> Option<(&[u8], usize)> { + let rest = b.get(at..)?; + let n = rest.iter().position(|&c| c == 0)?; + Some((rest.get(..n)?, at + n + 1)) + }; + let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|v| <[u8; 4]>::try_from(v).ok()).map(u32::from_le_bytes); + let mut at = 8; + loop { + // One header's attributes. + loop { + let Some((name, next)) = cstr(at) else { return Ok(()) }; + at = next; + if name.is_empty() { + break; + } + let Some((kind, next)) = cstr(at) else { return Ok(()) }; + let Some(size) = u32_at(next).and_then(|n| i32::try_from(n).ok()).and_then(|n| usize::try_from(n).ok()) else { return Ok(()) }; + let value = next + 4; + let end = match value.checked_add(size) { + Some(end) if end <= b.len() => end, + _ => return Ok(()), + }; + if kind == b"preview" { + let declared = + u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8); + if declared.is_none_or(|n| n > (size - 8) as u64) { + return Err(err("a preview attribute declares more pixels than it holds")); + } + } + at = end; + } + // A multi-part list ends with an empty header (a lone NUL). + if !multipart || b.get(at) == Some(&0) { + return Ok(()); + } + } +} + +/// Read the headers without the `exr` crate's validation (it rejects deep parts outright), +/// after checking every part's declared size against the limits, before any pixel allocation. +pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result { + check_previews(bytes)?; + let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(err)?; + // Flat decoding reads every part at once, so the parts' sizes are also capped together. + let mut total: u64 = 0; + // DWAA/DWAB parts are counted apart: they go past 2^16:1 (a flat 8192x4320 frame with 12 + // alpha-scheme mattes is about 30 KB, over 75000:1). + let mut dwa: u64 = 0; + for header in meta.headers.iter() { + let size = header.layer_size; + let (w, h) = (u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX)); + let bpp = header.channels.list.len().max(1) as u64 * 4; + limits.check_bytes(w, h, bpp)?; + let bytes = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + total = total.saturating_add(bytes); + if matches!(header.compression, Compression::DWAA(_) | Compression::DWAB(_)) { + dwa = dwa.saturating_add(bytes); + } + } + if total > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{total} bytes across {} parts exceed max_alloc {}", meta.headers.len(), limits.max_alloc))); + } + // The `exr` crate allocates a flat part's samples before reading its chunks, so the file + // must be able to hold them (#8597). The other compressions stay far below 2^16:1 even + // against this f32 budget. DWA's run-length channels are zlib-packed on top and reach about + // 132000:1, so DWA parts get 2^18:1. Deep parts size their samples from the file's own counts. + if !meta.requirements.has_deep_data { + Limits::check_plausible(total.saturating_sub(dwa).saturating_add(dwa / 4), bytes.len(), 1 << 16)?; + } + Ok(meta) +} + +/// Header facts of one part, in file order. +fn part_info(index: usize, header: &exr::meta::header::Header) -> ExrPartInfo { + let (w, h) = (u32::try_from(header.layer_size.0).unwrap_or(u32::MAX), u32::try_from(header.layer_size.1).unwrap_or(u32::MAX)); + let sample = |t: exr::meta::attribute::SampleType| match t { + exr::meta::attribute::SampleType::F16 => SampleType::F16, + // U32 channels (Arnold ID buffers) are listed as F32: exact up to 2^24. + exr::meta::attribute::SampleType::F32 | exr::meta::attribute::SampleType::U32 => SampleType::F32, + }; + let display = &header.shared_attributes.display_window; + ExrPartInfo { + index, + name: header.own_attributes.layer_name.as_ref().map(|n| n.to_string()), + view: header.own_attributes.view_name.as_ref().map(|v| v.to_string()), + width: w, + height: h, + data_window: (header.own_attributes.layer_position.0, header.own_attributes.layer_position.1, w, h), + display_window: ( + display.position.0, + display.position.1, + u32::try_from(display.size.0).unwrap_or(u32::MAX), + u32::try_from(display.size.1).unwrap_or(u32::MAX), + ), + deep: header.deep, + tiled: matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)), + channels: header.channels.list.iter().map(|c| ExrChannelInfo { name: c.name.to_string(), sample: sample(c.sample_type) }).collect(), + } +} + +/// Lists every part of the file (header facts only, no pixel data): what a multi-part +/// chooser offers, and what [`decode_part`] takes an index into. +pub(crate) fn info(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + let meta = read_meta(bytes, limits)?; + Ok(meta.headers.iter().enumerate().map(|(i, h)| part_info(i, h)).collect()) +} + +/// The part plain [`decode`] opens: the highest [`ExrPartInfo::color_rank`], earliest wins. +fn pick_part(meta: &MetaData) -> usize { + let mut best = 0; + let mut best_rank = meta.headers.first().map_or(0, |h| part_info(0, h).color_rank()); + for (i, header) in meta.headers.iter().enumerate().skip(1) { + let rank = part_info(i, header).color_rank(); + if rank > best_rank { + best = i; + best_rank = rank; + } + } + best +} + +/// Selects one part for the `exr` crate's first-valid-layer reader: `create_channels_reader` +/// is asked about the headers in file order and the first `Ok` wins, so every header before +/// `index` (and, harmlessly, those after it) is declined. Only the chosen part's chunks are +/// then decompressed and only its planes allocated; the other parts are skipped unread. +struct OnlyPart { + inner: C, + index: usize, + seen: std::cell::Cell, +} + +impl<'s, C: ReadChannels<'s>> ReadChannels<'s> for OnlyPart { + type Reader = C::Reader; + + fn create_channels_reader(&'s self, header: &exr::meta::header::Header) -> exr::error::Result { + let position = self.seen.get(); + self.seen.set(position.saturating_add(1)); + if position == self.index { self.inner.create_channels_reader(header) } else { Err(exr::error::Error::Invalid("part not requested".into())) } + } +} + +/// Decode one part's flat layer via the `exr` crate (it handles every flat compression). +/// Other parts are not decompressed, so an AOV file costs one part per request, not the file. +pub(super) fn read_layer(bytes: &[u8], index: usize) -> Result>, CodecError> { + check_previews(bytes)?; + let only = OnlyPart { inner: read().no_deep_data().largest_resolution_level().all_channels(), index, seen: std::cell::Cell::new(0) }; + only.first_valid_layer().all_attributes().from_buffered(Cursor::new(bytes)).map(|image| image.layer_data).map_err(map_exr) +} + +fn layer_to_image(layer: &Layer>) -> Result { + let (w, h) = (layer.size.0, layer.size.1); + let channels = &layer.channel_data.list; + let names: Vec<_> = channels.iter().map(|c| c.name.to_string()).collect(); + let (layout, selected) = exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; + let color: Vec<_> = selected.iter().map(|i| channels.get(*i).ok_or_else(|| err("selected channel is missing"))).collect::>()?; + let all_f16 = color.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); + let sample = if all_f16 { SampleType::F16 } else { SampleType::F32 }; + let npx = w * h; + if color.iter().any(|c| c.sample_data.len() != npx) { + return Err(err("channel sample count mismatch (subsampled channels are unsupported)")); + } + let nc = color.len(); + let (w32, h32) = (w as u32, h as u32); + let img = if all_f16 { + let mut out = vec![f16::ZERO; npx * nc]; + for (ci, c) in color.iter().enumerate() { + if let FlatSamples::F16(v) = &c.sample_data { + for (i, s) in v.iter().enumerate() { + out[i * nc + ci] = *s; + } + } + } + Image::from_f16(w32, h32, layout, &out)? + } else { + let mut out = vec![0f32; npx * nc]; + for (ci, c) in color.iter().enumerate() { + for (i, s) in c.sample_data.values_as_f32().enumerate() { + out[i * nc + ci] = s; + } + } + Image::from_f32(w32, h32, layout, &out)? + }; + debug_assert_eq!(img.sample_type(), sample); + Ok(img) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + + // Deep parts are rejected by the `exr` crate's reader; walk their chunks ourselves and + // composite the samples into the flat image (a warning says what was lost). + if meta.requirements.has_deep_data { + let deep = super::deep_exr::decode_deep(&meta, bytes, limits)?; + return super::deep_exr::flatten(&deep, limits); + } + + let pick = pick_part(&meta); + let mut img = layer_to_image(&read_layer(bytes, pick)?)?; + if meta.headers.len() > 1 { + let total = u32::try_from(meta.headers.len()).ok(); + img.warnings.push(DecodeWarning::MoreParts { total }); + } + Ok(img) +} + +/// The size of the part [`decode`] opens, from the headers alone. Deep files are refused (their +/// flattened size comes from the deep decoder), as is a part [`decode`] cannot show. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32), CodecError> { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "no header-only size probe for deep data")); + } + let pick = pick_part(&meta); + let info = meta.headers.get(pick).map(|h| part_info(pick, h)).ok_or_else(|| err("no parts"))?; + if info.color_rank() == 0 { + return Err(CodecError::unsupported(F, "no R/G/B or Y channels")); + } + Ok((info.width, info.height)) +} + +/// Decode one part of a multi-part file by its index into [`info`]'s list. A file with +/// deep parts is not mixed: the deep decoder composites those instead. +pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + } + if index >= meta.headers.len() { + return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); + } + layer_to_image(&read_layer(bytes, index)?) +} + +/// Decodes the named channels of one flat part (exact names as the file stores them, e.g. +/// `diffuse.R`) into an image: R/G/B (plus A when named) give Rgba, a lone Y gives Gray. This +/// is how a channel group renders without the part's other channels in the way. +pub(crate) fn decode_channels(bytes: &[u8], index: usize, names: &[&str], limits: &Limits) -> Result { + let meta = read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + } + if index >= meta.headers.len() { + return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); + } + let layer = read_layer(bytes, index)?; + let (w, h) = (layer.size.0, layer.size.1); + let channels = &layer.channel_data.list; + let npx = w * h; + let mut picked: Vec<&AnyChannel> = Vec::with_capacity(names.len()); + for name in names { + let c = channels.iter().find(|c| c.name.to_string() == *name).ok_or_else(|| err(format!("the part has no channel {name}")))?; + if c.sample_data.len() != npx { + return Err(err("channel sample count mismatch (subsampled channels are unsupported)")); + } + picked.push(c); + } + let base = |c: &AnyChannel| -> String { base_channel_name(&c.name.to_string()).to_string() }; + let has = |n: &str| picked.iter().any(|c| base(c) == n); + let (layout, order): (ChannelLayout, Vec>) = if has("R") && has("G") && has("B") { + let at = |n: &str| picked.iter().position(|c| base(c) == n); + match has("A").then(|| at("A")) { + Some(a) => (ChannelLayout::Rgba, vec![at("R"), at("G"), at("B"), a]), + None => (ChannelLayout::Rgb, vec![at("R"), at("G"), at("B"), None]), + } + } else if has("Y") { + let y = picked.iter().position(|c| base(c) == "Y"); + match picked.iter().position(|c| base(c) == "A") { + Some(a) => (ChannelLayout::GrayA, vec![y, Some(a)]), + None => (ChannelLayout::Gray, vec![y]), + } + } else if picked.len() == 1 { + (ChannelLayout::Gray, vec![Some(0)]) + } else { + return Err(CodecError::unsupported(F, "the named channels are no R/G/B or Y set")); + }; + let all_f16 = picked.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); + let nc = layout.channels(); + let (w32, h32) = (w as u32, h as u32); + // The None slots sit past the layout's own channels (the absent A of an RGB pick) and + // are skipped, so the buffer keeps its pixel stride. + let img = if all_f16 { + let mut out = vec![f16::ZERO; npx * nc]; + for (ci, pick) in order.iter().enumerate() { + let Some(idx) = pick else { continue }; + let Some(FlatSamples::F16(v)) = picked.get(*idx).map(|c| &c.sample_data) else { continue }; + for (i, s) in v.iter().enumerate() { + out[i * nc + ci] = *s; + } + } + Image::from_f16(w32, h32, layout, &out) + } else { + let mut out = vec![0f32; npx * nc]; + for (ci, pick) in order.iter().enumerate() { + let Some(idx) = pick else { continue }; + let Some(c) = picked.get(*idx) else { continue }; + for (i, s) in c.sample_data.values_as_f32().enumerate() { + out[i * nc + ci] = s; + } + } + Image::from_f32(w32, h32, layout, &out) + }; + img.map_err(|e| CodecError::encode(F, e)) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = (img.width() as usize, img.height() as usize); + let layout = img.layout(); + let names: &[&str] = match layout { + ChannelLayout::Gray => &["Y"], + ChannelLayout::GrayA => &["Y", "A"], + ChannelLayout::Rgb => &["R", "G", "B"], + ChannelLayout::Rgba => &["R", "G", "B", "A"], + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let nc = names.len(); + let mut list: Vec> = Vec::with_capacity(nc); + match img.sample_type() { + SampleType::F16 => { + let s = img.to_f16_samples().unwrap_or_default(); + for (ci, n) in names.iter().enumerate() { + let v: Vec = s.iter().skip(ci).step_by(nc).copied().collect(); + list.push(AnyChannel::new(*n, FlatSamples::F16(v))); + } + } + SampleType::F32 => { + let s = img.to_f32_samples().unwrap_or_default(); + for (ci, n) in names.iter().enumerate() { + let v: Vec = s.iter().skip(ci).step_by(nc).copied().collect(); + list.push(AnyChannel::new(*n, FlatSamples::F32(v))); + } + } + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + } + let compression = match opts.exr_compression { + ExrCompression::None => Compression::Uncompressed, + ExrCompression::Rle => Compression::RLE, + ExrCompression::Zip1 => Compression::ZIP1, + ExrCompression::Zip16 => Compression::ZIP16, + ExrCompression::Piz => Compression::PIZ, + }; + let encoding = Encoding { compression, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }; + let channels = AnyChannels::sort(list.into_iter().collect()); + let layer = Layer::new((w, h), LayerAttributes::default(), encoding, channels); + let image = exr::image::Image::from_layer(layer); + let mut cursor = Cursor::new(Vec::new()); + image.write().to_buffered(&mut cursor).map_err(|e| CodecError::encode(F, e))?; + Ok(cursor.into_inner()) +} + +#[cfg(test)] +mod preview_tests { + use super::*; + use exr::meta::attribute::Preview; + use exr::prelude::{ImageAttributes, IntegerBounds}; + + /// A 2x2 single-part file whose layer carries a 2x1 preview. + fn with_preview() -> Vec { + let mut attrs = LayerAttributes::named("rgb"); + attrs.preview = Some(Preview { size: exr::math::Vec2(2, 1), pixel_data: vec![7; 8] }); + let list = ["R", "G", "B"].into_iter().map(|n| AnyChannel::new(n, FlatSamples::F32(vec![0.5; 4]))).collect(); + let layer = Layer::new( + (2, 2), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (2, 2))), vec![layer]); + let mut out = Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).unwrap(); + out.into_inner() + } + + #[test] + fn a_preview_declaring_more_pixels_than_it_holds_is_refused_before_the_crate_reads_it() { + let good = with_preview(); + check_previews(&good).unwrap(); + assert_eq!(decode(&good, &Limits::default()).unwrap().dimensions(), (2, 2)); + // Patch the preview to 1024x1024 (4 MiB) while its value still holds 8 bytes. + let tag = b"preview\0preview\0"; + let at = good.windows(tag.len()).position(|w| w == tag).unwrap() + tag.len() + 4; + let mut bad = good.clone(); + bad[at..at + 4].copy_from_slice(&1024u32.to_le_bytes()); + bad[at + 4..at + 8].copy_from_slice(&1024u32.to_le_bytes()); + for r in [check_previews(&bad), decode(&bad, &Limits::default()).map(|_| ()), read_layer(&bad, 0).map(|_| ())] { + assert!(matches!(&r, Err(CodecError::Malformed { message, .. }) if message.contains("preview")), "{r:?}"); + } + // A header cut off mid-attribute is left to the crate. + check_previews(&good[..at]).unwrap(); + } +} diff --git a/crates/image-codecs/src/codecs/exr_cryptomatte.rs b/crates/image-codecs/src/codecs/exr_cryptomatte.rs new file mode 100644 index 000000000..62fd2e1e0 --- /dev/null +++ b/crates/image-codecs/src/codecs/exr_cryptomatte.rs @@ -0,0 +1,266 @@ +//! Cryptomatte (specification 1.2): per-pixel object/material IDs with coverage in EXR +//! channels, as written by Arnold, V-Ray, Redshift, Mantra/Karma and Cycles. Layers are +//! announced by `cryptomatte//name|hash|conversion|manifest` header attributes; the +//! samples live in `NN` RGBA channel sets, each holding two (ID, coverage) ranks: +//! `.red`/`.green` (rank 2·NN) and `.blue`/`.alpha` (rank 2·NN+1). +//! +//! IDs are the float *values*: MurmurHash3 x86-32 (seed 0) of the object name, its +//! exponent fixed away from 0/255 so no ID is denormal, infinite or NaN, read as an f32 +//! bit pattern. Everything here compares IDs by value, never by bit pattern. + +use std::io::Cursor; + +use exr::meta::MetaData; +use exr::meta::attribute::AttributeValue; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{CryptomatteBuffer, CryptomatteLayer}; +use crate::options::Limits; + +const F: Format = Format::OpenExr; + +/// A Cryptomatte stream channel (`crypto_asset00.red`) splits into its stream name +/// (`crypto_asset00`), the stream index, whether it is a coverage channel (green/alpha) +/// instead of an ID (red/blue), and its rank within the set (0 for red/green, 1 for +/// blue/alpha). +pub(crate) struct StreamChannel { + pub stream: String, + pub index: u32, + pub coverage: bool, + pub rank: usize, +} + +/// `NN` where NN is one or more digits (the reference readers match `\d+`), with a +/// `.red`/`.r` or `.blue`/`.b` (ID) or `.green`/`.g` or `.alpha`/`.a` (coverage) suffix, +/// in any case (Blender writes `.R`/`.G`/`.B`/`.A`). +pub(crate) fn split_stream(channel: &str) -> Option { + let (stream, suffix) = channel.rsplit_once('.')?; + let (coverage, rank) = match suffix.to_ascii_lowercase().as_str() { + "red" | "r" => (false, 0), + "green" | "g" => (true, 0), + "blue" | "b" => (false, 1), + "alpha" | "a" => (true, 1), + _ => return None, + }; + let digits = stream.chars().rev().take_while(|c| c.is_ascii_digit()).count(); + if digits == 0 || digits == stream.len() { + return None; + } + let split = stream.len() - digits; + let index = stream[split..].parse().ok()?; + Some(StreamChannel { stream: stream.to_string(), index, coverage, rank }) +} + +/// MurmurHash3, x86-32 bit variant, seed 0 (public-domain algorithm, Austin Appleby). +fn murmur3_32(data: &[u8]) -> u32 { + const C1: u32 = 0xcc9e_2d51; + const C2: u32 = 0x1b87_3593; + let mut h = 0u32; + let (chunks, rest) = data.as_chunks::<4>(); + for c in chunks { + let mut k = u32::from_le_bytes(*c); + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64); + } + if !rest.is_empty() { + let mut k = [0u8; 4]; + k[..rest.len()].copy_from_slice(rest); + let mut k = u32::from_le_bytes(k); + k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); + h ^= k; + } + h ^= data.len() as u32; + h ^= h >> 16; + h = h.wrapping_mul(0x85eb_ca6b); + h ^= h >> 13; + h = h.wrapping_mul(0xc2b2_ae35); + h ^ (h >> 16) +} + +/// The Cryptomatte ID of a name: the MurmurHash3 bits with the exponent fixed away from +/// 0 and 255, as an f32 value. +pub fn cryptomatte_id(name: &str) -> f32 { + let bits = murmur3_32(name.as_bytes()); + let exp = (bits >> 23) & 0xff; + let bits = if exp == 0 || exp == 255 { bits ^ (1 << 23) } else { bits }; + f32::from_bits(bits) +} + +/// The 7-hex-digit metadata key of a layer name (the ID bits without their last digit). +pub fn cryptomatte_key(name: &str) -> String { + format!("{:08x}", cryptomatte_id(name).to_bits())[..7].to_string() +} + +/// The preview colour of an ID (the reference formula): green and blue channels shifted +/// out of the ID's bit pattern, red unused. +pub fn cryptomatte_preview_color(id: f32) -> [f32; 3] { + let bits = id.to_bits(); + [0.0, f32::from_bits(bits << 8), f32::from_bits(bits << 16)] +} + +/// The `cryptomatte//` text attribute of a header, if present. +fn attribute(other: &std::collections::HashMap, key: &str, field: &str) -> Option { + let full = exr::meta::attribute::Text::new_or_none(format!("cryptomatte/{key}/{field}").as_str())?; + match other.get(&full)? { + AttributeValue::Text(t) => Some(t.to_string()), + _ => None, + } +} + +/// Parse a manifest JSON (`{"object name": "<8-hex ID bits>"}`) into (ID value, name) +/// pairs. Entries that are not strings of up to eight hex digits are skipped, like the +/// reference readers tolerate a partially broken manifest; a broken document yields none. +fn parse_manifest(json: &str) -> Vec<(f32, String)> { + let Ok(value) = serde_json::from_str::(json) else { return Vec::new() }; + let Some(map) = value.as_object() else { return Vec::new() }; + let mut out = Vec::with_capacity(map.len().min(1 << 20)); + for (name, hex) in map { + let Some(hex) = hex.as_str() else { continue }; + let Some(bits) = u32::from_str_radix(hex.trim_start_matches("0x"), 16).ok() else { continue }; + out.push((f32::from_bits(bits), name.clone())); + } + out +} + +/// The stream channel names of a layer (`crypto_asset00`, `crypto_asset01`, …, exactly as +/// the file spells them), matched leniently: the channel prefix and the `name` attribute +/// differ in case or `_`/`-` between renderers (`CryptoAsset` vs `crypto_asset`). +fn layer_streams(header: &exr::meta::header::Header, name: &str) -> Vec { + let normalize = |s: &str| s.to_ascii_lowercase().replace(['_', '-'], ""); + let want = normalize(name); + let mut found: Vec<(u32, String)> = header + .channels + .list + .iter() + .filter_map(|c| { + let s = split_stream(&c.name.to_string())?; + (!s.coverage).then_some((s.index, s.stream)) + }) + .filter(|(_, stream)| normalize(stream.trim_end_matches(|c: char| c.is_ascii_digit())) == want) + .collect(); + found.sort(); + found.dedup(); + found.into_iter().map(|(_, stream)| stream).collect() +} + +/// Lists every Cryptomatte layer of the file from the headers alone: what a Cryptomatte +/// picker offers. The embedded manifest (capped, JSON) is parsed into (ID, name) pairs. +pub(crate) fn layers(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + super::exr::check_previews(bytes)?; + let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(|e| CodecError::malformed(F, e))?; + // Manifests can be large; bound the embedded text before parsing anything. + const MANIFEST_CAP: usize = 16 << 20; + let mut out = Vec::new(); + for (part, header) in meta.headers.iter().enumerate() { + let size = header.layer_size; + let (w, h) = (u32::try_from(size.0).unwrap_or(u32::MAX), u32::try_from(size.1).unwrap_or(u32::MAX)); + limits.check_bytes(w, h, 8)?; + // Cache stream lists by resolved layer name: layer_streams() rescans every channel, + // and distinct file-controlled keys can all name the same layer, which would turn + // channel discovery into O(keys * channels) work and allocate a fresh list each time. + let mut streams_for: std::collections::HashMap> = std::collections::HashMap::new(); + for key in cryptomatte_keys(&header.own_attributes.other) { + let Some(name) = attribute(&header.own_attributes.other, &key, "name") else { continue }; + let channels = streams_for.entry(name.clone()).or_insert_with(|| layer_streams(header, name.as_str())).clone(); + if channels.is_empty() { + continue; + } + let manifest = attribute(&header.own_attributes.other, &key, "manifest") + .filter(|m| m.len() <= MANIFEST_CAP) + .map(|m| parse_manifest(m.as_str())) + .unwrap_or_default(); + out.push(CryptomatteLayer { + part, + name, + key: key.clone(), + conversion: attribute(&header.own_attributes.other, &key, "conversion"), + manifest, + sidecar_manifest: attribute(&header.own_attributes.other, &key, "manif_file"), + channels, + }); + } + } + out.sort_by(|a, b| (&a.part, &a.name).cmp(&(&b.part, &b.name))); + Ok(out) +} + +/// Every `cryptomatte//` key of a header, in file order, without duplicates. +fn cryptomatte_keys(other: &std::collections::HashMap) -> Vec { + let mut keys = Vec::new(); + // Track seen keys in a set: the attribute names are file-controlled, so membership with + // keys.iter().any() is O(n^2) in the number of distinct `cryptomatte//` + // groups. The Vec still records file order; the set only answers "seen before". + let mut seen = std::collections::HashSet::new(); + for name in other.keys() { + let full = name.to_string(); + let Some(rest) = full.strip_prefix("cryptomatte/") else { continue }; + let Some((key, _field)) = rest.split_once('/') else { continue }; + if !key.is_empty() && seen.insert(key.to_string()) { + keys.push(key.to_string()); + } + } + keys +} + +/// Decodes the samples of the Cryptomatte layer called `layer_name` (a +/// [`CryptomatteLayer::name`]): per pixel its (ID, coverage) pairs, coverage above zero, +/// sorted by descending coverage. Refuses files that also contain deep parts, like the +/// other flat part decoders. +pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result { + let found = layers(bytes, limits)?; + let layer = found + .iter() + .find(|l| l.name.eq_ignore_ascii_case(layer_name)) + .ok_or_else(|| CodecError::unsupported(F, format!("no Cryptomatte layer named {layer_name}")))?; + // Also caps all parts' declared sizes together, like every other flat decode. + let meta = super::exr::read_meta(bytes, limits)?; + if meta.requirements.has_deep_data { + return Err(CodecError::unsupported(F, "this file has deep parts; Cryptomatte decoding stays with flat files")); + } + let header = meta.headers.get(layer.part).ok_or_else(|| CodecError::malformed(F, "the Cryptomatte part is missing"))?; + let size = header.layer_size; + let (w, h) = (size.0, size.1); + let npx = w.checked_mul(h).ok_or_else(|| CodecError::malformed(F, "the layer is too large"))?; + let too_large = || CodecError::malformed(F, "the layer is too large"); + let (w32, h32) = (u32::try_from(w).map_err(|_| too_large())?, u32::try_from(h).map_err(|_| too_large())?); + // Each pixel also gets a pair list, so count its header with the channel samples (four + // f32 channels per set: two ranks of ID and coverage). Include the heap capacity the pair + // list can reach: up to one (id, coverage) pair per rank, 8 bytes each. Without this the + // returned per-pixel vectors could exceed the caller's max_alloc budget. + let ranks = layer.channels.len().saturating_mul(2); + let pair_capacity = (ranks as u64).saturating_mul(std::mem::size_of::<(f32, f32)>() as u64); + let per_pixel = + (layer.channels.len().max(1) as u64).saturating_mul(16).saturating_add(std::mem::size_of::>() as u64).saturating_add(pair_capacity); + limits.check_bytes(w32, h32, per_pixel)?; + + let ex_layer = super::exr::read_layer(bytes, layer.part)?; + + // Two (ID, coverage) ranks per set: rank `slot * 2 + stream.rank`. + let mut ids: Vec> = vec![Vec::new(); ranks]; + let mut coverages: Vec> = vec![Vec::new(); ranks]; + for c in &ex_layer.channel_data.list { + let Some(stream) = split_stream(&c.name.to_string()) else { continue }; + let Some(slot) = layer.channels.iter().position(|s| *s == stream.stream) else { continue }; + let values: Vec = c.sample_data.values_as_f32().collect(); + if values.len() != npx { + return Err(CodecError::malformed(F, "a Cryptomatte channel is smaller than its layer")); + } + let bucket = if stream.coverage { &mut coverages } else { &mut ids }; + let Some(dst) = slot.checked_mul(2).and_then(|r| r.checked_add(stream.rank)).and_then(|r| bucket.get_mut(r)) else { continue }; + *dst = values; + } + + let mut pixels = vec![Vec::new(); npx]; + for (p, out) in pixels.iter_mut().enumerate() { + for (id, cov) in ids.iter().zip(&coverages) { + let (id, cov) = (id.get(p).copied().unwrap_or(0.0), cov.get(p).copied().unwrap_or(0.0)); + if cov > 0.0 && id.is_finite() { + out.push((id, cov)); + } + } + out.sort_by(|a, b| b.1.total_cmp(&a.1)); + } + Ok(CryptomatteBuffer { width: w32, height: h32, pixels }) +} diff --git a/crates/image-codecs/src/codecs/fits.rs b/crates/image-codecs/src/codecs/fits.rs new file mode 100644 index 000000000..ae6e94505 --- /dev/null +++ b/crates/image-codecs/src/codecs/fits.rs @@ -0,0 +1,855 @@ +//! FITS (Flexible Image Transport System, FITS Standard 4.0), implemented here: the image in the +//! primary HDU, or the first `IMAGE` extension when the primary HDU holds no data. +//! +//! Reading: +//! +//! * `BITPIX` 8, 16, 32, 64, -32 and -64, with `BSCALE`/`BZERO` and integer `BLANK`. +//! * `NAXIS3 = 3` opens as RGB (the planes are R, G, B); any other plane count opens the first +//! plane as gray with [`DecodeWarning::MorePages`]. +//! * Rows are stored bottom-to-top, as the standard's convention has it (Siril, DS9, Astropy), +//! unless `ROWORDER = 'TOP-DOWN'`. +//! * Physical values that fit (unsigned 8-bit, or integers in 0..=65535 such as a camera's +//! `BITPIX = 16`, `BZERO = 32768`) are kept exactly as U8/U16. Everything else becomes F32: +//! values up to 2 are taken as already normalised (Siril, PixInsight), larger ones are scaled by +//! 65535 when they fit in it (DeepSkyStacker, most stackers) and by the maximum otherwise. NaN +//! and blank pixels read as 0. +//! * The other header cards are kept as text: `KEY` → value (strings unquoted), `COMMENT` and +//! `HISTORY` by their text, `HIERARCH` keywords by their long name. Long strings continued +//! on `CONTINUE` cards are joined. +//! +//! Writing: gray or RGB, U8 (`BITPIX = 8`), U16 (`BITPIX = 16`, `BZERO = 32768`) or F32 +//! (`BITPIX = -32`, 0..1), bottom-to-top with `ROWORDER = 'BOTTOM-UP'`, plus the text cards +//! (strings longer than one card on `CONTINUE` cards). +//! Tile-compressed files (fpack `ZIMAGE` tables) are recognised and refused. + +use std::collections::HashMap; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +const F: Format = Format::Fits; +const BLOCK: usize = 2880; +const CARD: usize = 80; +/// Most HDUs walked looking for an image; FITS files from cameras and stackers hold a handful. +const MAX_HDUS: usize = 1000; + +/// Keywords that describe the data layout. They are written from the image, never copied from +/// (or kept as) text metadata: after decoding they no longer describe the pixels. +const STRUCTURAL: &[&str] = &[ + "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", "DATAMIN", + "DATAMAX", "ZIMAGE", +]; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +#[derive(Debug, Clone, PartialEq)] +enum Value { + /// A quoted string, unescaped, trailing blanks removed. + Str(String), + /// Anything else (number, logical, complex) as written. + Raw(String), +} + +#[derive(Debug, Clone)] +struct Card { + key: String, + /// `None` for commentary cards (`COMMENT`, `HISTORY`, blank keyword). + value: Option, + /// The text of a commentary card. + text: String, + /// A `HIERARCH` keyword (`key` holds its long name). + hierarch: bool, +} + +fn parse_value(field: &str) -> Value { + let t = field.trim_start(); + if let Some(rest) = t.strip_prefix('\'') { + let mut s = String::new(); + let mut chars = rest.chars().peekable(); + while let Some(c) = chars.next() { + if c == '\'' { + if chars.peek() == Some(&'\'') { + chars.next(); + s.push('\''); + } else { + break; + } + } else { + s.push(c); + } + } + Value::Str(s.trim_end().to_string()) + } else { + Value::Raw(t.split('/').next().unwrap_or("").trim().to_string()) + } +} + +fn parse_card(raw: &[u8]) -> Card { + // Header bytes are ASCII 32–126 by the standard; anything else is replaced, never trusted. + let s: String = raw.iter().map(|&b| if (32..=126).contains(&b) { b as char } else { '?' }).collect(); + let key_field = s.get(..8).unwrap_or(&s).trim_end().to_string(); + if key_field == "HIERARCH" { + let rest = s.get(9..).unwrap_or(""); + if let Some((k, v)) = rest.split_once('=') { + return Card { key: k.trim().to_string(), value: Some(parse_value(v)), text: String::new(), hierarch: true }; + } + } + if s.get(8..10) == Some("= ") { + let v = parse_value(s.get(10..).unwrap_or("")); + return Card { key: key_field, value: Some(v), text: String::new(), hierarch: false }; + } + Card { key: key_field, value: None, text: s.get(8..).unwrap_or("").trim_end().to_string(), hierarch: false } +} + +struct Header { + cards: Vec, + /// First-occurrence index of each non-HIERARCH keyword, so `get` is O(1). The card + /// count and the number of looked-up keywords (NAXIS1..NAXISn) are both file-controlled, + /// so a linear scan per lookup lets a crafted header multiply commentary cards by axis + /// count into quadratic work. + index: HashMap, + /// Offset of the data unit. + data_start: usize, +} + +impl Header { + fn get(&self, key: &str) -> Option<&Value> { + let c = &self.cards[*self.index.get(key)?]; + c.value.as_ref() + } + + fn int(&self, key: &str) -> Option { + match self.get(key)? { + Value::Raw(r) => r.parse::().ok().or_else(|| self.float(key).filter(|f| f.fract() == 0.0 && f.abs() < 9.0e15).map(|f| f as i64)), + Value::Str(_) => None, + } + } + + fn float(&self, key: &str) -> Option { + match self.get(key)? { + // Fortran-style `D` exponents are allowed in FITS. + Value::Raw(r) => r.replace(['D', 'd'], "E").parse::().ok().filter(|f| f.is_finite()), + Value::Str(_) => None, + } + } + + fn logical(&self, key: &str) -> bool { + matches!(self.get(key), Some(Value::Raw(r)) if r == "T") + } + + fn string(&self, key: &str) -> Option<&str> { + match self.get(key)? { + Value::Str(s) => Some(s.trim()), + Value::Raw(_) => None, + } + } +} + +fn parse_header(b: &[u8], start: usize) -> Result { + let mut cards = Vec::new(); + let mut pos = start; + loop { + let raw = b.get(pos..pos.checked_add(CARD).ok_or_else(|| err("header too long"))?).ok_or_else(|| err("header ends before END"))?; + pos += CARD; + if raw.starts_with(b"END") && raw.get(3..).is_some_and(|r| r.iter().all(|&c| c == b' ')) { + break; + } + let card = parse_card(raw); + // Long strings (FITS 4.0 section 4.2.1.2): a string ending in '&' goes on in the next + // `CONTINUE` record. An orphaned `CONTINUE` stays a card of its own (commentary). + if let Some(Card { value: Some(Value::Str(s)), .. }) = cards.last_mut() + && s.ends_with('&') + && card.key == "CONTINUE" + && card.text.starts_with(" ") + && let Value::Str(more) = parse_value(&card.text) + { + s.pop(); + s.push_str(&more); + continue; + } + cards.push(card); + } + let data_start = pos.div_ceil(BLOCK).checked_mul(BLOCK).ok_or_else(|| err("header too long"))?; + // Index each non-HIERARCH keyword to its first occurrence, matching the previous + // `find(|c| !c.hierarch && c.key == key)` lookup while making `get` O(1). + let mut index = HashMap::with_capacity(cards.len()); + for (i, c) in cards.iter().enumerate() { + if !c.hierarch { + index.entry(c.key.clone()).or_insert(i); + } + } + Ok(Header { cards, index, data_start }) +} + +/// The axis lengths (`NAXIS1`, `NAXIS2`, …). +fn axes(h: &Header) -> Result, CodecError> { + let n = h.int("NAXIS").ok_or_else(|| err("missing NAXIS"))?; + if !(0..=999).contains(&n) { + return Err(err(format!("invalid NAXIS {n}"))); + } + (1..=n).map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))).collect() +} + +/// Bytes in the data unit (before padding to a block). +fn data_len(h: &Header, bitpix: i64) -> Result { + let ax = axes(h)?; + if ax.is_empty() { + return Ok(0); + } + let pcount = u64::try_from(h.int("PCOUNT").unwrap_or(0)).map_err(|_| err("negative PCOUNT"))?; + let gcount = u64::try_from(h.int("GCOUNT").unwrap_or(1)).map_err(|_| err("negative GCOUNT"))?; + let elems = ax.iter().try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("data size overflows"))?; + elems + .checked_add(pcount) + .and_then(|v| v.checked_mul(gcount)) + .and_then(|v| v.checked_mul(bitpix.unsigned_abs() / 8)) + .ok_or_else(|| err("data size overflows")) +} + +/// Inspect every HDU without decoding pixels: one gray plane or three RGB planes are retained, +/// while other cubes, image extensions, tables and other data-bearing HDUs cannot survive a flat save. +/// Incomplete or over-limit walks fail closed rather than treating an unchecked suffix as safe. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let mut start = 0; + let mut image_seen = false; + for index in 0..MAX_HDUS { + let h = parse_header(bytes, start)?; + if index == 0 && h.get("SIMPLE").is_none() { + return Err(err("missing SIMPLE")); + } + let ax = axes(&h)?; + let size = data_len(&h, h.int("BITPIX").unwrap_or(8))?; + let image_hdu = index == 0 || h.string("XTENSION") == Some("IMAGE"); + if h.logical("ZIMAGE") { + return Ok(true); + } + if !image_hdu && size > 0 { + return Ok(true); + } + // An image HDU with a zero axis has no data (FITS 4.0, NAXISn) and holds no image, like + // the empty primary: `find_image` skips it too. + if image_hdu && size > 0 && ax.len() >= 2 && ax[..2].iter().all(|n| *n > 0) { + let planes = ax.iter().skip(2).try_fold(1u64, |n, axis| n.checked_mul(*axis)).ok_or_else(|| err("plane count overflows"))?; + let retained = planes == 1 || (ax.get(2) == Some(&3) && planes == 3); + if image_seen || !retained || h.int("GCOUNT").unwrap_or(1) > 1 || h.int("PCOUNT").unwrap_or(0) > 0 { + return Ok(true); + } + image_seen = true; + } else if size > 0 { + return Ok(true); + } + let padded = size.div_ceil(BLOCK as u64).checked_mul(BLOCK as u64).ok_or_else(|| err("data size overflows"))?; + let next = usize::try_from(padded).ok().and_then(|size| h.data_start.checked_add(size)).ok_or_else(|| err("data size overflows"))?; + if next > bytes.len() { + return Err(err("truncated HDU data")); + } + if next == bytes.len() { + return Ok(false); + } + start = next; + } + Err(err("too many HDUs to inspect every source image")) +} + +/// Whether an HDU with these axes stores pixels: at least two axes and none of them zero (FITS 4.0, +/// NAXISn: a zero axis means no data follow the header). +fn holds_pixels(ax: &[u64]) -> bool { + ax.len() >= 2 && ax.iter().all(|n| *n > 0) +} + +/// Finds the image HDU: the primary one when it holds pixels, else the first `IMAGE` extension +/// that does. Returns its header and the primary header (for its descriptive cards). +fn find_image(b: &[u8]) -> Result<(Header, Option
), CodecError> { + let primary = parse_header(b, 0)?; + if primary.get("SIMPLE").is_none() { + return Err(err("missing SIMPLE")); + } + if holds_pixels(&axes(&primary)?) { + return Ok((primary, None)); + } + let mut compressed = false; + let mut cur_start = primary.data_start; + let mut cur_len = data_len(&primary, primary.int("BITPIX").unwrap_or(8))?; + for _ in 0..MAX_HDUS { + let next = usize::try_from(cur_len.div_ceil(BLOCK as u64).saturating_mul(BLOCK as u64)) + .ok() + .and_then(|l| cur_start.checked_add(l)) + .ok_or_else(|| err("data size overflows"))?; + if next >= b.len() { + break; + } + let h = parse_header(b, next)?; + let kind = h.string("XTENSION").unwrap_or("").to_string(); + if kind == "IMAGE" && holds_pixels(&axes(&h)?) { + return Ok((h, Some(primary))); + } + if kind == "BINTABLE" && h.logical("ZIMAGE") { + compressed = true; + } + cur_len = data_len(&h, h.int("BITPIX").unwrap_or(8))?; + cur_start = h.data_start; + } + if compressed { + return Err(CodecError::unsupported(F, "tile-compressed FITS (fpack) is not supported; decompress it with funpack first")); + } + Err(err("the file holds no image")) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (h, primary) = find_image(bytes)?; + let bitpix = h.int("BITPIX").ok_or_else(|| err("missing BITPIX"))?; + if !matches!(bitpix, 8 | 16 | 32 | 64 | -32 | -64) { + return Err(err(format!("invalid BITPIX {bitpix}"))); + } + let ax = axes(&h)?; + let (Some(&w), Some(&hgt)) = (ax.first(), ax.get(1)) else { return Err(err("the image has fewer than two axes")) }; + let w = u32::try_from(w).map_err(|_| CodecError::LimitExceeded(format!("width {w}")))?; + let hgt = u32::try_from(hgt).map_err(|_| CodecError::LimitExceeded(format!("height {hgt}")))?; + let planes = ax.iter().skip(2).try_fold(1u64, |a, &n| a.checked_mul(n)).ok_or_else(|| err("too many planes"))?; + let (layout, nplanes) = if ax.get(2) == Some(&3) && planes == 3 { (ChannelLayout::Rgb, 3usize) } else { (ChannelLayout::Gray, 1) }; + + let bscale = h.float("BSCALE").unwrap_or(1.0); + let bzero = h.float("BZERO").unwrap_or(0.0); + let blank = if bitpix > 0 { h.int("BLANK") } else { None }; + let integral = bscale == 1.0 && bzero.fract() == 0.0; + let candidate = match bitpix { + 8 if integral && bzero == 0.0 => SampleType::U8, + 8 | 16 if integral => SampleType::U16, + _ => SampleType::F32, + }; + limits.check(w, hgt, layout, candidate)?; + + let bps = (bitpix.unsigned_abs() / 8) as usize; + let plane_len = (w as usize).checked_mul(hgt as usize).ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{hgt} does not fit in memory")))?; + let n = plane_len.checked_mul(nplanes).ok_or_else(|| CodecError::LimitExceeded("image does not fit in memory".into()))?; + let need = n.checked_mul(bps).and_then(|l| l.checked_add(h.data_start)).ok_or_else(|| err("data size overflows"))?; + let data = bytes.get(h.data_start..need).ok_or_else(|| err("truncated data"))?; + + // Physical value of sample `i` (planar order); `None` for a blank or NaN pixel. + let physical = |i: usize| -> Option { + let s = data.get(i * bps..(i + 1) * bps)?; + let raw = match bitpix { + 8 => s.first().map(|&v| v as i64)?, + 16 => i16::from_be_bytes([s[0], s[1]]) as i64, + 32 => i32::from_be_bytes([s[0], s[1], s[2], s[3]]) as i64, + 64 => i64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]), + -32 => return Some(f32::from_be_bytes([s[0], s[1], s[2], s[3]]) as f64 * bscale + bzero).filter(|v| v.is_finite()), + _ => return Some(f64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]]) * bscale + bzero).filter(|v| v.is_finite()), + }; + if blank == Some(raw) { + return None; + } + Some(raw as f64 * bscale + bzero) + }; + + let top_down = h.string("ROWORDER").is_some_and(|r| r.eq_ignore_ascii_case("TOP-DOWN")); + let (wu, hu) = (w as usize, hgt as usize); + // Planar, bottom-up (by default) → interleaved, top-down. + let dest = |i: usize| -> usize { + let (p, rem) = (i / plane_len, i % plane_len); + let (y, x) = (rem / wu, rem % wu); + let y = if top_down { y } else { hu - 1 - y }; + (y * wu + x) * nplanes + p + }; + + let mut img = match candidate { + SampleType::U8 => { + let mut out = vec![0u8; n]; + for i in 0..n { + out[dest(i)] = physical(i).unwrap_or(0.0) as u8; + } + Image::from_u8(w, hgt, layout, out)? + } + _ => { + let mut out16 = if candidate == SampleType::U16 { Some(vec![0u16; n]) } else { None }; + if let Some(out) = out16.as_mut() { + for i in 0..n { + let v = physical(i).unwrap_or(0.0); + if !(0.0..=65535.0).contains(&v) { + // Signed data with negative values: fall back to float. + out16 = None; + break; + } + out[dest(i)] = v as u16; + } + } + match out16 { + Some(out) => Image::from_u16(w, hgt, layout, &out)?, + None => { + let max = (0..n).filter_map(physical).fold(0.0f64, f64::max); + let white = if max <= 2.0 { + 1.0 + } else if max <= 65535.0 { + 65535.0 + } else { + max + }; + let mut out = vec![0f32; n]; + for i in 0..n { + out[dest(i)] = (physical(i).unwrap_or(0.0) / white) as f32; + } + Image::from_f32(w, hgt, layout, &out)? + } + } + } + }; + + if planes > nplanes as u64 { + img.warnings.push(DecodeWarning::MorePages { total: u32::try_from(planes).ok() }); + } + let mut text = Vec::new(); + for c in primary.iter().flat_map(|p| p.cards.iter()).chain(h.cards.iter()) { + if !c.hierarch && (STRUCTURAL.contains(&c.key.as_str()) || is_naxis(&c.key)) { + continue; + } + let entry = match &c.value { + Some(Value::Str(s) | Value::Raw(s)) => (c.key.clone(), s.clone()), + None if c.key.is_empty() && c.text.trim().is_empty() => continue, + None => (if c.key.is_empty() { "COMMENT".to_string() } else { c.key.clone() }, c.text.trim().to_string()), + }; + text.push(entry); + } + img.meta.text = text; + Ok(img) +} + +fn is_naxis(key: &str) -> bool { + key.strip_prefix("NAXIS").is_some_and(|d| d.chars().all(|c| c.is_ascii_digit())) +} + +/// A standard keyword: 1–8 characters from `A-Z 0-9 _ -`. +fn standard_key(key: &str) -> bool { + (1..=8).contains(&key.len()) && key.bytes().all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_' || b == b'-') +} + +fn ascii(s: &str) -> String { + s.chars().map(|c| if (' '..='~').contains(&c) { c } else { '?' }).collect() +} + +/// A FITS literal (number or logical) that can be written unquoted. +fn literal(v: &str) -> bool { + v == "T" || v == "F" || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) +} + +fn quoted(v: &str, room: usize) -> String { + let mut s = String::from("'"); + for c in ascii(v).chars() { + let add = if c == '\'' { 2 } else { 1 }; + // Keep room for the closing quote. + if s.len() + add + 1 > room { + break; + } + s.push(c); + if c == '\'' { + s.push('\''); + } + } + // Strings are at least eight characters inside the quotes. + while s.len() < 9 { + s.push(' '); + } + s.push('\''); + s +} + +struct Writer { + out: Vec, +} + +impl Writer { + fn card(&mut self, text: &str) { + let mut c: Vec = ascii(text).into_bytes(); + c.truncate(CARD); + c.resize(CARD, b' '); + self.out.extend_from_slice(&c); + } + + fn value(&mut self, key: &str, v: &str) { + if literal(v) { + self.card(&format!("{key:<8}= {v:>20}")); + return; + } + // A string too long for one card goes on in `CONTINUE` cards, every piece but the last + // ending in '&' (FITS 4.0 section 4.2.1.2). A doubled quote is never split, and trailing + // blanks (not significant) are dropped so none are kept before an '&'. + let escaped = ascii(v).trim_end().replace('\'', "''"); + let mut rest = escaped.as_str(); + let mut head = format!("{key:<8}= "); + while rest.len() > CARD - 12 { + let cut = CARD - 13 - rest.get(..CARD - 13).unwrap_or("").bytes().rev().take_while(|&b| b == b'\'').count() % 2; + let (piece, tail) = rest.split_at_checked(cut).unwrap_or((rest, "")); + self.card(&format!("{head}'{piece}&'")); + head = "CONTINUE ".to_string(); + rest = tail; + } + self.card(&format!("{head}'{rest:<8}'")); + } + + fn commentary(&mut self, key: &str, text: &str) { + let chars: Vec = ascii(text).chars().collect(); + if chars.is_empty() { + self.card(key); + } + for chunk in chars.chunks(CARD - 8) { + self.card(&format!("{key:<8}{}", chunk.iter().collect::())); + } + } + + fn text(&mut self, key: &str, v: &str) { + let upper = key.to_ascii_uppercase(); + if matches!(upper.as_str(), "COMMENT" | "HISTORY") { + self.commentary(&upper, v); + } else if standard_key(key) { + // `CONTINUE` only ever carries a string's continuation, written by `value`. + if !STRUCTURAL.contains(&key) && !is_naxis(key) && key != "CONTINUE" { + self.value(key, v); + } + } else { + // Long or mixed-case names (`Description`, `Software`): the HIERARCH convention, + // when the card fits, else a comment. + let name = ascii(key); + let head = format!("HIERARCH {name} = "); + let fits_key = !name.contains('=') && !name.trim().is_empty(); + let body = if literal(v) { v.to_string() } else { quoted(v, CARD.saturating_sub(head.len())) }; + if fits_key && head.len() + body.len() <= CARD && (literal(v) || body.ends_with('\'')) && quoted_whole(v, &body) { + self.card(&format!("{head}{body}")); + } else { + self.commentary("COMMENT", &format!("{key} = {v}")); + } + } + } +} + +/// `body` holds all of `v` (it was not cut short to fit the card). +fn quoted_whole(v: &str, body: &str) -> bool { + literal(v) || body.trim_matches('\'').trim_end().replace("''", "'") == ascii(v).trim_end() +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let layout = img.layout(); + let nc = match layout { + ChannelLayout::Gray => 1, + ChannelLayout::Rgb => 3, + l => return Err(CodecError::encode(F, format!("FITS cannot store {l:?}"))), + }; + let bitpix = match img.sample_type() { + SampleType::U8 => 8, + SampleType::U16 => 16, + SampleType::F32 => -32, + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + }; + let mut wr = Writer { out: Vec::new() }; + wr.value("SIMPLE", "T"); + wr.value("BITPIX", &bitpix.to_string()); + wr.value("NAXIS", if nc == 3 { "3" } else { "2" }); + wr.value("NAXIS1", &w.to_string()); + wr.value("NAXIS2", &h.to_string()); + if nc == 3 { + wr.value("NAXIS3", "3"); + } + if bitpix == 16 { + wr.value("BZERO", "32768"); + wr.value("BSCALE", "1"); + } + wr.value("ROWORDER", "BOTTOM-UP"); + if opts.embed_metadata { + for (k, v) in &img.meta.text { + wr.text(k, v); + } + } + wr.card("END"); + let mut out = wr.out; + out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); + + // Planar, bottom row first. + let (wu, hu) = (w as usize, h as usize); + let idx = |p: usize, y: usize, x: usize| ((hu - 1 - y) * wu + x) * nc + p; + match bitpix { + 8 => { + let d = img.data(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + out.push(d[idx(p, y, x)]); + } + } + } + } + 16 => { + let d = img.to_u16_samples().unwrap_or_default(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + out.extend_from_slice(&((d[idx(p, y, x)] as i32 - 32768) as i16).to_be_bytes()); + } + } + } + } + _ => { + let d = img.to_f32_samples().unwrap_or_default(); + for p in 0..nc { + for y in 0..hu { + for x in 0..wu { + // 0..1 as documented: a value above 2 would make readers (ours too) + // take the data as ADU and scale it by 65535. NaN is written as 0. + let v = d[idx(p, y, x)]; + let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; + out.extend_from_slice(&v.to_be_bytes()); + } + } + } + } + } + out.resize(out.len().div_ceil(BLOCK) * BLOCK, 0); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn header(cards: &[&str]) -> Vec { + let mut w = Writer { out: Vec::new() }; + for c in cards { + w.card(c); + } + w.card("END"); + let mut out = w.out; + out.resize(out.len().div_ceil(BLOCK) * BLOCK, b' '); + out + } + + #[test] + fn continue_strings_decode() { + let mut b = header(&[ + "SIMPLE = T", + "BITPIX = 8", + "NAXIS = 2", + "NAXIS1 = 1", + "NAXIS2 = 1", + "OBJECT = 'aaa&'", + "CONTINUE 'bbb&' / a comment", + "CONTINUE '&'", + "CONTINUE ''", + "OBSERVER= 'R&'", + "COMMENT hi", + "CONTINUE 'orphan'", + ]); + b.push(0); + let img = decode(&b, &Limits::default()).unwrap(); + let text = [("OBJECT", "aaabbb"), ("OBSERVER", "R&"), ("COMMENT", "hi"), ("CONTINUE", "'orphan'")]; + assert_eq!(img.meta.text, text.map(|(k, v)| (k.to_string(), v.to_string()))); + } + + #[test] + fn continue_strings_round_trip() { + for value in ["a".repeat(100), format!("{}' {}", "a".repeat(65), "b".repeat(100)), "'".repeat(100), "a".repeat(68)] { + let mut img = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); + img.meta.text = vec![("OBJECT".into(), value.clone()), ("CONTINUE".into(), "orphan".into())]; + let b = crate::encode(&img, F, &EncodeOptions::default()).unwrap(); + assert!(!b.chunks(CARD).any(|c| c.starts_with(b"CONTINUE="))); + let decoded = decode(&b, &Limits::default()).unwrap(); + assert_eq!(decoded.meta.text, vec![("OBJECT".into(), value)]); + } + } + + #[test] + fn camera_u16_with_bzero_reads_exactly_and_bottom_up() { + let mut b = header(&[ + "SIMPLE = T", + "BITPIX = 16", + "NAXIS = 2", + "NAXIS1 = 2", + "NAXIS2 = 2", + "BZERO = 32768", + "OBJECT = 'M 31 '", + "EXPTIME = 300.0 / seconds", + ]); + for v in [0u16, 1000, 40000, 65535] { + b.extend_from_slice(&((v as i32 - 32768) as i16).to_be_bytes()); + } + b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + // The first stored row is the bottom one. + assert_eq!(img.to_u16_samples().unwrap(), vec![40000, 65535, 0, 1000]); + assert!(img.meta.text.contains(&("OBJECT".into(), "M 31".into()))); + assert!(img.meta.text.contains(&("EXPTIME".into(), "300.0".into()))); + assert!(!img.meta.text.iter().any(|(k, _)| k == "BZERO" || k == "NAXIS1")); + } + + #[test] + fn roworder_top_down_is_honoured() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 2", "ROWORDER= 'TOP-DOWN'"]); + b.extend_from_slice(&[10, 20]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[10, 20]); + } + + #[test] + fn stacked_float_in_adu_is_scaled_by_65535() { + let mut b = header(&["SIMPLE = T", "BITPIX = -32", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); + for v in [65535.0f32, f32::NAN] { + b.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.to_f32_samples().unwrap(), vec![1.0, 0.0]); + } + + #[test] + fn hdr_float_saves_clamped_and_reopens_as_saved() { + // One value above 2 used to make the reader scale everything by 65535 (all black). + let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap(); + let opts = EncodeOptions::default(); + let bytes = encode(&img, crate::fidelity::plan(&img, F, &opts), &opts).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap().to_f32_samples().unwrap(); + assert!((back[0] - 0.5).abs() < 1e-6, "{back:?}"); + assert!((back[1] - 1.0).abs() < 1e-6, "{back:?}"); + } + + #[test] + fn signed_negative_data_falls_back_to_float() { + let mut b = header(&["SIMPLE = T", "BITPIX = 16", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]); + for v in [-100i16, 1000] { + b.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::F32); + } + + #[test] + fn image_extension_after_empty_primary() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T", "TELESCOP= 'Seestar S50'"]); + b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"])); + b.push(42); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[42]); + assert!(img.meta.text.contains(&("TELESCOP".into(), "Seestar S50".into()))); + } + + /// A zero axis (`NAXIS3 = 0`) means the HDU has no data: it is neither the opened image nor + /// an additional one that Batch Save and Close would discard. + #[test] + fn zero_axis_image_hdus_are_empty() { + let empty = |primary: bool| { + let first = if primary { "SIMPLE = T" } else { "XTENSION= 'IMAGE '" }; + header(&[first, "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 0", "PCOUNT = 0", "GCOUNT = 1"]) + }; + let image = || { + let mut b = header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1", "PCOUNT = 0", "GCOUNT = 1"]); + b.push(42); + b.resize(b.len().div_ceil(BLOCK) * BLOCK, 0); + b + }; + let mut after_empty_primary = empty(true); + after_empty_primary.extend(image()); + let mut after_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); + after_empty_extension.extend(empty(false)); + after_empty_extension.extend(image()); + let mut before_empty_extension = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0", "EXTEND = T"]); + before_empty_extension.extend(image()); + before_empty_extension.extend(empty(false)); + for b in [&after_empty_primary, &after_empty_extension, &before_empty_extension] { + assert!(!crate::has_unretained_source_data(b).unwrap()); + assert_eq!(decode(b, &Limits::default()).unwrap().data(), &[42]); + } + // Only empty HDUs: nothing to open, nothing to lose. + assert!(!crate::has_unretained_source_data(&empty(true)).unwrap()); + assert!(decode(&empty(true), &Limits::default()).is_err()); + // A second non-empty image is still additional data. + let mut two = after_empty_primary.clone(); + two.extend(image()); + assert!(crate::has_unretained_source_data(&two).unwrap()); + } + + #[test] + fn fpack_is_refused_as_unsupported() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0", "ZIMAGE = T"])); + b.extend(header(&["XTENSION= 'BINTABLE'", "BITPIX = 8", "NAXIS = 1", "NAXIS1 = 0"])); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + + /// Hostile headers: absurd or negative axes, bad BITPIX, overflowing sizes, missing END, + /// a long chain of empty HDUs and declared data past the end all error, never panic or + /// allocate the declared size. + #[test] + fn hostile_headers_error_without_panicking() { + let base = ["SIMPLE = T", "BITPIX = 16"]; + let cases: &[&[&str]] = &[ + &["NAXIS = 2", "NAXIS1 = 4294967296", "NAXIS2 = 1"], + &["NAXIS = 2", "NAXIS1 = -5", "NAXIS2 = 1"], + &["NAXIS = 2", "NAXIS1 = 2000000", "NAXIS2 = 2000000"], + &["NAXIS = 3", "NAXIS1 = 65536", "NAXIS2 = 65536", "NAXIS3 = 9223372036854775807"], + &["NAXIS = 1000"], + &["NAXIS = -1"], + &["NAXIS = 2", "NAXIS1 = 0", "NAXIS2 = 0"], + &["NAXIS = 2", "NAXIS1 = 1.5D300", "NAXIS2 = 1"], + &["NAXIS = 2"], + &["NAXIS = 2", "NAXIS1 = 100", "NAXIS2 = 100"], + ]; + for extra in cases { + let cards: Vec<&str> = base.iter().chain(extra.iter()).copied().collect(); + let b = header(&cards); + assert!(decode(&b, &Limits::default()).is_err(), "{extra:?}"); + } + for bitpix in ["0", "7", "-16", "128", "'X'"] { + let card = format!("BITPIX = {bitpix}"); + let b = header(&["SIMPLE = T", &card, "NAXIS = 2", "NAXIS1 = 1", "NAXIS2 = 1"]); + assert!(decode(&b, &Limits::default()).is_err(), "BITPIX {bitpix}"); + } + // No END card: the header runs off the end of the file. + let mut w = Writer { out: Vec::new() }; + for c in ["SIMPLE = T", "BITPIX = 8", "NAXIS = 2"] { + w.card(c); + } + assert!(decode(&w.out, &Limits::default()).is_err()); + // An empty primary followed by many empty extensions: bounded walk, then "no image". + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + for _ in 0..(MAX_HDUS + 5) { + b.extend(header(&["XTENSION= 'IMAGE '", "BITPIX = 8", "NAXIS = 0"])); + } + assert!(decode(&b, &Limits::default()).is_err()); + // An extension whose declared data size overflows. + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 0"]); + b.extend(header(&[ + "XTENSION= 'BINTABLE'", + "BITPIX = 64", + "NAXIS = 2", + "NAXIS1 = 9223372036854775807", + "NAXIS2 = 4", + "PCOUNT = 0", + "GCOUNT = 1", + ])); + assert!(decode(&b, &Limits::default()).is_err()); + } + + #[test] + fn extra_planes_warn() { + let mut b = header(&["SIMPLE = T", "BITPIX = 8", "NAXIS = 3", "NAXIS1 = 1", "NAXIS2 = 1", "NAXIS3 = 4"]); + b.extend_from_slice(&[1, 2, 3, 4]); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.warnings, vec![DecodeWarning::MorePages { total: Some(4) }]); + } + + #[test] + fn strings_with_quotes_and_hierarch_round_trip() { + let mut w = Writer { out: Vec::new() }; + w.text("OBSERVER", "O'Brien"); + w.text("Software", "photocraft 1.0"); + w.text("HISTORY", "stacked"); + let cards: Vec = w.out.chunks(CARD).map(parse_card).collect(); + assert_eq!(cards[0].value, Some(Value::Str("O'Brien".into()))); + assert!(cards[1].hierarch && cards[1].key == "Software"); + assert_eq!(cards[1].value, Some(Value::Str("photocraft 1.0".into()))); + assert_eq!((cards[2].key.as_str(), cards[2].text.as_str()), ("HISTORY", "stacked")); + } +} diff --git a/crates/image-codecs/src/codecs/gif_frames.rs b/crates/image-codecs/src/codecs/gif_frames.rs new file mode 100644 index 000000000..b5439f6ef --- /dev/null +++ b/crates/image-codecs/src/codecs/gif_frames.rs @@ -0,0 +1,82 @@ +//! Every frame of an animated GIF. [`crate::decode`] keeps the first frame only; this walks the +//! whole animation for callers that can hold it (video layers). +//! +//! The `image` crate composites each frame onto the logical screen and applies the disposal +//! methods, so every returned frame is a full, self-contained picture. + +use std::io::Cursor; + +use image::codecs::gif::GifDecoder; +use image::{AnimationDecoder, ImageDecoder}; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, DecodeWarning, Image}; +use crate::options::DecodeOptions; + +/// Most frames one GIF (or animated WebP) may yield. Frames are held in memory, and a tiny +/// canvas could otherwise ask for millions of them while staying inside the byte budget. +pub const MAX_GIF_FRAMES: usize = 100_000; + +/// One composited frame of an animation and how long it stays on screen. +#[derive(Debug, Clone, PartialEq)] +pub struct AnimationFrame { + pub image: Image, + /// On-screen time in milliseconds. GIF counts in hundredths of a second, so there this is a + /// multiple of 10 (0 when the file gives none); WebP counts in milliseconds. + pub delay_ms: u32, +} + +/// Decodes every frame of a GIF, each as a full RGBA8 image of the logical screen size. +/// +/// The first frame must decode. If a later frame is damaged the frames before it are returned +/// and the first one carries [`DecodeWarning::Truncated`]. Dimensions, the frame count +/// ([`MAX_GIF_FRAMES`]) and the total decoded bytes are checked against `opts.limits` before +/// each frame is kept. +pub fn decode(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + let limits = &opts.limits; + let map = |e: image::ImageError| match e { + image::ImageError::Limits(l) => CodecError::LimitExceeded(l.to_string()), + image::ImageError::Unsupported(u) => CodecError::unsupported(Format::Gif, u.to_string()), + e => CodecError::malformed(Format::Gif, e), + }; + let mut decoder = GifDecoder::new(Cursor::new(bytes)).map_err(map)?; + let mut il = image::Limits::default(); + il.max_image_width = Some(limits.max_width); + il.max_image_height = Some(limits.max_height); + il.max_alloc = Some(limits.max_alloc); + decoder.set_limits(il).map_err(map)?; + let (w, h) = decoder.dimensions(); + limits.check_bytes(w, h, 4)?; + // The backend initializes the whole logical screen before it reads the first frame, so the + // same file-size plausibility as the first-frame decode applies before iterating (#8597). + super::via_image::gif_plausible(bytes, w, h, 4)?; + let frame_bytes = u64::from(w) * u64::from(h) * 4; + + let mut out: Vec = Vec::new(); + let mut damaged = false; + for frame in decoder.into_frames() { + let frame = match frame { + Ok(f) => f, + Err(e) if out.is_empty() => return Err(map(e)), + Err(_) => { + damaged = true; + break; + } + }; + if out.len() >= MAX_GIF_FRAMES { + return Err(CodecError::LimitExceeded(format!("more than {MAX_GIF_FRAMES} frames"))); + } + if frame_bytes.saturating_mul(out.len() as u64 + 1) > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{} frames of {w}x{h} exceed max_alloc {}", out.len() + 1, limits.max_alloc))); + } + let (numer, denom) = frame.delay().numer_denom_ms(); + let delay_ms = numer.checked_div(denom).unwrap_or(0); + let rgba = frame.into_buffer(); + out.push(AnimationFrame { image: Image::from_u8(w, h, ChannelLayout::Rgba, rgba.into_raw())?, delay_ms }); + } + if damaged && let Some(first) = out.first_mut() { + first.image.warnings.push(DecodeWarning::Truncated { format: Format::Gif }); + } + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/gif_palette.rs b/crates/image-codecs/src/codecs/gif_palette.rs new file mode 100644 index 000000000..eb0a0e218 --- /dev/null +++ b/crates/image-codecs/src/codecs/gif_palette.rs @@ -0,0 +1,126 @@ +//! The colour table of a GIF, read from its blocks without decoding pixels (GIF89a +//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control +//! Extension). +//! +//! A GIF is always palette-based, so it opens in Indexed Color with the table of the frame that +//! [`crate::decode`] keeps, the first: its Local Color Table when it has one, else the Global +//! one, and the transparent entry that frame's Graphic Control Extension names. + +/// Colours of the table the first frame uses, and its transparent entry. `None` for anything +/// that isn't a well-formed GIF up to its first image, or a first frame without a table. +pub(crate) fn palette(b: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { + if !(b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a")) { + return None; + } + // Logical Screen Descriptor: width, height, packed fields, background, aspect. + let packed = *b.get(10)?; + let mut at = 13; + let table = |at: usize, packed: u8| -> Option> { + let n = 2usize << (packed & 0x07); + let bytes = b.get(at..at.checked_add(n.checked_mul(3)?)?)?; + Some(bytes.as_chunks::<3>().0.to_vec()) + }; + let global = if packed & 0x80 != 0 { + let t = table(at, packed)?; + at += t.len() * 3; + Some(t) + } else { + None + }; + let mut transparent = None; + loop { + match *b.get(at)? { + // Extension: label, then data sub-blocks up to a zero-length one. + 0x21 => { + let label = *b.get(at + 1)?; + at += 2; + // Graphic Control Extension: block size 4, packed (bit 0: transparent colour + // given), delay (2 bytes), transparent colour index. + if label == 0xF9 && *b.get(at)? == 4 { + let flags = *b.get(at + 1)?; + transparent = (flags & 0x01 != 0).then_some(*b.get(at + 4)?); + } + loop { + let size = usize::from(*b.get(at)?); + at = at.checked_add(1 + size)?; + if size == 0 { + break; + } + } + } + // Image Descriptor: position and size (8 bytes), packed fields, then the Local Color + // Table if bit 7 is set. This is the first frame: its table is the one used. + 0x2C => { + let packed = *b.get(at + 9)?; + let colors = if packed & 0x80 != 0 { table(at + 10, packed)? } else { global? }; + let transparent = transparent.filter(|i| usize::from(*i) < colors.len()); + return Some((colors, transparent)); + } + // Trailer, or anything else: no image. + _ => return None, + } + } +} + +#[cfg(test)] +mod tests { + use super::palette; + + /// A GIF up to its first image: `global` table entries, optional GCE transparency, optional + /// local table. + fn gif(global: &[[u8; 3]], transparent: Option, local: Option<&[[u8; 3]]>) -> Vec { + let size_bits = |n: usize| (n.max(2).next_power_of_two().trailing_zeros() - 1) as u8; + let mut b = b"GIF89a".to_vec(); + b.extend([4, 0, 4, 0]); + b.push(if global.is_empty() { 0 } else { 0x80 | size_bits(global.len()) }); + b.extend([0, 0]); + let pad = |b: &mut Vec, t: &[[u8; 3]]| { + let n = 2usize << size_bits(t.len()); + for k in 0..n { + b.extend(t.get(k).copied().unwrap_or([0, 0, 0])); + } + }; + if !global.is_empty() { + pad(&mut b, global); + } + // A comment extension first, to skip. + b.extend([0x21, 0xFE, 3, b'h', b'i', b'!', 0]); + if let Some(i) = transparent { + b.extend([0x21, 0xF9, 4, 0x01, 0, 0, i, 0]); + } + b.extend([0x2C, 0, 0, 0, 0, 4, 0, 4, 0]); + match local { + Some(t) => { + b.push(0x80 | size_bits(t.len())); + pad(&mut b, t); + } + None => b.push(0), + } + b.extend([2, 2, 0x4C, 0x01, 0, 0x3B]); + b + } + + #[test] + fn the_first_frames_table_and_transparent_entry() { + let g = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]]; + assert_eq!(palette(&gif(&g, None, None)), Some((g.to_vec(), None))); + assert_eq!(palette(&gif(&g, Some(2), None)), Some((g.to_vec(), Some(2)))); + // A local table wins over the global one. + let l = [[1, 2, 3], [4, 5, 6]]; + assert_eq!(palette(&gif(&g, Some(1), Some(&l))), Some((l.to_vec(), Some(1)))); + // A transparent index past the table is no transparent entry. + assert_eq!(palette(&gif(&l, Some(9), None)), Some((l.to_vec(), None))); + } + + #[test] + fn no_table_or_not_a_gif_is_none() { + assert_eq!(palette(&gif(&[], None, None)), None, "no table at all"); + assert_eq!(palette(b"\x89PNG\r\n\x1a\n"), None); + assert_eq!(palette(b""), None); + let whole = gif(&[[9, 9, 9], [8, 8, 8]], Some(0), None); + for cut in 0..whole.len() - 6 { + // Cut short anywhere before the image descriptor's fields: never a panic. + let _ = palette(&whole[..cut]); + } + } +} diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs new file mode 100644 index 000000000..4fde27cf1 --- /dev/null +++ b/crates/image-codecs/src/codecs/heif.rs @@ -0,0 +1,126 @@ +//! HEIF / HEIC (the iPhone and Mac photo format), read-only: see [`crate::ASYMMETRIC_EXCEPTIONS`]. +//! +//! Decoding lives in the optional `craft-heif` crate (heic-rs, a pure-Rust HEVC +//! still-picture decoder), enabled by this crate's `heif` feature. Without it, HEIF files are +//! still detected and opening one is an [`CodecError::Unsupported`] error, never a panic. +//! +//! HEIF records orientation in the container (`irot`/`imir`, plus a `clap` crop), not in EXIF, +//! so those are applied by the decoder and the EXIF Orientation is rewritten to 1. Its EXIF tag +//! only mirrors what the container already says, and applying it as well would turn the photo +//! twice. + +use crate::Format; +use crate::error::CodecError; +use crate::image::Image; +use crate::options::Limits; + +const F: Format = Format::Heif; + +/// The reason given when this build has no HEIF decoder. +#[cfg(not(feature = "heif"))] +pub(crate) const NOT_IN_BUILD: &str = "HEIC/HEIF support isn't included in this build"; + +#[cfg(not(feature = "heif"))] +pub(crate) fn decode(_bytes: &[u8], _limits: &Limits, _keep_orientation: bool) -> Result { + Err(CodecError::unsupported(F, NOT_IN_BUILD)) +} + +#[cfg(feature = "heif")] +fn err(e: craft_heif::Error) -> CodecError { + match e { + craft_heif::Error::Unsupported(what) => CodecError::unsupported(F, what), + craft_heif::Error::Limit(what) => CodecError::LimitExceeded(what), + craft_heif::Error::Malformed(what) => CodecError::malformed(F, what), + } +} + +#[cfg(feature = "heif")] +pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> Result { + use crate::cicp::Cicp; + use crate::image::{ChannelLayout, Metadata, SampleType}; + + // The container alone: the declared size is checked before any pixel is decoded. + let info = craft_heif::probe(bytes).map_err(err)?; + let layout = if info.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let sample = if info.bit_depth > 8 { SampleType::U16 } else { SampleType::U8 }; + let (w, h) = if keep_orientation { (info.coded_width, info.coded_height) } else { (info.width, info.height) }; + limits.check(w, h, layout, sample)?; + // craft-heif returns 16-bit samples for every coded depth. Bound that intermediate, including + // the coded canvas before any clean-aperture crop or orientation transform. + limits.check(info.coded_width, info.coded_height, layout, SampleType::U16)?; + // The delegated decoder allocates the full coded image before applying any clean-aperture + // crop or orientation transform, so the coded dimensions must pass the limit policy even + // when the displayed (transformed) size is small. Checking only the transformed size let a + // tiny visible image hide a large coded allocation. + if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) { + limits.check(info.coded_width, info.coded_height, layout, sample)?; + } + let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, ..Default::default() }; + let decoded = craft_heif::decode(bytes, &options).map_err(err)?; + // The decoder re-reads the container; trust its output's shape, not the probe's. + let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let sample = if decoded.bit_depth > 8 { SampleType::U16 } else { SampleType::U8 }; + let image = Image::from_u16(decoded.width, decoded.height, layout, &decoded.data)?; + let mut img = if sample == SampleType::U16 { image } else { image.convert(layout, SampleType::U8) }; + img.icc = decoded.icc; + let colour = decoded.colour; + img.cicp = Some(Cicp { primaries: colour.primaries, transfer: colour.transfer, matrix: colour.matrix, full_range: colour.full_range }); + let (exif, xmp) = craft_heif::read_metadata(bytes).map_err(err)?; + img.meta = Metadata { + exif: exif.map(|e| if keep_orientation { e } else { crate::orientation::upright_exif(&e).into_owned() }), + xmp: xmp.map(|x| if keep_orientation { x } else { crate::orientation::upright_xmp(&x).into_owned() }), + ..Default::default() + }; + // A 10-bit HDR HEIC (BT.2100 HLG or PQ) opens as linear light in 32-bit float (#6752): no + // ICC curve can describe those transfers, and an 8/16-bit document would clip the highlights. + match img.cicp.filter(|c| c.is_hdr()) { + Some(c) => { + limits.check(decoded.width, decoded.height, layout, SampleType::F32)?; + crate::cicp::linearize(img, c) + } + None => Ok(img), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(not(feature = "heif"))] + #[test] + fn without_the_feature_heif_is_a_clear_unsupported_error() { + let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; + assert_eq!(crate::detect(header), Some(F), "still recognised"); + let r = crate::decode(header); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), "{r:?}"); + assert!(!crate::caps(F).read); + } + + #[cfg(feature = "heif")] + #[test] + fn with_the_feature_heif_is_readable_and_errors_map() { + assert!(crate::caps(F).read); + let r = crate::decode(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("sequence")), "{r:?}"); + assert!(matches!(crate::decode_as(F, b""), Err(CodecError::Malformed { .. } | CodecError::Unsupported { .. }))); + } + + /// Found by the `decode_heif` fuzz target: a malformed box makes heic-rs 0.1.1 slice out of range + /// (`boxes.rs:130`, "slice index starts at 24 but ends at 16"). It must be an error, not a crash. + #[cfg(feature = "heif")] + const HEIC_RS_BOX_PANIC: [u8; 72] = [ + 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, 0x72, 0x6d, + 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, 0x00, 0xa8, 0x00, + ]; + + #[cfg(feature = "heif")] + #[test] + fn a_heic_rs_panic_is_a_malformed_file_error() { + assert_eq!(crate::detect(&HEIC_RS_BOX_PANIC), Some(Format::Heif)); + for opts in [crate::DecodeOptions::default(), crate::DecodeOptions { keep_orientation: true, ..Default::default() }] { + let r = crate::decode_with(&HEIC_RS_BOX_PANIC, &opts); + assert!(matches!(r, Err(CodecError::Malformed { format: Format::Heif, .. })), "{r:?}"); + } + } +} diff --git a/crates/image-codecs/src/codecs/iff.rs b/crates/image-codecs/src/codecs/iff.rs new file mode 100644 index 000000000..4997817ca --- /dev/null +++ b/crates/image-codecs/src/codecs/iff.rs @@ -0,0 +1,544 @@ +//! Amiga IFF/ILBM reader and writer (EA "Interchange File Format", "InterLeaved BitMap" and the +//! chunky "PBM " variant). Written from the published IFF/ILBM layout: a `FORM` chunk of four-byte +//! chunks (`BMHD` bitmap header, `CMAP` palette, `CAMG` Amiga viewport mode, `BODY` pixel data). +//! +//! Read: 1–8 bitplanes with a palette (or a gray ramp when there is none) with masking 0/1/2, and +//! 24-bitplane "deep" ILBMs (8 planes each of red, green and blue, least significant bit first, +//! no palette) with masking 0/1, both stored (0) and ByteRun1/PackBits (1) compressed. Anything +//! else is a clear `Unsupported`/`Malformed` error, never a panic. +//! +//! Write: a 24-bitplane ILBM, ByteRun1 compressed one plane row at a time, with a mask plane +//! (masking 1) when the image has alpha. The mask plane is one bit per pixel: alpha of 128 or +//! more is opaque. + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +const F: Format = Format::Iff; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +fn unsupported(msg: impl Into) -> CodecError { + CodecError::unsupported(F, msg) +} + +fn be32(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(4)?)?.try_into().ok()?; + Some(u32::from_be_bytes(s)) +} + +fn be16(b: &[u8], at: usize) -> Option { + let s = b.get(at..at.checked_add(2)?)?.try_into().ok()?; + Some(u16::from_be_bytes(s)) +} + +/// `FORM` + a bitmap type (`ILBM` interleaved, `PBM ` chunky) at offset 8. +pub(crate) fn looks_like_iff(b: &[u8]) -> bool { + b.get(0..4) == Some(b"FORM") && matches!(b.get(8..12), Some(b"ILBM" | b"PBM ")) +} + +/// Bitplanes of a "deep" ILBM: red, green and blue, 8 each, least significant bit first. +const DEEP_PLANES: u8 = 24; + +/// The bitmap header fields decoding needs. +struct Header { + width: u32, + height: u32, + planes: u8, + masking: u8, + compression: u8, + transparent: u16, + /// `PBM ` stores one palette index per byte, padded to even row lengths. + chunky: bool, +} + +/// One four-byte chunk's id and byte range. +struct Chunk<'a> { + id: [u8; 4], + data: &'a [u8], +} + +/// Walk the `FORM`'s chunks from offset 12 to `end` (the form length), stopping at the first +/// malformed length so a hostile file can't run past the buffer. +fn chunks(b: &[u8]) -> impl Iterator> { + let end = be32(b, 4).map_or(b.len(), |n| (n as usize).saturating_add(8).min(b.len())); + let mut pos = 12usize; + std::iter::from_fn(move || { + if pos + 8 > end { + return None; + } + let id = b.get(pos..pos + 4)?.try_into().ok()?; + let len = be32(b, pos + 4)? as usize; + let start = pos + 8; + let data = b.get(start..start.checked_add(len)?)?; + // Chunks are padded to an even length. + pos = start.checked_add(len)?.checked_add(len & 1)?; + Some(Chunk { id, data }) + }) +} + +fn parse_header(b: &[u8], chunky: bool) -> Result { + let bmhd = chunks(b).find(|c| &c.id == b"BMHD").ok_or_else(|| bad("no BMHD chunk"))?; + let d = bmhd.data; + if d.len() < 20 { + return Err(bad("BMHD chunk is too short")); + } + let width = u32::from(be16(d, 0).ok_or_else(|| bad("BMHD width"))?); + let height = u32::from(be16(d, 2).ok_or_else(|| bad("BMHD height"))?); + let planes = *d.get(8).ok_or_else(|| bad("BMHD planes"))?; + let masking = *d.get(9).ok_or_else(|| bad("BMHD masking"))?; + let compression = *d.get(10).ok_or_else(|| bad("BMHD compression"))?; + if width == 0 || height == 0 { + return Err(bad("zero-sized bitmap")); + } + if !(1..=8).contains(&planes) && planes != DEEP_PLANES { + return Err(unsupported(format!("{planes} bitplanes (1–8 with a palette, or 24)"))); + } + if masking > 2 { + return Err(unsupported(format!("masking {masking}"))); + } + if planes == DEEP_PLANES && masking == 2 { + return Err(unsupported("a transparent color in a 24-bitplane ILBM")); + } + if compression > 1 { + return Err(unsupported(format!("compression {compression} (only stored and ByteRun1)"))); + } + if chunky && (planes != 8 || masking == 1) { + return Err(unsupported("PBM requires 8-bit indices without a mask plane")); + } + let transparent = be16(d, 12).ok_or_else(|| bad("BMHD transparent colour"))?; + Ok(Header { width, height, planes, masking, compression, transparent, chunky }) +} + +/// ByteRun1 / PackBits decode of one plane row into `out`; returns the bytes consumed. A `0x80` +/// control byte is a no-op, as the layout specifies. +fn unpack_byte_run1(src: &[u8], out: &mut [u8]) -> Result { + let (mut i, mut o) = (0usize, 0usize); + while o < out.len() { + let n = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 data"))? as i8; + i += 1; + if n >= 0 { + let count = n as usize + 1; + let lit = src.get(i..i.checked_add(count).ok_or_else(|| bad("ByteRun1 overflow"))?).ok_or_else(|| bad("truncated ByteRun1 run"))?; + if count > out.len() - o { + return Err(bad("ByteRun1 run crosses the plane row")); + } + out[o..o + count].copy_from_slice(lit); + i += count; + o += count; + } else if n != -128 { + let count = (1 - n as i32) as usize; + let value = *src.get(i).ok_or_else(|| bad("truncated ByteRun1 repeat"))?; + i += 1; + if count > out.len() - o { + return Err(bad("ByteRun1 repeat crosses the plane row")); + } + out[o..o + count].fill(value); + o += count; + } + } + Ok(i) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_iff(b) { + return Err(bad("not an IFF/ILBM file (no FORM ... ILBM)")); + } + let h = parse_header(b, b.get(8..12) == Some(b"PBM "))?; + if let Some(camg) = chunks(b).find(|c| &c.id == b"CAMG") { + let mode = be32(camg.data, 0).ok_or_else(|| bad("short CAMG chunk"))?; + if mode & (0x80 | 0x800) != 0 { + return Err(unsupported("EHB and HAM viewport modes")); + } + } + let layout = if h.masking == 0 { ChannelLayout::Rgb } else { ChannelLayout::Rgba }; + limits.check(h.width, h.height, layout, SampleType::U8)?; + + // Palette: three bytes per colour; missing entries read as black. + let cmap = chunks(b).find(|c| &c.id == b"CMAP").map(|c| c.data).filter(|p| !p.is_empty()); + let colors = 1usize << h.planes; + let palette = |idx: usize| -> [u8; 3] { + if let Some(p) = cmap { + p.get(idx * 3..idx * 3 + 3).map(|c| [c[0], c[1], c[2]]).unwrap_or([0; 3]) + } else { + let v = ((idx as u32 * 255) / (colors as u32 - 1).max(1)) as u8; + [v, v, v] + } + }; + + let body = chunks(b).find(|c| &c.id == b"BODY").map(|c| c.data).ok_or_else(|| bad("no BODY chunk"))?; + let (w, hh) = (h.width as usize, h.height as usize); + // Each plane row is stored as a whole number of 16-bit words. + let plane_row = if h.chunky { w.div_ceil(2) * 2 } else { w.div_ceil(16) * 2 }; + // A masking type 1 bitmap carries one extra (mask) plane per row. + let rows_planes: usize = if h.chunky { 1 } else { usize::from(h.planes) + usize::from(h.masking == 1) }; + + // Decode every stored plane row up front, then recombine. + let total_rows = hh.checked_mul(rows_planes).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; + let total_bytes = total_rows.checked_mul(plane_row).ok_or_else(|| CodecError::LimitExceeded("bitmap too large".into()))?; + if total_bytes as u64 > limits.max_alloc { + return Err(CodecError::LimitExceeded("bitmap storage exceeds max_alloc".into())); + } + Limits::check_plausible(total_bytes as u64, body.len(), if h.compression == 0 { 1 } else { 128 })?; + let mut stored = crate::options::alloc_zeroed::(total_bytes)?; + let mut cursor = 0usize; + for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) { + match h.compression { + 0 => { + let src = body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; + let n = row.len().min(src.len()); + row[..n].copy_from_slice(&src[..n]); + cursor += plane_row; + } + _ => { + let tail = body.get(cursor..).ok_or_else(|| bad("truncated BODY data"))?; + cursor += unpack_byte_run1(tail, row)?; + } + } + } + + let mut out = Vec::new(); + out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + let planes = usize::from(h.planes); + for y in 0..hh { + for x in 0..w { + let index = if h.chunky { + usize::from(*stored.get(y * plane_row + x).ok_or_else(|| bad("PBM pixel outside BODY"))?) + } else { + (0..planes).filter(|&p| bit_at(&stored, (y * rows_planes + p) * plane_row, x)).fold(0usize, |acc, p| acc | (1 << p)) + }; + let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) }; + out.extend_from_slice(&c); + if h.masking != 0 { + let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; + out.push(if opaque { 255 } else { 0 }); + } + } + } + Image::from_u8(h.width, h.height, layout, out) +} + +/// The bit for pixel `x` of a stored plane row at `base` (bits run MSB-first). +fn bit_at(stored: &[u8], base: usize, x: usize) -> bool { + stored.get(base + x / 8).is_some_and(|byte| byte & (0x80 >> (x % 8)) != 0) +} + +/// ByteRun1 / PackBits encode of one plane row, appended to `out`: three or more equal bytes +/// become a repeat (at most 128), everything else literal runs (at most 128 bytes). +fn pack_byte_run1(row: &[u8], out: &mut Vec) { + let mut i = 0usize; + while let Some(&value) = row.get(i) { + let repeat = row[i..].iter().take(128).take_while(|&&b| b == value).count(); + if repeat >= 3 { + out.push((1 - repeat as i32) as i8 as u8); + out.push(value); + i += repeat; + continue; + } + let start = i; + while i < row.len() && i - start < 128 { + if row.get(i..i + 3).is_some_and(|r| r[0] == r[1] && r[1] == r[2]) { + break; + } + i += 1; + } + out.push((i - start - 1) as u8); + out.extend_from_slice(&row[start..i]); + } +} + +fn push_chunk(out: &mut Vec, id: &[u8; 4], data: &[u8]) -> Result<(), CodecError> { + let len = u32::try_from(data.len()).map_err(|_| CodecError::encode(F, "chunk too large for an IFF file (4 GiB)"))?; + out.extend_from_slice(id); + out.extend_from_slice(&len.to_be_bytes()); + out.extend_from_slice(data); + // Chunks are padded to an even length. + if data.len() % 2 == 1 { + out.push(0); + } + Ok(()) +} + +/// Writes a 24-bitplane `FORM ILBM`: `BMHD` then a ByteRun1-compressed `BODY`, each row stored as +/// the red, green and blue planes (least significant bit first) and, for RGBA, a mask plane. +pub(crate) fn encode(src: &Image, plan: Plan) -> Result, CodecError> { + let enc = |msg: String| CodecError::encode(F, msg); + let img = src.converted(plan.layout, plan.sample); + let masked = match img.layout() { + ChannelLayout::Rgb => false, + ChannelLayout::Rgba => true, + l => return Err(enc(format!("IFF/ILBM cannot store {l:?}"))), + }; + if img.sample_type() != SampleType::U8 { + return Err(enc(format!("IFF/ILBM cannot store {:?} samples", img.sample_type()))); + } + let (w, h) = img.dimensions(); + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { + return Err(enc("IFF/ILBM stores at most 65535 × 65535 pixels".into())); + }; + let (w, ch) = (w as usize, img.layout().channels()); + let data = img.data(); + let row_len = w.checked_mul(ch).ok_or_else(|| enc("image too large".into()))?; + if data.len() != row_len.checked_mul(h as usize).ok_or_else(|| enc("image too large".into()))? { + return Err(enc("the image has the wrong number of samples".into())); + } + let planes = usize::from(DEEP_PLANES) + usize::from(masked); + let mut plane_row = vec![0u8; w.div_ceil(16) * 2]; + let mut body = Vec::new(); + for px in data.chunks_exact(row_len.max(1)) { + for plane in 0..planes { + plane_row.fill(0); + for (x, p) in px.chunks_exact(ch).enumerate() { + let on = if plane == usize::from(DEEP_PLANES) { p[3] >= 128 } else { (p[plane / 8] >> (plane % 8)) & 1 == 1 }; + if on { + plane_row[x / 8] |= 0x80 >> (x % 8); + } + } + pack_byte_run1(&plane_row, &mut body); + } + } + + let mut bmhd = Vec::with_capacity(20); + bmhd.extend_from_slice(&w16.to_be_bytes()); + bmhd.extend_from_slice(&h16.to_be_bytes()); + bmhd.extend_from_slice(&[0, 0, 0, 0]); // x/y origin + bmhd.push(DEEP_PLANES); + bmhd.push(u8::from(masked)); // masking: 1 = a mask plane follows the color planes + bmhd.push(1); // ByteRun1 + bmhd.push(0); // pad + bmhd.extend_from_slice(&[0, 0]); // transparent color (unused) + bmhd.extend_from_slice(&[1, 1]); // square pixels + bmhd.extend_from_slice(&w16.to_be_bytes()); // page size + bmhd.extend_from_slice(&h16.to_be_bytes()); + + let mut out = b"FORM".to_vec(); + out.extend_from_slice(&[0; 4]); // patched below + out.extend_from_slice(b"ILBM"); + push_chunk(&mut out, b"BMHD", &bmhd)?; + push_chunk(&mut out, b"BODY", &body)?; + let form_len = u32::try_from(out.len() - 8).map_err(|_| enc("image too large for an IFF file (4 GiB)".into()))?; + out[4..8].copy_from_slice(&form_len.to_be_bytes()); + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A `FORM ILBM` with a `BMHD`, `CMAP` and `BODY`. `planes` plane rows per row, + /// `compression` 0 (stored) or 1 (ByteRun1), MSB-first. + fn ilbm(w: u16, h: u16, planes: u8, palette: &[[u8; 3]], compression: u8, body: &[u8]) -> Vec { + let mut b = b"FORM".to_vec(); + b.extend_from_slice(&0u32.to_be_bytes()); // patched below + b.extend_from_slice(b"ILBM"); + // BMHD (20 bytes) + b.extend_from_slice(b"BMHD"); + b.extend_from_slice(&20u32.to_be_bytes()); + b.extend_from_slice(&w.to_be_bytes()); + b.extend_from_slice(&h.to_be_bytes()); + b.extend_from_slice(&[0, 0, 0, 0]); // x/y origin + b.push(planes); + b.push(0); // masking + b.push(compression); + b.push(0); // pad + b.extend_from_slice(&[0, 0]); // transparent colour + b.extend_from_slice(&[1, 1]); // aspect + b.extend_from_slice(&[0, 0, 0, 0]); // page size + // CMAP + b.extend_from_slice(b"CMAP"); + b.extend_from_slice(&((palette.len() * 3) as u32).to_be_bytes()); + for c in palette { + b.extend_from_slice(c); + } + if palette.len() % 2 == 1 { + b.push(0); + } + // BODY + b.extend_from_slice(b"BODY"); + b.extend_from_slice(&(body.len() as u32).to_be_bytes()); + b.extend_from_slice(body); + if body.len() % 2 == 1 { + b.push(0); + } + let size = (b.len() - 8) as u32; + b[4..8].copy_from_slice(&size.to_be_bytes()); + b + } + + #[test] + fn reads_a_stored_two_plane_bitmap() { + // 2×1, 2 planes: pixel 0 index 0 (plane bits 0,0), pixel 1 index 3 (bits 1,1). + // Plane 0 = 0b0100_0000, plane 1 = 0b0100_0000, each padded to 2 bytes. + let body = [0b0100_0000, 0, 0b0100_0000, 0]; + let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90], [100, 110, 120]]; + let b = ilbm(2, 1, 2, &pal, 0, &body); + assert!(looks_like_iff(&b)); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!(img.data(), &[10, 20, 30, 100, 110, 120]); + } + + #[test] + fn reads_byte_run1_compressed_rows() { + // Same image, ByteRun1: literal run of two bytes (0x01) then the two plane bytes each row. + let body = [0x01, 0b0100_0000, 0, 0x01, 0b0100_0000, 0]; + let pal = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [250, 251, 252]]; + let b = ilbm(2, 1, 2, &pal, 1, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[1, 2, 3, 250, 251, 252]); + } + + #[test] + fn no_palette_falls_back_to_a_grey_ramp() { + // 2×1, 1 plane, no CMAP: indices 0 and 1 → black and white. + let body = [0b0100_0000, 0]; + let b = ilbm(2, 1, 1, &[], 0, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[0, 0, 0, 255, 255, 255]); + } + + #[test] + fn pbm_reads_chunky_indices_with_odd_width_padding() { + let pal = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]; + for (compression, body) in [(0, vec![2, 0, 1, 255, 1, 2, 0, 255]), (1, vec![3, 2, 0, 1, 255, 3, 1, 2, 0, 255])] { + let mut b = ilbm(3, 2, 8, &pal, compression, &body); + b[8..12].copy_from_slice(b"PBM "); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.data(), &[70, 80, 90, 10, 20, 30, 40, 50, 60, 40, 50, 60, 70, 80, 90, 10, 20, 30]); + } + } + + #[test] + fn transparency_uses_the_mask_plane_or_transparent_index() { + for masking in [1, 2] { + let mut b = ilbm(2, 1, 1, &[[10, 20, 30], [40, 50, 60]], 0, if masking == 1 { &[0x40, 0, 0x40, 0] } else { &[0x40, 0] }); + b[29] = masking; + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.data(), &[10, 20, 30, 0, 40, 50, 60, 255]); + } + let mut b = ilbm(2, 1, 8, &[[10, 20, 30], [40, 50, 60]], 0, &[0, 1]); + b[8..12].copy_from_slice(b"PBM "); + b[29] = 2; + b[32..34].copy_from_slice(&1u16.to_be_bytes()); + assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[10, 20, 30, 255, 40, 50, 60, 0]); + } + + #[test] + fn rejects_ham_and_ehb_instead_of_returning_wrong_colours() { + for mode in [0x80u32, 0x800] { + let mut b = ilbm(1, 1, 1, &[], 0, &[0, 0]); + b.extend_from_slice(b"CAMG"); + b.extend_from_slice(&4u32.to_be_bytes()); + b.extend_from_slice(&mode.to_be_bytes()); + let len = (b.len() - 8) as u32; + b[4..8].copy_from_slice(&len.to_be_bytes()); + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + } + + #[test] + fn missing_palette_entries_are_black() { + let b = ilbm(1, 1, 2, &[[10, 20, 30]], 0, &[0x80, 0, 0x80, 0]); + assert_eq!(decode(&b, &Limits::default()).unwrap().data(), &[0, 0, 0]); + } + + fn plan(layout: ChannelLayout) -> Plan { + Plan { layout, sample: SampleType::U8 } + } + + #[test] + fn reads_a_stored_24_plane_bitmap() { + // 2×1: pixel 0 = (1, 0, 128), pixel 1 = (0, 255, 0). Each plane row is 2 bytes; red bit 0 + // has pixel 0 set, green bits 0–7 have pixel 1 set, blue bit 7 has pixel 0 set. + let mut body = Vec::new(); + for plane in 0..24 { + let byte = match plane { + 0 | 23 => 0x80, + 8..=15 => 0x40, + _ => 0, + }; + body.extend_from_slice(&[byte, 0]); + } + let b = ilbm(2, 1, 24, &[], 0, &body); + let img = decode(&b, &Limits::default()).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (2, 1, ChannelLayout::Rgb)); + assert_eq!(img.data(), &[1, 0, 128, 0, 255, 0]); + } + + #[test] + fn writes_24_plane_byte_run1_that_reads_back_exactly() { + // 37 pixels wide: three plane-row words, the last one partly padding. + let (w, h) = (37u32, 3u32); + let data: Vec = (0..w * h * 3).map(|i| (i * 7 % 256) as u8).collect(); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, data.clone()).unwrap(); + let bytes = encode(&img, plan(ChannelLayout::Rgb)).unwrap(); + assert!(looks_like_iff(&bytes)); + assert_eq!(be32(&bytes, 4).unwrap() as usize, bytes.len() - 8, "FORM length"); + let h = parse_header(&bytes, false).unwrap(); + assert_eq!((h.width, h.height, h.planes, h.masking, h.compression), (37, 3, 24, 0, 1)); + assert!(chunks(&bytes).all(|c| &c.id != b"CMAP"), "a deep ILBM has no palette"); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgb); + assert_eq!(back.data(), &data[..]); + } + + #[test] + fn alpha_is_written_as_a_mask_plane() { + let data = [10, 20, 30, 0, 40, 50, 60, 127, 70, 80, 90, 128, 1, 2, 3, 255]; + let img = Image::from_u8(4, 1, ChannelLayout::Rgba, data.to_vec()).unwrap(); + let bytes = encode(&img, plan(ChannelLayout::Rgba)).unwrap(); + assert_eq!(parse_header(&bytes, false).unwrap().masking, 1); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgba); + assert_eq!(back.data(), &[10, 20, 30, 0, 40, 50, 60, 0, 70, 80, 90, 255, 1, 2, 3, 255]); + } + + #[test] + fn byte_run1_round_trips_runs_literals_and_long_rows() { + let mut rows: Vec> = vec![vec![], vec![5], vec![5, 5], vec![5, 5, 5], vec![1, 2, 2, 2, 3], vec![9; 300]]; + rows.push((0..300).map(|i| (i % 251) as u8).collect()); + rows.push([vec![1, 2], vec![7; 129], vec![3]].concat()); + for row in rows { + let mut packed = Vec::new(); + pack_byte_run1(&row, &mut packed); + let mut back = vec![0; row.len()]; + assert_eq!(unpack_byte_run1(&packed, &mut back).unwrap(), packed.len(), "{row:?}"); + assert_eq!(back, row); + } + // A long repeat packs into two-byte runs. + let mut packed = Vec::new(); + pack_byte_run1(&[0; 256], &mut packed); + assert_eq!(packed, [0x81, 0, 0x81, 0]); + } + + #[test] + fn rejects_what_ilbm_cannot_store() { + let gray = Image::from_u8(1, 1, ChannelLayout::Gray, vec![0]).unwrap(); + assert!(encode(&gray, plan(ChannelLayout::Gray)).is_err()); + let wide = Image::from_u8(65536, 1, ChannelLayout::Rgb, vec![0; 65536 * 3]).unwrap(); + assert!(encode(&wide, plan(ChannelLayout::Rgb)).is_err()); + // A transparent color index has no meaning without a palette. + let mut b = ilbm(1, 1, 24, &[], 0, &[0; 48]); + b[29] = 2; + assert!(matches!(decode(&b, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } + + #[test] + fn hostile_input_errors_not_panics() { + // Truncations of a valid file never panic. + let body = [0b0100_0000, 0, 0b0100_0000, 0]; + let full = ilbm(2, 1, 2, &[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], 0, &body); + for cut in [0, 4, 8, 12, 40, full.len() - 1] { + assert!(decode(&full[..cut.min(full.len())], &Limits::default()).is_err(), "cut {cut}"); + } + // Too many planes is unsupported. + let bad = ilbm(1, 1, 9, &[], 0, &[0, 0]); + assert!(matches!(decode(&bad, &Limits::default()), Err(CodecError::Unsupported { .. }))); + } +} diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs new file mode 100644 index 000000000..e2e854622 --- /dev/null +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -0,0 +1,931 @@ +//! JPEG: decode via `zune-jpeg` (gray, YCbCr, CMYK, YCCK; arithmetic-coded and 12-bit files +//! via [`craft_jpeg`]), encode via +//! `jpeg-encoder` (gray, RGB, CMYK). Metadata (APP0 JFIF density, APP1 +//! EXIF/XMP, APP2 multi-segment ICC, APP13 Photoshop resources, APP14 Adobe) is +//! parsed by our own marker scanner so behaviour does not depend on decoder internals. +//! +//! **Resolution** (#1691) can be recorded four times; like Photoshop the first usable one of +//! these wins: the APP13 Photoshop ResolutionInfo, the XMP `tiff:` resolution, the EXIF IFD0 +//! resolution, and last the JFIF density (a unit-0 JFIF density is only an aspect ratio and +//! never counts). Cameras record their resolution in EXIF only, and many tools write a default +//! 72 into JFIF, so JFIF can't outrank EXIF (see [`crate::resolution`]). On export RGB and gray +//! images carry density in JFIF; CMYK omits JFIF APP0, which is invalid for four components, and +//! carries an available density in EXIF instead. + +use zune_core::bytestream::ZCursor; +use zune_core::colorspace::ColorSpace; +use zune_core::options::DecoderOptions; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, JpegFormat, Limits}; +use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; + +const F: Format = Format::Jpeg; +const EXIF_HEADER: &[u8] = b"Exif\0\0"; +const XMP_HEADER: &[u8] = b"http://ns.adobe.com/xap/1.0/\0"; +const ICC_HEADER: &[u8] = b"ICC_PROFILE\0"; +const PHOTOSHOP_HEADER: &[u8] = b"Photoshop 3.0\0"; + +#[derive(Default, Debug)] +pub(crate) struct JpegMeta { + pub icc: Option>, + pub exif: Option>, + pub xmp: Option, + pub dpi: Option<(f32, f32)>, + /// The APP13 Photoshop image resource blocks (`8BIM` ...), segments joined, without the header. + pub photoshop: Option>, + pub adobe_transform: Option, + /// (width, height, components) from the first SOFn. + pub frame: Option<(u32, u32, u8)>, +} + +/// Walk the marker segments up to SOS and collect metadata. +pub(crate) fn scan_metadata(b: &[u8]) -> JpegMeta { + let mut m = JpegMeta::default(); + let mut icc_parts: Vec<(u8, u8, &[u8])> = Vec::new(); + // Photoshop splits a large resource block over consecutive APP13 segments. + let mut photoshop: Vec = Vec::new(); + let mut jfif_dpi = None; + let mut i = 2; + while i + 4 <= b.len() { + if b[i] != 0xFF { + break; + } + let marker = b[i + 1]; + if marker == 0xFF { + i += 1; + continue; + } + if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { + i += 2; + continue; + } + if marker == 0xDA || marker == 0xD9 { + break; + } + let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize; + if len < 2 || i + 2 + len > b.len() { + break; + } + let seg = &b[i + 4..i + 2 + len]; + match marker { + 0xE0 if seg.len() >= 12 && seg.starts_with(b"JFIF\0") => { + let unit = seg[7]; + let x = u16::from_be_bytes([seg[8], seg[9]]) as f32; + let y = u16::from_be_bytes([seg[10], seg[11]]) as f32; + if x > 0.0 && y > 0.0 { + jfif_dpi = match unit { + 1 => Some((x, y)), + 2 => Some((x * 2.54, y * 2.54)), + _ => None, + }; + } + } + 0xE1 if seg.starts_with(EXIF_HEADER) && m.exif.is_none() => { + m.exif = Some(seg[EXIF_HEADER.len()..].to_vec()); + } + 0xE1 if seg.starts_with(XMP_HEADER) && m.xmp.is_none() => { + m.xmp = std::str::from_utf8(&seg[XMP_HEADER.len()..]).ok().map(|s| s.trim_end_matches('\0').to_owned()); + } + 0xED if seg.starts_with(PHOTOSHOP_HEADER) && photoshop.len() < 1 << 24 => { + photoshop.extend_from_slice(&seg[PHOTOSHOP_HEADER.len()..]); + } + 0xE2 if seg.len() >= 14 && seg.starts_with(ICC_HEADER) => { + icc_parts.push((seg[12], seg[13], &seg[14..])); + } + 0xC0..=0xCF if !matches!(marker, 0xC4 | 0xC8 | 0xCC) && seg.len() >= 6 && m.frame.is_none() => { + let h = u16::from_be_bytes([seg[1], seg[2]]) as u32; + let w = u16::from_be_bytes([seg[3], seg[4]]) as u32; + m.frame = Some((w, h, seg[5])); + } + 0xEE if seg.len() >= 12 && seg.starts_with(b"Adobe") => { + m.adobe_transform = Some(seg[11]); + } + _ => {} + } + i += 2 + len; + } + m.dpi = photoshop_resolution(&photoshop) + .or_else(|| m.xmp.as_deref().and_then(xmp_resolution)) + .or_else(|| m.exif.as_deref().and_then(exif_resolution)) + .or(jfif_dpi); + m.photoshop = Some(photoshop).filter(|p| !p.is_empty()); + if !icc_parts.is_empty() { + icc_parts.sort_by_key(|p| p.0); + let total = icc_parts[0].1 as usize; + let seqs_ok = icc_parts.len() == total && icc_parts.iter().enumerate().all(|(k, p)| p.0 as usize == k + 1 && p.1 as usize == total); + if seqs_ok || icc_parts.len() == 1 { + m.icc = Some(icc_parts.iter().flat_map(|p| p.2.iter().copied()).collect()); + } + } + m +} + +/// How a JPEG's data ends, seen from its marker structure. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum DataEnd { + /// An end-of-image marker follows the scans (or the structure is too odd to tell). + Complete, + /// The file ends inside the image data, before the end-of-image marker. + Truncated, + /// The file ends before the first scan holds any data: nothing can be decoded. + Empty, +} + +/// Walks the marker segments (skipped by their length) and the entropy-coded scan data +/// (skipped up to the next marker that isn't a stuffed `FF 00`, a restart or a fill byte), +/// so an embedded thumbnail's end marker or data after the end of the image don't count. +/// Also returns how many scan-data bytes the walk crossed: the truncated-input budget is +/// measured on these, not on the file length, which APP padding can inflate without +/// adding image data. +fn data_end(b: &[u8]) -> (DataEnd, u64) { + // Where the bytes run out: inside the image data once a scan has held some. + let cut = |scan_data: bool, scanned: u64| (if scan_data { DataEnd::Truncated } else { DataEnd::Empty }, scanned); + let mut i = 2; // past SOI + let mut scan_data = false; + let mut scanned = 0u64; + loop { + let Some(&byte) = b.get(i) else { return cut(scan_data, scanned) }; + if byte != 0xFF { + // Not where a marker should be: skip to the next one, as a lenient decoder does, so + // segments after the stray bytes (padding among them) are still recognised. Stray + // bytes are not scan data, so they never count (junk would buy budget as padding + // did); with no marker left, the image data ends here. + let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { return cut(scan_data, scanned) }; + i += p; + continue; + } + let Some(&code) = b.get(i + 1) else { return cut(scan_data, scanned) }; + match code { + 0xD9 => return (DataEnd::Complete, scanned), + 0xFF => i += 1, + 0x01 | 0xD0..=0xD8 => i += 2, + _ => { + let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { + return cut(scan_data, scanned); + }; + i = i.saturating_add(2 + usize::from(len)); + if code == 0xDA { + let start = i; + loop { + let Some(p) = b.get(i..).and_then(|r| r.iter().position(|&v| v == 0xFF)) else { + // EOF inside this scan: everything from the scan start to the end + // of the file is scan data (inside a declared segment it may not + // be, so only count what remains). + return cut(scan_data || b.len() > start, scanned + u64::try_from(b.len().saturating_sub(start)).unwrap_or(0)); + }; + i += p; + match b.get(i + 1) { + Some(0x00 | 0xD0..=0xD7) => i += 2, + Some(0xFF) => i += 1, + _ => break, + } + } + scan_data |= i > start; + scanned += u64::try_from(i - start).unwrap_or(0); + } + } + } + } +} + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +const EXTENDED_XMP_HEADER: &[u8] = b"http://ns.adobe.com/xmp/extension/\0"; + +/// The file without its Extended XMP APP1 segments (`None` when it has none), for `zune-jpeg`. +/// +/// zune-jpeg keeps every Extended XMP fragment until a GUID's set is complete and re-sorts and +/// re-checks all pending fragments after every marker it parses, so many small, consistent but +/// incomplete fragments (or a few followed by many other markers) cost quadratic time before a +/// pixel is decoded. Nothing here reads its reassembled Extended XMP ([`scan_metadata`] works on +/// the original bytes), so the fragments are left out of what it parses. The walk stops at the +/// first scan, as the header parse does, and skips stray bytes to the next `FF` as it does. +fn without_extended_xmp(b: &[u8]) -> Option> { + let mut drop: Vec> = Vec::new(); + let mut i = 2; // past SOI + while let Some(&byte) = b.get(i) { + if byte != 0xFF { + i += b.get(i..)?.iter().position(|&v| v == 0xFF)?; + continue; + } + let Some(&code) = b.get(i + 1) else { break }; + match code { + 0xFF => i += 1, + 0x01 | 0xD0..=0xD8 => i += 2, + 0xD9 | 0xDA => break, + _ => { + let Some(len) = b.get(i + 2..i + 4).and_then(|s| <[u8; 2]>::try_from(s).ok()).map(u16::from_be_bytes) else { break }; + let end = i.saturating_add(2 + usize::from(len)); + if code == 0xE1 && b.get(i + 4..).is_some_and(|p| p.starts_with(EXTENDED_XMP_HEADER)) { + drop.push(i..end.min(b.len())); + } + i = end; + } + } + } + if drop.is_empty() { + return None; + } + // Reserve what is kept, not the input: the dropped fragments may be most of it. + let kept = b.len().saturating_sub(drop.iter().map(|r| r.end - r.start).sum::()); + let mut out = Vec::with_capacity(kept); + let mut at = 0; + for r in drop { + out.extend_from_slice(b.get(at..r.start)?); + at = r.end; + } + out.extend_from_slice(b.get(at..)?); + Some(out) +} + +/// Whether `craft-jpeg` (rather than `zune-jpeg`) decodes the file: arithmetic coding +/// (SOF9–SOF11) or a DCT frame whose precision isn't 8 bits (#4934, #5003). Lossless (SOF3) and +/// 12-bit progressive Huffman (SOF2) go there too: craft-jpeg reports them unsupported, where +/// zune-jpeg's failure would read as a damaged file. +fn uses_craft_jpeg(bytes: &[u8]) -> bool { + match craft_jpeg::frame(bytes) { + Some(f) if (0xC9..=0xCB).contains(&f.marker) || f.marker == 0xC3 => true, + Some(f) if matches!(f.marker, 0xC0..=0xC2) => f.precision != 8, + _ => false, + } +} + +fn craft_jpeg_error(e: craft_jpeg::Error) -> CodecError { + match e { + craft_jpeg::Error::Malformed(m) => err(m), + craft_jpeg::Error::Unsupported(m) => CodecError::unsupported(F, m), + craft_jpeg::Error::LimitExceeded(m) => CodecError::LimitExceeded(m), + e => err(e), + } +} + +/// Decode a file [`uses_craft_jpeg`] routes away from `zune-jpeg`; `damaged` says entropy-coded +/// data was cut short or corrupt. +fn decode_craft_jpeg(bytes: &[u8], limits: &Limits, adobe_transform: Option) -> Result<(Image, bool), CodecError> { + let mut lim = craft_jpeg::Limits::default(); + lim.max_pixels = limits.max_pixels; + lim.max_alloc = limits.max_alloc; + let d = craft_jpeg::decode(bytes, &lim, adobe_transform).map_err(craft_jpeg_error)?; + let layout = match d.components { + 1 => ChannelLayout::Gray, + 3 => ChannelLayout::Rgb, + 4 => ChannelLayout::Cmyk, + n => return Err(err(format!("{n} components"))), + }; + let img = match d.samples { + craft_jpeg::Samples::U8(px) => Image::from_raw(d.width, d.height, layout, SampleType::U8, px)?, + craft_jpeg::Samples::U16(px) => Image::from_u16(d.width, d.height, layout, &px)?, + }; + Ok((img, d.damaged)) +} + +/// Most pixels one byte of a JPEG may account for. Every 8×8 block of a frame needs at least a +/// one-bit DC code (512 pixels a byte for a flat image under ideal Huffman tables), and real files, +/// truncated or not, are far above this. +const MAX_PIXELS_PER_BYTE: u64 = 2048; + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (end, scan_bytes) = data_end(bytes); + if end == DataEnd::Empty { + return Err(err("the file ends before any image data")); + } + let meta = scan_metadata(bytes); + if let Some((w, h, nc)) = meta.frame { + limits.check_bytes(w, h, u64::from(nc.max(1)))?; + } + if uses_craft_jpeg(bytes) { + // craft-jpeg sizes its buffers from the frame header before it reads a scan. + if scan_bytes == 0 { + return Err(err("the file has no scan data")); + } + if let Some((w, h, _)) = meta.frame + && end == DataEnd::Truncated + // Only truncated scans: arithmetic coding (SOF9–SOF11) can spend well under a bit on + // a flat block, so a complete scan may be shorter than this bound. + && u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes + { + return Err(err("the truncated scan is too short for the frame it declares")); + } + let (mut img, damaged) = decode_craft_jpeg(bytes, limits, meta.adobe_transform)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated || damaged { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + return Ok(img); + } + let options = DecoderOptions::default().set_strict_mode(false).set_max_width(65535).set_max_height(65535); + let stripped = without_extended_xmp(bytes); + let mut dec = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(stripped.as_deref().unwrap_or(bytes)), options); + dec.decode_headers().map_err(err)?; + let (w, h) = dec.dimensions().ok_or_else(|| err("no dimensions"))?; + let (w, h) = (w as u32, h as u32); + let input = dec.input_colorspace().ok_or_else(|| err("no colorspace"))?; + let (out_cs, layout) = match input { + ColorSpace::Luma | ColorSpace::LumaA => (ColorSpace::Luma, ChannelLayout::Gray), + ColorSpace::CMYK => (ColorSpace::CMYK, ChannelLayout::Cmyk), + ColorSpace::YCCK => (ColorSpace::YCCK, ChannelLayout::Cmyk), + _ => (ColorSpace::RGB, ChannelLayout::Rgb), + }; + limits.check(w, h, layout, SampleType::U8)?; + // The backend fills whatever the scan no longer describes, and allocates the whole output + // once a scan holds a byte, so a short scan under a large frame is a gigabyte of synthesized + // output from a few kilobytes of file (#8597), truncated or not. The budget is the scan data + // itself, not `bytes.len()`, which APP padding can inflate without describing one more pixel; + // where the walk stops early on an odd layout, what it couldn't read counts as scan data. + if u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes { + let what = if end == DataEnd::Truncated { "the truncated scan" } else { "the scan data" }; + return Err(err(format!("{what} is too short for the frame it declares"))); + } + dec.set_options(dec.options().jpeg_set_out_colorspace(out_cs)); + let mut px = dec.decode().map_err(err)?; + let expected = w as usize * h as usize * layout.channels(); + if px.len() < expected { + return Err(err("short pixel buffer")); + } + px.truncate(expected); + match input { + // Adobe-style CMYK is stored inverted (255 = no ink). + ColorSpace::CMYK => px.iter_mut().for_each(|v| *v = 255 - *v), + ColorSpace::YCCK => { + for p in px.as_chunks_mut::<4>().0 { + let (y, cb, cr) = (p[0] as f32, p[1] as f32 - 128.0, p[2] as f32 - 128.0); + let c = y + 1.402 * cr; + let m = y - 0.344_136 * cb - 0.714_136 * cr; + let yy = y + 1.772 * cb; + p[0] = c.round().clamp(0.0, 255.0) as u8; + p[1] = m.round().clamp(0.0, 255.0) as u8; + p[2] = yy.round().clamp(0.0, 255.0) as u8; + p[3] = 255 - p[3]; + } + } + _ => {} + } + let mut img = Image::from_raw(w, h, layout, SampleType::U8, px)?; + img.icc = meta.icc; + img.meta = Metadata { exif: meta.exif, xmp: meta.xmp, dpi: meta.dpi, photoshop_resources: meta.photoshop, ..Default::default() }; + if end == DataEnd::Truncated { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let (w16, h16) = match (u16::try_from(w), u16::try_from(h)) { + (Ok(a), Ok(b)) => (a, b), + _ => { + return Err(CodecError::encode(F, "JPEG dimensions are limited to 65535")); + } + }; + let ct = match img.layout() { + ChannelLayout::Gray => jpeg_encoder::ColorType::Luma, + ChannelLayout::Rgb => jpeg_encoder::ColorType::Rgb, + ChannelLayout::Cmyk => jpeg_encoder::ColorType::Cmyk, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, opts.jpeg_quality.clamp(1, 100)); + // CMYK is always written 4:4:4: subsampled 4-component JPEGs are not + // decoded consistently across implementations. + let subsampled = opts.jpeg_chroma_subsampling && img.layout() == ChannelLayout::Rgb; + let sampling = if subsampled { jpeg_encoder::SamplingFactor::R_4_2_0 } else { jpeg_encoder::SamplingFactor::R_4_4_4 }; + enc.set_sampling_factor(sampling); + // JPEG Options › Format Options. Fitted Huffman tables make a baseline scan non-interleaved + // (one scan per component), which zune-jpeg 0.5.15 decodes wrongly with 4:2:0 chroma + // (etemesi254/zune-image#419; see `web::encode_jpeg_rgb8`), so a subsampled Baseline + // Optimized keeps the standard tables until that decoder is updated. Progressive is fine. + let progressive = opts.jpeg_format == JpegFormat::Progressive; + enc.set_progressive(progressive); + let optimized = opts.jpeg_format == JpegFormat::Optimized && !subsampled; + enc.set_optimized_huffman_tables(progressive || optimized); + let e = |e: jpeg_encoder::EncodingError| CodecError::encode(F, e); + if opts.embed_metadata { + if img.layout() != ChannelLayout::Cmyk + && let Some((x, y)) = img.meta.dpi + && x >= 1.0 + && y >= 1.0 + { + enc.set_density(jpeg_encoder::Density::Inch { x: x.round().min(65535.0) as u16, y: y.round().min(65535.0) as u16 }); + } + // Metadata too large for its one segment is left out (and reported by the fidelity + // warnings) rather than failing the whole export. + let exif = if img.layout() == ChannelLayout::Cmyk { + match img.meta.exif.as_deref() { + Some(exif) => exif_segment_with_resolution(exif, img.meta.dpi), + None if usable_dpi(img.meta.dpi) => exif_segment_with_resolution(&[], img.meta.dpi), + None => None, + } + } else { + img.meta.exif.as_deref().and_then(|x| exif_segment(x, img.meta.dpi)) + }; + if let Some(seg) = exif { + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(seg) = img.meta.xmp.as_deref().and_then(|x| xmp_segment(x, img.meta.dpi)) { + enc.add_app_segment(1, &seg).map_err(e)?; + } + } + if opts.embed_icc + && let Some(icc) = &img.icc + { + enc.add_icc_profile(icc).map_err(e)?; + } + enc.encode(img.data(), w16, h16, ct).map_err(e)?; + if img.layout() == ChannelLayout::Cmyk { + strip_jfif_app0(&mut out); + } + Ok(out) +} + +/// JFIF APP0 requires one or three image components. A four-component CMYK JPEG must not carry +/// that marker; keep Adobe APP14, which identifies the CMYK encoding instead. +fn strip_jfif_app0(bytes: &mut Vec) { + if !bytes.starts_with(&[0xff, 0xd8]) { + return; + } + let mut i = 2usize; + loop { + if bytes.get(i) != Some(&0xff) { + return; + } + let Some(&marker) = bytes.get(i.saturating_add(1)) else { return }; + if matches!(marker, 0xda | 0xd9) { + return; + } + if marker == 0xff { + i = i.saturating_add(1); + continue; + } + if marker == 0x01 || (0xd0..=0xd8).contains(&marker) { + i = i.saturating_add(2); + continue; + } + let Some(length_bytes) = i.checked_add(2).and_then(|at| bytes.get(at..at.checked_add(2)?)) else { return }; + let Ok(length_bytes) = <[u8; 2]>::try_from(length_bytes) else { return }; + let length = usize::from(u16::from_be_bytes(length_bytes)); + if length < 2 { + return; + } + let Some(end) = i.checked_add(2).and_then(|at| at.checked_add(length)) else { return }; + if end > bytes.len() { + return; + } + if marker == 0xe0 && bytes.get(i.saturating_add(4)..i.saturating_add(9)) == Some(b"JFIF\0") { + bytes.drain(i..end); + } else { + i = end; + } + } +} + +/// Return whether both resolution values are finite and within the supported metadata range. +fn usable_dpi(dpi: Option<(f32, f32)>) -> bool { + dpi.is_some_and(|(x, y)| x.is_finite() && y.is_finite() && (1.0..=1_000_000.0).contains(&x) && (1.0..=1_000_000.0).contains(&y)) +} + +/// The most data one APP segment holds (its 16-bit length counts its own two bytes). +const MAX_SEGMENT: usize = 65533; + +/// XMP properties (local names, any namespace prefix) that only describe the layered document: +/// every document ever placed in it, the text of every type layer. A flat image doesn't need them, +/// and they alone can outgrow a segment. +const LAYERED_ONLY_XMP: [&str; 2] = ["DocumentAncestors", "TextLayers"]; + +/// The EXIF APP1 segment, or `None` when it doesn't fit in one segment. +pub(crate) fn exif_segment(exif: &[u8], dpi: Option<(f32, f32)>) -> Option> { + // The pixels are written as they are shown: never let a viewer rotate them again; and store + // the output resolution, not the source's (#1691). + let seg = [EXIF_HEADER, export_exif(exif, dpi).as_ref()].concat(); + (seg.len() <= MAX_SEGMENT).then_some(seg) +} + +/// Build an EXIF APP1 segment, adding missing resolution tags when the source metadata is valid. +fn exif_segment_with_resolution(exif: &[u8], dpi: Option<(f32, f32)>) -> Option> { + let seg = [EXIF_HEADER, crate::resolution::export_exif_adding_missing_resolution(exif, dpi).as_ref()].concat(); + (seg.len() <= MAX_SEGMENT).then_some(seg) +} + +/// The XMP APP1 segment. When the packet doesn't fit in one segment, the layered-document +/// properties are left out; `None` when it still doesn't fit. +pub(crate) fn xmp_segment(xmp: &str, dpi: Option<(f32, f32)>) -> Option> { + let xmp = export_xmp(xmp, dpi); + let seg = |x: &str| [XMP_HEADER, x.as_bytes()].concat(); + if XMP_HEADER.len() + xmp.len() <= MAX_SEGMENT { + return Some(seg(&xmp)); + } + let trimmed = LAYERED_ONLY_XMP.iter().fold(xmp.into_owned(), |x, name| remove_element(&x, name)); + (XMP_HEADER.len() + trimmed.len() <= MAX_SEGMENT).then(|| seg(&trimmed)) +} + +/// `xmp` without its `…` and `` elements. Unbalanced +/// markup is kept as is. +fn remove_element(xmp: &str, name: &str) -> String { + let local = format!(":{name}"); + let mut out = String::with_capacity(xmp.len()); + let mut rest = xmp; + while let Some(j) = rest.find(&local) { + let head = rest.get(..j).unwrap_or_default(); + let prefix_len = head.bytes().rev().take_while(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.')).count(); + let lt = j - prefix_len; + let after = rest.get(j + local.len()..).unwrap_or_default(); + // An opening tag of exactly this name (not a closing tag, an attribute or a longer name). + let opening = prefix_len > 0 && lt > 0 && rest.get(lt - 1..lt) == Some("<"); + if !opening || !after.starts_with(['>', '/', ' ', '\t', '\r', '\n']) { + out.push_str(rest.get(..j + local.len()).unwrap_or_default()); + rest = after; + continue; + } + let Some(gt) = after.find('>') else { break }; + let end = if after.get(..gt).is_some_and(|s| s.ends_with('/')) { + gt + 1 + } else { + let close = format!("", rest.get(lt..j).unwrap_or_default()); + let Some(c) = after.find(&close) else { break }; + c + close.len() + }; + out.push_str(rest.get(..lt - 1).unwrap_or_default()); + rest = after.get(end..).unwrap_or_default(); + } + out.push_str(rest); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Format, Metadata}; + + /// Build a little-endian TIFF with an empty IFD0 for metadata tests. + fn empty_tiff() -> Vec { + // Little-endian TIFF with an empty IFD0 and no next IFD. + b"II*\0\x08\0\0\0\0\0\0\0\0\0\0\0".to_vec() + } + + /// Return whether the JPEG begins with a JFIF APP0 segment. + fn has_jfif_app0(bytes: &[u8]) -> bool { + bytes.get(2..11) == Some(b"\xff\xe0\0\x10JFIF\0") + } + + /// An APP1 Extended XMP fragment (GUID, total size, offset, data). + fn extended_xmp(offset: u32) -> Vec { + let mut seg = EXTENDED_XMP_HEADER.to_vec(); + seg.extend_from_slice(&[b'A'; 32]); + seg.extend_from_slice(&1_000_000u32.to_be_bytes()); + seg.extend_from_slice(&offset.to_be_bytes()); + seg.push(b'x'); + let mut out = vec![0xFF, 0xE1]; + out.extend_from_slice(&u16::try_from(seg.len() + 2).unwrap().to_be_bytes()); + out.extend_from_slice(&seg); + out + } + + #[test] + fn extended_xmp_fragments_are_left_out_of_the_backend_parse() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![120; 16 * 16 * 3]).unwrap(); + let plain = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert!(without_extended_xmp(&plain).is_none()); + // 20000 consistent but incomplete fragments right after SOI: zune-jpeg would re-sort and + // re-check the growing pending list after each one. + let mut hostile = plain[..2].to_vec(); + for k in 0..20_000u32 { + hostile.extend_from_slice(&extended_xmp(k * 2)); + } + hostile.extend_from_slice(&plain[2..]); + assert_eq!(without_extended_xmp(&hostile).as_deref(), Some(&plain[..])); + let back = decode(&hostile, &Limits::default()).unwrap(); + assert_eq!(back.dimensions(), (16, 16)); + } + + /// A tiny truncated scan under a frame patched to 20000×20000 is refused before the backend + /// fills 400 million pixels from it; the same file with its real size still recovers. + #[test] + fn a_short_truncated_scan_cannot_declare_a_huge_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let cut = &full[..full.len() - 4]; + assert_eq!(data_end(cut).0, DataEnd::Truncated); + assert!(crate::decode_as(Format::Jpeg, cut).is_ok()); + let sof = cut.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = cut.to_vec(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + } + + /// The truncated-scan budget counts scan data, not the file length: APP padding before the + /// window cannot admit an oversized output. The same padded file with its real size still + /// decodes (with the truncation warning). + #[test] + fn app_padding_cannot_admit_an_oversized_truncated_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let cut = &full[..full.len() - 4]; + // Four full-length COMMENT segments (~256 KiB) stand where APP padding would; they are + // complete, so the file otherwise parses normally. + let fill = [b'p'; 65533]; + let mut padded = cut[..2].to_vec(); + for _ in 0..4 { + padded.extend_from_slice(&[0xFF, 0xFE]); + padded.extend_from_slice(&0xFFFFu16.to_be_bytes()); + padded.extend_from_slice(&fill); + } + padded.extend_from_slice(&cut[2..]); + assert_eq!(data_end(&padded).0, DataEnd::Truncated); + // Well above the frame's pixels / budget, yet far above the scan data: the old + // file-length bound admitted this frame. + assert!(padded.len() > (u64::from(20_000u32) * u64::from(20_000u32) / MAX_PIXELS_PER_BYTE) as usize, "padding too small"); + assert!(crate::decode_as(Format::Jpeg, &padded).is_ok()); + let sof = padded.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = padded.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + } + + /// A complete file is measured on its scan data too: padding can't admit an oversized frame. + /// The same padded file at its real size decodes, and so does an odd layout whose unread tail + /// counts as scan data. + #[test] + fn app_padding_cannot_admit_an_oversized_complete_frame() { + let img = Image::from_raw(16, 16, ChannelLayout::Rgb, SampleType::U8, vec![128; 16 * 16 * 3]).unwrap(); + let full = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let fill = [b'p'; 65533]; + let mut padded = full[..2].to_vec(); + for _ in 0..4 { + padded.extend_from_slice(&[0xFF, 0xFE]); + padded.extend_from_slice(&0xFFFFu16.to_be_bytes()); + padded.extend_from_slice(&fill); + } + padded.extend_from_slice(&full[2..]); + let (end, scanned) = data_end(&padded); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "the padding isn't scan data: {scanned}"); + assert!(padded.len() as u64 > u64::from(20_000u32) * 20_000 / MAX_PIXELS_PER_BYTE, "padding too small"); + assert!(crate::decode_as(Format::Jpeg, &padded).is_ok()); + let sof = padded.windows(2).position(|w| w == [0xFF, 0xC0] || w == [0xFF, 0xC2]).unwrap(); + let mut huge = padded.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode_as(Format::Jpeg, &huge).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // A stray byte where a marker should be, before the padding: the walk skips it and still + // recognises the padding, so an oversized frame is refused there too. + let mut odd = huge.clone(); + odd.insert(2, 0x00); + let (end, scanned) = data_end(&odd); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "the padding after a stray byte isn't scan data: {scanned}"); + let e = crate::decode_as(Format::Jpeg, &odd).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // Stray bytes never count: 256 KiB of junk where a marker should be (the decoder skips + // it), then the oversized frame's segments, is refused. (Bytes after a scan starts are + // entropy-coded data, any value, and count as such.) + let mut junk = huge[..2].to_vec(); + junk.extend(std::iter::repeat_n(0x00u8, 4 * 65536)); + junk.extend_from_slice(&huge[2..]); + let (end, scanned) = data_end(&junk); + assert_eq!(end, DataEnd::Complete); + assert!(scanned < 1024, "junk isn't scan data: {scanned}"); + let e = crate::decode_as(Format::Jpeg, &junk).unwrap_err(); + assert!(e.to_string().contains("too short"), "{e}"); + // Junk with no marker after it: no image data at all. + let mut tail = vec![0xFF, 0xD8]; + tail.extend(std::iter::repeat_n(0x00u8, 4096)); + assert_eq!(data_end(&tail), (DataEnd::Empty, 0)); + } + + /// CMYK JPEG output must omit JFIF while retaining Adobe's CMYK marker. + #[test] + fn cmyk_jpeg_omits_jfif_but_keeps_adobe_cmyk_marker() { + let img = Image::from_raw(2, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 32, 64, 96, 255, 224, 192, 160]).unwrap(); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + let meta = scan_metadata(&bytes); + assert!(!has_jfif_app0(&bytes)); + assert_eq!(meta.frame, Some((2, 1, 4))); + assert_eq!(meta.adobe_transform, Some(0)); + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().layout(), ChannelLayout::Cmyk); + } + + /// CMYK JPEG output stores image resolution in EXIF when the source has no EXIF. + #[test] + fn cmyk_jpeg_preserves_dpi_without_source_exif() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 0, 0, 0]).unwrap(); + img.meta.dpi = Some((300.0, 150.0)); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert!(!has_jfif_app0(&bytes)); + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().meta.dpi, Some((300.0, 150.0))); + } + + /// CMYK JPEG output adds missing EXIF resolution tags to existing metadata. + #[test] + fn cmyk_jpeg_preserves_dpi_with_source_exif_without_resolution_tags() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Cmyk, SampleType::U8, vec![0, 0, 0, 0]).unwrap(); + img.meta = Metadata { exif: Some(empty_tiff()), dpi: Some((240.0, 120.0)), ..Default::default() }; + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert_eq!(crate::decode_as(Format::Jpeg, &bytes).unwrap().meta.dpi, Some((240.0, 120.0))); + } + + /// Adding missing resolution tags makes them readable from the rewritten IFD0. + #[test] + fn exif_with_resolution_adds_missing_ifd0_entries() { + let exif = empty_tiff(); + let rewritten = crate::resolution::export_exif_adding_missing_resolution(&exif, Some((240.0, 120.0))); + + assert_eq!(crate::resolution::exif_resolution(&rewritten), Some((240.0, 120.0))); + } + + /// RGB JPEG output continues to store image resolution in JFIF density fields. + #[test] + fn rgb_jpeg_keeps_jfif_density() { + let mut img = Image::from_raw(1, 1, ChannelLayout::Rgb, SampleType::U8, vec![12, 34, 56]).unwrap(); + img.meta.dpi = Some((96.0, 96.0)); + let bytes = crate::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + + assert!(has_jfif_app0(&bytes)); + assert_eq!(scan_metadata(&bytes).dpi, Some((96.0, 96.0))); + } + + // --- Arithmetic-coded and 12-bit files, decoded by craft-jpeg --- + + fn segment(out: &mut Vec, marker: u8, body: &[u8]) { + out.extend_from_slice(&[0xFF, marker]); + out.extend_from_slice(&((body.len() + 2) as u16).to_be_bytes()); + out.extend_from_slice(body); + } + + /// One 8×8 gray block, quantizer 1, with entropy-coded scan data `data`. + fn gray_block(sof: u8, precision: u8, dht: Option<&[u8]>, data: &[u8]) -> Vec { + let mut s = vec![0xFF, 0xD8]; + let mut dqt = vec![0u8]; + dqt.extend([1u8; 64]); + segment(&mut s, 0xDB, &dqt); + segment(&mut s, sof, &[precision, 0, 8, 0, 8, 1, 1, 0x11, 0]); + if let Some(dht) = dht { + segment(&mut s, 0xC4, dht); + } + segment(&mut s, 0xDA, &[1, 1, 0x00, 0, 63, 0]); + s.extend_from_slice(data); + s.extend([0xFF, 0xD9]); + s + } + + /// SOF9 with only a DC coefficient (from a T.81 arithmetic encoder): 8-bit DC 200 → sample + /// 200 / 8 + 128 = 153; 12-bit DC 1000 → 1000 / 8 + 2048 = 2173. + fn arithmetic_block(precision: u8) -> Vec { + let data: &[u8] = if precision == 8 { &[210, 217, 68, 0] } else { &[210, 234, 134, 0] }; + gray_block(0xC9, precision, None, data) + } + + /// A 12-bit SOF1 stream: one DC coefficient. The DC table has one code (`0`, category 13), + /// the AC table one (`0`, end of block). DC = 7616 → sample 7616 / 8 + 2048 = 3000. + fn twelve_bit_huffman_block() -> Vec { + let mut dht = vec![0x00, 1]; + dht.extend([0u8; 15]); + dht.push(13); + dht.extend([0x10, 1]); + dht.extend([0u8; 15]); + dht.push(0x00); + // Bits: `0`, 7616 in 13 bits, `0`, one pad `1`. + let word: u16 = (7616 << 2) | 1; + gray_block(0xC1, 12, Some(&dht), &word.to_be_bytes()) + } + + fn twelve_bit(v: f32) -> u16 { + (v * 65535.0 / 4095.0).round() as u16 + } + + #[test] + fn routing() { + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC9, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC0, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8])); + assert!(!uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC2, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC2, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0])); + assert!(uses_craft_jpeg(&[0xFF, 0xD8, 0xFF, 0xC3, 0, 11, 8, 0, 8, 0, 8, 1, 1, 0x11, 0])); + } + + #[test] + fn lossless_and_twelve_bit_progressive_are_unsupported_not_damaged() { + for marker in [0xC3, 0xC2] { + let mut bytes = vec![0xFF, 0xD8, 0xFF, marker, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0]; + bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0x12, 0x34, 0xFF, 0xD9]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(matches!(e, CodecError::Unsupported { .. }), "{marker:#x}: {e:?}"); + } + } + + #[test] + fn a_twelve_bit_frame_without_scan_data_is_refused_before_decoding() { + let mut bytes = vec![0xFF, 0xD8, 0xFF, 0xC1, 0, 11, 12, 0x40, 0, 0x40, 0, 1, 1, 0x11, 0]; + bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0xFF, 0xD9]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(e.to_string().contains("no scan data"), "{e}"); + } + + #[test] + fn arithmetic_eight_bit_block_is_eight_bit() { + let img = crate::decode(&arithmetic_block(8)).unwrap(); + assert_eq!((img.dimensions(), img.sample_type(), img.layout()), ((8, 8), SampleType::U8, ChannelLayout::Gray)); + assert!(img.data().iter().all(|&v| v.abs_diff(153) <= 1), "{:?}", &img.data()[..4]); + assert!(img.warnings.is_empty()); + } + + #[test] + fn arithmetic_twelve_bit_block_is_sixteen_bit() { + let img = crate::decode(&arithmetic_block(12)).unwrap(); + assert_eq!((img.dimensions(), img.sample_type(), img.layout()), ((8, 8), SampleType::U16, ChannelLayout::Gray)); + let want = twelve_bit(2173.0); + assert!(img.to_u16_samples().unwrap().iter().all(|&v| v.abs_diff(want) <= 1)); + } + + #[test] + fn twelve_bit_huffman_block_decodes_to_sixteen_bit() { + let img = crate::decode(&twelve_bit_huffman_block()).unwrap(); + assert_eq!((img.dimensions(), img.sample_type()), ((8, 8), SampleType::U16)); + let want = twelve_bit(3000.0); + let px = img.to_u16_samples().unwrap(); + assert!(px.iter().all(|&v| v.abs_diff(want) <= 1), "{:?} vs {want}", &px[..4]); + assert!(img.warnings.is_empty()); + } + + #[test] + fn twelve_bit_jpeg_keeps_its_photoshop_resources() { + // An APP13 `8BIM` block (paths, Photoshop settings) right after SOI, as on the zune-jpeg path. + let resources = b"8BIM\x04\x04\0\0\0\0\0\0".to_vec(); + let mut app13 = PHOTOSHOP_HEADER.to_vec(); + app13.extend_from_slice(&resources); + let stream = twelve_bit_huffman_block(); + let mut with = stream[..2].to_vec(); + segment(&mut with, 0xED, &app13); + with.extend_from_slice(&stream[2..]); + assert!(uses_craft_jpeg(&with)); + let img = crate::decode(&with).unwrap(); + assert_eq!(img.meta.photoshop_resources.as_deref(), Some(resources.as_slice())); + } + + /// Where the entropy-coded data of the last scan starts. + fn last_scan_data(s: &[u8]) -> usize { + let sos = s.windows(2).rposition(|p| p == [0xFF, 0xDA]).unwrap(); + sos + 2 + usize::from(u16::from_be_bytes([s[sos + 2], s[sos + 3]])) + } + + /// A 12-bit scan whose entropy data runs out, EOI kept, is never a clean image. + #[test] + fn twelve_bit_scan_out_of_data_is_not_clean() { + let full = twelve_bit_huffman_block(); + let data = last_scan_data(&full); + for keep in 0..2 { + let mut s = full[..data + keep].to_vec(); + s.extend([0xFF, 0xD9]); + if let Ok(img) = crate::decode(&s) { + assert!(!img.warnings.is_empty(), "keep {keep}: clean decode of missing data"); + } + } + } + + /// A multi-MCU SOF1 12-bit scan cut in half, EOI kept, decodes with the Truncated warning; a + /// truncated scan too short for its frame is refused, as on the zune-jpeg path. + #[test] + fn damaged_or_truncated_twelve_bit_scan() { + let (w, h) = (64usize, 64usize); + let rgb: Vec = (0..w * h).flat_map(|i| [(i % w * 4) as u8, (i / w * 4) as u8, (i * 7) as u8]).collect(); + let mut out = Vec::new(); + jpeg_encoder::Encoder::new(&mut out, 90).encode(&rgb, w as u16, h as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + // Relabel the baseline frame as 12-bit SOF1: the Huffman data still parses, as different values. + let sof = out.windows(2).position(|p| p == [0xFF, 0xC0]).unwrap(); + out[sof + 1] = 0xC1; + out[sof + 4] = 12; + let data = last_scan_data(&out); + let end = out.len() - 2; + let mut cut = out[..data + (end - data) / 2].to_vec(); + if cut.last() == Some(&0xFF) { + cut.pop(); + } + cut.extend([0xFF, 0xD9]); + let img = crate::decode(&out).unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); + let img = crate::decode(&cut).unwrap(); + assert_eq!(img.warnings, vec![DecodeWarning::Truncated { format: Format::Jpeg }]); + // Without EOI the same cut is a truncated file; under a 20000×20000 frame it is refused. + cut.truncate(cut.len() - 2); + assert_eq!(data_end(&cut).0, DataEnd::Truncated); + assert!(crate::decode(&cut).is_ok()); + cut[sof + 5..sof + 9].copy_from_slice(&[0x4E, 0x20, 0x4E, 0x20]); + let e = crate::decode(&cut).unwrap_err(); + assert!(e.to_string().contains("too short for the frame it declares"), "{e}"); + } +} diff --git a/crates/image-codecs/src/codecs/jxl.rs b/crates/image-codecs/src/codecs/jxl.rs new file mode 100644 index 000000000..11591c930 --- /dev/null +++ b/crates/image-codecs/src/codecs/jxl.rs @@ -0,0 +1,159 @@ +//! Pure-Rust lossless JPEG XL (Modular) writer. +//! +//! Currently writes a bare JPEG XL codestream. The encoder accepts 8- or 16-bit +//! gray, RGB and RGBA samples, but does not write ICC/EXIF/XMP boxes. +//! Gray+alpha sources are expanded losslessly to RGBA by the export plan. +//! The flat export pipeline converts tagged RGB documents to sRGB first. + +use std::panic::{AssertUnwindSafe, catch_unwind}; + +use zune_core::{bit_depth::BitDepth, colorspace::ColorSpace, options::EncoderOptions as ZuneOptions}; +use zune_jpegxl::JxlSimpleEncoder; + +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::format::Format; +use crate::image::{ChannelLayout, Image, SampleType}; + +const F: Format = Format::Jxl; + +// zune-jpegxl 0.5.2 emits ExtraChannelInfo(all_default=true) for every alpha +// channel. In JPEG XL that flag means *8-bit* alpha, even when the modular +// payload was encoded from 16-bit samples. Replace only that metadata bundle, +// leaving the entire encoded frame and its TOC untouched. The replacement is +// exactly 16 bits longer, so subsequent byte-aligned data stays aligned. +// +// This deliberately verifies the pinned encoder's header layout rather than +// trying to parse or rewrite arbitrary JPEG XL codestreams. A different header +// must fail closed, not produce a file that silently corrupts transparency. +fn correct_16_bit_alpha_header(bytes: &[u8], width: u32, height: u32) -> Result, CodecError> { + fn read(bytes: &[u8], pos: &mut usize, count: usize) -> Option { + let mut value = 0u32; + for bit in 0..count { + let offset = *pos; + let byte = *bytes.get(offset / 8)?; + value |= u32::from((byte >> (offset % 8)) & 1) << bit; + *pos += 1; + } + Some(value) + } + + fn read_dimension(bytes: &[u8], pos: &mut usize) -> Option { + let bits = match read(bytes, pos, 2)? { + 0 => 9, + 1 => 13, + 2 => 18, + _ => 30, + }; + read(bytes, pos, bits)?.checked_add(1) + } + + fn write_bits(out: &mut Vec, pos: &mut usize, count: usize, value: u32) { + for bit in 0..count { + if (*pos).is_multiple_of(8) { + out.push(0); + } + out[*pos / 8] |= (((value >> bit) & 1) as u8) << (*pos % 8); + *pos += 1; + } + } + + let mut pos = 0; + let header_matches = read(bytes, &mut pos, 16) == Some(0x0aff) + && read(bytes, &mut pos, 1) == Some(0) + && read_dimension(bytes, &mut pos) == Some(height) + && read(bytes, &mut pos, 3) == Some(0) + && read_dimension(bytes, &mut pos) == Some(width) + && read(bytes, &mut pos, 1) == Some(0) // metadata not all-default + && read(bytes, &mut pos, 1) == Some(0) // no extra fields + && read(bytes, &mut pos, 1) == Some(0) // integer samples + && read(bytes, &mut pos, 2) == Some(3) // custom bit depth + && read(bytes, &mut pos, 6) == Some(15) // 16 bits + && read(bytes, &mut pos, 1) == Some(0) // uses >14 bits + && read(bytes, &mut pos, 2) == Some(1); // one extra channel + let alpha_flag_pos = pos; + let header_matches = header_matches + && read(bytes, &mut pos, 1) == Some(1) // incorrect default 8-bit alpha + && read(bytes, &mut pos, 1) == Some(0) // not XYB + && read(bytes, &mut pos, 1) == Some(1) // default sRGB encoding (RGBA) + && read(bytes, &mut pos, 2) == Some(0) // no extensions + && read(bytes, &mut pos, 1) == Some(1); // default transform + + let frame_offset = pos.div_ceil(8); + let header_matches = header_matches && frame_offset <= bytes.len() && (pos..frame_offset * 8).all(|bit| bytes[bit / 8] & (1 << (bit % 8)) == 0); + if !header_matches { + return Err(CodecError::encode(F, "unexpected zune-jpegxl header for 16-bit alpha")); + } + + let mut fixed = Vec::with_capacity(bytes.len() + 2); + let mut cursor = 0; + for bit in 0..alpha_flag_pos { + write_bits(&mut fixed, &mut cursor, 1, u32::from((bytes[bit / 8] >> (bit % 8)) & 1)); + } + + // JPEG XL ExtraChannelInfo, non-default, straight 16-bit alpha: + // type=kAlpha (Enum=0), bit_depth=uint16, dim_shift=0, name_len=0, + // alpha_associated=false. Adds exactly 16 bits versus all_default=true. + write_bits(&mut fixed, &mut cursor, 1, 0); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 1, 0); + write_bits(&mut fixed, &mut cursor, 2, 3); + write_bits(&mut fixed, &mut cursor, 6, 15); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 2, 0); + write_bits(&mut fixed, &mut cursor, 1, 0); + + for bit in alpha_flag_pos + 1..frame_offset * 8 { + write_bits(&mut fixed, &mut cursor, 1, u32::from((bytes[bit / 8] >> (bit % 8)) & 1)); + } + if cursor != (frame_offset + 2) * 8 { + return Err(CodecError::encode(F, "JPEG XL alpha header alignment mismatch")); + } + fixed.extend_from_slice(&bytes[frame_offset..]); + Ok(fixed) +} + +pub(crate) fn encode(image: &Image, plan: Plan) -> Result, CodecError> { + let image = image.converted(plan.layout, plan.sample); + let space = match image.layout() { + ChannelLayout::Gray => ColorSpace::Luma, + ChannelLayout::GrayA => ColorSpace::LumaA, + ChannelLayout::Rgb => ColorSpace::RGB, + ChannelLayout::Rgba => ColorSpace::RGBA, + _ => return Err(CodecError::unsupported(F, "JPEG XL export supports grayscale and RGB, with optional alpha")), + }; + let depth = match image.sample_type() { + SampleType::U8 => BitDepth::Eight, + SampleType::U16 => BitDepth::Sixteen, + _ => return Err(CodecError::unsupported(F, "JPEG XL lossless export supports 8 or 16-bit integer pixels")), + }; + let (width, height) = image.dimensions(); + let samples = (width as usize) + .checked_mul(height as usize) + .and_then(|n| n.checked_mul(image.layout().channels())) + .and_then(|n| n.checked_mul(image.sample_type().bytes())) + .ok_or_else(|| CodecError::encode(F, "image dimensions overflow the buffer size"))?; + if image.data().len() != samples { + return Err(CodecError::InvalidImage("incorrect JPEG XL pixel buffer length".into())); + } + // A panic in an external image encoder must not terminate the host application. + catch_unwind(AssertUnwindSafe(|| { + let options = ZuneOptions::new(width as usize, height as usize, space, depth); + let mut output = Vec::new(); + JxlSimpleEncoder::new(image.data(), options).encode(&mut output).map_err(|e| CodecError::encode(F, e.to_string()))?; + if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { correct_16_bit_alpha_header(&output, width, height) } else { Ok(output) } + })) + .unwrap_or_else(|_| Err(CodecError::encode(F, "the JPEG XL encoder failed"))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn alpha_header_correction_rejects_unexpected_or_truncated_headers() { + for bytes in [&[][..], &[0xff, 0x0a][..], &[0; 32][..]] { + assert!(correct_16_bit_alpha_header(bytes, 2, 2).is_err()); + } + } +} diff --git a/crates/image-codecs/src/codecs/pcx.rs b/crates/image-codecs/src/codecs/pcx.rs new file mode 100644 index 000000000..48dbf1169 --- /dev/null +++ b/crates/image-codecs/src/codecs/pcx.rs @@ -0,0 +1,139 @@ +//! ZSoft PCX (Paintbrush) reader: strictly bounded scanline RLE, 16/256-colour +//! palettes and 8-bit three-plane RGB. Read-only: modern saves should use PNG. + +use crate::SampleType; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::Limits; + +const HEADER: usize = 128; + +fn bad(reason: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Pcx, reason) +} +fn le16(b: &[u8], i: usize) -> u16 { + u16::from_le_bytes([b[i], b[i + 1]]) +} + +/// PCX lacks a strong magic number. Require plausible complete header fields before +/// identifying it, rather than classifying every file starting with 0x0a as PCX. +pub(crate) fn looks_like_pcx(b: &[u8]) -> bool { + if b.len() < HEADER || b[0] != 0x0a || !matches!(b[1], 0 | 2 | 3 | 5) || b[2] != 1 { + return false; + } + let bits = b[3]; + let planes = b[65]; + let stride = u32::from(le16(b, 66)); + let (x0, y0, x1, y1) = (le16(b, 4), le16(b, 6), le16(b, 8), le16(b, 10)); + if x1 < x0 || y1 < y0 || !matches!(bits, 1 | 2 | 4 | 8) || !(1..=4).contains(&planes) { + return false; + } + let width = u32::from(x1 - x0) + 1; + let min_stride = (width * u32::from(bits)).div_ceil(8); + stride >= min_stride && stride > 0 && (bits != 8 || matches!(planes, 1 | 3)) && (bits == 8 || u32::from(bits) * u32::from(planes) <= 4) +} + +/// Read one compressed scanline, including all planes. RLE packets never cross a scanline. +fn read_row(b: &[u8], pos: &mut usize, out: &mut [u8]) -> Result<(), CodecError> { + let mut n = 0; + while n < out.len() { + let code = *b.get(*pos).ok_or_else(|| bad("truncated pixel RLE"))?; + *pos += 1; + let (count, value) = if code >= 0xc0 { + let count = usize::from(code & 0x3f); + let value = *b.get(*pos).ok_or_else(|| bad("truncated RLE packet"))?; + *pos += 1; + (count, value) + } else { + (1, code) + }; + if count == 0 || count > out.len() - n { + return Err(bad("RLE packet crosses a scanline or has zero length")); + } + out[n..n + count].fill(value); + n += count; + } + Ok(()) +} + +pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { + if !looks_like_pcx(b) { + return Err(bad("invalid PCX header or unsupported pixel layout")); + } + let (w, h) = (u32::from(le16(b, 8) - le16(b, 4)) + 1, u32::from(le16(b, 10) - le16(b, 6)) + 1); + limits.check(w, h, ChannelLayout::Rgb, SampleType::U8)?; + let (bits, planes, stride) = (usize::from(b[3]), usize::from(b[65]), usize::from(le16(b, 66))); + let row_len = stride.checked_mul(planes).ok_or_else(|| bad("row size overflow"))?; + let total = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(3)).ok_or_else(|| bad("pixel size overflow"))?; + // The last 769 bytes are the optional VGA palette for 8-bit indexed images. + let vga = bits == 8 && planes == 1 && b.len() >= HEADER + 769 && b[b.len() - 769] == 0x0c; + if bits == 8 && planes == 1 && !vga && le16(b, 68) != 2 { + return Err(bad("8-bit indexed PCX has no 256-colour palette")); + } + let image_end = if vga { b.len() - 769 } else { b.len() }; + // A 63-byte RLE packet takes 2 bytes; a trailing remainder of 1 byte below 0xC0 + // can be encoded as a 1-byte literal, while any larger remainder takes at least 2 bytes. + // Because RLE packets may span planes within a scanline (`row_len`), compute the + // minimum encoded bytes across the full scanline (`row_len`) and multiply by `h`. + // Reject inputs that cannot possibly contain all scanlines before allocating + // the decoded image buffer. + let min_packets = (row_len / 63) + .checked_mul(2) + .and_then(|n| { + n.checked_add(match row_len % 63 { + 0 => 0, + 1 => 1, + _ => 2, + }) + }) + .ok_or_else(|| bad("compressed row size overflow"))?; + let min_encoded = min_packets.checked_mul(h as usize).ok_or_else(|| bad("compressed image size overflow"))?; + let encoded_len = image_end.saturating_sub(HEADER); + if encoded_len < min_encoded { + return Err(bad("PCX pixel data is too short for the declared image")); + } + let mut row = vec![0u8; row_len]; + let mut pixels = Vec::new(); + pixels.try_reserve_exact(total).map_err(|_| bad("PCX pixel buffer is too large"))?; + pixels.resize(total, 0); + let mut p = HEADER; + let width = w as usize; + for y in 0..h as usize { + read_row(&b[..image_end], &mut p, &mut row)?; + for x in 0..width { + let rgb = if bits == 8 && planes == 3 { + [row[x], row[stride + x], row[2 * stride + x]] + } else { + let index = if bits == 8 { + usize::from(row[x]) + } else { + let bit = x * bits; + let shift = 8 - bits - (bit % 8); + let mask = (1usize << bits) - 1; + (0..planes).fold(0usize, |v, plane| { + let sample = (usize::from(row[plane * stride + bit / 8]) >> shift) & mask; + v | sample << (plane * bits) + }) + }; + if vga { + let o = image_end + 1 + index * 3; + [b[o], b[o + 1], b[o + 2]] + } else if bits == 8 { + let g = index as u8; + [g, g, g] // palette-info 2: gray scale + } else { + let o = 16 + index * 3; + [b[o], b[o + 1], b[o + 2]] + } + }; + pixels[(y * width + x) * 3..(y * width + x + 1) * 3].copy_from_slice(&rgb); + } + } + let mut image = Image::from_u8(w, h, ChannelLayout::Rgb, pixels)?; + let (xdpi, ydpi) = (le16(b, 12), le16(b, 14)); + if xdpi > 0 && ydpi > 0 { + image.meta.dpi = Some((f32::from(xdpi), f32::from(ydpi))); + } + Ok(image) +} diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs new file mode 100644 index 000000000..d1ff5140c --- /dev/null +++ b/crates/image-codecs/src/codecs/png.rs @@ -0,0 +1,2139 @@ +//! PNG via the `png` crate directly: 8/16-bit, Adam7 read (and write), +//! iCCP, eXIf, tEXt/zTXt/iTXt (XMP in `XML:com.adobe.xmp`), pHYs. + +use std::io::{self, Cursor, Write}; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, PngCompression, PngStereo}; + +const F: Format = Format::Png; +pub(crate) const XMP_KEYWORD: &str = "XML:com.adobe.xmp"; +const METERS_PER_INCH: f32 = 0.0254; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +/// Pixels per inch for `pHYs` pixels per metre. A whole number of ppi is stored rounded to whole +/// pixels per metre (300 ppi = 11811), so reading it back gives 299.9994: the whole number that +/// rounds to the stored value is the resolution that was meant, as Photoshop reads it. +pub(crate) fn dpi_of_ppm(ppm: u32) -> f32 { + let dpi = f64::from(ppm) * f64::from(METERS_PER_INCH); + let whole = dpi.round(); + if (whole / f64::from(METERS_PER_INCH)).round() == f64::from(ppm) { whole as f32 } else { dpi as f32 } +} + +/// Decodes a whole PNG, by driving a [`PngDecodeRows`] to its end. +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + PngDecodeRows::new(bytes, limits)?.step(&mut || true)?.ok_or_else(|| err("the decoder stopped before the image ended")) +} + +/// PNG bytes shared between the decoder and whoever else keeps them (a placed file's embedded +/// source): cloning shares the buffer. +#[derive(Debug, Clone)] +pub struct SharedBytes(pub std::sync::Arc>); + +impl AsRef<[u8]> for SharedBytes { + fn as_ref(&self) -> &[u8] { + self.0.as_slice() + } +} + +/// The input of a [`PngDecodeRows`] decoder: a cursor that also tells where the reader stopped, which +/// the text chunks after the image are read up to. +struct Tracked { + cursor: Cursor, + at: std::sync::Arc, +} + +impl> Tracked { + fn sync(&self) { + self.at.store(self.cursor.position(), std::sync::atomic::Ordering::Relaxed); + } +} + +impl> std::io::Read for Tracked { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + let n = self.cursor.read(buf)?; + self.sync(); + Ok(n) + } +} + +impl> std::io::BufRead for Tracked { + fn fill_buf(&mut self) -> std::io::Result<&[u8]> { + self.cursor.fill_buf() + } + + fn consume(&mut self, amt: usize) { + self.cursor.consume(amt); + self.sync(); + } +} + +impl> std::io::Seek for Tracked { + fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result { + let at = self.cursor.seek(pos)?; + self.sync(); + Ok(at) + } +} + +/// The decoder's input, shared between the reader and the metadata pass: moving an owned buffer in +/// doesn't keep a second copy of it. +struct SharedInput(std::sync::Arc); + +impl Clone for SharedInput { + fn clone(&self) -> Self { + Self(std::sync::Arc::clone(&self.0)) + } +} + +impl> AsRef<[u8]> for SharedInput { + fn as_ref(&self) -> &[u8] { + (*self.0).as_ref() + } +} + +/// A PNG decoded in resumable steps, for a caller that must not block for the whole image (the +/// web build, which has no threads): [`PngDecodeRows::step`] decodes rows until the caller says to +/// stop, and returns the [`Image`] once the last row and the chunks after it are read. [`decode`] +/// drives one to the end, so the two give the same image. +/// +/// An interlaced (Adam7) PNG decodes in a single step: its passes land at stride positions all +/// over the image, so no row is final before the last pass. +pub struct PngDecodeRows> { + reader: png::Reader>>, + at: std::sync::Arc, + bytes: SharedInput, + limits: Limits, + buf: Vec, + layout: ChannelLayout, + sample: SampleType, + /// The image's size (the first frame's, for an animation) and bytes per output row. + width: u32, + height: usize, + line: usize, + /// The decoder's whole output size, for an interlaced image's single frame decode. + size: usize, + rows: usize, + /// For [`PngDecodeRows::step_stream`]: the first row of the group held in `buf`. + group_start: usize, + interlaced: bool, + images: u64, + /// The chunks after the pixels: scanned from `text_at` up to `text_end`, a batch at a time, + /// with `text` keeping what was accepted and the decompressor's state. + text_at: usize, + text_end: usize, + text: TextScan, + /// True once the pixel reader has been finished and the tail is known. + tail: bool, + done: bool, + /// Test-only count of the pixel finalization: metadata yields must not repeat it. + #[cfg(test)] + pixel_finalizations: u32, +} + +impl> PngDecodeRows { + /// Reads the header and refuses what [`decode`] refuses before any pixel is decoded: sizes + /// past `limits`, and a file too small for the image it declares. + pub fn new(bytes: B, limits: &Limits) -> Result { + Self::open(bytes, limits, true) + } + + /// [`PngDecodeRows::new`], optionally without reserving the whole image: a caller that hands + /// the rows on as they come ([`decode_rows`]) never holds them all. + fn open(bytes: B, limits: &Limits, reserve: bool) -> Result { + let at = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)); + let bytes = SharedInput(std::sync::Arc::new(bytes)); + let input = Tracked { cursor: Cursor::new(bytes.clone()), at: at.clone() }; + let mut decoder = png::Decoder::new_with_limits(input, png::Limits { bytes: limits.alloc_usize() }); + decoder.set_transformations(png::Transformations::EXPAND); + // png's lazy text get_text() inflates without consulting its allocation limit. + // Read text separately below, without retaining compressed copies in the decoder. + decoder.set_ignore_text_chunk(true); + { + let info = decoder.read_header_info().map_err(map_png_err)?; + let (w, h) = info.size(); + // Size checks now; the allocation budget once the output layout (channels and sample + // size) is known below, so a Gray8 image isn't charged for RGBA16. + limits.check_size(w, h)?; + // zlib never beats 1032:1; refuse a file too small for the declared image before the + // output buffer exists. Measured on the stored samples (before palette/bit expansion). + let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(info.bits_per_pixel() as u64).div_ceil(8); + Limits::check_plausible(stored, bytes.as_ref().len(), 1100)?; + } + let reader = decoder.read_info().map_err(map_png_err)?; + // APNG: the IDAT image is the first frame, or an extra default image when no fcTL precedes it. + let images = reader.info().animation_control.map_or(1, |a| u64::from(a.num_frames) + u64::from(reader.info().frame_control.is_none())); + let (color, depth) = reader.output_color_type(); + let layout = match color { + png::ColorType::Grayscale => ChannelLayout::Gray, + png::ColorType::GrayscaleAlpha => ChannelLayout::GrayA, + png::ColorType::Rgb => ChannelLayout::Rgb, + png::ColorType::Rgba => ChannelLayout::Rgba, + png::ColorType::Indexed => return Err(err("palette was not expanded")), + }; + let sample = match depth { + png::BitDepth::Eight => SampleType::U8, + png::BitDepth::Sixteen => SampleType::U16, + d => return Err(err(format!("unexpected output bit depth {d:?}"))), + }; + // The frame the pixels are of: an fcTL before the IDAT overrides the header's size. + let info = reader.info(); + let (width, height) = info.frame_control.map_or_else(|| info.size(), |f| (f.width, f.height)); + let line = reader.output_line_size(width).ok_or_else(|| err("image too large"))?; + // The output layout is known: charge its real bytes per pixel, not RGBA16's worst case. + limits.check(width, height, layout, sample)?; + let height = usize::try_from(height).map_err(|_| err("image too large"))?; + if width == 0 || height == 0 { + return Err(err("the image is empty")); + } + let size = reader.output_buffer_size().ok_or_else(|| err("image too large"))?; + if line.checked_mul(height).is_none_or(|needed| needed > size) { + return Err(err("image too large")); + } + // Reserve without touching: each row is zero-filled as it is decoded, so the first slice + // doesn't zero-fill the whole image (a stall on the web build). + let mut buf = Vec::new(); + if reserve { + buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image")))?; + } + let interlaced = info.interlaced; + Ok(Self { + reader, + at, + bytes, + limits: *limits, + buf, + layout, + sample, + width, + height, + line, + size, + rows: 0, + group_start: 0, + interlaced, + images, + text_at: 8, + text_end: 0, + text: TextScan::default(), + tail: false, + done: false, + #[cfg(test)] + pixel_finalizations: 0, + }) + } + + /// [`PngDecodeRows::new`] without reserving the whole image, for a caller that streams the rows + /// ([`PngDecodeRows::step_stream`]) and so never holds them all. A caller that finds it must + /// decode the whole image after all (an interlaced file) calls + /// [`PngDecodeRows::reserve_output`] before [`PngDecodeRows::step`]. + pub fn new_streaming(bytes: B, limits: &Limits) -> Result { + Self::open(bytes, limits, false) + } + + /// Reserves the buffer [`PngDecodeRows::step`] decodes the whole image into. + pub fn reserve_output(&mut self) -> Result<(), CodecError> { + let size = self.size; + self.buf.try_reserve_exact(size).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {size} bytes for the image"))) + } + + /// Whether the rows come in final order, so [`PngDecodeRows::step_stream`] can hand them on: + /// not an interlaced file, not an APNG (its first frame may be a sub-rectangle), and not one + /// whose `eXIf` before the image data asks for another orientation. (An `eXIf` after the image + /// data shows only in [`PngExtras::orientation`] at the end.) + pub fn streamable(&self) -> bool { + let info = self.reader.info(); + let upright = info.exif_metadata.as_deref().is_none_or(|e| crate::orientation::exif_orientation(e) == 1); + !self.interlaced && info.animation_control.is_none() && upright + } + + /// The size and pixel format the rows of [`PngDecodeRows::step_stream`] come in. + pub fn header(&self) -> PngHeader { + PngHeader { width: self.width, height: u32::try_from(self.height).unwrap_or(u32::MAX), layout: self.layout, sample: self.sample } + } + + /// The image's width and height in pixels. + pub fn size(&self) -> (u32, u32) { + (self.width, u32::try_from(self.height).unwrap_or(u32::MAX)) + } + + /// How much of the image is decoded, `0.0..=1.0`. + pub fn progress(&self) -> f32 { + if self.done { 1.0 } else { self.rows as f32 / self.height.max(1) as f32 } + } + + /// Decodes at least one row (the whole image if it is interlaced), then goes on while `more()` + /// returns true. `Some` once the image is complete, after which the decoder is spent. + pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, CodecError> { + if self.done { + return Err(err("the image was already decoded")); + } + // Once the pixels are complete and the tail is known, a step only reads metadata: + // running the pixel reader again after `begin_tail` would decode an Adam7 frame twice + // or finish the image data again on every metadata yield. + if !self.tail { + if self.interlaced { + // An Adam7 frame is decoded in one go, over the whole output buffer. + self.buf.resize(self.size, 0); + let out = self.reader.next_frame(&mut self.buf).map_err(map_png_err)?; + self.buf.truncate(out.buffer_size()); + self.rows = self.height; + if self.sample == SampleType::U16 { + to_native_endian(&mut self.buf); + } + } + while self.rows < self.height { + let at = self.rows.checked_mul(self.line).ok_or_else(|| err("image too large"))?; + let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; + // Grow by one row at a time: the zero-fill of the output is spread over the decodes + // instead of stalling the first slice. + if self.buf.len() < end { + self.buf.resize(end, 0); + } + let row = self.buf.get_mut(at..end).ok_or_else(|| err("the image data is longer than the image"))?; + self.reader.read_row(row).map_err(map_png_err)?.ok_or_else(|| err("the image data ended early"))?; + if self.sample == SampleType::U16 { + to_native_endian(row); + } + self.rows += 1; + if self.rows < self.height && !more() { + return Ok(None); + } + } + if !self.interlaced { + #[cfg(test)] + { + self.pixel_finalizations += 1; + } + // No row is left: this reads on to the end of the image data. + if self.reader.read_row(&mut []).map_err(map_png_err)?.is_some() { + return Err(err("the image has more rows than its header declares")); + } + self.buf.truncate(self.line.saturating_mul(self.height)); + } + // The chunks after the pixels, one bounded batch at a time: a large text chunk never + // makes one step block, and what was accepted stays accepted when a step stops. + self.begin_tail()?; + } + while !self.text.done { + self.text_batch()?; + if !self.text.done && !more() { + return Ok(None); + } + } + self.done = true; + self.finish().map(Some) + } + + /// Decodes the rows of a [`streamable`](PngDecodeRows::streamable) file a group at a time: + /// `sink(y0, data)` gets whole rows `y0..` in final order, `group` of them at a time (the last + /// group may be shorter; 0 counts as 1), native-endian, exactly the samples [`decode`] would + /// return, and the decoder keeps only that group. Decodes at least one row, then goes on while + /// `more()` returns true; `Some` with the extras once the file is read to its end, with the + /// resolution a format that lacks one takes from its EXIF or XMP. A damaged file fails with + /// the error [`decode`] gives, after however many rows were delivered. An orientation (an + /// `eXIf` after the image data may set one) is for the caller to apply. + pub fn step_stream>( + &mut self, + group: u32, + more: &mut dyn FnMut() -> bool, + sink: &mut PngRowSink<'_, E>, + ) -> Result, E> { + if self.done { + return Err(err("the image was already decoded").into()); + } + let group = usize::try_from(group.max(1)).unwrap_or(usize::MAX); + if !self.tail { + while self.rows < self.height { + let held = self.rows - self.group_start; + let at = held.checked_mul(self.line).ok_or_else(|| err("image too large"))?; + let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; + if self.buf.len() < end { + self.buf.try_reserve_exact(end - self.buf.len()).map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; + self.buf.resize(end, 0); + } + let row = self.buf.get_mut(at..end).ok_or_else(|| err("invalid PNG row group"))?; + if self.reader.read_row(row).map_err(map_png_err)?.is_none() { + return Err(err("the image data ends before the last row").into()); + } + if self.sample == SampleType::U16 { + to_native_endian(row); + } + self.rows += 1; + if self.rows - self.group_start == group || self.rows == self.height { + let data = self.buf.get(..end).ok_or_else(|| err("invalid PNG row group"))?; + sink(u32::try_from(self.group_start).map_err(|_| err("image too large"))?, data)?; + self.group_start = self.rows; + } + if self.rows < self.height && !more() { + return Ok(None); + } + } + // Past the last row: trailing damage fails the same way it does in `decode`. + if self.reader.read_row(&mut []).map_err(map_png_err)?.is_some() { + return Err(err("the image has more rows than its header declares").into()); + } + self.begin_tail()?; + } + while !self.text.done { + self.text_batch()?; + if !self.text.done && !more() { + return Ok(None); + } + } + self.done = true; + let mut extras = self.extras(); + // As `decode_as_with` does for every format: a resolution the format lacks comes from EXIF or XMP. + if extras.meta.dpi.is_none() { + extras.meta.dpi = crate::resolution::from_metadata(extras.meta.exif.as_deref(), extras.meta.xmp.as_deref()); + } + Ok(Some(extras)) + } + + /// Finishes the pixel reader and fixes where the metadata scan ends. Tolerate malformed + /// tails once pixels are complete, but never swallow an allocation-limit failure. + fn begin_tail(&mut self) -> Result<(), CodecError> { + if let Err(e @ png::DecodingError::LimitsExceeded) = self.reader.finish() { + return Err(map_png_err(e)); + } + self.text_end = usize::try_from(self.at.load(std::sync::atomic::Ordering::Relaxed)).map_err(|_| err("invalid PNG reader position"))?; + self.text.remaining = self.limits.alloc_usize(); + self.tail = true; + Ok(()) + } + + /// Reads one bounded batch of the chunks after the pixels: up to [`TEXT_CHUNK_BATCH`] chunks, + /// or one batch of one text chunk's CRC or value while either is mid-stream. + fn text_batch(&mut self) -> Result<(), CodecError> { + let bytes = self.bytes.clone(); + let bytes = bytes.as_ref(); + if let Some(pending) = self.text.pending.take() { + match pending { + TextPending::Crc(p) => self.crc_batch(p, bytes)?, + TextPending::Value(p) => match inflate_text(p, bytes)? { + TextBatch::More(p) => self.text.pending = Some(TextPending::Value(p)), + TextBatch::Finished(p) => { + let (keyword, keyword_len, kind, text) = (p.keyword, p.keyword_len, p.kind, p.out); + self.text.accept(keyword, keyword_len, kind, text)?; + } + TextBatch::Invalid(produced) => self.text.charge(produced)?, + }, + } + return Ok(()); + } + let mut chunks = 0; + while chunks < TEXT_CHUNK_BATCH && !self.text.done { + if self.scan_text_chunk(bytes)? { + break; + } + chunks += 1; + } + Ok(()) + } + + /// Hashes one bounded batch of a text chunk's data. Only a fully CRC-valid chunk is parsed + /// into a value, so a broken chunk is still dropped before its keyword or flags are used. + fn crc_batch(&mut self, mut p: PendingCrc, bytes: &[u8]) -> Result<(), CodecError> { + let Some(left) = p.data.end.checked_sub(p.at) else { return Ok(()) }; + let take = left.min(TEXT_BATCH); + let Some(end) = p.at.checked_add(take) else { return Ok(()) }; + let Some(slice) = bytes.get(p.at..end) else { return Ok(()) }; + p.crc.update(slice); + p.at = end; + if p.at < p.data.end { + self.text.pending = Some(TextPending::Crc(p)); + return Ok(()); + } + if p.crc.sum() != p.checksum { + return Ok(()); + } + let Some(data) = bytes.get(p.data.clone()) else { return Ok(()) }; + if let Some(value) = self.pending_text(p.kind, data, p.data.start)? { + self.text.pending = Some(TextPending::Value(value)); + } + Ok(()) + } + + /// The value of a CRC-valid text chunk, with its decoded keyword and where its bytes start. + fn pending_text(&self, kind: [u8; 4], data: &[u8], data_at: usize) -> Result, CodecError> { + // A keyword is 1..=79 Latin-1 bytes, so only the start of the chunk can hold it. + let Some(nul) = data.iter().take(80).position(|&b| b == 0) else { return Ok(None) }; + let Some(keyword) = data.get(..nul) else { return Ok(None) }; + let Some(value_start) = nul.checked_add(1) else { return Ok(None) }; + let Some(value) = data.get(value_start..) else { return Ok(None) }; + if keyword.is_empty() { + return Ok(None); + } + let (value, compressed, latin1, kind) = match &kind { + b"tEXt" => (value, false, true, TextKind::Plain), + b"zTXt" => { + let Some((&0, value)) = value.split_first() else { return Ok(None) }; + (value, true, true, TextKind::Compressed) + } + _ => { + let Some(([flag, method], value)) = value.split_first_chunk::<2>() else { return Ok(None) }; + if *flag > 1 || (*flag == 1 && *method != 0) { + return Ok(None); + } + let Some((language, value)) = split_nul(value) else { return Ok(None) }; + let Some((translated, value)) = split_nul(value) else { return Ok(None) }; + if !language.is_ascii() || std::str::from_utf8(translated).is_err() { + return Ok(None); + } + (value, *flag == 1, false, TextKind::International) + } + }; + let keyword_len = latin1_len(keyword).ok_or_else(text_limit)?; + let keyword: String = keyword.iter().map(|&b| char::from(b)).collect(); + let Some(value_at) = data_at.checked_add(data.len().saturating_sub(value.len())) else { return Ok(None) }; + let Some(value_end) = value_at.checked_add(value.len()) else { return Ok(None) }; + Ok(Some(PendingText { + kind, + keyword, + keyword_len, + limit: self.text.remaining.saturating_sub(keyword_len), + input: value_at..value_end, + at: value_at, + latin1, + decoder: compressed.then(|| flate2::Decompress::new(true)), + // An uncompressed iTXt value's UTF-8 is checked batch by batch, before its budget. + utf8: (!compressed && !latin1).then(Utf8Check::default), + over: false, + out: Vec::new(), + })) + } + + /// Reads the header of the next chunk, starting a text chunk's CRC (which then reads its + /// bytes in batches) or stepping over any other. False when the scan can go on in this + /// batch; true when a text chunk was started and its bytes need their own batches. Sets + /// [`TextScan::done`] at the end of the tail, at `IEND`, or at a malformed one, as the + /// previous one-pass reader did. + fn scan_text_chunk(&mut self, bytes: &[u8]) -> Result { + let Some(rest) = bytes.get(self.text_at..self.text_end) else { + self.text.done = true; + return Ok(false); + }; + let Some((length, rest)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let length = u32::from_be_bytes(*length); + // PNG chunk lengths are at most 2^31-1. Broken tails remain tolerated. + if length > i32::MAX as u32 { + self.text.done = true; + return Ok(false); + } + let Ok(length) = usize::try_from(length) else { + self.text.done = true; + return Ok(false); + }; + let Some((kind, rest)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let Some((_data, rest)) = rest.split_at_checked(length) else { + self.text.done = true; + return Ok(false); + }; + let Some((checksum, _)) = rest.split_first_chunk::<4>() else { + self.text.done = true; + return Ok(false); + }; + let Some(next) = self.text_at.checked_add(12).and_then(|at| at.checked_add(length)).filter(|&at| at <= self.text_end) else { + self.text.done = true; + return Ok(false); + }; + let Some(data_at) = self.text_at.checked_add(8) else { + self.text.done = true; + return Ok(false); + }; + self.text_at = next; + if kind == b"IEND" { + self.text.done = true; + return Ok(false); + } + if !matches!(kind, b"tEXt" | b"zTXt" | b"iTXt") { + return Ok(false); + } + let mut crc = flate2::Crc::new(); + crc.update(kind); + let Some(data_end) = data_at.checked_add(length) else { + self.text.done = true; + return Ok(false); + }; + // The CRC of a large chunk is hashed a bounded batch at a time too, so one metadata + // step never scans the whole chunk. + self.text.pending = + Some(TextPending::Crc(PendingCrc { kind: *kind, data: data_at..data_end, at: data_at, crc, checksum: u32::from_be_bytes(*checksum) })); + Ok(true) + } + + /// What the file holds besides its pixels, with what the metadata scan accepted. + fn extras(&mut self) -> PngExtras { + let info = self.reader.info(); + let icc = info.icc_profile.as_ref().map(|c| c.to_vec()); + let mut meta = Metadata { exif: info.exif_metadata.as_ref().map(|c| c.to_vec()), ..Default::default() }; + if let Some(d) = info.pixel_dims + && d.unit == png::Unit::Meter + && d.xppu > 0 + && d.yppu > 0 + { + meta.dpi = Some((dpi_of_ppm(d.xppu), dpi_of_ppm(d.yppu))); + } + meta.text = std::mem::take(&mut self.text.plain); + meta.text.extend(std::mem::take(&mut self.text.compressed)); + meta.text.extend(std::mem::take(&mut self.text.international)); + meta.xmp = self.text.xmp.take(); + let mut warnings = Vec::new(); + if self.images > 1 { + warnings.push(DecodeWarning::MoreFrames { total: u32::try_from(self.images).ok() }); + } + let orientation = meta.exif.as_deref().map_or(1, crate::orientation::exif_orientation); + PngExtras { icc, meta, warnings, orientation } + } + + /// The image itself, with what the metadata scan accepted. + fn finish(&mut self) -> Result { + let height = u32::try_from(self.height).map_err(|_| err("image too large"))?; + let mut img = Image::from_raw(self.width, height, self.layout, self.sample, std::mem::take(&mut self.buf))?; + let extras = self.extras(); + img.icc = extras.icc; + img.meta = extras.meta; + img.warnings = extras.warnings; + Ok(img) + } +} + +/// 16-bit samples from the file's big-endian bytes to the machine's. +fn to_native_endian(samples: &mut [u8]) { + for c in samples.as_chunks_mut::<2>().0 { + let v = u16::from_be_bytes([c[0], c[1]]); + c.copy_from_slice(&v.to_ne_bytes()); + } +} + +/// What a PNG holds besides its pixels. These can sit after the image data (`eXIf`, text), so +/// they are complete only once the pixels have been read. +pub struct PngExtras { + pub icc: Option>, + pub meta: Metadata, + pub warnings: Vec, + /// The EXIF orientation (1-8) the pixels are stored in; 1 when there is none. + pub orientation: u16, +} + +/// Inspect APNG's declared images, including a separate default image, without decoding pixels. +pub(crate) fn has_additional_images(bytes: &[u8]) -> Result { + let limits = Limits::default(); + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: limits.alloc_usize() }); + decoder.set_ignore_text_chunk(true); + let reader = decoder.read_info().map_err(map_png_err)?; + let info = reader.info(); + Ok(info.animation_control.is_some_and(|a| u64::from(a.num_frames) + u64::from(info.frame_control.is_none()) > 1)) +} + +/// The size and pixel format the rows of [`decode_rows`] come in. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct PngHeader { + pub width: u32, + pub height: u32, + pub layout: ChannelLayout, + pub sample: SampleType, +} + +/// Where [`decode_rows`] sends rows: the index of the first, then whole rows. +pub type PngRowSink<'a, E> = dyn FnMut(u32, &[u8]) -> Result<(), E> + 'a; + +/// Decodes a PNG a group of rows at a time, so the caller never needs the whole decoded image: +/// `begin` gets the header once and says whether to go on (after the same size and plausibility checks [`decode`] makes), +/// then `rows(y0, data)` gets whole rows `y0..` in final order, `group` of them at a time (the +/// last group may be shorter; a `group` of 0 counts as 1), native-endian, exactly the samples [`decode`] would return. Returns the extras once the file +/// has been read to its end, or `None` (having called neither closure) when the rows can't be +/// had in final order: an interlaced (Adam7) file, and an APNG, whose first frame may be a +/// sub-rectangle. A damaged file fails with the error [`decode`] gives, after however many rows +/// were delivered. The limits are checked before the first row; an orientation, which an `eXIf` +/// after the image data may set, is for the caller to apply. +pub(crate) fn decode_rows>( + bytes: &[u8], + limits: &Limits, + group: u32, + begin: &mut dyn FnMut(&PngHeader) -> Result, + rows: &mut PngRowSink<'_, E>, +) -> Result, E> { + // The limits and plausibility checks are made here, before any row; the output is not reserved. + let mut dec = PngDecodeRows::new_streaming(bytes, limits)?; + if !dec.streamable() || !begin(&dec.header())? { + return Ok(None); + } + // With a `more` that always says go on, the first call decodes the whole file. + dec.step_stream(group, &mut || true, rows) +} + +/// The colour table of a palette PNG (`PLTE`) and the one entry its `tRNS` makes transparent. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct PngPalette { + pub colors: Vec<[u8; 3]>, + pub transparent: Option, +} + +/// The colour table of a palette PNG (colour type 3), read from the headers without decoding +/// pixels. `None` for other colour types, and for a `tRNS` with partial alpha or several +/// transparent entries: an Indexed Color document holds one transparent entry. +pub fn palette(bytes: &[u8]) -> Result, CodecError> { + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits::default()); + decoder.set_ignore_text_chunk(true); + let reader = decoder.read_info().map_err(map_png_err)?; + let info = reader.info(); + if info.color_type != png::ColorType::Indexed { + return Ok(None); + } + let Some(plte) = info.palette.as_deref() else { return Ok(None) }; + let colors = plte.as_chunks::<3>().0.to_vec(); + let mut transparent = None; + for (i, &a) in info.trns.as_deref().unwrap_or_default().iter().enumerate() { + match a { + 255 => {} + 0 if transparent.is_none() && i < colors.len() => transparent = u8::try_from(i).ok(), + _ => return Ok(None), + } + } + Ok(Some(PngPalette { colors, transparent })) +} + +/// The stereo pairing a PNG declares in its `sTER` chunk (PNG Extensions 1.4.0, §3.6), read +/// from the headers without decoding pixels. `None` when the file has no CRC-valid `sTER` with a +/// defined mode byte. The first valid chunk wins, as a later one would contradict it. +pub fn stereo(bytes: &[u8]) -> Option { + let mut rest = bytes.get(8..)?; + while let Some((length, tail)) = rest.split_first_chunk::<4>() { + let Some((kind, tail)) = tail.split_first_chunk::<4>() else { break }; + let length = u32::from_be_bytes(*length); + if length > i32::MAX as u32 { + break; + } + let Ok(length) = usize::try_from(length) else { break }; + let Some(data) = tail.get(..length) else { break }; + let Some((checksum, next)) = tail.get(length..).and_then(<[u8]>::split_first_chunk::<4>) else { break }; + rest = next; + if kind == b"IEND" { + break; + } + if kind != b"sTER" || data.len() != 1 { + continue; + } + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + if crc.sum() != u32::from_be_bytes(*checksum) { + continue; + } + return PngStereo::from_mode(data[0]); + } + None +} + +fn map_png_err(e: png::DecodingError) -> CodecError { + match e { + png::DecodingError::LimitsExceeded => CodecError::LimitExceeded("PNG decoder memory limit".into()), + e => err(e), + } +} + +fn text_limit() -> CodecError { + CodecError::LimitExceeded("PNG decoded text metadata exceeds max_alloc".into()) +} + +fn split_nul(bytes: &[u8]) -> Option<(&[u8], &[u8])> { + let at = bytes.iter().position(|&b| b == 0)?; + Some((bytes.get(..at)?, bytes.get(at.checked_add(1)?..)?)) +} + +fn latin1_len(bytes: &[u8]) -> Option { + bytes.len().checked_add(bytes.iter().filter(|&&b| b >= 128).count()) +} + +/// The text chunks after the pixels, accepted one bounded batch at a time: one shared budget for +/// decoded UTF-8 keywords and values (including overwritten XMP), the CRC and decompressor of a +/// chunk kept between batches. Invalid ancillary text stays optional, as it was with png's text +/// decoder. +#[derive(Default)] +struct TextScan { + /// UTF-8 bytes left of [`Limits::max_alloc`] for the text. + remaining: usize, + plain: Vec<(String, String)>, + compressed: Vec<(String, String)>, + international: Vec<(String, String)>, + xmp: Option, + pending: Option, + done: bool, +} + +impl TextScan { + /// Charges `produced` bytes to the shared budget: what a chunk inflated counts even when its + /// text is then dropped (bad UTF-8, a corrupt or truncated stream), or every rejected chunk + /// could inflate up to the whole remaining budget again. + fn charge(&mut self, produced: usize) -> Result<(), CodecError> { + self.remaining = self.remaining.checked_sub(produced).ok_or_else(text_limit)?; + Ok(()) + } + + /// Keeps a decoded value, charging the keyword and text to the shared budget; invalid UTF-8 + /// text is dropped, but what it inflated is still charged. + fn accept(&mut self, keyword: String, keyword_len: usize, kind: TextKind, out: Vec) -> Result<(), CodecError> { + let produced = out.len(); + let Ok(text) = String::from_utf8(out) else { return self.charge(produced) }; + self.remaining = self.remaining.checked_sub(keyword_len).and_then(|left| left.checked_sub(text.len())).ok_or_else(text_limit)?; + match kind { + // Some writers put XMP in Latin-1 text chunks instead of the usual iTXt. + // Treat the reserved keyword consistently, after the same budget checks. + _ if keyword == XMP_KEYWORD => self.xmp = Some(text), + TextKind::Plain => self.plain.push((keyword, text)), + TextKind::Compressed => self.compressed.push((keyword, text)), + TextKind::International => self.international.push((keyword, text)), + } + Ok(()) + } +} + +/// Which list a text chunk is kept in, as the previous decoder grouped them by chunk type. +#[derive(Clone, Copy, PartialEq, Eq)] +enum TextKind { + Plain, + Compressed, + International, +} + +/// A text chunk being read: its CRC first, then (when valid) its value. +enum TextPending { + Crc(PendingCrc), + Value(PendingText), +} + +/// The CRC of a text chunk's data, hashed a bounded batch at a time before the chunk is parsed. +struct PendingCrc { + kind: [u8; 4], + /// The chunk's data in the file, and the next byte a batch hashes. + data: std::ops::Range, + at: usize, + crc: flate2::Crc, + checksum: u32, +} + +/// A text chunk's value being read, with its position in the file and (when compressed) the +/// inflator kept between batches. +struct PendingText { + kind: TextKind, + keyword: String, + keyword_len: usize, + /// The most decoded bytes this value may take: the budget left minus its keyword. + limit: usize, + /// The value's bytes in the file (the compressed ones for zTXt and compressed iTXt). + input: std::ops::Range, + /// The next byte of `input` a batch reads or inflates. + at: usize, + latin1: bool, + /// Some for a compressed value. + decoder: Option, + /// Some for an uncompressed iTXt value: its UTF-8 is checked before its bytes are charged. + utf8: Option, + /// The value has outgrown the budget but its UTF-8 validity is still being checked, so an + /// invalid value is still dropped instead of failing the decode. + over: bool, + out: Vec, +} + +/// Incremental UTF-8 validation across batches, so a value that spans batches is checked as a +/// whole before it can fail the budget. A sequence straddling two batches keeps its expected +/// continuation range here. +#[derive(Default)] +struct Utf8Check { + /// Continuation bytes still needed, and the range the next one may take. + need: u8, + min: u8, + max: u8, +} + +impl Utf8Check { + /// Feeds the next bytes of the value; false at the first one that isn't valid UTF-8. + fn feed(&mut self, bytes: &[u8]) -> bool { + for &b in bytes { + if self.need > 0 { + if b < self.min || b > self.max { + return false; + } + self.need -= 1; + (self.min, self.max) = (0x80, 0xBF); + } else { + match b { + 0x00..=0x7f => {} + 0xc2..=0xdf => { + self.need = 1; + (self.min, self.max) = (0x80, 0xBF); + } + 0xe0 => { + self.need = 2; + (self.min, self.max) = (0xa0, 0xBF); + } + 0xe1..=0xec | 0xee..=0xef => { + self.need = 2; + (self.min, self.max) = (0x80, 0xBF); + } + 0xed => { + self.need = 2; + (self.min, self.max) = (0x80, 0x9f); + } + 0xf0 => { + self.need = 3; + (self.min, self.max) = (0x90, 0xBF); + } + 0xf1..=0xf3 => { + self.need = 3; + (self.min, self.max) = (0x80, 0xBF); + } + 0xf4 => { + self.need = 3; + (self.min, self.max) = (0x80, 0x8f); + } + _ => return false, + } + } + } + true + } + + /// Whether the bytes so far end on a whole character. + fn complete(&self) -> bool { + self.need == 0 + } +} + +/// What one batch of a text value produced. +enum TextBatch { + /// The value needs another batch. + More(PendingText), + /// Every byte of the value was read; `out` is its UTF-8 text. + Finished(PendingText), + /// A broken chunk: drop it, as the previous reader did. + /// The bytes it inflated, which are charged to the budget even though the text is dropped. + Invalid(usize), +} + +/// How much of one text value one batch reads or inflates. +const TEXT_BATCH: usize = 8192; +/// How many chunks one batch scans when it isn't continuing a text value. +const TEXT_CHUNK_BATCH: usize = 64; + +/// Reads one batch of a text value. Inflation uses a fixed scratch buffer and checks each batch +/// against the budget before growing the output, so no input-sized allocation is made first. +fn inflate_text(mut p: PendingText, bytes: &[u8]) -> Result { + let Some(input) = bytes.get(p.input.clone()) else { return Ok(TextBatch::Invalid(p.out.len())) }; + let mut scratch = [0; TEXT_BATCH]; + if let Some(decoder) = p.decoder.as_mut() { + let Some(tail) = input.get(p.at.saturating_sub(p.input.start)..) else { return Ok(TextBatch::Invalid(p.out.len())) }; + let start_in = decoder.total_in(); + let start_out = decoder.total_out(); + let status = decoder.decompress(tail, &mut scratch, flate2::FlushDecompress::None); + let used = usize::try_from(decoder.total_in().saturating_sub(start_in)).unwrap_or(usize::MAX); + let made = usize::try_from(decoder.total_out().saturating_sub(start_out)).unwrap_or(usize::MAX); + let Ok(status) = status else { + // The backend may write a batch before it reports corruption: charge it too, Latin-1 + // text at its UTF-8 length as on the success path. + let charged = match scratch.get(..made) { + Some(batch) if p.latin1 => latin1_len(batch).unwrap_or(usize::MAX), + _ => made, + }; + return Ok(TextBatch::Invalid(p.out.len().saturating_add(charged))); + }; + let (Some(used), Some(batch)) = (p.at.checked_add(used), scratch.get(..made)) else { return Ok(TextBatch::Invalid(p.out.len())) }; + p.at = used; + if !append_checked(&mut p.out, &mut p.utf8, &mut p.over, batch, p.latin1, p.limit)? { + return Ok(TextBatch::Invalid(p.out.len())); + } + if status == flate2::Status::StreamEnd { + return Ok(TextBatch::Finished(p)); + } + if start_in == decoder.total_in() && start_out == decoder.total_out() { + return Ok(TextBatch::Invalid(p.out.len())); // Truncated stream: never keep partial text. + } + } else { + let remaining = input.len().saturating_sub(p.at.saturating_sub(p.input.start)); + let take = remaining.min(TEXT_BATCH); + let start = p.at.saturating_sub(p.input.start); + let Some(batch) = input.get(start..start.saturating_add(take)) else { return Ok(TextBatch::Invalid(0)) }; + p.at = p.at.saturating_add(take); + if !append_checked(&mut p.out, &mut p.utf8, &mut p.over, batch, p.latin1, p.limit)? { + // Stored text isn't inflated: nothing is charged for it. + return Ok(TextBatch::Invalid(0)); + } + if take == remaining { + if p.utf8.as_ref().is_some_and(|check| !check.complete()) { + return Ok(TextBatch::Invalid(0)); + } + if p.over { + return Err(text_limit()); + } + return Ok(TextBatch::Finished(p)); + } + } + Ok(TextBatch::More(p)) +} + +/// Appends one batch of decoded bytes, refusing to grow past the budget. Every Latin-1 code +/// point fits in at most two UTF-8 bytes. +/// +/// An uncompressed iTXt value is UTF-8 checked batch by batch before it is charged: an invalid +/// value is dropped even when it is over the budget, as the previous whole-value check did. +/// Its budget error is deferred until every byte has been seen and found valid. +fn append_checked(out: &mut Vec, utf8: &mut Option, over: &mut bool, bytes: &[u8], latin1: bool, limit: usize) -> Result { + if let Some(check) = utf8.as_mut() + && !check.feed(bytes) + { + return Ok(false); + } + if *over { + return Ok(true); + } + let added = if latin1 { latin1_len(bytes).ok_or_else(text_limit)? } else { bytes.len() }; + if out.len().checked_add(added).is_none_or(|len| len > limit) { + if utf8.is_some() { + *over = true; + return Ok(true); + } + return Err(text_limit()); + } + out.try_reserve(added).map_err(|_| text_limit())?; + if latin1 { + let mut buf = [0; 2]; + for &b in bytes { + out.extend_from_slice(char::from(b).encode_utf8(&mut buf).as_bytes()); + } + } else { + out.extend_from_slice(bytes); + } + Ok(true) +} + +fn is_latin1_keyword(k: &str) -> bool { + !k.is_empty() && k.len() <= 79 && k.chars().all(|c| (' '..='~').contains(&c) || ('\u{a1}'..='\u{ff}').contains(&c)) +} + +/// Makes the PNG writer's output growth report an encode error on allocation failure. +struct FallibleOutput<'a>(&'a mut Vec); + +impl Write for FallibleOutput<'_> { + fn write(&mut self, bytes: &[u8]) -> io::Result { + self.0.try_reserve(bytes.len()).map_err(|_| io::Error::other("not enough memory for PNG output"))?; + self.0.extend_from_slice(bytes); + Ok(bytes.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let bpp = img.layout().channels() * img.sample_type().bytes(); + // Big-endian sample bytes. + let mut data = std::borrow::Cow::Borrowed(img.data()); + if img.sample_type() == SampleType::U16 { + to_big_endian(data.to_mut()); + } + let mut out = Vec::new(); + { + let mut output = FallibleOutput(&mut out); + let mut writer = start(&mut output, (w, h), (img.layout(), img.sample_type()), img.icc.as_ref(), &img.meta, opts)?; + if opts.png_interlaced { + let idat = adam7_idat(&data, w as usize, h as usize, bpp, opts.png_compression)?; + write_idat(&mut writer, &idat, MAX_IDAT)?; + } else if let Some(idat) = parallel_idat(&data, w as usize, h as usize, bpp, opts.png_compression) { + write_idat(&mut writer, &idat, MAX_IDAT)?; + } else { + writer.write_image_data(&data).map_err(|e| CodecError::encode(F, e))?; + } + writer.finish().map_err(|e| CodecError::encode(F, e))?; + } + Ok(out) +} + +/// 16-bit native-endian samples to PNG's big-endian order, in place. +fn to_big_endian(data: &mut [u8]) { + for c in data.as_chunks_mut::<2>().0 { + let v = u16::from_ne_bytes([c[0], c[1]]); + c.copy_from_slice(&v.to_be_bytes()); + } +} + +/// Writes the signature and every chunk ahead of the image data for a `w` × `h` PNG of +/// `layout` and `sample` (already the encode plan's), with its profile and metadata. +fn start( + out: W, + (w, h): (u32, u32), + (layout, sample): (ChannelLayout, SampleType), + icc: Option<&Vec>, + meta: &Metadata, + opts: &EncodeOptions, +) -> Result, CodecError> { + let color = match layout { + ChannelLayout::Gray => png::ColorType::Grayscale, + ChannelLayout::GrayA => png::ColorType::GrayscaleAlpha, + ChannelLayout::Rgb => png::ColorType::Rgb, + ChannelLayout::Rgba => png::ColorType::Rgba, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let depth = match sample { + SampleType::U8 => png::BitDepth::Eight, + SampleType::U16 => png::BitDepth::Sixteen, + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + }; + let mut info = png::Info::with_size(w, h); + info.color_type = color; + info.bit_depth = depth; + info.interlaced = opts.png_interlaced; + if opts.embed_icc + && let Some(icc) = icc + { + info.icc_profile = Some(icc.clone().into()); + } + if opts.embed_metadata { + if let Some(exif) = &meta.exif { + // The pixels are written as they are shown: never let a viewer rotate them again. + info.exif_metadata = Some(crate::resolution::export_exif(exif, meta.dpi).into_owned().into()); + } + if let Some((x, y)) = meta.dpi + && x > 0.0 + && y > 0.0 + { + info.pixel_dims = + Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + } + } + let mut encoder = png::Encoder::with_info(out, info).map_err(|e| CodecError::encode(F, e))?; + encoder.set_compression(match opts.png_compression { + PngCompression::None => png::Compression::NoCompression, + PngCompression::Fast => png::Compression::Fast, + PngCompression::Default => png::Compression::Balanced, + PngCompression::Best => png::Compression::High, + }); + if opts.embed_metadata { + for (k, v) in &meta.text { + let latin1_text = v.chars().all(|c| (c as u32) < 256); + let res = if is_latin1_keyword(k) && latin1_text { + encoder.add_text_chunk(k.clone(), v.clone()) + } else if is_latin1_keyword(k) { + encoder.add_itxt_chunk(k.clone(), v.clone()) + } else { + continue; + }; + res.map_err(|e| CodecError::encode(F, e))?; + } + if let Some(xmp) = &meta.xmp { + encoder.add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()).map_err(|e| CodecError::encode(F, e))?; + } + } + let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; + if let Some(stereo) = opts.png_stereo { + // Ancillary chunk before IDAT, as the spec recommends for `sTER`. + writer.write_chunk(png::chunk::ChunkType(*b"sTER"), &[stereo.mode()]).map_err(|e| CodecError::encode(F, e))?; + } + Ok(writer) +} + +/// Largest IDAT chunk written. A chunk holds at most 2^31 - 1 bytes, so a zlib stream past that +/// (a multi-gigapixel image) is split over several IDAT chunks, which readers join back. +const MAX_IDAT: usize = 1 << 30; + +/// `zlib` as IDAT chunks of at most `max` bytes. +fn write_idat(writer: &mut png::Writer, zlib: &[u8], max: usize) -> Result<(), CodecError> { + for part in zlib.chunks(max.max(1)) { + writer.write_chunk(png::chunk::IDAT, part).map_err(|e| CodecError::encode(F, e))?; + } + Ok(()) +} + +/// A PNG encoded a band of rows at a time, for images too large to hold whole (a multi-gigapixel +/// print): each band is converted to the encode plan, filtered and deflated on all cores as it +/// arrives and written as IDAT chunks, so only about one band is ever held. The deflate bands are +/// [`parallel_idat`]'s, so the zlib stream is the one [`crate::encode`] writes for a large image. +/// Never interlaced (Adam7 needs the whole image). +pub struct PngRows { + writer: png::Writer, + plan: Plan, + width: u32, + height: u32, + /// Rows received so far. + rows: u32, + level: PngCompression, + /// Converted, big-endian rows that don't fill a deflate band yet. + pending: Vec, + /// The last row deflated: the row above `pending` for the filters. + prev: Vec, + adler: u32, + /// Whether the zlib header is written. + started: bool, +} + +impl PngRows { + /// Writes the header of a `first.width()` × `height` PNG to `out`. The layout, sample type, + /// profile and metadata come from `first`, the first band (not written yet: [`Self::push`] + /// it like the others), through the same plan as [`crate::encode`]. + pub fn new(out: W, first: &Image, height: u32, opts: &EncodeOptions) -> Result { + if opts.png_interlaced { + return Err(CodecError::unsupported(F, "an interlaced PNG can't be written a band of rows at a time")); + } + let width = first.width(); + if width == 0 || height == 0 { + return Err(CodecError::InvalidImage("cannot encode an empty image".into())); + } + let plan = crate::fidelity::plan(first, F, opts); + let writer = start(out, (width, height), (plan.layout, plan.sample), first.icc.as_ref(), &first.meta, opts)?; + Ok(Self { writer, plan, width, height, rows: 0, level: opts.png_compression, pending: Vec::new(), prev: Vec::new(), adler: 1, started: false }) + } + + /// Encodes the next rows of the image: `band` is as wide as the image, in any layout and + /// sample type (converted like the first band). + pub fn push(&mut self, band: &Image) -> Result<(), CodecError> { + if band.width() != self.width { + return Err(CodecError::InvalidImage(format!("a {} px wide band for a {} px wide image", band.width(), self.width))); + } + let rows = self.rows.checked_add(band.height()).filter(|&r| r <= self.height); + let Some(rows) = rows else { + return Err(CodecError::InvalidImage(format!("more than the image's {} rows", self.height))); + }; + let img = band.converted(self.plan.layout, self.plan.sample); + let at = self.pending.len(); + self.pending.try_reserve(img.data().len()).map_err(|_| CodecError::encode(F, "not enough memory for a band of rows"))?; + self.pending.extend_from_slice(img.data()); + if self.plan.sample == SampleType::U16 { + to_big_endian(self.pending.get_mut(at..).unwrap_or_default()); + } + self.rows = rows; + self.deflate_pending() + } + + /// Deflates the whole bands held, and the rest once every row is in. + fn deflate_pending(&mut self) -> Result<(), CodecError> { + let bpp = self.plan.layout.channels() * self.plan.sample.bytes(); + let stride = self.width as usize * bpp; + let per_band = band_rows(stride); + let held = self.pending.len() / stride.max(1); + let finish = self.rows == self.height; + let take = if finish { held } else { held / per_band * per_band }; + if take == 0 { + return Ok(()); + } + let rows = self.pending.get(..take * stride).unwrap_or_default(); + let prev = (!self.prev.is_empty()).then_some(self.prev.as_slice()); + let parts = deflate_rows(rows, prev, stride, bpp, per_band, deflate_level(self.level), finish, encode_threads()) + .ok_or_else(|| CodecError::encode(F, "a band of rows could not be compressed"))?; + let mut zlib = Vec::new(); + zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; + if !self.started { + zlib.extend_from_slice(&zlib_header(self.level)); + self.started = true; + } + for p in &parts { + zlib.extend_from_slice(&p.z); + self.adler = adler32_combine(self.adler, p.adler, p.len); + } + if finish { + zlib.extend_from_slice(&self.adler.to_be_bytes()); + } + write_idat(&mut self.writer, &zlib, MAX_IDAT)?; + self.prev.clear(); + self.prev.extend_from_slice(rows.get(rows.len() - stride..).unwrap_or_default()); + self.pending.drain(..take * stride); + Ok(()) + } + + /// Ends the file. Fails when fewer rows than the image's height were pushed. + pub fn finish(self) -> Result<(), CodecError> { + if self.rows != self.height || !self.pending.is_empty() { + return Err(CodecError::InvalidImage(format!("{} of the image's {} rows were written", self.rows, self.height))); + } + self.writer.finish().map_err(|e| CodecError::encode(F, e)) + } +} + +/// Build the zlib stream for an Adam7-interlaced image (filter type 0). +fn adam7_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Result, CodecError> { + // (x0, y0, dx, dy) per pass. + const PASSES: [(usize, usize, usize, usize); 7] = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; + let mut raw = Vec::new(); + raw.try_reserve_exact(data.len() + h * 7).map_err(|_| CodecError::encode(F, "not enough memory for PNG rows"))?; + for (x0, y0, dx, dy) in PASSES { + if x0 >= w || y0 >= h { + continue; + } + let mut y = y0; + while y < h { + raw.push(0u8); + let mut x = x0; + while x < w { + let o = (y * w + x) * bpp; + raw.extend_from_slice(&data[o..o + bpp]); + x += dx; + } + y += dy; + } + } + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), deflate_level(level)); + z.write_all(&raw).map_err(|e| CodecError::encode(F, e))?; + z.finish().map_err(|e| CodecError::encode(F, e)) +} + +/// The deflate level of [`PngCompression::Fast`]: zlib-rs level 1 writes static Huffman blocks +/// only (20-30% bigger on filtered photos); level 3 is the lowest that is never bigger than +/// miniz's level 1 was, at about twice its time. +const FAST: flate2::Compression = flate2::Compression::new(3); + +/// Images at least this large (bytes) are filtered and deflated on all cores. +const PARALLEL_MIN_BYTES: usize = 4 << 20; + +/// The zlib stream of a non-interlaced image, built in parallel (`None` below +/// [`PARALLEL_MIN_BYTES`], on wasm, or without compression): rows get the adaptive filter +/// (minimum sum of absolute differences over the five PNG filters, as libpng and the `png` +/// crate do), then bands of rows are deflated independently at the requested level, each +/// ending on a sync flush so the raw deflate streams concatenate into one (as `pigz` does). +/// A 36 MP RGB export drops from ~9 s to well under a second on 12 cores; the output is a few +/// percent larger (no dictionary across bands). +fn parallel_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Option> { + if cfg!(target_arch = "wasm32") || data.len() < PARALLEL_MIN_BYTES || level == PngCompression::None || w == 0 || h == 0 { + return None; + } + banded_idat(data, w, h, bpp, level, encode_threads()) +} + +/// Filtered bytes per deflate band. The bands depend only on the image, never on the number of +/// threads, so the same image and options give the same bytes whatever the machine's core count. +const BAND_BYTES: usize = 512 << 10; + +/// The deflate effort for `level`. +fn deflate_level(level: PngCompression) -> flate2::Compression { + match level { + PngCompression::None => flate2::Compression::none(), + PngCompression::Fast => FAST, + PngCompression::Default => flate2::Compression::default(), + PngCompression::Best => flate2::Compression::best(), + } +} + +/// The zlib stream header: deflate, 32 K window; FLEVEL only advertises the effort. +fn zlib_header(level: PngCompression) -> [u8; 2] { + match level { + PngCompression::None | PngCompression::Fast => [0x78, 0x01], + PngCompression::Best => [0x78, 0xDA], + PngCompression::Default => [0x78, 0x9C], + } +} + +/// Rows per deflate band for rows of `stride` bytes. +fn band_rows(stride: usize) -> usize { + (BAND_BYTES / stride.saturating_add(1)).max(1) +} + +/// Worker threads for the banded encoders: one on wasm (no threads), else the machine's cores. +fn encode_threads() -> usize { + if cfg!(target_arch = "wasm32") { 1 } else { std::thread::available_parallelism().map_or(1, |n| n.get()).clamp(1, 32) } +} + +/// [`parallel_idat`] on `threads` worker threads. +fn banded_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression, threads: usize) -> Option> { + let stride = w * bpp; + let parts = deflate_rows(data.get(..h * stride)?, None, stride, bpp, band_rows(stride), deflate_level(level), true, threads)?; + let mut out = Vec::new(); + out.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).ok()?; + out.extend_from_slice(&zlib_header(level)); + let mut adler = 1u32; + for p in &parts { + out.extend_from_slice(&p.z); + adler = adler32_combine(adler, p.adler, p.len); + } + out.extend_from_slice(&adler.to_be_bytes()); + Some(out) +} + +/// One band of rows filtered and deflated: the raw deflate bytes, and the Adler-32 and length of +/// the filtered bytes they hold. +struct Deflated { + z: Vec, + adler: u32, + len: usize, +} + +/// `rows` (whole rows of `stride` bytes; `prev` is the row above the first, if any) filtered and +/// deflated in independent bands of `band_rows` rows on up to `threads` workers (inline for one). +/// The last band finishes the deflate stream when `finish`; every other band ends on a sync flush. +/// `None` when a band did not deflate in one call ([`deflate_band`]). +#[allow(clippy::too_many_arguments)] +fn deflate_rows( + rows: &[u8], + prev: Option<&[u8]>, + stride: usize, + bpp: usize, + band_rows: usize, + lvl: flate2::Compression, + finish: bool, + threads: usize, +) -> Option> { + let h = rows.len() / stride.max(1); + let bands: Vec<(usize, usize)> = (0..h).step_by(band_rows.max(1)).map(|y| (y, (y + band_rows).min(h))).collect(); + let band = |&(y0, y1): &(usize, usize)| -> Option { + let mut filtered = Vec::new(); + filtered.try_reserve_exact((y1 - y0) * (stride + 1)).ok()?; + let mut scratch = Vec::new(); + scratch.try_reserve_exact(stride).ok()?; + scratch.resize(stride, 0); + for y in y0..y1 { + let row = rows.get(y * stride..(y + 1) * stride)?; + let above = if y > 0 { rows.get((y - 1) * stride..y * stride) } else { prev }; + filter_row(row, above, bpp, &mut scratch, &mut filtered); + } + let adler = adler32(1, &filtered); + let z = deflate_band(&filtered, lvl, finish && y1 == h)?; + Some(Deflated { z, adler, len: filtered.len() }) + }; + if threads <= 1 || bands.len() <= 1 { + return bands.iter().map(band).collect(); + } + let next = std::sync::atomic::AtomicUsize::new(0); + let mut parts: Vec<(usize, Deflated)> = std::thread::scope(|sc| { + let workers: Vec<_> = (0..threads.min(bands.len())) + .map(|_| { + sc.spawn(|| { + let mut out = Vec::new(); + loop { + let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let Some(b) = bands.get(i) else { break }; + let Some(d) = band(b) else { return Vec::new() }; + out.push((i, d)); + } + out + }) + }) + .collect(); + workers.into_iter().flat_map(|w| w.join().unwrap_or_default()).collect() + }); + if parts.len() != bands.len() { + return None; + } + parts.sort_by_key(|p| p.0); + Some(parts.into_iter().map(|p| p.1).collect()) +} + +/// Raw deflate of one band: ends on a sync flush, or finishes the stream for the `last` band. +/// The output buffer is sized past deflate's worst case so that one call takes the whole band +/// and completes the flush: when a sync flush fills the buffer, miniz_oxide (flate2's former Rust +/// backend) can return before compressing the last lookahead bytes, and the next call only +/// drains the pending output, so a resumed flush silently drops data. `None` (the caller then +/// uses the serial encoder) if the single call did not finish. +fn deflate_band(filtered: &[u8], lvl: flate2::Compression, last: bool) -> Option> { + let mut c = flate2::Compress::new(lvl, false); + // Stored blocks cost 5 bytes per 64 KiB, the flush an empty stored block: 1/8 is ample. + let mut z = Vec::new(); + z.try_reserve_exact(filtered.len() + filtered.len() / 8 + 1024).ok()?; + let flush = if last { flate2::FlushCompress::Finish } else { flate2::FlushCompress::Sync }; + let status = c.compress_vec(filtered, &mut z, flush).ok()?; + let done = if last { status == flate2::Status::StreamEnd } else { c.total_in() as usize == filtered.len() && z.len() < z.capacity() }; + done.then_some(z) +} + +/// One scanline with the adaptive filter choice, appended to `out` (type byte + data). +fn filter_row(row: &[u8], prev: Option<&[u8]>, bpp: usize, scratch: &mut [u8], out: &mut Vec) { + let n = row.len(); + let up = |i: usize| prev.map_or(0, |p| p[i]); + let left = |i: usize| if i >= bpp { row[i - bpp] } else { 0 }; + let ul = |i: usize| if i >= bpp { prev.map_or(0, |p| p[i - bpp]) } else { 0 }; + let paeth = |a: u8, b: u8, c: u8| { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } + }; + let cost = |v: &[u8]| v.iter().map(|&b| u64::from((b as i8).unsigned_abs())).sum::(); + let mut best = cost(row); + let start = out.len(); + out.push(0); + out.extend_from_slice(row); + for kind in 1u8..=4 { + if best == 0 { + break; + } + let mut c = 0u64; + for i in 0..n { + let x = row[i]; + scratch[i] = match kind { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + c += u64::from((scratch[i] as i8).unsigned_abs()); + if c >= best { + break; + } + } + if c < best { + best = c; + out[start] = kind; + out[start + 1..].copy_from_slice(&scratch[..n]); + } + } +} + +const ADLER_MOD: u32 = 65_521; + +fn adler32(start: u32, data: &[u8]) -> u32 { + let (mut a, mut b) = (start & 0xffff, start >> 16); + for chunk in data.chunks(5552) { + for &x in chunk { + a += u32::from(x); + b += a; + } + a %= ADLER_MOD; + b %= ADLER_MOD; + } + (b << 16) | a +} + +/// Adler-32 of A ‖ B from adler(A), adler(B) and |B| (zlib's `adler32_combine`). +fn adler32_combine(a1: u32, a2: u32, len2: usize) -> u32 { + let rem = (len2 % ADLER_MOD as usize) as u64; + let m = u64::from(ADLER_MOD); + let (s1a, s2a) = (u64::from(a1 & 0xffff), u64::from(a1 >> 16)); + let (s1b, s2b) = (u64::from(a2 & 0xffff), u64::from(a2 >> 16)); + let s1 = (s1a + s1b + m - 1) % m; + let s2 = (s2a + s2b + (rem * s1a) % m + m - rem) % m; + ((s2 as u32) << 16) | s1 as u32 +} + +/// Writes an 8-bit palette PNG (PNG-8): `indices` row-major, `palette` RGB entries (≤ 256), +/// optional fully transparent entry (`tRNS`) and optional resolution in dpi (`pHYs`, written when +/// both axes are positive and finite). Used for Indexed Color documents. +pub fn encode_indexed( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + transparent: Option, + dpi: Option<(f32, f32)>, + stereo: Option, +) -> Result, CodecError> { + if width == 0 || height == 0 || indices.len() != width as usize * height as usize { + return Err(CodecError::InvalidImage("index data does not match the size".into())); + } + if palette.is_empty() || palette.len() > 256 || indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(CodecError::InvalidImage("palette must have 1..=256 entries covering every index".into())); + } + let mut info = png::Info::with_size(width, height); + info.color_type = png::ColorType::Indexed; + info.bit_depth = png::BitDepth::Eight; + info.palette = Some(palette.iter().flatten().copied().collect::>().into()); + if let Some(t) = transparent.filter(|&t| usize::from(t) < palette.len()) { + let mut trns = vec![255u8; usize::from(t) + 1]; + trns[usize::from(t)] = 0; + info.trns = Some(trns.into()); + } + if let Some((x, y)) = dpi + && x.is_finite() + && y.is_finite() + && x > 0.0 + && y > 0.0 + { + info.pixel_dims = + Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + } + let mut out = Vec::new(); + { + let encoder = png::Encoder::with_info(&mut out, info).map_err(|e| CodecError::encode(F, e))?; + let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; + if let Some(stereo) = stereo { + writer.write_chunk(png::chunk::ChunkType(*b"sTER"), &[stereo.mode()]).map_err(|e| CodecError::encode(F, e))?; + } + writer.write_image_data(indices).map_err(|e| CodecError::encode(F, e))?; + writer.finish().map_err(|e| CodecError::encode(F, e))?; + } + Ok(out) +} + +#[cfg(test)] +mod stereo_tests { + use super::*; + + /// One CRC-valid chunk as it appears in a file. + fn chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&(data.len() as u32).to_be_bytes()); + out.extend_from_slice(kind); + out.extend_from_slice(data); + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out + } + + fn png_with(stereo: &[u8]) -> Vec { + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + out.extend_from_slice(&chunk(b"IHDR", &[0, 0, 0, 2, 0, 0, 0, 1, 8, 2, 0, 0, 0])); + if !stereo.is_empty() { + out.extend_from_slice(&chunk(b"sTER", stereo)); + } + out.extend_from_slice(&chunk(b"IDAT", &[0x78, 0x01, 0x01, 0x00, 0xff, 0xff, 0, 3, 1, 0, 0])); + out.extend_from_slice(&chunk(b"IEND", &[])); + out + } + + #[test] + fn reads_both_defined_views() { + assert_eq!(stereo(&png_with(&[0])), Some(PngStereo::CrossFuse)); + assert_eq!(stereo(&png_with(&[1])), Some(PngStereo::DivergingFuse)); + assert_eq!(stereo(&png_with(&[])), None); + // Reserved and unknown mode bytes are not a pairing we can keep. + assert_eq!(stereo(&png_with(&[2])), None); + assert_eq!(stereo(&png_with(&[255])), None); + // A two-byte payload is not sTER data. + assert_eq!(stereo(&png_with(&[0, 0])), None); + assert_eq!(stereo(b"not a png"), None); + } + + #[test] + fn ster_mode_bytes_describe_eye_order_not_vertical_packing() { + // PNG Extensions 1.4.0 §3.6: both views occupy horizontal halves. + // Each view is eight columns, so the second starts on an eight-column boundary. + for (mode, left_pixel, right_pixel) in [(0, [0, 0, 255], [255, 0, 0]), (1, [255, 0, 0], [0, 0, 255])] { + let pixels: Vec = [left_pixel; 8].into_iter().chain([right_pixel; 8]).flatten().collect(); + let img = Image::from_raw(16, 1, ChannelLayout::Rgb, SampleType::U8, pixels.clone()).unwrap(); + let pairing = if mode == 0 { PngStereo::CrossFuse } else { PngStereo::DivergingFuse }; + let opts = EncodeOptions { png_stereo: Some(pairing), ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let chunk = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + assert_eq!(bytes[chunk + 4], mode, "the specification's literal mode byte"); + let decoded = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(decoded.dimensions(), (16, 1)); + assert_eq!(decoded.data(), pixels); + } + } + + #[test] + fn a_broken_crc_is_not_a_stereo_mark() { + let mut bytes = png_with(&[0]); + let kind = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + // The CRC trails the type and the one-byte payload of the chunk. + bytes[kind + 8] ^= 0xff; + assert_eq!(stereo(&bytes), None); + } + + #[test] + fn encode_writes_the_chunk_before_idat_and_it_reads_back() { + let img = Image::from_raw(2, 1, ChannelLayout::Rgb, SampleType::U8, vec![9; 6]).unwrap(); + for view in [PngStereo::CrossFuse, PngStereo::DivergingFuse] { + let opts = EncodeOptions { png_stereo: Some(view), ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + assert_eq!(stereo(&bytes), Some(view)); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.data(), img.data()); + let ter = bytes.windows(4).position(|w| w == b"sTER").unwrap(); + let idat = bytes.windows(4).position(|w| w == b"IDAT").unwrap(); + assert!(ter < idat, "sTER before IDAT"); + } + let plain = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(stereo(&plain), None); + } + + #[test] + fn interlaced_and_indexed_encoders_keep_the_mark() { + let img = Image::from_raw(2, 2, ChannelLayout::Rgb, SampleType::U8, vec![9; 12]).unwrap(); + let opts = EncodeOptions { png_stereo: Some(PngStereo::DivergingFuse), png_interlaced: true, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + assert_eq!(stereo(&bytes), Some(PngStereo::DivergingFuse)); + let pal = [[255, 0, 0], [0, 0, 255]]; + let bytes = encode_indexed(2, 1, &[0, 1], &pal, None, None, Some(PngStereo::CrossFuse)).unwrap(); + assert_eq!(stereo(&bytes), Some(PngStereo::CrossFuse)); + assert_eq!(palette(&bytes).unwrap().map(|p| p.colors), Some(pal.to_vec())); + } +} + +#[cfg(test)] +mod indexed_tests { + use super::*; + + #[test] + fn png8_roundtrips_through_the_decoder() { + let pal = [[255, 0, 0], [0, 0, 255], [0, 0, 0]]; + let idx = [0u8, 1, 2, 1, 0, 2]; + let bytes = encode_indexed(3, 2, &idx, &pal, Some(2), None, None).unwrap(); + let img = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (3, 2)); + let px = img.convert(ChannelLayout::Rgba, SampleType::U8); + assert_eq!(&px.data()[..8], &[255, 0, 0, 255, 0, 0, 255, 255]); + assert_eq!(px.data()[11], 0, "transparent entry"); + assert!(encode_indexed(3, 2, &[9; 6], &pal, None, None, None).is_err()); + assert!(encode_indexed(3, 3, &idx, &pal, None, None, None).is_err()); + } + + fn png(color: png::ColorType, trns: Option<&[u8]>) -> Vec { + let mut out = Vec::new(); + let mut e = png::Encoder::new(&mut out, 2, 1); + e.set_color(color); + e.set_depth(png::BitDepth::Eight); + e.set_palette(vec![1, 2, 3, 4, 5, 6]); + if let Some(t) = trns { + e.set_trns(t.to_vec()); + } + let data = if color == png::ColorType::Rgb { vec![0; 6] } else { vec![0, 1] }; + e.write_header().unwrap().write_image_data(&data).unwrap(); + out + } + + #[test] + fn palette_reads_the_table_and_its_one_transparent_entry() { + let bytes = encode_indexed(2, 1, &[0, 1], &[[1, 2, 3], [4, 5, 6]], Some(1), None, None).unwrap(); + let colors = vec![[1, 2, 3], [4, 5, 6]]; + assert_eq!(palette(&bytes).unwrap(), Some(PngPalette { colors: colors.clone(), transparent: Some(1) })); + assert_eq!(palette(&png(png::ColorType::Indexed, None)).unwrap(), Some(PngPalette { colors, transparent: None })); + // A truecolour PNG's suggested palette is not a colour table. + assert_eq!(palette(&png(png::ColorType::Rgb, None)).unwrap(), None); + // Partial alpha or two transparent entries don't fit one transparent index. + assert_eq!(palette(&png(png::ColorType::Indexed, Some(&[255, 128]))).unwrap(), None); + assert_eq!(palette(&png(png::ColorType::Indexed, Some(&[0, 0]))).unwrap(), None); + } +} + +#[cfg(test)] +mod parallel_tests { + use super::*; + + #[test] + #[ignore = "manual 24 MP release benchmark"] + fn benchmark_filter_selection() { + use std::{hint::black_box, time::Instant}; + for pattern in ["zero", "identical", "varied"] { + let row: Vec = (0..6000 * 4).map(|i| if pattern == "zero" { 0 } else { (i * 17 + i / 13) as u8 }).collect(); + let prev: Vec<_> = row.iter().map(|v| if pattern == "identical" { *v } else { v.wrapping_add(3) }).collect(); + let mut scratch = vec![0; row.len()]; + let mut out = Vec::with_capacity(row.len() + 1); + for trial in 0..6 { + type Filter = fn(&[u8], Option<&[u8]>, usize, &mut [u8], &mut Vec); + let mut paths: [(&str, Filter); 2] = [("main", previous_filter_row), ("split", filter_row)]; + if trial % 2 == 1 { + paths.reverse(); + } + for (name, path) in paths { + let t = Instant::now(); + for _ in 0..4000 { + out.clear(); + black_box(path)(black_box(&row), Some(black_box(&prev)), 4, &mut scratch, &mut out); + black_box(&out); + } + println!("filter,{pattern},{trial},{name},{:.6}", t.elapsed().as_secs_f64() * 1000.0); + } + } + } + } + + // The main-branch implementation before this refactor. + fn previous_filter_row(row: &[u8], prev: Option<&[u8]>, bpp: usize, scratch: &mut [u8], out: &mut Vec) { + let n = row.len(); + let up = |i: usize| prev.map_or(0, |p| p[i]); + let left = |i: usize| if i >= bpp { row[i - bpp] } else { 0 }; + let ul = |i: usize| if i >= bpp { prev.map_or(0, |p| p[i - bpp]) } else { 0 }; + let paeth = |a: u8, b: u8, c: u8| { + let p = i16::from(a) + i16::from(b) - i16::from(c); + let (pa, pb, pc) = ((p - i16::from(a)).abs(), (p - i16::from(b)).abs(), (p - i16::from(c)).abs()); + if pa <= pb && pa <= pc { + a + } else if pb <= pc { + b + } else { + c + } + }; + let cost = |v: &[u8]| v.iter().map(|&b| u64::from((b as i8).unsigned_abs())).sum::(); + let mut best = (cost(row), 0u8); + for kind in 1u8..=4 { + for i in 0..n { + let x = row[i]; + scratch[i] = match kind { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + } + let c = cost(&scratch[..n]); + if c < best.0 { + best = (c, kind); + } + } + out.push(best.1); + if best.1 == 0 { + out.extend_from_slice(row); + return; + } + for i in 0..n { + let x = row[i]; + scratch[i] = match best.1 { + 1 => x.wrapping_sub(left(i)), + 2 => x.wrapping_sub(up(i)), + 3 => x.wrapping_sub(((u16::from(left(i)) + u16::from(up(i))) / 2) as u8), + _ => x.wrapping_sub(paeth(left(i), up(i), ul(i))), + }; + } + out.extend_from_slice(&scratch[..n]); + } + + #[test] + fn adaptive_filter_preserves_minimum_cost_and_ties() { + for bpp in [1, 2, 3, 4, 6, 8] { + for seed in 0..32usize { + let row: Vec = (0..137).map(|i| (i * seed + i / 5) as u8).collect(); + let shifted: Vec<_> = row.iter().map(|v| v.wrapping_add(seed as u8)).collect(); + for prev in [None, Some(row.as_slice()), Some(shifted.as_slice())] { + let mut expected = vec![99]; + let mut actual = expected.clone(); + let mut scratch = vec![0; row.len()]; + previous_filter_row(&row, prev, bpp, &mut scratch, &mut expected); + filter_row(&row, prev, bpp, &mut scratch, &mut actual); + assert_eq!(actual, expected, "bpp={bpp}, seed={seed}"); + } + } + } + let mut out = Vec::new(); + filter_row(&[0; 16], None, 4, &mut [0; 16], &mut out); + assert_eq!(out, vec![0; 17]); + out.clear(); + filter_row(&[1; 16], Some(&[0; 16]), 1, &mut [0; 16], &mut out); + assert_eq!(out, [vec![1, 1], vec![0; 15]].concat()); + } + + #[test] + fn adler_combine_matches_one_pass() { + let a: Vec = (0..100_000u32).map(|i| (i * 7 + i / 13) as u8).collect(); + let b: Vec = (0..70_001u32).map(|i| (i * 31) as u8).collect(); + let whole: Vec = a.iter().chain(&b).copied().collect(); + assert_eq!(adler32_combine(adler32(1, &a), adler32(1, &b), b.len()), adler32(1, &whole)); + } + + #[test] + fn bands_that_end_just_past_a_block_keep_all_their_data() { + // Deflate output ~0.6 of the input, cut just past one of miniz_oxide's block + // boundaries: a sync flush that fills the output buffer there used to lose the + // last lookahead bytes, corrupting every band after it. + for (k, len) in [(20u32, 136_916usize), (20, 345_044), (24, 195_796), (28, 189_142)] { + let mut x = 0x9E37_79B9u32 ^ k; + let data: Vec = (0..len) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + ((x >> 8) % k) as u8 + }) + .collect(); + for last in [false, true] { + let z = deflate_band(&data, flate2::Compression::default(), last).unwrap(); + let mut d = flate2::Decompress::new(false); + let mut back = Vec::with_capacity(len + 1); + d.decompress_vec(&z, &mut back, flate2::FlushDecompress::Sync).unwrap(); + assert!(back == data, "k {k} len {len} last {last}: {} of {len} bytes", back.len()); + } + } + } + + /// The bytes depend on the image, not on the machine: one, two, seven and 32 worker threads + /// (and the machine's own count, through `encode`) give the same stream. + #[test] + fn the_parallel_encoder_gives_the_same_bytes_on_any_thread_count() { + let (w, h, bpp) = (1201usize, 1013usize, 4usize); + let px: Vec = + (0..w * h * bpp).map(|i| ((i / 5) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 11) as u8 / 5 + (i % 89) as u8).collect(); + assert!(px.len() >= PARALLEL_MIN_BYTES && px.len() > 2 * BAND_BYTES); + for level in [PngCompression::Fast, PngCompression::Default, PngCompression::Best] { + let one = banded_idat(&px, w, h, bpp, level, 1).unwrap(); + for threads in [2, 7, 32] { + assert!(banded_idat(&px, w, h, bpp, level, threads).unwrap() == one, "{level:?}: {threads} threads differ from one"); + } + assert!(parallel_idat(&px, w, h, bpp, level).unwrap() == one, "{level:?}: this machine's thread count differs from one"); + let img = Image::from_raw(w as u32, h as u32, ChannelLayout::Rgba, SampleType::U8, px.clone()).unwrap(); + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let png = crate::encode(&img, Format::Png, &opts).unwrap(); + assert!(png.windows(one.len()).any(|c| c == one.as_slice()), "{level:?}: the file carries the stream"); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data()); + } + } + + #[test] + fn large_images_roundtrip_through_the_parallel_encoder() { + for (layout, sample, ch, bytes) in [(ChannelLayout::Rgb, SampleType::U8, 3usize, 1usize), (ChannelLayout::Rgba, SampleType::U16, 4, 2)] { + let (w, h) = (1531u32, 977u32); + let n = w as usize * h as usize * ch * bytes; + let px: Vec = (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); + let img = Image::from_raw(w, h, layout, sample, px.clone()).unwrap(); + assert!(n >= PARALLEL_MIN_BYTES || sample == SampleType::U8); + for level in [PngCompression::Fast, PngCompression::Default] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let back = decode(&bytes, &Limits::default()).unwrap(); + assert_eq!(back.dimensions(), (w, h)); + assert_eq!(back.convert(layout, sample).data(), img.data(), "{layout:?} {sample:?} {level:?}"); + // The oracle decoder agrees. + let o = image::load_from_memory_with_format(&bytes, image::ImageFormat::Png).unwrap(); + assert_eq!((o.width(), o.height()), (w, h)); + } + } + } +} + +#[cfg(test)] +mod streamed_tests { + use super::*; + + /// (bytes ahead of the first IDAT, the IDAT payloads joined, how many IDAT chunks). + fn split(png: &[u8]) -> (Vec, Vec, usize) { + let (mut at, mut head, mut zlib, mut count) = (8, None, Vec::new(), 0); + while at + 12 <= png.len() { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + if &png[at + 4..at + 8] == b"IDAT" { + head.get_or_insert_with(|| png[..at].to_vec()); + zlib.extend_from_slice(&png[at + 8..at + 8 + len]); + count += 1; + } + at += 12 + len; + } + (head.unwrap(), zlib, count) + } + + fn noise(n: usize, seed: u32) -> Vec { + (0..n).map(|i| ((i / 3) as u32 ^ seed).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect() + } + + /// `img` pushed in bands of `heights` rows (the last one takes the rest). + fn streamed(img: &Image, heights: &[u32], opts: &EncodeOptions) -> Vec { + let (w, h) = img.dimensions(); + let row = img.data().len() / h as usize; + let mut bands = Vec::new(); + let mut y = 0; + for &n in heights.iter().chain(std::iter::once(&u32::MAX)) { + let n = n.min(h - y); + if n == 0 { + continue; + } + let data = img.data()[y as usize * row..(y + n) as usize * row].to_vec(); + bands.push(Image::from_raw(w, n, img.layout(), img.sample_type(), data).unwrap().with_icc(img.icc.clone()).with_meta(img.meta.clone())); + y += n; + } + let mut out = Vec::new(); + let mut rows = PngRows::new(&mut out, &bands[0], h, opts).unwrap(); + for b in &bands { + rows.push(b).unwrap(); + } + rows.finish().unwrap(); + out + } + + /// Bands of any height give the zlib stream, header chunks and pixels the whole-image encoder + /// gives, at 8 and 16 bits, with a profile, resolution, text and XMP. + #[test] + fn streamed_rows_match_the_whole_image_encoder() { + for (layout, sample, ch, bytes) in [(ChannelLayout::Rgba, SampleType::U8, 4usize, 1usize), (ChannelLayout::Rgb, SampleType::U16, 3, 2)] { + let (w, h) = (1201u32, 1013u32); + let meta = Metadata { + dpi: Some((300.0, 300.0)), + text: vec![("Title".into(), "Tent".into())], + xmp: Some("".into()), + ..Default::default() + }; + let img = Image::from_raw(w, h, layout, sample, noise(w as usize * h as usize * ch * bytes, 7)) + .unwrap() + .with_icc(Some((0..600u32).map(|i| (i % 251) as u8).collect())) + .with_meta(meta); + for level in [PngCompression::Fast, PngCompression::Default] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let whole = crate::encode(&img, Format::Png, &opts).unwrap(); + let (head, zlib, _) = split(&whole); + for heights in [&[1u32, 77, 300][..], &[256, 256, 256], &[1013]] { + let png = streamed(&img, heights, &opts); + let (h2, z2, _) = split(&png); + assert!(h2 == head, "{layout:?} {level:?} {heights:?}: header chunks differ"); + assert!(z2 == zlib, "{layout:?} {level:?} {heights:?}: zlib stream differs"); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data()); + } + } + } + } + + /// Small images, no compression and bands in another sample type (converted by the plan). + #[test] + fn small_uncompressed_and_converted_bands_round_trip() { + let img = Image::from_raw(3, 5, ChannelLayout::GrayA, SampleType::U8, noise(30, 3)).unwrap(); + for level in [PngCompression::None, PngCompression::Best] { + let opts = EncodeOptions { png_compression: level, ..Default::default() }; + let png = streamed(&img, &[2, 1], &opts); + assert_eq!(decode(&png, &Limits::default()).unwrap().data(), img.data(), "{level:?}"); + } + // Float bands are written with 16 bits, as `encode` writes them. + let f = img.convert(ChannelLayout::GrayA, SampleType::F32); + let png = streamed(&f, &[4], &EncodeOptions::default()); + let back = decode(&png, &Limits::default()).unwrap(); + assert_eq!(back.sample_type(), SampleType::U16); + assert_eq!(back.convert(ChannelLayout::GrayA, SampleType::U8).data(), img.data()); + } + + /// A zlib stream longer than one chunk may hold is split over IDAT chunks that read back. + #[test] + fn long_streams_split_over_several_idat_chunks() { + let img = Image::from_raw(64, 40, ChannelLayout::Rgb, SampleType::U8, noise(64 * 40 * 3, 9)).unwrap(); + let whole = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let (_, zlib, _) = split(&whole); + let mut out = Vec::new(); + let mut w = start(&mut out, (64, 40), (ChannelLayout::Rgb, SampleType::U8), None, &Metadata::default(), &EncodeOptions::default()).unwrap(); + write_idat(&mut w, &zlib, 100).unwrap(); + w.finish().unwrap(); + let (_, joined, count) = split(&out); + assert_eq!(count, zlib.len().div_ceil(100)); + assert!(joined == zlib); + assert_eq!(decode(&out, &Limits::default()).unwrap().data(), img.data()); + } + + /// Bands that don't fit the image, missing rows and interlacing are errors, not panics. + #[test] + fn bands_that_dont_fit_are_errors() { + let band = Image::from_raw(4, 2, ChannelLayout::Rgb, SampleType::U8, vec![0; 24]).unwrap(); + let opts = EncodeOptions::default(); + assert!(PngRows::new(Vec::new(), &band, 0, &opts).is_err()); + assert!(PngRows::new(Vec::new(), &band, 3, &EncodeOptions { png_interlaced: true, ..Default::default() }).is_err()); + let mut rows = PngRows::new(Vec::new(), &band, 3, &opts).unwrap(); + rows.push(&band).unwrap(); + // Two more rows than the image holds. + assert!(rows.push(&band).is_err()); + let narrow = Image::from_raw(3, 1, ChannelLayout::Rgb, SampleType::U8, vec![0; 9]).unwrap(); + assert!(rows.push(&narrow).is_err()); + // Two of three rows. + assert!(rows.finish().is_err()); + } +} + +#[cfg(test)] +mod rows_tests { + use super::*; + + /// The pixels `decode` read before it was built on [`PngDecodeRows`]: the whole image in one + /// `next_frame` call, then the byte order of 16-bit samples. + fn one_call(bytes: &[u8]) -> Result<(u32, u32, Vec), CodecError> { + let mut decoder = png::Decoder::new_with_limits(Cursor::new(bytes), png::Limits { bytes: Limits::default().alloc_usize() }); + decoder.set_transformations(png::Transformations::EXPAND); + decoder.set_ignore_text_chunk(true); + let mut reader = decoder.read_info().map_err(map_png_err)?; + let mut buf = vec![0u8; reader.output_buffer_size().ok_or_else(|| err("image too large"))?]; + let out = reader.next_frame(&mut buf).map_err(map_png_err)?; + buf.truncate(out.buffer_size()); + if reader.output_color_type().1 == png::BitDepth::Sixteen { + to_native_endian(&mut buf); + } + Ok((out.width, out.height, buf)) + } + + fn image(w: u32, h: u32, layout: ChannelLayout, sample: SampleType) -> Image { + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let data: Vec = (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8).collect(); + Image::from_raw(w, h, layout, sample, data).unwrap() + } + + #[test] + fn rows_give_the_pixels_of_a_single_call() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::U8, SampleType::U16] { + for interlaced in [false, true] { + let img = image(33, 21, layout, sample); + let opts = EncodeOptions { png_interlaced: interlaced, ..Default::default() }; + let bytes = crate::encode(&img, Format::Png, &opts).unwrap(); + let (w, h, pixels) = one_call(&bytes).unwrap(); + for per_call in [1usize, 7, usize::MAX] { + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut left = per_call; + let got = loop { + left -= 1; + let mut more = || { + left = left.saturating_sub(1); + left > 0 + }; + if let Some(img) = rows.step(&mut more).unwrap() { + break img; + } + left = per_call; + }; + assert_eq!((got.width(), got.height()), (w, h)); + assert_eq!(got.data(), pixels.as_slice(), "{layout:?} {sample:?} interlaced={interlaced} per_call={per_call}"); + assert_eq!(got.data(), img.data()); + } + } + } + } + } + + /// A metadata yield after the last row must not finish the pixel reader again on every call. + #[test] + fn metadata_yields_do_not_finalize_the_pixels_again() { + let text = "A".repeat(20_000); + let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8).with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); + let bytes = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let got = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 10_000, "the decoder never finished"); + }; + assert!(calls > 1, "the metadata took more than the pixel step"); + assert_eq!(rows.pixel_finalizations, 1, "a metadata yield must not finalize the pixels again"); + assert_eq!(got.meta.text, vec![("Comment".into(), text)]); + } + /// A compressed iTXt chunk whose text inflates to `len` bytes of invalid UTF-8. + fn bad_itxt(len: usize) -> Vec { + use std::io::Write; + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + z.write_all(&vec![0xff; len]).unwrap(); + let mut data = b"Comment\0\x01\x00\0\0".to_vec(); + data.extend_from_slice(&z.finish().unwrap()); + let mut out = (data.len() as u32).to_be_bytes().to_vec(); + out.extend_from_slice(b"iTXt"); + out.extend_from_slice(&data); + let mut crc = flate2::Crc::new(); + crc.update(b"iTXt"); + crc.update(&data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out + } + + /// A small PNG with `chunks` placed before its `IEND`. + fn png_with(chunks: &[u8]) -> Vec { + let png = crate::encode(&image(4, 4, ChannelLayout::Rgb, SampleType::U8), Format::Png, &EncodeOptions::default()).unwrap(); + let (head, iend) = png.split_at(png.len() - 12); + [head, chunks, iend].concat() + } + + #[test] + fn dropped_text_still_counts_against_the_budget() { + let limits = Limits { max_alloc: 1 << 20, ..Limits::default() }; + let decode = |bytes: &[u8]| PngDecodeRows::new(bytes, &limits)?.step(&mut || true)?.ok_or_else(|| err("unfinished")); + let one = decode(&png_with(&bad_itxt(100_000))).unwrap(); + assert!(one.meta.text.is_empty(), "invalid UTF-8 is dropped"); + // Twenty chunks inflate 2 MB between them: each is dropped, but the work is charged. + assert!(matches!(decode(&png_with(&bad_itxt(100_000).repeat(20))), Err(CodecError::LimitExceeded(_)))); + } +} diff --git a/crates/image-codecs/src/codecs/pnm.rs b/crates/image-codecs/src/codecs/pnm.rs new file mode 100644 index 000000000..c16cccfd0 --- /dev/null +++ b/crates/image-codecs/src/codecs/pnm.rs @@ -0,0 +1,355 @@ +//! Netpbm family, implemented here: P1–P6 (PBM/PGM/PPM, ASCII and binary), +//! P7 (PAM, including the common `CMYK`/`CMYK_ALPHA` tuple types) and PFM +//! (`PF` colour / `Pf` gray float). +//! +//! Writing picks the subtype from the image: float → PFM (gray/RGB), gray +//! → P5, RGB → P6, anything with alpha or CMYK → P7. + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +const F: Format = Format::Pnm; + +fn err(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, msg) +} + +struct Reader<'a> { + b: &'a [u8], + pos: usize, +} + +impl<'a> Reader<'a> { + fn skip_ws_and_comments(&mut self) { + while self.pos < self.b.len() { + let c = self.b[self.pos]; + if c == b'#' { + while self.pos < self.b.len() && self.b[self.pos] != b'\n' && self.b[self.pos] != b'\r' { + self.pos += 1; + } + } else if c.is_ascii_whitespace() { + self.pos += 1; + } else { + break; + } + } + } + + fn token(&mut self) -> Result<&'a [u8], CodecError> { + self.skip_ws_and_comments(); + let start = self.pos; + while self.pos < self.b.len() && !self.b[self.pos].is_ascii_whitespace() && self.b[self.pos] != b'#' { + self.pos += 1; + } + if start == self.pos { + return Err(err("unexpected end of header")); + } + Ok(&self.b[start..self.pos]) + } + + fn uint(&mut self) -> Result { + let t = self.token()?; + std::str::from_utf8(t).ok().and_then(|s| s.parse::().ok()).ok_or_else(|| err("invalid integer in header")) + } + + /// Consume the single whitespace byte that separates header and raster. + fn single_ws(&mut self) -> Result<(), CodecError> { + match self.b.get(self.pos) { + Some(c) if c.is_ascii_whitespace() => { + self.pos += 1; + Ok(()) + } + _ => Err(err("missing whitespace after header")), + } + } + + /// PAM ends its header with an LF-terminated line, unlike P4–P6's single separator. + fn pam_endhdr(&mut self) -> Result<(), CodecError> { + while let Some(&c) = self.b.get(self.pos) { + self.pos += 1; + if c == b'\n' { + // Stop at the first LF: whitespace and '#' after it are raster bytes. + return Ok(()); + } + if !c.is_ascii_whitespace() { + return Err(err("unexpected data after PAM ENDHDR")); + } + } + Err(err("unterminated PAM ENDHDR line")) + } + + fn rest(&self) -> &'a [u8] { + &self.b[self.pos.min(self.b.len())..] + } +} + +fn sample_for_maxval(maxval: u32) -> Result { + match maxval { + 1..=255 => Ok(SampleType::U8), + 256..=65535 => Ok(SampleType::U16), + _ => Err(err(format!("invalid maxval {maxval}"))), + } +} + +/// The decoded raster, built straight in the output representation. It is the only buffer sized +/// by the header, so `Limits::check` on the output budgets all of it: no wider scratch buffer +/// (such as one `u32` per sample) sits beside it (#2938). +struct Samples { + maxval: u32, + sample: SampleType, + bytes: Vec, +} + +impl Samples { + /// Reserve room for `cap` samples (callers bound `cap` by the file length where the header + /// alone could overstate it). + fn with_capacity(maxval: u32, cap: usize) -> Result { + let sample = sample_for_maxval(maxval)?; + let len = cap.checked_mul(sample.bytes()).ok_or_else(|| CodecError::LimitExceeded(format!("{cap} samples do not fit in memory")))?; + let mut bytes = Vec::new(); + bytes.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?; + Ok(Samples { maxval, sample, bytes }) + } + + /// Append one raw value in `[0, maxval]`, rescaled to the full range of the output sample. + fn push(&mut self, v: u32) -> Result<(), CodecError> { + let m = self.maxval; + if v > m { + return Err(err("sample exceeds maxval")); + } + match (self.sample, m) { + (SampleType::U8, 255) => self.bytes.push(v as u8), + (SampleType::U8, m) => self.bytes.push(((v * 255 + m / 2) / m) as u8), + (_, 65535) => self.bytes.extend_from_slice(&(v as u16).to_ne_bytes()), + (_, m) => self.bytes.extend_from_slice(&(((v as u64 * 65535 + m as u64 / 2) / m as u64) as u16).to_ne_bytes()), + } + Ok(()) + } + + fn into_image(self, w: u32, h: u32, layout: ChannelLayout) -> Result { + Image::from_raw(w, h, layout, self.sample, self.bytes) + } +} + +/// `w × h × channels` samples. `Limits::none()` lets any header through, so the product is +/// checked here rather than trusted to the limits. +fn sample_count(w: u32, h: u32, channels: usize) -> Result { + (w as usize) + .checked_mul(h as usize) + .and_then(|p| p.checked_mul(channels)) + .ok_or_else(|| CodecError::LimitExceeded(format!("{w}x{h}x{channels} samples do not fit in memory"))) +} + +fn read_binary(data: &[u8], n: usize, maxval: u32) -> Result { + let mut out = Samples::with_capacity(maxval, n)?; + if maxval < 256 { + let Some(raw) = data.get(..n) else { + return Err(err("truncated raster")); + }; + for &v in raw { + out.push(v as u32)?; + } + } else { + let Some(raw) = n.checked_mul(2).and_then(|len| data.get(..len)) else { + return Err(err("truncated raster")); + }; + for c in raw.as_chunks::<2>().0 { + out.push(u16::from_be_bytes(*c) as u32)?; + } + } + Ok(out) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + if bytes.len() < 2 || bytes[0] != b'P' { + return Err(err("not a Netpbm file")); + } + let kind = bytes[1]; + let mut r = Reader { b: bytes, pos: 2 }; + match kind { + b'1' | b'4' => { + let (w, h) = (r.uint()?, r.uint()?); + limits.check(w, h, ChannelLayout::Gray, SampleType::U8)?; + let n = sample_count(w, h, 1)?; + let mut px; + if kind == b'4' { + r.single_ws()?; + let row = (w as usize).div_ceil(8); + let data = r.rest(); + if row.checked_mul(h as usize).is_none_or(|len| data.len() < len) { + return Err(err("truncated raster")); + } + px = Vec::with_capacity(n); + for y in 0..h as usize { + for x in 0..w as usize { + let bit = (data[y * row + x / 8] >> (7 - x % 8)) & 1; + px.push(if bit == 1 { 0 } else { 255 }); + } + } + } else { + // Each ASCII pixel takes at least a byte, so the file bounds the reservation (#1110). + px = Vec::with_capacity(n.min(r.rest().len())); + while px.len() < n { + r.skip_ws_and_comments(); + match r.b.get(r.pos) { + Some(b'0') => px.push(255), + Some(b'1') => px.push(0), + Some(_) => return Err(err("invalid PBM digit")), + None => return Err(err("truncated raster")), + } + r.pos += 1; + } + } + Image::from_u8(w, h, ChannelLayout::Gray, px) + } + b'2' | b'3' | b'5' | b'6' => { + let (w, h, maxval) = (r.uint()?, r.uint()?, r.uint()?); + let layout = if matches!(kind, b'2' | b'5') { ChannelLayout::Gray } else { ChannelLayout::Rgb }; + limits.check(w, h, layout, sample_for_maxval(maxval)?)?; + let n = sample_count(w, h, layout.channels())?; + let samples = if matches!(kind, b'5' | b'6') { + r.single_ws()?; + read_binary(r.rest(), n, maxval)? + } else { + // Each ASCII sample is a digit and a separator (the last may lack one), so the + // file, not the header, bounds the reservation (#1110). + let mut v = Samples::with_capacity(maxval, n.min(r.rest().len() / 2 + 1))?; + for _ in 0..n { + v.push(r.uint()?)?; + } + v + }; + samples.into_image(w, h, layout) + } + b'7' => decode_pam(&mut r, limits), + b'F' | b'f' => { + let layout = if kind == b'F' { ChannelLayout::Rgb } else { ChannelLayout::Gray }; + let (w, h) = (r.uint()?, r.uint()?); + let scale: f32 = std::str::from_utf8(r.token()?) + .ok() + .and_then(|s| s.parse().ok()) + .filter(|s: &f32| s.is_finite() && *s != 0.0) + .ok_or_else(|| err("invalid PFM scale"))?; + r.single_ws()?; + limits.check(w, h, layout, SampleType::F32)?; + let nc = layout.channels(); + let row = w as usize * nc; + let data = r.rest(); + if sample_count(w, h, nc)?.checked_mul(4).is_none_or(|len| data.len() < len) { + return Err(err("truncated raster")); + } + let le = scale < 0.0; + // Write native-endian bytes straight into the output buffer: a separate `Vec` + // would double the budgeted allocation (#2938). + let row_bytes = row * 4; + let mut out = vec![0u8; row_bytes * h as usize]; + for (y, src) in data.chunks_exact(row_bytes).take(h as usize).enumerate() { + // PFM rows are stored bottom-to-top. + let dst_start = (h as usize - 1 - y) * row_bytes; + let Some(dst) = out.get_mut(dst_start..dst_start + row_bytes) else { + return Err(err("truncated raster")); + }; + for (d, s) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src.as_chunks::<4>().0) { + let v = if le { f32::from_le_bytes(*s) } else { f32::from_be_bytes(*s) }; + *d = v.to_ne_bytes(); + } + } + Image::from_raw(w, h, layout, SampleType::F32, out) + } + _ => Err(err("unknown Netpbm subtype")), + } +} + +fn decode_pam(r: &mut Reader<'_>, limits: &Limits) -> Result { + let (mut w, mut h, mut depth, mut maxval) = (None, None, None, None); + let mut tupltype = String::new(); + loop { + let tok = r.token()?; + match tok { + b"WIDTH" => w = Some(r.uint()?), + b"HEIGHT" => h = Some(r.uint()?), + b"DEPTH" => depth = Some(r.uint()?), + b"MAXVAL" => maxval = Some(r.uint()?), + b"TUPLTYPE" => { + let t = r.token()?; + if !tupltype.is_empty() { + tupltype.push(' '); + } + tupltype.push_str(std::str::from_utf8(t).map_err(|_| err("bad TUPLTYPE"))?); + } + b"ENDHDR" => break, + _ => return Err(err("unknown PAM header field")), + } + } + r.pam_endhdr()?; + let (w, h, depth, maxval) = match (w, h, depth, maxval) { + (Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d), + _ => return Err(err("incomplete PAM header")), + }; + let layout = match (tupltype.as_str(), depth) { + ("BLACKANDWHITE" | "GRAYSCALE", 1) | ("", 1) => ChannelLayout::Gray, + ("BLACKANDWHITE_ALPHA" | "GRAYSCALE_ALPHA", 2) | ("", 2) => ChannelLayout::GrayA, + ("RGB", 3) | ("", 3) => ChannelLayout::Rgb, + ("RGB_ALPHA", 4) | ("", 4) => ChannelLayout::Rgba, + ("CMYK", 4) => ChannelLayout::Cmyk, + ("CMYK_ALPHA", 5) => ChannelLayout::CmykA, + (t, d) => { + return Err(CodecError::unsupported(F, format!("PAM tuple type {t:?} with depth {d}"))); + } + }; + limits.check(w, h, layout, sample_for_maxval(maxval)?)?; + let n = sample_count(w, h, layout.channels())?; + read_binary(r.rest(), n, maxval)?.into_image(w, h, layout) +} + +pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + let (w, h) = img.dimensions(); + let layout = img.layout(); + let mut out = Vec::new(); + match img.sample_type() { + SampleType::F32 => { + let tag = match layout { + ChannelLayout::Gray => "Pf", + ChannelLayout::Rgb => "PF", + l => return Err(CodecError::encode(F, format!("PFM cannot store {l:?}"))), + }; + out.extend_from_slice(format!("{tag}\n{w} {h}\n-1.0\n").as_bytes()); + let s = img.to_f32_samples().unwrap_or_default(); + let row = w as usize * layout.channels(); + for y in (0..h as usize).rev() { + for v in &s[y * row..(y + 1) * row] { + out.extend_from_slice(&v.to_le_bytes()); + } + } + } + sample @ (SampleType::U8 | SampleType::U16) => { + let maxval = if sample == SampleType::U8 { 255 } else { 65535 }; + match layout { + ChannelLayout::Gray => out.extend_from_slice(format!("P5\n{w} {h}\n{maxval}\n").as_bytes()), + ChannelLayout::Rgb => out.extend_from_slice(format!("P6\n{w} {h}\n{maxval}\n").as_bytes()), + l => { + let t = match l { + ChannelLayout::GrayA => "GRAYSCALE_ALPHA", + ChannelLayout::Rgba => "RGB_ALPHA", + ChannelLayout::Cmyk => "CMYK", + _ => "CMYK_ALPHA", + }; + out.extend_from_slice(format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes()); + } + } + if sample == SampleType::U8 { + out.extend_from_slice(img.data()); + } else { + for v in img.to_u16_samples().unwrap_or_default() { + out.extend_from_slice(&v.to_be_bytes()); + } + } + } + s => return Err(CodecError::encode(F, format!("unsupported sample {s:?}"))), + } + Ok(out) +} diff --git a/crates/image-codecs/src/codecs/tga.rs b/crates/image-codecs/src/codecs/tga.rs new file mode 100644 index 000000000..2f382efcf --- /dev/null +++ b/crates/image-codecs/src/codecs/tga.rs @@ -0,0 +1,175 @@ +//! Targa writer. The `image` crate still reads Targa ([`super::via_image`]); writing is ours +//! because its run-length packets ran on from the end of one row into the next. TGA 2.0 says a +//! packet must not cross a scanline, and readers that decode a row at a time (Pillow, texture +//! converters, game engines) misread such files (#6382). +//! +//! Like Photoshop's Targa: RLE (types 10 and 11), rows stored bottom-up (the bottom-left origin +//! every reader assumes, also those that ignore the origin bits), alpha bits in the descriptor, +//! and the TGA 2.0 footer. + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; + +/// Most pixels one packet holds (a 7-bit count, plus one). +const MAX_PACKET: usize = 128; + +/// The TGA 2.0 footer: no extension or developer area, then the signature. +const FOOTER: &[u8; 26] = b"\0\0\0\0\0\0\0\0TRUEVISION-XFILE.\0"; + +/// Encodes an 8-bit Gray, Gray+alpha, RGB or RGBA image as a run-length encoded Targa. +pub(crate) fn encode(img: &Image) -> Result, CodecError> { + let (w, h) = img.dimensions(); + if img.sample_type() != SampleType::U8 { + return Err(CodecError::encode(Format::Tga, format!("{:?} samples (Targa stores 8 bits)", img.sample_type()))); + } + let (bpp, image_type, alpha_bits) = match img.layout() { + ChannelLayout::Gray => (1, 11, 0), + ChannelLayout::GrayA => (2, 11, 8), + ChannelLayout::Rgb => (3, 10, 0), + ChannelLayout::Rgba => (4, 10, 8), + l => return Err(CodecError::encode(Format::Tga, format!("no Targa layout for {l:?}"))), + }; + let (Ok(w16), Ok(h16)) = (u16::try_from(w), u16::try_from(h)) else { + return Err(CodecError::encode(Format::Tga, format!("dimensions exceed 65535x65535: {w}x{h}"))); + }; + let row = w as usize * bpp; + let data = img.data(); + if data.len() != row * h as usize { + return Err(CodecError::InvalidImage("buffer size mismatch".into())); + } + let mut out = Vec::with_capacity(18 + data.len() + data.len() / MAX_PACKET + FOOTER.len()); + out.extend_from_slice(&[0, 0, image_type, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + out.extend_from_slice(&w16.to_le_bytes()); + out.extend_from_slice(&h16.to_le_bytes()); + // Bits per pixel; the descriptor holds the alpha bits, origin bits 0 (bottom-left). + out.extend_from_slice(&[(bpp * 8) as u8, alpha_bits]); + let mut px = vec![0u8; row]; + for y in (0..h as usize).rev() { + px.copy_from_slice(&data[y * row..(y + 1) * row]); + // Targa stores colour as BGR. + if bpp >= 3 { + for p in px.chunks_exact_mut(bpp) { + p.swap(0, 2); + } + } + rle_row(&px, bpp, &mut out); + } + out.extend_from_slice(FOOTER); + Ok(out) +} + +/// One scanline as run-length packets that end with it: a run of two or more equal pixels is a +/// run packet, anything else goes out in raw packets. +fn rle_row(row: &[u8], bpp: usize, out: &mut Vec) { + let px: Vec<&[u8]> = row.chunks_exact(bpp).collect(); + let n = px.len(); + let mut i = 0; + while i < n { + let mut run = 1; + while i + run < n && run < MAX_PACKET && px[i + run] == px[i] { + run += 1; + } + if run >= 2 { + out.push(0x80 | (run - 1) as u8); + out.extend_from_slice(px[i]); + i += run; + continue; + } + // Raw until two equal pixels start a run, the packet is full or the row ends. The first + // pixel always goes in: it differs from the one after it, or it is the row's last. + let start = i; + while i < n && i - start < MAX_PACKET && !(i > start && i + 1 < n && px[i] == px[i + 1]) { + i += 1; + } + out.push((i - start - 1) as u8); + for p in &px[start..i] { + out.extend_from_slice(p); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn run_packets_at_the_128_pixel_boundary() { + for bpp in 1..=4 { + for (pixels, headers) in [(1, vec![0]), (2, vec![0x81]), (127, vec![0xfe]), (128, vec![0xff]), (129, vec![0xff, 0])] { + let pixel: Vec = (1..=bpp as u8).collect(); + let mut encoded = Vec::new(); + rle_row(&pixel.repeat(pixels), bpp, &mut encoded); + let expected: Vec = headers.into_iter().flat_map(|header| std::iter::once(header).chain(pixel.iter().copied())).collect(); + assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel"); + } + } + } + + #[test] + fn raw_packets_at_the_128_pixel_boundary() { + for bpp in 1..=4 { + for pixels in [127usize, 128, 129] { + let row: Vec = (0..pixels).flat_map(|x| vec![x as u8; bpp]).collect(); + let mut encoded = Vec::new(); + rle_row(&row, bpp, &mut encoded); + let mut expected = vec![(pixels.min(128) - 1) as u8]; + expected.extend_from_slice(&row[..pixels.min(128) * bpp]); + if pixels > 128 { + expected.push(0); + expected.extend_from_slice(&row[128 * bpp..]); + } + assert_eq!(encoded, expected, "{pixels} pixels, {bpp} bytes per pixel"); + } + } + } + + #[test] + fn raw_packets_stop_before_a_run_of_complete_pixels() { + // Equal first channels alone do not make an RGBA run; alpha participates too. + let a = [10, 20, 30, 40]; + let b = [10, 20, 30, 41]; + let c = [50, 60, 70, 80]; + let row = [a, b, c, c, a].concat(); + let mut encoded = Vec::new(); + rle_row(&row, 4, &mut encoded); + assert_eq!(encoded, [vec![1], a.to_vec(), b.to_vec(), vec![0x81], c.to_vec(), vec![0], a.to_vec()].concat()); + } + + #[test] + fn rejects_sample_types_and_layouts_targa_cannot_store() { + for sample in [SampleType::U16, SampleType::F16, SampleType::F32] { + let img = Image::new(1, 1, ChannelLayout::Rgb, sample).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{sample:?}"); + } + for layout in [ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab, ChannelLayout::LabA] { + let img = Image::new(1, 1, layout, SampleType::U8).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{layout:?}"); + } + } + + #[test] + fn dimensions_fit_the_unsigned_16_bit_header() { + for (width, height) in [(65535, 1), (1, 65535)] { + let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap(); + let file = encode(&img).unwrap(); + assert_eq!(&file[12..14], &(width as u16).to_le_bytes()); + assert_eq!(&file[14..16], &(height as u16).to_le_bytes()); + } + for (width, height) in [(65536, 1), (1, 65536)] { + let img = Image::new(width, height, ChannelLayout::Gray, SampleType::U8).unwrap(); + assert!(matches!(encode(&img), Err(CodecError::Encode { format: Format::Tga, .. })), "{width}x{height}"); + } + } + + #[test] + fn invalid_buffers_are_rejected_before_targa_encoding() { + // Image keeps its buffer private, so malformed buffers must fail at construction. + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let len = 2 * 2 * layout.channels(); + for bytes in [len - 1, len + 1] { + assert!(matches!(Image::from_u8(2, 2, layout, vec![0; bytes]), Err(CodecError::InvalidImage(_))), "{layout:?}: {bytes} bytes"); + } + } + } +} diff --git a/crates/image-codecs/src/codecs/tiff.rs b/crates/image-codecs/src/codecs/tiff.rs new file mode 100644 index 000000000..c74ab4436 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff.rs @@ -0,0 +1,220 @@ +//! TIFF and BigTIFF: 1/2/4/8/16/32/64-bit integer and 16/32/64-bit float samples, gray / RGB / +//! CMYK / palette with optional alpha (unassociated, or associated and un-premultiplied) and +//! extra samples, WhiteIsZero, strips or tiles, planar configuration 1 or 2, ICC (34675), XMP +//! (700), DPI and a few ASCII text tags. +//! +//! **Structure.** [`super::tiff_ifd`] walks the directories with bounds checks (classic and +//! BigTIFF, either byte order, the whole IFD chain and its SubIFDs, cycles and hostile counts +//! defeated) and lists them as pages. Decoding opens the first full-resolution page, like +//! Photoshop (reduced-resolution copies and masks are skipped); further pages are reported as a +//! [`DecodeWarning::MorePages`], and [`decode_page`] opens any directory. +//! +//! **Pixels.** The `tiff` crate decompresses one strip or tile at a time (none, LZW, Deflate, +//! PackBits, with the horizontal and floating-point predictors). The decoder sees the chosen +//! directory through a byte view whose header points at it ([`Patched`]), so SubIFDs decode +//! like IFD0. Decoding is banded: the output image is allocated once, at its final size, and +//! each band of rows (one strip, or one row of tiles, across every plane) is decoded and +//! converted straight into it, in parallel on native targets. A strip that already has the +//! output's layout is decoded in place; anything else goes through one strip- or tile-sized +//! scratch buffer per worker, so no second full-size buffer exists. Image data cut off by the +//! end of the file leaves the missing chunks empty and adds a [`DecodeWarning::Truncated`]. +//! [`TiffBands`] runs the same decode a group of bands at a time and hands each group to its +//! caller, so a document can be filled without the image ever existing whole (#8169). +//! +//! Photoshop's private tags are carried as opaque bytes in [`Metadata`]: 34377 (image +//! resources) and 37724 (`ImageSourceData`, the layers of a layered TIFF). They are read with +//! a bounds-checked walk of the first directory ([`photoshop_tags`]) rather than through the +//! decoder, which would materialize a multi-megabyte layer block as one `Value` per byte, and +//! written back as BYTE / UNDEFINED tags. The encoder writes files in the native byte order +//! (see [`writes_little_endian`]), which the layer data must match. + +use std::borrow::Cow; +use std::io::{self, Read, Seek, SeekFrom}; + +use half::f16; +use tiff::decoder::{ChunkType, Decoder}; +use tiff::encoder::colortype::{self, ColorType as TiffColorType}; +use tiff::encoder::{Rational, TiffEncoder, TiffKind, TiffKindBig, TiffKindStandard, TiffValue}; +use tiff::tags::{PhotometricInterpretation, ResolutionUnit, SampleFormat, Tag, Type}; + +use super::tiff_exif::{self, ExifDirs}; +use super::tiff_ifd::{self as ifd, File, Ifd}; +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, TiffCompression}; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +fn map_err(e: tiff::TiffError) -> CodecError { + match e { + tiff::TiffError::LimitsExceeded => CodecError::LimitExceeded("TIFF decoder limit".into()), + tiff::TiffError::UnsupportedError(u) => CodecError::unsupported(F, u.to_string()), + e => err(e), + } +} + +/// TIFF/EP `PhotometricInterpretation` for colour filter array (sensor) data. +const PHOTOMETRIC_CFA: u32 = 32803; +/// DNG `PhotometricInterpretation` for demosaiced but undeveloped sensor data. +const PHOTOMETRIC_LINEAR_RAW: u32 = 34892; +const TAG_PHOTOMETRIC: u16 = 262; +const TAG_SUB_IFDS: u16 = 330; +const TAG_DNG_VERSION: u16 = 50706; + +/// Recognizes TIFF-structured camera raw files (TIFF/EP, DNG, CR2 and the +/// TIFF-based NEF/ARW/PEF… layouts): a CR2 signature, a DNGVersion tag, or +/// a CFA / LinearRaw image in IFD0, its SubIFDs or the next few IFDs. Their +/// first IFD is often a reduced preview, so this runs before decoding. +/// Reads only bounds-checked header bytes; anything malformed is "not raw". +fn camera_raw(b: &[u8]) -> bool { + let le = match b.get(0..4) { + Some(b"II*\0") => true, + Some(b"MM\0*") => false, + _ => return false, // BigTIFF and others: no raw layouts to recognize + }; + let u16_at = |o: usize| { + let s = b.get(o..o.checked_add(2)?)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let u32_at = |o: usize| { + let s: [u8; 4] = b.get(o..o.checked_add(4)?)?.try_into().ok()?; + Some(if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + }; + // CR2: "CR" and major version 2 right after the TIFF header. + if b.get(8..11) == Some(b"CR\x02") { + return true; + } + // Entry value (count 1) as SHORT or LONG; anything else is ignored. + let value = |e: usize| match u16_at(e.saturating_add(2))? { + 3 => u16_at(e.saturating_add(8)).map(u32::from), + 4 | 13 => u32_at(e.saturating_add(8)), + _ => None, + }; + let mut queue: Vec = u32_at(4).into_iter().collect(); + let mut visited = 0; + while let Some(ifd) = queue.pop() { + // A handful of IFDs is enough for every real layout; also stops loops. + visited += 1; + if visited > 16 { + break; + } + let ifd = ifd as usize; + let Some(n) = u16_at(ifd) else { continue }; + for i in 0..usize::from(n) { + let e = ifd.saturating_add(2 + 12 * i); + let (Some(tag), Some(count)) = (u16_at(e), u32_at(e.saturating_add(4))) else { + break; + }; + match tag { + TAG_DNG_VERSION => return true, + TAG_PHOTOMETRIC if matches!(value(e), Some(PHOTOMETRIC_CFA | PHOTOMETRIC_LINEAR_RAW)) => { + return true; + } + TAG_SUB_IFDS if count == 1 => queue.extend(value(e)), + TAG_SUB_IFDS => { + // More than one offset: they are stored at the entry's offset. + let at = u32_at(e.saturating_add(8)).unwrap_or(0) as usize; + for k in 0..count.min(8) as usize { + queue.extend(u32_at(at.saturating_add(4 * k))); + } + } + _ => {} + } + } + if let Some(next) = u32_at(ifd.saturating_add(2 + 12 * usize::from(n))).filter(|&o| o != 0) { + queue.push(next); + } + } + false +} + +macro_rules! custom_colortype { + ($name:ident, $inner:ty, $photo:expr, $bits:expr, $fmt:expr, int) => { + struct $name; + impl TiffColorType for $name { + type Inner = $inner; + const TIFF_VALUE: PhotometricInterpretation = $photo; + const BITS_PER_SAMPLE: &'static [u16] = $bits; + const SAMPLE_FORMAT: &'static [SampleFormat] = $fmt; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + let n = Self::SAMPLE_FORMAT.len(); + let n = n.min(row.len()); + result.extend_from_slice(&row[..n]); + result.extend(row.iter().zip(&row[n..]).map(|(p, c)| c.wrapping_sub(*p))); + } + } + }; + ($name:ident, $inner:ty, $photo:expr, $bits:expr, $fmt:expr, float) => { + struct $name; + impl TiffColorType for $name { + type Inner = $inner; + const TIFF_VALUE: PhotometricInterpretation = $photo; + const BITS_PER_SAMPLE: &'static [u16] = $bits; + const SAMPLE_FORMAT: &'static [SampleFormat] = $fmt; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + result.extend_from_slice(row); + } + } + }; +} + +use PhotometricInterpretation as PI; + +mod bands; +mod decode; +mod encode; +mod tags; +mod ycbcr; + +pub use decode::TiffBands; +pub(crate) use decode::{decode, decode_page, has_unretained_samples, orientation}; +pub(crate) use encode::encode; +#[cfg(test)] +use encode::{Extras, try_with_capacity}; +use tags::F; +pub use tags::{PhotoshopTags, photoshop_tags, writes_little_endian}; +custom_colortype!(GrayA8, u8, PI::BlackIsZero, &[8, 8], &[SampleFormat::Uint; 2], int); +custom_colortype!(GrayA16, u16, PI::BlackIsZero, &[16, 16], &[SampleFormat::Uint; 2], int); +custom_colortype!(GrayA32F, f32, PI::BlackIsZero, &[32, 32], &[SampleFormat::IEEEFP; 2], float); +custom_colortype!(CmykA16, u16, PI::CMYK, &[16; 5], &[SampleFormat::Uint; 5], int); +custom_colortype!(CmykA32F, f32, PI::CMYK, &[32; 5], &[SampleFormat::IEEEFP; 5], float); +// CIELab as Photoshop writes it: a* and b* signed, the sample format left unsigned. +custom_colortype!(Lab8, u8, PI::CIELab, &[8; 3], &[SampleFormat::Uint; 3], int); +custom_colortype!(Lab16, u16, PI::CIELab, &[16; 3], &[SampleFormat::Uint; 3], int); +custom_colortype!(LabA8, u8, PI::CIELab, &[8; 4], &[SampleFormat::Uint; 4], int); +custom_colortype!(LabA16, u16, PI::CIELab, &[16; 4], &[SampleFormat::Uint; 4], int); + +#[cfg(test)] +mod bits_len { + use super::Extras; + use tiff::encoder::colortype::ColorType as TiffColorType; + + #[test] + fn const_generic_sample_tables_have_one_entry_per_sample() { + // A repeat expression in an associated const (`&[BITS; N]`) has, on some compilers, + // promoted to a slice shorter than `N`. The encoder sizes every strip from that length. + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 4, "{:?}", as TiffColorType>::BITS_PER_SAMPLE); + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 5); + assert_eq!( as TiffColorType>::BITS_PER_SAMPLE.len(), 19); + assert_eq!( as TiffColorType>::SAMPLE_FORMAT.len(), 6); + } +} + +#[cfg(test)] +mod extra_sample_buffers { + use super::try_with_capacity; + + /// The extra-sample writer's buffers are allocated fallibly: a size no allocation can serve + /// is an out-of-memory error the save reports, not an abort. + #[test] + fn an_impossible_buffer_is_an_error() { + let err = try_with_capacity::(usize::MAX).unwrap_err(); + assert_eq!(err.kind(), std::io::ErrorKind::OutOfMemory); + assert!(try_with_capacity::(usize::MAX / 2).is_err()); + assert!(try_with_capacity::(64).is_ok_and(|v| v.is_empty() && v.capacity() >= 64)); + } +} diff --git a/crates/image-codecs/src/codecs/tiff/bands.rs b/crates/image-codecs/src/codecs/tiff/bands.rs new file mode 100644 index 000000000..093f90efd --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/bands.rs @@ -0,0 +1,509 @@ +use super::decode::{Layout, Patched, Prepared, Src, is_eof}; +use super::{CodecError, Decoder, SampleType, err, f16, map_err}; + +/// The a* and b* samples (1 and 2 of every pixel) of a Lab image between the file's encoding and +/// PhotoCraft's (8 bits: `a + 128`; 16 bits: `(a + 128) · 257`). In the file, CIELab holds them +/// signed (8 bits: `a`; 16 bits: `a · 256`) and ICCLab offset (8 bits: `a + 128`; 16 bits: +/// `(a + 128) · 256`). `to_file` picks the direction; samples are native-endian. +pub(super) fn convert_ab(data: &mut [u8], channels: usize, sample: SampleType, signed: bool, to_file: bool) { + match sample { + SampleType::U8 if signed => data.chunks_exact_mut(channels).for_each(|px| { + px[1] ^= 0x80; + px[2] ^= 0x80; + }), + SampleType::U16 => data.chunks_exact_mut(channels * 2).for_each(|px| { + for c in [1, 2] { + let v = u16::from_ne_bytes([px[2 * c], px[2 * c + 1]]); + // The file's value as `a · 256` (signed) and back. + let file_a256 = |v: u16| if signed { f64::from(v as i16) } else { f64::from(v) - 32768.0 }; + let out = if to_file { + let a256 = (f64::from(v) - 32896.0) * 256.0 / 257.0; + let a256 = a256.round().clamp(-32768.0, 32767.0); + if signed { (a256 as i16) as u16 } else { (a256 + 32768.0) as u16 } + } else { + (file_a256(v) * 257.0 / 256.0 + 32896.0).round().clamp(0.0, 65535.0) as u16 + }; + px[2 * c..2 * c + 2].copy_from_slice(&out.to_ne_bytes()); + } + }), + _ => {} + } +} + +/// File sample indices that are not a colour channel and not the one alpha sample, capped at +/// [`crate::MAX_EXTRA_SAMPLES`], and how many past the cap were left out. +pub(super) fn extra_sample_indices(spp: usize, color: usize, alpha_at: Option, has_alpha: bool) -> (Vec, usize) { + let mut used = vec![false; spp]; + for i in 0..color.min(spp) { + if let Some(slot) = used.get_mut(i) { + *slot = true; + } + } + if has_alpha { + let a = alpha_at.map(|k| color.saturating_add(k)).unwrap_or(color); + if let Some(slot) = used.get_mut(a) { + *slot = true; + } + } + let mut all: Vec = used.into_iter().enumerate().filter(|(_, on)| !*on).map(|(i, _)| i).collect(); + let over = all.len().saturating_sub(crate::MAX_EXTRA_SAMPLES); + all.truncate(crate::MAX_EXTRA_SAMPLES); + (all, over) +} + +/// Interleaved extra samples (`extra_at.len()` per pixel) → one plane per sample. Each plane is +/// allocated fallibly (the caller has checked their total against the limits). +pub(super) fn split_extra_samples( + buf: &[u8], + w: usize, + h: usize, + at: &[usize], + sample: SampleType, + mut kind_of: impl FnMut(usize) -> u16, +) -> Result, CodecError> { + let b = sample.bytes(); + let n = w.saturating_mul(h); + let spp = at.len(); + let mut out = Vec::with_capacity(spp); + for (ei, &si) in at.iter().enumerate() { + let mut data = zeroed(n.saturating_mul(b))?; + for i in 0..n { + let src = (i.saturating_mul(spp).saturating_add(ei)).saturating_mul(b); + let dst = i.saturating_mul(b); + if let (Some(s), Some(d)) = (buf.get(src..src.saturating_add(b)), data.get_mut(dst..dst.saturating_add(b))) { + d.copy_from_slice(s); + } + } + out.push(crate::ExtraSample { kind: kind_of(si), data }); + } + Ok(out) +} + +/// A zero-filled buffer, allocated fallibly. On native targets the zeroing (and the first +/// touch of every page, which dominates on a fresh multi-hundred-megabyte buffer) is spread over +/// all cores. +pub(super) fn zeroed(len: usize) -> Result, CodecError> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} bytes for the image")))?; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + // The capacity is already there: this only writes the zeros. + v.par_extend(rayon::iter::repeat_n(0u8, len)); + } + #[cfg(target_arch = "wasm32")] + v.resize(len, 0); + Ok(v) +} + +/// `v` refilled with `len` zero bytes, reusing its allocation when it is large enough. +pub(super) fn refill_zeroed(mut v: Vec, len: usize) -> Result, CodecError> { + if v.capacity() < len { + drop(v); + return zeroed(len); + } + v.clear(); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + v.par_extend(rayon::iter::repeat_n(0u8, len)); + } + #[cfg(target_arch = "wasm32")] + v.resize(len, 0); + Ok(v) +} + +/// Chunks decoded and chunks whose data ended early. +#[derive(Default, Clone, Copy)] +pub(super) struct Tally { + pub(super) decoded: usize, + pub(super) missing: usize, +} + +impl std::ops::Add for Tally { + type Output = Tally; + fn add(self, o: Tally) -> Tally { + Tally { decoded: self.decoded + o.decoded, missing: self.missing + o.missing } + } +} + +/// Decodes the bands from `first` on into `data` (`band_bytes` each), in parallel on native +/// targets (one decoder and scratch buffer per worker). `extra` receives samples that are not +/// part of the colour image, `extra_band` bytes per band, when it is non-empty. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn decode_bands( + p: &Prepared, + b: &[u8], + data: &mut [u8], + band_bytes: usize, + first: usize, + extra: &mut [u8], + extra_band: usize, +) -> Result { + use rayon::prelude::*; + if extra.is_empty() { + return data + .par_chunks_mut(band_bytes.max(1)) + .enumerate() + .map_init( + || p.open(b).map(|d| (d, Vec::new())).map_err(|e| e.to_string()), + |worker, (i, band)| { + let (dec, scratch) = worker.as_mut().map_err(|e| err(e.clone()))?; + decode_band(&p.lay, dec, scratch, first + i, band, None) + }, + ) + .try_reduce(Tally::default, |a, b| Ok(a + b)); + } + data.par_chunks_mut(band_bytes.max(1)) + .zip(extra.par_chunks_mut(extra_band.max(1))) + .enumerate() + .map_init( + || p.open(b).map(|d| (d, Vec::new())).map_err(|e| e.to_string()), + |worker, (i, (band, extra_band))| { + let (dec, scratch) = worker.as_mut().map_err(|e| err(e.clone()))?; + decode_band(&p.lay, dec, scratch, first + i, band, Some(extra_band)) + }, + ) + .try_reduce(Tally::default, |a, b| Ok(a + b)) +} + +/// Decodes the bands from `first` on into `data` (`band_bytes` each), one after the other. +#[cfg(target_arch = "wasm32")] +pub(super) fn decode_bands( + p: &Prepared, + b: &[u8], + data: &mut [u8], + band_bytes: usize, + first: usize, + extra: &mut [u8], + extra_band: usize, +) -> Result { + let mut dec = p.open(b)?; + let mut scratch = Vec::new(); + let mut tally = Tally::default(); + if extra.is_empty() { + for (i, band) in data.chunks_mut(band_bytes.max(1)).enumerate() { + tally = tally + decode_band(&p.lay, &mut dec, &mut scratch, first + i, band, None)?; + } + return Ok(tally); + } + for (i, (band, extra_band)) in data.chunks_mut(band_bytes.max(1)).zip(extra.chunks_mut(extra_band.max(1))).enumerate() { + tally = tally + decode_band(&p.lay, &mut dec, &mut scratch, first + i, band, Some(extra_band))?; + } + Ok(tally) +} + +/// Decodes band `b` (one strip, or one row of tiles, in every plane) into its output rows. +fn decode_band( + lay: &Layout, + dec: &mut Decoder>, + scratch: &mut Vec, + b: usize, + band: &mut [u8], + mut extra: Option<&mut [u8]>, +) -> Result { + let out_row = lay.out_row(); + let rows = band.len() / out_row.max(1); + let px = lay.out_channels() * lay.out.bytes(); + let mut tally = Tally::default(); + for plane in 0..lay.planes { + let file_plane = lay.source_sample(plane); + for col in 0..lay.across { + let chunk = file_plane + .checked_mul(lay.chunks_per_plane) + .and_then(|c| c.checked_add(b.checked_mul(lay.across)?)) + .and_then(|c| c.checked_add(col)) + .and_then(|c| u32::try_from(c).ok()) + .ok_or_else(|| err("chunk index out of range"))?; + let pref = dec.image_chunk_buffer_layout(chunk).map_err(map_err)?; + let stride = pref.row_stride.map(|s| s.get()).ok_or_else(|| err("empty chunk row"))?; + // In place: the strip's decoded bytes are exactly its output rows. + if lay.direct() && pref.len == band.len() && stride == out_row { + match dec.read_chunk_bytes(chunk, band) { + Ok(()) => tally.decoded += 1, + // Rows read before the end would survive: a missing strip is zeros. + Err(e) if is_eof(&e) => { + band.fill(0); + tally.missing += 1; + } + Err(e) => return Err(map_err(e)), + } + continue; + } + if pref.len > lay.scratch_cap { + return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); + } + if scratch.len() < pref.len { + scratch + .try_reserve_exact(pref.len - scratch.len()) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; + scratch.resize(pref.len, 0); + } + let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + match dec.read_chunk_bytes(chunk, buf) { + Ok(()) => tally.decoded += 1, + Err(e) if is_eof(&e) => { + tally.missing += 1; + continue; + } + Err(e) => return Err(map_err(e)), + } + let x0 = col * lay.cw; + let n = lay.cw.min(lay.w.saturating_sub(x0)); + for (ri, (src_row, dst_row)) in buf.chunks_exact(stride).zip(band.chunks_exact_mut(out_row)).take(rows).enumerate() { + let Some(dst) = dst_row.get_mut(x0 * px..(x0 + n) * px) else { continue }; + convert_row(lay, src_row, dst, n, plane); + if !lay.planar && plane == 0 && !lay.extra_at.is_empty() { + let erow = lay.extra_row(); + if let Some(extra) = extra.as_deref_mut() + && let Some(er) = extra.get_mut(ri.saturating_mul(erow)..ri.saturating_mul(erow).saturating_add(erow)) + { + deposit_extras(lay, src_row, er, n, x0); + } + } + } + } + } + if lay.planar && !lay.extra_at.is_empty() { + read_planar_extras(lay, dec, scratch, b, rows, extra, &mut tally)?; + } + finish_band(lay, band); + Ok(tally) +} + +/// Copies the non-colour samples of a chunky row into the extra-sample band. +fn deposit_extras(lay: &Layout, src: &[u8], dst: &mut [u8], n: usize, x0: usize) { + let ech = lay.extra_at.len(); + if ech == 0 { + return; + } + let px = ech * lay.out.bytes(); + let Some(dst) = dst.get_mut(x0.saturating_mul(px)..(x0.saturating_add(n)).saturating_mul(px)) else { return }; + for (ei, &si) in lay.extra_at.iter().enumerate() { + transfer(lay.src, src, lay.chunk_spp, si, dst, lay.out, ech, ei, n); + } +} + +/// Planar files store each extra sample as its own plane. Read those planes into `extra`. +fn read_planar_extras( + lay: &Layout, + dec: &mut Decoder>, + scratch: &mut Vec, + b: usize, + rows: usize, + extra: Option<&mut [u8]>, + tally: &mut Tally, +) -> Result<(), CodecError> { + let Some(extra) = extra else { return Ok(()) }; + let ech = lay.extra_at.len(); + let erow = lay.extra_row(); + if ech == 0 || erow == 0 { + return Ok(()); + } + for (ei, &file_plane) in lay.extra_at.iter().enumerate() { + for col in 0..lay.across { + let chunk = file_plane + .checked_mul(lay.chunks_per_plane) + .and_then(|c| c.checked_add(b.checked_mul(lay.across)?)) + .and_then(|c| c.checked_add(col)) + .and_then(|c| u32::try_from(c).ok()) + .ok_or_else(|| err("chunk index out of range"))?; + let pref = dec.image_chunk_buffer_layout(chunk).map_err(map_err)?; + let stride = pref.row_stride.map(|s| s.get()).ok_or_else(|| err("empty chunk row"))?; + if pref.len > lay.scratch_cap { + return Err(CodecError::LimitExceeded(format!("a {} byte strip or tile", pref.len))); + } + if scratch.len() < pref.len { + scratch + .try_reserve_exact(pref.len - scratch.len()) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {} bytes for a strip or tile", pref.len)))?; + scratch.resize(pref.len, 0); + } + let buf = scratch.get_mut(..pref.len).ok_or_else(|| err("scratch buffer"))?; + match dec.read_chunk_bytes(chunk, buf) { + Ok(()) => tally.decoded += 1, + Err(e) if is_eof(&e) => { + tally.missing += 1; + continue; + } + Err(e) => return Err(map_err(e)), + } + let x0 = col * lay.cw; + let n = lay.cw.min(lay.w.saturating_sub(x0)); + for (ri, src_row) in buf.chunks_exact(stride).take(rows).enumerate() { + let start = ri.saturating_mul(erow); + let Some(er) = extra.get_mut(start..start.saturating_add(erow)) else { continue }; + let px = ech * lay.out.bytes(); + let Some(dst) = er.get_mut(x0.saturating_mul(px)..(x0.saturating_add(n)).saturating_mul(px)) else { continue }; + transfer(lay.src, src_row, 1, 0, dst, lay.out, ech, ei, n); + } + } + } + Ok(()) +} + +/// Converts `n` pixels of one decoded chunk row into output pixels (`plane` for planar data). +fn convert_row(lay: &Layout, src: &[u8], dst: &mut [u8], n: usize, plane: usize) { + let och = lay.out_channels(); + if let Some(map) = &lay.palette { + let ob = lay.out.bytes(); + for (i, d) in dst.chunks_exact_mut(och * ob).take(n).enumerate() { + let index = sample_index(lay.src, src, i); + let Some(rgb) = map.get(index) else { continue }; + for (c, v) in d.chunks_exact_mut(ob).zip(rgb) { + if ob == 1 { + c.copy_from_slice(&[(v >> 8) as u8]); + } else { + c.copy_from_slice(&v.to_ne_bytes()); + } + } + } + return; + } + if lay.copyable() { + if let Some(s) = src.get(..dst.len()) { + dst.copy_from_slice(s); + } + return; + } + if lay.planar { + transfer(lay.src, src, 1, 0, dst, lay.out, och, plane, n); + } else { + for k in 0..och { + transfer(lay.src, src, lay.chunk_spp, lay.source_sample(k), dst, lay.out, och, k, n); + } + } +} + +/// The value of pixel `i` in a row of one-sample indices (palette and sub-byte gray). +fn sample_index(src: Src, row: &[u8], i: usize) -> usize { + match src { + Src::Bits(bits) => { + let bits = usize::from(bits); + let bit = i * bits; + let byte = row.get(bit / 8).copied().unwrap_or(0); + usize::from((byte >> (8 - bits - bit % 8)) & ((1u8 << bits) - 1)) + } + Src::U8 => row.get(i).copied().map_or(0, usize::from), + Src::U16 => row.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0, |s| usize::from(u16::from_ne_bytes(s))), + _ => 0, + } +} + +/// Copies sample `si` of each of `n` decoded pixels (`sspp` samples each) to output channel +/// `di` (`dch` channels), converting the sample type. +#[allow(clippy::too_many_arguments)] +fn transfer(src: Src, row: &[u8], sspp: usize, si: usize, dst: &mut [u8], out: SampleType, dch: usize, di: usize, n: usize) { + let ob = out.bytes(); + if let Src::Bits(bits) = src { + let max = (1u32 << bits) - 1; + for (i, d) in dst.chunks_exact_mut(dch * ob).take(n).enumerate() { + if let Some(d) = d.get_mut(di) { + *d = (sample_index(src, row, i) as u32 * 255 / max) as u8; + } + } + return; + } + let sb = src.bytes(); + if src.is_identity() { + // Same sample type: a strided copy. + if sb == 1 { + for (d, s) in dst.iter_mut().skip(di).step_by(dch).zip(row.iter().skip(si).step_by(sspp)).take(n) { + *d = *s; + } + } else { + for (d, s) in dst.chunks_exact_mut(ob).skip(di).step_by(dch).zip(row.chunks_exact(sb).skip(si).step_by(sspp)).take(n) { + d.copy_from_slice(s); + } + } + return; + } + for (s, d) in row.chunks_exact(sspp * sb).zip(dst.chunks_exact_mut(dch * ob)).take(n) { + let (Some(s), Some(d)) = (s.get(si * sb..(si + 1) * sb), d.get_mut(di * ob..(di + 1) * ob)) else { continue }; + match src { + Src::U32 => { + let v = s.try_into().map_or(0, u32::from_ne_bytes); + d.copy_from_slice(&((v >> 16) as u16).to_ne_bytes()); + } + Src::U64 => { + let v = s.try_into().map_or(0, u64::from_ne_bytes); + d.copy_from_slice(&((v >> 48) as u16).to_ne_bytes()); + } + Src::F64 => { + let v = s.try_into().map_or(0.0, f64::from_ne_bytes); + d.copy_from_slice(&(v as f32).to_ne_bytes()); + } + _ => d.copy_from_slice(s), + } + } +} + +/// Applies what the stored samples leave to the reader: WhiteIsZero inversion of the gray +/// channel, and turning associated (premultiplied) alpha into straight alpha. +fn finish_band(lay: &Layout, band: &mut [u8]) { + if !lay.invert && !lay.premultiplied { + return; + } + let ch = lay.out_channels(); + let ob = lay.out.bytes(); + let color = lay.out_layout.color_channels(); + for p in band.chunks_exact_mut(ch * ob) { + match lay.out { + SampleType::U8 => { + if lay.invert + && let Some(g) = p.first_mut() + { + *g = 255 - *g; + } + if lay.premultiplied { + let a = u32::from(p.get(ch - 1).copied().unwrap_or(255)); + for c in p.iter_mut().take(color) { + *c = (u32::from(*c) * 255 + a / 2).checked_div(a).unwrap_or(0).min(255) as u8; + } + } + } + SampleType::U16 => { + let get = |p: &[u8], i: usize| p.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0, u16::from_ne_bytes); + let a = u32::from(get(p, ch - 1)); + for i in 0..color { + let mut v = u32::from(get(p, i)); + if lay.invert && i == 0 { + v = 65535 - v; + } + if lay.premultiplied { + v = (v * 65535 + a / 2).checked_div(a).unwrap_or(0).min(65535); + } + if let Some(s) = p.get_mut(i * 2..i * 2 + 2) { + s.copy_from_slice(&(v as u16).to_ne_bytes()); + } + } + } + SampleType::F16 | SampleType::F32 => { + let get = |p: &[u8], i: usize| -> f32 { + if ob == 2 { + p.get(i * 2..i * 2 + 2).and_then(|s| s.try_into().ok()).map_or(0.0, |s| f16::from_bits(u16::from_ne_bytes(s)).to_f32()) + } else { + p.get(i * 4..i * 4 + 4).and_then(|s| s.try_into().ok()).map_or(0.0, f32::from_ne_bytes) + } + }; + let a = get(p, ch - 1); + for i in 0..color { + let mut v = get(p, i); + if lay.invert && i == 0 { + v = 1.0 - v; + } + if lay.premultiplied && a > 0.0 { + v /= a; + } + if ob == 2 { + if let Some(s) = p.get_mut(i * 2..i * 2 + 2) { + s.copy_from_slice(&f16::from_f32(v).to_bits().to_ne_bytes()); + } + } else if let Some(s) = p.get_mut(i * 4..i * 4 + 4) { + s.copy_from_slice(&v.to_ne_bytes()); + } + } + } + } + } +} diff --git a/crates/image-codecs/src/codecs/tiff/decode.rs b/crates/image-codecs/src/codecs/tiff/decode.rs new file mode 100644 index 000000000..d8e11c447 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/decode.rs @@ -0,0 +1,932 @@ +use std::borrow::Cow; + +use super::bands::{Tally, convert_ab, decode_bands, extra_sample_indices, refill_zeroed, split_extra_samples, zeroed}; +use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS, photoshop_tags}; +use super::ycbcr::YCbCr; +use super::{ + ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, Tag, + camera_raw, err, ifd, io, map_err, tiff_exif, +}; + +/// Decodes the page Photoshop would open: the first full-resolution directory. +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + decode_page(bytes, None, limits) +} + +/// Decodes page `page` of [`ifd::tiff_info`]'s list (`None`: the default page). The image +/// carries the page's metadata, a [`DecodeWarning::MorePages`] when the file holds more +/// full-resolution pages, and a [`DecodeWarning::Truncated`] when image data is cut off. +pub(crate) fn decode_page(bytes: &[u8], page: Option, limits: &Limits) -> Result { + let (file, dir, more) = open_page(bytes, page)?; + let mut img = decode_ifd(&file, &dir, limits)?; + let extra_samples = std::mem::take(&mut img.meta.extra_samples); + img.meta = metadata(&file, &dir, &mut img.icc); + img.meta.extra_samples = extra_samples; + img.warnings.extend(more); + Ok(img) +} + +/// Inspect the selected directory's sample classification without decoding any pixels. +/// Mirror document import: inspect bands first, then the default whole-image route when +/// auxiliary channels are present. Preparation reads metadata, not strips or tiles. +pub(crate) fn has_unretained_samples(bytes: &[u8], offset: u64) -> Result { + let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; + let dir = file.ifd(offset).ok_or_else(|| err("image directory cut off"))?; + let bands = prepare(&file, &dir, &Limits::banded(), cfg!(target_pointer_width = "32"))?; + if bands.extras_over_limit > 0 || bands.extras_dropped > 0 { + return Ok(true); + } + if bands.lay.extra_at.is_empty() { + return Ok(false); + } + Ok(prepare(&file, &dir, &Limits::default(), true)?.extras_dropped > 0) +} + +/// The file, page `page`'s directory (`None`: the default page) and the +/// [`DecodeWarning::MorePages`] the page opens with when the file holds more. +fn open_page(bytes: &[u8], page: Option) -> Result<(File<'_>, Ifd, Option), CodecError> { + if camera_raw(bytes) { + return Err(CodecError::unsupported(F, "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw")); + } + let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; + let info = ifd::info_of(&file)?; + let index = match page { + Some(i) => i, + None => info.default_page().ok_or_else(|| err("no image directory"))?, + }; + let p = info + .pages + .get(index) + .ok_or_else(|| CodecError::InvalidImage(format!("TIFF page {index} does not exist (the file has {} directories)", info.pages.len())))?; + let dir = file.ifd(p.ifd_offset).ok_or_else(|| err("image directory cut off"))?; + let pages = info.page_count(); + let more = (pages > 1).then(|| DecodeWarning::MorePages { total: info.complete.then_some(u32::try_from(pages).unwrap_or(u32::MAX)) }); + Ok((file, dir, more)) +} + +/// Output bytes [`TiffBands::decode`] aims to decode at once (at least one band per worker): small +/// next to the document the rows are written into, and to the copy a caller may convert them to. +const GROUP_BYTES: usize = 64 << 20; + +/// A TIFF page opened for decoding a group of bands at a time, for images too large to hold +/// twice (#8169): [`TiffBands::decode`] hands each group to its caller, who writes it into the +/// document, so only one group exists beside the document. The pixels, layout and metadata are +/// those [`decode_page`] returns; [`TiffBands::upright`] applies the orientation as +/// [`Image::oriented`] does. +pub struct TiffBands<'a> { + b: &'a [u8], + p: Prepared, + /// The page's Orientation tag (1–8). + stored: u16, + /// The orientation the bands are turned by (1: as stored). + turn: u16, + /// `Limits::max_alloc`: the most one group of bands may take. + cap: u64, + pub icc: Option>, + pub meta: Metadata, + /// What the decoder noticed: more pages, and after [`TiffBands::decode`] data cut off. + pub warnings: Vec, +} + +impl<'a> TiffBands<'a> { + /// Opens page `page` (`None`: the page [`decode_page`] opens by default) and checks it + /// against `limits`; on 64-bit targets its `max_alloc` bounds each group of bands, not the + /// whole image. + pub fn open(bytes: &'a [u8], page: Option, limits: &Limits) -> Result { + let (file, dir, more) = open_page(bytes, page)?; + // On 32-bit targets the whole image must still fit `max_alloc`, as it did before: the + // document holding it shares the same small address space (wasm32: 4 GiB). + let p = prepare(&file, &dir, limits, cfg!(target_pointer_width = "32"))?; + let band = p.band_bytes()?; + if band as u64 > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!( + "one strip or row of tiles of this {}x{} image decodes to {band} bytes, over the {} byte limit", + p.w, p.h, limits.max_alloc + ))); + } + let mut icc = None; + let meta = metadata(&file, &dir, &mut icc); + let stored = ifd::orientation_of(&file, &dir); + // Extra samples are dropped here only on 32-bit targets, where the whole image counts. + let mut warnings = prepared_warnings(&p); + warnings.extend(more); + Ok(TiffBands { b: bytes, p, stored, turn: 1, cap: limits.max_alloc, icc, meta, warnings }) + } + + /// The bands turned upright for the page's Orientation tag, as [`Image::oriented`] turns a + /// whole image: the size and resolution swap for 5–8, and the EXIF and XMP orientation are + /// rewritten to 1. + pub fn upright(mut self) -> Self { + let o = self.stored; + if !(2..=8).contains(&o) || self.turn == o { + return self; + } + self.turn = o; + if let Some(e) = &mut self.meta.exif + && let Cow::Owned(fixed) = crate::orientation::upright_exif(e) + { + *e = fixed; + } + if let Some(x) = &mut self.meta.xmp + && let Cow::Owned(fixed) = crate::orientation::upright_xmp(x) + { + *x = fixed; + } + if o >= 5 { + self.meta.dpi = self.meta.dpi.map(|(x, y)| (y, x)); + } + self + } + + /// The page's Orientation tag (1–8; 1 when absent or invalid). + pub fn orientation(&self) -> u16 { + self.stored + } + + /// The size of the image the bands make up (upright when [`TiffBands::upright`] was applied). + pub fn dimensions(&self) -> (u32, u32) { + if self.turn >= 5 { (self.p.h, self.p.w) } else { (self.p.w, self.p.h) } + } + + pub fn layout(&self) -> ChannelLayout { + self.p.lay.out_layout + } + + /// How many samples of a pixel are neither colour nor the alpha channel: alpha and spot + /// channels that [`TiffBands::decode`] does not read (a whole-image decode does). + pub fn extra_samples(&self) -> usize { + self.p.lay.extra_at.len() + } + + pub fn sample_type(&self) -> SampleType { + self.p.lay.out + } + + /// Bands decoded at once: about [`GROUP_BYTES`], at least one per worker, at most `max_alloc`. + fn bands_per_group(&self) -> Result { + let band = self.p.band_bytes()?.max(1); + #[cfg(not(target_arch = "wasm32"))] + let workers = rayon::current_num_threads(); + #[cfg(target_arch = "wasm32")] + let workers = 1; + let cap = usize::try_from(self.cap).unwrap_or(usize::MAX); + Ok((GROUP_BYTES / band).max(workers).min(cap / band).max(1)) + } + + /// Bytes of the largest group [`TiffBands::decode`] hands over: what decoding needs beside + /// the document, for a caller checking that it fits in memory. A file stored as one strip + /// is one group, the whole image. + pub fn group_bytes(&self) -> Result { + let rows = self.bands_per_group()?.saturating_mul(self.p.band_rows).min(self.p.h as usize); + self.p.lay.out_row().checked_mul(rows).ok_or_else(|| CodecError::LimitExceeded("band size".into())) + } + + /// A conservative bound on the decoder's own scratch memory in a whole-image decode of this + /// page (as `decode_page` does it): one strip or tile per worker that decodes a band at + /// once. Zero when strips are decoded in place. Add the decoded image itself and any + /// extra-sample planes. + pub fn decode_scratch_bytes(&self) -> u64 { + #[cfg(not(target_arch = "wasm32"))] + let workers = rayon::current_num_threads() as u64; + #[cfg(target_arch = "wasm32")] + let workers = 1; + scratch_bound(self.p.lay.direct(), self.p.scratch_bytes as u64, self.p.band_rows, self.p.h, workers) + } + + /// Decodes the image a group of bands at a time, in order, handing each group to `sink` + /// with the position of its top-left pixel in the image [`TiffBands::dimensions`] gives. A + /// group is about 64 MiB (at least one strip or row of tiles per worker, at most `max_alloc`; + /// see [`TiffBands::group_bytes`]). Data cut off by the end of the file adds a + /// [`DecodeWarning::Truncated`] to `warnings`. + pub fn decode>(&mut self, mut sink: impl FnMut(u32, u32, &Image) -> Result<(), E>) -> Result<(), E> { + let per_group = self.bands_per_group()?; + let p = &self.p; + let band = p.band_bytes()?.max(1); + let bands = (p.h as usize).div_ceil(p.band_rows.max(1)); + let mut buf: Vec = Vec::new(); + let mut tally = Tally::default(); + let mut first = 0; + while first < bands { + let n = per_group.min(bands - first); + let y0 = first * p.band_rows; + let rows = (n * p.band_rows).min(p.h as usize - y0); + let len = p.lay.out_row().checked_mul(rows).ok_or_else(|| CodecError::LimitExceeded("band size".into()))?; + // Missing chunks leave their rows as they are: they must start empty in every group. + buf = refill_zeroed(buf, len)?; + tally = tally + decode_bands(p, self.b, &mut buf, band, first, &mut [], 0)?; + if let Some(c) = p.ycbcr { + c.convert(&mut buf, p.lay.out_channels(), p.lay.out); + } + if let Some(signed) = p.lab { + convert_ab(&mut buf, p.lay.out_channels(), p.lay.out, signed, false); + } + let rows = u32::try_from(rows).map_err(|_| err("band too tall"))?; + let y0 = u32::try_from(y0).map_err(|_| err("band out of range"))?; + let group = Image::from_raw(p.w, rows, p.lay.out_layout, p.lay.out, std::mem::take(&mut buf))?; + let (x, y) = match self.turn { + 3 | 4 => (0, p.h.saturating_sub(y0 + rows)), + 5 | 8 => (y0, 0), + 6 | 7 => (p.h.saturating_sub(y0 + rows), 0), + _ => (0, y0), + }; + let group = group.oriented(self.turn)?; + sink(x, y, &group)?; + buf = group.into_data(); + first += n; + } + if tally.decoded == 0 && tally.missing > 0 { + return Err(err("the image data is missing (cut off or past the end of the file)").into()); + } + if tally.missing > 0 { + // After the extra-sample warnings and before `MorePages`, as the whole-image decode + // orders them. + let at = self.warnings.iter().position(|w| matches!(w, DecodeWarning::MorePages { .. })).unwrap_or(self.warnings.len()); + self.warnings.insert(at, DecodeWarning::Truncated { format: F }); + } + Ok(()) + } +} + +/// [`TiffBands::decode_scratch_bytes`]'s bound: none when chunks are decoded in place, +/// otherwise one chunk per worker that decodes a band, and never more chunks than the image has. +fn scratch_bound(direct: bool, chunk_bytes: u64, band_rows: usize, h: u32, workers: u64) -> u64 { + if direct { + return 0; + } + let bands = u64::from(h).div_ceil(band_rows.max(1) as u64).max(1); + chunk_bytes.saturating_mul(workers.min(bands)) +} + +/// The Orientation (1–8) of page `page` (`None`: the default page); 1 when absent, malformed +/// or out of range. Classic TIFF and BigTIFF. +pub(crate) fn orientation(bytes: &[u8], page: Option) -> u16 { + let Some(file) = File::parse(bytes) else { return 1 }; + let Ok(info) = ifd::info_of(&file) else { return 1 }; + page.or_else(|| info.default_page()).and_then(|i| info.pages.get(i)).and_then(|p| file.ifd(p.ifd_offset)).map_or(1, |d| ifd::orientation_of(&file, &d)) +} + +/// The page's metadata; the ICC profile goes to `icc`. +fn metadata(f: &File<'_>, dir: &Ifd, icc: &mut Option>) -> Metadata { + *icc = f.tag_bytes(dir, Tag::IccProfile.to_u16()).filter(|b| !b.is_empty()).map(<[u8]>::to_vec); + let text = |tag: u16| { + let b = f.tag_bytes(dir, tag)?; + let s = std::str::from_utf8(b).ok()?.trim_end_matches('\0'); + (!s.is_empty()).then(|| s.to_owned()) + }; + let mut meta = Metadata { xmp: text(TAG_XMP), ..Default::default() }; + let unit = f.tag_uint(dir, Tag::ResolutionUnit.to_u16()).unwrap_or(2); + if let (Some(x), Some(y)) = (f.tag_f64(dir, Tag::XResolution.to_u16()), f.tag_f64(dir, Tag::YResolution.to_u16())) + && x > 0.0 + && y > 0.0 + && x.is_finite() + && y.is_finite() + { + meta.dpi = match unit { + 2 => Some((x as f32, y as f32)), + 3 => Some(((x * 2.54) as f32, (y * 2.54) as f32)), + _ => None, + }; + } + for (tag, key) in TEXT_TAGS { + if let Some(s) = text(tag.to_u16()) { + meta.text.push(((*key).to_owned(), s)); + } + } + // Photoshop keeps a layered TIFF's layers and resources in IFD0, where `photoshop_tags` + // reads them; any other page reads its own directory's copies. + let (resources, layers) = + if dir.at == f.first_ifd { photoshop_tags(f.b) } else { (f.tag_bytes(dir, TAG_PHOTOSHOP), f.tag_bytes(dir, TAG_IMAGE_SOURCE_DATA)) }; + meta.photoshop_resources = resources.map(<[u8]>::to_vec); + meta.photoshop_layers = layers.map(<[u8]>::to_vec); + // EXIF: the Exif and GPS directories IFD0 points to, with IFD0's descriptive tags, as an + // EXIF block (the other pages are reduced copies or layers of the same image). + if dir.at == f.first_ifd + && let Some(e) = tiff_exif::read(f.b).filter(ExifDirs::has_directories) + { + meta.exif = Some(tiff_exif::block(&e)); + } + meta +} + +// --------------------------------------------------------------------------- +// Banded pixel decoding +// --------------------------------------------------------------------------- + +/// The file seen with a few byte ranges replaced: the header's first-IFD offset (so the `tiff` +/// decoder opens the chosen directory as its IFD0) and, for WhiteIsZero and palette images, +/// the PhotometricInterpretation value (so the decoder hands over the stored samples and this +/// module applies the interpretation itself). Reads past the end return no bytes. +#[derive(Clone, Copy)] +pub(super) struct Patched<'a> { + b: &'a [u8], + patches: &'a [(u64, Vec)], + pos: u64, +} + +impl Read for Patched<'_> { + fn read(&mut self, out: &mut [u8]) -> io::Result { + let Some(src) = usize::try_from(self.pos).ok().and_then(|p| self.b.get(p..)) else { return Ok(0) }; + let n = out.len().min(src.len()); + let (Some(o), Some(s)) = (out.get_mut(..n), src.get(..n)) else { return Ok(0) }; + o.copy_from_slice(s); + let end = self.pos.saturating_add(n as u64); + for (at, bytes) in self.patches { + let lo = (*at).max(self.pos); + let hi = at.saturating_add(bytes.len() as u64).min(end); + for i in lo..hi { + let (Ok(oi), Ok(pi)) = (usize::try_from(i - self.pos), usize::try_from(i - at)) else { continue }; + if let (Some(d), Some(&v)) = (o.get_mut(oi), bytes.get(pi)) { + *d = v; + } + } + } + self.pos = end; + Ok(n) + } +} + +impl Seek for Patched<'_> { + fn seek(&mut self, to: SeekFrom) -> io::Result { + let new = match to { + SeekFrom::Start(n) => Some(n), + SeekFrom::End(d) => (self.b.len() as u64).checked_add_signed(d), + SeekFrom::Current(d) => self.pos.checked_add_signed(d), + }; + let new = new.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "seek out of range"))?; + self.pos = new; + Ok(new) + } +} + +/// How a stored sample is laid out in the `tiff` crate's chunk buffer (native endian). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(super) enum Src { + /// 1, 2 or 4-bit unsigned, packed MSB first, rows padded to a byte. + Bits(u8), + U8, + U16, + U32, + U64, + F16, + F32, + F64, +} + +impl Src { + pub(super) fn bytes(self) -> usize { + match self { + Src::Bits(_) | Src::U8 => 1, + Src::U16 | Src::F16 => 2, + Src::U32 | Src::F32 => 4, + Src::U64 | Src::F64 => 8, + } + } + + /// The output sample type: integers above 16 bits keep their top 16, doubles become f32. + fn out(self) -> SampleType { + match self { + Src::Bits(_) | Src::U8 => SampleType::U8, + Src::U16 | Src::U32 | Src::U64 => SampleType::U16, + Src::F16 => SampleType::F16, + Src::F32 | Src::F64 => SampleType::F32, + } + } + + /// Stored bytes and output bytes are the same: a plain copy. + pub(super) fn is_identity(self) -> bool { + matches!(self, Src::U8 | Src::U16 | Src::F16 | Src::F32) + } +} + +/// Everything needed to decode and place the chunks of one directory. +pub(super) struct Layout { + pub(super) w: usize, + pub(super) src: Src, + pub(super) out: SampleType, + pub(super) out_layout: ChannelLayout, + /// Samples per pixel in a decoded chunk (1 for planar data). + pub(super) chunk_spp: usize, + pub(super) planar: bool, + /// Planes decoded for planar data: one per output channel (extra planes are skipped). + pub(super) planes: usize, + /// Tile width (a strip is `w` wide). + pub(super) cw: usize, + pub(super) across: usize, + pub(super) chunks_per_plane: usize, + /// WhiteIsZero: invert the gray channel. + pub(super) invert: bool, + /// The decoded sample (chunky) or file plane (planar) of the alpha channel (when there is one). + alpha_sample: usize, + /// Associated (premultiplied) alpha to turn straight. + pub(super) premultiplied: bool, + /// Palette images: the colour map (16-bit RGB per index). + pub(super) palette: Option>, + /// Upper bound for one chunk's scratch buffer. + pub(super) scratch_cap: usize, + /// File sample indices that are neither colour nor the chosen alpha (spot / extra alpha). + pub(super) extra_at: Vec, +} + +impl Layout { + pub(super) fn out_channels(&self) -> usize { + self.out_layout.channels() + } + /// The decoded sample (chunky) or file plane (planar) of output channel `k`. + pub(super) fn source_sample(&self, k: usize) -> usize { + if self.out_layout.has_alpha() && k + 1 == self.out_channels() { self.alpha_sample } else { k } + } + pub(super) fn out_row(&self) -> usize { + self.w * self.out_channels() * self.out.bytes() + } + pub(super) fn extra_row(&self) -> usize { + self.w.saturating_mul(self.extra_at.len()).saturating_mul(self.out.bytes()) + } + /// A chunky strip already in the output's layout: decoded in place. + pub(super) fn direct(&self) -> bool { + self.copyable() && self.across == 1 + } + /// Chunk rows are output rows: copied without conversion. + pub(super) fn copyable(&self) -> bool { + !self.planar && self.palette.is_none() && self.src.is_identity() && self.chunk_spp == self.out_channels() + } +} + +/// A chunk read that hit the end of the file (the data is missing, not malformed). +pub(super) fn is_eof(e: &tiff::TiffError) -> bool { + matches!(e, tiff::TiffError::IoError(io) if io.kind() == io::ErrorKind::UnexpectedEof) +} + +/// The sample type of a (SampleFormat, BitsPerSample) pair. +fn sample_src(format: u64, bits: u64) -> Result { + Ok(match (format, bits) { + (1, 1 | 2 | 4) => Src::Bits(bits as u8), + (1, 8) => Src::U8, + (1, 16) => Src::U16, + (1, 32) => Src::U32, + (1, 64) => Src::U64, + (3, 16) => Src::F16, + (3, 32) => Src::F32, + (3, 64) => Src::F64, + (2, _) => return Err(CodecError::unsupported(F, "signed integer samples")), + _ => return Err(CodecError::unsupported(F, format!("{bits}-bit samples (sample format {format})"))), + }) +} + +/// The ColorMap of a palette image as RGB entries. +fn read_palette(f: &File<'_>, dir: &Ifd, spp: u64, bits: u64, src: Src) -> Result, CodecError> { + if spp != 1 || !matches!(src, Src::Bits(_) | Src::U8 | Src::U16) { + return Err(CodecError::unsupported(F, format!("palette image with {spp} samples of {bits} bits"))); + } + let n = 1u64 << bits; + let map = dir.get(ifd::TAG_COLOR_MAP).filter(|e| e.ty == 3 && e.count == 3 * n).ok_or_else(|| err("palette image without a valid ColorMap"))?; + let at = |i: u64| f.uint(map, i).and_then(|v| u16::try_from(v).ok()); + let table: Option> = (0..n).map(|i| Some([at(i)?, at(n + i)?, at(2 * n + i)?])).collect(); + table.ok_or_else(|| err("palette ColorMap cut off")) +} + +/// The layout of a multiband read that was asked for every sample. +fn multiband_layout(photometric: u64, has_alpha: bool, gray_alpha: bool) -> ChannelLayout { + match (photometric, has_alpha) { + (2 | 8 | 9, true) => ChannelLayout::Rgba, + (2 | 8 | 9, false) => ChannelLayout::Rgb, + (5, true) => ChannelLayout::CmykA, + (5, false) => ChannelLayout::Cmyk, + _ if gray_alpha => ChannelLayout::GrayA, + _ => ChannelLayout::Gray, + } +} + +/// The chunk tables must match the geometry. Checked in 64 bits, before the `tiff` crate does +/// the same arithmetic in narrower types. +fn check_chunk_tables(f: &File<'_>, dir: &Ifd, (w, h): (u32, u32), spp: u64, planar: bool, tiled: bool) -> Result<(u64, u64, u64), CodecError> { + let need = |tag: u16, name: &str| f.tag_uint(dir, tag).ok_or_else(|| err(format!("missing or unreadable {name}"))); + let (cw, ch) = if tiled { + let tw = need(ifd::TAG_TILE_WIDTH, "TileWidth")?; + let tl = need(ifd::TAG_TILE_LENGTH, "TileLength")?; + (tw, tl) + } else { + (u64::from(w), f.tag_uint(dir, ifd::TAG_ROWS_PER_STRIP).unwrap_or(u64::from(h)).min(u64::from(h))) + }; + if cw == 0 || ch == 0 { + return Err(err("zero tile or strip size")); + } + // The chunk tables must match the geometry. Checked here, in 64 bits, before the `tiff` + // crate does the same arithmetic in narrower types. + let planes_in_file = if planar { spp } else { 1 }; + let expected = u64::from(w) + .div_ceil(cw) + .checked_mul(u64::from(h).div_ceil(ch)) + .and_then(|n| n.checked_mul(planes_in_file)) + .filter(|&n| n <= u64::from(u32::MAX)) + .ok_or_else(|| err("too many strips or tiles"))?; + let (offsets_tag, counts_tag) = if tiled { (324, 325) } else { (273, 279) }; + let counts = (dir.get(offsets_tag).map(|e| e.count), dir.get(counts_tag).map(|e| e.count)); + if counts != (Some(expected), Some(expected)) { + return Err(err(format!("{expected} strips or tiles expected, the tables list {counts:?}"))); + } + Ok((cw, ch, expected)) +} + +/// Byte patches the `tiff` crate reads through: the directory is seen as the last of its chain, +/// and PhotometricInterpretation is handed over as black-is-zero (or RGB for Lab) when asked to. +fn header_patches( + f: &File<'_>, + dir: &Ifd, + photometric_entry: Option, + patch_photometric: bool, + lab_only: bool, +) -> Result)>, CodecError> { + let mut patches: Vec<(u64, Vec)> = Vec::new(); + let encode_u = |v: u64, len: usize| -> Vec { + let b = if f.le { v.to_le_bytes() } else { v.to_be_bytes() }; + if f.le { b.get(..len).map(<[u8]>::to_vec).unwrap_or_default() } else { b.get(8 - len..).map(<[u8]>::to_vec).unwrap_or_default() } + }; + if f.big { + patches.push((8, encode_u(dir.at, 8))); + } else { + let at = u32::try_from(dir.at).map_err(|_| err("directory offset out of range"))?; + patches.push((4, encode_u(u64::from(at), 4))); + } + // The directory is seen as the last of its chain: the chain was walked already, and a + // cycle through it must not fail this page. + patches.push((dir.next_at, vec![0; if f.big { 8 } else { 4 }])); + if patch_photometric { + let e = photometric_entry.ok_or_else(|| err("missing PhotometricInterpretation"))?; + let len = match e.ty { + 3 if e.count == 1 => 2, + 4 if e.count == 1 => 4, + _ => return Err(err("malformed PhotometricInterpretation")), + }; + patches.push((e.value_at, encode_u(if lab_only { 2 } else { 1 }, len))); + } + Ok(patches) +} + +/// Lab and YCbCr are converted after decoding, from 8 or 16-bit samples only: Lab needs at least +/// its three colour samples, YCbCr exactly three. +fn check_converted_model(photometric: u64, spp: u64, bits: u64, src: Src) -> Result<(), CodecError> { + let (name, samples_ok) = match photometric { + 8 | 9 => ("Lab", spp >= 3), + 6 => ("YCbCr", spp == 3), + _ => return Ok(()), + }; + if samples_ok && matches!(src, Src::U8 | Src::U16) { + return Ok(()); + } + Err(CodecError::unsupported(F, format!("{name} image with {spp} samples of {bits} bits"))) +} + +/// A directory checked and laid out for decoding, before any pixel is read. +pub(super) struct Prepared { + w: u32, + h: u32, + pub(super) lay: Layout, + /// Rows per band: one strip, or one row of tiles. + band_rows: usize, + patches: Vec<(u64, Vec)>, + tl: tiff::decoder::Limits, + /// Lab samples: `Some(true)` for CIELab (a* and b* stored signed), `Some(false)` for ICCLab. + lab: Option, + /// YCbCr samples: the conversion to RGB (#10316). + ycbcr: Option, + /// The colour samples of a pixel in the file's colour model. + color: usize, + /// The ExtraSamples tag (0 unspecified, 1 associated alpha, 2 unassociated alpha). + extra_kinds: Vec, + /// Extra samples past `MAX_EXTRA_SAMPLES`, left out. + extras_over_limit: usize, + /// Extra samples left out because they did not fit the allocation budget beside the image. + extras_dropped: u32, + /// Bytes one strip or tile decodes to, before clipping to the image: a worker's scratch + /// buffer. A strip is the full width; a tile keeps its declared size even at an edge. + scratch_bytes: usize, +} + +impl Prepared { + /// A decoder over `b` (the file the directory was read from) that opens the directory. + pub(super) fn open<'a>(&'a self, b: &'a [u8]) -> Result>, CodecError> { + Decoder::new(Patched { b, patches: &self.patches, pos: 0 }).map(|d| d.with_limits(self.tl.clone())).map_err(map_err) + } + + /// Output bytes of one full band. + fn band_bytes(&self) -> Result { + self.lay.out_row().checked_mul(self.band_rows).ok_or_else(|| CodecError::LimitExceeded("band size".into())) + } +} + +/// Decodes the pixels of one directory. +fn decode_ifd(f: &File<'_>, dir: &Ifd, limits: &Limits) -> Result { + let p = prepare(f, dir, limits, true)?; + // The output, allocated once and fallibly. + let len = p.lay.out_row().checked_mul(p.h as usize).ok_or_else(|| CodecError::LimitExceeded("image size".into()))?; + let mut data = zeroed(len)?; + let extra_row = p.lay.extra_row(); + let extra_len = extra_row.checked_mul(p.h as usize).ok_or_else(|| CodecError::LimitExceeded("extra samples".into()))?; + let mut extra_data = if p.lay.extra_at.is_empty() { Vec::new() } else { zeroed(extra_len)? }; + let extra_band = extra_row.saturating_mul(p.band_rows); + let tally = decode_bands(&p, f.b, &mut data, p.band_bytes()?, 0, extra_data.as_mut_slice(), extra_band)?; + if tally.decoded == 0 && tally.missing > 0 { + return Err(err("the image data is missing (cut off or past the end of the file)")); + } + if let Some(c) = p.ycbcr { + c.convert(&mut data, p.lay.out_channels(), p.lay.out); + } + if let Some(signed) = p.lab { + convert_ab(&mut data, p.lay.out_channels(), p.lay.out, signed, false); + } + let mut img = Image::from_raw(p.w, p.h, p.lay.out_layout, p.lay.out, data)?; + if !p.lay.extra_at.is_empty() { + // TIFF 6.0 defines ExtraSamples 0, 1 and 2 only; a reserved value reads as unspecified, + // so a save never writes it back. + let kind_of = |si: usize| match si.checked_sub(p.color).and_then(|k| p.extra_kinds.get(k)).copied() { + Some(v @ 0..=2) => v as u16, + _ => 0, + }; + img.meta.extra_samples = split_extra_samples(&extra_data, p.w as usize, p.h as usize, &p.lay.extra_at, p.lay.out, kind_of)?; + } + img.warnings.extend(prepared_warnings(&p)); + if tally.missing > 0 { + img.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(img) +} + +/// What [`prepare`] left out of the image: extra samples past the supported count, then extra +/// samples over the allocation budget. Both decoders report them, in this order. +fn prepared_warnings(p: &Prepared) -> Vec { + let mut warnings = Vec::new(); + if p.extras_over_limit > 0 { + let count = u32::try_from(p.extras_over_limit).unwrap_or(u32::MAX); + warnings.push(DecodeWarning::ExtraSamplesOverLimit { format: F, count }); + } + if p.extras_dropped > 0 { + warnings.push(DecodeWarning::ExtraSamplesDropped { format: F, count: p.extras_dropped }); + } + warnings +} + +/// Checks one directory and lays it out for decoding. `whole`: the image is decoded into one +/// buffer, which `limits.max_alloc` bounds; otherwise only its size is checked here. +fn prepare(f: &File<'_>, dir: &Ifd, limits: &Limits, whole: bool) -> Result { + // The values the `tiff` crate reads must exist: a count past the end of the file is + // neither allocated nor read. + if let Some(e) = dir.entries.iter().find(|e| ifd::STRUCTURAL_TAGS.contains(&e.tag) && f.data(e).is_none()) { + return Err(err(format!("tag {} points past the end of the file", e.tag))); + } + let need = |tag: u16, name: &str| f.tag_uint(dir, tag).ok_or_else(|| err(format!("missing or unreadable {name}"))); + let w = u32::try_from(need(ifd::TAG_WIDTH, "ImageWidth")?).map_err(|_| err("ImageWidth out of range"))?; + let h = u32::try_from(need(ifd::TAG_HEIGHT, "ImageLength")?).map_err(|_| err("ImageLength out of range"))?; + if w == 0 || h == 0 { + return Err(CodecError::InvalidImage(format!("zero-sized image {w}x{h}"))); + } + let spp = f.tag_uint(dir, ifd::TAG_SAMPLES).unwrap_or(1); + if spp == 0 || spp > 64 { + return Err(CodecError::unsupported(F, format!("{spp} samples per pixel"))); + } + let bits_all = f.tag_uints(dir, ifd::TAG_BITS, spp).unwrap_or_else(|| vec![1]); + let bits = bits_all.first().copied().unwrap_or(1); + if bits_all.iter().any(|&b| b != bits) { + return Err(CodecError::unsupported(F, format!("mixed bits per sample {bits_all:?}"))); + } + let format = f.tag_uint(dir, ifd::TAG_SAMPLE_FORMAT).unwrap_or(1); + let photometric_entry = dir.get(ifd::TAG_PHOTOMETRIC).copied(); + let photometric = f.tag_uint(dir, ifd::TAG_PHOTOMETRIC).ok_or_else(|| CodecError::unsupported(F, "missing PhotometricInterpretation"))?; + let planar = f.tag_uint(dir, ifd::TAG_PLANAR) == Some(2); + let extra = f.tag_uints(dir, ifd::TAG_EXTRA_SAMPLES, 64).unwrap_or_default(); + let tiled = dir.get(ifd::TAG_TILE_OFFSETS).is_some(); + let (cw, ch, expected) = check_chunk_tables(f, dir, (w, h), spp, planar, tiled)?; + let (offsets_tag, counts_tag) = if tiled { (324, 325) } else { (273, 279) }; + let src = sample_src(format, bits)?; + // WhiteIsZero and palette images are handed to the `tiff` crate as BlackIsZero: it then + // returns the stored samples, and the interpretation is applied here (for every depth and + // with alpha, which the crate's own inversion does not cover). + let invert = photometric == 0; + let palette = if photometric == 3 { Some(read_palette(f, dir, spp, bits, src)?) } else { None }; + // CIE L*a*b*: CIELab (8; a* and b* signed) and ICCLab (9; a* and b* offset) are handed to the + // `tiff` crate as RGB, then labelled Lab with a* and b* in PhotoCraft's encoding + // (`convert_ab`). + let lab = matches!(photometric, 8 | 9); + // YCbCr (6), as scanners and cameras write it (usually JPEG-compressed with subsampled + // chroma): the `tiff` crate decodes and upsamples the samples, and they are turned into RGB + // after decoding (#10316). + let ycbcr = photometric == 6; + check_converted_model(photometric, spp, bits, src)?; + // TIFF 6.0 lets any extra sample be the alpha (#1349). For RGB and CMYK the `tiff` crate + // returns the alpha only when it is the first extra sample: otherwise it is asked for every + // sample (as multiband gray) and the colour and alpha samples are picked here. An alpha + // position past the samples the file has is ignored. + let color: usize = match photometric { + 2 | 6 | 8 | 9 => 3, + 5 => 4, + _ => 1, + }; + let alpha_at = extra.iter().position(|&e| e == 1 || e == 2).filter(|&a| (color as u64).saturating_add(a as u64) < spp); + // Samples the colour model does not keep. RGB, CMYK and Lab only hand back every sample when + // asked (photometric patched to black-is-zero below); gray already comes back as multiband. + let unused = !extra.is_empty() && (spp as usize) > color.saturating_add(usize::from(alpha_at.is_some())); + let all_samples = palette.is_none() && matches!(photometric, 2 | 5 | 8 | 9) && (alpha_at.is_some_and(|a| a > 0) || (!planar && unused)); + let patches = header_patches(f, dir, photometric_entry, invert || palette.is_some() || all_samples || lab, lab && !all_samples)?; + + let tl = { + let mut l = tiff::decoder::Limits::default(); + l.decoding_buffer_size = limits.alloc_usize(); + // No compressed chunk can be larger than the file. + l.intermediate_buffer_size = f.b.len().max(1); + l + }; + let mut dec = Decoder::new(Patched { b: f.b, patches: &patches, pos: 0 }).map(|d| d.with_limits(tl.clone())).map_err(map_err)?; + if dec.dimensions().map_err(map_err)? != (w, h) || (dec.get_chunk_type() == ChunkType::Tile) != tiled { + return Err(err("inconsistent image directory")); + } + let ct = dec.colortype().map_err(map_err)?; + let gray_alpha = alpha_at.is_some() || (extra.is_empty() && spp == 2); + let (out_layout, chunk_spp) = match ct { + _ if palette.is_some() => (ChannelLayout::Rgb, 1), + tiff::ColorType::Gray(_) => (ChannelLayout::Gray, 1), + tiff::ColorType::Multiband { num_samples, .. } if all_samples => { + // Lab reads as RGB here and is relabelled below. + let layout = multiband_layout(photometric, alpha_at.is_some(), gray_alpha); + (layout, usize::from(num_samples)) + } + tiff::ColorType::Multiband { num_samples, .. } if photometric <= 1 => { + (if gray_alpha { ChannelLayout::GrayA } else { ChannelLayout::Gray }, usize::from(num_samples)) + } + tiff::ColorType::RGB(_) => (ChannelLayout::Rgb, 3), + tiff::ColorType::YCbCr(_) if ycbcr => (ChannelLayout::Rgb, 3), + tiff::ColorType::RGBA(_) => (ChannelLayout::Rgba, 4), + tiff::ColorType::CMYK(_) => (ChannelLayout::Cmyk, 4), + tiff::ColorType::CMYKA(_) => (ChannelLayout::CmykA, 5), + other => return Err(CodecError::unsupported(F, format!("colour type {other:?}"))), + }; + let out_layout = match out_layout { + ChannelLayout::Rgb if lab => ChannelLayout::Lab, + ChannelLayout::Rgba if lab => ChannelLayout::LabA, + _ if lab => return Err(err("Lab image that does not read as three colour samples")), + l => l, + }; + let chunk_spp = if planar { 1 } else { chunk_spp }; + // The sample (chunky) or plane (planar) that holds the alpha: the colour samples come first. + let alpha_sample = out_layout.color_channels() + alpha_at.unwrap_or(0); + if out_layout.has_alpha() && (alpha_sample as u64 >= spp || (!planar && alpha_sample >= chunk_spp)) { + return Err(err("the alpha sample is past the samples of a pixel")); + } + if matches!(src, Src::Bits(_)) && (out_layout.channels() > 1 || spp != 1) && palette.is_none() { + return Err(CodecError::unsupported(F, format!("{bits}-bit samples with {spp} samples per pixel"))); + } + if (!planar && palette.is_none() && chunk_spp < out_layout.channels()) || (planar && out_layout.channels() as u64 > spp) { + return Err(err("fewer samples than the colour type needs")); + } + let out = match (&palette, src) { + (Some(_), Src::U16) => SampleType::U16, + (Some(_), _) => SampleType::U8, + _ => src.out(), + }; + if whole { + limits.check(w, h, out_layout, out)?; + } else { + limits.check_size(w, h)?; + } + // Decompression-bomb guard: no compression method expands data by more than a few thousand + // times (Deflate at most ~1032:1, LZW ~2730:1, PackBits 128:1), so a file far too small for + // its declared size is refused before the image is allocated. CCITT Group 4 codes a blank row + // of a strip or tile in one bit (a V0 code), so its bound is the chunk width (the decoder + // reads at most 65535): a blank landscape A4 page at 600 dpi, 7016 px wide, compresses past + // 4096:1 (Groups 3 and 2 are not decoded). Uncompressed data may be up + // to three quarters missing (that decodes with a `Truncated` warning). Only the bytes the + // strip or tile tables point at inside the file count (and never more than the file), so + // padding the file while pointing the chunks past its end does not raise the bound: the + // banded decoder has no whole-image `max_alloc` to fall back on. + let compression = f.tag_uint(dir, ifd::TAG_COMPRESSION).unwrap_or(1); + let stored = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(spp).saturating_mul(bits).div_ceil(8); + let ratio: u64 = match compression { + 1 => 4, + 32773 => 130, + 4 => cw.clamp(4096, 65_535), + _ => 4096, + }; + let len = f.b.len() as u64; + let (offsets, sizes) = ( + dir.get(offsets_tag).ok_or_else(|| err("missing strip or tile offsets"))?, + dir.get(counts_tag).ok_or_else(|| err("missing strip or tile byte counts"))?, + ); + let mut held = 0u64; + for i in 0..expected { + if held >= len { + break; + } + if let (Some(at), Some(n)) = (f.uint(offsets, i), f.uint(sizes, i)) { + held = held.saturating_add(n.min(len.saturating_sub(at))); + } + } + let held = held.min(len); + if stored > held.saturating_mul(ratio).saturating_add(1 << 20) { + return Err(err(format!("the strips or tiles in this file hold {held} bytes, too few for a {w}x{h} image"))); + } + + let (wu, hu) = (w as usize, h as usize); + let (cw, ch) = (usize::try_from(cw).map_err(|_| err("tile too wide"))?, usize::try_from(ch).map_err(|_| err("tile too tall"))?); + let across = wu.div_ceil(cw); + let down = hu.div_ceil(ch); + let chunks_per_plane = across.checked_mul(down).ok_or_else(|| err("too many tiles"))?; + let planes_in_file = if planar { spp as usize } else { 1 }; + let total = if tiled { dec.tile_count() } else { dec.strip_count() }.map_err(map_err)?; + drop(dec); + if Some(total as usize) != chunks_per_plane.checked_mul(planes_in_file) { + return Err(err("strip or tile count does not match the image size")); + } + // One chunk's decoded bytes, clipped to the image: the scratch buffer may hold a little + // more (the `tiff` crate reads whole tiles in some planar cases) but never an amount that + // only a hostile tile size could ask for. + let chunk_bytes = cw + .min(wu) + .checked_mul(ch.min(hu)) + .and_then(|px| px.checked_mul(chunk_spp)) + .and_then(|s| s.checked_mul(src.bytes())) + .ok_or_else(|| CodecError::LimitExceeded("tile size".into()))?; + let scratch_cap = chunk_bytes.saturating_mul(2).max(64 << 20).min(limits.alloc_usize()); + // One chunk as the decoder actually reads it: a tile keeps its declared size at an edge, + // which can be more than the clipped image bytes above. + let scratch_bytes = cw.saturating_mul(ch).saturating_mul(chunk_spp).saturating_mul(src.bytes()); + let (extra_at, extras_over_limit) = extra_sample_indices(spp as usize, color, alpha_at, out_layout.has_alpha()); + let mut lay = Layout { + w: wu, + src, + out, + out_layout, + chunk_spp, + planar, + planes: if planar { out_layout.channels() } else { 1 }, + cw, + across, + chunks_per_plane, + invert: invert && palette.is_none(), + alpha_sample, + premultiplied: out_layout.has_alpha() && alpha_at.and_then(|a| extra.get(a)) == Some(&1), + palette, + scratch_cap, + extra_at, + }; + // Extra samples share the allocation budget with the image: their interleaved buffer and the + // planes split from it are held next to the image, so both count against `max_alloc`. When + // they don't fit, the image still opens (it passed its own check above) without them. + let mut extras_dropped = 0; + if whole && !lay.extra_at.is_empty() { + let per_pixel = out_layout.channels().saturating_add(lay.extra_at.len().saturating_mul(2)).saturating_mul(out.bytes()); + if limits.check_bytes(w, h, per_pixel as u64).is_err() { + extras_dropped = u32::try_from(lay.extra_at.len()).unwrap_or(u32::MAX); + lay.extra_at.clear(); + } + } + Ok(Prepared { + w, + h, + lay, + band_rows: ch.min(hu), + patches, + tl, + lab: lab.then_some(photometric == 8), + ycbcr: ycbcr.then(|| YCbCr::of(f, dir, compression, if out == SampleType::U16 { 65535.0 } else { 255.0 })), + color, + extra_kinds: extra, + extras_over_limit, + extras_dropped, + scratch_bytes, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{EncodeOptions, ExtraSample, Format}; + + /// `TiffBands::open` drops extra samples over the budget only on 32-bit targets (wasm32), + /// so it reports them through the same function as the whole-image decode. + #[test] + fn extra_samples_over_the_budget_are_reported_by_both_decoders() { + // Several strips (about 1 MB each), so one strip of all four samples fits the image's budget. + let (w, h) = (1024u32, 1024u32); + let n = (w * h) as usize; + let mut img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::U8, vec![90; 3 * n]).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![7; n] }]; + let bytes = crate::encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let (file, dir, _) = open_page(&bytes, None).unwrap(); + let tight = Limits { max_alloc: 3 * n as u64, ..Limits::default() }; + let p = prepare(&file, &dir, &tight, true).unwrap(); + assert_eq!(prepared_warnings(&p), vec![DecodeWarning::ExtraSamplesDropped { format: F, count: 1 }]); + assert!(prepared_warnings(&prepare(&file, &dir, &tight, false).unwrap()).is_empty(), "banded: only the band counts"); + assert!(prepared_warnings(&prepare(&file, &dir, &Limits::default(), true).unwrap()).is_empty()); + let whole = decode(&bytes, &tight).unwrap(); + assert_eq!(whole.warnings, prepared_warnings(&p)); + } + + /// The scratch bound a whole-image decode reserves: none for chunks decoded in place, one + /// chunk per worker otherwise, never more chunks than the image has bands. + #[test] + fn whole_image_scratch_is_one_chunk_per_worker() { + assert_eq!(scratch_bound(true, 1 << 20, 16, 1024, 8), 0, "strips decoded in place need no scratch"); + assert_eq!(scratch_bound(false, 1 << 20, 16, 1024, 8), 8 << 20, "one strip per worker"); + assert_eq!(scratch_bound(false, 1 << 20, 16, 32, 8), 2 << 20, "never more strips than the image has"); + assert_eq!(scratch_bound(false, 1 << 20, 0, 2, 8), 2 << 20, "a zero band height counts as one row"); + assert_eq!(scratch_bound(false, u64::MAX, 1, 1, 8), u64::MAX, "the bound saturates"); + } +} diff --git a/crates/image-codecs/src/codecs/tiff/encode.rs b/crates/image-codecs/src/codecs/tiff/encode.rs new file mode 100644 index 000000000..18b02a603 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/encode.rs @@ -0,0 +1,659 @@ +use super::bands::convert_ab; +use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS}; +use super::{ + ChannelLayout, CmykA16, CmykA32F, CodecError, Cow, EncodeOptions, ExifDirs, GrayA8, GrayA16, GrayA32F, Image, Lab8, Lab16, LabA8, LabA16, + PhotometricInterpretation, Plan, Rational, ResolutionUnit, SampleFormat, SampleType, Tag, TiffColorType, TiffCompression, TiffEncoder, TiffKind, + TiffKindBig, TiffKindStandard, TiffValue, Type, colortype, io, tiff_exif, +}; +use std::io::Cursor; + +// --------------------------------------------------------------------------- +// Encoding +// --------------------------------------------------------------------------- + +/// Bytes written with TIFF type UNDEFINED (7), as required for ICC. +struct Undefined<'a>(&'a [u8]); + +impl TiffValue for Undefined<'_> { + const BYTE_LEN: u8 = 1; + const FIELD_TYPE: Type = Type::UNDEFINED; + fn count(&self) -> usize { + self.0.len() + } + fn data(&self) -> Cow<'_, [u8]> { + Cow::Borrowed(self.0) + } +} + +// Colour types with 1..=MAX_EXTRA_SAMPLES trailing extra samples (spot channels, extra alpha). +// One generic type per (sample, photometric, bit depth, total sample count) instead of a +// hand-written type for every layout. `N` is the total sample count, not the extra count. +const fn extra_photo(v: u16) -> PhotometricInterpretation { + match v { + 1 => PhotometricInterpretation::BlackIsZero, + 2 => PhotometricInterpretation::RGB, + 5 => PhotometricInterpretation::CMYK, + 8 => PhotometricInterpretation::CIELab, + _ => PhotometricInterpretation::RGB, + } +} + +trait ExtraKind: Copy + TiffValue + 'static { + const FORMAT: SampleFormat; + fn predict(row: &[Self], out: &mut Vec, samples: usize); +} + +impl ExtraKind for u8 { + const FORMAT: SampleFormat = SampleFormat::Uint; + fn predict(row: &[Self], out: &mut Vec, samples: usize) { + let n = samples.min(row.len()); + out.extend_from_slice(&row[..n]); + out.extend(row.iter().zip(&row[n..]).map(|(prev, current)| current.wrapping_sub(*prev))); + } +} + +impl ExtraKind for u16 { + const FORMAT: SampleFormat = SampleFormat::Uint; + fn predict(row: &[Self], out: &mut Vec, samples: usize) { + let n = samples.min(row.len()); + out.extend_from_slice(&row[..n]); + out.extend(row.iter().zip(&row[n..]).map(|(prev, current)| current.wrapping_sub(*prev))); + } +} + +impl ExtraKind for f32 { + const FORMAT: SampleFormat = SampleFormat::IEEEFP; + fn predict(row: &[Self], out: &mut Vec, _samples: usize) { + out.extend_from_slice(row); + } +} + +pub(super) struct Extras(pub(super) std::marker::PhantomData); + +impl TiffColorType for Extras +where + T: ExtraKind, +{ + type Inner = T; + const TIFF_VALUE: PhotometricInterpretation = extra_photo(PHOTO); + const BITS_PER_SAMPLE: &'static [u16] = &[BITS; N]; + const SAMPLE_FORMAT: &'static [SampleFormat] = &[T::FORMAT; N]; + fn horizontal_predict(row: &[Self::Inner], result: &mut Vec) { + T::predict(row, result, N); + } +} + +macro_rules! write_extras { + ($n:expr, $enc:expr, $w:expr, $h:expr, $data:expr, $tags:expr, $sample:ty, $photo:literal, $bits:literal, $base:literal) => { + match $n { + 1 => write_one::, K>($enc, $w, $h, $data, $tags), + 2 => write_one::, K>($enc, $w, $h, $data, $tags), + 3 => write_one::, K>($enc, $w, $h, $data, $tags), + 4 => write_one::, K>($enc, $w, $h, $data, $tags), + 5 => write_one::, K>($enc, $w, $h, $data, $tags), + 6 => write_one::, K>($enc, $w, $h, $data, $tags), + 7 => write_one::, K>($enc, $w, $h, $data, $tags), + 8 => write_one::, K>($enc, $w, $h, $data, $tags), + 9 => write_one::, K>($enc, $w, $h, $data, $tags), + 10 => write_one::, K>($enc, $w, $h, $data, $tags), + 11 => write_one::, K>($enc, $w, $h, $data, $tags), + 12 => write_one::, K>($enc, $w, $h, $data, $tags), + 13 => write_one::, K>($enc, $w, $h, $data, $tags), + 14 => write_one::, K>($enc, $w, $h, $data, $tags), + 15 => write_one::, K>($enc, $w, $h, $data, $tags), + 16 => write_one::, K>($enc, $w, $h, $data, $tags), + other => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported extra samples ({other})")).into()), + } + }; +} + +/// An empty buffer with room for `len` elements, allocated fallibly: a save too large for memory +/// is an error, not an abort. +pub(super) fn try_with_capacity(len: usize) -> io::Result> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; + Ok(v) +} + +fn bytes_as_u16(b: &[u8]) -> io::Result> { + let chunks = b.as_chunks::<2>().0; + let mut v = try_with_capacity(chunks.len())?; + v.extend(chunks.iter().map(|c| u16::from_ne_bytes(*c))); + Ok(v) +} + +fn bytes_as_f32(b: &[u8]) -> io::Result> { + let chunks = b.as_chunks::<4>().0; + let mut v = try_with_capacity(chunks.len())?; + v.extend(chunks.iter().map(|c| f32::from_ne_bytes(*c))); + Ok(v) +} + +/// Interleaves colour (and alpha) samples with [`Metadata::extra_samples`]. +fn interleave_extras(img: &Image) -> Result, String> { + let (w, h) = img.dimensions(); + let n = (w as usize).checked_mul(h as usize).ok_or_else(|| "image size overflows".to_string())?; + let sb = img.sample_type().bytes(); + let ch = img.layout().channels(); + let extras = &img.meta.extra_samples; + let total = ch.checked_add(extras.len()).ok_or_else(|| "too many samples".to_string())?; + let px = total.checked_mul(sb).ok_or_else(|| "sample size overflows".to_string())?; + let len = n.checked_mul(px).ok_or_else(|| "image size overflows".to_string())?; + let mut out = try_with_capacity::(len).map_err(|e| e.to_string())?; + out.resize(len, 0); + let base = img.data(); + let base_px = ch.saturating_mul(sb); + for i in 0..n { + let src = i.saturating_mul(base_px); + let dst = i.saturating_mul(px); + let Some(s) = base.get(src..src.saturating_add(base_px)) else { + return Err("image buffer is shorter than its dimensions".into()); + }; + let Some(d) = out.get_mut(dst..dst.saturating_add(base_px)) else { + return Err("extra-sample buffer is short".into()); + }; + d.copy_from_slice(s); + for (k, e) in extras.iter().enumerate() { + let es = i.saturating_mul(sb); + let ed = dst.saturating_add(base_px).saturating_add(k.saturating_mul(sb)); + let Some(s) = e.data.get(es..es.saturating_add(sb)) else { + return Err(format!("extra sample {k} is shorter than the image")); + }; + let Some(d) = out.get_mut(ed..ed.saturating_add(sb)) else { + return Err("extra-sample buffer is short".into()); + }; + d.copy_from_slice(s); + } + } + Ok(out) +} + +fn write_with_extras(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + use ChannelLayout as L; + use SampleType as S; + let (w, h) = img.dimensions(); + let n = img.meta.extra_samples.len(); + if img.layout().is_lab() { + let file = lab_file(img); + let bytes = interleave_extras(&file).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + return match (file.layout(), file.sample_type()) { + (L::Lab, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 8, 8, 3), + (L::Lab, _) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 8, 16, 3) + } + (_, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 8, 8, 4), + _ => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 8, 16, 4) + } + }; + } + let bytes = interleave_extras(img).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; + match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 1, 8, 1), + (L::Gray, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 1, 16, 1) + } + (L::Gray, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 1, 32, 1) + } + (L::GrayA, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 1, 8, 2), + (L::GrayA, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 1, 16, 2) + } + (L::GrayA, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 1, 32, 2) + } + (L::Rgb, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 2, 8, 3), + (L::Rgb, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 2, 16, 3) + } + (L::Rgb, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 2, 32, 3) + } + (L::Rgba, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 2, 8, 4), + (L::Rgba, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 2, 16, 4) + } + (L::Rgba, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 2, 32, 4) + } + (L::Cmyk, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 5, 8, 4), + (L::Cmyk, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 5, 16, 4) + } + (L::Cmyk, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 5, 32, 4) + } + (L::CmykA, S::U8) => write_extras!(n, enc, w, h, &bytes, tags, u8, 5, 8, 5), + (L::CmykA, S::U16) => { + let v = bytes_as_u16(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, u16, 5, 16, 5) + } + (L::CmykA, S::F32) => { + let v = bytes_as_f32(&bytes)?; + write_extras!(n, enc, w, h, &v, tags, f32, 5, 32, 5) + } + _ => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported extra samples ({n})")).into()), + } +} + +struct TagSet<'a> { + icc: Option<&'a [u8]>, + /// The EXIF block's directories and IFD0 tags, written as the TIFF's own. + exif: Option, + xmp: Option<&'a [u8]>, + dpi: Option<(f32, f32)>, + text: Vec<(Tag, &'a str)>, + /// ExtraSamples tag, including the unassociated-alpha marker when the image has alpha. + extra_samples: &'a [u16], + photoshop_resources: Option<&'a [u8]>, + photoshop_layers: Option<&'a [u8]>, + /// An image pyramid level: written with `NewSubfileType` 1 (reduced resolution). + reduced: bool, +} + +/// TIFF `NewSubfileType` (254). +const TAG_NEW_SUBFILE_TYPE: u16 = 254; + +/// Writes one image directory. (The `tiff` encoder only compresses through `write_data`, which +/// takes every sample at once.) +fn write_one(enc: &mut TiffEncoder<&mut Cursor>, K>, w: u32, h: u32, data: &[C::Inner], tags: &TagSet<'_>) -> tiff::TiffResult<()> +where + C: TiffColorType, + [C::Inner]: TiffValue, + K: TiffKind, +{ + // The Exif and GPS directories go before the image, outside its chain; IFD0 points to them. + let mut sub = |e: &ExifDirs, fields: &[tiff_exif::Field]| (!fields.is_empty()).then(|| tiff_exif::write_directory(enc, fields, e.le)).transpose(); + let exif_at = match &tags.exif { + Some(e) => sub(e, &e.exif)?, + None => None, + }; + let gps_at = match &tags.exif { + Some(e) => sub(e, &e.gps)?, + None => None, + }; + let mut im = enc.new_image::(w, h)?; + if let Some((x, y)) = tags.dpi { + im.resolution_unit(ResolutionUnit::Inch); + im.x_resolution(Rational { n: (x * 1000.0).round() as u32, d: 1000 }); + im.y_resolution(Rational { n: (y * 1000.0).round() as u32, d: 1000 }); + } + let d = im.encoder(); + if tags.reduced { + d.write_tag(Tag::Unknown(TAG_NEW_SUBFILE_TYPE), 1u32)?; + } + if !tags.extra_samples.is_empty() { + d.write_tag(Tag::ExtraSamples, tags.extra_samples)?; + } + if let Some(icc) = tags.icc { + d.write_tag(Tag::IccProfile, Undefined(icc))?; + } + if let Some(xmp) = tags.xmp { + d.write_tag(Tag::Unknown(TAG_XMP), xmp)?; + } + for (tag, s) in &tags.text { + d.write_tag(*tag, *s)?; + } + if let Some(e) = &tags.exif { + // The EXIF block's descriptive IFD0 tags, unless the text metadata already wrote them. + let ifd0: Vec = e.ifd0.iter().filter(|f| !tags.text.iter().any(|(t, _)| t.to_u16() == f.tag)).cloned().collect(); + tiff_exif::write_fields(d, &ifd0, e.le)?; + } + if let Some(at) = exif_at { + d.write_tag(Tag::Unknown(tiff_exif::TAG_EXIF_IFD), at)?; + } + if let Some(at) = gps_at { + d.write_tag(Tag::Unknown(tiff_exif::TAG_GPS_IFD), at)?; + } + if let Some(r) = tags.photoshop_resources { + d.write_tag(Tag::Unknown(TAG_PHOTOSHOP), r)?; + } + if let Some(l) = tags.photoshop_layers { + d.write_tag(Tag::Unknown(TAG_IMAGE_SOURCE_DATA), Undefined(l))?; + } + im.write_data(data) +} + +/// A Lab image as CIELab stores it: a* and b* signed, 8 or 16 bits (CIELab has no float form: +/// 32-bit Lab is written with 16 bits). Extra samples follow the new sample type. +fn lab_file(img: &Image) -> Image { + let sample = if img.sample_type() == SampleType::U8 { SampleType::U8 } else { SampleType::U16 }; + // `convert` brings the extra samples to the new sample type with the image. + let mut file = img.convert(img.layout(), sample); + let channels = file.layout().channels(); + convert_ab(file.data_mut(), channels, sample, true, true); + file +} + +/// Writes the image with the colour type matching its layout and sample type. +fn write_image(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + use ChannelLayout as L; + use SampleType as S; + if !img.meta.extra_samples.is_empty() { + return write_with_extras::(enc, img, tags); + } + let (w, h) = img.dimensions(); + let u16s = || img.to_u16_samples().unwrap_or_default(); + let f32s = || img.to_f32_samples().unwrap_or_default(); + let d8 = img.data(); + match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Gray, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Gray, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::GrayA, S::U8) => write_one::(enc, w, h, d8, tags), + (L::GrayA, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::GrayA, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Rgb, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Rgb, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Rgb, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Rgba, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Rgba, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Rgba, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Cmyk, S::U8) => write_one::(enc, w, h, d8, tags), + (L::Cmyk, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::Cmyk, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::CmykA, S::U8) => write_one::(enc, w, h, d8, tags), + (L::CmykA, S::U16) => write_one::(enc, w, h, &u16s(), tags), + (L::CmykA, S::F32) => write_one::(enc, w, h, &f32s(), tags), + (L::Lab | L::LabA, _) => { + let file = lab_file(img); + let u16s = || file.to_u16_samples().unwrap_or_default(); + match (file.layout(), file.sample_type()) { + (L::Lab, S::U8) => write_one::(enc, w, h, file.data(), tags), + (L::Lab, _) => write_one::(enc, w, h, &u16s(), tags), + (_, S::U8) => write_one::(enc, w, h, file.data(), tags), + _ => write_one::(enc, w, h, &u16s(), tags), + } + } + (l, s) => Err(io::Error::new(io::ErrorKind::InvalidInput, format!("unsupported {l:?} {s:?}")).into()), + } +} + +/// Writes the image pyramid after the full image: each level half the size of the one before +/// (rounded up), colour and alpha only, until the longer side is at most +/// [`crate::TIFF_PYRAMID_MIN`]. An image already that small gets no levels. +fn write_pyramid(enc: &mut TiffEncoder<&mut Cursor>, K>, img: &Image, tags: &TagSet<'_>) -> tiff::TiffResult<()> { + let mut level: Option = None; + loop { + let prev = level.as_ref().unwrap_or(img); + let (w, h) = prev.dimensions(); + if w.max(h) <= crate::TIFF_PYRAMID_MIN { + return Ok(()); + } + let next = half_size(prev).map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e.to_string()))?; + write_image::(enc, &next, tags)?; + level = Some(next); + } +} + +/// `img` at half its size, rounded up, by averaging each 2×2 block (an odd last row or column +/// averages what it has). Colour is weighted by alpha, so transparent pixels don't darken +/// the edges of what they surround. Extra samples are not carried over. Rows are worked in +/// parallel on native targets. +pub(super) fn half_size(img: &Image) -> Result { + let (w, h) = img.dimensions(); + let (nw, nh) = (w.div_ceil(2).max(1), h.div_ceil(2).max(1)); + let layout = img.layout(); + let sample = img.sample_type(); + let ch = layout.channels(); + let bps = sample.bytes(); + let src_row = (w as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "image row size overflows"))?; + let out_row = (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; + let len = out_row.checked_mul(nh as usize).ok_or_else(|| CodecError::encode(F, "pyramid level size overflows"))?; + if img.data().len() < src_row.saturating_mul(h as usize) { + return Err(CodecError::encode(F, "image buffer is shorter than its dimensions")); + } + let mut out = super::bands::zeroed(len)?; + let level = Level { data: img.data(), w: w as usize, h: h as usize, ch, sample, src_row, alpha: layout.has_alpha() }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(out_row.max(1)).enumerate().for_each(|(y, row)| level.row(y, row)); + } + #[cfg(target_arch = "wasm32")] + out.chunks_mut(out_row.max(1)).enumerate().for_each(|(y, row)| level.row(y, row)); + let mut img2 = Image::from_raw(nw, nh, layout, sample, out)?; + img2.icc = img.icc.clone(); + Ok(img2) +} + +/// The source of one [`half_size`] level. +struct Level<'a> { + data: &'a [u8], + w: usize, + h: usize, + ch: usize, + sample: SampleType, + src_row: usize, + /// The last channel is alpha. + alpha: bool, +} + +impl Level<'_> { + /// Source row `y` as numbers (empty past the bottom). + fn samples(&self, y: usize) -> Vec { + let Some(row) = (y < self.h).then(|| self.data.get(y * self.src_row..(y + 1) * self.src_row)).flatten() else { return Vec::new() }; + match self.sample { + SampleType::U8 => row.iter().map(|&v| f64::from(v)).collect(), + SampleType::U16 => row.as_chunks::<2>().0.iter().map(|b| f64::from(u16::from_ne_bytes(*b))).collect(), + SampleType::F32 => row.as_chunks::<4>().0.iter().map(|b| f64::from(f32::from_ne_bytes(*b))).collect(), + SampleType::F16 => row.as_chunks::<2>().0.iter().map(|b| f64::from(half::f16::from_bits(u16::from_ne_bytes(*b)).to_f32())).collect(), + } + } + + /// Writes output row `y` into `out`. + fn row(&self, y: usize, out: &mut [u8]) { + let rows = [self.samples(y * 2), self.samples(y * 2 + 1)]; + let ch = self.ch; + let bps = self.sample.bytes(); + let mut sums = vec![0f64; ch]; + for (x, px) in out.chunks_exact_mut(ch * bps).enumerate() { + sums.iter_mut().for_each(|s| *s = 0.0); + let (mut n, mut weight) = (0f64, 0f64); + for row in rows.iter().filter(|r| !r.is_empty()) { + for sx in [x * 2, x * 2 + 1].into_iter().filter(|&sx| sx < self.w) { + let Some(p) = row.get(sx * ch..(sx + 1) * ch) else { continue }; + let a = if self.alpha { p.last().copied().unwrap_or(1.0) } else { 1.0 }; + for (c, (sum, v)) in sums.iter_mut().zip(p).enumerate() { + *sum += if self.alpha && c + 1 == ch { *v } else { v * a }; + } + n += 1.0; + weight += a; + } + } + for (c, (sum, dst)) in sums.iter().zip(px.chunks_exact_mut(bps)).enumerate() { + let v = if self.alpha && c + 1 == ch { + sum / n.max(1.0) + } else if weight > 0.0 { + sum / weight + } else { + 0.0 + }; + match self.sample { + SampleType::U8 => dst.copy_from_slice(&[v.round().clamp(0.0, 255.0) as u8]), + SampleType::U16 => dst.copy_from_slice(&(v.round().clamp(0.0, 65535.0) as u16).to_ne_bytes()), + SampleType::F32 => dst.copy_from_slice(&(v as f32).to_ne_bytes()), + SampleType::F16 => dst.copy_from_slice(&half::f16::from_f32(v as f32).to_bits().to_ne_bytes()), + } + } + } + } +} + +/// Classic TIFF offsets are 32-bit: a file that may grow past this is written as BigTIFF. +const CLASSIC_LIMIT: u64 = u32::MAX as u64; + +/// An upper estimate of the file size: the uncompressed samples plus every tag payload plus +/// room for the directory and strip tables. +fn estimated_size(img: &Image, tags: &TagSet<'_>) -> u64 { + let payloads = [tags.icc, tags.xmp, tags.photoshop_resources, tags.photoshop_layers].iter().flatten().map(|p| p.len() as u64).sum::(); + let text = tags.text.iter().map(|(_, s)| s.len() as u64).sum::() + + tags.exif.iter().flat_map(|e| e.ifd0.iter().chain(&e.exif).chain(&e.gps)).map(|f| f.value.len() as u64 + 12).sum::(); + // Strip tables: one offset and one count per strip of about 1 MB. + let tables = (img.data().len() as u64 / (1 << 20) + 1) * 16; + let extras: u64 = img.meta.extra_samples.iter().map(|e| e.data.len() as u64).sum(); + (img.data().len() as u64).saturating_add(extras).saturating_add(payloads).saturating_add(text).saturating_add(tables).saturating_add(1 << 20) +} + +/// The byte length of one extra-sample plane: one sample per pixel of `img`, in its sample type. +/// `None` when the pixel count overflows a `usize` (no such image buffer can exist). +fn extra_plane_len(img: &Image) -> Option { + let (w, h) = img.dimensions(); + (w as usize).checked_mul(h as usize)?.checked_mul(img.sample_type().bytes()) +} + +/// Every extra plane must hold exactly one sample per pixel of `img`, in its sample type. +/// +/// The writer copies one sample per pixel out of each plane, so a plane of any other length +/// would be silently truncated (long) or read past its end (short). A sample-type conversion +/// reinterprets the bytes in the new width instead, so an unchecked plane would also change +/// meaning rather than fail. Refuse the image instead of writing either. +fn check_extra_planes(img: &Image) -> Result<(), CodecError> { + let (w, h) = img.dimensions(); + let sample = img.sample_type(); + let plane_len = extra_plane_len(img).ok_or_else(|| CodecError::encode(F, format!("the extra-sample size of a {w}x{h} image overflows")))?; + for (k, e) in img.meta.extra_samples.iter().enumerate() { + if e.data.len() != plane_len { + return Err(CodecError::encode( + F, + format!("extra sample {k} is {} bytes; a {w}x{h} {sample:?} image needs exactly {plane_len} (one sample per pixel)", e.data.len()), + )); + } + } + Ok(()) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + // Check the source planes before `converted`, which brings them to the plan's sample type + // with the image but deliberately leaves a wrong-sized plane alone: past that point a bad + // plane would be reinterpreted in the new width rather than rejected. + check_extra_planes(src)?; + // `converted` brings the extra samples to the plan's sample type with the image. + encode_ready(&src.converted(plan.layout, plan.sample), opts) +} + +fn encode_ready(img: &Image, opts: &EncodeOptions) -> Result, CodecError> { + let compression = match opts.tiff_compression { + TiffCompression::None => tiff::encoder::Compression::Uncompressed, + TiffCompression::Lzw => tiff::encoder::Compression::Lzw, + TiffCompression::Deflate => tiff::encoder::Compression::Deflate(tiff::encoder::DeflateLevel::Balanced), + TiffCompression::PackBits => tiff::encoder::Compression::Packbits, + }; + let predictor = if !img.sample_type().is_float() && matches!(opts.tiff_compression, TiffCompression::Lzw | TiffCompression::Deflate) { + tiff::encoder::Predictor::Horizontal + } else { + tiff::encoder::Predictor::None + }; + let text: Vec<(Tag, &str)> = if opts.embed_metadata { + img.meta + .text + .iter() + .filter_map(|(k, v)| TEXT_TAGS.iter().find(|(_, key)| key.eq_ignore_ascii_case(k)).map(|(t, _)| (*t, v.as_str()))) + .filter(|(_, v)| v.is_ascii() && !v.contains('\0')) + .collect() + } else { + Vec::new() + }; + // TIFF writes no Orientation tag (= 1): keep the XMP from contradicting it. + let xmp = if opts.embed_metadata { img.meta.xmp.as_deref().map(|x| crate::resolution::export_xmp(x, img.meta.dpi)) } else { None }; + if img.meta.extra_samples.len() > crate::MAX_EXTRA_SAMPLES { + return Err(CodecError::encode(F, format!("TIFF stores at most {} extra samples", crate::MAX_EXTRA_SAMPLES))); + } + // Validate again in the sample type the planes now hold: `Image::convert` takes a plane's + // bytes before it rebuilds them, so one it could not rebuild would arrive here empty. The Lab + // path's later re-type through `lab_file` converts a plane only when its length already + // matches the image, which this guarantees. + check_extra_planes(img)?; + let mut kinds = Vec::with_capacity(img.meta.extra_samples.len().saturating_add(1)); + if img.layout().has_alpha() { + kinds.push(2); + } + // TIFF 6.0 reserves ExtraSamples values above 2: those are written as 0 (unspecified). + for e in &img.meta.extra_samples { + kinds.push(if e.kind <= 2 { e.kind } else { 0 }); + } + let tags = TagSet { + icc: if opts.embed_icc { img.icc.as_deref() } else { None }, + exif: if opts.embed_metadata { img.meta.exif.as_deref().and_then(|e| tiff_exif::read(crate::orientation::tiff_body(e))) } else { None }, + xmp: xmp.as_deref().map(str::as_bytes), + dpi: if opts.embed_metadata { img.meta.dpi.filter(|d| d.0 > 0.0 && d.1 > 0.0) } else { None }, + text, + extra_samples: &kinds, + // Document content, not metadata: written whenever present. + photoshop_resources: img.meta.photoshop_resources.as_deref().filter(|r| !r.is_empty()), + photoshop_layers: img.meta.photoshop_layers.as_deref().filter(|l| !l.is_empty()), + reduced: false, + }; + // A pyramid level holds the colour and alpha only, with the profile so it shows the same. + let alpha_kind: &[u16] = if img.layout().has_alpha() { &[2] } else { &[] }; + let level_tags = TagSet { + icc: tags.icc, + exif: None, + xmp: None, + dpi: None, + text: Vec::new(), + extra_samples: alpha_kind, + photoshop_resources: None, + photoshop_layers: None, + reduced: true, + }; + + // BigTIFF when asked, or when the file could pass the 4 GiB a classic TIFF can address. A + // classic write that still runs out of 32-bit offsets (compression grew the data) is redone + // as BigTIFF. + let estimate = estimated_size(img, &tags); + // The pyramid adds at most a third of the image (1/4 + 1/16 + …). + let estimate = if opts.tiff_pyramid { estimate.saturating_add(img.data().len() as u64 / 3) } else { estimate }; + let write = |big: bool| -> tiff::TiffResult> { + // An uncompressed file is allocated once at its estimated size: grown as written, the + // buffer would double near the end and copy the file (#6607). Compressed output is + // usually a fraction of the estimate, so it grows as written instead: reserving the + // estimate would hold a second full-size buffer while encoding, which wasm32 never + // returns and Windows counts as committed memory. + let mut out = Vec::new(); + if matches!(opts.tiff_compression, TiffCompression::None) { + // Best effort: without the room the buffer grows as before. + let _ = out.try_reserve(usize::try_from(estimate).unwrap_or(usize::MAX)); + } + let mut cursor = Cursor::new(out); + if big { + let mut enc = TiffEncoder::new_big(&mut cursor)?.with_compression(compression).with_predictor(predictor); + write_image::(&mut enc, img, &tags)?; + if opts.tiff_pyramid { + write_pyramid::(&mut enc, img, &level_tags)?; + } + } else { + let mut enc = TiffEncoder::new(&mut cursor)?.with_compression(compression).with_predictor(predictor); + write_image::(&mut enc, img, &tags)?; + if opts.tiff_pyramid { + write_pyramid::(&mut enc, img, &level_tags)?; + } + } + // Growth (or an estimate that was too high) can leave spare capacity: give it back + // instead of holding it while the file is saved. + let mut out = cursor.into_inner(); + if out.capacity() - out.len() > out.len() / 8 { + out.shrink_to_fit(); + } + Ok(out) + }; + let big = opts.tiff_bigtiff || estimate > CLASSIC_LIMIT; + match write(big) { + Ok(v) => Ok(v), + Err(tiff::TiffError::IntSizeError) if !big => write(true).map_err(|e| CodecError::encode(F, e)), + Err(e) => Err(CodecError::encode(F, e)), + } +} diff --git a/crates/image-codecs/src/codecs/tiff/tags.rs b/crates/image-codecs/src/codecs/tiff/tags.rs new file mode 100644 index 000000000..a41b15413 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/tags.rs @@ -0,0 +1,99 @@ +use super::{Format, Tag}; + +pub(super) const F: Format = Format::Tiff; + +pub(super) const TAG_XMP: u16 = 700; + +/// Photoshop image resources (`8BIM` blocks). +pub(super) const TAG_PHOTOSHOP: u16 = 34377; + +/// Photoshop `ImageSourceData`: the layers of a layered TIFF. +pub(super) const TAG_IMAGE_SOURCE_DATA: u16 = 37724; + +/// `true` when [`encode`] writes little-endian (`II`) files: the `tiff` encoder writes the +/// running target's byte order, and every target PhotoCraft builds for is little-endian. +/// Byte-order-sensitive payloads (Photoshop layer data) must be produced to match. +pub fn writes_little_endian() -> bool { + cfg!(target_endian = "little") +} + +/// `(34377 image resources, 37724 image source data)` payloads, each absent when the tag is. +pub type PhotoshopTags<'a> = (Option<&'a [u8]>, Option<&'a [u8]>); + +/// The raw payloads of the Photoshop private tags in the first directory: `(34377 image +/// resources, 37724 image source data)`. Classic and BigTIFF, either byte order; only +/// header bytes and the two payloads are touched, and nothing is allocated. Anything malformed +/// reads as "absent". +pub fn photoshop_tags(b: &[u8]) -> PhotoshopTags<'_> { + fn walk(b: &[u8]) -> Option> { + let le = match b.get(0..2)? { + b"II" => true, + b"MM" => false, + _ => return None, + }; + let u16_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s = b.get(o..o.checked_add(2)?)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let u32_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s: [u8; 4] = b.get(o..o.checked_add(4)?)?.try_into().ok()?; + Some(if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + }; + let u64_at = |o: u64| -> Option { + let o = usize::try_from(o).ok()?; + let s: [u8; 8] = b.get(o..o.checked_add(8)?)?.try_into().ok()?; + Some(if le { u64::from_le_bytes(s) } else { u64::from_be_bytes(s) }) + }; + let (big, ifd) = match u16_at(2)? { + 42 => (false, u64::from(u32_at(4)?)), + 43 => (true, u64_at(8)?), + _ => return None, + }; + let n = if big { u64_at(ifd)? } else { u64::from(u16_at(ifd)?) }.min(4096); + let (entry, first, inline) = if big { (20u64, ifd.checked_add(8)?, 8u64) } else { (12, ifd.checked_add(2)?, 4) }; + let mut resources = None; + let mut layers = None; + for i in 0..n { + let e = first.checked_add(i.checked_mul(entry)?)?; + let tag = u16_at(e)?; + if tag != TAG_PHOTOSHOP && tag != TAG_IMAGE_SOURCE_DATA { + continue; + } + // BYTE, ASCII, SBYTE or UNDEFINED: one byte per element. + if !matches!(u16_at(e + 2)?, 1 | 2 | 6 | 7) { + continue; + } + let count = if big { u64_at(e + 4)? } else { u64::from(u32_at(e + 4)?) }; + let value_at = e + if big { 12 } else { 8 }; + let start = if count <= inline { + value_at + } else if big { + u64_at(value_at)? + } else { + u64::from(u32_at(value_at)?) + }; + let start = usize::try_from(start).ok()?; + let data = b.get(start..start.checked_add(usize::try_from(count).ok()?)?)?; + if tag == TAG_PHOTOSHOP { + resources = Some(data); + } else { + layers = Some(data); + } + } + Some((resources, layers)) + } + walk(b).unwrap_or((None, None)) +} + +/// ASCII tags mapped to `Metadata::text` keys. +pub(super) const TEXT_TAGS: &[(Tag, &str)] = &[ + (Tag::ImageDescription, "Description"), + (Tag::Make, "Make"), + (Tag::Model, "Model"), + (Tag::Software, "Software"), + (Tag::DateTime, "DateTime"), + (Tag::Artist, "Artist"), + (Tag::Copyright, "Copyright"), +]; diff --git a/crates/image-codecs/src/codecs/tiff/ycbcr.rs b/crates/image-codecs/src/codecs/tiff/ycbcr.rs new file mode 100644 index 000000000..2983a8050 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff/ycbcr.rs @@ -0,0 +1,222 @@ +//! YCbCr (PhotometricInterpretation 6) to RGB, after the `tiff` crate has decoded the samples. +//! +//! The crate decompresses JPEG data and upsamples subsampled chroma, but hands back the stored +//! Y, Cb and Cr values. This module turns them into RGB in place: ReferenceBlackWhite scales the +//! codes and YCbCrCoefficients gives the luma weights. JPEG-compressed data (compression 7) uses +//! the JPEG (JFIF) conversion, full range with the BT.601 weights, and ignores both tags, as +//! libtiff does when it asks the JPEG library for RGB. +//! +//! # Sources +//! +//! - TIFF Revision 6.0 (Final, June 3, 1992), Section 21 "YCbCr Images", pp. 89–92; the +//! YCbCrCoefficients field and its inverse equations are on pp. 90–91: +//! `R = Cr·(2 - 2·LumaRed) + Y`, `B = Cb·(2 - 2·LumaBlue) + Y`, +//! `G = (Y - LumaBlue·B - LumaRed·R) / LumaGreen`. +//! +//! - libtiff, `libtiff/tif_color.c`: `TIFFYCbCrToRGBInit` (tables) and `TIFFYCbCrtoRGB` (per pixel). +//! +//! +//! # Green: a compatibility choice +//! +//! Red and blue follow the spec. Green follows libtiff, which rewrites the spec's equation as +//! `G = Y - (LumaRed·(2 - 2·LumaRed) / LumaGreen)·Cr - (LumaBlue·(2 - 2·LumaBlue) / LumaGreen)·Cb`, +//! adding Y unscaled. The two agree only when the weights sum to 1. Every standard set does +//! (the spec's own examples, BT.601 and BT.709), but the spec only calls the weights "the +//! proportions of red, green, and blue respectively in luminance" and does not require the sum. +//! For other weights the spec's equation scales Y in green by `(1 - LumaRed - LumaBlue) / LumaGreen`, +//! which tints neutral grays. +//! +//! PhotoCraft deliberately follows libtiff here (decided in PR #10319, for issue #10316), so such +//! files open as they do in libtiff-based tools (ImageMagick, GIMP) and grays stay neutral. This is +//! a contributor choice, not a requirement of the spec; the test +//! `weights_that_do_not_sum_to_one_keep_grays_neutral` in `tests/it/tiff_ycbcr.rs` pins it. + +use super::{File, Ifd, SampleType}; + +const TAG_YCBCR_COEFFICIENTS: u16 = 529; +const TAG_REFERENCE_BLACK_WHITE: u16 = 532; + +/// BT.601 luma weights: the TIFF default for YCbCrCoefficients. +const BT601: [f64; 3] = [0.299, 0.587, 0.114]; + +/// How one file's YCbCr codes map to RGB. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(super) struct YCbCr { + /// Luma weights of red, green and blue. + luma: [f64; 3], + /// Black and white codes of Y, Cb and Cr. + ref_bw: [f64; 6], +} + +impl YCbCr { + /// The conversion for directory `dir`, whose samples have `max` as their largest code + /// (255 or 65535). Missing or unusable tags fall back to the TIFF defaults. + pub(super) fn of(f: &File<'_>, dir: &Ifd, compression: u64, max: f64) -> Self { + let defaults = Self::defaults(max); + if compression == 7 { + return defaults; + } + let luma = f + .tag_f64s(dir, TAG_YCBCR_COEFFICIENTS, 3) + .and_then(|v| <[f64; 3]>::try_from(v).ok()) + .filter(|l| l.iter().all(|c| c.is_finite() && *c > 0.0 && *c < 1.0)) + .unwrap_or(defaults.luma); + let ref_bw = f + .tag_f64s(dir, TAG_REFERENCE_BLACK_WHITE, 6) + .and_then(|v| <[f64; 6]>::try_from(v).ok()) + .filter(|r| r.iter().all(|v| v.is_finite()) && r.as_chunks::<2>().0.iter().all(|[black, white]| (white - black).abs() > f64::EPSILON)) + .unwrap_or(defaults.ref_bw); + YCbCr { luma, ref_bw } + } + + /// TIFF's defaults: BT.601 weights, full-range luma, chroma centred on the middle code. + fn defaults(max: f64) -> Self { + let mid = (max + 1.0) / 2.0; + YCbCr { luma: BT601, ref_bw: [0.0, max, mid, max, mid, max] } + } + + /// Converts interleaved Y, Cb, Cr pixels of `channels` samples (the first three are the + /// colour) to R, G, B in place. Other sample types are left as they are. + pub(super) fn convert(self, data: &mut [u8], channels: usize, sample: SampleType) { + match sample { + SampleType::U8 => { + let t = Tables::new(&self, 255); + for_each_pixel(data, channels, |px| { + if let [y, cb, cr, ..] = px { + let [r, g, b] = t.rgb(usize::from(*y), usize::from(*cb), usize::from(*cr)); + (*y, *cb, *cr) = (r as u8, g as u8, b as u8); + } + }); + } + SampleType::U16 => { + let t = Tables::new(&self, 65535); + for_each_pixel(data, channels * 2, |px| { + if let [y0, y1, b0, b1, r0, r1, ..] = px { + let get = |a: u8, b: u8| usize::from(u16::from_ne_bytes([a, b])); + let [r, g, b] = t.rgb(get(*y0, *y1), get(*b0, *b1), get(*r0, *r1)); + [*y0, *y1] = (r as u16).to_ne_bytes(); + [*b0, *b1] = (g as u16).to_ne_bytes(); + [*r0, *r1] = (b as u16).to_ne_bytes(); + } + }); + } + _ => {} + } + } +} + +/// Runs `f` on every `stride`-byte pixel, in parallel on native targets. +fn for_each_pixel(data: &mut [u8], stride: usize, f: impl Fn(&mut [u8]) + Sync + Send) { + if stride == 0 { + return; + } + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + // Bands of whole pixels, so each task has a useful amount of work. + let band = stride.saturating_mul(1 << 14); + data.par_chunks_mut(band).for_each(|b| b.chunks_exact_mut(stride).for_each(&f)); + } + #[cfg(target_arch = "wasm32")] + data.chunks_exact_mut(stride).for_each(f); +} + +/// Per-code contributions, so a pixel costs a few additions. +struct Tables { + max: f64, + /// Scaled luma of each Y code. + y: Vec, + /// Red and green contributions of each Cr code. + cr_r: Vec, + cr_g: Vec, + /// Green and blue contributions of each Cb code. + cb_g: Vec, + cb_b: Vec, +} + +impl Tables { + fn new(c: &YCbCr, max: u32) -> Self { + let m = f64::from(max); + let [lr, lg, lb] = c.luma; + let [y_black, y_white, cb_black, cb_white, cr_black, cr_white] = c.ref_bw; + // TIFF 6.0: chroma codes span ±(max + 1) / 2 - 1 around the black code; with the + // default ReferenceBlackWhite that is the code minus the middle code. + let half = (m + 1.0) / 2.0 - 1.0; + let codes = || (0..=max).map(f64::from); + let y = codes().map(|v| (v - y_black) * m / (y_white - y_black)).collect(); + let cb: Vec = codes().map(|v| (v - cb_black) * half / (cb_white - cb_black)).collect(); + let cr: Vec = codes().map(|v| (v - cr_black) * half / (cr_white - cr_black)).collect(); + // R = Y + (2 - 2·Lr)·Cr and B = Y + (2 - 2·Lb)·Cb (TIFF 6.0 p. 91); green as libtiff: + // G = Y - (Lr·(2 - 2·Lr) / Lg)·Cr - (Lb·(2 - 2·Lb) / Lg)·Cb (see the module notes). + let (kr, kb) = (2.0 - 2.0 * lr, 2.0 - 2.0 * lb); + Tables { + max: m, + y, + cr_r: cr.iter().map(|v| kr * v).collect(), + cr_g: cr.iter().map(|v| -lr * kr * v / lg).collect(), + cb_g: cb.iter().map(|v| -lb * kb * v / lg).collect(), + cb_b: cb.iter().map(|v| kb * v).collect(), + } + } + + /// R, G and B of one pixel, rounded and clamped to the code range. + fn rgb(&self, y: usize, cb: usize, cr: usize) -> [u32; 3] { + let at = |t: &[f64], i: usize| t.get(i).copied().unwrap_or(0.0); + let yv = at(&self.y, y); + // Green adds Y unscaled, as libtiff does (see "Green: a compatibility choice" above). + let r = yv + at(&self.cr_r, cr); + let g = yv + at(&self.cr_g, cr) + at(&self.cb_g, cb); + let b = yv + at(&self.cb_b, cb); + [r, g, b].map(|v| v.round().clamp(0.0, self.max) as u32) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn convert(c: &YCbCr, px: [u8; 3]) -> [u8; 3] { + let mut d = px.to_vec(); + c.convert(&mut d, 3, SampleType::U8); + [d[0], d[1], d[2]] + } + + #[test] + fn jfif_conversion_matches_known_colours() { + let c = YCbCr::defaults(255.0); + assert_eq!(convert(&c, [0, 128, 128]), [0, 0, 0]); + assert_eq!(convert(&c, [255, 128, 128]), [255, 255, 255]); + assert_eq!(convert(&c, [128, 128, 128]), [128, 128, 128]); + // Pure red, green and blue as JFIF encodes them. + assert_eq!(convert(&c, [76, 85, 255]), [254, 0, 0]); + assert_eq!(convert(&c, [150, 44, 21]), [0, 255, 1]); + assert_eq!(convert(&c, [29, 255, 107]), [0, 0, 254]); + } + + #[test] + fn sixteen_bit_and_extra_samples() { + let c = YCbCr::defaults(65535.0); + let mut d: Vec = [65535u16, 32768, 32768, 1234].iter().flat_map(|v| v.to_ne_bytes()).collect(); + c.convert(&mut d, 4, SampleType::U16); + let v: Vec = d.as_chunks::<2>().0.iter().map(|b| u16::from_ne_bytes(*b)).collect(); + assert_eq!(v, [65535, 65535, 65535, 1234]); + } + + #[test] + fn studio_range_reference_black_white() { + // Y 16–235, chroma 128 ± 112 (ITU-R BT.601 studio range). + let c = YCbCr { luma: BT601, ref_bw: [16.0, 235.0, 128.0, 240.0, 128.0, 240.0] }; + assert_eq!(convert(&c, [16, 128, 128]), [0, 0, 0]); + assert_eq!(convert(&c, [235, 128, 128]), [255, 255, 255]); + } + + #[test] + fn odd_buffers_do_not_panic() { + let c = YCbCr::defaults(255.0); + let mut d = vec![1u8, 2]; + c.convert(&mut d, 3, SampleType::U8); + c.convert(&mut d, 0, SampleType::U8); + c.convert(&mut d, 0, SampleType::U16); + assert_eq!(d, [1, 2]); + } +} diff --git a/crates/image-codecs/src/codecs/tiff_exif.rs b/crates/image-codecs/src/codecs/tiff_exif.rs new file mode 100644 index 000000000..2e6885b6e --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff_exif.rs @@ -0,0 +1,238 @@ +//! EXIF in a TIFF file: the Exif (34665) and GPS (34853) directories that IFD0 points to, plus +//! IFD0's descriptive tags (make, model, software, artist, …), the way cameras and Photoshop +//! write them. +//! +//! An EXIF block (`Metadata::exif`) is itself a small TIFF stream, so both directions go through +//! the same reader ([`super::tiff_ifd::File`]): reading a TIFF builds a block from the file's own +//! directories, writing one copies the block's directories into the file. Values are copied as +//! bytes (re-ordered when the byte orders differ), so tags PhotoCraft doesn't know survive. +//! Classic TIFF only on the reading side: an EXIF block has 4-byte offsets. What is copied is +//! bounded by the stream's own size and a fixed cap, because entries of a hostile file can +//! overlap (see [`read`]). + +use tiff::encoder::{DirectoryEncoder, TiffEncoder, TiffKind}; +use tiff::tags::{Tag, Type}; +use tiff::{Directory, TiffResult}; + +use super::tiff_ifd::{File, Ifd}; + +/// IFD0's pointer to the Exif directory. +pub(crate) const TAG_EXIF_IFD: u16 = 34665; +/// IFD0's pointer to the GPS directory. +pub(crate) const TAG_GPS_IFD: u16 = 34853; +/// The Exif directory's PixelXDimension and PixelYDimension: the size of the image the +/// directory describes. +const TAG_PIXEL_X_DIMENSION: u16 = 40962; +const TAG_PIXEL_Y_DIMENSION: u16 = 40963; +/// The Exif directory's pointer to the Interoperability directory (not carried: it holds only +/// the interoperability index, and a copied pointer would point nowhere). +const TAG_INTEROP_IFD: u16 = 40965; +/// IFD0 tags an EXIF block carries next to its directories: ImageDescription, Make, Model, +/// Software, DateTime, Artist, Copyright. Structure, resolution and orientation belong to the +/// image and are written from it. +const IFD0_TAGS: [u16; 7] = [270, 271, 272, 305, 306, 315, 33432]; +/// At most this many entries are copied from one directory. +const MAX_FIELDS: usize = 1024; +/// At most this many value bytes are copied from one stream, over all its directories. Real EXIF +/// is far smaller (the largest Nikon maker note seen is under 1 MB); the cap leaves room for +/// that and for a few such notes. A stream never yields more than its own length either (see +/// [`read`]). +const MAX_COPIED_BYTES: usize = 4 * 1024 * 1024; + +/// One entry with its value bytes in the byte order of the stream it came from. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Field { + pub tag: u16, + pub ty: u16, + pub count: u32, + pub value: Vec, +} + +/// The EXIF parts of a TIFF stream, in its byte order (`le`). +#[derive(Clone, Debug, Default, PartialEq)] +pub(crate) struct ExifDirs { + pub le: bool, + pub ifd0: Vec, + pub exif: Vec, + pub gps: Vec, +} + +impl ExifDirs { + /// Whether there is an Exif or GPS directory (IFD0's descriptive tags alone are not EXIF). + pub fn has_directories(&self) -> bool { + !self.exif.is_empty() || !self.gps.is_empty() + } +} + +/// The entries of `ifd` that `keep` accepts, with their values copied. An entry's value only has +/// to lie inside the file, so entries may overlap: `budget` is the number of value bytes that may +/// still be copied (shared by all the directories of one stream), and an entry whose value does +/// not fit in what is left is skipped, so later, smaller entries can still be kept. +fn fields(f: &File<'_>, ifd: &Ifd, budget: &mut usize, keep: impl Fn(u16) -> bool) -> Vec { + let mut out = Vec::new(); + for e in ifd.entries.iter().filter(|e| keep(e.tag) && !matches!(e.tag, TAG_EXIF_IFD | TAG_GPS_IFD | TAG_INTEROP_IFD)) { + if out.len() >= MAX_FIELDS { + break; + } + let (Ok(count), Some(value)) = (u32::try_from(e.count), f.data(e)) else { continue }; + let Some(left) = budget.checked_sub(value.len()) else { continue }; + *budget = left; + out.push(Field { tag: e.tag, ty: e.ty, count, value: value.to_vec() }); + } + out +} + +fn sub(f: &File<'_>, ifd: &Ifd, tag: u16) -> Option { + let at = f.tag_uint(ifd, tag)?; + (at != ifd.at).then(|| f.ifd(at)).flatten() +} + +/// The EXIF parts of the classic TIFF stream `b` (a TIFF file, or an EXIF block without its +/// `Exif\0\0` prefix): IFD0's descriptive tags and the Exif and GPS directories. +/// +/// The values copied add up to at most [`MAX_COPIED_BYTES`] and to at most `b.len()`: the values +/// of a well-formed stream are distinct bytes of it, while a hostile one can have many entries +/// point at the same bytes and so ask for far more than its size. Entries that do not fit in what +/// is left are left out (the small descriptive directories come first, so a huge maker note +/// cannot crowd out the GPS position). +pub(crate) fn read(b: &[u8]) -> Option { + read_file(File::parse(b)?) +} + +/// The EXIF parts of a camera raw built on TIFF (NEF, ARW, CR2, DNG, Panasonic RW2), as +/// [`read`] gives them, without the Exif directory's PixelXDimension and PixelYDimension: in a +/// raw they give the sensor's or the preview's size, not the developed image's. +pub(crate) fn read_raw(b: &[u8]) -> Option { + let mut dirs = read_file(File::parse_raw(b)?)?; + dirs.exif.retain(|f| !matches!(f.tag, TAG_PIXEL_X_DIMENSION | TAG_PIXEL_Y_DIMENSION)); + Some(dirs) +} + +fn read_file(f: File<'_>) -> Option { + let b = f.b; + if f.big { + return None; + } + let ifd0 = f.ifd(f.first_ifd)?; + let mut budget = MAX_COPIED_BYTES.min(b.len()); + let ifd0_fields = fields(&f, &ifd0, &mut budget, |t| IFD0_TAGS.contains(&t)); + let mut dir = |tag: u16| sub(&f, &ifd0, tag).map(|d| fields(&f, &d, &mut budget, |_| true)).unwrap_or_default(); + let gps = dir(TAG_GPS_IFD); + let exif = dir(TAG_EXIF_IFD); + Some(ExifDirs { le: f.le, ifd0: ifd0_fields, exif, gps }) +} + +/// Bytes per element whose order a byte-order change reverses: a RATIONAL is two LONGs. +fn element(ty: u16) -> usize { + match ty { + 3 | 8 => 2, + 4 | 5 | 9 | 10 | 11 | 13 => 4, + 12 | 16 | 17 | 18 => 8, + _ => 1, + } +} + +/// `value` of type `ty` in byte order `to_le`, from `from_le`. +pub(crate) fn reorder(ty: u16, value: &[u8], from_le: bool, to_le: bool) -> Vec { + let n = element(ty); + if from_le == to_le || n == 1 { + return value.to_vec(); + } + value.chunks(n).flat_map(|c| c.iter().rev().copied()).collect() +} + +/// An EXIF block: a classic TIFF stream in `dirs.le` order whose IFD0 holds `dirs.ifd0` and +/// points to the Exif and GPS directories. +pub(crate) fn block(dirs: &ExifDirs) -> Vec { + let le = dirs.le; + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let u32b = |v: u32| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + let mut ifd0: Vec = dirs.ifd0.clone(); + // Placeholders for the pointers; their offsets are known once the layout is. + let pointer = |tag: u16| Field { tag, ty: 4, count: 1, value: vec![0; 4] }; + if !dirs.exif.is_empty() { + ifd0.push(pointer(TAG_EXIF_IFD)); + } + if !dirs.gps.is_empty() { + ifd0.push(pointer(TAG_GPS_IFD)); + } + // Layout: header, then each directory followed by its out-of-line values. + let size = |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); + let at0 = 8; + let at_exif = at0 + size(&ifd0); + let at_gps = at_exif + if dirs.exif.is_empty() { 0 } else { size(&dirs.exif) }; + for f in &mut ifd0 { + match f.tag { + TAG_EXIF_IFD => f.value = u32b(at_exif as u32).to_vec(), + TAG_GPS_IFD => f.value = u32b(at_gps as u32).to_vec(), + _ => {} + } + } + let mut out = Vec::new(); + out.extend_from_slice(if le { b"II" } else { b"MM" }); + out.extend_from_slice(&u16b(42)); + out.extend_from_slice(&u32b(at0 as u32)); + let write_dir = |out: &mut Vec, fs: &[Field]| { + let mut fs = fs.to_vec(); + fs.sort_by_key(|f| f.tag); + let start = out.len(); + let mut extra = start + 2 + 12 * fs.len() + 4; + let mut values = Vec::new(); + out.extend_from_slice(&u16b(fs.len() as u16)); + for f in &fs { + out.extend_from_slice(&u16b(f.tag)); + out.extend_from_slice(&u16b(f.ty)); + out.extend_from_slice(&u32b(f.count)); + if f.value.len() <= 4 { + let mut inline = f.value.clone(); + inline.resize(4, 0); + out.extend_from_slice(&inline); + } else { + out.extend_from_slice(&u32b(extra as u32)); + values.extend_from_slice(&f.value); + if f.value.len() % 2 == 1 { + values.push(0); + } + extra += f.value.len().next_multiple_of(2); + } + } + out.extend_from_slice(&u32b(0)); + out.extend_from_slice(&values); + }; + write_dir(&mut out, &ifd0); + if !dirs.exif.is_empty() { + write_dir(&mut out, &dirs.exif); + } + if !dirs.gps.is_empty() { + write_dir(&mut out, &dirs.gps); + } + out +} + +/// Writes `fields` (in byte order `from_le`) into a directory of `enc` that is not part of the +/// page chain, and returns its offset for the pointer tag. +pub(crate) fn write_directory( + enc: &mut TiffEncoder, + fields: &[Field], + from_le: bool, +) -> TiffResult { + let mut sub = enc.extra_directory()?; + write_fields(&mut sub, fields, from_le)?; + Ok(sub.finish_with_offsets()?.offset) +} + +/// Adds `fields` (in byte order `from_le`) to directory `d`, in the file's (native) byte order. +pub(crate) fn write_fields( + d: &mut DirectoryEncoder<'_, W, K>, + fields: &[Field], + from_le: bool, +) -> TiffResult<()> { + let mut dir = Directory::empty(); + for f in fields { + let Some(ty) = Type::from_u16(f.ty) else { continue }; + let entry = d.write_entry_bytes(ty, &reorder(f.ty, &f.value, from_le, cfg!(target_endian = "little")))?; + dir.extend([(Tag::from_u16_exhaustive(f.tag), entry)]); + } + d.extend_from(&dir); + Ok(()) +} diff --git a/crates/image-codecs/src/codecs/tiff_ifd.rs b/crates/image-codecs/src/codecs/tiff_ifd.rs new file mode 100644 index 000000000..8fac8c6b4 --- /dev/null +++ b/crates/image-codecs/src/codecs/tiff_ifd.rs @@ -0,0 +1,502 @@ +//! A bounds-checked reader for the TIFF directory structure: classic TIFF (version 42, 4-byte +//! offsets) and BigTIFF (version 43, 8-byte offsets and the LONG8 / SLONG8 / IFD8 types), in +//! either byte order. +//! +//! Every offset and count comes from the file and is untrusted: reads go through `get` with +//! checked arithmetic, directory tables must lie wholly inside the file, the IFD chain and the +//! SubIFD trees (tag 330) are walked with a seen-set, a depth limit and a cap on the number of +//! directories, and nothing is allocated in proportion to a declared count before the bytes +//! behind it are known to exist. +//! +//! This module lists the pages of a file ([`TiffInfo`]) and reads the metadata of a directory +//! without decoding pixels; `tiff.rs` decodes the pixels of one directory. + +use std::collections::HashSet; + +use crate::error::CodecError; +use crate::format::Format; + +/// At most this many directories are listed (main chain and SubIFDs together). +pub(crate) const MAX_IFDS: usize = 10_000; +/// Entries per directory: the classic format's own limit (a `u16` count), applied to BigTIFF too. +const MAX_ENTRIES: u64 = u16::MAX as u64; +/// SubIFD nesting depth that is followed (real files use one level). +const MAX_SUB_DEPTH: usize = 4; +/// SubIFD offsets followed per directory. +const MAX_SUBIFDS: u64 = 256; + +pub(crate) const TAG_NEW_SUBFILE_TYPE: u16 = 254; +pub(crate) const TAG_SUBFILE_TYPE: u16 = 255; +pub(crate) const TAG_WIDTH: u16 = 256; +pub(crate) const TAG_HEIGHT: u16 = 257; +pub(crate) const TAG_BITS: u16 = 258; +pub(crate) const TAG_COMPRESSION: u16 = 259; +pub(crate) const TAG_PHOTOMETRIC: u16 = 262; +pub(crate) const TAG_ORIENTATION: u16 = 274; +pub(crate) const TAG_SAMPLES: u16 = 277; +pub(crate) const TAG_ROWS_PER_STRIP: u16 = 278; +pub(crate) const TAG_PLANAR: u16 = 284; +pub(crate) const TAG_COLOR_MAP: u16 = 320; +pub(crate) const TAG_TILE_WIDTH: u16 = 322; +pub(crate) const TAG_TILE_LENGTH: u16 = 323; +pub(crate) const TAG_TILE_OFFSETS: u16 = 324; +pub(crate) const TAG_SUB_IFDS: u16 = 330; +pub(crate) const TAG_EXTRA_SAMPLES: u16 = 338; +pub(crate) const TAG_SAMPLE_FORMAT: u16 = 339; + +/// Tags the `tiff` crate reads to set up decoding. Their values must lie inside the file, so a +/// hostile count can neither make it allocate nor read past the end. +pub(crate) const STRUCTURAL_TAGS: &[u16] = &[256, 257, 258, 259, 262, 273, 277, 278, 279, 284, 317, 322, 323, 324, 325, 338, 339, 347, 530]; + +/// Bytes per value of a TIFF field type; `None` for unknown types (whose entries are skipped). +fn type_size(ty: u16) -> Option { + Some(match ty { + 1 | 2 | 6 | 7 => 1, // BYTE, ASCII, SBYTE, UNDEFINED + 3 | 8 => 2, // SHORT, SSHORT + 4 | 9 | 11 | 13 => 4, // LONG, SLONG, FLOAT, IFD + 5 | 10 | 12 | 16 | 17 | 18 => 8, // RATIONAL, SRATIONAL, DOUBLE, LONG8, SLONG8, IFD8 + _ => return None, + }) +} + +/// One directory entry, located but not read. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Entry { + pub tag: u16, + pub ty: u16, + pub count: u64, + /// Position of the value: inside the entry when it fits, else the stored offset. + pub value_at: u64, +} + +/// One directory: its entries in file order and the next-IFD offset (0 at the end of a chain). +#[derive(Clone, Debug)] +pub(crate) struct Ifd { + pub at: u64, + pub entries: Vec, + pub next: u64, + /// Position of the next-IFD pointer field. + pub next_at: u64, +} + +impl Ifd { + /// The first entry with this tag. + pub fn get(&self, tag: u16) -> Option<&Entry> { + self.entries.iter().find(|e| e.tag == tag) + } +} + +/// A TIFF or BigTIFF file's header and bytes. +#[derive(Clone, Copy)] +pub(crate) struct File<'a> { + pub b: &'a [u8], + pub le: bool, + pub big: bool, + pub first_ifd: u64, +} + +impl<'a> File<'a> { + /// The header, or `None` when this is not a (Big)TIFF. + pub fn parse(b: &'a [u8]) -> Option { + let le = match b.get(0..2)? { + b"II" => true, + b"MM" => false, + _ => return None, + }; + let mut f = File { b, le, big: false, first_ifd: 0 }; + match f.u16_at(2)? { + 42 => f.first_ifd = u64::from(f.u32_at(4)?), + 43 => { + // Bytesize of offsets (always 8) and a reserved zero. + if f.u16_at(4)? != 8 || f.u16_at(6)? != 0 { + return None; + } + f.big = true; + f.first_ifd = f.u64_at(8)?; + } + _ => return None, + } + Some(f) + } + + /// [`parse`](Self::parse), also taking a Panasonic RW2 raw, whose classic little-endian TIFF + /// header has 0x55 (`IIU\0`) in place of 42. + pub fn parse_raw(b: &'a [u8]) -> Option { + if let Some(f) = Self::parse(b) { + return Some(f); + } + let mut f = File { b, le: true, big: false, first_ifd: 0 }; + if b.get(0..2)? != b"II" || f.u16_at(2)? != 0x55 { + return None; + } + f.first_ifd = u64::from(f.u32_at(4)?); + Some(f) + } + + /// Length of the header; no directory can start inside it. + pub fn header_len(&self) -> u64 { + if self.big { 16 } else { 8 } + } + + fn slice(&self, at: u64, len: u64) -> Option<&'a [u8]> { + let at = usize::try_from(at).ok()?; + let len = usize::try_from(len).ok()?; + self.b.get(at..at.checked_add(len)?) + } + + pub fn u16_at(&self, at: u64) -> Option { + let s: [u8; 2] = self.slice(at, 2)?.try_into().ok()?; + Some(if self.le { u16::from_le_bytes(s) } else { u16::from_be_bytes(s) }) + } + + pub fn u32_at(&self, at: u64) -> Option { + let s: [u8; 4] = self.slice(at, 4)?.try_into().ok()?; + Some(if self.le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) }) + } + + pub fn u64_at(&self, at: u64) -> Option { + let s: [u8; 8] = self.slice(at, 8)?.try_into().ok()?; + Some(if self.le { u64::from_le_bytes(s) } else { u64::from_be_bytes(s) }) + } + + /// The declared entry count of the directory at `at`, before its table is parsed. + pub fn entry_count(&self, at: u64) -> Option { + if at < self.header_len() { + return None; + } + if self.big { self.u64_at(at) } else { self.u16_at(at).map(u64::from) } + } + + /// The directory at `at`: its whole table, up to and including the next-IFD pointer, must + /// be inside the file and after the header. + pub fn ifd(&self, at: u64) -> Option { + if at < self.header_len() { + return None; + } + let (n, first, entry, inline) = + if self.big { (self.u64_at(at)?, at.checked_add(8)?, 20u64, 8u64) } else { (u64::from(self.u16_at(at)?), at.checked_add(2)?, 12, 4) }; + if n > MAX_ENTRIES { + return None; + } + let next_at = first.checked_add(n.checked_mul(entry)?)?; + let next = if self.big { self.u64_at(next_at)? } else { u64::from(self.u32_at(next_at)?) }; + let mut entries = Vec::with_capacity(usize::try_from(n).ok()?); + for i in 0..n { + let e = first.checked_add(i.checked_mul(entry)?)?; + let tag = self.u16_at(e)?; + let ty = self.u16_at(e.checked_add(2)?)?; + let count = if self.big { self.u64_at(e.checked_add(4)?)? } else { u64::from(self.u32_at(e.checked_add(4)?)?) }; + let field = e.checked_add(if self.big { 12 } else { 8 })?; + // Unknown types are skipped, as the specification says. + let Some(size) = type_size(ty) else { + // Keep an unreadable child declaration visible to the completeness check. + if tag == TAG_SUB_IFDS { + entries.push(Entry { tag, ty, count, value_at: field }); + } + continue; + }; + let fits = count.checked_mul(size).is_some_and(|len| len <= inline); + let value_at = if fits { + field + } else if self.big { + self.u64_at(field)? + } else { + u64::from(self.u32_at(field)?) + }; + entries.push(Entry { tag, ty, count, value_at }); + } + Some(Ifd { at, entries, next, next_at }) + } + + /// The value bytes of an entry, when they are all inside the file. + pub fn data(&self, e: &Entry) -> Option<&'a [u8]> { + self.slice(e.value_at, e.count.checked_mul(type_size(e.ty)?)?) + } + + /// Value `i` of an unsigned integer entry (BYTE, SHORT, LONG, LONG8, IFD, IFD8). + pub fn uint(&self, e: &Entry, i: u64) -> Option { + if i >= e.count { + return None; + } + let size = type_size(e.ty)?; + let at = e.value_at.checked_add(i.checked_mul(size)?)?; + match e.ty { + 1 => self.slice(at, 1)?.first().map(|&v| u64::from(v)), + 3 => self.u16_at(at).map(u64::from), + 4 | 13 => self.u32_at(at).map(u64::from), + 16 | 18 => self.u64_at(at), + _ => None, + } + } + + /// The first value of an unsigned integer tag. + pub fn tag_uint(&self, ifd: &Ifd, tag: u16) -> Option { + self.uint(ifd.get(tag)?, 0) + } + + /// Up to `max` values of an unsigned integer tag (`None` if any of them is unreadable). + pub fn tag_uints(&self, ifd: &Ifd, tag: u16, max: u64) -> Option> { + let e = ifd.get(tag)?; + (0..e.count.min(max)).map(|i| self.uint(e, i)).collect() + } + + /// The first value of a numeric tag as a float: RATIONAL, FLOAT, DOUBLE or an integer. + pub fn tag_f64(&self, ifd: &Ifd, tag: u16) -> Option { + self.float(ifd.get(tag)?, 0) + } + + /// The values of a numeric tag as floats, when it holds exactly `n` (`None` for any other + /// count or an unreadable value). + pub fn tag_f64s(&self, ifd: &Ifd, tag: u16, n: u64) -> Option> { + let e = ifd.get(tag)?; + if e.count != n { + return None; + } + (0..n).map(|i| self.float(e, i)).collect() + } + + /// Value `i` of a numeric entry as a float: RATIONAL, FLOAT, DOUBLE or an integer. + fn float(&self, e: &Entry, i: u64) -> Option { + if i >= e.count { + return None; + } + let at = e.value_at.checked_add(i.checked_mul(type_size(e.ty)?)?)?; + match e.ty { + 5 => { + let (n, d) = (self.u32_at(at)?, self.u32_at(at.checked_add(4)?)?); + (d != 0).then(|| f64::from(n) / f64::from(d)) + } + 11 => Some(f64::from(f32::from_bits(self.u32_at(at)?))), + 12 => Some(f64::from_bits(self.u64_at(at)?)), + _ => self.uint(e, i).map(|v| v as f64), + } + } + + /// The bytes of a one-byte-per-value tag (BYTE, ASCII, SBYTE or UNDEFINED). + pub fn tag_bytes(&self, ifd: &Ifd, tag: u16) -> Option<&'a [u8]> { + let e = ifd.get(tag)?; + matches!(e.ty, 1 | 2 | 6 | 7).then(|| self.data(e)).flatten() + } + + /// SubIFD offsets of a directory (tag 330 as LONG, IFD, LONG8 or IFD8). + fn sub_ifds(&self, ifd: &Ifd) -> (Vec, bool) { + let mut entries = ifd.entries.iter().filter(|e| e.tag == TAG_SUB_IFDS); + let Some(e) = entries.next() else { return (Vec::new(), true) }; + let mut complete = entries.next().is_none() && e.count <= MAX_SUBIFDS; + if e.count == 0 { + return (Vec::new(), complete); + } + if !matches!(e.ty, 4 | 13 | 16 | 18) { + return (Vec::new(), false); + } + let mut children = Vec::new(); + for i in 0..e.count.min(MAX_SUBIFDS) { + let Some(at) = self.uint(e, i) else { + complete = false; + break; + }; + if at != 0 { + children.push(at); + } + } + (children, complete) + } +} + +/// A directory's Orientation (1–8), with the rules of [`crate::exif_orientation`]: the first +/// Orientation entry wins, it must be exactly one SHORT or LONG, and anything else reads as 1. +pub(crate) fn orientation_of(f: &File<'_>, ifd: &Ifd) -> u16 { + ifd.get(TAG_ORIENTATION) + .filter(|e| e.count == 1 && matches!(e.ty, 3 | 4)) + .and_then(|e| f.uint(e, 0)) + .and_then(|v| u16::try_from(v).ok()) + .filter(|v| (1..=8).contains(v)) + .unwrap_or(1) +} + +/// What a TIFF directory holds, from `NewSubfileType` (254) or the older `SubfileType` (255). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TiffPageKind { + /// A full-resolution image: a page of the document. + Page, + /// A reduced-resolution copy of another image (a thumbnail or a pyramid level). + ReducedResolution, + /// A transparency mask for another image. + Mask, +} + +/// One image directory of a TIFF file, described without decoding its pixels. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TiffPage { + /// Byte offset of the directory in the file. + pub ifd_offset: u64, + /// For a SubIFD (tag 330): the index (in [`TiffInfo::pages`]) of the directory listing it. + /// `None` for the directories of the main IFD chain. + pub parent: Option, + pub kind: TiffPageKind, + /// 0 when the directory has no readable dimensions. + pub width: u32, + pub height: u32, + pub samples_per_pixel: u16, + pub bits_per_sample: u16, + /// TIFF `SampleFormat`: 1 unsigned integer, 2 signed integer, 3 IEEE float. + pub sample_format: u16, + /// TIFF `PhotometricInterpretation`, when present. + pub photometric: Option, + /// TIFF `Compression` (1 = none). + pub compression: u16, + /// Tiles rather than strips. + pub tiled: bool, + /// `PlanarConfiguration` 2: each sample in its own plane. + pub planar: bool, +} + +/// The directory structure of a TIFF file: its byte order, BigTIFF or not, and every image +/// directory of the main chain and its SubIFDs. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct TiffInfo { + pub big_tiff: bool, + pub little_endian: bool, + /// Main-chain directories in file order, each followed by its SubIFDs (depth first). + pub pages: Vec, + /// `false` for unreadable directories or child lists, cycles, or directory/depth/count limits. + /// The pages listed up to that point are still valid. + pub complete: bool, +} + +impl TiffInfo { + /// The page opened by default, like Photoshop: the first full-resolution directory of the + /// main chain (reduced-resolution copies and masks are skipped), else the first directory. + pub fn default_page(&self) -> Option { + self.pages + .iter() + .position(|p| p.parent.is_none() && p.kind == TiffPageKind::Page && p.width > 0 && p.height > 0) + .or_else(|| (!self.pages.is_empty()).then_some(0)) + } + + /// Full-resolution pages in the main chain (what Photoshop calls the pages of the file). + pub fn page_count(&self) -> usize { + self.pages.iter().filter(|p| p.parent.is_none() && p.kind == TiffPageKind::Page).count() + } +} + +fn describe(f: &File<'_>, ifd: &Ifd, parent: Option) -> TiffPage { + let new_kind = f.tag_uint(ifd, TAG_NEW_SUBFILE_TYPE).unwrap_or(0); + let old_kind = f.tag_uint(ifd, TAG_SUBFILE_TYPE).unwrap_or(1); + let kind = if new_kind & 4 != 0 { + TiffPageKind::Mask + } else if new_kind & 1 != 0 || old_kind == 2 { + TiffPageKind::ReducedResolution + } else { + TiffPageKind::Page + }; + let u32_of = |tag| f.tag_uint(ifd, tag).and_then(|v| u32::try_from(v).ok()).unwrap_or(0); + let u16_of = |tag, default| f.tag_uint(ifd, tag).and_then(|v| u16::try_from(v).ok()).unwrap_or(default); + TiffPage { + ifd_offset: ifd.at, + parent, + kind, + width: u32_of(TAG_WIDTH), + height: u32_of(TAG_HEIGHT), + samples_per_pixel: u16_of(TAG_SAMPLES, 1), + bits_per_sample: u16_of(TAG_BITS, 1), + sample_format: u16_of(TAG_SAMPLE_FORMAT, 1), + photometric: f.tag_uint(ifd, TAG_PHOTOMETRIC).and_then(|v| u16::try_from(v).ok()), + compression: u16_of(TAG_COMPRESSION, 1), + tiled: ifd.get(TAG_TILE_OFFSETS).is_some(), + planar: f.tag_uint(ifd, TAG_PLANAR) == Some(2), + } +} + +struct Walk<'a, 'f> { + f: &'f File<'a>, + seen: HashSet, + pages: Vec, + complete: bool, + /// Directory entries that may still be parsed. Real directory tables are disjoint, so their + /// total size is bounded by the file; overlapping tables (a hostile file) would otherwise + /// make every one of thousands of directories cost a full 65,535-entry parse. + entry_budget: u64, +} + +impl Walk<'_, '_> { + /// Follows a chain of directories starting at `at`, listing each with `parent` and then + /// its SubIFDs. + fn chain(&mut self, mut at: u64, parent: Option, depth: usize) { + while at != 0 { + if self.pages.len() >= MAX_IFDS || !self.seen.insert(at) { + self.complete = false; + return; + } + let Some(n) = self.f.entry_count(at) else { + self.complete = false; + return; + }; + if n > self.entry_budget { + self.complete = false; + return; + } + self.entry_budget -= n; + let Some(ifd) = self.f.ifd(at) else { + self.complete = false; + return; + }; + let index = self.pages.len(); + self.pages.push(describe(self.f, &ifd, parent)); + let (children, complete) = self.f.sub_ifds(&ifd); + self.complete &= complete; + if depth < MAX_SUB_DEPTH { + for sub in children { + self.chain(sub, Some(index), depth + 1); + } + } else if !children.is_empty() { + self.complete = false; + } + at = ifd.next; + } + } +} + +/// Lists the directories of a (Big)TIFF file. An error only when the bytes are not a TIFF or +/// the first directory cannot be read; a damaged chain after it ends the list early +/// (`complete == false`). +pub fn tiff_info(bytes: &[u8]) -> Result { + let f = File::parse(bytes).ok_or_else(|| CodecError::malformed(Format::Tiff, "not a TIFF or BigTIFF header"))?; + info_of(&f) +} + +pub(crate) fn info_of(f: &File<'_>) -> Result { + let mut w = Walk { f, seen: HashSet::new(), pages: Vec::new(), complete: true, entry_budget: (f.b.len() as u64) / 12 + 64 }; + w.chain(f.first_ifd, None, 0); + if w.pages.is_empty() { + return Err(CodecError::malformed(Format::Tiff, "the first image directory is missing or cut off")); + } + Ok(TiffInfo { big_tiff: f.big, little_endian: f.le, pages: w.pages, complete: w.complete }) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// 10,000 directories 4 bytes apart, each declaring 65,535 overlapping entries: the walk must + /// stop after the file-size entry budget instead of parsing ~6.5e8 entries. + #[test] + fn overlapping_directory_tables_exhaust_the_entry_budget() { + let first = 8usize; + let table = 2 + 65_535 * 12 + 4; + let mut b = vec![0u8; first + table + 4 * 10_000]; + b[..8].copy_from_slice(&[b'I', b'I', 42, 0, first as u8, 0, 0, 0]); + for i in 0..10_000usize { + let at = first + 4 * i; + b[at..at + 2].copy_from_slice(&u16::MAX.to_le_bytes()); + let next_cell = at + 2 + 65_535 * 12; + let next = (first + 4 * (i + 1)) as u32; + if let Some(cell) = b.get_mut(next_cell..next_cell + 4) { + cell.copy_from_slice(&next.to_le_bytes()); + } + } + let info = tiff_info(&b).expect("the first directory is readable"); + assert!(!info.complete); + assert!(info.pages.len() < 200, "{} directories parsed", info.pages.len()); + } +} diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs new file mode 100644 index 000000000..7c02c614e --- /dev/null +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -0,0 +1,455 @@ +//! Formats delegated to the `image` crate (pure-Rust features only): +//! GIF, BMP, TGA, ICO, QOI, Radiance HDR, and AVIF encode (feature `avif`). Targa is written by +//! [`super::tga`] and only read here. + +use std::io::{Cursor, Read}; + +use image::{DynamicImage, ImageDecoder, ImageFormat}; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, SampleType}; +use crate::options::{EncodeOptions, Limits}; + +fn image_format(f: Format) -> Option { + Some(match f { + Format::Gif => ImageFormat::Gif, + Format::Bmp => ImageFormat::Bmp, + Format::Tga => ImageFormat::Tga, + Format::Ico => ImageFormat::Ico, + Format::Qoi => ImageFormat::Qoi, + Format::Hdr => ImageFormat::Hdr, + Format::Avif => ImageFormat::Avif, + _ => return None, + }) +} + +fn map_err(f: Format, e: image::ImageError) -> CodecError { + match e { + image::ImageError::Limits(l) => CodecError::LimitExceeded(l.to_string()), + image::ImageError::Unsupported(u) => CodecError::unsupported(f, u.to_string()), + e => CodecError::malformed(f, e), + } +} + +pub(crate) fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { + let decoder = image::codecs::bmp::BmpDecoder::new(Cursor::new(bytes)).map_err(|e| map_err(Format::Bmp, e))?; + let mut palette = decoder.get_palette().map(|palette| palette.to_vec()); + // The decoder always hands back 256 entries (it pads a short table with black to survive + // corrupt files); the file's own count is what the document's colour table should hold. + if let (Some(p), Some(n)) = (palette.as_mut(), bmp_palette_len(bytes)) { + p.truncate(n.max(1)); + } + Ok(palette) +} + +/// How many colours a palette BMP's header declares: `biClrUsed`, or 2^bits when it is 0. +/// `None` for a header this does not recognise, which leaves the table as decoded. +fn bmp_palette_len(bytes: &[u8]) -> Option { + let u16_at = |at: usize| bytes.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + let u32_at = |at: usize| bytes.get(at..at + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + let (bit_count, used) = match u32_at(14)? { + // OS/2 BITMAPCOREHEADER: no colour count, the bit depth sets the table size. + 12 => (u16_at(24)?, 0), + n if n >= 40 => (u16_at(28)?, u32_at(46)?), + _ => return None, + }; + if !(1..=8).contains(&bit_count) { + return None; + } + let max = 1usize << bit_count; + Some(if used == 0 { max } else { (used as usize).min(max) }) +} + +/// Writes an 8-bit palette BMP: `indices` row-major, `palette` RGB entries (1..=256). The +/// counterpart of [`bmp_palette`], for Indexed Color documents. +pub(crate) fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { + let f = Format::Bmp; + if width == 0 || height == 0 || u64::from(width) * u64::from(height) != indices.len() as u64 { + return Err(CodecError::InvalidImage("index data does not match the size".into())); + } + if palette.is_empty() || palette.len() > 256 { + return Err(CodecError::InvalidImage("a palette BMP holds 1 to 256 colours".into())); + } + if indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(CodecError::InvalidImage("palette index out of range".into())); + } + let mut out = Cursor::new(Vec::new()); + image::codecs::bmp::BmpEncoder::new(&mut out) + .encode_with_palette(indices, width, height, image::ExtendedColorType::L8, Some(palette)) + .map_err(|e| CodecError::encode(f, e))?; + Ok(out.into_inner()) +} + +/// The `image` decoder for `f` with our limits, its header read and its size checked. +fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { + let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; + let mut reader = image::ImageReader::with_format(Cursor::new(bytes), fmt); + let mut il = image::Limits::default(); + il.max_image_width = Some(limits.max_width); + il.max_image_height = Some(limits.max_height); + il.max_alloc = Some(limits.max_alloc); + reader.limits(il); + if f == Format::Ico { + // The ICO decoder builds its embedded PNG decoder without limits, which inflates an + // iCCP profile up to usize::MAX while the reader is constructed, before `il` applies. + ico_png_profiles_fit(bytes, limits)?; + } + let dec = reader.into_decoder().map_err(|e| map_err(f, e))?; + let (w, h) = dec.dimensions(); + let bpp = dec.color_type().bytes_per_pixel() as u64; + limits.check_bytes(w, h, bpp)?; + // `DynamicImage::from_decoder` allocates the whole image before reading a pixel, so a + // header-only file must not be able to ask for gigabytes (#8597). GIF's LZW tops out near + // 11000:1 of RGBA output and TGA's and QOI's runs far below; AVIF has already decoded by now. + // Radiance HDR's old-style RLE repeat markers multiply their counts by 256 each in a row, so + // no ratio bounds a valid file: only the allocation limit above applies to it. + let declared = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + match f { + Format::Avif | Format::Hdr => {} + Format::Gif => gif_plausible(bytes, w, h, bpp)?, + // A BMP's RLE escapes (end of line, end of bitmap, delta) leave pixels unwritten, so no + // ratio bounds a valid file either. The decoder still allocates and clears the whole + // canvas, so like a GIF's screen only `UNBACKED_CANVAS` of it is free of data. + Format::Bmp if bmp_is_rle(bytes) => Limits::check_plausible(declared.saturating_sub(UNBACKED_CANVAS), bytes.len(), 1 << 14)?, + _ => Limits::check_plausible(declared, bytes.len(), 1 << 14)?, + } + Ok(dec) +} + +/// Canvas bytes a GIF's logical screen or a sparse RLE BMP may declare without data behind +/// them (256 MiB: an 8192x8192 RGBA screen). Past this, the file must back the rest. +const UNBACKED_CANVAS: u64 = 1 << 28; + +/// Whether a GIF whose `w`x`h` logical screen decodes to `bpp` bytes a pixel is plausible for +/// its file size. The first frame is what the file must hold: the logical screen around it needs +/// no data, so up to [`UNBACKED_CANVAS`] of screen is free and only the rest must be backed. +/// Shared by the first-frame decode and the animation decode ([`super::gif_frames`]), whose +/// backend also initializes the whole screen before reading the first frame. +pub(crate) fn gif_plausible(bytes: &[u8], w: u32, h: u32, bpp: u64) -> Result<(), CodecError> { + let declared = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(bpp); + let declared = match gif_first_frame(bytes) { + Some((fw, fh)) => u64::from(fw).saturating_mul(u64::from(fh)).saturating_mul(bpp).max(declared.saturating_sub(UNBACKED_CANVAS)), + None => declared, + }; + Limits::check_plausible(declared, bytes.len(), 1 << 14) +} + +/// Most bytes one embedded iCCP profile of an icon may inflate to: 255 APP2 segments of a JPEG, +/// the largest profile any of our formats carries, and far past any real one. +const MAX_ICO_ICC_BYTES: u64 = 255 * 65_519; +/// Most bytes all of an icon's embedded iCCP profiles may inflate to together while checked. +/// Entries may point into overlapping chunk chains, so this bounds the check's own work. +const MAX_ICO_ICC_TOTAL: u64 = 64 << 20; + +/// Refuses an icon whose embedded PNG images carry an iCCP profile larger than the profile cap +/// (or `max_alloc`). The `image` crate's ICO decoder opens the PNG with no limits, and the `png` +/// crate inflates the profile while the decoder is built: a few kilobytes of zlib would ask for +/// gigabytes. Each profile is inflated here into a sink, so the check itself allocates nothing. +fn ico_png_profiles_fit(b: &[u8], limits: &Limits) -> Result<(), CodecError> { + const PNG_SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; + let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + let u32_at = |at: usize| b.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + let be32 = |at: usize| b.get(at..at.checked_add(4)?).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes); + let cap = MAX_ICO_ICC_BYTES.min(limits.max_alloc); + let Some(count) = u16_at(4) else { return Ok(()) }; + let mut offsets: Vec = (0..usize::from(count)).filter_map(|i| u32_at(6 + 16 * i + 12)).filter_map(|o| usize::try_from(o).ok()).collect(); + offsets.sort_unstable(); + offsets.dedup(); + let mut total = 0u64; + // Chunk offsets already walked by any start: a walk from here repeats the earlier one, so stop. + let mut visited = std::collections::HashSet::new(); + for start in offsets { + if b.get(start..start.saturating_add(8)) != Some(&PNG_SIGNATURE[..]) { + continue; + } + // Chunks up to the image data: the png crate parses iCCP only before IDAT. + let mut at = start + 8; + while let (Some(len), Some(kind)) = (be32(at), b.get(at.saturating_add(4)..at.saturating_add(8))) { + if !visited.insert(at) { + break; + } + let data_at = at + 8; + let Some(data) = usize::try_from(len).ok().and_then(|n| b.get(data_at..data_at.checked_add(n)?)) else { break }; + match kind { + b"IDAT" | b"IEND" => break, + b"iCCP" => { + // Profile name, NUL, compression method, then the zlib stream. + let Some(z) = data.iter().position(|&c| c == 0).and_then(|n| data.get(n + 2..)) else { break }; + // Count what inflates before a corrupt stream fails too: the png crate + // allocates that much before it finds the error. + let mut inflated = flate2::read::ZlibDecoder::new(z).take(cap.saturating_add(1)); + let (mut n, mut buf) = (0u64, [0u8; 8192]); + while let Ok(got @ 1..) = inflated.read(&mut buf) { + n += got as u64; + } + total = total.saturating_add(n); + if n > cap || total > MAX_ICO_ICC_TOTAL { + return Err(CodecError::LimitExceeded(format!("an icon's embedded colour profile inflates past {cap} bytes"))); + } + // The png crate refuses a second iCCP chunk without inflating it. + break; + } + _ => {} + } + at = data_at + data.len() + 4; + } + } + Ok(()) +} + +pub(crate) fn decode(f: Format, bytes: &[u8], limits: &Limits) -> Result { + let mut dec = open(f, bytes, limits)?; + let icc = dec.icc_profile().ok().flatten(); + let exif = dec.exif_metadata().ok().flatten(); + let dynimg = DynamicImage::from_decoder(dec).map_err(|e| map_err(f, e))?; + let mut img = from_dynamic(f, dynimg)?; + img.icc = icc; + img.meta.exif = exif; + if f == Format::Gif { + img.warnings.extend(gif_more_frames(bytes)); + } + Ok(img) +} + +/// The stored size and EXIF block [`decode`] would find, from the decoder's header alone: the +/// same reader, limits and size check, without decoding pixels. AVIF is refused: its decoder +/// decodes the image as it opens. +pub(crate) fn probe(f: Format, bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + if f == Format::Avif { + return Err(CodecError::unsupported(f, "no header-only size probe")); + } + let mut dec = open(f, bytes, limits)?; + let (w, h) = dec.dimensions(); + Ok((w, h, dec.exif_metadata().ok().flatten())) +} + +/// Whether a BMP's `biCompression` is RLE8 or RLE4 (only headers of 40 bytes or more have one). +fn bmp_is_rle(b: &[u8]) -> bool { + let u32_at = |at: usize| b.get(at..at + 4).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes); + u32_at(14).is_some_and(|n| n >= 40) && matches!(u32_at(30), Some(1 | 2)) +} + +/// The width and height of a GIF's first image descriptor, if the blocks reach one. +fn gif_first_frame(b: &[u8]) -> Option<(u32, u32)> { + let mut at = None; + gif_walk(b, |i| { + at = Some(i); + false + }); + let u16_at = |at: usize| b.get(at..at + 2).and_then(|b| b.try_into().ok()).map(u16::from_le_bytes); + // Descriptor: separator, left, top, width, height (little-endian u16s), packed fields. + let i = at?; + Some((u32::from(u16_at(i + 5)?), u32::from(u16_at(i + 7)?))) +} + +/// Counts a GIF's frames (image descriptors) by walking its blocks, without decompressing any. +/// The total is unknown when the blocks end before the trailer or break off. +pub(crate) fn gif_more_frames(b: &[u8]) -> Option { + let (frames, total) = gif_walk(b, |_| true); + (frames > 1).then_some(DecodeWarning::MoreFrames { total }) +} + +/// Whether a GIF holds more than one frame, or None when the walk stops before the trailer +/// after at most one frame (so frames past the break could not be counted). +pub(crate) fn gif_has_more_frames(b: &[u8]) -> Option { + let (frames, total) = gif_walk(b, |_| true); + if frames > 1 { Some(true) } else { total.map(|_| false) } +} + +/// Walks a GIF's blocks without decompressing any, handing `frame` the offset of each image +/// descriptor until it returns false. Gives the descriptors seen, and the frame count if the +/// blocks reached the trailer. +fn gif_walk(b: &[u8], mut frame: impl FnMut(usize) -> bool) -> (u32, Option) { + // Size of the colour table a packed-fields byte announces. + let table = |flags: u8| if flags & 0x80 != 0 { 3usize << ((flags & 7) + 1) } else { 0 }; + // Skips the data sub-blocks starting at `i` (each length-prefixed, ended by a zero length). + let skip = |mut i: usize| -> Option { + loop { + let n = usize::from(*b.get(i)?); + i = i.checked_add(1 + n)?; + if n == 0 { + return Some(i); + } + } + }; + // Header (6 bytes), logical screen descriptor (7) and the global colour table. + let Some(&flags) = b.get(10) else { + return (0, None); + }; + let mut i = 13 + table(flags); + let mut frames = 0u32; + let total = loop { + let next = match b.get(i) { + Some(0x3B) => break Some(frames), + Some(0x21) => skip(i + 2), + Some(0x2C) => { + frames = frames.saturating_add(1); + if !frame(i) { + break None; + } + // Descriptor (10 bytes, packed fields last), local colour table, LZW code size. + b.get(i + 9).and_then(|&flags| skip(i + 11 + table(flags))) + } + _ => None, + }; + match next { + Some(n) => i = n, + None => break None, + } + }; + (frames, total) +} + +pub(crate) fn from_dynamic(f: Format, d: DynamicImage) -> Result { + let (w, h) = (d.width(), d.height()); + Ok(match d { + DynamicImage::ImageLuma8(b) => Image::from_u8(w, h, ChannelLayout::Gray, b.into_raw())?, + DynamicImage::ImageLumaA8(b) => Image::from_u8(w, h, ChannelLayout::GrayA, b.into_raw())?, + DynamicImage::ImageRgb8(b) => Image::from_u8(w, h, ChannelLayout::Rgb, b.into_raw())?, + DynamicImage::ImageRgba8(b) => Image::from_u8(w, h, ChannelLayout::Rgba, b.into_raw())?, + DynamicImage::ImageLuma16(b) => Image::from_u16(w, h, ChannelLayout::Gray, &b.into_raw())?, + DynamicImage::ImageLumaA16(b) => Image::from_u16(w, h, ChannelLayout::GrayA, &b.into_raw())?, + DynamicImage::ImageRgb16(b) => Image::from_u16(w, h, ChannelLayout::Rgb, &b.into_raw())?, + DynamicImage::ImageRgba16(b) => Image::from_u16(w, h, ChannelLayout::Rgba, &b.into_raw())?, + DynamicImage::ImageRgb32F(b) => Image::from_f32(w, h, ChannelLayout::Rgb, &b.into_raw())?, + DynamicImage::ImageRgba32F(b) => Image::from_f32(w, h, ChannelLayout::Rgba, &b.into_raw())?, + other => { + let b = other.into_rgba32f(); + Image::from_f32(w, h, ChannelLayout::Rgba, &b.into_raw()).map_err(|e| CodecError::malformed(f, e))? + } + }) +} + +fn to_dynamic(img: &Image) -> Result { + let (w, h) = img.dimensions(); + let bad = || CodecError::InvalidImage("buffer size mismatch".into()); + Ok(match (img.layout(), img.sample_type()) { + (ChannelLayout::Gray, SampleType::U8) => DynamicImage::ImageLuma8(image::GrayImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::GrayA, SampleType::U8) => DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgb, SampleType::U8) => DynamicImage::ImageRgb8(image::RgbImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgba, SampleType::U8) => DynamicImage::ImageRgba8(image::RgbaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::Rgb, SampleType::F32) => { + DynamicImage::ImageRgb32F(image::Rgb32FImage::from_raw(w, h, img.to_f32_samples().unwrap_or_default()).ok_or_else(bad)?) + } + (ChannelLayout::Rgba, SampleType::F32) => { + DynamicImage::ImageRgba32F(image::Rgba32FImage::from_raw(w, h, img.to_f32_samples().unwrap_or_default()).ok_or_else(bad)?) + } + (l, s) => { + return Err(CodecError::InvalidImage(format!("no image-crate mapping for {l:?} {s:?}"))); + } + }) +} + +pub(crate) fn encode(f: Format, src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; + if let Some((mw, mh)) = f.max_dimensions() + && (src.width() > mw || src.height() > mh) + { + return Err(CodecError::encode(f, format!("dimensions exceed {mw}x{mh}"))); + } + let img = src.converted(plan.layout, plan.sample); + // Targa is written by our encoder: the image crate's RLE packets cross scanlines (#6382). + if f == Format::Tga { + return super::tga::encode(&img); + } + let dynimg = to_dynamic(&img)?; + let mut cursor = Cursor::new(Vec::new()); + match f { + #[cfg(feature = "avif")] + Format::Avif => { + let enc = image::codecs::avif::AvifEncoder::new_with_speed_quality(&mut cursor, 8, opts.jpeg_quality.clamp(1, 100)); + dynimg.write_with_encoder(enc).map_err(|e| CodecError::encode(f, e))?; + } + _ => { + let _ = opts; + dynimg.write_to(&mut cursor, fmt).map_err(|e| CodecError::encode(f, e))?; + } + } + Ok(cursor.into_inner()) +} + +#[cfg(test)] +mod indexed_tests { + use super::*; + + #[test] + fn palette_bmp_roundtrips_its_table_and_pixels() { + // 3 px wide: rows are padded to 4 bytes, so a wrong stride shows in the second row. + let pal = [[255, 0, 0], [0, 0, 255], [10, 20, 30]]; + let idx = [0u8, 1, 2, 2, 1, 0]; + let bytes = encode_bmp_indexed(3, 2, &idx, &pal).unwrap(); + assert_eq!(bmp_palette(&bytes).unwrap().as_deref(), Some(&pal[..]), "the table survives, not a 256-entry grey ramp"); + let img = decode(Format::Bmp, &bytes, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (3, 2)); + let rgb = img.convert(ChannelLayout::Rgb, SampleType::U8); + let want: Vec = idx.iter().flat_map(|&i| pal[usize::from(i)]).collect(); + assert_eq!(rgb.data(), want.as_slice()); + } + + #[test] + fn palette_len_follows_the_header() { + let mut bytes = encode_bmp_indexed(2, 1, &[0, 1], &[[1, 2, 3], [4, 5, 6]]).unwrap(); + assert_eq!(bmp_palette_len(&bytes), Some(2)); + // biClrUsed = 0 means the full 2^bits table. + bytes[46..50].copy_from_slice(&0u32.to_le_bytes()); + assert_eq!(bmp_palette_len(&bytes), Some(256)); + // A short or unknown header leaves the decoded table alone instead of panicking. + assert_eq!(bmp_palette_len(&bytes[..30]), None); + bytes[14..18].copy_from_slice(&7u32.to_le_bytes()); + assert_eq!(bmp_palette_len(&bytes), None); + } + + /// A 64x48 icon holding one PNG with an iCCP chunk of `profile` bytes (zeros). + fn icon_with_profile(profile: usize) -> Vec { + use std::io::Write; + let img = Image::from_u8(64, 48, ChannelLayout::Rgba, vec![128; 64 * 48 * 4]).unwrap(); + let png = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best()); + z.write_all(&vec![0u8; profile]).unwrap(); + let mut data = b"icc\0\0".to_vec(); + data.extend_from_slice(&z.finish().unwrap()); + let mut crc = flate2::Crc::new(); + crc.update(b"iCCP"); + crc.update(&data); + let mut chunk = (data.len() as u32).to_be_bytes().to_vec(); + chunk.extend_from_slice(b"iCCP"); + chunk.extend_from_slice(&data); + chunk.extend_from_slice(&crc.sum().to_be_bytes()); + // Signature (8) and IHDR (25), then the profile. + let mut png_icc = png[..33].to_vec(); + png_icc.extend_from_slice(&chunk); + png_icc.extend_from_slice(&png[33..]); + let mut ico = vec![0, 0, 1, 0, 1, 0, 64, 48, 0, 0, 1, 0, 32, 0]; + ico.extend_from_slice(&(png_icc.len() as u32).to_le_bytes()); + ico.extend_from_slice(&22u32.to_le_bytes()); + ico.extend_from_slice(&png_icc); + ico + } + + #[test] + fn an_icon_profile_inflating_past_the_cap_is_refused_before_the_png_opens() { + let small = icon_with_profile(4096); + let img = decode(Format::Ico, &small, &Limits::default()).unwrap(); + assert_eq!(img.dimensions(), (64, 48)); + let huge = icon_with_profile(MAX_ICO_ICC_BYTES as usize + 1); + assert!(huge.len() < 64 << 10, "{} bytes", huge.len()); + for r in [decode(Format::Ico, &huge, &Limits::default()).map(|_| ()), probe(Format::Ico, &huge, &Limits::default()).map(|_| ())] { + assert!(matches!(&r, Err(CodecError::LimitExceeded(m)) if m.contains("colour profile")), "{r:?}"); + } + // max_alloc lowers the cap. + let tight = Limits { max_alloc: 2048, ..Limits::default() }; + assert!(matches!(decode(Format::Ico, &small, &tight), Err(CodecError::LimitExceeded(_)))); + } + + #[test] + fn palette_bmp_rejects_bad_input() { + let pal = [[0, 0, 0], [255, 255, 255]]; + assert!(encode_bmp_indexed(3, 2, &[0; 5], &pal).is_err(), "size mismatch"); + assert!(encode_bmp_indexed(0, 2, &[], &pal).is_err(), "empty"); + assert!(encode_bmp_indexed(2, 1, &[0, 2], &pal).is_err(), "index past the table"); + assert!(encode_bmp_indexed(1, 1, &[0], &[]).is_err(), "no colours"); + assert!(encode_bmp_indexed(1, 1, &[0], &[[0; 3]; 257]).is_err(), "more than 256 colours"); + } +} diff --git a/crates/image-codecs/src/codecs/vp8.rs b/crates/image-codecs/src/codecs/vp8.rs new file mode 100644 index 000000000..a24fb727c --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8.rs @@ -0,0 +1,848 @@ +//! A clean-room VP8 key-frame encoder (the lossy WebP bitstream), implemented from RFC 6386. +//! +//! One intra frame: 16×16 macroblocks in raster order, each predicted from the frame's own +//! reconstruction with one of the four whole-block luma modes or the sixteen 4×4 sub-block +//! modes (`B_PRED`), chroma with one of four modes; the residual goes through the 4×4 DCT +//! (and the WHT of the luma DCs for whole-block modes), one frame-wide quantizer derived from the +//! quality, and the boolean entropy coder with the specification's default token probabilities, +//! refined per frame from the frame's own token statistics. Macroblocks without coefficients +//! are skipped. The loop filter is left to the decoder at a level matched to the quantizer. +//! +//! What it does not do (yet): rate-distortion optimized mode decisions and quantization, +//! segments, multiple token partitions, or SIMD. It is deterministic, allocation-bounded and +//! never panics: dimensions are checked up front and every index is derived from them. + +mod bool_encoder; +mod predict; +mod tables; +mod transform; + +use bool_encoder::BoolEncoder; +use predict::{Plane, SubEdges, predict_block, predict_sub}; +use tables::{ + AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, DC_QLOOKUP, + DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, KF_BMODE_PROBS, + KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, +}; +use transform::{fdct, fwht, idct, iwht}; + +/// The largest dimension a VP8 frame header can hold (14 bits). +pub const MAX_DIMENSION: u32 = 16383; + +/// A planar 4:2:0 picture: `y` is `width × height`, `u` and `v` are `ceil(w/2) × ceil(h/2)`. +pub struct Yuv { + pub width: u32, + pub height: u32, + pub y: Vec, + pub u: Vec, + pub v: Vec, +} + +impl Yuv { + /// Converts interleaved 8-bit RGB (`stride` bytes per pixel, 3 or 4) to studio-range BT.601 + /// Y'CbCr with the chroma averaged over each 2 × 2 block, the convention WebP decoders invert. + pub fn from_rgb(rgb: &[u8], width: u32, height: u32, stride: usize) -> Yuv { + let (w, h) = (width as usize, height as usize); + let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); + let mut y = vec![0u8; w * h]; + let mut u = vec![0u8; cw * ch]; + let mut v = vec![0u8; cw * ch]; + let px = |x: usize, yy: usize| -> (i32, i32, i32) { + let i = (yy.min(h - 1) * w + x.min(w - 1)) * stride; + (i32::from(rgb[i]), i32::from(rgb[i + 1]), i32::from(rgb[i + 2])) + }; + for yy in 0..h { + for x in 0..w { + let (r, g, b) = px(x, yy); + y[yy * w + x] = (((66 * r + 129 * g + 25 * b + 128) >> 8) + 16) as u8; + } + } + for cy in 0..ch { + for cx in 0..cw { + let (mut r, mut g, mut b) = (0, 0, 0); + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let (pr, pg, pb) = px(cx * 2 + dx, cy * 2 + dy); + r += pr; + g += pg; + b += pb; + } + let (r, g, b) = ((r + 2) >> 2, (g + 2) >> 2, (b + 2) >> 2); + u[cy * cw + cx] = (((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128).clamp(0, 255) as u8; + v[cy * cw + cx] = (((112 * r - 94 * g - 18 * b + 128) >> 8) + 128).clamp(0, 255) as u8; + } + } + Yuv { width, height, y, u, v } + } +} + +/// Encoder settings. +#[derive(Debug, Clone, Copy)] +pub struct Params { + /// 0 (smallest) … 100 (best); maps to the frame quantizer. + pub quality: u8, +} + +/// Why a frame cannot be encoded. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Vp8Error { + /// A dimension is 0 or above [`MAX_DIMENSION`]. + Dimensions, + /// The plane buffers do not match the dimensions. + Planes, +} + +impl std::fmt::Display for Vp8Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Vp8Error::Dimensions => write!(f, "VP8 frames are 1 to {MAX_DIMENSION} pixels per side"), + Vp8Error::Planes => write!(f, "plane sizes do not match the dimensions"), + } + } +} + +/// Quality 0..=100 → quantizer index 0..=127 (0 is finest). A gentle curve: quality 90 ≈ 11, +/// 75 ≈ 28, 50 ≈ 60, 25 ≈ 92. +pub fn quantizer_index(quality: u8) -> u8 { + let q = f64::from(quality.min(100)); + let t = (100.0 - q) / 100.0; + (t.powf(1.1) * 127.0).round().clamp(0.0, 127.0) as u8 +} + +/// Dequantization factors for one quantizer index (RFC 6386 §14.1 and dixie's `dequant_init`). +#[derive(Debug, Clone, Copy)] +struct Quant { + y_dc: i32, + y_ac: i32, + y2_dc: i32, + y2_ac: i32, + uv_dc: i32, + uv_ac: i32, +} + +impl Quant { + fn new(qi: u8) -> Quant { + let qi = usize::from(qi.min(127)); + Quant { + y_dc: DC_QLOOKUP[qi], + y_ac: AC_QLOOKUP[qi], + y2_dc: DC_QLOOKUP[qi] * 2, + y2_ac: (AC_QLOOKUP[qi] * 155 / 100).max(8), + uv_dc: DC_QLOOKUP[qi].min(132), + uv_ac: AC_QLOOKUP[qi], + } + } +} + +/// Quantizes one coefficient: round to nearest for DC, with a small dead zone for AC (fewer +/// ±1 coefficients cost far fewer bits than they are worth in distortion). +#[inline] +fn quantize(coeff: i32, factor: i32, dc: bool) -> i32 { + let bias = if dc { factor / 2 } else { factor * 7 / 16 }; + let q = (coeff.abs() + bias) / factor; + let q = q.min(2048); + if coeff < 0 { -q } else { q } +} + +/// Block types of the token probability table (RFC 6386 §13.3). +const TYPE_Y_AFTER_Y2: usize = 0; +const TYPE_Y2: usize = 1; +const TYPE_UV: usize = 2; +const TYPE_Y_WITH_DC: usize = 3; + +/// Which coefficient token codes `v` and, for the categories, the extra bits to send. +fn token_of(v: i32) -> (u8, Option<(usize, u32)>) { + let a = v.unsigned_abs() as i32; + match a { + 0 => (DCT_0, None), + 1 => (DCT_1, None), + 2 => (DCT_2, None), + 3 => (DCT_3, None), + 4 => (DCT_4, None), + 5..=6 => (DCT_CAT1, Some((0, (a - CAT_BASE[0]) as u32))), + 7..=10 => (DCT_CAT2, Some((1, (a - CAT_BASE[1]) as u32))), + 11..=18 => (DCT_CAT3, Some((2, (a - CAT_BASE[2]) as u32))), + 19..=34 => (DCT_CAT4, Some((3, (a - CAT_BASE[3]) as u32))), + 35..=66 => (DCT_CAT5, Some((4, (a - CAT_BASE[4]) as u32))), + _ => (DCT_CAT6, Some((5, (a - CAT_BASE[5]) as u32))), + } +} + +/// One coded block's quantized coefficients in zig-zag order, from `first` (1 for Y after Y2). +struct Block { + coeffs: [i32; 16], + first: usize, + /// One past the last non-zero zig-zag position (`first` when the block is empty). + eob: usize, +} + +impl Block { + fn from_raster(raster: &[i32; 16], first: usize) -> Block { + let mut coeffs = [0i32; 16]; + for (pos, &idx) in ZIGZAG.iter().enumerate() { + coeffs[pos] = raster[idx]; + } + let eob = (first..16).rev().find(|&p| coeffs[p] != 0).map_or(first, |p| p + 1); + Block { coeffs, first, eob } + } + + fn has_coeffs(&self) -> bool { + self.eob > self.first + } +} + +/// Token-count statistics, `[type][band][ctx][token]`, from which the frame's probabilities are +/// derived (and the per-node branch counts needed to turn them into tree probabilities). +#[derive(Clone)] +struct Stats { + /// Per tree node (11 per context): how often the bool was 0 and how often 1. + node: Vec<[[[[u32; 2]; 11]; 3]; 8]>, +} + +impl Stats { + fn new() -> Stats { + Stats { node: vec![[[[[0; 2]; 11]; 3]; 8]; 4] } + } +} + +/// Everything about a block's tokens the two passes share: the branch path of each token in the +/// coefficient tree. Walked once per block to count branches, once to write them. +fn tree_path(value: u8, start: usize) -> Vec<(usize, bool)> { + fn find(node: usize, value: u8, path: &mut Vec<(usize, bool)>) -> bool { + for bit in [false, true] { + let i = node + usize::from(bit); + let Some(&entry) = COEFF_TREE.get(i) else { continue }; + path.push((node >> 1, bit)); + if entry <= 0 { + if (-entry) as u8 == value { + return true; + } + } else if find(entry as usize, value, path) { + return true; + } + path.pop(); + } + false + } + let mut path = Vec::with_capacity(8); + find(start, value, &mut path); + path +} + +/// One bool of a block's token sequence. +enum Event { + /// A coefficient-tree branch: `(type, band, ctx, node, bit)`, coded with `probs[type][band][ctx][node]`. + Node(usize, usize, usize, usize, bool), + /// The extra bits of a category token: `(category, bits)`. + Extra(usize, u32), + /// The sign of a non-zero coefficient (`true` = negative). + Sign(bool), +} + +/// Visits every bool of a block's token sequence in coding order. +fn for_each_token_bool(block: &Block, ty: usize, ctx0: usize, mut f: impl FnMut(Event)) { + let mut ctx = ctx0; + let mut prev_zero = false; + let mut pos = block.first; + while pos < block.eob { + let v = block.coeffs[pos]; + let (token, cat) = token_of(v); + let band = COEFF_BANDS[pos]; + for (node, bit) in tree_path(token, if prev_zero { 2 } else { 0 }) { + f(Event::Node(ty, band, ctx, node, bit)); + } + if let Some((c, bits)) = cat { + f(Event::Extra(c, bits)); + } + if v != 0 { + f(Event::Sign(v < 0)); + } + ctx = match v.abs() { + 0 => 0, + 1 => 1, + _ => 2, + }; + prev_zero = v == 0; + pos += 1; + } + if block.eob < 16 { + // End of block (never directly after a zero: the coded run ends with a non-zero value). + let band = COEFF_BANDS[block.eob]; + for (node, bit) in tree_path(DCT_EOB, 0) { + f(Event::Node(ty, band, ctx, node, bit)); + } + } +} + +/// A macroblock's decisions and coefficients. +struct Macroblock { + y_mode: u8, + b_modes: [u8; 16], + uv_mode: u8, + /// Y2 (whole-block modes only), 16 Y, 4 U, 4 V. + y2: Option, + y: Vec, + u: Vec, + v: Vec, + skip: bool, +} + +/// Non-zero contexts: 4 Y (by column for "above", by row for "left"), 2 U, 2 V, 1 Y2. +type Ctx = [u8; 9]; + +struct Encoder<'a> { + src: &'a Yuv, + mb_cols: usize, + mb_rows: usize, + q: Quant, + rec_y: Plane, + rec_u: Plane, + rec_v: Plane, + mbs: Vec, +} + +impl<'a> Encoder<'a> { + fn new(src: &'a Yuv, qi: u8) -> Encoder<'a> { + let mb_cols = (src.width as usize).div_ceil(16); + let mb_rows = (src.height as usize).div_ceil(16); + Encoder { + src, + mb_cols, + mb_rows, + q: Quant::new(qi), + rec_y: Plane::new(mb_cols * 16, mb_rows * 16), + rec_u: Plane::new(mb_cols * 8, mb_rows * 8), + rec_v: Plane::new(mb_cols * 8, mb_rows * 8), + mbs: Vec::with_capacity(mb_cols * mb_rows), + } + } + + /// Source luma at (`x`, `y`), the frame's last row and column extended over the padding. + #[inline] + fn src_y(&self, x: usize, y: usize) -> i32 { + let (w, h) = (self.src.width as usize, self.src.height as usize); + i32::from(self.src.y[y.min(h - 1) * w + x.min(w - 1)]) + } + + #[inline] + fn src_c(&self, plane: &[u8], x: usize, y: usize) -> i32 { + let (w, h) = ((self.src.width as usize).div_ceil(2), (self.src.height as usize).div_ceil(2)); + i32::from(plane[y.min(h - 1) * w + x.min(w - 1)]) + } + + /// Encodes one 4×4 block: residual → coefficients → quantized (in raster order, with the + /// quantization factors) → the dequantized coefficients the decoder will see. + fn code_block(residual: &[i32; 16], dc_factor: i32, ac_factor: i32, with_dc: bool) -> ([i32; 16], [i32; 16]) { + let coeffs = fdct(residual); + let mut quantized = [0i32; 16]; + let mut dequantized = [0i32; 16]; + for i in 0..16 { + let factor = if i == 0 { dc_factor } else { ac_factor }; + if i == 0 && !with_dc { + continue; + } + quantized[i] = quantize(coeffs[i], factor, i == 0); + dequantized[i] = quantized[i] * factor; + } + (quantized, dequantized) + } + + /// Sum of squared differences between a predicted block and the source. + fn sse(pred: &[u8], src: &[i32]) -> u64 { + pred.iter() + .zip(src) + .map(|(&p, &s)| { + let d = i64::from(p) - i64::from(s); + (d * d) as u64 + }) + .sum() + } + + /// Chroma of one macroblock: one mode for both planes; returns it with the U and V blocks. + fn encode_chroma(&mut self, mbx: usize, mby: usize) -> (u8, Vec, Vec) { + let (cx0, cy0) = (mbx * 8, mby * 8); + let mut src_u = [0i32; 64]; + let mut src_v = [0i32; 64]; + for r in 0..8 { + for c in 0..8 { + src_u[r * 8 + c] = self.src_c(&self.src.u, cx0 + c, cy0 + r); + src_v[r * 8 + c] = self.src_c(&self.src.v, cx0 + c, cy0 + r); + } + } + let mut uv_mode = DC_PRED; + let mut uv_best = u64::MAX; + let mut pred_u = [0u8; 64]; + let mut pred_v = [0u8; 64]; + let mut pu = [0u8; 64]; + let mut pv = [0u8; 64]; + for mode in [DC_PRED, V_PRED, H_PRED, TM_PRED] { + predict_block(&self.rec_u, cx0, cy0, 8, mode, &mut pu); + predict_block(&self.rec_v, cx0, cy0, 8, mode, &mut pv); + let e = Self::sse(&pu, &src_u) + Self::sse(&pv, &src_v); + if e < uv_best { + uv_best = e; + uv_mode = mode; + pred_u = pu; + pred_v = pv; + } + } + let mut u_blocks = Vec::with_capacity(4); + let mut v_blocks = Vec::with_capacity(4); + for (plane_src, pred, blocks, is_u) in [(&src_u, &pred_u, &mut u_blocks, true), (&src_v, &pred_v, &mut v_blocks, false)] { + for b in 0..4 { + let mut residual = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 1) * 4 + r) * 8 + (b & 1) * 4 + c; + residual[r * 4 + c] = plane_src[i] - i32::from(pred[i]); + } + } + let (quantized, dequantized) = Self::code_block(&residual, self.q.uv_dc, self.q.uv_ac, true); + let recon = idct(&dequantized); + let (bx, by) = (cx0 + (b & 1) * 4, cy0 + (b >> 1) * 4); + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 1) * 4 + r) * 8 + (b & 1) * 4 + c; + let value = (i32::from(pred[i]) + recon[r * 4 + c]).clamp(0, 255) as u8; + if is_u { + self.rec_u.set(bx + c, by + r, value); + } else { + self.rec_v.set(bx + c, by + r, value); + } + } + } + blocks.push(Block::from_raster(&quantized, 0)); + } + } + (uv_mode, u_blocks, v_blocks) + } + + // Sub-block indices address several arrays by geometry; ranges read better than iterators here. + #[allow(clippy::needless_range_loop)] + fn encode_macroblock(&mut self, mbx: usize, mby: usize) { + let (x0, y0) = (mbx * 16, mby * 16); + // Source luma of this macroblock. + let mut src = [0i32; 256]; + for r in 0..16 { + for c in 0..16 { + src[r * 16 + c] = self.src_y(x0 + c, y0 + r); + } + } + // Whole-block luma modes: the least squared prediction error wins. + let mut pred16 = [0u8; 256]; + let mut best_mode = DC_PRED; + let mut best_sse = u64::MAX; + let mut best_pred = [0u8; 256]; + for mode in [DC_PRED, V_PRED, H_PRED, TM_PRED] { + predict_block(&self.rec_y, x0, y0, 16, mode, &mut pred16); + let e = Self::sse(&pred16, &src); + if e < best_sse { + best_sse = e; + best_mode = mode; + best_pred = pred16; + } + } + // Rate-aware comparison with B_PRED: each sub-block mode costs about three bits, and + // the whole-block path codes the DCs once in Y2. lambda scales distortion per bit with + // the quantizer (squared error units). + let lambda = u64::from((self.q.y_ac * self.q.y_ac) as u32) / 8; + let whole_cost = best_sse + 3 * lambda; + + // B_PRED: sub-blocks in raster order, each predicted from the reconstruction so far. + let saved_y = self.rec_y.data.clone(); + let mut b_modes = [B_DC_PRED; 16]; + let mut b_blocks: Vec = Vec::with_capacity(16); + let mut b_sse = 0u64; + { + for b in 0..16 { + let (bx, by) = (x0 + (b & 3) * 4, y0 + (b >> 2) * 4); + let edges = SubEdges::gather(&self.rec_y, x0, y0, b, self.mb_cols); + let mut sub_src = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + sub_src[r * 4 + c] = src[((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c]; + } + } + let mut best = (u64::MAX, B_DC_PRED, [0u8; 16]); + let mut pred = [0u8; 16]; + for mode in 0..10u8 { + predict_sub(&edges, mode, &mut pred); + let e = Self::sse(&pred, &sub_src); + if e < best.0 { + best = (e, mode, pred); + } + } + b_modes[b] = best.1; + b_sse += best.0; + let residual: [i32; 16] = std::array::from_fn(|i| sub_src[i] - i32::from(best.2[i])); + let (quantized, dequantized) = Self::code_block(&residual, self.q.y_dc, self.q.y_ac, true); + let recon = idct(&dequantized); + for r in 0..4 { + for c in 0..4 { + self.rec_y.set(bx + c, by + r, (i32::from(best.2[r * 4 + c]) + recon[r * 4 + c]).clamp(0, 255) as u8); + } + } + b_blocks.push(Block::from_raster(&quantized, 0)); + } + } + let b_cost = b_sse + 50 * lambda; + + let (y_mode, y2, y_blocks) = if b_cost < whole_cost { + (B_PRED, None, b_blocks) + } else { + // Whole-block mode: undo the B_PRED reconstruction, code 16 blocks + Y2. + self.rec_y.data = saved_y; + let mut dcs = [0i32; 16]; + let mut ac_blocks: Vec<[i32; 16]> = Vec::with_capacity(16); + let mut deq_blocks: Vec<[i32; 16]> = Vec::with_capacity(16); + for b in 0..16 { + let mut residual = [0i32; 16]; + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c; + residual[r * 4 + c] = src[i] - i32::from(best_pred[i]); + } + } + let coeffs = fdct(&residual); + dcs[b] = coeffs[0]; + let mut quantized = [0i32; 16]; + let mut dequantized = [0i32; 16]; + for i in 1..16 { + quantized[i] = quantize(coeffs[i], self.q.y_ac, false); + dequantized[i] = quantized[i] * self.q.y_ac; + } + ac_blocks.push(quantized); + deq_blocks.push(dequantized); + } + // The DCs through the WHT, quantized as Y2, and back through the inverse WHT so the + // reconstruction uses exactly the decoder's DC values. + let y2_coeffs = fwht(&dcs); + let mut y2_q = [0i32; 16]; + let mut y2_deq = [0i32; 16]; + for i in 0..16 { + let factor = if i == 0 { self.q.y2_dc } else { self.q.y2_ac }; + y2_q[i] = quantize(y2_coeffs[i], factor, i == 0); + y2_deq[i] = y2_q[i] * factor; + } + let dc_rec = iwht(&y2_deq); + for b in 0..16 { + deq_blocks[b][0] = dc_rec[b]; + let recon = idct(&deq_blocks[b]); + let (bx, by) = (x0 + (b & 3) * 4, y0 + (b >> 2) * 4); + for r in 0..4 { + for c in 0..4 { + let i = ((b >> 2) * 4 + r) * 16 + (b & 3) * 4 + c; + self.rec_y.set(bx + c, by + r, (i32::from(best_pred[i]) + recon[r * 4 + c]).clamp(0, 255) as u8); + } + } + } + let blocks = ac_blocks.iter().map(|q| Block::from_raster(q, 1)).collect(); + (best_mode, Some(Block::from_raster(&y2_q, 0)), blocks) + }; + + let (uv_mode, u_blocks, v_blocks) = self.encode_chroma(mbx, mby); + + let skip = y2.as_ref().is_none_or(|b| !b.has_coeffs()) + && y_blocks.iter().all(|b| !b.has_coeffs()) + && u_blocks.iter().all(|b| !b.has_coeffs()) + && v_blocks.iter().all(|b| !b.has_coeffs()); + self.mbs.push(Macroblock { y_mode, b_modes, uv_mode, y2, y: y_blocks, u: u_blocks, v: v_blocks, skip }); + } + + /// Runs `f` over every token bool of the frame in coding order, with the non-zero contexts + /// maintained exactly as the decoder does (RFC 6386 §13.3; skipped macroblocks reset them, + /// leaving Y2 alone for `B_PRED` macroblocks). + fn walk_tokens(&self, mut f: impl FnMut(&Block, usize, usize), mut on_mb: impl FnMut(usize, &Macroblock)) { + let mut above: Vec = vec![[0; 9]; self.mb_cols]; + for mby in 0..self.mb_rows { + let mut left: Ctx = [0; 9]; + for (mbx, a) in above.iter_mut().enumerate() { + let mb = &self.mbs[mby * self.mb_cols + mbx]; + on_mb(mby * self.mb_cols + mbx, mb); + if mb.skip { + for i in 0..8 { + left[i] = 0; + a[i] = 0; + } + if mb.y_mode != B_PRED { + left[8] = 0; + a[8] = 0; + } + continue; + } + if let Some(y2) = &mb.y2 { + let ctx = usize::from(left[8] + a[8]); + f(y2, TYPE_Y2, ctx); + let t = u8::from(y2.has_coeffs()); + left[8] = t; + a[8] = t; + } + let ty = if mb.y2.is_some() { TYPE_Y_AFTER_Y2 } else { TYPE_Y_WITH_DC }; + for (i, b) in mb.y.iter().enumerate() { + let (ai, li) = (i & 3, i >> 2); + let ctx = usize::from(left[li] + a[ai]); + f(b, ty, ctx); + let t = u8::from(b.has_coeffs()); + left[li] = t; + a[ai] = t; + } + for (base, blocks) in [(4usize, &mb.u), (6usize, &mb.v)] { + for (i, b) in blocks.iter().enumerate() { + let (ai, li) = (base + (i & 1), base + (i >> 1)); + let ctx = usize::from(left[li] + a[ai]); + f(b, TYPE_UV, ctx); + let t = u8::from(b.has_coeffs()); + left[li] = t; + a[ai] = t; + } + } + } + } + } + + /// Token probabilities for this frame: the defaults, replaced where the frame's own branch + /// counts give a clearly better estimate (sent as updates in the header). + fn choose_probs(&self, stats: &Stats) -> [[[[u8; 11]; 3]; 8]; 4] { + let mut probs = DEFAULT_COEFF_PROBS; + for (t, plane) in probs.iter_mut().enumerate() { + for (b, band) in plane.iter_mut().enumerate() { + for (c, ctx) in band.iter_mut().enumerate() { + for (n, p) in ctx.iter_mut().enumerate() { + let [zeros, ones] = stats.node[t][b][c][n]; + let total = zeros + ones; + if total < 24 { + continue; + } + // Probability of a 0 branch, kept inside 1..=255. + let est = ((u64::from(zeros) * 256 + u64::from(total) / 2) / u64::from(total)).clamp(1, 255) as u8; + // Only worth the update bits when the estimate moves the coding cost. + let cost = |pr: u8| -> f64 { + let p0 = f64::from(pr) / 256.0; + -(f64::from(zeros) * p0.log2() + f64::from(ones) * (1.0 - p0).log2()) + }; + let update_bits = 8.0 + 1.0; + if cost(*p) - cost(est) > update_bits * 2.0 { + *p = est; + } + } + } + } + } + probs + } + + fn write_frame(&self, probs: &[[[[u8; 11]; 3]; 8]; 4], loop_filter_level: u8, qi: u8) -> Vec { + // First partition: frame header, then per-macroblock prediction records. + let mut h = BoolEncoder::new(); + h.put_flag(false); // color_space: BT.601-like + h.put_flag(false); // clamping_type: the decoder clamps + h.put_flag(false); // segmentation_enabled + h.put_flag(false); // filter_type: normal + h.put_literal(6, u32::from(loop_filter_level.min(63))); + h.put_literal(3, 0); // sharpness_level + h.put_flag(false); // loop_filter_adj_enable + h.put_literal(2, 0); // one token partition + h.put_literal(7, u32::from(qi.min(127))); // y_ac_qi + for _ in 0..5 { + h.put_flag(false); // no quantizer deltas + } + h.put_flag(true); // refresh_entropy_probs (meaningless for a single frame) + for t in 0..4 { + for b in 0..8 { + for c in 0..3 { + for n in 0..11 { + let changed = probs[t][b][c][n] != DEFAULT_COEFF_PROBS[t][b][c][n]; + h.put(COEFF_UPDATE_PROBS[t][b][c][n], changed); + if changed { + h.put_literal(8, u32::from(probs[t][b][c][n])); + } + } + } + } + } + h.put_flag(true); // mb_no_skip_coeff + let skipped = self.mbs.iter().filter(|m| m.skip).count() as u64; + let total = self.mbs.len().max(1) as u64; + let prob_skip_false = (((total - skipped) * 256 + total / 2) / total).clamp(1, 255) as u8; + h.put_literal(8, u32::from(prob_skip_false)); + + for mby in 0..self.mb_rows { + for mbx in 0..self.mb_cols { + let mb = &self.mbs[mby * self.mb_cols + mbx]; + h.put(prob_skip_false, mb.skip); + h.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, mb.y_mode); + if mb.y_mode == B_PRED { + for b in 0..16 { + let above = if b >= 4 { + mb.b_modes[b - 4] + } else if mby == 0 { + B_DC_PRED + } else { + self.sub_mode_of(&self.mbs[(mby - 1) * self.mb_cols + mbx], b + 12) + }; + let left = if b & 3 != 0 { + mb.b_modes[b - 1] + } else if mbx == 0 { + B_DC_PRED + } else { + self.sub_mode_of(&self.mbs[mby * self.mb_cols + mbx - 1], b + 3) + }; + h.put_tree(BMODE_TREE, &KF_BMODE_PROBS[usize::from(above)][usize::from(left)], mb.b_modes[b]); + } + } + h.put_tree(UV_MODE_TREE, &KF_UV_MODE_PROB, mb.uv_mode); + } + } + let first = h.finish(); + + // Token partition. + let mut t = BoolEncoder::new(); + self.walk_tokens( + |block, ty, ctx0| { + for_each_token_bool(block, ty, ctx0, |ev| match ev { + Event::Node(ty, band, ctx, node, bit) => t.put(probs[ty][band][ctx][node], bit), + Event::Extra(cat, bits) => { + let p = PCAT[cat]; + for (i, &prob) in p.iter().enumerate() { + let shift = p.len() - 1 - i; + t.put(prob, (bits >> shift) & 1 != 0); + } + } + Event::Sign(negative) => t.put_flag(negative), + }); + }, + |_, _| {}, + ); + let tokens = t.finish(); + + // Frame tag, start code, dimensions, partitions. + let mut out = Vec::with_capacity(10 + first.len() + tokens.len()); + let size = (first.len() as u32).min((1 << 19) - 1); + // Bit 0 = 0: key frame; bits 1-3 = 0: version 0; bit 4: show_frame; then the size. + let tag: u32 = (1 << 4) | (size << 5); + out.extend_from_slice(&tag.to_le_bytes()[..3]); + out.extend_from_slice(&[0x9d, 0x01, 0x2a]); + out.extend_from_slice(&(self.src.width as u16).to_le_bytes()); + out.extend_from_slice(&(self.src.height as u16).to_le_bytes()); + out.extend_from_slice(&first); + out.extend_from_slice(&tokens); + out + } + + /// The sub-block mode context a neighbouring macroblock provides for sub-block `b` (an + /// index into the neighbour's 16 sub-blocks): its own sub-block mode when it is `B_PRED`, + /// else the sub-block equivalent of its whole-block mode (RFC 6386 §11.3). + fn sub_mode_of(&self, mb: &Macroblock, b: usize) -> u8 { + match mb.y_mode { + B_PRED => mb.b_modes[b.min(15)], + V_PRED => B_VE_PRED, + H_PRED => B_HE_PRED, + TM_PRED => B_TM_PRED, + _ => B_DC_PRED, + } + } +} + +/// Encodes `yuv` as one VP8 key frame (the payload of a WebP `VP8 ` chunk). +pub fn encode_frame(yuv: &Yuv, params: Params) -> Result, Vp8Error> { + if yuv.width == 0 || yuv.height == 0 || yuv.width > MAX_DIMENSION || yuv.height > MAX_DIMENSION { + return Err(Vp8Error::Dimensions); + } + let (w, h) = (yuv.width as usize, yuv.height as usize); + if yuv.y.len() != w * h || yuv.u.len() != w.div_ceil(2) * h.div_ceil(2) || yuv.v.len() != yuv.u.len() { + return Err(Vp8Error::Planes); + } + let qi = quantizer_index(params.quality); + let mut enc = Encoder::new(yuv, qi); + for mby in 0..enc.mb_rows { + for mbx in 0..enc.mb_cols { + enc.encode_macroblock(mbx, mby); + } + } + // Pass 1 over the tokens: branch statistics → this frame's probabilities. + let mut stats = Stats::new(); + enc.walk_tokens( + |block, ty, ctx0| { + for_each_token_bool(block, ty, ctx0, |ev| { + if let Event::Node(ty, band, ctx, node, bit) = ev { + stats.node[ty][band][ctx][node][usize::from(bit)] += 1; + } + }); + }, + |_, _| {}, + ); + let probs = enc.choose_probs(&stats); + // The loop filter smooths block edges in proportion to the quantizer; none at the finest. + let loop_filter_level = if qi == 0 { 0 } else { (u32::from(qi) / 2 + 4).min(63) as u8 }; + Ok(enc.write_frame(&probs, loop_filter_level, qi)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn quantizer_curve_is_monotone_and_spans_the_range() { + assert_eq!(quantizer_index(100), 0); + assert_eq!(quantizer_index(0), 127); + let mut last = 0; + for q in (0..=100).rev() { + let qi = quantizer_index(q); + assert!(qi >= last, "quality {q}"); + last = qi; + } + assert!((8..=14).contains(&quantizer_index(90)), "{}", quantizer_index(90)); + assert!((24..=32).contains(&quantizer_index(75)), "{}", quantizer_index(75)); + } + + #[test] + fn tokens_cover_every_magnitude() { + for v in [0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 18, 19, 34, 35, 66, 67, 1000, 2048] { + let (tok, extra) = token_of(v); + let base = match extra { + None => i32::from(tok), + Some((c, bits)) => { + assert!(bits < (1 << PCAT[c].len()), "{v}"); + CAT_BASE[c] + bits as i32 + } + }; + assert_eq!(base, v); + assert_eq!(token_of(-v).0, tok); + } + } + + #[test] + fn block_eob_is_after_the_last_nonzero() { + let mut raster = [0i32; 16]; + let b = Block::from_raster(&raster, 1); + assert_eq!(b.eob, 1); + assert!(!b.has_coeffs()); + raster[ZIGZAG[5]] = 3; + let b = Block::from_raster(&raster, 0); + assert_eq!(b.eob, 6); + assert!(b.has_coeffs()); + raster[ZIGZAG[15]] = -1; + assert_eq!(Block::from_raster(&raster, 0).eob, 16); + } + + #[test] + fn rejects_bad_dimensions() { + let y = Yuv { width: 0, height: 4, y: vec![], u: vec![], v: vec![] }; + assert_eq!(encode_frame(&y, Params { quality: 80 }).unwrap_err(), Vp8Error::Dimensions); + let y = Yuv { width: 4, height: 4, y: vec![0; 15], u: vec![0; 4], v: vec![0; 4] }; + assert_eq!(encode_frame(&y, Params { quality: 80 }).unwrap_err(), Vp8Error::Planes); + } + + #[test] + fn frame_header_is_well_formed() { + let rgb: Vec = (0..(20 * 13 * 3)).map(|i| (i * 7 % 256) as u8).collect(); + let yuv = Yuv::from_rgb(&rgb, 20, 13, 3); + let frame = encode_frame(&yuv, Params { quality: 75 }).unwrap(); + assert_eq!(frame[0] & 1, 0, "key frame"); + assert_eq!((frame[0] >> 4) & 1, 1, "shown"); + assert_eq!(&frame[3..6], &[0x9d, 0x01, 0x2a]); + assert_eq!(u16::from_le_bytes([frame[6], frame[7]]) & 0x3fff, 20); + assert_eq!(u16::from_le_bytes([frame[8], frame[9]]) & 0x3fff, 13); + let first_size = (u32::from(frame[0]) | (u32::from(frame[1]) << 8) | (u32::from(frame[2]) << 16)) >> 5; + assert!((10 + first_size as usize) < frame.len()); + } +} diff --git a/crates/image-codecs/src/codecs/vp8/bool_encoder.rs b/crates/image-codecs/src/codecs/vp8/bool_encoder.rs new file mode 100644 index 000000000..d16341bce --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/bool_encoder.rs @@ -0,0 +1,273 @@ +//! The VP8 boolean entropy coder, writing side (RFC 6386 §7): every symbol is a bool with an +//! 8-bit probability of being zero; literals and tree-coded values are built from bools. + +use super::tables::Tree; + +/// Writes bools at given probabilities into a byte buffer. +pub struct BoolEncoder { + out: Vec, + range: u32, + bottom: u32, + bit_count: i32, +} + +impl Default for BoolEncoder { + fn default() -> Self { + Self::new() + } +} + +impl BoolEncoder { + pub fn new() -> Self { + BoolEncoder { out: Vec::new(), range: 255, bottom: 0, bit_count: 24 } + } + + /// Propagates a carry into the bytes already written (never past the start: the coded + /// value is below one). + fn add_one_to_output(&mut self) { + for b in self.out.iter_mut().rev() { + if *b == 255 { + *b = 0; + } else { + *b += 1; + return; + } + } + } + + /// Writes `bit` whose probability of being 0 is `prob / 256`. + pub fn put(&mut self, prob: u8, bit: bool) { + let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8); + if bit { + self.bottom = self.bottom.wrapping_add(split); + self.range -= split; + } else { + self.range = split; + } + while self.range < 128 { + self.range <<= 1; + if self.bottom & (1 << 31) != 0 { + self.add_one_to_output(); + } + self.bottom <<= 1; + self.bit_count -= 1; + if self.bit_count == 0 { + self.out.push((self.bottom >> 24) as u8); + self.bottom &= (1 << 24) - 1; + self.bit_count = 8; + } + } + } + + /// A bool at probability 1/2. + pub fn put_flag(&mut self, bit: bool) { + self.put(128, bit); + } + + /// An `n`-bit unsigned literal, most significant bit first, each bit at probability 1/2. + pub fn put_literal(&mut self, n: u32, value: u32) { + for i in (0..n).rev() { + self.put_flag((value >> i) & 1 != 0); + } + } + + /// A tree-coded value: the path of bools from the root to the leaf `value`, each node's bool + /// at `probs[node / 2]`. + pub fn put_tree(&mut self, tree: Tree, probs: &[u8], value: u8) { + self.put_tree_from(tree, probs, value, 0); + } + + /// [`Self::put_tree`] starting at node `start` (2 skips the first branch of the coefficient tree). + pub fn put_tree_from(&mut self, tree: Tree, probs: &[u8], value: u8, start: usize) { + // Find the path to the leaf by searching the tree; trees are tiny (≤ 22 entries). + let mut path = Vec::with_capacity(8); + if !Self::find(tree, start, value, &mut path) { + return; + } + for (node, bit) in path { + self.put(probs.get(node >> 1).copied().unwrap_or(128), bit); + } + } + + fn find(tree: Tree, node: usize, value: u8, path: &mut Vec<(usize, bool)>) -> bool { + for bit in [false, true] { + let i = node + usize::from(bit); + let Some(&entry) = tree.get(i) else { continue }; + path.push((node, bit)); + if entry <= 0 { + if (-entry) as u8 == value { + return true; + } + } else if Self::find(tree, entry as usize, value, path) { + return true; + } + path.pop(); + } + false + } + + /// Finishes the partition and returns its bytes. + pub fn finish(mut self) -> Vec { + let mut c = self.bit_count; + let mut v = self.bottom; + if c <= 32 && v & (1u32 << (32 - c)) != 0 { + self.add_one_to_output(); + } + v <<= (c & 7) as u32; + c >>= 3; + while c > 0 { + v <<= 8; + c -= 1; + } + for _ in 0..4 { + self.out.push((v >> 24) as u8); + v <<= 8; + } + self.out + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The decoder from RFC 6386 §7.3, for the round-trip tests. + struct BoolDecoder<'a> { + input: &'a [u8], + pos: usize, + range: u32, + value: u32, + bit_count: i32, + } + + impl<'a> BoolDecoder<'a> { + fn new(input: &'a [u8]) -> Self { + let mut value = 0; + for i in 0..2 { + value = (value << 8) | u32::from(input.get(i).copied().unwrap_or(0)); + } + BoolDecoder { input, pos: 2, range: 255, value, bit_count: 0 } + } + fn get(&mut self, prob: u8) -> bool { + let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8); + let big_split = split << 8; + let bit = if self.value >= big_split { + self.range -= split; + self.value -= big_split; + true + } else { + self.range = split; + false + }; + while self.range < 128 { + self.value <<= 1; + self.range <<= 1; + self.bit_count += 1; + if self.bit_count == 8 { + self.bit_count = 0; + self.value |= u32::from(self.input.get(self.pos).copied().unwrap_or(0)); + self.pos += 1; + } + } + bit + } + fn literal(&mut self, n: u32) -> u32 { + (0..n).fold(0, |v, _| (v << 1) | u32::from(self.get(128))) + } + fn tree(&mut self, tree: Tree, probs: &[u8], start: usize) -> u8 { + let mut i = start; + loop { + let bit = self.get(probs[i >> 1]); + let entry = tree[i + usize::from(bit)]; + if entry <= 0 { + return (-entry) as u8; + } + i = entry as usize; + } + } + } + + #[test] + fn bools_round_trip_at_every_probability() { + let mut rng = 0x1234_5678_9abc_def0u64; + let mut next = || { + rng ^= rng << 13; + rng ^= rng >> 7; + rng ^= rng << 17; + rng + }; + let symbols: Vec<(u8, bool)> = (0..20_000) + .map(|_| { + let r = next(); + let prob = (r & 0xff) as u8; + // Bits follow their probability, roughly, so both paths are exercised. + ((prob.max(1)), (r >> 8) & 0xff >= u64::from(prob)) + }) + .collect(); + let mut e = BoolEncoder::new(); + for &(p, b) in &symbols { + e.put(p, b); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + for &(p, b) in &symbols { + assert_eq!(d.get(p), b); + } + } + + #[test] + fn literals_and_trees_round_trip() { + use super::super::tables::*; + let mut e = BoolEncoder::new(); + e.put_literal(7, 93); + e.put_literal(19, 0x7_ffff); + e.put_literal(1, 0); + e.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, TM_PRED); + e.put_tree(KF_YMODE_TREE, &KF_YMODE_PROB, B_PRED); + e.put_tree(UV_MODE_TREE, &KF_UV_MODE_PROB, H_PRED); + for m in 0..10u8 { + e.put_tree(BMODE_TREE, &KF_BMODE_PROBS[3][7], m); + } + let probs = [200u8, 150, 100, 128, 128, 128, 128, 128, 128, 128, 128]; + for t in 0..12u8 { + e.put_tree(COEFF_TREE, &probs, t); + } + // From node 2 (after a zero token) the end-of-block leaf is unreachable: tokens 0..=10. + for t in 0..11u8 { + e.put_tree_from(COEFF_TREE, &probs, t, 2); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + assert_eq!(d.literal(7), 93); + assert_eq!(d.literal(19), 0x7_ffff); + assert_eq!(d.literal(1), 0); + assert_eq!(d.tree(KF_YMODE_TREE, &KF_YMODE_PROB, 0), TM_PRED); + assert_eq!(d.tree(KF_YMODE_TREE, &KF_YMODE_PROB, 0), B_PRED); + assert_eq!(d.tree(UV_MODE_TREE, &KF_UV_MODE_PROB, 0), H_PRED); + for m in 0..10u8 { + assert_eq!(d.tree(BMODE_TREE, &KF_BMODE_PROBS[3][7], 0), m); + } + for t in 0..12u8 { + assert_eq!(d.tree(COEFF_TREE, &probs, 0), t); + } + for t in 0..11u8 { + assert_eq!(d.tree(COEFF_TREE, &probs, 2), t); + } + } + + #[test] + fn carry_propagates_through_full_bytes() { + // Many ones at a tiny probability push the interval up and force carries. + let mut e = BoolEncoder::new(); + for _ in 0..3000 { + e.put(1, true); + e.put(255, false); + } + let bytes = e.finish(); + let mut d = BoolDecoder::new(&bytes); + for _ in 0..3000 { + assert!(d.get(1)); + assert!(!d.get(255)); + } + } +} diff --git a/crates/image-codecs/src/codecs/vp8/predict.rs b/crates/image-codecs/src/codecs/vp8/predict.rs new file mode 100644 index 000000000..7ddea0423 --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/predict.rs @@ -0,0 +1,382 @@ +//! Intra prediction (RFC 6386 §12): whole-macroblock luma and chroma modes, and the ten 4×4 +//! sub-block modes. Predictions read the frame's *unfiltered* reconstruction, which is what the +//! decoder predicts from too (the loop filter runs on the finished frame). + +#[cfg(test)] +use super::tables::{B_DC_PRED, DC_PRED}; +use super::tables::{B_HD_PRED, B_HE_PRED, B_HU_PRED, B_LD_PRED, B_RD_PRED, B_TM_PRED, B_VE_PRED, B_VL_PRED, B_VR_PRED, H_PRED, TM_PRED, V_PRED}; + +/// A plane of the reconstruction with the virtual border VP8 defines: the row above the frame +/// reads as 127, the column left of it as 129, and the pixel above-left of the frame as 127. +pub struct Plane { + pub data: Vec, + pub width: usize, +} + +impl Plane { + pub fn new(width: usize, height: usize) -> Self { + Plane { data: vec![0; width * height], width } + } + + #[inline] + pub fn at(&self, x: isize, y: isize) -> u8 { + if y < 0 { + 127 + } else if x < 0 { + 129 + } else { + self.data.get(y as usize * self.width + x as usize).copied().unwrap_or(127) + } + } + + /// The row above an `n`-wide block at (`x`, `y`), plus the four pixels to its upper right + /// (sub-block modes use them; `avail_right` says whether those exist in the frame yet). + pub fn above(&self, x: usize, y: usize, n: usize, out: &mut [u8]) { + for (i, o) in out.iter_mut().enumerate().take(n) { + *o = self.at((x + i) as isize, y as isize - 1); + } + } + + pub fn left(&self, x: usize, y: usize, n: usize, out: &mut [u8]) { + for (i, o) in out.iter_mut().enumerate().take(n) { + *o = self.at(x as isize - 1, (y + i) as isize); + } + } + + /// The pixel above-left of (`x`, `y`). + pub fn corner(&self, x: usize, y: usize) -> u8 { + self.at(x as isize - 1, y as isize - 1) + } + + #[inline] + pub fn set(&mut self, x: usize, y: usize, v: u8) { + if let Some(p) = self.data.get_mut(y * self.width + x) { + *p = v; + } + } +} + +#[inline] +fn clamp255(v: i32) -> u8 { + v.clamp(0, 255) as u8 +} + +/// Whole-block prediction of an `n × n` block at (`x`, `y`) in `plane` (16 for luma, 8 for chroma) +/// with one of DC / V / H / TM, written row-major into `out`. +pub fn predict_block(plane: &Plane, x: usize, y: usize, n: usize, mode: u8, out: &mut [u8]) { + let mut above = [0u8; 16]; + let mut left = [0u8; 16]; + plane.above(x, y, n, &mut above); + plane.left(x, y, n, &mut left); + let top_row = y == 0; + let left_col = x == 0; + match mode { + V_PRED => { + for r in 0..n { + out[r * n..r * n + n].copy_from_slice(&above[..n]); + } + } + H_PRED => { + for r in 0..n { + out[r * n..r * n + n].fill(left[r]); + } + } + TM_PRED => { + let p = i32::from(plane.corner(x, y)); + for r in 0..n { + for c in 0..n { + out[r * n + c] = clamp255(i32::from(left[r]) + i32::from(above[c]) - p); + } + } + } + _ => { + // DC: the average of the visible edge pixels; 128 in the top-left corner. + let shift = if n == 16 { 4 } else { 3 }; + let v = match (top_row, left_col) { + (true, true) => 128, + (true, false) => { + let s: u32 = left[..n].iter().map(|&v| u32::from(v)).sum(); + ((s + (1 << (shift - 1))) >> shift) as u8 + } + (false, true) => { + let s: u32 = above[..n].iter().map(|&v| u32::from(v)).sum(); + ((s + (1 << (shift - 1))) >> shift) as u8 + } + (false, false) => { + let s: u32 = above[..n].iter().chain(&left[..n]).map(|&v| u32::from(v)).sum(); + ((s + (1 << shift)) >> (shift + 1)) as u8 + } + }; + out[..n * n].fill(v); + } + } +} + +#[inline] +fn avg2(a: u8, b: u8) -> u8 { + ((u16::from(a) + u16::from(b) + 1) >> 1) as u8 +} + +#[inline] +fn avg3(a: u8, b: u8, c: u8) -> u8 { + ((u16::from(a) + 2 * u16::from(b) + u16::from(c) + 2) >> 2) as u8 +} + +/// The edge pixels a 4×4 sub-block prediction reads: `above[0..8]` (the four above and the four +/// above-right), `left[0..4]`, and the corner `p` above-left. +pub struct SubEdges { + pub above: [u8; 8], + pub left: [u8; 4], + pub p: u8, +} + +impl SubEdges { + /// Edges of sub-block `b` (0..16, raster) of the macroblock at (`mx`, `my`) in luma pixels. + /// The above-right pixels of the right-column sub-blocks come from the row above the + /// macroblock (positions (-1, 16..20)), replicated from (-1, 15) for the rightmost macroblock + /// (RFC 6386 §12.3); for the top macroblock row they read 127 like every above pixel. + pub fn gather(plane: &Plane, mx: usize, my: usize, b: usize, mb_cols: usize) -> SubEdges { + let bx = mx + (b & 3) * 4; + let by = my + (b >> 2) * 4; + let mut above = [0u8; 8]; + let mut left = [0u8; 4]; + plane.above(bx, by, 4, &mut above); + plane.left(bx, by, 4, &mut left); + let p = plane.corner(bx, by); + if b & 3 == 3 { + // Right column: the above-right pixels are those above the macroblock's right edge. + let last_col = mx / 16 + 1 == mb_cols; + for i in 0..4 { + let ax = if last_col { mx as isize + 15 } else { mx as isize + 16 + i as isize }; + above[4 + i] = plane.at(ax, my as isize - 1); + } + } else { + for i in 0..4 { + above[4 + i] = plane.at((bx + 4 + i) as isize, by as isize - 1); + } + } + SubEdges { above, left, p } + } +} + +/// Predicts one 4×4 sub-block with `mode` into `out` (row-major), RFC 6386 §12.3. +// Written as the specification's position-by-position assignments, which index on purpose. +#[allow(clippy::needless_range_loop)] +pub fn predict_sub(e: &SubEdges, mode: u8, out: &mut [u8; 16]) { + let a = &e.above; + let l = &e.left; + // E: the nine edge pixels from bottom-left, through the corner, to the right. + let ed = [l[3], l[2], l[1], l[0], e.p, a[0], a[1], a[2], a[3]]; + // avg3 centred on E[i], avg2 of E[i] and E[i + 1]. + let e3 = |i: usize| avg3(ed[i - 1], ed[i], ed[i + 1]); + let e2 = |i: usize| avg2(ed[i], ed[i + 1]); + // The same over the above row (A[-1] is P). + let ap = |i: isize| if i < 0 { e.p } else { a[i as usize] }; + let a3 = |i: isize| avg3(ap(i - 1), ap(i), ap(i + 1)); + let a2 = |i: isize| avg2(ap(i), ap(i + 1)); + let lp = |i: isize| if i < 0 { e.p } else { l[i as usize] }; + let l3 = |i: isize| avg3(lp(i - 1), lp(i), lp(i + 1)); + let l2 = |i: isize| avg2(lp(i), lp(i + 1)); + let mut b = [[0u8; 4]; 4]; + match mode { + B_TM_PRED => { + for r in 0..4 { + for c in 0..4 { + b[r][c] = clamp255(i32::from(l[r]) + i32::from(a[c]) - i32::from(e.p)); + } + } + } + B_VE_PRED => { + for c in 0..4 { + let v = a3(c as isize); + for r in 0..4 { + b[r][c] = v; + } + } + } + B_HE_PRED => { + let rows = [l3(0), l3(1), l3(2), avg3(l[2], l[3], l[3])]; + for r in 0..4 { + b[r] = [rows[r]; 4]; + } + } + B_LD_PRED => { + b[0][0] = a3(1); + b[0][1] = a3(2); + b[1][0] = b[0][1]; + b[0][2] = a3(3); + b[1][1] = b[0][2]; + b[2][0] = b[0][2]; + b[0][3] = a3(4); + b[1][2] = b[0][3]; + b[2][1] = b[0][3]; + b[3][0] = b[0][3]; + b[1][3] = a3(5); + b[2][2] = b[1][3]; + b[3][1] = b[1][3]; + b[2][3] = a3(6); + b[3][2] = b[2][3]; + b[3][3] = avg3(a[6], a[7], a[7]); + } + B_RD_PRED => { + b[3][0] = e3(1); + b[3][1] = e3(2); + b[2][0] = b[3][1]; + b[3][2] = e3(3); + b[2][1] = b[3][2]; + b[1][0] = b[3][2]; + b[3][3] = e3(4); + b[2][2] = b[3][3]; + b[1][1] = b[3][3]; + b[0][0] = b[3][3]; + b[2][3] = e3(5); + b[1][2] = b[2][3]; + b[0][1] = b[2][3]; + b[1][3] = e3(6); + b[0][2] = b[1][3]; + b[0][3] = e3(7); + } + B_VR_PRED => { + b[3][0] = e3(2); + b[2][0] = e3(3); + b[3][1] = e3(4); + b[1][0] = b[3][1]; + b[2][1] = e2(4); + b[0][0] = b[2][1]; + b[3][2] = e3(5); + b[1][1] = b[3][2]; + b[2][2] = e2(5); + b[0][1] = b[2][2]; + b[3][3] = e3(6); + b[1][2] = b[3][3]; + b[2][3] = e2(6); + b[0][2] = b[2][3]; + b[1][3] = e3(7); + b[0][3] = e2(7); + } + B_VL_PRED => { + b[0][0] = a2(0); + b[1][0] = a3(1); + b[2][0] = a2(1); + b[0][1] = b[2][0]; + b[1][1] = a3(2); + b[3][0] = b[1][1]; + b[2][1] = a2(2); + b[0][2] = b[2][1]; + b[3][1] = a3(3); + b[1][2] = b[3][1]; + b[2][2] = a2(3); + b[0][3] = b[2][2]; + b[3][2] = a3(4); + b[1][3] = b[3][2]; + b[2][3] = a3(5); + b[3][3] = a3(6); + } + B_HD_PRED => { + b[3][0] = e2(0); + b[3][1] = e3(1); + b[2][0] = e2(1); + b[3][2] = b[2][0]; + b[2][1] = e3(2); + b[3][3] = b[2][1]; + b[2][2] = e2(2); + b[1][0] = b[2][2]; + b[2][3] = e3(3); + b[1][1] = b[2][3]; + b[1][2] = e2(3); + b[0][0] = b[1][2]; + b[1][3] = e3(4); + b[0][1] = b[1][3]; + b[0][2] = e3(5); + b[0][3] = e3(6); + } + B_HU_PRED => { + b[0][0] = l2(0); + b[0][1] = l3(1); + b[0][2] = l2(1); + b[1][0] = b[0][2]; + b[0][3] = l3(2); + b[1][1] = b[0][3]; + b[1][2] = l2(2); + b[2][0] = b[1][2]; + b[1][3] = avg3(l[2], l[3], l[3]); + b[2][1] = b[1][3]; + b[2][2] = l[3]; + b[2][3] = l[3]; + b[3] = [l[3]; 4]; + } + _ => { + // B_DC_PRED: the average of the four above and four left pixels. + let s: u32 = a[..4].iter().chain(&l[..]).map(|&v| u32::from(v)).sum(); + b = [[((s + 4) >> 3) as u8; 4]; 4]; + } + } + for r in 0..4 { + out[r * 4..r * 4 + 4].copy_from_slice(&b[r]); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn border_values() { + let p = Plane::new(16, 16); + assert_eq!(p.at(-1, -1), 127); + assert_eq!(p.at(5, -1), 127); + assert_eq!(p.at(-1, 5), 129); + assert_eq!(p.corner(0, 0), 127); + assert_eq!(p.corner(0, 16), 129); + assert_eq!(p.corner(16, 0), 127); + } + + #[test] + fn dc_prediction_edge_cases() { + let mut p = Plane::new(32, 32); + for y in 0..32 { + for x in 0..32 { + p.set(x, y, if y < 16 { 10 } else { 200 }); + } + } + let mut out = [0u8; 256]; + predict_block(&p, 0, 0, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 128), "top-left is 128"); + predict_block(&p, 16, 0, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 10), "top row averages the left column only"); + predict_block(&p, 0, 16, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == 10), "left column averages the above row only"); + predict_block(&p, 16, 16, 16, DC_PRED, &mut out); + assert!(out.iter().all(|&v| v == (10 + 200) / 2)); + predict_block(&p, 16, 16, 16, TM_PRED, &mut out); + // L + A - P = 200 + 10 - 10 = 200 everywhere. + assert!(out.iter().all(|&v| v == 200)); + } + + #[test] + fn sub_block_modes_are_in_range_and_dc_averages() { + let mut p = Plane::new(32, 32); + for y in 0..32 { + for x in 0..32 { + p.set(x, y, ((x * 7 + y * 13) % 256) as u8); + } + } + for b in 0..16 { + let e = SubEdges::gather(&p, 16, 16, b, 2); + let mut out = [0u8; 16]; + for mode in 0..10u8 { + predict_sub(&e, mode, &mut out); + } + predict_sub(&e, B_DC_PRED, &mut out); + let s: u32 = e.above[..4].iter().chain(&e.left).map(|&v| u32::from(v)).sum(); + assert!(out.iter().all(|&v| u32::from(v) == (s + 4) >> 3)); + } + // Rightmost macroblock: the above-right pixels replicate (-1, 15). + let e = SubEdges::gather(&p, 16, 16, 3, 2); + assert!(e.above[4..].iter().all(|&v| v == p.at(31, 15))); + // Top row: everything above is 127. + let e = SubEdges::gather(&p, 0, 0, 3, 2); + assert!(e.above.iter().all(|&v| v == 127)); + assert_eq!(e.p, 127); + } +} diff --git a/crates/image-codecs/src/codecs/vp8/tables.rs b/crates/image-codecs/src/codecs/vp8/tables.rs new file mode 100644 index 000000000..261231e9e --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/tables.rs @@ -0,0 +1,593 @@ +//! Constant tables of the VP8 bitstream, as published in RFC 6386. The four large tables were +//! generated from the RFC text (sections 11.5, 13.4, 13.5 and 14.1); the small ones are +//! transcribed from sections 11.2, 11.4, 13.2 and 13.3. + +/// Luma intra modes of a macroblock (RFC 6386 §11.2), in the order of `intra_mbmode`. +pub const DC_PRED: u8 = 0; +pub const V_PRED: u8 = 1; +pub const H_PRED: u8 = 2; +pub const TM_PRED: u8 = 3; +pub const B_PRED: u8 = 4; + +/// Sub-block intra modes (RFC 6386 §11.2), in the order of `intra_bmode`. +pub const B_DC_PRED: u8 = 0; +pub const B_TM_PRED: u8 = 1; +pub const B_VE_PRED: u8 = 2; +pub const B_HE_PRED: u8 = 3; +pub const B_LD_PRED: u8 = 4; +pub const B_RD_PRED: u8 = 5; +pub const B_VR_PRED: u8 = 6; +pub const B_VL_PRED: u8 = 7; +pub const B_HD_PRED: u8 = 8; +pub const B_HU_PRED: u8 = 9; + +/// A coding tree as in the RFC: positive entries index the next node pair, `-leaf` entries end +/// a path (the leaf value is negated; `-0` is written as the sentinel below). +pub type Tree = &'static [i8]; + +/// `kf_ymode_tree`: `B_PRED` = "0", then DC "100", V "101", H "110", TM "111". +pub const KF_YMODE_TREE: Tree = &[-(B_PRED as i8), 2, 4, 6, -(DC_PRED as i8), -(V_PRED as i8), -(H_PRED as i8), -(TM_PRED as i8)]; +pub const KF_YMODE_PROB: [u8; 4] = [145, 156, 163, 128]; + +/// `uv_mode_tree`: DC "0", V "10", H "110", TM "111". +pub const UV_MODE_TREE: Tree = &[-(DC_PRED as i8), 2, -(V_PRED as i8), 4, -(H_PRED as i8), -(TM_PRED as i8)]; +pub const KF_UV_MODE_PROB: [u8; 3] = [142, 114, 183]; + +/// `bmode_tree` (RFC 6386 §11.2). +pub const BMODE_TREE: Tree = &[ + -(B_DC_PRED as i8), + 2, + -(B_TM_PRED as i8), + 4, + -(B_VE_PRED as i8), + 6, + 8, + 12, + -(B_HE_PRED as i8), + 10, + -(B_RD_PRED as i8), + -(B_VR_PRED as i8), + -(B_LD_PRED as i8), + 14, + -(B_VL_PRED as i8), + 16, + -(B_HD_PRED as i8), + -(B_HU_PRED as i8), +]; + +/// Coefficient tokens (RFC 6386 §13.2). +pub const DCT_0: u8 = 0; +pub const DCT_1: u8 = 1; +pub const DCT_2: u8 = 2; +pub const DCT_3: u8 = 3; +pub const DCT_4: u8 = 4; +pub const DCT_CAT1: u8 = 5; +pub const DCT_CAT2: u8 = 6; +pub const DCT_CAT3: u8 = 7; +pub const DCT_CAT4: u8 = 8; +pub const DCT_CAT5: u8 = 9; +pub const DCT_CAT6: u8 = 10; +pub const DCT_EOB: u8 = 11; + +/// `coeff_tree` (RFC 6386 §13.2). The EOB leaf is at index 0, so the tree is also walked from +/// index 2 when an EOB cannot follow (after a zero token). +pub const COEFF_TREE: Tree = &[ + -(DCT_EOB as i8), + 2, + -(DCT_0 as i8), + 4, + -(DCT_1 as i8), + 6, + 8, + 12, + -(DCT_2 as i8), + 10, + -(DCT_3 as i8), + -(DCT_4 as i8), + 14, + 16, + -(DCT_CAT1 as i8), + -(DCT_CAT2 as i8), + 18, + 20, + -(DCT_CAT3 as i8), + -(DCT_CAT4 as i8), + -(DCT_CAT5 as i8), + -(DCT_CAT6 as i8), +]; + +/// Extra-bit probabilities of the six value categories (RFC 6386 §13.2), most significant bit +/// first; the category bases are 5, 7, 11, 19, 35 and 67. +pub const PCAT: [&[u8]; 6] = + [&[159], &[165, 145], &[173, 148, 140], &[176, 155, 140, 135], &[180, 157, 141, 134, 130], &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129]]; +pub const CAT_BASE: [i32; 6] = [5, 7, 11, 19, 35, 67]; + +/// Coefficient position → probability band (RFC 6386 §13.3). +pub const COEFF_BANDS: [usize; 16] = [0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7]; + +/// Zig-zag scan: the raster index of the coefficient coded at each position (RFC 6386 §13). +pub const ZIGZAG: [usize; 16] = [0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15]; + +/// Default token probabilities `[plane][band][context][token]` (RFC 6386 §13.5). +pub const DEFAULT_COEFF_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ + [ + [ + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [253, 136, 254, 255, 228, 219, 128, 128, 128, 128, 128], + [189, 129, 242, 255, 227, 213, 255, 219, 128, 128, 128], + [106, 126, 227, 252, 214, 209, 255, 255, 128, 128, 128], + ], + [ + [1, 98, 248, 255, 236, 226, 255, 255, 128, 128, 128], + [181, 133, 238, 254, 221, 234, 255, 154, 128, 128, 128], + [78, 134, 202, 247, 198, 180, 255, 219, 128, 128, 128], + ], + [ + [1, 185, 249, 255, 243, 255, 128, 128, 128, 128, 128], + [184, 150, 247, 255, 236, 224, 128, 128, 128, 128, 128], + [77, 110, 216, 255, 236, 230, 128, 128, 128, 128, 128], + ], + [ + [1, 101, 251, 255, 241, 255, 128, 128, 128, 128, 128], + [170, 139, 241, 252, 236, 209, 255, 255, 128, 128, 128], + [37, 116, 196, 243, 228, 255, 255, 255, 128, 128, 128], + ], + [ + [1, 204, 254, 255, 245, 255, 128, 128, 128, 128, 128], + [207, 160, 250, 255, 238, 128, 128, 128, 128, 128, 128], + [102, 103, 231, 255, 211, 171, 128, 128, 128, 128, 128], + ], + [ + [1, 152, 252, 255, 240, 255, 128, 128, 128, 128, 128], + [177, 135, 243, 255, 234, 225, 128, 128, 128, 128, 128], + [80, 129, 211, 255, 194, 224, 128, 128, 128, 128, 128], + ], + [ + [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [246, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [198, 35, 237, 223, 193, 187, 162, 160, 145, 155, 62], + [131, 45, 198, 221, 172, 176, 220, 157, 252, 221, 1], + [68, 47, 146, 208, 149, 167, 221, 162, 255, 223, 128], + ], + [ + [1, 149, 241, 255, 221, 224, 255, 255, 128, 128, 128], + [184, 141, 234, 253, 222, 220, 255, 199, 128, 128, 128], + [81, 99, 181, 242, 176, 190, 249, 202, 255, 255, 128], + ], + [ + [1, 129, 232, 253, 214, 197, 242, 196, 255, 255, 128], + [99, 121, 210, 250, 201, 198, 255, 202, 128, 128, 128], + [23, 91, 163, 242, 170, 187, 247, 210, 255, 255, 128], + ], + [ + [1, 200, 246, 255, 234, 255, 128, 128, 128, 128, 128], + [109, 178, 241, 255, 231, 245, 255, 255, 128, 128, 128], + [44, 130, 201, 253, 205, 192, 255, 255, 128, 128, 128], + ], + [ + [1, 132, 239, 251, 219, 209, 255, 165, 128, 128, 128], + [94, 136, 225, 251, 218, 190, 255, 255, 128, 128, 128], + [22, 100, 174, 245, 186, 161, 255, 199, 128, 128, 128], + ], + [ + [1, 182, 249, 255, 232, 235, 128, 128, 128, 128, 128], + [124, 143, 241, 255, 227, 234, 128, 128, 128, 128, 128], + [35, 77, 181, 251, 193, 211, 255, 205, 128, 128, 128], + ], + [ + [1, 157, 247, 255, 236, 231, 255, 255, 128, 128, 128], + [121, 141, 235, 255, 225, 227, 255, 255, 128, 128, 128], + [45, 99, 188, 251, 195, 217, 255, 224, 128, 128, 128], + ], + [ + [1, 1, 251, 255, 213, 255, 128, 128, 128, 128, 128], + [203, 1, 248, 255, 255, 128, 128, 128, 128, 128, 128], + [137, 1, 177, 255, 224, 255, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [253, 9, 248, 251, 207, 208, 255, 192, 128, 128, 128], + [175, 13, 224, 243, 193, 185, 249, 198, 255, 255, 128], + [73, 17, 171, 221, 161, 179, 236, 167, 255, 234, 128], + ], + [ + [1, 95, 247, 253, 212, 183, 255, 255, 128, 128, 128], + [239, 90, 244, 250, 211, 209, 255, 255, 128, 128, 128], + [155, 77, 195, 248, 188, 195, 255, 255, 128, 128, 128], + ], + [ + [1, 24, 239, 251, 218, 219, 255, 205, 128, 128, 128], + [201, 51, 219, 255, 196, 186, 128, 128, 128, 128, 128], + [69, 46, 190, 239, 201, 218, 255, 228, 128, 128, 128], + ], + [ + [1, 191, 251, 255, 255, 128, 128, 128, 128, 128, 128], + [223, 165, 249, 255, 213, 255, 128, 128, 128, 128, 128], + [141, 124, 248, 255, 255, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 16, 248, 255, 255, 128, 128, 128, 128, 128, 128], + [190, 36, 230, 255, 236, 255, 128, 128, 128, 128, 128], + [149, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 226, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [247, 192, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [240, 128, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [1, 134, 252, 255, 255, 128, 128, 128, 128, 128, 128], + [213, 62, 250, 255, 255, 128, 128, 128, 128, 128, 128], + [55, 93, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + [ + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + [128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], + [ + [ + [202, 24, 213, 235, 186, 191, 220, 160, 240, 175, 255], + [126, 38, 182, 232, 169, 184, 228, 174, 255, 187, 128], + [61, 46, 138, 219, 151, 178, 240, 170, 255, 216, 128], + ], + [ + [1, 112, 230, 250, 199, 191, 247, 159, 255, 255, 128], + [166, 109, 228, 252, 211, 215, 255, 174, 128, 128, 128], + [39, 77, 162, 232, 172, 180, 245, 178, 255, 255, 128], + ], + [ + [1, 52, 220, 246, 198, 199, 249, 220, 255, 255, 128], + [124, 74, 191, 243, 183, 193, 250, 221, 255, 255, 128], + [24, 71, 130, 219, 154, 170, 243, 182, 255, 255, 128], + ], + [ + [1, 182, 225, 249, 219, 240, 255, 224, 128, 128, 128], + [149, 150, 226, 252, 216, 205, 255, 171, 128, 128, 128], + [28, 108, 170, 242, 183, 194, 254, 223, 255, 255, 128], + ], + [ + [1, 81, 230, 252, 204, 203, 255, 192, 128, 128, 128], + [123, 102, 209, 247, 188, 196, 255, 233, 128, 128, 128], + [20, 95, 153, 243, 164, 173, 255, 203, 128, 128, 128], + ], + [ + [1, 222, 248, 255, 216, 213, 128, 128, 128, 128, 128], + [168, 175, 246, 252, 235, 205, 255, 255, 128, 128, 128], + [47, 116, 215, 255, 211, 212, 255, 255, 128, 128, 128], + ], + [ + [1, 121, 236, 253, 212, 214, 255, 255, 128, 128, 128], + [141, 84, 213, 252, 201, 202, 255, 219, 128, 128, 128], + [42, 80, 160, 240, 162, 185, 255, 205, 128, 128, 128], + ], + [ + [1, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [244, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + [238, 1, 255, 128, 128, 128, 128, 128, 128, 128, 128], + ], + ], +]; + +/// Probabilities with which each token probability's update flag is coded (RFC 6386 §13.4). +pub const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ + [ + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [176, 246, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [223, 241, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 244, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [234, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 246, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [239, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 253, 255, 254, 255, 255, 255, 255, 255, 255], + [250, 255, 254, 255, 254, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [217, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [225, 252, 241, 253, 255, 255, 254, 255, 255, 255, 255], + [234, 250, 241, 250, 253, 255, 253, 254, 255, 255, 255], + ], + [ + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [223, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [238, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 248, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [247, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [186, 251, 250, 255, 255, 255, 255, 255, 255, 255, 255], + [234, 251, 244, 254, 255, 255, 255, 255, 255, 255, 255], + [251, 251, 243, 253, 254, 255, 254, 255, 255, 255, 255], + ], + [ + [255, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [236, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [251, 253, 253, 254, 254, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], + [ + [ + [248, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255], + [248, 254, 249, 253, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [246, 253, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 254, 251, 254, 254, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 254, 252, 255, 255, 255, 255, 255, 255, 255, 255], + [248, 254, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 255, 254, 254, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [245, 251, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [253, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 252, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [249, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255], + [250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + [ + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], + ], + ], +]; + +/// DC dequantization factors by quantizer index (RFC 6386 §14.1). +pub const DC_QLOOKUP: [i32; 128] = [ + 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, + 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, + 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, + 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, +]; + +/// AC dequantization factors by quantizer index (RFC 6386 §14.1). +pub const AC_QLOOKUP: [i32; 128] = [ + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, + 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, + 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, + 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, +]; + +/// Key-frame sub-block mode probabilities `[above mode][left mode][tree node]` (RFC 6386 §11.5). +pub const KF_BMODE_PROBS: [[[u8; 9]; 10]; 10] = [ + [ + [231, 120, 48, 89, 115, 113, 120, 152, 112], + [152, 179, 64, 126, 170, 118, 46, 70, 95], + [175, 69, 143, 80, 85, 82, 72, 155, 103], + [56, 58, 10, 171, 218, 189, 17, 13, 152], + [144, 71, 10, 38, 171, 213, 144, 34, 26], + [114, 26, 17, 163, 44, 195, 21, 10, 173], + [121, 24, 80, 195, 26, 62, 44, 64, 85], + [170, 46, 55, 19, 136, 160, 33, 206, 71], + [63, 20, 8, 114, 114, 208, 12, 9, 226], + [81, 40, 11, 96, 182, 84, 29, 16, 36], + ], + [ + [134, 183, 89, 137, 98, 101, 106, 165, 148], + [72, 187, 100, 130, 157, 111, 32, 75, 80], + [66, 102, 167, 99, 74, 62, 40, 234, 128], + [41, 53, 9, 178, 241, 141, 26, 8, 107], + [104, 79, 12, 27, 217, 255, 87, 17, 7], + [74, 43, 26, 146, 73, 166, 49, 23, 157], + [65, 38, 105, 160, 51, 52, 31, 115, 128], + [87, 68, 71, 44, 114, 51, 15, 186, 23], + [47, 41, 14, 110, 182, 183, 21, 17, 194], + [66, 45, 25, 102, 197, 189, 23, 18, 22], + ], + [ + [88, 88, 147, 150, 42, 46, 45, 196, 205], + [43, 97, 183, 117, 85, 38, 35, 179, 61], + [39, 53, 200, 87, 26, 21, 43, 232, 171], + [56, 34, 51, 104, 114, 102, 29, 93, 77], + [107, 54, 32, 26, 51, 1, 81, 43, 31], + [39, 28, 85, 171, 58, 165, 90, 98, 64], + [34, 22, 116, 206, 23, 34, 43, 166, 73], + [68, 25, 106, 22, 64, 171, 36, 225, 114], + [34, 19, 21, 102, 132, 188, 16, 76, 124], + [62, 18, 78, 95, 85, 57, 50, 48, 51], + ], + [ + [193, 101, 35, 159, 215, 111, 89, 46, 111], + [60, 148, 31, 172, 219, 228, 21, 18, 111], + [112, 113, 77, 85, 179, 255, 38, 120, 114], + [40, 42, 1, 196, 245, 209, 10, 25, 109], + [100, 80, 8, 43, 154, 1, 51, 26, 71], + [88, 43, 29, 140, 166, 213, 37, 43, 154], + [61, 63, 30, 155, 67, 45, 68, 1, 209], + [142, 78, 78, 16, 255, 128, 34, 197, 171], + [41, 40, 5, 102, 211, 183, 4, 1, 221], + [51, 50, 17, 168, 209, 192, 23, 25, 82], + ], + [ + [125, 98, 42, 88, 104, 85, 117, 175, 82], + [95, 84, 53, 89, 128, 100, 113, 101, 45], + [75, 79, 123, 47, 51, 128, 81, 171, 1], + [57, 17, 5, 71, 102, 57, 53, 41, 49], + [115, 21, 2, 10, 102, 255, 166, 23, 6], + [38, 33, 13, 121, 57, 73, 26, 1, 85], + [41, 10, 67, 138, 77, 110, 90, 47, 114], + [101, 29, 16, 10, 85, 128, 101, 196, 26], + [57, 18, 10, 102, 102, 213, 34, 20, 43], + [117, 20, 15, 36, 163, 128, 68, 1, 26], + ], + [ + [138, 31, 36, 171, 27, 166, 38, 44, 229], + [67, 87, 58, 169, 82, 115, 26, 59, 179], + [63, 59, 90, 180, 59, 166, 93, 73, 154], + [40, 40, 21, 116, 143, 209, 34, 39, 175], + [57, 46, 22, 24, 128, 1, 54, 17, 37], + [47, 15, 16, 183, 34, 223, 49, 45, 183], + [46, 17, 33, 183, 6, 98, 15, 32, 183], + [65, 32, 73, 115, 28, 128, 23, 128, 205], + [40, 3, 9, 115, 51, 192, 18, 6, 223], + [87, 37, 9, 115, 59, 77, 64, 21, 47], + ], + [ + [104, 55, 44, 218, 9, 54, 53, 130, 226], + [64, 90, 70, 205, 40, 41, 23, 26, 57], + [54, 57, 112, 184, 5, 41, 38, 166, 213], + [30, 34, 26, 133, 152, 116, 10, 32, 134], + [75, 32, 12, 51, 192, 255, 160, 43, 51], + [39, 19, 53, 221, 26, 114, 32, 73, 255], + [31, 9, 65, 234, 2, 15, 1, 118, 73], + [88, 31, 35, 67, 102, 85, 55, 186, 85], + [56, 21, 23, 111, 59, 205, 45, 37, 192], + [55, 38, 70, 124, 73, 102, 1, 34, 98], + ], + [ + [102, 61, 71, 37, 34, 53, 31, 243, 192], + [69, 60, 71, 38, 73, 119, 28, 222, 37], + [68, 45, 128, 34, 1, 47, 11, 245, 171], + [62, 17, 19, 70, 146, 85, 55, 62, 70], + [75, 15, 9, 9, 64, 255, 184, 119, 16], + [37, 43, 37, 154, 100, 163, 85, 160, 1], + [63, 9, 92, 136, 28, 64, 32, 201, 85], + [86, 6, 28, 5, 64, 255, 25, 248, 1], + [56, 8, 17, 132, 137, 255, 55, 116, 128], + [58, 15, 20, 82, 135, 57, 26, 121, 40], + ], + [ + [164, 50, 31, 137, 154, 133, 25, 35, 218], + [51, 103, 44, 131, 131, 123, 31, 6, 158], + [86, 40, 64, 135, 148, 224, 45, 183, 128], + [22, 26, 17, 131, 240, 154, 14, 1, 209], + [83, 12, 13, 54, 192, 255, 68, 47, 28], + [45, 16, 21, 91, 64, 222, 7, 1, 197], + [56, 21, 39, 155, 60, 138, 23, 102, 213], + [85, 26, 85, 85, 128, 128, 32, 146, 171], + [18, 11, 7, 63, 144, 171, 4, 4, 246], + [35, 27, 10, 146, 174, 171, 12, 26, 128], + ], + [ + [190, 80, 35, 99, 180, 80, 126, 54, 45], + [85, 126, 47, 87, 176, 51, 41, 20, 32], + [101, 75, 128, 139, 118, 146, 116, 128, 85], + [56, 41, 15, 176, 236, 85, 37, 9, 62], + [146, 36, 19, 30, 171, 255, 97, 27, 20], + [71, 30, 17, 119, 118, 255, 17, 18, 138], + [101, 38, 60, 138, 55, 70, 43, 26, 142], + [138, 45, 61, 62, 219, 1, 81, 188, 64], + [32, 41, 20, 117, 151, 142, 20, 21, 163], + [112, 19, 12, 61, 195, 128, 48, 4, 24], + ], +]; diff --git a/crates/image-codecs/src/codecs/vp8/transform.rs b/crates/image-codecs/src/codecs/vp8/transform.rs new file mode 100644 index 000000000..398bf7130 --- /dev/null +++ b/crates/image-codecs/src/codecs/vp8/transform.rs @@ -0,0 +1,183 @@ +//! The 4×4 transforms. The inverse DCT and inverse WHT are the bit-exact procedures of RFC 6386 +//! §14.3 and §14.4 (the encoder reconstructs with them so its prediction matches the decoder's). +//! The forward transforms are their derived counterparts: the inverse DCT is twice the +//! orthonormal transform per dimension and then `>> 3`, so coefficients are twice the +//! orthonormal DCT of the residual; the inverse WHT is an unnormalized Hadamard pair `>> 3`, so +//! the Y2 input is half the Hadamard pair of the DC values. + +/// `sqrt(2) * cos(pi / 8) - 1` and `sqrt(2) * sin(pi / 8)` in 16.16 fixed point (RFC 6386 §14.4). +const C1: i32 = 20091; +const S1: i32 = 35468; + +/// Residual (16 values, row-major) → coefficients (row-major; the zig-zag is applied when coding). +pub fn fdct(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + // Columns first (so that the row pass mirrors the decoder's order of rounding steps). + for i in 0..4 { + let (x0, x1, x2, x3) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a = x0 + x3; + let b = x1 + x2; + let c = x1 - x2; + let d = x0 - x3; + tmp[i] = a + b; + tmp[8 + i] = a - b; + // X1 = d * sqrt2cos(pi/8) + c * sqrt2sin(pi/8); X3 = d * sqrt2sin(pi/8) - c * sqrt2cos(pi/8). + tmp[4 + i] = ((d * (65536 + C1) + c * S1 + 32768) >> 16).clamp(-32768, 32767); + tmp[12 + i] = ((d * S1 - c * (65536 + C1) + 32768) >> 16).clamp(-32768, 32767); + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (x0, x1, x2, x3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a = x0 + x3; + let b = x1 + x2; + let c = x1 - x2; + let d = x0 - x3; + // Two passes give 4× the orthonormal transform; the coefficients are 2× it. + out[r * 4] = (a + b + 1) >> 1; + out[r * 4 + 2] = (a - b + 1) >> 1; + out[r * 4 + 1] = ((d * (65536 + C1) + c * S1 + 65536) >> 17).clamp(-32768, 32767); + out[r * 4 + 3] = ((d * S1 - c * (65536 + C1) + 65536) >> 17).clamp(-32768, 32767); + } + out +} + +/// Dequantized coefficients → residual, bit-exact with the decoder (RFC 6386 §14.4). +pub fn idct(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let (ip0, ip4, ip8, ip12) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a1 = ip0 + ip8; + let b1 = ip0 - ip8; + let temp1 = (ip4 * S1) >> 16; + let temp2 = ip12 + ((ip12 * C1) >> 16); + let c1 = temp1 - temp2; + let temp1 = ip4 + ((ip4 * C1) >> 16); + let temp2 = (ip12 * S1) >> 16; + let d1 = temp1 + temp2; + tmp[i] = a1 + d1; + tmp[12 + i] = a1 - d1; + tmp[4 + i] = b1 + c1; + tmp[8 + i] = b1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (ip0, ip1, ip2, ip3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a1 = ip0 + ip2; + let b1 = ip0 - ip2; + let temp1 = (ip1 * S1) >> 16; + let temp2 = ip3 + ((ip3 * C1) >> 16); + let c1 = temp1 - temp2; + let temp1 = ip1 + ((ip1 * C1) >> 16); + let temp2 = (ip3 * S1) >> 16; + let d1 = temp1 + temp2; + out[r * 4] = (a1 + d1 + 4) >> 3; + out[r * 4 + 3] = (a1 - d1 + 4) >> 3; + out[r * 4 + 1] = (b1 + c1 + 4) >> 3; + out[r * 4 + 2] = (b1 - c1 + 4) >> 3; + } + out +} + +/// The 16 luma DC values (row-major by sub-block) → Y2 coefficients. +pub fn fwht(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let (x0, x1, x2, x3) = (input[i], input[4 + i], input[8 + i], input[12 + i]); + let a1 = x0 + x3; + let b1 = x1 + x2; + let c1 = x1 - x2; + let d1 = x0 - x3; + tmp[i] = a1 + b1; + tmp[4 + i] = c1 + d1; + tmp[8 + i] = a1 - b1; + tmp[12 + i] = d1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let (x0, x1, x2, x3) = (tmp[r * 4], tmp[r * 4 + 1], tmp[r * 4 + 2], tmp[r * 4 + 3]); + let a1 = x0 + x3; + let b1 = x1 + x2; + let c1 = x1 - x2; + let d1 = x0 - x3; + // Half the unnormalized pair, rounded to nearest (ties away from zero). + let half = |v: i32| (v + v.signum()) / 2; + out[r * 4] = half(a1 + b1); + out[r * 4 + 1] = half(c1 + d1); + out[r * 4 + 2] = half(a1 - b1); + out[r * 4 + 3] = half(d1 - c1); + } + out +} + +/// Dequantized Y2 coefficients → the 16 luma DC values, bit-exact with the decoder (RFC 6386 §14.3). +pub fn iwht(input: &[i32; 16]) -> [i32; 16] { + let mut tmp = [0i32; 16]; + for i in 0..4 { + let a1 = input[i] + input[12 + i]; + let b1 = input[4 + i] + input[8 + i]; + let c1 = input[4 + i] - input[8 + i]; + let d1 = input[i] - input[12 + i]; + tmp[i] = a1 + b1; + tmp[4 + i] = c1 + d1; + tmp[8 + i] = a1 - b1; + tmp[12 + i] = d1 - c1; + } + let mut out = [0i32; 16]; + for r in 0..4 { + let a1 = tmp[r * 4] + tmp[r * 4 + 3]; + let b1 = tmp[r * 4 + 1] + tmp[r * 4 + 2]; + let c1 = tmp[r * 4 + 1] - tmp[r * 4 + 2]; + let d1 = tmp[r * 4] - tmp[r * 4 + 3]; + out[r * 4] = (a1 + b1 + 3) >> 3; + out[r * 4 + 1] = (c1 + d1 + 3) >> 3; + out[r * 4 + 2] = (a1 - b1 + 3) >> 3; + out[r * 4 + 3] = (d1 - c1 + 3) >> 3; + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn lcg(seed: &mut u64) -> i32 { + *seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + ((*seed >> 33) % 511) as i32 - 255 + } + + #[test] + fn dct_inverts_within_rounding() { + let mut seed = 7; + for _ in 0..2000 { + let input: [i32; 16] = std::array::from_fn(|_| lcg(&mut seed)); + let back = idct(&fdct(&input)); + for (a, b) in input.iter().zip(&back) { + assert!((a - b).abs() <= 1, "{input:?} -> {back:?}"); + } + } + } + + #[test] + fn dct_of_a_constant_block_is_a_dc_only_block() { + let c = fdct(&[37; 16]); + assert_eq!(c[0], 37 * 8, "DC is 8× the mean (2× orthonormal, 4 × 4 samples)"); + assert!(c[1..].iter().all(|&v| v == 0)); + assert_eq!(idct(&c), [37; 16]); + } + + #[test] + fn wht_inverts_exactly_in_the_decoders_range() { + let mut seed = 11; + for _ in 0..2000 { + let input: [i32; 16] = std::array::from_fn(|_| lcg(&mut seed) * 8); + let back = iwht(&fwht(&input)); + for (a, b) in input.iter().zip(&back) { + assert!((a - b).abs() <= 1, "{input:?} -> {back:?}"); + } + } + let c = fwht(&[80; 16]); + assert_eq!(c[0], 80 * 8); + assert!(c[1..].iter().all(|&v| v == 0)); + assert_eq!(iwht(&c), [80; 16]); + } +} diff --git a/crates/image-codecs/src/codecs/webp.rs b/crates/image-codecs/src/codecs/webp.rs new file mode 100644 index 000000000..3f69d3c9c --- /dev/null +++ b/crates/image-codecs/src/codecs/webp.rs @@ -0,0 +1,254 @@ +//! WebP: decode lossy/lossless (first frame) via `image-webp`; encode lossless via +//! `image-webp`'s VP8L encoder, or lossy with our own VP8 encoder (`super::vp8`) wrapped in the +//! RIFF container written here (a bare `VP8 ` chunk, or `VP8X` with `ALPH` alpha and `ICCP` / +//! `EXIF` / `XMP ` chunks). ICC/EXIF/XMP both ways. + +use std::io::Cursor; + +use crate::Format; +use crate::error::CodecError; +use crate::fidelity::Plan; +use crate::image::{ChannelLayout, DecodeWarning, Image, Metadata, SampleType}; +use crate::options::{EncodeOptions, Limits, alloc_zeroed}; + +const F: Format = Format::WebP; + +fn err(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(F, e) +} + +type Decoder<'a> = image_webp::WebPDecoder>; + +/// The decoder with its chunks read, the stored size and the output layout, the size checked. +fn open<'a>(bytes: &'a [u8], limits: &Limits) -> Result<(Decoder<'a>, u32, u32, ChannelLayout), CodecError> { + let mut dec = image_webp::WebPDecoder::new(Cursor::new(bytes)).map_err(err)?; + dec.set_memory_limit(limits.alloc_usize()); + let (w, h) = dec.dimensions(); + let layout = if dec.has_alpha() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + limits.check(w, h, layout, SampleType::U8)?; + // VP8L back-references and VP8 skipped macroblocks compress flat areas extremely well, so the + // ratio is generous; it still stops a header-only file from asking for a gigabyte. + Limits::check_plausible(u64::from(w) * u64::from(h) * layout.channels() as u64, bytes.len(), 1 << 16)?; + Ok((dec, w, h, layout)) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + let (mut dec, w, h, layout) = open(bytes, limits)?; + let size = dec.output_buffer_size().ok_or_else(|| err("image too large"))?; + let mut buf = alloc_zeroed::(size)?; + dec.read_image(&mut buf).map_err(err)?; + let icc = dec.icc_profile().ok().flatten(); + let exif = dec.exif_metadata().ok().flatten(); + let xmp = dec.xmp_metadata().ok().flatten().and_then(|b| String::from_utf8(b).ok()); + let mut img = Image::from_u8(w, h, layout, buf)?; + img.icc = icc; + img.meta = Metadata { exif, xmp, ..Default::default() }; + if dec.is_animated() && dec.num_frames() > 1 { + img.warnings.push(DecodeWarning::MoreFrames { total: Some(dec.num_frames()) }); + } + Ok(img) +} + +/// The stored size and EXIF block [`decode`] would find, from the container's chunks alone: the +/// same header reads and size checks, without decoding the bitstream. +pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32, Option>), CodecError> { + let (mut dec, w, h, _) = open(bytes, limits)?; + Ok((w, h, dec.exif_metadata().ok().flatten())) +} + +/// Whether `bytes` is an animated WebP of more than one frame. +pub(crate) fn is_animated(bytes: &[u8]) -> bool { + image_webp::WebPDecoder::new(Cursor::new(bytes)).is_ok_and(|d| d.is_animated() && d.num_frames() > 1) +} + +/// Every frame of an animated WebP, or `None` when it is a still one (or an animation of one +/// frame), which [`decode`] reads whole. The frames come composited onto the canvas by +/// `image-webp` (offsets, blending and disposal applied), each a full picture with the file's +/// ICC profile and its duration in milliseconds. +/// +/// The first frame must decode. If a later frame is damaged the frames before it are returned +/// and the first one carries [`DecodeWarning::Truncated`]. The frame count +/// ([`super::gif_frames::MAX_GIF_FRAMES`]) and the total decoded bytes are checked against +/// `limits` before each frame is kept. +pub(crate) fn decode_frames(bytes: &[u8], limits: &Limits) -> Result>, CodecError> { + use super::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; + if !is_animated(bytes) { + return Ok(None); + } + let (mut dec, w, h, layout) = open(bytes, limits)?; + let size = dec.output_buffer_size().ok_or_else(|| err("image too large"))?; + let icc = dec.icc_profile().ok().flatten(); + let mut buf = alloc_zeroed::(size)?; + let mut out: Vec = Vec::new(); + let mut damaged = false; + for _ in 0..dec.num_frames() { + if out.len() >= MAX_GIF_FRAMES { + return Err(CodecError::LimitExceeded(format!("more than {MAX_GIF_FRAMES} frames"))); + } + // Every frame keeps its own copy of the file's ICC profile along with its pixels. + let frame_bytes = (size as u64).saturating_add(icc.as_ref().map_or(0, |p| p.len() as u64)); + if frame_bytes.saturating_mul(out.len() as u64 + 1) > limits.max_alloc { + return Err(CodecError::LimitExceeded(format!("{} frames of {w}x{h} exceed max_alloc {}", out.len() + 1, limits.max_alloc))); + } + let delay_ms = match dec.read_frame(&mut buf) { + Ok(d) => d, + Err(e) if out.is_empty() => return Err(err(e)), + Err(_) => { + damaged = true; + break; + } + }; + let mut image = Image::from_u8(w, h, layout, buf.clone())?; + image.icc.clone_from(&icc); + out.push(AnimationFrame { image, delay_ms }); + } + if damaged && let Some(first) = out.first_mut() { + first.image.warnings.push(DecodeWarning::Truncated { format: F }); + } + Ok(Some(out)) +} + +pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result, CodecError> { + let img = src.converted(plan.layout, plan.sample); + if !opts.webp_lossless { + return encode_lossy(&img, opts); + } + let ct = match img.layout() { + ChannelLayout::Gray => image_webp::ColorType::L8, + ChannelLayout::GrayA => image_webp::ColorType::La8, + ChannelLayout::Rgb => image_webp::ColorType::Rgb8, + ChannelLayout::Rgba => image_webp::ColorType::Rgba8, + l => return Err(CodecError::encode(F, format!("unsupported layout {l:?}"))), + }; + let mut out = Vec::new(); + let mut enc = image_webp::WebPEncoder::new(&mut out); + if opts.embed_icc + && let Some(icc) = &img.icc + { + enc.set_icc_profile(icc.clone()); + } + if opts.embed_metadata { + if let Some(exif) = export_exif(&img) { + enc.set_exif_metadata(exif); + } + if let Some(xmp) = &img.meta.xmp { + enc.set_xmp_metadata(crate::resolution::export_xmp(xmp, img.meta.dpi).as_bytes().to_vec()); + } + } + enc.encode(img.data(), img.width(), img.height(), ct).map_err(|e| CodecError::encode(F, e))?; + Ok(out) +} + +/// Lossy: the picture as a VP8 key frame, alpha (when any pixel is translucent) as an +/// uncompressed `ALPH` chunk, metadata as chunks. +fn encode_lossy(img: &Image, opts: &EncodeOptions) -> Result, CodecError> { + use super::vp8; + let (w, h) = img.dimensions(); + if w > vp8::MAX_DIMENSION || h > vp8::MAX_DIMENSION { + return Err(CodecError::encode(F, format!("lossy WebP holds at most {} pixels per side", vp8::MAX_DIMENSION))); + } + // The VP8 path takes 8-bit RGB(A); gray becomes RGB with flat chroma. + let has_alpha = img.layout().has_alpha(); + let target = if has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + let rgb = img.converted(target, SampleType::U8); + let stride = target.channels(); + let yuv = vp8::Yuv::from_rgb(rgb.data(), w, h, stride); + let frame = vp8::encode_frame(&yuv, vp8::Params { quality: opts.webp_quality.min(100) }).map_err(|e| CodecError::encode(F, e))?; + let alpha: Option> = if has_alpha { + let a: Vec = rgb.data().iter().skip(3).step_by(4).copied().collect(); + if a.iter().all(|&v| v == 255) { None } else { Some(a) } + } else { + None + }; + let icc = if opts.embed_icc { img.icc.as_deref() } else { None }; + let exif = if opts.embed_metadata { export_exif(img) } else { None }; + let xmp = if opts.embed_metadata { img.meta.xmp.as_deref().map(|x| crate::resolution::export_xmp(x, img.meta.dpi)) } else { None }; + Ok(riff(w, h, &frame, alpha.as_deref(), icc, exif.as_deref(), xmp.as_deref().map(str::as_bytes))) +} + +/// The EXIF to write: the image's, upright (the pixels are written as they are shown, so a viewer +/// must never rotate them again), carrying the resolution. WebP has no resolution field of its own, +/// so the resolution goes into EXIF, added when the image has none (#2399). +pub(crate) fn export_exif(img: &Image) -> Option> { + let exif = img.meta.exif.as_deref().unwrap_or_default(); + // A WebP without EXIF already reads as 72 ppi, so the default needs no block of its own (and + // the extended container it would bring to every web export). + if exif.is_empty() && img.meta.dpi.is_none_or(is_default_ppi) { + return None; + } + let out = crate::resolution::export_exif_adding_missing_resolution(exif, img.meta.dpi).into_owned(); + (!out.is_empty()).then_some(out) +} + +/// Whether a WebP of `img` keeps its resolution: the EXIF written carries it. Not when the image's +/// own EXIF can't be parsed: that block is written as it is rather than replaced, so the +/// resolution is dropped (and reported) instead of the metadata. +pub(crate) fn keeps_dpi(img: &Image) -> bool { + let bare_default = img.meta.exif.as_deref().is_none_or(<[u8]>::is_empty) && img.meta.dpi.is_none_or(is_default_ppi); + // The XMP written beside it carries the resolution too, and readers look there first. + let in_xmp = img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); + bare_default || in_xmp || export_exif(img).is_some_and(|e| crate::resolution::exif_resolution(&e).is_some()) +} + +/// 72 ppi each way: what a file without a resolution opens as. +fn is_default_ppi((x, y): (f32, f32)) -> bool { + (x - 72.0).abs() < 0.01 && (y - 72.0).abs() < 0.01 +} + +/// A RIFF chunk: FourCC, little-endian size, payload, padded to an even length. +fn chunk(out: &mut Vec, fourcc: &[u8; 4], payload: &[u8]) { + out.extend_from_slice(fourcc); + out.extend_from_slice(&(payload.len() as u32).to_le_bytes()); + out.extend_from_slice(payload); + if payload.len() % 2 == 1 { + out.push(0); + } +} + +/// The WebP container around a VP8 frame: the simple form when there is nothing else, else the +/// extended form (`VP8X`) with the alpha plane and the metadata chunks in the specified order. +fn riff(w: u32, h: u32, frame: &[u8], alpha: Option<&[u8]>, icc: Option<&[u8]>, exif: Option<&[u8]>, xmp: Option<&[u8]>) -> Vec { + let mut body = b"WEBP".to_vec(); + let extended = alpha.is_some() || icc.is_some() || exif.is_some() || xmp.is_some(); + if extended { + let mut flags = 0u8; + if icc.is_some() { + flags |= 0x20; + } + if alpha.is_some() { + flags |= 0x10; + } + if exif.is_some() { + flags |= 0x08; + } + if xmp.is_some() { + flags |= 0x04; + } + let mut vp8x = vec![flags, 0, 0, 0]; + vp8x.extend_from_slice(&(w - 1).to_le_bytes()[..3]); + vp8x.extend_from_slice(&(h - 1).to_le_bytes()[..3]); + chunk(&mut body, b"VP8X", &vp8x); + if let Some(icc) = icc { + chunk(&mut body, b"ICCP", icc); + } + if let Some(a) = alpha { + // Header byte: reserved, no pre-processing, no filtering, no compression. + let mut alph = Vec::with_capacity(a.len() + 1); + alph.push(0); + alph.extend_from_slice(a); + chunk(&mut body, b"ALPH", &alph); + } + } + chunk(&mut body, b"VP8 ", frame); + if let Some(exif) = exif { + chunk(&mut body, b"EXIF", exif); + } + if let Some(xmp) = xmp { + chunk(&mut body, b"XMP ", xmp); + } + let mut out = Vec::with_capacity(body.len() + 8); + out.extend_from_slice(b"RIFF"); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend_from_slice(&body); + out +} diff --git a/crates/image-codecs/src/codecs/xpm.rs b/crates/image-codecs/src/codecs/xpm.rs new file mode 100644 index 000000000..c20f3c58e --- /dev/null +++ b/crates/image-codecs/src/codecs/xpm.rs @@ -0,0 +1,226 @@ +//! X PixMap reader for XPM2 plain-text icons and XPM3 C-source icon arrays. +//! Supports transparency (None), RGB hex at 4/8/16 bits per channel and +//! common X11 colour names. Does not execute C code. + +use std::collections::HashMap; + +mod x11_colors; + +use crate::SampleType; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, Image}; +use crate::options::Limits; + +const MAX_SOURCE: usize = 256 << 20; +const MAX_COLOURS: usize = 65_536; +const MAX_CPP: usize = 8; + +fn bad(msg: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::Xpm, msg) +} + +/// Extract C string literals without treating quoted text or // in a string as a comment. +/// XPM uses one literal for the header, each palette entry and each row. +fn c_strings(bytes: &[u8]) -> Result>, CodecError> { + let mut lines = Vec::new(); + let mut i = 0usize; + while i < bytes.len() { + if bytes[i..].starts_with(b"/*") { + let tail = &bytes[i + 2..]; + let Some(end) = tail.windows(2).position(|p| p == b"*/") else { return Err(bad("unterminated C comment")) }; + i += end + 4; + } else if bytes[i..].starts_with(b"//") { + i += bytes[i..].iter().position(|&c| c == b'\n').unwrap_or(bytes.len() - i); + } else if bytes[i] == b'"' { + i += 1; + let mut s = Vec::new(); + let mut closed = false; + while i < bytes.len() { + let c = bytes[i]; + i += 1; + match c { + b'"' => { + closed = true; + break; + } + b'\\' => { + let next = *bytes.get(i).ok_or_else(|| bad("truncated C escape"))?; + i += 1; + s.push(match next { + b'n' => b'\n', + b'r' => b'\r', + b't' => b'\t', + b'0' => 0, + _ => next, // quoted backslash or quote + }); + } + _ => s.push(c), + } + } + if !closed { + return Err(bad("unterminated XPM string")); + } + lines.push(s); + } else { + i += 1; + } + } + Ok(lines) +} + +/// Extract XPM2's plain-text lines without trimming pixel rows: leading and +/// trailing spaces can be palette keys, and physical CRLF is not image data. +fn xpm2_lines(bytes: &[u8]) -> Result>, CodecError> { + let mut input = bytes.split(|&b| b == b'\n'); + let header = input.next().ok_or_else(|| bad("missing XPM2 marker"))?; + if header.strip_suffix(b"\r").unwrap_or(header) != b"! XPM2" { + return Err(bad("not an XPM2 image")); + } + let mut lines = Vec::new(); + for raw in input { + let line = raw.strip_suffix(b"\r").unwrap_or(raw); + // The header may be preceded by comments and blank lines. After the + // header, a leading '!' is allowed as a palette key or pixel value. + if lines.is_empty() && (line.is_empty() || line.starts_with(b"!")) { + continue; + } + lines.push(line.to_vec()); + } + Ok(lines) +} + +fn channel(hex: &str) -> Option { + let v = u32::from_str_radix(hex, 16).ok()?; + let max = (1u32 << (hex.len() * 4)) - 1; + Some(((v * 255 + max / 2) / max) as u8) +} + +fn named(name: &str) -> Option<[u8; 3]> { + let s = name.to_ascii_lowercase().replace([' ', '-', '_'], ""); + // XPM imports X11 names, whose RGB values differ from CSS for names + // such as gray, maroon and purple. Keep these exact rather than guessing. + if let Ok(index) = x11_colors::X11_COLORS.binary_search_by_key(&s.as_str(), |(name, _)| *name) { + return Some(x11_colors::X11_COLORS[index].1); + } + // Retain useful web color aliases not present in the X11 database. + Some(match s.as_str() { + "silver" => [192, 192, 192], + "olive" => [128, 128, 0], + "teal" => [0, 128, 128], + "fuchsia" => [255, 0, 255], + "navyblue" => [0, 0, 128], + _ => return None, + }) +} + +fn color(name: &str) -> Option<[u8; 4]> { + if name.eq_ignore_ascii_case("none") { + return Some([0, 0, 0, 0]); + } + let rgb = if let Some(hex) = name.strip_prefix('#') { + // UTF-8 byte lengths can match a supported hex width while slicing would panic. + // `from_str_radix` would also take a leading `+`: accept hex digits only. + if !hex.bytes().all(|b| b.is_ascii_hexdigit()) { + return None; + } + let digits = match hex.len() { + 3 => 1, + 6 => 2, + 12 => 4, + _ => return None, + }; + let rgb = (0..3).map(|i| channel(&hex[i * digits..(i + 1) * digits])).collect::>>()?; + [rgb[0], rgb[1], rgb[2]] + } else { + named(name)? + }; + Some([rgb[0], rgb[1], rgb[2], 255]) +} + +pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { + if bytes.len() > MAX_SOURCE { + return Err(CodecError::LimitExceeded("XPM source exceeds 256 MiB".into())); + } + let lines = if bytes.starts_with(b"/* XPM */") { + c_strings(bytes)? + } else if bytes.starts_with(b"! XPM2\n") || bytes.starts_with(b"! XPM2\r\n") { + xpm2_lines(bytes)? + } else { + return Err(bad("not an XPM2/XPM3 image")); + }; + decode_lines(&lines, limits) +} + +fn decode_lines(lines: &[Vec], limits: &Limits) -> Result { + let header = lines.first().ok_or_else(|| bad("missing image header"))?; + let parts = std::str::from_utf8(header).map_err(|_| bad("non-UTF-8 XPM header"))?; + let mut parts = parts.split_ascii_whitespace(); + let number = |key: &str, s: Option<&str>| -> Result { + s.ok_or_else(|| bad(format!("missing {key}")))?.parse::().map_err(|_| bad(format!("invalid {key}"))) + }; + let width = number("width", parts.next())?; + let height = number("height", parts.next())?; + let colours = number("palette size", parts.next())?; + let cpp = number("chars per pixel", parts.next())?; + if width == 0 || height == 0 || colours == 0 || colours > MAX_COLOURS || !(1..=MAX_CPP).contains(&cpp) { + return Err(bad("invalid XPM dimensions, palette size or chars per pixel")); + } + let (w, h) = (u32::try_from(width).map_err(|_| bad("width overflow"))?, u32::try_from(height).map_err(|_| bad("height overflow"))?); + limits.check(w, h, ChannelLayout::Rgba, SampleType::U8)?; + let required = 1usize.checked_add(colours).and_then(|n| n.checked_add(height)).ok_or_else(|| bad("row count overflow"))?; + if lines.len() < required { + return Err(bad("truncated palette or image rows")); + } + let mut palette = HashMap::, [u8; 4]>::with_capacity(colours); + for line in &lines[1..1 + colours] { + if line.len() < cpp { + return Err(bad("truncated colour key")); + } + let fields = std::str::from_utf8(&line[cpp..]).map_err(|_| bad("invalid colour name"))?; + // XPM priority is colour, gray, four-level gray, then monochrome, + // regardless of the order of keys in the palette entry. + let terms: Vec<&str> = fields.split_ascii_whitespace().collect(); + let mut picked: Option<(u8, String)> = None; + let mut i = 0; + while i < terms.len() { + let priority = match terms[i] { + "c" => 4, + "g" => 3, + "g4" => 2, + "m" => 1, + _ => 0, // Symbolic names ('s') are not colour values. + }; + let start = i + 1; + i = start; + while i < terms.len() && !matches!(terms[i], "c" | "g" | "g4" | "m" | "s") { + i += 1; + } + if priority > 0 && i > start && picked.as_ref().is_none_or(|(current, _)| priority > *current) { + picked = Some((priority, terms[start..i].join(" "))); + } + } + let value = picked.as_ref().and_then(|(_, name)| color(name)).ok_or_else(|| bad("unsupported or missing XPM colour"))?; + if palette.insert(line[..cpp].to_vec(), value).is_some() { + return Err(bad("duplicate XPM palette key")); + } + } + let row_len = width.checked_mul(cpp).ok_or_else(|| bad("row byte count overflow"))?; + // Validate all source rows before reserving the decoded RGBA image. A malformed + // short row must not be able to make a large header-sized allocation first. + if lines[1 + colours..required].iter().any(|line| line.len() != row_len) { + return Err(bad("XPM row has the wrong width")); + } + let total = width.checked_mul(height).and_then(|n| n.checked_mul(4)).ok_or_else(|| bad("pixel size overflow"))?; + let mut pixels = Vec::new(); + pixels.try_reserve_exact(total).map_err(|_| bad("XPM pixel buffer is too large"))?; + pixels.resize(total, 0); + for (y, line) in lines[1 + colours..required].iter().enumerate() { + for x in 0..width { + let key = &line[x * cpp..(x + 1) * cpp]; + let rgba = palette.get(key).ok_or_else(|| bad("unknown palette key"))?; + pixels[(y * width + x) * 4..(y * width + x + 1) * 4].copy_from_slice(rgba); + } + } + Image::from_u8(w, h, ChannelLayout::Rgba, pixels) +} diff --git a/crates/image-codecs/src/codecs/xpm/x11_colors.rs b/crates/image-codecs/src/codecs/xpm/x11_colors.rs new file mode 100644 index 000000000..1d47b3706 --- /dev/null +++ b/crates/image-codecs/src/codecs/xpm/x11_colors.rs @@ -0,0 +1,666 @@ +//! X11 color-name database (X.Org rgb.txt) normalized to lowercase without +//! spaces, underscores, or hyphens. RGB values are *X11* values, not CSS values. +//! Source: https://cgit.freedesktop.org/xorg/app/rgb/tree/rgb.txt +//! License: MIT (X.Org Foundation), see the upstream app/rgb COPYING file. +//! Aliases differing only in case or spaces have been collapsed. +//! Keep sorted: decoder uses binary search without allocation per candidate. + +pub(super) const X11_COLORS: &[(&str, [u8; 3])] = &[ + ("aliceblue", [240, 248, 255]), + ("antiquewhite", [250, 235, 215]), + ("antiquewhite1", [255, 239, 219]), + ("antiquewhite2", [238, 223, 204]), + ("antiquewhite3", [205, 192, 176]), + ("antiquewhite4", [139, 131, 120]), + ("aquamarine", [127, 255, 212]), + ("aquamarine1", [127, 255, 212]), + ("aquamarine2", [118, 238, 198]), + ("aquamarine3", [102, 205, 170]), + ("aquamarine4", [69, 139, 116]), + ("azure", [240, 255, 255]), + ("azure1", [240, 255, 255]), + ("azure2", [224, 238, 238]), + ("azure3", [193, 205, 205]), + ("azure4", [131, 139, 139]), + ("beige", [245, 245, 220]), + ("bisque", [255, 228, 196]), + ("bisque1", [255, 228, 196]), + ("bisque2", [238, 213, 183]), + ("bisque3", [205, 183, 158]), + ("bisque4", [139, 125, 107]), + ("black", [0, 0, 0]), + ("blanchedalmond", [255, 235, 205]), + ("blue", [0, 0, 255]), + ("blue1", [0, 0, 255]), + ("blue2", [0, 0, 238]), + ("blue3", [0, 0, 205]), + ("blue4", [0, 0, 139]), + ("blueviolet", [138, 43, 226]), + ("brown", [165, 42, 42]), + ("brown1", [255, 64, 64]), + ("brown2", [238, 59, 59]), + ("brown3", [205, 51, 51]), + ("brown4", [139, 35, 35]), + ("burlywood", [222, 184, 135]), + ("burlywood1", [255, 211, 155]), + ("burlywood2", [238, 197, 145]), + ("burlywood3", [205, 170, 125]), + ("burlywood4", [139, 115, 85]), + ("cadetblue", [95, 158, 160]), + ("cadetblue1", [152, 245, 255]), + ("cadetblue2", [142, 229, 238]), + ("cadetblue3", [122, 197, 205]), + ("cadetblue4", [83, 134, 139]), + ("chartreuse", [127, 255, 0]), + ("chartreuse1", [127, 255, 0]), + ("chartreuse2", [118, 238, 0]), + ("chartreuse3", [102, 205, 0]), + ("chartreuse4", [69, 139, 0]), + ("chocolate", [210, 105, 30]), + ("chocolate1", [255, 127, 36]), + ("chocolate2", [238, 118, 33]), + ("chocolate3", [205, 102, 29]), + ("chocolate4", [139, 69, 19]), + ("coral", [255, 127, 80]), + ("coral1", [255, 114, 86]), + ("coral2", [238, 106, 80]), + ("coral3", [205, 91, 69]), + ("coral4", [139, 62, 47]), + ("cornflowerblue", [100, 149, 237]), + ("cornsilk", [255, 248, 220]), + ("cornsilk1", [255, 248, 220]), + ("cornsilk2", [238, 232, 205]), + ("cornsilk3", [205, 200, 177]), + ("cornsilk4", [139, 136, 120]), + ("cyan", [0, 255, 255]), + ("cyan1", [0, 255, 255]), + ("cyan2", [0, 238, 238]), + ("cyan3", [0, 205, 205]), + ("cyan4", [0, 139, 139]), + ("darkblue", [0, 0, 139]), + ("darkcyan", [0, 139, 139]), + ("darkgoldenrod", [184, 134, 11]), + ("darkgoldenrod1", [255, 185, 15]), + ("darkgoldenrod2", [238, 173, 14]), + ("darkgoldenrod3", [205, 149, 12]), + ("darkgoldenrod4", [139, 101, 8]), + ("darkgray", [169, 169, 169]), + ("darkgreen", [0, 100, 0]), + ("darkgrey", [169, 169, 169]), + ("darkkhaki", [189, 183, 107]), + ("darkmagenta", [139, 0, 139]), + ("darkolivegreen", [85, 107, 47]), + ("darkolivegreen1", [202, 255, 112]), + ("darkolivegreen2", [188, 238, 104]), + ("darkolivegreen3", [162, 205, 90]), + ("darkolivegreen4", [110, 139, 61]), + ("darkorange", [255, 140, 0]), + ("darkorange1", [255, 127, 0]), + ("darkorange2", [238, 118, 0]), + ("darkorange3", [205, 102, 0]), + ("darkorange4", [139, 69, 0]), + ("darkorchid", [153, 50, 204]), + ("darkorchid1", [191, 62, 255]), + ("darkorchid2", [178, 58, 238]), + ("darkorchid3", [154, 50, 205]), + ("darkorchid4", [104, 34, 139]), + ("darkred", [139, 0, 0]), + ("darksalmon", [233, 150, 122]), + ("darkseagreen", [143, 188, 143]), + ("darkseagreen1", [193, 255, 193]), + ("darkseagreen2", [180, 238, 180]), + ("darkseagreen3", [155, 205, 155]), + ("darkseagreen4", [105, 139, 105]), + ("darkslateblue", [72, 61, 139]), + ("darkslategray", [47, 79, 79]), + ("darkslategray1", [151, 255, 255]), + ("darkslategray2", [141, 238, 238]), + ("darkslategray3", [121, 205, 205]), + ("darkslategray4", [82, 139, 139]), + ("darkslategrey", [47, 79, 79]), + ("darkturquoise", [0, 206, 209]), + ("darkviolet", [148, 0, 211]), + ("deeppink", [255, 20, 147]), + ("deeppink1", [255, 20, 147]), + ("deeppink2", [238, 18, 137]), + ("deeppink3", [205, 16, 118]), + ("deeppink4", [139, 10, 80]), + ("deepskyblue", [0, 191, 255]), + ("deepskyblue1", [0, 191, 255]), + ("deepskyblue2", [0, 178, 238]), + ("deepskyblue3", [0, 154, 205]), + ("deepskyblue4", [0, 104, 139]), + ("dimgray", [105, 105, 105]), + ("dimgrey", [105, 105, 105]), + ("dodgerblue", [30, 144, 255]), + ("dodgerblue1", [30, 144, 255]), + ("dodgerblue2", [28, 134, 238]), + ("dodgerblue3", [24, 116, 205]), + ("dodgerblue4", [16, 78, 139]), + ("firebrick", [178, 34, 34]), + ("firebrick1", [255, 48, 48]), + ("firebrick2", [238, 44, 44]), + ("firebrick3", [205, 38, 38]), + ("firebrick4", [139, 26, 26]), + ("floralwhite", [255, 250, 240]), + ("forestgreen", [34, 139, 34]), + ("gainsboro", [220, 220, 220]), + ("ghostwhite", [248, 248, 255]), + ("gold", [255, 215, 0]), + ("gold1", [255, 215, 0]), + ("gold2", [238, 201, 0]), + ("gold3", [205, 173, 0]), + ("gold4", [139, 117, 0]), + ("goldenrod", [218, 165, 32]), + ("goldenrod1", [255, 193, 37]), + ("goldenrod2", [238, 180, 34]), + ("goldenrod3", [205, 155, 29]), + ("goldenrod4", [139, 105, 20]), + ("gray", [190, 190, 190]), + ("gray0", [0, 0, 0]), + ("gray1", [3, 3, 3]), + ("gray10", [26, 26, 26]), + ("gray100", [255, 255, 255]), + ("gray11", [28, 28, 28]), + ("gray12", [31, 31, 31]), + ("gray13", [33, 33, 33]), + ("gray14", [36, 36, 36]), + ("gray15", [38, 38, 38]), + ("gray16", [41, 41, 41]), + ("gray17", [43, 43, 43]), + ("gray18", [46, 46, 46]), + ("gray19", [48, 48, 48]), + ("gray2", [5, 5, 5]), + ("gray20", [51, 51, 51]), + ("gray21", [54, 54, 54]), + ("gray22", [56, 56, 56]), + ("gray23", [59, 59, 59]), + ("gray24", [61, 61, 61]), + ("gray25", [64, 64, 64]), + ("gray26", [66, 66, 66]), + ("gray27", [69, 69, 69]), + ("gray28", [71, 71, 71]), + ("gray29", [74, 74, 74]), + ("gray3", [8, 8, 8]), + ("gray30", [77, 77, 77]), + ("gray31", [79, 79, 79]), + ("gray32", [82, 82, 82]), + ("gray33", [84, 84, 84]), + ("gray34", [87, 87, 87]), + ("gray35", [89, 89, 89]), + ("gray36", [92, 92, 92]), + ("gray37", [94, 94, 94]), + ("gray38", [97, 97, 97]), + ("gray39", [99, 99, 99]), + ("gray4", [10, 10, 10]), + ("gray40", [102, 102, 102]), + ("gray41", [105, 105, 105]), + ("gray42", [107, 107, 107]), + ("gray43", [110, 110, 110]), + ("gray44", [112, 112, 112]), + ("gray45", [115, 115, 115]), + ("gray46", [117, 117, 117]), + ("gray47", [120, 120, 120]), + ("gray48", [122, 122, 122]), + ("gray49", [125, 125, 125]), + ("gray5", [13, 13, 13]), + ("gray50", [127, 127, 127]), + ("gray51", [130, 130, 130]), + ("gray52", [133, 133, 133]), + ("gray53", [135, 135, 135]), + ("gray54", [138, 138, 138]), + ("gray55", [140, 140, 140]), + ("gray56", [143, 143, 143]), + ("gray57", [145, 145, 145]), + ("gray58", [148, 148, 148]), + ("gray59", [150, 150, 150]), + ("gray6", [15, 15, 15]), + ("gray60", [153, 153, 153]), + ("gray61", [156, 156, 156]), + ("gray62", [158, 158, 158]), + ("gray63", [161, 161, 161]), + ("gray64", [163, 163, 163]), + ("gray65", [166, 166, 166]), + ("gray66", [168, 168, 168]), + ("gray67", [171, 171, 171]), + ("gray68", [173, 173, 173]), + ("gray69", [176, 176, 176]), + ("gray7", [18, 18, 18]), + ("gray70", [179, 179, 179]), + ("gray71", [181, 181, 181]), + ("gray72", [184, 184, 184]), + ("gray73", [186, 186, 186]), + ("gray74", [189, 189, 189]), + ("gray75", [191, 191, 191]), + ("gray76", [194, 194, 194]), + ("gray77", [196, 196, 196]), + ("gray78", [199, 199, 199]), + ("gray79", [201, 201, 201]), + ("gray8", [20, 20, 20]), + ("gray80", [204, 204, 204]), + ("gray81", [207, 207, 207]), + ("gray82", [209, 209, 209]), + ("gray83", [212, 212, 212]), + ("gray84", [214, 214, 214]), + ("gray85", [217, 217, 217]), + ("gray86", [219, 219, 219]), + ("gray87", [222, 222, 222]), + ("gray88", [224, 224, 224]), + ("gray89", [227, 227, 227]), + ("gray9", [23, 23, 23]), + ("gray90", [229, 229, 229]), + ("gray91", [232, 232, 232]), + ("gray92", [235, 235, 235]), + ("gray93", [237, 237, 237]), + ("gray94", [240, 240, 240]), + ("gray95", [242, 242, 242]), + ("gray96", [245, 245, 245]), + ("gray97", [247, 247, 247]), + ("gray98", [250, 250, 250]), + ("gray99", [252, 252, 252]), + ("green", [0, 255, 0]), + ("green1", [0, 255, 0]), + ("green2", [0, 238, 0]), + ("green3", [0, 205, 0]), + ("green4", [0, 139, 0]), + ("greenyellow", [173, 255, 47]), + ("grey", [190, 190, 190]), + ("grey0", [0, 0, 0]), + ("grey1", [3, 3, 3]), + ("grey10", [26, 26, 26]), + ("grey100", [255, 255, 255]), + ("grey11", [28, 28, 28]), + ("grey12", [31, 31, 31]), + ("grey13", [33, 33, 33]), + ("grey14", [36, 36, 36]), + ("grey15", [38, 38, 38]), + ("grey16", [41, 41, 41]), + ("grey17", [43, 43, 43]), + ("grey18", [46, 46, 46]), + ("grey19", [48, 48, 48]), + ("grey2", [5, 5, 5]), + ("grey20", [51, 51, 51]), + ("grey21", [54, 54, 54]), + ("grey22", [56, 56, 56]), + ("grey23", [59, 59, 59]), + ("grey24", [61, 61, 61]), + ("grey25", [64, 64, 64]), + ("grey26", [66, 66, 66]), + ("grey27", [69, 69, 69]), + ("grey28", [71, 71, 71]), + ("grey29", [74, 74, 74]), + ("grey3", [8, 8, 8]), + ("grey30", [77, 77, 77]), + ("grey31", [79, 79, 79]), + ("grey32", [82, 82, 82]), + ("grey33", [84, 84, 84]), + ("grey34", [87, 87, 87]), + ("grey35", [89, 89, 89]), + ("grey36", [92, 92, 92]), + ("grey37", [94, 94, 94]), + ("grey38", [97, 97, 97]), + ("grey39", [99, 99, 99]), + ("grey4", [10, 10, 10]), + ("grey40", [102, 102, 102]), + ("grey41", [105, 105, 105]), + ("grey42", [107, 107, 107]), + ("grey43", [110, 110, 110]), + ("grey44", [112, 112, 112]), + ("grey45", [115, 115, 115]), + ("grey46", [117, 117, 117]), + ("grey47", [120, 120, 120]), + ("grey48", [122, 122, 122]), + ("grey49", [125, 125, 125]), + ("grey5", [13, 13, 13]), + ("grey50", [127, 127, 127]), + ("grey51", [130, 130, 130]), + ("grey52", [133, 133, 133]), + ("grey53", [135, 135, 135]), + ("grey54", [138, 138, 138]), + ("grey55", [140, 140, 140]), + ("grey56", [143, 143, 143]), + ("grey57", [145, 145, 145]), + ("grey58", [148, 148, 148]), + ("grey59", [150, 150, 150]), + ("grey6", [15, 15, 15]), + ("grey60", [153, 153, 153]), + ("grey61", [156, 156, 156]), + ("grey62", [158, 158, 158]), + ("grey63", [161, 161, 161]), + ("grey64", [163, 163, 163]), + ("grey65", [166, 166, 166]), + ("grey66", [168, 168, 168]), + ("grey67", [171, 171, 171]), + ("grey68", [173, 173, 173]), + ("grey69", [176, 176, 176]), + ("grey7", [18, 18, 18]), + ("grey70", [179, 179, 179]), + ("grey71", [181, 181, 181]), + ("grey72", [184, 184, 184]), + ("grey73", [186, 186, 186]), + ("grey74", [189, 189, 189]), + ("grey75", [191, 191, 191]), + ("grey76", [194, 194, 194]), + ("grey77", [196, 196, 196]), + ("grey78", [199, 199, 199]), + ("grey79", [201, 201, 201]), + ("grey8", [20, 20, 20]), + ("grey80", [204, 204, 204]), + ("grey81", [207, 207, 207]), + ("grey82", [209, 209, 209]), + ("grey83", [212, 212, 212]), + ("grey84", [214, 214, 214]), + ("grey85", [217, 217, 217]), + ("grey86", [219, 219, 219]), + ("grey87", [222, 222, 222]), + ("grey88", [224, 224, 224]), + ("grey89", [227, 227, 227]), + ("grey9", [23, 23, 23]), + ("grey90", [229, 229, 229]), + ("grey91", [232, 232, 232]), + ("grey92", [235, 235, 235]), + ("grey93", [237, 237, 237]), + ("grey94", [240, 240, 240]), + ("grey95", [242, 242, 242]), + ("grey96", [245, 245, 245]), + ("grey97", [247, 247, 247]), + ("grey98", [250, 250, 250]), + ("grey99", [252, 252, 252]), + ("honeydew", [240, 255, 240]), + ("honeydew1", [240, 255, 240]), + ("honeydew2", [224, 238, 224]), + ("honeydew3", [193, 205, 193]), + ("honeydew4", [131, 139, 131]), + ("hotpink", [255, 105, 180]), + ("hotpink1", [255, 110, 180]), + ("hotpink2", [238, 106, 167]), + ("hotpink3", [205, 96, 144]), + ("hotpink4", [139, 58, 98]), + ("indianred", [205, 92, 92]), + ("indianred1", [255, 106, 106]), + ("indianred2", [238, 99, 99]), + ("indianred3", [205, 85, 85]), + ("indianred4", [139, 58, 58]), + ("ivory", [255, 255, 240]), + ("ivory1", [255, 255, 240]), + ("ivory2", [238, 238, 224]), + ("ivory3", [205, 205, 193]), + ("ivory4", [139, 139, 131]), + ("khaki", [240, 230, 140]), + ("khaki1", [255, 246, 143]), + ("khaki2", [238, 230, 133]), + ("khaki3", [205, 198, 115]), + ("khaki4", [139, 134, 78]), + ("lavender", [230, 230, 250]), + ("lavenderblush", [255, 240, 245]), + ("lavenderblush1", [255, 240, 245]), + ("lavenderblush2", [238, 224, 229]), + ("lavenderblush3", [205, 193, 197]), + ("lavenderblush4", [139, 131, 134]), + ("lawngreen", [124, 252, 0]), + ("lemonchiffon", [255, 250, 205]), + ("lemonchiffon1", [255, 250, 205]), + ("lemonchiffon2", [238, 233, 191]), + ("lemonchiffon3", [205, 201, 165]), + ("lemonchiffon4", [139, 137, 112]), + ("lightblue", [173, 216, 230]), + ("lightblue1", [191, 239, 255]), + ("lightblue2", [178, 223, 238]), + ("lightblue3", [154, 192, 205]), + ("lightblue4", [104, 131, 139]), + ("lightcoral", [240, 128, 128]), + ("lightcyan", [224, 255, 255]), + ("lightcyan1", [224, 255, 255]), + ("lightcyan2", [209, 238, 238]), + ("lightcyan3", [180, 205, 205]), + ("lightcyan4", [122, 139, 139]), + ("lightgoldenrod", [238, 221, 130]), + ("lightgoldenrod1", [255, 236, 139]), + ("lightgoldenrod2", [238, 220, 130]), + ("lightgoldenrod3", [205, 190, 112]), + ("lightgoldenrod4", [139, 129, 76]), + ("lightgoldenrodyellow", [250, 250, 210]), + ("lightgray", [211, 211, 211]), + ("lightgreen", [144, 238, 144]), + ("lightgrey", [211, 211, 211]), + ("lightpink", [255, 182, 193]), + ("lightpink1", [255, 174, 185]), + ("lightpink2", [238, 162, 173]), + ("lightpink3", [205, 140, 149]), + ("lightpink4", [139, 95, 101]), + ("lightsalmon", [255, 160, 122]), + ("lightsalmon1", [255, 160, 122]), + ("lightsalmon2", [238, 149, 114]), + ("lightsalmon3", [205, 129, 98]), + ("lightsalmon4", [139, 87, 66]), + ("lightseagreen", [32, 178, 170]), + ("lightskyblue", [135, 206, 250]), + ("lightskyblue1", [176, 226, 255]), + ("lightskyblue2", [164, 211, 238]), + ("lightskyblue3", [141, 182, 205]), + ("lightskyblue4", [96, 123, 139]), + ("lightslateblue", [132, 112, 255]), + ("lightslategray", [119, 136, 153]), + ("lightslategrey", [119, 136, 153]), + ("lightsteelblue", [176, 196, 222]), + ("lightsteelblue1", [202, 225, 255]), + ("lightsteelblue2", [188, 210, 238]), + ("lightsteelblue3", [162, 181, 205]), + ("lightsteelblue4", [110, 123, 139]), + ("lightyellow", [255, 255, 224]), + ("lightyellow1", [255, 255, 224]), + ("lightyellow2", [238, 238, 209]), + ("lightyellow3", [205, 205, 180]), + ("lightyellow4", [139, 139, 122]), + ("limegreen", [50, 205, 50]), + ("linen", [250, 240, 230]), + ("magenta", [255, 0, 255]), + ("magenta1", [255, 0, 255]), + ("magenta2", [238, 0, 238]), + ("magenta3", [205, 0, 205]), + ("magenta4", [139, 0, 139]), + ("maroon", [176, 48, 96]), + ("maroon1", [255, 52, 179]), + ("maroon2", [238, 48, 167]), + ("maroon3", [205, 41, 144]), + ("maroon4", [139, 28, 98]), + ("mediumaquamarine", [102, 205, 170]), + ("mediumblue", [0, 0, 205]), + ("mediumorchid", [186, 85, 211]), + ("mediumorchid1", [224, 102, 255]), + ("mediumorchid2", [209, 95, 238]), + ("mediumorchid3", [180, 82, 205]), + ("mediumorchid4", [122, 55, 139]), + ("mediumpurple", [147, 112, 219]), + ("mediumpurple1", [171, 130, 255]), + ("mediumpurple2", [159, 121, 238]), + ("mediumpurple3", [137, 104, 205]), + ("mediumpurple4", [93, 71, 139]), + ("mediumseagreen", [60, 179, 113]), + ("mediumslateblue", [123, 104, 238]), + ("mediumspringgreen", [0, 250, 154]), + ("mediumturquoise", [72, 209, 204]), + ("mediumvioletred", [199, 21, 133]), + ("midnightblue", [25, 25, 112]), + ("mintcream", [245, 255, 250]), + ("mistyrose", [255, 228, 225]), + ("mistyrose1", [255, 228, 225]), + ("mistyrose2", [238, 213, 210]), + ("mistyrose3", [205, 183, 181]), + ("mistyrose4", [139, 125, 123]), + ("moccasin", [255, 228, 181]), + ("navajowhite", [255, 222, 173]), + ("navajowhite1", [255, 222, 173]), + ("navajowhite2", [238, 207, 161]), + ("navajowhite3", [205, 179, 139]), + ("navajowhite4", [139, 121, 94]), + ("navy", [0, 0, 128]), + ("navyblue", [0, 0, 128]), + ("oldlace", [253, 245, 230]), + ("olivedrab", [107, 142, 35]), + ("olivedrab1", [192, 255, 62]), + ("olivedrab2", [179, 238, 58]), + ("olivedrab3", [154, 205, 50]), + ("olivedrab4", [105, 139, 34]), + ("orange", [255, 165, 0]), + ("orange1", [255, 165, 0]), + ("orange2", [238, 154, 0]), + ("orange3", [205, 133, 0]), + ("orange4", [139, 90, 0]), + ("orangered", [255, 69, 0]), + ("orangered1", [255, 69, 0]), + ("orangered2", [238, 64, 0]), + ("orangered3", [205, 55, 0]), + ("orangered4", [139, 37, 0]), + ("orchid", [218, 112, 214]), + ("orchid1", [255, 131, 250]), + ("orchid2", [238, 122, 233]), + ("orchid3", [205, 105, 201]), + ("orchid4", [139, 71, 137]), + ("palegoldenrod", [238, 232, 170]), + ("palegreen", [152, 251, 152]), + ("palegreen1", [154, 255, 154]), + ("palegreen2", [144, 238, 144]), + ("palegreen3", [124, 205, 124]), + ("palegreen4", [84, 139, 84]), + ("paleturquoise", [175, 238, 238]), + ("paleturquoise1", [187, 255, 255]), + ("paleturquoise2", [174, 238, 238]), + ("paleturquoise3", [150, 205, 205]), + ("paleturquoise4", [102, 139, 139]), + ("palevioletred", [219, 112, 147]), + ("palevioletred1", [255, 130, 171]), + ("palevioletred2", [238, 121, 159]), + ("palevioletred3", [205, 104, 137]), + ("palevioletred4", [139, 71, 93]), + ("papayawhip", [255, 239, 213]), + ("peachpuff", [255, 218, 185]), + ("peachpuff1", [255, 218, 185]), + ("peachpuff2", [238, 203, 173]), + ("peachpuff3", [205, 175, 149]), + ("peachpuff4", [139, 119, 101]), + ("peru", [205, 133, 63]), + ("pink", [255, 192, 203]), + ("pink1", [255, 181, 197]), + ("pink2", [238, 169, 184]), + ("pink3", [205, 145, 158]), + ("pink4", [139, 99, 108]), + ("plum", [221, 160, 221]), + ("plum1", [255, 187, 255]), + ("plum2", [238, 174, 238]), + ("plum3", [205, 150, 205]), + ("plum4", [139, 102, 139]), + ("powderblue", [176, 224, 230]), + ("purple", [160, 32, 240]), + ("purple1", [155, 48, 255]), + ("purple2", [145, 44, 238]), + ("purple3", [125, 38, 205]), + ("purple4", [85, 26, 139]), + ("red", [255, 0, 0]), + ("red1", [255, 0, 0]), + ("red2", [238, 0, 0]), + ("red3", [205, 0, 0]), + ("red4", [139, 0, 0]), + ("rosybrown", [188, 143, 143]), + ("rosybrown1", [255, 193, 193]), + ("rosybrown2", [238, 180, 180]), + ("rosybrown3", [205, 155, 155]), + ("rosybrown4", [139, 105, 105]), + ("royalblue", [65, 105, 225]), + ("royalblue1", [72, 118, 255]), + ("royalblue2", [67, 110, 238]), + ("royalblue3", [58, 95, 205]), + ("royalblue4", [39, 64, 139]), + ("saddlebrown", [139, 69, 19]), + ("salmon", [250, 128, 114]), + ("salmon1", [255, 140, 105]), + ("salmon2", [238, 130, 98]), + ("salmon3", [205, 112, 84]), + ("salmon4", [139, 76, 57]), + ("sandybrown", [244, 164, 96]), + ("seagreen", [46, 139, 87]), + ("seagreen1", [84, 255, 159]), + ("seagreen2", [78, 238, 148]), + ("seagreen3", [67, 205, 128]), + ("seagreen4", [46, 139, 87]), + ("seashell", [255, 245, 238]), + ("seashell1", [255, 245, 238]), + ("seashell2", [238, 229, 222]), + ("seashell3", [205, 197, 191]), + ("seashell4", [139, 134, 130]), + ("sienna", [160, 82, 45]), + ("sienna1", [255, 130, 71]), + ("sienna2", [238, 121, 66]), + ("sienna3", [205, 104, 57]), + ("sienna4", [139, 71, 38]), + ("skyblue", [135, 206, 235]), + ("skyblue1", [135, 206, 255]), + ("skyblue2", [126, 192, 238]), + ("skyblue3", [108, 166, 205]), + ("skyblue4", [74, 112, 139]), + ("slateblue", [106, 90, 205]), + ("slateblue1", [131, 111, 255]), + ("slateblue2", [122, 103, 238]), + ("slateblue3", [105, 89, 205]), + ("slateblue4", [71, 60, 139]), + ("slategray", [112, 128, 144]), + ("slategray1", [198, 226, 255]), + ("slategray2", [185, 211, 238]), + ("slategray3", [159, 182, 205]), + ("slategray4", [108, 123, 139]), + ("slategrey", [112, 128, 144]), + ("snow", [255, 250, 250]), + ("snow1", [255, 250, 250]), + ("snow2", [238, 233, 233]), + ("snow3", [205, 201, 201]), + ("snow4", [139, 137, 137]), + ("springgreen", [0, 255, 127]), + ("springgreen1", [0, 255, 127]), + ("springgreen2", [0, 238, 118]), + ("springgreen3", [0, 205, 102]), + ("springgreen4", [0, 139, 69]), + ("steelblue", [70, 130, 180]), + ("steelblue1", [99, 184, 255]), + ("steelblue2", [92, 172, 238]), + ("steelblue3", [79, 148, 205]), + ("steelblue4", [54, 100, 139]), + ("tan", [210, 180, 140]), + ("tan1", [255, 165, 79]), + ("tan2", [238, 154, 73]), + ("tan3", [205, 133, 63]), + ("tan4", [139, 90, 43]), + ("thistle", [216, 191, 216]), + ("thistle1", [255, 225, 255]), + ("thistle2", [238, 210, 238]), + ("thistle3", [205, 181, 205]), + ("thistle4", [139, 123, 139]), + ("tomato", [255, 99, 71]), + ("tomato1", [255, 99, 71]), + ("tomato2", [238, 92, 66]), + ("tomato3", [205, 79, 57]), + ("tomato4", [139, 54, 38]), + ("turquoise", [64, 224, 208]), + ("turquoise1", [0, 245, 255]), + ("turquoise2", [0, 229, 238]), + ("turquoise3", [0, 197, 205]), + ("turquoise4", [0, 134, 139]), + ("violet", [238, 130, 238]), + ("violetred", [208, 32, 144]), + ("violetred1", [255, 62, 150]), + ("violetred2", [238, 58, 140]), + ("violetred3", [205, 50, 120]), + ("violetred4", [139, 34, 82]), + ("wheat", [245, 222, 179]), + ("wheat1", [255, 231, 186]), + ("wheat2", [238, 216, 174]), + ("wheat3", [205, 186, 150]), + ("wheat4", [139, 126, 102]), + ("white", [255, 255, 255]), + ("whitesmoke", [245, 245, 245]), + ("yellow", [255, 255, 0]), + ("yellow1", [255, 255, 0]), + ("yellow2", [238, 238, 0]), + ("yellow3", [205, 205, 0]), + ("yellow4", [139, 139, 0]), + ("yellowgreen", [154, 205, 50]), +]; diff --git a/crates/image-codecs/src/error.rs b/crates/image-codecs/src/error.rs new file mode 100644 index 000000000..fec9f35c1 --- /dev/null +++ b/crates/image-codecs/src/error.rs @@ -0,0 +1,31 @@ +use crate::format::Format; + +/// Errors produced by this crate. Decoders never panic on malformed input; +/// they return [`CodecError::Malformed`] instead. +#[derive(Debug, thiserror::Error)] +pub enum CodecError { + #[error("unrecognized image format")] + UnknownFormat, + #[error("{format:?}: {reason}")] + Unsupported { format: Format, reason: String }, + #[error("{format:?}: malformed data: {message}")] + Malformed { format: Format, message: String }, + #[error("limit exceeded: {0}")] + LimitExceeded(String), + #[error("{format:?}: encode failed: {message}")] + Encode { format: Format, message: String }, + #[error("invalid image: {0}")] + InvalidImage(String), +} + +impl CodecError { + pub(crate) fn malformed(format: Format, e: impl std::fmt::Display) -> Self { + CodecError::Malformed { format, message: e.to_string() } + } + pub(crate) fn encode(format: Format, e: impl std::fmt::Display) -> Self { + CodecError::Encode { format, message: e.to_string() } + } + pub(crate) fn unsupported(format: Format, reason: impl Into) -> Self { + CodecError::Unsupported { format, reason: reason.into() } + } +} diff --git a/crates/image-codecs/src/exif_color.rs b/crates/image-codecs/src/exif_color.rs new file mode 100644 index 000000000..4259bb15c --- /dev/null +++ b/crates/image-codecs/src/exif_color.rs @@ -0,0 +1,138 @@ +//! The EXIF ColorSpace tag (0xA001, in the Exif sub-IFD that IFD0's tag 0x8769 points to): +//! 1 = sRGB, 0xFFFF = uncalibrated (EXIF 2.3, §4.6.5). +//! +//! Cameras write it instead of an ICC profile. Photoshop opens a file with no embedded profile but +//! ColorSpace = sRGB as sRGB, unless Preferences › File Handling › Ignore EXIF Profile Tag is on. +//! Anything malformed, truncated or missing reads as no tag. + +use crate::orientation::{Order, ifd0_entries, tiff_body}; + +/// IFD0's pointer to the Exif sub-IFD. +const TAG_EXIF_IFD: u16 = 0x8769; +/// The ColorSpace tag in the Exif sub-IFD. +const TAG_COLOR_SPACE: u16 = 0xA001; +const TYPE_SHORT: u16 = 3; +const TYPE_LONG: u16 = 4; +/// TIFF type 13: an IFD offset (some writers use it for sub-IFD pointers). +const TYPE_IFD: u16 = 13; +/// Bytes per classic TIFF IFD entry; EXIF is never BigTIFF. +const ENTRY: usize = 12; +/// No IFD can start inside the 8-byte TIFF header. +const HEADER: usize = 8; + +/// The ColorSpace value meaning sRGB. +pub const EXIF_COLOR_SPACE_SRGB: u16 = 1; + +/// The EXIF ColorSpace value of a TIFF-structured block (an EXIF payload with or without the +/// `Exif\0\0` prefix), or `None` when it has none or it is malformed. +pub fn exif_color_space(exif: &[u8]) -> Option { + let b = tiff_body(exif); + let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; + if big_tiff { + return None; + } + let ptr = find(order, b, first, count, entry_bytes, TAG_EXIF_IFD)?; + if !matches!(order.u16(b, ptr.checked_add(2)?)?, TYPE_LONG | TYPE_IFD) || order.u32(b, ptr.checked_add(4)?)? != 1 { + return None; + } + let ifd = usize::try_from(order.u32(b, ptr.checked_add(8)?)?).ok()?; + if ifd < HEADER { + return None; + } + let n = usize::from(order.u16(b, ifd)?); + let first = ifd.checked_add(2)?; + // The whole table must be present. + if first.checked_add(n.checked_mul(ENTRY)?)? > b.len() { + return None; + } + let e = find(order, b, first, n, ENTRY, TAG_COLOR_SPACE)?; + if order.u32(b, e.checked_add(4)?)? != 1 { + return None; + } + match order.u16(b, e.checked_add(2)?)? { + TYPE_SHORT => order.u16(b, e.checked_add(8)?), + TYPE_LONG => u16::try_from(order.u32(b, e.checked_add(8)?)?).ok(), + _ => None, + } +} + +/// Offset of the first entry tagged `tag` in the IFD whose `count` entries start at `first`. +fn find(order: Order, b: &[u8], first: usize, count: usize, entry_bytes: usize, tag: u16) -> Option { + (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(tag)) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A little- or big-endian EXIF block: IFD0 holding only the Exif pointer, then an Exif IFD + /// holding `entries` (tag, type, count, value). + fn exif(big_endian: bool, entries: &[(u16, u16, u32, u32)]) -> Vec { + let u16b = |v: u16| if big_endian { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big_endian { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut b = if big_endian { b"MM\0\x2a".to_vec() } else { b"II\x2a\0".to_vec() }; + b.extend(u32b(8)); + // IFD0: one entry, then the next-IFD pointer. The Exif IFD starts right after (8 + 2 + 12 + 4). + b.extend(u16b(1)); + b.extend(u16b(TAG_EXIF_IFD)); + b.extend(u16b(TYPE_LONG)); + b.extend(u32b(1)); + b.extend(u32b(26)); + b.extend(u32b(0)); + b.extend(u16b(entries.len() as u16)); + for &(tag, ty, count, value) in entries { + b.extend(u16b(tag)); + b.extend(u16b(ty)); + b.extend(u32b(count)); + // SHORT values are left-justified in the 4-byte field. + if ty == TYPE_SHORT { + b.extend(u16b(value as u16)); + b.extend([0, 0]); + } else { + b.extend(u32b(value)); + } + } + b.extend(u32b(0)); + b + } + + #[test] + fn reads_the_color_space_in_either_byte_order() { + for big in [false, true] { + let srgb = exif(big, &[(0x9000, 7, 4, 0x3032_3330), (TAG_COLOR_SPACE, TYPE_SHORT, 1, 1)]); + assert_eq!(exif_color_space(&srgb), Some(EXIF_COLOR_SPACE_SRGB), "big endian: {big}"); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend(&srgb); + assert_eq!(exif_color_space(&prefixed), Some(1)); + assert_eq!(exif_color_space(&exif(big, &[(TAG_COLOR_SPACE, TYPE_SHORT, 1, 0xFFFF)])), Some(0xFFFF)); + assert_eq!(exif_color_space(&exif(big, &[(TAG_COLOR_SPACE, TYPE_LONG, 1, 1)])), Some(1), "LONG is accepted"); + } + } + + #[test] + fn missing_or_malformed_reads_as_no_tag() { + assert_eq!(exif_color_space(&exif(false, &[(0x9000, 7, 4, 0)])), None, "no ColorSpace entry"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, TYPE_SHORT, 2, 1)])), None, "count ≠ 1"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, 2, 1, 1)])), None, "ASCII type"); + assert_eq!(exif_color_space(&exif(false, &[(TAG_COLOR_SPACE, TYPE_LONG, 1, 0x1_0000)])), None, "LONG over u16"); + let good = exif(false, &[(TAG_COLOR_SPACE, TYPE_SHORT, 1, 1)]); + // Every truncation is rejected without panicking. + for len in 0..good.len() - 4 { + assert_eq!(exif_color_space(&good[..len]), None, "truncated to {len}"); + } + // An Exif pointer past the end, into the header, or of the wrong type. + for (at, value) in [(18usize, 0xFFFF_FFF0u32), (18, 4), (18, u32::MAX)] { + let mut b = good.clone(); + b[at..at + 4].copy_from_slice(&value.to_le_bytes()); + assert_eq!(exif_color_space(&b), None, "pointer {value:#x}"); + } + let mut b = good.clone(); + b[12..14].copy_from_slice(&TYPE_SHORT.to_le_bytes()); + assert_eq!(exif_color_space(&b), None, "pointer type SHORT"); + // An Exif IFD claiming more entries than the block holds. + let mut b = good; + b[26..28].copy_from_slice(&u16::MAX.to_le_bytes()); + assert_eq!(exif_color_space(&b), None); + assert_eq!(exif_color_space(b"not exif"), None); + } +} diff --git a/crates/image-codecs/src/exr_stream.rs b/crates/image-codecs/src/exr_stream.rs new file mode 100644 index 000000000..1ed8ba127 --- /dev/null +++ b/crates/image-codecs/src/exr_stream.rs @@ -0,0 +1,294 @@ +//! Bounded, seekable single-part scanline OpenEXR decoding. Total pixel count does not +//! size a decoded image buffer: only one compression block is returned at a time. +use std::io::{Cursor, Read, Seek, SeekFrom}; + +use crate::{CodecError, Format}; +use exr::{ + block::{ + UncompressedBlock, + chunk::{Chunk, CompressedBlock, CompressedScanLineBlock}, + lines::LineIndex, + }, + meta::{BlockDescription, MetaData, attribute::SampleType}, +}; + +const META_BYTES: usize = 8 << 20; +const BLOCK_BYTES: usize = 64 << 20; +const MAX_CHUNKS: usize = 1 << 20; + +fn invalid(e: impl std::fmt::Display) -> CodecError { + CodecError::malformed(Format::OpenExr, e) +} +fn limit(s: &str) -> CodecError { + CodecError::LimitExceeded(s.into()) +} +fn unsupported(s: &str) -> CodecError { + CodecError::unsupported(Format::OpenExr, s) +} + +fn read(r: &mut impl Read) -> Result<[u8; N], CodecError> { + let mut bytes = [0; N]; + r.read_exact(&mut bytes).map_err(invalid)?; + Ok(bytes) +} + +fn string(r: &mut impl Read, prefix: &mut Vec) -> Result { + for i in 0..256 { + let b = read::<1>(r)?; + prefix.extend(b); + if b == [0] { + return Ok(i != 0); + } + } + Err(limit("EXR attribute name exceeds 255 bytes")) +} + +fn metadata(r: &mut impl Read, cancel: &dyn Fn() -> bool) -> Result, CodecError> { + let start = read::<8>(r)?; + if start.get(..4) != Some(&[0x76, 0x2f, 0x31, 0x01]) { + return Err(invalid("EXR signature")); + } + let version = u32::from_le_bytes(start.get(4..).ok_or_else(|| invalid("version"))?.try_into().map_err(invalid)?); + if version & (0x200 | 0x800 | 0x1000) != 0 { + return Err(unsupported("streaming requires a single flat scanline part")); + } + let mut prefix = start.to_vec(); + let mut attributes = 0; + loop { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + if !string(r, &mut prefix)? { + break; + } + attributes += 1; + if attributes > 4096 { + return Err(limit("EXR metadata exceeds 4096 attributes")); + } + string(r, &mut prefix)?; + let length = read::<4>(r)?; + prefix.extend(length); + let n = usize::try_from(i32::from_le_bytes(length)).map_err(invalid)?; + if n > META_BYTES || prefix.len().checked_add(n).is_none_or(|v| v > META_BYTES) { + return Err(limit("EXR metadata exceeds 8 MiB")); + } + let begin = prefix.len(); + prefix.try_reserve_exact(n).map_err(|_| limit("EXR metadata allocation"))?; + prefix.resize(begin + n, 0); + for bytes in prefix.get_mut(begin..).ok_or_else(|| invalid("attribute size"))?.chunks_mut(64 << 10) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + r.read_exact(bytes).map_err(invalid)?; + } + } + Ok(prefix) +} + +/// Header facts used by the document owner to admit tile and working memory. +#[derive(Clone, Copy, Debug)] +pub struct Info { + pub width: u32, + pub height: u32, + pub gray: bool, + pub has_alpha: bool, + pub all_half: bool, + pub working_bytes: u64, + pub blocks: usize, +} + +/// Interleaved little-endian F32, GrayA or RGBA, at source resolution. +pub struct Band { + pub x: u32, + pub y: u32, + pub width: u32, + pub height: u32, + pub data: Vec, +} + +pub struct Decoder { + source: R, + meta: MetaData, + info: Info, + offsets: Vec, + seen: Vec, + channels: Vec>, + next: usize, + file_len: u64, + chunks_start: u64, + raw_limit: usize, + rows: usize, +} + +impl Decoder { + /// Read only bounded metadata and offsets; allocate no full-image or channel buffer. + pub fn new(mut source: R, cancel: &dyn Fn() -> bool) -> Result { + let file_len = source.seek(SeekFrom::End(0)).map_err(invalid)?; + source.seek(SeekFrom::Start(0)).map_err(invalid)?; + let prefix = metadata(&mut source, cancel)?; + let meta = std::panic::catch_unwind(|| MetaData::read_from_buffered(Cursor::new(&prefix), false)) + .map_err(|_| invalid("invalid EXR metadata"))? + .map_err(invalid)?; + let header = meta.headers.first().ok_or_else(|| invalid("missing header"))?; + if meta.headers.len() != 1 || header.deep || !matches!(header.blocks, BlockDescription::ScanLines) { + return Err(unsupported("streaming requires a single flat scanline part")); + } + let (w, h) = (header.layer_size.0, header.layer_size.1); + if w == 0 || h == 0 || w > 1 << 18 || h > 1 << 18 { + return Err(limit("EXR dimensions exceed the streaming limit of 262144")); + } + let list = &header.channels.list; + if list.is_empty() || list.len() > 64 || list.iter().any(|c| c.sampling.0 != 1 || c.sampling.1 != 1) { + return Err(unsupported("EXR channel count or subsampling")); + } + // The same channels the whole-file decoder shows, so both routes open the same image. + let names: Vec = list.iter().map(|c| c.name.to_string()).collect(); + let (layout, picked) = crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; + let gray = matches!(layout, crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA); + let mut selected: Vec> = picked.into_iter().map(Some).collect(); + if !matches!(layout, crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgba) { + selected.push(None); + } + if selected.len() != if gray { 2 } else { 4 } { + return Err(unsupported("EXR channel selection")); + } + // The document path currently tags Rec. 709 linear samples. Keep custom + // primaries on the existing decoder until their profiles can be mapped. + if !gray && header.shared_attributes.chromaticities.is_some() { + return Err(unsupported("streaming custom EXR chromaticities requires a color profile")); + } + let all_half = selected.iter().flatten().all(|i| list.get(*i).is_some_and(|c| c.sample_type == SampleType::F16)); + let has_alpha = selected.last().copied().flatten().is_some(); + let channels = (0..list.len()).map(|i| selected.iter().position(|s| *s == Some(i))).collect(); + let rows = header.max_block_pixel_size().1; + let expected = h.div_ceil(rows.max(1)); + if rows == 0 || header.chunk_count != expected || expected > MAX_CHUNKS { + return Err(limit("EXR block count exceeds the streaming budget")); + } + let raw_limit = w + .checked_mul(rows) + .and_then(|n| n.checked_mul(header.channels.bytes_per_pixel)) + .filter(|n| *n <= BLOCK_BYTES) + .ok_or_else(|| limit("EXR compression block exceeds 64 MiB"))?; + let output = (w as u64).saturating_mul(rows as u64).saturating_mul(selected.len() as u64 * 4); + let info = Info { + width: w as u32, + height: h as u32, + gray, + has_alpha, + all_half, + blocks: expected, + working_bytes: (raw_limit as u64).saturating_mul(8).saturating_add(output.saturating_mul(2)).saturating_add(1 << 20), + }; + let chunks_start = source + .stream_position() + .map_err(invalid)? + .checked_add(expected as u64 * 8) + .filter(|v| *v <= file_len) + .ok_or_else(|| invalid("truncated offset table"))?; + let mut offsets = Vec::new(); + offsets.try_reserve_exact(expected).map_err(|_| limit("EXR offset allocation"))?; + for _ in 0..expected { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + let offset = u64::from_le_bytes(read(&mut source)?); + if offset < chunks_start || offset.checked_add(8).is_none_or(|v| v > file_len) { + return Err(invalid("EXR chunk offset")); + } + offsets.push(offset); + } + Ok(Self { source, meta, info, offsets, seen: vec![false; expected], channels, next: 0, file_len, chunks_start, raw_limit, rows }) + } + + pub fn info(&self) -> Info { + self.info + } + + /// Decode one block. Check cancellation while reading and between channel rows. + pub fn next_band(&mut self, cancel: &dyn Fn() -> bool) -> Result, CodecError> { + let Some(offset) = self.offsets.get(self.next).copied() else { + return Ok(None); + }; + if cancel() { + return Err(invalid("EXR import cancelled")); + } + self.source.seek(SeekFrom::Start(offset)).map_err(invalid)?; + let y = i32::from_le_bytes(read(&mut self.source)?); + let n = usize::try_from(i32::from_le_bytes(read(&mut self.source)?)).map_err(invalid)?; + if n > self.raw_limit.saturating_mul(2).saturating_add(64 << 10) + || offset < self.chunks_start + || offset.checked_add(8).and_then(|v| v.checked_add(n as u64)).is_none_or(|v| v > self.file_len) + { + return Err(invalid("EXR compressed block length")); + } + let header = self.meta.headers.first().ok_or_else(|| invalid("missing header"))?; + let row = i64::from(y) - i64::from(header.own_attributes.layer_position.1); + if row < 0 || row as u64 >= u64::from(self.info.height) || !(row as usize).is_multiple_of(self.rows) { + return Err(invalid("EXR block coordinate")); + } + let seen = self.seen.get_mut(row as usize / self.rows).ok_or_else(|| invalid("EXR block coordinate"))?; + if *seen { + return Err(invalid("duplicate EXR block")); + } + *seen = true; + let mut packed = Vec::new(); + packed.try_reserve_exact(n).map_err(|_| limit("EXR block allocation"))?; + packed.resize(n, 0); + for bytes in packed.chunks_mut(64 << 10) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + self.source.read_exact(bytes).map_err(invalid)?; + } + let chunk = + Chunk { layer_index: 0, compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }) }; + let block = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| UncompressedBlock::decompress_chunk(chunk, &self.meta, false))) + .map_err(|_| invalid("invalid EXR compression block"))? + .map_err(invalid)?; + let (w, h) = (block.index.pixel_size.0, block.index.pixel_size.1); + if w != self.info.width as usize + || block.index.pixel_position.0 != 0 + || block.index.pixel_position.1 != row as usize + || h != self.rows.min(self.info.height as usize - row as usize) + { + return Err(invalid("EXR block shape")); + } + let expected = w.checked_mul(h).and_then(|n| n.checked_mul(header.channels.bytes_per_pixel)).ok_or_else(|| invalid("EXR block size"))?; + if block.data.len() != expected { + return Err(invalid("EXR block sample count")); + } + let nc = if self.info.gray { 2 } else { 4 }; + let count = w.checked_mul(h).and_then(|n| n.checked_mul(nc * 4)).ok_or_else(|| limit("EXR output block size"))?; + let mut data = Vec::new(); + data.try_reserve_exact(count).map_err(|_| limit("EXR output allocation"))?; + data.resize(count, 0); + if !self.info.has_alpha { + for pixel in data.chunks_exact_mut(nc * 4) { + pixel.get_mut((nc - 1) * 4..).ok_or_else(|| invalid("alpha sample"))?.copy_from_slice(&1_f32.to_le_bytes()); + } + } + for (range, line) in LineIndex::lines_in_block(block.index, &header.channels) { + if cancel() { + return Err(invalid("EXR import cancelled")); + } + let Some(channel) = self.channels.get(line.channel).copied().flatten() else { + continue; + }; + let sample = header.channels.list.get(line.channel).ok_or_else(|| invalid("EXR channel"))?.sample_type; + let input = block.data.get(range).ok_or_else(|| invalid("EXR row size"))?; + let y = line.position.1.checked_sub(block.index.pixel_position.1).ok_or_else(|| invalid("EXR row coordinate"))?; + let output = data.get_mut(y * w * nc * 4..(y + 1) * w * nc * 4).ok_or_else(|| invalid("EXR row coordinate"))?; + for (bytes, pixel) in input.chunks_exact(sample.bytes_per_sample()).zip(output.chunks_exact_mut(nc * 4)) { + let value = match sample { + SampleType::F16 => half::f16::from_bits(u16::from_ne_bytes(bytes.try_into().map_err(invalid)?)).to_f32(), + SampleType::F32 => f32::from_ne_bytes(bytes.try_into().map_err(invalid)?), + SampleType::U32 => u32::from_ne_bytes(bytes.try_into().map_err(invalid)?) as f32, + }; + pixel.get_mut(channel * 4..channel * 4 + 4).ok_or_else(|| invalid("EXR channel"))?.copy_from_slice(&value.to_le_bytes()); + } + } + self.next += 1; + Ok(Some(Band { x: 0, y: row as u32, width: w as u32, height: h as u32, data })) + } +} diff --git a/crates/image-codecs/src/fidelity.rs b/crates/image-codecs/src/fidelity.rs new file mode 100644 index 000000000..984f2ac45 --- /dev/null +++ b/crates/image-codecs/src/fidelity.rs @@ -0,0 +1,282 @@ +//! Encode planning and fidelity warnings. `encode` and `fidelity_warnings` +//! share [`plan`], so a warning is emitted exactly when the encoder will +//! actually transform or drop something. + +use std::fmt; + +use crate::codecs::dpx; +use crate::format::{Format, caps}; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::{DdsCompression, EncodeOptions}; + +/// Something that will be lost or altered when writing an image to a format. +#[derive(Debug, Clone, PartialEq)] +pub enum FidelityWarning { + /// The format cannot be written in this build. + WriteUnsupported { + format: Format, + }, + /// Bit depth / precision reduced, e.g. 16-bit → 8-bit or f32 → f16. + DepthReduced { + from: SampleType, + to: SampleType, + }, + /// Samples will be stored with fewer bits than their sample type holds: DPX at 10 or 12 + /// bits, Cineon at 10. + BitsReduced { + from: SampleType, + bits: u8, + }, + /// Float samples outside `[0, 1]` will be clipped by integer storage. + RangeClipped, + /// Non-opaque alpha will be discarded. + AlphaDiscarded, + /// Alpha will be reduced to fully transparent / fully opaque (GIF, DDS BC1, IFF). + AlphaBinarized, + /// CMYK data will be converted (naively, not colour-managed) to another model. + CmykConverted { + to: ChannelLayout, + }, + /// L*a*b* data will be converted to another model (sRGB, D50 to D65). + LabConverted { + to: ChannelLayout, + }, + /// Colour data will be reduced to grayscale. + ColorToGray, + /// The image will be quantized to a 256-colour palette. + PaletteQuantized, + /// The format's encoder is lossy. + LossyCompression, + IccDropped, + ExifDropped, + XmpDropped, + DpiDropped, + TextDropped, + /// Metadata larger than the format can hold in one piece (`what`: "EXIF", "XMP") will be + /// dropped. + MetadataTooLarge { + what: &'static str, + bytes: usize, + }, + /// Image exceeds the format's maximum size; encoding will fail. + DimensionsExceeded { + max_width: u32, + max_height: u32, + }, +} + +impl FidelityWarning { + /// `true` for warnings that make encoding fail outright. + pub fn is_fatal(&self) -> bool { + matches!(self, FidelityWarning::WriteUnsupported { .. } | FidelityWarning::DimensionsExceeded { .. }) + } +} + +fn depth_name(s: SampleType) -> &'static str { + match s { + SampleType::U8 => "8-bit", + SampleType::U16 => "16-bit", + SampleType::F16 => "16-bit float", + SampleType::F32 => "32-bit float", + } +} + +impl fmt::Display for FidelityWarning { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use FidelityWarning::*; + match self { + WriteUnsupported { format } => { + write!(f, "{} cannot be written in this build", format.name()) + } + DepthReduced { from, to } => write!(f, "{} will be reduced to {}", depth_name(*from), depth_name(*to)), + BitsReduced { from, bits } => write!(f, "{} will be reduced to {bits}-bit", depth_name(*from)), + RangeClipped => write!(f, "HDR values outside 0..1 will be clipped"), + AlphaDiscarded => write!(f, "alpha will be discarded"), + AlphaBinarized => write!(f, "alpha will be reduced to on/off transparency"), + CmykConverted { to } => { + write!(f, "CMYK will be converted to {to:?} (not colour-managed)") + } + LabConverted { to } => write!(f, "Lab will be converted to {to:?} (sRGB)"), + ColorToGray => write!(f, "colour will be converted to grayscale"), + PaletteQuantized => write!(f, "colours will be quantized to a 256-colour palette"), + LossyCompression => write!(f, "lossy compression"), + IccDropped => write!(f, "ICC profile not supported; it will be dropped"), + ExifDropped => write!(f, "EXIF not supported; it will be dropped"), + XmpDropped => write!(f, "XMP not supported; it will be dropped"), + DpiDropped => write!(f, "resolution (DPI) not supported; it will be dropped"), + TextDropped => write!(f, "text metadata not supported; it will be dropped"), + MetadataTooLarge { what, bytes } => write!(f, "{what} ({bytes} bytes) is too large for the format; it will be dropped"), + DimensionsExceeded { max_width, max_height } => { + write!(f, "image exceeds the format maximum of {max_width}x{max_height}") + } + } + } +} + +/// Target storage chosen for an image/format pair. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) struct Plan { + pub layout: ChannelLayout, + pub sample: SampleType, +} + +fn pick_sample(src: SampleType, depths: &[SampleType]) -> SampleType { + if depths.contains(&src) { + return src; + } + let pref: &[SampleType] = match src { + // Integers widen into float losslessly; F32 represents U8/U16 exactly. + SampleType::U8 => &[SampleType::U16, SampleType::F32, SampleType::F16], + SampleType::U16 => &[SampleType::F32, SampleType::U8, SampleType::F16], + SampleType::F16 => &[SampleType::F32, SampleType::U16, SampleType::U8], + SampleType::F32 => &[SampleType::F16, SampleType::U16, SampleType::U8], + }; + pref.iter().copied().find(|s| depths.contains(s)).unwrap_or(depths[0]) +} + +fn pick_layout(src: ChannelLayout, layouts: &[ChannelLayout], alpha: bool) -> ChannelLayout { + let mut cand = src; + if !alpha { + cand = cand.without_alpha(); + } + if layouts.contains(&cand) { + return cand; + } + // CMYK and Lab fall back to RGB; gray expands to RGB. + if cand.is_cmyk() || cand.is_lab() || cand.is_gray() { + cand = if cand.has_alpha() { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; + } + if layouts.contains(&cand) { + return cand; + } + if layouts.contains(&cand.with_alpha()) { + return cand.with_alpha(); + } + if layouts.contains(&cand.without_alpha()) { + return cand.without_alpha(); + } + layouts[0] +} + +pub(crate) fn plan(image: &Image, format: Format, opts: &EncodeOptions) -> Plan { + let c = caps(format); + let (layout, sample) = (image.layout(), image.sample_type()); + match format { + // PNM: integer data can use any layout (PAM); float goes to PFM + // which only has gray and RGB. + Format::Pnm if sample.is_float() => Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 }, + // DPX has no luma + alpha descriptor: gray with alpha is written as luma, alpha dropped. + // The sample type follows the chosen bits (U8 for 8, U16 for 10, 12 and 16). + Format::Dpx => Plan { + layout: if layout == ChannelLayout::GrayA { ChannelLayout::Gray } else { pick_layout(layout, c.layouts, c.alpha) }, + sample: dpx::plan_sample(opts.dpx_bits, sample), + }, + // CIELab has no float form: float Lab is written with 16 bits. + Format::Tiff if layout.is_lab() && sample.is_float() => Plan { layout: pick_layout(layout, c.layouts, c.alpha), sample: SampleType::U16 }, + // A block-compressed DDS stores RGBA whatever the source; uncompressed keeps gray and RGB. + Format::Dds if opts.dds.compression.is_block() => Plan { layout: ChannelLayout::Rgba, sample: SampleType::U8 }, + _ => Plan { layout: pick_layout(layout, c.layouts, c.alpha), sample: pick_sample(sample, c.depths) }, + } +} + +/// Warnings for writing `image` as `format` with default options. +pub fn fidelity_warnings(image: &Image, format: Format) -> Vec { + fidelity_warnings_with(image, format, &EncodeOptions::default()) +} + +/// Warnings for writing `image` as `format` with the given options. +pub fn fidelity_warnings_with(image: &Image, format: Format, opts: &EncodeOptions) -> Vec { + use FidelityWarning as W; + let c = caps(format); + let mut w = Vec::new(); + if !c.write { + w.push(W::WriteUnsupported { format }); + return w; + } + if let Some((mw, mh)) = format.max_dimensions() + && (image.width() > mw || image.height() > mh) + { + w.push(W::DimensionsExceeded { max_width: mw, max_height: mh }); + } + let p = plan(image, format, opts); + let (sl, ss) = (image.layout(), image.sample_type()); + + // Depth + let lossy_depth = match (ss, p.sample) { + (a, b) if a == b => false, + (SampleType::U8, _) => false, + (SampleType::U16, SampleType::F32) => false, + (SampleType::F16, SampleType::F32) => false, + _ => true, + }; + // DPX at 10 or 12 bits and Cineon keep fewer bits than their U16 plan: float samples always + // lose precision there, 16-bit ones unless they are already code values at that depth. + if let Some(bits) = dpx::packed_bits(format, opts).filter(|_| ss != SampleType::U8) { + if ss.is_float() || !dpx::keeps_u16_samples(&image.converted(p.layout, SampleType::U16), bits) { + w.push(W::BitsReduced { from: ss, bits }); + } + } else if lossy_depth { + w.push(W::DepthReduced { from: ss, to: p.sample }); + } + // FITS stores floats as 0..1 (see `codecs::fits`), so out-of-range values are clipped too. + if ss.is_float() && (!p.sample.is_float() || format == Format::Fits) && image.has_out_of_range() { + w.push(W::RangeClipped); + } + + // Layout + if sl.has_alpha() && !p.layout.has_alpha() && image.has_translucency() { + w.push(W::AlphaDiscarded); + } + // An IFF/ILBM mask plane holds one bit per pixel. + let binary_alpha = matches!(format, Format::Gif | Format::Iff) || (format == Format::Dds && opts.dds.compression == DdsCompression::Bc1); + if binary_alpha && p.layout.has_alpha() && image.has_translucency() { + w.push(W::AlphaBinarized); + } + if sl.is_cmyk() && !p.layout.is_cmyk() { + w.push(W::CmykConverted { to: p.layout }); + } + if sl.is_lab() && !p.layout.is_lab() { + w.push(W::LabConverted { to: p.layout }); + } + if !sl.is_gray() && p.layout.is_gray() { + w.push(W::ColorToGray); + } + + // Compression + if format == Format::Gif { + w.push(W::PaletteQuantized); + } else if c.lossy || (format == Format::WebP && !opts.webp_lossless) || (format == Format::Dds && opts.dds.compression.is_block()) { + w.push(W::LossyCompression); + } + + // Metadata + if image.icc.is_some() && opts.embed_icc && (!c.icc || !sl.same_model(p.layout)) { + w.push(W::IccDropped); + } + if opts.embed_metadata { + let m = &image.meta; + if m.exif.is_some() && !c.exif { + w.push(W::ExifDropped); + } + if m.xmp.is_some() && !c.xmp { + w.push(W::XmpDropped); + } + if format == Format::Jpeg { + if let Some(exif) = m.exif.as_deref().filter(|e| crate::codecs::jpeg::exif_segment(e, None).is_none()) { + w.push(W::MetadataTooLarge { what: "EXIF", bytes: exif.len() }); + } + if let Some(xmp) = m.xmp.as_deref().filter(|x| crate::codecs::jpeg::xmp_segment(x, None).is_none()) { + w.push(W::MetadataTooLarge { what: "XMP", bytes: xmp.len() }); + } + } + if m.dpi.is_some() && (!c.dpi || (format == Format::WebP && !crate::codecs::webp::keeps_dpi(image))) { + w.push(W::DpiDropped); + } + // DPX keeps its own header fields, not free-form text. + let text_kept = |k: &str| c.text && (format != Format::Dpx || crate::codecs::dpx::KEYS.contains(&k)); + if m.text.iter().any(|(k, _)| !text_kept(k)) { + w.push(W::TextDropped); + } + } + w +} diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs new file mode 100644 index 000000000..20a520e31 --- /dev/null +++ b/crates/image-codecs/src/format.rs @@ -0,0 +1,425 @@ +//! Format identification and capability declarations. + +use crate::image::{ChannelLayout, SampleType}; + +/// Supported (or known) raster file formats. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Format { + Png, + Jpeg, + Tiff, + WebP, + Gif, + Bmp, + Tga, + Ico, + /// Netpbm family: PBM/PGM/PPM (P1–P6), PAM (P7) and PFM (`PF`/`Pf`). + Pnm, + /// ZSoft Paintbrush: 1/2/4-bit planar palettes, 8-bit indexed and 24-bit RGB. + Pcx, + /// X PixMap icons (XPM2 plain text and XPM3 C-string arrays). + Xpm, + /// DirectDraw Surface: a 2D texture, uncompressed or BC1–BC3. Reading takes the top mip + /// level; writing can add the full mip chain ([`crate::DdsOptions`]). + Dds, + Qoi, + OpenExr, + /// Radiance RGBE. + Hdr, + /// Encode-only behind the `avif` feature; see [`ASYMMETRIC_EXCEPTIONS`]. + Avif, + /// HEIF/HEIC (HEVC-coded): read-only; see [`ASYMMETRIC_EXCEPTIONS`]. + Heif, + /// FITS (Flexible Image Transport System), the astronomy format: gray or RGB image data. + Fits, + /// DPX (SMPTE 268M Digital Picture Exchange): luma, RGB or RGBA film and video frames. + Dpx, + /// JPEG XL: lossless export-only until the separate importer lands. + Jxl, + /// Kodak Cineon (`.cin`, the 10-bit film-scan format DPX grew from). + Cineon, + /// DICOM single-frame images (uncompressed): read-only. + Dicom, + /// Amiga IFF/ILBM (`FORM ... ILBM`): planar bitplanes with a palette or 24-bit RGB. + Iff, +} + +/// What a format can hold **and** what this crate reads/writes for it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FormatCaps { + /// Decoding is available in this build. + pub read: bool, + /// Encoding is available in this build. + pub write: bool, + /// Sample types stored without conversion. + pub depths: &'static [SampleType], + /// Layouts stored *and read back* without conversion. + pub layouts: &'static [ChannelLayout], + pub alpha: bool, + pub icc: bool, + pub exif: bool, + pub xmp: bool, + /// Physical resolution (DPI) is preserved. + pub dpi: bool, + /// Free-form key/value text is preserved. + pub text: bool, + /// The container can hold animation/multiple frames. This crate only + /// decodes the first frame and encodes a single frame. + pub animation: bool, + /// Our encoder is lossy (quantization or lossy compression). + pub lossy: bool, +} + +/// Formats that are enabled but intentionally not symmetric, with the reason. +/// The test-suite asserts that every other enabled format is read+write. +pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ + (Format::Xpm, "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead."), + ( + Format::Dicom, + "DICOM single-frame images are imported for medical review; this crate has no DICOM writer, so save an imported frame as TIFF or PNG instead.", + ), + ( + Format::Pcx, + "PCX is supported for opening legacy paint and clipart assets. New files should use PNG; writing obsolete indexed and planar variants is intentionally not provided.", + ), + ( + Format::Avif, + "AVIF encode uses ravif (pure Rust) but decoding requires dav1d (C); read stays unsupported \ + until a pure-Rust AV1 decoder is viable. Only enabled with the non-default `avif` feature.", + ), + (Format::Jxl, "Lossless JPEG XL export uses the pure-Rust zune-jpegxl encoder; JPEG XL reading is tracked separately and is not yet available."), + ( + Format::Heif, + "HEIC decode uses heic-rs (pure Rust, in the optional craft-heif crate behind the \ + non-default `heif` feature, which official builds enable), so iPhone and Mac photos open; \ + writing needs an HEVC encoder, and the mature ones (x265, libheif) are C, so write stays \ + unsupported. Without the feature HEIF is detected but neither read nor written.", + ), +]; + +use ChannelLayout as L; +use SampleType as S; + +const RGB_GRAY: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba]; +const ALL_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA]; +/// TIFF also stores CIE L*a*b* (CIELab). +const TIFF_LAYOUTS: &[ChannelLayout] = &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA, L::Lab, L::LabA]; + +impl Format { + /// Every format known to the crate (enabled or not). + pub const ALL: [Format; 23] = [ + Format::Png, + Format::Jpeg, + Format::Jxl, + Format::Tiff, + Format::WebP, + Format::Gif, + Format::Bmp, + Format::Tga, + Format::Ico, + Format::Pnm, + Format::Pcx, + Format::Xpm, + Format::Dds, + Format::Qoi, + Format::OpenExr, + Format::Hdr, + Format::Avif, + Format::Heif, + Format::Fits, + Format::Dpx, + Format::Cineon, + Format::Dicom, + Format::Iff, + ]; + + pub fn caps(self) -> FormatCaps { + caps(self) + } + + /// Readable or writable in this build. + pub fn is_enabled(self) -> bool { + let c = caps(self); + c.read || c.write + } + + pub fn name(self) -> &'static str { + match self { + Format::Png => "PNG", + Format::Jpeg => "JPEG", + Format::Tiff => "TIFF", + Format::WebP => "WebP", + Format::Gif => "GIF", + Format::Bmp => "BMP", + Format::Tga => "TGA", + Format::Ico => "ICO", + Format::Pnm => "Netpbm", + Format::Pcx => "PCX", + Format::Xpm => "XPM", + Format::Dds => "DDS", + Format::Qoi => "QOI", + Format::OpenExr => "OpenEXR", + Format::Hdr => "Radiance HDR", + Format::Avif => "AVIF", + Format::Heif => "HEIF", + Format::Fits => "FITS", + Format::Dpx => "DPX", + Format::Jxl => "JPEG XL", + Format::Cineon => "Cineon", + Format::Dicom => "DICOM", + Format::Iff => "Amiga IFF", + } + } + + /// Recognized file extensions (lowercase, without dot); the first one is + /// the preferred extension for writing. + pub fn extensions(self) -> &'static [&'static str] { + match self { + Format::Png => &["png", "apng"], + Format::Jpeg => &["jpg", "jpeg", "jpe", "jfif"], + Format::Tiff => &["tif", "tiff"], + Format::WebP => &["webp"], + Format::Gif => &["gif"], + Format::Bmp => &["bmp", "dib"], + Format::Tga => &["tga", "icb", "vda", "vst"], + Format::Ico => &["ico"], + Format::Pnm => &["pnm", "pbm", "pgm", "ppm", "pam", "pfm"], + Format::Pcx => &["pcx"], + Format::Xpm => &["xpm"], + Format::Dds => &["dds"], + Format::Qoi => &["qoi"], + Format::OpenExr => &["exr"], + Format::Hdr => &["hdr"], + Format::Avif => &["avif"], + Format::Heif => &["heic", "heif", "hif"], + Format::Fits => &["fits", "fit", "fts"], + Format::Dpx => &["dpx"], + Format::Jxl => &["jxl"], + Format::Cineon => &["cin"], + Format::Dicom => &["dcm", "dicom", "dc3"], + Format::Iff => &["iff", "lbm", "ilbm"], + } + } + + pub fn mime_type(self) -> &'static str { + match self { + Format::Png => "image/png", + Format::Jpeg => "image/jpeg", + Format::Tiff => "image/tiff", + Format::WebP => "image/webp", + Format::Gif => "image/gif", + Format::Bmp => "image/bmp", + Format::Tga => "image/x-tga", + Format::Ico => "image/x-icon", + Format::Pnm => "image/x-portable-anymap", + Format::Pcx => "image/x-pcx", + Format::Xpm => "image/x-xpixmap", + Format::Dds => "image/vnd-ms.dds", + Format::Qoi => "image/qoi", + Format::OpenExr => "image/x-exr", + Format::Hdr => "image/vnd.radiance", + Format::Avif => "image/avif", + Format::Heif => "image/heif", + Format::Fits => "image/fits", + Format::Dpx => "image/x-dpx", + Format::Jxl => "image/jxl", + Format::Cineon => "image/x-cin", + Format::Dicom => "application/dicom", + Format::Iff => "image/x-iff", + } + } + + /// Maximum encodable dimensions, if the format has a hard limit. + pub fn max_dimensions(self) -> Option<(u32, u32)> { + match self { + Format::Jpeg | Format::Gif | Format::Iff => Some((65535, 65535)), + Format::WebP => Some((16384, 16384)), + Format::Ico => Some((256, 256)), + _ => None, + } + } +} + +/// Capability table. `read`/`write` reflect the current build's features. +pub fn caps(format: Format) -> FormatCaps { + let base = FormatCaps { + read: true, + write: true, + depths: &[S::U8], + layouts: &[L::Rgb, L::Rgba], + alpha: true, + icc: false, + exif: false, + xmp: false, + dpi: false, + text: false, + animation: false, + lossy: false, + }; + match format { + Format::Png => { + FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, icc: true, exif: true, xmp: true, dpi: true, text: true, animation: true, ..base } + } + Format::Jpeg => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base }, + Format::Tiff => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base }, + // Resolution travels in EXIF (WebP has no field of its own). + Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, + Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, + Format::Bmp => base, + Format::Tga => FormatCaps { layouts: RGB_GRAY, ..base }, + Format::Ico => FormatCaps { layouts: &[L::Rgba], ..base }, + Format::Pnm => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: ALL_LAYOUTS, ..base }, + Format::Pcx => FormatCaps { read: true, write: false, layouts: &[L::Rgb], alpha: false, ..base }, + Format::Xpm => FormatCaps { read: true, write: false, layouts: &[L::Rgba], ..base }, + // Uncompressed by default; a block compression is lossy (see `fidelity_warnings_with`). + Format::Dds => FormatCaps { layouts: RGB_GRAY, ..base }, + Format::Qoi => base, + Format::OpenExr => FormatCaps { depths: &[S::F16, S::F32], layouts: RGB_GRAY, ..base }, + Format::Hdr => FormatCaps { depths: &[S::F32], layouts: &[L::Rgb], alpha: false, lossy: true, ..base }, + Format::Avif => FormatCaps { read: false, write: cfg!(feature = "avif"), lossy: true, ..base }, + Format::Heif => { + FormatCaps { read: cfg!(feature = "heif"), write: false, depths: &[S::U8, S::U16], icc: true, exif: true, xmp: true, lossy: true, ..base } + } + Format::Fits => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: &[L::Gray, L::Rgb], alpha: false, text: true, ..base }, + // `text`: the DPX header fields only (see `fidelity_warnings_with`). + Format::Dpx => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], text: true, ..base }, + // GrayA is widened to RGBA at the same depth; the 16-bit alpha header is corrected. + Format::Jxl => FormatCaps { read: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb, L::Rgba], ..base }, + Format::Cineon => FormatCaps { depths: &[S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, + Format::Dicom => FormatCaps { read: true, write: false, depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, + Format::Iff => FormatCaps { depths: &[S::U8], layouts: &[L::Rgb, L::Rgba], alpha: true, ..base }, + } +} + +/// Identify a format from its leading bytes (magic numbers). TGA has no +/// magic and is recognized last, by header plausibility. +pub fn detect(bytes: &[u8]) -> Option { + let b = bytes; + if b.starts_with(b"\x89PNG\r\n\x1a\n") { + return Some(Format::Png); + } + if b.starts_with(&[0xFF, 0xD8, 0xFF]) { + return Some(Format::Jpeg); + } + // JPEG XL bare codestream and ISO/IEC 18181-2 container signature box. + if b.starts_with(&[0xFF, 0x0A]) || b.starts_with(b"\0\0\0\x0cJXL \r\n\x87\n") { + return Some(Format::Jxl); + } + if b.starts_with(b"II*\0") || b.starts_with(b"MM\0*") || b.starts_with(b"II+\0") || b.starts_with(b"MM\0+") { + return Some(Format::Tiff); + } + if b.len() >= 12 && &b[0..4] == b"RIFF" && &b[8..12] == b"WEBP" { + return Some(Format::WebP); + } + if b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a") { + return Some(Format::Gif); + } + if b.starts_with(b"qoif") { + return Some(Format::Qoi); + } + if b.starts_with(b"DDS ") { + return Some(Format::Dds); + } + if b.starts_with(&[0x76, 0x2F, 0x31, 0x01]) { + return Some(Format::OpenExr); + } + if b.starts_with(b"#?RADIANCE") || b.starts_with(b"#?RGBE") { + return Some(Format::Hdr); + } + if b.len() >= 12 && &b[4..8] == b"ftyp" { + if is_avif_ftyp(b) { + return Some(Format::Avif); + } + if is_heif_ftyp(b) { + return Some(Format::Heif); + } + } + if b.len() >= 14 && b.starts_with(b"BM") { + return Some(Format::Bmp); + } + if b.len() >= 6 && b[0..4] == [0, 0, 1, 0] && (b[4] != 0 || b[5] != 0) { + return Some(Format::Ico); + } + if b.len() >= 3 && b[0] == b'P' && matches!(b[1], b'1'..=b'7' | b'F' | b'f') && b[2].is_ascii_whitespace() { + return Some(Format::Pnm); + } + if b.starts_with(b"SDPX") || b.starts_with(b"XPDS") { + return Some(Format::Dpx); + } + if crate::codecs::dpx::is_cineon(b) { + return Some(Format::Cineon); + } + if crate::codecs::dicom::is_dicom(b) { + return Some(Format::Dicom); + } + if crate::codecs::iff::looks_like_iff(b) { + return Some(Format::Iff); + } + if b.starts_with(b"SIMPLE =") { + return Some(Format::Fits); + } + if b.starts_with(b"/* XPM */") || b.starts_with(b"! XPM2\n") || b.starts_with(b"! XPM2\r\n") { + return Some(Format::Xpm); + } + if crate::codecs::pcx::looks_like_pcx(b) { + return Some(Format::Pcx); + } + if looks_like_tga(b) { + return Some(Format::Tga); + } + None +} + +/// The `ftyp` brands: the major brand, then the compatible ones (the minor version between them is skipped). +fn ftyp_brands(b: &[u8]) -> impl Iterator { + let size = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + let end = size.clamp(12, b.len().min(64)); + b[8..end].as_chunks::<4>().0.iter().enumerate().filter(|(i, _)| *i != 1).map(|(_, c)| c) +} + +fn is_avif_ftyp(b: &[u8]) -> bool { + ftyp_brands(b).any(|c| c == b"avif" || c == b"avis") +} + +/// HEVC-coded HEIF (`heic`, `heix`, ...) or a generic HEIF file (`mif1`, `msf1`, `miaf`) that is +/// not AVIF (checked first). Sequence-only files (`hevc`, `msf1`) are recognised so that opening +/// one says what is unsupported instead of "unrecognized format". +fn is_heif_ftyp(b: &[u8]) -> bool { + ftyp_brands(b).any(|c| matches!(c, b"heic" | b"heix" | b"heim" | b"heis" | b"hevc" | b"hevx" | b"mif1" | b"msf1" | b"miaf")) +} + +fn looks_like_tga(b: &[u8]) -> bool { + if b.len() < 18 { + return false; + } + if b.len() >= 26 && &b[b.len() - 18..] == b"TRUEVISION-XFILE.\0" { + return true; + } + let cmap_type = b[1]; + let img_type = b[2]; + let cmap_len = u16::from_le_bytes([b[5], b[6]]); + let cmap_depth = b[7]; + let w = u16::from_le_bytes([b[12], b[13]]); + let h = u16::from_le_bytes([b[14], b[15]]); + let depth = b[16]; + let desc = b[17]; + let type_ok = matches!(img_type, 1 | 2 | 3 | 9 | 10 | 11); + let cmap_ok = match cmap_type { + 0 => cmap_len == 0 && cmap_depth == 0 && !matches!(img_type, 1 | 9), + 1 => matches!(img_type, 1 | 9) && cmap_len > 0 && matches!(cmap_depth, 15 | 16 | 24 | 32), + _ => false, + }; + let depth_ok = match img_type { + 1 | 9 => depth == 8, + 3 | 11 => matches!(depth, 8 | 16), + _ => matches!(depth, 15 | 16 | 24 | 32), + }; + type_ok && cmap_ok && depth_ok && w > 0 && h > 0 && desc & 0xC0 == 0 +} + +/// Look up a format by file extension (case-insensitive, with or without a +/// leading dot, or a full file name/path). +pub fn from_extension(ext: &str) -> Option { + let ext = ext.rsplit(['.', '/', '\\']).next().unwrap_or(ext).to_ascii_lowercase(); + Format::ALL.into_iter().find(|f| f.extensions().contains(&ext.as_str())) +} diff --git a/crates/image-codecs/src/image.rs b/crates/image-codecs/src/image.rs new file mode 100644 index 000000000..fb3294948 --- /dev/null +++ b/crates/image-codecs/src/image.rs @@ -0,0 +1,1028 @@ +//! The crate's own flat raster image type. +//! +//! Samples are stored interleaved (`RGBARGBA…`), row-major, top-to-bottom, +//! in **native endianness**. Integer samples are unsigned and normalized to +//! `[0, max]`; float samples are nominally `[0, 1]` but may exceed it (HDR). + +use std::fmt; + +use crate::error::CodecError; +use crate::format::Format; +use half::f16; + +/// Channel layout of an [`Image`]. Alpha, when present, is always the last +/// channel and is *straight* (not premultiplied). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum ChannelLayout { + Gray, + GrayA, + Rgb, + Rgba, + /// Ink amounts, `0 = no ink`, `max = full ink` (not Adobe-inverted). + Cmyk, + CmykA, + /// CIE L*a*b* (D50), as PhotoCraft's Lab documents hold it: L* 0..100 as 0..1, a* and b* + /// −128..127 as `(v + 128) / 255` (Photoshop's unsigned encoding). + Lab, + LabA, +} + +impl ChannelLayout { + pub const ALL: [ChannelLayout; 8] = [ + ChannelLayout::Gray, + ChannelLayout::GrayA, + ChannelLayout::Rgb, + ChannelLayout::Rgba, + ChannelLayout::Cmyk, + ChannelLayout::CmykA, + ChannelLayout::Lab, + ChannelLayout::LabA, + ]; + + /// Total number of channels including alpha. + pub const fn channels(self) -> usize { + match self { + ChannelLayout::Gray => 1, + ChannelLayout::GrayA => 2, + ChannelLayout::Rgb | ChannelLayout::Lab => 3, + ChannelLayout::Rgba | ChannelLayout::Cmyk | ChannelLayout::LabA => 4, + ChannelLayout::CmykA => 5, + } + } + + /// Number of colour (non-alpha) channels. + pub const fn color_channels(self) -> usize { + self.channels() - self.has_alpha() as usize + } + + pub const fn has_alpha(self) -> bool { + matches!(self, ChannelLayout::GrayA | ChannelLayout::Rgba | ChannelLayout::CmykA | ChannelLayout::LabA) + } + + pub const fn is_gray(self) -> bool { + matches!(self, ChannelLayout::Gray | ChannelLayout::GrayA) + } + + pub const fn is_rgb(self) -> bool { + matches!(self, ChannelLayout::Rgb | ChannelLayout::Rgba) + } + + pub const fn is_cmyk(self) -> bool { + matches!(self, ChannelLayout::Cmyk | ChannelLayout::CmykA) + } + + pub const fn is_lab(self) -> bool { + matches!(self, ChannelLayout::Lab | ChannelLayout::LabA) + } + + /// The same colour model with alpha added. + pub const fn with_alpha(self) -> Self { + match self { + ChannelLayout::Gray | ChannelLayout::GrayA => ChannelLayout::GrayA, + ChannelLayout::Rgb | ChannelLayout::Rgba => ChannelLayout::Rgba, + ChannelLayout::Cmyk | ChannelLayout::CmykA => ChannelLayout::CmykA, + ChannelLayout::Lab | ChannelLayout::LabA => ChannelLayout::LabA, + } + } + + /// The same colour model without alpha. + pub const fn without_alpha(self) -> Self { + match self { + ChannelLayout::Gray | ChannelLayout::GrayA => ChannelLayout::Gray, + ChannelLayout::Rgb | ChannelLayout::Rgba => ChannelLayout::Rgb, + ChannelLayout::Cmyk | ChannelLayout::CmykA => ChannelLayout::Cmyk, + ChannelLayout::Lab | ChannelLayout::LabA => ChannelLayout::Lab, + } + } + + /// `true` if both layouts share a colour model (ignoring alpha). + pub const fn same_model(self, other: Self) -> bool { + (self.is_gray() && other.is_gray()) || (self.is_rgb() && other.is_rgb()) || (self.is_cmyk() && other.is_cmyk()) || (self.is_lab() && other.is_lab()) + } +} + +/// Storage type of one sample. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum SampleType { + U8, + U16, + /// IEEE 754 half float, stored as its 16 bit pattern. + F16, + F32, +} + +impl SampleType { + pub const ALL: [SampleType; 4] = [SampleType::U8, SampleType::U16, SampleType::F16, SampleType::F32]; + + pub const fn bytes(self) -> usize { + match self { + SampleType::U8 => 1, + SampleType::U16 | SampleType::F16 => 2, + SampleType::F32 => 4, + } + } + + pub const fn is_float(self) -> bool { + matches!(self, SampleType::F16 | SampleType::F32) + } +} + +/// Basic, format-independent metadata. +#[derive(Debug, Clone, Default, PartialEq)] +pub struct Metadata { + /// Raw EXIF payload in TIFF structure (starting with `II*\0`/`MM\0*`), + /// **without** the JPEG `Exif\0\0` prefix. + pub exif: Option>, + /// XMP packet as UTF-8. + pub xmp: Option, + /// Horizontal and vertical resolution in dots per inch. + pub dpi: Option<(f32, f32)>, + /// Free-form key/value text (PNG tEXt/iTXt, TIFF ASCII tags). + pub text: Vec<(String, String)>, + /// Photoshop image resources (`8BIM` resource blocks): TIFF tag 34377. Kept opaque here; + /// `photocraft-io` reads and writes them through `photocraft-psd`. + pub photoshop_resources: Option>, + /// Photoshop layer data (TIFF tag 37724 `ImageSourceData`): a layered TIFF's layers. Kept + /// opaque here; `photocraft-io` reads and writes it through `photocraft-psd`. + pub photoshop_layers: Option>, + /// Samples per pixel beyond the colour channels and the one alpha sample. TIFF extra + /// samples (spot colours, extra alpha channels) the pixel layout does not hold. Each entry + /// is one sample per pixel, native endian, in the image's [`SampleType`], row-major. + pub extra_samples: Vec, +} + +/// How many extra TIFF samples an encoded file may carry. Decoders keep at most this many. +/// Sixteen covers a UV-DTF stack (white, varnish, gloss, primer, and further spots) without +/// approaching the PSD 56-channel limit. +pub const MAX_EXTRA_SAMPLES: usize = 16; + +/// One TIFF extra sample (a spot channel or an alpha channel that is not the image alpha). +#[derive(Debug, Clone, PartialEq)] +pub struct ExtraSample { + /// TIFF ExtraSamples value: 0 unspecified, 1 associated alpha, 2 unassociated alpha. + pub kind: u16, + /// One sample per pixel, native endian, same sample type as the image. + pub data: Vec, +} + +impl Metadata { + pub fn is_empty(&self) -> bool { + self.exif.is_none() + && self.xmp.is_none() + && self.dpi.is_none() + && self.text.is_empty() + && self.photoshop_resources.is_none() + && self.photoshop_layers.is_none() + && self.extra_samples.is_empty() + } +} + +/// Something a decoder noticed about the file that the decoded pixels don't show: the file +/// held more than was decoded, or less than it should. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DecodeWarning { + /// Only the first frame of an animation was decoded; `total` counts every frame when known. + MoreFrames { total: Option }, + /// Only the first page of a multi-page file was decoded; `total` counts every page when known. + MorePages { total: Option }, + /// Only one part of a multi-part file was decoded; `total` counts every part when known. + MoreParts { total: Option }, + /// The image opened on the full `display_window` stage; the data region sits at `origin`. + DisplayWindowStage { stage: (u32, u32), origin: (i32, i32) }, + /// The image data ends early (truncated or damaged file); the decoder filled in the rest + /// (a baseline JPEG's missing rows come out grey). + Truncated { format: Format }, + /// Deep image data (per-pixel sample lists) was composited into the flat image; the samples + /// themselves (Z and other deep channels) are not part of the flat result. + DeepFlattened { format: Format, max_samples_per_pixel: u32 }, + /// Only the first image element of a DPX file was decoded; `total` counts them all. + MoreElements { total: u32 }, + /// A DPX element's transfer characteristic and colorimetric specification (SMPTE 268M codes, + /// 255 when undefined). The code values are imported as stored, without a colour transform. + DpxEncoding { transfer: u8, colorimetric: u8 }, + /// The file's `count` extra samples (spot or extra alpha channels) did not fit the allocation + /// limit next to the image, so only the image was decoded. + ExtraSamplesDropped { format: Format, count: u32 }, + /// The file has `count` extra samples past the first [`MAX_EXTRA_SAMPLES`], which are all a + /// decoder keeps; those past the cap were not imported. + ExtraSamplesOverLimit { format: Format, count: u32 }, +} + +/// The SMPTE 268M name of a transfer characteristic or colorimetric code. +fn dpx_code_name(code: u8) -> &'static str { + match code { + 0 => "user-defined", + 1 => "printing density", + 2 => "linear", + 3 => "logarithmic", + 4 => "unspecified video", + 5 => "SMPTE 274M", + 6 => "ITU-R BT.709", + 7 => "ITU-R BT.601 625-line", + 8 => "ITU-R BT.601 525-line", + 9 => "NTSC composite video", + 10 => "PAL composite video", + 11 => "Z linear", + 12 => "Z homogeneous", + 255 => "undefined", + _ => "unknown", + } +} + +impl fmt::Display for DecodeWarning { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + DecodeWarning::MoreFrames { total: Some(n) } => write!(f, "only the first of {n} frames was imported"), + DecodeWarning::MoreFrames { total: None } => write!(f, "only the first frame of the animation was imported"), + DecodeWarning::MorePages { total: Some(n) } => write!(f, "only the first of {n} pages was imported"), + DecodeWarning::MorePages { total: None } => write!(f, "only the first page of the file was imported"), + DecodeWarning::MoreParts { total: Some(n) } => write!(f, "only one of {n} EXR parts was imported"), + DecodeWarning::MoreParts { total: None } => write!(f, "only one EXR part of the file was imported"), + DecodeWarning::DisplayWindowStage { stage, origin } => { + write!(f, "opened on the full display window stage ({}x{}); the data sits at ({}, {})", stage.0, stage.1, origin.0, origin.1) + } + DecodeWarning::Truncated { format } => { + write!(f, "{} data ends early (the file is truncated or damaged); part of the image is missing", format.name()) + } + DecodeWarning::DeepFlattened { format, max_samples_per_pixel } => write!( + f, + "{format} deep data was composited into a flat image (up to {max_samples_per_pixel} samples per pixel); per-pixel depth is not kept", + format = format.name() + ), + DecodeWarning::MoreElements { total } => write!(f, "only the first of {total} image elements was imported"), + DecodeWarning::DpxEncoding { transfer, colorimetric } => write!( + f, + "DPX transfer characteristic: {} ({transfer}), colorimetric: {} ({colorimetric}); the code values were imported as stored, without a colour transform", + dpx_code_name(*transfer), + dpx_code_name(*colorimetric) + ), + DecodeWarning::ExtraSamplesDropped { format, count } => write!( + f, + "{count} extra {} file channel{} (spot or alpha) did not fit in memory and {} not imported", + format.name(), + if *count == 1 { "" } else { "s" }, + if *count == 1 { "was" } else { "were" } + ), + DecodeWarning::ExtraSamplesOverLimit { format, count } => write!( + f, + "{count} extra {} file channel{} (spot or alpha) past the first {MAX_EXTRA_SAMPLES} {} not imported", + format.name(), + if *count == 1 { "" } else { "s" }, + if *count == 1 { "was" } else { "were" } + ), + } + } +} + +/// One channel of a [`DeepImage`]: the samples of every pixel, one flat list in pixel order. +#[derive(Debug, Clone, PartialEq)] +pub struct DeepChannel { + /// The channel name as in the file (e.g. `R`, `G`, `B`, `A`, `Z`). + pub name: String, + /// The sample type as stored in the file; `samples` always holds the numeric value as + /// f32 (half and 32-bit floats exactly; 32-bit unsigned integers exactly up to 2^24). + pub sample: SampleType, + /// Every sample of the channel, in pixel order; the samples of pixel `i` are + /// `counts[i]..counts[i+1]` (see [`DeepImage::counts`]). + pub samples: Vec, +} + +/// Deep image data (OpenEXR `deepscanline`/`deeptile`): a variable-length list of samples +/// per pixel, each with its own colour, alpha and depth. All channels share the same sample +/// layout, so a sample is one index into every channel. +#[derive(Debug, Clone, PartialEq)] +pub struct DeepImage { + pub width: u32, + pub height: u32, + /// The full stage the image opens on: the EXR `display_window` size, when the file declares + /// one the data region fits into and the limits allow. `None` when absent, absurd or not + /// containing the data region — then the image opens on the data window alone, as before. + pub stage: Option<(u32, u32)>, + /// Where this image's (0, 0) sits on the stage: `data_window.position - display_window.position`. + /// `(0, 0)` when [`Self::stage`] is `None`. + pub origin: (i32, i32), + /// The sample channels in file order. + pub channels: Vec, + /// Cumulative sample counts, `len == width as usize * height as usize + 1`: pixel `i` + /// holds the samples `counts[i]..counts[i+1]` of every channel. Monotonically + /// non-decreasing, starting and ending at the total sample count. + pub counts: Vec, +} + +impl DeepImage { + /// A channel by its exact name. + pub fn channel(&self, name: &str) -> Option<&DeepChannel> { + self.channels.iter().find(|c| c.name == name) + } + + /// The samples of one pixel, as indices into every channel's `samples`. + pub fn sample_range(&self, pixel: usize) -> std::ops::Range { + let end = self.counts.get(pixel + 1).copied().unwrap_or(0); + self.counts.get(pixel).copied().unwrap_or(end)..end + } + + /// The total number of samples over all pixels. + pub fn total_samples(&self) -> u64 { + self.counts.last().copied().unwrap_or(0) + } +} + +/// One channel of an [`ExrPartInfo`]: the name as in the file (a Maya/Arnold AOV layer +/// keeps its prefix, e.g. `diffuse.R`) and the sample type that part stores it with. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExrChannelInfo { + pub name: String, + pub sample: SampleType, +} + +/// One part of a multi-part OpenEXR file (what a Maya/Arnold render writes per AOV): +/// header facts only, no pixel data. [`crate::exr_info`] lists them, and the index is +/// what [`crate::decode_exr_part`] decodes; [`crate::decode_as_with`] opens the part +/// the ranking in [`ExrPartInfo::color_rank`] prefers. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExrPartInfo { + /// Index into the part list, in file order. + pub index: usize, + /// The part `name` attribute, when the file records one (Arnold names parts after AOVs). + pub name: Option, + /// The part `view` attribute for stereo files (`left`/`right`). + pub view: Option, + pub width: u32, + pub height: u32, + /// Absolute data-window bounds: x, y, width and height. + pub data_window: (i32, i32, u32, u32), + /// Absolute display-window bounds: x, y, width and height. + pub display_window: (i32, i32, u32, u32), + /// True for `deepscanline`/`deeptile` parts; those are composited by the deep + /// decoder ([`crate::decode_deep_exr`]), not decoded part by part. + pub deep: bool, + /// True when the part is stored as tiles instead of scanlines. + pub tiled: bool, + /// The part's channels in file order. + pub channels: Vec, +} + +impl ExrPartInfo { + /// How much this part looks like a colour image the plain [`Image`] decode can show: + /// 4 RGBA, 3 RGB, 2 YA, 1 Y or any-single-channel, 0 anything else (e.g. `Z` beside `A`). + /// The colour and alpha channels must share a complete layer prefix. The highest-ranked + /// group wins; equal groups prefer unprefixed channels, then the first prefix alphabetically. + /// The auto-pick takes the highest rank; ties go to the earlier part. + pub fn color_rank(&self) -> u8 { + exr_color_channels(self.channels.iter().map(|c| c.name.as_str())).map_or(0, |(layout, _)| layout.channels() as u8) + } +} + +/// One selection policy for header ranking/probing and flat decoding: never synthesize +/// RGB(A) or YA from separate layers. OpenEXR layers use the full prefix before the last +/// dot, including nested layers; a leading or trailing dot alone does not name a layer. +pub(crate) fn exr_color_channels<'a>(names: impl IntoIterator) -> Option<(ChannelLayout, Vec)> { + #[derive(Default)] + struct Group { + r: Option, + g: Option, + b: Option, + a: Option, + y: Option, + } + + // Sorted prefixes make rank ties deterministic independently of channel order, with + // the empty (unprefixed) group first. Unsupported channels cannot complete a group. + let mut groups = std::collections::BTreeMap::<&str, Group>::new(); + let mut count = 0usize; + for (index, name) in names.into_iter().enumerate() { + count = count.saturating_add(1); + let (prefix, base) = name.rsplit_once('.').filter(|(prefix, base)| !prefix.is_empty() && !base.is_empty()).unwrap_or(("", name)); + if !matches!(base, "R" | "G" | "B" | "A" | "Y") { + continue; + } + let group = groups.entry(prefix).or_default(); + let component = match base { + "R" => &mut group.r, + "G" => &mut group.g, + "B" => &mut group.b, + "A" => &mut group.a, + "Y" => &mut group.y, + _ => continue, + }; + component.get_or_insert(index); + } + + let mut best = None; + let mut best_rank = 0; + for group in groups.values() { + let candidate = if let (Some(r), Some(g), Some(b)) = (group.r, group.g, group.b) { + match group.a { + Some(a) => (ChannelLayout::Rgba, vec![r, g, b, a]), + None => (ChannelLayout::Rgb, vec![r, g, b]), + } + } else if let Some(y) = group.y { + match group.a { + Some(a) => (ChannelLayout::GrayA, vec![y, a]), + None => (ChannelLayout::Gray, vec![y]), + } + } else { + continue; + }; + let rank = candidate.0.channels(); + if rank > best_rank { + best = Some(candidate); + best_rank = rank; + } + } + best.or_else(|| (count == 1).then(|| (ChannelLayout::Gray, vec![0]))) +} + +/// Strip an optional `layer.` prefix from a channel name (`diffuse.R` → `R`). +pub(crate) fn base_channel_name(name: &str) -> &str { + name.rsplit('.').next().unwrap_or(name) +} + +/// One Cryptomatte layer of an EXR file (Cryptomatte specification 1.2): per-pixel object or +/// material IDs with coverage, as written by Arnold, V-Ray, Redshift, Mantra/Karma or Cycles. +/// Header facts plus the parsed manifest; the samples come from +/// [`crate::decode_cryptomatte`]. +#[derive(Debug, Clone, PartialEq)] +pub struct CryptomatteLayer { + /// The part (see [`crate::exr_info`]) the channels and attributes live in. + pub part: usize, + /// The `cryptomatte//name` attribute, e.g. `CryptoAsset` or `crypto_asset`. + pub name: String, + /// The 7-hex-digit metadata key of the layer. + pub key: String, + /// The `conversion` attribute, when recorded (canonically `uint32_to_float32`). + pub conversion: Option, + /// The manifest: object names with their ID values. A sidecar manifest file is only + /// recorded in [`Self::sidecar_manifest`] and not read here (the codec is I/O free). + pub manifest: Vec<(f32, String)>, + /// The `manif_file` sidecar attribute, when the manifest is not embedded. + pub sidecar_manifest: Option, + /// The stream channel bases in file order, e.g. `["crypto_asset00", "crypto_asset01"]`; + /// each holds an ID (`.red`) and a coverage (`.green`) channel. + pub channels: Vec, +} + +impl CryptomatteLayer { + /// The manifest name for an ID value, when the manifest lists it. + pub fn name_of(&self, id: f32) -> Option<&str> { + self.manifest.iter().find(|(i, _)| *i == id).map(|(_, n)| n.as_str()) + } +} + +/// Decoded Cryptomatte samples: per pixel the (ID, coverage) pairs with coverage above zero, +/// sorted by descending coverage. The IDs are the float values as stored (compared by value, +/// never by bit pattern, like the reference readers). +#[derive(Debug, Clone, PartialEq)] +pub struct CryptomatteBuffer { + pub width: u32, + pub height: u32, + pub pixels: Vec>, +} + +impl CryptomatteBuffer { + /// The pairs of one pixel; empty for a pixel covered by nothing. + pub fn at(&self, x: u32, y: u32) -> &[(f32, f32)] { + let i = y as usize * self.width as usize + x as usize; + self.pixels.get(i).map_or(&[], |v| v) + } +} + +/// A single flat raster image. +#[derive(Debug, Clone, PartialEq)] +pub struct Image { + width: u32, + height: u32, + layout: ChannelLayout, + sample: SampleType, + data: Vec, + /// Embedded ICC profile (must describe `layout`'s colour model). + pub icc: Option>, + /// The file's CICP colour description (a HEIF `nclx`), when it has one. Only an image whose + /// `icc` is `None` relies on it. + pub cicp: Option, + pub meta: Metadata, + /// What the decoder noticed about the file: empty for a complete, single image and for + /// images built in memory. Encoders ignore it. + pub warnings: Vec, +} + +fn byte_len(width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Option { + (width as usize).checked_mul(height as usize)?.checked_mul(layout.channels())?.checked_mul(sample.bytes()) +} + +impl Image { + /// A zero-filled image, or an error if the byte size overflows `usize`. + pub fn new(width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Result { + let len = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + Ok(Image { width, height, layout, sample, data: vec![0; len], icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) + } + + /// Wrap raw interleaved, native-endian bytes. + pub fn from_raw(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, data: Vec) -> Result { + let expected = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + if data.len() != expected { + return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", data.len()))); + } + Ok(Image { width, height, layout, sample, data, icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) + } + + pub fn from_u8(width: u32, height: u32, layout: ChannelLayout, data: Vec) -> Result { + Self::from_raw(width, height, layout, SampleType::U8, data) + } + + pub fn from_u16(width: u32, height: u32, layout: ChannelLayout, data: &[u16]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::U16, bytes) + } + + pub fn from_f16(width: u32, height: u32, layout: ChannelLayout, data: &[f16]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_bits().to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::F16, bytes) + } + + pub fn from_f32(width: u32, height: u32, layout: ChannelLayout, data: &[f32]) -> Result { + let bytes = data.iter().flat_map(|v| v.to_ne_bytes()).collect(); + Self::from_raw(width, height, layout, SampleType::F32, bytes) + } + + /// Build an image from normalized f32 samples (`[0,1]` for integer + /// targets; values are clamped and rounded when quantizing). + pub fn from_normalized(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, values: &[f32]) -> Result { + let data = quantize(values, sample); + Self::from_raw(width, height, layout, sample, data) + } + + pub fn with_icc(mut self, icc: Option>) -> Self { + self.icc = icc; + self + } + + pub fn with_meta(mut self, meta: Metadata) -> Self { + self.meta = meta; + self + } + + pub fn width(&self) -> u32 { + self.width + } + pub fn height(&self) -> u32 { + self.height + } + pub fn dimensions(&self) -> (u32, u32) { + (self.width, self.height) + } + pub fn layout(&self) -> ChannelLayout { + self.layout + } + pub fn sample_type(&self) -> SampleType { + self.sample + } + pub fn pixel_count(&self) -> usize { + self.width as usize * self.height as usize + } + /// Number of samples (pixels × channels). + pub fn sample_count(&self) -> usize { + self.pixel_count() * self.layout.channels() + } + /// Raw interleaved native-endian bytes. + pub fn data(&self) -> &[u8] { + &self.data + } + pub fn data_mut(&mut self) -> &mut [u8] { + &mut self.data + } + pub fn into_data(self) -> Vec { + self.data + } + + /// Borrow samples as `u8` (only when the sample type is `U8`). + pub fn as_u8(&self) -> Option<&[u8]> { + (self.sample == SampleType::U8).then_some(&self.data[..]) + } + + /// Copy samples out as `u16` (only when the sample type is `U16`). + pub fn to_u16_samples(&self) -> Option> { + (self.sample == SampleType::U16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]])).collect()) + } + + /// Copy samples out as `f16` (only when the sample type is `F16`). + pub fn to_f16_samples(&self) -> Option> { + (self.sample == SampleType::F16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) + } + + /// Copy samples out as `f32` (only when the sample type is `F32`). + pub fn to_f32_samples(&self) -> Option> { + (self.sample == SampleType::F32).then(|| self.data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes([c[0], c[1], c[2], c[3]])).collect()) + } + + /// Sample `index` (in samples, not bytes) as a normalized f32. + pub fn sample_normalized(&self, index: usize) -> f32 { + read_normalized(&self.data, self.sample, index) + } + + /// Normalized f32 of channel `c` at pixel `(x, y)`. + pub fn get(&self, x: u32, y: u32, c: usize) -> f32 { + let idx = (y as usize * self.width as usize + x as usize) * self.layout.channels() + c; + self.sample_normalized(idx) + } + + /// All samples as normalized f32 (integers mapped to `[0,1]`). + pub fn to_normalized(&self) -> Vec { + to_normalized(&self.data, self.sample) + } + + /// `true` when the image has alpha and at least one pixel is not fully + /// opaque. + pub fn has_translucency(&self) -> bool { + if !self.layout.has_alpha() { + return false; + } + let ch = self.layout.channels(); + (0..self.pixel_count()).any(|p| self.sample_normalized(p * ch + ch - 1) < 1.0) + } + + /// `true` when this is a float image with any sample outside `[0, 1]` + /// (or NaN). + pub fn has_out_of_range(&self) -> bool { + if !self.sample.is_float() { + return false; + } + (0..self.sample_count()).any(|i| { + let v = self.sample_normalized(i); + !(0.0..=1.0).contains(&v) + }) + } + + /// Convert to another layout and/or sample type. The ICC profile is kept + /// only if the colour model is unchanged (an RGB profile on CMYK data + /// would be wrong). Metadata is kept. + pub fn convert(&self, layout: ChannelLayout, sample: SampleType) -> Image { + if layout == self.layout && sample == self.sample { + return self.clone(); + } + let data = convert_data(&self.data, self.layout, self.sample, layout, sample); + let mut meta = self.meta.clone(); + if sample != self.sample { + // Extra samples are stored in the image's sample type, so they convert with it. A + // plane of the wrong size is left as it is: importers drop it with a warning. + let plane_len = (self.width as usize).saturating_mul(self.height as usize).saturating_mul(self.sample.bytes()); + for e in meta.extra_samples.iter_mut().filter(|e| e.data.len() == plane_len) { + if let Ok(plane) = Image::from_raw(self.width, self.height, ChannelLayout::Gray, self.sample, std::mem::take(&mut e.data)) { + e.data = plane.convert(ChannelLayout::Gray, sample).data; + } + } + } + Image { + width: self.width, + height: self.height, + layout, + sample, + data, + icc: if layout.same_model(self.layout) { self.icc.clone() } else { None }, + cicp: if layout.same_model(self.layout) { self.cicp } else { None }, + meta, + warnings: self.warnings.clone(), + } + } + + /// [`Image::convert`], borrowing `self` when it already has that layout and sample type + /// (encoders use it so a full-size image isn't copied for nothing). + pub fn converted(&self, layout: ChannelLayout, sample: SampleType) -> std::borrow::Cow<'_, Image> { + if layout == self.layout && sample == self.sample { std::borrow::Cow::Borrowed(self) } else { std::borrow::Cow::Owned(self.convert(layout, sample)) } + } + + /// Interleaved RGBA, 8 bits per sample. + pub fn to_rgba8(&self) -> Vec { + self.convert(ChannelLayout::Rgba, SampleType::U8).data + } + + /// Interleaved RGBA, 16 bits per sample. + pub fn to_rgba16(&self) -> Vec { + self.convert(ChannelLayout::Rgba, SampleType::U16).to_u16_samples().unwrap_or_default() + } + + /// Interleaved RGBA as f32 (integer inputs normalized to `[0,1]`). + pub fn to_rgba_f32(&self) -> Vec { + convert_layout(&self.to_normalized(), self.layout, ChannelLayout::Rgba) + } +} + +/// The samples of `data` (`from` layout and sample type) in the `layout` and `sample` type: the +/// arithmetic of [`Image::convert`], on borrowed bytes. +fn convert_data(data: &[u8], from: ChannelLayout, from_sample: SampleType, layout: ChannelLayout, sample: SampleType) -> Vec { + if layout == from && sample == from_sample { + data.to_vec() + } else if sample == from_sample && !from.has_alpha() && layout == from.with_alpha() { + // Fast path (opening any opaque file): copy the samples, append an opaque alpha. + let bps = sample.bytes(); + let src = from.channels() * bps; + let one: Vec = match sample { + SampleType::U8 => vec![u8::MAX], + SampleType::U16 => u16::MAX.to_ne_bytes().to_vec(), + SampleType::F16 => f16::ONE.to_bits().to_ne_bytes().to_vec(), + SampleType::F32 => 1.0f32.to_ne_bytes().to_vec(), + }; + let mut out = Vec::with_capacity(data.len() / src * (src + bps)); + for px in data.chunks_exact(src) { + out.extend_from_slice(px); + out.extend_from_slice(&one); + } + out + } else if let Some(fast) = convert_int_direct(data, from, from_sample, layout, sample) { + fast + } else if layout == from { + quantize(&to_normalized(data, from_sample), sample) + } else { + let src = to_normalized(data, from_sample); + let values = convert_layout(&src, from, layout); + quantize(&values, sample) + } +} + +/// [`Image::convert`] on borrowed pixels: `rows` rows of `width` pixels in `from` (layout, sample +/// type) become the same pixels in `to`, bit for bit what the image conversion gives. Lets a +/// caller convert a band of a decoded image without copying it into an [`Image`] first. +pub fn convert_samples(src: &[u8], width: u32, rows: u32, from: (ChannelLayout, SampleType), to: (ChannelLayout, SampleType)) -> Result, CodecError> { + let expected = byte_len(width, rows, from.0, from.1).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; + if src.len() != expected { + return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", src.len(), from.0, from.1))); + } + Ok(convert_data(src, from.0, from.1, to.0, to.1)) +} + +/// Integer fast path of [`Image::convert`] for U8/U16 sources and targets whose result is +/// a pure channel copy: same colour model (alpha added as opaque, or dropped) or gray to +/// RGB. Bit-identical to the generic f32 route (see the equivalence tests) but without +/// the two intermediate f32 buffers. Returns `None` for everything else. +fn convert_int_direct(data: &[u8], from: ChannelLayout, from_sample: SampleType, layout: ChannelLayout, sample: SampleType) -> Option> { + let int = |s: SampleType| matches!(s, SampleType::U8 | SampleType::U16); + if !int(from_sample) || !int(sample) { + return None; + } + let gray_to_rgb = from.is_gray() && layout.is_rgb(); + if !gray_to_rgb && !layout.same_model(from) { + return None; + } + let one: &[u8] = match sample { + SampleType::U8 => &[u8::MAX], + _ => &u16::MAX.to_ne_bytes(), + }; + let (fc, tc) = (from.channels(), layout.channels()); + let (sb, db) = (from_sample.bytes(), sample.bytes()); + let (src_color, dst_color) = (from.color_channels(), layout.color_channels()); + let n = data.len() / (fc * sb); + let mut out = Vec::with_capacity(n.checked_mul(tc)?.checked_mul(db)?); + macro_rules! run { + ($conv:expr) => {{ + let conv = $conv; + for px in data.chunks_exact(fc * sb) { + for j in 0..dst_color { + let c = if src_color == 1 { 0 } else { j }; + conv(&px[c * sb..(c + 1) * sb], &mut out); + } + if layout.has_alpha() { + if from.has_alpha() { + conv(&px[src_color * sb..(src_color + 1) * sb], &mut out); + } else { + out.extend_from_slice(one); + } + } + } + }}; + } + match (from_sample, sample) { + (SampleType::U8, SampleType::U8) => run!(|s: &[u8], o: &mut Vec| o.push(s[0])), + (SampleType::U16, SampleType::U16) => run!(|s: &[u8], o: &mut Vec| o.extend_from_slice(s)), + (SampleType::U8, SampleType::U16) => run!(|s: &[u8], o: &mut Vec| o.extend_from_slice(&(u16::from(s[0]) * 257).to_ne_bytes())), + _ => run!(|s: &[u8], o: &mut Vec| o.push(((u32::from(u16::from_ne_bytes([s[0], s[1]])) + 128) / 257) as u8)), + } + Some(out) +} + +// --------------------------------------------------------------------------- +// Sample conversion helpers +// --------------------------------------------------------------------------- + +#[inline] +pub(crate) fn read_normalized(data: &[u8], sample: SampleType, i: usize) -> f32 { + match sample { + SampleType::U8 => data[i] as f32 / 255.0, + SampleType::U16 => u16::from_ne_bytes([data[2 * i], data[2 * i + 1]]) as f32 / 65535.0, + SampleType::F16 => f16::from_bits(u16::from_ne_bytes([data[2 * i], data[2 * i + 1]])).to_f32(), + SampleType::F32 => f32::from_ne_bytes([data[4 * i], data[4 * i + 1], data[4 * i + 2], data[4 * i + 3]]), + } +} + +pub(crate) fn to_normalized(data: &[u8], sample: SampleType) -> Vec { + match sample { + SampleType::U8 => data.iter().map(|&v| v as f32 / 255.0).collect(), + SampleType::U16 => data.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes([c[0], c[1]]) as f32 / 65535.0).collect(), + SampleType::F16 => data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]])).to_f32()).collect(), + SampleType::F32 => data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes([c[0], c[1], c[2], c[3]])).collect(), + } +} + +#[inline] +fn clamp01(v: f32) -> f32 { + if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) } +} + +pub(crate) fn quantize(values: &[f32], sample: SampleType) -> Vec { + match sample { + SampleType::U8 => values.iter().map(|&v| (clamp01(v) * 255.0).round() as u8).collect(), + SampleType::U16 => values.iter().flat_map(|&v| ((clamp01(v) * 65535.0).round() as u16).to_ne_bytes()).collect(), + SampleType::F16 => values.iter().flat_map(|&v| f16::from_f32(v).to_bits().to_ne_bytes()).collect(), + SampleType::F32 => values.iter().flat_map(|&v| v.to_ne_bytes()).collect(), + } +} + +/// Rec. 709 luma weights applied to encoded values. +const LUMA: [f32; 3] = [0.2126, 0.7152, 0.0722]; + +fn cmyk_to_rgb(c: f32, m: f32, y: f32, k: f32) -> [f32; 3] { + [(1.0 - c) * (1.0 - k), (1.0 - m) * (1.0 - k), (1.0 - y) * (1.0 - k)] +} + +fn rgb_to_cmyk(r: f32, g: f32, b: f32) -> [f32; 4] { + let k = 1.0 - r.max(g).max(b); + if k >= 1.0 { + return [0.0, 0.0, 0.0, 1.0]; + } + let d = 1.0 - k; + [(1.0 - r - k) / d, (1.0 - g - k) / d, (1.0 - b - k) / d, k] +} + +/// Convert normalized interleaved samples between layouts. Naive +/// (non-colour-managed) formulas; colour-managed conversion belongs in +/// `photocraft-color`. +pub(crate) fn convert_layout(src: &[f32], from: ChannelLayout, to: ChannelLayout) -> Vec { + if from == to { + return src.to_vec(); + } + let fc = from.channels(); + let tc = to.channels(); + let n = src.len() / fc; + let mut out = Vec::with_capacity(n * tc); + for px in src.chunks_exact(fc) { + let alpha = if from.has_alpha() { px[fc - 1] } else { 1.0 }; + let color = &px[..from.color_channels()]; + if from.same_model(to) { + out.extend_from_slice(color); + } else { + let rgb = match from { + ChannelLayout::Gray | ChannelLayout::GrayA => [color[0]; 3], + ChannelLayout::Rgb | ChannelLayout::Rgba => [color[0], color[1], color[2]], + ChannelLayout::Cmyk | ChannelLayout::CmykA => cmyk_to_rgb(color[0], color[1], color[2], color[3]), + ChannelLayout::Lab | ChannelLayout::LabA => crate::lab::lab_to_srgb([color[0], color[1], color[2]]), + }; + match to { + ChannelLayout::Gray | ChannelLayout::GrayA => { + out.push(rgb[0] * LUMA[0] + rgb[1] * LUMA[1] + rgb[2] * LUMA[2]); + } + ChannelLayout::Rgb | ChannelLayout::Rgba => out.extend_from_slice(&rgb), + ChannelLayout::Cmyk | ChannelLayout::CmykA => out.extend_from_slice(&rgb_to_cmyk(rgb[0], rgb[1], rgb[2])), + ChannelLayout::Lab | ChannelLayout::LabA => out.extend_from_slice(&crate::lab::srgb_to_lab(rgb)), + } + } + if to.has_alpha() { + out.push(alpha); + } + } + out +} + +#[cfg(test)] +mod tests { + #[test] + fn adding_alpha_fast_path_matches_the_general_conversion() { + for sample in SampleType::ALL { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Cmyk] { + let n = 7 * 3 * layout.channels(); + let vals: Vec = (0..n).map(|i| (i as f32 * 0.137) % 1.0).collect(); + let img = super::Image::from_normalized(7, 3, layout, sample, &vals).unwrap(); + let fast = img.convert(layout.with_alpha(), sample); + let slow = super::quantize(&super::convert_layout(&img.to_normalized(), layout, layout.with_alpha()), sample); + assert_eq!(fast.data(), &slow[..], "{layout:?} {sample:?}"); + } + } + } + + use super::*; + + #[test] + fn convert_samples_matches_image_convert_and_rejects_a_wrong_length() { + for sample in SampleType::ALL { + let vals: Vec = (0..5 * 2 * 3).map(|i| (i as f32 * 0.31) % 1.0).collect(); + let img = Image::from_normalized(5, 2, ChannelLayout::Rgb, sample, &vals).unwrap(); + for (layout, to) in [(ChannelLayout::Rgba, sample), (ChannelLayout::Gray, SampleType::F32), (ChannelLayout::Rgb, SampleType::U8)] { + let want = img.convert(layout, to); + let got = convert_samples(img.data(), 5, 2, (ChannelLayout::Rgb, sample), (layout, to)).unwrap(); + assert_eq!(got, want.data(), "{sample:?} to {layout:?} {to:?}"); + } + assert!(convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err()); + } + } + + /// The generic f32 route `convert` used before the integer fast paths (the reference). + fn convert_via_f32(img: &Image, layout: ChannelLayout, sample: SampleType) -> Vec { + quantize(&convert_layout(&img.to_normalized(), img.layout, layout), sample) + } + + #[test] + fn int_fast_path_matches_f32_route_for_every_value() { + // Every U8 and every U16 value through every depth pair (alpha goes through the same per-sample conversion; covered by the layout test). + let all8: Vec = (0..=255).collect(); + let all16: Vec = (0..=65535).collect(); + for (src, to) in + [(SampleType::U8, SampleType::U8), (SampleType::U8, SampleType::U16), (SampleType::U16, SampleType::U8), (SampleType::U16, SampleType::U16)] + { + let img = if src == SampleType::U8 { + Image::from_u8(256, 1, ChannelLayout::Gray, all8.clone()).unwrap() + } else { + Image::from_u16(65536, 1, ChannelLayout::Gray, &all16).unwrap() + }; + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb] { + assert_eq!(img.convert(layout, to).data(), &convert_via_f32(&img, layout, to)[..], "{src:?}->{to:?} {layout:?}"); + } + } + } + + #[test] + fn int_fast_path_matches_f32_route_for_every_layout() { + let mut seed = 0x9e3779b97f4a7c15u64; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for from_s in [SampleType::U8, SampleType::U16] { + for from_l in ChannelLayout::ALL { + let n = 64 * from_l.channels(); + let mut vals = Vec::new(); + for i in 0..n { + // Edge values (0, 1, max-1, max, mid) mixed with random ones. + let r = next(); + let v = match i % 7 { + 0 => 0.0, + 1 => 1.0, + 2 => 1.0 / 255.0, + 3 => 254.0 / 255.0, + _ => (r % 65536) as f32 / 65535.0, + }; + vals.push(v); + } + let img = Image::from_normalized(64, 1, from_l, from_s, &vals).unwrap(); + for to_l in ChannelLayout::ALL { + for to_s in SampleType::ALL { + let got = img.convert(to_l, to_s); + assert_eq!(got.data(), &convert_via_f32(&img, to_l, to_s)[..], "{from_l:?}/{from_s:?} -> {to_l:?}/{to_s:?}"); + } + } + } + } + } + + #[test] + fn u8_u16_roundtrip_exact() { + let v: Vec = (0..=255).collect(); + let img = Image::from_u8(256, 1, ChannelLayout::Gray, v.clone()).unwrap(); + let back = img.convert(ChannelLayout::Gray, SampleType::U16).convert(ChannelLayout::Gray, SampleType::U8); + assert_eq!(back.data(), &v[..]); + } + + #[test] + fn u16_f32_roundtrip_exact() { + let v: Vec = (0..=65535u32).step_by(7).map(|x| x as u16).collect(); + let img = Image::from_u16(v.len() as u32, 1, ChannelLayout::Gray, &v).unwrap(); + let back = img.convert(ChannelLayout::Gray, SampleType::F32).convert(ChannelLayout::Gray, SampleType::U16); + assert_eq!(back.to_u16_samples().unwrap(), v); + } + + #[test] + fn icc_dropped_on_model_change() { + let img = Image::new(2, 2, ChannelLayout::Cmyk, SampleType::U8).unwrap().with_icc(Some(vec![1, 2, 3])); + assert!(img.convert(ChannelLayout::Rgb, SampleType::U8).icc.is_none()); + assert!(img.convert(ChannelLayout::CmykA, SampleType::U16).icc.is_some()); + } + + #[test] + fn from_raw_rejects_bad_len() { + assert!(Image::from_raw(2, 2, ChannelLayout::Rgb, SampleType::U8, vec![0; 11]).is_err()); + } + + #[test] + fn rgba8_of_gray() { + let img = Image::from_u8(1, 1, ChannelLayout::GrayA, vec![10, 20]).unwrap(); + assert_eq!(img.to_rgba8(), vec![10, 10, 10, 20]); + } + + #[test] + fn cmyk_rgb_basic() { + let img = Image::from_u8(1, 1, ChannelLayout::Cmyk, vec![0, 0, 0, 255]).unwrap(); + assert_eq!(img.to_rgba8(), vec![0, 0, 0, 255]); + let img = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![255, 0, 0]).unwrap(); + let c = img.convert(ChannelLayout::Cmyk, SampleType::U8); + assert_eq!(c.data(), &[0, 255, 255, 0]); + } +} diff --git a/crates/image-codecs/src/lab.rs b/crates/image-codecs/src/lab.rs new file mode 100644 index 000000000..c53ab4c33 --- /dev/null +++ b/crates/image-codecs/src/lab.rs @@ -0,0 +1,56 @@ +//! CIE L*a*b* (D50) ⇄ encoded sRGB for [`crate::ChannelLayout::Lab`] samples, the same formulas +//! and constants as `photocraft-color` (this crate stays standalone). Samples are normalized as +//! PhotoCraft holds Lab: L* 0..100 as 0..1, a* and b* −128..127 as `(v + 128) / 255`. + +const D50: [f32; 3] = [0.964_22, 1.0, 0.825_21]; +/// Linear sRGB → D50 XYZ (Bradford-adapted, as in the ICC sRGB profile). +const SRGB_TO_XYZ_D50: [[f32; 3]; 3] = [[0.436_074, 0.385_064, 0.143_080], [0.222_504, 0.716_878, 0.060_618], [0.013_932, 0.097_104, 0.714_173]]; +/// D50 XYZ → linear sRGB. +const XYZ_D50_TO_SRGB: [[f32; 3]; 3] = [[3.133_856, -1.616_867, -0.490_615], [-0.978_768, 1.916_142, 0.033_454], [0.071_945, -0.228_991, 1.405_243]]; + +fn mul(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] { + m.map(|r| r[0] * v[0] + r[1] * v[1] + r[2] * v[2]) +} + +fn to_linear(v: f32) -> f32 { + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } +} + +fn to_encoded(v: f32) -> f32 { + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } +} + +/// Normalized Lab → encoded sRGB in 0..1 (out-of-gamut colours clipped). +pub(crate) fn lab_to_srgb(n: [f32; 3]) -> [f32; 3] { + let (l, a, b) = (n[0] * 100.0, n[1] * 255.0 - 128.0, n[2] * 255.0 - 128.0); + let fy = (l + 16.0) / 116.0; + let (fx, fz) = (fy + a / 500.0, fy - b / 200.0); + let finv = |t: f32| if t > 6.0 / 29.0 { t * t * t } else { 3.0 * (6.0f32 / 29.0).powi(2) * (t - 4.0 / 29.0) }; + let xyz = [finv(fx) * D50[0], finv(fy) * D50[1], finv(fz) * D50[2]]; + mul(&XYZ_D50_TO_SRGB, xyz).map(|v| to_encoded(v.clamp(0.0, 1.0))) +} + +/// Encoded sRGB → normalized Lab. +pub(crate) fn srgb_to_lab(rgb: [f32; 3]) -> [f32; 3] { + let xyz = mul(&SRGB_TO_XYZ_D50, rgb.map(|v| to_linear(v.clamp(0.0, 1.0)))); + let f = |t: f32| if t > (6.0f32 / 29.0).powi(3) { t.cbrt() } else { t / (3.0 * (6.0f32 / 29.0).powi(2)) + 4.0 / 29.0 }; + let (fx, fy, fz) = (f(xyz[0] / D50[0]), f(xyz[1] / D50[1]), f(xyz[2] / D50[2])); + let (l, a, b) = (116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz)); + [(l / 100.0).clamp(0.0, 1.0), ((a + 128.0) / 255.0).clamp(0.0, 1.0), ((b + 128.0) / 255.0).clamp(0.0, 1.0)] +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn white_black_and_round_trips() { + let w = srgb_to_lab([1.0; 3]); + assert!((w[0] - 1.0).abs() < 1e-3 && (w[1] - 128.0 / 255.0).abs() < 1e-3 && (w[2] - 128.0 / 255.0).abs() < 1e-3, "{w:?}"); + assert!(srgb_to_lab([0.0; 3])[0].abs() < 1e-4); + for rgb in [[0.2, 0.5, 0.8], [0.9, 0.1, 0.3], [0.5; 3]] { + let back = lab_to_srgb(srgb_to_lab(rgb)); + assert!(rgb.iter().zip(back).all(|(a, b)| (a - b).abs() < 2e-3), "{rgb:?} → {back:?}"); + } + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs new file mode 100644 index 000000000..a5922d323 --- /dev/null +++ b/crates/image-codecs/src/lib.rs @@ -0,0 +1,421 @@ +//! `craft-image-codecs`: symmetric, depth- and metadata-preserving codecs for +//! flat raster image formats. +//! +//! * Every format we **write** we can also **read** (see +//! [`ASYMMETRIC_EXCEPTIONS`] for the documented, feature-gated exception). +//! * Bit depth (8/16-bit integer, 16/32-bit float), ICC profiles and basic +//! metadata (EXIF, XMP, DPI, text) are preserved where the format allows. +//! * [`caps`] declares per-format capabilities and [`fidelity_warnings`] +//! tells the UI exactly what an export will lose. +//! * APIs use bytes or caller-provided readers, with no ambient filesystem access, +//! and build for `wasm32-unknown-unknown`. Byte decoders enforce [`Limits`]; +//! [`exr_stream`] instead bounds metadata and one decoded compression block. + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +pub mod cicp; +mod codecs; +mod error; +pub mod exif_color; +pub mod exr_stream; +mod fidelity; +mod format; +mod image; +mod lab; +mod options; +pub mod orientation; +pub mod resolution; +pub mod source_settings; +pub mod web; +pub use crate::cicp::Cicp; +pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; +pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; +pub use crate::codecs::png::{ + PngDecodeRows, PngExtras, PngHeader, PngPalette, PngRowSink, PngRows, SharedBytes, encode_indexed as encode_png_indexed, palette as png_palette, + stereo as png_stereo, +}; +pub use crate::codecs::tiff::{ + PhotoshopTags as TiffPhotoshopTags, TiffBands, photoshop_tags as tiff_photoshop_tags, writes_little_endian as tiff_writes_little_endian, +}; +pub use crate::codecs::tiff_ifd::{TiffInfo, TiffPage, TiffPageKind}; +pub use crate::error::CodecError; +pub use crate::exif_color::{EXIF_COLOR_SPACE_SRGB, exif_color_space}; +pub use crate::fidelity::{FidelityWarning, fidelity_warnings, fidelity_warnings_with}; +pub use crate::format::{ASYMMETRIC_EXCEPTIONS, Format, FormatCaps, caps, detect, from_extension}; +pub use crate::image::{ + ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, + MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, +}; +pub use crate::options::{ + DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, + TiffCompression, +}; +pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; +pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; +pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; +pub use half::f16; + +use crate::codecs::{dds, dicom, dpx, exr, fits, heif, iff, jpeg, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; + +/// Detect the format and decode with default [`Limits`]. +pub fn decode(bytes: &[u8]) -> Result { + decode_with(bytes, &DecodeOptions::default()) +} + +/// Detect the format and decode. +pub fn decode_with(bytes: &[u8], opts: &DecodeOptions) -> Result { + let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; + decode_as_with(format, bytes, opts) +} + +/// Decode as a specific format with default [`Limits`]. +pub fn decode_as(format: Format, bytes: &[u8]) -> Result { + decode_as_with(format, bytes, &DecodeOptions::default()) +} + +/// Decode as a specific format. +pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Result { + // HEIF without the `heif` feature: its decoder stub says what is missing from this build. + if !caps(format).read && !matches!(format, Format::Heif | Format::Jxl) { + return Err(CodecError::unsupported(format, "decoding is not available for this format")); + } + let l = &opts.limits; + let img = match format { + Format::Png => png::decode(bytes, l), + Format::Jpeg => jpeg::decode(bytes, l), + Format::Tiff => tiff::decode(bytes, l), + Format::WebP => webp::decode(bytes, l), + Format::Pnm => pnm::decode(bytes, l), + Format::Pcx => pcx::decode(bytes, l), + Format::Xpm => xpm::decode(bytes, l), + Format::Dds => dds::decode(bytes, l), + Format::Fits => fits::decode(bytes, l), + Format::Dpx => dpx::decode(bytes, l), + Format::Cineon => dpx::decode_cineon(bytes, l), + Format::Dicom => dicom::decode(bytes, l), + Format::Iff => iff::decode(bytes, l), + Format::OpenExr => exr::decode(bytes, l), + Format::Heif => heif::decode(bytes, l, opts.keep_orientation), + Format::Jxl => Err(CodecError::unsupported(format, "JPEG XL reading is not yet available; export is supported")), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::decode(format, bytes, l), + }?; + finish_format(format, bytes, img, opts) +} + +/// What [`decode_as_with`] does with a PNG decoded in steps by [`PngDecodeRows`] (`img` is its +/// output): the resolution from the metadata, the upright orientation and the final limit check, +/// so the result equals [`decode_as_with`]'s on the same file. +pub fn finish_png_decode(img: Image, opts: &DecodeOptions) -> Result { + match begin_png_finish(img, opts)? { + PngFinish::Done(img) => Ok(*img), + PngFinish::Orient(mut steps) => loop { + if let Some(img) = steps.step(&mut || true)? { + return Ok(img); + } + }, + } +} + +/// Where [`begin_png_finish`] leaves a decoded PNG. +pub enum PngFinish { + /// Finished: it was already upright (or the options keep the orientation). + Done(Box), + /// To be turned upright, a band of rows at a time, by driving these steps. + Orient(Box), +} + +/// [`finish_png_decode`] with the turning upright (orientations 2-8) left to the caller, in +/// bands it can pace: the resolution from the metadata and the limit checks are made here, and the +/// result of the steps equals what [`finish_png_decode`] returns. +pub fn begin_png_finish(mut img: Image, opts: &DecodeOptions) -> Result { + if img.meta.dpi.is_none() { + img.meta.dpi = resolution::from_metadata(img.meta.exif.as_deref(), img.meta.xmp.as_deref()); + } + let o = img.meta.exif.as_deref().map_or(1, exif_orientation); + let l = &opts.limits; + // As `finish_decode` does: the final guard, then (turning 5-8 swaps the sides) the upright size. + l.check(img.width(), img.height(), img.layout(), img.sample_type())?; + if opts.keep_orientation || !(2..=8).contains(&o) { + return Ok(PngFinish::Done(Box::new(img))); + } + if (5..=8).contains(&o) { + l.check(img.height(), img.width(), img.layout(), img.sample_type())?; + } + Ok(PngFinish::Orient(Box::new(OrientSteps::new(img, o)?))) +} + +/// The steps after a decoder of `format` has produced `img` from `bytes`. +fn finish_format(format: Format, bytes: &[u8], mut img: Image, opts: &DecodeOptions) -> Result { + // Without a resolution of the format's own (PNG pHYs, TIFF tags, JPEG's metadata), the + // XMP or EXIF one counts, like Photoshop: a WebP, HEIF or PNG from a camera or phone + // opens at its recorded resolution, not at the 72 ppi default. + if img.meta.dpi.is_none() && format != Format::Tiff { + img.meta.dpi = resolution::from_metadata(img.meta.exif.as_deref(), img.meta.xmp.as_deref()); + } + // Turn the pixels upright, like Photoshop: a TIFF records it in the decoded page's own + // directory, the others in their EXIF block. The metadata is rewritten to Orientation = 1 + // on the way. HEIF keeps it in its container, and its decoder has already applied it. + let o = match format { + Format::Tiff => tiff::orientation(bytes, None), + Format::Heif => 1, + Format::Dpx => dpx::orientation(bytes), + Format::Cineon => dpx::cineon_orientation(bytes), + _ => img.meta.exif.as_deref().map_or(1, exif_orientation), + }; + finish_decode(img, o, opts) +} + +/// Decodes a PNG a group of rows at a time, for a caller that builds its own pixel storage and +/// never needs the whole decoded image. `begin` gets the [`PngHeader`] once and returns whether to go on, then `rows(y0, data)` +/// gets whole rows `y0..` (`group` of them, fewer in the last call; native-endian) in final order, holding exactly the +/// samples [`decode`] would return. The result is the metadata, complete once the file is read. +/// +/// `Ok(None)`, with no row delivered, means the rows can't be had in final order (an +/// interlaced or animated PNG) or `begin` returned false: use [`decode_as_with`]. Damaged files fail as [`decode_as_with`] +/// fails them, possibly after some rows. The limits are checked before the first row; the +/// orientation is not applied, so [`PngExtras::orientation`] other than 1 is for the caller to +/// handle (or to fall back to [`decode_as_with`], which turns the image upright). +pub fn decode_png_rows>( + bytes: &[u8], + opts: &DecodeOptions, + group: u32, + begin: &mut dyn FnMut(&PngHeader) -> Result, + rows: &mut PngRowSink<'_, E>, +) -> Result, E> { + png::decode_rows(bytes, &opts.limits, group, begin, rows) +} + +/// The upright pixel size [`decode`] would return, read from the file's header without decoding +/// pixels, for WebP, OpenEXR (flat) and the GIF/BMP/TGA/ICO/QOI/HDR family. It runs the same +/// header reads and size checks as the decoder (default [`Limits`]) and swaps the size for an +/// EXIF orientation of 5–8, so wherever [`decode`] succeeds the two agree. Other formats (and +/// deep EXR or AVIF) return [`CodecError::Unsupported`]: decode those to learn their size. +pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let format = detect(bytes).ok_or(CodecError::UnknownFormat)?; + if !caps(format).read { + return Err(CodecError::unsupported(format, "decoding is not available for this format")); + } + let l = &Limits::default(); + let (w, h, exif) = match format { + Format::WebP => webp::probe(bytes, l)?, + Format::OpenExr => exr::probe(bytes, l).map(|(w, h)| (w, h, None))?, + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::probe(format, bytes, l)?, + _ => return Err(CodecError::unsupported(format, "no header-only size probe")), + }; + let o = exif.as_deref().map_or(1, exif_orientation); + Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) }) +} + +/// The colour table of a GIF's first frame (its Local Color Table, else the Global one) and the +/// entry its Graphic Control Extension makes transparent, read without decoding pixels. +pub fn gif_palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option)> { + crate::codecs::gif_palette::palette(bytes) +} + +/// Returns the embedded colour table for an indexed BMP, if it has one. +pub fn bmp_palette(bytes: &[u8]) -> Result>, CodecError> { + via_image::bmp_palette(bytes) +} + +/// Encodes `indices` (row-major, one byte per pixel) as an 8-bit palette BMP with the colour +/// table `palette` (1 to 256 entries). Used for Indexed Color documents; BMP has no transparency. +pub fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palette: &[[u8; 3]]) -> Result, CodecError> { + via_image::encode_bmp_indexed(width, height, indices, palette) +} + +/// Lists the pages (image directories) of a TIFF or BigTIFF file without decoding pixels: the +/// main IFD chain and its SubIFDs, each with its size, kind (page, reduced-resolution copy or +/// mask) and storage. [`TiffInfo::default_page`] is the page [`decode`] opens. +pub fn tiff_info(bytes: &[u8]) -> Result { + crate::codecs::tiff_ifd::tiff_info(bytes) +} + +/// Decodes page `page` (an index into [`tiff_info`]'s `pages`) of a TIFF or BigTIFF file, +/// applying that page's orientation unless `opts.keep_orientation`. +pub fn decode_tiff_page(bytes: &[u8], page: usize, opts: &DecodeOptions) -> Result { + let img = tiff::decode_page(bytes, Some(page), &opts.limits)?; + finish_decode(img, tiff::orientation(bytes, Some(page)), opts) +} + +/// Decodes every frame of a GIF (a still GIF yields one), each a full RGBA8 picture of the +/// logical screen with its delay. [`decode`] keeps the first frame only. See +/// [`MAX_GIF_FRAMES`] and `opts.limits` for what is refused. +pub fn decode_gif_frames(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + crate::codecs::gif_frames::decode(bytes, opts) +} + +/// Decodes every frame of a WebP (a still one yields one frame with a 0 delay, as [`decode`] +/// reads it), each a full picture of the canvas with its duration and the file's ICC profile. +/// [`decode`] keeps the first frame only. See [`MAX_GIF_FRAMES`] and `opts.limits` for what is +/// refused. +pub fn decode_webp_frames(bytes: &[u8], opts: &DecodeOptions) -> Result, CodecError> { + match crate::codecs::webp::decode_frames(bytes, &opts.limits)? { + Some(frames) => Ok(frames), + None => Ok(vec![AnimationFrame { image: decode_as_with(Format::WebP, bytes, opts)?, delay_ms: 0 }]), + } +} + +/// Whether `bytes` is an animated WebP of more than one frame (which [`decode`] opens as its +/// first frame, with a warning, and [`decode_webp_frames`] reads whole). +pub fn webp_is_animated(bytes: &[u8]) -> bool { + crate::codecs::webp::is_animated(bytes) +} + +/// Inspect declared source data which a flat decode and save cannot retain: additional frames, +/// TIFF pages, masks or unretained channels, ICO entries, DDS mip levels, slices, faces or arrays, +/// DPX elements and FITS HDU payloads. +/// Pixels are not decoded; derived TIFF thumbnails do not require preservation here. +/// Other formats return false; this does not establish preservation of other format-specific data. +pub fn has_unretained_source_data(bytes: &[u8]) -> Result { + match detect(bytes) { + Some(Format::Gif) => via_image::gif_has_more_frames(bytes).ok_or_else(|| CodecError::malformed(Format::Gif, "cannot inspect every frame")), + Some(Format::Png) => png::has_additional_images(bytes), + Some(Format::WebP) => Ok(webp_is_animated(bytes)), + Some(Format::Tiff) => { + let info = tiff_info(bytes)?; + if !info.complete { + return Err(CodecError::malformed(Format::Tiff, "cannot inspect every source image directory")); + } + let retained = info.default_page(); + if info.pages.iter().enumerate().any(|(index, page)| Some(index) != retained && page.kind != TiffPageKind::ReducedResolution) { + return Ok(true); + } + retained.and_then(|index| info.pages.get(index)).map_or(Ok(false), |page| tiff::has_unretained_samples(bytes, page.ifd_offset)) + } + Some(Format::Ico) => { + let count = bytes.get(4..6).and_then(|b| <[u8; 2]>::try_from(b).ok()).ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; + Ok(u16::from_le_bytes(count) > 1) + } + Some(Format::Dds) => dds::has_additional_images(bytes), + Some(Format::Dpx) => dpx::has_additional_images(bytes), + Some(Format::Fits) => fits::has_additional_images(bytes), + _ => Ok(false), + } +} + +/// The Orientation (1–8) of the page [`decode`] opens from a TIFF or BigTIFF file; 1 when it is +/// absent, malformed or out of range. +pub fn tiff_orientation(bytes: &[u8]) -> u16 { + tiff::orientation(bytes, None) +} + +/// The Orientation (1–8) of page `page` (an index into [`tiff_info`]'s `pages`); 1 when it is +/// absent, malformed or out of range, or the page does not exist. +pub fn tiff_page_orientation(bytes: &[u8], page: usize) -> u16 { + tiff::orientation(bytes, Some(page)) +} + +/// The EXIF block (`Metadata::exif`) of a camera raw built on TIFF (NEF, ARW, CR2, DNG, +/// Panasonic RW2), for the image developed from it: IFD0's descriptive tags (make, model, +/// date…) and the Exif and GPS directories, as a TIFF opens with. Orientation is not part of +/// it, nor the Exif directory's pixel dimensions (the sensor's or the preview's size). When those +/// were all its Exif directory held, the block is IFD0's tags alone, so the camera's make and +/// model are still kept. `None` when the file is not built on a classic TIFF stream or has none +/// of these tags. +pub fn raw_exif(bytes: &[u8]) -> Option> { + let dirs = crate::codecs::tiff_exif::read_raw(bytes).filter(|d| d.has_directories() || !d.ifd0.is_empty())?; + Some(crate::codecs::tiff_exif::block(&dirs)) +} + +/// The limits check and the orientation, shared by every decoder. +fn finish_decode(img: Image, o: u16, opts: &DecodeOptions) -> Result { + let l = &opts.limits; + // Final guard for decoders whose header we could not pre-inspect. + l.check(img.width(), img.height(), img.layout(), img.sample_type())?; + if opts.keep_orientation { + return Ok(img); + } + // Orientations 5–8 swap width and height: with asymmetric limits a stored landscape + // that fit can turn into a portrait that does not, so check the upright size too. + if (5..=8).contains(&o) { + l.check(img.height(), img.width(), img.layout(), img.sample_type())?; + } + img.oriented(o) +} + +/// Lists the parts of an OpenEXR file without decoding pixels: header facts per part +/// (name, view, size, channels, deep/tiled), what a multi-part chooser — a Maya/Arnold +/// render writes one part per AOV — offers. The indices are what [`decode_exr_part`] +/// takes; [`decode_as_with`] opens the part with the highest [`ExrPartInfo::color_rank`]. +pub fn exr_info(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + exr::info(bytes, limits) +} + +/// Decodes part `part` (an index into [`exr_info`]'s list) of an OpenEXR file. +/// Deep parts stay with [`decode_deep_exr`]; a file that has any is refused here. +pub fn decode_exr_part(bytes: &[u8], part: usize, opts: &DecodeOptions) -> Result { + let img = exr::decode_part(bytes, part, &opts.limits)?; + finish_decode(img, 1, opts) +} + +/// Lists every Cryptomatte layer of an EXR file (specification 1.2: object/material IDs +/// with coverage, as written by Arnold, V-Ray, Redshift, Mantra/Karma and Cycles) from the +/// headers alone: name, metadata key, channels and the parsed embedded manifest. The +/// samples come from [`decode_cryptomatte`]; names hash to IDs with [`cryptomatte_id`]. +pub fn cryptomatte_layers(bytes: &[u8], limits: &Limits) -> Result, CodecError> { + codecs::exr_cryptomatte::layers(bytes, limits) +} + +/// Decodes the samples of the Cryptomatte layer called `layer_name` (a +/// [`CryptomatteLayer::name`]): per pixel its (ID, coverage) pairs, coverage above zero, +/// sorted by descending coverage. +pub fn decode_cryptomatte(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result { + codecs::exr_cryptomatte::decode(bytes, layer_name, limits) +} + +/// Decodes the named channels of one part (exact names as the file stores them, e.g. +/// `diffuse.R`) into an image: R/G/B (plus A when named) give Rgba, a lone Y gives Gray. +/// This renders a channel group without the part's other channels in the way. +pub fn decode_exr_channels(bytes: &[u8], part: usize, names: &[&str], opts: &DecodeOptions) -> Result { + let img = exr::decode_channels(bytes, part, names, &opts.limits)?; + finish_decode(img, 1, opts) +} + +/// Deep OpenEXR samples (`deepscanline`/`deeptile`): the structured per-pixel sample lists +/// (colour, alpha and Z per sample), for callers that composite deep data themselves — +/// [`decode_as_with`] instead flattens a deep file into a normal [`Image`] with a +/// [`DecodeWarning::DeepFlattened`]. `Err(Unsupported)` when the file has no deep part. +pub fn decode_deep_exr(bytes: &[u8], limits: &Limits) -> Result { + codecs::exr::check_previews(bytes)?; + let mut cur = std::io::Cursor::new(bytes); + let meta = ::exr::meta::MetaData::read_from_buffered(&mut cur, false).map_err(|e| CodecError::malformed(Format::OpenExr, e))?; + codecs::deep_exr::decode_deep(&meta, bytes, limits) +} + +/// Encode `image` as `format`. Conversions follow the same plan that +/// [`fidelity_warnings_with`] reports; fatal conditions (unsupported +/// format, oversize image) return an error. +pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result, CodecError> { + if !caps(format).write { + return Err(CodecError::unsupported(format, "encoding is not available in this build")); + } + if image.width() == 0 || image.height() == 0 { + return Err(CodecError::InvalidImage("cannot encode an empty image".into())); + } + if let Some((mw, mh)) = format.max_dimensions() + && (image.width() > mw || image.height() > mh) + { + return Err(CodecError::encode(format, format!("dimensions exceed {mw}x{mh}"))); + } + let plan = fidelity::plan(image, format, opts); + match format { + Format::Png => png::encode(image, plan, opts), + Format::Jpeg => jpeg::encode(image, plan, opts), + Format::Tiff => tiff::encode(image, plan, opts), + Format::WebP => webp::encode(image, plan, opts), + Format::Pnm => pnm::encode(image, plan, opts), + Format::Pcx => Err(CodecError::unsupported(format, "PCX is import-only; save as PNG instead")), + Format::Xpm => Err(CodecError::unsupported(format, "XPM is import-only; save as PNG instead")), + Format::Dds => dds::encode(image, plan, opts), + Format::Fits => fits::encode(image, plan, opts), + Format::Dpx => dpx::encode(image, plan, opts), + Format::Cineon => dpx::encode_cineon(image, plan), + Format::Dicom => Err(CodecError::unsupported(format, "DICOM is import-only; save as TIFF or PNG instead")), + Format::Iff => iff::encode(image, plan), + Format::OpenExr => exr::encode(image, plan, opts), + Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), + Format::Jxl => jxl::encode(image, plan), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), + } +} diff --git a/crates/image-codecs/src/options.rs b/crates/image-codecs/src/options.rs new file mode 100644 index 000000000..9674b4edf --- /dev/null +++ b/crates/image-codecs/src/options.rs @@ -0,0 +1,364 @@ +//! Decode limits and encode options. + +use crate::error::CodecError; +use crate::image::{ChannelLayout, SampleType}; + +/// Decompression-bomb guards. Dimension checks precede pixel allocation; +/// metadata decoding checks its budget before growing decoded output. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Limits { + pub max_width: u32, + pub max_height: u32, + /// Maximum `width * height`. + pub max_pixels: u64, + /// Maximum bytes for the decoded pixel buffer (and passed on as the + /// allocation budget to underlying decoders). + /// PNG also applies this to the aggregate decoded text/XMP UTF-8 bytes, + /// including keywords, separately from pixels. Animated WebP charges + /// every retained frame's full pixel copy plus its own copy of the + /// file's ICC profile against this budget. This is not a total RSS cap. + pub max_alloc: u64, +} + +impl Default for Limits { + fn default() -> Self { + // 2^30 pixels (e.g. 32768²) and, on 64-bit targets, 8 GiB: enough for the large + // documents Photoshop users open (a 20000² RGBA 16-bit image is 3.2 GB). On 32-bit + // targets (wasm32 has a 4 GiB address space shared with the document, history and + // the decoder's own buffers) 1 GiB is the most one buffer can safely ask for. + let max_alloc = if cfg!(target_pointer_width = "64") { 8 << 30 } else { 1 << 30 }; + Limits { max_width: 1 << 18, max_height: 1 << 18, max_pixels: 1 << 30, max_alloc } + } +} + +impl Limits { + /// For a decoder that writes a band of rows at a time into the document's tiles instead of + /// one buffer (TIFF, #8169): the largest document size, 300,000 px a side, on 64-bit + /// targets, with `max_alloc` bounding each group of bands. 32-bit targets keep the + /// [`Default`] limits: wasm32's 4 GiB address space can't hold a larger document anyway. + pub fn banded() -> Self { + let l = Limits::default(); + if cfg!(target_pointer_width = "64") { Limits { max_width: 300_000, max_height: 300_000, max_pixels: 300_000 * 300_000, ..l } } else { l } + } + + /// Effectively unlimited (still bounded by `usize` overflow checks). + pub fn none() -> Self { + Limits { max_width: u32::MAX, max_height: u32::MAX, max_pixels: u64::MAX, max_alloc: u64::MAX } + } + + /// Validate declared dimensions for an output buffer of the given layout + /// and sample type. + pub fn check(&self, width: u32, height: u32, layout: ChannelLayout, sample: SampleType) -> Result<(), CodecError> { + self.check_bytes(width, height, (layout.channels() * sample.bytes()) as u64) + } + + pub(crate) fn check_bytes(&self, width: u32, height: u32, bytes_per_pixel: u64) -> Result<(), CodecError> { + self.check_size(width, height)?; + let bytes = (width as u64 * height as u64).saturating_mul(bytes_per_pixel); + if bytes > self.max_alloc { + return Err(CodecError::LimitExceeded(format!("{bytes} bytes exceed max_alloc {}", self.max_alloc))); + } + if usize::try_from(bytes).is_err() { + return Err(CodecError::LimitExceeded(format!("{bytes} bytes do not fit in memory"))); + } + Ok(()) + } + + /// The dimension and pixel-count checks of [`Limits::check`], for a decoder that never holds + /// the whole image in one buffer (`max_alloc` then bounds each piece it decodes). + pub(crate) fn check_size(&self, width: u32, height: u32) -> Result<(), CodecError> { + if width == 0 || height == 0 { + return Err(CodecError::InvalidImage(format!("zero-sized image {width}x{height}"))); + } + if width > self.max_width || height > self.max_height { + return Err(CodecError::LimitExceeded(format!("dimensions {width}x{height} exceed max {}x{}", self.max_width, self.max_height))); + } + let pixels = width as u64 * height as u64; + if pixels > self.max_pixels { + return Err(CodecError::LimitExceeded(format!("{pixels} pixels exceed max {}", self.max_pixels))); + } + Ok(()) + } + + /// Decompression-bomb guard for formats that compress: refuse a file too small to hold + /// `declared_bytes` of (stored, pre-expansion) image data, given that the compression + /// never beats `max_ratio`:1. Called before the output buffer is allocated, so a + /// header-only hostile file cannot make the decoder reserve gigabytes. A fixed 1 MiB of + /// slack keeps tiny images and headers with large metadata unaffected. + pub(crate) fn check_plausible(declared_bytes: u64, file_len: usize, max_ratio: u64) -> Result<(), CodecError> { + let capacity = (file_len as u64).saturating_mul(max_ratio).saturating_add(1 << 20); + if declared_bytes > capacity { + return Err(CodecError::LimitExceeded(format!("a {file_len} byte file cannot hold {declared_bytes} bytes of image data"))); + } + Ok(()) + } + + pub(crate) fn alloc_usize(&self) -> usize { + usize::try_from(self.max_alloc).unwrap_or(usize::MAX) + } +} + +/// Options for [`crate::decode_with`]. +#[derive(Debug, Clone, Default)] +pub struct DecodeOptions { + pub limits: Limits, + /// Keep the pixels as stored instead of applying the EXIF / TIFF + /// orientation (by default they are turned upright and the metadata's + /// orientation is rewritten to 1). + pub keep_orientation: bool, +} + +/// PNG zlib effort. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum PngCompression { + None, + Fast, + #[default] + Default, + Best, +} + +/// How a PNG's `sTER` chunk pairs its two views (PNG Extensions 1.4.0, §3.6). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PngStereo { + /// Side by side with the right-eye view first (cross-eyed viewing, mode 0). + CrossFuse, + /// Side by side with the left-eye view first (parallel viewing, mode 1). + DivergingFuse, +} + +impl PngStereo { + /// The view for an `sTER` mode byte; reserved and unknown values are `None`. + pub fn from_mode(mode: u8) -> Option { + match mode { + 0 => Some(Self::CrossFuse), + 1 => Some(Self::DivergingFuse), + _ => None, + } + } + + /// The `sTER` mode byte. + pub fn mode(self) -> u8 { + match self { + Self::CrossFuse => 0, + Self::DivergingFuse => 1, + } + } +} + +/// TIFF strip compression. The default is none, as in Photoshop: uncompressed strips are the +/// only kind every TIFF reader opens (Deflate and the predictor are not baseline TIFF, #4484). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum TiffCompression { + #[default] + None, + Lzw, + Deflate, + PackBits, +} + +/// JPEG scan layout: Photoshop's JPEG Options › Format Options. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum JpegFormat { + /// Baseline ("Standard"): one sequential scan with the standard Huffman tables. + #[default] + Baseline, + /// Baseline Optimized: one sequential scan with Huffman tables fitted to the image. Only at + /// 4:4:4 for now: a 4:2:0 (subsampled RGB) baseline scan keeps the standard tables, because + /// fitted tables make jpeg-encoder write one scan per component, which zune-jpeg 0.5.15 (our + /// decoder) reads back wrongly when chroma is subsampled (see `web::encode_jpeg_rgb8`). + Optimized, + /// Progressive: the image refines over several scans as it loads. + Progressive, +} + +impl JpegFormat { + /// `"baseline"`, `"optimized"` or `"progressive"`, as parameters and scripts spell it. + pub fn name(self) -> &'static str { + match self { + JpegFormat::Baseline => "baseline", + JpegFormat::Optimized => "optimized", + JpegFormat::Progressive => "progressive", + } + } + + /// The inverse of [`Self::name`]. + pub fn parse(s: &str) -> Option { + [JpegFormat::Baseline, JpegFormat::Optimized, JpegFormat::Progressive].into_iter().find(|f| f.name() == s) + } +} + +/// OpenEXR compression (lossless methods only). +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum ExrCompression { + None, + Rle, + Zip1, + #[default] + Zip16, + Piz, +} + +/// Bits per sample of a DPX file. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum DpxBits { + Bits8, + Bits10, + Bits12, + Bits16, +} + +impl DpxBits { + /// `8`, `10`, `12` or `16` as [`DpxBits`]; any other number is `None`. + pub fn from_bits(bits: u64) -> Option { + match bits { + 8 => Some(DpxBits::Bits8), + 10 => Some(DpxBits::Bits10), + 12 => Some(DpxBits::Bits12), + 16 => Some(DpxBits::Bits16), + _ => None, + } + } + + /// The bits per sample. + pub fn bits(self) -> u8 { + match self { + DpxBits::Bits8 => 8, + DpxBits::Bits10 => 10, + DpxBits::Bits12 => 12, + DpxBits::Bits16 => 16, + } + } +} + +/// How a DDS texture stores its pixels. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum DdsCompression { + /// Uncompressed, 8 bits per channel: BGRA (BGRX without alpha), or luminance with or + /// without alpha for grayscale. Lossless. + #[default] + None, + /// BC1 (DXT1): 4 bits per pixel. Alpha is on or off: texels under half opacity become + /// transparent black. + Bc1, + /// BC2 (DXT3): 8 bits per pixel, with 4 bits of explicit alpha per texel. + Bc2, + /// BC3 (DXT5): 8 bits per pixel, with smooth interpolated alpha. + Bc3, +} + +impl DdsCompression { + pub const ALL: [DdsCompression; 4] = [DdsCompression::None, DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3]; + + /// The name scripts and the CLI use: `none`, `bc1`, `bc2` or `bc3`. + pub fn name(self) -> &'static str { + match self { + DdsCompression::None => "none", + DdsCompression::Bc1 => "bc1", + DdsCompression::Bc2 => "bc2", + DdsCompression::Bc3 => "bc3", + } + } + + /// [`Self::name`] back to a compression, also taking the DXT names (`dxt1`, `dxt3`, + /// `dxt5`), in any case. Anything else is `None`. + pub fn from_name(name: &str) -> Option { + match name.to_ascii_lowercase().as_str() { + "none" => Some(DdsCompression::None), + "bc1" | "dxt1" => Some(DdsCompression::Bc1), + "bc2" | "dxt3" => Some(DdsCompression::Bc2), + "bc3" | "dxt5" => Some(DdsCompression::Bc3), + _ => None, + } + } + + /// A block compression (lossy), not uncompressed pixels. + pub fn is_block(self) -> bool { + self != DdsCompression::None + } +} + +/// What a DDS file is written with. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct DdsOptions { + pub compression: DdsCompression, + /// Write the full mip chain down to 1×1, each level a box filter of the one above. + pub mipmaps: bool, +} + +/// The longer side, in pixels, at which a TIFF image pyramid stops: the last level written is +/// the first one no longer than this ([`EncodeOptions::tiff_pyramid`]). +pub const TIFF_PYRAMID_MIN: u32 = 64; + +/// Options for [`crate::encode`]. +#[derive(Debug, Clone)] +pub struct EncodeOptions { + /// JPEG quality 1..=100 (also AVIF quality when enabled). + pub jpeg_quality: u8, + /// JPEG 4:2:0 chroma subsampling (`false` = 4:4:4). + pub jpeg_chroma_subsampling: bool, + /// JPEG scan layout (baseline, baseline optimized or progressive). + pub jpeg_format: JpegFormat, + pub png_compression: PngCompression, + /// Write Adam7-interlaced PNG. + pub png_interlaced: bool, + /// Write an `sTER` chunk marking the image as a stereo pair (PNG Extensions 1.4.0, §3.6), + /// so a stereo PNG keeps its mark when saved again (#6063). + pub png_stereo: Option, + /// WebP: lossless (VP8L, via `image-webp`) or lossy (our VP8 encoder, see + /// `codecs::vp8`) with [`Self::webp_quality`]. + pub webp_lossless: bool, + /// Lossy WebP quality 0..=100 (the source application's scale; 75–85 is typical for photos). + pub webp_quality: u8, + pub tiff_compression: TiffCompression, + /// Always write TIFF as BigTIFF (8-byte offsets). Without it a TIFF is written as BigTIFF + /// only when it could pass the 4 GiB a classic TIFF can address. + pub tiff_bigtiff: bool, + /// TIFF: after the full image, write Photoshop's "Save Image Pyramid" levels: copies at half, + /// a quarter, … of the size, each marked reduced-resolution (`NewSubfileType` 1), down to + /// [`crate::TIFF_PYRAMID_MIN`] px on the longer side. Readers that don't know pyramids open + /// the full image, which comes first. + pub tiff_pyramid: bool, + pub exr_compression: ExrCompression, + /// DPX bits per sample. `None` follows the image: 8 for 8-bit, 16 otherwise. + pub dpx_bits: Option, + /// DDS compression and mip chain. + pub dds: DdsOptions, + /// Embed the ICC profile when the format supports it. + pub embed_icc: bool, + /// Embed EXIF/XMP/DPI/text when the format supports it. + pub embed_metadata: bool, +} + +impl Default for EncodeOptions { + fn default() -> Self { + EncodeOptions { + jpeg_quality: 90, + jpeg_chroma_subsampling: true, + jpeg_format: JpegFormat::Baseline, + png_compression: PngCompression::Default, + png_interlaced: false, + png_stereo: None, + webp_lossless: true, + webp_quality: 80, + tiff_compression: TiffCompression::None, + tiff_bigtiff: false, + tiff_pyramid: false, + exr_compression: ExrCompression::Zip16, + dpx_bits: None, + dds: DdsOptions::default(), + embed_icc: true, + embed_metadata: true, + } + } +} + +/// A zero-filled buffer of `len` elements, allocated fallibly: an allocation failure becomes +/// [`CodecError::LimitExceeded`] instead of aborting the process (which `catch_unwind` cannot +/// stop). +pub(crate) fn alloc_zeroed(len: usize) -> Result, CodecError> { + let mut v = Vec::new(); + v.try_reserve_exact(len).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {len} elements for the image")))?; + v.resize(len, T::default()); + Ok(v) +} diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs new file mode 100644 index 000000000..5ad8be252 --- /dev/null +++ b/crates/image-codecs/src/orientation.rs @@ -0,0 +1,476 @@ +//! EXIF / TIFF orientation (tag 274): reading it, applying it to the pixels, +//! and rewriting it to 1 ("top-left", upright) once the pixels are upright. +//! +//! Decoders apply the orientation by default (like Photoshop and every photo +//! viewer), so a document is always edited upright. Encoders then write +//! Orientation = 1, so a viewer never rotates the already-upright pixels a +//! second time. +//! +//! Orientation values (TIFF 6.0, EXIF 2.3): 1 = upright, 2 = mirrored, 3 = +//! rotated 180°, 4 = flipped vertically, 5 = transposed, 6 = needs a 90° +//! clockwise turn, 7 = transverse, 8 = needs a 90° counter-clockwise turn. +//! Anything malformed, truncated or out of range reads as 1: an IFD0 that +//! starts inside the 8-byte header or is cut off before its next-IFD pointer, +//! or an Orientation entry that is not exactly one SHORT or LONG (the spec says +//! SHORT, but some cameras and tools write LONG). When IFD0 holds +//! several Orientation entries the first one wins (like libexif). + +use std::borrow::Cow; + +use crate::error::CodecError; +use crate::image::Image; + +/// The TIFF/EXIF Orientation tag. +const TAG_ORIENTATION: u16 = 274; +/// Field types accepted for the Orientation tag: SHORT (the spec) and LONG +/// (written by some cameras and tools). +const TYPE_SHORT: u16 = 3; +const TYPE_LONG: u16 = 4; +/// Bytes per IFD entry. +const ENTRY: usize = 12; +/// The TIFF header is 8 bytes, so no IFD can start before it. +const HEADER: usize = 8; +/// BigTIFF uses a 16-byte header, 20-byte entries, and 8-byte offsets. +const BIG_ENTRY: usize = 20; +const BIG_HEADER: usize = 16; + +/// Byte order of a TIFF structure. +#[derive(Clone, Copy)] +pub(crate) enum Order { + Little, + Big, +} + +impl Order { + pub(crate) fn u16(self, b: &[u8], at: usize) -> Option { + let s: [u8; 2] = b.get(at..at.checked_add(2)?)?.try_into().ok()?; + Some(match self { + Order::Little => u16::from_le_bytes(s), + Order::Big => u16::from_be_bytes(s), + }) + } + + pub(crate) fn u32(self, b: &[u8], at: usize) -> Option { + let s: [u8; 4] = b.get(at..at.checked_add(4)?)?.try_into().ok()?; + Some(match self { + Order::Little => u32::from_le_bytes(s), + Order::Big => u32::from_be_bytes(s), + }) + } + + pub(crate) fn u64(self, b: &[u8], at: usize) -> Option { + let s: [u8; 8] = b.get(at..at.checked_add(8)?)?.try_into().ok()?; + Some(match self { + Order::Little => u64::from_le_bytes(s), + Order::Big => u64::from_be_bytes(s), + }) + } +} + +/// The TIFF payload of an EXIF block (a JPEG-style `Exif\0\0` prefix is skipped). +pub(crate) fn tiff_body(b: &[u8]) -> &[u8] { + b.strip_prefix(b"Exif\0\0").unwrap_or(b) +} + +/// IFD0 of a TIFF structure: byte order, entry layout, and entry offsets. +/// +/// The full table and next-IFD pointer must be present, and IFD0 must start +/// after the corresponding TIFF or BigTIFF header. +pub(crate) fn ifd0_entries(b: &[u8]) -> Option<(Order, usize, usize, usize, bool)> { + let (order, big_tiff) = match b.get(0..4)? { + [b'I', b'I', 42, 0] => (Order::Little, false), + [b'M', b'M', 0, 42] => (Order::Big, false), + [b'I', b'I', 43, 0] => (Order::Little, true), + [b'M', b'M', 0, 43] => (Order::Big, true), + _ => return None, + }; + let (ifd, count_bytes, entry_bytes, next_ifd_bytes, header) = if big_tiff { + if order.u16(b, 4)? != 8 || order.u16(b, 6)? != 0 { + return None; + } + (usize::try_from(order.u64(b, 8)?).ok()?, 8usize, BIG_ENTRY, 8usize, BIG_HEADER) + } else { + (usize::try_from(order.u32(b, 4)?).ok()?, 2usize, ENTRY, 4usize, HEADER) + }; + if ifd < header { + return None; + } + let count = if big_tiff { usize::try_from(order.u64(b, ifd)?).ok()? } else { usize::from(order.u16(b, ifd)?) }; + let first = ifd.checked_add(count_bytes)?; + let next_ifd = first.checked_add(count.checked_mul(entry_bytes)?)?; + if next_ifd.checked_add(next_ifd_bytes)? > b.len() { + return None; + } + // In bounds: every entry ends at or before `next_ifd`. + Some((order, first, count, entry_bytes, big_tiff)) +} + +/// A well-formed Orientation entry's value: where it sits and its field type. +#[derive(Clone, Copy)] +struct Value { + at: usize, + ty: u16, +} + +impl Value { + /// The stored value (a SHORT or LONG, left-justified in the 4-byte field). + fn read(self, order: Order, b: &[u8]) -> Option { + match self.ty { + TYPE_SHORT => order.u16(b, self.at).map(u32::from), + _ => order.u32(b, self.at), + } + } + + /// 1 in this entry's own type, width and byte order. + fn one(self, order: Order) -> Vec { + match (self.ty, order) { + (TYPE_SHORT, Order::Little) => 1u16.to_le_bytes().to_vec(), + (TYPE_SHORT, Order::Big) => 1u16.to_be_bytes().to_vec(), + (_, Order::Little) => 1u32.to_le_bytes().to_vec(), + (_, Order::Big) => 1u32.to_be_bytes().to_vec(), + } + } +} + +/// The value of the entry at `e` if it is a well-formed Orientation entry: +/// exactly one SHORT or LONG. +fn orientation_value(order: Order, b: &[u8], e: usize, big_tiff: bool) -> Option { + let count = if big_tiff { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; + if order.u16(b, e)? != TAG_ORIENTATION || count != 1 { + return None; + } + let ty = order.u16(b, e.checked_add(2)?)?; + let value_offset = if big_tiff { 12 } else { 8 }; + matches!(ty, TYPE_SHORT | TYPE_LONG).then_some(Value { at: e.checked_add(value_offset)?, ty }) +} + +/// Locates the Orientation value in IFD0. +/// +/// The **first** Orientation entry wins (like libexif and Photoshop); if that +/// one is malformed (count ≠ 1, or neither SHORT nor LONG) the tag is ignored, +/// even when a later duplicate is well formed. +fn find_entry(b: &[u8]) -> Option<(Order, Value)> { + let (order, first, count, entry_bytes, big_tiff) = ifd0_entries(b)?; + let e = (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).find(|&e| order.u16(b, e) == Some(TAG_ORIENTATION))?; + Some((order, orientation_value(order, b, e, big_tiff)?)) +} + +/// The orientation (1–8) recorded in a TIFF-structured block: an EXIF payload +/// (with or without the `Exif\0\0` prefix) or a whole TIFF file. Missing, +/// malformed or out-of-range values give 1. +pub fn exif_orientation(exif: &[u8]) -> u16 { + let b = tiff_body(exif); + find_entry(b).and_then(|(order, v)| v.read(order, b)).and_then(|v| u16::try_from(v).ok()).filter(|v| (1..=8).contains(v)).unwrap_or(1) +} + +/// `exif` with its Orientation rewritten to 1, borrowed when there is nothing +/// to change (no well-formed tag, already 1, or unparseable). +/// +/// Only the value bytes of each well-formed Orientation entry change, written +/// as 1 in the entry's existing type and width (SHORT or LONG); every other +/// byte (other tags, their offset-based values, sub-IFDs) is kept as is. +/// Well-formed duplicates are all set to 1, so a reader that lets a later +/// duplicate win cannot rotate the upright pixels either. +pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { + return Cow::Borrowed(exif); + }; + let values: Vec = + (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).filter_map(|e| orientation_value(order, body, e, big_tiff)).collect(); + if values.iter().all(|v| v.read(order, body) == Some(1)) { + return Cow::Borrowed(exif); + } + let mut out = exif.to_vec(); + for v in values { + let one = v.one(order); + let slot = prefix.checked_add(v.at).and_then(|at| out.get_mut(at..at.checked_add(one.len())?)); + if let Some(slot) = slot { + slot.copy_from_slice(&one); + } + } + Cow::Owned(out) +} + +/// `xmp` with any `tiff:Orientation` (attribute or element form) set to 1, +/// borrowed when there is nothing to change. +pub fn upright_xmp(xmp: &str) -> Cow<'_, str> { + const KEY: &str = "tiff:Orientation"; + if !xmp.contains(KEY) { + return Cow::Borrowed(xmp); + } + let mut out = String::with_capacity(xmp.len()); + let mut rest = xmp; + let mut changed = false; + while let Some(i) = rest.find(KEY) { + let (head, tail) = rest.split_at(i + KEY.len()); + out.push_str(head); + rest = tail; + // `tiff:Orientation="6"`, `tiff:Orientation='6'` or `6<`. + let open = rest.char_indices().take_while(|&(_, c)| c.is_ascii_whitespace() || matches!(c, '=' | '"' | '\'' | '>')).last(); + let Some((at, c)) = open else { continue }; + if !matches!(c, '"' | '\'' | '>') { + continue; + } + let start = at + 1; + let digits = rest.get(start..).map_or(0, |s| s.bytes().take_while(u8::is_ascii_digit).count()); + if digits == 0 || rest.get(start..start + digits) == Some("1") { + continue; + } + out.push_str(rest.get(..start).unwrap_or_default()); + out.push('1'); + rest = rest.get(start + digits..).unwrap_or_default(); + changed = true; + } + out.push_str(rest); + if changed { Cow::Owned(out) } else { Cow::Borrowed(xmp) } +} + +/// Source pixel index for output pixel `(0, y2)` and the step per output column, for +/// orientation `o` of a `w`×`h` source (values 2–8; see the module docs). +fn walk(o: u16, w: isize, h: isize, y2: isize) -> (isize, isize) { + match o { + 2 => (y2 * w + w - 1, -1), + 3 => ((h - 1 - y2) * w + w - 1, -1), + 4 => ((h - 1 - y2) * w, 1), + 5 => (y2, w), + 6 => ((h - 1) * w + y2, -w), + 7 => ((h - 1) * w + (w - 1 - y2), -w), + 8 => (w - 1 - y2, w), + _ => (y2 * w, 1), + } +} + +/// Output columns per block: a block's source pixels (for a 90° turn, `BLOCK` +/// rows × the band height) stay in cache while it is written. +const BLOCK: usize = 64; +/// Output rows per parallel band. +const BAND: usize = 64; + +/// Writes the oriented pixels of one band of output rows, `N` bytes per pixel. +fn band(src: &[[u8; N]], dst: &mut [[u8; N]], o: u16, (w, h): (usize, usize), ow: usize, y0: usize) { + let (wi, hi) = (w as isize, h as isize); + let rows = dst.len() / ow.max(1); + for x0 in (0..ow).step_by(BLOCK) { + let x1 = (x0 + BLOCK).min(ow); + for r in 0..rows { + let (base, step) = walk(o, wi, hi, (y0 + r) as isize); + let Some(out) = dst.get_mut(r * ow + x0..r * ow + x1) else { return }; + let mut i = base + x0 as isize * step; + for px in out { + if let Some(s) = usize::try_from(i).ok().and_then(|i| src.get(i)) { + *px = *s; + } + i += step; + } + } + } +} + +/// Orients `src` (`N` bytes per pixel) into a new buffer, a band of rows per task. +fn orient_n(src: &[u8], o: u16, (w, h): (usize, usize), ow: usize) -> Result, CodecError> { + let mut out = alloc_zeroed(src.len())?; + let (s, _) = src.as_chunks::(); + let (d, _) = out.as_chunks_mut::(); + let per = (BAND * ow).max(1); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + d.par_chunks_mut(per).enumerate().for_each(|(b, chunk)| band(s, chunk, o, (w, h), ow, b * BAND)); + } + #[cfg(target_arch = "wasm32")] + for (b, chunk) in d.chunks_mut(per).enumerate() { + band(s, chunk, o, (w, h), ow, b * BAND); + } + Ok(out) +} + +/// A zeroed buffer of `n` bytes, or an error (not an abort) when memory runs out. +fn alloc_zeroed(n: usize) -> Result, CodecError> { + let mut out = Vec::new(); + out.try_reserve_exact(n).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {n} bytes to orient the image")))?; + out.resize(n, 0); + Ok(out) +} + +/// Pixel-format-agnostic orientation: any byte size per pixel. +fn orient_bytes(src: &[u8], bpp: usize, o: u16, (w, h): (usize, usize), ow: usize) -> Result, CodecError> { + match bpp { + 1 => orient_n::<1>(src, o, (w, h), ow), + 2 => orient_n::<2>(src, o, (w, h), ow), + 3 => orient_n::<3>(src, o, (w, h), ow), + 4 => orient_n::<4>(src, o, (w, h), ow), + 5 => orient_n::<5>(src, o, (w, h), ow), + 6 => orient_n::<6>(src, o, (w, h), ow), + 8 => orient_n::<8>(src, o, (w, h), ow), + 10 => orient_n::<10>(src, o, (w, h), ow), + 12 => orient_n::<12>(src, o, (w, h), ow), + 16 => orient_n::<16>(src, o, (w, h), ow), + _ => orient_n::<20>(src, o, (w, h), ow), + } +} + +/// Writes the oriented output rows `y0..` that fit in `dst` (whole rows of `ow` pixels), `bpp` +/// bytes per pixel: one band of [`orient_bytes`], the same pixels it writes. +fn orient_band(src: &[u8], dst: &mut [u8], bpp: usize, o: u16, (w, h): (usize, usize), ow: usize, y0: usize) { + macro_rules! go { + ($n:literal) => {{ + let (s, _) = src.as_chunks::<$n>(); + let (d, _) = dst.as_chunks_mut::<$n>(); + band(s, d, o, (w, h), ow, y0) + }}; + } + match bpp { + 1 => go!(1), + 2 => go!(2), + 3 => go!(3), + 4 => go!(4), + 5 => go!(5), + 6 => go!(6), + 8 => go!(8), + 10 => go!(10), + 12 => go!(12), + 16 => go!(16), + _ => go!(20), + } +} + +/// An image turned upright a band of rows at a time, for a caller that must not block for the +/// whole image (the web build has no threads): [`OrientSteps::step`] gives the [`Image`] +/// [`Image::oriented`] gives, once every band is written. +pub struct OrientSteps { + src: Image, + o: u16, + bpp: usize, + /// The oriented size. + ow: usize, + oh: usize, + out: Vec, + /// Output rows written so far. + done: usize, +} + +impl OrientSteps { + /// Sets up turning `img` upright for orientation `o` (2–8; anything else leaves it as it is). + /// Allocates the output, which the image is turned into beside the original. + pub fn new(img: Image, o: u16) -> Result { + let (w, h) = (img.width() as usize, img.height() as usize); + let (ow, oh) = if o >= 5 { (h, w) } else { (w, h) }; + let (layout, sample) = (img.layout(), img.sample_type()); + let bpp = layout.channels() * sample.bytes(); + let turns = (2..=8).contains(&o); + if turns && (!matches!(bpp, 1..=6 | 8 | 10 | 12 | 16 | 20) || w == 0 || h == 0) { + return Err(CodecError::InvalidImage(format!("cannot orient {layout:?} {sample:?} {w}x{h}"))); + } + // Reserve without touching: `step` grows the buffer a band at a time, so no slice + // zero-fills the whole output. + let mut out = Vec::new(); + if turns { + let n = img.data().len(); + out.try_reserve_exact(n).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {n} bytes to orient the image")))?; + } + Ok(Self { src: img, o, bpp, ow, oh, out, done: 0 }) + } + + /// How much of the image is turned, `0.0..=1.0`. + pub fn progress(&self) -> f32 { + if self.oh == 0 { 1.0 } else { self.done as f32 / self.oh as f32 } + } + + /// Writes bands of [`BAND`] output rows, at least one, then goes on while `more()` returns + /// true; `Some` with the upright image once the last band is written. + pub fn step(&mut self, more: &mut dyn FnMut() -> bool) -> Result, CodecError> { + if !(2..=8).contains(&self.o) { + let placeholder = Image::from_raw(0, 0, self.src.layout(), self.src.sample_type(), Vec::new())?; + return Ok(Some(std::mem::replace(&mut self.src, placeholder))); + } + let (w, h) = (self.src.width() as usize, self.src.height() as usize); + while self.done < self.oh { + let rows = BAND.min(self.oh - self.done); + let row_bytes = self.ow.checked_mul(self.bpp).ok_or_else(|| CodecError::InvalidImage("image too large".into()))?; + let at = self.done.saturating_mul(row_bytes); + let end = at.saturating_add(rows.saturating_mul(row_bytes)); + if self.out.len() < end { + self.out.resize(end, 0); + } + let dst = self.out.get_mut(at..end).ok_or_else(|| CodecError::InvalidImage("the oriented rows are out of range".into()))?; + orient_band(self.src.data(), dst, self.bpp, self.o, (w, h), self.ow, self.done); + self.done += rows; + if self.done < self.oh && !more() { + return Ok(None); + } + } + let (layout, sample) = (self.src.layout(), self.src.sample_type()); + let placeholder = Image::from_raw(0, 0, layout, sample, Vec::new())?; + let src = std::mem::replace(&mut self.src, placeholder); + let data = std::mem::take(&mut self.out); + src.oriented_with(self.o, data).map(Some) + } +} + +impl Image { + /// The image turned upright for orientation `o` (1–8; anything else is + /// treated as 1 and returns the image unchanged). Width and height swap + /// for 5–8, as does the DPI, and the EXIF and XMP orientation are + /// rewritten to 1 so the result is not rotated again on display or export. + pub fn oriented(self, o: u16) -> Result { + if !(2..=8).contains(&o) { + return Ok(self); + } + let (w, h) = (self.width() as usize, self.height() as usize); + let ow = if o >= 5 { h } else { w }; + let (layout, sample) = (self.layout(), self.sample_type()); + let bpp = layout.channels() * sample.bytes(); + // Every layout/sample pair is 1–20 bytes per pixel and has a fixed-size path. + if !matches!(bpp, 1..=6 | 8 | 10 | 12 | 16 | 20) || w == 0 || h == 0 { + return Err(CodecError::InvalidImage(format!("cannot orient {layout:?} {sample:?} {w}x{h}"))); + } + let data = orient_bytes(self.data(), bpp, o, (w, h), ow)?; + self.oriented_with(o, data) + } + + /// The upright image of orientation `o` (2–8) whose turned pixels are `data`: the size, + /// metadata and extra samples of [`Image::oriented`]. + fn oriented_with(self, o: u16, data: Vec) -> Result { + let (w, h) = (self.width() as usize, self.height() as usize); + let swap = o >= 5; + let (ow, oh) = if swap { (h, w) } else { (w, h) }; + let (layout, sample) = (self.layout(), self.sample_type()); + let mut meta = self.meta.clone(); + // Extra samples (a TIFF's alpha and spot planes, one sample a pixel) turn with the image. + // A plane of the wrong size is left as it is: importers drop it with a warning. + let plane_len = w.saturating_mul(h).saturating_mul(sample.bytes()); + for e in meta.extra_samples.iter_mut().filter(|e| e.data.len() == plane_len) { + e.data = orient_bytes(&e.data, sample.bytes(), o, (w, h), ow)?; + } + if let Some(e) = &mut meta.exif + && let Cow::Owned(fixed) = upright_exif(e) + { + *e = fixed; + } + if let Some(x) = &mut meta.xmp + && let Cow::Owned(fixed) = upright_xmp(x) + { + *x = fixed; + } + if swap { + meta.dpi = meta.dpi.map(|(x, y)| (y, x)); + } + let mut out = Image::from_raw(ow as u32, oh as u32, layout, sample, data)?.with_icc(self.icc).with_meta(meta); + out.warnings = self.warnings; + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_impossible_allocation_is_an_error_not_an_abort() { + assert!(matches!(alloc_zeroed(usize::MAX), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(orient_n::<1>(&[], 6, (0, 0), 0), Ok(v) if v.is_empty())); + assert_eq!(alloc_zeroed(3).unwrap(), vec![0, 0, 0]); + } +} diff --git a/crates/image-codecs/src/resolution.rs b/crates/image-codecs/src/resolution.rs new file mode 100644 index 000000000..a787cacbc --- /dev/null +++ b/crates/image-codecs/src/resolution.rs @@ -0,0 +1,575 @@ +//! Image resolution (pixels per inch) recorded in metadata blocks: EXIF / TIFF IFD0 +//! (XResolution 282, YResolution 283, ResolutionUnit 296), XMP (`tiff:XResolution`, +//! `tiff:YResolution`, `tiff:ResolutionUnit`) and Photoshop's image resource +//! ResolutionInfo (`8BIM` resource 0x03ED); reading them, and rewriting the EXIF and XMP +//! copies on export so they agree with the resolution the file is written at. +//! +//! Implemented from TIFF 6.0 (section 8, "Baseline fields": XResolution / YResolution are +//! RATIONAL, ResolutionUnit is SHORT, 1 = no absolute unit, 2 = inch, 3 = centimetre, +//! default 2), EXIF 2.3 (same tags in the 0th IFD, same default) and the Adobe Photoshop File +//! Formats Specification (ResolutionInfo: Fixed 16.16 hRes in pixels per inch, then the +//! display units; the vertical resolution likewise). +//! +//! **Precedence** when a file carries several, as Photoshop resolves them: Photoshop's own +//! ResolutionInfo, then the XMP `tiff:` properties, then EXIF, and only then the format's own +//! density field when it is the generic JFIF one (see the JPEG decoder). JFIF density is last +//! because cameras record the real resolution in EXIF while many tools and libraries write a +//! default JFIF density of 72 (or a bare aspect ratio) without knowing it; Photoshop shows a +//! camera JPEG at its EXIF resolution whatever its JFIF header says. Formats whose own field is +//! written deliberately (PNG `pHYs`, TIFF tags) keep it first; the metadata blocks only fill in +//! when it is missing. +//! +//! Everything read here is untrusted: offsets and counts are bounds-checked, both byte orders +//! and BigTIFF-style EXIF are handled, and anything malformed reads as "no resolution" rather +//! than as a wrong one. + +use std::borrow::Cow; + +use crate::orientation::{Order, ifd0_entries, tiff_body, upright_exif, upright_xmp}; + +const TAG_X_RESOLUTION: u16 = 282; +const TAG_Y_RESOLUTION: u16 = 283; +const TAG_RESOLUTION_UNIT: u16 = 296; +const TYPE_SHORT: u16 = 3; +const TYPE_RATIONAL: u16 = 5; +const CM_PER_INCH: f64 = 2.54; +/// Photoshop's ResolutionInfo image resource. +const RESOLUTION_INFO: u16 = 0x03ED; +/// Resolutions above this (pixels per inch) are treated as damage, not data. +const MAX_PPI: f64 = 1_000_000.0; +/// Two resolutions this close (pixels per inch) are the same. +const SAME_PPI: f64 = 1e-3; + +/// A usable resolution pair, or `None`. +fn ppi_pair(x: f64, y: f64) -> Option<(f32, f32)> { + let ok = |v: f64| v.is_finite() && v > 0.0 && v <= MAX_PPI; + (ok(x) && ok(y)).then_some((x as f32, y as f32)) +} + +fn same(a: (f32, f32), b: (f32, f32)) -> bool { + (f64::from(a.0) - f64::from(b.0)).abs() < SAME_PPI && (f64::from(a.1) - f64::from(b.1)).abs() < SAME_PPI +} + +/// `ppi` as a TIFF RATIONAL: a whole number over 1 when it is one, else over 1000. +fn rational(ppi: f32) -> (u32, u32) { + let p = f64::from(ppi); + if (p - p.round()).abs() < SAME_PPI { (p.round() as u32, 1) } else { ((p * 1000.0).round() as u32, 1000) } +} + +/// One of the three resolution entries of IFD0. +#[derive(Clone, Copy)] +struct Field { + /// Offset of the 12- (BigTIFF: 20-) byte entry. + entry: usize, + tag: u16, + ty: u16, + count: u64, + /// Where the value bytes are, when the type is one we read and they lie inside the block. + value: Option, +} + +impl Field { + fn short(&self, order: Order, b: &[u8]) -> Option { + (self.ty == TYPE_SHORT && self.count == 1).then_some(())?; + order.u16(b, self.value?) + } + + fn rational(&self, order: Order, b: &[u8]) -> Option { + (self.ty == TYPE_RATIONAL && self.count == 1).then_some(())?; + let at = self.value?; + let (n, d) = (order.u32(b, at)?, order.u32(b, at.checked_add(4)?)?); + (d != 0).then(|| f64::from(n) / f64::from(d)) + } +} + +/// IFD0's resolution entries. +struct Ifd0 { + order: Order, + big: bool, + first: usize, + count: usize, + entry_bytes: usize, + fields: Vec, +} + +impl Ifd0 { + fn parse(b: &[u8]) -> Option { + let (order, first, count, entry_bytes, big) = ifd0_entries(b)?; + let mut fields = Vec::new(); + for i in 0..count { + let e = first.checked_add(i.checked_mul(entry_bytes)?)?; + let tag = order.u16(b, e)?; + if !matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { + continue; + } + let ty = order.u16(b, e.checked_add(2)?)?; + let count = if big { order.u64(b, e.checked_add(4)?)? } else { u64::from(order.u32(b, e.checked_add(4)?)?) }; + let value = value_at(order, b, e, big, ty, count); + fields.push(Field { entry: e, tag, ty, count, value }); + } + Some(Ifd0 { order, big, first, count, entry_bytes, fields }) + } + + fn first_of(&self, tag: u16) -> Option<&Field> { + self.fields.iter().find(|f| f.tag == tag) + } +} + +/// Where an entry's SHORT or RATIONAL value bytes are: inline in the entry when they fit, +/// else at the offset it holds. `None` for other types or when they leave the block. +fn value_at(order: Order, b: &[u8], e: usize, big: bool, ty: u16, count: u64) -> Option { + let size = match ty { + TYPE_SHORT => 2u64, + TYPE_RATIONAL => 8, + _ => return None, + } + .checked_mul(count)?; + let (field, inline) = if big { (e.checked_add(12)?, 8) } else { (e.checked_add(8)?, 4) }; + let at = if size <= inline { + field + } else if big { + usize::try_from(order.u64(b, field)?).ok()? + } else { + usize::try_from(order.u32(b, field)?).ok()? + }; + (at.checked_add(usize::try_from(size).ok()?)? <= b.len()).then_some(at) +} + +/// The resolution (pixels per inch, horizontal and vertical) recorded in IFD0 of an EXIF block +/// (with or without the JPEG `Exif\0\0` prefix) or a TIFF structure. +/// +/// Both XResolution and YResolution must be one RATIONAL each with a non-zero denominator. +/// ResolutionUnit 2 (inch, also when the tag is absent) is used as is, 3 (centimetre) is +/// converted; 1 (no absolute unit), any other value or a malformed unit entry gives `None`. +/// The first of duplicated entries wins. +pub fn exif_resolution(exif: &[u8]) -> Option<(f32, f32)> { + let b = tiff_body(exif); + let ifd = Ifd0::parse(b)?; + let unit = match ifd.first_of(TAG_RESOLUTION_UNIT) { + None => 2, + Some(f) => f.short(ifd.order, b)?, + }; + let scale = match unit { + 2 => 1.0, + 3 => CM_PER_INCH, + _ => return None, + }; + let x = ifd.first_of(TAG_X_RESOLUTION)?.rational(ifd.order, b)?; + let y = ifd.first_of(TAG_Y_RESOLUTION)?.rational(ifd.order, b)?; + ppi_pair(x * scale, y * scale) +} + +/// `exif` with its IFD0 resolution set to `ppi` (pixels per inch), borrowed when it has no +/// resolution entries, already records that resolution, or cannot be parsed. +/// +/// Existing well-formed XResolution / YResolution (one RATIONAL) entries are rewritten in place, +/// and every well-formed ResolutionUnit (one SHORT) becomes 2 (inch). Malformed entries are +/// removed from IFD0 instead, so no stale value survives; all other bytes are kept. +pub fn exif_with_resolution(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { + exif_with_resolution_inner(exif, ppi, false) +} + +/// Like [`exif_with_resolution`], but adds X/Y resolution tags when a valid IFD0 has none. +/// Existing TIFF data stays in place, so its offsets remain valid. Used by CMYK JPEG, which +/// cannot carry a JFIF density marker. +pub(crate) fn exif_with_resolution_adding_missing(exif: &[u8], ppi: (f32, f32)) -> Cow<'_, [u8]> { + exif_with_resolution_inner(exif, ppi, true) +} + +/// Rewrite valid IFD0 resolution tags and optionally append tags that are missing or malformed. +fn exif_with_resolution_inner(exif: &[u8], ppi: (f32, f32), add_missing: bool) -> Cow<'_, [u8]> { + if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() { + return Cow::Borrowed(exif); + } + if exif.is_empty() { + if !add_missing { + return Cow::Borrowed(exif); + } + // An image with DPI but no source EXIF still needs a valid EXIF block when the output + // format cannot carry resolution in its own header (CMYK JPEG has no valid JFIF marker). + return Cow::Owned(exif_with_resolution_inner(b"II*\0\x08\0\0\0\0\0\0\0\0\0\0", ppi, true).into_owned()); + } + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let Some(ifd) = Ifd0::parse(body) else { return Cow::Borrowed(exif) }; + if exif_resolution(body).is_some_and(|r| same(r, ppi)) { + return Cow::Borrowed(exif); + } + let missing_xy = ifd.first_of(TAG_X_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none() + || ifd.first_of(TAG_Y_RESOLUTION).and_then(|f| f.rational(ifd.order, body)).is_none(); + if add_missing && missing_xy { + let Some(body) = append_resolution_ifd(body, &ifd, ppi) else { return Cow::Borrowed(exif) }; + let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); + full.extend_from_slice(&body); + return Cow::Owned(full); + } + if ifd.fields.is_empty() { + return Cow::Borrowed(exif); + } + let order = ifd.order; + let mut out = body.to_vec(); + let mut removed = Vec::new(); + for f in &ifd.fields { + let bytes = match f.tag { + TAG_RESOLUTION_UNIT if f.short(order, body).is_some() => put_u16(order, 2).to_vec(), + TAG_X_RESOLUTION | TAG_Y_RESOLUTION if f.rational(order, body).is_some() => { + let (n, d) = rational(if f.tag == TAG_X_RESOLUTION { ppi.0 } else { ppi.1 }); + [put_u32(order, n), put_u32(order, d)].concat() + } + _ => { + removed.push(f.entry); + continue; + } + }; + let slot = f.value.and_then(|at| out.get_mut(at..at.checked_add(bytes.len())?)); + match slot { + Some(slot) => slot.copy_from_slice(&bytes), + None => removed.push(f.entry), + } + } + if !removed.is_empty() { + remove_entries(&mut out, &ifd, &removed); + } + let mut full = exif.get(..prefix).unwrap_or_default().to_vec(); + full.extend_from_slice(&out); + if full == exif { Cow::Borrowed(exif) } else { Cow::Owned(full) } +} + +/// Append a replacement IFD0 with fresh resolution tags. Every other entry keeps its original +/// bytes and offsets, since the TIFF block's existing data remains where it was. +fn append_resolution_ifd(body: &[u8], ifd: &Ifd0, ppi: (f32, f32)) -> Option> { + let (count_bytes, next_bytes, alignment) = if ifd.big { (8usize, 8usize, 8usize) } else { (2usize, 4usize, 2usize) }; + let mut entries = Vec::with_capacity(ifd.count.saturating_add(3)); + for index in 0..ifd.count { + let entry = ifd.first.checked_add(index.checked_mul(ifd.entry_bytes)?)?; + let tag = ifd.order.u16(body, entry)?; + if matches!(tag, TAG_X_RESOLUTION | TAG_Y_RESOLUTION | TAG_RESOLUTION_UNIT) { + continue; + } + entries.push((tag, body.get(entry..entry.checked_add(ifd.entry_bytes)?)?.to_vec())); + } + let count = entries.len().checked_add(3)?; + if !ifd.big && count > usize::from(u16::MAX) { + return None; + } + + let new_ifd = align_up(body.len(), alignment)?; + let table_end = new_ifd.checked_add(count_bytes)?.checked_add(count.checked_mul(ifd.entry_bytes)?)?; + let next_at = ifd.first.checked_add(ifd.count.checked_mul(ifd.entry_bytes)?)?; + let next_ifd = body.get(next_at..next_at.checked_add(next_bytes)?)?; + let rational_at = align_up(table_end.checked_add(next_bytes)?, if ifd.big { 8 } else { 4 })?; + let y_rational_at = rational_at.checked_add(8)?; + if !ifd.big && (new_ifd > u32::MAX as usize || y_rational_at.checked_add(8)? > u32::MAX as usize) { + return None; + } + + entries.push((TAG_X_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_X_RESOLUTION, u64::try_from(rational_at).ok()?)?)); + entries.push((TAG_Y_RESOLUTION, resolution_entry(ifd.order, ifd.big, TAG_Y_RESOLUTION, u64::try_from(y_rational_at).ok()?)?)); + entries.push((TAG_RESOLUTION_UNIT, resolution_entry(ifd.order, ifd.big, TAG_RESOLUTION_UNIT, 2)?)); + entries.sort_by_key(|(tag, _)| *tag); + + let mut out = body.to_vec(); + out.resize(new_ifd, 0); + if ifd.big { + out.extend_from_slice(&put_u64(ifd.order, count as u64)); + } else { + out.extend_from_slice(&put_u16(ifd.order, count as u16)); + } + for (_, entry) in entries { + out.extend_from_slice(&entry); + } + out.extend_from_slice(next_ifd); + out.resize(rational_at, 0); + let (xn, xd) = rational(ppi.0); + let (yn, yd) = rational(ppi.1); + out.extend_from_slice(&put_u32(ifd.order, xn)); + out.extend_from_slice(&put_u32(ifd.order, xd)); + out.extend_from_slice(&put_u32(ifd.order, yn)); + out.extend_from_slice(&put_u32(ifd.order, yd)); + + let offset_at: usize = if ifd.big { 8 } else { 4 }; + if ifd.big { + out.get_mut(offset_at..offset_at.checked_add(8)?)?.copy_from_slice(&put_u64(ifd.order, new_ifd as u64)); + } else { + out.get_mut(offset_at..offset_at.checked_add(4)?)?.copy_from_slice(&put_u32(ifd.order, u32::try_from(new_ifd).ok()?)); + } + Some(out) +} + +/// One IFD entry. Rational fields store an offset; the SHORT unit is inline. +fn resolution_entry(order: Order, big: bool, tag: u16, value: u64) -> Option> { + let entry_bytes = if big { 20 } else { 12 }; + let count_at = 4; + let value_at = if big { 12 } else { 8 }; + let mut entry = vec![0; entry_bytes]; + entry.get_mut(..2)?.copy_from_slice(&put_u16(order, tag)); + entry.get_mut(2..4)?.copy_from_slice(&put_u16(order, if tag == TAG_RESOLUTION_UNIT { TYPE_SHORT } else { TYPE_RATIONAL })); + if big { + entry.get_mut(count_at..count_at + 8)?.copy_from_slice(&put_u64(order, 1)); + if tag == TAG_RESOLUTION_UNIT { + entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); + } else { + entry.get_mut(value_at..value_at + 8)?.copy_from_slice(&put_u64(order, value)); + } + } else { + entry.get_mut(count_at..count_at + 4)?.copy_from_slice(&put_u32(order, 1)); + if tag == TAG_RESOLUTION_UNIT { + entry.get_mut(value_at..value_at + 2)?.copy_from_slice(&put_u16(order, value as u16)); + } else { + entry.get_mut(value_at..value_at + 4)?.copy_from_slice(&put_u32(order, u32::try_from(value).ok()?)); + } + } + Some(entry) +} + +/// Round `value` up to the next `alignment` boundary, returning `None` on overflow. +fn align_up(value: usize, alignment: usize) -> Option { + value.checked_add((alignment - value % alignment) % alignment) +} + +fn put_u16(order: Order, v: u16) -> [u8; 2] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +fn put_u32(order: Order, v: u32) -> [u8; 4] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +fn put_u64(order: Order, v: u64) -> [u8; 8] { + match order { + Order::Little => v.to_le_bytes(), + Order::Big => v.to_be_bytes(), + } +} + +/// Removes the IFD0 entries at offsets `removed` (see [`exif_with_resolution`]). +fn remove_entries(b: &mut [u8], ifd: &Ifd0, removed: &[usize]) { + let eb = ifd.entry_bytes; + let next_bytes = if ifd.big { 8 } else { 4 }; + let count_bytes = if ifd.big { 8 } else { 2 }; + let Some(table_end) = ifd.count.checked_mul(eb).and_then(|n| ifd.first.checked_add(n)) else { return }; + let Some(old) = b.get(ifd.first..table_end.saturating_add(next_bytes)).map(<[u8]>::to_vec) else { return }; + let mut table = Vec::with_capacity(old.len()); + let mut kept = 0usize; + for (i, entry) in old.chunks_exact(eb).take(ifd.count).enumerate() { + if !removed.contains(&(ifd.first + i * eb)) { + table.extend_from_slice(entry); + kept += 1; + } + } + table.extend_from_slice(old.get(ifd.count * eb..).unwrap_or_default()); + table.resize(old.len(), 0); + let count: Vec = if ifd.big { put_u64(ifd.order, kept as u64).to_vec() } else { put_u16(ifd.order, kept as u16).to_vec() }; + let Some(count_at) = ifd.first.checked_sub(count_bytes) else { return }; + if let Some(slot) = b.get_mut(count_at..ifd.first) { + slot.copy_from_slice(&count); + } + if let Some(slot) = b.get_mut(ifd.first..ifd.first + table.len()) { + slot.copy_from_slice(&table); + } +} + +/// The resolution in a Photoshop image resource block (a sequence of `8BIM` resources, as in a +/// JPEG's APP13 "Photoshop 3.0" segment or TIFF tag 34377): its ResolutionInfo (0x03ED), whose +/// Fixed 16.16 hRes / vRes are pixels per inch whatever display unit follows them. +pub fn photoshop_resolution(resources: &[u8]) -> Option<(f32, f32)> { + let be16 = |at: usize| resources.get(at..at.checked_add(2)?).map(|s| u16::from_be_bytes([s[0], s[1]])); + let be32 = |at: usize| resources.get(at..at.checked_add(4)?).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])); + let mut i = 0usize; + while i < resources.len() { + let signature = resources.get(i..i.checked_add(4)?)?; + let id = be16(i.checked_add(4)?)?; + // Pascal name: length byte plus text, padded to an even size. + let name_len = usize::from(*resources.get(i.checked_add(6)?)?); + let name = (name_len + 1).next_multiple_of(2); + let size_at = i.checked_add(6)?.checked_add(name)?; + let size = usize::try_from(be32(size_at)?).ok()?; + let data = size_at.checked_add(4)?; + if signature == b"8BIM" && id == RESOLUTION_INFO { + let info = resources.get(data..data.checked_add(size)?)?; + if info.len() < 16 { + return None; + } + let fixed = |at: usize| be32(data + at).map(|v| f64::from(v) / 65536.0); + return ppi_pair(fixed(0)?, fixed(8)?); + } + i = data.checked_add(size)?.checked_add(size % 2)?; + } + None +} + +/// The text ranges of every value of XMP property `key` (`key="v"`, `key='v'` or +/// `v`), in order. +fn xmp_values(xmp: &str, key: &str) -> Vec<(usize, usize)> { + let mut out = Vec::new(); + let mut from = 0; + while let Some(k) = xmp.get(from..).and_then(|s| s.find(key)).map(|k| k + from) { + from = k + key.len(); + // An attribute or an opening tag of exactly this name (not ``, not `xkey`). + if !xmp.get(..k).and_then(|h| h.chars().next_back()).is_some_and(|c| c == '<' || c.is_ascii_whitespace()) { + continue; + } + let rest = xmp.get(from..).unwrap_or_default(); + let after_key = rest.trim_start(); + let (value, close) = if let Some(r) = after_key.strip_prefix('=') { + let r = r.trim_start(); + match r.chars().next() { + Some(q @ ('"' | '\'')) => (r.get(1..).unwrap_or_default(), q), + _ => continue, + } + } else if let Some(r) = rest.strip_prefix('>') { + (r, '<') + } else { + continue; + }; + let start = xmp.len() - value.len(); + // Advance past this value before searching for the next key. Without this, `from` + // only moved past the key text, so property-like text repeated inside an unrelated + // attribute re-ran this full-suffix `find(close)` for every occurrence, which is + // quadratic in the packet size. A missing close means the value is unterminated, so + // no later value can parse either; stop scanning in that case. + let Some(len) = value.find(close) else { break }; + out.push((start, start + len)); + from = start + len + close.len_utf8(); + } + out +} + +/// An XMP rational (`300/1`) or plain number. +fn xmp_number(s: &str) -> Option { + let s = s.trim(); + match s.split_once('/') { + Some((n, d)) => { + let (n, d) = (n.trim().parse::().ok()?, d.trim().parse::().ok()?); + (d != 0.0).then(|| n / d) + } + None => s.parse().ok(), + } +} + +const XMP_X: &str = "tiff:XResolution"; +const XMP_Y: &str = "tiff:YResolution"; +const XMP_UNIT: &str = "tiff:ResolutionUnit"; + +/// The resolution (pixels per inch) in an XMP packet's `tiff:XResolution` / `tiff:YResolution` +/// (with `tiff:ResolutionUnit`, 2 when absent, 3 converted from centimetres), or `None`. +pub fn xmp_resolution(xmp: &str) -> Option<(f32, f32)> { + let first = |key: &str| xmp_values(xmp, key).first().and_then(|&(a, b)| xmp.get(a..b)); + let unit = match first(XMP_UNIT) { + None => 2.0, + Some(u) => xmp_number(u)?, + }; + let scale = if unit == 2.0 { + 1.0 + } else if unit == 3.0 { + CM_PER_INCH + } else { + return None; + }; + ppi_pair(xmp_number(first(XMP_X)?)? * scale, xmp_number(first(XMP_Y)?)? * scale) +} + +/// `xmp` with its `tiff:` resolution properties set to `ppi` (and the unit to 2, inch), +/// borrowed when it has none or already says so. +pub fn xmp_with_resolution(xmp: &str, ppi: (f32, f32)) -> Cow<'_, str> { + if ppi_pair(f64::from(ppi.0), f64::from(ppi.1)).is_none() || xmp_resolution(xmp).is_some_and(|r| same(r, ppi)) { + return Cow::Borrowed(xmp); + } + let text = |v: f32| { + let (n, d) = rational(v); + format!("{n}/{d}") + }; + let mut edits: Vec<(usize, usize, String)> = Vec::new(); + for (key, value) in [(XMP_X, text(ppi.0)), (XMP_Y, text(ppi.1)), (XMP_UNIT, "2".to_owned())] { + edits.extend(xmp_values(xmp, key).into_iter().map(|(a, b)| (a, b, value.clone()))); + } + if edits.is_empty() { + return Cow::Borrowed(xmp); + } + edits.sort_by_key(|e| e.0); + let mut out = String::with_capacity(xmp.len() + 16); + let mut at = 0; + for (a, b, v) in edits { + // Overlapping ranges can only come from malformed markup; keep the first edit. + if a < at { + continue; + } + out.push_str(xmp.get(at..a).unwrap_or_default()); + out.push_str(&v); + at = b; + } + out.push_str(xmp.get(at..).unwrap_or_default()); + if out == xmp { Cow::Borrowed(xmp) } else { Cow::Owned(out) } +} + +/// `exif` without its embedded thumbnail, or `None` when it has none (or doesn't parse). +/// +/// EXIF keeps the thumbnail in IFD1, the IFD IFD0's next-IFD pointer names. Finder, Android's +/// gallery and upload pickers show that thumbnail instead of decoding the image, so a copied one +/// keeps showing the photo as it was before it was edited (#2147, #2373). The pointer is cleared; +/// nothing else moves, so every other offset in the block stays valid. +fn exif_without_thumbnail(exif: &[u8]) -> Option> { + let body = tiff_body(exif); + let prefix = exif.len() - body.len(); + let (_, first, count, entry_bytes, big_tiff) = ifd0_entries(body)?; + let next = prefix.checked_add(first)?.checked_add(count.checked_mul(entry_bytes)?)?; + let width = if big_tiff { 8 } else { 4 }; + // In bounds: `ifd0_entries` checked that the whole pointer is present. + if exif.get(next..next.checked_add(width)?)?.iter().all(|b| *b == 0) { + return None; + } + let mut out = exif.to_vec(); + out.get_mut(next..next + width)?.fill(0); + Some(out) +} + +/// The EXIF to write next to upright pixels at resolution `ppi`: Orientation 1 (see +/// [`upright_exif`]), no thumbnail of the source image (see [`exif_without_thumbnail`]) and, +/// when `ppi` is known, the resolution (see [`exif_with_resolution`]). +pub fn export_exif(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { + export_exif_inner(exif, ppi, false) +} + +/// Export EXIF for a format without its own resolution field, adding missing X/Y resolution +/// entries to valid metadata when necessary. +pub(crate) fn export_exif_adding_missing_resolution(exif: &[u8], ppi: Option<(f32, f32)>) -> Cow<'_, [u8]> { + export_exif_inner(exif, ppi, true) +} + +/// Normalize EXIF orientation and thumbnails, optionally adding missing output resolution tags. +fn export_exif_inner(exif: &[u8], ppi: Option<(f32, f32)>, add_missing: bool) -> Cow<'_, [u8]> { + let up = upright_exif(exif); + let up = match exif_without_thumbnail(&up) { + Some(v) => Cow::Owned(v), + None => up, + }; + let Some(ppi) = ppi else { return up }; + let result = if add_missing { exif_with_resolution_adding_missing(&up, ppi) } else { exif_with_resolution(&up, ppi) }; + if let Cow::Owned(v) = result { + return Cow::Owned(v); + } + up +} + +/// The XMP to write next to upright pixels at resolution `ppi` (see [`upright_xmp`] and +/// [`xmp_with_resolution`]). +pub fn export_xmp(xmp: &str, ppi: Option<(f32, f32)>) -> Cow<'_, str> { + let up = upright_xmp(xmp); + let Some(ppi) = ppi else { return up }; + if let Cow::Owned(v) = xmp_with_resolution(&up, ppi) { + return Cow::Owned(v); + } + up +} + +/// The resolution a decoded file's metadata blocks record, in precedence order: XMP, then +/// EXIF (Photoshop's ResolutionInfo is format-specific and read by the decoder). +pub(crate) fn from_metadata(exif: Option<&[u8]>, xmp: Option<&str>) -> Option<(f32, f32)> { + xmp.and_then(xmp_resolution).or_else(|| exif.and_then(exif_resolution)) +} diff --git a/crates/image-codecs/src/source_settings.rs b/crates/image-codecs/src/source_settings.rs new file mode 100644 index 000000000..ebd0421c3 --- /dev/null +++ b/crates/image-codecs/src/source_settings.rs @@ -0,0 +1,355 @@ +//! The encoder settings a lossy file was written with, read back from its own data, so a save +//! can write it again the way it came (File › Save keeps a JPEG's quality and a WebP's lossy or +//! lossless encoding). +//! +//! * **JPEG**: the quality is estimated from the quantisation tables (DQT). Most encoders scale +//! the example tables of ITU-T T.81 Annex K (K.1 luminance, K.2 chrominance) by the +//! Independent JPEG Group's quality curve, so the quality whose scaled tables match the file's +//! best is the one it was saved at (exact for such tables; the closest fit for others). +//! * **WebP**: a `VP8L` image is lossless; a `VP8 ` image is lossy, and its base quantiser index +//! (RFC 6386 §9.6) maps back to a quality on the scale our encoder uses. +//! +//! Everything here reads hostile bytes: every index is checked and a malformed file gives `None`. + +use crate::codecs::vp8; + +/// ITU-T T.81 Annex K.1, the example luminance quantisation table, in natural (row) order. +const ANNEX_K_LUMA: [u16; 64] = [ + 16, 11, 10, 16, 24, 40, 51, 61, // + 12, 12, 14, 19, 26, 58, 60, 55, // + 14, 13, 16, 24, 40, 57, 69, 56, // + 14, 17, 22, 29, 51, 87, 80, 62, // + 18, 22, 37, 56, 68, 109, 103, 77, // + 24, 35, 55, 64, 81, 104, 113, 92, // + 49, 64, 78, 87, 103, 121, 120, 101, // + 72, 92, 95, 98, 112, 100, 103, 99, +]; + +/// ITU-T T.81 Annex K.2, the example chrominance quantisation table, in natural order. +const ANNEX_K_CHROMA: [u16; 64] = [ + 17, 18, 24, 47, 99, 99, 99, 99, // + 18, 21, 26, 66, 99, 99, 99, 99, // + 24, 26, 56, 99, 99, 99, 99, 99, // + 47, 66, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, // + 99, 99, 99, 99, 99, 99, 99, 99, +]; + +/// The natural-order index of the k-th coefficient in zig-zag order (T.81 Figure A.6): DQT +/// stores its 64 values in this order. +const ZIGZAG: [usize; 64] = [ + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, + 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, +]; + +/// One quantisation table from a DQT segment, in natural order, with its precision. +#[derive(Debug, Clone, Copy)] +struct Table { + values: [u16; 64], + /// 16-bit entries (Pq = 1) rather than 8-bit. + wide: bool, +} + +/// The quality (1–100, the Independent JPEG Group's scale that Save As and Export As use) a +/// JPEG was most likely saved at, from its quantisation tables; `None` when it has no usable +/// luminance table (or isn't a JPEG). +pub fn jpeg_quality(bytes: &[u8]) -> Option { + let tables = jpeg_tables(bytes); + let [luma, chroma, ..] = tables; + let luma = luma?; + let mut best: Option<(u64, u8)> = None; + for q in 1..=100u8 { + let mut err = table_error(&luma, &ANNEX_K_LUMA, q); + if let Some(c) = chroma { + err = err.saturating_add(table_error(&c, &ANNEX_K_CHROMA, q)); + } + if best.is_none_or(|(e, _)| err < e) { + best = Some((err, q)); + } + } + best.map(|(_, q)| q) +} + +/// The Independent JPEG Group's quality scaling of `base` (libjpeg's `jpeg_quality_scaling` and +/// `jpeg_add_quant_table`, as published with its source): a percentage scale of 5000 / q below +/// quality 50 and 200 − 2q from it, rounded, and kept within 1..=255 (8-bit) or 1..=32767. +fn scaled(base: u16, q: u8, wide: bool) -> u32 { + let q = u32::from(q.clamp(1, 100)); + let scale = if q < 50 { 5000 / q } else { 200 - 2 * q }; + let v = (u32::from(base) * scale + 50) / 100; + v.clamp(1, if wide { 32767 } else { 255 }) +} + +/// How far `t` is from `base` scaled to quality `q`: the summed absolute difference. +fn table_error(t: &Table, base: &[u16; 64], q: u8) -> u64 { + t.values.iter().zip(base).map(|(&v, &b)| u64::from(u32::from(v).abs_diff(scaled(b, q, t.wide)))).sum() +} + +/// The quantisation tables 0..=3 defined before the first scan (a later definition of the same +/// slot replaces an earlier one). +fn jpeg_tables(b: &[u8]) -> [Option; 4] { + let mut tables = [None; 4]; + if b.get(..2) != Some(&[0xFF, 0xD8]) { + return tables; + } + let mut i = 2usize; + while let (Some(&0xFF), Some(&marker)) = (b.get(i), b.get(i.saturating_add(1))) { + if marker == 0xFF { + i = i.saturating_add(1); + continue; + } + if marker == 0xD8 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 { + i = i.saturating_add(2); + continue; + } + if marker == 0xDA || marker == 0xD9 { + break; + } + let Some(len) = b.get(i + 2..i + 4).map(|l| usize::from(u16::from_be_bytes([l[0], l[1]]))) else { break }; + let Some(seg) = len.checked_sub(2).and_then(|n| b.get(i + 4..i + 4 + n)) else { break }; + if marker == 0xDB { + read_dqt(seg, &mut tables); + } + i = i.saturating_add(2).saturating_add(len); + } + tables +} + +/// The tables of one DQT segment; a truncated or malformed table ends the segment. +fn read_dqt(mut seg: &[u8], tables: &mut [Option
; 4]) { + while let Some((&pq_tq, rest)) = seg.split_first() { + let wide = pq_tq >> 4 == 1; + if pq_tq >> 4 > 1 { + return; + } + let slot = usize::from(pq_tq & 0x0F); + let n = if wide { 128 } else { 64 }; + let Some(data) = rest.get(..n) else { return }; + let mut values = [0u16; 64]; + for (k, &natural) in ZIGZAG.iter().enumerate() { + let v = if wide { data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) } else { data.get(k).map_or(0, |&p| u16::from(p)) }; + if let Some(dst) = values.get_mut(natural) { + *dst = v; + } + } + if let Some(dst) = tables.get_mut(slot) { + *dst = Some(Table { values, wide }); + } + seg = rest.get(n..).unwrap_or(&[]); + } +} + +/// How a WebP file's image is compressed. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WebpEncoding { + /// VP8L. + Lossless, + /// VP8, with the quality (0–100, our encoder's scale) its quantiser maps back to, when the + /// frame header could be read. + Lossy { quality: Option }, +} + +/// The compression of a WebP file's (first) image, or `None` when it isn't a WebP or holds no +/// image chunk. +pub fn webp_encoding(bytes: &[u8]) -> Option { + if bytes.get(..4) != Some(b"RIFF") || bytes.get(8..12) != Some(b"WEBP") { + return None; + } + find_image_chunk(bytes.get(12..)?, 0) +} + +/// The first `VP8 ` or `VP8L` chunk among `chunks`, looking inside animation frames. +fn find_image_chunk(mut chunks: &[u8], depth: u8) -> Option { + while let Some(head) = chunks.get(..8) { + let fourcc = head.get(..4)?; + let size = usize::try_from(u32::from_le_bytes([head[4], head[5], head[6], head[7]])).ok()?; + let end = 8usize.checked_add(size)?; + // A truncated last chunk still holds its header. + let payload = chunks.get(8..end.min(chunks.len()))?; + match fourcc { + b"VP8L" => return Some(WebpEncoding::Lossless), + b"VP8 " => return Some(WebpEncoding::Lossy { quality: vp8_quality(payload) }), + // An animation frame: a 16-byte header, then the frame's own chunks. + b"ANMF" if depth == 0 => { + if let Some(found) = payload.get(16..).and_then(|c| find_image_chunk(c, 1)) { + return Some(found); + } + } + _ => {} + } + chunks = chunks.get(end.checked_add(size % 2)?..).unwrap_or(&[]); + } + None +} + +/// The quality a VP8 key frame's base quantiser index maps back to on our encoder's scale (the +/// quality whose index is nearest; the middle one when several share it). +fn vp8_quality(frame: &[u8]) -> Option { + let qi = vp8_base_quantizer(frame)?; + let distance = |q: u8| vp8::quantizer_index(q).abs_diff(qi); + let best = (0..=100u8).map(distance).min()?; + let matches: Vec = (0..=100u8).filter(|&q| distance(q) == best).collect(); + let mid = matches.get(matches.len() / 2)?; + Some(*mid) +} + +/// The base quantiser index of a VP8 key frame: `y_ac_qi` from its frame header (RFC 6386 +/// §9.2–9.6, §19.2), averaged over the segments when segmentation sets absolute quantisers or +/// adjusted by their mean delta otherwise. +fn vp8_base_quantizer(frame: &[u8]) -> Option { + let tag = frame.get(..3)?; + // Bit 0 clear: a key frame, which carries the start code and dimensions. + if tag[0] & 1 != 0 || frame.get(3..6)? != [0x9D, 0x01, 0x2A] { + return None; + } + let mut d = BoolDecoder::new(frame.get(10..)?); + d.literal(1); // color_space + d.literal(1); // clamping_type + let mut segments: Option<(bool, [i32; 4])> = None; + if d.flag() { + let update_map = d.flag(); + if d.flag() { + let absolute = d.flag(); + let mut q = [0i32; 4]; + for v in &mut q { + *v = if d.flag() { d.signed(7) } else { 0 }; + } + for _ in 0..4 { + if d.flag() { + d.signed(6); + } + } + segments = Some((absolute, q)); + } + if update_map { + for _ in 0..3 { + if d.flag() { + d.literal(8); + } + } + } + } + d.literal(1); // filter_type + d.literal(6); // loop_filter_level + d.literal(3); // sharpness_level + if d.flag() && d.flag() { + // loop_filter_adj_enable, mode_ref_lf_delta_update: 4 reference and 4 mode deltas. + for _ in 0..8 { + if d.flag() { + d.signed(6); + } + } + } + d.literal(2); // log2_nbr_of_dct_partitions + if d.exhausted() { + return None; + } + let base = d.literal(7) as i32; + let qi = match segments { + Some((true, q)) => q.iter().sum::() / 4, + Some((false, q)) => base + q.iter().sum::() / 4, + None => base, + }; + u8::try_from(qi.clamp(0, 127)).ok() +} + +/// RFC 6386 §7's boolean decoder, reading zeros past the end of its data. +struct BoolDecoder<'a> { + data: &'a [u8], + pos: usize, + value: u32, + range: u32, + bit_count: u32, + /// Bytes read past the end of the data. + overrun: usize, +} + +impl<'a> BoolDecoder<'a> { + fn new(data: &'a [u8]) -> Self { + let mut d = BoolDecoder { data, pos: 0, value: 0, range: 255, bit_count: 0, overrun: 0 }; + d.value = (d.next_byte() << 8) | d.next_byte(); + d + } + + fn next_byte(&mut self) -> u32 { + match self.data.get(self.pos) { + Some(&b) => { + self.pos += 1; + u32::from(b) + } + None => { + self.overrun = self.overrun.saturating_add(1); + 0 + } + } + } + + /// The header was read from beyond the end of the data (a couple of bytes of look-ahead + /// are normal). + fn exhausted(&self) -> bool { + self.overrun > 2 + } + + fn bool(&mut self, prob: u32) -> bool { + let split = 1 + (((self.range - 1) * prob) >> 8); + let big_split = split << 8; + let bit = if self.value >= big_split { + self.range -= split; + self.value -= big_split; + true + } else { + self.range = split; + false + }; + while self.range < 128 { + self.value <<= 1; + self.range <<= 1; + self.bit_count += 1; + if self.bit_count == 8 { + self.bit_count = 0; + self.value |= self.next_byte(); + } + } + bit + } + + fn flag(&mut self) -> bool { + self.bool(128) + } + + fn literal(&mut self, bits: u32) -> u32 { + (0..bits).fold(0, |v, _| (v << 1) | u32::from(self.flag())) + } + + /// A magnitude of `bits` bits followed by a sign bit. + fn signed(&mut self, bits: u32) -> i32 { + let v = self.literal(bits) as i32; + if self.flag() { -v } else { v } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scaling_matches_the_published_curve() { + // Quality 50 is the Annex K table itself; 100 is all ones; 75 halves it. + assert_eq!(scaled(16, 50, false), 16); + assert_eq!(scaled(121, 100, false), 1); + assert_eq!(scaled(16, 75, false), 8); + assert_eq!(scaled(121, 1, false), 255); + assert_eq!(scaled(121, 1, true), 6050); + } + + #[test] + fn zigzag_is_a_permutation() { + let mut seen = [false; 64]; + for &n in &ZIGZAG { + seen[n] = true; + } + assert!(seen.iter().all(|&s| s)); + } +} diff --git a/crates/image-codecs/src/web.rs b/crates/image-codecs/src/web.rs new file mode 100644 index 000000000..dfd0cd9b9 --- /dev/null +++ b/crates/image-codecs/src/web.rs @@ -0,0 +1,525 @@ +//! Web-optimised writers for Save for Web: indexed GIF (our own LZW encoder, GIF89a spec), +//! WBMP (WAP Wireless Bitmap, type 0) and baseline/progressive JPEG from 8-bit RGB. +//! +//! Indexed PNG is [`crate::encode_png_indexed`]. Every writer here has a matching reader (GIF +//! through the regular decoder, WBMP through [`decode_wbmp`]), keeping the crate symmetric. + +use std::collections::HashMap; + +use crate::error::CodecError; +use crate::format::Format; + +fn gif_err(msg: impl Into) -> CodecError { + CodecError::InvalidImage(msg.into()) +} + +/// Packs variable-length LZW codes LSB-first into GIF data sub-blocks. +struct BitSink { + bytes: Vec, + acc: u32, + nbits: u32, +} + +impl BitSink { + fn put(&mut self, code: u16, size: u32) { + self.acc |= u32::from(code) << self.nbits; + self.nbits += size; + while self.nbits >= 8 { + self.bytes.push(self.acc as u8); + self.acc >>= 8; + self.nbits -= 8; + } + } + fn finish(mut self) -> Vec { + if self.nbits > 0 { + self.bytes.push(self.acc as u8); + } + self.bytes + } +} + +/// GIF variable-code-length LZW compression of `indices` (each < 2^`min_code_size`). +pub fn lzw_encode(indices: &[u8], min_code_size: u8) -> Vec { + let min = u32::from(min_code_size.clamp(2, 8)); + let clear = 1u16 << min; + let eoi = clear + 1; + let mut size = min + 1; + let mut next = eoi + 1; + let mut dict: HashMap = HashMap::with_capacity(4096); + let mut out = BitSink { bytes: Vec::with_capacity(indices.len() / 2 + 16), acc: 0, nbits: 0 }; + out.put(clear, size); + let Some((&first, rest)) = indices.split_first() else { + out.put(eoi, size); + return out.finish(); + }; + let mut prefix = u16::from(first); + for &k in rest { + let key = u32::from(prefix) << 8 | u32::from(k); + if let Some(&c) = dict.get(&key) { + prefix = c; + continue; + } + out.put(prefix, size); + // The decoder adds its entries one code late, so widen once the next free code no + // longer fits (before assigning it), exactly like the classic compress-based encoders. + if u32::from(next) >= 1 << size && size < 12 { + size += 1; + } + if next < 4096 { + dict.insert(key, next); + next += 1; + } else { + out.put(clear, size); + dict.clear(); + size = min + 1; + next = eoi + 1; + } + prefix = u16::from(k); + } + out.put(prefix, size); + if u32::from(next) >= 1 << size && size < 12 { + size += 1; + } + out.put(eoi, size); + out.finish() +} + +/// Rows in GIF interlaced order (passes every 8th from 0, every 8th from 4, every 4th from 2, +/// every 2nd from 1). +fn interlaced_rows(h: usize) -> Vec { + let mut v = Vec::with_capacity(h); + for (start, step) in [(0, 8), (4, 8), (2, 4), (1, 2)] { + v.extend((start..h).step_by(step)); + } + v +} + +/// An indexed GIF89a: `palette` (1..=256 entries), one index per pixel, optional transparent +/// index (Graphic Control Extension) and interlacing. +pub fn encode_gif_indexed( + width: u32, + height: u32, + indices: &[u8], + palette: &[[u8; 3]], + transparent: Option, + interlaced: bool, +) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || width > 65535 || height > 65535 || indices.len() != w * h { + return Err(gif_err("GIF size must be 1..=65535 and match the index data")); + } + if palette.is_empty() || palette.len() > 256 || indices.iter().any(|&i| usize::from(i) >= palette.len()) { + return Err(gif_err("palette must have 1..=256 entries covering every index")); + } + // Colour table size: 2^(bits) entries, bits ≥ 1. + let bits = (1..=8u8).find(|b| (1usize << b) >= palette.len()).unwrap_or(8); + let mut out = b"GIF89a".to_vec(); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x80 | ((bits - 1) << 4) | (bits - 1)); + out.push(transparent.unwrap_or(0)); + out.push(0); + for i in 0..(1usize << bits) { + out.extend(palette.get(i).copied().unwrap_or([0, 0, 0])); + } + if let Some(t) = transparent { + out.extend([0x21, 0xF9, 0x04, 0x01, 0, 0, t, 0]); + } + out.push(0x2C); + out.extend([0, 0, 0, 0]); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(if interlaced { 0x40 } else { 0 }); + let min = bits.max(2); + out.push(min); + let data = if interlaced { + let mut v = Vec::with_capacity(indices.len()); + for y in interlaced_rows(h) { + v.extend_from_slice(&indices[y * w..(y + 1) * w]); + } + lzw_encode(&v, min) + } else { + lzw_encode(indices, min) + }; + for chunk in data.chunks(255) { + out.push(chunk.len() as u8); + out.extend_from_slice(chunk); + } + out.push(0); + out.push(0x3B); + Ok(out) +} + +/// One frame of an animated GIF: palette indices, its (local) palette, an optional transparent +/// index, and the on-screen delay in centiseconds. +pub struct GifFrame { + pub indices: Vec, + pub palette: Vec<[u8; 3]>, + pub transparent: Option, + pub delay_cs: u16, +} + +/// Encode an animated GIF (GIF89a). Each frame carries its own local colour table; `loop_forever` +/// adds the NETSCAPE2.0 looping extension. +pub fn encode_gif_animated(width: u32, height: u32, frames: &[GifFrame], loop_forever: bool) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || width > 65535 || height > 65535 { + return Err(gif_err("GIF size must be 1..=65535")); + } + if frames.is_empty() { + return Err(gif_err("an animated GIF needs at least one frame")); + } + let mut out = b"GIF89a".to_vec(); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x70); // no global colour table, 8-bit colour resolution + out.push(0); // background colour index + out.push(0); // pixel aspect ratio + if loop_forever { + out.extend([0x21, 0xFF, 0x0B]); + out.extend_from_slice(b"NETSCAPE2.0"); + out.extend([0x03, 0x01, 0x00, 0x00, 0x00]); // loop count 0 = forever + } + for f in frames { + if f.palette.is_empty() || f.palette.len() > 256 || f.indices.len() != w * h || f.indices.iter().any(|&i| usize::from(i) >= f.palette.len()) { + return Err(gif_err("every frame's palette must have 1..=256 entries covering its indices")); + } + let bits = (1..=8u8).find(|b| (1usize << b) >= f.palette.len()).unwrap_or(8); + // Graphic Control Extension: delay and optional transparency. Every frame covers the whole + // image, so an opaque one is left in place (disposal 1), while one with a transparent index + // is cleared afterwards (disposal 2): otherwise the frames before it show through. + let packed = if f.transparent.is_some() { 0x09 } else { 0x04 }; + out.extend([0x21, 0xF9, 0x04, packed]); + out.extend(f.delay_cs.to_le_bytes()); + out.push(f.transparent.unwrap_or(0)); + out.push(0); + // Image Descriptor with a Local Colour Table. + out.push(0x2C); + out.extend([0, 0, 0, 0]); + out.extend((width as u16).to_le_bytes()); + out.extend((height as u16).to_le_bytes()); + out.push(0x80 | (bits - 1)); + for i in 0..(1usize << bits) { + out.extend(f.palette.get(i).copied().unwrap_or([0, 0, 0])); + } + let min = bits.max(2); + out.push(min); + for chunk in lzw_encode(&f.indices, min).chunks(255) { + out.push(chunk.len() as u8); + out.extend_from_slice(chunk); + } + out.push(0); + } + out.push(0x3B); + Ok(out) +} + +fn put_multibyte(out: &mut Vec, v: u32) { + let mut groups = vec![(v & 0x7f) as u8]; + let mut v = v >> 7; + while v > 0 { + groups.push((v & 0x7f) as u8 | 0x80); + v >>= 7; + } + out.extend(groups.iter().rev()); +} + +/// WBMP type 0 (1 bit per pixel, rows padded to bytes, 1 = white). `white[i]` per pixel. +pub fn encode_wbmp(width: u32, height: u32, white: &[bool]) -> Result, CodecError> { + let (w, h) = (width as usize, height as usize); + if width == 0 || height == 0 || white.len() != w * h { + return Err(CodecError::InvalidImage("WBMP size must match the pixel data".into())); + } + let mut out = vec![0u8, 0u8]; + put_multibyte(&mut out, width); + put_multibyte(&mut out, height); + let stride = w.div_ceil(8); + for y in 0..h { + let mut row = vec![0u8; stride]; + for x in 0..w { + if white[y * w + x] { + row[x / 8] |= 0x80 >> (x % 8); + } + } + out.extend(row); + } + Ok(out) +} + +/// Reads a WBMP type 0 image: (width, height, white per pixel). +pub fn decode_wbmp(bytes: &[u8]) -> Result<(u32, u32, Vec), CodecError> { + let bad = || CodecError::InvalidImage("not a type 0 WBMP".into()); + let mut i = 0usize; + let mut next = || -> Result { + let b = *bytes.get(i).ok_or_else(bad)?; + i += 1; + Ok(b) + }; + if next()? != 0 || next()? & 0x9f != 0 { + return Err(bad()); + } + let mut mb = || -> Result { + let mut v: u32 = 0; + for _ in 0..5 { + let b = next()?; + v = v.checked_mul(128).ok_or_else(bad)? | u32::from(b & 0x7f); + if b & 0x80 == 0 { + return Ok(v); + } + } + Err(bad()) + }; + let (w, h) = (mb()?, mb()?); + if w == 0 || h == 0 || u64::from(w) * u64::from(h) > 1 << 28 { + return Err(bad()); + } + let stride = (w as usize).div_ceil(8); + let data = bytes.get(i..i + stride * h as usize).ok_or_else(bad)?; + let mut px = Vec::with_capacity(w as usize * h as usize); + for y in 0..h as usize { + for x in 0..w as usize { + px.push(data[y * stride + x / 8] & (0x80 >> (x % 8)) != 0); + } + } + Ok((w, h, px)) +} + +/// JPEG from 8-bit RGB (`rgb.len() == 3·w·h`): quality 1..=100, optional progressive scans and +/// optimised Huffman tables (Save for Web's "Optimized"), optional ICC profile, density, EXIF and +/// XMP. EXIF that doesn't fit in one segment is left out (see [`jpeg_exif_fits`]). +#[allow(clippy::too_many_arguments)] +pub fn encode_jpeg_rgb8( + width: u32, + height: u32, + rgb: &[u8], + quality: u8, + progressive: bool, + optimized: bool, + icc: Option<&[u8]>, + dpi: Option, + exif: Option<&[u8]>, + xmp: Option<&str>, +) -> Result, CodecError> { + let e = |e: jpeg_encoder::EncodingError| CodecError::encode(Format::Jpeg, e); + let (Ok(w16), Ok(h16)) = (u16::try_from(width), u16::try_from(height)) else { + return Err(CodecError::encode(Format::Jpeg, "JPEG dimensions are limited to 65535")); + }; + if rgb.len() != width as usize * height as usize * 3 { + return Err(CodecError::InvalidImage("RGB data does not match the size".into())); + } + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, quality.clamp(1, 100)); + // Like Photoshop's Save for Web: 4:2:0 chroma up to quality 50, 4:4:4 from 51 (measured on + // Photoshop 2025's output; its Save As keeps 4:2:0 at every quality). + let subsampled = quality <= 50; + enc.set_progressive(progressive); + // jpeg-encoder 0.6 writes corrupt baseline scans when optimised Huffman tables meet 4:2:0 + // (both zune-jpeg and image-rs decode them green); progressive output is fine. + enc.set_optimized_huffman_tables(progressive || (optimized && !subsampled)); + enc.set_sampling_factor(if subsampled { jpeg_encoder::SamplingFactor::R_4_2_0 } else { jpeg_encoder::SamplingFactor::R_4_4_4 }); + if let Some(d) = dpi.filter(|d| *d >= 1.0) { + let v = d.round().min(65535.0) as u16; + enc.set_density(jpeg_encoder::Density::Inch { x: v, y: v }); + } + let ppi = dpi.filter(|d| *d >= 1.0).map(|d| (d, d)); + // Upright, without the source's thumbnail, at the JFIF resolution, as Save As writes it. + if let Some(seg) = exif.and_then(|x| crate::codecs::jpeg::exif_segment(x, ppi)) { + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(x) = xmp { + // Its `tiff:` resolution, if any, says what the JFIF density says (#1691). + let x = crate::resolution::export_xmp(x, ppi); + let mut seg = b"http://ns.adobe.com/xap/1.0/\0".to_vec(); + seg.extend_from_slice(x.as_bytes()); + enc.add_app_segment(1, &seg).map_err(e)?; + } + if let Some(icc) = icc { + enc.add_icc_profile(icc).map_err(e)?; + } + enc.encode(rgb, w16, h16, jpeg_encoder::ColorType::Rgb).map_err(e)?; + Ok(out) +} + +/// Whether [`encode_jpeg_rgb8`] can write `exif` at `dpi` (it must fit in one APP1 segment). +pub fn jpeg_exif_fits(exif: &[u8], dpi: Option) -> bool { + crate::codecs::jpeg::exif_segment(exif, dpi.filter(|d| *d >= 1.0).map(|d| (d, d))).is_some() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn decode_rgba(bytes: &[u8]) -> (u32, u32, Vec) { + let img = crate::decode(bytes).unwrap(); + let (w, h) = img.dimensions(); + (w, h, img.convert(crate::ChannelLayout::Rgba, crate::SampleType::U8).data().to_vec()) + } + + fn pattern(w: usize, h: usize, n: usize) -> Vec { + (0..w * h).map(|i| (((i % w) * 7 + (i / w) * 13 + (i * i) % 5) % n) as u8).collect() + } + + /// The luma sampling factors (h, v) of a baseline or progressive JPEG. + fn luma_sampling(bytes: &[u8]) -> Option<(u8, u8)> { + let i = bytes.windows(2).position(|w| w[0] == 0xFF && matches!(w[1], 0xC0..=0xC2))?; + let f = *bytes.get(i + 11)?; + Some((f >> 4, f & 15)) + } + + #[test] + fn jpeg_chroma_is_full_above_quality_50() { + let rgb: Vec = (0..16 * 16 * 3).map(|i| (i * 37 % 256) as u8).collect(); + for (q, want) in [(1, (2, 2)), (50, (2, 2)), (51, (1, 1)), (60, (1, 1)), (89, (1, 1)), (100, (1, 1))] { + for progressive in [false, true] { + let bytes = encode_jpeg_rgb8(16, 16, &rgb, q, progressive, false, None, None, None, None).unwrap(); + assert_eq!(luma_sampling(&bytes), Some(want), "quality {q} progressive {progressive}"); + } + } + } + + #[test] + fn gif_roundtrips_through_the_decoder() { + let pal: Vec<[u8; 3]> = (0..200).map(|i| [i as u8, (255 - i) as u8, (i * 3 % 256) as u8]).collect(); + for (w, h, interlaced) in [(1, 1, false), (37, 23, false), (37, 23, true), (300, 200, false)] { + let idx = pattern(w, h, pal.len()); + let bytes = encode_gif_indexed(w as u32, h as u32, &idx, &pal, None, interlaced).unwrap(); + let (dw, dh, px) = decode_rgba(&bytes); + assert_eq!((dw, dh), (w as u32, h as u32)); + for (i, k) in idx.iter().enumerate() { + assert_eq!(&px[i * 4..i * 4 + 3], &pal[*k as usize], "pixel {i} of {w}x{h} interlaced={interlaced}"); + } + } + } + + #[test] + fn gif_small_palettes_and_transparency() { + let pal = [[255, 255, 255], [0, 0, 0], [255, 0, 0]]; + let idx: Vec = (0..64 * 64).map(|i| (i % 3) as u8).collect(); + let bytes = encode_gif_indexed(64, 64, &idx, &pal, Some(2), false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + assert_eq!(px[3], 255); + assert_eq!(px[2 * 4 + 3], 0, "index 2 is transparent"); + // Two-colour image: minimum code size 2. + let bw: Vec = (0..1000).map(|i| u8::from(i % 7 == 0)).collect(); + let bytes = encode_gif_indexed(100, 10, &bw, &pal[..2], None, false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + assert_eq!(px[0], 0, "index 1 = black at x 0"); + assert!(encode_gif_indexed(2, 2, &[0, 1, 2, 3], &pal, None, false).is_err()); + } + + #[test] + fn gif_long_runs_hit_the_dictionary_limit() { + // Noise forces many codes and dictionary resets; flat areas make long strings. + let mut seed = 12345u32; + let idx: Vec = (0..512 * 512) + .map(|i| { + seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12345); + if (i / 512) % 64 < 32 { (seed >> 16) as u8 } else { 7 } + }) + .collect(); + let pal: Vec<[u8; 3]> = (0..256).map(|i| [i as u8, i as u8, 255 - i as u8]).collect(); + let bytes = encode_gif_indexed(512, 512, &idx, &pal, None, false).unwrap(); + let (_, _, px) = decode_rgba(&bytes); + for (i, k) in idx.iter().enumerate().step_by(97) { + assert_eq!(px[i * 4], *k); + } + } + + #[test] + fn wbmp_roundtrips() { + let px: Vec = (0..13 * 5).map(|i| i % 3 == 0).collect(); + let bytes = encode_wbmp(13, 5, &px).unwrap(); + assert_eq!(&bytes[..4], &[0, 0, 13, 5]); + assert_eq!(decode_wbmp(&bytes).unwrap(), (13, 5, px)); + let big = encode_wbmp(300, 1, &[true; 300]).unwrap(); + assert_eq!(&big[2..4], &[0x82, 0x2c], "300 as a multi-byte integer"); + assert!(decode_wbmp(&big[..10]).is_err()); + assert!(decode_wbmp(&[1, 0, 1, 1, 0]).is_err()); + } + + #[test] + fn progressive_jpeg_decodes() { + let rgb: Vec = (0..64 * 48 * 3).map(|i| (i % 251) as u8).collect(); + let base = encode_jpeg_rgb8(64, 48, &rgb, 80, false, false, None, Some(72.0), None, Some("")).unwrap(); + let prog = encode_jpeg_rgb8(64, 48, &rgb, 80, true, true, None, None, None, None).unwrap(); + assert_ne!(base, prog); + assert_eq!(crate::decode(&base).unwrap().meta.xmp.as_deref(), Some("")); + // SOF2 marks a progressive frame. + assert!(prog.windows(2).any(|w| w == [0xFF, 0xC2])); + assert_eq!(crate::decode(&prog).unwrap().dimensions(), (64, 48)); + } + + /// #1691: the XMP resolution of a Save for Web JPEG says what its JFIF density says. + #[test] + fn web_jpeg_xmp_resolution_follows_the_density() { + let rgb = vec![100u8; 8 * 8 * 3]; + let xmp = r#""#; + let out = encode_jpeg_rgb8(8, 8, &rgb, 80, false, false, None, Some(72.0), None, Some(xmp)).unwrap(); + let img = crate::decode(&out).unwrap(); + assert_eq!(img.meta.dpi, Some((72.0, 72.0))); + assert_eq!(img.meta.xmp.as_deref().and_then(crate::xmp_resolution), Some((72.0, 72.0))); + } +} + +#[cfg(test)] +mod subsampling_tests { + use super::*; + + /// jpeg-encoder 0.6 writes broken scans for baseline 4:2:0 with optimised Huffman tables; + /// `encode_jpeg_rgb8` must avoid that combination. + #[test] + fn every_option_combination_decodes_to_the_source() { + let (w, h) = (64u32, 48u32); + let rgb: Vec = (0..w * h).flat_map(|i| [((i % w) * 4) as u8, 76, ((i / w) * 5) as u8]).collect(); + for q in [30, 60, 95] { + for (prog, opt) in [(false, false), (false, true), (true, false), (true, true)] { + let bytes = encode_jpeg_rgb8(w, h, &rgb, q, prog, opt, None, None, None, None).unwrap(); + let ours = crate::decode(&bytes).unwrap().convert(crate::ChannelLayout::Rgb, crate::SampleType::U8); + let err = ours.data().iter().zip(&rgb).map(|(a, b)| i32::from(*a).abs_diff(i32::from(*b))).max().unwrap(); + assert!(err < 24, "q{q} progressive={prog} optimized={opt}: max error {err}"); + } + } + } +} + +#[cfg(test)] +mod anim_tests { + use super::*; + + #[test] + fn animated_gif_has_header_and_frames() { + let frames: Vec = (0..3) + .map(|i| GifFrame { indices: vec![i as u8; 4], palette: vec![[0, 0, 0], [255, 0, 0], [0, 255, 0]], transparent: None, delay_cs: 4 }) + .collect(); + let g = encode_gif_animated(2, 2, &frames, true).unwrap(); + assert_eq!(&g[..6], b"GIF89a"); + assert_eq!(*g.last().unwrap(), 0x3B); + // Three image descriptors (0x2C) for three frames. + assert_eq!(g.iter().filter(|&&b| b == 0x2C).count(), 3); + // NETSCAPE loop extension present. + assert!(g.windows(11).any(|w| w == b"NETSCAPE2.0")); + // First frame still decodes. + let img = crate::decode(&g).unwrap(); + assert_eq!(img.dimensions(), (2, 2)); + } + + #[test] + fn transparent_frames_are_cleared_before_the_next() { + let frame = |transparent| GifFrame { indices: vec![0, 1, 1, 1], palette: vec![[0, 0, 0], [255, 0, 0]], transparent, delay_cs: 4 }; + // The Graphic Control Extension's packed byte follows its 0x21 0xF9 0x04 introducer. + let packed = |g: &[u8]| g.windows(4).find(|w| w[..3] == [0x21, 0xF9, 0x04]).map(|w| w[3]).unwrap(); + let opaque = encode_gif_animated(2, 2, &[frame(None)], false).unwrap(); + assert_eq!(packed(&opaque), 0x04, "disposal 1, no transparency"); + let clear = encode_gif_animated(2, 2, &[frame(Some(0))], false).unwrap(); + assert_eq!(packed(&clear), 0x09, "disposal 2 with the transparency flag"); + let px = crate::decode(&clear).unwrap().to_rgba8(); + assert_eq!((px[3], &px[4..8]), (0, &[255, 0, 0, 255][..]), "index 0 transparent, index 1 red"); + } + + #[test] + fn rejects_bad_frames() { + assert!(encode_gif_animated(2, 2, &[], false).is_err()); + let bad = GifFrame { indices: vec![0; 3], palette: vec![[0, 0, 0]], transparent: None, delay_cs: 1 }; + assert!(encode_gif_animated(2, 2, &[bad], false).is_err()); + } +} diff --git a/crates/image-codecs/tests/it/camera_raw.rs b/crates/image-codecs/tests/it/camera_raw.rs new file mode 100644 index 000000000..a650f3506 --- /dev/null +++ b/crates/image-codecs/tests/it/camera_raw.rs @@ -0,0 +1,134 @@ +//! Camera raw files are TIFF-structured (TIFF/EP, DNG, CR2, NEF, ARW…) but +//! hold sensor data this crate does not develop. Opening one must say so, +//! not fail with a TIFF decoder detail or silently return the embedded +//! preview. The files here are tiny synthetic TIFFs with the structural +//! markers from the public TIFF/EP and DNG specifications. + +use craft_image_codecs::*; + +const SHORT: u16 = 3; +const LONG: u16 = 4; +const BYTE4: u16 = 1; // BYTE, written with count 4 (the value's 4 bytes inline) +const CFA: u32 = 32803; // TIFF/EP PhotometricInterpretation: colour filter array +const LINEAR_RAW: u32 = 34892; // DNG PhotometricInterpretation: LinearRaw + +/// A little-endian TIFF with the given IFDs (each a list of +/// `(tag, type, value)` entries with count 1), chained in order. Every IFD +/// gets 2×2 8-bit strip data. `header8` replaces bytes 8..12 (CR2 puts +/// "CR" + version there and starts IFD0 at 16). +fn tiff(ifds: &[Vec<(u16, u16, u32)>], header8: Option<[u8; 4]>) -> Vec { + let mut b = b"II*\0".to_vec(); + let first = if header8.is_some() { 16u32 } else { 8 }; + b.extend_from_slice(&first.to_le_bytes()); + if let Some(h) = header8 { + b.extend_from_slice(&h); + b.extend_from_slice(&0u32.to_le_bytes()); + } + // Layout: IFDs back to back, then one shared 12-byte pixel strip. + let sizes: Vec = ifds.iter().map(|e| 2 + 12 * (e.len() as u32 + 1) + 4).collect(); + let strip = first + sizes.iter().sum::(); + let mut at = first; + for (i, entries) in ifds.iter().enumerate() { + let mut entries = entries.clone(); + entries.push((273, LONG, strip)); // StripOffsets + entries.sort_by_key(|e| e.0); + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, v) in entries { + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + let count: u32 = if ty == BYTE4 { 4 } else { 1 }; + b.extend_from_slice(&count.to_le_bytes()); + if ty == SHORT { + b.extend_from_slice(&(v as u16).to_le_bytes()); + b.extend_from_slice(&[0, 0]); + } else { + b.extend_from_slice(&v.to_le_bytes()); + } + } + at += sizes[i]; + let next = if i + 1 < ifds.len() { at } else { 0 }; + b.extend_from_slice(&next.to_le_bytes()); + } + b.extend_from_slice(&[128; 12]); + b +} + +/// A 2×2 image IFD with the given photometric interpretation. +fn image_ifd(photometric: u32, samples: u32) -> Vec<(u16, u16, u32)> { + vec![ + (256, SHORT, 2), // ImageWidth + (257, SHORT, 2), // ImageLength + (258, SHORT, 8), // BitsPerSample + (259, SHORT, 1), // Compression: none + (262, SHORT, photometric), // PhotometricInterpretation + (277, SHORT, samples), // SamplesPerPixel + (278, SHORT, 2), // RowsPerStrip + (279, LONG, 4 * samples), // StripByteCounts + ] +} + +fn assert_camera_raw(bytes: &[u8]) { + assert_eq!(detect(bytes), Some(Format::Tiff)); + match decode(bytes) { + Err(CodecError::Unsupported { reason, .. }) => { + assert!(reason.contains("camera raw"), "unexpected reason: {reason}"); + } + Err(e) => panic!("expected a camera raw error, got: {e}"), + Ok(img) => panic!("expected a camera raw error, decoded {:?}", img.dimensions()), + } +} + +#[test] +fn cfa_image_is_reported_as_camera_raw() { + // TIFF/EP-style raw (NEF/ARW-like): the main image is a CFA. + assert_camera_raw(&tiff(&[image_ifd(CFA, 1)], None)); +} + +#[test] +fn linear_raw_image_is_reported_as_camera_raw() { + assert_camera_raw(&tiff(&[image_ifd(LINEAR_RAW, 3)], None)); +} + +#[test] +fn raw_behind_a_preview_is_reported_as_camera_raw() { + // IFD0 is a small RGB preview, the CFA raw is a SubIFD (tag 330): decoding + // must not quietly return the preview as the photo. + let mut ifd0 = image_ifd(2, 3); + let ifd0_size = 2 + 12 * (ifd0.len() as u32 + 2) + 4; + ifd0.push((330, LONG, 8 + ifd0_size)); // SubIFDs → the next IFD + let mut b = tiff(&[ifd0, image_ifd(CFA, 1)], None); + // Unchain the raw IFD from IFD0's next-IFD pointer so it is reachable only + // as a SubIFD. + let next_at = 8 + ifd0_size as usize - 4; + b[next_at..next_at + 4].copy_from_slice(&0u32.to_le_bytes()); + assert_camera_raw(&b); +} + +#[test] +fn dng_is_reported_as_camera_raw() { + let mut ifd0 = image_ifd(2, 3); + ifd0.push((50706, BYTE4, 0x0000_0401)); // DNGVersion 1.4.0.0 + assert_camera_raw(&tiff(&[ifd0], None)); +} + +#[test] +fn cr2_is_reported_as_camera_raw() { + // CR2: TIFF header + "CR", major 2, minor 0 at byte 8; IFD0 at 16. + assert_camera_raw(&tiff(&[image_ifd(6, 3)], Some(*b"CR\x02\0"))); +} + +#[test] +fn ordinary_tiffs_still_decode() { + let rgb = decode(&tiff(&[image_ifd(2, 3)], None)).unwrap(); + assert_eq!(rgb.dimensions(), (2, 2)); + let gray = decode(&tiff(&[image_ifd(1, 1)], None)).unwrap(); + assert_eq!(gray.dimensions(), (2, 2)); +} + +#[test] +fn truncated_raw_markers_do_not_panic() { + let b = tiff(&[image_ifd(CFA, 1)], Some(*b"CR\x02\0")); + for n in 0..b.len() { + let _ = decode(&b[..n]); + } +} diff --git a/crates/image-codecs/tests/it/common/mod.rs b/crates/image-codecs/tests/it/common/mod.rs new file mode 100644 index 000000000..6b03752f3 --- /dev/null +++ b/crates/image-codecs/tests/it/common/mod.rs @@ -0,0 +1,122 @@ +//! Shared helpers for integration tests: deterministic PRNG, synthetic +//! image generators, comparison metrics. +#![allow(dead_code)] + +pub mod tiffgen; + +use craft_image_codecs::*; + +/// SplitMix64 — tiny deterministic PRNG. +pub struct Rng(u64); + +impl Rng { + pub fn new(seed: u64) -> Self { + Rng(seed) + } + pub fn next_u64(&mut self) -> u64 { + self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = self.0; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + /// Uniform in [0, 1). + pub fn f32(&mut self) -> f32 { + (self.next_u64() >> 40) as f32 / (1u64 << 24) as f32 + } + pub fn bytes(&mut self, n: usize) -> Vec { + (0..n).map(|_| self.next_u64() as u8).collect() + } +} + +/// Gradient + noise image with values in [0, 1]. `noise` is the amplitude +/// of uniform noise added to the gradient. Alpha is a separate gradient +/// that includes fully transparent and fully opaque regions. +pub fn synth(w: u32, h: u32, layout: ChannelLayout, sample: SampleType, seed: u64, noise: f32) -> Image { + let mut rng = Rng::new(seed); + let nc = layout.channels(); + let mut v = Vec::with_capacity((w * h) as usize * nc); + for y in 0..h { + for x in 0..w { + let fx = x as f32 / (w.max(2) - 1) as f32; + let fy = y as f32 / (h.max(2) - 1) as f32; + for c in 0..nc { + let is_alpha = layout.has_alpha() && c == nc - 1; + let base = if is_alpha { + ((fx + fy) * 0.75 - 0.25).clamp(0.0, 1.0) + } else { + match c % 4 { + 0 => fx, + 1 => fy, + 2 => 1.0 - fx * 0.5 - fy * 0.5, + _ => (fx * fy).sqrt(), + } + }; + let n = (rng.f32() - 0.5) * noise; + v.push((base + n).clamp(0.0, 1.0)); + } + } + } + Image::from_normalized(w, h, layout, sample, &v).unwrap() +} + +/// Default test image: 37x23 with moderate noise. +pub fn test_image(layout: ChannelLayout, sample: SampleType) -> Image { + synth(37, 23, layout, sample, 0xC0DEC, 0.2) +} + +/// PSNR in dB over normalized samples (clamped to [0,1]). +pub fn psnr(a: &Image, b: &Image) -> f64 { + assert_eq!(a.dimensions(), b.dimensions()); + assert_eq!(a.layout(), b.layout()); + let (av, bv) = (a.to_normalized(), b.to_normalized()); + let mse: f64 = av + .iter() + .zip(&bv) + .map(|(x, y)| { + let d = (x.clamp(0.0, 1.0) - y.clamp(0.0, 1.0)) as f64; + d * d + }) + .sum::() + / av.len() as f64; + if mse == 0.0 { f64::INFINITY } else { 10.0 * (1.0 / mse).log10() } +} + +pub fn max_abs_diff(a: &Image, b: &Image) -> f32 { + a.to_normalized().iter().zip(b.to_normalized()).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max) +} + +/// Formats that can be written in this build. +pub fn writable_formats() -> Vec { + Format::ALL.into_iter().filter(|f| caps(*f).write).collect() +} + +/// Formats that can be written *and* read in this build. +pub fn rw_formats() -> Vec { + Format::ALL.into_iter().filter(|f| caps(*f).write && caps(*f).read).collect() +} + +/// A minimal valid little-endian TIFF-structured EXIF blob (one IFD with +/// an Orientation entry). +pub fn sample_exif() -> Vec { + let mut v = b"II*\0\x08\0\0\0".to_vec(); + v.extend_from_slice(&1u16.to_le_bytes()); // 1 entry + v.extend_from_slice(&0x0112u16.to_le_bytes()); // Orientation + v.extend_from_slice(&3u16.to_le_bytes()); // SHORT + v.extend_from_slice(&1u32.to_le_bytes()); + v.extend_from_slice(&1u32.to_le_bytes()); + v.extend_from_slice(&0u32.to_le_bytes()); // next IFD + v +} + +/// Fake ICC profile of `len` bytes (header-ish prefix + deterministic noise). +pub fn sample_icc(len: usize) -> Vec { + let mut v = Rng::new(len as u64).bytes(len); + v[..4].copy_from_slice(&(len as u32).to_be_bytes()); + if len >= 40 { + v[36..40].copy_from_slice(b"acsp"); + } + v +} + +pub const SAMPLE_XMP: &str = ""; diff --git a/crates/image-codecs/tests/it/common/tiffgen.rs b/crates/image-codecs/tests/it/common/tiffgen.rs new file mode 100644 index 000000000..84f0e36d2 --- /dev/null +++ b/crates/image-codecs/tests/it/common/tiffgen.rs @@ -0,0 +1,327 @@ +//! A tiny hand-rolled TIFF / BigTIFF writer for tests: any byte order, strips or tiles, any +//! planar configuration, several IFDs in a chain plus free-standing ones (SubIFDs), and access +//! to every offset so tests can corrupt the structure precisely. +#![allow(dead_code, clippy::too_many_arguments)] + +/// A tag value: a number or a reference to another directory's offset (for SubIFDs). +#[derive(Clone, Copy, Debug)] +pub enum V { + N(u64), + Ifd(usize), +} + +/// TIFF field types used by the tests. +pub const BYTE: u16 = 1; +pub const ASCII: u16 = 2; +pub const SHORT: u16 = 3; +pub const LONG: u16 = 4; +pub const RATIONAL: u16 = 5; +pub const UNDEFINED: u16 = 7; +pub const IFD: u16 = 13; +pub const LONG8: u16 = 16; +pub const IFD8: u16 = 18; + +fn type_size(ty: u16) -> usize { + match ty { + BYTE | ASCII | UNDEFINED | 6 => 1, + SHORT | 8 => 2, + LONG | IFD | 9 | 11 => 4, + RATIONAL | 10 | 12 | LONG8 | 17 | IFD8 => 8, + _ => 1, + } +} + +#[derive(Clone, Debug, Default)] +pub struct Dir { + /// `(tag, type, values)`; strip / tile offsets and byte counts are added from `chunks`. + pub entries: Vec<(u16, u16, Vec)>, + /// Pixel data, one blob per strip or tile. + pub chunks: Vec>, + /// Tiles (322/323 must be in `entries`) instead of strips. + pub tiled: bool, +} + +impl Dir { + pub fn tag(mut self, tag: u16, ty: u16, values: &[u64]) -> Self { + self.entries.push((tag, ty, values.iter().map(|&v| V::N(v)).collect())); + self + } + pub fn bytes(mut self, tag: u16, ty: u16, data: &[u8]) -> Self { + self.entries.push((tag, ty, data.iter().map(|&v| V::N(u64::from(v))).collect())); + self + } + pub fn sub_ifds(mut self, ty: u16, dirs: &[usize]) -> Self { + self.entries.push((330, ty, dirs.iter().map(|&d| V::Ifd(d)).collect())); + self + } +} + +/// A basic directory: `w`×`h`, `spp` samples of `bits` bits, the given photometric, chunky, +/// uncompressed, one strip per `rows` rows holding `data` (already in file byte order). +pub fn strips(w: u32, h: u32, bits: u16, spp: u16, photometric: u16, rows: u32, data: &[u8]) -> Dir { + let row = (w as usize * bits as usize * spp as usize).div_ceil(8); + let chunks = data.chunks((row * rows as usize).max(1)).map(<[u8]>::to_vec).collect(); + Dir { entries: base(w, h, bits, spp, photometric), chunks, tiled: false }.tag(278, LONG, &[u64::from(rows)]) +} + +pub fn base(w: u32, h: u32, bits: u16, spp: u16, photometric: u16) -> Vec<(u16, u16, Vec)> { + vec![ + (256, LONG, vec![V::N(u64::from(w))]), + (257, LONG, vec![V::N(u64::from(h))]), + (258, SHORT, vec![V::N(u64::from(bits)); spp as usize]), + (259, SHORT, vec![V::N(1)]), + (262, SHORT, vec![V::N(u64::from(photometric))]), + (277, SHORT, vec![V::N(u64::from(spp))]), + ] +} + +/// Where things ended up in a built file. +#[derive(Debug, Default)] +pub struct Built { + pub bytes: Vec, + /// Offset of each directory (`dirs` order). + pub ifd_at: Vec, + /// Offset of each directory's next-IFD pointer field. + pub next_at: Vec, + /// Offset of each chunk's data, per directory. + pub chunk_at: Vec>, +} + +/// Builds a file. `chain` lists the directories of the main IFD chain in order; directories not +/// in it are only reachable through SubIFD references. +pub fn build(little: bool, big: bool, dirs: &[Dir], chain: &[usize]) -> Built { + let e16 = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let e32 = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let e64 = |v: u64| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let enc = |ty: u16, v: u64| -> Vec { + match type_size(ty) { + 1 => vec![v as u8], + 2 => e16(v as u16), + 4 => e32(v as u32), + _ => e64(v), + } + }; + let header = if big { 16 } else { 8 }; + let entry_len = if big { 20 } else { 12 }; + let inline = if big { 8 } else { 4 }; + let off_ty = if big { LONG8 } else { LONG }; + + // Complete entry lists (with chunk offsets / byte counts), sorted by tag. + let mut full: Vec)>> = Vec::new(); + for d in dirs { + let mut e = d.entries.clone(); + let (ot, ct) = if d.tiled { (324, 325) } else { (273, 279) }; + if !d.chunks.is_empty() || !e.iter().any(|x| x.0 == ot) { + e.retain(|x| x.0 != ot && x.0 != ct); + e.push((ot, off_ty, vec![V::N(0); d.chunks.len()])); + e.push((ct, off_ty, d.chunks.iter().map(|c| V::N(c.len() as u64)).collect())); + } + e.sort_by_key(|x| x.0); + full.push(e); + } + + // Pass 1: positions. Layout: header, then per directory its chunks, its out-of-line values + // and its table. + let mut pos = header as u64; + let mut ifd_at = Vec::new(); + let mut chunk_at = Vec::new(); + for (d, e) in dirs.iter().zip(&full) { + let mut ca = Vec::new(); + for c in &d.chunks { + ca.push(pos); + pos += c.len() as u64; + } + chunk_at.push(ca); + for (_, ty, vals) in e { + let n = type_size(*ty) * vals.len(); + if n > inline { + pos += n as u64; + } + } + pos += pos % 2; // word alignment + ifd_at.push(pos); + pos += (if big { 8 } else { 2 }) + (e.len() * entry_len) as u64 + if big { 8 } else { 4 }; + } + + // Pass 2: bytes. + let mut f = Vec::new(); + f.extend_from_slice(if little { b"II" } else { b"MM" }); + let first = chain.first().map_or(0, |&i| ifd_at[i]); + if big { + f.extend(e16(43)); + f.extend(e16(8)); + f.extend(e16(0)); + f.extend(e64(first)); + } else { + f.extend(e16(42)); + f.extend(e32(first as u32)); + } + let mut next_at = vec![0; dirs.len()]; + for (k, (d, e)) in dirs.iter().zip(&full).enumerate() { + for c in &d.chunks { + f.extend_from_slice(c); + } + let (ot, _) = if d.tiled { (324, 325) } else { (273, 279) }; + let resolve = |tag: u16, i: usize, v: V| -> u64 { + if tag == ot && !d.chunks.is_empty() { + return chunk_at[k][i]; + } + match v { + V::N(n) => n, + V::Ifd(j) => ifd_at[j], + } + }; + let mut table = Vec::new(); + table.extend(if big { e64(e.len() as u64) } else { e16(e.len() as u16) }); + for (tag, ty, vals) in e { + let mut data = Vec::new(); + for (i, v) in vals.iter().enumerate() { + data.extend(enc(*ty, resolve(*tag, i, *v))); + } + table.extend(e16(*tag)); + table.extend(e16(*ty)); + table.extend(if big { e64(vals.len() as u64) } else { e32(vals.len() as u32) }); + if data.len() <= inline { + data.resize(inline, 0); + table.extend(data); + } else { + let at = f.len() as u64; + f.extend(data); + table.extend(if big { e64(at) } else { e32(at as u32) }); + } + } + if f.len() % 2 == 1 { + f.push(0); + } + assert_eq!(f.len() as u64, ifd_at[k]); + let next = chain.iter().position(|&c| c == k).and_then(|p| chain.get(p + 1)).map_or(0, |&n| ifd_at[n]); + f.extend(table); + next_at[k] = f.len() as u64; + f.extend(if big { e64(next) } else { e32(next as u32) }); + } + Built { bytes: f, ifd_at, next_at, chunk_at } +} + +/// Overwrites the next-IFD pointer of directory `k` (for cycles and dangling pointers). +pub fn set_next(b: &mut Built, little: bool, big: bool, k: usize, to: u64) { + let at = b.next_at[k] as usize; + let v = if big { + if little { to.to_le_bytes().to_vec() } else { to.to_be_bytes().to_vec() } + } else if little { + (to as u32).to_le_bytes().to_vec() + } else { + (to as u32).to_be_bytes().to_vec() + }; + b.bytes[at..at + v.len()].copy_from_slice(&v); +} + +/// Locates entry `tag` of directory `k`: `(entry position, type, count, value position)`. +pub fn find_entry(b: &Built, little: bool, big: bool, k: usize, tag: u16) -> Option<(usize, u16, u64, usize)> { + let f = &b.bytes; + let rd = |at: usize, n: usize| -> u64 { + let s = &f[at..at + n]; + let mut v = 0u64; + for i in 0..n { + let byte = if little { s[n - 1 - i] } else { s[i] }; + v = (v << 8) | u64::from(byte); + } + v + }; + let at = b.ifd_at[k] as usize; + let (n, first, entry, inline) = if big { (rd(at, 8), at + 8, 20, 8) } else { (rd(at, 2), at + 2, 12, 4) }; + for i in 0..n as usize { + let e = first + i * entry; + if rd(e, 2) as u16 != tag { + continue; + } + let ty = rd(e + 2, 2) as u16; + let count = if big { rd(e + 4, 8) } else { rd(e + 4, 4) }; + let field = e + if big { 12 } else { 8 }; + let value_at = if count as usize * type_size(ty) <= inline { field } else { rd(field, inline) as usize }; + return Some((e, ty, count, value_at)); + } + None +} + +/// Overwrites value `i` of entry `tag` in directory `k` (in its own type and width). +pub fn patch_value(b: &mut Built, little: bool, big: bool, k: usize, tag: u16, i: usize, value: u64) { + let (_, ty, _, at) = find_entry(b, little, big, k, tag).expect("tag present"); + let n = type_size(ty); + let bytes = if little { value.to_le_bytes() } else { value.to_be_bytes() }; + let v = if little { bytes[..n].to_vec() } else { bytes[8 - n..].to_vec() }; + let at = at + i * n; + b.bytes[at..at + n].copy_from_slice(&v); +} + +/// Overwrites the count field of entry `tag` in directory `k`. +pub fn patch_count(b: &mut Built, little: bool, big: bool, k: usize, tag: u16, count: u64) { + let (e, ..) = find_entry(b, little, big, k, tag).expect("tag present"); + let v = if big { + if little { count.to_le_bytes().to_vec() } else { count.to_be_bytes().to_vec() } + } else if little { + (count as u32).to_le_bytes().to_vec() + } else { + (count as u32).to_be_bytes().to_vec() + }; + b.bytes[e + 4..e + 4 + v.len()].copy_from_slice(&v); +} + +/// Samples to file bytes in the given order. +pub fn u16s(v: &[u16], little: bool) -> Vec { + v.iter().flat_map(|x| if little { x.to_le_bytes() } else { x.to_be_bytes() }).collect() +} +pub fn f32s(v: &[f32], little: bool) -> Vec { + v.iter().flat_map(|x| if little { x.to_le_bytes() } else { x.to_be_bytes() }).collect() +} + +/// Splits an interleaved `w`×`h` image of `spp` samples (`bps` bytes each) into tiles of +/// `tw`×`th`, padded with zeros; chunky (one blob per tile) or planar (all tiles of plane 0, +/// then plane 1, …). +pub fn tile_split(px: &[u8], w: usize, h: usize, spp: usize, bps: usize, tw: usize, th: usize, planar: bool) -> Vec> { + let across = w.div_ceil(tw); + let down = h.div_ceil(th); + let planes = if planar { spp } else { 1 }; + let per = if planar { 1 } else { spp }; + let mut out = Vec::new(); + for p in 0..planes { + for ty in 0..down { + for tx in 0..across { + let mut t = vec![0u8; tw * th * per * bps]; + for y in 0..th { + for x in 0..tw { + let (ix, iy) = (tx * tw + x, ty * th + y); + if ix >= w || iy >= h { + continue; + } + for s in 0..per { + let src = ((iy * w + ix) * spp + if planar { p } else { s }) * bps; + let dst = ((y * tw + x) * per + s) * bps; + t[dst..dst + bps].copy_from_slice(&px[src..src + bps]); + } + } + } + out.push(t); + } + } + } + out +} + +/// Splits an interleaved image into planar strips of `rows` rows: plane 0's strips, then +/// plane 1's, …. +pub fn planar_strips(px: &[u8], w: usize, h: usize, spp: usize, bps: usize, rows: usize) -> Vec> { + let mut out = Vec::new(); + for p in 0..spp { + for y0 in (0..h).step_by(rows) { + let mut s = Vec::new(); + for y in y0..(y0 + rows).min(h) { + for x in 0..w { + let src = ((y * w + x) * spp + p) * bps; + s.extend_from_slice(&px[src..src + bps]); + } + } + out.push(s); + } + } + out +} diff --git a/crates/image-codecs/tests/it/dds.rs b/crates/image-codecs/tests/it/dds.rs new file mode 100644 index 000000000..4f766a54c --- /dev/null +++ b/crates/image-codecs/tests/it/dds.rs @@ -0,0 +1,410 @@ +use craft_image_codecs::{ + ChannelLayout, CodecError, DecodeOptions, Format, Limits, SampleType, caps, decode, decode_as, decode_as_with, detect, encode, from_extension, +}; + +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; +const DDSD_PITCH: u32 = 0x8; + +/// A DDS_HEADER for a single-level 2D texture, after the `DDS ` signature. +fn header(width: u32, height: u32, pf_flags: u32, fourcc: &[u8; 4], bits: u32, masks: [u32; 4]) -> Vec { + let mut b = vec![0u8; 128]; + b[0..4].copy_from_slice(b"DDS "); + let mut put = |at: usize, v: u32| b[at..at + 4].copy_from_slice(&v.to_le_bytes()); + put(4, 124); + put(8, 0x1007); // CAPS | HEIGHT | WIDTH | PIXELFORMAT + put(12, height); + put(16, width); + put(76, 32); + put(80, pf_flags); + put(88, bits); + for (i, m) in masks.into_iter().enumerate() { + put(92 + 4 * i, m); + } + put(108, 0x1000); // DDSCAPS_TEXTURE + b[84..88].copy_from_slice(fourcc); + b +} + +fn compressed(width: u32, height: u32, fourcc: &[u8; 4]) -> Vec { + header(width, height, DDPF_FOURCC, fourcc, 0, [0; 4]) +} + +fn set_pitch(b: &mut [u8], pitch: u32) { + b[8..12].copy_from_slice(&(0x1007 | DDSD_PITCH).to_le_bytes()); + b[20..24].copy_from_slice(&pitch.to_le_bytes()); +} + +/// The DX10 extension header after a `DX10` FourCC: format, dimension, misc flags, array size. +fn dx10(width: u32, height: u32, dxgi: u32, dimension: u32, misc: u32, array: u32) -> Vec { + let mut b = compressed(width, height, b"DX10"); + for v in [dxgi, dimension, misc, array, 0] { + b.extend(v.to_le_bytes()); + } + b +} + +/// BC1 colour endpoints as RGB565, then one 2-bit index per pixel, row by row. +fn bc1_block(c0: u16, c1: u16, rows: [u8; 4]) -> Vec { + let mut b = Vec::new(); + b.extend(c0.to_le_bytes()); + b.extend(c1.to_le_bytes()); + b.extend(rows); + b +} + +const RED: u16 = 0xf800; +const GREEN: u16 = 0x07e0; +const BLUE: u16 = 0x001f; +const BLACK: u16 = 0x0000; +const WHITE: u16 = 0xffff; + +/// Indices 0, 1 on the top row and 2, 3 below them, for the top-left 2x2 pixels. +const INDICES_0123: [u8; 4] = [0x04, 0x0e, 0, 0]; + +#[test] +fn dds_is_readable_and_writable() { + assert_eq!(from_extension("texture.DDS"), Some(Format::Dds)); + assert!(caps(Format::Dds).read && caps(Format::Dds).write); + let img = craft_image_codecs::Image::from_u8(1, 1, ChannelLayout::Rgba, vec![1, 2, 3, 0]).unwrap(); + let b = encode(&img, Format::Dds, &Default::default()).unwrap(); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 0]); +} + +#[test] +fn decodes_bc1_four_colour_blocks() { + // c0 > c1: four opaque colours, the two in between at thirds. + let mut b = compressed(2, 2, b"DXT1"); + b.extend(bc1_block(RED, BLUE, INDICES_0123)); + assert_eq!(detect(&b), Some(Format::Dds)); + let im = decode(&b).unwrap(); + assert_eq!((im.dimensions(), im.layout(), im.sample_type()), ((2, 2), ChannelLayout::Rgba, SampleType::U8)); + assert_eq!(im.data(), &[255, 0, 0, 255, 0, 0, 255, 255, 170, 0, 85, 255, 85, 0, 170, 255]); +} + +#[test] +fn decodes_bc1_one_bit_alpha_blocks() { + // c0 <= c1: three colours (the third halfway) and index 3 is transparent black. + let mut b = compressed(2, 2, b"DXT1"); + b.extend(bc1_block(BLUE, RED, INDICES_0123)); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 255, 255, 255, 0, 0, 255, 128, 0, 128, 255, 0, 0, 0, 0]); +} + +#[test] +fn decodes_bc2_explicit_alpha_with_four_colour_blocks() { + let mut b = compressed(2, 2, b"DXT3"); + // Four bits of alpha per pixel: 15, 0 on the top row, 8, 1 below. + b.extend([0x0f, 0x00, 0x18, 0x00, 0, 0, 0, 0]); + // c0 < c1 would mean transparency in BC1; BC2 colour blocks always have four colours. + b.extend(bc1_block(BLACK, WHITE, [0x04, 0x0b, 0, 0])); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgba); + assert_eq!(im.data(), &[0, 0, 0, 255, 255, 255, 255, 0, 170, 170, 170, 136, 85, 85, 85, 17]); +} + +#[test] +fn decodes_bc3_interpolated_alpha() { + // a0 > a1: eight alpha values, six interpolated at sevenths. Indices 0, 1 / 2, 7. + let mut b = compressed(2, 2, b"DXT5"); + b.extend([255, 0, 0x08, 0xa0, 0x03, 0, 0, 0]); + b.extend(bc1_block(BLACK, WHITE, [0x04, 0x0b, 0, 0])); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 0, 255, 255, 255, 255, 0, 170, 170, 170, 219, 85, 85, 85, 36]); + + // a0 <= a1: six values, four interpolated at fifths, then 0 and 255. Indices 2, 5 / 6, 7. + let mut b = compressed(2, 2, b"DXT5"); + let bits: u64 = 2 | 5 << 3 | 6 << 12 | 7 << 15; + b.extend([0, 255]); + b.extend(&bits.to_le_bytes()[..6]); + b.extend(bc1_block(GREEN, GREEN, [0; 4])); + assert_eq!(decode(&b).unwrap().data(), &[0, 255, 0, 51, 0, 255, 0, 204, 0, 255, 0, 0, 0, 255, 0, 255]); +} + +#[test] +fn bc_textures_need_not_be_a_multiple_of_four() { + // 5x3 is two blocks wide and one high; the right block's first column is x = 4. + let mut b = compressed(5, 3, b"DXT1"); + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + let im = decode(&b).unwrap(); + assert_eq!(im.dimensions(), (5, 3)); + let px = |x: usize, y: usize| &im.data()[(y * 5 + x) * 4..(y * 5 + x + 1) * 4]; + assert_eq!((px(3, 2), px(4, 0), px(4, 2)), (&[255, 0, 0, 255][..], &[0, 0, 255, 255][..], &[0, 0, 255, 255][..])); +} + +#[test] +fn decodes_uncompressed_rgba_and_bgra_masks() { + let rgba = [0xff, 0xff00, 0xff_0000, 0xff00_0000]; + let mut b = header(2, 1, DDPF_RGB | DDPF_ALPHAPIXELS, &[0; 4], 32, rgba); + b.extend([1, 2, 3, 4, 250, 251, 252, 253]); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgba); + assert_eq!(im.data(), &[1, 2, 3, 4, 250, 251, 252, 253]); + + let bgra = [0xff_0000, 0xff00, 0xff, 0xff00_0000]; + let mut b = header(2, 1, DDPF_RGB | DDPF_ALPHAPIXELS, &[0; 4], 32, bgra); + b.extend([3, 2, 1, 4, 252, 251, 250, 253]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 250, 251, 252, 253]); + + // An alpha mask without DDPF_ALPHAPIXELS is not alpha: X8R8G8B8 opens as RGB. + let mut b = header(1, 1, DDPF_RGB, &[0; 4], 32, bgra); + b.extend([3, 2, 1, 0]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::Rgb, &[1u8, 2, 3][..])); +} + +#[test] +fn decodes_packed_masks_and_luminance() { + // R5G6B5: each field scaled to 0..255; 3 of 31 is 25. + let mut b = header(4, 1, DDPF_RGB, &[0; 4], 16, [0xf800, 0x07e0, 0x001f, 0]); + for v in [RED, GREEN, BLUE, 3 << 11] { + b.extend(v.to_le_bytes()); + } + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgb); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0, 0, 0, 255, 25, 0, 0]); + + // 24-bit rows are packed three bytes a pixel, without padding. + let mut b = header(2, 2, DDPF_RGB, &[0; 4], 24, [0xff_0000, 0xff00, 0xff, 0]); + b.extend([3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + + let mut b = header(2, 1, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, &[0; 4], 16, [0xff, 0, 0, 0xff00]); + b.extend([10, 20, 30, 40]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::GrayA, &[10u8, 20, 30, 40][..])); +} + +#[test] +fn decodes_padded_rgb_rows() { + // DWORD-aligned 24-bit rows: the fourth byte is padding, not the next pixel. + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, 4); + b.extend([255, 0, 0, 99, 0, 255, 0, 88]); + let im = decode(&b).unwrap(); + assert_eq!((im.dimensions(), im.layout()), ((1, 2), ChannelLayout::Rgb)); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0]); + + // Padding can contain whole pixels; only the declared width is decoded. + let mut b = header(2, 2, DDPF_RGB, &[0; 4], 16, [0xf800, 0x07e0, 0x001f, 0]); + set_pitch(&mut b, 8); + for v in [RED, GREEN, WHITE, WHITE, BLUE, BLACK, WHITE, WHITE] { + b.extend(v.to_le_bytes()); + } + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0]); +} + +#[test] +fn decodes_padded_luminance_rows() { + let mut b = header(2, 2, DDPF_LUMINANCE, &[0; 4], 8, [0xff, 0, 0, 0]); + set_pitch(&mut b, 4); + b.extend([10, 20, 99, 98, 30, 40, 97, 96]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::Gray, &[10u8, 20, 30, 40][..])); + + let mut b = header(1, 2, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, &[0; 4], 16, [0xff, 0, 0, 0xff00]); + set_pitch(&mut b, 4); + b.extend([10, 20, 99, 98, 30, 40, 97, 96]); + let im = decode(&b).unwrap(); + assert_eq!((im.layout(), im.data()), (ChannelLayout::GrayA, &[10u8, 20, 30, 40][..])); +} + +#[test] +fn decodes_padded_dx10_rows() { + let mut b = dx10(1, 2, 28, 3, 0, 1); // R8G8B8A8_UNORM + set_pitch(&mut b, 8); + b.extend([1, 2, 3, 4, 99, 98, 97, 96, 5, 6, 7, 8, 95, 94, 93, 92]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4, 5, 6, 7, 8]); +} + +#[test] +fn ignores_pitch_without_the_pitch_flag() { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + b[20..24].copy_from_slice(&u32::MAX.to_le_bytes()); + b.extend([255, 0, 0, 0, 255, 0]); + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); + + set_pitch(&mut b, 3); // An explicitly tightly packed row is also valid. + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); +} + +#[test] +fn compressed_linear_size_is_not_a_row_pitch() { + let mut b = compressed(4, 8, b"DXT1"); + b[8..12].copy_from_slice(&(0x1007u32 | 0x8_0000).to_le_bytes()); // DDSD_LINEARSIZE + b[20..24].copy_from_slice(&16u32.to_le_bytes()); // Two 8-byte block rows. + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + let im = decode(&b).unwrap(); + assert!(im.data()[..64].as_chunks::<4>().0.iter().all(|p| p == &[255, 0, 0, 255])); + assert!(im.data()[64..].as_chunks::<4>().0.iter().all(|p| p == &[0, 0, 255, 255])); +} + +#[test] +fn rejects_pitch_smaller_than_a_pixel_row() { + for pitch in [0, 1, 2] { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, pitch); + b.extend([255, 0, 0, 0, 255, 0]); + assert!(matches!(decode(&b), Err(CodecError::Malformed { .. })), "pitch {pitch}"); + } +} + +#[test] +fn rejects_truncated_padded_rows() { + let mut b = header(1, 2, DDPF_RGB, &[0; 4], 24, [0xff, 0xff00, 0xff_0000, 0]); + set_pitch(&mut b, 4); + b.extend([255, 0, 0, 99, 0, 255, 0, 88]); + assert!(decode(&b).is_ok()); + for n in 128..b.len() { + assert!(matches!(decode(&b[..n]), Err(CodecError::Malformed { .. })), "decoded {n} of {} bytes", b.len()); + } +} + +#[test] +fn rejects_huge_pitch_without_allocating() { + let mut b = header(1, 2, DDPF_LUMINANCE, &[0; 4], 8, [0xff, 0, 0, 0]); + set_pitch(&mut b, u32::MAX); + b.extend([10, 20]); + // On 32-bit targets pitch * height overflows; on 64-bit the payload is too short. + assert!(matches!(decode(&b), Err(CodecError::Malformed { .. }))); +} + +#[test] +fn decodes_dx10_headers() { + let mut b = dx10(1, 1, 28, 3, 0, 1); // R8G8B8A8_UNORM + b.extend([1, 2, 3, 4]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + + let mut b = dx10(1, 1, 91, 3, 0, 1); // B8G8R8A8_UNORM_SRGB + b.extend([3, 2, 1, 4]); + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + + let mut b = dx10(2, 2, 71, 3, 0, 1); // BC1_UNORM + b.extend(bc1_block(BLUE, RED, INDICES_0123)); + assert_eq!(decode(&b).unwrap().data(), &[0, 0, 255, 255, 255, 0, 0, 255, 128, 0, 128, 255, 0, 0, 0, 0]); +} + +#[test] +fn only_the_top_mip_level_is_read() { + let mut b = compressed(4, 4, b"DXT1"); + b[28..32].copy_from_slice(&3u32.to_le_bytes()); + b.extend(bc1_block(RED, RED, [0; 4])); + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + b.extend(bc1_block(GREEN, GREEN, [0; 4])); + let im = decode(&b).unwrap(); + assert_eq!(im.dimensions(), (4, 4)); + assert!(im.data().chunks(4).all(|p| p == [255, 0, 0, 255])); +} + +#[test] +fn refuses_cubemaps_arrays_and_volumes() { + let unsupported = |b: &[u8], what: &str| match decode_as(Format::Dds, b) { + Err(CodecError::Unsupported { reason, .. }) => assert!(reason.contains(what), "{reason}"), + other => panic!("{what}: {other:?}"), + }; + let mut cube = compressed(4, 4, b"DXT1"); + cube[112..116].copy_from_slice(&0xfe00u32.to_le_bytes()); // DDSCAPS2_CUBEMAP and all faces + cube.extend([0; 8 * 6]); + unsupported(&cube, "cubemap"); + let mut volume = compressed(4, 4, b"DXT1"); + volume[8..12].copy_from_slice(&(0x1007u32 | 0x80_0000).to_le_bytes()); // DDSD_DEPTH + volume[24..28].copy_from_slice(&4u32.to_le_bytes()); + volume[112..116].copy_from_slice(&0x20_0000u32.to_le_bytes()); // DDSCAPS2_VOLUME + volume.extend([0; 8 * 4]); + unsupported(&volume, "volume"); + unsupported(&dx10(4, 4, 71, 3, 0x4, 1), "cubemap"); + unsupported(&dx10(4, 4, 71, 3, 0, 6), "array"); + unsupported(&dx10(4, 4, 71, 4, 0, 1), "2D"); + unsupported(&dx10(4, 4, 98, 3, 0, 1), "98"); // BC7_UNORM + unsupported(&compressed(4, 4, b"ATI2"), "ATI2"); + unsupported(&compressed(4, 4, b"DXT2"), "premultiplied"); +} + +/// Face declarations matter even when the general cube flag is absent; padding does not. +#[test] +fn source_probe_distinguishes_extra_faces_from_single_images_and_padding() { + for caps in [0u32, 0x400, 0xc00, 0xfc00] { + let mut b = compressed(4, 4, b"DXT1"); + b[112..116].copy_from_slice(&caps.to_le_bytes()); + b.extend(bc1_block(RED, RED, [0; 4])); + for _ in 1..caps.count_ones() { + b.extend(bc1_block(BLUE, BLUE, [0; 4])); + } + assert!(decode(&b).unwrap().data().chunks(4).all(|p| p == [255, 0, 0, 255])); + assert_eq!(craft_image_codecs::has_unretained_source_data(&b).unwrap(), caps.count_ones() > 1); + } + // A FourCC-looking unused union and trailing bytes are not a DX10 declaration. + let mut single = header(1, 1, DDPF_RGB, b"DX10", 24, [0xff, 0xff00, 0xff_0000, 0]); + single.extend([1, 2, 3]); + single.extend([0xff; 64]); + assert_eq!(decode(&single).unwrap().data(), &[1, 2, 3]); + for mips in [0u32, 1] { + single[28..32].copy_from_slice(&mips.to_le_bytes()); + assert!(!craft_image_codecs::has_unretained_source_data(&single).unwrap()); + } +} + +/// The metadata probe follows the same DX10 discriminator as preview decoding. +#[test] +fn source_probe_reads_dx10_array_and_cube_declarations() { + for (array, misc) in [(0, 0), (1, 0), (2, 0), (1, 4)] { + let mut b = dx10(1, 1, 28, 3, misc, array); + b.extend([1, 2, 3, 4]); + let multiple = array > 1 || misc == 4; + assert_eq!(craft_image_codecs::has_unretained_source_data(&b).unwrap(), multiple); + if multiple { + assert!(matches!(decode(&b), Err(CodecError::Unsupported { .. }))); + } else { + assert_eq!(decode(&b).unwrap().data(), &[1, 2, 3, 4]); + } + } +} + +#[test] +fn rejects_truncated_files_at_every_length_without_panicking() { + let mut b = dx10(5, 3, 77, 3, 0, 1); // BC3_UNORM: two 16-byte blocks + b.extend([0x5a; 32]); + assert!(decode(&b).is_ok()); + for n in 0..b.len() { + assert!(decode_as(Format::Dds, &b[..n]).is_err(), "decoded {n} of {} bytes", b.len()); + } + let mut b = header(3, 2, DDPF_RGB, &[0; 4], 24, [0xff_0000, 0xff00, 0xff, 0]); + b.extend([7; 18]); + assert!(decode(&b).is_ok()); + assert!(decode_as(Format::Dds, &b[..b.len() - 1]).is_err(), "one byte short"); +} + +#[test] +fn rejects_bogus_sizes_and_pixel_formats() { + // Huge dimensions are refused before anything is allocated. + let mut huge = compressed(u32::MAX, u32::MAX, b"DXT1"); + huge.extend([0; 8]); + assert!(matches!(decode_as(Format::Dds, &huge), Err(CodecError::LimitExceeded(_)))); + let opts = DecodeOptions { limits: Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + let mut tight = header(64, 64, DDPF_RGB, &[0; 4], 32, [0xff, 0xff00, 0xff_0000, 0]); + tight.extend(vec![0; 64 * 64 * 4]); + assert!(matches!(decode_as_with(Format::Dds, &tight, &opts), Err(CodecError::LimitExceeded(_)))); + assert!(decode_as(Format::Dds, &compressed(0, 4, b"DXT1")).is_err(), "zero width"); + + let mut bad_size = compressed(4, 4, b"DXT1"); + bad_size[4..8].copy_from_slice(&100u32.to_le_bytes()); + bad_size.extend([0; 8]); + assert!(matches!(decode_as(Format::Dds, &bad_size), Err(CodecError::Malformed { .. })), "header size must be 124"); + + for (bits, masks) in [ + (12, [0xf00, 0xf0, 0xf, 0]), // not a whole number of bytes + (16, [0xff_0000, 0xff00, 0xff, 0]), // a mask wider than the pixel + (32, [0xf0f, 0xf0f0, 0xf_0000, 0]), // a mask with a gap + (32, [0, 0, 0, 0]), // no colour at all + (0, [0xff, 0xff00, 0xff_0000, 0]), // zero bits per pixel + ] { + let mut b = header(1, 1, DDPF_RGB, &[0; 4], bits, masks); + b.extend([0; 4]); + assert!(decode_as(Format::Dds, &b).is_err(), "{bits} bits, masks {masks:x?}"); + } + let mut no_format = header(1, 1, 0, &[0; 4], 32, [0xff, 0xff00, 0xff_0000, 0]); + no_format.extend([0; 4]); + assert!(matches!(decode_as(Format::Dds, &no_format), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/it/dds_write.rs b/crates/image-codecs/tests/it/dds_write.rs new file mode 100644 index 000000000..4f66d3679 --- /dev/null +++ b/crates/image-codecs/tests/it/dds_write.rs @@ -0,0 +1,375 @@ +//! Writing DDS textures: every storage read back through the reader, the header fields, sizes +//! that aren't a multiple of four, the mip chain, and the block-compression error on ramps. + +use craft_image_codecs::{ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with}; + +const DDSD_PITCH: u32 = 0x8; +const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; +const DDSD_LINEARSIZE: u32 = 0x8_0000; +const REQUIRED: u32 = 0x1007; // CAPS | HEIGHT | WIDTH | PIXELFORMAT +const DDSCAPS_COMPLEX: u32 = 0x8; +const DDSCAPS_TEXTURE: u32 = 0x1000; +const DDSCAPS_MIPMAP: u32 = 0x40_0000; +const DDPF_ALPHAPIXELS: u32 = 0x1; +const DDPF_FOURCC: u32 = 0x4; +const DDPF_RGB: u32 = 0x40; +const DDPF_LUMINANCE: u32 = 0x2_0000; + +const BLOCKS: [DdsCompression; 3] = [DdsCompression::Bc1, DdsCompression::Bc2, DdsCompression::Bc3]; + +fn opts(compression: DdsCompression, mipmaps: bool) -> EncodeOptions { + EncodeOptions { dds: DdsOptions { compression, mipmaps }, ..Default::default() } +} + +fn le32(b: &[u8], at: usize) -> u32 { + u32::from_le_bytes(b[at..at + 4].try_into().unwrap()) +} + +/// A ramp along one line through RGB (red up, green up half as fast, blue down) across the +/// texture, and alpha falling from opaque down it: every block's colours lie on a line, which +/// is what a colour block stores. +fn ramp(w: u32, h: u32, layout: ChannelLayout) -> Image { + let ch = layout.channels(); + let at = |v: u32, n: u32| (v * 255 / (n - 1).max(1)) as u8; + let mut data = Vec::new(); + for y in 0..h { + for x in 0..w { + let r = at(x, w); + let rgba = [r, 64 + r / 2, 255 - r, 255 - at(y, h)]; + match ch { + 1 => data.push(rgba[0]), + 2 => data.extend([rgba[0], rgba[3]]), + _ => data.extend(&rgba[..ch]), + } + } + } + Image::from_u8(w, h, layout, data).unwrap() +} + +/// The same RGBA image, fully opaque. +fn opaque(img: &Image) -> Image { + let data = img.data().chunks(4).flat_map(|p| [p[0], p[1], p[2], 255]).collect(); + Image::from_u8(img.width(), img.height(), ChannelLayout::Rgba, data).unwrap() +} + +/// Largest difference in channel `c` (of four) between RGBA images. +fn max_diff_channel(a: &Image, b: &Image, c: usize) -> u8 { + a.data().chunks(4).zip(b.data().chunks(4)).map(|(x, y)| x[c].abs_diff(y[c])).max().unwrap_or(0) +} + +/// Largest colour difference between RGBA images where `b` is visible (BC1 turns texels under +/// half opacity into transparent black). +fn max_colour_diff(a: &Image, b: &Image) -> u8 { + a.data().chunks(4).zip(b.data().chunks(4)).filter(|(_, y)| y[3] != 0).flat_map(|(x, y)| (0..3).map(|c| x[c].abs_diff(y[c]))).max().unwrap_or(0) +} + +/// A 2D gradient: red across, green down and blue along the diagonal, opaque. +fn gradient(w: u32, h: u32) -> Image { + let data = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); + Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap() +} + +/// PSNR of the colour (not the alpha) of two RGBA images. +fn psnr(a: &Image, b: &Image) -> f64 { + let rgb = |i: &Image| i.data().chunks(4).flat_map(|p| [p[0], p[1], p[2]]).map(f64::from).collect::>(); + let (a, b) = (rgb(a), rgb(b)); + let mse = a.iter().zip(&b).map(|(x, y)| (x - y).powi(2)).sum::() / a.len() as f64; + if mse == 0.0 { f64::INFINITY } else { 10.0 * (255.0f64.powi(2) / mse).log10() } +} + +/// Bytes of one level as a reader computes them from the header's pixel format. +fn level_bytes(file: &[u8], w: usize, h: usize) -> usize { + if le32(file, 80) & DDPF_FOURCC != 0 { + let block = if &file[84..88] == b"DXT1" { 8 } else { 16 }; + w.div_ceil(4) * h.div_ceil(4) * block + } else { + w * h * le32(file, 88) as usize / 8 + } +} + +/// Mip level `i` of a written texture, cut out as a single-level texture and decoded. +fn level(file: &[u8], i: u32) -> Image { + let (w, h) = (le32(file, 16) as usize, le32(file, 12) as usize); + let size = |i: u32| ((w >> i).max(1), (h >> i).max(1)); + let start = 128 + (0..i).map(|j| level_bytes(file, size(j).0, size(j).1)).sum::(); + let (lw, lh) = size(i); + let mut b = file[..128].to_vec(); + b[12..16].copy_from_slice(&(lh as u32).to_le_bytes()); + b[16..20].copy_from_slice(&(lw as u32).to_le_bytes()); + b[28..32].copy_from_slice(&1u32.to_le_bytes()); + // The pitch field is a row stride for uncompressed textures and the level size for BC. + let pitch = if le32(file, 80) & DDPF_FOURCC != 0 { level_bytes(file, lw, lh) } else { lw * le32(file, 88) as usize / 8 }; + b[20..24].copy_from_slice(&(pitch as u32).to_le_bytes()); + b.extend(&file[start..start + level_bytes(file, lw, lh)]); + decode(&b).unwrap() +} + +#[test] +fn uncompressed_textures_round_trip_every_layout_exactly() { + for (layout, pf_flags, bits, masks) in [ + (ChannelLayout::Gray, DDPF_LUMINANCE, 8, [0xff, 0, 0, 0]), + (ChannelLayout::GrayA, DDPF_LUMINANCE | DDPF_ALPHAPIXELS, 16, [0xff, 0, 0, 0xff00]), + (ChannelLayout::Rgb, DDPF_RGB, 32, [0xff_0000, 0xff00, 0xff, 0]), + (ChannelLayout::Rgba, DDPF_RGB | DDPF_ALPHAPIXELS, 32, [0xff_0000, 0xff00, 0xff, 0xff00_0000]), + ] { + for (w, h) in [(1, 1), (5, 3), (17, 9)] { + let img = ramp(w, h, layout); + let b = encode(&img, Format::Dds, &EncodeOptions::default()).unwrap(); + let back = decode(&b).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {w}x{h}"); + assert_eq!(back.data(), img.data(), "{layout:?} {w}x{h}"); + + assert_eq!((&b[..4], le32(&b, 4), le32(&b, 76)), (&b"DDS "[..], 124, 32)); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_PITCH, "pitch, not linear size, and no mip count"); + assert_eq!((le32(&b, 12), le32(&b, 16)), (h, w)); + assert_eq!(le32(&b, 20), w * bits / 8, "pitch of {layout:?} {w}x{h}"); + assert_eq!((le32(&b, 28), le32(&b, 108)), (1, DDSCAPS_TEXTURE)); + assert_eq!((le32(&b, 80), le32(&b, 88)), (pf_flags, bits)); + assert_eq!([le32(&b, 92), le32(&b, 96), le32(&b, 100), le32(&b, 104)], masks); + assert_eq!(b.len(), 128 + (w * h * bits / 8) as usize); + } + } +} + +#[test] +fn block_compressed_textures_round_trip_through_the_reader() { + for (compression, fourcc, block) in [(DdsCompression::Bc1, b"DXT1", 8), (DdsCompression::Bc2, b"DXT3", 16), (DdsCompression::Bc3, b"DXT5", 16)] { + // Not multiples of four, and the 1×1 and 2×2 textures smaller than one block. + for (w, h) in [(1, 1), (2, 2), (4, 4), (5, 3), (7, 13), (33, 17)] { + let img = ramp(w, h, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, false)).unwrap(); + let linear = w.div_ceil(4) * h.div_ceil(4) * block; + assert_eq!(&b[84..88], fourcc); + assert_eq!((le32(&b, 80), le32(&b, 88)), (DDPF_FOURCC, 0)); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_LINEARSIZE, "linear size, not pitch"); + assert_eq!(le32(&b, 20), linear, "{compression:?} {w}x{h}: the top level's size"); + assert_eq!(b.len(), 128 + linear as usize); + + let back = decode(&b).unwrap(); + assert_eq!((back.dimensions(), back.layout()), ((w, h), ChannelLayout::Rgba)); + // Steep ramps across tiny textures put three colours where a block interpolates at + // thirds, so this bound is loose; it still catches swapped channels or misplaced + // blocks. `block_compression_error_on_gradients_is_bounded` is the tight one. A BC1 + // block with transparent texels has only three colours, so BC1's colours are + // checked on the opaque ramp. + let (src, back_colour) = if compression == DdsCompression::Bc1 { + let o = opaque(&img); + let back = decode(&encode(&o, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + (o, back) + } else { + (img.clone(), back.clone()) + }; + let colour = max_colour_diff(&src, &back_colour); + assert!(colour <= 24, "{compression:?} {w}x{h}: colour off by {colour}"); + let alpha = max_diff_channel(&img, &back, 3); + match compression { + DdsCompression::Bc1 => assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + _ => assert!(alpha <= 8, "{compression:?} {w}x{h}: alpha off by {alpha}"), + } + } + } +} + +#[test] +fn gray_and_rgb_sources_are_written_as_rgba_blocks() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb] { + for compression in BLOCKS { + let img = ramp(8, 8, layout); + let back = decode(&encode(&img, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let want = img.convert(ChannelLayout::Rgba, img.sample_type()); + assert_eq!(back.layout(), ChannelLayout::Rgba); + let colour = max_colour_diff(&want, &back); + assert!(colour <= 8, "{layout:?} {compression:?}: colour off by {colour}"); + } + } +} + +#[test] +fn block_compression_error_on_gradients_is_bounded() { + let (line, plane) = (ramp(64, 64, ChannelLayout::Rgba), gradient(64, 64)); + for compression in BLOCKS { + let roundtrip = |img: &Image| decode(&encode(img, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let (line_back, plane_back) = (roundtrip(&line), roundtrip(&plane)); + let (line_err, plane_err) = (max_colour_diff(&line, &line_back), max_colour_diff(&plane, &plane_back)); + let p = psnr(&plane, &plane_back); + let alpha = max_diff_channel(&line, &line_back, 3); + // Colours on a line only lose the RGB565 rounding of the endpoints (up to 4 levels in + // red and blue). A gradient in two directions is not on a line: the spread off a + // block's best line alone limits this one to about 40 dB, and RGB565 costs a little + // more (the encoder measures 38.6 dB and 9 levels at most). + assert!(line_err <= 5, "{compression:?}: colour ramp off by {line_err}"); + assert!(plane_err <= 10, "{compression:?}: 2D gradient off by {plane_err}"); + assert!(p >= 38.0, "{compression:?}: 2D gradient PSNR {p:.1} dB"); + match compression { + DdsCompression::Bc1 => assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + // Four bits: within half a step of 17. + DdsCompression::Bc2 => assert!(alpha <= 8, "BC2 alpha off by {alpha}"), + // Eight values across a block's few levels of alpha. + _ => assert!(alpha <= 2, "BC3 alpha off by {alpha}"), + } + } +} + +#[test] +fn solid_and_cutout_textures_keep_their_values() { + // RGB565-exact colours and BC2's 4-bit alpha steps come back exactly. + let solid = Image::from_u8(6, 5, ChannelLayout::Rgba, [255, 0, 255, 170].repeat(30)).unwrap(); + for compression in BLOCKS { + let back = decode(&encode(&solid, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + let want = if compression == DdsCompression::Bc1 { [255, 0, 255, 255] } else { [255, 0, 255, 170] }; + assert!(back.data().chunks(4).all(|p| p == want), "{compression:?}: {:?}", &back.data()[..4]); + } + // A cutout: BC1's transparent texels decode as transparent black, the rest stay opaque. + let cutout: Vec = (0..36).flat_map(|i| if (i / 6 + i % 6) % 2 == 0 { [200, 40, 40, 255] } else { [10, 10, 10, 0] }).collect(); + let cutout = Image::from_u8(6, 6, ChannelLayout::Rgba, cutout).unwrap(); + for compression in BLOCKS { + let back = decode(&encode(&cutout, Format::Dds, &opts(compression, false)).unwrap()).unwrap(); + assert_eq!(max_diff_channel(&cutout, &back, 3), 0, "{compression:?}"); + if compression == DdsCompression::Bc1 { + assert!(back.data().chunks(4).filter(|p| p[3] == 0).all(|p| p == [0, 0, 0, 0])); + } + } +} + +#[test] +fn mipmaps_write_the_full_chain_box_filtered() { + // 16×8 halves to 8×4, 4×2, 2×1 and 1×1: five levels. + let img = ramp(16, 8, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_PITCH | DDSD_MIPMAPCOUNT); + assert_eq!(le32(&b, 28), 5); + assert_eq!(le32(&b, 108), DDSCAPS_TEXTURE | DDSCAPS_COMPLEX | DDSCAPS_MIPMAP); + assert_eq!(b.len(), 128 + 4 * (16 * 8 + 8 * 4 + 4 * 2 + 2 + 1)); + assert_eq!(decode(&b).unwrap().data(), img.data(), "the reader still reads the top level"); + let mut above = img.clone(); + for i in 1..5 { + let got = level(&b, i); + assert_eq!(got.dimensions(), ((16 >> i).max(1), (8 >> i).max(1))); + let want = box_filter(&above); + assert_eq!(got.data(), want.data(), "level {i}"); + above = got; + } + // 1×1 has a single level. + let one = encode(&ramp(1, 1, ChannelLayout::Rgba), Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!((le32(&one, 28), le32(&one, 8) & DDSD_MIPMAPCOUNT), (1, 0)); +} + +/// The documented filter, written independently: each pixel averages the 2×2 pixels it covers, +/// and the last column and row of an odd-sized level take in the leftover pixel too. +fn box_filter(src: &Image) -> Image { + let (w, h) = (src.width() as usize, src.height() as usize); + let (nw, nh) = ((w / 2).max(1), (h / 2).max(1)); + let ch = src.layout().channels(); + let span = |i: usize, n: usize, size: usize| (2 * i, if i + 1 == n { size } else { 2 * i + 2 }); + let mut out = Vec::new(); + for y in 0..nh { + for x in 0..nw { + let ((x0, x1), (y0, y1)) = (span(x, nw, w), span(y, nh, h)); + for c in 0..ch { + let values: Vec = + (y0..y1).flat_map(|sy| (x0..x1).map(move |sx| (sy, sx))).map(|(sy, sx)| u32::from(src.data()[(sy * w + sx) * ch + c])).collect(); + let n = values.len() as u32; + out.push(((values.iter().sum::() + n / 2) / n) as u8); + } + } + } + Image::from_u8(nw as u32, nh as u32, src.layout(), out).unwrap() +} + +#[test] +fn odd_sized_mip_chains_keep_every_pixel() { + // 5×3 → 2×1 → 1×1. The 2×1 level's right pixel averages columns 2–4 of all three rows. + let img = ramp(5, 3, ChannelLayout::Gray); + let b = encode(&img, Format::Dds, &opts(DdsCompression::None, true)).unwrap(); + assert_eq!(le32(&b, 28), 3); + assert_eq!(b.len(), 128 + 5 * 3 + 2 + 1); + let second = level(&b, 1); + assert_eq!(second.data(), box_filter(&img).data()); + let px = |x: usize, y: usize| u32::from(img.data()[y * 5 + x]); + let right: u32 = (0..3).flat_map(|y| (2..5).map(move |x| (x, y))).map(|(x, y)| px(x, y)).sum(); + assert_eq!(u32::from(second.data()[1]), (right + 4) / 9); +} + +#[test] +fn block_compressed_mip_chains_have_a_block_per_small_level() { + for compression in BLOCKS { + let block = if compression == DdsCompression::Bc1 { 8 } else { 16 }; + let img = ramp(12, 6, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, true)).unwrap(); + // 12×6, 6×3, 3×1, 1×1: 3×2, 2×1, 1 and 1 blocks. + assert_eq!(le32(&b, 28), 4); + assert_eq!(le32(&b, 8), REQUIRED | DDSD_LINEARSIZE | DDSD_MIPMAPCOUNT); + assert_eq!(le32(&b, 20), 6 * block); + assert_eq!(b.len(), 128 + (6 + 2 + 1 + 1) * block as usize); + for i in 0..4 { + let got = level(&b, i); + assert_eq!(got.dimensions(), ((12 >> i).max(1), (6 >> i).max(1)), "{compression:?} level {i}"); + } + } +} + +#[test] +fn strips_and_tiny_textures_encode_with_every_option() { + for (w, h) in [(1, 1), (1, 9), (9, 1), (2, 3), (3, 2)] { + for compression in DdsCompression::ALL { + for mipmaps in [false, true] { + let img = ramp(w, h, ChannelLayout::Rgba); + let b = encode(&img, Format::Dds, &opts(compression, mipmaps)).unwrap_or_else(|e| panic!("{w}x{h} {compression:?} {mipmaps}: {e}")); + assert_eq!(decode(&b).unwrap().dimensions(), (w, h)); + } + } + } +} + +#[test] +fn fidelity_warnings_follow_the_compression() { + let translucent = ramp(8, 8, ChannelLayout::Rgba); + let warns = |c| fidelity_warnings_with(&translucent, Format::Dds, &opts(c, false)); + assert!(warns(DdsCompression::None).is_empty(), "uncompressed RGBA loses nothing"); + for c in BLOCKS { + assert!(warns(c).contains(&FidelityWarning::LossyCompression), "{c:?}"); + } + assert!(warns(DdsCompression::Bc1).contains(&FidelityWarning::AlphaBinarized)); + assert!(!warns(DdsCompression::Bc3).contains(&FidelityWarning::AlphaBinarized)); +} + +#[test] +fn compression_names_round_trip() { + for c in DdsCompression::ALL { + assert_eq!(DdsCompression::from_name(c.name()), Some(c)); + } + assert_eq!(DdsCompression::from_name("DXT5"), Some(DdsCompression::Bc3)); + assert_eq!(DdsCompression::from_name("dxt1"), Some(DdsCompression::Bc1)); + for bad in ["", "bc7", "dxt2", "uncompressed "] { + assert_eq!(DdsCompression::from_name(bad), None, "{bad:?}"); + } +} + +/// Encode timings on a synthetic ~30 MP texture (6336×4752). Ignored by default; run in release: +/// +/// ```sh +/// cargo test --release -p craft-image-codecs --test it -- dds_write:: --ignored --nocapture +/// ``` +#[test] +#[ignore] +fn dds_encode_timings_on_30_mp() { + let (w, h) = (6336u32, 4752u32); + let mut s = 0x1234_5678u32; + let mut data = Vec::with_capacity(w as usize * h as usize * 4); + for y in 0..h { + for x in 0..w { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + data.extend([(x / 25 + y / 19) as u8, (x / 31) as u8 ^ (s as u8 & 3), (y / 23) as u8, (255 - x / 25) as u8]); + } + } + let img = Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap(); + for compression in DdsCompression::ALL { + for mipmaps in [false, true] { + let t = std::time::Instant::now(); + let b = encode(&img, Format::Dds, &opts(compression, mipmaps)).unwrap(); + eprintln!("{compression:?} mipmaps={mipmaps}: {:.0} ms, {} MB", t.elapsed().as_secs_f64() * 1e3, b.len() >> 20); + } + } +} diff --git a/crates/image-codecs/tests/it/decode_warnings.rs b/crates/image-codecs/tests/it/decode_warnings.rs new file mode 100644 index 000000000..5995de931 --- /dev/null +++ b/crates/image-codecs/tests/it/decode_warnings.rs @@ -0,0 +1,511 @@ +//! Decode warnings: a file that holds more than was decoded (#523: animations, multi-page +//! TIFF) or less than it should (#518: truncated JPEG) says so; complete single images don't. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +const W: u32 = 24; +const H: u32 = 16; + +fn solid(rgb: [u8; 3]) -> Image { + Image::from_u8(W, H, ChannelLayout::Rgb, rgb.repeat((W * H) as usize)).unwrap() +} + +const COLOURS: [[u8; 3]; 3] = [[200, 30, 10], [10, 200, 30], [30, 10, 200]]; + +// --------------------------------------------------------------------------- +// #518: truncated JPEG +// --------------------------------------------------------------------------- + +fn jpeg(layout: ChannelLayout, progressive: bool) -> Vec { + let img = synth(64, 48, layout, SampleType::U8, 3, 0.3); + let ct = match layout { + ChannelLayout::Gray => jpeg_encoder::ColorType::Luma, + ChannelLayout::Cmyk => jpeg_encoder::ColorType::Cmyk, + _ => jpeg_encoder::ColorType::Rgb, + }; + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 90); + enc.set_progressive(progressive); + enc.encode(img.data(), 64, 48, ct).unwrap(); + out +} + +/// Offset of the first byte of entropy-coded data (just past the first SOS header). +fn scan_start(b: &[u8]) -> usize { + let sos = b.windows(2).position(|w| w == [0xFF, 0xDA]).unwrap(); + sos + 2 + u16::from_be_bytes([b[sos + 2], b[sos + 3]]) as usize +} + +const TRUNCATED: DecodeWarning = DecodeWarning::Truncated { format: Format::Jpeg }; + +#[test] +fn truncated_jpeg_decodes_with_a_warning() { + for layout in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Cmyk] { + for progressive in [false, true] { + let full = jpeg(layout, progressive); + assert_eq!(decode(&full).unwrap().warnings, [], "{layout:?} progressive {progressive}"); + let half = decode(&full[..full.len() / 2]).unwrap(); + assert_eq!(half.warnings, [TRUNCATED], "{layout:?} progressive {progressive}"); + assert_eq!(half.dimensions(), (64, 48)); + // One byte short: the end-of-image marker is incomplete. + assert_eq!(decode(&full[..full.len() - 1]).unwrap().warnings, [TRUNCATED]); + // A few bytes of scan data still decode, with the warning. + let start = scan_start(&full); + assert_eq!(decode(&full[..start + 8]).unwrap().warnings, [TRUNCATED]); + } + } +} + +#[test] +fn truncated_jpeg_message_names_the_problem() { + let s = TRUNCATED.to_string(); + assert!(s.starts_with("JPEG data ends early") && s.contains("truncated"), "{s}"); +} + +#[test] +fn jpeg_without_any_scan_data_is_an_error() { + let full = jpeg(ChannelLayout::Rgb, false); + let start = scan_start(&full); + for cut in [start - 3, start - 1, start] { + let e = decode(&full[..cut]).unwrap_err().to_string(); + assert!(e.contains("before any image data"), "cut {cut}: {e}"); + } +} + +#[test] +fn data_after_the_end_of_image_is_not_truncation() { + let mut b = jpeg(ChannelLayout::Rgb, false); + b.extend_from_slice(b"\0\0trailing bytes \xFF\xD8\xFF"); + assert_eq!(decode(&b).unwrap().warnings, []); +} + +#[test] +fn an_end_marker_inside_metadata_does_not_hide_truncation() { + // An EXIF block holding FF D9 (as an embedded thumbnail does) must be skipped as a segment. + let mut img = synth(64, 48, ChannelLayout::Rgb, SampleType::U8, 3, 0.3); + let mut exif = sample_exif(); + exif.extend_from_slice(&[0xFF, 0xD8, 0x00, 0xFF, 0xD9]); + img.meta.exif = Some(exif); + let full = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&full).unwrap().warnings, []); + assert_eq!(decode(&full[..full.len() * 2 / 3]).unwrap().warnings, [TRUNCATED]); +} + +#[test] +fn restart_markers_and_fill_bytes_are_scan_data() { + // Restart markers (FF D0–D7) and fill bytes (FF FF) inside a scan don't end it. + let full = jpeg(ChannelLayout::Rgb, false); + let start = scan_start(&full); + let mut b = full[..start].to_vec(); + b.extend_from_slice(&[0x12, 0xFF, 0x00, 0x34, 0xFF, 0xD0, 0x56, 0xFF, 0xFF, 0xD7, 0x78]); + assert_eq!(decode(&b).unwrap().warnings, [TRUNCATED]); + b.extend_from_slice(&[0xFF, 0xFF, 0xD9]); + assert_eq!(decode(&b).unwrap().warnings, []); +} + +// --------------------------------------------------------------------------- +// #523: only the first frame or page is decoded +// --------------------------------------------------------------------------- + +fn more_frames(n: u32) -> Vec { + vec![DecodeWarning::MoreFrames { total: Some(n) }] +} + +fn gif(frames: usize) -> Vec { + let mut out = Vec::new(); + let mut enc = image::codecs::gif::GifEncoder::new(&mut out); + let frames = COLOURS.iter().take(frames).map(|c| image::Frame::new(image::RgbaImage::from_pixel(W, H, image::Rgba([c[0], c[1], c[2], 255])))); + enc.encode_frames(frames).unwrap(); + drop(enc); + out +} + +#[test] +fn animated_gif_warns_and_keeps_the_first_frame() { + let one = decode(&gif(1)).unwrap(); + assert_eq!(one.warnings, []); + for n in [2, 3] { + let img = decode(&gif(n)).unwrap(); + assert_eq!(img.warnings, more_frames(n as u32)); + assert_eq!(img.to_rgba8(), one.to_rgba8(), "{n} frames: first frame pixels"); + } + // A single-frame GIF written by our own encoder. + let ours = encode(&solid(COLOURS[0]), Format::Gif, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&ours).unwrap().warnings, []); +} + +#[test] +fn gif_without_a_trailer_warns_without_a_total() { + let b = gif(3); + assert_eq!(b.last(), Some(&0x3B)); + let img = decode(&b[..b.len() - 1]).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MoreFrames { total: None }]); + assert_eq!(img.warnings[0].to_string(), "only the first frame of the animation was imported"); +} + +/// An APNG of `frames` solid frames at 8 or 16 bits; with `separate_default` the IDAT image is +/// an extra default image outside the animation. +fn apng(frames: u32, sixteen: bool, separate_default: bool) -> Vec { + let mut out = Vec::new(); + let mut enc = png::Encoder::new(&mut out, W, H); + enc.set_color(png::ColorType::Rgb); + enc.set_depth(if sixteen { png::BitDepth::Sixteen } else { png::BitDepth::Eight }); + let images = frames + u32::from(separate_default); + if frames > 0 { + enc.set_animated(frames, 0).unwrap(); + enc.set_sep_def_img(separate_default).unwrap(); + } + let mut w = enc.write_header().unwrap(); + for c in COLOURS.iter().cycle().take(images.max(1) as usize) { + let px: Vec = if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; + w.write_image_data(&px).unwrap(); + } + w.finish().unwrap(); + out +} + +#[test] +fn apng_warns_and_keeps_the_first_frame() { + for sixteen in [false, true] { + let still = decode(&apng(0, sixteen, false)).unwrap(); + assert_eq!(still.warnings, []); + assert_eq!(decode(&apng(1, sixteen, false)).unwrap().warnings, [], "a one-frame APNG is a still"); + let img = decode(&apng(3, sixteen, false)).unwrap(); + assert_eq!(img.warnings, more_frames(3), "16-bit {sixteen}"); + assert_eq!((img.sample_type(), img.data()), (still.sample_type(), still.data()), "16-bit {sixteen}: first frame pixels"); + // Default image + 2 animation frames: three images, the default one kept. + let img = decode(&apng(2, sixteen, true)).unwrap(); + assert_eq!(img.warnings, more_frames(3)); + assert_eq!(img.data(), still.data()); + } +} + +fn chunk(id: &[u8; 4], data: &[u8]) -> Vec { + let mut c = id.to_vec(); + c.extend_from_slice(&(data.len() as u32).to_le_bytes()); + c.extend_from_slice(data); + if data.len() % 2 == 1 { + c.push(0); + } + c +} + +fn u24(v: u32) -> [u8; 3] { + let b = v.to_le_bytes(); + [b[0], b[1], b[2]] +} + +/// An animated WebP: each frame is a full-canvas lossless still wrapped in an ANMF chunk. +fn animated_webp(frames: &[Image]) -> Vec { + let mut body = b"WEBP".to_vec(); + let mut vp8x = vec![0x02, 0, 0, 0]; // animation flag + vp8x.extend_from_slice(&u24(W - 1)); + vp8x.extend_from_slice(&u24(H - 1)); + body.extend(chunk(b"VP8X", &vp8x)); + body.extend(chunk(b"ANIM", &[0, 0, 0, 0, 0, 0])); + for f in frames { + let still = encode(f, Format::WebP, &EncodeOptions::default()).unwrap(); + let at = still.windows(4).position(|w| w == b"VP8L").unwrap(); + let len = u32::from_le_bytes(still[at + 4..at + 8].try_into().unwrap()) as usize; + let mut anmf = vec![0; 6]; // frame x, y + anmf.extend_from_slice(&u24(W - 1)); + anmf.extend_from_slice(&u24(H - 1)); + anmf.extend_from_slice(&u24(100)); // duration + anmf.push(0); + anmf.extend(chunk(b"VP8L", &still[at + 8..at + 8 + len])); + body.extend(chunk(b"ANMF", &anmf)); + } + let mut out = b"RIFF".to_vec(); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend(body); + out +} + +#[test] +fn animated_webp_warns_and_keeps_the_first_frame() { + let frames: Vec = COLOURS.iter().map(|&c| solid(c)).collect(); + let still = decode(&encode(&frames[0], Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(still.warnings, []); + assert_eq!(decode(&animated_webp(&frames[..1])).unwrap().warnings, [], "a one-frame animation"); + for n in [2, 3] { + let img = decode(&animated_webp(&frames[..n])).unwrap(); + assert_eq!(img.warnings, more_frames(n as u32)); + // The animation decoder composites the frame onto its canvas: within one level. + let (a, b) = (img.to_rgba8(), still.to_rgba8()); + assert!(a.len() == b.len() && a.iter().zip(&b).all(|(x, y)| x.abs_diff(*y) <= 1), "{n} frames: first frame pixels"); + } +} + +/// A TIFF with one IFD per entry: `(NewSubfileType, sample type)`. +fn tiff(ifds: &[(u32, SampleType)]) -> Vec { + use tiff::encoder::{TiffEncoder, colortype}; + use tiff::tags::Tag; + let mut out = std::io::Cursor::new(Vec::new()); + let mut enc = TiffEncoder::new(&mut out).unwrap(); + let n = (W * H) as usize; + // Each page is a flat grey at its own level (`k`), written at `sample` depth. + for (k, &(kind, sample)) in ifds.iter().enumerate() { + macro_rules! page { + ($ct:ty, $data:expr) => {{ + let mut im = enc.new_image::<$ct>(W, H).unwrap(); + if kind != 0 { + im.encoder().write_tag(Tag::NewSubfileType, kind).unwrap(); + } + im.write_data($data).unwrap(); + }}; + } + let v = 40 * k as u8 + 10; + match sample { + SampleType::U8 => page!(colortype::Gray8, &vec![v; n]), + SampleType::U16 => page!(colortype::Gray16, &vec![u16::from(v) * 257; n]), + _ => page!(colortype::Gray32Float, &vec![f32::from(v) / 255.0; n]), + } + } + out.into_inner() +} + +#[test] +fn multi_page_tiff_warns_and_keeps_the_first_page() { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let one = decode(&tiff(&[(0, sample)])).unwrap(); + assert_eq!(one.warnings, [], "{sample:?}"); + let img = decode(&tiff(&[(0, sample), (2, sample)])).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }], "{sample:?}"); + assert_eq!(img.data(), one.data(), "{sample:?}: first page pixels"); + let img = decode(&tiff(&[(0, sample), (0, sample), (0, sample)])).unwrap(); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(3) }], "{sample:?}"); + assert_eq!(img.warnings[0].to_string(), "only the first of 3 pages was imported"); + } +} + +#[test] +fn tiff_thumbnails_and_masks_are_not_pages() { + let u8 = SampleType::U8; + // A reduced-resolution copy (bit 0) or a transparency mask (bit 2) is part of the page. + assert_eq!(decode(&tiff(&[(0, u8), (1, u8)])).unwrap().warnings, []); + assert_eq!(decode(&tiff(&[(0, u8), (4, u8)])).unwrap().warnings, []); + assert_eq!(decode(&tiff(&[(0, u8), (1, u8), (0, u8)])).unwrap().warnings, [DecodeWarning::MorePages { total: Some(2) }]); +} + +#[test] +fn single_images_carry_no_warnings() { + for f in rw_formats() { + let img = synth(9, 7, ChannelLayout::Rgba, SampleType::U8, 5, 0.2); + let Ok(bytes) = encode(&img, f, &EncodeOptions::default()) else { continue }; + assert_eq!(decode(&bytes).unwrap().warnings, [], "{f:?}"); + } +} + +#[test] +fn warnings_survive_orientation() { + let mut img = synth(8, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.warnings.push(DecodeWarning::MorePages { total: None }); + for o in 1..=8 { + assert_eq!(img.clone().oriented(o).unwrap().warnings, img.warnings, "orientation {o}"); + } + assert_eq!(img.convert(ChannelLayout::Rgba, SampleType::U16).warnings, img.warnings); +} + +/// A flagless depth still declares slices the flat reader and writer cannot retain. +#[test] +fn dds_depth_probe_refuses_unretained_slices_without_volume_flags() { + let single = encode(&solid(COLOURS[0]), Format::Dds, &Default::default()).unwrap(); + let expected = decode(&single).unwrap(); + let mut second = single[128..].to_vec(); + second[0] ^= 0xff; + for depth in [0u32, 1, 2, 3] { + let mut bytes = single.clone(); + bytes[24..28].copy_from_slice(&depth.to_le_bytes()); + for _ in 1..depth { + bytes.extend_from_slice(&second); + } + let flags = u32::from_le_bytes(bytes[8..12].try_into().unwrap()); + let caps = u32::from_le_bytes(bytes[112..116].try_into().unwrap()); + assert_eq!(flags & 0x0080_0000, 0); + assert_eq!(caps & 0x0020_0000, 0); + // Decoding remains permissive for preview; only source replacement is refused. + assert_eq!(decode(&bytes).unwrap().data(), expected.data()); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), depth > 1); + } +} + +/// Header-only inspection protects unretained source payloads while allowing stills and thumbnails. +#[test] +fn additional_images_probe_uses_container_structure_without_decoding_pixels() { + use craft_image_codecs::has_unretained_source_data; + let frames: Vec<_> = COLOURS.iter().take(2).map(|&c| solid(c)).collect(); + for (bytes, multiple) in [ + (gif(1), false), + (gif(2), true), + (apng(0, false, false), false), + (apng(1, false, false), false), + (apng(1, false, true), true), + (apng(2, true, false), true), + (animated_webp(&frames[..1]), false), + (animated_webp(&frames), true), + (tiff(&[(0, SampleType::U8)]), false), + (tiff(&[(0, SampleType::U8), (2, SampleType::U8)]), true), + (tiff(&[(0, SampleType::U8), (1, SampleType::U8)]), false), + (tiff(&[(4, SampleType::U8)]), false), + (tiff(&[(0, SampleType::U8), (4, SampleType::U8)]), true), + (tiff(&[(4, SampleType::U8), (0, SampleType::U8)]), true), + (tiff(&[(0, SampleType::U8), (1, SampleType::U8), (4, SampleType::U8)]), true), + ] { + assert_eq!(has_unretained_source_data(&bytes).unwrap(), multiple); + } + // A GIF whose blocks break off before the trailer after one frame may hide later frames: + // the probe refuses it instead of reporting a still. + let one = gif(1); + assert!(has_unretained_source_data(&one[..one.len() - 1]).is_err(), "trailer cut off"); + let two = gif(2); + let mut broken = gif(1); + broken.pop(); + broken.push(0x99); + broken.extend_from_slice(&two[two.len() - 10..]); + assert!(has_unretained_source_data(&broken).is_err(), "junk block after the first frame"); + assert!(has_unretained_source_data(&two[..two.len() - 1]).unwrap(), "two frames seen, trailer cut off"); + let image = solid(COLOURS[0]); + for mipmaps in [false, true] { + let mut options = EncodeOptions::default(); + options.dds.mipmaps = mipmaps; + let bytes = encode(&image, Format::Dds, &options).unwrap(); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), mipmaps); + if mipmaps { + let mut flagless = bytes; + let flags = u32::from_le_bytes(flagless[8..12].try_into().unwrap()); + flagless[8..12].copy_from_slice(&(flags & !0x0002_0000).to_le_bytes()); + decode(&flagless).unwrap(); + // Microsoft DDS_HEADER: readers cannot rely on DDSD_MIPMAPCOUNT being set. + assert!(has_unretained_source_data(&flagless).unwrap()); + } + } + let dpx = encode(&image, Format::Dpx, &Default::default()).unwrap(); + for little in [false, true] { + let mut bytes = dpx.clone(); + if little { + bytes[..4].copy_from_slice(b"XPDS"); + for at in [4, 772, 776, 780, 808, 812] { + bytes[at..at + 4].reverse(); + } + for at in [768, 770, 804, 806] { + bytes[at..at + 2].reverse(); + } + } + assert!(!has_unretained_source_data(&bytes).unwrap()); + for count in [2u16, 8] { + bytes[770..772].copy_from_slice(&if little { count.to_le_bytes() } else { count.to_be_bytes() }); + assert!(has_unretained_source_data(&bytes).unwrap()); + } + bytes[770..772].fill(0); + assert!(has_unretained_source_data(&bytes).is_err()); + assert!(has_unretained_source_data(&bytes[..771]).is_err()); + } + let one = encode(&image, Format::Ico, &Default::default()).unwrap(); + assert!(!has_unretained_source_data(&one).unwrap()); + let mut multiple = one[..6].to_vec(); + multiple[4..6].copy_from_slice(&2u16.to_le_bytes()); + let mut entry = one[6..22].to_vec(); + let offset = u32::from_le_bytes(entry[12..16].try_into().unwrap()) + 16; + entry[12..16].copy_from_slice(&offset.to_le_bytes()); + multiple.extend_from_slice(&entry); + multiple.extend_from_slice(&entry); + multiple.extend_from_slice(&one[22..]); + decode(&multiple).unwrap(); + assert!(has_unretained_source_data(&multiple).unwrap()); + assert!(has_unretained_source_data(b"\x89PNG\r\n\x1a\n").is_err()); +} + +/// FITS RGB planes form one image; other cubes and later image HDUs would be discarded. +#[test] +fn fits_additional_images_probe_distinguishes_color_planes_and_hdus() { + let hdu = |primary: bool, axes: &[usize]| { + let mut cards = vec![ + if primary { "SIMPLE = T".to_string() } else { "XTENSION= 'IMAGE'".to_string() }, + "BITPIX = 8".into(), + format!("NAXIS = {}", axes.len() + 2), + "NAXIS1 = 1".into(), + "NAXIS2 = 1".into(), + ]; + cards.extend(axes.iter().enumerate().map(|(index, n)| format!("NAXIS{} = {n}", index + 3))); + cards.push("END".into()); + let mut bytes = Vec::new(); + for card in cards.drain(..) { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(bytes.len().div_ceil(2880) * 2880, b' '); + bytes.extend((0..axes.iter().product::()).map(|i| i as u8 + 1)); + bytes.resize(bytes.len().div_ceil(2880) * 2880, 0); + bytes + }; + for (axes, multiple) in [(vec![1], false), (vec![2], true), (vec![3], false), (vec![4], true), (vec![1, 3], true), (vec![3, 1], false)] { + let bytes = hdu(true, &axes); + let image = decode(&bytes).unwrap(); + assert_eq!(image.warnings.iter().any(|w| matches!(w, DecodeWarning::MorePages { .. })), multiple); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), multiple); + } + let mut bytes = hdu(true, &[1]); + bytes.extend(hdu(false, &[1])); + decode(&bytes).unwrap(); + assert!(has_unretained_source_data(&bytes).unwrap()); + let mut incomplete = hdu(true, &[1]); + incomplete.extend_from_slice(b"XTENSION= 'IMAGE'"); + assert!(has_unretained_source_data(&incomplete).is_err()); +} + +/// Tables and extension heaps are source data even though the flat image decoder skips them. +#[test] +fn fits_source_probe_preserves_nonimage_payload_and_allows_empty_extensions() { + let primary = encode(&solid(COLOURS[0]), Format::Fits, &Default::default()).unwrap(); + for (kind, rows, heap, unretained) in + [("BINTABLE", 1, 0, true), ("TABLE", 1, 0, true), ("OTHER", 1, 0, true), ("BINTABLE", 0, 4, true), ("BINTABLE", 0, 0, false)] + { + let mut bytes = primary.clone(); + for card in [ + format!("XTENSION= '{kind}'"), + "BITPIX = 8".into(), + "NAXIS = 2".into(), + "NAXIS1 = 4".into(), + format!("NAXIS2 = {rows}"), + format!("PCOUNT = {heap}"), + "GCOUNT = 1".into(), + "TFIELDS = 1".into(), + "TTYPE1 = 'DATA'".into(), + "TFORM1 = '4B'".into(), + "END".into(), + ] { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(bytes.len().div_ceil(2880) * 2880, b' '); + bytes.extend(std::iter::repeat_n(0xa5, rows * 4 + heap)); + bytes.resize(bytes.len().div_ceil(2880) * 2880, 0); + assert_eq!(decode(&bytes).unwrap().data(), decode(&primary).unwrap().data()); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), unretained, "{kind} rows={rows} heap={heap}"); + } + let mut heap = primary.clone(); + let end = heap.as_chunks::<80>().0.iter().position(|c| c.starts_with(b"END ")).unwrap() * 80; + heap[end..end + 160].fill(b' '); + heap[end..end + 11].copy_from_slice(b"PCOUNT = 1"); + heap[end + 80..end + 83].copy_from_slice(b"END"); + heap[2880 + (W * H * 3) as usize] = 0xa5; + decode(&heap).unwrap(); + assert!(has_unretained_source_data(&heap).unwrap()); + for data in [false, true] { + let mut bytes = Vec::new(); + for card in ["SIMPLE = T", "BITPIX = 8", if data { "NAXIS = 1" } else { "NAXIS = 0" }, "NAXIS1 = 2", "END"] { + bytes.extend_from_slice(format!("{card:<80}").as_bytes()); + } + bytes.resize(2880, b' '); + if data { + bytes.extend_from_slice(&[0x11, 0x22]); + bytes.resize(5760, 0); + } + let mut image = primary.clone(); + image[..80].fill(b' '); + image[..17].copy_from_slice(b"XTENSION= 'IMAGE'"); + bytes.extend(image); + decode(&bytes).unwrap(); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), data, "empty primary versus skipped one-dimensional payload"); + } +} diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs new file mode 100644 index 000000000..a3c003f75 --- /dev/null +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -0,0 +1,992 @@ +//! Deep EXR: a synthetic generator builds deep chunks per the OpenEXR file layout +//! specification (NONE, RLE and ZIPS × scanline and tile), the oracle tests check the +//! compositing math against hand-computed values, and malformed inputs must error, never +//! panic. With the `corpus` feature, the OpenEXR project's deep test images (`corpus/exr/`, +//! pinned) are checked against the ID manifests their sidecars list. + +use craft_image_codecs::CodecError; +use craft_image_codecs::f16; +use craft_image_codecs::{Limits, decode, decode_deep_exr}; +use std::io::Write as _; + +// --------------------------------------------------------------------------------------------- +// A minimal deep-EXR writer, mirroring what the reference files show on disk: +// version 2 | deep bit (and the tile bit for tiles), one header with the mandatory +// attributes in alphabetical order, a chunk offset table, then deep chunks of +// [y | tile coords][u64 packed table][u64 packed data][u64 unpacked data][table][data]. +// The table holds u32 sample counts, cumulative within each row of the block; the sample +// data is grouped per row, then per channel, in pixel order (the layout the OpenEXR library +// writes). NONE stores raw; RLE and ZIPS separate even/odd bytes, apply the +128 predictor, +// then RLE packs runs and ZIPS deflates. + +fn attr(out: &mut Vec, name: &str, ty: &str, payload: &[u8]) { + out.extend_from_slice(name.as_bytes()); + out.push(0); + out.extend_from_slice(ty.as_bytes()); + out.push(0); + out.extend_from_slice(&(payload.len() as i32).to_le_bytes()); + out.extend_from_slice(payload); +} + +fn samples_to_differences(buf: &mut [u8]) { + if let Some(&first) = buf.first() { + let mut prev = i16::from(first); + for b in buf.iter_mut().skip(1) { + let v = i16::from(*b); + *b = (v - prev + 128) as u8; + prev = v; + } + } +} + +fn separate_bytes_fragments(buf: &mut [u8]) { + let copy = buf.to_vec(); + let half = copy.len().div_ceil(2); + for i in 0..copy.len() { + buf[if i % 2 == 0 { i / 2 } else { half + i / 2 }] = copy[i]; + } +} + +fn rle_bytes(src: &[u8]) -> Vec { + let mut out = Vec::new(); + let mut i = 0; + while i < src.len() { + let mut run = 1; + while i + run < src.len() && run < 128 && src[i + run] == src[i] { + run += 1; + } + // OpenEXR tokens: `n >= 0` repeats the next byte n + 1 times, `n < 0` copies -n bytes. + if run > 1 { + out.push((run - 1) as u8); + out.push(src[i]); + i += run; + } else { + let start = i; + i += 1; + while i < src.len() && (i - start) < 127 && src[i] != src[i - 1] { + i += 1; + } + let n = i - start; + out.push((-(n as i32)) as u8); + out.extend_from_slice(&src[start..start + n]); + } + } + out +} + +fn compress(kind: u8, raw: &[u8]) -> Vec { + match kind { + 1 => { + // Like ZIPS, RLE packs the byte-split, predicted stream. + let mut b = raw.to_vec(); + separate_bytes_fragments(&mut b); + samples_to_differences(&mut b); + let c = rle_bytes(&b); + if c.len() < raw.len() { c } else { raw.to_vec() } + } + 2 => { + let mut b = raw.to_vec(); + separate_bytes_fragments(&mut b); + samples_to_differences(&mut b); + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + // Writing to a Vec never fails; the encoder errors surface in `finish`. + #[allow(clippy::unwrap_used)] + z.write_all(&b).unwrap(); + #[allow(clippy::unwrap_used)] + let c = z.finish().unwrap(); + if c.len() < raw.len() { c } else { raw.to_vec() } + } + _ => raw.to_vec(), + } +} + +/// One channel: a name (the caller must pass channels in alphabetical order, as chlist +/// requires) and the sample type as in the chlist attribute (0 = u32, 1 = half, 2 = float). +struct Chan { + name: &'static str, + ty: i32, +} + +/// `counts[p]` samples per pixel; `sample(p, ci)` returns channel `ci`'s values for pixel +/// `p` (its length must be `counts[p]`). `tile` switches between deepscanline and deeptile. +#[allow(clippy::too_many_arguments)] +fn gen_deep( + w: usize, + h: usize, + channels: &[Chan], + compression: u8, + tile: Option<(usize, usize)>, + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, +) -> Vec { + gen_deep_tables(w, h, channels, compression, tile, counts, sample, false) +} + +/// [`gen_deep`]; with `full_tile_tables` every tile's count table is stored at the full tile +/// size, as the OpenEXR library writes edge tiles: the block's entries first (in rows of its +/// clipped width), then zeros. +#[allow(clippy::too_many_arguments)] +fn gen_deep_tables( + w: usize, + h: usize, + channels: &[Chan], + compression: u8, + tile: Option<(usize, usize)>, + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, + full_tile_tables: bool, +) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + // Single-part deep files (deepscanline and deeptile alike) carry only the deep-data + // flag; the tiling lives in the `tiles` attribute and the `type` string. + out.extend_from_slice(&(2u32 | 0x800).to_le_bytes()); + + let mut chlist = Vec::new(); + for c in channels { + chlist.extend_from_slice(c.name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&c.ty.to_le_bytes()); + chlist.push(0); // pLinear + chlist.extend_from_slice(&[0u8; 3]); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + attr(&mut out, "channels", "chlist", &chlist); + attr(&mut out, "compression", "compression", &[compression]); + // box2i is inclusive: the data window of a w×h image at the origin ends at (w-1, h-1). + let dw: Vec = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "dataWindow", "box2i", &dw); + attr(&mut out, "displayWindow", "box2i", &dw); + attr(&mut out, "lineOrder", "lineOrder", &[0]); + attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut out, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + if let Some((tw, th)) = tile { + let mut td = Vec::new(); + td.extend_from_slice(&(tw as u32).to_le_bytes()); + td.extend_from_slice(&(th as u32).to_le_bytes()); + td.push(0); // one level, round down + attr(&mut out, "tiles", "tiledesc", &td); + } + attr(&mut out, "type", "string", if tile.is_some() { b"deeptile" } else { b"deepscanline" }); + attr(&mut out, "version", "int", &1i32.to_le_bytes()); + out.push(0); // end of the header + + // The blocks: scan lines (one line each) or the level-0 tiles. + struct Block { + pixels: Vec, + /// Pixels per row: the count table restarts and the sample data is grouped per row. + row_w: usize, + } + let blocks: Vec = match tile { + None => (0..h).map(|y| Block { pixels: (y * w..y * w + w).collect(), row_w: w }).collect(), + Some((tw, th)) => { + let tx = w.div_ceil(tw); + let ty = h.div_ceil(th); + let mut v = Vec::new(); + for by in 0..ty { + for bx in 0..tx { + let mut px = Vec::new(); + for yy in (by * th)..((by * th + th).min(h)) { + for xx in (bx * tw)..((bx * tw + tw).min(w)) { + px.push(yy * w + xx); + } + } + v.push(Block { pixels: px, row_w: (bx * tw + tw).min(w) - bx * tw }); + } + } + v + } + }; + + let table_len = |b: &Block| b.pixels.len() * 4; + let data_len = |b: &Block| -> usize { + let samples: u64 = b.pixels.iter().map(|&p| counts[p] as u64).sum(); + channels.iter().map(|c| samples as usize * if c.ty == 1 { 2 } else { 4 }).sum() + }; + + // Build every chunk, remember the offsets, then write the offset table first. + let mut chunks: Vec<(usize, Vec)> = Vec::new(); // (block index, framed chunk) + for (bi, b) in blocks.iter().enumerate() { + let mut c = Vec::new(); + match tile { + None => { + let y = (bi % h) as i32; + c.extend_from_slice(&y.to_le_bytes()); + } + Some((tw, _)) => { + let tx = w.div_ceil(tw); + let (bx, by) = (bi % tx, bi / tx); + c.extend_from_slice(&(bx as i32).to_le_bytes()); + c.extend_from_slice(&(by as i32).to_le_bytes()); + c.extend_from_slice(&0i32.to_le_bytes()); + c.extend_from_slice(&0i32.to_le_bytes()); + } + } + let mut table = Vec::with_capacity(b.pixels.len() * 4); + for row in b.pixels.chunks(b.row_w) { + let mut acc = 0u32; + for &p in row { + acc += counts[p]; + table.extend_from_slice(&acc.to_le_bytes()); + } + } + assert_eq!(table.len(), table_len(b)); + if let (true, Some((tw, th))) = (full_tile_tables, tile) { + table.resize(tw * th * 4, 0); + } + let mut data = Vec::new(); + for row in b.pixels.chunks(b.row_w) { + for (ci, ch) in channels.iter().enumerate() { + for &p in row { + for v in sample(p, ci) { + match ch.ty { + 1 => data.extend_from_slice(&f16::from_f32(v).to_le_bytes()), + 0 => data.extend_from_slice(&(v as u32).to_le_bytes()), + _ => data.extend_from_slice(&v.to_le_bytes()), + } + } + } + } + } + let ct = compress(compression, &table); + let cd = compress(compression, &data); + c.extend_from_slice(&(ct.len() as u64).to_le_bytes()); + c.extend_from_slice(&(cd.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&ct); + c.extend_from_slice(&cd); + assert_eq!(data.len(), data_len(b)); + chunks.push((bi, c)); + } + let mut pos = out.len() + chunks.len() * 8; + let mut table = Vec::new(); + for (_, c) in &chunks { + table.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += c.len(); + } + out.extend_from_slice(&table); + for (_, c) in chunks { + out.extend_from_slice(&c); + } + out +} + +/// RGBA float channels in chlist (alphabetical) order, no Z (everything composites). +const RGBA_NO_Z: [Chan; 4] = [Chan { name: "A", ty: 2 }, Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }]; + +/// RGBA(Z) float channels in chlist (alphabetical) order. +const RGBAZ: [Chan; 5] = + [Chan { name: "A", ty: 2 }, Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }, Chan { name: "Z", ty: 2 }]; + +fn sample_rgba(p: usize, ci: usize) -> Vec { + // One sample per pixel: a small checkerboard-ish pattern with varying depth. Deep + // colours are premultiplied, so the stored RGB values include the 0.5 alpha. + let v = [0.25f32, 0.5, 0.75, 1.0][p % 4]; + match ci { + 0 => vec![0.5], // A + 1 => vec![0.125 * v], // B = straight 0.25v, premultiplied + 2 => vec![0.25 * v], // G = straight 0.5v, premultiplied + 3 => vec![0.5 * v], // R = straight v, premultiplied + _ => vec![10.0 + p as f32], // Z + } +} + +const COUNTS_ONE: &[u32] = &[1; 16]; // for a 4x4 image + +fn rgba_flat(bytes: &[u8]) -> Vec { + let img = decode(bytes).expect("decodes"); + let s = img.to_f32_samples().expect("f32"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgba); + assert_eq!(img.sample_type(), craft_image_codecs::SampleType::F32); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DeepFlattened { .. }))); + s +} + +// --------------------------------------------------------------------------------------------- +// Oracles + +/// One sample per pixel: the flat image is the stored colour un-premultiplied. +#[test] +fn single_sample_matches_flat() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let px = rgba_flat(&bytes); + for p in 0..16 { + let v = [0.25f32, 0.5, 0.75, 1.0][p % 4]; + let (r, g, b, a) = (px[p * 4], px[p * 4 + 1], px[p * 4 + 2], px[p * 4 + 3]); + assert!((r - v).abs() < 1e-6, "r at {p}: {r}"); + assert!((g - 0.5 * v).abs() < 1e-6, "g at {p}: {g}"); + assert!((b - 0.25 * v).abs() < 1e-6, "b at {p}: {b}"); + assert!((a - 0.5).abs() < 1e-6, "a at {p}: {a}"); + } +} + +/// Two samples stored back-to-front: the front (smaller Z) covers the back. Hand-computed: +/// front a=0.5, premultiplied colour (0.5, 0.25, 0); back a=0.25, premultiplied (0.25, 0.25, 0.25). +/// Over (front first): a = 0.5 + (1−0.5)·0.25 = 0.625; c = (0.5 + 0.5·0.25, 0.25 + 0.5·0.25, +/// 0 + 0.5·0.25) = (0.625, 0.375, 0.125); straight = c / a = (1.0, 0.6, 0.2). +#[test] +fn z_order_composites_front_over_back() { + // pixel 0: samples [(z=9, back), (z=1, front)] — the file order is reversed on purpose. + let counts = vec![2u32; 16]; + let sample = |p: usize, ci: usize| -> Vec { + if p != 0 { + let v = sample_rgba(p, ci); + return vec![v[0], v[0]]; + } + match ci { + 0 => vec![0.25, 0.5], // A: back first + 1 => vec![0.25, 0.0], // B + 2 => vec![0.25, 0.25], // G + 3 => vec![0.25, 0.5], // R + _ => vec![9.0, 1.0], // Z + } + }; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&bytes); + let a = 0.5f32 + 0.5 * 0.25; + assert!((px[3] - a).abs() < 1e-6, "alpha {}", px[3]); + assert!((px[0] - 0.625 / a).abs() < 1e-6, "r {}", px[0]); + assert!((px[1] - 0.375 / a).abs() < 1e-6, "g {}", px[1]); + assert!((px[2] - 0.125 / a).abs() < 1e-6, "b {}", px[2]); +} + +/// A fully opaque front sample hides everything behind it. +#[test] +fn opaque_front_hides_back() { + let counts = vec![3u32; 16]; + let sample = |p: usize, ci: usize| -> Vec { + if p != 1 { + let v = sample_rgba(p, ci); + return vec![v[0], v[0], v[0]]; + } + match ci { + 0 => vec![1.0, 0.9, 1.0], // front opaque, middle partial, back opaque + 1 => vec![0.0, 1.0, 1.0], + 2 => vec![1.0, 1.0, 1.0], + 3 => vec![1.0, 0.0, 0.0], + _ => vec![1.0, 5.0, 9.0], + } + }; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&bytes); + let (r, g, b, a) = (px[4], px[5], px[6], px[7]); + assert!((r - 1.0).abs() < 1e-6 && (g - 1.0).abs() < 1e-6 && (b - 0.0).abs() < 1e-6, "colour ({r},{g},{b})"); + assert!((a - 1.0).abs() < 1e-6, "alpha {a}"); +} + +/// Deep EXR is HDR: colour above 1.0 survives the composite (it used to be clamped), and a +/// tiled file whose edge tiles are narrower than the tile size decodes like the scan lines. +#[test] +fn hdr_colour_is_kept_and_edge_tiles_match_scanlines() { + let (w, h) = (7, 5); + let counts: Vec = (0..w * h).map(|p| (p as u32 * 3) % 4).collect(); + let sample = + |p: usize, ci: usize| -> Vec { (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() }; + for compression in [0u8, 1, 2] { + let scan = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, None, &counts, &sample)); + let tiled = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, Some((3, 2)), &counts, &sample)); + assert_eq!(scan, tiled, "compression {compression}"); + // Pixel 1 has 3 opaque samples; the front one (k = 2, Z = 8) has R = 2.125. + assert!((scan[4] - 2.125).abs() < 1e-5, "compression {compression}: R {}", scan[4]); + } +} + +/// Pixels without samples are transparent black, and the structured counts say so. +#[test] +fn empty_pixels_are_transparent() { + let mut counts = vec![0u32; 16]; + counts[5] = 1; + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &|p, ci| { + if p == 5 { vec![[1.0f32, 0.5, 0.5, 0.5, 1.0][ci]] } else { Vec::new() } + }); + let px = rgba_flat(&bytes); + for p in 0..16 { + let base = p * 4; + if p == 5 { + assert!((px[base + 3] - 1.0).abs() < 1e-6); + } else { + assert!(px[base..base + 4].iter().all(|v| *v == 0.0), "pixel {p} not empty"); + } + } + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!(deep.total_samples(), 1); + assert_eq!(deep.sample_range(5).end, 1); +} + +/// RLE, ZIPS and NONE decode to identical pixels and identical structured samples. +#[test] +fn rle_and_zips_match_uncompressed() { + let counts: Vec = (0..16).map(|p| if p % 3 == 0 { 2 } else { 1 }).collect(); + let sample = |p: usize, ci: usize| -> Vec { + if counts[p] == 2 { + match ci { + 0 => vec![0.75, 0.25], + 1 => vec![0.1, 0.2], + 2 => vec![0.3, 0.4], + 3 => vec![0.5, 0.6], + _ => vec![7.0, 3.0], + } + } else { + sample_rgba(p, ci) + } + }; + let base = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let px = rgba_flat(&base); + let deep_base = decode_deep_exr(&base, &Limits::none()).expect("deep"); + for kind in [1u8, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, kind, None, &counts, &sample); + let other = rgba_flat(&bytes); + for (i, (a, b)) in px.iter().zip(other.iter()).enumerate() { + assert!((a - b).abs() < 1e-6, "compression {kind}, sample {i}: {a} vs {b}"); + } + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!(deep.counts, deep_base.counts); + for (a, b) in deep.channels.iter().zip(deep_base.channels.iter()) { + assert_eq!(a.name, b.name); + assert_eq!(a.samples, b.samples, "channel {}", a.name); + } + } +} + +/// Deep tiles decode to the same pixels as deep scanlines of the same data. +#[test] +fn tiles_match_scanlines() { + let counts = vec![1u32, 0, 2, 1, 1, 1, 0, 1, 2, 1, 1, 0, 1, 1, 1, 1]; + let sample = |p: usize, ci: usize| -> Vec { + if counts[p] == 2 { + match ci { + 0 => vec![0.5, 0.5], + 1 => vec![0.2, 0.1], + 2 => vec![0.3, 0.3], + 3 => vec![0.4, 0.4], + _ => vec![2.0, 8.0], + } + } else if counts[p] > 0 { + vec![[0.5, 0.2, 0.4, 0.7, 4.0][ci]] + } else { + Vec::new() + } + }; + let sl = gen_deep(4, 4, &RGBAZ, 0, None, &counts, &sample); + let tiled = gen_deep(4, 4, &RGBAZ, 0, Some((2, 2)), &counts, &sample); + let a = rgba_flat(&sl); + let b = rgba_flat(&tiled); + for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() { + assert!((x - y).abs() < 1e-6, "tile sample {i}: {x} vs {y}"); + } +} + +/// Half-float channels come back as F16 samples with exact values. +#[test] +fn half_channels_round_trip() { + let channels = [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; + let bytes = gen_deep(4, 4, &channels, 2, None, COUNTS_ONE, &|p, _ci| { + let v = f16::from_f32(0.5).to_f32(); + vec![v * (p as f32 + 1.0) / 4.0] + }); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + for c in &deep.channels { + assert_eq!(c.sample, craft_image_codecs::SampleType::F16, "{}", c.name); + } + let a = deep.channel("A").expect("A"); + assert_eq!(a.samples.len(), 16); +} + +/// Without an alpha channel the front-most sample wins, opaque; luminance maps to gray. +#[test] +fn without_alpha_front_sample_is_opaque() { + let channels = [Chan { name: "B", ty: 2 }, Chan { name: "G", ty: 2 }, Chan { name: "R", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let counts = vec![2u32; 16]; + let bytes = gen_deep(4, 4, &channels, 0, None, &counts, &|p, ci| { + if p % 2 == 0 { + match ci { + 0 => vec![0.9, 0.1], + 1 => vec![0.2, 0.8], + 2 => vec![0.4, 0.6], + _ => vec![2.0, 6.0], + } + } else { + vec![[0.0, 0.0, 1.0, 1.0][ci], [0.0, 0.0, 1.0, 9.0][ci]] + } + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Rgb); + let px = img.to_f32_samples().expect("f32"); + // Even pixels: the front sample (z=2) is (r 0.4, g 0.2, b 0.9). + assert!((px[0] - 0.4).abs() < 1e-6 && (px[1] - 0.2).abs() < 1e-6 && (px[2] - 0.9).abs() < 1e-6); +} + +/// Grayscale deep files (Y + Z) flatten to opaque gray, Y + A + Z keeps its alpha: the alpha +/// must land in the GrayA alpha slot, not a luminance copy (that was the bug). +#[test] +fn grayscale_deep_keeps_its_alpha() { + let channels = [Chan { name: "A", ty: 2 }, Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| { + match ci { + 0 => vec![0.5], // A + 1 => vec![0.5], // Y, premultiplied: straight 1.0 × 0.5 + _ => vec![p as f32], // Z + } + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::GrayA); + let px = img.to_f32_samples().expect("f32"); + for (p, s) in px.chunks(2).enumerate() { + let (y, a) = (s[0], s[1]); + assert!((a - 0.5).abs() < 1e-6, "pixel {p}: alpha {a}, want 0.5"); + assert!((y - 1.0).abs() < 1e-6, "pixel {p}: luminance {y}, want the un-premultiplied 1.0"); + } + + // Without an alpha channel the same file is opaque gray. + let channels = [Chan { name: "Y", ty: 2 }, Chan { name: "Z", ty: 2 }]; + let bytes = gen_deep(4, 4, &channels, 0, None, COUNTS_ONE, &|p, ci| match ci { + 0 => vec![0.4], + _ => vec![p as f32], + }); + let img = decode(&bytes).expect("decodes"); + assert_eq!(img.layout(), craft_image_codecs::ChannelLayout::Gray); + let px = img.to_f32_samples().expect("f32"); + for (p, v) in px.iter().enumerate() { + assert!((v - 0.4).abs() < 1e-6, "pixel {p}: {v}, want 0.4"); + } +} + +/// decode_deep returns the counts and channels in file order. +#[test] +fn decode_deep_structure() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + let deep = decode_deep_exr(&bytes, &Limits::none()).expect("deep"); + assert_eq!((deep.width, deep.height), (4, 4)); + assert_eq!(deep.channels.iter().map(|c| c.name.as_str()).collect::>(), ["A", "B", "G", "R", "Z"]); + assert_eq!(deep.counts.len(), 17); + assert_eq!(deep.counts[0], 0); + assert_eq!(deep.total_samples(), 16); + for p in 0..16 { + assert_eq!(deep.sample_range(p).end - deep.sample_range(p).start, 1); + } + assert_eq!(deep.channel("Z").expect("Z").samples[5], 15.0); +} + +// --------------------------------------------------------------------------------------------- +// Hostile input: every damaged file errors, nothing panics. + +fn truncate_sweep(bytes: &[u8]) { + // Cut at every offset (cap the count for speed) — a cut file must error, not panic. + let step = (bytes.len() / 96).max(1); + let mut i = step; + while i < bytes.len() { + let cut = &bytes[..i]; + let _ = decode_deep_exr(cut, &Limits::none()); + let _ = decode(cut); + i += step; + } +} + +#[test] +fn truncated_files_error_never_panic() { + for kind in [0u8, 1, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, kind, None, COUNTS_ONE, &sample_rgba); + truncate_sweep(&bytes); + } + let tiled = gen_deep(4, 4, &RGBAZ, 1, Some((2, 2)), COUNTS_ONE, &sample_rgba); + truncate_sweep(&tiled); +} + +#[test] +fn hostile_sizes_and_offsets_error() { + let bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + // A tiny sample-data budget rejects the file. + let tight = Limits { max_alloc: 64, ..Limits::none() }; + assert!(matches!(decode_deep_exr(&bytes, &tight), Err(CodecError::LimitExceeded(_)))); + // A non-deep file is unsupported for decode_deep. + let flat = craft_image_codecs::encode( + &craft_image_codecs::Image::from_u8(2, 2, craft_image_codecs::ChannelLayout::Rgba, [128; 16].to_vec()).expect("img"), + craft_image_codecs::Format::OpenExr, + &Default::default(), + ) + .expect("encode"); + assert!(matches!(decode_deep_exr(&flat, &Limits::none()), Err(CodecError::Unsupported { .. }))); + // The last chunk's y coordinate destroyed: out of the data window. + let mut broken = bytes.clone(); + let header_end = header_end_of(&broken); + let last_off = u64::from_le_bytes(broken[header_end + 24..header_end + 32].try_into().expect("8")) as usize; + broken[last_off..last_off + 4].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(decode_deep_exr(&broken, &Limits::none()).is_err()); + assert!(decode(&broken).is_err()); + // Two chunks claiming the same block: duplicate offsets. + let mut dup = bytes.clone(); + let header_end = header_end_of(&dup); + let first: [u8; 8] = dup[header_end..header_end + 8].try_into().expect("8"); + dup[header_end + 8..header_end + 16].copy_from_slice(&first); + assert!(matches!(decode_deep_exr(&dup, &Limits::none()), Err(CodecError::Malformed { .. }))); + // An offset pointing past the file. + let mut far = bytes.clone(); + far[header_end..header_end + 8].copy_from_slice(&u64::MAX.to_le_bytes()); + assert!(matches!(decode_deep_exr(&far, &Limits::none()), Err(CodecError::Malformed { .. }))); +} + +/// #4958: a deep tile's count table may be stored at the full tile size, and that size comes +/// from the header alone: nothing ties it to the data window. One 1024×1024 tile over a 4×4 +/// image, its table stored at that size, makes a compressed file of tens of KB whose table +/// inflates to 4 MiB, plus as much again for the copy that undoes the byte split. Under a +/// budget smaller than both the table must be refused before it is allocated; with room for +/// both it decodes. Tables stored at the clipped size, and the full-size edge tables of +/// ordinary tiles, still decode under a small budget. +#[test] +fn deep_tile_count_table_is_admitted_against_max_alloc() { + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + let tight = budget(1 << 20); + let table: u64 = 1024 * 1024 * 4; // the full tile's count table, in bytes + let scan = decode_deep_exr(&gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba), &Limits::none()).expect("scan lines"); + for compression in [0u8, 1, 2] { + let big = gen_deep_tables(4, 4, &RGBAZ, compression, Some((1024, 1024)), COUNTS_ONE, &sample_rgba, true); + assert_eq!(decode_deep_exr(&big, &Limits::none()).expect("full-size table, no limits"), scan, "compression {compression}"); + let got = decode_deep_exr(&big, &tight); + assert!( + matches!(got, Err(CodecError::LimitExceeded(_))), + "compression {compression}: a 4 MiB count table under a 1 MiB budget gave {:?}", + got.map(|d| d.total_samples()) + ); + let flat = craft_image_codecs::decode_with(&big, &craft_image_codecs::DecodeOptions { limits: tight, ..Default::default() }); + assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "compression {compression}: the flat decode gave {:?}", flat.map(|i| i.width())); + // The same tile with its table stored at the clipped size fits the small budget. + let clipped = gen_deep(4, 4, &RGBAZ, compression, Some((1024, 1024)), COUNTS_ONE, &sample_rgba); + assert_eq!(decode_deep_exr(&clipped, &tight).expect("clipped table, 1 MiB budget"), scan, "compression {compression}"); + if compression != 0 { + assert!(big.len() < 1 << 20, "compression {compression}: the file is {} bytes", big.len()); + } + // The table and its scratch copy are admitted together: 8 MiB. + assert_eq!(decode_deep_exr(&big, &budget(2 * table)).expect("room for the table and its copy"), scan, "compression {compression}"); + let got = decode_deep_exr(&big, &budget(2 * table - 1)); + assert!(matches!(got, Err(CodecError::LimitExceeded(_))), "compression {compression}: one byte short gave {:?}", got.map(|d| d.total_samples())); + } + // Ordinary tiles: full-size edge tables decode under the small budget, like clipped ones. + let counts: Vec = (0..35).map(|p| (p * 3) % 4).collect(); + let sample = |p: usize, ci: usize| -> Vec { (0..counts[p]).map(|k| [0.5, 0.25, 0.125 * k as f32, 1.0, 10.0 - k as f32][ci]).collect() }; + for compression in [0u8, 1, 2] { + let full = gen_deep_tables(7, 5, &RGBAZ, compression, Some((3, 2)), &counts, &sample, true); + let clipped = gen_deep(7, 5, &RGBAZ, compression, Some((3, 2)), &counts, &sample); + assert_eq!( + decode_deep_exr(&full, &tight).expect("full-size edge tables"), + decode_deep_exr(&clipped, &tight).expect("clipped edge tables"), + "compression {compression}" + ); + } +} + +/// #4958, the edges of that admission. A tile of the largest size exr accepts (2^30 - 2 a side, +/// a 2^62 byte count table) decodes from a clipped table under every budget, including no limits, +/// where its full size passes the check and is only tried as the data arrives. Each tile is +/// admitted on its own, because its table is freed before the next chunk: three 16×16 tiles over +/// a 48×1 image are all edge tiles, so every full-size table reaches the check (an interior tile's +/// full size equals its clipped size, so it uses the clipped check). They need 2 KiB each for +/// the table and its copy, 6 KiB in all, and decode under a budget of 4096 bytes. Half and uint +/// channels decode after full-size tables like float ones. +#[test] +fn deep_tile_table_admission_edges() { + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + let scan = decode_deep_exr(&gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba), &Limits::none()).expect("scan lines"); + let side = (1 << 30) - 2; + for compression in [0u8, 1, 2] { + let bytes = gen_deep(4, 4, &RGBAZ, compression, Some((side, side)), COUNTS_ONE, &sample_rgba); + for limits in [Limits::none(), Limits::default(), budget(1 << 20)] { + assert_eq!(decode_deep_exr(&bytes, &limits).expect("the largest tile, clipped table"), scan, "compression {compression}, {limits:?}"); + } + } + let counts: Vec = (0..48).map(|p| (p % 3) as u32).collect(); + let half: [Chan; 5] = RGBAZ.map(|c| Chan { ty: 1, ..c }); + let [a, b, g, r, z] = RGBAZ; + let with_id = [a, b, g, r, z, Chan { name: "id", ty: 0 }]; + for channels in [&RGBAZ[..], &half[..], &with_id[..]] { + let sample = |p: usize, ci: usize| -> Vec { (0..counts[p]).map(|k| [0.5, 0.25, 0.125, 1.0, 2.0 + k as f32, 7.0][ci]).collect() }; + for compression in [0u8, 1, 2] { + let full = gen_deep_tables(48, 1, channels, compression, Some((16, 16)), &counts, &sample, true); + let clipped = gen_deep(48, 1, channels, compression, Some((16, 16)), &counts, &sample); + let what = format!("{} channels, compression {compression}", channels.len()); + let want = decode_deep_exr(&clipped, &Limits::none()).expect("clipped tables"); + assert_eq!(decode_deep_exr(&full, &budget(4096)).expect("full-size tables, admitted one at a time"), want, "{what}"); + } + } +} + +/// A one-channel data-window check admits only four bytes per pixel, but a compressed count +/// table and its scratch copy need eight. Cover scanlines, interior tiles and the clipped fallback; +/// the existing multichannel cases have enough pixel allowance to hide this gap. +#[test] +fn clipped_count_tables_include_scratch_in_the_budget() { + let channels = [Chan { name: "Y", ty: 2 }]; + let counts = vec![0u32; 64]; + let sample = |_: usize, _: usize| Vec::new(); + let budget = |max_alloc: u64| Limits { max_alloc, ..Limits::none() }; + for tile in [None, Some((64, 1)), Some((128, 2))] { + for compression in [0u8, 1, 2] { + let bytes = gen_deep(64, 1, &channels, compression, tile, &counts, &sample); + let what = format!("tile {tile:?}, compression {compression}"); + let want = decode_deep_exr(&bytes, &Limits::none()).expect("unlimited decode"); + let required = if compression == 0 { 256 } else { 512 }; + assert_eq!(decode_deep_exr(&bytes, &budget(required)).expect("table and any scratch fit exactly"), want, "{what}"); + assert!(matches!(decode_deep_exr(&bytes, &budget(required - 1)), Err(CodecError::LimitExceeded(_))), "{what}"); + let flat = craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); + assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "{what}"); + if compression == 0 { + assert_eq!(decode_deep_exr(&bytes, &budget(511)).expect("raw table needs no scratch"), want, "{what}"); + } + } + } +} + +/// RLE and ZIPS store incompressible tables raw too. Their actual payload, not the header's +/// compression tag, decides whether a scratch buffer is needed. +#[test] +fn raw_count_tables_need_no_scratch_even_with_compression() { + let channels = [Chan { name: "Y", ty: 2 }]; + let limits = Limits { max_alloc: 4, ..Limits::none() }; + for tile in [None, Some((1, 1)), Some((2, 2))] { + for compression in [0u8, 1, 2] { + let bytes = gen_deep(1, 1, &channels, compression, tile, &[0], &|_, _| Vec::new()); + let header_end = header_end_of(&bytes); + let chunk = u64::from_le_bytes(bytes[header_end..header_end + 8].try_into().expect("chunk offset")) as usize; + let table_size = chunk + if tile.is_some() { 16 } else { 4 }; + let packed = u64::from_le_bytes(bytes[table_size..table_size + 8].try_into().expect("table size")); + assert_eq!(packed, 4, "the four-byte count table is stored raw"); + let want = decode_deep_exr(&bytes, &Limits::none()).expect("unlimited decode"); + assert_eq!(decode_deep_exr(&bytes, &limits).expect("raw table fits"), want, "tile {tile:?}, compression {compression}"); + } + } +} + +/// Walk the attributes like the format prescribes: the offset table starts after the +/// header's terminating zero byte. +fn header_end_of(bytes: &[u8]) -> usize { + let mut p = 8usize; // magic + version + loop { + let Some(nul) = bytes[p..].iter().position(|&b| b == 0) else { panic!("unterminated attribute name") }; + if nul == 0 { + return p + 1; // the empty name terminates the header + } + p += nul + 1; + let Some(t) = bytes[p..].iter().position(|&b| b == 0) else { panic!("unterminated attribute type") }; + p += t + 1; + let size = i32::from_le_bytes(bytes[p..p + 4].try_into().expect("size")) as usize; + p += 4 + size; + } +} + +#[test] +fn inconsistent_count_table_errors() { + // The declared unpacked size disagrees with the count table. + let mut bytes = gen_deep(4, 4, &RGBAZ, 0, None, COUNTS_ONE, &sample_rgba); + // Find the first chunk: the offset table follows the header (find the "version" attr end). + let header_end = header_end_of(&bytes); + let first_chunk = u64::from_le_bytes(bytes[header_end..header_end + 8].try_into().expect("8")) as usize; + // Scanline chunk: y (4), three u64s (24); bump the unpacked size by one. + let at = first_chunk + 4 + 16; + let v = u64::from_le_bytes(bytes[at..at + 8].try_into().expect("8")); + bytes[at..at + 8].copy_from_slice(&(v + 1).to_le_bytes()); + assert!(matches!(decode_deep_exr(&bytes, &Limits::none()), Err(CodecError::Malformed { .. }))); +} + +// --------------------------------------------------------------------------------------------- +// Real reference files: the deep test images of the OpenEXR repository, pinned in +// `xtask/src/corpus_pins.rs` and fetched into `corpus/exr/` by `cargo xtask corpus --exr` +// (cargo feature `corpus`; `cargo xtask test-corpus`). Their `.txt` sidecars upstream list the +// ID manifest of each file, an independent oracle for the u32 ID channels. + +/// One pinned file: name, total samples, most samples in one pixel, and per ID channel the +/// manifest IDs its sidecar lists. +#[cfg(feature = "corpus")] +type RealFile = (&'static str, u64, u64, &'static [(&'static str, &'static [u32])]); + +#[cfg(feature = "corpus")] +const MULTIVARIATE_IDS: &[u32] = &[ + 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, 1672208113, + 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, 2505186565, 2698909330, + 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, 3739501011, 3936563811, 4048943790, +]; + +#[cfg(feature = "corpus")] +const REAL_FILES: &[RealFile] = &[ + ( + "11.deep.exr", + 6056, + 2, + &[("id", &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011])], + ), + ( + "42.deep.exr", + 67605, + 8, + &[ + ("modelid", &[147148503, 1252544961, 1917926828, 3170049167, 3338960663, 3411631724]), + ("materialid", &[489905694, 507819813, 2340321182, 3683581971, 3905155331, 3947898621, 4179825792]), + ], + ), + // 64-bit IDs split over `.id0`/`.id1`: only the sample counts are checked. + ("64.deep.exr", 64543, 6, &[]), + ("multivariate.deep.exr", 64543, 6, &[("id", MULTIVARIATE_IDS)]), + // The same samples as multivariate; its sidecar has no manifest. + ("objectid.deep.exr", 64543, 6, &[]), +]; + +#[cfg(feature = "corpus")] +#[test] +fn real_deep_files_decode() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/exr"); + for &(name, total, max_per_pixel, ids) in REAL_FILES { + let path = dir.join(name); + let bytes = std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e} (run `cargo xtask corpus --exr`)", path.display())); + let deep = decode_deep_exr(&bytes, &Limits::none()).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!((deep.width, deep.height), (256, 256), "{name}"); + assert_eq!(deep.total_samples(), total, "{name}"); + assert_eq!(deep.counts.len(), 256 * 256 + 1, "{name}"); + assert!(deep.counts.windows(2).all(|w| w[0] <= w[1]), "{name}: counts must be cumulative"); + assert_eq!(deep.counts.windows(2).map(|w| w[1] - w[0]).max(), Some(max_per_pixel), "{name}"); + for c in &deep.channels { + assert_eq!(c.samples.len() as u64, total, "{name}: channel {}", c.name); + } + // Every decoded ID is one the sidecar lists, and every listed ID occurs. Samples hold + // u32 IDs as f32 (exact only to 2^24), so compare at f32 precision. + for &(channel, manifest) in ids { + let c = deep.channel(channel).unwrap_or_else(|| panic!("{name}: no channel {channel}")); + let want: std::collections::BTreeSet = manifest.iter().map(|&id| (id as f32).to_bits()).collect(); + let got: std::collections::BTreeSet = c.samples.iter().map(|v| v.to_bits()).collect(); + assert_eq!(got, want, "{name}: channel {channel}"); + } + // The flat decode composites the same samples. + let img = decode(&bytes).unwrap_or_else(|e| panic!("{name}: {e}")); + assert_eq!((img.width(), img.height()), (deep.width, deep.height), "{name}"); + assert_eq!(img.to_f32_samples().expect("f32").len(), 256 * 256 * 4, "{name}"); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DeepFlattened { .. })), "{name}"); + } +} + +/// `gen_deep` for a cropped render: the data window sits at `(dwx, dwy)` inside a larger +/// display window of `sw × sh` (the stage the renderer composed onto). Scanline chunk Y +/// coordinates are absolute display rows, so they carry the data-window offset. +#[allow(clippy::too_many_arguments)] +fn gen_deep_cropped( + w: usize, + h: usize, + dwx: i32, + dwy: i32, + sw: usize, + sh: usize, + channels: &[Chan], + counts: &[u32], + sample: &dyn Fn(usize, usize) -> Vec, +) -> Vec { + let mut out = Vec::new(); + out.extend_from_slice(&0x0131_2f76u32.to_le_bytes()); + out.extend_from_slice(&(2u32 | 0x800).to_le_bytes()); + + let mut chlist = Vec::new(); + for c in channels { + chlist.extend_from_slice(c.name.as_bytes()); + chlist.push(0); + chlist.extend_from_slice(&c.ty.to_le_bytes()); + chlist.push(0); + chlist.extend_from_slice(&[0u8; 3]); + chlist.extend_from_slice(&1i32.to_le_bytes()); + chlist.extend_from_slice(&1i32.to_le_bytes()); + } + chlist.push(0); + attr(&mut out, "channels", "chlist", &chlist); + attr(&mut out, "compression", "compression", &[0u8]); + // box2i is inclusive: the windows end at position + size - 1. + let dw: Vec = [dwx, dwy, dwx + w as i32 - 1, dwy + h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "dataWindow", "box2i", &dw); + let disp: Vec = [0i32, 0, sw as i32 - 1, sh as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect(); + attr(&mut out, "displayWindow", "box2i", &disp); + attr(&mut out, "lineOrder", "lineOrder", &[0]); + attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes()); + attr(&mut out, "screenWindowCenter", "v2f", &[0f32.to_le_bytes(), 0f32.to_le_bytes()].concat()); + attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes()); + attr(&mut out, "type", "string", b"deepscanline"); + attr(&mut out, "version", "int", &1i32.to_le_bytes()); + out.push(0); + + // One scanline block per data row; the pixel indexing stays data-relative. + let mut chunks: Vec> = Vec::new(); + for y in 0..h { + let pixels: Vec = (y * w..y * w + w).collect(); + let mut c = Vec::new(); + c.extend_from_slice(&(dwy + y as i32).to_le_bytes()); + let mut table = Vec::with_capacity(pixels.len() * 4); + let mut acc = 0u32; + for &p in &pixels { + acc += counts[p]; + table.extend_from_slice(&acc.to_le_bytes()); + } + let mut data = Vec::new(); + for (ci, _) in channels.iter().enumerate() { + for &p in &pixels { + for v in sample(p, ci) { + data.extend_from_slice(&v.to_le_bytes()); + } + } + } + c.extend_from_slice(&(table.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&(data.len() as u64).to_le_bytes()); + c.extend_from_slice(&table); + c.extend_from_slice(&data); + chunks.push(c); + } + let mut pos = out.len() + chunks.len() * 8; + let mut table = Vec::new(); + for c in &chunks { + table.extend_from_slice(&(pos as u64).to_le_bytes()); + pos += c.len(); + } + out.extend_from_slice(&table); + for c in chunks { + out.extend_from_slice(&c); + } + out +} + +/// A cropped render opens on the full display-window stage: MY27-style renders carry a +/// `display_window` of the full frame (e.g. 4000×3000) while the deep data window is the +/// render pass's crop inside it. The image must open at stage size with the data at its +/// crop offset, transparent elsewhere — not cropped to the data window. +#[test] +fn cropped_deep_data_opens_on_the_display_stage() { + let (w, h, dwx, dwy, sw, sh) = (24usize, 16usize, 8i32, 6i32, 48usize, 32usize); + let counts = vec![1u32; w * h]; + let sample = |p: usize, ci: usize| -> Vec { + // chlist order (RGBA_NO_Z): A, B, G, R. + let x = p % w; + let y = p / w; + match ci { + 0 => vec![1.0], // A + 1 => vec![0.5], // B + 2 => vec![y as f32 / h as f32], // G + _ => vec![x as f32 / w as f32], // R + } + }; + let bytes = gen_deep_cropped(w, h, dwx, dwy, sw, sh, &RGBA_NO_Z, &counts, &sample); + let img = decode(&bytes).expect("cropped deep EXR decodes"); + assert_eq!((img.width(), img.height()), (sw as u32, sh as u32), "opens on the display window stage"); + assert!(img.warnings.iter().any(|w| matches!(w, craft_image_codecs::DecodeWarning::DisplayWindowStage { origin, .. } if *origin == (dwx, dwy)))); + + // The data sits at its crop offset: the first data pixel's green channel carries its + // data-row fraction, everything outside the data region stays transparent black. + let s = img.to_f32_samples().expect("f32"); + let nc = s.len() / (sw * sh); + let px = |x: usize, y: usize| -> Vec { s[y * sw * nc + x * nc..y * sw * nc + x * nc + nc].to_vec() }; + let first = px(dwx as usize, dwy as usize); + assert!( + (first[0] - 0.0).abs() < 1e-5 && (first[1] - 0.0).abs() < 1e-5 && (first[2] - 0.5).abs() < 1e-5 && (first[3] - 1.0).abs() < 1e-5, + "first data pixel at the offset: {first:?}" + ); + let last = px(dwx as usize + w - 1, dwy as usize + h - 1); + assert!( + (last[0] - (w - 1) as f32 / w as f32).abs() < 1e-5 && (last[1] - (h - 1) as f32 / h as f32).abs() < 1e-5, + "last data pixel keeps its coordinates: {last:?}" + ); + let outside = px(0, 0); + assert_eq!(outside, vec![0.0, 0.0, 0.0, 0.0], "outside the data region: transparent"); +} diff --git a/crates/image-codecs/tests/it/detect_caps.rs b/crates/image-codecs/tests/it/detect_caps.rs new file mode 100644 index 000000000..640772c83 --- /dev/null +++ b/crates/image-codecs/tests/it/detect_caps.rs @@ -0,0 +1,309 @@ +//! Magic-number detection, extensions, and the capability table +//! (including the symmetric read/write guarantee). + +use crate::common; +use common::*; +use craft_image_codecs::*; + +// --------------------------------------------------------------------------- +// Symmetric guarantee +// --------------------------------------------------------------------------- + +#[test] +fn every_enabled_format_is_symmetric_or_whitelisted() { + for f in Format::ALL { + let c = caps(f); + if !(c.read || c.write) { + continue; + } + if c.read && c.write { + continue; + } + let exc = ASYMMETRIC_EXCEPTIONS.iter().find(|(g, _)| *g == f); + let (_, reason) = exc.unwrap_or_else(|| panic!("{f:?} is enabled but not symmetric (read={}, write={})", c.read, c.write)); + assert!(reason.len() > 20, "{f:?} exception needs a real reason"); + } +} + +#[test] +fn exceptions_are_documented_and_unique() { + for (i, (f, reason)) in ASYMMETRIC_EXCEPTIONS.iter().enumerate() { + assert!(!reason.is_empty()); + assert!(ASYMMETRIC_EXCEPTIONS[i + 1..].iter().all(|(g, _)| g != f)); + } +} + +#[test] +fn never_write_only_without_exception() { + // Stronger form: no format may be writable-but-unreadable unless listed. + for f in writable_formats() { + if !caps(f).read { + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(g, _)| *g == f), "{f:?}"); + } + } +} + +#[test] +fn default_build_formats_all_symmetric() { + let expected = [ + Format::Png, + Format::Jpeg, + Format::Tiff, + Format::WebP, + Format::Gif, + Format::Bmp, + Format::Tga, + Format::Ico, + Format::Pnm, + Format::Qoi, + Format::OpenExr, + Format::Hdr, + Format::Fits, + Format::Dpx, + Format::Cineon, + Format::Iff, + ]; + for f in expected { + assert!(caps(f).read && caps(f).write, "{f:?}"); + } +} + +#[test] +fn avif_never_readable() { + assert!(!caps(Format::Avif).read); + assert_eq!(caps(Format::Avif).write, cfg!(feature = "avif")); + assert!(matches!(decode_as(Format::Avif, b"\0\0\0\x1cftypavif"), Err(CodecError::Unsupported { .. }))); +} + +#[test] +fn heif_is_detected_always_and_readable_only_with_the_feature() { + let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; + assert_eq!(detect(header), Some(Format::Heif)); + assert_eq!(from_extension("IMG_0001.HEIC"), Some(Format::Heif)); + assert_eq!(caps(Format::Heif).read, cfg!(feature = "heif")); + assert!(!caps(Format::Heif).write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Heif)); + // Either way a bare header is an error, never a panic. + assert!(decode_as(Format::Heif, header).is_err()); +} + +#[test] +fn detect_heif_brands() { + assert_eq!(detect(b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"), Some(Format::Heif)); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1heic"), Some(Format::Heif), "generic major brand"); + assert_eq!(detect(b"\0\0\0\x18ftypheix\0\0\0\0mif1heix"), Some(Format::Heif), "10-bit"); + assert_eq!(detect(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"), Some(Format::Heif), "image sequence"); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1avif"), Some(Format::Avif), "AVIF wins over the generic brand"); + assert_eq!(detect(b"\0\0\0\x18ftypisom\0\0\0\0isommp41"), None, "mp4 is not HEIF"); + assert_eq!(detect(b"\0\0\0\x14ftypqt \0\0\0\0qt "), None, "QuickTime is not HEIF"); +} + +#[test] +fn caps_are_internally_consistent() { + for f in Format::ALL { + let c = caps(f); + assert!(!c.depths.is_empty() && !c.layouts.is_empty(), "{f:?}"); + assert_eq!(c.alpha, c.layouts.iter().any(|l| l.has_alpha()), "{f:?} alpha flag vs layouts"); + // DICOM's registered media type is `application/dicom`; every other format is an image. + let mime_ok = f.mime_type().starts_with("image/") || (f == Format::Dicom && f.mime_type() == "application/dicom"); + assert!(!f.extensions().is_empty() && !f.name().is_empty() && mime_ok, "{f:?}"); + } +} + +#[test] +fn caps_function_matches_method() { + for f in Format::ALL { + assert_eq!(caps(f), f.caps()); + } +} + +// --------------------------------------------------------------------------- +// Detection +// --------------------------------------------------------------------------- + +#[test] +fn detect_encoded_output_of_every_format() { + for f in writable_formats() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8); + let bytes = encode(&img, f, &EncodeOptions::default()).unwrap(); + assert_eq!(detect(&bytes), Some(f)); + } +} + +#[test] +fn detect_magic_png() { + assert_eq!(detect(b"\x89PNG\r\n\x1a\n\0\0"), Some(Format::Png)); +} +#[test] +fn detect_magic_jpeg() { + assert_eq!(detect(&[0xFF, 0xD8, 0xFF, 0xE0]), Some(Format::Jpeg)); + assert_eq!(detect(&[0xFF, 0xD8]), None); +} +#[test] +fn detect_magic_tiff_both_endians_and_bigtiff() { + assert_eq!(detect(b"II*\0\x08\0\0\0"), Some(Format::Tiff)); + assert_eq!(detect(b"MM\0*\0\0\0\x08"), Some(Format::Tiff)); + assert_eq!(detect(b"II+\0\x08\0\0\0"), Some(Format::Tiff)); + assert_eq!(detect(b"MM\0+\0\x08\0\0"), Some(Format::Tiff)); +} +#[test] +fn detect_magic_webp() { + assert_eq!(detect(b"RIFF\x10\0\0\0WEBPVP8L"), Some(Format::WebP)); + assert_eq!(detect(b"RIFF\x10\0\0\0WAVEfmt "), None); +} +#[test] +fn detect_magic_gif() { + assert_eq!(detect(b"GIF87a\x01\0"), Some(Format::Gif)); + assert_eq!(detect(b"GIF89a\x01\0"), Some(Format::Gif)); + assert_eq!(detect(b"GIF90a\x01\0"), None); +} +#[test] +fn detect_magic_bmp() { + let mut b = b"BM".to_vec(); + b.resize(54, 0); + assert_eq!(detect(&b), Some(Format::Bmp)); + assert_eq!(detect(b"BM"), None); +} +#[test] +fn detect_magic_ico() { + assert_eq!(detect(&[0, 0, 1, 0, 1, 0, 16, 16]), Some(Format::Ico)); + assert_eq!(detect(&[0, 0, 1, 0, 0, 0]), None, "zero images"); +} +#[test] +fn detect_magic_pnm_family() { + for m in [&b"P1\n"[..], b"P2 ", b"P3\t", b"P4\n", b"P5\r", b"P6\n", b"P7\n", b"PF\n", b"Pf\n"] { + assert_eq!(detect(m), Some(Format::Pnm), "{:?}", std::str::from_utf8(m)); + } + assert_eq!(detect(b"P8\n"), None); + assert_eq!(detect(b"P6x"), None); +} +#[test] +fn detect_magic_qoi() { + assert_eq!(detect(b"qoif\0\0\0\x01\0\0\0\x01\x03\0"), Some(Format::Qoi)); +} +#[test] +fn detect_magic_exr() { + assert_eq!(detect(&[0x76, 0x2F, 0x31, 0x01, 2, 0, 0, 0]), Some(Format::OpenExr)); +} +#[test] +fn detect_magic_hdr() { + assert_eq!(detect(b"#?RADIANCE\n"), Some(Format::Hdr)); + assert_eq!(detect(b"#?RGBE\n"), Some(Format::Hdr)); +} +#[test] +fn detect_magic_cineon() { + let mut b = vec![0u8; 208]; + b[0..4].copy_from_slice(&[0x80, 0x2A, 0x5F, 0xD7]); + assert_eq!(detect(&b), Some(Format::Cineon)); + assert_eq!(from_extension("scan.CIN"), Some(Format::Cineon)); + assert!(caps(Format::Cineon).read && caps(Format::Cineon).write, "Cineon is read and written"); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Cineon)); + // A bare magic is an error, never a panic. + assert!(decode_as(Format::Cineon, &b).is_err()); +} + +#[test] +fn detect_magic_iff() { + // FORM + a four-byte length + the bitmap type. + assert_eq!(detect(b"FORM\0\0\0\x14ILBM"), Some(Format::Iff)); + assert_eq!(detect(b"FORM\0\0\0\x14PBM "), Some(Format::Iff)); + assert_eq!(from_extension("art.LBM"), Some(Format::Iff)); + assert_eq!(from_extension("art.ilbm"), Some(Format::Iff)); + assert!(caps(Format::Iff).read && caps(Format::Iff).write, "IFF is read and written"); + assert!(caps(Format::Iff).alpha); + assert!(caps(Format::Iff).layouts.contains(&craft_image_codecs::ChannelLayout::Rgba)); + assert!(!ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Iff)); + assert_eq!(Format::Iff.max_dimensions(), Some((65535, 65535))); + // A bare FORM without a bitmap type is not IFF. + assert_eq!(detect(b"FORM\0\0\0\x14AIFF"), None); + assert!(decode_as(Format::Iff, b"FORM\0\0\0\x14ILBM").is_err()); +} +#[test] +fn detect_magic_avif() { + assert_eq!(detect(b"\0\0\0\x1cftypavif\0\0\0\0avifmif1miaf"), Some(Format::Avif)); + assert_eq!(detect(b"\0\0\0\x1cftypavis\0\0\0\0avismsf1miaf"), Some(Format::Avif)); + assert_eq!(detect(b"\0\0\0\x18ftypmif1\0\0\0\0mif1avif"), Some(Format::Avif), "compatible brand"); + assert_eq!(detect(b"\0\0\0\x18ftypisom\0\0\0\0isommp41"), None, "mp4 is not avif"); +} +#[test] +fn detect_magic_dicom() { + let mut b = vec![0u8; 128]; + b.extend_from_slice(b"DICM"); + assert_eq!(detect(&b), Some(Format::Dicom)); + assert_eq!(from_extension("scan.DCM"), Some(Format::Dicom)); + assert_eq!(from_extension("scan.dc3"), Some(Format::Dicom)); + assert!(caps(Format::Dicom).read && !caps(Format::Dicom).write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Dicom)); + // A bare magic is an error, never a panic. + assert!(decode_as(Format::Dicom, &b).is_err()); +} + +#[test] +fn detect_tga_heuristic() { + // Uncompressed true-colour 2x2, 24 bpp. + let mut b = vec![0u8, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 24, 0]; + b.extend_from_slice(&[0; 12]); + assert_eq!(detect(&b), Some(Format::Tga)); + let mut bad = b.clone(); + bad[2] = 7; // invalid image type + assert_eq!(detect(&bad), None); + let mut bad = b.clone(); + bad[16] = 13; // invalid depth + assert_eq!(detect(&bad), None); +} +#[test] +fn detect_negatives() { + assert_eq!(detect(b""), None); + assert_eq!(detect(b"x"), None); + assert_eq!(detect(b"hello world, this is not an image"), None); + assert_eq!(detect(b"%PDF-1.7\n"), None); + assert_eq!(detect(b"PK\x03\x04"), None); + assert_eq!(detect(&[0u8; 64]), None); +} +#[test] +fn detect_random_bytes_rarely_matches_tga() { + let mut rng = Rng::new(5); + let hits = (0..2000).filter(|_| detect(&rng.bytes(64)) == Some(Format::Tga)).count(); + assert!(hits < 10, "{hits} false TGA positives"); +} +#[test] +fn decode_unknown_is_error() { + assert!(matches!(decode(b"garbage garbage"), Err(CodecError::UnknownFormat))); +} + +// --------------------------------------------------------------------------- +// Extensions +// --------------------------------------------------------------------------- + +#[test] +fn extensions_map_back() { + for f in Format::ALL { + for e in f.extensions() { + assert_eq!(from_extension(e), Some(f), "{e}"); + assert_eq!(from_extension(&e.to_uppercase()), Some(f)); + assert_eq!(from_extension(&format!(".{e}")), Some(f)); + assert_eq!(from_extension(&format!("dir/file.name.{e}")), Some(f)); + } + } +} +#[test] +fn extension_specific_cases() { + assert_eq!(from_extension("JPG"), Some(Format::Jpeg)); + assert_eq!(from_extension("tif"), Some(Format::Tiff)); + assert_eq!(from_extension("pfm"), Some(Format::Pnm)); + assert_eq!(from_extension("pam"), Some(Format::Pnm)); + assert_eq!(from_extension("FIT"), Some(Format::Fits)); + assert_eq!(from_extension("m31.fits"), Some(Format::Fits)); + assert_eq!(from_extension("exr"), Some(Format::OpenExr)); + assert_eq!(from_extension("psd"), None); + assert_eq!(from_extension(""), None); + assert_eq!(from_extension("png.bak"), None); +} +#[test] +fn extensions_unique_across_formats() { + let mut all: Vec<&str> = Format::ALL.iter().flat_map(|f| f.extensions().iter().copied()).collect(); + let n = all.len(); + all.sort(); + all.dedup(); + assert_eq!(all.len(), n); +} diff --git a/crates/image-codecs/tests/it/dpx.rs b/crates/image-codecs/tests/it/dpx.rs new file mode 100644 index 000000000..4d4835cc7 --- /dev/null +++ b/crates/image-codecs/tests/it/dpx.rs @@ -0,0 +1,521 @@ +//! DPX (SMPTE 268M) read and write, against fixtures packed here independently of the codec: +//! every descriptor, bit depth and packing it reads, both byte orders, the header fields kept +//! through a round trip, the bit-depth choice on export and the files it refuses (#1049). + +use craft_image_codecs::*; + +const HEADER: usize = 2048; +const ELEMENT0: usize = 780; + +/// A DPX file built field by field. +#[derive(Clone)] +struct Fixture { + big: bool, + width: u32, + height: u32, + descriptor: u8, + bits: u8, + packing: u16, + encoding: u16, + signed: bool, + transfer: u8, + colorimetric: u8, + orientation: u16, + elements: u16, + strings: Vec<((usize, usize), &'static str)>, + frame_position: u32, + frame_rate: f32, + time_code: u32, + /// Code values, interleaved, top line first. + codes: Vec, +} + +impl Fixture { + fn new(descriptor: u8, bits: u8, packing: u16, width: u32, height: u32) -> Self { + let channels = channels(descriptor); + let max = (1u32 << bits) - 1; + // Spread over the whole range, ends included. + let n = (width * height) as usize * channels; + let codes = (0..n) + .map(|i| { + if i == 0 { + 0 + } else if i == 1 { + max as u16 + } else { + ((i as u64 * 2654435761) % (u64::from(max) + 1)) as u16 + } + }) + .collect(); + Fixture { + big: true, + width, + height, + descriptor, + bits, + packing, + encoding: 0, + signed: false, + transfer: 255, + colorimetric: 255, + orientation: 0, + elements: 1, + strings: Vec::new(), + frame_position: u32::MAX, + frame_rate: f32::from_bits(u32::MAX), + time_code: u32::MAX, + codes, + } + } + + fn u16(&self, v: u16) -> [u8; 2] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + + fn u32(&self, v: u32) -> [u8; 4] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + + fn line(&self, codes: &[u16]) -> Vec { + let mut out = Vec::new(); + match (self.bits, self.packing) { + (8, _) => out.extend(codes.iter().map(|&c| c as u8)), + (16, _) => out.extend(codes.iter().flat_map(|&c| self.u16(c))), + (10, 1 | 2) => { + let base = if self.packing == 1 { 22 } else { 20 }; + for chunk in codes.chunks(3) { + let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (base - 10 * i as u32))); + out.extend(self.u32(w)); + } + } + (12, 1) => out.extend(codes.iter().flat_map(|&c| self.u16(c << 4))), + _ => { + // One bit stream, high bits first, cut into 32-bit words. + let mut bits: Vec = codes.iter().flat_map(|&c| (0..self.bits).rev().map(move |b| (c >> b) & 1 == 1)).collect(); + bits.resize(bits.len().div_ceil(32) * 32, false); + for word in bits.chunks(32) { + out.extend(self.u32(word.iter().fold(0u32, |w, &b| (w << 1) | u32::from(b)))); + } + } + } + out.resize(out.len().div_ceil(4) * 4, 0); + out + } + + fn bytes(&self) -> Vec { + let mut b = vec![0u8; HEADER]; + let mut put = |at: usize, v: &[u8]| b[at..at + v.len()].copy_from_slice(v); + put(0, if self.big { b"SDPX" } else { b"XPDS" }); + put(4, &self.u32(HEADER as u32)); + put(8, b"V2.0"); + put(24, &self.u32(1664)); + put(28, &self.u32(384)); + put(768, &self.u16(self.orientation)); + put(770, &self.u16(self.elements)); + put(772, &self.u32(self.width)); + put(776, &self.u32(self.height)); + for el in 0..usize::from(self.elements.min(8)) { + let e = ELEMENT0 + el * 72; + put(e, &self.u32(u32::from(self.signed))); + put(e + 20, &[self.descriptor, self.transfer, self.colorimetric, self.bits]); + put(e + 24, &self.u16(self.packing)); + put(e + 26, &self.u16(self.encoding)); + put(e + 28, &self.u32(HEADER as u32)); + } + for &((at, len), s) in &self.strings { + assert!(s.len() < len); + put(at, s.as_bytes()); + } + put(1712, &self.u32(self.frame_position)); + put(1724, &self.u32(self.frame_rate.to_bits())); + put(1920, &self.u32(self.time_code)); + let per_line = self.width as usize * channels(self.descriptor); + for line in self.codes.chunks(per_line) { + b.extend(self.line(line)); + } + b + } +} + +fn channels(descriptor: u8) -> usize { + match descriptor { + 6 => 1, + 50 => 3, + _ => 4, + } +} + +fn layout(descriptor: u8) -> ChannelLayout { + match descriptor { + 6 => ChannelLayout::Gray, + 50 => ChannelLayout::Rgb, + _ => ChannelLayout::Rgba, + } +} + +/// The 16-bit sample a code value opens as: `round(c × 65535 / (2^b − 1))`. +fn expected(c: u16, bits: u8) -> u16 { + (f64::from(c) * 65535.0 / f64::from((1u32 << bits) - 1)).round() as u16 +} + +/// Reads the code values of a big-endian file we wrote (packing 0 at 8/16 bits, 1 at 10/12). +fn written_codes(b: &[u8]) -> (u8, u16, Vec) { + let be16 = |at: usize| u16::from_be_bytes([b[at], b[at + 1]]); + let be32 = |at: usize| u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]); + let (w, h) = (be32(772) as usize, be32(776) as usize); + let nc = channels(b[ELEMENT0 + 20]); + let (bits, packing) = (b[ELEMENT0 + 23], be16(ELEMENT0 + 24)); + let off = be32(ELEMENT0 + 28) as usize; + let per_line = w * nc; + let stride = match bits { + 10 => per_line.div_ceil(3) * 4, + _ => (per_line * if bits == 8 { 1 } else { 2 }).div_ceil(4) * 4, + }; + let mut codes = Vec::new(); + for y in 0..h { + let line = &b[off + y * stride..]; + for i in 0..per_line { + codes.push(match bits { + 8 => u16::from(line[i]), + 10 => ((be32_at(line, i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3FF) as u16, + 12 => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]) >> 4, + _ => u16::from_be_bytes([line[i * 2], line[i * 2 + 1]]), + }); + } + } + (bits, packing, codes) +} + +fn be32_at(b: &[u8], at: usize) -> u32 { + u32::from_be_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) +} + +fn opts(bits: Option) -> EncodeOptions { + EncodeOptions { dpx_bits: bits, ..EncodeOptions::default() } +} + +const COMBOS: [(u8, u16); 7] = [(8, 0), (10, 0), (10, 1), (10, 2), (12, 0), (12, 1), (16, 0)]; + +#[test] +fn every_descriptor_depth_and_packing_opens_with_its_model_and_values() { + for descriptor in [6, 50, 51] { + for (bits, packing) in COMBOS { + // Widths that leave partly filled words at the end of a line. + let fx = Fixture::new(descriptor, bits, packing, 5, 3); + let img = decode(&fx.bytes()).unwrap_or_else(|e| panic!("{descriptor} {bits}/{packing}: {e}")); + let what = format!("descriptor {descriptor}, {bits} bits, packing {packing}"); + assert_eq!(detect(&fx.bytes()), Some(Format::Dpx)); + assert_eq!((img.width(), img.height()), (5, 3), "{what}"); + assert_eq!(img.layout(), layout(descriptor), "{what}"); + assert_eq!(img.layout().has_alpha(), descriptor == 51, "{what}"); + match bits { + 8 => assert_eq!(img.as_u8().unwrap(), fx.codes.iter().map(|&c| c as u8).collect::>(), "{what}"), + 16 => assert_eq!(img.to_u16_samples().unwrap(), fx.codes, "{what}"), + _ => assert_eq!(img.to_u16_samples().unwrap(), fx.codes.iter().map(|&c| expected(c, bits)).collect::>(), "{what}"), + } + } + } +} + +#[test] +fn big_and_little_endian_files_open_identically() { + for (bits, packing) in COMBOS { + let be = Fixture::new(50, bits, packing, 4, 2); + let le = Fixture { big: false, ..be.clone() }; + assert_eq!(&le.bytes()[..4], b"XPDS"); + assert_eq!(decode(&be.bytes()).unwrap(), decode(&le.bytes()).unwrap(), "{bits} bits, packing {packing}"); + } +} + +#[test] +fn ten_bit_code_value_opens_as_the_rounded_16_bit_value() { + let mut fx = Fixture::new(6, 10, 1, 1, 1); + fx.codes = vec![512]; + assert_eq!(decode(&fx.bytes()).unwrap().to_u16_samples().unwrap(), vec![(512.0f64 * 65535.0 / 1023.0).round() as u16]); +} + +#[test] +fn ten_and_twelve_bit_frames_written_back_at_their_depth_keep_their_code_values() { + for (bits, choice) in [(10, DpxBits::Bits10), (12, DpxBits::Bits12)] { + for packing in [0, 1] { + let fx = Fixture::new(50, bits, packing, 7, 3); + let out = encode(&decode(&fx.bytes()).unwrap(), Format::Dpx, &opts(Some(choice))).unwrap(); + let (wbits, wpacking, codes) = written_codes(&out); + assert_eq!((wbits, wpacking), (bits, 1)); + assert_eq!(codes, fx.codes, "{bits} bits, packing {packing}"); + } + } +} + +#[test] +fn sixteen_bit_frames_round_trip_losslessly_and_files_start_with_sdpx() { + let fx = Fixture { big: false, ..Fixture::new(51, 16, 0, 6, 4) }; + let img = decode(&fx.bytes()).unwrap(); + let out = encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap(); + assert_eq!(&out[..4], b"SDPX"); + let (bits, packing, codes) = written_codes(&out); + assert_eq!((bits, packing, codes), (16, 0, fx.codes)); + assert_eq!(decode(&out).unwrap().to_u16_samples(), img.to_u16_samples()); +} + +#[test] +fn log_frames_report_their_encoding_and_keep_the_plain_mapping() { + let mut fx = Fixture::new(50, 10, 1, 3, 2); + (fx.transfer, fx.colorimetric) = (3, 1); + let img = decode(&fx.bytes()).unwrap(); + assert_eq!(img.warnings, vec![DecodeWarning::DpxEncoding { transfer: 3, colorimetric: 1 }]); + let note = img.warnings[0].to_string(); + assert!(note.contains("logarithmic (3)") && note.contains("printing density (1)"), "{note}"); + assert_eq!(img.to_u16_samples().unwrap(), fx.codes.iter().map(|&c| expected(c, 10)).collect::>()); +} + +#[test] +fn only_the_first_of_several_image_elements_is_imported() { + let mut fx = Fixture::new(6, 8, 0, 2, 2); + fx.elements = 2; + let img = decode(&fx.bytes()).unwrap(); + assert_eq!(img.warnings.iter().map(ToString::to_string).collect::>(), ["only the first of 2 image elements was imported"]); +} + +#[test] +fn listed_header_fields_survive_a_round_trip() { + let mut fx = Fixture::new(50, 10, 1, 2, 2); + fx.strings = vec![((160, 100), "Resolve 20.1"), ((260, 200), "Reel 7"), ((460, 200), "(c) 2026 Studio"), ((136, 24), "2026:10:12:09:30:00UTC")]; + (fx.transfer, fx.colorimetric, fx.frame_position, fx.frame_rate, fx.time_code) = (3, 1, 86400, 23.976, 0x0102_0304); + let img = decode(&fx.bytes()).unwrap(); + for (k, v) in [ + ("DPX:Creator", "Resolve 20.1"), + ("DPX:Project", "Reel 7"), + ("DPX:Copyright", "(c) 2026 Studio"), + ("DPX:CreationTime", "2026:10:12:09:30:00UTC"), + ("DPX:Transfer", "3"), + ("DPX:Colorimetric", "1"), + ("DPX:FramePosition", "86400"), + ("DPX:FrameRate", "23.976"), + ("DPX:TimeCode", "01:02:03:04"), + ] { + assert!(img.meta.text.contains(&(k.into(), v.into())), "{k} = {v} in {:?}", img.meta.text); + } + let out = encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits10))).unwrap(); + let src = fx.bytes(); + // Field by field: the strings, the element's encoding codes, film and TV fields. + for (at, len) in [(160, 100), (260, 200), (460, 200), (136, 24), (ELEMENT0 + 21, 2), (1712, 4), (1724, 4), (1920, 4)] { + assert_eq!(out[at..at + len], src[at..at + len], "field at {at}"); + } + assert_eq!(decode(&out).unwrap().meta.text, img.meta.text); +} + +#[test] +fn chosen_bit_depth_sets_the_header_and_packing() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000).collect::>()).unwrap(); + for (choice, bits, packing) in [(DpxBits::Bits8, 8, 0), (DpxBits::Bits10, 10, 1), (DpxBits::Bits12, 12, 1), (DpxBits::Bits16, 16, 0)] { + let out = encode(&img, Format::Dpx, &opts(Some(choice))).unwrap(); + assert_eq!((out[ELEMENT0 + 23], u16::from_be_bytes([out[ELEMENT0 + 24], out[ELEMENT0 + 25]])), (bits, packing), "{choice:?}"); + let back = decode(&out).unwrap(); + assert_eq!((back.width(), back.height(), back.layout()), (5, 2, ChannelLayout::Rgb)); + } + // Without a choice the depth follows the image. + let gray8 = Image::from_u8(3, 1, ChannelLayout::Gray, vec![0, 128, 255]).unwrap(); + assert_eq!(encode(&gray8, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 8); + assert_eq!(encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 16); + let float = img.convert(ChannelLayout::Rgb, SampleType::F32); + assert_eq!(encode(&float, Format::Dpx, &EncodeOptions::default()).unwrap()[ELEMENT0 + 23], 16); + assert!(fidelity_warnings(&float, Format::Dpx).contains(&FidelityWarning::DepthReduced { from: SampleType::F32, to: SampleType::U16 })); +} + +/// The depth warnings of `img` written as `format` with `o`. +fn depth_warnings(img: &Image, format: Format, o: &EncodeOptions) -> Vec { + fidelity_warnings_with(img, format, o) + .into_iter() + .filter(|w| matches!(w, FidelityWarning::DepthReduced { .. } | FidelityWarning::BitsReduced { .. })) + .collect() +} + +#[test] +fn saving_sixteen_bits_at_ten_or_twelve_warns_with_the_stored_bits() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000 + 1).collect::>()).unwrap(); + for (choice, bits) in [(DpxBits::Bits10, 10), (DpxBits::Bits12, 12)] { + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(choice))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits }]); + assert_eq!(FidelityWarning::BitsReduced { from: SampleType::U16, bits }.to_string(), format!("16-bit will be reduced to {bits}-bit")); + // Float samples name the stored bits too, not the 16-bit words they are packed in. + let float = img.convert(ChannelLayout::Rgb, SampleType::F32); + assert_eq!(depth_warnings(&float, Format::Dpx, &opts(Some(choice))), [FidelityWarning::BitsReduced { from: SampleType::F32, bits }]); + // 8-bit samples widen into 10 and 12 bits without loss. + let eight = img.convert(ChannelLayout::Rgb, SampleType::U8); + assert_eq!(depth_warnings(&eight, Format::Dpx, &opts(Some(choice))), []); + } + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits16))), []); + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(None)), []); + assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }]); + assert_eq!(depth_warnings(&img, Format::Cineon, &EncodeOptions::default()), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); +} + +#[test] +fn a_frame_opened_at_ten_or_twelve_bits_saves_back_without_a_warning() { + let img = Image::from_u16(5, 2, ChannelLayout::Rgb, &(0..30u16).map(|i| i * 2000 + 1).collect::>()).unwrap(); + for (choice, bits) in [(DpxBits::Bits10, 10), (DpxBits::Bits12, 12)] { + let opened = decode(&encode(&img, Format::Dpx, &opts(Some(choice))).unwrap()).unwrap(); + assert_eq!(depth_warnings(&opened, Format::Dpx, &opts(Some(choice))), [], "{bits}-bit"); + assert_eq!(decode(&encode(&opened, Format::Dpx, &opts(Some(choice))).unwrap()).unwrap(), opened, "{bits}-bit"); + } + // A 12-bit frame saved at 10 bits does lose its two low bits. + let twelve = decode(&encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits12))).unwrap()).unwrap(); + assert_eq!(depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); + let cineon = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); + assert_eq!(depth_warnings(&cineon, Format::Cineon, &EncodeOptions::default()), []); + assert_eq!(depth_warnings(&cineon, Format::Dpx, &opts(Some(DpxBits::Bits10))), []); +} + +#[test] +fn gray_with_alpha_is_written_as_luma_with_a_warning() { + let img = Image::from_u8(2, 1, ChannelLayout::GrayA, vec![10, 128, 200, 255]).unwrap(); + let out = encode(&img, Format::Dpx, &EncodeOptions::default()).unwrap(); + assert_eq!(out[ELEMENT0 + 20], 6); + assert_eq!(decode(&out).unwrap().as_u8().unwrap(), &[10, 200]); + assert!(fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::AlphaDiscarded)); +} + +#[test] +fn only_free_form_text_is_reported_as_dropped() { + let mut img = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![1, 2, 3]).unwrap(); + img.meta.text = vec![("DPX:Project".into(), "Reel 7".into())]; + assert!(!fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::TextDropped)); + img.meta.text.push(("Comment".into(), "from a PNG".into())); + assert!(fidelity_warnings(&img, Format::Dpx).contains(&FidelityWarning::TextDropped)); +} + +#[test] +fn stored_orientation_is_turned_upright() { + // Orientation 4: the stored lines are the image's columns, left to right. + let mut fx = Fixture::new(6, 8, 0, 3, 1); + fx.codes = vec![1, 2, 3]; + fx.orientation = 4; + let img = decode(&fx.bytes()).unwrap(); + assert_eq!((img.width(), img.height(), img.as_u8().unwrap()), (1, 3, &[1u8, 2, 3][..])); +} + +#[test] +fn unsupported_and_damaged_files_error_instead_of_panicking() { + let unsupported = |bytes: Vec, what: &str| { + assert!(matches!(decode(&bytes), Err(CodecError::Unsupported { format: Format::Dpx, .. })), "{what}"); + }; + unsupported(Fixture { descriptor: 100, ..Fixture::new(50, 8, 0, 2, 2) }.bytes(), "ABGR descriptor"); + unsupported(Fixture { encoding: 1, ..Fixture::new(50, 10, 1, 2, 2) }.bytes(), "run-length encoding"); + unsupported(Fixture { signed: true, ..Fixture::new(50, 16, 0, 2, 2) }.bytes(), "signed samples"); + let mut float = Fixture::new(50, 16, 0, 2, 2).bytes(); + float[ELEMENT0 + 23] = 32; + unsupported(float, "32-bit float"); + let mut method_b = Fixture::new(50, 12, 1, 2, 2).bytes(); + method_b[ELEMENT0 + 24..ELEMENT0 + 26].copy_from_slice(&2u16.to_be_bytes()); + unsupported(method_b, "12-bit method B"); + + let good = Fixture::new(51, 10, 1, 9, 9).bytes(); + let mut no_elements = good.clone(); + no_elements[770..772].copy_from_slice(&0u16.to_be_bytes()); + let mut past_end = good.clone(); + past_end[ELEMENT0 + 28..ELEMENT0 + 32].copy_from_slice(&u32::MAX.wrapping_sub(1).to_be_bytes()); + let mut huge = good.clone(); + huge[772..780].copy_from_slice(&[0xFF; 8]); + for (what, bytes) in [ + ("header cut short", good[..900].to_vec()), + ("truncated element", good[..good.len() - 1].to_vec()), + ("no image elements", no_elements), + ("data offset past the end", past_end), + ("absurd size", huge), + ("magic alone", b"SDPX".to_vec()), + ] { + assert!(decode_as(Format::Dpx, &bytes).is_err(), "{what}"); + } +} + +// ---------------------------------------------------------------- Cineon (Kodak .cin) + +/// A Cineon 4.5 file built field by field: the 1024-byte generic header, a 1024-byte film header, +/// then `codes` (interleaved, top line first) three 10-bit values to a big-endian word, left +/// justified, each line on a word boundary. +fn cineon_bytes(channels: u8, width: u32, height: u32, codes: &[u16]) -> Vec { + let mut b = vec![0u8; 2048]; + b[0..4].copy_from_slice(&0x802A_5FD7u32.to_be_bytes()); + b[4..8].copy_from_slice(&2048u32.to_be_bytes()); + b[193] = channels; + b[198] = 10; + b[200..204].copy_from_slice(&width.to_be_bytes()); + b[204..208].copy_from_slice(&height.to_be_bytes()); + for line in codes.chunks(width as usize * usize::from(channels)) { + for chunk in line.chunks(3) { + let w = chunk.iter().enumerate().fold(0u32, |w, (i, &c)| w | (u32::from(c) << (22 - 10 * i as u32))); + b.extend(w.to_be_bytes()); + } + } + b +} + +/// Code values spread over 0..=1023, ends included. +fn cineon_codes(n: usize) -> Vec { + (0..n).map(|i| if i == 1 { 1023 } else { ((i as u64 * 2654435761) % 1024) as u16 }).collect() +} + +/// Reads back what we wrote: (channels, width, height, code values), from the header's own fields. +fn cineon_written(b: &[u8]) -> (u8, u32, u32, Vec) { + let off = be32_at(b, 4) as usize; + let (nc, w, h) = (b[193], be32_at(b, 200), be32_at(b, 204)); + let per_line = w as usize * usize::from(nc); + let stride = per_line.div_ceil(3) * 4; + let mut codes = Vec::new(); + for y in 0..h as usize { + let line = &b[off + y * stride..]; + codes.extend((0..per_line).map(|i| ((be32_at(line, i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3FF) as u16)); + } + (nc, w, h, codes) +} + +#[test] +fn cineon_frames_saved_as_cineon_keep_their_code_values_and_reopen_identically() { + // Widths that leave partly filled words at the end of each line. + for (channels, layout) in [(1u8, ChannelLayout::Gray), (3, ChannelLayout::Rgb)] { + let codes = cineon_codes(5 * 3 * usize::from(channels)); + let img = decode(&cineon_bytes(channels, 5, 3, &codes)).unwrap(); + assert_eq!(img.layout(), layout); + let out = encode(&img, Format::Cineon, &EncodeOptions::default()).unwrap_or_else(|e| panic!("{layout:?}: {e}")); + assert_eq!(detect(&out), Some(Format::Cineon), "{layout:?}"); + assert_eq!(cineon_written(&out), (channels, 5, 3, codes), "{layout:?}"); + assert_eq!(decode(&out).unwrap(), img, "{layout:?}"); + } +} + +#[test] +fn cineon_header_follows_the_kodak_layout() { + let img = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); + let out = encode(&img, Format::Cineon, &EncodeOptions::default()).unwrap(); + let f32_at = |at: usize| f32::from_bits(be32_at(&out, at)); + // Generic and film header lengths, user data, total size, version. + assert_eq!((be32_at(&out, 4), be32_at(&out, 8), be32_at(&out, 12), be32_at(&out, 16)), (2048, 1024, 1024, 0)); + assert_eq!(be32_at(&out, 20) as usize, out.len()); + assert_eq!(&out[24..29], b"V4.5\0"); + // Upright, three channels, each the universal metric's red, green, blue at 10 bits. + assert_eq!((out[192], out[193]), (0, 3)); + for (i, colour) in [1u8, 2, 3].into_iter().enumerate() { + let c = 196 + 28 * i; + assert_eq!(&out[c..c + 3], &[0, colour, 10], "channel {i}"); + assert_eq!((be32_at(&out, c + 4), be32_at(&out, c + 8)), (4, 2), "channel {i}"); + // Minimum and maximum data value and quantity, all four 32-bit floats. + assert_eq!((f32_at(c + 12), f32_at(c + 16), f32_at(c + 20), f32_at(c + 24)), (0.0, 0.0, 1023.0, 2.048), "channel {i}"); + } + // Pixel interleave, 32-bit words left justified, unsigned, positive, no padding. + assert_eq!(&out[680..692], &[0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + // Two lines of twelve samples: four words each. + assert_eq!(out.len(), 2048 + 2 * 16); +} + +#[test] +fn any_image_saves_as_ten_bit_cineon() { + // 16-bit samples land on the nearest 10-bit code; colour with alpha drops it, gray stays gray. + let wide = Image::from_u16(3, 1, ChannelLayout::Rgba, &[0, 32, 65535, 65535, 30000, 40000, 50000, 0, 1, 2, 3, 4]).unwrap(); + let out = encode(&wide, Format::Cineon, &EncodeOptions::default()).unwrap(); + let to_code = |v: u16| (f64::from(v) * 1023.0 / 65535.0).round() as u16; + let want: Vec = [0u16, 32, 65535, 30000, 40000, 50000, 1, 2, 3].into_iter().map(to_code).collect(); + assert_eq!(cineon_written(&out), (3, 3, 1, want)); + assert!(fidelity_warnings(&wide, Format::Cineon).contains(&FidelityWarning::AlphaDiscarded)); + let gray8 = Image::from_u8(2, 1, ChannelLayout::Gray, vec![0, 255]).unwrap(); + assert_eq!(cineon_written(&encode(&gray8, Format::Cineon, &EncodeOptions::default()).unwrap()), (1, 2, 1, vec![0, 1023])); +} diff --git a/crates/image-codecs/tests/it/exr_cryptomatte.rs b/crates/image-codecs/tests/it/exr_cryptomatte.rs new file mode 100644 index 000000000..80c843492 --- /dev/null +++ b/crates/image-codecs/tests/it/exr_cryptomatte.rs @@ -0,0 +1,271 @@ +//! Cryptomatte (specification 1.2): the hash function is checked against the official +//! test vectors from the reference repository, synthetic files carry ID/coverage streams +//! with header attributes and a manifest, and the info/decode tests check the listing, +//! the manifest round trip and the per-pixel pairs. Malformed inputs must error or +//! degrade, never panic. + +use exr::meta::attribute::{AttributeValue, Text}; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage, +}; +use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; + +// ------------------------------------------------------------------------------------------- +// The official hash test vectors (cryptomatte_utilities_tests.py, CryptoHashing). + +#[test] +// The values are quoted verbatim from the reference test suite; their extra digits are +// intentional documentation, not a precision mistake. +#[allow(clippy::excessive_precision)] +fn mm3_hash_matches_the_official_vectors() { + // The values are the f32 IDs the reference implementation produces (single precision). + let vectors: &[(&str, f64)] = &[ + ("hello", 6.0705627102400005616e-17), + ("cube", -4.08461912519e+15), + ("sphere", 2.79018604383e+15), + ("plane", 3.66557617593e-11), + ("равнина", -1.3192631212399999468e-25), + ("mädchen", 6.2361298211599995797e+25), + ]; + for (name, want) in vectors { + let got = cryptomatte_id(name); + assert_eq!(got.to_bits(), (*want as f32).to_bits(), "ID bits of {name:?}"); + } +} + +#[test] +fn every_id_is_a_finite_normal_float() { + // The exponent fix keeps IDs away from denormals, infinity and NaN, whatever the name. + for name in ["", "a", "hello", "CryptoAsset", "very long object name with ünïcödé"] { + let id = cryptomatte_id(name); + assert!(id.is_finite() && id.abs() >= f32::MIN_POSITIVE, "{name:?} -> {id}"); + } +} + +#[test] +fn key_is_seven_hex_digits_of_the_id() { + let key = cryptomatte_key("CryptoAsset"); + assert_eq!(key.len(), 7); + assert!(key.chars().all(|c| c.is_ascii_hexdigit())); + let full = format!("{:08x}", cryptomatte_id("CryptoAsset").to_bits()); + assert_eq!(key, full[..7]); +} + +#[test] +fn preview_color_shifts_the_id_bits() { + let id = cryptomatte_id("cube"); + let [r, g, b] = cryptomatte_preview_color(id); + assert_eq!((r, g, b), (0.0, f32::from_bits(id.to_bits() << 8), f32::from_bits(id.to_bits() << 16))); +} + +// ------------------------------------------------------------------------------------------- +// Synthetic Cryptomatte files. + +fn text(s: &str) -> Text { + Text::new_or_panic(s) +} + +/// A 4x4 EXR with one Cryptomatte layer `crypto_asset` of two streams: pixel (x, y) holds +/// `cube` (coverage 0.6) and `sphere` (coverage 0.25) where the streams' values say so. +fn cryptomatte_file(name: &str) -> Vec { + let (w, h) = (4usize, 4usize); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + // Stream 00: cube everywhere at 0.6; stream 01: sphere only in the first column. + let s0: Vec = vec![cube; w * h]; + let s0c: Vec = vec![0.6; w * h]; + let s1: Vec = (0..w * h).map(|p| if p % w == 0 { sphere } else { 0.0 }).collect(); + let s1c: Vec = (0..w * h).map(|p| if p % w == 0 { 0.25 } else { 0.0 }).collect(); + let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); + let list = vec![mk(format!("{name}00.red"), s0), mk(format!("{name}00.green"), s0c), mk(format!("{name}01.red"), s1), mk(format!("{name}01.green"), s1c)]; + let key = cryptomatte_key("CryptoAsset"); // attributes always use the canonical name + let manifest = format!("{{\"cube\":\"{:08x}\",\"sphere\":\"{:08x}\"}}", cube.to_bits(), sphere.to_bits()); + let mut attrs = LayerAttributes::named(name); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("CryptoAsset"))); + attrs.other.insert(text(&format!("cryptomatte/{key}/hash")), AttributeValue::Text(text(&key))); + attrs.other.insert(text(&format!("cryptomatte/{key}/conversion")), AttributeValue::Text(text("uint32_to_float32"))); + attrs.other.insert(text(&format!("cryptomatte/{key}/manifest")), AttributeValue::Text(text(&manifest))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +/// The same layer, but with the short `.r`/`.g` channel suffixes Nuke also matches. +fn cryptomatte_file_short_suffix() -> Vec { + let (w, h) = (4usize, 4usize); + let id = cryptomatte_id("cube"); + let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); + let list = vec![mk("crypto_asset00.r".into(), vec![id; w * h]), mk("crypto_asset00.g".into(), vec![1.0; w * h])]; + let key = cryptomatte_key("CryptoAsset"); + let mut attrs = LayerAttributes::named("crypto_asset"); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("CryptoAsset"))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +#[test] +fn layers_list_name_streams_and_manifest() { + let bytes = cryptomatte_file("crypto_asset"); + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + let layer = &found[0]; + assert_eq!(layer.part, 0); + assert_eq!(layer.name, "CryptoAsset"); + assert_eq!(layer.key, cryptomatte_key("CryptoAsset")); + assert_eq!(layer.conversion.as_deref(), Some("uint32_to_float32")); + assert_eq!(layer.channels, ["crypto_asset00", "crypto_asset01"]); + // The manifest maps names to their hash IDs, hex bits -> float value. + assert_eq!(layer.manifest.len(), 2); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + assert!(layer.manifest.contains(&(cube, "cube".to_string()))); + assert!(layer.manifest.contains(&(sphere, "sphere".to_string()))); + assert_eq!(layer.name_of(cube), Some("cube")); +} + +#[test] +fn decode_gives_the_pairs_per_pixel_sorted_by_coverage() { + let bytes = cryptomatte_file("crypto_asset"); + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + assert_eq!((buf.width, buf.height), (4, 4)); + let cube = cryptomatte_id("cube"); + let sphere = cryptomatte_id("sphere"); + for y in 0..4 { + // First column: cube (0.6) over sphere (0.25), sorted by coverage. + let both = buf.at(0, y); + assert_eq!(both, &[(cube, 0.6), (sphere, 0.25)], "column 0, row {y}"); + // Other columns: cube only. + for x in 1..4 { + assert_eq!(buf.at(x, y), &[(cube, 0.6)], "({x}, {y})"); + } + } +} + +#[test] +fn short_channel_suffixes_decode_too() { + let bytes = cryptomatte_file_short_suffix(); + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + let cube = cryptomatte_id("cube"); + assert_eq!(buf.at(3, 3), &[(cube, 1.0)]); +} + +/// A 4x4 layer `crypto` of one RGBA set `crypto00.///` with the given suffixes: +/// rank 0 (red/green) holds `cube` at 0.6 everywhere, rank 1 (blue/alpha) holds `sphere` +/// at 0.4 everywhere, the second (ID, coverage) pair the specification stores per set. +fn cryptomatte_file_rgba(suffixes: [&str; 4]) -> Vec { + let (w, h) = (4usize, 4usize); + let [r, g, b, a] = suffixes; + let mk = |s: &str, v: f32| AnyChannel::new(format!("crypto00.{s}").as_str(), FlatSamples::F32(vec![v; w * h])); + let list = vec![mk(r, cryptomatte_id("cube")), mk(g, 0.6), mk(b, cryptomatte_id("sphere")), mk(a, 0.4)]; + let key = cryptomatte_key("crypto"); + let mut attrs = LayerAttributes::named("crypto"); + attrs.other.insert(text(&format!("cryptomatte/{key}/name")), AttributeValue::Text(text("crypto"))); + let layer = Layer::new( + (w, h), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic cryptomatte exr writes"); + out.into_inner() +} + +#[test] +fn blue_alpha_rank_of_a_set_decodes_too() { + // Each RGBA set holds two ranks: (red, green) and (blue, alpha). Both must come back. + let bytes = cryptomatte_file_rgba(["red", "green", "blue", "alpha"]); + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + assert_eq!(found[0].channels, ["crypto00"], "one set, listed once"); + let buf = decode_cryptomatte(&bytes, "crypto", &Limits::default()).unwrap(); + let (cube, sphere) = (cryptomatte_id("cube"), cryptomatte_id("sphere")); + for y in 0..4 { + for x in 0..4 { + assert_eq!(buf.at(x, y), &[(cube, 0.6), (sphere, 0.4)], "({x}, {y})"); + } + } +} + +#[test] +fn uppercase_channel_suffixes_decode_both_ranks() { + // Blender and others write `.R/.G/.B/.A`; suffixes match regardless of case. + for suffixes in [["R", "G", "B", "A"], ["Red", "Green", "Blue", "Alpha"], ["r", "g", "b", "a"]] { + let bytes = cryptomatte_file_rgba(suffixes); + let buf = decode_cryptomatte(&bytes, "crypto", &Limits::default()).unwrap(); + let (cube, sphere) = (cryptomatte_id("cube"), cryptomatte_id("sphere")); + assert_eq!(buf.at(2, 1), &[(cube, 0.6), (sphere, 0.4)], "{suffixes:?}"); + } +} + +#[test] +fn unknown_layer_name_is_unsupported() { + let bytes = cryptomatte_file("crypto_asset"); + let e = decode_cryptomatte(&bytes, "CryptoMaterial", &Limits::default()).unwrap_err(); + assert!(e.to_string().contains("no Cryptomatte layer"), "{e}"); +} + +#[test] +fn plain_exr_has_no_cryptomatte_layers() { + // A normal RGB file (the multi-part fixture style) lists nothing. + let (w, h) = (2usize, 2usize); + let mk = |n: &str, v: f32| AnyChannel::new(n, FlatSamples::F32(vec![v; w * h])); + let list = vec![mk("R", 0.5), mk("G", 0.5), mk("B", 0.5)]; + let layer = Layer::new( + (w, h), + LayerAttributes::named("beauty"), + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let image = exr::image::Image::from_layers(ImageAttributes::new(IntegerBounds::new((0, 0), (w, h))), vec![layer]); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic exr writes"); + assert!(cryptomatte_layers(&out.into_inner(), &Limits::default()).unwrap().is_empty()); +} + +#[test] +fn a_broken_manifest_degrades_to_empty_not_a_panic() { + let mut bytes = cryptomatte_file("crypto_asset"); + // Corrupt the embedded manifest text (keep the attribute length: overwrite with '#'). + let needle = b"{\"cube\":"; + let at = bytes.windows(needle.len()).position(|w| w == needle).expect("manifest json"); + for b in &mut bytes[at..at + 8] { + *b = b'#'; + } + let found = cryptomatte_layers(&bytes, &Limits::default()).unwrap(); + assert_eq!(found.len(), 1); + assert!(found[0].manifest.is_empty(), "a broken manifest yields no entries"); + // The samples still decode. + let buf = decode_cryptomatte(&bytes, "CryptoAsset", &Limits::default()).unwrap(); + assert_eq!(buf.at(1, 0).len(), 1); +} + +#[test] +fn hostile_input_never_panics() { + let bytes = cryptomatte_file("crypto_asset"); + for end in (8..bytes.len()).step_by(7) { + let cut = &bytes[..end]; + let _ = cryptomatte_layers(cut, &Limits::default()); + let _ = decode_cryptomatte(cut, "CryptoAsset", &Limits::default()); + let _ = decode_cryptomatte(cut, "nothing", &Limits::default()); + } + // Tiny limits refuse before allocating. + let e = cryptomatte_layers(&bytes, &Limits { max_alloc: 8, ..Limits::default() }).unwrap_err(); + assert!(e.to_string().contains("exceed"), "{e}"); +} diff --git a/crates/image-codecs/tests/it/exr_multipart.rs b/crates/image-codecs/tests/it/exr_multipart.rs new file mode 100644 index 000000000..246b3d9ed --- /dev/null +++ b/crates/image-codecs/tests/it/exr_multipart.rs @@ -0,0 +1,334 @@ +//! Multi-part EXR (a Maya/Arnold render writes one part per AOV): the `exr` crate +//! writes synthetic files with several parts, the info/decode tests check the part +//! listing, the colour-ranked auto-pick and part-by-index decoding, and malformed +//! inputs must error, never panic. + +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, +}; +use craft_image_codecs::f16; +use craft_image_codecs::{ + ChannelLayout, DecodeOptions, DecodeWarning, Format, Limits, SampleType, decode, decode_as_with, decode_exr_part, exr_info, probe_size, +}; + +fn layer_f32(name: &str, comp: Compression, chans: &[(&str, Vec)]) -> Layer> { + let n = chans.iter().find(|(_, v)| !v.is_empty()).map_or(0, |(_, v)| v.len()); + let list = chans + .iter() + .map(|(cn, v)| { + assert_eq!(v.len(), n, "channel lengths must match"); + AnyChannel::new(*cn, FlatSamples::F32(v.clone())) + }) + .collect::>(); + let h = (n as f32).sqrt().round() as usize; // tests use square images + Layer::new( + (h, h), + LayerAttributes::named(name), + Encoding { compression: comp, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ) +} + +/// Three parts in the order an AOV-heavy render might use: a depth-only `Z` part first +/// (which used to make the whole file fail with "no R/G/B or Y channels"), the `beauty` +/// RGBA part second and a `diffuse` RGB part third, each with its own compression. +fn aov_file() -> Vec { + let n = 16; + let z: Vec = (0..n).map(|i| i as f32).collect(); + let beauty: [Vec; 4] = std::array::from_fn(|c| { + (0..n) + .map(|i| match c { + 0 => i as f32 / 15.0, + 1 => 1.0 - i as f32 / 15.0, + 2 => 0.25, + _ => 1.0, + }) + .collect() + }); + let diffuse: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| [0.3, 0.6, 0.9][c]).collect()); + let layers = vec![ + layer_f32("Z", Compression::Uncompressed, &[("Z", z)]), + layer_f32("beauty", Compression::ZIP1, &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())]), + layer_f32("diffuse", Compression::RLE, &[("R", diffuse[0].clone()), ("G", diffuse[1].clone()), ("B", diffuse[2].clone())]), + ]; + write_image(layers) +} + +fn write_image(layers: Vec>>) -> Vec { + let size: (usize, usize) = layers.first().map_or((1, 1), |l| l.size.into()); + let bounds = IntegerBounds::new((0, 0), size); + let image = Image::from_layers(ImageAttributes::new(bounds), layers); + let mut out = std::io::Cursor::new(Vec::new()); + image.write().to_buffered(&mut out).expect("synthetic exr writes"); + out.into_inner() +} + +fn stereo_file() -> Vec { + let n = 4; + let px = |k: f32| -> [Vec; 4] { std::array::from_fn(|c| (0..n).map(|i| if c == 0 { i as f32 / 3.0 * k } else { k }).collect()) }; + let left = px(0.1); + let right = px(0.9); + let with_view = |name: &str, view: &str, p: [Vec; 4]| { + let attrs = LayerAttributes { view_name: Some(view.into()), ..LayerAttributes::named(name) }; + let list: Vec<_> = ["R", "G", "B", "A"].iter().enumerate().map(|(c, cn)| AnyChannel::new(*cn, FlatSamples::F32(p[c].clone()))).collect(); + Layer::new( + (2, 2), + attrs, + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ) + }; + // The `exr` crate refuses duplicate part names when writing, so a stereo pair is + // emulated with distinct names that still carry the view attribute. + write_image(vec![with_view("beauty_left", "left", left), with_view("beauty_right", "right", right)]) +} + +fn near(a: f32, b: f32) -> bool { + (a - b).abs() < 1e-6 +} + +#[test] +fn info_lists_every_part() { + let parts = exr_info(&aov_file(), &Limits::default()).unwrap(); + assert_eq!(parts.len(), 3); + assert_eq!(parts.iter().map(|p| p.name.as_deref()).collect::>(), [Some("Z"), Some("beauty"), Some("diffuse")]); + let z = &parts[0]; + assert_eq!((z.width, z.height), (4, 4)); + assert!(!z.deep && !z.tiled); + assert_eq!(z.channels.len(), 1); + assert_eq!(z.channels[0].name, "Z"); + assert_eq!(z.channels[0].sample, SampleType::F32); + let beauty = &parts[1]; + // OpenEXR stores the channel list in the header alphabetically; the info shows the file as-is. + assert_eq!(beauty.channels.iter().map(|c| c.name.as_str()).collect::>(), ["A", "B", "G", "R"]); + assert_eq!(beauty.channels.iter().map(|c| c.sample).collect::>(), [SampleType::F32; 4]); +} + +#[test] +fn auto_pick_skips_the_depth_part() { + let img = decode(&aov_file()).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert!(img.warnings.contains(&DecodeWarning::MoreParts { total: Some(3) }), "{:?}", img.warnings); + for i in 0..16 { + let (x, y) = (i % 4, i / 4); + assert!(near(img.get(x, y, 0), i as f32 / 15.0), "pixel {i}"); + assert!(near(img.get(x, y, 1), 1.0 - i as f32 / 15.0), "pixel {i}"); + assert!(near(img.get(x, y, 2), 0.25), "pixel {i}"); + assert!(near(img.get(x, y, 3), 1.0), "pixel {i}"); + } +} + +#[test] +fn parts_decode_by_index() { + let bytes = aov_file(); + // The depth part opens as a grayscale image instead of failing the file. + let z = decode_exr_part(&bytes, 0, &DecodeOptions::default()).unwrap(); + assert_eq!(z.layout(), ChannelLayout::Gray); + for i in 0..16 { + assert!(near(z.get(i % 4, i / 4, 0), i as f32), "pixel {i}"); + } + let diffuse = decode_exr_part(&bytes, 2, &DecodeOptions::default()).unwrap(); + assert_eq!(diffuse.layout(), ChannelLayout::Rgb); + assert!(near(diffuse.get(1, 2, 2), 0.9)); + // An explicit part choice does not warn about the parts left out. + assert!(!diffuse.warnings.contains(&DecodeWarning::MoreParts { total: Some(3) })); +} + +#[test] +fn part_index_out_of_range_errors() { + let bytes = aov_file(); + let e = decode_exr_part(&bytes, 3, &DecodeOptions::default()).unwrap_err().to_string(); + assert!(e.contains("out of range") && e.contains("3 parts"), "{e}"); +} + +#[test] +fn views_are_listed_and_left_wins_the_auto_pick() { + let bytes = stereo_file(); + let parts = exr_info(&bytes, &Limits::default()).unwrap(); + assert_eq!(parts.iter().map(|p| p.view.as_deref()).collect::>(), [Some("left"), Some("right")]); + let img = decode(&bytes).unwrap(); + assert!(near(img.get(0, 0, 0), 0.0) && near(img.get(3, 0, 0), 0.1), "the left view is the first part"); + assert!(near(img.get(1, 1, 1), 0.1), "left's G is 0.1 everywhere"); + let right = decode_exr_part(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert!(near(right.get(1, 1, 1), 0.9), "right's G is 0.9 everywhere"); +} + +#[test] +fn single_part_files_do_not_warn() { + let n = 4; + let v: Vec = (0..n).map(|i| i as f32 / 3.0).collect(); + let bytes = write_image(vec![layer_f32("main", Compression::ZIP1, &[("Y", v)])]); + let img = decode(&bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); +} + +#[test] +fn half_float_parts_stay_half() { + let n = 4; + let chans: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| f16::from_f32([0.5, 0.25, 1.0][c])).collect()); + let list: Vec<_> = ["R", "G", "B"].iter().enumerate().map(|(c, cn)| AnyChannel::new(*cn, FlatSamples::F16(chans[c].clone()))).collect(); + let layer = Layer::new( + (2, 2), + LayerAttributes::named("half"), + Encoding { compression: Compression::Uncompressed, blocks: Blocks::ScanLines, line_order: LineOrder::Increasing }, + AnyChannels::sort(list.into_iter().collect()), + ); + let bytes = write_image(vec![layer]); + let img = decode_exr_part(&bytes, 0, &DecodeOptions::default()).unwrap(); + assert_eq!(img.sample_type(), SampleType::F16); + assert!(near(img.get(1, 1, 0), 0.5)); +} + +#[test] +fn truncated_files_error_never_panic() { + let bytes = aov_file(); + for end in (8..bytes.len()).step_by(5) { + let cut = &bytes[..end]; + let _ = exr_info(cut, &Limits::default()); + let _ = decode_as_with(Format::OpenExr, cut, &DecodeOptions::default()); + let _ = decode_exr_part(cut, 1, &DecodeOptions::default()); + } +} + +#[test] +fn parts_are_capped_together() { + // Each part fits alone (largest 256 bytes) but the three together (512) do not. + let limits = Limits { max_alloc: 300, ..Limits::default() }; + let e = exr_info(&aov_file(), &limits).unwrap_err().to_string(); + assert!(e.contains("across 3 parts"), "{e}"); +} + +/// Two ZIP1 parts of 4x4 RGBA; every chunk of part 0 is then overwritten with garbage, so +/// inflating it fails. +fn file_with_corrupt_first_part() -> Vec { + let px = |k: f32| -> Vec<(&'static str, Vec)> { ["R", "G", "B", "A"].iter().map(|c| (*c, vec![k; 16])).collect() }; + let mut bytes = write_image(vec![layer_f32("first", Compression::ZIP1, &px(0.25)), layer_f32("second", Compression::ZIP1, &px(0.75))]); + let mut cursor = std::io::Cursor::new(&bytes[..]); + exr::meta::MetaData::read_from_buffered(&mut cursor, false).unwrap(); + let table = cursor.position() as usize; + // Part 0 owns the first four of the eight chunks (one scan line per ZIP1 block). + for i in 0..4 { + let at = table + i * 8; + let offset = u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) as usize; + let size = u32::from_le_bytes(bytes[offset + 8..offset + 12].try_into().unwrap()) as usize; + bytes[offset + 12..offset + 12 + size].fill(0xA5); + } + bytes +} + +#[test] +fn decoding_one_part_does_not_decode_the_others() { + let bytes = file_with_corrupt_first_part(); + // Part 0 is unreadable ... + assert!(decode_exr_part(&bytes, 0, &DecodeOptions::default()).is_err()); + // ... but asking for part 1 must not touch it. + let img = decode_exr_part(&bytes, 1, &DecodeOptions::default()).expect("part 1 decodes on its own"); + assert_eq!((img.width(), img.height()), (4, 4)); + assert!(img.to_normalized().iter().all(|v| near(*v, 0.75)), "wrong pixels"); +} + +fn channel_group_file(channels: &[(&str, f32)]) -> Vec { + let channels: Vec<_> = channels.iter().map(|(name, value)| (*name, vec![*value; 4])).collect(); + write_image(vec![layer_f32("groups", Compression::ZIP1, &channels)]) +} + +fn assert_channel_group(bytes: &[u8], layout: ChannelLayout, expected: &[f32]) { + assert_eq!(exr_info(bytes, &Limits::default()).unwrap()[0].color_rank(), expected.len() as u8); + assert_eq!(probe_size(bytes).unwrap(), (2, 2)); + for img in [decode(bytes).unwrap(), decode_exr_part(bytes, 0, &DecodeOptions::default()).unwrap()] { + assert_eq!(img.layout(), layout); + assert_eq!(img.to_normalized(), expected.repeat(4)); + } +} + +#[test] +fn flat_decode_keeps_rgb_channels_in_one_group() { + // A partial group sorts before the nested beauty layer, but cannot donate its R. + let bytes = channel_group_file(&[("aaa.R", 9.0), ("beauty.diffuse.R", 0.25), ("beauty.diffuse.G", 0.5), ("beauty.diffuse.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); +} + +#[test] +fn flat_decode_does_not_borrow_another_groups_alpha() { + for alpha in ["A", "aaa.A", "beauty.other.A"] { + let bytes = channel_group_file(&[(alpha, 0.125), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); + let bytes = channel_group_file(&[(alpha, 0.125), ("beauty.Y", 0.5)]); + assert_channel_group(&bytes, ChannelLayout::Gray, &[0.5]); + } +} + +#[test] +fn flat_decode_rejects_rgb_split_between_groups() { + for channels in [ + [("aaa.R", 0.25), ("bbb.G", 0.5), ("ccc.B", 2.0)], + [("beauty.R", 0.25), ("beauty.diffuse.G", 0.5), ("beauty.diffuse.B", 2.0)], + [(".R", 0.25), ("G", 0.5), ("B", 2.0)], + ] { + let bytes = channel_group_file(&channels); + assert_eq!(exr_info(&bytes, &Limits::default()).unwrap()[0].color_rank(), 0); + assert!(probe_size(&bytes).is_err()); + assert!(decode(&bytes).is_err()); + assert!(decode_exr_part(&bytes, 0, &DecodeOptions::default()).is_err()); + } +} + +#[test] +fn auto_pick_and_probe_skip_incoherent_rgb_parts() { + let bytes = write_image(vec![ + layer_f32("split", Compression::ZIP1, &[("aaa.R", vec![9.0; 4]), ("bbb.G", vec![8.0; 4]), ("ccc.B", vec![7.0; 4])]), + layer_f32("luminance", Compression::ZIP1, &[("Y", vec![0.5; 16]), ("A", vec![0.75; 16])]), + ]); + let parts = exr_info(&bytes, &Limits::default()).unwrap(); + assert_eq!(parts.iter().map(|part| part.color_rank()).collect::>(), [0, 2]); + assert_eq!(probe_size(&bytes).unwrap(), (4, 4)); + let img = decode(&bytes).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (4, 4, ChannelLayout::GrayA)); + assert_eq!(img.to_f32_samples().unwrap(), [0.5, 0.75].repeat(16)); +} + +#[test] +fn flat_decode_prefers_the_highest_ranked_complete_group() { + let bytes = + channel_group_file(&[("aaa.R", 9.0), ("aaa.G", 8.0), ("aaa.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)]); + assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); + let bytes = channel_group_file(&[("aaa.Y", 9.0), ("beauty.Y", 0.5), ("beauty.A", 0.75)]); + assert_channel_group(&bytes, ChannelLayout::GrayA, &[0.5, 0.75]); +} + +#[test] +fn flat_decode_breaks_group_ties_by_prefix() { + let bytes = channel_group_file(&[("z.R", 9.0), ("z.G", 8.0), ("z.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); + // Even a prefix that sorts before the unprefixed channel names loses a rank tie. + let bytes = channel_group_file(&[("0.R", 9.0), ("0.G", 8.0), ("0.B", 7.0), ("R", 0.25), ("G", 0.5), ("B", 2.0)]); + assert_channel_group(&bytes, ChannelLayout::Rgb, &[0.25, 0.5, 2.0]); +} + +#[test] +fn flat_decode_preserves_luminance_and_lone_channel_fallbacks() { + for prefix in ["", "beauty."] { + let y = format!("{prefix}Y"); + let a = format!("{prefix}A"); + assert_channel_group(&channel_group_file(&[(&y, 2.0), (&a, 0.75)]), ChannelLayout::GrayA, &[2.0, 0.75]); + assert_channel_group(&channel_group_file(&[(&y, 2.0)]), ChannelLayout::Gray, &[2.0]); + } + for name in ["depth.Z", "A", "beauty.A"] { + // A lone alpha is one grayscale channel, not the same samples duplicated as YA. + assert_channel_group(&channel_group_file(&[(name, 9.0)]), ChannelLayout::Gray, &[9.0]); + } +} + +#[test] +fn half_float_group_does_not_use_unrelated_float_channels() { + let mut layer = layer_f32("mixed", Compression::ZIP1, &[("aaa.R", vec![9.0; 4]), ("aaa.A", vec![0.125; 4])]); + for (name, value) in [("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)] { + layer.channel_data.list.push(AnyChannel::new(name, FlatSamples::F16(vec![f16::from_f32(value); 4]))); + } + layer.channel_data = AnyChannels::sort(layer.channel_data.list); + let bytes = write_image(vec![layer]); + assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); + assert_eq!(decode(&bytes).unwrap().sample_type(), SampleType::F16); +} diff --git a/crates/image-codecs/tests/it/exr_stream.rs b/crates/image-codecs/tests/it/exr_stream.rs new file mode 100644 index 000000000..5d6ee4158 --- /dev/null +++ b/crates/image-codecs/tests/it/exr_stream.rs @@ -0,0 +1,161 @@ +use craft_image_codecs::{exr_stream::Decoder, *}; +use std::io::Cursor; + +fn fixture(layout: ChannelLayout, sample: SampleType, compression: ExrCompression) -> Vec { + let n = 31 * 35 * layout.channels(); + let values: Vec = (0..n) + .map(|i| match i % 5 { + 0 => -0.25, + 1 => 3.5, + 2 => 0.0, + 3 => 0.0001, + _ => 0.5, + }) + .collect(); + let image = Image::from_f32(31, 35, layout, &values).unwrap().convert(layout, sample); + encode(&image, Format::OpenExr, &EncodeOptions { exr_compression: compression, ..Default::default() }).unwrap() +} + +#[test] +fn scanline_blocks_match_whole_decoder_at_each_layout_depth_and_compression() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::F16, SampleType::F32] { + for compression in [ExrCompression::None, ExrCompression::Rle, ExrCompression::Zip1, ExrCompression::Zip16, ExrCompression::Piz] { + let bytes = fixture(layout, sample, compression); + let reference = decode(&bytes).unwrap(); + let target = if layout.is_gray() { ChannelLayout::GrayA } else { ChannelLayout::Rgba }; + let reference = reference.convert(target, SampleType::F32); + let mut stream = Decoder::new(Cursor::new(&bytes), &|| false).unwrap(); + assert_eq!(stream.info().all_half, sample == SampleType::F16); + let row = 31 * target.channels() * 4; + let mut actual = vec![0; reference.data().len()]; + let mut blocks = 0; + while let Some(band) = stream.next_band(&|| false).unwrap() { + actual[band.y as usize * row..(band.y + band.height) as usize * row].copy_from_slice(&band.data); + blocks += 1; + } + assert_eq!(blocks, stream.info().blocks); + assert_eq!(actual, reference.data(), "{layout:?}/{sample:?}/{compression:?}"); + } + } + } +} + +fn change_window(bytes: &mut [u8], width: i32, height: i32) { + let needle = b"dataWindow\0box2i\0\x10\0\0\0"; + let at = bytes.windows(needle.len()).position(|v| v == needle).unwrap() + needle.len(); + bytes[at + 8..at + 12].copy_from_slice(&(width - 1).to_le_bytes()); + bytes[at + 12..at + 16].copy_from_slice(&(height - 1).to_le_bytes()); +} + +#[test] +fn enormous_header_is_not_a_whole_image_allocation() { + let mut bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + change_window(&mut bytes, 65536, 32768); + assert!(matches!(decode(&bytes), Err(CodecError::LimitExceeded(ref message)) if message.contains("2147483648 pixels"))); + // Streaming gets as far as the now-truncated offset table, without a pixel allocation. + assert!(matches!(Decoder::new(Cursor::new(bytes), &|| false), Err(CodecError::Malformed { .. }))); +} + +#[test] +fn cancellation_truncation_duplicate_offsets_and_forged_lengths_fail() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + assert!(Decoder::new(Cursor::new(&bytes), &|| true).is_err()); + let mut decoder = Decoder::new(Cursor::new(&bytes), &|| false).unwrap(); + assert!(decoder.next_band(&|| true).is_err()); + for end in [0, 8, bytes.len() / 2] { + assert!(Decoder::new(Cursor::new(&bytes[..end]), &|| false).and_then(|mut r| r.next_band(&|| false)).is_err()); + } + // Locate the offset table by finding the validated header end. + let mut input = Cursor::new(&bytes); + let _ = exr::meta::MetaData::read_from_buffered(&mut input, false).unwrap(); + let at = input.position() as usize; + let offset = u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) as usize; + let mut corrupt = bytes.clone(); + corrupt[offset + 4..offset + 8].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(Decoder::new(Cursor::new(corrupt), &|| false).unwrap().next_band(&|| false).is_err()); + let mut duplicate = bytes.clone(); + duplicate[at + 8..at + 16].copy_from_slice(&bytes[at..at + 8]); + let mut decoder = Decoder::new(Cursor::new(duplicate), &|| false).unwrap(); + assert!(decoder.next_band(&|| false).unwrap().is_some()); + assert!(decoder.next_band(&|| false).is_err()); +} + +#[test] +fn hostile_metadata_is_bounded_and_mutated_inputs_never_panic() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + let mut large = bytes.clone(); + let needle = b"channels\0chlist\0"; + let length = bytes.windows(needle.len()).position(|v| v == needle).unwrap() + needle.len(); + large[length..length + 4].copy_from_slice(&i32::MAX.to_le_bytes()); + assert!(matches!(Decoder::new(Cursor::new(large), &|| false), Err(CodecError::LimitExceeded(_)))); + let mut many = bytes[..8].to_vec(); + for _ in 0..4097 { + many.extend_from_slice(b"z\0string\0\0\0\0\0"); + } + many.push(0); + assert!(matches!(Decoder::new(Cursor::new(many), &|| false), Err(CodecError::LimitExceeded(_)))); + // Small deterministic mutations cover headers, offsets and compressed payloads. + for i in 0..256 { + let mut corrupt = bytes.clone(); + let at = i * (bytes.len() - 1) / 255; + corrupt[at] ^= 0xff; + assert!( + std::panic::catch_unwind(|| { + if let Ok(mut decoder) = Decoder::new(Cursor::new(corrupt), &|| false) { + while let Ok(Some(_)) = decoder.next_band(&|| false) {} + } + }) + .is_ok(), + "mutation at byte {at}" + ); + } +} + +#[test] +fn mixed_samples_and_offset_data_window_keep_coordinates_and_values() { + use exr::prelude::{AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, WritableImage}; + let channels = AnyChannels::sort( + vec![ + AnyChannel::new("R", FlatSamples::F16(vec![f16::from_f32(-0.25); 31 * 35])), + AnyChannel::new("G", FlatSamples::F32(vec![3.5; 31 * 35])), + AnyChannel::new("B", FlatSamples::U32(vec![42; 31 * 35])), + ] + .into_iter() + .collect(), + ); + let attributes = LayerAttributes { layer_position: exr::math::Vec2(-15, 40), ..Default::default() }; + let layer = + Layer::new((31, 35), attributes, Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, channels); + let mut bytes = Cursor::new(Vec::new()); + exr::image::Image::from_layer(layer).write().to_buffered(&mut bytes).unwrap(); + let mut decoder = Decoder::new(bytes, &|| false).unwrap(); + assert!(!decoder.info().all_half); + let mut rows = 0; + while let Some(band) = decoder.next_band(&|| false).unwrap() { + assert_eq!(band.y, rows); + rows += band.height; + for p in band.data.as_chunks::<16>().0 { + let v: Vec = p.as_chunks::<4>().0.iter().map(|s| f32::from_le_bytes(*s)).collect(); + assert_eq!(v, [-0.25, 3.5, 42.0, 1.0]); + } + } + assert_eq!(rows, 35); +} + +#[test] +fn unsupported_variants_stay_on_the_guarded_legacy_path() { + let bytes = fixture(ChannelLayout::Rgb, SampleType::F16, ExrCompression::Zip16); + for flag in [0x200_u32, 0x800, 0x1000] { + let mut changed = bytes.clone(); + changed[4..8].copy_from_slice(&(2 | flag).to_le_bytes()); + assert!(matches!(Decoder::new(Cursor::new(changed), &|| false), Err(CodecError::Unsupported { .. }))); + } + let mut custom = bytes[..8].to_vec(); + custom.extend_from_slice(b"chromaticities\0chromaticities\0\x20\0\0\0"); + for v in [0.64_f32, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] { + custom.extend_from_slice(&v.to_le_bytes()); + } + custom.extend_from_slice(&bytes[8..]); + assert!(matches!(Decoder::new(Cursor::new(custom), &|| false), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/it/fidelity.rs b/crates/image-codecs/tests/it/fidelity.rs new file mode 100644 index 000000000..37c7aba0c --- /dev/null +++ b/crates/image-codecs/tests/it/fidelity.rs @@ -0,0 +1,243 @@ +//! `fidelity_warnings` cases, and consistency with what `encode` does. + +use crate::common; +use FidelityWarning as W; +use common::*; +use craft_image_codecs::*; + +fn warns(img: &Image, f: Format) -> Vec { + fidelity_warnings(img, f) +} + +#[test] +fn png_u8_rgb_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Png).is_empty()); +} +#[test] +fn png_u16_rgba_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::U16), Format::Png).is_empty()); +} +#[test] +fn tiff_is_clean_for_all_integer_and_f32_layouts() { + for l in ChannelLayout::ALL { + for s in [SampleType::U8, SampleType::U16, SampleType::F32, SampleType::F16] { + let w = warns(&test_image(l, s), Format::Tiff); + if l.is_lab() && s.is_float() { + // CIELab has no float form: it is written with 16 bits, and says so. + assert_eq!(w, vec![FidelityWarning::DepthReduced { from: s, to: SampleType::U16 }], "{l:?} {s:?}"); + } else { + assert!(w.is_empty(), "{l:?} {s:?}"); + } + } + } +} +#[test] +fn sixteen_bit_to_jpeg_reduces() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U16), Format::Jpeg); + assert!(w.contains(&W::DepthReduced { from: SampleType::U16, to: SampleType::U8 })); + assert!(w.contains(&W::LossyCompression)); +} +#[test] +fn sixteen_bit_message_text() { + let w = W::DepthReduced { from: SampleType::U16, to: SampleType::U8 }; + assert_eq!(w.to_string(), "16-bit will be reduced to 8-bit"); +} +#[test] +fn alpha_to_jpeg_discarded() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Jpeg); + assert!(w.contains(&W::AlphaDiscarded)); +} +#[test] +fn opaque_alpha_not_reported() { + let mut img = test_image(ChannelLayout::Rgba, SampleType::U8); + for px in img.data_mut().chunks_mut(4) { + px[3] = 255; + } + assert!(!warns(&img, Format::Jpeg).contains(&W::AlphaDiscarded)); +} +#[test] +fn alpha_to_hdr_discarded() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::F32), Format::Hdr).contains(&W::AlphaDiscarded)); +} +#[test] +fn cmyk_to_png_converted() { + let w = warns(&test_image(ChannelLayout::Cmyk, SampleType::U8), Format::Png); + assert!(w.contains(&W::CmykConverted { to: ChannelLayout::Rgb })); +} +#[test] +fn cmyka_to_webp_converted_keeps_alpha() { + let w = warns(&test_image(ChannelLayout::CmykA, SampleType::U8), Format::WebP); + assert!(w.contains(&W::CmykConverted { to: ChannelLayout::Rgba })); + assert!(!w.contains(&W::AlphaDiscarded)); +} +#[test] +fn cmyk_to_jpeg_and_tiff_not_converted() { + for f in [Format::Jpeg, Format::Tiff, Format::Pnm] { + let w = warns(&test_image(ChannelLayout::Cmyk, SampleType::U8), f); + assert!(!w.iter().any(|w| matches!(w, W::CmykConverted { .. })), "{f:?}"); + } +} +#[test] +fn icc_dropped_for_bmp() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(100))); + assert!(warns(&img, Format::Bmp).contains(&W::IccDropped)); + assert!(!warns(&img, Format::Png).contains(&W::IccDropped)); +} +#[test] +fn icc_warning_suppressed_when_not_embedding() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(100))); + let opts = EncodeOptions { embed_icc: false, ..Default::default() }; + assert!(!fidelity_warnings_with(&img, Format::Bmp, &opts).contains(&W::IccDropped)); +} +#[test] +fn exif_xmp_dpi_text_dropped_for_qoi() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = + Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + let w = warns(&img, Format::Qoi); + for x in [W::ExifDropped, W::XmpDropped, W::DpiDropped, W::TextDropped] { + assert!(w.contains(&x), "{x:?}"); + } +} +/// WebP keeps the resolution in EXIF (#2399); only free-form text has nowhere to go. +#[test] +fn webp_drops_text_only() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = + Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + let w = warns(&img, Format::WebP); + assert_eq!(w, vec![W::TextDropped]); +} +#[test] +fn float_to_png_reduces_and_clips() { + let vals: Vec = (0..12).map(|i| i as f32 * 0.3 - 0.5).collect(); + let img = Image::from_f32(2, 2, ChannelLayout::Rgb, &vals).unwrap(); + let w = warns(&img, Format::Png); + assert!(w.contains(&W::DepthReduced { from: SampleType::F32, to: SampleType::U16 })); + assert!(w.contains(&W::RangeClipped)); +} +#[test] +fn in_range_float_not_clipped() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::F32), Format::Png); + assert!(!w.contains(&W::RangeClipped)); +} +#[test] +fn hdr_float_to_fits_clips() { + let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap(); + assert!(warns(&img, Format::Fits).contains(&W::RangeClipped)); + assert!(!warns(&test_image(ChannelLayout::Gray, SampleType::F32), Format::Fits).contains(&W::RangeClipped)); +} +#[test] +fn float_to_exr_and_tiff_is_clean() { + for f in [Format::OpenExr, Format::Tiff, Format::Pnm] { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::F32), f).is_empty(), "{f:?}"); + } +} +#[test] +fn f16_to_exr_is_clean() { + assert!(warns(&test_image(ChannelLayout::Rgba, SampleType::F16), Format::OpenExr).is_empty()); +} +#[test] +fn integer_to_exr_is_lossless() { + for s in [SampleType::U8, SampleType::U16] { + assert!(warns(&test_image(ChannelLayout::Rgb, s), Format::OpenExr).is_empty(), "{s:?}"); + } +} +#[test] +fn float_pnm_drops_alpha() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::F32), Format::Pnm); + assert!(w.contains(&W::AlphaDiscarded)); +} +#[test] +fn gif_quantizes_and_binarizes() { + let w = warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Gif); + assert!(w.contains(&W::PaletteQuantized)); + assert!(w.contains(&W::AlphaBinarized)); + assert!(!w.contains(&W::LossyCompression)); +} +#[test] +fn iff_binarizes_alpha_and_reduces_16_bit() { + assert_eq!(warns(&test_image(ChannelLayout::Rgba, SampleType::U8), Format::Iff), vec![W::AlphaBinarized]); + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U16), Format::Iff); + assert!(w.contains(&W::DepthReduced { from: SampleType::U16, to: SampleType::U8 })); + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Iff).is_empty()); +} +#[test] +fn hdr_is_lossy() { + assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::F32), Format::Hdr).contains(&W::LossyCompression)); +} +#[test] +fn ico_oversize_is_fatal() { + let img = Image::new(512, 16, ChannelLayout::Rgba, SampleType::U8).unwrap(); + let w = warns(&img, Format::Ico); + assert!(w.iter().any(|w| w.is_fatal())); + assert!(w.contains(&W::DimensionsExceeded { max_width: 256, max_height: 256 })); +} +#[test] +fn avif_default_build_write_unsupported() { + let w = warns(&test_image(ChannelLayout::Rgb, SampleType::U8), Format::Avif); + if cfg!(feature = "avif") { + assert!(w.contains(&W::LossyCompression)); + } else { + assert_eq!(w, vec![W::WriteUnsupported { format: Format::Avif }]); + assert!(w[0].is_fatal()); + } +} +#[test] +fn every_warning_has_a_message() { + let all = [ + W::WriteUnsupported { format: Format::Avif }, + W::DepthReduced { from: SampleType::F32, to: SampleType::F16 }, + W::BitsReduced { from: SampleType::U16, bits: 10 }, + W::RangeClipped, + W::AlphaDiscarded, + W::AlphaBinarized, + W::CmykConverted { to: ChannelLayout::Rgb }, + W::LabConverted { to: ChannelLayout::Rgb }, + W::ColorToGray, + W::PaletteQuantized, + W::LossyCompression, + W::IccDropped, + W::ExifDropped, + W::XmpDropped, + W::DpiDropped, + W::TextDropped, + W::DimensionsExceeded { max_width: 1, max_height: 1 }, + ]; + for w in all { + assert!(w.to_string().len() > 5); + } +} + +/// The warnings must agree with what actually comes back from the file. +#[test] +fn warnings_match_actual_roundtrip_behaviour() { + for f in rw_formats() { + for l in ChannelLayout::ALL { + for s in SampleType::ALL { + let img = test_image(l, s); + let w = warns(&img, f); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + let depth_reduced = w.iter().any(|w| match w { + W::DepthReduced { .. } => true, + W::BitsReduced { from, .. } => from.is_float(), + _ => false, + }); + let lost_depth = back.sample_type() != s && back.sample_type() != SampleType::F32 && s != SampleType::U8; + assert_eq!(depth_reduced, lost_depth, "{f:?} {l:?} {s:?} {w:?} -> {:?}", back.sample_type()); + // Cineon stores 10 bits: 16-bit samples come back changed exactly when warned. + if f == Format::Cineon && s == SampleType::U16 && back.sample_type() == s { + let bits_reduced = w.iter().any(|w| matches!(w, W::BitsReduced { .. })); + let changed = back.to_u16_samples() != img.convert(back.layout(), s).to_u16_samples(); + assert_eq!(bits_reduced, changed, "{f:?} {l:?} {w:?}"); + } + let alpha_lost = l.has_alpha() && !back.layout().has_alpha(); + assert_eq!(w.contains(&W::AlphaDiscarded), alpha_lost, "{f:?} {l:?} {s:?}"); + let cmyk_lost = l.is_cmyk() && !back.layout().is_cmyk(); + assert_eq!(w.iter().any(|w| matches!(w, W::CmykConverted { .. })), cmyk_lost, "{f:?} {l:?} {s:?}"); + let lab_lost = l.is_lab() && !back.layout().is_lab(); + assert_eq!(w.iter().any(|w| matches!(w, W::LabConverted { .. })), lab_lost, "{f:?} {l:?} {s:?}"); + } + } + } +} diff --git a/crates/image-codecs/tests/it/gif_frames.rs b/crates/image-codecs/tests/it/gif_frames.rs new file mode 100644 index 000000000..df7b29839 --- /dev/null +++ b/crates/image-codecs/tests/it/gif_frames.rs @@ -0,0 +1,168 @@ +//! `decode_gif_frames`: every frame of an animated GIF, composited, with its delay. + +use craft_image_codecs::{AnimationFrame, CodecError, DecodeOptions, DecodeWarning, Format, Limits, decode, decode_gif_frames}; + +const RED: [u8; 4] = [255, 0, 0, 255]; +const GREEN: [u8; 4] = [0, 255, 0, 255]; +const BLUE: [u8; 4] = [0, 0, 255, 255]; +const CLEAR: [u8; 4] = [0, 0, 0, 0]; + +fn frames(bytes: &[u8]) -> Vec { + decode_gif_frames(bytes, &DecodeOptions::default()).unwrap() +} + +fn pixels(f: &AnimationFrame) -> Vec { + f.image.to_rgba8() +} + +/// Solid full-screen frames written by the `image` crate's encoder, `delay_ms` apart. +fn solid_gif(colours: &[[u8; 4]], w: u32, h: u32, delay_ms: u32) -> Vec { + let mut out = Vec::new(); + let mut enc = image::codecs::gif::GifEncoder::new(&mut out); + let list = colours.iter().map(|c| { + let buf = image::RgbaImage::from_pixel(w, h, image::Rgba(*c)); + image::Frame::from_parts(buf, 0, 0, image::Delay::from_numer_denom_ms(delay_ms, 1)) + }); + enc.encode_frames(list).unwrap(); + drop(enc); + out +} + +/// A palette-index image LZW-packed with a 2-bit minimum code size. Few enough pixels that the +/// code size never grows past 3 bits. +fn lzw2(indices: &[u8]) -> Vec { + let mut codes = vec![4u8]; + codes.extend_from_slice(indices); + codes.push(5); + let (mut acc, mut nbits, mut out) = (0u32, 0, Vec::new()); + for c in codes { + acc |= u32::from(c) << nbits; + nbits += 3; + while nbits >= 8 { + out.push((acc & 0xFF) as u8); + acc >>= 8; + nbits -= 8; + } + } + if nbits > 0 { + out.push(acc as u8); + } + out +} + +/// One image of [`raw_gif`]: `(x, y, width, height, disposal method, palette indices)`. +type Part<'a> = (u16, u16, u16, u16, u8, &'a [u8]); + +/// A hand-built GIF89a: a `w`x`h` screen with the 4-colour global palette red, green, blue, +/// black, and one image per [`Part`]. Frames are 10 cs apart. +fn raw_gif(w: u16, h: u16, parts: &[Part]) -> Vec { + let mut b = b"GIF89a".to_vec(); + b.extend_from_slice(&w.to_le_bytes()); + b.extend_from_slice(&h.to_le_bytes()); + b.extend_from_slice(&[0b1000_0001, 0, 0]); + b.extend_from_slice(&[255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0]); + for &(x, y, fw, fh, disposal, indices) in parts { + // Graphic control extension: disposal method, 10 cs delay, no transparency. + b.extend_from_slice(&[0x21, 0xF9, 4, disposal << 2, 10, 0, 0, 0]); + b.push(0x2C); + for v in [x, y, fw, fh] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0, 2]); + let data = lzw2(indices); + b.push(data.len() as u8); + b.extend_from_slice(&data); + b.push(0); + } + b.push(0x3B); + b +} + +#[test] +fn a_still_gif_gives_one_frame_equal_to_decode() { + let still = solid_gif(&[RED], 3, 2, 100); + let all = frames(&still); + assert_eq!(all.len(), 1); + assert_eq!((all[0].image.width(), all[0].image.height()), (3, 2)); + assert_eq!(pixels(&all[0]), decode(&still).unwrap().to_rgba8()); + assert!(all[0].image.warnings.is_empty()); +} + +#[test] +fn every_frame_comes_back_in_order_with_its_delay() { + let all = frames(&solid_gif(&[RED, GREEN, BLUE], 2, 2, 120)); + assert_eq!(all.len(), 3); + for (f, want) in all.iter().zip([RED, GREEN, BLUE]) { + assert_eq!(pixels(f), want.repeat(4)); + assert_eq!(f.delay_ms, 120); + } +} + +#[test] +fn a_smaller_frame_is_drawn_over_the_previous_one() { + // Frame 2 is one pixel at x = 1, kept ("do not dispose", 1): the screen is red, blue. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 1, &[0, 0]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(all.len(), 2); + assert_eq!(pixels(&all[0]), [RED, RED].concat()); + assert_eq!(pixels(&all[1]), [RED, BLUE].concat()); + assert_eq!(all[1].delay_ms, 100); +} + +#[test] +fn disposal_to_background_clears_the_previous_frame() { + // Frame 1 (disposal 2, restore to background) covers the screen; frame 2 draws one pixel. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 2, &[1, 1]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(pixels(&all[0]), [GREEN, GREEN].concat()); + assert_eq!(pixels(&all[1]), [CLEAR, BLUE].concat()); +} + +#[test] +fn disposal_to_previous_restores_what_was_there() { + // Frame 1 is kept; frame 2 (disposal 3, restore previous) paints one pixel over it, so frame + // 3, which draws elsewhere, sees frame 1 again. + let all = frames(&raw_gif(2, 1, &[(0, 0, 2, 1, 1, &[0, 0]), (0, 0, 1, 1, 3, &[1]), (1, 0, 1, 1, 1, &[2])])); + assert_eq!(pixels(&all[1]), [GREEN, RED].concat()); + assert_eq!(pixels(&all[2]), [RED, BLUE].concat()); +} + +#[test] +fn a_damaged_later_frame_keeps_the_frames_before_it() { + let good = solid_gif(&[RED, GREEN, BLUE], 8, 8, 100); + // Cut inside the last frame's data, so frames 1 and 2 are intact. + let cut = &good[..good.len() - 12]; + let all = frames(cut); + assert!((1..3).contains(&all.len()), "{} frames", all.len()); + assert_eq!(all[0].image.warnings, [DecodeWarning::Truncated { format: Format::Gif }]); + assert!(all[1..].iter().all(|f| f.image.warnings.is_empty())); +} + +#[test] +fn the_first_frame_must_decode() { + assert!(decode_gif_frames(b"", &DecodeOptions::default()).is_err()); + assert!(decode_gif_frames(b"GIF89a", &DecodeOptions::default()).is_err()); + assert!(decode_gif_frames(&[0xFF; 64], &DecodeOptions::default()).is_err()); +} + +#[test] +fn every_prefix_of_a_gif_returns() { + let g = solid_gif(&[RED, GREEN, BLUE], 5, 4, 100); + for n in 0..g.len() { + let _ = decode_gif_frames(&g[..n], &DecodeOptions::default()); + } +} + +#[test] +fn dimension_limits_apply() { + let g = solid_gif(&[RED], 4, 4, 100); + let opts = DecodeOptions { limits: Limits { max_width: 3, ..Limits::default() }, ..Default::default() }; + assert!(matches!(decode_gif_frames(&g, &opts), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn the_byte_budget_covers_all_frames_together() { + // One 4x4 RGBA frame is 64 bytes: two frames fit in 128, three do not. + let limits = Limits { max_alloc: 128, ..Limits::default() }; + let opts = DecodeOptions { limits, ..Default::default() }; + assert_eq!(decode_gif_frames(&solid_gif(&[RED, GREEN], 4, 4, 100), &opts).unwrap().len(), 2); + assert!(matches!(decode_gif_frames(&solid_gif(&[RED, GREEN, BLUE], 4, 4, 100), &opts), Err(CodecError::LimitExceeded(_)))); +} diff --git a/crates/image-codecs/tests/it/heif.rs b/crates/image-codecs/tests/it/heif.rs new file mode 100644 index 000000000..c5cd36c23 --- /dev/null +++ b/crates/image-codecs/tests/it/heif.rs @@ -0,0 +1,194 @@ +//! HEIF/HEIC decoding against Apple's own encoder and decoder (features `corpus` and `heif`; +//! run with `cargo xtask test-corpus`). The fixtures are fetched into `corpus/heif/` by +//! `cargo xtask corpus --heif` (pins: `xtask/src/corpus_pins.rs`); a missing corpus fails. +//! Most come from heic-rs (`heic-rs/`, MIT OR Apache-2.0): encoded by macOS `sips` from synthetic +//! PNGs, each `.ref.png` being Apple's decode of the `.heic` next to it, the ground truth we +//! compare with. The 10-bit one is pillow-heif's (`pillow-heif/`, BSD-3-Clause), with the source +//! it was encoded from. + +#![cfg(all(feature = "corpus", feature = "heif"))] + +use crate::common; +use common::*; +use craft_image_codecs::*; +use std::sync::LazyLock; + +/// A file of `corpus/heif/`. +fn corpus(rel: &str) -> Vec { + let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/heif").join(rel); + std::fs::read(&p).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --all`", p.display())) +} + +static FILES: LazyLock<[Vec; 4]> = + LazyLock::new(|| ["heic-rs/checker-64.heic", "heic-rs/rgb-strips-96.heic", "heic-rs/checker-1024.heic", "heic-rs/with-exif.heic"].map(corpus)); + +fn checker_64() -> &'static [u8] { + &FILES[0] +} +fn strips_96() -> &'static [u8] { + &FILES[1] +} +/// iPhone-style grid of 512x512 tiles (2x2). +fn checker_grid() -> &'static [u8] { + &FILES[2] +} +fn with_exif() -> &'static [u8] { + &FILES[3] +} + +fn reference(png: &[u8]) -> Image { + decode_as(Format::Png, png).unwrap().convert(ChannelLayout::Rgb, SampleType::U8) +} + +/// `bytes` with the first `irot` box set to `angle` × 90° anticlockwise (every fixture has one, at 0). +fn with_irot(bytes: &[u8], angle: u8) -> Vec { + let at = bytes.windows(4).position(|w| w == b"irot").unwrap() + 4; + let mut b = bytes.to_vec(); + b[at] = angle; + b +} + +/// `bytes` with the big-endian EXIF Orientation entry (SHORT, count 1) set to `o`. +fn with_exif_orientation(bytes: &[u8], o: u8) -> Vec { + let at = bytes.windows(8).position(|w| w == [0x01, 0x12, 0, 3, 0, 0, 0, 1]).unwrap() + 9; + let mut b = bytes.to_vec(); + b[at] = o; + b +} + +fn keep() -> DecodeOptions { + DecodeOptions { keep_orientation: true, ..Default::default() } +} + +#[test] +fn heif_is_read_only_and_listed() { + let c = caps(Format::Heif); + assert!(c.read && !c.write); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(f, _)| *f == Format::Heif)); + for ext in ["heic", "HEIC", "photo.heif", "IMG_0001.HIF"] { + assert_eq!(from_extension(ext), Some(Format::Heif), "{ext}"); + } + let img = decode(checker_64()).unwrap(); + assert!(matches!(encode(&img, Format::Heif, &EncodeOptions::default()), Err(CodecError::Unsupported { .. }))); + assert_eq!(fidelity_warnings(&img, Format::Heif), vec![FidelityWarning::WriteUnsupported { format: Format::Heif }]); +} + +#[test] +fn detect_heif_brands() { + for f in [checker_64(), strips_96(), checker_grid(), with_exif()] { + assert_eq!(detect(f), Some(Format::Heif)); + } +} + +#[test] +fn single_picture_matches_apple_decode() { + for (heic, png) in [(checker_64(), "heic-rs/checker-64.ref.png"), (strips_96(), "heic-rs/rgb-strips-96.ref.png")] { + let img = decode(heic).unwrap(); + let want = reference(&corpus(png)); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.dimensions(), want.dimensions()); + assert!(max_abs_diff(&img, &want) <= 3.0 / 255.0, "max diff {}", max_abs_diff(&img, &want) * 255.0); + assert!(psnr(&img, &want) > 45.0); + } +} + +#[test] +fn grid_tiled_photo_matches_apple_decode() { + // iPhone photos are grids of 512x512 HEVC tiles: here 2x2. + let img = decode(checker_grid()).unwrap(); + let want = reference(&corpus("heic-rs/checker-1024.ref.png")); + assert_eq!(img.dimensions(), (1024, 1024)); + assert!(max_abs_diff(&img, &want) <= 3.0 / 255.0); +} + +#[test] +fn rgb_strips_keep_their_colours() { + // Three vertical strips, pure red, green and blue: catches swapped channels or matrices. + let img = decode(strips_96()).unwrap(); + for (x, c) in [(16, 0), (48, 1), (80, 2)] { + for ch in 0..3 { + let v = img.get(x, 16, ch); + assert!(if ch == c { v > 0.9 } else { v < 0.1 }, "strip at x={x} channel {ch} = {v}"); + } + } +} + +#[test] +fn container_rotation_is_applied_once() { + // irot 1 = 90° anticlockwise: the red strip (left) ends at the bottom, blue (right) on top. + let rotated = with_irot(strips_96(), 1); + let img = decode(&rotated).unwrap(); + assert_eq!(img.dimensions(), (32, 96)); + assert!(img.get(16, 8, 2) > 0.9 && img.get(16, 8, 0) < 0.1, "blue on top"); + assert!(img.get(16, 88, 0) > 0.9 && img.get(16, 88, 2) < 0.1, "red at the bottom"); + // keep_orientation hands back the pixels as coded. + assert_eq!(decode_with(&rotated, &keep()).unwrap().dimensions(), (96, 32)); +} + +#[test] +fn exif_and_xmp_survive_and_exif_orientation_is_not_applied_twice() { + let img = decode(with_exif()).unwrap(); + assert_eq!(img.dimensions(), (2048, 1536)); + let exif = img.meta.exif.as_deref().unwrap(); + assert!(exif.starts_with(b"MM\0*"), "TIFF-structured EXIF, without the HEIF offset header"); + assert!(img.meta.xmp.as_deref().unwrap().contains("x:xmpmeta")); + + // HEIF's EXIF Orientation repeats what irot says; the container is the authority. + let tagged = with_exif_orientation(with_exif(), 6); + let img = decode(&tagged).unwrap(); + assert_eq!(img.dimensions(), (2048, 1536), "not turned by the EXIF tag"); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "rewritten so exports don't turn it either"); + let raw = decode_with(&tagged, &keep()).unwrap(); + assert_eq!(exif_orientation(raw.meta.exif.as_deref().unwrap()), 6, "kept verbatim when asked"); +} + +#[test] +fn limits_are_checked_before_decoding() { + let tight = |limits| DecodeOptions { limits, ..Default::default() }; + let small = Limits { max_width: 512, ..Default::default() }; + assert!(matches!(decode_with(checker_grid(), &tight(small)), Err(CodecError::LimitExceeded(_)))); + let few = Limits { max_pixels: 1000, ..Default::default() }; + assert!(matches!(decode_with(checker_64(), &tight(few)), Err(CodecError::LimitExceeded(_)))); + let bytes = Limits { max_alloc: 64 * 64 * 3 - 1, ..Default::default() }; + assert!(matches!(decode_with(checker_64(), &tight(bytes)), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn unsupported_and_broken_files_are_clear_errors() { + // An ftyp and nothing else: a HEIF image sequence would have a moov here instead of a meta. + let r = decode(b"\0\0\0\x18ftypmsf1\0\0\0\0msf1hevc"); + assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("sequence")), "{r:?}"); + assert!(decode_as(Format::Heif, b"").is_err()); + assert!(decode_as(Format::Heif, &checker_grid()[..checker_grid().len() / 2]).is_err(), "half a file"); +} + +#[test] +fn every_truncation_errors_or_decodes_never_panics() { + for bytes in [checker_64(), strips_96(), checker_grid()] { + for cut in 0..bytes.len() { + let _ = decode_as(Format::Heif, &bytes[..cut]); + } + } +} + +#[test] +fn ten_bit_with_alpha_decodes_to_16_bit_rgba() { + // pillow-heif's fixture: a 16-bit RGBA gradient (the .src.png) encoded as 10-bit HEIF by libheif/x265. + let img = decode(&corpus("pillow-heif/heif/RGBA_10__29x100.heif")).unwrap(); + let src = decode_as(Format::Png, &corpus("pillow-heif/non_heif/RGBA_16__29x100.png")).unwrap(); + assert_eq!((img.dimensions(), img.layout(), img.sample_type()), ((29, 100), ChannelLayout::Rgba, SampleType::U16)); + // Depth is kept, not 8 bits stretched: most samples are not multiples of 257. + let samples = img.to_u16_samples().unwrap(); + assert!(samples.iter().filter(|v| *v % 257 != 0).count() > samples.len() / 2); + // The alpha plane is coded on its own and comes back almost exactly. + for y in 0..100 { + for x in 0..29 { + assert!((img.get(x, y, 3) - src.get(x, y, 3)).abs() < 0.002, "alpha at {x},{y}"); + } + } + // Colour went through 4:2:0 chroma on a 29-pixel-wide saturated gradient, which costs about + // 26 dB at any depth (the 8-bit encode of the same source scores the same, as does a second + // decoder); a decoder bug (range, matrix, bit shift) lands far below. + let rgb = |i: &Image| i.convert(ChannelLayout::Rgb, SampleType::U16); + assert!(psnr(&rgb(&img), &rgb(&src)) > 24.0, "{}", psnr(&rgb(&img), &rgb(&src))); +} diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs new file mode 100644 index 000000000..69e510151 --- /dev/null +++ b/crates/image-codecs/tests/it/limits.rs @@ -0,0 +1,670 @@ +//! Decompression-bomb guards. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn opts(l: Limits) -> DecodeOptions { + DecodeOptions { limits: l, ..Default::default() } +} + +fn is_limit(r: Result) -> bool { + matches!(r, Err(CodecError::LimitExceeded(_))) +} + +fn encoded(f: Format) -> Vec { + let img = synth(64, 48, ChannelLayout::Rgba, SampleType::U8, 1, 0.1); + encode(&img, f, &EncodeOptions::default()).unwrap() +} + +#[test] +fn max_pixels_enforced_for_every_format() { + for f in rw_formats() { + let b = encoded(f); + let l = Limits { max_pixels: 1000, ..Limits::default() }; + assert!(is_limit(decode_with(&b, &opts(l))), "{f:?}"); + let l = Limits { max_pixels: 64 * 48, ..Limits::default() }; + assert!(decode_with(&b, &opts(l)).is_ok(), "{f:?} exact limit should pass"); + } +} + +#[test] +fn max_width_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_width: 63, ..Limits::default() }; + assert!(is_limit(decode_with(&encoded(f), &opts(l))), "{f:?}"); + } +} + +#[test] +fn max_height_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_height: 47, ..Limits::default() }; + assert!(is_limit(decode_with(&encoded(f), &opts(l))), "{f:?}"); + } +} + +#[test] +fn max_alloc_enforced_for_every_format() { + for f in rw_formats() { + let l = Limits { max_alloc: 64 * 48, ..Limits::default() }; + assert!(decode_with(&encoded(f), &opts(l)).is_err(), "{f:?}"); + } +} + +#[test] +fn limits_none_accepts() { + for f in rw_formats() { + assert!(decode_with(&encoded(f), &opts(Limits::none())).is_ok(), "{f:?}"); + } +} + +fn png_chunk(ty: &[u8; 4], data: &[u8]) -> Vec { + let mut v = (data.len() as u32).to_be_bytes().to_vec(); + v.extend_from_slice(ty); + v.extend_from_slice(data); + let mut crc_in = ty.to_vec(); + crc_in.extend_from_slice(data); + v.extend_from_slice(&crc32(&crc_in).to_be_bytes()); + v +} + +fn crc32(d: &[u8]) -> u32 { + let mut c = 0xFFFF_FFFFu32; + for &b in d { + c ^= b as u32; + for _ in 0..8 { + c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 }; + } + } + !c +} + +#[test] +fn png_bomb_header_rejected_before_alloc() { + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&100_000u32.to_be_bytes()); + ihdr.extend_from_slice(&100_000u32.to_be_bytes()); + ihdr.extend_from_slice(&[16, 6, 0, 0, 0]); + let mut b = b"\x89PNG\r\n\x1a\n".to_vec(); + b.extend(png_chunk(b"IHDR", &ihdr)); + b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01])); + b.extend(png_chunk(b"IEND", &[])); + assert!(is_limit(decode(&b))); +} + +#[test] +fn png_header_at_the_default_limit_needs_a_plausible_file_size() { + // 32768 x 32768 x RGBA16 is exactly the default max_alloc: it passes the dimension limits, + // but a ~100 byte file cannot hold it (zlib tops out near 1032:1), so it is refused before + // the 8 GiB output buffer is allocated. + let mut ihdr = Vec::new(); + ihdr.extend_from_slice(&32768u32.to_be_bytes()); + ihdr.extend_from_slice(&32768u32.to_be_bytes()); + ihdr.extend_from_slice(&[16, 6, 0, 0, 0]); + let mut b = b"\x89PNG\r\n\x1a\n".to_vec(); + b.extend(png_chunk(b"IHDR", &ihdr)); + b.extend(png_chunk(b"IDAT", &[0x78, 0x9C, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01])); + b.extend(png_chunk(b"IEND", &[])); + assert!(is_limit(decode(&b))); +} + +#[test] +fn png_highly_compressible_image_still_decodes() { + let img = Image::from_u8(3000, 3000, ChannelLayout::Rgba, vec![255u8; 3000 * 3000 * 4]).unwrap(); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(b.len() < 100_000, "{}", b.len()); + assert_eq!(decode(&b).unwrap().width(), 3000); +} + +const PNG_TEXT_BUDGET: usize = 64 << 10; + +fn png_text_decode(bytes: &[u8], max_alloc: usize) -> Result { + decode_with(bytes, &opts(Limits { max_alloc: max_alloc as u64, ..Limits::default() })) +} + +fn zlib(text: &[u8]) -> Vec { + use std::io::Write; + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + encoder.write_all(text).unwrap(); + encoder.finish().unwrap() +} + +// zTXt and iTXt payloads supplied here are already compressed. +fn png_text_stream(ty: &[u8; 4], keyword: &[u8], payload: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + match ty { + b"tEXt" => {} + b"zTXt" => data.push(0), + b"iTXt" => data.extend_from_slice(&[1, 0, 0, 0]), + _ => panic!("unexpected text chunk"), + } + data.extend_from_slice(payload); + png_chunk(ty, &data) +} + +fn png_text(ty: &[u8; 4], keyword: &[u8], text: &[u8]) -> Vec { + if ty == b"tEXt" { png_text_stream(ty, keyword, text) } else { png_text_stream(ty, keyword, &zlib(text)) } +} + +fn png_with_text(before_idat: &[Vec], after_idat: &[Vec]) -> Vec { + let mut image = Vec::new(); + { + let mut encoder = png::Encoder::new(&mut image, 1, 1); + encoder.set_color(png::ColorType::Grayscale); + encoder.set_depth(png::BitDepth::Eight); + let mut writer = encoder.write_header().unwrap(); + writer.write_image_data(&[127]).unwrap(); + writer.finish().unwrap(); + } + let mut at = 8; + let mut idat = None; + let iend = loop { + let len = u32::from_be_bytes(image[at..at + 4].try_into().unwrap()) as usize; + let ty = &image[at + 4..at + 8]; + if ty == b"IDAT" && idat.is_none() { + idat = Some(at); + } + if ty == b"IEND" { + break at; + } + at += len + 12; + }; + let idat = idat.unwrap(); + let mut output = image[..idat].to_vec(); + for chunk in before_idat { + output.extend_from_slice(chunk); + } + output.extend_from_slice(&image[idat..iend]); + for chunk in after_idat { + output.extend_from_slice(chunk); + } + output.extend_from_slice(&image[iend..]); + output +} + +#[test] +fn png_compressed_text_bombs_obey_limits_before_and_after_idat() { + let bomb = vec![b'A'; PNG_TEXT_BUDGET * 8]; + for ty in [b"zTXt", b"iTXt"] { + for after_idat in [false, true] { + let chunks = [png_text(ty, b"Comment", &bomb)]; + let bytes = if after_idat { png_with_text(&[], &chunks) } else { png_with_text(&chunks, &[]) }; + assert!(bytes.len() < PNG_TEXT_BUDGET / 2, "fixture must fit comfortably within the compressed-input budget"); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET)), "{ty:?}, after IDAT={after_idat}"); + } + } +} + +#[test] +fn png_text_chunks_share_an_aggregate_budget() { + for text_len in [(PNG_TEXT_BUDGET - 3) / 3, PNG_TEXT_BUDGET / 3] { + let text = vec![b'A'; text_len]; + let before = [png_text(b"tEXt", b"A", &text), png_text(b"zTXt", b"B", &text)]; + let after = [png_text(b"iTXt", b"C", &text)]; + let result = png_text_decode(&png_with_text(&before, &after), PNG_TEXT_BUDGET); + if 3 * (text_len + 1) <= PNG_TEXT_BUDGET { + let image = result.unwrap(); + assert_eq!(image.meta.text.len(), 3); + assert!(image.meta.text.iter().all(|(_, value)| value.len() == text_len)); + } else { + assert!(is_limit(result), "chunks fitting individually must still share a budget"); + } + } +} + +#[test] +fn png_text_accepts_the_exact_utf8_budget() { + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + let text = vec![b'A'; PNG_TEXT_BUDGET - 1]; + let bytes = png_with_text(&[png_text(ty, b"K", &text)], &[]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("K".into(), String::from_utf8(text).unwrap())]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1)), "{ty:?}: keyword and text both count"); + } +} + +#[test] +fn png_plain_itxt_obeys_the_exact_budget_and_validates_fields() { + let mut payload = b"K\0\0\0en\0Keyword\0".to_vec(); + payload.extend_from_slice(&vec![b'A'; PNG_TEXT_BUDGET - 1]); + let bytes = png_with_text(&[], &[png_chunk(b"iTXt", &payload)]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("K".into(), "A".repeat(PNG_TEXT_BUDGET - 1))]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1))); + + let invalid_fields: &[&[u8]] = &[ + b"K\0\0\0\0\0\xFF", // Invalid text UTF-8. + b"K\0\0\0\xFF\0\0text", // Non-ASCII language tag. + b"K\0\0\0\0\xFF\0text", // Invalid translated-keyword UTF-8. + b"K\0\x02\0\0\0text", // Unknown compression flag. + b"K\0\x01\x01\0\0text", // Unknown compression method for compressed text. + b"K\0\0", // Missing compression method. + b"K\0\0\0\0text", // Missing translated-keyword separator. + ]; + for fields in invalid_fields { + let bytes = png_with_text(&[png_chunk(b"iTXt", fields)], &[png_text(b"tEXt", b"Good", b"retained")]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } +} + +#[test] +fn png_latin1_expansion_counts_keyword_and_text_utf8_bytes() { + let text_len = (PNG_TEXT_BUDGET - 2) / 2; + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + let text = if ty == b"iTXt" { "é".repeat(text_len).into_bytes() } else { vec![0xE9; text_len] }; + let bytes = png_with_text(&[], &[png_text(ty, &[0xE9], &text)]); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("é".into(), "é".repeat(text_len))]); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - 1)), "{ty:?}: Latin-1 expands when stored as UTF-8"); + } +} + +#[test] +fn png_duplicate_xmp_chunks_each_consume_budget() { + const KEYWORD: &[u8] = b"XML:com.adobe.xmp"; + let text_len = (PNG_TEXT_BUDGET - 2 * KEYWORD.len()) / 2; + for extra in [0, 1] { + let first = png_text(b"iTXt", KEYWORD, &vec![b'A'; text_len + extra]); + let last_text = vec![b'B'; text_len]; + let last = png_text(b"iTXt", KEYWORD, &last_text); + let result = png_text_decode(&png_with_text(&[first], &[last]), PNG_TEXT_BUDGET); + if extra == 0 { + let image = result.unwrap(); + assert_eq!(image.meta.xmp, Some(String::from_utf8(last_text).unwrap())); + assert!(image.meta.text.is_empty()); + } else { + assert!(is_limit(result), "replacing an XMP packet must not reset its budget charge"); + } + } +} + +#[test] +fn png_xmp_in_all_text_chunk_types_is_preserved() { + const XMP: &str = ""; + for ty in [b"tEXt", b"zTXt", b"iTXt"] { + for after_idat in [false, true] { + let chunks = [png_text(ty, b"XML:com.adobe.xmp", XMP.as_bytes())]; + let bytes = if after_idat { png_with_text(&[], &chunks) } else { png_with_text(&chunks, &[]) }; + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.xmp.as_deref(), Some(XMP), "{ty:?}, after IDAT: {after_idat}"); + assert!(image.meta.text.is_empty(), "XMP must not also be retained as ordinary text"); + let saved = encode(&image, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&saved).unwrap().meta.xmp, image.meta.xmp); + } + } +} + +#[test] +fn png_mixed_xmp_chunk_types_use_last_packet_and_share_budget() { + for first_ty in [b"tEXt", b"zTXt", b"iTXt"] { + for last_ty in [b"tEXt", b"zTXt", b"iTXt"] { + let first = png_text(first_ty, b"XML:com.adobe.xmp", b"first"); + let last = png_text(last_ty, b"XML:com.adobe.xmp", b"last"); + let bytes = png_with_text(&[first], &[last]); + let budget = 2 * b"XML:com.adobe.xmp".len() + b"first".len() + b"last".len(); + // Pixel decoding needs a larger limit, so check the shared budget with larger packets. + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.xmp.as_deref(), Some("last")); + assert!(image.meta.text.is_empty()); + let first = png_text(first_ty, b"XML:com.adobe.xmp", &vec![b'A'; PNG_TEXT_BUDGET - budget]); + let last = png_text(last_ty, b"XML:com.adobe.xmp", b"last"); + let bytes = png_with_text(&[first], &[last]); + assert!(png_text_decode(&bytes, PNG_TEXT_BUDGET - b"first".len()).is_ok()); + assert!(is_limit(png_text_decode(&bytes, PNG_TEXT_BUDGET - b"first".len() - 1))); + } + } +} + +#[test] +fn png_corrupt_text_is_still_skipped() { + let valid = zlib(b"discard"); + let mut bad_header = valid.clone(); + bad_header[0] = 0; + let truncated = valid[..valid.len() - 4].to_vec(); + for ty in [b"zTXt", b"iTXt"] { + for stream in [&bad_header, &truncated] { + let before = [png_text_stream(ty, b"Bad", stream)]; + let after = [png_text(b"tEXt", b"Good", b"retained")]; + let image = png_text_decode(&png_with_text(&before, &after), PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } + } + let invalid_utf8 = png_text(b"iTXt", b"Bad", &[0xFF]); + let image = png_text_decode(&png_with_text(&[], &[invalid_utf8]), PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); +} + +#[test] +fn png_text_with_bad_crc_and_text_after_iend_are_ignored() { + let bomb = vec![b'A'; PNG_TEXT_BUDGET * 8]; + for ty in [b"zTXt", b"iTXt"] { + let valid = png_text(ty, b"Comment", &bomb); + let mut bad_crc = valid.clone(); + *bad_crc.last_mut().unwrap() ^= 1; + for after_idat in [false, true] { + let bytes = if after_idat { png_with_text(&[], &[bad_crc.clone()]) } else { png_with_text(&[bad_crc.clone()], &[]) }; + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); + } + let mut bytes = png_with_text(&[], &[]); + bytes.extend_from_slice(&valid); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert!(image.meta.text.is_empty()); + } +} + +#[test] +fn png_malformed_chunk_tails_remain_tolerated_after_pixels() { + let base = png_with_text(&[png_text(b"tEXt", b"Good", b"retained")], &[png_chunk(b"vpAg", &[])]); + let iend = base.len() - 12; + let tails: &[&[u8]] = &[&[], b"\x00\x00\x00", b"\x00\x00\x00\x10iTXtK\0\0", b"\xFF\xFF\xFF\xFFiTXt"]; + for tail in tails { + let mut bytes = base[..iend].to_vec(); + bytes.extend_from_slice(tail); + let image = png_text_decode(&bytes, PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.dimensions(), (1, 1)); + assert_eq!(image.meta.text, vec![("Good".into(), "retained".into())]); + } +} + +#[test] +fn png_invalid_optional_text_does_not_spend_an_exhausted_budget() { + let full = png_text(b"tEXt", b"K", &vec![b'A'; PNG_TEXT_BUDGET - 1]); + let invalid = [png_chunk(b"iTXt", b"Bad\0\0\0\0\0\xFF"), png_text_stream(b"zTXt", b"Bad", b"invalid")]; + let image = png_text_decode(&png_with_text(&[full], &invalid), PNG_TEXT_BUDGET).unwrap(); + assert_eq!(image.meta.text.len(), 1); + assert_eq!(image.meta.text[0].1.len(), PNG_TEXT_BUDGET - 1); +} + +#[test] +fn pnm_bomb_header_rejected() { + assert!(is_limit(decode(b"P6\n200000 200000\n255\n\0\0\0"))); + assert!(is_limit(decode(b"P7\nWIDTH 99999\nHEIGHT 99999\nDEPTH 4\nMAXVAL 65535\nTUPLTYPE RGB_ALPHA\nENDHDR\n"))); + assert!(is_limit(decode(b"PF\n100000 100000\n-1.0\n"))); +} + +#[test] +fn pnm_header_alone_reserves_nothing_without_limits() { + // #1110: ASCII PGM/PPM reserved four bytes per declared sample before reading any. A + // 2³¹ × 2³¹ header asked for more than isize::MAX ("capacity overflow"), and PBM reserved + // 2⁶² bytes before finding the raster missing (an abort). + let none = opts(Limits::none()); + let side = 1u32 << 31; + for f in [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] { + assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); + } +} + +#[test] +fn hdr_header_sizes_that_overflow_u64_are_errors_without_limits() { + let b = b"#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y 2147483648 +X 2147483648\n"; + assert!(decode_with(b, &opts(Limits::none())).is_err()); +} + +#[test] +fn pnm_header_sizes_that_overflow_usize_are_errors_without_limits() { + let m = u32::MAX; + let none = opts(Limits::none()); + for f in [ + format!("P3\n{m} {m}\n255\n"), + format!("P6\n{m} {m}\n255\n"), + format!("P5\n{m} {m}\n65535\n"), + format!("PF\n{m} {m}\n-1.0\n"), + format!("P7\nWIDTH {m}\nHEIGHT {m}\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n"), + ] { + assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); + } +} + +#[test] +fn ascii_pnm_with_the_tightest_layout_still_decodes() { + // The capacity bound for #1110 must not cut off files with no slack: PBM digits need no + // separator, and the last ASCII sample needs no trailing whitespace. + assert_eq!(decode(b"P1\n3 1\n010").unwrap().data(), &[255, 0, 255]); + assert_eq!(decode(b"P2\n3 1\n255\n1 2 3").unwrap().data(), &[1, 2, 3]); + assert_eq!(decode(b"P3\n1 1\n255\n9 8 7").unwrap().data(), &[9, 8, 7]); +} + +#[test] +fn jpeg_bomb_header_rejected() { + // SOI + SOF0 claiming 65535x65535x3 with no scan data. + let b = [0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x11, 8, 0xFF, 0xFF, 0xFF, 0xFF, 3, 1, 0x11, 0, 2, 0x11, 0, 3, 0x11, 0, 0xFF, 0xD9]; + let l = Limits { max_pixels: 1 << 24, ..Limits::default() }; + assert!(is_limit(decode_with(&b, &opts(l)))); +} + +#[test] +fn tiff_bomb_header_rejected() { + // Minimal little-endian TIFF claiming 1_000_000 x 1_000_000 8-bit gray. + let mut b = b"II*\0\x08\0\0\0".to_vec(); + let entries: [(u16, u16, u32, u32); 6] = [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, count, val) in entries { + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(&val.to_le_bytes()); + } + b.extend_from_slice(&0u32.to_le_bytes()); + assert!(is_limit(decode(&b))); +} + +#[test] +fn limits_check_api() { + let l = Limits::default(); + assert!(l.check(100, 100, ChannelLayout::Rgba, SampleType::F32).is_ok()); + assert!(matches!(l.check(0, 1, ChannelLayout::Gray, SampleType::U8), Err(CodecError::InvalidImage(_)))); + assert!(matches!(l.check(u32::MAX, 1, ChannelLayout::Gray, SampleType::U8), Err(CodecError::LimitExceeded(_)))); + let l = Limits { max_alloc: 399, ..Limits::default() }; + assert!(l.check(10, 10, ChannelLayout::Rgba, SampleType::U8).is_err()); + assert!(l.check(10, 10, ChannelLayout::Rgb, SampleType::U8).is_ok()); +} + +/// #8597: a small file whose header declares about 3 GB, inside the default limits, is refused +/// by the size-against-file check before the decoder allocates its output. +fn refused_as_implausible(f: Format, b: &[u8]) { + match decode(b) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains("cannot hold"), "{f:?}: {m}"), + other => panic!("{f:?}: expected an implausible-size refusal, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +fn find(b: &[u8], needle: &[u8]) -> usize { + b.windows(needle.len()).position(|w| w == needle).unwrap() +} + +#[test] +fn jpeg_header_declaring_gigabytes_needs_a_plausible_file_size() { + let mut b = encoded(Format::Jpeg); + // SOF0: marker, length, precision, then height and width (big-endian). + let sof = find(&b, &[0xFF, 0xC0]); + b[sof + 5..sof + 7].copy_from_slice(&16000u16.to_be_bytes()); + b[sof + 7..sof + 9].copy_from_slice(&65000u16.to_be_bytes()); + // The decoder's own scan-size guard refuses it (one refusal path for truncated and complete + // files alike), before the backend allocates the frame. + match decode(&b) { + Err(e) => assert!(e.to_string().contains("too short for the frame"), "{e}"), + Ok(i) => panic!("decoded a {}×{} frame from {} bytes", i.width(), i.height(), b.len()), + } +} + +#[test] +fn image_crate_headers_declaring_gigabytes_need_a_plausible_file_size() { + let side = 30000u32; + let mut bmp = encoded(Format::Bmp); + bmp[18..22].copy_from_slice(&(side as i32).to_le_bytes()); + bmp[22..26].copy_from_slice(&(side as i32).to_le_bytes()); + refused_as_implausible(Format::Bmp, &bmp); + let mut qoi = encoded(Format::Qoi); + // The qoi crate refuses more than 400 M pixels itself, so 20000 x 20000 (1.6 GB). + qoi[4..8].copy_from_slice(&20000u32.to_be_bytes()); + qoi[8..12].copy_from_slice(&20000u32.to_be_bytes()); + refused_as_implausible(Format::Qoi, &qoi); + let mut gif = encoded(Format::Gif); + gif[6..8].copy_from_slice(&(side as u16).to_le_bytes()); + gif[8..10].copy_from_slice(&(side as u16).to_le_bytes()); + refused_as_implausible(Format::Gif, &gif); + // The frame too. Descriptor: separator, left, top, width, height. + let frame = find(&gif, &[0x2C, 0, 0, 0, 0, 64, 0, 48, 0]); + gif[frame + 5..frame + 7].copy_from_slice(&(side as u16).to_le_bytes()); + gif[frame + 7..frame + 9].copy_from_slice(&(side as u16).to_le_bytes()); + refused_as_implausible(Format::Gif, &gif); +} + +/// GIF89a with a two-colour global table and one frame at the top left holding no pixels or +/// one pixel (LZW minimum code size 2: clear, index 0, end of information). +fn tiny_gif(screen: (u16, u16), frame: (u16, u16)) -> Vec { + let mut b = b"GIF89a".to_vec(); + for v in [screen.0, screen.1] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0x80, 0, 0, 0, 0, 0, 255, 255, 255, 0x2C, 0, 0, 0, 0]); + for v in [frame.0, frame.1] { + b.extend_from_slice(&v.to_le_bytes()); + } + b.extend_from_slice(&[0, 2, 2, 0x44, 0x01, 0, 0x3B]); + b +} + +#[test] +fn a_gif_frame_much_smaller_than_its_screen_still_decodes() { + let img = decode(&tiny_gif((4000, 4000), (1, 1))).unwrap(); + assert_eq!((img.width(), img.height()), (4000, 4000)); +} + +#[test] +fn a_tiny_gif_frame_does_not_admit_a_gigabyte_screen() { + refused_as_implausible(Format::Gif, &tiny_gif((30000, 30000), (1, 1))); + // 324 MB of screen: past the 256 MiB that needs no data. + refused_as_implausible(Format::Gif, &tiny_gif((9000, 9000), (1, 1))); + refused_as_implausible(Format::Gif, &tiny_gif((30000, 30000), (0, 0))); + // A frame bigger than its screen is decoded at its own size. + refused_as_implausible(Format::Gif, &tiny_gif((100, 100), (40000, 40000))); +} + +#[test] +fn an_rle_bmp_ending_early_still_decodes() { + // 4000x4000 RLE8 whose only data is the end-of-bitmap escape: every pixel is left at index 0. + let data = 14 + 40 + 1024; + let mut b = b"BM".to_vec(); + for v in [data + 2, 0, data, 40, 4000, 4000] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&8u16.to_le_bytes()); + for v in [1, 2, 0, 0, 0, 0] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.resize(data, 0); + b.extend_from_slice(&[0, 1]); + let img = decode(&b).unwrap(); + assert_eq!((img.width(), img.height()), (4000, 4000)); +} + +#[test] +fn an_rle_bmp_canvas_past_the_unbacked_allowance_needs_data() { + // 30000x30000 RLE8 with only the end-of-bitmap escape: 2.7 GB of RGB from about 1 KB. + let data = 14 + 40 + 1024; + let mut b = b"BM".to_vec(); + for v in [data + 2, 0, data, 40, 30000, 30000] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.extend_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&8u16.to_le_bytes()); + for v in [1, 2, 0, 0, 0, 0] { + b.extend_from_slice(&(v as u32).to_le_bytes()); + } + b.resize(data, 0); + b.extend_from_slice(&[0, 1]); + refused_as_implausible(Format::Bmp, &b); +} + +#[test] +fn animated_gif_frames_need_a_plausible_file_size_too() { + let frames = |b: &[u8]| decode_gif_frames(b, &DecodeOptions::default()); + assert_eq!(frames(&tiny_gif((4000, 4000), (1, 1))).unwrap().len(), 1); + for gif in [tiny_gif((30000, 30000), (1, 1)), tiny_gif((100, 100), (40000, 40000))] { + match frames(&gif) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains("cannot hold"), "{m}"), + other => panic!("expected an implausible-size refusal, got {:?}", other.map(|f| f.len())), + } + } +} + +#[test] +fn pcx_header_declaring_gigabytes_needs_a_plausible_file_size() { + // 24-bit (three 8-bit planes) 65535 x 16384, with one row of data. + let mut b = vec![0u8; 128]; + b[0] = 0x0a; + b[1] = 5; + b[2] = 1; + b[3] = 8; + b[8..10].copy_from_slice(&65534u16.to_le_bytes()); + b[10..12].copy_from_slice(&16383u16.to_le_bytes()); + b[65] = 3; + b[66..68].copy_from_slice(&65535u16.to_le_bytes()); + b[68..70].copy_from_slice(&1u16.to_le_bytes()); + b.extend_from_slice(&[0xff, 0]); + // PCX's own minimum-packet check refuses it before the output is allocated. + match decode(&b) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("too short"), "{message}"), + other => panic!("expected a too-short refusal, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +#[test] +fn xpm_rows_shorter_than_the_header_declares_are_refused_before_allocating() { + let b = b"/* XPM */\nstatic char *x[] = {\n\"30000 30000 1 1\",\n\". c #000000\",\n\".\",\n};\n".to_vec(); + match decode(&b) { + Err(CodecError::Malformed { .. }) => {} + other => panic!("expected a malformed-row error, got {:?}", other.map(|i| (i.width(), i.height()))), + } +} + +#[test] +fn exr_header_declaring_gigabytes_needs_a_plausible_file_size() { + let mut b = encoded(Format::OpenExr); + // dataWindow: name, type, size, then xMin, yMin, xMax, yMax (little-endian i32). + let at = find(&b, b"dataWindow\0box2i\0") + b"dataWindow\0box2i\0".len() + 4; + b[at + 8..at + 12].copy_from_slice(&15999i32.to_le_bytes()); + b[at + 12..at + 16].copy_from_slice(&15999i32.to_le_bytes()); + refused_as_implausible(Format::OpenExr, &b); +} + +#[test] +fn a_dwab_exr_gets_a_looser_file_size_bound() { + // DWA passes 2^16:1 (a flat 8K frame with 12 alpha-scheme mattes is about 30 KB), so a DWAB + // part may declare 4 times more than a ZIP part from the same file, and no more. + let img = synth(1, 1, ChannelLayout::Rgba, SampleType::U8, 1, 0.1); + let mut b = encode(&img, Format::OpenExr, &EncodeOptions::default()).unwrap(); + let at = find(&b, b"dataWindow\0box2i\0") + b"dataWindow\0box2i\0".len() + 4; + let c = find(&b, b"compression\0compression\0") + b"compression\0compression\0".len() + 4; + // RGBA as f32: 16 bytes a pixel against a file of a few hundred bytes. + let size = |b: &mut Vec, side: i32| { + b[at + 8..at + 12].copy_from_slice(&(side - 1).to_le_bytes()); + b[at + 12..at + 16].copy_from_slice(&(side - 1).to_le_bytes()); + }; + let refused = |b: &[u8]| matches!(exr_info(b, &Limits::default()), Err(CodecError::LimitExceeded(m)) if m.contains("cannot hold")); + size(&mut b, 2048); + b[c] = 3; // ZIP + assert!(refused(&b), "{} bytes", b.len()); + for dwa in [8, 9] { + b[c] = dwa; // DWAA, DWAB + let parts = exr_info(&b, &Limits::default()).unwrap(); + assert_eq!((parts[0].width, parts[0].height), (2048, 2048)); + } + size(&mut b, 4096); + assert!(refused(&b), "{} bytes", b.len()); +} + +#[test] +fn highly_compressible_jpeg_and_gif_still_decode() { + let img = Image::from_u8(3000, 3000, ChannelLayout::Rgba, vec![255u8; 3000 * 3000 * 4]).unwrap(); + for f in [Format::Jpeg, Format::Gif, Format::Qoi, Format::OpenExr] { + let b = encode(&img, f, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&b).unwrap().width(), 3000, "{f:?}"); + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs new file mode 100644 index 000000000..f465a0cbb --- /dev/null +++ b/crates/image-codecs/tests/it/main.rs @@ -0,0 +1,39 @@ +//! The crate's integration tests, linked into one test binary. + +mod camera_raw; +mod common; +mod dds; +mod dds_write; +mod decode_warnings; +mod deep_exr; +mod detect_caps; +mod dpx; +mod exr_cryptomatte; +mod exr_multipart; +mod exr_stream; +mod fidelity; +mod gif_frames; +mod heif; +mod limits; +mod malformed; +mod metadata; +mod orientation; +mod pcx; +mod png_jpeg; +mod pnm_headers; +mod resolution; +mod roundtrip; +mod source_settings; +mod tga; +mod tiff_bands; +mod tiff_bench; +mod tiff_exif; +mod tiff_lab; +mod tiff_pages; +mod tiff_photoshop; +mod tiff_pyramid; +mod tiff_regressions; +mod tiff_ycbcr; +mod webp_frames; +mod webp_lossy; +mod xpm; diff --git a/crates/image-codecs/tests/it/malformed.rs b/crates/image-codecs/tests/it/malformed.rs new file mode 100644 index 000000000..ec06109e0 --- /dev/null +++ b/crates/image-codecs/tests/it/malformed.rs @@ -0,0 +1,270 @@ +//! Malformed input must produce errors, never panics or huge allocations. + +use crate::common; +use common::*; +use craft_image_codecs::*; +use proptest::prelude::*; + +fn tight() -> DecodeOptions { + DecodeOptions { limits: Limits { max_width: 4096, max_height: 4096, max_pixels: 1 << 22, max_alloc: 64 << 20 }, ..Default::default() } +} + +fn samples() -> Vec<(Format, Vec)> { + let mut v = Vec::new(); + for f in rw_formats() { + for (l, s) in [(ChannelLayout::Rgba, SampleType::U8), (ChannelLayout::Gray, SampleType::U16), (ChannelLayout::Rgb, SampleType::F32)] { + let mut img = synth(19, 13, l, s, 11, 0.3); + img.icc = Some(sample_icc(300)); + img.meta.exif = Some(sample_exif()); + img.meta.xmp = Some(SAMPLE_XMP.into()); + v.push((f, encode(&img, f, &EncodeOptions::default()).unwrap())); + } + } + // Read-only formats: Apple-encoded HEIC from `corpus/heif` (`cargo xtask corpus --heif`), a + // single picture and a grid of tiles. + #[cfg(all(feature = "corpus", feature = "heif"))] + for name in ["rgb-strips-96.heic", "checker-1024.heic"] { + let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/heif/heic-rs").join(name); + let bytes = std::fs::read(&p).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --all`", p.display())); + v.push((Format::Heif, bytes)); + } + // Extra variants: interlaced PNG, ASCII PNM, CMYK JPEG. + let img = synth(19, 13, ChannelLayout::Rgb, SampleType::U8, 1, 0.3); + v.push((Format::Png, encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap())); + v.push((Format::Pnm, b"P3\n2 2\n255\n0 1 2 3 4 5 6 7 8 9 10 11\n".to_vec())); + v.push((Format::Jpeg, encode(&synth(19, 13, ChannelLayout::Cmyk, SampleType::U8, 1, 0.3), Format::Jpeg, &EncodeOptions::default()).unwrap())); + v +} + +fn no_panic(f: Format, bytes: &[u8]) { + let _ = decode_with(bytes, &tight()); + let _ = decode_as_with(f, bytes, &tight()); +} + +#[test] +fn truncated_at_many_offsets() { + for (f, bytes) in samples() { + let n = bytes.len(); + let step = (n / 97).max(1); + let mut cuts: Vec = (0..n.min(80)).collect(); + cuts.extend((0..n).step_by(step)); + cuts.extend(n.saturating_sub(40)..n); + for cut in cuts { + no_panic(f, &bytes[..cut]); + } + } +} + +#[test] +fn truncated_files_mostly_error() { + // Chopping off the second half must not silently succeed for formats + // with length-checked rasters. + for (f, bytes) in samples() { + if matches!(f, Format::Png | Format::Pnm | Format::Fits | Format::Dpx | Format::Qoi | Format::OpenExr | Format::Tiff | Format::Bmp) { + let r = decode_as_with(f, &bytes[..bytes.len() / 2], &tight()); + assert!(r.is_err(), "{f:?} decoded half a file"); + } + // JPEG decodes leniently (the missing part grey) but never silently (#518). + if f == Format::Jpeg { + match decode_as_with(f, &bytes[..bytes.len() / 2], &tight()) { + Ok(img) => assert_eq!(img.warnings, [DecodeWarning::Truncated { format: f }]), + Err(e) => assert!(e.to_string().contains("before any image data"), "{e}"), + } + } + } +} + +#[test] +fn bit_flips_never_panic() { + let mut rng = common::Rng::new(42); + for (f, bytes) in samples() { + for _ in 0..150 { + let mut b = bytes.clone(); + let flips = 1 + (rng.next_u64() % 4) as usize; + for _ in 0..flips { + let i = (rng.next_u64() as usize) % b.len(); + b[i] ^= 1 << (rng.next_u64() % 8); + } + no_panic(f, &b); + } + } +} + +#[test] +fn byte_overwrites_in_header_never_panic() { + for (f, bytes) in samples() { + for i in 0..bytes.len().min(64) { + for v in [0x00, 0x7F, 0xFF] { + let mut b = bytes.clone(); + b[i] = v; + no_panic(f, &b); + } + } + } +} + +#[test] +fn empty_input_errors_for_every_format() { + for f in Format::ALL { + assert!(decode_as(f, &[]).is_err(), "{f:?}"); + } +} + +#[test] +fn pnm_specific_malformations() { + let cases: &[&[u8]] = &[ + b"P6\n", + b"P6\n2 2\n", + b"P6\n2 2\n255", + b"P6\n2 2\n0\n", + b"P6\n2 2\n70000\n", + b"P6\n-2 2\n255\n", + b"P6\n2 2\n255\n\x01\x02", + b"P5\n2 2\n65535\n\x00\x00\x00", + b"P2\n2 2\n10\n1 2 3 11\n", + b"P1\n2 2\n0 1 2 0\n", + b"P4\n9 2\n\x00", + b"P7\nWIDTH 2\nHEIGHT 2\nDEPTH 9\nMAXVAL 255\nENDHDR\n", + b"P7\nWIDTH 2\nHEIGHT 2\nMAXVAL 255\nENDHDR\n", + b"P7\nWIDTH 2\nBOGUS 3\nENDHDR\n", + b"PF\n2 2\n0.0\n", + b"PF\n2 2\nnan\n", + b"PF\n2 2\n-1.0\n\0\0\0\0", + b"P6 4294967295 4294967295 255\n", + ]; + for c in cases { + assert!(decode(c).is_err(), "{:?}", String::from_utf8_lossy(c)); + } +} + +#[test] +fn pnm_valid_variants() { + let img = decode(b"P1\n# comment\n3 2\n1 0 1\n0 1 0\n").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P1\n3 2\n101010").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P4\n3 2\n\xA0\x40").unwrap(); + assert_eq!(img.data(), &[0, 255, 0, 255, 0, 255]); + let img = decode(b"P2\n2 1\n15\n0 15\n").unwrap(); + assert_eq!(img.data(), &[0, 255]); + let img = decode(b"P2\n2 1\n1023\n0 1023\n").unwrap(); + assert_eq!(img.sample_type(), SampleType::U16); + assert_eq!(img.to_u16_samples().unwrap(), vec![0, 65535]); + let img = decode(b"P3\n1 1\n255\n1 2 3\n").unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgb, &[1u8, 2, 3][..])); + let img = decode(b"P7\nWIDTH 1\nHEIGHT 1\nDEPTH 2\nMAXVAL 255\nTUPLTYPE GRAYSCALE_ALPHA\nENDHDR\n\x10\x20").unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::GrayA, &[0x10u8, 0x20][..])); + let mut pfm = b"PF\n1 2\n1.0\n".to_vec(); // big-endian, bottom-up + for v in [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0] { + pfm.extend_from_slice(&v.to_be_bytes()); + } + let img = decode(&pfm).unwrap(); + assert_eq!(img.to_f32_samples().unwrap(), vec![4.0, 5.0, 6.0, 1.0, 2.0, 3.0]); +} + +#[test] +fn jpeg_marker_garbage() { + let cases: &[&[u8]] = &[ + &[0xFF, 0xD8, 0xFF], + &[0xFF, 0xD8, 0xFF, 0xE0, 0x00], + &[0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x01], + &[0xFF, 0xD8, 0xFF, 0xE2, 0x00, 0x10, b'I', b'C', b'C', b'_', b'P', b'R', b'O', b'F', b'I', b'L', b'E', 0, 1, 1], + &[0xFF, 0xD8, 0xFF, 0xC0, 0x00, 0x0B, 8, 0xFF, 0xFF, 0xFF, 0xFF, 3, 0, 0, 0], + &[0xFF, 0xD8, 0xFF, 0xD9], + ]; + for c in cases { + assert!(decode(c).is_err()); + } +} + +proptest! { + #![proptest_config(ProptestConfig { cases: 400, .. ProptestConfig::default() })] + + #[test] + fn random_bytes_never_panic(data in proptest::collection::vec(any::(), 0..2048)) { + let _ = decode_with(&data, &tight()); + } + + #[test] + fn random_bytes_with_magic_never_panic(idx in 0usize..14, data in proptest::collection::vec(any::(), 0..1024)) { + let magics: [&[u8]; 14] = [ + b"\x89PNG\r\n\x1a\n", &[0xFF, 0xD8, 0xFF], b"II*\0", b"RIFF\0\0\0\0WEBP", b"GIF89a", b"BM", + &[0, 0, 1, 0, 1, 0], b"P6\n", b"qoif", &[0x76, 0x2F, 0x31, 0x01], b"#?RADIANCE\n", b"P7\n", b"PF\n", + b"\0\0\0\x18ftypheic\0\0\0\0mif1heic", + ]; + let mut b = magics[idx].to_vec(); + b.extend_from_slice(&data); + let _ = decode_with(&b, &tight()); + } + + #[test] + fn random_bytes_each_format_never_panic(idx in 0usize..Format::ALL.len(), data in proptest::collection::vec(any::(), 0..512)) { + let _ = decode_as_with(Format::ALL[idx], &data, &tight()); + } + + #[test] + fn mutated_valid_files_never_panic(which in 0usize..40, pos in any::(), val in any::(), cut in any::()) { + let s = samples_cached(); + let (f, bytes) = &s[which % s.len()]; + let mut b = bytes.clone(); + let p = pos % b.len(); + b[p] = val; + let c = cut % (b.len() + 1); + let _ = decode_as_with(*f, &b[..c.max(p + 1).min(b.len())], &tight()); + } +} + +fn samples_cached() -> &'static [(Format, Vec)] { + static S: std::sync::OnceLock)>> = std::sync::OnceLock::new(); + S.get_or_init(samples) +} + +/// A little-endian TIFF with one 1x1 8-bit grey strip and the given extra IFD entries +/// (tag, type, count, value), sorted into place. +fn tiny_tiff(extra: &[(u16, u16, u32, u32)]) -> Vec { + const SHORT: u16 = 3; + const LONG: u16 = 4; + let mut entries = vec![ + (256, SHORT, 1, 1), // ImageWidth + (257, SHORT, 1, 1), // ImageLength + (258, SHORT, 1, 8), // BitsPerSample + (259, SHORT, 1, 1), // Compression: none + (262, SHORT, 1, 1), // Photometric: BlackIsZero + (273, LONG, 1, 0), // StripOffsets (patched below) + (277, SHORT, 1, 1), // SamplesPerPixel + (278, SHORT, 1, 1), // RowsPerStrip + (279, LONG, 1, 1), // StripByteCounts + ]; + entries.extend_from_slice(extra); + entries.sort_by_key(|e| e.0); + let ifd_len = 2 + 12 * entries.len() + 4; + let strip = 8 + ifd_len as u32; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, ty, count, value) in entries { + let value = if tag == 273 { strip } else { value }; + b.extend_from_slice(&tag.to_le_bytes()); + b.extend_from_slice(&ty.to_le_bytes()); + b.extend_from_slice(&count.to_le_bytes()); + b.extend_from_slice(&value.to_le_bytes()); + } + b.extend_from_slice(&0u32.to_le_bytes()); + b.push(0x80); + b +} + +#[test] +fn tiff_fixture_decodes() { + let img = decode_as_with(Format::Tiff, &tiny_tiff(&[]), &tight()).unwrap(); + assert_eq!((img.width(), img.height()), (1, 1)); +} + +/// Regression: an empty SampleFormat tag made the tiff 0.10 decoder index an empty list +/// (`index out of bounds: the len is 0`). Found by mutation fuzzing; fixed by tiff 0.11. +#[test] +fn tiff_empty_sample_format_is_an_error() { + let bytes = tiny_tiff(&[(339, 3, 0, 0)]); + assert!(decode_as_with(Format::Tiff, &bytes, &tight()).is_err()); + assert!(decode_with(&bytes, &tight()).is_err()); +} diff --git a/crates/image-codecs/tests/it/metadata.rs b/crates/image-codecs/tests/it/metadata.rs new file mode 100644 index 000000000..d6d292557 --- /dev/null +++ b/crates/image-codecs/tests/it/metadata.rs @@ -0,0 +1,340 @@ +//! ICC / EXIF / XMP / DPI / text preservation, per format, per caps. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn rich_image(layout: ChannelLayout) -> Image { + let mut img = test_image(layout, SampleType::U8); + img.icc = Some(sample_icc(3000)); + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some(SAMPLE_XMP.to_owned()), + dpi: Some((300.0, 150.0)), + text: vec![("Description".into(), "synthetic test image".into()), ("Software".into(), "photocraft".into())], + ..Default::default() + }; + img +} + +fn check_format(format: Format) { + let c = caps(format); + if !(c.read && c.write) { + return; + } + let layout = if c.layouts.contains(&ChannelLayout::Rgb) { ChannelLayout::Rgb } else { c.layouts[0] }; + let img = rich_image(layout); + let bytes = encode(&img, format, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + let warnings = fidelity_warnings(&img, format); + + assert_eq!(back.icc.is_some(), c.icc, "{format:?} icc presence"); + if c.icc { + assert_eq!(back.icc, img.icc, "{format:?} icc bytes"); + } else { + assert!(warnings.contains(&FidelityWarning::IccDropped), "{format:?}"); + } + if c.exif && format == Format::Tiff { + // TIFF writes the block's directories as its own and rebuilds a block from them on + // reading (tests/it/tiff_exif.rs); IFD0's orientation here is the image's. + assert!(!warnings.contains(&FidelityWarning::ExifDropped), "{format:?}"); + } else if c.exif && format == Format::WebP { + // WebP's resolution lives in EXIF, so the block comes back with resolution tags added. + let back_exif = back.meta.exif.as_deref().unwrap_or_else(|| panic!("{format:?} lost exif")); + assert_eq!(craft_image_codecs::resolution::exif_resolution(back_exif).map(|(x, y)| (x.round(), y.round())), Some((300.0, 150.0))); + // The rest of the block survives too: the Orientation tag (0x0112, little-endian). + assert!(back_exif.windows(2).any(|w| w == b"\x12\x01"), "{format:?} lost Orientation"); + } else if c.exif { + assert_eq!(back.meta.exif, img.meta.exif, "{format:?} exif"); + } else { + assert!(warnings.contains(&FidelityWarning::ExifDropped), "{format:?}"); + } + if c.xmp { + assert_eq!(back.meta.xmp, img.meta.xmp, "{format:?} xmp"); + } else { + assert!(back.meta.xmp.is_none()); + assert!(warnings.contains(&FidelityWarning::XmpDropped), "{format:?}"); + } + if c.dpi { + let (x, y) = back.meta.dpi.unwrap_or_else(|| panic!("{format:?} lost dpi")); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "{format:?} dpi {x},{y}"); + } else { + assert!(warnings.contains(&FidelityWarning::DpiDropped), "{format:?}"); + } + if c.text { + for kv in &img.meta.text { + assert!(back.meta.text.contains(kv), "{format:?} lost text {kv:?}: {:?}", back.meta.text); + } + } else { + assert!(back.meta.text.is_empty()); + assert!(warnings.contains(&FidelityWarning::TextDropped), "{format:?}"); + } +} + +#[test] +fn png_metadata() { + check_format(Format::Png); +} +#[test] +fn jpeg_metadata() { + check_format(Format::Jpeg); +} +#[test] +fn tiff_metadata() { + check_format(Format::Tiff); +} +#[test] +fn webp_metadata() { + check_format(Format::WebP); +} +#[test] +fn gif_metadata() { + check_format(Format::Gif); +} +#[test] +fn bmp_metadata() { + check_format(Format::Bmp); +} +#[test] +fn tga_metadata() { + check_format(Format::Tga); +} +#[test] +fn ico_metadata() { + check_format(Format::Ico); +} +#[test] +fn fits_metadata() { + check_format(Format::Fits); +} +#[test] +fn pnm_metadata() { + check_format(Format::Pnm); +} +#[test] +fn qoi_metadata() { + check_format(Format::Qoi); +} +#[test] +fn exr_metadata() { + check_format(Format::OpenExr); +} +#[test] +fn hdr_metadata() { + check_format(Format::Hdr); +} + +#[test] +fn metadata_on_16bit_png_and_tiff() { + for f in [Format::Png, Format::Tiff] { + let mut img = test_image(ChannelLayout::Rgba, SampleType::U16); + img.icc = Some(sample_icc(512)); + img.meta.dpi = Some((72.0, 72.0)); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.sample_type(), SampleType::U16); + } +} + +#[test] +fn icc_on_cmyk_tiff_and_jpeg() { + for f in [Format::Tiff, Format::Jpeg] { + let img = test_image(ChannelLayout::Cmyk, SampleType::U8).with_icc(Some(sample_icc(900))); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk, "{f:?}"); + assert_eq!(back.icc, img.icc, "{f:?}"); + } +} + +#[test] +fn cmyk_icc_not_written_to_rgb_format() { + let img = test_image(ChannelLayout::Cmyk, SampleType::U8).with_icc(Some(sample_icc(900))); + let back = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.icc.is_none(), "a CMYK profile must not be attached to converted RGB data"); + assert!(fidelity_warnings(&img, Format::Png).contains(&FidelityWarning::IccDropped)); +} + +#[test] +fn jpeg_multi_segment_icc() { + // > 65519 bytes forces several APP2 segments. + for len in [65519, 65520, 150_000, 300_001] { + let img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(len))); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let app2 = bytes.windows(14).filter(|w| w[0] == 0xFF && w[1] == 0xE2 && &w[4..14] == b"ICC_PROFIL").count(); + assert_eq!(app2, len.div_ceil(65519), "segments for {len}"); + assert_eq!(decode(&bytes).unwrap().icc.map(|v| v.len()), Some(len)); + // Oracle: the image crate reads the same profile. + use image::ImageDecoder; + let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); + } +} + +/// A layered document's XMP can list every document ever placed in it and the text of every type +/// layer: far more than the 64 KB one JPEG segment holds. The JPEG is still written, with that +/// bookkeeping left out of the XMP; metadata too large even then is dropped with a warning. +#[test] +fn jpeg_with_oversized_metadata_still_encodes() { + let ancestors: String = (0..3000).map(|i| format!("xmp.did:{i:032x}")).collect(); + let texts: String = (0..400) + .map(|i| format!("Layer {i}Words on layer {i}")) + .collect(); + let xmp = format!( + "\n{ancestors}{texts}" + ); + assert!(xmp.len() > 120_000, "{}", xmp.len()); + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut img = test_image(ChannelLayout::Rgb, sample); + img.meta.xmp = Some(xmp.clone()); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap().meta.xmp.unwrap(); + assert!(back.contains("xmp:CreatorTool=\"synthetic\"") && back.ends_with(""), "{sample:?}"); + assert!(!back.contains("DocumentAncestors>") && !back.contains("TextLayers"), "{sample:?}"); + assert!(back.contains(""), "{sample:?}: a longer name is not the element"); + assert!(!fidelity_warnings(&img, Format::Jpeg).iter().any(|w| matches!(w, FidelityWarning::MetadataTooLarge { .. }))); + // Formats without a segment limit keep the packet whole. + assert_eq!(decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap().meta.xmp.as_deref(), Some(xmp.as_str())); + } + // Still too large: the JPEG is written without it. + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + let big = format!("", "x".repeat(70_000)); + let mut exif = sample_exif(); + exif.resize(70_000, 0); + img.meta.xmp = Some(big.clone()); + img.meta.exif = Some(exif); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.xmp.is_none() && back.meta.exif.is_none()); + let w = fidelity_warnings(&img, Format::Jpeg); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "XMP", bytes: big.len() }), "{w:?}"); + assert!(w.contains(&FidelityWarning::MetadataTooLarge { what: "EXIF", bytes: 70_000 }), "{w:?}"); +} + +#[test] +fn embed_flags_disable_metadata() { + let img = rich_image(ChannelLayout::Rgb); + let opts = EncodeOptions { embed_icc: false, embed_metadata: false, ..Default::default() }; + for f in [Format::Png, Format::Jpeg, Format::Tiff, Format::WebP] { + let back = decode(&encode(&img, f, &opts).unwrap()).unwrap(); + assert!(back.icc.is_none(), "{f:?}"); + assert!(back.meta.exif.is_none() && back.meta.xmp.is_none(), "{f:?}"); + assert!(back.meta.text.is_empty(), "{f:?}"); + assert!(fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped))); + } +} + +#[test] +fn png_unicode_text_uses_itxt() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.text = vec![("Title".into(), "Ünïcödé ☃ 雪".into()), ("Author".into(), "latin1 ok é".into())]; + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(4).any(|w| w == b"iTXt")); + assert!(bytes.windows(4).any(|w| w == b"tEXt")); + let mut back = decode(&bytes).unwrap().meta.text; + let mut want = img.meta.text.clone(); + back.sort(); + want.sort(); + assert_eq!(back, want); +} + +#[test] +fn png_xmp_in_itxt_keyword() { + let mut img = test_image(ChannelLayout::Gray, SampleType::U8); + img.meta.xmp = Some(SAMPLE_XMP.into()); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(17).any(|w| w == b"XML:com.adobe.xmp")); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.xmp.as_deref(), Some(SAMPLE_XMP)); + assert!(back.meta.text.is_empty(), "XMP must not leak into text"); +} + +#[test] +fn png_exif_chunk_written() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.exif = Some(sample_exif()); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert!(bytes.windows(4).any(|w| w == b"eXIf")); + assert!(!bytes.windows(4).any(|w| w == b"iCCP")); +} + +#[test] +fn jpeg_exif_readable_by_oracle() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta.exif = Some(sample_exif()); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::jpeg::JpegDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + let exif = d.exif_metadata().unwrap().unwrap(); + assert!(exif.ends_with(&sample_exif())); +} + +#[test] +fn jpeg_dpi_in_jfif() { + let mut img = test_image(ChannelLayout::Gray, SampleType::U8); + img.meta.dpi = Some((96.0, 200.0)); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.meta.dpi, Some((96.0, 200.0))); +} + +#[test] +fn tiff_icc_readable_by_oracle() { + let img = test_image(ChannelLayout::Rgb, SampleType::U16).with_icc(Some(sample_icc(1234))); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::tiff::TiffDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); +} + +#[test] +fn png_icc_readable_by_oracle() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8).with_icc(Some(sample_icc(777))); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + use image::ImageDecoder; + let mut d = image::codecs::png::PngDecoder::new(std::io::Cursor::new(&bytes)).unwrap(); + assert_eq!(d.icc_profile().unwrap(), img.icc); +} + +#[test] +fn metadata_survives_depth_conversion() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::F32).with_icc(Some(sample_icc(300))); + img.meta.xmp = Some(SAMPLE_XMP.into()); + let back = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.meta.xmp, img.meta.xmp); +} + +/// #2399: a WebP export keeps the resolution even when the image has no EXIF (WebP has no +/// resolution field of its own, so a minimal EXIF block carries it), lossless and lossy alike, +/// and no longer warns that it is dropped. +#[test] +fn webp_keeps_dpi_without_source_exif() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); + img.meta = Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }; + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + for lossless in [true, false] { + let opts = EncodeOptions { webp_lossless: lossless, ..Default::default() }; + let back = decode(&encode(&img, Format::WebP, &opts).unwrap()).unwrap(); + let (x, y) = back.meta.dpi.unwrap_or_else(|| panic!("lossless {lossless}: dpi lost")); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "lossless {lossless}: {x},{y}"); + } + // EXIF that can't be parsed is written as it is, so the resolution is reported as dropped. + img.meta = Metadata { exif: Some(b"Exif\0\0garbage".to_vec()), dpi: Some((300.0, 150.0)), ..Default::default() }; + assert!(fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.exif.is_some()); + // ...unless the XMP written beside it records the resolution, which readers look at first. + img.meta.xmp = Some(r#""#.into()); + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + let (x, y) = back.meta.dpi.expect("dpi from XMP"); + assert!((x - 300.0).abs() < 0.05 && (y - 150.0).abs() < 0.05, "{x},{y}"); + // The default 72 ppi needs no EXIF (a WebP without one opens at 72), so a plain export stays + // a simple-format file; nor does an image without a resolution. + img.meta = Metadata { dpi: Some((72.0, 72.0)), ..Default::default() }; + assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).unwrap(); + assert_eq!(bytes.get(12..16), Some(&b"VP8 "[..])); + // Without a resolution or EXIF there is still no EXIF chunk. + img.meta = Metadata::default(); + let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(back.meta.exif.is_none()); +} diff --git a/crates/image-codecs/tests/it/orientation.rs b/crates/image-codecs/tests/it/orientation.rs new file mode 100644 index 000000000..c278f36f6 --- /dev/null +++ b/crates/image-codecs/tests/it/orientation.rs @@ -0,0 +1,670 @@ +//! EXIF / TIFF orientation (#285): files are opened upright, and every export +//! writes Orientation = 1 so the upright pixels are never rotated twice. + +use crate::common; +use common::*; +use craft_image_codecs::*; +use proptest::prelude::{any, prop_assert, prop_assert_eq, proptest}; + +/// A minimal EXIF (TIFF-structured) block: a filler tag, then Orientation. +fn exif(o: u32, big: bool, long: bool) -> Vec { + let u16b = |v: u16| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut v = if big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&u32b(8)); + v.extend_from_slice(&u16b(2)); + // ImageDescription-ish filler (ASCII, 4 bytes inline). + v.extend_from_slice(&u16b(0x010E)); + v.extend_from_slice(&u16b(2)); + v.extend_from_slice(&u32b(4)); + v.extend_from_slice(b"abc\0"); + v.extend_from_slice(&u16b(0x0112)); + if long { + v.extend_from_slice(&u16b(4)); + v.extend_from_slice(&u32b(1)); + v.extend_from_slice(&u32b(o)); + } else { + v.extend_from_slice(&u16b(3)); + v.extend_from_slice(&u32b(1)); + v.extend_from_slice(&u16b(o as u16)); + v.extend_from_slice(&[0, 0]); + } + v.extend_from_slice(&u32b(0)); + v +} + +/// The "upright" test picture: `w`×`h` gray values, distinct per pixel. +fn upright(w: usize, h: usize, value: impl Fn(usize, usize) -> u8) -> Vec { + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| value(x, y)).collect() +} + +/// What a camera stores for orientation `o` when the scene is `up` (`w`×`h`): the inverse +/// of the turn a viewer applies. Returns the stored pixels and their size. +fn stored(up: &[u8], w: usize, h: usize, o: u16) -> (Vec, usize, usize) { + let (sw, sh) = if o >= 5 { (h, w) } else { (w, h) }; + let mut s = vec![0u8; up.len()]; + for y in 0..h { + for x in 0..w { + // Where upright pixel (x, y) sits in the stored image. + let (sx, sy) = match o { + 1 => (x, y), + 2 => (w - 1 - x, y), // mirrored horizontally + 3 => (w - 1 - x, h - 1 - y), // rotated 180° + 4 => (x, h - 1 - y), // mirrored vertically + 5 => (y, x), // transposed + 6 => (y, w - 1 - x), // viewer turns it 90° clockwise + 7 => (h - 1 - y, w - 1 - x), // transverse + 8 => (h - 1 - y, x), // viewer turns it 90° counter-clockwise + _ => unreachable!(), + }; + s[sy * sw + sx] = up[y * w + x]; + } + } + (s, sw, sh) +} + +/// Inserts a JPEG APP1 EXIF segment right after SOI. +fn jpeg_with_exif(jpeg: &[u8], exif: &[u8]) -> Vec { + let mut seg = b"Exif\0\0".to_vec(); + seg.extend_from_slice(exif); + let mut out = jpeg[..2].to_vec(); + out.extend_from_slice(&[0xFF, 0xE1]); + out.extend_from_slice(&((seg.len() + 2) as u16).to_be_bytes()); + out.extend_from_slice(&seg); + out.extend_from_slice(&jpeg[2..]); + out +} + +/// Inserts a PNG eXIf chunk before the first IDAT. +fn png_with_exif(png: &[u8], exif: &[u8]) -> Vec { + let at = png.windows(4).position(|w| w == b"IDAT").unwrap() - 4; + let mut chunk = (exif.len() as u32).to_be_bytes().to_vec(); + let mut body = b"eXIf".to_vec(); + body.extend_from_slice(exif); + let mut crc = flate2::Crc::new(); + crc.update(&body); + chunk.extend_from_slice(&body); + chunk.extend_from_slice(&crc.sum().to_be_bytes()); + let mut out = png[..at].to_vec(); + out.extend_from_slice(&chunk); + out.extend_from_slice(&png[at..]); + out +} + +fn tiff_with_orientation(data: &[u8], w: u32, h: u32, o: u16) -> Vec { + use tiff::encoder::{TiffEncoder, colortype::Gray8}; + let mut cursor = std::io::Cursor::new(Vec::new()); + let mut enc = TiffEncoder::new(&mut cursor).unwrap(); + let mut im = enc.new_image::(w, h).unwrap(); + im.encoder().write_tag(tiff::tags::Tag::Orientation, o).unwrap(); + im.write_data(data).unwrap(); + cursor.into_inner() +} + +fn gray(w: usize, h: usize, px: Vec) -> Image { + Image::from_u8(w as u32, h as u32, ChannelLayout::Gray, px).unwrap() +} + +/// Exact pixels: distinct values per pixel. +fn exact_scene(w: usize, h: usize) -> Vec { + upright(w, h, |x, y| (y * w + x) as u8 * 3 + 1) +} + +#[test] +fn reads_orientation_in_every_encoding() { + for o in 1..=8 { + for big in [false, true] { + for long in [false, true] { + let e = exif(o, big, long); + assert_eq!(exif_orientation(&e), o as u16, "o={o} big={big} long={long}"); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&e); + assert_eq!(exif_orientation(&prefixed), o as u16); + let fixed = upright_exif(&prefixed); + assert_eq!(exif_orientation(&fixed), 1); + assert_eq!(fixed.len(), prefixed.len(), "rewritten in place"); + assert_eq!(&fixed[..6], b"Exif\0\0"); + // Rewritten in the entry's own type and width: exactly `exif(1, ..)`. + assert_eq!(&fixed[6..], &exif(1, big, long)[..], "o={o} big={big} long={long}"); + } + } + } + // Already upright, no tag, or garbage: borrowed unchanged. + assert!(matches!(upright_exif(&exif(1, false, false)), std::borrow::Cow::Borrowed(_))); + assert!(matches!(upright_exif(&sample_exif()), std::borrow::Cow::Borrowed(_))); + assert!(matches!(upright_exif(b"nonsense"), std::borrow::Cow::Borrowed(_))); +} + +#[test] +fn malformed_or_out_of_range_orientation_reads_as_1() { + for bad in [0, 9, 255, 0xFFFF, 0x1_0006] { + assert_eq!(exif_orientation(&exif(bad, false, true)), 1, "{bad}"); + } + let good = exif(6, false, false); + assert_eq!(exif_orientation(&good), 6); + for n in 0..good.len() { + // Any cut, even one that keeps the Orientation entry but loses the next-IFD pointer. + assert_eq!(exif_orientation(&good[..n]), 1, "truncated at {n}"); + assert!(matches!(upright_exif(&good[..n]), std::borrow::Cow::Borrowed(_)), "truncated at {n}"); + } + // IFD offset past the end, a huge entry count, a wrong field type. + let mut far = good.clone(); + far[4..8].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(exif_orientation(&far), 1); + let mut many = good.clone(); + many[8..10].copy_from_slice(&u16::MAX.to_le_bytes()); + assert_eq!(exif_orientation(&many), 1, "an entry count past the end is a truncated table"); + let mut typed = good.clone(); + typed[24..26].copy_from_slice(&2u16.to_le_bytes()); // ASCII + assert_eq!(exif_orientation(&typed), 1); + assert_eq!(exif_orientation(&[]), 1); + assert_eq!(exif_orientation(b"II*\0"), 1); +} + +proptest! { + #[test] + fn random_exif_never_panics(bytes in proptest::collection::vec(any::(), 0..256), le in any::()) { + let mut b = if le { b"II*\0\x08\0\0\0".to_vec() } else { b"MM\0*\0\0\0\x08".to_vec() }; + b.extend_from_slice(&bytes); + let o = exif_orientation(&b); + prop_assert!((1..=8).contains(&o)); + prop_assert_eq!(exif_orientation(&upright_exif(&b)), 1); + let _ = exif_orientation(&bytes); + let _ = upright_exif(&bytes); + } + + #[test] + fn random_xmp_never_panics(s in ".{0,64}", tail in ".{0,16}") { + let x = format!("{s}tiff:Orientation{tail}"); + let _ = upright_xmp(&x); + } +} + +#[test] +fn xmp_orientation_is_rewritten() { + let attr = r#""#; + assert_eq!(upright_xmp(attr), r#""#); + let elem = "83"; + assert_eq!(upright_xmp(elem), "11"); + assert_eq!(upright_xmp("tiff:Orientation='12'"), "tiff:Orientation='1'"); + for same in [r#"tiff:Orientation="1""#, "no orientation", "tiff:Orientation", "tiff:Orientation=\"\"", "tiff:Orientation: 6"] { + assert!(matches!(upright_xmp(same), std::borrow::Cow::Borrowed(_)), "{same}"); + } +} + +#[test] +fn png_opens_upright_for_every_orientation() { + let (w, h) = (5, 3); + let up = exact_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let png = encode(&gray(sw, sh, s.clone()), Format::Png, &EncodeOptions::default()).unwrap(); + let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false)); + let img = decode(&file).unwrap(); + assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(img.data(), &up[..], "o={o}"); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1, "o={o}: metadata says upright"); + // Decoded in steps and finished by `finish_png_decode`, it is the same image. + let stepped = PngDecodeRows::new(file.as_slice(), &Limits::default()).unwrap().step(&mut || true).unwrap().unwrap(); + assert_eq!(finish_png_decode(stepped, &DecodeOptions::default()).unwrap(), img, "o={o}: stepped"); + // Opting out keeps the stored pixels and the tag. + let raw = decode_with(&file, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!((raw.dimensions(), raw.data()), ((sw as u32, sh as u32), &s[..]), "o={o}"); + assert_eq!(exif_orientation(raw.meta.exif.as_deref().unwrap()), o); + } +} + +/// An image turned upright a band of rows at a time is the image `oriented` gives, for every +/// orientation, pixel size and shape, and takes a call per band. +#[test] +fn orient_steps_equal_oriented() { + for o in 1..=8u16 { + for (layout, sample) in [ + (ChannelLayout::Gray, SampleType::U8), + (ChannelLayout::GrayA, SampleType::U16), + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Rgba, SampleType::F32), + ] { + for (w, h) in [(1u32, 1u32), (7, 5), (130, 66), (65, 200)] { + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let data: Vec = (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 11) as u8).collect(); + let mut img = Image::from_raw(w, h, layout, sample, data).unwrap(); + img.meta.dpi = Some((100.0, 200.0)); + img.meta.exif = Some(exif(u32::from(o), false, false)); + let want = img.clone().oriented(o).unwrap(); + let mut steps = OrientSteps::new(img, o).unwrap(); + let mut calls = 0; + let got = loop { + calls += 1; + assert!(steps.progress() <= 1.0); + if let Some(img) = steps.step(&mut || false).unwrap() { + break img; + } + }; + assert_eq!(got, want, "o={o} {layout:?} {sample:?} {w}x{h}"); + let rows = if o >= 5 { w } else { h } as usize; + let expected = if (2..=8).contains(&o) { rows.div_ceil(64) } else { 1 }; + assert_eq!(calls, expected, "o={o} {w}x{h}: one call per band of 64 rows"); + } + } + } +} + +/// A PNG turned upright in bands by `begin_png_finish` is the image `decode` gives, for every +/// orientation, and a band of a tall image is not the whole of it. +#[test] +fn png_finishes_upright_in_bands_for_every_orientation() { + let (w, h) = (70, 150); + let up = upright(w, h, |x, y| (x * 7 + y * 13) as u8); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let png = encode(&gray(sw, sh, s), Format::Png, &EncodeOptions::default()).unwrap(); + let file = png_with_exif(&png, &exif(u32::from(o), o % 2 == 0, false)); + let want = decode(&file).unwrap(); + let img = PngDecodeRows::new(file.as_slice(), &Limits::default()).unwrap().step(&mut || true).unwrap().unwrap(); + let got = match begin_png_finish(img, &DecodeOptions::default()).unwrap() { + PngFinish::Done(img) => { + assert_eq!(o, 1); + *img + } + PngFinish::Orient(mut steps) => { + assert!(o > 1); + let mut calls = 0; + loop { + calls += 1; + if let Some(img) = steps.step(&mut || false).unwrap() { + assert!(calls > 1, "o={o}: more than one band"); + break img; + } + } + } + }; + assert_eq!(got, want, "o={o}"); + } +} + +/// Extra samples (a TIFF's alpha and spot planes) turn with the image, so a channel stays +/// aligned with the pixels for every orientation. +#[test] +fn extra_samples_turn_with_the_image() { + let (w, h) = (4, 7); + let up = exact_scene(w, h); + let up_extra: Vec = up.iter().map(|v| 255 - v).collect(); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let (s_extra, _, _) = stored(&up_extra, w, h, o); + let mut img = gray(sw, sh, s); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: s_extra }]; + let turned = img.oriented(o).unwrap(); + assert_eq!(turned.data(), &up[..], "o={o}"); + assert_eq!(turned.meta.extra_samples.len(), 1, "o={o}"); + assert_eq!(turned.meta.extra_samples[0].data, up_extra, "o={o}"); + } +} + +#[test] +fn tiff_opens_upright_for_every_orientation() { + let (w, h) = (4, 7); + let up = exact_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let img = decode(&tiff_with_orientation(&s, sw as u32, sh as u32, o)).unwrap(); + assert_eq!(img.dimensions(), (w as u32, h as u32), "o={o}"); + assert_eq!(img.data(), &up[..], "o={o}"); + } +} + +#[test] +fn webp_opens_upright() { + let (w, h) = (6, 2); + let up = exact_scene(w, h); + for o in [3, 6, 8] { + let (s, sw, sh) = stored(&up, w, h, o); + let mut out = Vec::new(); + let mut enc = image_webp::WebPEncoder::new(&mut out); + enc.set_exif_metadata(exif(u32::from(o), false, false)); + enc.encode(&s, sw as u32, sh as u32, image_webp::ColorType::L8).unwrap(); + // WebP has no gray mode: it comes back as RGB. + let img = decode(&out).unwrap().convert(ChannelLayout::Gray, SampleType::U8); + assert_eq!((img.dimensions(), img.data()), ((w as u32, h as u32), &up[..]), "o={o}"); + assert_eq!(probe_size(&out).unwrap(), (w as u32, h as u32), "the header probe turns upright too, o={o}"); + } +} + +/// 8×8-block scene so JPEG keeps each block's value: block (bx, by) is distinct. +fn block_scene(w: usize, h: usize) -> Vec { + upright(w, h, |x, y| (20 + 25 * (x / 8 + (w / 8) * (y / 8))) as u8) +} + +fn assert_blocks(img: &Image, up: &[u8], w: usize, h: usize, what: &str) { + assert_eq!(img.dimensions(), (w as u32, h as u32), "{what}"); + let d = img.data(); + for by in 0..h / 8 { + for bx in 0..w / 8 { + let i = (by * 8 + 4) * w + bx * 8 + 4; + assert!((i32::from(d[i]) - i32::from(up[i])).abs() <= 4, "{what}: block ({bx},{by}) is {} not {}", d[i], up[i]); + } + } +} + +#[test] +fn jpeg_opens_upright_for_every_orientation() { + let (w, h) = (32, 16); + let up = block_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let file = jpeg_with_exif(&jpeg, &exif(u32::from(o), o > 4, o == 7)); + let img = decode(&file).unwrap(); + assert_blocks(&img, &up, w, h, &format!("o={o}")); + assert_eq!(exif_orientation(img.meta.exif.as_deref().unwrap()), 1); + } +} + +#[test] +fn rgb_jpeg_with_dpi_swaps_resolution_on_a_quarter_turn() { + let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]).unwrap().with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let back = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); + assert_eq!(back.dimensions(), (8, 16)); + assert_eq!(back.meta.dpi, Some((150.0, 300.0))); +} + +#[test] +fn export_writes_orientation_1_and_keeps_pixels() { + let (w, h) = (32, 16); + let up = block_scene(w, h); + let (s, sw, sh) = stored(&up, w, h, 6); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let opened = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); + assert_blocks(&opened, &up, w, h, "opened"); + + // Even an image whose metadata still claims 6 (e.g. from a PSD) is written as 1. + let mut stale = opened.clone(); + stale.meta.exif = Some(exif(6, true, false)); + stale.meta.xmp = Some(r#""#.into()); + for format in [Format::Png, Format::Jpeg, Format::WebP, Format::Tiff] { + let bytes = encode(&stale, format, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let raw = decode_with(&bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + if let Some(e) = &raw.meta.exif { + assert_eq!(exif_orientation(e), 1, "{format:?}"); + } + if let Some(x) = &raw.meta.xmp { + assert!(x.contains(r#"tiff:Orientation="1""#), "{format:?}: {x}"); + } + assert_eq!(exif_orientation(&bytes), 1, "{format:?} file"); + // Pixels as shown, both with and without applying orientation on reopen. + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), raw.dimensions(), "{format:?}"); + if format == Format::Jpeg { + assert_blocks(&back, &up, w, h, "jpeg reopen"); + } else { + assert_eq!(back.convert(ChannelLayout::Gray, SampleType::U8).data(), opened.data(), "{format:?} lossless"); + } + } +} + +#[test] +fn round_trip_open_save_reopen_is_stable() { + let (w, h) = (24, 16); + let up = block_scene(w, h); + for o in 1..=8u16 { + let (s, sw, sh) = stored(&up, w, h, o); + let jpeg = encode(&gray(sw, sh, s), Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let first = decode(&jpeg_with_exif(&jpeg, &exif(u32::from(o), false, false))).unwrap(); + let saved = encode(&first, Format::Jpeg, &EncodeOptions { jpeg_quality: 100, ..Default::default() }).unwrap(); + let second = decode(&saved).unwrap(); + assert_blocks(&second, &up, w, h, &format!("reopened o={o}")); + assert_eq!(exif_orientation(second.meta.exif.as_deref().unwrap()), 1); + } +} + +#[test] +fn malformed_exif_in_a_jpeg_opens_as_stored() { + let img = gray(16, 8, vec![90; 128]); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let mut rng = Rng::new(285); + let mut cases = vec![Vec::new(), b"II*\0".to_vec(), b"MM\0*\xff\xff\xff\xff".to_vec(), exif(6, false, false)[..20].to_vec()]; + cases.extend((0..64).map(|i| { + let mut b = if i % 2 == 0 { b"II*\0\x08\0\0\0".to_vec() } else { b"MM\0*\0\0\0\x08".to_vec() }; + b.extend(rng.bytes(i * 7)); + b + })); + for e in cases { + let back = decode(&jpeg_with_exif(&jpeg, &e)).unwrap(); + let o = exif_orientation(&e); + let want = if o >= 5 { (8, 16) } else { (16, 8) }; + assert_eq!(back.dimensions(), want, "{e:?}"); + } +} + +#[test] +fn every_layout_and_depth_orients_and_inverts() { + let inverse = |o: u16| match o { + 6 => 8, + 8 => 6, + o => o, + }; + for layout in ChannelLayout::ALL { + for sample in SampleType::ALL { + let img = test_image(layout, sample); + for o in 0..=9u16 { + let turned = img.clone().oriented(o).unwrap(); + if (2..=8).contains(&o) && o >= 5 { + assert_eq!(turned.dimensions(), (img.height(), img.width())); + } else { + assert_eq!(turned.dimensions(), img.dimensions()); + } + let back = turned.oriented(inverse(o)).unwrap(); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?} o={o}"); + } + } + } +} + +/// #285 performance budget: turning a 24 MP photo upright on open adds ≤ 50 ms +/// (release build: `cargo test --release -p craft-image-codecs --test it -- orientation:: --ignored`). +#[test] +#[ignore = "timing; run in release"] +fn rotating_24_mp_is_fast() { + let (w, h) = (6000u32, 4000u32); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, (0..w as usize * h as usize * 3).map(|i| i as u8).collect()).unwrap(); + for o in [6u16, 3, 8] { + let mut best = f64::MAX; + for _ in 0..5 { + let c = img.clone(); + let t = std::time::Instant::now(); + let r = c.oriented(o).unwrap(); + best = best.min(t.elapsed().as_secs_f64() * 1000.0); + std::hint::black_box(r); + } + eprintln!("orientation {o}: 24 MP RGB8 in {best:.1} ms"); + assert!(best <= 50.0, "orientation {o} took {best:.1} ms"); + } +} + +/// A TIFF-structured EXIF block: IFD0 at offset 8 holding `entries` +/// (`tag, type, count, 4-byte value field`), a zero next-IFD pointer, then `tail`. +fn ifd(big: bool, entries: &[(u16, u16, u32, [u8; 4])], tail: &[u8]) -> Vec { + let u16b = |v: u16| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let mut v = if big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&u32b(8)); + v.extend_from_slice(&u16b(entries.len() as u16)); + for &(tag, ty, count, value) in entries { + v.extend_from_slice(&u16b(tag)); + v.extend_from_slice(&u16b(ty)); + v.extend_from_slice(&u32b(count)); + v.extend_from_slice(&value); + } + v.extend_from_slice(&u32b(0)); + v.extend_from_slice(tail); + v +} + +/// The 4-byte value field of a SHORT. +fn short(v: u16, big: bool) -> [u8; 4] { + let b = if big { v.to_be_bytes() } else { v.to_le_bytes() }; + [b[0], b[1], 0, 0] +} + +#[test] +fn duplicate_orientation_entries_first_wins() { + for big in [false, true] { + let o = |v| (0x0112, 3, 1, short(v, big)); + assert_eq!(exif_orientation(&ifd(big, &[o(6), o(3)], &[])), 6); + assert_eq!(exif_orientation(&ifd(big, &[o(3), o(6)], &[])), 3); + assert_eq!(exif_orientation(&ifd(big, &[o(1), o(8)], &[])), 1); + // A malformed first entry is ignored as a whole: a later good one does not count. + let bad_first = ifd(big, &[(0x0112, 3, 2, short(6, big)), o(8)], &[]); + assert_eq!(exif_orientation(&bad_first), 1); + let long = |v: u32| (0x0112, 4, 1, if big { v.to_be_bytes() } else { v.to_le_bytes() }); + let long_first = ifd(big, &[long(6), o(8)], &[]); + assert_eq!(exif_orientation(&long_first), 6, "a LONG first entry counts"); + assert_eq!(&upright_exif(&long_first)[..], &ifd(big, &[long(1), o(1)], &[])[..], "each in its own width"); + let rational_first = ifd(big, &[(0x0112, 5, 1, short(6, big)), o(8)], &[]); + assert_eq!(exif_orientation(&rational_first), 1); + // Upright: every well-formed duplicate becomes 1, so no reader rotates twice. + let dup = ifd(big, &[o(6), o(3)], &[]); + let fixed = upright_exif(&dup); + assert_eq!(fixed.len(), dup.len()); + assert_eq!(&fixed[..], &ifd(big, &[o(1), o(1)], &[])[..]); + // Only a later duplicate disagrees: it is rewritten too. + assert_eq!(&upright_exif(&ifd(big, &[o(1), o(8)], &[]))[..], &ifd(big, &[o(1), o(1)], &[])[..]); + assert!(matches!(upright_exif(&ifd(big, &[o(1), o(1)], &[])), std::borrow::Cow::Borrowed(_))); + } +} + +#[test] +fn strict_ifd_parsing_ignores_the_tag() { + for big in [false, true] { + let o6 = (0x0112, 3, 1, short(6, big)); + let good = ifd(big, &[o6], &[]); + assert_eq!(exif_orientation(&good), 6); + // Truncated before (or inside) the next-IFD pointer. + for cut in 1..=4 { + assert_eq!(exif_orientation(&good[..good.len() - cut]), 1, "cut {cut}"); + } + // IFD offsets inside the 8-byte header. + for off in 0..8u32 { + let mut b = good.clone(); + b[4..8].copy_from_slice(&if big { off.to_be_bytes() } else { off.to_le_bytes() }); + assert_eq!(exif_orientation(&b), 1, "offset {off}"); + assert!(matches!(upright_exif(&b), std::borrow::Cow::Borrowed(_))); + } + // Count must be exactly 1, type must be SHORT or LONG. + let long6 = if big { 6u32.to_be_bytes() } else { 6u32.to_le_bytes() }; + for count in [0, 2, 3, u32::MAX] { + assert_eq!(exif_orientation(&ifd(big, &[(0x0112, 3, count, short(6, big))], &[])), 1, "count {count}"); + assert_eq!(exif_orientation(&ifd(big, &[(0x0112, 4, count, long6)], &[])), 1, "LONG count {count}"); + } + for ty in [1, 2, 5, 6, 7, 8, 9, 0, 0xFFFF] { + let b = ifd(big, &[(0x0112, ty, 1, short(6, big))], &[]); + assert_eq!(exif_orientation(&b), 1, "type {ty}"); + assert!(matches!(upright_exif(&b), std::borrow::Cow::Borrowed(_)), "type {ty}"); + } + } +} + +#[test] +fn upright_exif_keeps_every_other_byte() { + for big in [false, true] { + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + // IFD0: Make (12-byte ASCII stored after the IFD), Orientation 6, Software (inline ASCII), + // ExifIFD pointer to a sub-IFD with its own entries. + let ifd_end = 8 + 2 + 4 * 12 + 4; + let make_at = ifd_end as u32; + let sub_at = make_at + 12; + let mut tail = b"PhotoCraftCo".to_vec(); + // Sub-IFD: one DateTimeOriginal-ish SHORT entry, then next-IFD 0. + tail.extend_from_slice(&if big { 1u16.to_be_bytes() } else { 1u16.to_le_bytes() }); + tail.extend_from_slice(&if big { 0x9000u16.to_be_bytes() } else { 0x9000u16.to_le_bytes() }); + tail.extend_from_slice(&if big { 7u16.to_be_bytes() } else { 7u16.to_le_bytes() }); + tail.extend_from_slice(&u32b(4)); + tail.extend_from_slice(b"0232"); + tail.extend_from_slice(&u32b(0)); + let e = ifd(big, &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], &tail); + let mut prefixed = b"Exif\0\0".to_vec(); + prefixed.extend_from_slice(&e); + let fixed = upright_exif(&prefixed).into_owned(); + assert_eq!(exif_orientation(&fixed), 1); + assert_eq!(fixed.len(), prefixed.len()); + // Only the Orientation value (2 bytes in the second entry) differs. + let value = 6 + 8 + 2 + 12 + 8; + let changed: Vec = (0..fixed.len()).filter(|&i| fixed[i] != prefixed[i]).collect(); + assert_eq!(changed, vec![if big { value + 1 } else { value }], "big={big}"); + assert_eq!(&fixed[..value], &prefixed[..value]); + assert_eq!(&fixed[value + 2..], &prefixed[value + 2..]); + // The offset-based Make string and the inline Software tag are intact. + assert_eq!(&fixed[6 + make_at as usize..][..12], b"PhotoCraftCo"); + assert!(fixed.windows(4).any(|w| w == b"PC1\0")); + } +} + +#[test] +fn limits_apply_to_the_upright_size() { + // Stored 32×16 landscape; Orientation 6 makes it a 16×32 portrait. + let jpeg = encode(&gray(32, 16, vec![90; 32 * 16]), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let tall = jpeg_with_exif(&jpeg, &exif(6, false, false)); + let limits = Limits { max_width: 32, max_height: 16, ..Limits::default() }; + let opts = DecodeOptions { limits, ..Default::default() }; + assert!(matches!(decode_with(&tall, &opts), Err(CodecError::LimitExceeded(_))), "the upright portrait is too tall"); + // As stored it fits, and a 180° turn keeps the size. + let kept = decode_with(&tall, &DecodeOptions { keep_orientation: true, ..opts.clone() }).unwrap(); + assert_eq!(kept.dimensions(), (32, 16)); + let flipped = decode_with(&jpeg_with_exif(&jpeg, &exif(3, false, false)), &opts).unwrap(); + assert_eq!(flipped.dimensions(), (32, 16)); + // The same holds for a TIFF, whose orientation lives in its own IFD0. + let tiff = tiff_with_orientation(&[7; 32 * 16], 32, 16, 8); + assert!(matches!(decode_with(&tiff, &opts), Err(CodecError::LimitExceeded(_)))); + assert_eq!(decode_with(&tiff, &DecodeOptions::default()).unwrap().dimensions(), (16, 32)); +} + +#[test] +fn long_orientation_reads_and_rewrites_in_both_byte_orders() { + for big in [false, true] { + let u32b = |v: u32| if big { v.to_be_bytes() } else { v.to_le_bytes() }; + let long = |v: u32| (0x0112u16, 4u16, 1u32, u32b(v)); + for o in 1..=8u32 { + let e = ifd(big, &[(0x0131, 2, 4, *b"PC1\0"), long(o)], b"tail"); + assert_eq!(exif_orientation(&e), o as u16, "o={o} big={big}"); + let fixed = upright_exif(&e); + assert_eq!(exif_orientation(&fixed), 1); + // Still a LONG, all 4 value bytes set to 1 in the file's byte order; nothing else moved. + assert_eq!(&fixed[..], &ifd(big, &[(0x0131, 2, 4, *b"PC1\0"), long(1)], b"tail")[..], "o={o} big={big}"); + } + // Out of range, including values that only fit in a LONG, reads as 1 and is left alone. + for bad in [0, 9, 0x1_0006, u32::MAX] { + let e = ifd(big, &[long(bad)], &[]); + assert_eq!(exif_orientation(&e), 1, "{bad}"); + } + // A LONG 6 in a JPEG opens upright. + let jpeg = encode(&gray(32, 16, vec![90; 32 * 16]), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let img = decode(&jpeg_with_exif(&jpeg, &ifd(big, &[long(6)], &[]))).unwrap(); + assert_eq!(img.dimensions(), (16, 32)); + let kept = img.meta.exif.as_deref().unwrap(); + assert_eq!(kept.strip_prefix(b"Exif\0\0").unwrap_or(kept), &ifd(big, &[long(1)], &[])[..]); + } +} + +/// `probe_size` reads the size `decode` returns from the header alone, for every format it +/// covers, and a file cut short inside its header is refused rather than given a size. +#[test] +fn probe_size_matches_decode() { + let img = synth(7, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.0); + let probed = [Format::WebP, Format::Gif, Format::Bmp, Format::Tga, Format::Qoi, Format::Hdr, Format::Ico, Format::OpenExr]; + assert!(probed.iter().all(|f| rw_formats().contains(f))); + for format in rw_formats() { + let bytes = encode(&img, format, &EncodeOptions::default()).unwrap(); + let decoded = decode(&bytes).unwrap().dimensions(); + match probe_size(&bytes) { + Ok(size) => assert_eq!(size, decoded, "{format:?}"), + Err(_) => assert!(!probed.contains(&format), "{format:?} should be probed"), + } + if probed.contains(&format) { + assert!(probe_size(&bytes[..10]).is_err(), "{format:?} cut inside its header"); + } + } + assert!(probe_size(&[]).is_err()); +} diff --git a/crates/image-codecs/tests/it/pcx.rs b/crates/image-codecs/tests/it/pcx.rs new file mode 100644 index 000000000..b112a3af1 --- /dev/null +++ b/crates/image-codecs/tests/it/pcx.rs @@ -0,0 +1,137 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, SampleType, caps, decode, decode_as, detect, encode, from_extension}; + +fn header(bits: u8, planes: u8, width: u16, height: u16, stride: u16) -> Vec { + let mut b = vec![0u8; 128]; + b[0] = 0x0a; // ZSoft + b[1] = 5; // PC Paintbrush 3.0+ + b[2] = 1; // RLE + b[3] = bits; + b[8..10].copy_from_slice(&(width - 1).to_le_bytes()); + b[10..12].copy_from_slice(&(height - 1).to_le_bytes()); + b[12..14].copy_from_slice(&300u16.to_le_bytes()); + b[14..16].copy_from_slice(&150u16.to_le_bytes()); + b[65] = planes; + b[66..68].copy_from_slice(&stride.to_le_bytes()); + b[68..70].copy_from_slice(&1u16.to_le_bytes()); + b +} + +fn rle_row(out: &mut Vec, row: &[u8]) { + for &value in row { + if value >= 0xc0 { + out.extend_from_slice(&[0xc1, value]); + } else { + out.push(value); + } + } +} + +#[test] +fn pcx_is_readable_but_not_writable() { + assert_eq!(from_extension("archive.PCX"), Some(Format::Pcx)); + assert!(caps(Format::Pcx).read && !caps(Format::Pcx).write); + let img = craft_image_codecs::Image::from_u8(1, 1, ChannelLayout::Rgb, vec![0, 0, 0]).unwrap(); + assert!(matches!(encode(&img, Format::Pcx, &Default::default()), Err(CodecError::Unsupported { .. }))); +} + +#[test] +fn decodes_three_plane_rgb_and_keeps_dpi() { + let mut b = header(8, 3, 2, 1, 2); + for row in [&[255, 0][..], &[0, 255][..], &[0, 0][..]] { + rle_row(&mut b, row); + } + assert_eq!(detect(&b), Some(Format::Pcx)); + let im = decode(&b).unwrap(); + assert_eq!(im.layout(), ChannelLayout::Rgb); + assert_eq!(im.sample_type(), SampleType::U8); + assert_eq!(im.dimensions(), (2, 1)); + assert_eq!(im.data(), &[255, 0, 0, 0, 255, 0]); + assert_eq!(im.meta.dpi, Some((300.0, 150.0))); +} + +#[test] +fn decodes_256_colour_vga_palette() { + let mut b = header(8, 1, 2, 1, 2); + rle_row(&mut b, &[1, 2]); + b.push(0x0c); + let mut palette = vec![0; 256 * 3]; + palette[3..6].copy_from_slice(&[255, 0, 0]); + palette[6..9].copy_from_slice(&[0, 255, 0]); + b.extend(palette); + assert_eq!(decode(&b).unwrap().data(), &[255, 0, 0, 0, 255, 0]); +} + +#[test] +fn decodes_4bit_and_monochrome_palettes() { + let mut four = header(4, 1, 2, 1, 2); + four[16 + 3..16 + 6].copy_from_slice(&[255, 0, 0]); + four[16 + 6..16 + 9].copy_from_slice(&[0, 0, 255]); + rle_row(&mut four, &[0x12, 0]); + assert_eq!(decode(&four).unwrap().data(), &[255, 0, 0, 0, 0, 255]); + + let mut one = header(1, 1, 2, 1, 2); + one[16 + 3..16 + 6].copy_from_slice(&[255, 0, 0]); + rle_row(&mut one, &[0x80, 0]); + assert_eq!(decode(&one).unwrap().data(), &[255, 0, 0, 0, 0, 0]); +} + +#[test] +fn rejects_truncated_and_overshooting_rle_without_panicking() { + let mut b = header(8, 3, 2, 1, 2); + assert!(decode_as(Format::Pcx, &b).is_err(), "header alone is truncated"); + b.extend([0xc7, 0xff]); // a seven-byte run cannot cross a six-byte scanline + assert!(decode_as(Format::Pcx, &b).is_err()); + let mut b = header(8, 3, 2, 1, 2); + b.extend([0xc1]); // missing run value + assert!(decode_as(Format::Pcx, &b).is_err()); + let mut b = header(8, 3, 2, 1, 2); + b[66..68].copy_from_slice(&0u16.to_le_bytes()); + assert_eq!(detect(&b), None, "invalid stride is not a PCX signature"); + + // A solid image with stride of the form 63k + 1 (64, 127) encodes as k full 63-byte runs + // plus a 1-byte literal (2k + 1 bytes per plane row), which is valid and must decode. + for width in [64u16, 127] { + let mut solid = header(8, 3, width, 1, width); + for _plane in 0..3 { + let mut rem = usize::from(width); + while rem >= 63 { + solid.extend_from_slice(&[0xff, 0x00]); // 63-byte run of 0x00 + rem -= 63; + } + if rem == 1 { + solid.push(0x00); // 1-byte literal 0x00 + } + } + let im = decode(&solid).unwrap(); + assert_eq!(im.dimensions(), (u32::from(width), 1)); + assert!(im.data().iter().all(|&v| v == 0)); + } +} + +#[test] +fn refuses_headers_that_exceed_allocation_budget() { + let mut b = header(8, 3, 3000, 3000, 3000); + // The default decoder rejects the allocation before reading any scanline. + let opts = craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + assert!(matches!(craft_image_codecs::decode_as_with(Format::Pcx, &b, &opts), Err(CodecError::LimitExceeded(_)))); + b[2] = 0; + assert_eq!(detect(&b), None, "unsupported uncompressed encoding"); +} + +#[test] +fn decodes_rgb_runs_across_planes() { + let mut b = header(8, 3, 2, 1, 2); + b.extend([0xc4, 0xff, 0xc2, 0]); + let im = decode(&b).unwrap(); + assert_eq!(im.data(), &[255, 255, 0, 255, 255, 0]); + let png = encode(&im, Format::Png, &Default::default()).unwrap(); + assert_eq!(decode(&png).unwrap().data(), im.data()); +} + +#[test] +fn decodes_four_plane_runs_across_planes() { + let mut b = header(1, 4, 2, 1, 2); + b[16 + 15 * 3..16 + 16 * 3].copy_from_slice(&[255, 255, 0]); + b.extend([0xc8, 0xff]); + assert_eq!(decode(&b).unwrap().data(), &[255, 255, 0, 255, 255, 0]); +} diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs new file mode 100644 index 000000000..e98b0ae09 --- /dev/null +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -0,0 +1,646 @@ +//! Format-specific behaviour: Adam7 PNG, JPEG colour models, oracle +//! cross-checks against the `image` crate, and the optional PngSuite corpus. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +// --------------------------------------------------------------------------- +// PNG Adam7 +// --------------------------------------------------------------------------- + +fn interlaced(img: &Image) -> Vec { + encode(img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap() +} + +fn check_adam7(w: u32, h: u32, layout: ChannelLayout, sample: SampleType) { + let img = synth(w, h, layout, sample, (w * 31 + h) as u64, 0.5); + let a = interlaced(&img); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(a[28], 1, "IHDR interlace flag"); + assert_eq!(b[28], 0); + let da = decode(&a).unwrap(); + let db = decode(&b).unwrap(); + assert_eq!(da, db, "{w}x{h} {layout:?} {sample:?}: interlaced != progressive"); + assert_eq!(da.data(), img.data()); + // Oracle: the image crate agrees. + let o = image::load_from_memory_with_format(&a, image::ImageFormat::Png).unwrap(); + let ours = da.convert(ChannelLayout::Rgba, SampleType::U16).to_u16_samples().unwrap(); + assert_eq!(o.into_rgba16().into_raw(), ours); +} + +#[test] +fn adam7_small_sizes_all_layouts() { + for (w, h) in [(1, 1), (2, 2), (3, 3), (5, 1), (1, 9), (7, 5), (8, 8), (9, 17), (33, 31)] { + for l in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + check_adam7(w, h, l, SampleType::U8); + } + } +} + +#[test] +fn adam7_16bit() { + for (w, h) in [(1, 1), (4, 3), (13, 11), (64, 40)] { + for l in [ChannelLayout::Gray, ChannelLayout::Rgba] { + check_adam7(w, h, l, SampleType::U16); + } + } +} + +#[test] +fn adam7_with_metadata() { + let mut img = test_image(ChannelLayout::Rgb, SampleType::U8).with_icc(Some(sample_icc(400))); + img.meta.text.push(("Comment".into(), "interlaced".into())); + let back = decode(&interlaced(&img)).unwrap(); + assert_eq!(back.icc, img.icc); + assert_eq!(back.meta.text, img.meta.text); +} + +#[test] +fn adam7_compressions() { + let img = test_image(ChannelLayout::Rgba, SampleType::U8); + for c in [PngCompression::None, PngCompression::Fast, PngCompression::Best] { + let b = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, png_compression: c, ..Default::default() }).unwrap(); + assert_eq!(decode(&b).unwrap().data(), img.data()); + } +} + +#[test] +fn png_palette_and_low_bit_depth_expand() { + // Build a 2-bit gray PNG and a palette PNG with tRNS using the png crate. + let mut out = Vec::new(); + { + let mut e = png::Encoder::new(&mut out, 4, 1); + e.set_color(png::ColorType::Grayscale); + e.set_depth(png::BitDepth::Two); + let mut w = e.write_header().unwrap(); + w.write_image_data(&[0b00_01_10_11]).unwrap(); + } + let img = decode(&out).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Gray, &[0u8, 85, 170, 255][..])); + + let mut out = Vec::new(); + { + let mut e = png::Encoder::new(&mut out, 2, 1); + e.set_color(png::ColorType::Indexed); + e.set_depth(png::BitDepth::Eight); + e.set_palette(vec![255, 0, 0, 0, 0, 255]); + e.set_trns(vec![128]); + let mut w = e.write_header().unwrap(); + w.write_image_data(&[0, 1]).unwrap(); + } + let img = decode(&out).unwrap(); + assert_eq!((img.layout(), img.data()), (ChannelLayout::Rgba, &[255u8, 0, 0, 128, 0, 0, 255, 255][..])); +} + +// --------------------------------------------------------------------------- +// JPEG colour models +// --------------------------------------------------------------------------- + +#[test] +fn jpeg_grayscale_decodes_as_gray() { + let img = synth(40, 30, ChannelLayout::Gray, SampleType::U8, 2, 0.02); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Gray); + assert!(psnr(&img, &back) > 35.0); +} + +#[test] +fn jpeg_cmyk_roundtrip_with_adobe_marker() { + let img = synth(40, 30, ChannelLayout::Cmyk, SampleType::U8, 3, 0.02); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 95, ..Default::default() }).unwrap(); + assert!(bytes.windows(5).any(|w| w == b"Adobe"), "APP14 Adobe marker"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk); + assert!(psnr(&img, &back) > 35.0, "{}", psnr(&img, &back)); +} + +#[test] +fn jpeg_ycck_decodes_to_cmyk() { + let img = synth(40, 32, ChannelLayout::Cmyk, SampleType::U8, 4, 0.02); + let mut bytes = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut bytes, 95); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4); + enc.encode(img.data(), 40, 32, jpeg_encoder::ColorType::CmykAsYcck).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Cmyk); + assert!(psnr(&img, &back) > 32.0, "{}", psnr(&img, &back)); +} + +#[test] +fn jpeg_cmyk_flat_colour_exact_ish() { + let img = Image::from_u8(16, 16, ChannelLayout::Cmyk, [10u8, 100, 200, 50].repeat(256)).unwrap(); + let back = decode(&encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap()).unwrap(); + for px in back.data().chunks(4) { + for (a, b) in px.iter().zip([10u8, 100, 200, 50]) { + assert!((*a as i32 - b as i32).abs() <= 2); + } + } +} + +#[test] +fn jpeg_rgb_matches_oracle_decoder() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let bytes = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let ours = decode(&bytes).unwrap(); + let theirs = image::load_from_memory_with_format(&bytes, image::ImageFormat::Jpeg).unwrap().into_rgb8(); + let max = ours.data().iter().zip(theirs.as_raw()).map(|(a, b)| (*a as i32 - *b as i32).abs()).max().unwrap(); + assert!(max <= 1, "max diff {max}"); +} + +#[test] +fn jpeg_progressive_from_other_encoder_decodes() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let mut bytes = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut bytes, 90); + enc.set_progressive(true); + enc.encode(img.data(), img.width() as u16, img.height() as u16, jpeg_encoder::ColorType::Rgb).unwrap(); + let back = decode(&bytes).unwrap(); + assert!(psnr(&img, &back) > 25.0); +} + +// --------------------------------------------------------------------------- +// Oracle: files we write are readable by the image crate +// --------------------------------------------------------------------------- + +#[test] +fn png_output_matches_oracle_all_layouts() { + for l in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for s in [SampleType::U8, SampleType::U16] { + let img = test_image(l, s); + let b = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba16().into_raw(); + assert_eq!(o, img.to_rgba16(), "{l:?} {s:?}"); + } + } +} + +#[test] +fn tiff_output_matches_oracle() { + for l in [ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for s in [SampleType::U8, SampleType::U16] { + let img = test_image(l, s); + let b = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba16().into_raw(); + assert_eq!(o, img.to_rgba16(), "{l:?} {s:?}"); + } + } +} + +#[test] +fn tiff_f32_output_matches_oracle() { + let img = test_image(ChannelLayout::Rgba, SampleType::F32); + let b = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap(); + let o = image::load_from_memory(&b).unwrap().into_rgba32f().into_raw(); + assert_eq!(o, img.to_f32_samples().unwrap()); +} + +// --------------------------------------------------------------------------- +// PNG decoded in steps (PngDecodeRows) +// --------------------------------------------------------------------------- + +/// Decodes `bytes` with [`PngDecodeRows`], going on for `per_call` rows per call (0: unlimited); +/// returns the image and how many calls it took. +fn decode_in_steps(bytes: &[u8], per_call: usize) -> Result<(Image, usize), CodecError> { + let mut rows = PngDecodeRows::new(bytes, &Limits::default())?; + let mut calls = 0; + loop { + calls += 1; + let mut left = per_call; + let mut more = || { + left = left.wrapping_sub(1); + per_call == 0 || left > 0 + }; + if let Some(img) = rows.step(&mut more)? { + return Ok((img, calls)); + } + assert!(calls < 100_000, "the decoder never finished"); + } +} + +fn assert_steps_equal_decode(bytes: &[u8], what: &str) { + let whole = decode(bytes); + for per_call in [1, 7, 0] { + // `decode` also takes the resolution from EXIF or XMP and turns the image upright; + // `finish_png_decode` does that for a stepped decode, as the stepped import does. + let stepped = decode_in_steps(bytes, per_call).and_then(|(img, calls)| Ok((finish_png_decode(img, &DecodeOptions::default())?, calls))); + match (&whole, stepped) { + (Ok(a), Ok((b, _))) => assert_eq!(a, &b, "{what}, {per_call} rows per call"), + (Err(_), Err(_)) => {} + (a, b) => panic!("{what}, {per_call} rows per call: decode {:?}, steps {:?}", a.as_ref().map(|_| ()), b.map(|_| ())), + } + } +} + +#[test] +fn png_rows_equal_decode_in_every_layout_and_depth() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for sample in [SampleType::U8, SampleType::U16] { + let mut img = synth(41, 29, layout, sample, 77, 0.3).with_icc(Some(sample_icc(400))); + img.meta.text.push(("Comment".into(), "stepped".into())); + img.meta.dpi = Some((300.0, 300.0)); + for interlaced in [false, true] { + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: interlaced, ..Default::default() }).unwrap(); + assert_steps_equal_decode(&bytes, &format!("{layout:?} {sample:?} interlaced={interlaced}")); + } + } + } +} + +#[test] +fn png_rows_equal_decode_for_palette_and_transparency() { + let pal = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [9, 9, 9]]; + let idx: Vec = (0..50 * 20).map(|i| (i % 4) as u8).collect(); + let bytes = encode_png_indexed(50, 20, &idx, &pal, Some(3), None, None).unwrap(); + assert_steps_equal_decode(&bytes, "palette with a transparent entry"); +} + +#[test] +fn png_rows_take_one_call_per_row_and_report_progress() { + let bytes = encode(&synth(16, 40, ChannelLayout::Rgb, SampleType::U8, 5, 0.2), Format::Png, &EncodeOptions::default()).unwrap(); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + assert_eq!(rows.size(), (16, 40)); + assert_eq!(rows.progress(), 0.0); + let mut seen = Vec::new(); + let img = loop { + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + seen.push(rows.progress()); + }; + assert_eq!(seen.len(), 39, "one call per row but the last, which also finishes the file"); + assert!(seen.windows(2).all(|w| w[0] < w[1])); + assert_eq!(rows.progress(), 1.0); + assert_eq!(img, decode(&bytes).unwrap()); + // A decoder that has finished says so instead of decoding again. + assert!(rows.step(&mut || true).is_err()); +} + +#[test] +fn png_rows_share_their_bytes_without_copying_them() { + let bytes = std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); + let mut rows = PngDecodeRows::new(SharedBytes(bytes.clone()), &Limits::default()).unwrap(); + let img = rows.step(&mut || true).unwrap().unwrap(); + assert_eq!(img, decode(&bytes).unwrap()); + drop(rows); + assert_eq!(std::sync::Arc::strong_count(&bytes), 1, "the decoder let go of the bytes"); +} + +/// A CRC-valid PNG chunk. +fn png_chunk(kind: &[u8; 4], data: &[u8]) -> Vec { + let mut crc = flate2::Crc::new(); + crc.update(kind); + crc.update(data); + let mut out = u32::try_from(data.len()).unwrap().to_be_bytes().to_vec(); + out.extend_from_slice(kind); + out.extend_from_slice(data); + out.extend_from_slice(&crc.sum().to_be_bytes()); + out +} + +fn zlib(text: &[u8]) -> Vec { + use std::io::Write; + let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + encoder.write_all(text).unwrap(); + encoder.finish().unwrap() +} + +/// A `zTXt` chunk (`deflated`) or a compressed `iTXt` one (`!deflated`) with `text` as its value. +fn png_text_chunk(kind: &[u8; 4], keyword: &[u8], text: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + if kind == b"zTXt" { + data.push(0); + } else { + data.extend_from_slice(&[1, 0, 0, 0]); + } + data.extend_from_slice(&zlib(text)); + png_chunk(kind, &data) +} + +/// `png` with `chunks` between the image data and `IEND` (the text-after-IDAT case). +fn with_chunks_before_iend(png: &[u8], chunks: &[Vec]) -> Vec { + let mut at = 8; + loop { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + if &png[at + 4..at + 8] == b"IEND" { + let mut out = png[..at].to_vec(); + for c in chunks { + out.extend_from_slice(c); + } + out.extend_from_slice(&png[at..]); + return out; + } + at += len + 12; + } +} + +#[test] +fn png_rows_read_compressed_text_in_bounded_batches() { + let img = synth(19, 23, ChannelLayout::Rgba, SampleType::U8, 9, 0.4); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let text = "A".repeat(150_000); + for kind in [b"zTXt", b"iTXt"] { + let bytes = with_chunks_before_iend(&base, &[png_text_chunk(kind, b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + // An empty budget: every step reads one bounded batch and stops. + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped, decode(&bytes).unwrap(), "{kind:?}"); + assert_eq!(stepped.meta.text, vec![("Comment".into(), text.clone())], "{kind:?}: the whole value is kept"); + assert!(calls > 20, "{kind:?}: {calls} bounded calls, not one blocking pass"); + assert_steps_equal_decode(&bytes, &format!("compressed {kind:?} text")); + } +} + +/// An Adam7 image whose compressed text needs many metadata yields: the pixels are decoded +/// once, before the first yield, and later steps only read text. +#[test] +fn png_rows_finish_adam7_text_in_bounded_batches() { + let img = synth(19, 23, ChannelLayout::Rgba, SampleType::U8, 9, 0.4); + let base = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap(); + let text = "A".repeat(150_000); + for kind in [b"zTXt", b"iTXt"] { + let bytes = with_chunks_before_iend(&base, &[png_text_chunk(kind, b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped, decode(&bytes).unwrap(), "{kind:?}"); + assert_eq!(stepped.meta.text, vec![("Comment".into(), text.clone())], "{kind:?}: the whole value is kept"); + assert!(calls > 20, "{kind:?}: {calls} bounded calls after the one Adam7 pass"); + } +} + +/// An uncompressed `iTXt` chunk (`value` is its text, so its CRC covers the whole value). +fn png_itxt_uncompressed(keyword: &[u8], value: &[u8]) -> Vec { + let mut data = keyword.to_vec(); + data.push(0); + data.extend_from_slice(&[0, 0, 0, 0]); + data.extend_from_slice(value); + png_chunk(b"iTXt", &data) +} + +/// The CRC of a large text chunk takes bounded batches too, before any of its value is read. +#[test] +fn png_rows_read_a_large_text_crc_in_bounded_batches() { + let img = synth(5, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let text: String = (0..300_000u32).map(|i| char::from(b'a' + (i % 26) as u8)).collect(); + let bytes = with_chunks_before_iend(&base, &[png_itxt_uncompressed(b"Comment", text.as_bytes())]); + let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); + let mut calls = 0; + let stepped = loop { + calls += 1; + if let Some(img) = rows.step(&mut || false).unwrap() { + break img; + } + assert!(calls < 100_000, "the decoder never finished"); + }; + assert_eq!(stepped.meta.text, vec![("Comment".into(), text)]); + assert!(calls > 60, "{calls} calls: the CRC and the value each take bounded batches"); +} + +/// A text chunk whose CRC does not match is still dropped after its bounded CRC batches. +#[test] +fn png_rows_drop_a_corrupt_text_crc() { + let img = synth(5, 5, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut chunk = png_itxt_uncompressed(b"Comment", b"not accepted"); + let at = chunk.len() - 5; + chunk[at] ^= 0xFF; + let bytes = with_chunks_before_iend(&base, &[chunk]); + let stepped = decode_in_steps(&bytes, 0).unwrap().0; + assert!(stepped.meta.text.is_empty(), "a broken CRC drops the text"); + assert_eq!(stepped, decode(&bytes).unwrap(), "the one-pass decode drops it too"); +} + +/// An uncompressed iTXt value that is not UTF-8 is dropped without charging its bytes against +/// the budget, even when it is larger than all of it: the previous reader checked UTF-8 first. +#[test] +fn png_rows_drop_invalid_uncompressed_text_before_charging_the_budget() { + let img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 3, 0.2); + let base = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let mut value = vec![b'A'; 12_000]; + value[11_999] = 0xFF; + let bytes = with_chunks_before_iend(&base, &[png_itxt_uncompressed(b"Comment", &value)]); + let opts = DecodeOptions { limits: Limits { max_alloc: 200, ..Default::default() }, ..Default::default() }; + let mut rows = PngDecodeRows::new(bytes.as_slice(), &opts.limits).unwrap(); + let stepped = rows.step(&mut || true).unwrap().unwrap(); + let stepped = finish_png_decode(stepped, &opts).unwrap(); + assert!(stepped.meta.text.is_empty(), "invalid UTF-8 is skipped, not a budget failure"); + let whole = decode_with(&bytes, &opts).unwrap(); + assert!(whole.meta.text.is_empty(), "the one-pass decode skips it too"); +} + +#[test] +fn png_rows_fail_like_decode_on_damaged_files() { + let img = synth(64, 64, ChannelLayout::Rgb, SampleType::U8, 11, 1.0); + for interlaced in [false, true] { + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: interlaced, ..Default::default() }).unwrap(); + // Cuts inside the signature, the IHDR chunk or the next chunk header leave no complete image. + for cut in [8, 20, 40] { + let truncated = bytes.get(..cut).unwrap(); + assert!(decode(truncated).is_err(), "cut at {cut} interlaced={interlaced}"); + assert!(decode_in_steps(truncated, 1).is_err(), "cut at {cut} interlaced={interlaced}"); + } + for cut in [bytes.len() / 3, bytes.len() / 2, bytes.len() - 20] { + assert_steps_equal_decode(bytes.get(..cut).unwrap(), &format!("cut at {cut} interlaced={interlaced}")); + } + // A flipped byte in the middle of the compressed data. + let mut bad = bytes.clone(); + let mid = bad.len() / 2; + bad[mid] ^= 0xFF; + assert_steps_equal_decode(&bad, "a damaged byte"); + } + assert!(decode_in_steps(&[], 1).is_err()); + assert!(decode_in_steps(b"not a png at all, just some text bytes", 1).is_err()); +} + +// --------------------------------------------------------------------------- +// PngSuite corpus (repo-root corpus/pngsuite/*.png; feature `corpus`, fetched by +// `cargo xtask corpus --all`; missing = failure) +// --------------------------------------------------------------------------- + +#[cfg(feature = "corpus")] +#[test] +fn pngsuite_corpus_matches_oracle() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let mut checked = 0; + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + let oracle = image::load_from_memory_with_format(&bytes, image::ImageFormat::Png); + let ours = decode(&bytes); + match (ours, oracle) { + (Ok(a), Ok(b)) => { + assert_eq!(a.to_rgba16(), b.into_rgba16().into_raw(), "{}", p.display()); + checked += 1; + } + (Err(e), Ok(_)) => panic!("{}: we failed but oracle decoded: {e}", p.display()), + _ => {} + } + } + eprintln!("pngsuite: {checked} files compared"); + assert!(checked > 100, "{}: only {checked} PNGs compared: run `cargo xtask corpus --all`", dir.display()); +} + +/// The preflight charges the decoded layout's bytes per pixel: a 1000×1000 Gray8 PNG (1 MB) fits a +/// 1.5 MB budget, which the old worst-case RGBA16 preflight (8 MB) refused. +#[test] +fn png_preflight_uses_the_output_layout() { + let img = synth(1000, 1000, ChannelLayout::Gray, SampleType::U8, 7, 0.5); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let opts = DecodeOptions { limits: Limits { max_alloc: 1_500_000, ..Limits::none() }, ..Default::default() }; + let got = decode_with(&bytes, &opts).expect("a Gray8 image is charged 1 byte per pixel"); + assert_eq!(got.data(), img.data()); + let tight = DecodeOptions { limits: Limits { max_alloc: 900_000, ..Limits::none() }, ..Default::default() }; + assert!(decode_with(&bytes, &tight).is_err(), "the actual pixel budget still applies"); +} + +#[cfg(feature = "corpus")] +#[test] +fn png_rows_equal_decode_on_the_pngsuite_corpus() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let mut checked = 0; + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + assert_steps_equal_decode(&bytes, &p.display().to_string()); + checked += 1; + } + assert!(checked > 100, "{}: only {checked} PNGs compared: run `cargo xtask corpus --all`", dir.display()); +} + +/// Row-by-row decoding gives `decode`'s pixels and metadata for every PngSuite file it takes (all +/// but the interlaced ones), and fails where `decode` fails. +#[cfg(feature = "corpus")] +#[test] +fn pngsuite_corpus_rows_match_the_whole_decode() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/pngsuite"); + let Ok(entries) = std::fs::read_dir(&dir) else { + panic!("{} is missing: run `cargo xtask corpus --all`", dir.display()); + }; + let opts = DecodeOptions { keep_orientation: true, ..Default::default() }; + let (mut streamed, mut refused, mut skipped) = (0, 0, 0); + for e in entries.flatten() { + let p = e.path(); + if p.extension().and_then(|s| s.to_str()) != Some("png") { + continue; + } + let bytes = std::fs::read(&p).unwrap(); + // The importer only streams files `detect` takes for PNG. + if detect(&bytes) != Some(Format::Png) { + skipped += 1; + continue; + } + let whole = decode_with(&bytes, &opts); + let (mut data, mut header) = (Vec::new(), None); + let rows = decode_png_rows::( + &bytes, + &opts, + 256, + &mut |h| { + header = Some(*h); + Ok(true) + }, + &mut |_, r| { + data.extend_from_slice(r); + Ok(()) + }, + ); + match (whole, rows) { + (Ok(img), Ok(Some(x))) => { + let h = header.unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (h.width, h.height, h.layout, h.sample), "{}", p.display()); + assert!(img.data() == data, "{}: pixels", p.display()); + assert_eq!( + (&img.icc, &img.meta.exif, &img.meta.text, &img.meta.xmp, &img.warnings), + (&x.icc, &x.meta.exif, &x.meta.text, &x.meta.xmp, &x.warnings), + "{}", + p.display() + ); + assert_eq!(img.meta.dpi, x.meta.dpi, "{}", p.display()); + streamed += 1; + } + (Ok(_), Ok(None)) => refused += 1, + (Err(a), Err(b)) => assert_eq!(a.to_string(), b.to_string(), "{}", p.display()), + (a, b) => panic!("{}: whole {:?}, rows {:?}", p.display(), a.map(|_| ()), b.map(|r| r.map(|_| ()))), + } + } + eprintln!("pngsuite: {streamed} streamed, {refused} refused (interlaced), {skipped} skipped (not detected as PNG)"); + assert!(streamed > 100, "only {streamed} PNGs streamed: run `cargo xtask corpus --all`"); +} + +/// Streaming a PNG's rows a group at a time gives the pixels and metadata `decode` gives, holds +/// only a group at a time, and stops between rows when asked. +#[test] +fn step_stream_gives_the_pixels_and_extras_of_decode() { + for (layout, sample) in [(ChannelLayout::Gray, SampleType::U8), (ChannelLayout::Rgb, SampleType::U16), (ChannelLayout::Rgba, SampleType::U8)] { + let (w, h) = (23u32, 19u32); + let len = w as usize * h as usize * layout.channels() * sample.bytes(); + let mut img = Image::from_raw(w, h, layout, sample, (0..len).map(|i| (i.wrapping_mul(2_654_435_761) >> 11) as u8).collect()).unwrap(); + img.meta.dpi = Some((144.0, 144.0)); + img.meta.text.push(("Comment".into(), "streamed".into())); + img.icc = Some(vec![7; 40]); + let bytes = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let want = decode(&bytes).unwrap(); + for group in [0u32, 1, 4, 19, 64] { + let mut dec = PngDecodeRows::new_streaming(bytes.as_slice(), &Limits::default()).unwrap(); + assert!(dec.streamable()); + assert_eq!(dec.header(), PngHeader { width: w, height: h, layout, sample }); + let (mut data, mut next, mut calls) = (Vec::new(), 0u32, 0); + let extras = loop { + calls += 1; + let mut sink = |y0: u32, rows: &[u8]| -> Result<(), CodecError> { + assert_eq!(y0, next, "{layout:?} group {group}"); + assert!(rows.len() <= group.max(1) as usize * dec_line(w, layout, sample), "a group at most"); + next += (rows.len() / dec_line(w, layout, sample)) as u32; + data.extend_from_slice(rows); + Ok(()) + }; + if let Some(extras) = dec.step_stream(group, &mut || false, &mut sink).unwrap() { + break extras; + } + assert!(calls < 10_000); + }; + assert!(calls > 1, "{layout:?}: stopped between rows"); + assert_eq!(data, want.data(), "{layout:?} group {group}: pixels"); + assert_eq!((extras.icc.as_deref(), extras.meta.dpi, &extras.meta.text), (want.icc.as_deref(), want.meta.dpi, &want.meta.text)); + assert_eq!(extras.orientation, 1); + assert!(dec.step_stream::(group, &mut || true, &mut |_, _| Ok(())).is_err(), "spent"); + } + } + // An interlaced file doesn't stream. + let img = Image::from_raw(9, 9, ChannelLayout::Gray, SampleType::U8, vec![1; 81]).unwrap(); + let bytes = encode(&img, Format::Png, &EncodeOptions { png_interlaced: true, ..Default::default() }).unwrap(); + assert!(!PngDecodeRows::new_streaming(bytes.as_slice(), &Limits::default()).unwrap().streamable()); +} + +/// Bytes per row of `w` pixels. +fn dec_line(w: u32, layout: ChannelLayout, sample: SampleType) -> usize { + w as usize * layout.channels() * sample.bytes() +} diff --git a/crates/image-codecs/tests/it/pnm_headers.rs b/crates/image-codecs/tests/it/pnm_headers.rs new file mode 100644 index 000000000..363e941ad --- /dev/null +++ b/crates/image-codecs/tests/it/pnm_headers.rs @@ -0,0 +1,82 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, SampleType, decode, decode_as}; + +const PAM_GRAY: &[u8] = b"P7\nWIDTH 1\nHEIGHT 1\nDEPTH 1\nMAXVAL 255\nENDHDR"; +const ENDINGS: &[&[u8]] = &[b"\n", b" \n", b"\t \n", b"\r\n", b" \t\r\n"]; + +#[test] +fn pam_consumes_the_complete_endhdr_line() { + for ending in ENDINGS { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(0x80); + let img = decode(&bytes).unwrap(); + assert_eq!(img.dimensions(), (1, 1)); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &[0x80], "ENDHDR ending {ending:?}"); + } +} + +#[test] +fn pam_preserves_every_possible_first_raster_byte() { + // In particular, LF, CR, space, tab and '#' must not be skipped as header syntax. + for ending in ENDINGS { + for first in 0..=255u8 { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(first); + assert_eq!(decode(&bytes).unwrap().data(), &[first], "ENDHDR ending {ending:?}, raster byte {first}"); + } + } +} + +#[test] +fn pam_crlf_header_preserves_u16_samples_and_alpha() { + for ending in ENDINGS { + let mut bytes = b"P7\r\nWIDTH 1\r\nHEIGHT 1\r\nDEPTH 2\r\nMAXVAL 65535\r\nTUPLTYPE GRAYSCALE_ALPHA\r\nENDHDR".to_vec(); + bytes.extend_from_slice(ending); + bytes.extend_from_slice(b" \n#\r"); + let img = decode(&bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.sample_type(), SampleType::U16); + assert_eq!(img.to_u16_samples().unwrap(), vec![0x200a, 0x230d], "ENDHDR ending {ending:?}"); + } +} + +#[test] +fn pam_rejects_unterminated_or_invalid_endhdr_lines() { + for suffix in [b"".as_slice(), b" ", b"\r", b" \t\r", b" \x80", b" \x80\n\x7f", b"#comment\n\x7f"] { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(suffix); + assert!(matches!(decode_as(Format::Pnm, &bytes), Err(CodecError::Malformed { .. })), "ENDHDR suffix {suffix:?}"); + } +} + +#[test] +fn pam_truncation_never_uses_header_whitespace_as_pixels() { + for ending in ENDINGS { + let mut bytes = PAM_GRAY.to_vec(); + bytes.extend_from_slice(ending); + bytes.push(0x80); + for cut in 0..bytes.len() { + assert!(decode_as(Format::Pnm, &bytes[..cut]).is_err(), "ENDHDR ending {ending:?}, truncated at {cut}"); + } + } +} + +#[test] +fn binary_pnm_keeps_its_single_whitespace_separator() { + for separator in *b" \t\r\n" { + for first in *b" \t\r\n#" { + for (header, channels) in [(b"P5\n1 1\n255".as_slice(), 1), (b"P6\n1 1\n255", 3)] { + let mut bytes = header.to_vec(); + bytes.push(separator); + bytes.extend(std::iter::repeat_n(first, channels)); + assert_eq!(decode(&bytes).unwrap().data(), vec![first; channels]); + } + let mut bytes = b"P4\n8 1".to_vec(); + bytes.extend_from_slice(&[separator, first]); + let expected: Vec = (0..8).map(|bit| if first & (0x80 >> bit) == 0 { 255 } else { 0 }).collect(); + assert_eq!(decode(&bytes).unwrap().data(), expected); + } + } +} diff --git a/crates/image-codecs/tests/it/resolution.rs b/crates/image-codecs/tests/it/resolution.rs new file mode 100644 index 000000000..fe2c51c8e --- /dev/null +++ b/crates/image-codecs/tests/it/resolution.rs @@ -0,0 +1,546 @@ +//! Resolution recorded in metadata (#1691): a camera JPEG keeps its resolution in EXIF only, and +//! Photoshop opens it at that resolution; Photoshop's own APP13 ResolutionInfo and the XMP +//! `tiff:` properties outrank EXIF, and the JFIF density comes last. Exports write the image's +//! resolution into JFIF, EXIF and XMP alike. Every file here is built by hand. + +use std::borrow::Cow; + +use craft_image_codecs::resolution::{exif_with_resolution, xmp_with_resolution}; +use craft_image_codecs::*; +use proptest::prelude::{any, proptest}; + +const X: u16 = 282; +const Y: u16 = 283; +const UNIT: u16 = 296; +const SHORT: u16 = 3; +const LONG: u16 = 4; +const RATIONAL: u16 = 5; +const ASCII: u16 = 2; + +/// One IFD0 entry: tag, type, count and the value bytes (in the block's byte order). +struct E(u16, u16, u32, Vec); + +struct Exif { + big: bool, +} + +impl Exif { + fn u16(&self, v: u16) -> [u8; 2] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + fn u32(&self, v: u32) -> [u8; 4] { + if self.big { v.to_be_bytes() } else { v.to_le_bytes() } + } + fn rational(&self, n: u32, d: u32) -> Vec { + [self.u32(n), self.u32(d)].concat() + } + fn short(&self, v: u16) -> Vec { + self.u16(v).to_vec() + } + fn x(&self, n: u32, d: u32) -> E { + E(X, RATIONAL, 1, self.rational(n, d)) + } + fn y(&self, n: u32, d: u32) -> E { + E(Y, RATIONAL, 1, self.rational(n, d)) + } + fn unit(&self, u: u16) -> E { + E(UNIT, SHORT, 1, self.short(u)) + } + fn make(&self) -> E { + E(0x010F, ASCII, 6, b"NIKON\0".to_vec()) + } + fn orientation(&self, o: u16) -> E { + E(0x0112, SHORT, 1, self.short(o)) + } + + /// A TIFF-structured EXIF block: header, IFD0 at 8, then the out-of-line values. + fn build(&self, entries: &[E]) -> Vec { + let mut v = if self.big { b"MM\0*".to_vec() } else { b"II*\0".to_vec() }; + v.extend_from_slice(&self.u32(8)); + v.extend_from_slice(&self.u16(entries.len() as u16)); + let mut data_at = 8 + 2 + entries.len() * 12 + 4; + let mut data = Vec::new(); + for E(tag, ty, count, value) in entries { + v.extend_from_slice(&self.u16(*tag)); + v.extend_from_slice(&self.u16(*ty)); + v.extend_from_slice(&self.u32(*count)); + if value.len() <= 4 { + let mut inline = value.clone(); + inline.resize(4, 0); + v.extend_from_slice(&inline); + } else { + v.extend_from_slice(&self.u32(data_at as u32)); + data.extend_from_slice(value); + data_at += value.len(); + } + } + v.extend_from_slice(&self.u32(0)); + v.extend_from_slice(&data); + v + } +} + +const LE: Exif = Exif { big: false }; +const BE: Exif = Exif { big: true }; + +/// A D3200-style block: Make, XResolution 300/1, YResolution 300/1, ResolutionUnit 2. +fn camera_exif(e: &Exif) -> Vec { + e.build(&[e.make(), e.x(300, 1), e.y(300, 1), e.unit(2)]) +} + +fn segment(marker: u8, payload: &[u8]) -> Vec { + let mut s = vec![0xFF, marker]; + s.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes()); + s.extend_from_slice(payload); + s +} + +fn jfif(unit: u8, x: u16, y: u16) -> Vec { + let mut p = b"JFIF\0\x01\x02".to_vec(); + p.push(unit); + p.extend_from_slice(&x.to_be_bytes()); + p.extend_from_slice(&y.to_be_bytes()); + p.extend_from_slice(&[0, 0]); + segment(0xE0, &p) +} + +fn exif_app1(exif: &[u8]) -> Vec { + segment(0xE1, &[b"Exif\0\0".as_slice(), exif].concat()) +} + +fn xmp_app1(xmp: &str) -> Vec { + segment(0xE1, &[b"http://ns.adobe.com/xap/1.0/\0".as_slice(), xmp.as_bytes()].concat()) +} + +/// An 8BIM resource block holding ResolutionInfo at `ppi` (display unit inch), after an +/// unrelated resource with an odd-length name and an odd-sized body. +fn irb(ppi: f64) -> Vec { + let mut b = b"8BIM".to_vec(); + b.extend_from_slice(&0x0404u16.to_be_bytes()); + b.extend_from_slice(&[3, b'a', b'b', b'c']); + b.extend_from_slice(&3u32.to_be_bytes()); + b.extend_from_slice(&[1, 2, 3, 0]); + b.extend_from_slice(b"8BIM"); + b.extend_from_slice(&0x03EDu16.to_be_bytes()); + b.extend_from_slice(&[0, 0]); + b.extend_from_slice(&16u32.to_be_bytes()); + let fixed = ((ppi * 65536.0).round() as u32).to_be_bytes(); + for _ in 0..2 { + b.extend_from_slice(&fixed); + b.extend_from_slice(&1u16.to_be_bytes()); + b.extend_from_slice(&2u16.to_be_bytes()); + } + b +} + +fn app13(resources: &[u8]) -> Vec { + segment(0xED, &[b"Photoshop 3.0\0".as_slice(), resources].concat()) +} + +/// A 16×8 baseline JPEG whose APPn segments are exactly `segments` (the encoder's own JFIF +/// header is taken out). +fn jpeg_with(segments: &[Vec]) -> Vec { + let img = Image::from_raw(16, 8, ChannelLayout::Gray, SampleType::U8, vec![128; 128]).unwrap(); + let base = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let mut i = 2; + while (0xE0..=0xEF).contains(&base[i + 1]) { + i += 2 + u16::from_be_bytes([base[i + 2], base[i + 3]]) as usize; + } + let mut out = base[..2].to_vec(); + for s in segments { + out.extend_from_slice(s); + } + out.extend_from_slice(&base[i..]); + out +} + +fn dpi_of(bytes: &[u8]) -> Option<(f32, f32)> { + decode(bytes).unwrap().meta.dpi +} + +fn near(got: Option<(f32, f32)>, want: f32) { + let (x, y) = got.unwrap_or_else(|| panic!("no resolution, wanted {want}")); + assert!((x - want).abs() < 0.01 && (y - want).abs() < 0.01, "got {x}×{y}, wanted {want}"); +} + +/// The APPn payloads of a JPEG: (marker, payload). +fn app_segments(b: &[u8]) -> Vec<(u8, Vec)> { + let mut out = Vec::new(); + let mut i = 2; + while i + 4 <= b.len() && b[i] == 0xFF && b[i + 1] != 0xDA { + let len = u16::from_be_bytes([b[i + 2], b[i + 3]]) as usize; + if (0xE0..=0xEF).contains(&b[i + 1]) { + out.push((b[i + 1], b[i + 4..i + 2 + len].to_vec())); + } + i += 2 + len; + } + out +} + +/// (unit, x, y) of a JPEG's JFIF header. +fn jfif_density(b: &[u8]) -> (u8, u16, u16) { + let (_, p) = app_segments(b).into_iter().find(|(m, p)| *m == 0xE0 && p.starts_with(b"JFIF\0")).unwrap(); + (p[7], u16::from_be_bytes([p[8], p[9]]), u16::from_be_bytes([p[10], p[11]])) +} + +fn exif_of(b: &[u8]) -> Vec { + let (_, p) = app_segments(b).into_iter().find(|(m, p)| *m == 0xE1 && p.starts_with(b"Exif\0\0")).unwrap(); + p[6..].to_vec() +} + +#[test] +fn exif_only_camera_jpeg_opens_at_its_exif_resolution() { + for e in [&LE, &BE] { + near(dpi_of(&jpeg_with(&[exif_app1(&camera_exif(e))])), 300.0); + // ResolutionUnit absent: inch, the EXIF default. + near(dpi_of(&jpeg_with(&[exif_app1(&e.build(&[e.x(300, 1), e.y(300, 1)]))])), 300.0); + // Non-unit denominators. + near(dpi_of(&jpeg_with(&[exif_app1(&e.build(&[e.x(3_000_000, 10_000), e.y(1200, 4), e.unit(2)]))])), 300.0); + } +} + +#[test] +fn exif_resolution_in_centimetres_is_converted() { + for e in [&LE, &BE] { + let exif = e.build(&[e.x(11811, 100), e.y(11811, 100), e.unit(3)]); + near(exif_resolution(&exif), 299.9994); + near(dpi_of(&jpeg_with(&[exif_app1(&exif)])), 299.9994); + } +} + +#[test] +fn jfif_aspect_ratio_only_with_exif_uses_exif() { + near(dpi_of(&jpeg_with(&[jfif(0, 1, 1), exif_app1(&camera_exif(&LE))])), 300.0); +} + +#[test] +fn exif_outranks_a_default_jfif_density() { + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(&LE))])), 300.0); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(&BE))])), 300.0); + // JFIF alone still counts, in either unit. + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72)])), 72.0); + near(dpi_of(&jpeg_with(&[jfif(2, 100, 100)])), 254.0); + assert_eq!(dpi_of(&jpeg_with(&[jfif(0, 1, 1)])), None); +} + +#[test] +fn photoshop_resolution_info_wins_then_xmp_then_exif() { + let xmp = r#""#; + let exif = exif_app1(&camera_exif(&LE)); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif.clone(), xmp_app1(xmp), app13(&irb(240.0))])), 240.0); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif.clone(), xmp_app1(xmp)])), 200.0); + // A fractional Photoshop resolution survives the 16.16 fixed point. + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif, app13(&irb(299.5))])), 299.5); + near(photoshop_resolution(&irb(150.0)), 150.0); + // Element form, centimetres. + let elem = "100/11003"; + near(xmp_resolution(elem), 254.0); +} + +#[test] +fn unusable_exif_falls_back_to_jfif() { + let e = &LE; + let bad = [ + // No absolute unit. + e.build(&[e.x(300, 1), e.y(300, 1), e.unit(1)]), + // Unknown unit, malformed unit entry. + e.build(&[e.x(300, 1), e.y(300, 1), e.unit(9)]), + e.build(&[e.x(300, 1), e.y(300, 1), E(UNIT, LONG, 1, e.u32(2).to_vec())]), + // Zero denominator, zero value, wrong type, wrong count. + e.build(&[e.x(300, 0), e.y(300, 1)]), + e.build(&[e.x(0, 1), e.y(300, 1)]), + e.build(&[E(X, SHORT, 1, e.short(300)), e.y(300, 1)]), + e.build(&[E(X, RATIONAL, 2, [e.rational(300, 1), e.rational(300, 1)].concat()), e.y(300, 1)]), + // Only one axis. + e.build(&[e.x(300, 1)]), + ]; + for exif in &bad { + assert_eq!(exif_resolution(exif), None); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(exif)])), 72.0); + } + // A value offset pointing past the end. + let mut far = camera_exif(e); + let at = 8 + 2 + 12 + 8; // XResolution entry's value field + far[at..at + 4].copy_from_slice(&e.u32(60_000)); + assert_eq!(exif_resolution(&far), None); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&far)])), 72.0); +} + +#[test] +fn truncated_metadata_never_panics_and_falls_back() { + let exif = camera_exif(&BE); + for n in 0..exif.len() { + let _ = exif_resolution(&exif[..n]); + let _ = exif_with_resolution(&exif[..n], (240.0, 240.0)); + let dpi = dpi_of(&jpeg_with(&[jfif(1, 72, 72), exif_app1(&exif[..n])])); + assert!(dpi == Some((72.0, 72.0)) || dpi == Some((300.0, 300.0)), "{n}: {dpi:?}"); + } + let r = irb(300.0); + for n in 0..r.len() { + assert_eq!(photoshop_resolution(&r[..n]), None, "{n}"); + near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), app13(&r[..n])])), 72.0); + } + for x in ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] { + assert_eq!(xmp_resolution(x), None, "{x}"); + let _ = xmp_with_resolution(x, (1.0, 1.0)); + } + + // Property-like text repeated inside the packet must not trigger a full-suffix rescan + // per occurrence (it used to be quadratic). With the cursor advancing past each located + // value, this completes well under a second even with many decoys; a timeout here would + // indicate the quadratic scan returned. A clean packet still extracts correctly. + let start = std::time::Instant::now(); + let decoys = " tiff:ResolutionUnit>".repeat(50_000); + let _ = xmp_resolution(&decoys); + assert!(start.elapsed() < std::time::Duration::from_secs(2), "xmp scan took too long: {:?}", start.elapsed()); + let clean = "300/1300/12"; + near(xmp_resolution(clean), 300.0); +} + +proptest! { + #[test] + fn hostile_bytes_never_panic(bytes in proptest::collection::vec(any::(), 0..256), ppi in 0.0f32..2e6) { + let _ = exif_resolution(&bytes); + let _ = exif_with_resolution(&bytes, (ppi, ppi)); + let _ = photoshop_resolution(&bytes); + let s = String::from_utf8_lossy(&bytes); + let _ = xmp_resolution(&s); + let _ = xmp_with_resolution(&s, (ppi, ppi)); + } + + #[test] + fn mutated_exif_never_panics(i in 0usize..80, v in any::(), ppi in 1.0f32..10_000.0) { + let mut exif = camera_exif(&LE); + if let Some(b) = exif.get_mut(i) { *b = v; } + let _ = exif_resolution(&exif); + let fixed = exif_with_resolution(&exif, (ppi, ppi)); + let _ = exif_resolution(&fixed); + let _ = exif_orientation(&fixed); + } +} + +#[test] +fn rewriting_keeps_every_other_byte() { + for e in [&LE, &BE] { + let src = e.build(&[e.make(), e.orientation(6), e.x(300, 1), e.y(300, 1), e.unit(3)]); + let out = exif_with_resolution(&src, (240.0, 240.0)).into_owned(); + assert_eq!(out.len(), src.len()); + near(exif_resolution(&out), 240.0); + let want = e.build(&[e.make(), e.orientation(6), e.x(240, 1), e.y(240, 1), e.unit(2)]); + assert_eq!(out, want); + // Fractions keep three decimals. + near(exif_resolution(&exif_with_resolution(&src, (299.5, 72.25))).map(|(x, _)| (x, x)), 299.5); + // Nothing to change: borrowed. + assert!(matches!(exif_with_resolution(&camera_exif(e), (300.0, 300.0)), Cow::Borrowed(_))); + let no_res = e.build(&[e.make()]); + assert!(matches!(exif_with_resolution(&no_res, (240.0, 240.0)), Cow::Borrowed(_))); + // With the JPEG prefix. + let prefixed = [b"Exif\0\0".as_slice(), &src].concat(); + assert_eq!(exif_with_resolution(&prefixed, (240.0, 240.0)).as_ref(), [b"Exif\0\0".as_slice(), &want].concat()); + } +} + +#[test] +fn entries_that_cannot_be_rewritten_are_removed() { + for e in [&LE, &BE] { + // XResolution as a SHORT and a LONG unit: both go, the rest stays readable. + let src = e.build(&[e.make(), E(X, SHORT, 1, e.short(300)), e.y(300, 1), E(UNIT, LONG, 1, e.u32(2).to_vec()), e.orientation(6)]); + let out = exif_with_resolution(&src, (240.0, 240.0)).into_owned(); + assert_eq!(out.len(), src.len()); + let count = if e.big { u16::from_be_bytes([out[8], out[9]]) } else { u16::from_le_bytes([out[8], out[9]]) }; + assert_eq!(count, 3); + assert_eq!(exif_orientation(&out), 6); + // Only YResolution is left (240): no X, so no resolution at all, and no stale 300. + assert_eq!(exif_resolution(&out), None); + let tags: Vec = (0..3) + .map(|k| { + let at = 10 + k * 12; + if e.big { u16::from_be_bytes([out[at], out[at + 1]]) } else { u16::from_le_bytes([out[at], out[at + 1]]) } + }) + .collect(); + assert_eq!(tags, vec![0x010F, Y, 0x0112]); + // The next-IFD pointer followed the table, the freed entries are zero. + assert_eq!(&out[10 + 36..10 + 40], &[0, 0, 0, 0]); + assert!(out[10 + 40..10 + 64].iter().all(|&b| b == 0)); + // The Make string (out of line) did not move. + assert!(out.windows(6).any(|w| w == b"NIKON\0")); + } +} + +#[test] +fn xmp_resolution_is_rewritten() { + let attr = r#""#; + assert_eq!( + xmp_with_resolution(attr, (240.0, 240.0)), + r#""# + ); + let elem = "300/1300/1"; + assert_eq!(xmp_with_resolution(elem, (72.5, 72.5)), "72500/100072500/1000"); + for same in [r#"tiff:XResolution="240" tiff:YResolution="240/1""#, ""] { + assert!(matches!(xmp_with_resolution(same, (240.0, 240.0)), Cow::Borrowed(_)), "{same}"); + } +} + +#[test] +fn jpeg_export_writes_one_resolution_everywhere() { + for e in [&LE, &BE] { + let xmp = r#""#; + let src = jpeg_with(&[jfif(1, 72, 72), exif_app1(&camera_exif(e)), xmp_app1(xmp)]); + let mut img = decode(&src).unwrap(); + near(img.meta.dpi, 300.0); + img.meta.dpi = Some((240.0, 240.0)); + let out = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(jfif_density(&out), (1, 240, 240)); + near(exif_resolution(&exif_of(&out)), 240.0); + let (_, x) = app_segments(&out).into_iter().find(|(m, p)| *m == 0xE1 && p.starts_with(b"http://ns.adobe.com/xap/1.0/\0")).unwrap(); + near(xmp_resolution(std::str::from_utf8(&x).unwrap()), 240.0); + near(dpi_of(&out), 240.0); + // The rest of the EXIF came along. + assert!(exif_of(&out).windows(6).any(|w| w == b"NIKON\0")); + } +} + +#[test] +fn other_formats_take_the_exif_resolution_when_they_have_none() { + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(camera_exif(&LE)); + for fmt in [Format::WebP, Format::Png] { + // Written at 240 with the 300 ppi EXIF: the EXIF follows the image. + img.meta.dpi = Some((240.0, 240.0)); + let out = encode(&img, fmt, &EncodeOptions::default()).unwrap(); + let back = decode(&out).unwrap(); + near(back.meta.dpi, 240.0); + near(back.meta.exif.as_deref().and_then(exif_resolution), 240.0); + // Written without a resolution: the EXIF one is read back. + img.meta.dpi = None; + let out = encode(&img, fmt, &EncodeOptions::default()).unwrap(); + near(decode(&out).unwrap().meta.dpi, 300.0); + } +} + +fn crc32(data: &[u8]) -> u32 { + let mut c = !0u32; + for &b in data { + c ^= u32::from(b); + for _ in 0..8 { + c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 }; + } + } + !c +} + +#[test] +fn png_phys_outranks_its_exif() { + // eXIf says 300 ppi, then a pHYs chunk of 150 ppi (5906 px/m) goes in after IHDR. + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(camera_exif(&LE)); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + near(decode(&png).unwrap().meta.dpi, 300.0); + near(decode(&with_phys(&png, 5906)).unwrap().meta.dpi, 150.0); +} + +/// `png` with a `pHYs` chunk of `ppm` pixels per metre after IHDR. +fn with_phys(png: &[u8], ppm: u32) -> Vec { + let mut body = b"pHYs".to_vec(); + body.extend_from_slice(&ppm.to_be_bytes()); + body.extend_from_slice(&ppm.to_be_bytes()); + body.push(1); + let mut chunk = 9u32.to_be_bytes().to_vec(); + chunk.extend_from_slice(&body); + chunk.extend_from_slice(&crc32(&body).to_be_bytes()); + let ihdr_end = 8 + 8 + 13 + 4; + [&png[..ihdr_end], &chunk, &png[ihdr_end..]].concat() +} + +#[test] +fn png_phys_of_a_whole_ppi_reads_back_whole() { + // pHYs holds whole pixels per metre: 300 ppi is stored as 11811 (299.9994 ppi), 72 as 2835 + // (72.009). The whole number that rounds to the stored value is read, as Photoshop does; a + // value no whole ppi rounds to stays as it is. + let img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + let png = encode(&img, Format::Png, &EncodeOptions { embed_metadata: false, ..Default::default() }).unwrap(); + for (ppm, want) in [(11811, 300.0), (2835, 72.0), (3937, 100.0), (11812, 300.0248)] { + let (x, y) = decode(&with_phys(&png, ppm)).unwrap().meta.dpi.unwrap(); + assert!((x - want).abs() < 1e-4 && (y - want).abs() < 1e-4, "{ppm} px/m: {x} × {y}, wanted {want}"); + } + // And a 300 ppi PNG written by the encoder opens at exactly 300. + let mut img = img; + img.meta.dpi = Some((300.0, 300.0)); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&png).unwrap().meta.dpi, Some((300.0, 300.0))); +} + +/// `camera_exif` plus IFD1, the embedded thumbnail a camera (or the last editor) wrote: +/// JPEGInterchangeFormat / JPEGInterchangeFormatLength naming a fake JPEG after the IFD. +fn exif_with_thumbnail(e: &Exif) -> Vec { + let mut v = camera_exif(e); + let ifd1 = v.len() as u32; + // IFD0 (4 entries) ends with its next-IFD pointer. + v[8 + 2 + 4 * 12..8 + 2 + 4 * 12 + 4].copy_from_slice(&e.u32(ifd1)); + let thumb = b"\xFF\xD8stale thumbnail\xFF\xD9"; + let thumb_at = ifd1 + 2 + 2 * 12 + 4; + v.extend_from_slice(&e.u16(2)); + for (tag, value) in [(0x0201u16, thumb_at), (0x0202, thumb.len() as u32)] { + v.extend_from_slice(&e.u16(tag)); + v.extend_from_slice(&e.u16(LONG)); + v.extend_from_slice(&e.u32(1)); + v.extend_from_slice(&e.u32(value)); + } + v.extend_from_slice(&e.u32(0)); + v.extend_from_slice(thumb); + v +} + +fn next_ifd(e: &Exif, exif: &[u8]) -> u32 { + let at = 8 + 2 + 4 * 12; + let b: [u8; 4] = exif[at..at + 4].try_into().unwrap(); + if e.big { u32::from_be_bytes(b) } else { u32::from_le_bytes(b) } +} + +/// #2147 / #2373: an export must not carry the source's EXIF thumbnail (IFD1). Finder, Android +/// and upload pickers show it instead of the image, so an edited photo kept its old thumbnail. +#[test] +fn exports_drop_the_source_exif_thumbnail() { + for e in [&LE, &BE] { + let exif = exif_with_thumbnail(e); + assert_ne!(next_ifd(e, &exif), 0); + for ppi in [None, Some((300.0, 300.0)), Some((240.0, 240.0))] { + let out = export_exif(&exif, ppi); + assert_eq!(next_ifd(e, &out), 0, "IFD1 is unlinked ({ppi:?})"); + assert_eq!(out.len(), exif.len(), "nothing moves"); + assert!(out.windows(6).any(|w| w == b"NIKON\0"), "the rest of the EXIF is kept"); + near(exif_resolution(&out), ppi.map_or(300.0, |p| p.0)); + } + // Only the pointer changes. + let out = export_exif(&exif, None); + let differing: Vec = (0..exif.len()).filter(|&i| exif[i] != out[i]).collect(); + assert!(differing.iter().all(|&i| (8 + 2 + 4 * 12..8 + 2 + 4 * 12 + 4).contains(&i)), "{differing:?}"); + // With a JPEG-style prefix too. + let prefixed = [b"Exif\0\0".as_slice(), &exif].concat(); + assert_eq!(next_ifd(e, &export_exif(&prefixed, None)[6..]), 0); + // An EXIF without a thumbnail comes through untouched. + assert!(matches!(export_exif(&camera_exif(e), None), Cow::Borrowed(_))); + // Through every encoder that writes EXIF. + let mut img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + img.meta.exif = Some(exif.clone()); + let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!(next_ifd(e, &exif_of(&jpeg)), 0, "JPEG"); + for format in [Format::Png, Format::WebP] { + let back = decode(&encode(&img, format, &EncodeOptions::default()).unwrap()).unwrap(); + let got = back.meta.exif.unwrap_or_else(|| panic!("{format:?} kept no EXIF")); + let body = got.strip_prefix(b"Exif\0\0").unwrap_or(&got); + assert_eq!(next_ifd(e, body), 0, "{format:?}"); + } + } +} + +#[test] +fn thumbnail_removal_never_panics_on_truncated_exif() { + for e in [&LE, &BE] { + let exif = exif_with_thumbnail(e); + for n in 0..exif.len() { + let _ = export_exif(&exif[..n], None); + let _ = export_exif(&exif[..n], Some((300.0, 300.0))); + } + } +} diff --git a/crates/image-codecs/tests/it/roundtrip.rs b/crates/image-codecs/tests/it/roundtrip.rs new file mode 100644 index 000000000..c9b179bc8 --- /dev/null +++ b/crates/image-codecs/tests/it/roundtrip.rs @@ -0,0 +1,348 @@ +//! Round-trip every writable format × every sample type × every layout. +//! Lossless formats must reproduce the (planned) conversion of the input +//! exactly; lossy formats must stay above a PSNR threshold. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +/// Minimum PSNR (dB) for lossy formats at default options. +fn psnr_threshold(format: Format) -> f64 { + match format { + Format::Jpeg => 30.0, + Format::Gif => 20.0, + Format::Hdr => 40.0, + Format::Avif => 28.0, + _ => f64::INFINITY, + } +} + +fn check_roundtrip(format: Format, layout: ChannelLayout, sample: SampleType) { + let c = caps(format); + if !c.write { + return; + } + // GIF quantizes; keep its input smooth so the threshold is meaningful. + let img = if format == Format::Gif { + synth(37, 23, layout, sample, 7, 0.0) + } else if c.lossy { + synth(37, 23, layout, sample, 7, 0.02) + } else { + test_image(layout, sample) + }; + let opts = EncodeOptions::default(); + let bytes = encode(&img, format, &opts).unwrap_or_else(|e| panic!("{format:?} {layout:?} {sample:?}: {e}")); + assert_eq!(detect(&bytes), Some(format), "detect after encode"); + if !c.read { + return; + } + let back = decode(&bytes).unwrap_or_else(|e| panic!("{format:?} {layout:?} {sample:?} decode: {e}")); + assert_eq!(back.dimensions(), img.dimensions()); + + // Native storage must be preserved when the format declares support. + let float_pnm_special = format == Format::Pnm && sample.is_float(); + if c.layouts.contains(&layout) && c.depths.contains(&sample) && !float_pnm_special { + assert_eq!(back.layout(), layout, "{format:?} must keep layout"); + assert_eq!(back.sample_type(), sample, "{format:?} must keep sample type"); + } + if c.depths.contains(&sample) && !float_pnm_special { + assert_eq!(back.sample_type(), sample, "{format:?} must keep supported depth"); + } + // Integer sources never lose precision in formats offering that depth. + if sample == SampleType::U16 && c.depths.contains(&SampleType::U16) { + assert!(matches!(back.sample_type(), SampleType::U16 | SampleType::F32)); + } + + let expected = img.convert(back.layout(), back.sample_type()); + let mut expected_cmp = expected.clone(); + if format == Format::Gif && layout.has_alpha() { + // GIF alpha is binary: compare colour only on opaque pixels. + let ch = back.layout().channels(); + let mut e = expected.to_normalized(); + let b = back.to_normalized(); + for (pe, pb) in e.chunks_mut(ch).zip(b.chunks(ch)) { + if pb[ch - 1] == 0.0 { + pe.copy_from_slice(pb); + } + pe[ch - 1] = pb[ch - 1]; + } + expected_cmp = Image::from_normalized(img.width(), img.height(), back.layout(), back.sample_type(), &e).unwrap(); + } + + if c.lossy { + let p = psnr(&expected_cmp, &back); + assert!(p >= psnr_threshold(format), "{format:?} {layout:?} {sample:?}: PSNR {p:.2} dB"); + } else { + assert_eq!( + expected_cmp.data(), + back.data(), + "{format:?} {layout:?} {sample:?}: lossless round-trip mismatch (max diff {})", + max_abs_diff(&expected_cmp, &back) + ); + } +} + +macro_rules! cases { + ($fmt:expr) => { + use super::*; + cases!(@ $fmt; + gray_u8: Gray, U8; gray_u16: Gray, U16; gray_f16: Gray, F16; gray_f32: Gray, F32; + graya_u8: GrayA, U8; graya_u16: GrayA, U16; graya_f16: GrayA, F16; graya_f32: GrayA, F32; + rgb_u8: Rgb, U8; rgb_u16: Rgb, U16; rgb_f16: Rgb, F16; rgb_f32: Rgb, F32; + rgba_u8: Rgba, U8; rgba_u16: Rgba, U16; rgba_f16: Rgba, F16; rgba_f32: Rgba, F32; + cmyk_u8: Cmyk, U8; cmyk_u16: Cmyk, U16; cmyk_f16: Cmyk, F16; cmyk_f32: Cmyk, F32; + cmyka_u8: CmykA, U8; cmyka_u16: CmykA, U16; cmyka_f16: CmykA, F16; cmyka_f32: CmykA, F32 + ); + }; + (@ $fmt:expr; $($name:ident : $l:ident, $s:ident);*) => { + $( + #[test] + fn $name() { + check_roundtrip($fmt, ChannelLayout::$l, SampleType::$s); + } + )* + }; +} + +mod png { + cases!(Format::Png); +} +mod jpeg { + cases!(Format::Jpeg); +} +mod tiff { + cases!(Format::Tiff); +} +mod webp { + cases!(Format::WebP); +} +mod gif { + cases!(Format::Gif); +} +mod bmp { + cases!(Format::Bmp); +} +mod tga { + cases!(Format::Tga); +} +mod ico { + cases!(Format::Ico); +} +mod pnm { + cases!(Format::Pnm); +} +mod fits { + cases!(Format::Fits); +} +mod dpx { + cases!(Format::Dpx); +} +mod dds { + cases!(Format::Dds); +} +mod qoi { + cases!(Format::Qoi); +} +mod exr { + cases!(Format::OpenExr); +} +mod hdr { + cases!(Format::Hdr); +} + +// --------------------------------------------------------------------------- +// Targeted round-trips beyond the matrix +// --------------------------------------------------------------------------- + +/// Float formats must keep HDR values (> 1, and negative where allowed). +#[test] +fn hdr_values_survive_float_formats() { + let vals: Vec = (0..16 * 8 * 3).map(|i| (i as f32 - 40.0) * 0.37).collect(); + let img = Image::from_f32(16, 8, ChannelLayout::Rgb, &vals).unwrap(); + for f in [Format::Tiff, Format::OpenExr, Format::Pnm] { + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.to_f32_samples().unwrap(), vals, "{f:?}"); + } +} + +#[test] +fn hdr_format_keeps_bright_values_approximately() { + let vals: Vec = (0..16 * 8 * 3).map(|i| 0.01 + i as f32 * 0.25).collect(); + let img = Image::from_f32(16, 8, ChannelLayout::Rgb, &vals).unwrap(); + let back = decode(&encode(&img, Format::Hdr, &EncodeOptions::default()).unwrap()).unwrap(); + // RGBE shares one exponent per pixel: error is relative to the brightest channel. + let got = back.to_f32_samples().unwrap(); + for (pa, pb) in vals.chunks(3).zip(got.chunks(3)) { + let m = pa.iter().copied().fold(0.0, f32::max); + for (a, b) in pa.iter().zip(pb) { + assert!((a - b).abs() <= m * 0.01, "{a} vs {b}"); + } + } +} + +#[test] +fn f16_exr_is_bit_exact() { + let vals: Vec = (0..20 * 10 * 4).map(|i| f16::from_f32(i as f32 * 0.013 - 1.0)).collect(); + let img = Image::from_f16(20, 10, ChannelLayout::Rgba, &vals).unwrap(); + for comp in [ExrCompression::None, ExrCompression::Rle, ExrCompression::Zip1, ExrCompression::Zip16, ExrCompression::Piz] { + let opts = EncodeOptions { exr_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::OpenExr, &opts).unwrap()).unwrap(); + assert_eq!(back.to_f16_samples().unwrap(), vals, "{comp:?}"); + } +} + +#[test] +fn tiff_all_compressions_lossless() { + for layout in ChannelLayout::ALL { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let img = test_image(layout, sample); + for comp in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let opts = EncodeOptions { tiff_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::Tiff, &opts).unwrap()).unwrap(); + if layout.is_lab() && sample != SampleType::U8 { + // CIELab holds a* and b* as 256·a at 16 bits and has no float form: 16-bit and + // float Lab come back within one 16-bit step (the PSD's 16-bit Lab does the same). + let (a, b) = (img.to_normalized(), back.convert(layout, sample).to_normalized()); + let worst = a.iter().zip(&b).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); + assert!(worst <= 1.5 / 65535.0, "{layout:?} {sample:?} {comp:?}: {worst}"); + continue; + } + assert_eq!(back, img.clone().with_meta(back.meta.clone()), "{layout:?} {sample:?} {comp:?}"); + } + } + } +} + +#[test] +fn compressed_tiff_does_not_keep_the_uncompressed_reservation() { + // The encoder reserves the uncompressed size up front for an uncompressed file (#6607). A + // smooth image compresses to a fraction of it, and no returned file holds much spare room. + let (w, h) = (512u32, 512u32); + let px: Vec = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x / 2) as u8, (y / 2) as u8, 128])).collect(); + let img = Image::from_u8(w, h, ChannelLayout::Rgb, px).unwrap(); + for comp in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate] { + let v = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: comp, ..Default::default() }).unwrap(); + if !matches!(comp, TiffCompression::None) { + assert!(v.len() < img.data().len() / 2, "{comp:?}: the image should compress well ({} bytes)", v.len()); + } + assert!(v.capacity() - v.len() <= v.len() / 8, "{comp:?}: {} bytes kept for a {} byte file", v.capacity(), v.len()); + assert_eq!(decode(&v).unwrap().data(), img.data(), "{comp:?}"); + } +} + +#[test] +fn png_all_compressions_lossless() { + let img = test_image(ChannelLayout::Rgba, SampleType::U16); + for comp in [PngCompression::None, PngCompression::Fast, PngCompression::Default, PngCompression::Best] { + let opts = EncodeOptions { png_compression: comp, ..Default::default() }; + let back = decode(&encode(&img, Format::Png, &opts).unwrap()).unwrap(); + assert_eq!(back.data(), img.data(), "{comp:?}"); + } +} + +#[test] +fn jpeg_quality_affects_size_and_error() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let lo = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 20, ..Default::default() }).unwrap(); + let hi = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 98, ..Default::default() }).unwrap(); + assert!(lo.len() < hi.len()); + let plo = psnr(&img, &decode(&lo).unwrap()); + let phi = psnr(&img, &decode(&hi).unwrap()); + assert!(phi > plo, "{phi} <= {plo}"); +} + +#[test] +fn jpeg_444_beats_420_on_chroma_detail() { + let img = synth(64, 64, ChannelLayout::Rgb, SampleType::U8, 3, 0.5); + let a = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: false, jpeg_quality: 95, ..Default::default() }).unwrap(); + let b = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: true, jpeg_quality: 95, ..Default::default() }).unwrap(); + assert!(psnr(&img, &decode(&a).unwrap()) > psnr(&img, &decode(&b).unwrap())); +} + +/// JPEG Options › Format Options: each scan layout decodes back to the same image, Progressive +/// writes a progressive frame (SOF2), and Baseline Optimized fits its Huffman tables (smaller at +/// 4:4:4; at 4:2:0 it keeps the standard tables, see `JpegFormat::Optimized`). +#[test] +fn jpeg_format_options_round_trip() { + let img = synth(64, 48, ChannelLayout::Rgb, SampleType::U8, 3, 0.5); + let sof = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]); + for subsampled in [false, true] { + let enc = |f| { + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }).unwrap() + }; + let (base, opt, prog) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized), enc(JpegFormat::Progressive)); + // The scan layout only changes how the same quantized coefficients are stored, so every + // format decodes as faithfully as Baseline. + let reference = psnr(&img, &decode(&base).unwrap()); + for (name, b) in [("optimized", &opt), ("progressive", &prog)] { + let back = decode(b).unwrap(); + assert_eq!(back.dimensions(), img.dimensions(), "{name}"); + let p = psnr(&img, &back); + assert!((p - reference).abs() < 0.5, "{name}, subsampled {subsampled}: {p} vs baseline {reference}"); + } + assert!(sof(&base, 0xC0) && !sof(&base, 0xC2)); + assert!(sof(&prog, 0xC2), "progressive frame"); + if subsampled { + assert_eq!(opt, base, "4:2:0 keeps the standard tables"); + } else { + assert!(opt.len() < base.len(), "fitted tables are smaller: {} vs {}", opt.len(), base.len()); + } + } + // Gray is 4:4:4 too, so Baseline Optimized shrinks it. + let gray = synth(64, 48, ChannelLayout::Gray, SampleType::U8, 3, 0.5); + let enc = |f| encode(&gray, Format::Jpeg, &EncodeOptions { jpeg_format: f, ..Default::default() }).unwrap(); + let (base, opt) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized)); + assert!(opt.len() < base.len(), "gray: {} vs {}", opt.len(), base.len()); + assert!((psnr(&gray, &decode(&opt).unwrap()) - psnr(&gray, &decode(&base).unwrap())).abs() < 0.5); + assert_eq!(JpegFormat::parse("progressive"), Some(JpegFormat::Progressive)); + assert_eq!(JpegFormat::parse(JpegFormat::Optimized.name()), Some(JpegFormat::Optimized)); + for bad in ["Progressive", "", "standard"] { + assert_eq!(JpegFormat::parse(bad), None, "{bad}"); + } +} + +#[test] +fn webp_lossy_request_is_a_lossy_file() { + let img = test_image(ChannelLayout::Rgb, SampleType::U8); + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).unwrap(); + assert_eq!(&bytes[12..16], b"VP8 ", "a bare lossy frame"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), img.dimensions()); + assert!( + fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).contains(&FidelityWarning::LossyCompression) + ); +} + +#[test] +fn one_pixel_images_roundtrip() { + for f in rw_formats() { + let img = synth(1, 1, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.dimensions(), (1, 1), "{f:?}"); + } +} + +#[test] +fn ico_rejects_oversize() { + let img = Image::new(300, 10, ChannelLayout::Rgba, SampleType::U8).unwrap(); + assert!(matches!(encode(&img, Format::Ico, &EncodeOptions::default()), Err(CodecError::Encode { .. }))); + let img = Image::new(256, 256, ChannelLayout::Rgba, SampleType::U8).unwrap(); + assert!(encode(&img, Format::Ico, &EncodeOptions::default()).is_ok()); +} + +#[test] +fn empty_image_rejected() { + let img = Image::new(0, 5, ChannelLayout::Rgb, SampleType::U8).unwrap(); + for f in writable_formats() { + assert!(encode(&img, f, &EncodeOptions::default()).is_err(), "{f:?}"); + } +} + +#[test] +fn larger_image_png_tiff_exact() { + let img = synth(513, 257, ChannelLayout::Rgba, SampleType::U16, 99, 1.0); + for f in [Format::Png, Format::Tiff, Format::Pnm] { + let back = decode(&encode(&img, f, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.data(), img.data(), "{f:?}"); + } +} diff --git a/crates/image-codecs/tests/it/source_settings.rs b/crates/image-codecs/tests/it/source_settings.rs new file mode 100644 index 000000000..07b20471a --- /dev/null +++ b/crates/image-codecs/tests/it/source_settings.rs @@ -0,0 +1,111 @@ +//! Reading back the settings a lossy file was saved with: a JPEG's quality from its +//! quantisation tables, a WebP's lossy or lossless encoding (and its quality). + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn photo(w: u32, h: u32, layout: ChannelLayout) -> Image { + synth(w, h, layout, SampleType::U8, 5, 0.03) +} + +#[test] +fn jpeg_quality_is_estimated_within_two() { + for layout in [ChannelLayout::Rgb, ChannelLayout::Gray] { + let img = photo(40, 24, layout); + for q in [1u8, 5, 10, 25, 40, 50, 60, 70, 75, 80, 85, 90, 95, 98, 100] { + let bytes = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: q, ..Default::default() }).unwrap(); + let est = jpeg_quality(&bytes).unwrap_or_else(|| panic!("{layout:?} q{q}: no estimate")); + assert!(est.abs_diff(q) <= 2, "{layout:?}: saved at {q}, estimated {est}"); + } + } +} + +#[test] +fn jpeg_quality_of_hand_built_tables() { + // A minimal JPEG prefix: SOI, one DQT holding the Annex K luminance table (quality 50) in + // zig-zag order, then SOS. + let luma: [u8; 64] = [ + 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, + 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, + ]; + let mut b = vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00]; + b.extend_from_slice(&luma); + b.extend_from_slice(&[0xFF, 0xDA, 0, 2]); + assert_eq!(jpeg_quality(&b), Some(50)); +} + +#[test] +fn hostile_jpeg_tables_give_no_estimate_and_never_panic() { + let img = photo(16, 16, ChannelLayout::Rgb); + let good = encode(&img, Format::Jpeg, &EncodeOptions { jpeg_quality: 70, ..Default::default() }).unwrap(); + // Every truncation of a real file. + for n in 0..good.len().min(800) { + let _ = jpeg_quality(&good[..n]); + } + let cases: Vec> = vec![ + vec![], + vec![0xFF], + vec![0xFF, 0xD8], + // A DQT whose length runs past the end. + vec![0xFF, 0xD8, 0xFF, 0xDB, 0xFF, 0xFF, 0x00, 1, 2, 3], + // A DQT of length 0 and 1 (less than its own length field). + vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x00], + vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x01], + // A table cut short, a bad precision, a slot past 3, a 16-bit table cut mid-entry. + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 12, 0x00], vec![9; 9]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x70], vec![9; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x0F], vec![9; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 68, 0x10], vec![0xFF; 65]].concat(), + // All-zero and all-0xFF tables. + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00], vec![0; 64]].concat(), + [vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 131, 0x10], vec![0xFF; 128]].concat(), + vec![0xFF; 64], + ]; + for c in &cases { + let _ = jpeg_quality(c); + } + assert_eq!(jpeg_quality(&[0xFF, 0xD8, 0xFF, 0xDB, 0x00, 0x00]), None); + assert_eq!(jpeg_quality(b"not a jpeg at all"), None); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(jpeg_quality(&png), None); +} + +#[test] +fn webp_lossless_and_lossy_are_told_apart() { + let img = photo(33, 21, ChannelLayout::Rgb); + let lossless = encode(&img, Format::WebP, &EncodeOptions::default()).unwrap(); + assert_eq!(webp_encoding(&lossless), Some(WebpEncoding::Lossless)); + for q in [10u8, 30, 50, 75, 80, 90, 100] { + let bytes = encode(&img, Format::WebP, &EncodeOptions { webp_lossless: false, webp_quality: q, ..Default::default() }).unwrap(); + let Some(WebpEncoding::Lossy { quality: Some(est) }) = webp_encoding(&bytes) else { panic!("q{q}: {:?}", webp_encoding(&bytes)) }; + assert!(est.abs_diff(q) <= 2, "lossy WebP saved at {q}, estimated {est}"); + } + // With alpha and metadata (the extended container). + let rgba = photo(20, 20, ChannelLayout::Rgba); + let bytes = encode(&rgba, Format::WebP, &EncodeOptions { webp_lossless: false, webp_quality: 60, ..Default::default() }).unwrap(); + assert!(matches!(webp_encoding(&bytes), Some(WebpEncoding::Lossy { quality: Some(58..=62) })), "{:?}", webp_encoding(&bytes)); +} + +#[test] +fn hostile_webp_never_panics() { + let img = photo(24, 24, ChannelLayout::Rgb); + for opts in [EncodeOptions::default(), EncodeOptions { webp_lossless: false, ..Default::default() }] { + let good = encode(&img, Format::WebP, &opts).unwrap(); + for n in 0..good.len().min(400) { + let _ = webp_encoding(&good[..n]); + } + let mut flipped = good.clone(); + for i in 12..flipped.len().min(64) { + flipped[i] ^= 0xA5; + let _ = webp_encoding(&flipped); + } + } + let mut huge = b"RIFF\0\0\0\0WEBPVP8 ".to_vec(); + huge.extend_from_slice(&u32::MAX.to_le_bytes()); + let _ = webp_encoding(&huge); + let mut anmf = b"RIFF\0\0\0\0WEBPANMF".to_vec(); + anmf.extend_from_slice(&3u32.to_le_bytes()); + let _ = webp_encoding(&anmf); + assert_eq!(webp_encoding(b"RIFF\0\0\0\0WAVE"), None); +} diff --git a/crates/image-codecs/tests/it/tga.rs b/crates/image-codecs/tests/it/tga.rs new file mode 100644 index 000000000..489e2eed1 --- /dev/null +++ b/crates/image-codecs/tests/it/tga.rs @@ -0,0 +1,91 @@ +//! Targa writing (#6382): run-length packets end with their scanline, rows are stored bottom-up, +//! and the file reads back exactly. + +use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image, decode_as, encode}; + +/// The packets of a run-length encoded Targa body, as (pixels, is a run), from just after the +/// 18-byte header (no image ID or colour map) up to `pixels` pixels. +fn packets(file: &[u8], bpp: usize, pixels: usize) -> Vec<(usize, bool)> { + let mut at = 18; + let mut seen = 0; + let mut out = Vec::new(); + while seen < pixels { + let head = file[at]; + let count = usize::from(head & 0x7f) + 1; + let run = head & 0x80 != 0; + at += 1 + if run { bpp } else { count * bpp }; + seen += count; + out.push((count, run)); + } + out +} + +/// A `w`×`h` image whose rows mix long runs (wider than a packet), short ones and noise, and +/// whose last pixel equals the next row's first, so a packet would run on if it could. +fn sample(w: u32, h: u32, layout: ChannelLayout) -> Image { + let n = layout.channels(); + let mut data = Vec::new(); + for y in 0..h { + for x in 0..w { + let v = match x { + _ if x < 150 => 40, + _ if x % 3 == 0 => (x * 7 + y * 13) as u8, + _ => 200, + }; + let px = [v, v.wrapping_add(y as u8), v ^ 0x5a, 255 - v]; + data.extend_from_slice(&px[..n]); + } + } + Image::from_u8(w, h, layout, data).unwrap() +} + +#[test] +fn rle_packets_end_with_their_scanline() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let (w, h) = (300u32, 4u32); + let file = encode(&sample(w, h, layout), Format::Tga, &EncodeOptions::default()).unwrap(); + let bpp = layout.channels(); + let mut col = 0; + for (count, _) in packets(&file, bpp, (w * h) as usize) { + assert!(count <= 128, "{layout:?}: a packet holds at most 128 pixels"); + assert!(col + count <= w as usize, "{layout:?}: a packet runs from column {col} past the row's end"); + col = (col + count) % w as usize; + } + // A solid image is one run per row (three packets of 128, 128 and 44), not one long run. + let solid = Image::from_u8(w, h, layout, vec![90; (w * h) as usize * bpp]).unwrap(); + let file = encode(&solid, Format::Tga, &EncodeOptions::default()).unwrap(); + let p = packets(&file, bpp, (w * h) as usize); + assert_eq!(p.len(), 3 * h as usize, "{layout:?}"); + assert!(p.iter().all(|&(_, run)| run)); + } +} + +#[test] +fn the_file_is_bottom_up_rle_with_its_alpha_bits_and_the_tga2_footer() { + let mut data = vec![0u8; 2 * 2 * 4]; + // Top row red, bottom row blue, opaque. + for (i, px) in data.as_chunks_mut::<4>().0.iter_mut().enumerate() { + *px = if i < 2 { [255, 0, 0, 255] } else { [0, 0, 255, 255] }; + } + let file = encode(&Image::from_u8(2, 2, ChannelLayout::Rgba, data).unwrap(), Format::Tga, &EncodeOptions::default()).unwrap(); + assert_eq!(file[2], 10, "run-length true colour"); + assert_eq!((file[16], file[17]), (32, 8), "32 bits per pixel, 8 alpha bits, bottom-left origin"); + // The first stored row is the bottom one: a run of two blue pixels, in BGRA. + assert_eq!(&file[18..23], &[0x81, 255, 0, 0, 255]); + assert!(file.ends_with(b"TRUEVISION-XFILE.\0")); + let gray = encode(&Image::from_u8(2, 1, ChannelLayout::Gray, vec![1, 2]).unwrap(), Format::Tga, &EncodeOptions::default()).unwrap(); + assert_eq!((gray[2], gray[16], gray[17]), (11, 8, 0), "run-length grayscale, no alpha bits"); +} + +#[test] +fn every_layout_reads_back_exactly() { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + for (w, h) in [(1, 1), (300, 4), (129, 3), (5, 130)] { + let img = sample(w, h, layout); + let file = encode(&img, Format::Tga, &EncodeOptions::default()).unwrap(); + let back = decode_as(Format::Tga, &file).unwrap(); + assert_eq!((back.dimensions(), back.layout()), ((w, h), layout), "{layout:?} {w}x{h}"); + assert_eq!(back.data(), img.data(), "{layout:?} {w}x{h}"); + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_bands.rs b/crates/image-codecs/tests/it/tiff_bands.rs new file mode 100644 index 000000000..a449ae2b7 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_bands.rs @@ -0,0 +1,367 @@ +//! `TiffBands` (#8169): decoding a TIFF a group of bands at a time gives exactly the image the +//! whole-image decoder does, for every layout the decoder reads, every orientation, and groups of +//! any size; and it opens images whose single buffer the whole-image decoder refuses. + +use crate::common::tiffgen::*; +use craft_image_codecs::*; + +/// A deterministic byte pattern. +fn noise(n: usize, seed: u32) -> Vec { + let mut x = seed.wrapping_mul(2_654_435_761).max(1); + (0..n) + .map(|_| { + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + (x >> 24) as u8 + }) + .collect() +} + +/// The image `TiffBands` decodes, assembled from its groups. +fn banded(bytes: &[u8], page: Option, limits: &Limits, upright: bool) -> Result { + let mut t = TiffBands::open(bytes, page, limits)?; + if upright { + t = t.upright(); + } + let (w, h) = t.dimensions(); + let mut out = Image::new(w, h, t.layout(), t.sample_type())?; + let bpp = t.layout().channels() * t.sample_type().bytes(); + let mut groups = 0; + t.decode(|x, y, g: &Image| -> Result<(), CodecError> { + groups += 1; + assert_eq!((g.layout(), g.sample_type()), (out.layout(), out.sample_type())); + let (gw, gh) = g.dimensions(); + assert!(x + gw <= w && y + gh <= h, "group {gw}x{gh} at {x},{y} outside {w}x{h}"); + let row = gw as usize * bpp; + for r in 0..gh as usize { + let dst = ((y as usize + r) * w as usize + x as usize) * bpp; + out.data_mut()[dst..dst + row].copy_from_slice(&g.data()[r * row..(r + 1) * row]); + } + Ok(()) + })?; + assert!(groups >= 1); + out.icc = t.icc.clone(); + out.meta = t.meta.clone(); + out.warnings = t.warnings.clone(); + Ok(out) +} + +/// The whole-image decode of the same page. +fn whole(bytes: &[u8], page: Option, upright: bool) -> Image { + let opts = DecodeOptions { keep_orientation: !upright, ..Default::default() }; + match page { + Some(p) => decode_tiff_page(bytes, p, &opts).unwrap(), + None => decode_with(bytes, &opts).unwrap(), + } +} + +/// Limits whose `max_alloc` makes each group `bands` bands of `band_bytes`. +fn grouped(band_bytes: usize, bands: usize) -> Limits { + Limits { max_alloc: (band_bytes * bands) as u64, ..Limits::banded() } +} + +fn assert_same(bytes: &[u8], band_bytes: usize, what: &str) { + for upright in [false, true] { + let want = whole(bytes, None, upright); + for limits in [Limits::banded(), grouped(band_bytes, 1), grouped(band_bytes, 2), grouped(band_bytes, 3)] { + let got = banded(bytes, None, &limits, upright).unwrap_or_else(|e| panic!("{what}: {e}")); + assert_eq!(got.dimensions(), want.dimensions(), "{what}"); + assert_eq!((got.layout(), got.sample_type()), (want.layout(), want.sample_type()), "{what}"); + assert!(got.data() == want.data(), "{what}: pixels differ (max_alloc {})", limits.max_alloc); + assert_eq!(got.icc, want.icc, "{what}"); + assert_eq!(got.meta, want.meta, "{what}"); + assert_eq!(got.warnings, want.warnings, "{what}"); + } + } +} + +/// `n / 1` as a little-endian RATIONAL value. +fn rational(n: u64) -> u64 { + n | 1 << 32 +} + +const W: u32 = 37; +const H: u32 = 29; + +#[test] +fn every_layout_matches_the_whole_image_decoder() { + let n = (W * H) as usize; + let rows = 4u32; + let row = |spp: usize, bps: usize| W as usize * spp * bps; + let cases: Vec<(&str, Dir, usize)> = vec![ + ("rgb8 strips", strips(W, H, 8, 3, 2, rows, &noise(n * 3, 1)), row(3, 1) * rows as usize), + ("gray8 WhiteIsZero", strips(W, H, 8, 1, 0, rows, &noise(n, 2)), row(1, 1) * rows as usize), + ("gray16 big-endian", strips(W, H, 16, 1, 1, rows, &noise(n * 2, 3)), row(1, 2) * rows as usize), + ("cmyk8", strips(W, H, 8, 4, 5, rows, &noise(n * 4, 4)), row(4, 1) * rows as usize), + ("cielab8", strips(W, H, 8, 3, 8, rows, &noise(n * 3, 5)), row(3, 1) * rows as usize), + ("rgba16 associated alpha", strips(W, H, 16, 4, 2, rows, &noise(n * 8, 6)).tag(338, SHORT, &[1]), row(4, 2) * rows as usize), + ( + "gray f32", + strips(W, H, 32, 1, 1, rows, &f32s(&noise(n, 7).iter().map(|&b| f32::from(b) / 255.0).collect::>(), true)).tag(339, SHORT, &[3]), + row(1, 4) * rows as usize, + ), + ( + "palette 4-bit", + strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag(320, SHORT, &(0..48u64).map(|i| i * 1365).collect::>()), + row(3, 1) * rows as usize, + ), + ("gray 1-bit", strips(W, H, 1, 1, 1, rows, &noise((W as usize).div_ceil(8) * H as usize, 9)), row(1, 1) * rows as usize), + ]; + for (what, dir, band) in cases { + // Both byte orders: the generator writes multi-byte samples as given, so the + // big-endian files hold different values; each is compared against its own decode. + for little in [true, false] { + assert_same(&build(little, false, std::slice::from_ref(&dir), &[0]).bytes, band, what); + } + } +} + +#[test] +fn tiles_and_planar_data_match() { + let (w, h) = (W as usize, H as usize); + let px = noise(w * h * 3 * 2, 11); + let tiled = |planar: bool| { + let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true }.tag(322, SHORT, &[16]).tag( + 323, + SHORT, + &[16], + ); + if planar { + d = d.tag(284, SHORT, &[2]); + } + d + }; + let band = w * 3 * 2 * 16; + assert_same(&build(true, false, &[tiled(false)], &[0]).bytes, band, "chunky tiles"); + assert_same(&build(true, true, &[tiled(true)], &[0]).bytes, band, "planar tiles, BigTIFF"); + let planar = Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); + assert_same(&build(true, false, &[planar], &[0]).bytes, w * 3 * 2 * 5, "planar strips"); +} + +#[test] +fn every_orientation_matches() { + let n = (W * H) as usize; + for o in 1..=8u64 { + let dir = strips(W, H, 8, 3, 2, 3, &noise(n * 3, 20 + o as u32)) + .tag(274, SHORT, &[o]) + .tag(282, RATIONAL, &[rational(300)]) + .tag(283, RATIONAL, &[rational(150)]) + .tag(296, SHORT, &[2]); + let bytes = build(true, false, &[dir], &[0]).bytes; + assert_same(&bytes, W as usize * 3 * 3, &format!("orientation {o}")); + let t = TiffBands::open(&bytes, None, &Limits::banded()).unwrap(); + assert_eq!(t.orientation(), o as u16); + let up = t.upright(); + let swapped = o >= 5; + assert_eq!(up.dimensions(), if swapped { (H, W) } else { (W, H) }, "orientation {o}"); + assert_eq!(up.meta.dpi, Some(if swapped { (150.0, 300.0) } else { (300.0, 150.0) }), "orientation {o}"); + } +} + +#[test] +fn cut_off_data_matches_and_warns() { + let n = (W * H) as usize; + let built = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 30))], &[0]); + // The last four of eight strips point past the end of the file: their data is missing. + let mut built = built; + let past = built.bytes.len() as u64 + 4096; + for i in 4..8 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + assert_same(&built.bytes, W as usize * 3 * 4, "cut off"); + let img = banded(&built.bytes, None, &Limits::banded(), true).unwrap(); + assert!(img.warnings.contains(&DecodeWarning::Truncated { format: Format::Tiff })); +} + +#[test] +fn a_strip_cut_off_partway_decodes_as_zeros() { + // An uncompressed strip read straight into its output rows, whose data starts in the file + // and ends past it: the bytes read before the end must not survive (#8536 review). + let n = (W * H) as usize; + let mut built = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 31))], &[0]); + let strip = W as usize * 3 * 4; + let near_end = built.bytes.len() as u64 - 100; + patch_value(&mut built, true, false, 0, 273, 6, near_end); + let img = banded(&built.bytes, None, &Limits::banded(), false).unwrap(); + assert!(img.warnings.contains(&DecodeWarning::Truncated { format: Format::Tiff })); + assert!(img.data()[6 * strip..7 * strip].iter().all(|&v| v == 0), "partial strip bytes kept"); + assert!(img.data()[..6 * strip] == whole(&built.bytes, None, false).data()[..6 * strip]); +} + +#[test] +fn other_pages_and_the_more_pages_warning_match() { + let n = (W * H) as usize; + let a = strips(W, H, 8, 1, 1, 5, &noise(n, 40)); + let b = strips(W, H, 8, 3, 2, 5, &noise(n * 3, 41)); + let bytes = build(true, false, &[a, b], &[0, 1]).bytes; + for page in [None, Some(0), Some(1)] { + let want = whole(&bytes, page, false); + let got = banded(&bytes, page, &Limits::banded(), false).unwrap(); + assert!(got.data() == want.data(), "page {page:?}"); + assert_eq!(got.warnings, want.warnings, "page {page:?}"); + } + assert!(matches!(TiffBands::open(&bytes, Some(9), &Limits::banded()), Err(CodecError::InvalidImage(_)))); +} + +#[test] +fn a_cut_off_page_of_several_warns_in_the_whole_image_order() { + // Truncated before MorePages, as the whole-image decode reports them (#8536 review). + let n = (W * H) as usize; + let a = strips(W, H, 8, 3, 2, 4, &noise(n * 3, 42)); + let b = strips(W, H, 8, 1, 1, 5, &noise(n, 43)); + let mut built = build(true, false, &[a, b], &[0, 1]); + let past = built.bytes.len() as u64 + 4096; + for i in 4..8 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + let want = whole(&built.bytes, None, false); + assert!(matches!(want.warnings[..], [DecodeWarning::Truncated { .. }, DecodeWarning::MorePages { .. }]), "{:?}", want.warnings); + let got = banded(&built.bytes, None, &Limits::banded(), false).unwrap(); + assert_eq!(got.warnings, want.warnings); +} + +#[test] +fn group_bytes_is_the_largest_group_handed_over() { + let n = (W * H) as usize; + // Eight strips of four rows, two to a group; one strip holding the whole image. + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 44))], &[0]).bytes; + let band = W as usize * 3 * 4; + let mut t = TiffBands::open(&bytes, None, &grouped(band, 2)).unwrap(); + assert_eq!(t.group_bytes().unwrap(), 2 * band); + let mut largest = 0; + t.decode(|_, _, g: &Image| -> Result<(), CodecError> { + largest = largest.max(g.data().len()); + Ok(()) + }) + .unwrap(); + assert_eq!(largest, 2 * band); + let single = build(true, false, &[strips(W, H, 8, 3, 2, H, &noise(n * 3, 45))], &[0]).bytes; + let t = TiffBands::open(&single, None, &Limits::banded()).unwrap(); + assert_eq!(t.group_bytes().unwrap(), n * 3, "a single strip is one group: the whole image"); +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn opens_an_image_over_the_whole_image_budget() { + // The whole-image decoder needs the image in one buffer; the banded one only a band (on + // 64-bit targets: 32-bit ones still check the whole image). + let n = (W * H) as usize; + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 2, &noise(n * 3, 50))], &[0]).bytes; + let band = W as u64 * 3 * 2; + let tight = Limits { max_alloc: band * 3, ..Limits::banded() }; + let refused = decode_with(&bytes, &DecodeOptions { limits: tight, keep_orientation: false }); + assert!(matches!(refused, Err(CodecError::LimitExceeded(_))), "{refused:?}"); + let got = banded(&bytes, None, &tight, true).unwrap(); + assert!(got.data() == whole(&bytes, None, true).data()); +} + +#[test] +fn size_limits_still_apply() { + let n = (W * H) as usize; + let bytes = build(true, false, &[strips(W, H, 8, 3, 2, 4, &noise(n * 3, 60))], &[0]).bytes; + let few_pixels = Limits { max_pixels: u64::from(W * H) - 1, ..Limits::banded() }; + assert!(matches!(TiffBands::open(&bytes, None, &few_pixels), Err(CodecError::LimitExceeded(_)))); + let narrow = Limits { max_width: W - 1, ..Limits::banded() }; + assert!(matches!(TiffBands::open(&bytes, None, &narrow), Err(CodecError::LimitExceeded(_)))); + // One strip is more than `max_alloc`: refused before decoding, with the image size. + let tiny = Limits { max_alloc: 10, ..Limits::banded() }; + match TiffBands::open(&bytes, None, &tiny) { + Err(CodecError::LimitExceeded(m)) => assert!(m.contains(&format!("{W}x{H}")), "{m}"), + other => panic!("{:?}", other.map(|t| t.dimensions())), + } +} + +#[test] +fn banded_limits_allow_the_largest_document_size() { + let l = Limits::banded(); + if cfg!(target_pointer_width = "64") { + assert_eq!((l.max_width, l.max_height), (300_000, 300_000)); + assert!(l.max_pixels >= 300_000 * 300_000); + // The banner from #8456 / #8169: 136 ft × 26 ft at 72 ppi. + assert!(117_504u64 * 22_464 > Limits::default().max_pixels); + assert!(117_504u64 * 22_464 <= l.max_pixels); + } else { + assert_eq!(l, Limits::default()); + } + assert_eq!(l.max_alloc, Limits::default().max_alloc); +} + +#[test] +fn a_hostile_header_is_refused_before_allocating() { + // 300000² declared in a file of a few kilobytes: one 270 GB strip, which neither the + // decompression-bomb guard nor the per-group budget lets through. + let mut built = build(true, true, &[strips(1000, 4, 8, 3, 2, 4, &noise(12_000, 70))], &[0]); + for tag in [256, 257, 278] { + patch_value(&mut built, true, true, 0, tag, 0, 300_000); + } + assert!(matches!(TiffBands::open(&built.bytes, None, &Limits::banded()), Err(CodecError::Malformed { .. } | CodecError::LimitExceeded(_)))); + // Past the 300000 px a side documents allow. + let mut wide = build(true, true, &[strips(1000, 4, 8, 3, 2, 4, &noise(12_000, 71))], &[0]); + patch_value(&mut wide, true, true, 0, 256, 0, 300_001); + assert!(matches!(TiffBands::open(&wide.bytes, None, &Limits::banded()), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn padding_does_not_let_missing_chunks_past_the_bomb_guard() { + // 20000² RGB declared as Deflate (1.2 GB of samples), every strip past the end of the file, + // and 400 KB of padding: the file is large enough for that size at Deflate's ratio, but its + // strips hold nothing. Refused before anything is allocated, by both decoders (#8536 review). + let (w, h) = (20_000, 20_000); + let mut dir = Dir { entries: base(w, h, 8, 3, 2), chunks: vec![vec![0]; 20], tiled: false }.tag(278, LONG, &[1000]); + dir.entries.retain(|e| e.0 != 259); + let dir = dir.tag(259, SHORT, &[8]).bytes(65_000, UNDEFINED, &vec![0; 400_000]); + let mut built = build(true, false, &[dir], &[0]); + let past = built.bytes.len() as u64 + 4096; + for i in 0..20 { + patch_value(&mut built, true, false, 0, 273, i, past); + } + match TiffBands::open(&built.bytes, None, &Limits::banded()) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("hold 0 bytes"), "{message}"), + other => panic!("{:?}", other.map(|t| t.dimensions())), + } + assert!(matches!(decode(&built.bytes), Err(CodecError::Malformed { .. }))); +} + +/// An all-white CCITT Group 4 strip: one V0 code (a 1 bit) per row, then the EOFB. +fn blank_g4(h: u32) -> Vec { + let mut bits = vec![true; h as usize]; + for _ in 0..2 { + bits.extend([false; 11]); + bits.push(true); + } + bits.chunks(8).map(|c| c.iter().enumerate().fold(0u8, |a, (i, &b)| a | (u8::from(b) << (7 - i)))).collect() +} + +#[test] +fn a_blank_fax_page_wider_than_4096_px_passes_the_bomb_guard() { + // A blank 600 dpi page (A4 landscape, A3) codes each row in one bit, past 4096:1: it must + // open, with or without the metadata and other pages that used to raise the bound. + for (w, h) in [(7016u32, 4961u32), (7016, 9921)] { + let mut d = Dir { entries: base(w, h, 1, 1, 0), chunks: vec![blank_g4(h)], tiled: false }.tag(278, LONG, &[u64::from(h)]); + d.entries.retain(|e| e.0 != 259); + let d = d.tag(259, SHORT, &[4]); + let pages: Vec = (0..3).map(|_| d.clone()).collect(); + for (what, bytes) in + [("one page", build(true, false, std::slice::from_ref(&d), &[0]).bytes), ("three pages", build(true, false, &pages, &[0, 1, 2]).bytes)] + { + let img = decode(&bytes).unwrap_or_else(|e| panic!("{w}x{h} {what}: {e}")); + assert_eq!(img.dimensions(), (w, h), "{what}"); + assert!(img.data().iter().all(|&v| v == 255), "{w}x{h} {what}: not white"); + } + } + let mut d = Dir { entries: base(7016, 4961, 1, 1, 0), chunks: vec![blank_g4(4961)], tiled: false }.tag(278, LONG, &[4961]); + d.entries.retain(|e| e.0 != 259); + let bytes = build(true, false, &[d.tag(259, SHORT, &[4])], &[0]).bytes; + assert!(banded(&bytes, None, &Limits::banded(), true).unwrap().data() == decode(&bytes).unwrap().data()); + // Still a bound: 100 bytes of Group 4 can't hold that page (4096:1 for Group 3, which is not decoded). + for compression in [4, 3] { + let mut d = Dir { entries: base(7016, 4961, 1, 1, 0), chunks: vec![vec![0x80; 100]], tiled: false }.tag(278, LONG, &[4961]); + d.entries.retain(|e| e.0 != 259); + let bytes = build(true, false, &[d.tag(259, SHORT, &[compression])], &[0]).bytes; + match decode(&bytes) { + Err(CodecError::Malformed { message, .. }) => assert!(message.contains("hold 100 bytes"), "{message}"), + other => panic!("compression {compression}: {:?}", other.map(|i| i.dimensions())), + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_bench.rs b/crates/image-codecs/tests/it/tiff_bench.rs new file mode 100644 index 000000000..55cff55ca --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_bench.rs @@ -0,0 +1,124 @@ +//! TIFF decode timings on a synthetic ~30 MP image (6336×4752). Ignored by default; run in +//! release: +//! +//! ```sh +//! cargo test --release -p craft-image-codecs --test it -- tiff_bench:: --ignored --nocapture +//! ``` + +use crate::common; +use common::tiffgen::*; +use craft_image_codecs::*; +use std::time::Instant; + +const W: u32 = 6336; +const H: u32 = 4752; + +fn pixels(spp: usize) -> Vec { + // A smooth gradient with a little texture: compresses like a photo, not like noise. + let mut v = Vec::with_capacity(W as usize * H as usize * spp); + let mut s = 0x1234_5678u32; + for y in 0..H { + for x in 0..W { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + for c in 0..spp { + let g = (x * (c as u32 + 1) / 25 + y / 19) as u8; + v.push(g.wrapping_add((s >> (8 * c)) as u8 & 3)); + } + } + } + v +} + +/// The decoder before FILE-215-8, for a same-run comparison: the `tiff` crate decodes the whole +/// image into one buffer (`read_image`, first plane only), which is then copied into an `Image`. +fn legacy(bytes: &[u8]) -> Result { + use tiff::decoder::{Decoder, DecodingResult, Limits as TLimits}; + let mut l = TLimits::default(); + l.decoding_buffer_size = 8 << 30; + l.intermediate_buffer_size = 1 << 30; + let mut dec = Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| e.to_string())?.with_limits(l); + let (w, h) = dec.dimensions().map_err(|e| e.to_string())?; + let layout = match dec.colortype().map_err(|e| e.to_string())? { + tiff::ColorType::RGB(_) => ChannelLayout::Rgb, + tiff::ColorType::Gray(_) => ChannelLayout::Gray, + c => return Err(format!("{c:?}")), + }; + match dec.read_image().map_err(|e| e.to_string())? { + DecodingResult::U8(v) => Image::from_u8(w, h, layout, v), + DecodingResult::U16(v) => Image::from_u16(w, h, layout, &v), + _ => return Err("sample type".into()), + } + .map_err(|e| e.to_string()) +} + +fn best_of(f: impl Fn() -> Result) -> Result { + let mut best = f64::MAX; + for _ in 0..7 { + let t = Instant::now(); + f()?; + best = best.min(t.elapsed().as_secs_f64() * 1000.0); + } + Ok(best) +} + +fn time(name: &str, bytes: &[u8]) { + let new = best_of(|| decode(bytes).map_err(|e| e.to_string())); + let old = best_of(|| legacy(bytes)); + let show = |r: &Result| match r { + Ok(ms) => format!("{ms:>7.1} ms"), + Err(e) => format!("error ({})", e.chars().take(48).collect::()), + }; + println!("{name:<34} new {} before {} ({:.1} MB file)", show(&new), show(&old), bytes.len() as f64 / 1e6); +} + +#[test] +#[ignore = "benchmark: run in release with --ignored --nocapture"] +fn decode_30mp() { + let rgb = pixels(3); + let img = Image::from_u8(W, H, ChannelLayout::Rgb, rgb.clone()).unwrap(); + for (name, c) in + [("strips, none", TiffCompression::None), ("strips, LZW + predictor", TiffCompression::Lzw), ("strips, Deflate + predictor", TiffCompression::Deflate)] + { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + time(&format!("RGB 8 {name}"), &bytes); + } + let img16 = img.convert(ChannelLayout::Rgb, SampleType::U16); + let bytes = encode(&img16, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::Deflate, ..Default::default() }).unwrap(); + time("RGB 16 strips, Deflate + predictor", &bytes); + let bytes = encode(&img16, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap(); + time("RGB 16 strips, none", &bytes); + + // Hand-built layouts the encoder does not write: one big strip, tiles, planar. + let le = cfg!(target_endian = "little"); + let one = build(le, false, &[strips(W, H, 8, 3, 2, H, &rgb)], &[0]); + time("RGB 8 one strip, none", &one.bytes); + let tiles = tile_split(&rgb, W as usize, H as usize, 3, 1, 256, 256, false); + let mut d = Dir { entries: base(W, H, 8, 3, 2), chunks: tiles, tiled: true }; + d = d.tag(322, LONG, &[256]).tag(323, LONG, &[256]); + time("RGB 8 tiles 256, none", &build(le, false, &[d], &[0]).bytes); + let planes = planar_strips(&rgb, W as usize, H as usize, 3, 1, 64); + let mut d = Dir { entries: base(W, H, 8, 3, 2), chunks: planes, tiled: false }; + d = d.tag(278, LONG, &[64]).tag(284, SHORT, &[2]); + time("RGB 8 planar strips, none", &build(le, false, &[d], &[0]).bytes); + let d = strips(W, H, 8, 3, 2, 64, &rgb); + time("RGB 8 BigTIFF strips, none", &build(le, true, &[d], &[0]).bytes); +} + +/// Save Image Pyramid's cost on top of a plain encode (#8827): RGB/8 and RGBA/16. +#[test] +#[ignore = "benchmark: run in release with --ignored --nocapture"] +fn encode_with_pyramid() { + for (layout, sample, spp) in [(ChannelLayout::Rgb, SampleType::U8, 3), (ChannelLayout::Rgba, SampleType::U16, 4)] { + let raw = pixels(spp); + let data = if sample == SampleType::U16 { raw.iter().flat_map(|&v| (u16::from(v) * 257).to_ne_bytes()).collect() } else { raw }; + let img = Image::from_raw(W, H, layout, sample, data).unwrap(); + for pyramid in [false, true] { + let opts = EncodeOptions { tiff_pyramid: pyramid, ..Default::default() }; + let t = Instant::now(); + let bytes = encode(&img, Format::Tiff, &opts).unwrap(); + println!("{layout:?} {sample:?} pyramid {pyramid}: {:.0} ms, {} MB", t.elapsed().as_secs_f64() * 1000.0, bytes.len() >> 20); + } + } +} diff --git a/crates/image-codecs/tests/it/tiff_exif.rs b/crates/image-codecs/tests/it/tiff_exif.rs new file mode 100644 index 000000000..a1ad5c542 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_exif.rs @@ -0,0 +1,262 @@ +//! EXIF in TIFF: an EXIF block's Exif and GPS directories and IFD0's descriptive tags are +//! written as the TIFF's own directories (IFD0 pointing to them, as cameras and Photoshop write +//! them) and read back into a block. + +use craft_image_codecs::*; + +/// A classic TIFF stream builder for EXIF blocks: directories of (tag, type, count, value bytes). +struct Block { + le: bool, +} + +type Entry = (u16, u16, u32, Vec); + +impl Block { + fn u16(&self, v: u16) -> [u8; 2] { + if self.le { v.to_le_bytes() } else { v.to_be_bytes() } + } + fn u32(&self, v: u32) -> [u8; 4] { + if self.le { v.to_le_bytes() } else { v.to_be_bytes() } + } + fn rational(&self, n: u32, d: u32) -> Vec { + [self.u32(n), self.u32(d)].concat() + } + /// IFD0 (with pointers to `exif` and `gps` appended) followed by the two directories. + fn build(&self, mut ifd0: Vec, exif: Vec, gps: Vec) -> Vec { + let size = |d: &[Entry]| 2 + 12 * d.len() + 4 + d.iter().filter(|e| e.3.len() > 4).map(|e| e.3.len().next_multiple_of(2)).sum::(); + ifd0.push((34665, 4, 1, vec![0; 4])); + ifd0.push((34853, 4, 1, vec![0; 4])); + let at_exif = 8 + size(&ifd0); + let at_gps = at_exif + size(&exif); + let n = ifd0.len(); + ifd0[n - 2].3 = self.u32(at_exif as u32).to_vec(); + ifd0[n - 1].3 = self.u32(at_gps as u32).to_vec(); + let mut out = if self.le { b"II".to_vec() } else { b"MM".to_vec() }; + out.extend(self.u16(42)); + out.extend(self.u32(8)); + for d in [ifd0, exif, gps] { + let start = out.len(); + let mut extra = start + 2 + 12 * d.len() + 4; + let mut values = Vec::new(); + out.extend(self.u16(d.len() as u16)); + for (tag, ty, count, v) in &d { + out.extend(self.u16(*tag)); + out.extend(self.u16(*ty)); + out.extend(self.u32(*count)); + if v.len() <= 4 { + let mut inline = v.clone(); + inline.resize(4, 0); + out.extend(inline); + } else { + out.extend(self.u32(extra as u32)); + values.extend(v); + if v.len() % 2 == 1 { + values.push(0); + } + extra += v.len().next_multiple_of(2); + } + } + out.extend(self.u32(0)); + out.extend(values); + } + out + } +} + +/// The entries (tag, type, count, value bytes in the block's order) of IFD0 and of the +/// directories its tags `34665` and `34853` point to. +fn dirs(b: &[u8]) -> (bool, Vec, Vec, Vec) { + let le = &b[..2] == b"II"; + let u16 = |at: usize| if le { u16::from_le_bytes([b[at], b[at + 1]]) } else { u16::from_be_bytes([b[at], b[at + 1]]) }; + let u32 = |at: usize| { + let s = [b[at], b[at + 1], b[at + 2], b[at + 3]]; + if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) } + }; + let size = |ty: u16| match ty { + 3 | 8 => 2, + 4 | 9 | 11 | 13 => 4, + 5 | 10 | 12 => 8, + _ => 1, + }; + let read = |at: usize| -> Vec { + (0..usize::from(u16(at))) + .map(|i| { + let e = at + 2 + 12 * i; + let (tag, ty, count) = (u16(e), u16(e + 2), u32(e + 4)); + let len = count as usize * size(ty); + let at = if len <= 4 { e + 8 } else { u32(e + 8) as usize }; + (tag, ty, count, b[at..at + len].to_vec()) + }) + .collect() + }; + let ifd0 = read(u32(4) as usize); + let sub = |tag: u16| ifd0.iter().find(|e| e.0 == tag).map(|e| read(u32_of(&e.3, le) as usize)).unwrap_or_default(); + let (exif, gps) = (sub(34665), sub(34853)); + (le, ifd0.into_iter().filter(|e| !matches!(e.0, 34665 | 34853)).collect(), exif, gps) +} + +fn u32_of(v: &[u8], le: bool) -> u32 { + let s = [v[0], v[1], v[2], v[3]]; + if le { u32::from_le_bytes(s) } else { u32::from_be_bytes(s) } +} + +/// The entries of `d` with their values in little-endian order, sorted by tag. +fn normalized(d: &[Entry], le: bool) -> Vec<(u16, u16, u32, Vec)> { + let swap = |ty: u16, v: &[u8]| -> Vec { + let n = match ty { + 3 | 8 => 2, + 4 | 5 | 9 | 10 | 11 | 13 => 4, + 12 => 8, + _ => 1, + }; + if le || n == 1 { v.to_vec() } else { v.chunks(n).flat_map(|c| c.iter().rev().copied()).collect() } + }; + let mut v: Vec<_> = d.iter().map(|e| (e.0, e.1, e.2, swap(e.1, &e.3))).collect(); + v.sort_by_key(|e| e.0); + v +} + +fn camera_block(le: bool) -> Vec { + let b = Block { le }; + let ascii = |s: &str| [s.as_bytes(), b"\0"].concat(); + b.build( + vec![(271, 2, 4, ascii("Cam")), (272, 2, 9, ascii("Model X1")), (315, 2, 16, ascii("A. Photographer")), (274, 3, 1, b.u16(1).to_vec())], + vec![ + (33434, 5, 1, b.rational(1, 125)), + (34855, 3, 1, b.u16(200).to_vec()), + (36867, 2, 20, ascii("2026:10:13 09:30:00")), + (37386, 5, 1, b.rational(50, 1)), + (37500, 7, 6, b"maker!".to_vec()), + ], + vec![(1, 2, 2, ascii("N")), (2, 5, 3, [b.rational(51, 1), b.rational(30, 1), b.rational(0, 1)].concat())], + ) +} + +#[test] +fn exif_round_trips_through_a_tiff_in_either_byte_order() { + for le in [true, false] { + let block = camera_block(le); + let mut img = Image::from_raw(4, 3, ChannelLayout::Rgb, SampleType::U8, vec![90; 36]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(fidelity_warnings(&img, Format::Tiff).iter().all(|w| *w != FidelityWarning::ExifDropped)); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (ble, b0, bexif, bgps) = dirs(&back); + let (ole, o0, oexif, ogps) = dirs(&block); + assert_eq!(normalized(&bexif, ble), normalized(&oexif, ole), "Exif directory (le {le})"); + assert_eq!(normalized(&bgps, ble), normalized(&ogps, ole), "GPS directory (le {le})"); + // IFD0's descriptive tags come along; its orientation is the image's own. + let descriptive: Vec<_> = o0.into_iter().filter(|e| e.0 != 274).collect(); + assert_eq!(normalized(&b0, ble), normalized(&descriptive, ole), "IFD0 (le {le})"); + } +} + +#[test] +fn a_tiff_without_exif_directories_reads_no_exif() { + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.text = vec![("Artist".into(), "A. Photographer".into())]; + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&tiff).unwrap().meta.exif, None); +} + +#[test] +fn no_exif_is_written_without_embed_metadata() { + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(camera_block(true)); + let tiff = encode(&img, Format::Tiff, &EncodeOptions { embed_metadata: false, ..Default::default() }).unwrap(); + assert_eq!(decode(&tiff).unwrap().meta.exif, None); +} + +// --------------------------------------------------------------------------- +// Bounded copying: an entry's value only has to lie inside the file, so a hostile file can have +// many entries point at the same bytes. +// --------------------------------------------------------------------------- + +/// A valid 1x1 gray TIFF whose Exif directory has `entries` UNDEFINED entries of `count` bytes +/// that all point at the same `count` bytes: the file is about `count` bytes long and asks to +/// copy `entries * count`. +fn overlapping_tiff(entries: usize, count: u32) -> Vec { + let entry = |tag: u16, ty: u16, count: u32, value: u32| { + [tag.to_le_bytes().to_vec(), ty.to_le_bytes().to_vec(), count.to_le_bytes().to_vec(), value.to_le_bytes().to_vec()].concat() + }; + let at_exif = 8 + 2 + 12 * 10 + 4; + let data_at = at_exif + 2 + 12 * entries + 4; + let mut out = b"II".to_vec(); + out.extend(42u16.to_le_bytes()); + out.extend(8u32.to_le_bytes()); + out.extend(10u16.to_le_bytes()); + for (tag, ty, value) in + [(256, 3, 1), (257, 3, 1), (258, 3, 8), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, 1), (279, 4, 1), (34665, 4, at_exif as u32)] + { + out.extend(entry(tag, ty, 1, value)); + } + out.extend(0u32.to_le_bytes()); + out.extend((entries as u16).to_le_bytes()); + for i in 0..entries { + out.extend(entry(i as u16 + 1, 7, count, data_at as u32)); + } + out.extend(0u32.to_le_bytes()); + out.extend(vec![0x5A; count as usize]); + out +} + +#[test] +fn overlapping_field_values_are_bounded() { + // About 62 KB on disk; copied entry by entry it would be 1,000 x 50,000 bytes. + let tiff = overlapping_tiff(1000, 50_000); + assert!(tiff.len() < 64 * 1024); + let img = decode(&tiff).expect("the image itself is fine"); + assert_eq!(img.dimensions(), (1, 1)); + let exif = img.meta.exif.expect("the first entries are kept"); + assert!(exif.len() <= 2 * tiff.len(), "{} bytes of EXIF from a {} byte file", exif.len(), tiff.len()); +} + +#[test] +fn overlapping_field_values_in_an_exif_block_are_bounded_on_export() { + // The same block as an image's EXIF: the writer reads it with the same code. + let block = overlapping_tiff(1000, 50_000); + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(tiff.len() <= 2 * block.len(), "a {} byte block wrote a {} byte file", block.len(), tiff.len()); +} + +/// An EXIF block with a maker note of `len` bytes, an exposure time and a GPS latitude reference. +fn block_with_maker_note(le: bool, len: usize) -> Vec { + let b = Block { le }; + b.build( + vec![(271, 2, 4, b"Cam\0".to_vec())], + vec![(33434, 5, 1, b.rational(1, 125)), (37500, 7, len as u32, (0..len).map(|i| (i * 7) as u8).collect())], + vec![(1, 2, 2, b"N\0".to_vec())], + ) +} + +#[test] +fn large_maker_notes_still_survive() { + // Nikon maker notes run from 100 KB to nearly 1 MB. + for len in [200 * 1024, 1000 * 1024] { + let block = block_with_maker_note(true, len); + let mut img = Image::from_raw(4, 3, ChannelLayout::Rgb, SampleType::U8, vec![90; 36]).unwrap(); + img.meta.exif = Some(block.clone()); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (ble, _, bexif, bgps) = dirs(&back); + let (ole, _, oexif, ogps) = dirs(&block); + assert_eq!(normalized(&bexif, ble), normalized(&oexif, ole), "{len} byte maker note"); + assert_eq!(normalized(&bgps, ble), normalized(&ogps, ole)); + } +} + +#[test] +fn a_field_beyond_the_cap_is_dropped_and_the_rest_kept() { + let block = block_with_maker_note(false, 6 * 1024 * 1024); + let mut img = Image::from_raw(2, 2, ChannelLayout::Gray, SampleType::U8, vec![0; 4]).unwrap(); + img.meta.exif = Some(block); + let tiff = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert!(tiff.len() < 1024 * 1024, "{} bytes", tiff.len()); + let back = decode(&tiff).unwrap().meta.exif.expect("EXIF read back"); + let (_, ifd0, exif, gps) = dirs(&back); + assert_eq!(exif.iter().map(|e| e.0).collect::>(), [33434], "the exposure time stays, the 6 MiB maker note goes"); + assert_eq!(gps.iter().map(|e| e.0).collect::>(), [1]); + assert_eq!(ifd0.iter().map(|e| e.0).collect::>(), [271]); +} diff --git a/crates/image-codecs/tests/it/tiff_lab.rs b/crates/image-codecs/tests/it/tiff_lab.rs new file mode 100644 index 000000000..b36435ef4 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_lab.rs @@ -0,0 +1,109 @@ +//! CIE L*a*b* TIFFs: CIELab (PhotometricInterpretation 8) as Photoshop writes it, with a* and b* +//! signed, and ICCLab (9) with them offset, both read into the Lab layout (a* and b* as +//! `(a + 128) / 255`). + +use craft_image_codecs::*; + +/// One Lab pixel in the codec's normalized form. +fn lab(l: f32, a: f32, b: f32) -> [f32; 3] { + [l / 100.0, (a + 128.0) / 255.0, (b + 128.0) / 255.0] +} + +fn image(sample: SampleType) -> Image { + let px: Vec = [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].concat(); + Image::from_normalized(2, 1, ChannelLayout::Lab, sample, &px).unwrap() +} + +fn uncompressed(img: &Image) -> Vec { + encode(img, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::None, ..Default::default() }).unwrap() +} + +fn find(hay: &[u8], needle: &[u8]) -> Option { + hay.windows(needle.len()).position(|w| w == needle) +} + +#[test] +fn eight_bit_cielab_holds_a_and_b_signed() { + let file = uncompressed(&image(SampleType::U8)); + // L* 50 → 128, a* 50, b* −30 as a signed byte; then white with neutral a* and b*. + assert!(find(&file, &[128, 50, (-30i8) as u8, 255, 0, 0]).is_some(), "CIELab samples"); + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U8)); + assert_eq!(back.data(), image(SampleType::U8).data()); +} + +#[test] +fn sixteen_bit_cielab_holds_256_a() { + let file = uncompressed(&image(SampleType::U16)); + let le = &file[..2] == b"II"; + let u16b = |v: u16| if le { v.to_le_bytes() } else { v.to_be_bytes() }; + // a* 50 → 12800, b* −30 → −7680, white's a* and b* 0. + let want: Vec = [32768u16, 12800, (-7680i16) as u16, 65535, 0, 0].iter().flat_map(|v| u16b(*v)).collect(); + assert!(find(&file, &want).is_some(), "CIELab samples"); + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U16)); + let (a, b) = (image(SampleType::U16).to_normalized(), back.to_normalized()); + assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() <= 1.5 / 65535.0), "{a:?} {b:?}"); +} + +#[test] +fn icclab_reads_with_offset_a_and_b() { + // The CIELab file turned into ICCLab: PhotometricInterpretation 9, a* and b* + 128. + let mut file = uncompressed(&image(SampleType::U8)); + let at = find(&file, &[128, 50, (-30i8) as u8, 255, 0, 0]).unwrap(); + for i in [1, 2, 4, 5] { + file[at + i] ^= 0x80; + } + let le = &file[..2] == b"II"; + let entry: Vec = if le { [262u16.to_le_bytes(), 3u16.to_le_bytes()].concat() } else { [262u16.to_be_bytes(), 3u16.to_be_bytes()].concat() }; + let e = find(&file, &entry).expect("PhotometricInterpretation entry"); + let nine = if le { 9u16.to_le_bytes() } else { 9u16.to_be_bytes() }; + file[e + 8..e + 10].copy_from_slice(&nine); + let back = decode(&file).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Lab); + assert_eq!(back.data(), image(SampleType::U8).data()); +} + +#[test] +fn lab_converts_to_rgb_for_other_formats() { + // White and a saturated orange-red; PNG has no Lab, so the encoder plan converts to RGB. + let img = image(SampleType::U8); + let png = decode(&encode(&img, Format::Png, &EncodeOptions::default()).unwrap()).unwrap(); + assert!(png.layout().is_rgb()); + let p = png.to_rgba8(); + assert_eq!(&p[4..7], &[255, 255, 255], "white"); + assert!(p[0] > 180 && p[1] < 120 && p[2] > 140, "L 50, a 50, b −30 is a magenta-ish purple: {:?}", &p[..3]); +} + +#[test] +fn lab_keeps_extra_samples() { + // A spot plate next to Lab: written as CIELab plus one unspecified extra sample, and read + // back as Lab with the spot (not as LabA). + for (layout, sample) in [(ChannelLayout::Lab, SampleType::U8), (ChannelLayout::LabA, SampleType::U16)] { + let px: Vec = + [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].iter().flat_map(|p| p.iter().copied().chain(layout.has_alpha().then_some(0.5))).collect(); + let mut img = Image::from_normalized(2, 1, layout, sample, &px).unwrap(); + let spot: Vec = (0..2 * sample.bytes()).map(|i| (i * 37 + 5) as u8).collect(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: spot.clone() }]; + let file = uncompressed(&img); + if layout == ChannelLayout::Lab { + assert!(find(&file, &[128, 50, (-30i8) as u8, spot[0], 255, 0, 0, spot[1]]).is_some(), "CIELab samples with the spot"); + } + let back = decode(&file).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (layout, sample)); + let (a, b) = (img.to_normalized(), back.to_normalized()); + assert!(a.iter().zip(&b).all(|(x, y)| (x - y).abs() <= 1.5 / 65535.0), "{layout:?}: {a:?} {b:?}"); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: spot }], "{layout:?}"); + } +} + +#[test] +fn float_lab_writes_its_extra_samples_with_16_bits() { + let px: Vec = [lab(50.0, 50.0, -30.0), lab(100.0, 0.0, 0.0)].concat(); + let mut img = Image::from_normalized(2, 1, ChannelLayout::Lab, SampleType::F32, &px).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: [0.25f32, 1.0].iter().flat_map(|v| v.to_ne_bytes()).collect() }]; + let back = decode(&uncompressed(&img)).unwrap(); + assert_eq!((back.layout(), back.sample_type()), (ChannelLayout::Lab, SampleType::U16)); + let spot: Vec = [16384u16, 65535].iter().flat_map(|v| v.to_ne_bytes()).collect(); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: spot }]); +} diff --git a/crates/image-codecs/tests/it/tiff_pages.rs b/crates/image-codecs/tests/it/tiff_pages.rs new file mode 100644 index 000000000..fbe4ffb8b --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_pages.rs @@ -0,0 +1,841 @@ +//! Large-file TIFF support (FILE-215-8): BigTIFF in both byte orders, every IFD of a multi-page +//! file and its SubIFDs, banded decoding of strips and tiles in either planar configuration, the +//! sample formats and interpretations around them, and hostile structures (cycles, counts and +//! offsets past the end, absurd sizes) that must give `Ok` or a clean `Err`, never a panic. +//! Every file here is hand-built by `common::tiffgen`. + +use crate::common; +use common::Rng; +use common::tiffgen::*; +use craft_image_codecs::*; + +const ORDERS: [(bool, bool); 4] = [(true, false), (false, false), (true, true), (false, true)]; + +fn ne16(v: &[u16]) -> Vec { + v.iter().flat_map(|x| x.to_ne_bytes()).collect() +} +fn ne32f(v: &[f32]) -> Vec { + v.iter().flat_map(|x| x.to_ne_bytes()).collect() +} + +/// A test image's samples in each storage: 8-bit, 16-bit and 32-bit float. +struct Truth { + u8s: Vec, + u16s: Vec, + f32s: Vec, +} + +fn truth(n: usize, seed: u64) -> Truth { + let mut r = Rng::new(seed); + let u16s: Vec = (0..n).map(|_| r.next_u64() as u16).collect(); + Truth { u8s: u16s.iter().map(|v| (v >> 8) as u8).collect(), f32s: u16s.iter().map(|&v| f32::from(v) / 65535.0).collect(), u16s } +} + +/// `(bits, sample format, file bytes in this order, expected native bytes, sample type)`. +type Depth = (u16, u16, Vec, Vec, SampleType); + +fn depths(t: &Truth, little: bool) -> Vec { + vec![ + (8, 1, t.u8s.clone(), t.u8s.clone(), SampleType::U8), + (16, 1, u16s(&t.u16s, little), ne16(&t.u16s), SampleType::U16), + (32, 3, f32s(&t.f32s, little), ne32f(&t.f32s), SampleType::F32), + ] +} + +fn decode_ok(bytes: &[u8]) -> Image { + decode(bytes).unwrap_or_else(|e| panic!("decode failed: {e}")) +} + +#[test] +fn bigtiff_and_classic_in_both_byte_orders_at_every_depth() { + let (w, h) = (7u32, 5u32); + for spp in [1u16, 3, 4] { + let t = truth((w * h) as usize * spp as usize, u64::from(spp)); + for (little, big) in ORDERS { + for (bits, format, file, want, sample) in depths(&t, little) { + let photometric = if spp == 1 { 1 } else { 2 }; + let mut d = strips(w, h, bits, spp, photometric, 2, &file).tag(339, SHORT, &vec![u64::from(format); spp as usize]); + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!((img.dimensions(), img.sample_type()), ((w, h), sample), "little {little} big {big} bits {bits} spp {spp}"); + assert_eq!(img.layout().channels(), spp as usize); + assert_eq!(img.data(), &want[..], "little {little} big {big} bits {bits} spp {spp}"); + assert_eq!(img.warnings, []); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.big_tiff, info.little_endian, info.pages.len(), info.complete), (big, little, 1, true)); + } + } + } +} + +#[test] +fn tiles_with_padding_in_both_planar_configurations() { + let (w, h) = (37usize, 29usize); + for spp in [1usize, 3, 4] { + let t = truth(w * h * spp, 9 + spp as u64); + for (little, big) in ORDERS { + for (bits, format, file, want, _) in depths(&t, little) { + let bps = bits as usize / 8; + for planar in [false, true] { + let mut d = Dir { + entries: base(w as u32, h as u32, bits, spp as u16, if spp == 1 { 1 } else { 2 }), + chunks: tile_split(&file, w, h, spp, bps, 16, 16, planar), + tiled: true, + }; + d = d.tag(322, SHORT, &[16]).tag(323, SHORT, &[16]).tag(339, SHORT, &[u64::from(format)]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!(img.data(), &want[..], "tiles: little {little} big {big} bits {bits} spp {spp} planar {planar}"); + let info = tiff_info(&b.bytes).unwrap(); + assert!(info.pages[0].tiled && info.pages[0].planar == planar); + } + } + } + } +} + +#[test] +fn planar_strips_at_every_depth() { + let (w, h) = (11usize, 9usize); + for spp in [3usize, 4] { + let t = truth(w * h * spp, 3); + for (little, big) in ORDERS { + for (bits, format, file, want, _) in depths(&t, little) { + for rows in [1usize, 4, 9] { + let mut d = Dir { + entries: base(w as u32, h as u32, bits, spp as u16, 2), + chunks: planar_strips(&file, w, h, spp, bits as usize / 8, rows), + tiled: false, + }; + d = d.tag(278, LONG, &[rows as u64]).tag(284, SHORT, &[2]).tag(339, SHORT, &[u64::from(format)]); + if spp == 4 { + d = d.tag(338, SHORT, &[2]); + } + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + assert_eq!(img.data(), &want[..], "planar strips: little {little} big {big} bits {bits} spp {spp} rows {rows}"); + } + } + } + } +} + +/// Extra samples that are not the alpha (ExtraSamples 0: a spot colour or an extra channel) are +/// not part of the image: they come back in `meta.extra_samples`, one plane each. +#[test] +fn extra_samples_that_are_not_alpha_are_kept_apart() { + let (w, h) = (5usize, 3usize); + let t = truth(w * h * 5, 4); + let rgb_of = |spp: usize, keep: usize| -> Vec { t.u8s[..w * h * spp].chunks(spp).flat_map(|p| p[..keep].to_vec()).collect() }; + let extra = |spp: usize, at: usize| vec![ExtraSample { kind: 0, data: t.u8s[..w * h * spp].chunks(spp).map(|p| p[at]).collect() }]; + for planar in [false, true] { + // RGB + one unspecified extra sample: RGB, and the sample as an extra plane. + let px = &t.u8s[..w * h * 4]; + let chunks = if planar { planar_strips(px, w, h, 4, 1, 2) } else { px.chunks(w * 4 * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, 8, 4, 2), chunks, tiled: false }.tag(278, LONG, &[2]).tag(338, SHORT, &[0]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(img.data(), &rgb_of(4, 3)[..], "planar {planar}"); + assert_eq!(img.meta.extra_samples, extra(4, 3), "planar {planar}"); + // RGB + alpha + an unspecified extra sample: RGBA and one extra plane. + let px = &t.u8s[..w * h * 5]; + let chunks = if planar { planar_strips(px, w, h, 5, 1, 2) } else { px.chunks(w * 5 * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, 8, 5, 2), chunks, tiled: false }.tag(278, LONG, &[2]).tag(338, SHORT, &[2, 0]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(false, true, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.data(), &rgb_of(5, 4)[..], "planar {planar}"); + assert_eq!(img.meta.extra_samples, extra(5, 4), "planar {planar}"); + } + // Gray + alpha + extra: gray and alpha; gray + an unspecified extra: gray. Each keeps the + // extra sample as a plane. + let px = &t.u8s[..w * h * 3]; + let d = strips(w as u32, h as u32, 8, 3, 1, 3, px).tag(338, SHORT, &[2, 0]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &rgb_of(3, 2)[..]); + assert_eq!(img.meta.extra_samples, extra(3, 2)); + let px = &t.u8s[..w * h * 2]; + let d = strips(w as u32, h as u32, 8, 2, 1, 3, px).tag(338, SHORT, &[0]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::Gray); + assert_eq!(img.data(), &rgb_of(2, 1)[..]); + assert_eq!(img.meta.extra_samples, extra(2, 1)); +} + +/// A file may hold more extra samples than the `MAX_EXTRA_SAMPLES` a decode keeps (up to 64 +/// samples a pixel open): the first ones open, and a warning counts those left out, so nothing +/// is lost without notice. +#[test] +fn extra_samples_past_the_cap_are_reported() { + let (w, h) = (3usize, 2usize); + const EXTRAS: usize = MAX_EXTRA_SAMPLES + 4; + let spp = 3 + EXTRAS; + let px: Vec = (0..w * h * spp).map(|i| (i % spp) as u8).collect(); + let d = strips(w as u32, h as u32, 8, spp as u16, 2, h as u32, &px).tag(338, SHORT, &[0; EXTRAS]); + let bytes = build(true, false, &[d], &[0]).bytes; + let img = decode_ok(&bytes); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(img.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert_eq!(img.meta.extra_samples.last().map(|e| e.data[0]), Some((3 + MAX_EXTRA_SAMPLES - 1) as u8)); + assert_eq!(img.warnings, vec![DecodeWarning::ExtraSamplesOverLimit { format: Format::Tiff, count: 4 }]); + assert!(has_unretained_source_data(&bytes).unwrap()); + let text = img.warnings[0].to_string(); + assert!(text.contains("4 extra TIFF file channels") && text.contains("past the first 16"), "{text}"); + // At the cap exactly, nothing is left out. + let spp = 3 + MAX_EXTRA_SAMPLES; + let px: Vec = (0..w * h * spp).map(|i| (i % spp) as u8).collect(); + let d = strips(w as u32, h as u32, 8, spp as u16, 2, h as u32, &px).tag(338, SHORT, &[0; MAX_EXTRA_SAMPLES]); + let bytes = build(true, false, &[d], &[0]).bytes; + let img = decode_ok(&bytes); + assert_eq!(img.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert!(img.warnings.is_empty(), "{:?}", img.warnings); + assert!(!has_unretained_source_data(&bytes).unwrap()); +} + +/// The guard follows decoder classification, including non-first, repeated or missing alpha tags. +#[test] +fn source_sample_cap_matches_the_decoder_at_layout_and_alpha_boundaries() { + for (photo, color) in [(1u16, 1usize), (2, 3), (5, 4), (8, 3), (9, 3)] { + for extras in [MAX_EXTRA_SAMPLES - 1, MAX_EXTRA_SAMPLES, MAX_EXTRA_SAMPLES + 1] { + for kind in 0..7 { + let alpha = matches!(kind, 1..=3); + let spp = color + extras + usize::from(alpha); + let px: Vec<_> = (0..spp).map(|i| i as u8 + 1).collect(); + let mut kinds = vec![0; spp - color]; + if alpha { + kinds[if kind == 2 { 1 } else { 0 }] = 2; + } + if kind == 3 { + kinds[1] = 1; + } + if kind == 4 { + kinds.push(2); + } + let mut dir = strips(1, 1, 8, spp as u16, photo, 1, &px); + if kind != 5 { + dir = dir.tag(338, SHORT, &kinds); + } + if kind == 6 { + dir = dir.tag(338, SHORT, &vec![2; spp - color]); + } + let bytes = build(true, false, &[dir], &[0]).bytes; + let image = decode(&bytes); + let guarded = has_unretained_source_data(&bytes); + match image { + Ok(image) => { + let dropped = image.warnings.iter().any(|w| matches!(w, DecodeWarning::ExtraSamplesOverLimit { .. })); + assert_eq!(guarded.unwrap(), dropped, "photo={photo} extras={extras} alpha tags={kind}"); + if kind <= 3 { + assert_eq!(dropped, extras > MAX_EXTRA_SAMPLES); + } + } + Err(_) if kind >= 4 => assert!(guarded.is_err(), "unsupported tags remain refused"), + Err(error) => panic!("supported photo={photo} extras={extras} tags={kind}: {error}"), + } + } + } + } +} + +/// Independent compressed zero rows satisfy the decoder's file-size safety bound. Inspection +/// follows import's default budget without allocating or decoding the declared whole image. +#[cfg(target_pointer_width = "64")] +#[test] +fn source_sample_guard_follows_whole_aux_and_banded_plain_budget_routes() { + for (size, bits, extras, guarded) in [(16384u32, 8u16, MAX_EXTRA_SAMPLES, true), (32768, 32, 0, false)] { + let samples = 3 + extras; + let entries = if extras == 0 { 11u16 } else { 12 }; + let bits_at = 8 + 2 + usize::from(entries) * 12 + 4; + let kinds_at = bits_at + samples * 2; + let offsets_at = kinds_at + extras * 2; + let counts_at = offsets_at + size as usize * 4; + let data_at = counts_at + size as usize * 4; + let sample = if bits == 32 { SampleType::F32 } else { SampleType::U8 }; + let mut image = Image::from_raw(size, 1, ChannelLayout::Rgb, sample, vec![0; size as usize * 3 * sample.bytes()]).unwrap(); + image.meta.extra_samples = (0..extras).map(|_| ExtraSample { kind: 0, data: vec![0; size as usize * sample.bytes()] }).collect(); + let encoded = encode(&image, Format::Tiff, &EncodeOptions { tiff_compression: TiffCompression::Deflate, ..Default::default() }).unwrap(); + decode_ok(&encoded); + assert_eq!(&encoded[..2], b"II"); + let first = u32::from_le_bytes(encoded[4..8].try_into().unwrap()) as usize; + let n = u16::from_le_bytes(encoded[first..first + 2].try_into().unwrap()) as usize; + let field = |tag: u16| { + let at = (0..n).map(|i| first + 2 + i * 12).find(|&at| u16::from_le_bytes(encoded[at..at + 2].try_into().unwrap()) == tag).unwrap(); + assert_eq!(u32::from_le_bytes(encoded[at + 4..at + 8].try_into().unwrap()), 1); + u32::from_le_bytes(encoded[at + 8..at + 12].try_into().unwrap()) as usize + }; + let row = &encoded[field(273)..field(273) + field(279)]; + let mut tags = vec![ + (256u16, 4u16, 1u32, size), + (257, 4, 1, size), + (258, 3, samples as u32, bits_at as u32), + (259, 3, 1, field(259) as u32), + (262, 3, 1, 2), + (273, 4, size, offsets_at as u32), + (277, 3, 1, samples as u32), + (278, 4, 1, 1), + (279, 4, size, counts_at as u32), + (284, 3, 1, 1), + (339, 3, 1, if bits == 32 { 3 } else { 1 }), + ]; + if extras > 0 { + tags.push((338, 3, extras as u32, kinds_at as u32)); + } + tags.sort_unstable_by_key(|t| t.0); + let mut bytes = b"II\x2a\0\x08\0\0\0".to_vec(); + bytes.extend_from_slice(&entries.to_le_bytes()); + for (tag, ty, count, value) in tags { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&ty.to_le_bytes()); + bytes.extend_from_slice(&count.to_le_bytes()); + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + for _ in 0..samples { + bytes.extend_from_slice(&bits.to_le_bytes()); + } + for _ in 0..extras { + bytes.extend_from_slice(&0u16.to_le_bytes()); + } + for index in 0..size { + bytes.extend_from_slice(&((data_at + index as usize * row.len()) as u32).to_le_bytes()); + } + for _ in 0..size { + bytes.extend_from_slice(&(row.len() as u32).to_le_bytes()); + } + assert_eq!(bytes.len(), data_at); + for _ in 0..size { + bytes.extend_from_slice(row); + } + assert!(bytes.len() < 32 << 20); + assert_eq!(has_unretained_source_data(&bytes).unwrap(), guarded); + } +} + +#[test] +fn alpha_after_an_unspecified_extra_sample_is_kept() { + // #1349: TIFF 6.0 lets alpha be any extra sample; ExtraSamples [0, 2] puts it second. + let (w, h) = (5usize, 3usize); + for (photometric, color, layout) in [(2u16, 3usize, ChannelLayout::Rgba), (5, 4, ChannelLayout::CmykA), (1, 1, ChannelLayout::GrayA)] { + let spp = color + 2; + let t = truth(w * h * spp, 13 + spp as u64); + for (little, big) in ORDERS { + for (bits, format, file, want_all, sample) in depths(&t, little) { + let bps = bits as usize / 8; + // The colour samples, then the second extra sample (the alpha). + let want: Vec = want_all.chunks(spp * bps).flat_map(|p| [&p[..color * bps], &p[(color + 1) * bps..]].concat()).collect(); + for planar in [false, true] { + let chunks = if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; + let mut d = Dir { entries: base(w as u32, h as u32, bits, spp as u16, photometric), chunks, tiled: false } + .tag(278, LONG, &[2]) + .tag(339, SHORT, &vec![u64::from(format); spp]) + .tag(338, SHORT, &[0, 2]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + let what = format!("photometric {photometric} little {little} big {big} bits {bits} planar {planar}"); + assert_eq!((img.layout(), img.sample_type()), (layout, sample), "{what}"); + assert_eq!(img.data(), &want[..], "{what}"); + assert_eq!(img.warnings, [], "{what}"); + } + } + } + } + // The issue's file: 2x2 RGB, every pixel 200 100 50, then 17 (unspecified), then alpha 128. + let px: Vec = [200u8, 100, 50, 17, 128].repeat(4); + let d = strips(2, 2, 8, 5, 2, 2, &px).tag(338, SHORT, &[0, 2]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.data(), &[200u8, 100, 50, 128].repeat(4)[..]); + // Associated alpha in the same position is made straight. + let px: Vec = [100u8, 50, 25, 17, 128].repeat(4); + let d = strips(2, 2, 8, 5, 2, 2, &px).tag(338, SHORT, &[0, 1]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.data(), &[199u8, 100, 50, 128].repeat(4)[..]); +} + +#[test] +fn associated_alpha_is_made_straight() { + // Premultiplied: half-transparent mid gray is stored as 64 with alpha 128. + let px = [64u8, 128, 200, 255, 0, 0]; + let d = strips(3, 1, 8, 2, 1, 1, &px).tag(338, SHORT, &[1]); + let img = decode_ok(&build(true, false, &[d], &[0]).bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &[128, 128, 200, 255, 0, 0]); + let px16 = u16s(&[16384, 32768], false); + let d = strips(1, 1, 16, 2, 1, 1, &px16).tag(338, SHORT, &[1]); + let img = decode_ok(&build(false, false, &[d], &[0]).bytes); + assert_eq!(img.to_u16_samples().unwrap(), [32768, 32768]); +} + +#[test] +fn white_is_zero_opens_with_the_right_tones() { + // #691: 00 FF 00 in a WhiteIsZero file is white, black, white. + let b = build(true, false, &[strips(3, 1, 8, 1, 0, 1, &[0x00, 0xff, 0x00])], &[0]); + let ours = decode_ok(&b.bytes); + let oracle = image::load_from_memory_with_format(&b.bytes, image::ImageFormat::Tiff).unwrap().into_luma8(); + assert_eq!(oracle.as_raw(), &[255u8, 0, 255]); + assert_eq!(ours.data(), &oracle.as_raw()[..]); + // The BlackIsZero control keeps the stored bytes. + let b = build(true, false, &[strips(3, 1, 8, 1, 1, 1, &[0x00, 0xff, 0x00])], &[0]); + assert_eq!(decode_ok(&b.bytes).data(), &[0, 255, 0]); + // 16-bit, in both byte orders and BigTIFF. + for (little, big) in ORDERS { + let b = build(little, big, &[strips(2, 1, 16, 1, 0, 1, &u16s(&[0, 65535], little))], &[0]); + assert_eq!(decode_ok(&b.bytes).to_u16_samples().unwrap(), [65535, 0]); + } + // 1-bit (a fax-style scan): bit set = black. + let b = build(true, false, &[strips(8, 1, 1, 1, 0, 1, &[0b1010_0000])], &[0]); + assert_eq!(decode_ok(&b.bytes).data(), &[0, 255, 0, 255, 255, 255, 255, 255]); + // With alpha: the gray inverts, the alpha does not. + let b = build(true, false, &[strips(2, 1, 8, 2, 0, 1, &[0, 255, 255, 10]).tag(338, SHORT, &[2])], &[0]); + let img = decode_ok(&b.bytes); + assert_eq!(img.layout(), ChannelLayout::GrayA); + assert_eq!(img.data(), &[255, 255, 0, 10]); + // 32-bit float. + let b = build(true, false, &[strips(2, 1, 32, 1, 0, 1, &f32s(&[0.0, 0.25], true)).tag(339, SHORT, &[3])], &[0]); + assert_eq!(decode_ok(&b.bytes).to_f32_samples().unwrap(), [1.0, 0.75]); +} + +#[test] +fn bigtiff_orientation_is_applied_like_classic_tiff() { + // #693: the same 2×1 gray image with Orientation 6, classic and BigTIFF, both byte orders. + for (little, big) in ORDERS { + let d = strips(2, 1, 8, 1, 1, 1, &[10, 200]).tag(274, SHORT, &[6]); + let b = build(little, big, &[d], &[0]); + assert_eq!(exif_orientation(&b.bytes), 6, "little {little} big {big}"); + assert_eq!(tiff_orientation(&b.bytes), 6); + let img = decode_ok(&b.bytes); + assert_eq!(img.dimensions(), (1, 2), "little {little} big {big}"); + assert_eq!(img.data(), &[10, 200]); + let kept = decode_with(&b.bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + assert_eq!(kept.dimensions(), (2, 1)); + } +} + +#[test] +fn palette_images_open_as_rgb() { + // Two 8-bit entries used: 0 = red, 1 = (16, 32, 48); the map has 256 entries per channel. + let mut map = vec![0u64; 768]; + map[0] = 0xffff; + map[1] = 16 * 257; + map[256 + 1] = 32 * 257; + map[512 + 1] = 48 * 257; + for (little, big) in ORDERS { + let d = strips(3, 1, 8, 1, 3, 1, &[0, 1, 0]).tag(320, SHORT, &map); + let img = decode_ok(&build(little, big, &[d], &[0]).bytes); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U8)); + assert_eq!(img.data(), &[255, 0, 0, 16, 32, 48, 255, 0, 0]); + } + // 4-bit indices. + let mut map = vec![0u64; 48]; + map[15] = 0xffff; + map[16 + 15] = 0x8000; + let d = strips(2, 1, 4, 1, 3, 1, &[0xf0]).tag(320, SHORT, &map); + assert_eq!(decode_ok(&build(true, false, &[d], &[0]).bytes).data(), &[255, 128, 0, 0, 0, 0]); + // A missing or short colour map is an error. + let d = strips(2, 1, 8, 1, 3, 1, &[0, 1]); + assert!(decode(&build(true, false, &[d], &[0]).bytes).is_err()); + let d = strips(2, 1, 8, 1, 3, 1, &[0, 1]).tag(320, SHORT, &[1, 2, 3]); + assert!(decode(&build(true, false, &[d], &[0]).bytes).is_err()); +} + +#[test] +fn wide_and_odd_sample_formats() { + for (little, big) in ORDERS { + let e = |v: u32| if little { v.to_le_bytes() } else { v.to_be_bytes() }; + // 32-bit unsigned keeps its top 16 bits. + let px: Vec = [0x1234_5678u32, 0xffff_0000].iter().flat_map(|&v| e(v)).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 32, 1, 1, 1, &px)], &[0]).bytes); + assert_eq!(img.to_u16_samples().unwrap(), [0x1234, 0xffff]); + // 64-bit float becomes 32-bit float. + let px: Vec = [0.5f64, 2.0].iter().flat_map(|v| if little { v.to_le_bytes() } else { v.to_be_bytes() }).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 64, 1, 1, 1, &px).tag(339, SHORT, &[3])], &[0]).bytes); + assert_eq!(img.to_f32_samples().unwrap(), [0.5, 2.0]); + // 16-bit float stays half. + let px: Vec = [f16::from_f32(0.25), f16::ONE].iter().flat_map(|v| if little { v.to_le_bytes() } else { v.to_be_bytes() }).collect(); + let img = decode_ok(&build(little, big, &[strips(2, 1, 16, 1, 1, 1, &px).tag(339, SHORT, &[3])], &[0]).bytes); + assert_eq!(img.sample_type(), SampleType::F16); + assert_eq!(img.to_normalized(), [0.25, 1.0]); + } + // 2- and 4-bit gray, rows padded to a byte, in several strips. + let img = decode_ok(&build(true, false, &[strips(3, 2, 2, 1, 1, 1, &[0b0001_1011, 0b1110_0100])], &[0]).bytes); + assert_eq!(img.data(), &[0, 85, 170, 255, 170, 85]); + let img = decode_ok(&build(false, true, &[strips(3, 2, 4, 1, 1, 1, &[0x0f, 0x80, 0x12, 0x30])], &[0]).bytes); + assert_eq!(img.data(), &[0, 255, 136, 17, 34, 51]); + // Signed integers are unsupported, not a crash. + let d = strips(2, 1, 16, 1, 1, 1, &[0, 0, 0, 0]).tag(339, SHORT, &[2]); + assert!(matches!(decode(&build(true, false, &[d], &[0]).bytes), Err(CodecError::Unsupported { .. }))); +} + +/// A flat gray page of `w`×`h` at `level`, with an optional NewSubfileType. +fn gray_page(w: u32, h: u32, level: u8, kind: Option) -> Dir { + let d = strips(w, h, 8, 1, 1, h, &vec![level; (w * h) as usize]); + match kind { + Some(k) => d.tag(254, LONG, &[k]), + None => d, + } +} + +#[test] +fn every_ifd_and_subifd_is_listed_and_decodable() { + for (little, big) in ORDERS { + let sub_ty = if big { IFD8 } else { IFD }; + let dirs = vec![ + gray_page(4, 3, 1, Some(1)), // 0: thumbnail first + gray_page(9, 7, 2, None).sub_ifds(sub_ty, &[4, 5]), // 1: page A, two SubIFDs + gray_page(9, 7, 3, Some(4)), // 2: its transparency mask + gray_page(5, 5, 4, Some(2)), // 3: page B (multi-page bit) + gray_page(3, 2, 5, Some(1)), // 4: SubIFD: reduced copy of A + gray_page(2, 1, 6, Some(1)), // 5: SubIFD: smaller copy + ]; + let b = build(little, big, &dirs, &[0, 1, 2, 3]); + let info = tiff_info(&b.bytes).unwrap(); + assert!(info.complete); + let summary: Vec<(u32, Option, TiffPageKind)> = info.pages.iter().map(|p| (p.width, p.parent, p.kind)).collect(); + use TiffPageKind::*; + assert_eq!( + summary, + [(4, None, ReducedResolution), (9, None, Page), (3, Some(1), ReducedResolution), (2, Some(1), ReducedResolution), (9, None, Mask), (5, None, Page)] + ); + assert_eq!(info.pages[1].ifd_offset, b.ifd_at[1]); + assert_eq!((info.default_page(), info.page_count()), (Some(1), 2)); + // Like Photoshop: the first full-resolution page opens; the thumbnail is skipped. + let img = decode_ok(&b.bytes); + assert_eq!((img.dimensions(), img.data()[0]), ((9, 7), 2)); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: Some(2) }]); + // Every directory decodes on request. + for (i, (w, level)) in [(4, 1u8), (9, 2), (3, 5), (2, 6), (9, 3), (5, 4)].into_iter().enumerate() { + let img = decode_tiff_page(&b.bytes, i, &DecodeOptions::default()).unwrap(); + assert_eq!((img.width(), img.data()[0]), (w, level), "page {i} little {little} big {big}"); + } + assert!(matches!(decode_tiff_page(&b.bytes, 6, &DecodeOptions::default()), Err(CodecError::InvalidImage(_)))); + } +} + +#[test] +fn a_file_of_only_thumbnails_still_opens() { + let b = build(true, false, &[gray_page(4, 3, 7, Some(1))], &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().default_page(), Some(0)); + assert_eq!(decode_ok(&b.bytes).data()[0], 7); +} + +#[test] +fn cyclic_chains_and_subifds_terminate() { + for (little, big) in ORDERS { + // IFD1 points back to IFD0. + let mut b = build(little, big, &[gray_page(2, 2, 1, None), gray_page(2, 2, 2, None)], &[0, 1]); + let first = b.ifd_at[0]; + set_next(&mut b, little, big, 1, first); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + let img = decode_ok(&b.bytes); + assert_eq!(img.warnings, [DecodeWarning::MorePages { total: None }]); + // A directory pointing at itself. + let mut b = build(little, big, &[gray_page(2, 2, 1, None)], &[0]); + let first = b.ifd_at[0]; + set_next(&mut b, little, big, 0, first); + assert_eq!(tiff_info(&b.bytes).unwrap().pages.len(), 1); + assert_eq!(decode_ok(&b.bytes).warnings, []); + // A SubIFD listing its own parent, and one listing itself. + let sub_ty = if big { IFD8 } else { IFD }; + let b = build(little, big, &[gray_page(2, 2, 1, None).sub_ifds(sub_ty, &[1]), gray_page(1, 1, 2, Some(1)).sub_ifds(sub_ty, &[0, 1])], &[0]); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + decode_ok(&b.bytes); + // A next pointer past the end, or into the header. + for to in [b.bytes.len() as u64 + 100, 3, u64::MAX] { + let mut b = build(little, big, &[gray_page(2, 2, 1, None), gray_page(2, 2, 2, None)], &[0, 1]); + set_next(&mut b, little, big, 1, to); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (2, false)); + decode_ok(&b.bytes); + } + } +} + +#[test] +fn the_chain_walk_is_capped() { + // 10 001 tiny directories: the walk stops at 10 000 and says the list is incomplete. + let dirs: Vec = (0..10_001).map(|_| gray_page(1, 1, 9, None)).collect(); + let chain: Vec = (0..dirs.len()).collect(); + let b = build(true, false, &dirs, &chain); + let info = tiff_info(&b.bytes).unwrap(); + assert_eq!((info.pages.len(), info.complete), (10_000, false)); + assert_eq!(decode_ok(&b.bytes).warnings, [DecodeWarning::MorePages { total: None }]); +} + +/// A partial child walk remains usable for decoding, but cannot authorize destructive saves. +#[test] +fn incomplete_subifd_lists_and_depth_are_reported() { + for (little, big) in ORDERS { + let ty = if big { IFD8 } else { IFD }; + for depth in [4usize, 5] { + let dirs: Vec<_> = (0..=depth) + .map(|i| { + let dir = gray_page( + 2, + 2, + i as u8, + Some(if i == 0 { + 0 + } else if i == depth { + 4 + } else { + 1 + }), + ); + if i < depth { dir.sub_ifds(ty, &[i + 1]) } else { dir } + }) + .collect(); + let b = build(little, big, &dirs, &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().complete, depth == 4); + decode_ok(&b.bytes); + if depth == 4 { + assert!(has_unretained_source_data(&b.bytes).unwrap()); + } else { + assert!(has_unretained_source_data(&b.bytes).is_err()); + } + } + for count in [0usize, 1, 256, 257] { + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &vec![0; count])], &[0]); + assert_eq!(tiff_info(&b.bytes).unwrap().complete, count <= 256, "null child slots {count}"); + decode_ok(&b.bytes); + } + for invalid in [SHORT, u16::MAX] { + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, invalid, &[0])], &[0]); + assert!(!tiff_info(&b.bytes).unwrap().complete); + assert!(has_unretained_source_data(&b.bytes).is_err()); + decode_ok(&b.bytes); + } + let b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &[0]).tag(330, ty, &[0])], &[0]); + assert!(!tiff_info(&b.bytes).unwrap().complete, "duplicate child declarations"); + let mut b = build(little, big, &[gray_page(2, 2, 9, None).tag(330, ty, &[0, 0])], &[0]); + let (entry, ..) = find_entry(&b, little, big, 0, 330).unwrap(); + let offset = b.bytes.len() as u64 - 1; + let value = if little { offset.to_le_bytes() } else { offset.to_be_bytes() }; + let (field, width) = if big { (entry + 12, 8) } else { (entry + 8, 4) }; + b.bytes[field..field + width].copy_from_slice(if little { &value[..width] } else { &value[8 - width..] }); + assert!(!tiff_info(&b.bytes).unwrap().complete, "truncated child list"); + assert!(has_unretained_source_data(&b.bytes).is_err()); + decode_ok(&b.bytes); + } +} + +#[test] +fn image_data_cut_off_decodes_with_a_warning() { + let (w, h) = (4u32, 8u32); + let px: Vec = (0..32).collect(); + for (little, big) in ORDERS { + // The last strip starts past the end of the file: its rows stay empty. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + patch_value(&mut b, little, big, 0, 273, 3, len + 1000); + let img = decode_ok(&b.bytes); + assert_eq!(img.warnings, [DecodeWarning::Truncated { format: Format::Tiff }]); + assert_eq!(&img.data()[..24], &px[..24]); + assert_eq!(&img.data()[24..], &[0; 8]); + // A strip running into the end of the file. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + patch_value(&mut b, little, big, 0, 273, 1, len - 3); + assert_eq!(decode_ok(&b.bytes).warnings, [DecodeWarning::Truncated { format: Format::Tiff }]); + // Every strip missing: nothing to show, an error. + let mut b = build(little, big, &[strips(w, h, 8, 1, 1, 2, &px)], &[0]); + let len = b.bytes.len() as u64; + for i in 0..4 { + patch_value(&mut b, little, big, 0, 273, i, len + 7); + } + assert!(decode(&b.bytes).is_err()); + } +} + +#[test] +fn hostile_sizes_and_counts_are_refused_without_allocating() { + let tight = DecodeOptions { limits: Limits { max_alloc: 64 << 20, ..Limits::default() }, ..Default::default() }; + for (little, big) in ORDERS { + let px = vec![7u8; 16]; + let fresh = || build(little, big, &[strips(4, 4, 8, 1, 1, 1, &px)], &[0]); + // Huge declared dimensions: over the limits, or far more than the file can hold. + for (wv, hv) in [(1u64 << 31, 4u64), (4, 1 << 31), (u32::MAX as u64, u32::MAX as u64), (200_000, 200_000), (30_000, 30_000)] { + let mut b = fresh(); + patch_value(&mut b, little, big, 0, 256, 0, wv); + patch_value(&mut b, little, big, 0, 257, 0, hv); + assert!(decode(&b.bytes).is_err(), "{wv}x{hv}"); + assert!(decode_with(&b.bytes, &DecodeOptions { limits: Limits::none(), ..Default::default() }).is_err(), "{wv}x{hv} without limits"); + } + // A strip-offset count far past the end of the file. + let mut b = fresh(); + patch_count(&mut b, little, big, 0, 273, 1 << 30); + assert!(matches!(decode(&b.bytes), Err(CodecError::Malformed { .. }))); + let mut b = fresh(); + patch_count(&mut b, little, big, 0, 258, u32::MAX as u64); + assert!(decode(&b.bytes).is_err()); + // Zero rows per strip, zero samples, absurd samples per pixel. + for (tag, v) in [(278u16, 0u64), (277, 0), (277, 60_000)] { + let mut b = fresh(); + patch_value(&mut b, little, big, 0, tag, 0, v); + assert!(decode(&b.bytes).is_err(), "tag {tag} = {v}"); + } + // Absurd tiles over a tiny image, chunky and planar. + for planar in [false, true] { + for (tw, th) in [(1u64 << 30, 16u64), (16, 1 << 30), (u32::MAX as u64, u32::MAX as u64)] { + let spp = if planar { 3 } else { 1 }; + let mut d = Dir { entries: base(4, 4, 8, spp, if planar { 2 } else { 1 }), chunks: vec![vec![1u8; 64]; spp as usize], tiled: true }; + d = d.tag(322, LONG, &[tw]).tag(323, LONG, &[th]); + if planar { + d = d.tag(284, SHORT, &[2]); + } + let b = build(little, big, &[d], &[0]); + let _ = decode_with(&b.bytes, &tight); + let _ = decode(&b.bytes); + } + } + } + // A BigTIFF directory claiming 2^60 entries. + let mut b = build(true, true, &[gray_page(2, 2, 1, None)], &[0]); + let at = b.ifd_at[0] as usize; + b.bytes[at..at + 8].copy_from_slice(&(1u64 << 60).to_le_bytes()); + assert!(tiff_info(&b.bytes).is_err()); + assert!(decode(&b.bytes).is_err()); +} + +#[test] +fn banded_decode_matches_the_encoder_on_larger_images() { + // Many strips decoded in parallel bands, at every depth and compression. + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Gray, SampleType::F32), + (ChannelLayout::Cmyk, SampleType::U8), + ] { + let img = common::synth(613, 411, layout, sample, 3, 0.2); + for c in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + let back = decode_ok(&bytes); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?} {c:?}"); + } + } + // Tiles across many bands. + let t = truth(300 * 260 * 3, 77); + let d = Dir { entries: base(300, 260, 16, 3, 2), chunks: tile_split(&u16s(&t.u16s, false), 300, 260, 3, 2, 64, 32, false), tiled: true } + .tag(322, SHORT, &[64]) + .tag(323, SHORT, &[32]); + assert_eq!(decode_ok(&build(false, true, &[d], &[0]).bytes).data(), &ne16(&t.u16s)[..]); +} + +#[test] +fn bigtiff_is_written_on_request_and_reads_back() { + let mut img = common::synth(23, 17, ChannelLayout::Rgba, SampleType::U16, 2, 0.3); + img.meta.photoshop_layers = Some(b"Adobe Photoshop Document Data Block\0MIB8ryaL".to_vec()); + img.meta.photoshop_resources = Some(b"8BIM\x04\x04\0\0\0\0\0\0".to_vec()); + for c in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw] { + let classic = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); + let big = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, tiff_bigtiff: true, ..Default::default() }).unwrap(); + let version = |b: &[u8]| if b.starts_with(b"II") { u16::from_le_bytes([b[2], b[3]]) } else { u16::from_be_bytes([b[2], b[3]]) }; + assert_eq!((version(&classic), version(&big)), (42, 43)); + assert!(tiff_info(&big).unwrap().big_tiff); + let back = decode_ok(&big); + assert_eq!(back.data(), img.data(), "{c:?}"); + assert_eq!(back.meta.photoshop_layers, img.meta.photoshop_layers); + assert_eq!(back.meta.photoshop_resources, img.meta.photoshop_resources); + assert_eq!(tiff_photoshop_tags(&big), (img.meta.photoshop_resources.as_deref(), img.meta.photoshop_layers.as_deref())); + } +} + +/// Small files covering every structure: multi-IFD, SubIFDs, BigTIFF both orders, tiles, planar. +fn zoo() -> Vec> { + let mut v = Vec::new(); + let t = truth(6 * 5 * 4, 5); + for (little, big) in ORDERS { + let sub_ty = if big { IFD8 } else { IFD }; + let tiles = Dir { entries: base(6, 5, 8, 3, 2), chunks: tile_split(&t.u8s, 6, 5, 3, 1, 16, 16, true), tiled: true } + .tag(322, SHORT, &[16]) + .tag(323, SHORT, &[16]) + .tag(284, SHORT, &[2]); + let planar = Dir { entries: base(6, 5, 16, 4, 2), chunks: planar_strips(&u16s(&t.u16s, little), 6, 5, 4, 2, 2), tiled: false } + .tag(278, SHORT, &[2]) + .tag(284, SHORT, &[2]) + .tag(338, SHORT, &[1]); + let dirs = vec![gray_page(3, 2, 1, Some(1)), tiles.sub_ifds(sub_ty, &[3]), planar, gray_page(2, 2, 4, Some(1)).tag(274, SHORT, &[6])]; + v.push(build(little, big, &dirs, &[0, 1, 2]).bytes); + v.push(build(little, big, &[strips(5, 3, 1, 1, 0, 2, &[0xa5; 3])], &[0]).bytes); + } + v +} + +fn poke(bytes: &[u8]) { + let _ = decode(bytes); + let _ = decode_with(bytes, &DecodeOptions { limits: Limits { max_alloc: 16 << 20, ..Limits::default() }, ..Default::default() }); + let _ = exif_orientation(bytes); + let _ = tiff_orientation(bytes); + if let Ok(info) = tiff_info(bytes) { + for i in 0..info.pages.len().min(8) { + let _ = decode_tiff_page(bytes, i, &DecodeOptions::default()); + } + } +} + +#[test] +fn zoo_decodes_and_never_panics_when_cut_or_corrupted() { + for f in zoo() { + let info = tiff_info(&f).unwrap(); + for i in 0..info.pages.len() { + decode_tiff_page(&f, i, &DecodeOptions::default()).unwrap(); + } + for cut in 0..f.len() { + poke(&f[..cut]); + } + for i in 0..f.len() { + for flip in [0xffu8, 0x80, 0x01] { + let mut g = f.clone(); + g[i] ^= flip; + poke(&g); + } + } + let mut r = Rng::new(f.len() as u64); + for _ in 0..300 { + let mut g = f.clone(); + for _ in 0..4 { + let i = (r.next_u64() as usize) % g.len(); + g[i] = r.next_u64() as u8; + } + poke(&g); + } + } +} + +/// A longer random-mutation run over the zoo (ignored by default; `-- --ignored`, with +/// `TIFF_FUZZ_ITERS` to change the count). +#[test] +#[ignore = "long fuzz run"] +fn zoo_long_random_mutations() { + let iters: usize = std::env::var("TIFF_FUZZ_ITERS").ok().and_then(|v| v.parse().ok()).unwrap_or(20_000); + let mut r = Rng::new(0x5eed); + let zoo = zoo(); + for k in 0..iters { + let mut g = zoo[k % zoo.len()].clone(); + for _ in 0..1 + r.next_u64() % 8 { + let i = (r.next_u64() as usize) % g.len(); + g[i] = match r.next_u64() % 4 { + 0 => 0, + 1 => 0xff, + _ => r.next_u64() as u8, + }; + } + poke(&g); + } +} diff --git a/crates/image-codecs/tests/it/tiff_photoshop.rs b/crates/image-codecs/tests/it/tiff_photoshop.rs new file mode 100644 index 000000000..698a4296d --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_photoshop.rs @@ -0,0 +1,436 @@ +//! Photoshop's private TIFF tags (34377 image resources, 37724 ImageSourceData) travel through +//! `Metadata` as opaque bytes: written when present, read from either byte order and from +//! BigTIFF, and never a panic or an allocation on malformed input. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn with_tags(layout: ChannelLayout, sample: SampleType) -> Image { + let mut img = synth(9, 7, layout, sample, 5, 0.3); + img.meta.photoshop_resources = Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); + let mut layers = b"Adobe Photoshop Document Data Block\0".to_vec(); + layers.extend_from_slice(b"MIB8ryaL"); + layers.extend_from_slice(&4u32.to_le_bytes()); + layers.extend_from_slice(&[0, 0, 0, 0]); + img.meta.photoshop_layers = Some(layers); + img +} + +#[test] +fn tags_round_trip_through_the_encoder() { + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U16), + (ChannelLayout::Gray, SampleType::F32), + (ChannelLayout::CmykA, SampleType::U8), + ] { + for compression in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw, TiffCompression::PackBits] { + let img = with_tags(layout, sample); + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: compression, ..Default::default() }).unwrap(); + // The encoder writes the running target's byte order. + assert_eq!(bytes.starts_with(b"II"), tiff_writes_little_endian()); + let (res, layers) = tiff_photoshop_tags(&bytes); + assert_eq!(res, img.meta.photoshop_resources.as_deref()); + assert_eq!(layers, img.meta.photoshop_layers.as_deref()); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.photoshop_resources, img.meta.photoshop_resources, "{layout:?} {sample:?} {compression:?}"); + assert_eq!(back.meta.photoshop_layers, img.meta.photoshop_layers); + assert_eq!(back.data(), img.data()); + } + } +} + +#[test] +fn extra_samples_round_trip() { + for (layout, sample) in [ + (ChannelLayout::Rgb, SampleType::U8), + (ChannelLayout::Rgba, SampleType::U8), + (ChannelLayout::Gray, SampleType::U16), + (ChannelLayout::Cmyk, SampleType::F32), + ] { + let mut img = synth(4, 3, layout, sample, 2, 0.4); + let n = 4 * 3; + let b = sample.bytes(); + let data: Vec = (0..n * b).map(|i| (i * 17) as u8).collect(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: data.clone() }]; + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {sample:?}"); + assert_eq!(back.data(), img.data(), "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples.len(), 1, "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples[0].kind, 0, "{layout:?} {sample:?}"); + assert_eq!(back.meta.extra_samples[0].data, data, "{layout:?} {sample:?}"); + } +} + +/// Tall enough that the default strip size (about 1 MB) cannot hold every row, so a writer +/// that only fills the first strip, or predicts across the wrong sample count, cannot hide. +/// The spot plane is checked with the `tiff` crate, not PhotoCraft's decoder. +#[test] +fn multi_strip_spot_round_trip_and_dump() { + let w = 800u32; + let h = 400u32; + let n = (w as usize) * (h as usize); + for (layout, channels) in [(ChannelLayout::Rgb, 3usize), (ChannelLayout::Rgba, 4usize)] { + let mut px = vec![0u8; n * channels]; + let mut spot = vec![0u8; n]; + for y in 0..h as usize { + for x in 0..w as usize { + let i = y * w as usize + x; + px[i * channels] = (x % 251) as u8; + px[i * channels + 1] = (y % 251) as u8; + px[i * channels + 2] = 40; + if channels == 4 { + px[i * channels + 3] = if x > 20 && x < 780 && y > 10 && y + 10 < h as usize { 255 } else { 0 }; + } + spot[i] = ((x * 3 + y * 17) % 251) as u8; + } + } + let mut img = Image::from_u8(w, h, layout, px).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: spot.clone() }]; + for compression in [TiffCompression::None, TiffCompression::Deflate, TiffCompression::Lzw, TiffCompression::PackBits] { + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: compression, ..Default::default() }).unwrap(); + let label = format!("{layout:?} {compression:?}"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.extra_samples.len(), 1, "{label}"); + assert_eq!(back.meta.extra_samples[0].data, spot, "own decoder {label}"); + assert_eq!(back.data(), img.data(), "own decoder rgb {label}"); + let mut dec = tiff::decoder::Decoder::new(std::io::Cursor::new(bytes.as_slice())).unwrap(); + let spp: u16 = dec.get_tag_unsigned(tiff::tags::Tag::SamplesPerPixel).unwrap(); + let bps = dec.get_tag_u16_vec(tiff::tags::Tag::BitsPerSample).unwrap(); + let extra = dec.get_tag_u16_vec(tiff::tags::Tag::ExtraSamples).unwrap(); + let rows = dec.find_tag_unsigned::(tiff::tags::Tag::RowsPerStrip).unwrap(); + assert_eq!(bps.len(), spp as usize, "{label}"); + assert_eq!(spp as usize, channels + 1, "{label}"); + assert_eq!(*extra.last().unwrap(), 0, "{label} spot ExtraSamples {extra:?}"); + assert!(rows.is_some_and(|r| r < h) || compression == TiffCompression::PackBits, "{label} rows {rows:?}"); + let patched = min_is_black(&bytes); + let mut indep = tiff::decoder::Decoder::new(std::io::Cursor::new(patched.as_slice())).unwrap(); + let tiff::decoder::DecodingResult::U8(samples) = indep.read_image().unwrap() else { panic!("{label} not u8") }; + let spp = spp as usize; + assert_eq!(samples.len(), n * spp, "{label}"); + let mut bad = 0usize; + let mut first = None; + for (i, want) in spot.iter().enumerate() { + let got = samples.get(i * spp + spp - 1).copied(); + if got != Some(*want) { + bad += 1; + first.get_or_insert((i, got)); + } + } + assert_eq!(bad, 0, "{label} independent reader, first {first:?}"); + } + } +} + +/// Retag photometric interpretation 262 as MinIsBlack so `tiff`'s decoder returns every sample. +fn min_is_black(src: &[u8]) -> Vec { + let mut bytes = src.to_vec(); + let le = bytes.first().copied() == Some(b'I'); + let ru16 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 2)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let ru32 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 4)?; + Some(if le { u32::from_le_bytes(s.try_into().ok()?) } else { u32::from_be_bytes(s.try_into().ok()?) }) + }; + let Some(ifd) = ru32(&bytes, 4) else { return bytes }; + let Some(n) = ru16(&bytes, ifd as usize) else { return bytes }; + for e in 0..n as usize { + let at = ifd as usize + 2 + e * 12; + if ru16(&bytes, at) == Some(262) { + let value = if le { 1u16.to_le_bytes() } else { 1u16.to_be_bytes() }; + if let Some(slot) = bytes.get_mut(at + 8..at + 10) { + slot.copy_from_slice(&value); + } + break; + } + } + bytes +} + +#[test] +fn more_than_sixteen_extra_samples_is_refused() { + let mut img = synth(2, 2, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES + 1).map(|_| ExtraSample { kind: 0, data: vec![0; 4] }).collect(); + assert!(encode(&img, Format::Tiff, &EncodeOptions::default()).is_err()); +} + +#[test] +fn sixteen_extra_samples_round_trip() { + for (layout, sample, base) in [ + (ChannelLayout::Rgb, SampleType::U8, 3usize), + (ChannelLayout::Rgba, SampleType::U8, 4), + (ChannelLayout::Gray, SampleType::U16, 1), + (ChannelLayout::Cmyk, SampleType::F32, 4), + ] { + let mut img = synth(2, 2, layout, sample, 1, 0.0); + let n = 2 * 2 * sample.bytes(); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES).map(|k| ExtraSample { kind: 0, data: vec![k as u8; n] }).collect(); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), layout, "{layout:?} {sample:?} base {base}"); + assert_eq!(back.data(), img.data(), "{layout:?}"); + assert_eq!(back.meta.extra_samples.len(), MAX_EXTRA_SAMPLES, "{layout:?}"); + assert_eq!(back.meta.extra_samples[7].kind, 0, "{layout:?}"); + assert_eq!(back.meta.extra_samples[15].data, vec![15; n], "{layout:?}"); + } +} + +/// An extra plane holds exactly one sample per pixel of the image's sample type. A plane of any +/// other length is refused rather than written: the writer copies one sample per pixel, so a +/// longer plane would lose its tail and a shorter one would run out mid-image. +#[test] +fn wrong_sized_extra_planes_are_refused() { + let (w, h) = (2u32, 1u32); + for (label, plane) in [("too long", vec![10u8, 20, 30]), ("too short", vec![10u8]), ("empty", Vec::new())] { + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: plane.clone() }]; + let err = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap_err(); + assert!(matches!(err, CodecError::Encode { .. }), "{label}: {err}"); + assert!(err.to_string().contains("extra sample 0") && err.to_string().contains("exactly 2"), "{label}: {err}"); + } + // The exact length still writes, and comes back unchanged. + let plane = vec![10u8, 20]; + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: plane.clone() }]; + let back = decode(&encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap()).unwrap(); + assert_eq!(back.meta.extra_samples, vec![ExtraSample { kind: 0, data: plane }]); +} + +/// The sample-type conversion an encode performs must not launder a wrong-sized plane: float Lab +/// is stored as CIELab at 16 bits, and `Image::convert` leaves a plane whose length does not match +/// the image alone, so a 7- or 9-byte plane for two pixels would otherwise reach the writer +/// reinterpreted at the new width. +#[test] +fn a_wrong_sized_plane_is_refused_before_a_sample_type_conversion() { + let (w, h) = (2u32, 1u32); + // Two pixels at 32-bit float are 8 bytes; the file's 16-bit samples are 4. + for (label, len) in [("short", 7usize), ("long", 9)] { + let mut img = synth(w, h, ChannelLayout::Lab, SampleType::F32, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![0; len] }]; + let err = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap_err(); + assert!(matches!(err, CodecError::Encode { .. }), "{label}: {err}"); + assert!(err.to_string().contains("exactly 8"), "{label}: {err}"); + } +} + +/// Extra samples count against `max_alloc` with the image (their interleaved buffer and the +/// planes split from it): a budget that holds the RGB image but not sixteen extra planes opens +/// the image without them, with a warning, instead of allocating past it. +#[test] +fn extra_samples_count_against_the_allocation_limit() { + // Big enough for several strips (about 1 MB each): one strip of all 19 samples fits the budget + // of the image alone, as in real files. + let (w, h) = (1024u32, 1024u32); + let n = (w * h) as usize; + let mut img = synth(w, h, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = (0..MAX_EXTRA_SAMPLES).map(|k| ExtraSample { kind: 0, data: vec![k as u8; n] }).collect(); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + let with = |max_alloc: u64| decode_with(&bytes, &DecodeOptions { limits: Limits { max_alloc, ..Limits::default() }, ..DecodeOptions::default() }); + // RGB is 3 bytes a pixel; the extra samples add 2 x 16 more. + let image_only = 3 * n as u64; + let with_extras = (3 + 2 * MAX_EXTRA_SAMPLES as u64) * n as u64; + let back = with(with_extras).expect("everything fits"); + assert_eq!(back.meta.extra_samples.len(), MAX_EXTRA_SAMPLES); + assert!(back.warnings.is_empty()); + // The image fits, its extra samples don't: it opens as before they were read, with a warning. + for max_alloc in [image_only, with_extras - 1] { + let only = with(max_alloc).expect("the image alone fits"); + assert!(only.meta.extra_samples.is_empty()); + assert_eq!(only.warnings, vec![DecodeWarning::ExtraSamplesDropped { format: Format::Tiff, count: MAX_EXTRA_SAMPLES as u32 }]); + assert_eq!(only.data(), back.data()); + } + assert!(matches!(with(image_only - 1), Err(CodecError::LimitExceeded(_))), "the image itself must still fit"); +} + +/// The value field of a little-endian classic TIFF's ExtraSamples entry (tag 338, SHORT) holding +/// two values, so they sit in the entry itself. +fn extra_samples_values(bytes: &mut [u8]) -> &mut [u8] { + assert_eq!(&bytes[..4], b"II*\0"); + let entry = [0x52, 0x01, 3, 0, 2, 0, 0, 0]; + let at = bytes.windows(8).position(|w| w == entry).expect("an ExtraSamples entry with two values"); + &mut bytes[at + 8..at + 12] +} + +/// ExtraSamples values above 2 are reserved (TIFF 6.0, p. 31): they read as 0 (unspecified) and +/// are written as 0, so a save never carries a reserved value. +#[test] +fn reserved_extra_sample_kinds_read_as_unspecified() { + let n = 4 * 4; + let mut img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![7; n] }, ExtraSample { kind: 0, data: vec![9; n] }]; + let mut bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + // A file from another writer with reserved values 3 and 65535. + let values = extra_samples_values(&mut bytes); + assert_eq!(values, [0, 0, 0, 0]); + values.copy_from_slice(&[3, 0, 0xFF, 0xFF]); + let back = decode(&bytes).unwrap(); + assert_eq!(back.meta.extra_samples.iter().map(|e| e.kind).collect::>(), [0, 0]); + assert_eq!(back.layout(), ChannelLayout::Rgb, "a reserved value is no alpha"); + assert_eq!(back.meta.extra_samples[0].data, vec![7; n]); + assert_eq!(back.meta.extra_samples[1].data, vec![9; n]); + + // An image that carries a reserved kind saves it as 0. + img.meta.extra_samples[0].kind = 3; + img.meta.extra_samples[1].kind = u16::MAX; + let mut bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(extra_samples_values(&mut bytes), [0, 0, 0, 0]); +} + +/// Converting an image's sample type converts its extra samples with it, so they keep matching +/// the image: a 16-bit float TIFF's spot planes survive the 32-bit float a layered import uses. +#[test] +fn extra_samples_follow_a_sample_type_conversion() { + let (w, h) = (3u32, 2u32); + let n = (w * h) as usize; + let values = [0.0f32, 0.25, 0.5, 0.75, 1.0, 0.125]; + let half = |v: f32| f16::from_f32(v).to_bits().to_ne_bytes(); + let rgb: Vec = (0..n * 3).flat_map(|_| half(0.5)).collect(); + let mut img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::F16, rgb).unwrap(); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: values.iter().flat_map(|v| half(*v)).collect() }]; + + let wide = img.convert(ChannelLayout::Rgb, SampleType::F32); + let plane: Vec = wide.meta.extra_samples[0].data.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes(*c)).collect(); + assert_eq!(plane, values); + + // To 8 bits, the plane quantizes exactly as a gray image of the same values does. + let narrow = wide.convert(ChannelLayout::Rgb, SampleType::U8); + let gray = Image::from_raw(w, h, ChannelLayout::Gray, SampleType::F32, wide.meta.extra_samples[0].data.clone()).unwrap(); + assert_eq!(narrow.meta.extra_samples[0].data, gray.convert(ChannelLayout::Gray, SampleType::U8).data()); + assert_eq!(narrow.meta.extra_samples[0].data.len(), n); +} + +#[test] +fn absent_and_empty_tags_are_not_written() { + let mut img = synth(4, 4, ChannelLayout::Rgb, SampleType::U8, 1, 0.0); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(tiff_photoshop_tags(&bytes), (None, None)); + assert!(decode(&bytes).unwrap().meta.photoshop_layers.is_none()); + img.meta.photoshop_layers = Some(Vec::new()); + img.meta.photoshop_resources = Some(Vec::new()); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap(); + assert_eq!(tiff_photoshop_tags(&bytes), (None, None)); +} + +/// A minimal uncompressed 2×1 gray TIFF with a 37724 tag, in either byte order, classic or +/// BigTIFF: `payload` is stored inline when it fits the value field, at an offset otherwise. +fn hand_made(little: bool, big: bool, payload: &[u8]) -> Vec { + let w16 = |v: u16| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let w32 = |v: u32| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let w64 = |v: u64| if little { v.to_le_bytes().to_vec() } else { v.to_be_bytes().to_vec() }; + let mut f = Vec::new(); + f.extend_from_slice(if little { b"II" } else { b"MM" }); + let header_len = if big { 16 } else { 8 }; + let pixels_at = header_len; + let ifd_at = pixels_at + 2; + f.extend(w16(if big { 43 } else { 42 })); + if big { + f.extend(w16(8)); + f.extend(w16(0)); + f.extend(w64(ifd_at as u64)); + } else { + f.extend(w32(ifd_at as u32)); + } + f.extend_from_slice(&[0x40, 0xc0]); + // Entries: width, length, bits, compression, photometric, strip offsets, samples, rows per + // strip, strip byte counts, 37724. + let tags: [(u16, u16, u64, u64); 10] = [ + (256, 3, 1, 2), + (257, 3, 1, 1), + (258, 3, 1, 8), + (259, 3, 1, 1), + (262, 3, 1, 1), + (273, 4, 1, pixels_at as u64), + (277, 3, 1, 1), + (278, 3, 1, 1), + (279, 4, 1, 2), + (37724, 7, payload.len() as u64, 0), + ]; + let entry_len = if big { 20 } else { 12 }; + let inline = if big { 8 } else { 4 }; + let count_len = if big { 8 } else { 2 }; + let next_len = if big { 8 } else { 4 }; + let data_at = ifd_at + count_len + tags.len() * entry_len + next_len; + let mut entries = Vec::new(); + for (tag, ty, count, value) in tags { + entries.extend(w16(tag)); + entries.extend(w16(ty)); + entries.extend(if big { w64(count) } else { w32(count as u32) }); + let mut v = Vec::new(); + if tag == 37724 { + if payload.len() <= inline { + v.extend_from_slice(payload); + } else { + v.extend(if big { w64(data_at as u64) } else { w32(data_at as u32) }); + } + } else if ty == 3 { + v.extend(w16(value as u16)); + } else { + v.extend(if big { w64(value) } else { w32(value as u32) }); + } + v.resize(inline, 0); + entries.extend(v); + } + f.extend(if big { w64(tags.len() as u64) } else { w16(tags.len() as u16) }); + f.extend(entries); + f.extend(if big { w64(0) } else { w32(0) }); + assert_eq!(f.len(), data_at); + if payload.len() > inline { + f.extend_from_slice(payload); + } + f +} + +#[test] +fn hand_made_files_in_every_layout() { + let long = b"Adobe Photoshop Document Data Block\0MIB8".to_vec(); + for little in [false, true] { + for big in [false, true] { + for payload in [&b"ab"[..], &b"abcd"[..], &b"abcdefgh"[..], &long[..]] { + let f = hand_made(little, big, payload); + let (res, layers) = tiff_photoshop_tags(&f); + assert_eq!(res, None); + assert_eq!(layers, Some(payload), "little {little} big {big} len {}", payload.len()); + let img = decode(&f).unwrap(); + assert_eq!(img.dimensions(), (2, 1)); + assert_eq!(img.meta.photoshop_layers.as_deref(), Some(payload)); + } + } + } +} + +#[test] +fn malformed_tags_read_as_absent_without_panicking() { + let long = vec![7u8; 40]; + for little in [false, true] { + for big in [false, true] { + let f = hand_made(little, big, &long); + // Truncated anywhere: never a panic; the payload is absent once it is cut. + for cut in 0..f.len() { + let (_, layers) = tiff_photoshop_tags(&f[..cut]); + if cut < f.len() { + assert!(layers.is_none_or(|l| l.len() == 40)); + } + let _ = decode(&f[..cut]); + } + // Bit flips in the directory. + for i in 0..f.len() - 40 { + let mut g = f.clone(); + g[i] ^= 0xff; + let _ = tiff_photoshop_tags(&g); + let _ = decode(&g); + } + } + } + assert_eq!(tiff_photoshop_tags(b""), (None, None)); + assert_eq!(tiff_photoshop_tags(b"II*\0"), (None, None)); + assert_eq!(tiff_photoshop_tags(b"MM\0*\xff\xff\xff\xff"), (None, None)); + // A count that overflows the file. + let mut f = hand_made(true, false, &long); + let at = f.windows(2).position(|w| w == 37724u16.to_le_bytes()).unwrap(); + f[at + 4..at + 8].copy_from_slice(&u32::MAX.to_le_bytes()); + assert_eq!(tiff_photoshop_tags(&f).1, None); +} diff --git a/crates/image-codecs/tests/it/tiff_pyramid.rs b/crates/image-codecs/tests/it/tiff_pyramid.rs new file mode 100644 index 000000000..48c846944 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_pyramid.rs @@ -0,0 +1,184 @@ +//! TIFF "Save Image Pyramid" (#8827): reduced-resolution levels after the full image, each half +//! the size of the one before, that every reader skips when it opens the file. + +use craft_image_codecs::*; + +fn pyramid() -> EncodeOptions { + EncodeOptions { tiff_pyramid: true, ..Default::default() } +} + +/// An image of `layout`/`sample` where every sample is `v` (on the 0–1 scale). +fn solid(w: u32, h: u32, layout: ChannelLayout, sample: SampleType, v: f32) -> Image { + let n = w as usize * h as usize * layout.channels(); + let data: Vec = match sample { + SampleType::U8 => vec![(v * 255.0).round() as u8; n], + SampleType::U16 => (0..n).flat_map(|_| ((v * 65535.0).round() as u16).to_ne_bytes()).collect(), + SampleType::F32 => (0..n).flat_map(|_| v.to_ne_bytes()).collect(), + SampleType::F16 => (0..n).flat_map(|_| f16::from_f32(v).to_bits().to_ne_bytes()).collect(), + }; + Image::from_raw(w, h, layout, sample, data).unwrap() +} + +fn sizes(bytes: &[u8]) -> Vec<(TiffPageKind, u32, u32)> { + tiff_info(bytes).unwrap().pages.iter().map(|p| (p.kind, p.width, p.height)).collect() +} + +#[test] +fn levels_halve_down_to_the_minimum() { + use TiffPageKind::*; + let img = solid(300, 200, ChannelLayout::Rgb, SampleType::U8, 0.5); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + // 300 → 150 → 75 → 38: the first level no longer than 64 px is the last. + assert_eq!(sizes(&bytes), [(Page, 300, 200), (ReducedResolution, 150, 100), (ReducedResolution, 75, 50), (ReducedResolution, 38, 25)]); + // Opening the file opens the full image, and the levels are not extra pages. + let back = decode(&bytes).unwrap(); + assert_eq!(back.dimensions(), (300, 200)); + assert_eq!(back.data(), img.data()); + assert!(!back.warnings.iter().any(|w| matches!(w, DecodeWarning::MorePages { .. })), "{:?}", back.warnings); +} + +#[test] +fn off_by_default_and_none_for_small_images() { + let img = solid(300, 200, ChannelLayout::Rgb, SampleType::U8, 0.5); + assert_eq!(sizes(&encode(&img, Format::Tiff, &EncodeOptions::default()).unwrap()).len(), 1); + let small = solid(64, 10, ChannelLayout::Rgb, SampleType::U8, 0.5); + assert_eq!(sizes(&encode(&small, Format::Tiff, &pyramid()).unwrap()).len(), 1); + // A 1-px-wide strip still halves its long side. + let strip = solid(1, 200, ChannelLayout::Gray, SampleType::U8, 0.5); + let s = sizes(&encode(&strip, Format::Tiff, &pyramid()).unwrap()); + assert_eq!(s.iter().map(|&(_, w, h)| (w, h)).collect::>(), [(1, 200), (1, 100), (1, 50)]); +} + +#[test] +fn every_depth_layout_and_compression() { + let layouts = + [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba, ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab]; + for layout in layouts { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + for compression in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let img = solid(120, 70, layout, sample, 0.6); + let opts = EncodeOptions { tiff_compression: compression, ..pyramid() }; + let bytes = encode(&img, Format::Tiff, &opts).unwrap_or_else(|e| panic!("{layout:?} {sample:?} {compression:?}: {e}")); + let info = tiff_info(&bytes).unwrap(); + assert_eq!(info.pages.len(), 2, "{layout:?} {sample:?} {compression:?}"); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.dimensions(), (60, 35)); + assert_eq!(level.layout(), decode(&bytes).unwrap().layout(), "{layout:?} {sample:?}"); + // A solid image stays the same colour at every level. + let full = decode(&bytes).unwrap().convert(level.layout(), SampleType::F32); + let level = level.convert(level.layout(), SampleType::F32); + let (a, b) = (full.to_f32_samples().unwrap(), level.to_f32_samples().unwrap()); + for (c, v) in b.iter().take(level.layout().channels()).enumerate() { + assert!((v - a[c]).abs() < 1e-3, "{layout:?} {sample:?} channel {c}: {v} vs {}", a[c]); + } + } + } + } +} + +/// A 128×128 image of 2×2 `tile`s (top left, top right, bottom left, bottom right; alpha last), +/// and the first pixel of its first reduced level. +fn first_level_pixel(layout: ChannelLayout, tile: [&[u8]; 4]) -> Vec { + let mut data = Vec::new(); + for _ in 0..64 { + for x in 0..128 { + data.extend_from_slice(tile[x % 2]); + } + for x in 0..128 { + data.extend_from_slice(tile[2 + x % 2]); + } + } + let img = Image::from_raw(128, 128, layout, SampleType::U8, data).unwrap(); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.dimensions(), (64, 64), "{layout:?}"); + assert_eq!(level.layout(), layout); + level.data()[..layout.channels()].to_vec() +} + +#[test] +fn colour_is_weighted_by_alpha() { + // One opaque pixel and three transparent ones of another colour: the level keeps the opaque + // colour at a quarter alpha. Unweighted, the transparent colour would bleed in (pink, not red). + assert_eq!(first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), [255, 0, 0, 64]); + // Gray and CMYK reach the same path with other channel counts. + assert_eq!(first_level_pixel(ChannelLayout::GrayA, [&[40, 255], &[255, 0], &[255, 0], &[255, 0]]), [40, 64]); + assert_eq!(first_level_pixel(ChannelLayout::CmykA, [&[200, 10, 30, 90, 255], &[0; 5], &[0; 5], &[0; 5]]), [200, 10, 30, 90, 64]); +} + +#[test] +fn colour_is_weighted_between_translucent_pixels() { + // Two half-transparent pixels of different colours (red, blue) and two clear ones: the level + // averages the two by their alpha (purple), where copying either pixel would give red or blue. + let tile: [&[u8]; 4] = [&[255, 0, 0, 128], &[0, 0, 255, 128], &[0; 4], &[0; 4]]; + assert_eq!(first_level_pixel(ChannelLayout::Rgba, tile), [128, 0, 128, 64]); +} + +#[test] +fn layered_and_tagged_data_stay_on_the_full_image() { + let mut img = solid(200, 100, ChannelLayout::Rgb, SampleType::U8, 0.25); + img.icc = Some(photocraft_cms_free_profile()); + img.meta.photoshop_layers = Some(vec![1, 2, 3, 4]); + img.meta.extra_samples = vec![ExtraSample { kind: 0, data: vec![9; 200 * 100] }]; + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let info = tiff_info(&bytes).unwrap(); + assert_eq!(info.pages.len(), 3); + // Only the full image carries the layers and the extra samples. + assert_eq!(info.pages[0].samples_per_pixel, 4); + assert!(info.pages[1..].iter().all(|p| p.samples_per_pixel == 3)); + assert_eq!(tiff_photoshop_tags(&bytes).1, Some(&[1u8, 2, 3, 4][..])); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.icc, img.icc, "levels keep the profile so they show the same colours"); + assert!(level.meta.extra_samples.is_empty()); +} + +/// Any bytes a decoder keeps as the ICC profile: the codec stores it without parsing. +fn photocraft_cms_free_profile() -> Vec { + (0..=255u8).collect() +} + +#[test] +fn levels_average_exactly_at_every_depth() { + // One 2×2 block of gray values 0, 0.25, 0.5, 1 averages to 0.4375; a 2×2 block with half + // transparent pixels keeps only the opaque colour and averages the alpha. + for sample in [SampleType::U16, SampleType::F32] { + let vals = [0.0f32, 0.25, 0.5, 1.0]; + let mut img = solid(130, 2, ChannelLayout::Gray, sample, 0.0); + let w = 130; + for (i, v) in vals.iter().enumerate() { + let (x, y) = (i % 2, i / 2); + let at = (y * w + x) * sample.bytes(); + let bytes: Vec = match sample { + SampleType::U16 => ((v * 65535.0).round() as u16).to_ne_bytes().to_vec(), + _ => v.to_ne_bytes().to_vec(), + }; + img.data_mut()[at..at + bytes.len()].copy_from_slice(&bytes); + } + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap().convert(ChannelLayout::Gray, SampleType::F32); + let v = level.to_f32_samples().unwrap()[0]; + assert!((v - 0.4375).abs() < 1e-4, "{sample:?}: {v}"); + } + let mut data = Vec::new(); + for _ in 0..2 { + for x in 0..130u32 { + // RGBA16: opaque blue on even columns, transparent red on odd. + let px: [u16; 4] = if x % 2 == 0 { [0, 0, 65535, 65535] } else { [65535, 0, 0, 0] }; + data.extend(px.iter().flat_map(|v| v.to_ne_bytes())); + } + } + let img = Image::from_raw(130, 2, ChannelLayout::Rgba, SampleType::U16, data).unwrap(); + let bytes = encode(&img, Format::Tiff, &pyramid()).unwrap(); + let level = decode_tiff_page(&bytes, 1, &DecodeOptions::default()).unwrap(); + assert_eq!(level.to_u16_samples().unwrap()[..4], [0, 0, 65535, 32768]); +} + +#[test] +fn bigtiff_files_carry_the_pyramid_too() { + let img = solid(300, 200, ChannelLayout::Rgba, SampleType::U8, 0.5); + let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_bigtiff: true, ..pyramid() }).unwrap(); + let info = tiff_info(&bytes).unwrap(); + assert!(info.big_tiff); + assert_eq!(info.pages.iter().map(|p| (p.kind, p.width)).collect::>().len(), 4); + assert_eq!(decode_tiff_page(&bytes, 3, &DecodeOptions::default()).unwrap().dimensions(), (38, 25)); +} diff --git a/crates/image-codecs/tests/it/tiff_regressions.rs b/crates/image-codecs/tests/it/tiff_regressions.rs new file mode 100644 index 000000000..2df89fbbb --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_regressions.rs @@ -0,0 +1,176 @@ +use crate::common; + +use common::tiffgen::*; +use craft_image_codecs::{Image, decode, exif_orientation, upright_exif}; + +fn classic_white_is_zero() -> Vec { + let width = 4u16; + let height = 1u16; + let pixels = [0u8, 64, 192, 255]; + let entries: [(u16, u16, u32); 9] = [ + (256, 3, u32::from(width)), + (257, 3, u32::from(height)), + (258, 3, 8), + (259, 3, 1), + (262, 3, 0), // WhiteIsZero + (273, 4, 122), // StripOffsets: immediately after IFD0. + (277, 3, 1), + (278, 3, u32::from(height)), + (279, 4, pixels.len() as u32), + ]; + let mut bytes = b"II*\0\x08\0\0\0".to_vec(); + bytes.extend_from_slice(&(entries.len() as u16).to_le_bytes()); + for (tag, field_type, value) in entries { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&field_type.to_le_bytes()); + bytes.extend_from_slice(&1u32.to_le_bytes()); + if field_type == 3 { + bytes.extend_from_slice(&(value as u16).to_le_bytes()); + bytes.extend_from_slice(&[0; 2]); + } else { + bytes.extend_from_slice(&value.to_le_bytes()); + } + } + bytes.extend_from_slice(&0u32.to_le_bytes()); + bytes.extend_from_slice(&pixels); + bytes +} + +fn big_tiff_gray(stored: &[u8], width: u16, height: u16, orientation: u16) -> Vec { + let mut entries = vec![ + (256u16, 3u16, 1u64, u64::from(width)), + (257, 3, 1, u64::from(height)), + (258, 3, 1, 8), + (259, 3, 1, 1), + (262, 3, 1, 1), // BlackIsZero + (273, 4, 1, 0), // StripOffsets, patched below. + (274, 3, 1, u64::from(orientation)), + (277, 3, 1, 1), + (278, 3, 1, u64::from(height)), + (279, 4, 1, stored.len() as u64), + ]; + entries.sort_by_key(|entry| entry.0); + let pixel_offset = 16u64 + 8 + entries.len() as u64 * 20 + 8; + if let Some(strip_offset) = entries.iter_mut().find(|entry| entry.0 == 273) { + strip_offset.3 = pixel_offset; + } + + let mut bytes = b"II+\0\x08\0\0\0\x10\0\0\0\0\0\0\0".to_vec(); + bytes.extend_from_slice(&(entries.len() as u64).to_le_bytes()); + for (tag, field_type, count, value) in entries { + bytes.extend_from_slice(&tag.to_le_bytes()); + bytes.extend_from_slice(&field_type.to_le_bytes()); + bytes.extend_from_slice(&count.to_le_bytes()); + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes.extend_from_slice(&0u64.to_le_bytes()); + bytes.extend_from_slice(stored); + bytes +} + +fn stored_for_orientation_6_or_8(orientation: u16) -> Vec { + match orientation { + 6 => vec![3, 6, 2, 5, 1, 4], + 8 => vec![4, 1, 5, 2, 6, 3], + _ => unreachable!(), + } +} + +#[test] +fn white_is_zero_is_normalized_once_by_the_tiff_decoder() { + let image = decode(&classic_white_is_zero()).expect("synthetic WhiteIsZero TIFF should decode"); + + assert_eq!(image.dimensions(), (4, 1)); + assert_eq!(image.data(), &[255, 191, 63, 0]); +} + +#[test] +fn big_tiff_orientation_6_and_8_are_applied_to_decoded_pixels() { + let upright = [1u8, 2, 3, 4, 5, 6]; + + for orientation in [6, 8] { + let bytes = big_tiff_gray(&stored_for_orientation_6_or_8(orientation), 2, 3, orientation); + let image: Image = decode(&bytes).expect("synthetic BigTIFF should decode"); + + assert_eq!(exif_orientation(&bytes), orientation); + assert_eq!(exif_orientation(&upright_exif(&bytes)), 1); + assert_eq!(image.dimensions(), (3, 2), "orientation {orientation}"); + assert_eq!(image.data(), &upright, "orientation {orientation}"); + } +} + +fn with_compression(mut dir: Dir, code: u64) -> Dir { + for entry in &mut dir.entries { + if entry.0 == 259 { + entry.2 = vec![V::N(code)]; + } + } + dir +} + +#[test] +fn jpeg_compressed_tiff_decodes() { + // A flat mid-grey 16x16 image as a baseline JPEG stored in one strip (Compression = 7). + let mut jpeg = Vec::new(); + let gray = vec![100u8; 16 * 16]; + jpeg_encoder::Encoder::new(&mut jpeg, 95).encode(&gray, 16, 16, jpeg_encoder::ColorType::Luma).expect("encode the JPEG strip"); + let mut dir = with_compression(strips(16, 16, 8, 1, 1, 16, &[0; 256]), 7); + dir.chunks = vec![jpeg]; + let bytes = build(true, false, &[dir], &[0]).bytes; + + let image = decode(&bytes).expect("a JPEG-compressed TIFF should decode"); + assert_eq!(image.dimensions(), (16, 16)); + assert!(image.data().iter().all(|&v| v.abs_diff(100) <= 4), "{:?}", image.data().get(..4)); +} + +#[test] +fn ccitt_group4_tiff_decodes() { + // 8x3 bilevel: a white row, then two black rows. Bits: V0 | H(white 0, black 8) | V0 V0 | EOFB. + let bits = "1".to_string() + "001" + "00110101" + "000101" + "11" + "000000000001" + "000000000001"; + let mut data = Vec::new(); + for chunk in bits.as_bytes().chunks(8) { + let mut byte = 0u8; + for (i, c) in chunk.iter().enumerate() { + if *c == b'1' { + byte |= 0x80 >> i; + } + } + data.push(byte); + } + let mut dir = with_compression(strips(8, 3, 1, 1, 0, 3, &[0; 3]), 4); + dir.chunks = vec![data]; + let bytes = build(true, false, &[dir], &[0]).bytes; + + let image = decode(&bytes).expect("a CCITT G4 TIFF should decode"); + assert_eq!(image.dimensions(), (8, 3)); + let d = image.data(); + assert_eq!(d.len(), 24, "{d:?}"); + assert!(d[..8].iter().all(|&v| v == d[0]), "{d:?}"); + assert!(d[8..].iter().all(|&v| v == d[8]), "{d:?}"); + assert_ne!(d[0], d[8], "{d:?}"); + assert_eq!(d[0], 255, "white row of a WhiteIsZero image"); +} + +/// #4484: a TIFF saved with the default options must open in software that only reads +/// baseline TIFF (embroidery and plotter tools, older readers). Like Photoshop's default, that +/// means uncompressed strips with no predictor, classic offsets and unassociated alpha. +#[test] +fn default_tiff_is_baseline() { + use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, SampleType, encode}; + use std::io::Cursor; + use tiff::tags::Tag; + + for layout in [ChannelLayout::Rgb, ChannelLayout::Rgba] { + let img = common::synth(33, 21, layout, SampleType::U8, 7, 0.4); + let bytes = encode(&img, Format::Tiff, &EncodeOptions::default()).expect("encode"); + assert!(bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*"), "{layout:?}: not a classic TIFF"); + let mut dec = tiff::decoder::Decoder::new(Cursor::new(&bytes[..])).expect("open"); + assert_eq!(dec.get_tag_u32(Tag::Compression).ok(), Some(1), "{layout:?}: Compression"); + assert_eq!(dec.find_tag(Tag::Predictor).ok().flatten().and_then(|v| v.into_u16().ok()).unwrap_or(1), 1, "{layout:?}: Predictor"); + assert!(dec.find_tag(Tag::TileWidth).ok().flatten().is_none(), "{layout:?}: tiled"); + assert_eq!(dec.get_tag_u32(Tag::PlanarConfiguration).ok().unwrap_or(1), 1, "{layout:?}: planar"); + let extra = dec.find_tag(Tag::ExtraSamples).ok().flatten().and_then(|v| v.into_u16_vec().ok()); + assert_eq!(extra, if layout == ChannelLayout::Rgba { Some(vec![2]) } else { None }, "{layout:?}: ExtraSamples"); + assert_eq!(decode(&bytes).expect("decode").data(), img.data(), "{layout:?}: round trip"); + } +} diff --git a/crates/image-codecs/tests/it/tiff_ycbcr.rs b/crates/image-codecs/tests/it/tiff_ycbcr.rs new file mode 100644 index 000000000..15cac9410 --- /dev/null +++ b/crates/image-codecs/tests/it/tiff_ycbcr.rs @@ -0,0 +1,259 @@ +//! YCbCr TIFFs (PhotometricInterpretation 6), as scanners write them: JPEG-compressed with 2×2 +//! chroma subsampling, and uncompressed with ReferenceBlackWhite and YCbCrCoefficients (#10316). +//! Every file here is hand-built by `common::tiffgen`. + +use crate::common::sample_icc; +use crate::common::tiffgen::*; +use craft_image_codecs::*; + +const COMPRESSION: u16 = 259; +const SUBSAMPLING: u16 = 530; +const YCBCR_COEFFICIENTS: u16 = 529; +const REFERENCE_BLACK_WHITE: u16 = 532; +const ICC_PROFILE: u16 = 34675; + +/// A directory whose Compression is `c`. +fn compressed(mut d: Dir, c: u64) -> Dir { + d.entries.retain(|e| e.0 != COMPRESSION); + d.tag(COMPRESSION, SHORT, &[c]) +} + +/// A little-endian RATIONAL value for `tiffgen`. +fn rational(num: u32, den: u32) -> u64 { + u64::from(num) | (u64::from(den) << 32) +} + +/// The image's pixels as RGB bytes. +fn rgb(img: &Image) -> Vec { + img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|[r, g, b, _]| [*r, *g, *b]).collect() +} + +/// A smooth colour test card, `w`×`h` RGB. +fn card(w: u32, h: u32) -> Vec { + let mut v = Vec::new(); + for y in 0..h { + for x in 0..w { + v.extend([(x * 255 / (w - 1)) as u8, (y * 255 / (h - 1)) as u8, (255 - x * 255 / (w - 1)) as u8]); + } + } + v +} + +/// A 4:2:0 baseline JPEG stream of `rgb`. +fn jpeg(rgb: &[u8], w: u32, h: u32) -> Vec { + let img = Image::from_raw(w, h, ChannelLayout::Rgb, SampleType::U8, rgb.to_vec()).unwrap(); + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_chroma_subsampling: true, ..Default::default() }).unwrap() +} + +#[test] +fn jpeg_compressed_ycbcr_with_subsampled_chroma_reads_as_rgb() { + let (w, h) = (32, 24); + let stream = jpeg(&card(w, h), w, h); + // The stream really is subsampled: the luma component samples 2×2 in the frame header. + let sof = stream.windows(2).position(|m| m == [0xFF, 0xC0]).expect("baseline frame header"); + assert_eq!(stream[sof + 11], 0x22, "luma sampling factors"); + let dir = compressed(strips(w, h, 8, 3, 6, h, &stream), 7).tag(SUBSAMPLING, SHORT, &[2, 2]); + let file = build(true, false, &[dir], &[0]).bytes; + + let img = decode(&file).unwrap(); + assert_eq!((img.width(), img.height(), img.layout(), img.sample_type()), (w, h, ChannelLayout::Rgb, SampleType::U8)); + // The oracle: the same JPEG stream decoded to RGB by an independent decoder. + let want = image::load_from_memory_with_format(&stream, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw(); + let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); + assert!(worst <= 2, "largest difference from the oracle: {worst}"); +} + +#[test] +fn uncompressed_ycbcr_uses_the_default_conversion() { + // Black, white, mid gray and JFIF's pure red, without subsampling. + let px = [0, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; + let dir = strips(4, 1, 8, 3, 6, 1, &px).tag(SUBSAMPLING, SHORT, &[1, 1]); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgb); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0]); +} + +#[test] +fn reference_black_white_sets_the_code_range() { + // Studio range: Y 16–235, chroma 128 ± 112. + let rbw = [rational(16, 1), rational(235, 1), rational(128, 1), rational(240, 1), rational(128, 1), rational(240, 1)]; + let dir = strips(2, 1, 8, 3, 6, 1, &[16, 128, 128, 235, 128, 128]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(REFERENCE_BLACK_WHITE, RATIONAL, &rbw); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255]); +} + +#[test] +fn weights_that_do_not_sum_to_one_keep_grays_neutral() { + // 0.25 each (sum 0.75): TIFF 6.0's full inverse would scale green's Y by (1 − Lr − Lb) / Lg + // = 2 and give mid gray G 255 (clamped). Like libtiff, green keeps Y, so gray stays gray, and + // the weights still set the chroma terms: R = Y + 1.5·Cr, B = Y + 1.5·Cb. + let weights = [rational(1, 4), rational(1, 4), rational(1, 4)]; + let dir = strips(2, 1, 8, 3, 6, 1, &[128, 128, 128, 128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + // Green for the second pixel: 128 − (Lb·1.5/Lg)·Cb − (Lr·1.5/Lg)·Cr = 128 + 96 − 96. + assert_eq!(rgb(&img), [128, 128, 128, 224, 128, 32]); +} + +#[test] +fn ycbcr_coefficients_set_the_luma_weights() { + // Weights 0.25, 0.5, 0.25 with Cb −64 and Cr +64: R = Y + 1.5·Cr, B = Y + 1.5·Cb, and G keeps Y + // (BT.601 would give R 218 and B 15). + let weights = [rational(1, 4), rational(1, 2), rational(1, 4)]; + let dir = strips(1, 1, 8, 3, 6, 1, &[128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [224, 128, 32]); +} + +#[test] +fn ycbcr_tags_with_the_wrong_count_use_the_defaults() { + // Four weights and seven black/white codes: both tags are malformed, so BT.601 and full range + // apply (the weights alone would give 224, 128, 32). + let weights = [rational(1, 4), rational(1, 2), rational(1, 4), rational(1, 4)]; + let rbw = [rational(16, 1), rational(235, 1), rational(128, 1), rational(240, 1), rational(128, 1), rational(240, 1), rational(0, 1)]; + let dir = strips(1, 1, 8, 3, 6, 1, &[128, 64, 192]).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).tag( + REFERENCE_BLACK_WHITE, + RATIONAL, + &rbw, + ); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [218, 104, 15]); +} + +#[test] +fn sixteen_bit_ycbcr_reads_as_rgb() { + let px: Vec = [65535u16, 32768, 32768, 0, 32768, 32768].iter().flat_map(|v| v.to_le_bytes()).collect(); + let dir = strips(2, 1, 16, 3, 6, 1, &px).tag(SUBSAMPLING, SHORT, &[1, 1]); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U16)); + let n = img.to_normalized(); + assert!(n[..3].iter().all(|&v| v == 1.0) && n[3..].iter().all(|&v| v == 0.0), "{n:?}"); +} + +#[test] +#[cfg(target_pointer_width = "64")] +fn sixteen_bit_ycbcr_bands_match_whole_decode_with_a_missing_last_strip() { + // On 32-bit targets max_alloc also bounds the whole image, so a small allocation cap + // cannot force these tiny strips into separate output groups there. + let (w, h, rows) = (4u32, 5u32, 2u32); + let pixels = [[0u16, 32768, 32768], [65535, 32768, 32768], [19595, 21710, 65535], [32768, 40960, 24576]]; + let samples: Vec = (0..w * h).flat_map(|i| pixels[i as usize % pixels.len()]).collect(); + let weights = [rational(1, 4), rational(1, 2), rational(1, 4)]; + let icc = sample_icc(512); + for little in [true, false] { + for missing in [false, true] { + let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).bytes( + ICC_PROFILE, + UNDEFINED, + &icc, + ); + let mut file = build(little, false, &[dir], &[0]); + if missing { + let past = file.bytes.len() as u64 + 4096; + patch_value(&mut file, little, false, 0, 273, 2, past); + } + let want = decode_with(&file.bytes, &DecodeOptions { keep_orientation: true, ..Default::default() }).unwrap(); + let limits = Limits { max_alloc: u64::from(w * rows * 3 * 2), ..Limits::banded() }; + let mut bands = TiffBands::open(&file.bytes, None, &limits).unwrap(); + assert_eq!((bands.dimensions(), bands.layout(), bands.sample_type()), ((w, h), ChannelLayout::Rgb, SampleType::U16)); + let mut got = Image::new(w, h, bands.layout(), bands.sample_type()).unwrap(); + let mut positions = Vec::new(); + bands + .decode(|x, y, group: &Image| -> Result<(), CodecError> { + positions.push((x, y, group.height())); + assert_eq!((x, group.width(), group.layout(), group.sample_type()), (0, w, ChannelLayout::Rgb, SampleType::U16)); + let at = y as usize * w as usize * 3 * 2; + got.data_mut()[at..at + group.data().len()].copy_from_slice(group.data()); + Ok(()) + }) + .unwrap(); + assert_eq!(positions, [(0, 0, 2), (0, 2, 2), (0, 4, 1)], "one output group per strip, including the short last strip"); + assert_eq!(got.data(), want.data(), "little endian: {little}, missing last strip: {missing}"); + assert_eq!(bands.icc, want.icc); + assert_eq!(bands.meta, want.meta); + assert_eq!(bands.warnings, want.warnings); + assert_eq!(bands.warnings.iter().any(|w| matches!(w, DecodeWarning::Truncated { .. })), missing); + } + } +} + +#[test] +fn unsupported_ycbcr_files_fail_cleanly() { + // Four samples (an extra sample next to YCbCr). + let four = strips(1, 1, 8, 4, 6, 1, &[128, 128, 128, 255]).tag(SUBSAMPLING, SHORT, &[1, 1]); + assert!(decode(&build(true, false, &[four], &[0]).bytes).is_err()); + // Subsampled chroma without JPEG compression (the default subsampling is 2×2). + let sub = strips(2, 2, 8, 3, 6, 2, &[128; 12]); + assert!(decode(&build(true, false, &[sub], &[0]).bytes).is_err()); + // A JPEG strip that is not JPEG data. + let junk = compressed(strips(16, 16, 8, 3, 6, 16, &[0x5A; 64]), 7).tag(SUBSAMPLING, SHORT, &[2, 2]); + assert!(decode(&build(true, false, &[junk], &[0]).bytes).is_err()); + // Coefficients and ReferenceBlackWhite that would divide by zero fall back to the defaults. + let bad = strips(1, 1, 8, 3, 6, 1, &[255, 128, 128]) + .tag(SUBSAMPLING, SHORT, &[1, 1]) + .tag(YCBCR_COEFFICIENTS, RATIONAL, &[rational(0, 1), rational(0, 0), rational(5, 1)]) + .tag(REFERENCE_BLACK_WHITE, RATIONAL, &[rational(7, 1); 6]); + let img = decode(&build(true, false, &[bad], &[0]).bytes).unwrap(); + assert_eq!(&img.to_rgba8()[..3], &[255, 255, 255]); +} + +#[test] +fn tiled_jpeg_ycbcr_keeps_its_icc_profile() { + // Two 16×16 tiles, each its own 4:2:0 JPEG stream, and an embedded profile, like a scanner's + // tiled output. + let (w, h, t) = (32u32, 16u32, 16u32); + let px = card(w, h); + let tile = |tx: u32| -> Vec { + (0..t) + .flat_map(|y| { + let at = ((y * w + tx * t) * 3) as usize; + px[at..at + (t * 3) as usize].to_vec() + }) + .collect() + }; + let streams = [jpeg(&tile(0), t, t), jpeg(&tile(1), t, t)]; + let icc = sample_icc(512); + let dir = Dir { entries: base(w, h, 8, 3, 6), chunks: streams.to_vec(), tiled: true }; + let dir = compressed(dir, 7).tag(322, SHORT, &[u64::from(t)]).tag(323, SHORT, &[u64::from(t)]).tag(SUBSAMPLING, SHORT, &[2, 2]).bytes( + ICC_PROFILE, + UNDEFINED, + &icc, + ); + let img = decode(&build(true, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!((img.width(), img.height(), img.layout()), (w, h, ChannelLayout::Rgb)); + assert_eq!(img.icc.as_deref(), Some(icc.as_slice()), "the embedded profile is kept"); + // The oracle: each tile's stream decoded by an independent decoder, placed side by side. + let tiles: Vec> = streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); + let row = (t * 3) as usize; + let want: Vec = (0..t as usize).flat_map(|y| [&tiles[0][y * row..(y + 1) * row], &tiles[1][y * row..(y + 1) * row]].concat()).collect(); + let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); + assert!(worst <= 2, "largest difference from the oracle: {worst}"); +} + +#[test] +fn big_endian_sixteen_bit_ycbcr_reads_as_rgb() { + // White, black, and red as full-range 16-bit YCbCr (Y 0.299·65535, Cb 32768 − 0.1687·65535, Cr max). + let px = [65535u16, 32768, 32768, 0, 32768, 32768, 19595, 21710, 65535]; + let dir = strips(3, 1, 16, 3, 6, 1, &u16s(&px, false)).tag(SUBSAMPLING, SHORT, &[1, 1]); + let file = build(false, false, &[dir], &[0]).bytes; + assert_eq!(&file[..2], b"MM"); + let img = decode(&file).unwrap(); + assert_eq!((img.layout(), img.sample_type()), (ChannelLayout::Rgb, SampleType::U16)); + let n = img.to_normalized(); + let want = [1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]; + assert!(n.iter().zip(want).all(|(g, w)| (g - w).abs() < 2.0 / 255.0), "{n:?}"); +} + +#[test] +fn planar_ycbcr_reads_as_rgb_in_both_byte_orders() { + // The pixels of the default-conversion test, one plane per component. + let px = [0u8, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; + for little in [true, false] { + let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false }.tag(278, LONG, &[1]).tag(284, SHORT, &[2]).tag( + SUBSAMPLING, + SHORT, + &[1, 1], + ); + let img = decode(&build(little, false, &[dir], &[0]).bytes).unwrap(); + assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0], "little endian: {little}"); + } +} diff --git a/crates/image-codecs/tests/it/webp_frames.rs b/crates/image-codecs/tests/it/webp_frames.rs new file mode 100644 index 000000000..c099ec981 --- /dev/null +++ b/crates/image-codecs/tests/it/webp_frames.rs @@ -0,0 +1,166 @@ +//! `decode_webp_frames`: every frame of an animated WebP, composited, with its duration (#1198). + +use craft_image_codecs::{ + AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, encode, + webp_is_animated, +}; + +const RED: [u8; 4] = [255, 0, 0, 255]; +const GREEN: [u8; 4] = [0, 255, 0, 255]; +const BLUE: [u8; 4] = [0, 0, 255, 255]; + +fn frames(bytes: &[u8]) -> Vec { + decode_webp_frames(bytes, &DecodeOptions::default()).unwrap() +} + +fn solid(c: [u8; 4], w: u32, h: u32) -> Image { + Image::from_u8(w, h, ChannelLayout::Rgba, c.repeat((w * h) as usize)).unwrap() +} + +fn chunk(id: &[u8; 4], data: &[u8]) -> Vec { + let mut c = id.to_vec(); + c.extend_from_slice(&(data.len() as u32).to_le_bytes()); + c.extend_from_slice(data); + if data.len() % 2 == 1 { + c.push(0); + } + c +} + +fn u24(v: u32) -> [u8; 3] { + let b = v.to_le_bytes(); + [b[0], b[1], b[2]] +} + +/// One frame of [`animated`]: the picture, where it sits on the canvas (even offsets, as WebP +/// stores them halved), its duration in milliseconds, and whether it is alpha-blended over the +/// canvas rather than replacing it. +struct Part { + image: Image, + at: [u32; 2], + duration_ms: u32, + blend: bool, +} + +fn part(c: [u8; 4], w: u32, h: u32, duration_ms: u32) -> Part { + Part { image: solid(c, w, h), at: [0, 0], duration_ms, blend: false } +} + +/// An animated WebP with a `w`x`h` canvas (transparent background) and one ANMF chunk per part, +/// each holding a lossless still from our own encoder. +fn animated(w: u32, h: u32, parts: &[Part]) -> Vec { + let mut body = b"WEBP".to_vec(); + let mut vp8x = vec![0x02 | 0x10, 0, 0, 0]; // animation and alpha flags + vp8x.extend_from_slice(&u24(w - 1)); + vp8x.extend_from_slice(&u24(h - 1)); + body.extend(chunk(b"VP8X", &vp8x)); + body.extend(chunk(b"ANIM", &[0, 0, 0, 0, 0, 0])); // background 0, loop forever + for p in parts { + let still = encode(&p.image, Format::WebP, &EncodeOptions::default()).unwrap(); + let at = still.windows(4).position(|w| w == b"VP8L").unwrap(); + let len = u32::from_le_bytes(still[at + 4..at + 8].try_into().unwrap()) as usize; + let mut anmf = Vec::new(); + anmf.extend_from_slice(&u24(p.at[0] / 2)); + anmf.extend_from_slice(&u24(p.at[1] / 2)); + anmf.extend_from_slice(&u24(p.image.width() - 1)); + anmf.extend_from_slice(&u24(p.image.height() - 1)); + anmf.extend_from_slice(&u24(p.duration_ms)); + anmf.push(if p.blend { 0 } else { 0x02 }); // bit 1: do not blend + anmf.extend(chunk(b"VP8L", &still[at + 8..at + 8 + len])); + body.extend(chunk(b"ANMF", &anmf)); + } + let mut out = b"RIFF".to_vec(); + out.extend_from_slice(&(body.len() as u32).to_le_bytes()); + out.extend(body); + out +} + +fn pixel(f: &AnimationFrame, x: u32, y: u32) -> [u8; 4] { + let rgba = f.image.to_rgba8(); + let i = ((y * f.image.width() + x) * 4) as usize; + rgba[i..i + 4].try_into().unwrap() +} + +#[test] +fn a_still_webp_gives_one_frame_equal_to_decode() { + let still = encode(&solid(RED, 6, 4), Format::WebP, &EncodeOptions::default()).unwrap(); + assert!(!webp_is_animated(&still)); + let f = frames(&still); + assert_eq!(f.len(), 1); + assert_eq!(f[0].delay_ms, 0); + assert_eq!(f[0].image, decode(&still).unwrap()); +} + +#[test] +fn a_one_frame_animation_is_not_animated() { + let a = animated(6, 4, &[part(RED, 6, 4, 70)]); + assert!(!webp_is_animated(&a)); + assert_eq!(frames(&a).len(), 1); +} + +#[test] +fn every_frame_comes_back_in_order_with_its_duration() { + let a = animated(6, 4, &[part(RED, 6, 4, 40), part(GREEN, 6, 4, 120), part(BLUE, 6, 4, 7)]); + assert!(webp_is_animated(&a)); + let f = frames(&a); + assert_eq!(f.iter().map(|f| f.delay_ms).collect::>(), [40, 120, 7], "WebP durations are milliseconds"); + for (f, c) in f.iter().zip([RED, GREEN, BLUE]) { + assert_eq!((f.image.width(), f.image.height()), (6, 4)); + assert_eq!(pixel(f, 0, 0), c); + assert_eq!(pixel(f, 5, 3), c); + assert_eq!(f.image.warnings, []); + } +} + +#[test] +fn a_smaller_frame_is_drawn_over_the_previous_one() { + let small = Part { image: solid(BLUE, 2, 2), at: [2, 2], duration_ms: 50, blend: true }; + let f = frames(&animated(6, 4, &[part(RED, 6, 4, 50), small])); + assert_eq!(f.len(), 2); + // Alpha blending rounds: within one level. + let near = |a: [u8; 4], b: [u8; 4]| a.iter().zip(b).all(|(x, y)| x.abs_diff(y) <= 1); + assert!(near(pixel(&f[1], 0, 0), RED), "outside the second frame the first shows through"); + assert!(near(pixel(&f[1], 2, 2), BLUE)); + assert!(near(pixel(&f[1], 3, 3), BLUE)); + assert!(near(pixel(&f[1], 4, 2), RED)); +} + +#[test] +fn a_damaged_later_frame_keeps_the_frames_before_it() { + let mut a = animated(6, 4, &[part(RED, 6, 4, 50), part(GREEN, 6, 4, 50), part(BLUE, 6, 4, 50)]); + // Break the last frame's VP8L signature byte; the container around it stays whole. + let last = a.windows(4).rposition(|w| w == b"VP8L").unwrap(); + a[last + 8] = 0; + let f = frames(&a); + assert_eq!(f.len(), 2); + assert_eq!(pixel(&f[0], 0, 0), RED); + assert_eq!(pixel(&f[1], 0, 0), GREEN); + assert_eq!(f[0].image.warnings, [DecodeWarning::Truncated { format: Format::WebP }]); +} + +#[test] +fn every_prefix_of_an_animated_webp_returns() { + let a = animated(5, 4, &[part(RED, 5, 4, 50), part(GREEN, 5, 4, 50), part(BLUE, 5, 4, 50)]); + for n in 0..a.len() { + let _ = decode_webp_frames(&a[..n], &DecodeOptions::default()); + } +} + +#[test] +fn dimension_limits_apply() { + let a = animated(4, 4, &[part(RED, 4, 4, 50), part(GREEN, 4, 4, 50)]); + let opts = DecodeOptions { limits: Limits { max_width: 3, ..Limits::default() }, ..Default::default() }; + assert!(matches!(decode_webp_frames(&a, &opts), Err(CodecError::LimitExceeded(_)))); +} + +#[test] +fn the_byte_budget_covers_all_frames_together() { + // One 4x4 RGBA frame is 64 bytes. The decoder's own canvas counts against the budget too, so + // two frames need more than 128; ten frames of the same size must not fit where two do. + let two = animated(4, 4, &[part(RED, 4, 4, 50), part(GREEN, 4, 4, 50)]); + let ten: Vec = (0..10).map(|i| part(if i % 2 == 0 { RED } else { BLUE }, 4, 4, 50)).collect(); + let ten = animated(4, 4, &ten); + let opts = DecodeOptions { limits: Limits { max_alloc: 256, ..Limits::default() }, ..Default::default() }; + assert_eq!(decode_webp_frames(&two, &opts).unwrap().len(), 2); + assert!(matches!(decode_webp_frames(&ten, &opts), Err(CodecError::LimitExceeded(_)))); +} diff --git a/crates/image-codecs/tests/it/webp_lossy.rs b/crates/image-codecs/tests/it/webp_lossy.rs new file mode 100644 index 000000000..2d0c4861c --- /dev/null +++ b/crates/image-codecs/tests/it/webp_lossy.rs @@ -0,0 +1,140 @@ +//! Lossy WebP (our VP8 encoder) checked against an independent decoder (`image-webp`): the +//! files decode, look like the input (PSNR by quality), get smaller as quality drops and are far +//! smaller than lossless on photographic content; alpha and metadata travel in the container; +//! odd sizes and the limits behave. + +use crate::common; +use common::*; +use craft_image_codecs::*; + +fn lossy(q: u8) -> EncodeOptions { + EncodeOptions { webp_lossless: false, webp_quality: q, ..Default::default() } +} + +/// A smooth photograph-like image: gradients with a little grain. +fn photo(w: u32, h: u32, seed: u64) -> Image { + synth(w, h, ChannelLayout::Rgb, SampleType::U8, seed, 0.03) +} + +#[test] +fn decodes_with_the_right_size_and_looks_like_the_input() { + let img = photo(97, 61, 3); + for (q, min_psnr) in [(100u8, 40.0), (90, 36.0), (75, 33.0), (50, 30.0), (20, 26.0)] { + let bytes = encode(&img, Format::WebP, &lossy(q)).unwrap(); + let back = decode(&bytes).unwrap_or_else(|e| panic!("q{q}: {e}")); + assert_eq!(back.dimensions(), (97, 61)); + assert_eq!(back.layout(), ChannelLayout::Rgb); + let p = psnr(&img, &back); + assert!(p >= min_psnr, "quality {q}: PSNR {p:.1} dB below {min_psnr}"); + } +} + +#[test] +fn smaller_at_lower_quality_and_much_smaller_than_lossless_on_photos() { + let img = synth(256, 192, ChannelLayout::Rgb, SampleType::U8, 9, 0.08); + let lossless = encode(&img, Format::WebP, &EncodeOptions::default()).unwrap().len(); + let sizes: Vec = [95u8, 80, 60, 40, 20].iter().map(|&q| encode(&img, Format::WebP, &lossy(q)).unwrap().len()).collect(); + for w in sizes.windows(2) { + assert!(w[1] <= w[0], "sizes must not grow as quality drops: {sizes:?}"); + } + assert!(sizes[1] * 4 < lossless, "q80 {} vs lossless {lossless}", sizes[1]); + assert!(sizes[4] * 10 < lossless, "q20 {} vs lossless {lossless}", sizes[4]); +} + +#[test] +fn flat_areas_cost_almost_nothing() { + // A single colour: every macroblock is skipped; the file is dominated by the header. + let img = Image::from_u8(640, 480, ChannelLayout::Rgb, vec![200u8; 640 * 480 * 3]).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(80)).unwrap(); + assert!(bytes.len() < 1200, "{} bytes", bytes.len()); + let back = decode(&bytes).unwrap(); + assert!(max_abs_diff(&img, &back) <= 3.0 / 255.0, "flat colour reproduced within rounding"); +} + +#[test] +fn alpha_is_kept_exactly_and_metadata_travels() { + let mut img = synth(40, 30, ChannelLayout::Rgba, SampleType::U8, 5, 0.05); + img.icc = Some(sample_icc(400)); + img.meta.exif = Some(sample_exif()); + img.meta.xmp = Some(SAMPLE_XMP.to_owned()); + let bytes = encode(&img, Format::WebP, &lossy(85)).unwrap(); + assert_eq!(&bytes[12..16], b"VP8X", "extended container"); + let back = decode(&bytes).unwrap(); + assert_eq!(back.layout(), ChannelLayout::Rgba); + for (a, b) in img.data().as_chunks::<4>().0.iter().zip(back.data().as_chunks::<4>().0) { + assert_eq!(a[3], b[3], "alpha is stored losslessly"); + } + assert!(psnr(&img, &back) > 30.0); + assert_eq!(back.icc, img.icc); + assert!(back.meta.xmp.is_some()); + assert!(back.meta.exif.is_some()); + // Fully opaque alpha is dropped rather than stored. + let mut data = synth(40, 30, ChannelLayout::Rgba, SampleType::U8, 5, 0.0).data().to_vec(); + for px in data.as_chunks_mut::<4>().0 { + px[3] = 255; + } + let opaque = Image::from_u8(40, 30, ChannelLayout::Rgba, data).unwrap(); + let bytes = encode(&opaque, Format::WebP, &EncodeOptions { embed_icc: false, embed_metadata: false, ..lossy(85) }).unwrap(); + assert_eq!(&bytes[12..16], b"VP8 "); + assert_eq!(decode(&bytes).unwrap().layout(), ChannelLayout::Rgb); +} + +#[test] +fn gray_and_odd_sizes_and_tiny_images() { + for (w, h) in [(1, 1), (2, 3), (17, 9), (33, 1), (1, 47), (16, 16), (31, 15)] { + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + // Little noise: at these sizes the 2 × 2 chroma average alone would otherwise dominate. + let img = synth(w, h, layout, SampleType::U8, u64::from(w * 100 + h), 0.02); + let bytes = encode(&img, Format::WebP, &lossy(80)).unwrap_or_else(|e| panic!("{w}x{h} {layout:?}: {e}")); + let back = decode(&bytes).unwrap_or_else(|e| panic!("{w}x{h} {layout:?}: {e}")); + assert_eq!(back.dimensions(), (w, h)); + // Below 8 px the 2 × 2 chroma average of random saturated pixels is the whole error. + if w >= 8 && h >= 8 { + let p = psnr(&img.convert(back.layout(), SampleType::U8), &back); + assert!(p > 24.0, "{w}x{h} {layout:?}: {p:.1} dB"); + } + } + } +} + +#[test] +fn sixteen_bit_input_is_quantised_to_eight() { + let img = synth(24, 24, ChannelLayout::Rgb, SampleType::U16, 2, 0.02); + let bytes = encode(&img, Format::WebP, &lossy(90)).unwrap(); + let back = decode(&bytes).unwrap(); + assert!(psnr(&img.convert(ChannelLayout::Rgb, SampleType::U8), &back) > 34.0); +} + +#[test] +fn dimension_limit_is_an_error_not_a_panic() { + let img = Image::from_u8(16384, 1, ChannelLayout::Rgb, vec![0u8; 16384 * 3]).unwrap(); + let e = encode(&img, Format::WebP, &lossy(50)).unwrap_err(); + assert!(matches!(e, CodecError::Encode { .. }), "{e}"); + let img = Image::from_u8(16383, 1, ChannelLayout::Rgb, vec![0u8; 16383 * 3]).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(50)).unwrap(); + assert_eq!(decode(&bytes).unwrap().dimensions(), (16383, 1)); +} + +#[test] +fn hard_edges_and_text_like_content_keep_their_structure() { + // Black text-like strokes on white: B_PRED sub-block modes keep edges sharp enough that the + // thresholded result matches the source almost everywhere. + let (w, h) = (128u32, 64u32); + let mut data = vec![255u8; (w * h * 3) as usize]; + for y in 0..h { + for x in 0..w { + let stroke = (x % 11 < 2 && y > 8 && y < 56) || (y % 13 < 2 && x > 4 && x < 124); + if stroke { + let i = ((y * w + x) * 3) as usize; + data[i..i + 3].fill(0); + } + } + } + let img = Image::from_u8(w, h, ChannelLayout::Rgb, data).unwrap(); + let bytes = encode(&img, Format::WebP, &lossy(90)).unwrap(); + let back = decode(&bytes).unwrap(); + let wrong = img.data().as_chunks::<3>().0.iter().zip(back.data().as_chunks::<3>().0).filter(|(a, b)| (a[0] < 128) != (b[0] < 128)).count(); + let total = (w * h) as usize; + assert!(wrong * 100 < total * 2, "{wrong} of {total} pixels flipped side of mid-gray"); + assert!(psnr(&img, &back) > 30.0, "{:.1}", psnr(&img, &back)); +} diff --git a/crates/image-codecs/tests/it/xpm.rs b/crates/image-codecs/tests/it/xpm.rs new file mode 100644 index 000000000..b499cea33 --- /dev/null +++ b/crates/image-codecs/tests/it/xpm.rs @@ -0,0 +1,123 @@ +use craft_image_codecs::{ChannelLayout, CodecError, Format, Image, caps, decode, decode_as, detect, encode, from_extension}; + +const SAMPLE: &[u8] = br#"/* XPM */ +static char *icon[] = { +"2 2 3 1", +". c #ff0000", +" c None", +"+ c #00ff00", +".+", +" +" +}; +"#; + +#[test] +fn xpm3_decodes_palette_transparency_and_spaces_in_keys() { + assert_eq!(detect(SAMPLE), Some(Format::Xpm)); + assert_eq!(from_extension("icon.XPM"), Some(Format::Xpm)); + let img = decode(SAMPLE).unwrap(); + assert_eq!(img.layout(), ChannelLayout::Rgba); + assert_eq!(img.dimensions(), (2, 2)); + assert_eq!(img.data(), &[255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 0, 0, 0, 255, 0, 255]); +} + +#[test] +fn rgb_precisions_and_x11_name() { + let xpm = br#"/* XPM */ +static char *i[] = { +"3 1 3 1", +"a c #f00", +"b c #ffff00000000", +"c c gray70", +"abc" +};"#; + let img = decode(xpm).unwrap(); + assert_eq!(img.data(), &[255, 0, 0, 255, 255, 0, 0, 255, 179, 179, 179, 255]); +} + +#[test] +fn rejects_truncation_unsupported_colours_and_unknown_keys() { + for bytes in [ + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c None\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c NotAColor\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c #a\xC3\xA9\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c #+f0000\", \"a\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c black\", \"z\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 1 1\", \"a c black\", \"aa\" };"[..], + &b"/* XPM */\nstatic char *i[] = { \"1 1 2 1\", \"a c black\", \"a c white\", \"a\" };"[..], + ] { + assert!(decode_as(Format::Xpm, bytes).is_err(), "{:?}", bytes); + } + assert!(decode_as(Format::Xpm, b"/* XPM */\n\"4294967295 4294967295 1 1\"").is_err()); + assert!(decode_as(Format::Xpm, b"/* XPM */\n\"2 1 1 1\" \"a c black\" \"aa\" /* bad").is_err()); +} + +#[test] +fn x11_names_and_field_priority() { + let xpm = br#"/* XPM */ +static char *i[] = { +"4 1 4 1", +"a c LightGray", +"b c DarkBlue", +"c m black g white c light blue", +"d m black g4 red g green", +"abcd" +};"#; + let image = decode(xpm).unwrap(); + assert_eq!(image.data(), &[211, 211, 211, 255, 0, 0, 139, 255, 173, 216, 230, 255, 0, 255, 0, 255]); +} + +#[test] +fn named_x11_colors_use_x11_not_css_values() { + let bytes = br#"/* XPM */ +static char *icon[] = { +"6 1 6 1", +"a c gray", +"b c maroon", +"c c purple", +"d c LightGoldenrodYellow", +"e c DarkSlateGray4", +"f c Ghost White", +"abcdef" +};"#; + let image = decode(bytes).unwrap(); + assert_eq!(image.data(), &[190, 190, 190, 255, 176, 48, 96, 255, 160, 32, 240, 255, 250, 250, 210, 255, 82, 139, 139, 255, 248, 248, 255, 255,]); +} + +#[test] +fn xpm2_decodes_crlf_comments_spaces_and_multichar_keys() { + let bytes = b"! XPM2\r\n! exported by an icon tool\r\n2 2 3 2\r\n.a c #f00\r\n!_ c None\r\nzz c DarkBlue\r\n.a!_\r\nzz.a\r\n"; + assert_eq!(detect(bytes), Some(Format::Xpm)); + let image = decode(bytes).unwrap(); + assert_eq!(image.dimensions(), (2, 2)); + assert_eq!(image.data(), &[255, 0, 0, 255, 0, 0, 0, 0, 0, 0, 139, 255, 255, 0, 0, 255]); +} + +#[test] +fn xpm2_allows_exclamation_as_a_pixel_key_and_preserves_space_keys() { + let bytes = b"! XPM2\n2 1 2 1\n! c red\n c None\n! \n"; + let image = decode(bytes).unwrap(); + assert_eq!(image.data(), &[255, 0, 0, 255, 0, 0, 0, 0]); +} + +#[test] +fn xpm2_malformed_inputs_return_errors() { + for bytes in [ + &b"! XPM2\n1 1 1 1\na c black\n"[..], + &b"! XPM2\n1 1 1 1\na c black\nb\n"[..], + &b"! XPM2\n1 1 1 1\na c #a\xc3\xa9\na\n"[..], + &b"! XPM2 invalid\n1 1 1 1\na c black\na\n"[..], + &b"! XPM2\n100000 100000 1 1\na c red\na\n"[..], + ] { + assert!(decode_as(Format::Xpm, bytes).is_err(), "{bytes:?}"); + } + assert_ne!(detect(b"! XPM23\n1 1 1 1\na c black\na\n"), Some(Format::Xpm)); +} + +#[test] +fn xpm_is_import_only() { + assert!(caps(Format::Xpm).read); + assert!(!caps(Format::Xpm).write); + let image = Image::from_u8(1, 1, ChannelLayout::Rgba, vec![0, 0, 0, 0]).unwrap(); + assert!(matches!(encode(&image, Format::Xpm, &Default::default()), Err(CodecError::Unsupported { .. }))); +} diff --git a/crates/image-codecs/tests/jxl_export.rs b/crates/image-codecs/tests/jxl_export.rs new file mode 100644 index 000000000..61659f55a --- /dev/null +++ b/crates/image-codecs/tests/jxl_export.rs @@ -0,0 +1,110 @@ +//! Lossless JPEG XL export and honest export-only capability reporting. + +use craft_image_codecs::{ + ASYMMETRIC_EXCEPTIONS, ChannelLayout, CodecError, EncodeOptions, Format, Image, SampleType, caps, decode, detect, encode, from_extension, +}; + +#[test] +fn jpeg_xl_is_export_only_and_detects_both_signatures() { + let c = caps(Format::Jxl); + assert!(c.write && !c.read); + assert_eq!(c.depths, &[SampleType::U8, SampleType::U16]); + assert_eq!(c.layouts, &[ChannelLayout::Gray, ChannelLayout::Rgb, ChannelLayout::Rgba]); + assert!(c.alpha && !c.icc && !c.exif && !c.xmp && !c.dpi && !c.lossy); + assert_eq!(Format::Jxl.mime_type(), "image/jxl"); + assert_eq!(from_extension("image.JXL"), Some(Format::Jxl)); + assert_eq!(detect(&[0xff, 0x0a, 0, 0]), Some(Format::Jxl)); + assert_eq!(detect(b"\0\0\0\x0cJXL \r\n\x87\n"), Some(Format::Jxl)); + assert!(ASYMMETRIC_EXCEPTIONS.iter().any(|(format, reason)| *format == Format::Jxl && !reason.is_empty())); + let err = decode(&[0xff, 0x0a]); + assert!(matches!(err, Err(CodecError::Unsupported { format: Format::Jxl, .. })), "{err:?}"); +} + +#[test] +fn jpeg_xl_encoding_is_deterministic_and_converts_float_to_integer() { + let input = + Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]).unwrap(); + let opts = EncodeOptions::default(); + let one = encode(&input, Format::Jxl, &opts).unwrap(); + let two = encode(&input, Format::Jxl, &opts).unwrap(); + assert_eq!(one, two); + assert_eq!(detect(&one), Some(Format::Jxl)); + let float = Image::from_f32(2, 2, ChannelLayout::Rgb, &[0.0, 0.5, 1.0, 0.25, 0.5, 0.75, 0.75, 0.5, 0.25, 1.0, 0.0, 0.5]).unwrap(); + let bytes = encode(&float, Format::Jxl, &opts).unwrap(); + assert_eq!(detect(&bytes), Some(Format::Jxl)); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn jpeg_xl_decodes_losslessly_with_independent_decoder() { + use jxl_oxide::JxlImage; + + for layout in [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba] { + let channels = layout.channels(); + for sample in [SampleType::U8, SampleType::U16] { + let (w, h) = (7, 5); + let count = w as usize * h as usize * channels; + let expected = match sample { + SampleType::U8 => (0..count).map(|i| (((i * 37 + 19) % 256) as u16) * 257).collect::>(), + SampleType::U16 => (0..count).map(|i| ((i * 9973 + 27) % 65536) as u16).collect::>(), + _ => unreachable!(), + }; + let input = match sample { + SampleType::U8 => { + let data = expected.iter().map(|v| (v / 257) as u8).collect(); + Image::from_u8(w, h, layout, data).unwrap() + } + SampleType::U16 => Image::from_u16(w, h, layout, &expected).unwrap(), + _ => unreachable!(), + }; + // GrayA is losslessly widened to RGBA. Keep the oracle comparison + // against original gray and alpha samples, not only against a converted image. + let (output_channels, expected_output) = if layout == ChannelLayout::GrayA { + let expanded: Vec = expected.as_chunks::<2>().0.iter().flat_map(|[gray, alpha]| [*gray, *gray, *gray, *alpha]).collect(); + (4, expanded) + } else { + (channels, expected.clone()) + }; + let encoded = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap(); + let decoder = JxlImage::builder().read(&encoded[..]).unwrap(); + assert_eq!((decoder.width(), decoder.height()), (w, h)); + let render = decoder.render_frame(0).unwrap(); + let mut stream = render.stream(); + assert_eq!(stream.channels() as usize, output_channels, "{layout:?} {sample:?}"); + let mut output = vec![0u16; expected_output.len()]; + assert_eq!(stream.write_to_buffer(&mut output), expected_output.len()); + assert_eq!(output, expected_output, "{layout:?} {sample:?}"); + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn jpeg_xl_preserves_sixteen_bit_alpha_across_dimension_header_sizes() { + use jxl_oxide::JxlImage; + + // Exercise each size-header coding tier, and non-opaque alpha extremes. + for width in [2u32, 513, 8193] { + let height = 2u32; + let mut samples = Vec::with_capacity(width as usize * height as usize * 4); + for i in 0..(width as usize * height as usize) { + samples.extend_from_slice(&[(i as u16).wrapping_mul(127), 65535, (i as u16).wrapping_mul(9973), [0u16, 1, 32768, 65535][i % 4]]); + } + let input = Image::from_u16(width, height, ChannelLayout::Rgba, &samples).unwrap(); + let encoded = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap(); + let decoder = JxlImage::builder().read(&encoded[..]).unwrap(); + let render = decoder.render_frame(0).unwrap(); + let mut stream = render.stream(); + assert_eq!(stream.channels(), 4, "width={width}"); + let mut output = vec![0u16; samples.len()]; + assert_eq!(stream.write_to_buffer(&mut output), samples.len()); + assert_eq!(output, samples, "width={width}"); + } +} + +#[test] +fn jpeg_xl_rejects_one_pixel_images_without_panicking() { + // zune-jpegxl currently requires at least two pixels on each axis. + let image = Image::from_u8(1, 1, ChannelLayout::Rgb, vec![42, 100, 255]).unwrap(); + assert!(matches!(encode(&image, Format::Jxl, &EncodeOptions::default()), Err(CodecError::Encode { format: Format::Jxl, .. }))); +} diff --git a/crates/image-codecs/tests/pnm_peak_memory.rs b/crates/image-codecs/tests/pnm_peak_memory.rs new file mode 100644 index 000000000..33bf39b3c --- /dev/null +++ b/crates/image-codecs/tests/pnm_peak_memory.rs @@ -0,0 +1,36 @@ +//! #2938: `Limits::max_alloc` budgets the decoded pixel buffer, so the PNM decoder must not build a +//! wider scratch copy (it used to collect one `u32` per sample, four times an 8-bit image) next to it. +//! +//! This file holds a single test so it runs in its own process: it reads the process's peak RSS +//! (`VmHWM`, Linux only) and nothing else may allocate meanwhile. + +#[cfg(target_os = "linux")] +fn peak_rss_bytes() -> Option { + let status = std::fs::read_to_string("/proc/self/status").ok()?; + let line = status.lines().find(|l| l.starts_with("VmHWM:"))?; + let kib: u64 = line.trim_start_matches("VmHWM:").trim().trim_end_matches("kB").trim().parse().ok()?; + Some(kib * 1024) +} + +#[cfg(target_os = "linux")] +#[test] +fn pnm_decode_peak_memory_stays_near_the_output_size() { + use craft_image_codecs::*; + + const SIDE: u32 = 4096; // 16 MiB of 8-bit gray output + let out_bytes = (SIDE * SIDE) as u64; + let mut file = format!("P5\n{SIDE} {SIDE}\n255\n").into_bytes(); + file.extend((0..out_bytes).map(|i| (i % 251) as u8)); + // Writing 5 resets the peak RSS to the current RSS; skip where the kernel doesn't allow it. + if std::fs::write("/proc/self/clear_refs", "5").is_err() { + return; + } + let Some(before) = peak_rss_bytes() else { return }; + let img = decode(&file).unwrap(); + let after = peak_rss_bytes().unwrap(); + assert_eq!(img.data().len() as u64, out_bytes); + assert_eq!(img.data()[300], (300 % 251) as u8); + // The output buffer plus slack; a `u32`-per-sample scratch copy alone would be 4× it. + let grown = after.saturating_sub(before); + assert!(grown < 2 * out_bytes, "decoding a {out_bytes} byte image raised peak RSS by {grown} bytes"); +} diff --git a/crates/raster-codecs/Cargo.toml b/crates/raster-codecs/Cargo.toml index cadf9cbd0..e8d6bc3ba 100644 --- a/crates/raster-codecs/Cargo.toml +++ b/crates/raster-codecs/Cargo.toml @@ -11,8 +11,9 @@ repository.workspace = true workspace = true [dependencies] +craft-image-codecs = { workspace = true } thiserror = { workspace = true } -image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp", "gif", "tiff", "webp"] } +image = { version = "0.25", default-features = false } # The DDS encoder compresses rows of blocks in parallel on native; wasm stays single-threaded. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/raster-codecs/README.md b/crates/raster-codecs/README.md index b69ca2817..6735769ae 100644 --- a/crates/raster-codecs/README.md +++ b/crates/raster-codecs/README.md @@ -3,8 +3,10 @@ Small raster image formats for the Craft apps, from PhotoCraft's `photocraft-codecs`: one module per format, one implementation of each. Decoders take untrusted bytes and `Limits`, check the declared size before allocating, and never panic. No `unsafe`; it builds for -`wasm32-unknown-unknown`. Dependencies: `thiserror`, and `rayon` on native targets (the DDS -encoder compresses rows of blocks in parallel). +`wasm32-unknown-unknown`. The `common` RGBA image facade delegates PNG/JPEG/BMP/GIF/TIFF/WebP +decoding and PNG/JPEG encoding to [`craft-image-codecs`](../image-codecs/README.md), the shared +depth- and metadata-preserving codec implementation. Other dependencies are `thiserror`, and +`rayon` on native targets (the DDS encoder compresses rows of blocks in parallel). ```toml craft-raster-codecs = { git = "https://github.com/storytold/craft-libs", rev = "" } diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index da3c1c39d..ec61aeedd 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -4,9 +4,7 @@ //! the source was PNG, JPEG, BMP, GIF, TIFF, or WebP. Format sniffing and codec dispatch stay //! inside this crate. Callers choose their own stricter file and image limits. -use std::io::Cursor; - -use image::{ImageReader, Limits as ImageLimits, RgbaImage}; +use image::RgbaImage; use crate::CodecError; @@ -33,33 +31,24 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result limits.max_file_bytes { return Err(CodecError::LimitExceeded(format!("{} encoded bytes exceed max {}", bytes.len(), limits.max_file_bytes))); } - if is_heif(bytes) { - return Err(CodecError::unsupported("image", "HEIC/HEIF is not supported by the enabled codecs")); + let mut decode_limits = craft_image_codecs::Limits::default(); + decode_limits.max_width = limits.max_width; + decode_limits.max_height = limits.max_height; + decode_limits.max_pixels = limits.max_pixels; + decode_limits.max_alloc = limits.max_alloc_bytes; + let decoded = craft_image_codecs::decode_with(bytes, &craft_image_codecs::DecodeOptions { limits: decode_limits, ..Default::default() }) + .map_err(map_codec_error)?; + let output_bytes = u64::from(decoded.width()).saturating_mul(u64::from(decoded.height())).saturating_mul(4); + if output_bytes > limits.max_alloc_bytes { + return Err(CodecError::LimitExceeded(format!("{output_bytes} RGBA bytes exceed max {}", limits.max_alloc_bytes))); } - let reader = || -> Result>, CodecError> { - let mut reader = ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(|e| CodecError::malformed("image", e))?; - if reader.format().is_none() { - return Err(CodecError::unsupported("image", "unrecognised image format")); - } - let mut image_limits = ImageLimits::default(); - image_limits.max_image_width = Some(limits.max_width); - image_limits.max_image_height = Some(limits.max_height); - image_limits.max_alloc = Some(limits.max_alloc_bytes); - reader.limits(image_limits); - Ok(reader) - }; - let (width, height) = reader()?.into_dimensions().map_err(|e| CodecError::malformed("image", e))?; - check_dimensions(width, height, limits)?; - let decoded = reader()?.decode().map_err(|e| CodecError::malformed("image", e))?; - check_dimensions(decoded.width(), decoded.height(), limits)?; - Ok(decoded.to_rgba8()) + RgbaImage::from_raw(decoded.width(), decoded.height(), decoded.to_rgba8()) + .ok_or_else(|| CodecError::malformed("image", "decoded RGBA dimensions do not match its pixel buffer")) } /// Encode an RGBA image as PNG. pub fn encode_png(pixels: &RgbaImage) -> Result, CodecError> { - let mut out = Cursor::new(Vec::new()); - pixels.write_to(&mut out, image::ImageFormat::Png).map_err(|e| CodecError::encode("PNG", e))?; - Ok(out.into_inner()) + encode(pixels, craft_image_codecs::Format::Png, craft_image_codecs::EncodeOptions::default()) } /// Encode RGBA pixels as JPEG at the requested quality (1–100). @@ -67,30 +56,32 @@ pub fn encode_jpeg(pixels: &RgbaImage, quality: u8) -> Result, CodecErro if !(1..=100).contains(&quality) { return Err(CodecError::InvalidImage(format!("JPEG quality {quality} is outside 1–100"))); } - let rgb = image::DynamicImage::ImageRgba8(pixels.clone()).to_rgb8(); - let mut out = Cursor::new(Vec::new()); - image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, quality).encode_image(&rgb).map_err(|e| CodecError::encode("JPEG", e))?; - Ok(out.into_inner()) + encode(pixels, craft_image_codecs::Format::Jpeg, craft_image_codecs::EncodeOptions { jpeg_quality: quality, ..Default::default() }) +} + +fn encode(pixels: &RgbaImage, format: craft_image_codecs::Format, options: craft_image_codecs::EncodeOptions) -> Result, CodecError> { + let image = craft_image_codecs::Image::from_u8(pixels.width(), pixels.height(), craft_image_codecs::ChannelLayout::Rgba, pixels.as_raw().clone()) + .map_err(map_codec_error)?; + craft_image_codecs::encode(&image, format, &options).map_err(map_codec_error) } /// Encode pixels according to a destination filename's extension (`png`, `jpg`, or `jpeg`). pub fn encode_for_path(path: impl AsRef, pixels: &RgbaImage) -> Result, CodecError> { let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str).unwrap_or_default(); - match extension.to_ascii_lowercase().as_str() { - "png" => encode_png(pixels), - "jpg" | "jpeg" => encode_jpeg(pixels, 90), - other => Err(CodecError::unsupported("image", format!("cannot encode files with the .{other} extension"))), + match craft_image_codecs::from_extension(extension) { + Some(craft_image_codecs::Format::Png) => encode_png(pixels), + Some(craft_image_codecs::Format::Jpeg) => encode_jpeg(pixels, 90), + _ => Err(CodecError::unsupported("image", format!("cannot encode files with the .{extension} extension"))), } } -fn is_heif(bytes: &[u8]) -> bool { - bytes.get(4..8) == Some(b"ftyp") && matches!(bytes.get(8..12), Some(b"heic" | b"heix" | b"hevc" | b"mif1" | b"msf1" | b"avif")) -} - -fn check_dimensions(width: u32, height: u32, limits: DecodeLimits) -> Result<(), CodecError> { - let pixels = u64::from(width).checked_mul(u64::from(height)).ok_or_else(|| CodecError::LimitExceeded("image dimensions overflow".into()))?; - if width == 0 || height == 0 || width > limits.max_width || height > limits.max_height || pixels > limits.max_pixels { - return Err(CodecError::LimitExceeded(format!("image dimensions {width}x{height} exceed configured limits"))); +fn map_codec_error(error: craft_image_codecs::CodecError) -> CodecError { + match error { + craft_image_codecs::CodecError::UnknownFormat => CodecError::unsupported("image", "unrecognized image format"), + craft_image_codecs::CodecError::Unsupported { format, reason } => CodecError::unsupported(format.name(), reason), + craft_image_codecs::CodecError::Malformed { format, message } => CodecError::malformed(format.name(), message), + craft_image_codecs::CodecError::LimitExceeded(message) => CodecError::LimitExceeded(message), + craft_image_codecs::CodecError::Encode { format, message } => CodecError::Encode { format: format.name(), message }, + craft_image_codecs::CodecError::InvalidImage(message) => CodecError::InvalidImage(message), } - Ok(()) } From 3bdf0eaadd2c20402413ae76038928fb15ca9670 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:17:35 -0500 Subject: [PATCH 02/15] Format shared image codec crate --- crates/image-codecs/src/codecs/dds.rs | 5 +- crates/image-codecs/src/codecs/dds_bc.rs | 3 +- crates/image-codecs/src/codecs/deep_exr.rs | 55 ++++++++++-------- crates/image-codecs/src/codecs/dicom.rs | 16 +++++- crates/image-codecs/src/codecs/dpx.rs | 15 +++-- crates/image-codecs/src/codecs/exr.rs | 22 ++++++-- .../src/codecs/exr_cryptomatte.rs | 6 +- crates/image-codecs/src/codecs/fits.rs | 12 ++-- crates/image-codecs/src/codecs/heif.rs | 12 ++-- crates/image-codecs/src/codecs/iff.rs | 9 ++- crates/image-codecs/src/codecs/jxl.rs | 6 +- crates/image-codecs/src/codecs/png.rs | 56 +++++++++++++++---- crates/image-codecs/src/codecs/pnm.rs | 4 +- crates/image-codecs/src/codecs/tiff/decode.rs | 14 +++-- crates/image-codecs/src/codecs/tiff/encode.rs | 6 +- crates/image-codecs/src/codecs/tiff_exif.rs | 3 +- crates/image-codecs/src/codecs/via_image.rs | 4 +- crates/image-codecs/src/codecs/vp8.rs | 6 +- crates/image-codecs/src/codecs/vp8/tables.rs | 26 +++++---- crates/image-codecs/src/codecs/webp.rs | 3 +- crates/image-codecs/src/exr_stream.rs | 9 ++- crates/image-codecs/src/fidelity.rs | 4 +- crates/image-codecs/src/format.rs | 25 +++++++-- crates/image-codecs/src/image.rs | 47 ++++++++++++---- crates/image-codecs/src/lib.rs | 13 +++-- crates/image-codecs/src/orientation.rs | 6 +- crates/image-codecs/src/source_settings.rs | 10 +++- crates/image-codecs/tests/it/dds_write.rs | 15 +++-- .../image-codecs/tests/it/decode_warnings.rs | 3 +- crates/image-codecs/tests/it/deep_exr.rs | 29 +++++++--- crates/image-codecs/tests/it/dpx.rs | 10 +++- .../image-codecs/tests/it/exr_cryptomatte.rs | 12 +++- crates/image-codecs/tests/it/exr_multipart.rs | 25 ++++++--- crates/image-codecs/tests/it/exr_stream.rs | 8 ++- crates/image-codecs/tests/it/fidelity.rs | 18 ++++-- crates/image-codecs/tests/it/limits.rs | 7 ++- crates/image-codecs/tests/it/metadata.rs | 13 ++++- crates/image-codecs/tests/it/orientation.rs | 10 +++- crates/image-codecs/tests/it/pcx.rs | 3 +- crates/image-codecs/tests/it/png_jpeg.rs | 3 +- crates/image-codecs/tests/it/resolution.rs | 9 ++- crates/image-codecs/tests/it/roundtrip.rs | 6 +- .../image-codecs/tests/it/source_settings.rs | 4 +- crates/image-codecs/tests/it/tiff_bands.rs | 17 +++--- crates/image-codecs/tests/it/tiff_bench.rs | 8 ++- crates/image-codecs/tests/it/tiff_exif.rs | 15 ++++- crates/image-codecs/tests/it/tiff_pages.rs | 12 +++- .../image-codecs/tests/it/tiff_photoshop.rs | 3 +- crates/image-codecs/tests/it/tiff_pyramid.rs | 16 +++++- crates/image-codecs/tests/it/tiff_ycbcr.rs | 21 ++++--- crates/image-codecs/tests/it/webp_frames.rs | 4 +- crates/image-codecs/tests/jxl_export.rs | 3 +- 52 files changed, 474 insertions(+), 197 deletions(-) diff --git a/crates/image-codecs/src/codecs/dds.rs b/crates/image-codecs/src/codecs/dds.rs index 1d220e6ac..9ed4e3f35 100644 --- a/crates/image-codecs/src/codecs/dds.rs +++ b/crates/image-codecs/src/codecs/dds.rs @@ -437,8 +437,9 @@ fn header(width: u32, height: u32, store: Store, levels: u32, top: usize) -> Res }; // Uncompressed: the pitch of one row. Compressed: the whole top level (its "linear size"). let (size_flag, pitch_or_size) = if store.is_block() { (DDSD_LINEARSIZE, Some(top)) } else { (DDSD_PITCH, store.row_bytes(width as usize)) }; - let pitch_or_size = - pitch_or_size.and_then(|v| u32::try_from(v).ok()).ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; + let pitch_or_size = pitch_or_size + .and_then(|v| u32::try_from(v).ok()) + .ok_or_else(|| encode_err(format!("a {width}x{height} level is too large for a DDS header")))?; let mipmapped = levels > 1; put(4, 124); put(8, DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | size_flag | if mipmapped { DDSD_MIPMAPCOUNT } else { 0 }); diff --git a/crates/image-codecs/src/codecs/dds_bc.rs b/crates/image-codecs/src/codecs/dds_bc.rs index bbd55b4bf..df60e462c 100644 --- a/crates/image-codecs/src/codecs/dds_bc.rs +++ b/crates/image-codecs/src/codecs/dds_bc.rs @@ -252,7 +252,8 @@ fn least_squares(block: &Block, opaque: Mask, bytes: &[u8; 8]) -> Option<(u16, u /// between the extremes other than 0 and 255, plus exact 0 and 255) fits better. fn alpha_block(block: &Block, inside: Mask) -> [u8; 8] { let alphas = || block.iter().enumerate().filter(move |(t, _)| inside >> t & 1 != 0).map(|(_, texel)| texel[3]); - let range = |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); + let range = + |values: &mut dyn Iterator| values.fold(None, |r: Option<(u8, u8)>, a| Some(r.map_or((a, a), |(lo, hi)| (lo.min(a), hi.max(a))))); let (lo, hi) = range(&mut alphas()).unwrap_or((0, 0)); // Eight values need a0 > a1; equal ends fall to the six-value mode, which starts with a0 too. let eight = alpha_fit(block, inside, hi, lo); diff --git a/crates/image-codecs/src/codecs/deep_exr.rs b/crates/image-codecs/src/codecs/deep_exr.rs index 0652112bf..0cc69e261 100644 --- a/crates/image-codecs/src/codecs/deep_exr.rs +++ b/crates/image-codecs/src/codecs/deep_exr.rs @@ -53,7 +53,12 @@ fn read_exact<'a>(cur: &mut Cursor<&'a [u8]>, n: usize, what: &str) -> Result<&' fn i32(cur: &mut Cursor<&[u8]>) -> Result { let b = read_exact(cur, 4, "an i32")?; - Ok(i32::from_le_bytes([b.first().copied().unwrap_or(0), b.get(1).copied().unwrap_or(0), b.get(2).copied().unwrap_or(0), b.get(3).copied().unwrap_or(0)])) + Ok(i32::from_le_bytes([ + b.first().copied().unwrap_or(0), + b.get(1).copied().unwrap_or(0), + b.get(2).copied().unwrap_or(0), + b.get(3).copied().unwrap_or(0), + ])) } fn u64(cur: &mut Cursor<&[u8]>) -> Result { @@ -125,7 +130,8 @@ fn reserve_up_to(out: &mut Vec, extra: usize, limit: usize, what: &str) -> R } if need > out.capacity() { let target = out.capacity().saturating_mul(2).max(need).min(limit); - out.try_reserve_exact(target.saturating_sub(out.len())).map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; + out.try_reserve_exact(target.saturating_sub(out.len())) + .map_err(|_| CodecError::LimitExceeded(format!("cannot allocate {target} bytes for {what}")))?; } Ok(()) } @@ -344,30 +350,31 @@ pub(crate) fn decode_deep(meta: &MetaData, bytes: &[u8], limits: &Limits) -> Res // Block coordinates → the block's pixel bounds and its slot. The per-pixel index is // built only after the chunk's declared payload sizes are validated below, so a // truncated or lying chunk cannot drive a large index allocation first. - let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = match &header.blocks { - BlockDescription::ScanLines => { - let y = i32(&mut cur)?; - let row = match y.checked_sub(y0) { - Some(d) if d >= 0 && (d as usize) < h => d as usize, - _ => return Err(err(format!("scan line block y={y} is outside the data window"))), - }; - (row..row + 1, 0..w, row, w, true) - } - BlockDescription::Tiles(_) => { - let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); - if (lx, ly) != (0, 0) { - return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + let (block_rows, block_cols, slot, row_w, scanline_row): (std::ops::Range, std::ops::Range, usize, usize, bool) = + match &header.blocks { + BlockDescription::ScanLines => { + let y = i32(&mut cur)?; + let row = match y.checked_sub(y0) { + Some(d) if d >= 0 && (d as usize) < h => d as usize, + _ => return Err(err(format!("scan line block y={y} is outside the data window"))), + }; + (row..row + 1, 0..w, row, w, true) } - if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { - return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + BlockDescription::Tiles(_) => { + let (tx, ty, lx, ly) = (i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?, i32(&mut cur)?); + if (lx, ly) != (0, 0) { + return Err(err(format!("deep tile at level ({lx},{ly}); only level 0 exists in a single-level file"))); + } + if tx < 0 || ty < 0 || (tx as usize) >= tiles_x || (ty as usize) * tile_h >= h { + return Err(err(format!("tile ({tx},{ty}) is outside the data window"))); + } + let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); + let rows = (by + tile_h).min(h); + let cols = (bx + tile_w).min(w); + let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; + (by..rows, bx..cols, slot, cols - bx, false) } - let (bx, by) = ((tx as usize) * tile_w, (ty as usize) * tile_h); - let rows = (by + tile_h).min(h); - let cols = (bx + tile_w).min(w); - let slot = (ty as usize).saturating_mul(tiles_x) + tx as usize; - (by..rows, bx..cols, slot, cols - bx, false) - } - }; + }; let block_len = block_rows.len().saturating_mul(block_cols.len()); if seen.get(slot).copied().unwrap_or(true) { return Err(err(format!("two chunks cover block {slot}"))); diff --git a/crates/image-codecs/src/codecs/dicom.rs b/crates/image-codecs/src/codecs/dicom.rs index ea2674dae..912d8672c 100644 --- a/crates/image-codecs/src/codecs/dicom.rs +++ b/crates/image-codecs/src/codecs/dicom.rs @@ -254,7 +254,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { .get(pixel.start..pixel.start.checked_add(need).ok_or_else(|| CodecError::LimitExceeded("pixel data too large".into()))?) .ok_or_else(|| bad("pixel data is truncated"))?; let out_channels = layout.channels(); - let out_len = pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; + let out_len = + pixels.checked_mul(out_channels).and_then(|k| k.checked_mul(bytes_per)).ok_or_else(|| CodecError::LimitExceeded("image too large".into()))?; let mut out = Vec::new(); out.try_reserve_exact(out_len).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; // Raw sample of channel `ch` at pixel `p`, honouring the planar configuration. @@ -367,7 +368,18 @@ mod tests { /// A DICOM with the given transfer syntax UID and data-set encoding. #[allow(clippy::too_many_arguments)] - fn synth(ts: &str, explicit: bool, big: bool, planar: u16, rows: u16, cols: u16, samples: u16, bits: u16, photometric: &str, pixels: &[u8]) -> Vec { + fn synth( + ts: &str, + explicit: bool, + big: bool, + planar: u16, + rows: u16, + cols: u16, + samples: u16, + bits: u16, + photometric: &str, + pixels: &[u8], + ) -> Vec { let mut b = vec![0u8; 128]; b.extend_from_slice(b"DICM"); // One extra meta element before the syntax, so a reader that only looks at the last diff --git a/crates/image-codecs/src/codecs/dpx.rs b/crates/image-codecs/src/codecs/dpx.rs index 86e4938b0..39be0e255 100644 --- a/crates/image-codecs/src/codecs/dpx.rs +++ b/crates/image-codecs/src/codecs/dpx.rs @@ -382,7 +382,8 @@ pub(crate) fn decode_cineon(bytes: &[u8], limits: &Limits) -> Result Result, CodecErr return Err(enc("the image has the wrong number of samples".into())); } let data_len = line_bytes(per_line, 10, 1).and_then(|l| l.checked_mul(h as usize)).ok_or_else(|| enc("image too large".into()))?; - let total = - CINEON_HEADER_LEN.checked_add(data_len).and_then(|t| u32::try_from(t).ok()).ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; + let total = CINEON_HEADER_LEN + .checked_add(data_len) + .and_then(|t| u32::try_from(t).ok()) + .ok_or_else(|| enc("image too large for a Cineon file (4 GiB)".into()))?; let mut wr = Writer { h: vec![0; CINEON_HEADER_LEN] }; // File information. @@ -731,8 +734,10 @@ mod tests { /// A minimal header for `w`×`h` with element 0 set; image data at 2048. fn header(o: Order, w: u32, h: u32, descriptor: u8, bits: u8, packing: u16) -> Vec { let mut b = vec![0u8; HEADER_LEN]; - let put32 = |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); - let put16 = |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put32 = + |b: &mut Vec, at: usize, v: u32| b[at..at + 4].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); + let put16 = + |b: &mut Vec, at: usize, v: u16| b[at..at + 2].copy_from_slice(&if o == Order::Big { v.to_be_bytes() } else { v.to_le_bytes() }); b[..4].copy_from_slice(if o == Order::Big { b"SDPX" } else { b"XPDS" }); put32(&mut b, 4, HEADER_LEN as u32); put32(&mut b, 28, INDUSTRY_LEN); diff --git a/crates/image-codecs/src/codecs/exr.rs b/crates/image-codecs/src/codecs/exr.rs index 0d68d20ca..04019bcdc 100644 --- a/crates/image-codecs/src/codecs/exr.rs +++ b/crates/image-codecs/src/codecs/exr.rs @@ -8,7 +8,8 @@ use std::io::Cursor; use exr::meta::MetaData; use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, read, + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Layer, LayerAttributes, LineOrder, ReadChannels, ReadLayers, WritableImage, + read, }; use half::f16; @@ -71,8 +72,10 @@ pub(crate) fn check_previews(b: &[u8]) -> Result<(), CodecError> { _ => return Ok(()), }; if kind == b"preview" { - let declared = - u32_at(value).zip(u32_at(value + 4)).and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)).filter(|_| size >= 8); + let declared = u32_at(value) + .zip(u32_at(value + 4)) + .and_then(|(w, h)| u64::from(w).checked_mul(u64::from(h))?.checked_mul(4)) + .filter(|_| size >= 8); if declared.is_none_or(|n| n > (size - 8) as u64) { return Err(err("a preview attribute declares more pixels than it holds")); } @@ -201,7 +204,8 @@ fn layer_to_image(layer: &Layer>) -> Result = channels.iter().map(|c| c.name.to_string()).collect(); - let (layout, selected) = exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; + let (layout, selected) = + exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| CodecError::unsupported(F, "no R/G/B or Y channels"))?; let color: Vec<_> = selected.iter().map(|i| channels.get(*i).ok_or_else(|| err("selected channel is missing"))).collect::>()?; let all_f16 = color.iter().all(|c| matches!(c.sample_data, FlatSamples::F16(_))); let sample = if all_f16 { SampleType::F16 } else { SampleType::F32 }; @@ -273,7 +277,10 @@ pub(crate) fn probe(bytes: &[u8], limits: &Limits) -> Result<(u32, u32), CodecEr pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result { let meta = read_meta(bytes, limits)?; if meta.requirements.has_deep_data { - return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + return Err(CodecError::unsupported( + F, + "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)", + )); } if index >= meta.headers.len() { return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); @@ -287,7 +294,10 @@ pub(crate) fn decode_part(bytes: &[u8], index: usize, limits: &Limits) -> Result pub(crate) fn decode_channels(bytes: &[u8], index: usize, names: &[&str], limits: &Limits) -> Result { let meta = read_meta(bytes, limits)?; if meta.requirements.has_deep_data { - return Err(CodecError::unsupported(F, "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)")); + return Err(CodecError::unsupported( + F, + "this file has deep parts; the deep decoder composites those (mixing flat and deep parts is not supported)", + )); } if index >= meta.headers.len() { return Err(err(format!("part index {index} is out of range ({} parts)", meta.headers.len()))); diff --git a/crates/image-codecs/src/codecs/exr_cryptomatte.rs b/crates/image-codecs/src/codecs/exr_cryptomatte.rs index 62fd2e1e0..f942231da 100644 --- a/crates/image-codecs/src/codecs/exr_cryptomatte.rs +++ b/crates/image-codecs/src/codecs/exr_cryptomatte.rs @@ -231,8 +231,10 @@ pub(crate) fn decode(bytes: &[u8], layer_name: &str, limits: &Limits) -> Result< // returned per-pixel vectors could exceed the caller's max_alloc budget. let ranks = layer.channels.len().saturating_mul(2); let pair_capacity = (ranks as u64).saturating_mul(std::mem::size_of::<(f32, f32)>() as u64); - let per_pixel = - (layer.channels.len().max(1) as u64).saturating_mul(16).saturating_add(std::mem::size_of::>() as u64).saturating_add(pair_capacity); + let per_pixel = (layer.channels.len().max(1) as u64) + .saturating_mul(16) + .saturating_add(std::mem::size_of::>() as u64) + .saturating_add(pair_capacity); limits.check_bytes(w32, h32, per_pixel)?; let ex_layer = super::exr::read_layer(bytes, layer.part)?; diff --git a/crates/image-codecs/src/codecs/fits.rs b/crates/image-codecs/src/codecs/fits.rs index ae6e94505..67d37ca3e 100644 --- a/crates/image-codecs/src/codecs/fits.rs +++ b/crates/image-codecs/src/codecs/fits.rs @@ -39,8 +39,8 @@ const MAX_HDUS: usize = 1000; /// Keywords that describe the data layout. They are written from the image, never copied from /// (or kept as) text metadata: after decoding they no longer describe the pixels. const STRUCTURAL: &[&str] = &[ - "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", "DATAMIN", - "DATAMAX", "ZIMAGE", + "SIMPLE", "BITPIX", "NAXIS", "EXTEND", "BZERO", "BSCALE", "BLANK", "END", "XTENSION", "PCOUNT", "GCOUNT", "ROWORDER", "CHECKSUM", "DATASUM", + "DATAMIN", "DATAMAX", "ZIMAGE", ]; fn err(msg: impl std::fmt::Display) -> CodecError { @@ -192,7 +192,9 @@ fn axes(h: &Header) -> Result, CodecError> { if !(0..=999).contains(&n) { return Err(err(format!("invalid NAXIS {n}"))); } - (1..=n).map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))).collect() + (1..=n) + .map(|i| h.int(&format!("NAXIS{i}")).and_then(|v| u64::try_from(v).ok()).ok_or_else(|| err(format!("missing or negative NAXIS{i}")))) + .collect() } /// Bytes in the data unit (before padding to a block). @@ -433,7 +435,9 @@ fn ascii(s: &str) -> String { /// A FITS literal (number or logical) that can be written unquoted. fn literal(v: &str) -> bool { - v == "T" || v == "F" || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) + v == "T" + || v == "F" + || (v.replace(['D', 'd'], "E").parse::().is_ok_and(|f| f.is_finite()) && v.bytes().all(|b| b"0123456789+-.EeDd".contains(&b))) } fn quoted(v: &str, room: usize) -> String { diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs index 4fde27cf1..775891ed8 100644 --- a/crates/image-codecs/src/codecs/heif.rs +++ b/crates/image-codecs/src/codecs/heif.rs @@ -92,7 +92,10 @@ mod tests { let header = b"\0\0\0\x18ftypheic\0\0\0\0mif1heic"; assert_eq!(crate::detect(header), Some(F), "still recognised"); let r = crate::decode(header); - assert!(matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), "{r:?}"); + assert!( + matches!(&r, Err(CodecError::Unsupported { format: Format::Heif, reason }) if reason.contains("isn't included in this build")), + "{r:?}" + ); assert!(!crate::caps(F).read); } @@ -109,9 +112,10 @@ mod tests { /// (`boxes.rs:130`, "slice index starts at 24 but ends at 16"). It must be an error, not a crash. #[cfg(feature = "heif")] const HEIC_RS_BOX_PANIC: [u8; 72] = [ - 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, 0x72, 0x6d, - 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, 0x00, 0xa8, 0x00, + 0x00, 0x00, 0x00, 0x24, 0x66, 0x74, 0x79, 0x70, 0x68, 0x65, 0x69, 0x63, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x69, 0x66, 0x31, 0x4d, 0x69, 0x50, + 0x72, 0x6d, 0x69, 0x61, 0x66, 0x4d, 0x69, 0x48, 0x42, 0x72, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x74, 0x61, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x75, 0x75, 0x69, 0x64, 0x00, 0x00, 0x00, 0x00, 0x03, 0x08, 0x08, 0x08, 0x00, 0x03, 0x01, 0x03, 0x70, + 0x00, 0xa8, 0x00, ]; #[cfg(feature = "heif")] diff --git a/crates/image-codecs/src/codecs/iff.rs b/crates/image-codecs/src/codecs/iff.rs index 4997817ca..e006b3c11 100644 --- a/crates/image-codecs/src/codecs/iff.rs +++ b/crates/image-codecs/src/codecs/iff.rs @@ -190,7 +190,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { for row in stored.chunks_mut(plane_row.max(1)).take(total_rows) { match h.compression { 0 => { - let src = body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; + let src = + body.get(cursor..cursor.checked_add(plane_row).ok_or_else(|| bad("BODY overflow"))?).ok_or_else(|| bad("truncated BODY data"))?; let n = row.len().min(src.len()); row[..n].copy_from_slice(&src[..n]); cursor += plane_row; @@ -203,7 +204,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { } let mut out = Vec::new(); - out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())).map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; + out.try_reserve_exact(w.saturating_mul(hh).saturating_mul(layout.channels())) + .map_err(|_| CodecError::LimitExceeded("cannot allocate the image".into()))?; let planes = usize::from(h.planes); for y in 0..hh { for x in 0..w { @@ -215,7 +217,8 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result { let c = if h.planes == DEEP_PLANES { [index as u8, (index >> 8) as u8, (index >> 16) as u8] } else { palette(index) }; out.extend_from_slice(&c); if h.masking != 0 { - let opaque = if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; + let opaque = + if h.masking == 1 { bit_at(&stored, (y * rows_planes + planes) * plane_row, x) } else { index != usize::from(h.transparent) }; out.push(if opaque { 255 } else { 0 }); } } diff --git a/crates/image-codecs/src/codecs/jxl.rs b/crates/image-codecs/src/codecs/jxl.rs index 11591c930..39dd287a8 100644 --- a/crates/image-codecs/src/codecs/jxl.rs +++ b/crates/image-codecs/src/codecs/jxl.rs @@ -141,7 +141,11 @@ pub(crate) fn encode(image: &Image, plan: Plan) -> Result, CodecError> { let options = ZuneOptions::new(width as usize, height as usize, space, depth); let mut output = Vec::new(); JxlSimpleEncoder::new(image.data(), options).encode(&mut output).map_err(|e| CodecError::encode(F, e.to_string()))?; - if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { correct_16_bit_alpha_header(&output, width, height) } else { Ok(output) } + if depth == BitDepth::Sixteen && image.layout() == ChannelLayout::Rgba { + correct_16_bit_alpha_header(&output, width, height) + } else { + Ok(output) + } })) .unwrap_or_else(|_| Err(CodecError::encode(F, "the JPEG XL encoder failed"))) } diff --git a/crates/image-codecs/src/codecs/png.rs b/crates/image-codecs/src/codecs/png.rs index d1ff5140c..cf061f49b 100644 --- a/crates/image-codecs/src/codecs/png.rs +++ b/crates/image-codecs/src/codecs/png.rs @@ -355,7 +355,9 @@ impl> PngDecodeRows { let at = held.checked_mul(self.line).ok_or_else(|| err("image too large"))?; let end = at.checked_add(self.line).ok_or_else(|| err("image too large"))?; if self.buf.len() < end { - self.buf.try_reserve_exact(end - self.buf.len()).map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; + self.buf + .try_reserve_exact(end - self.buf.len()) + .map_err(|_| CodecError::LimitExceeded("cannot allocate a group of rows".into()))?; self.buf.resize(end, 0); } let row = self.buf.get_mut(at..end).ok_or_else(|| err("invalid PNG row group"))?; @@ -990,7 +992,14 @@ fn inflate_text(mut p: PendingText, bytes: &[u8]) -> Result, utf8: &mut Option, over: &mut bool, bytes: &[u8], latin1: bool, limit: usize) -> Result { +fn append_checked( + out: &mut Vec, + utf8: &mut Option, + over: &mut bool, + bytes: &[u8], + latin1: bool, + limit: usize, +) -> Result { if let Some(check) = utf8.as_mut() && !check.feed(bytes) { @@ -1112,8 +1121,11 @@ fn start( && x > 0.0 && y > 0.0 { - info.pixel_dims = - Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + info.pixel_dims = Some(png::PixelDimensions { + xppu: (x / METERS_PER_INCH).round() as u32, + yppu: (y / METERS_PER_INCH).round() as u32, + unit: png::Unit::Meter, + }); } } let mut encoder = png::Encoder::with_info(out, info).map_err(|e| CodecError::encode(F, e))?; @@ -1136,7 +1148,9 @@ fn start( res.map_err(|e| CodecError::encode(F, e))?; } if let Some(xmp) = &meta.xmp { - encoder.add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()).map_err(|e| CodecError::encode(F, e))?; + encoder + .add_itxt_chunk(XMP_KEYWORD.into(), crate::resolution::export_xmp(xmp, meta.dpi).into_owned()) + .map_err(|e| CodecError::encode(F, e))?; } } let mut writer = encoder.write_header().map_err(|e| CodecError::encode(F, e))?; @@ -1195,7 +1209,18 @@ impl PngRows { } let plan = crate::fidelity::plan(first, F, opts); let writer = start(out, (width, height), (plan.layout, plan.sample), first.icc.as_ref(), &first.meta, opts)?; - Ok(Self { writer, plan, width, height, rows: 0, level: opts.png_compression, pending: Vec::new(), prev: Vec::new(), adler: 1, started: false }) + Ok(Self { + writer, + plan, + width, + height, + rows: 0, + level: opts.png_compression, + pending: Vec::new(), + prev: Vec::new(), + adler: 1, + started: false, + }) } /// Encodes the next rows of the image: `band` is as wide as the image, in any layout and @@ -1235,7 +1260,8 @@ impl PngRows { let parts = deflate_rows(rows, prev, stride, bpp, per_band, deflate_level(self.level), finish, encode_threads()) .ok_or_else(|| CodecError::encode(F, "a band of rows could not be compressed"))?; let mut zlib = Vec::new(); - zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6).map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; + zlib.try_reserve_exact(parts.iter().map(|p| p.z.len()).sum::() + 6) + .map_err(|_| CodecError::encode(F, "not enough memory for PNG data"))?; if !self.started { zlib.extend_from_slice(&zlib_header(self.level)); self.started = true; @@ -1266,7 +1292,8 @@ impl PngRows { /// Build the zlib stream for an Adam7-interlaced image (filter type 0). fn adam7_idat(data: &[u8], w: usize, h: usize, bpp: usize, level: PngCompression) -> Result, CodecError> { // (x0, y0, dx, dy) per pass. - const PASSES: [(usize, usize, usize, usize); 7] = [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; + const PASSES: [(usize, usize, usize, usize); 7] = + [(0, 0, 8, 8), (4, 0, 8, 8), (0, 4, 4, 8), (2, 0, 4, 4), (0, 2, 2, 4), (1, 0, 2, 2), (0, 1, 1, 2)]; let mut raw = Vec::new(); raw.try_reserve_exact(data.len() + h * 7).map_err(|_| CodecError::encode(F, "not enough memory for PNG rows"))?; for (x0, y0, dx, dy) in PASSES { @@ -1553,8 +1580,11 @@ pub fn encode_indexed( && x > 0.0 && y > 0.0 { - info.pixel_dims = - Some(png::PixelDimensions { xppu: (x / METERS_PER_INCH).round() as u32, yppu: (y / METERS_PER_INCH).round() as u32, unit: png::Unit::Meter }); + info.pixel_dims = Some(png::PixelDimensions { + xppu: (x / METERS_PER_INCH).round() as u32, + yppu: (y / METERS_PER_INCH).round() as u32, + unit: png::Unit::Meter, + }); } let mut out = Vec::new(); { @@ -1880,7 +1910,8 @@ mod parallel_tests { for (layout, sample, ch, bytes) in [(ChannelLayout::Rgb, SampleType::U8, 3usize, 1usize), (ChannelLayout::Rgba, SampleType::U16, 4, 2)] { let (w, h) = (1531u32, 977u32); let n = w as usize * h as usize * ch * bytes; - let px: Vec = (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); + let px: Vec = + (0..n).map(|i| ((i / 3) as u32).wrapping_mul(2_654_435_761).rotate_left(i as u32 % 7) as u8 / 3 + (i % 97) as u8).collect(); let img = Image::from_raw(w, h, layout, sample, px.clone()).unwrap(); assert!(n >= PARALLEL_MIN_BYTES || sample == SampleType::U8); for level in [PngCompression::Fast, PngCompression::Default] { @@ -2088,7 +2119,8 @@ mod rows_tests { #[test] fn metadata_yields_do_not_finalize_the_pixels_again() { let text = "A".repeat(20_000); - let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8).with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); + let img = image(8, 6, ChannelLayout::Rgb, SampleType::U8) + .with_meta(Metadata { text: vec![("Comment".into(), text.clone())], ..Default::default() }); let bytes = crate::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); let mut rows = PngDecodeRows::new(bytes.as_slice(), &Limits::default()).unwrap(); let mut calls = 0; diff --git a/crates/image-codecs/src/codecs/pnm.rs b/crates/image-codecs/src/codecs/pnm.rs index c16cccfd0..2931d8da6 100644 --- a/crates/image-codecs/src/codecs/pnm.rs +++ b/crates/image-codecs/src/codecs/pnm.rs @@ -338,7 +338,9 @@ pub(crate) fn encode(src: &Image, plan: Plan, _opts: &EncodeOptions) -> Result "CMYK", _ => "CMYK_ALPHA", }; - out.extend_from_slice(format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes()); + out.extend_from_slice( + format!("P7\nWIDTH {w}\nHEIGHT {h}\nDEPTH {}\nMAXVAL {maxval}\nTUPLTYPE {t}\nENDHDR\n", l.channels()).as_bytes(), + ); } } if sample == SampleType::U8 { diff --git a/crates/image-codecs/src/codecs/tiff/decode.rs b/crates/image-codecs/src/codecs/tiff/decode.rs index d8e11c447..97efdd979 100644 --- a/crates/image-codecs/src/codecs/tiff/decode.rs +++ b/crates/image-codecs/src/codecs/tiff/decode.rs @@ -4,8 +4,8 @@ use super::bands::{Tally, convert_ab, decode_bands, extra_sample_indices, refill use super::tags::{F, TAG_IMAGE_SOURCE_DATA, TAG_PHOTOSHOP, TAG_XMP, TEXT_TAGS, photoshop_tags}; use super::ycbcr::YCbCr; use super::{ - ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, Tag, - camera_raw, err, ifd, io, map_err, tiff_exif, + ChannelLayout, ChunkType, CodecError, DecodeWarning, Decoder, ExifDirs, File, Ifd, Image, Limits, Metadata, Read, SampleType, Seek, SeekFrom, + Tag, camera_raw, err, ifd, io, map_err, tiff_exif, }; /// Decodes the page Photoshop would open: the first full-resolution directory. @@ -46,7 +46,10 @@ pub(crate) fn has_unretained_samples(bytes: &[u8], offset: u64) -> Result) -> Result<(File<'_>, Ifd, Option), CodecError> { if camera_raw(bytes) { - return Err(CodecError::unsupported(F, "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw")); + return Err(CodecError::unsupported( + F, + "camera raw files (such as CR2, NEF, ARW or DNG) are not flat images; decode them with photocraft-raw", + )); } let file = File::parse(bytes).ok_or_else(|| err("not a TIFF or BigTIFF header"))?; let info = ifd::info_of(&file)?; @@ -262,7 +265,10 @@ fn scratch_bound(direct: bool, chunk_bytes: u64, band_rows: usize, h: u32, worke pub(crate) fn orientation(bytes: &[u8], page: Option) -> u16 { let Some(file) = File::parse(bytes) else { return 1 }; let Ok(info) = ifd::info_of(&file) else { return 1 }; - page.or_else(|| info.default_page()).and_then(|i| info.pages.get(i)).and_then(|p| file.ifd(p.ifd_offset)).map_or(1, |d| ifd::orientation_of(&file, &d)) + page.or_else(|| info.default_page()) + .and_then(|i| info.pages.get(i)) + .and_then(|p| file.ifd(p.ifd_offset)) + .map_or(1, |d| ifd::orientation_of(&file, &d)) } /// The page's metadata; the ICC profile goes to `icc`. diff --git a/crates/image-codecs/src/codecs/tiff/encode.rs b/crates/image-codecs/src/codecs/tiff/encode.rs index 18b02a603..47b814e37 100644 --- a/crates/image-codecs/src/codecs/tiff/encode.rs +++ b/crates/image-codecs/src/codecs/tiff/encode.rs @@ -111,7 +111,8 @@ macro_rules! write_extras { /// is an error, not an abort. pub(super) fn try_with_capacity(len: usize) -> io::Result> { let mut v = Vec::new(); - v.try_reserve_exact(len).map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; + v.try_reserve_exact(len) + .map_err(|_| io::Error::new(io::ErrorKind::OutOfMemory, format!("cannot allocate {len} samples for the extra samples")))?; Ok(v) } @@ -411,7 +412,8 @@ pub(super) fn half_size(img: &Image) -> Result { let ch = layout.channels(); let bps = sample.bytes(); let src_row = (w as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "image row size overflows"))?; - let out_row = (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; + let out_row = + (nw as usize).checked_mul(ch).and_then(|n| n.checked_mul(bps)).ok_or_else(|| CodecError::encode(F, "pyramid row size overflows"))?; let len = out_row.checked_mul(nh as usize).ok_or_else(|| CodecError::encode(F, "pyramid level size overflows"))?; if img.data().len() < src_row.saturating_mul(h as usize) { return Err(CodecError::encode(F, "image buffer is shorter than its dimensions")); diff --git a/crates/image-codecs/src/codecs/tiff_exif.rs b/crates/image-codecs/src/codecs/tiff_exif.rs index 2e6885b6e..e0fb8c641 100644 --- a/crates/image-codecs/src/codecs/tiff_exif.rs +++ b/crates/image-codecs/src/codecs/tiff_exif.rs @@ -157,7 +157,8 @@ pub(crate) fn block(dirs: &ExifDirs) -> Vec { ifd0.push(pointer(TAG_GPS_IFD)); } // Layout: header, then each directory followed by its out-of-line values. - let size = |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); + let size = + |fs: &[Field]| 2 + 12 * fs.len() + 4 + fs.iter().filter(|f| f.value.len() > 4).map(|f| f.value.len().next_multiple_of(2)).sum::(); let at0 = 8; let at_exif = at0 + size(&ifd0); let at_gps = at_exif + if dirs.exif.is_empty() { 0 } else { size(&dirs.exif) }; diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs index 7c02c614e..02efc0287 100644 --- a/crates/image-codecs/src/codecs/via_image.rs +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -327,7 +327,9 @@ fn to_dynamic(img: &Image) -> Result { let bad = || CodecError::InvalidImage("buffer size mismatch".into()); Ok(match (img.layout(), img.sample_type()) { (ChannelLayout::Gray, SampleType::U8) => DynamicImage::ImageLuma8(image::GrayImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), - (ChannelLayout::GrayA, SampleType::U8) => DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), + (ChannelLayout::GrayA, SampleType::U8) => { + DynamicImage::ImageLumaA8(image::GrayAlphaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?) + } (ChannelLayout::Rgb, SampleType::U8) => DynamicImage::ImageRgb8(image::RgbImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), (ChannelLayout::Rgba, SampleType::U8) => DynamicImage::ImageRgba8(image::RgbaImage::from_raw(w, h, img.data().to_vec()).ok_or_else(bad)?), (ChannelLayout::Rgb, SampleType::F32) => { diff --git a/crates/image-codecs/src/codecs/vp8.rs b/crates/image-codecs/src/codecs/vp8.rs index a24fb727c..ee27f2e38 100644 --- a/crates/image-codecs/src/codecs/vp8.rs +++ b/crates/image-codecs/src/codecs/vp8.rs @@ -20,9 +20,9 @@ mod transform; use bool_encoder::BoolEncoder; use predict::{Plane, SubEdges, predict_block, predict_sub}; use tables::{ - AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, DC_QLOOKUP, - DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, KF_BMODE_PROBS, - KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, + AC_QLOOKUP, B_DC_PRED, B_HE_PRED, B_PRED, B_TM_PRED, B_VE_PRED, BMODE_TREE, CAT_BASE, COEFF_BANDS, COEFF_TREE, COEFF_UPDATE_PROBS, DC_PRED, + DC_QLOOKUP, DCT_0, DCT_1, DCT_2, DCT_3, DCT_4, DCT_CAT1, DCT_CAT2, DCT_CAT3, DCT_CAT4, DCT_CAT5, DCT_CAT6, DCT_EOB, DEFAULT_COEFF_PROBS, H_PRED, + KF_BMODE_PROBS, KF_UV_MODE_PROB, KF_YMODE_PROB, KF_YMODE_TREE, PCAT, TM_PRED, UV_MODE_TREE, V_PRED, ZIGZAG, }; use transform::{fdct, fwht, idct, iwht}; diff --git a/crates/image-codecs/src/codecs/vp8/tables.rs b/crates/image-codecs/src/codecs/vp8/tables.rs index 261231e9e..0178286bd 100644 --- a/crates/image-codecs/src/codecs/vp8/tables.rs +++ b/crates/image-codecs/src/codecs/vp8/tables.rs @@ -98,8 +98,14 @@ pub const COEFF_TREE: Tree = &[ /// Extra-bit probabilities of the six value categories (RFC 6386 §13.2), most significant bit /// first; the category bases are 5, 7, 11, 19, 35 and 67. -pub const PCAT: [&[u8]; 6] = - [&[159], &[165, 145], &[173, 148, 140], &[176, 155, 140, 135], &[180, 157, 141, 134, 130], &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129]]; +pub const PCAT: [&[u8]; 6] = [ + &[159], + &[165, 145], + &[173, 148, 140], + &[176, 155, 140, 135], + &[180, 157, 141, 134, 130], + &[254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129], +]; pub const CAT_BASE: [i32; 6] = [5, 7, 11, 19, 35, 67]; /// Coefficient position → probability band (RFC 6386 §13.3). @@ -454,18 +460,18 @@ pub const COEFF_UPDATE_PROBS: [[[[u8; 11]; 3]; 8]; 4] = [ /// DC dequantization factors by quantizer index (RFC 6386 §14.1). pub const DC_QLOOKUP: [i32; 128] = [ - 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, - 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, - 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 122, 124, - 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, + 4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 93, 95, 96, 98, 100, 101, 102, 104, 106, 108, 110, + 112, 114, 116, 118, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 143, 145, 148, 151, 154, 157, ]; /// AC dequantization factors by quantizer index (RFC 6386 §14.1). pub const AC_QLOOKUP: [i32; 128] = [ - 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, - 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, - 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, 177, 181, 185, 189, 193, 197, - 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, + 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, + 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 119, 122, 125, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 161, 164, 167, 170, 173, + 177, 181, 185, 189, 193, 197, 201, 205, 209, 213, 217, 221, 225, 229, 234, 239, 245, 249, 254, 259, 264, 269, 274, 279, 284, ]; /// Key-frame sub-block mode probabilities `[above mode][left mode][tree node]` (RFC 6386 §11.5). diff --git a/crates/image-codecs/src/codecs/webp.rs b/crates/image-codecs/src/codecs/webp.rs index 3f69d3c9c..ab2ffc95e 100644 --- a/crates/image-codecs/src/codecs/webp.rs +++ b/crates/image-codecs/src/codecs/webp.rs @@ -186,7 +186,8 @@ pub(crate) fn export_exif(img: &Image) -> Option> { pub(crate) fn keeps_dpi(img: &Image) -> bool { let bare_default = img.meta.exif.as_deref().is_none_or(<[u8]>::is_empty) && img.meta.dpi.is_none_or(is_default_ppi); // The XMP written beside it carries the resolution too, and readers look there first. - let in_xmp = img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); + let in_xmp = + img.meta.xmp.as_deref().is_some_and(|x| crate::resolution::xmp_resolution(&crate::resolution::export_xmp(x, img.meta.dpi)).is_some()); bare_default || in_xmp || export_exif(img).is_some_and(|e| crate::resolution::exif_resolution(&e).is_some()) } diff --git a/crates/image-codecs/src/exr_stream.rs b/crates/image-codecs/src/exr_stream.rs index 1ed8ba127..936814360 100644 --- a/crates/image-codecs/src/exr_stream.rs +++ b/crates/image-codecs/src/exr_stream.rs @@ -143,7 +143,8 @@ impl Decoder { } // The same channels the whole-file decoder shows, so both routes open the same image. let names: Vec = list.iter().map(|c| c.name.to_string()).collect(); - let (layout, picked) = crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; + let (layout, picked) = + crate::image::exr_color_channels(names.iter().map(String::as_str)).ok_or_else(|| unsupported("no R/G/B or Y channels"))?; let gray = matches!(layout, crate::ChannelLayout::Gray | crate::ChannelLayout::GrayA); let mut selected: Vec> = picked.into_iter().map(Some).collect(); if !matches!(layout, crate::ChannelLayout::GrayA | crate::ChannelLayout::Rgba) { @@ -241,8 +242,10 @@ impl Decoder { } self.source.read_exact(bytes).map_err(invalid)?; } - let chunk = - Chunk { layer_index: 0, compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }) }; + let chunk = Chunk { + layer_index: 0, + compressed_block: CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: y, compressed_pixels_le: packed }), + }; let block = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| UncompressedBlock::decompress_chunk(chunk, &self.meta, false))) .map_err(|_| invalid("invalid EXR compression block"))? .map_err(invalid)?; diff --git a/crates/image-codecs/src/fidelity.rs b/crates/image-codecs/src/fidelity.rs index 984f2ac45..12c285d7b 100644 --- a/crates/image-codecs/src/fidelity.rs +++ b/crates/image-codecs/src/fidelity.rs @@ -164,7 +164,9 @@ pub(crate) fn plan(image: &Image, format: Format, opts: &EncodeOptions) -> Plan match format { // PNM: integer data can use any layout (PAM); float goes to PFM // which only has gray and RGB. - Format::Pnm if sample.is_float() => Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 }, + Format::Pnm if sample.is_float() => { + Plan { layout: pick_layout(layout, &[ChannelLayout::Gray, ChannelLayout::Rgb], false), sample: SampleType::F32 } + } // DPX has no luma + alpha descriptor: gray with alpha is written as luma, alpha dropped. // The sample type follows the chosen bits (U8 for 8, U16 for 10, 12 and 16). Format::Dpx => Plan { diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index 20a520e31..c77f9a09f 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -73,7 +73,10 @@ pub struct FormatCaps { /// Formats that are enabled but intentionally not symmetric, with the reason. /// The test-suite asserts that every other enabled format is read+write. pub const ASYMMETRIC_EXCEPTIONS: &[(Format, &str)] = &[ - (Format::Xpm, "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead."), + ( + Format::Xpm, + "XPM is a legacy Unix icon interchange format. Reading existing XPM icons is supported; writing modern image assets should use PNG instead.", + ), ( Format::Dicom, "DICOM single-frame images are imported for medical review; this crate has no DICOM writer, so save an imported frame as TIFF or PNG instead.", @@ -257,11 +260,23 @@ pub fn caps(format: Format) -> FormatCaps { lossy: false, }; match format { - Format::Png => { - FormatCaps { depths: &[S::U8, S::U16], layouts: RGB_GRAY, icc: true, exif: true, xmp: true, dpi: true, text: true, animation: true, ..base } + Format::Png => FormatCaps { + depths: &[S::U8, S::U16], + layouts: RGB_GRAY, + icc: true, + exif: true, + xmp: true, + dpi: true, + text: true, + animation: true, + ..base + }, + Format::Jpeg => { + FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base } + } + Format::Tiff => { + FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base } } - Format::Jpeg => FormatCaps { layouts: &[L::Gray, L::Rgb, L::Cmyk], alpha: false, icc: true, exif: true, xmp: true, dpi: true, lossy: true, ..base }, - Format::Tiff => FormatCaps { depths: &[S::U8, S::U16, S::F32], layouts: TIFF_LAYOUTS, icc: true, exif: true, xmp: true, dpi: true, text: true, ..base }, // Resolution travels in EXIF (WebP has no field of its own). Format::WebP => FormatCaps { icc: true, exif: true, xmp: true, dpi: true, animation: true, ..base }, Format::Gif => FormatCaps { layouts: &[L::Rgba], animation: true, lossy: true, ..base }, diff --git a/crates/image-codecs/src/image.rs b/crates/image-codecs/src/image.rs index fb3294948..dc555c42b 100644 --- a/crates/image-codecs/src/image.rs +++ b/crates/image-codecs/src/image.rs @@ -97,7 +97,10 @@ impl ChannelLayout { /// `true` if both layouts share a colour model (ignoring alpha). pub const fn same_model(self, other: Self) -> bool { - (self.is_gray() && other.is_gray()) || (self.is_rgb() && other.is_rgb()) || (self.is_cmyk() && other.is_cmyk()) || (self.is_lab() && other.is_lab()) + (self.is_gray() && other.is_gray()) + || (self.is_rgb() && other.is_rgb()) + || (self.is_cmyk() && other.is_cmyk()) + || (self.is_lab() && other.is_lab()) } } @@ -521,7 +524,10 @@ impl Image { pub fn from_raw(width: u32, height: u32, layout: ChannelLayout, sample: SampleType, data: Vec) -> Result { let expected = byte_len(width, height, layout, sample).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; if data.len() != expected { - return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", data.len()))); + return Err(CodecError::InvalidImage(format!( + "buffer has {} bytes, expected {expected} for {width}x{height} {layout:?} {sample:?}", + data.len() + ))); } Ok(Image { width, height, layout, sample, data, icc: None, cicp: None, meta: Metadata::default(), warnings: Vec::new() }) } @@ -607,7 +613,8 @@ impl Image { /// Copy samples out as `f16` (only when the sample type is `F16`). pub fn to_f16_samples(&self) -> Option> { - (self.sample == SampleType::F16).then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) + (self.sample == SampleType::F16) + .then(|| self.data.as_chunks::<2>().0.iter().map(|c| f16::from_bits(u16::from_ne_bytes([c[0], c[1]]))).collect()) } /// Copy samples out as `f32` (only when the sample type is `F32`). @@ -688,7 +695,11 @@ impl Image { /// [`Image::convert`], borrowing `self` when it already has that layout and sample type /// (encoders use it so a full-size image isn't copied for nothing). pub fn converted(&self, layout: ChannelLayout, sample: SampleType) -> std::borrow::Cow<'_, Image> { - if layout == self.layout && sample == self.sample { std::borrow::Cow::Borrowed(self) } else { std::borrow::Cow::Owned(self.convert(layout, sample)) } + if layout == self.layout && sample == self.sample { + std::borrow::Cow::Borrowed(self) + } else { + std::borrow::Cow::Owned(self.convert(layout, sample)) + } } /// Interleaved RGBA, 8 bits per sample. @@ -742,10 +753,21 @@ fn convert_data(data: &[u8], from: ChannelLayout, from_sample: SampleType, layou /// [`Image::convert`] on borrowed pixels: `rows` rows of `width` pixels in `from` (layout, sample /// type) become the same pixels in `to`, bit for bit what the image conversion gives. Lets a /// caller convert a band of a decoded image without copying it into an [`Image`] first. -pub fn convert_samples(src: &[u8], width: u32, rows: u32, from: (ChannelLayout, SampleType), to: (ChannelLayout, SampleType)) -> Result, CodecError> { +pub fn convert_samples( + src: &[u8], + width: u32, + rows: u32, + from: (ChannelLayout, SampleType), + to: (ChannelLayout, SampleType), +) -> Result, CodecError> { let expected = byte_len(width, rows, from.0, from.1).ok_or_else(|| CodecError::InvalidImage("image size overflows usize".into()))?; if src.len() != expected { - return Err(CodecError::InvalidImage(format!("buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", src.len(), from.0, from.1))); + return Err(CodecError::InvalidImage(format!( + "buffer has {} bytes, expected {expected} for {width}x{rows} {:?} {:?}", + src.len(), + from.0, + from.1 + ))); } Ok(convert_data(src, from.0, from.1, to.0, to.1)) } @@ -919,7 +941,9 @@ mod tests { let got = convert_samples(img.data(), 5, 2, (ChannelLayout::Rgb, sample), (layout, to)).unwrap(); assert_eq!(got, want.data(), "{sample:?} to {layout:?} {to:?}"); } - assert!(convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err()); + assert!( + convert_samples(img.data().get(1..).unwrap_or_default(), 5, 2, (ChannelLayout::Rgb, sample), (ChannelLayout::Rgba, sample)).is_err() + ); } } @@ -933,9 +957,12 @@ mod tests { // Every U8 and every U16 value through every depth pair (alpha goes through the same per-sample conversion; covered by the layout test). let all8: Vec = (0..=255).collect(); let all16: Vec = (0..=65535).collect(); - for (src, to) in - [(SampleType::U8, SampleType::U8), (SampleType::U8, SampleType::U16), (SampleType::U16, SampleType::U8), (SampleType::U16, SampleType::U16)] - { + for (src, to) in [ + (SampleType::U8, SampleType::U8), + (SampleType::U8, SampleType::U16), + (SampleType::U16, SampleType::U8), + (SampleType::U16, SampleType::U16), + ] { let img = if src == SampleType::U8 { Image::from_u8(256, 1, ChannelLayout::Gray, all8.clone()).unwrap() } else { diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index a5922d323..974ff205b 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -48,8 +48,8 @@ pub use crate::image::{ MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; pub use crate::options::{ - DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, - TiffCompression, + DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, + TIFF_PYRAMID_MIN, TiffCompression, }; pub use crate::orientation::{OrientSteps, exif_orientation, upright_exif, upright_xmp}; pub use crate::resolution::{exif_resolution, export_exif, export_xmp, photoshop_resolution, xmp_resolution}; @@ -284,7 +284,10 @@ pub fn has_unretained_source_data(bytes: &[u8]) -> Result { retained.and_then(|index| info.pages.get(index)).map_or(Ok(false), |page| tiff::has_unretained_samples(bytes, page.ifd_offset)) } Some(Format::Ico) => { - let count = bytes.get(4..6).and_then(|b| <[u8; 2]>::try_from(b).ok()).ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; + let count = bytes + .get(4..6) + .and_then(|b| <[u8; 2]>::try_from(b).ok()) + .ok_or_else(|| CodecError::malformed(Format::Ico, "truncated image count"))?; Ok(u16::from_le_bytes(count) > 1) } Some(Format::Dds) => dds::has_additional_images(bytes), @@ -416,6 +419,8 @@ pub fn encode(image: &Image, format: Format, opts: &EncodeOptions) -> Result exr::encode(image, plan, opts), Format::Heif => Err(CodecError::unsupported(format, "encoding is not available in this build")), Format::Jxl => jxl::encode(image, plan), - Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => via_image::encode(format, image, plan, opts), + Format::Gif | Format::Bmp | Format::Tga | Format::Ico | Format::Qoi | Format::Hdr | Format::Avif => { + via_image::encode(format, image, plan, opts) + } } } diff --git a/crates/image-codecs/src/orientation.rs b/crates/image-codecs/src/orientation.rs index 5ad8be252..f77e6691f 100644 --- a/crates/image-codecs/src/orientation.rs +++ b/crates/image-codecs/src/orientation.rs @@ -177,8 +177,10 @@ pub fn upright_exif(exif: &[u8]) -> Cow<'_, [u8]> { let Some((order, first, count, entry_bytes, big_tiff)) = ifd0_entries(body) else { return Cow::Borrowed(exif); }; - let values: Vec = - (0..count).filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)).filter_map(|e| orientation_value(order, body, e, big_tiff)).collect(); + let values: Vec = (0..count) + .filter_map(|i| first.checked_add(i.checked_mul(entry_bytes)?)) + .filter_map(|e| orientation_value(order, body, e, big_tiff)) + .collect(); if values.iter().all(|v| v.read(order, body) == Some(1)) { return Cow::Borrowed(exif); } diff --git a/crates/image-codecs/src/source_settings.rs b/crates/image-codecs/src/source_settings.rs index ebd0421c3..c7bb51baf 100644 --- a/crates/image-codecs/src/source_settings.rs +++ b/crates/image-codecs/src/source_settings.rs @@ -40,8 +40,8 @@ const ANNEX_K_CHROMA: [u16; 64] = [ /// The natural-order index of the k-th coefficient in zig-zag order (T.81 Figure A.6): DQT /// stores its 64 values in this order. const ZIGZAG: [usize; 64] = [ - 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, - 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, + 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, ]; /// One quantisation table from a DQT segment, in natural order, with its precision. @@ -129,7 +129,11 @@ fn read_dqt(mut seg: &[u8], tables: &mut [Option
; 4]) { let Some(data) = rest.get(..n) else { return }; let mut values = [0u16; 64]; for (k, &natural) in ZIGZAG.iter().enumerate() { - let v = if wide { data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) } else { data.get(k).map_or(0, |&p| u16::from(p)) }; + let v = if wide { + data.get(2 * k..2 * k + 2).map_or(0, |p| u16::from_be_bytes([p[0], p[1]])) + } else { + data.get(k).map_or(0, |&p| u16::from(p)) + }; if let Some(dst) = values.get_mut(natural) { *dst = v; } diff --git a/crates/image-codecs/tests/it/dds_write.rs b/crates/image-codecs/tests/it/dds_write.rs index 4f66d3679..f9d9add8e 100644 --- a/crates/image-codecs/tests/it/dds_write.rs +++ b/crates/image-codecs/tests/it/dds_write.rs @@ -1,7 +1,9 @@ //! Writing DDS textures: every storage read back through the reader, the header fields, sizes //! that aren't a multiple of four, the mip chain, and the block-compression error on ramps. -use craft_image_codecs::{ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with}; +use craft_image_codecs::{ + ChannelLayout, DdsCompression, DdsOptions, EncodeOptions, FidelityWarning, Format, Image, decode, encode, fidelity_warnings_with, +}; const DDSD_PITCH: u32 = 0x8; const DDSD_MIPMAPCOUNT: u32 = 0x2_0000; @@ -65,7 +67,8 @@ fn max_colour_diff(a: &Image, b: &Image) -> u8 { /// A 2D gradient: red across, green down and blue along the diagonal, opaque. fn gradient(w: u32, h: u32) -> Image { - let data = (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); + let data = + (0..h).flat_map(|y| (0..w).flat_map(move |x| [(x * 4).min(255) as u8, (y * 4).min(255) as u8, ((x + y) * 2).min(255) as u8, 255])).collect(); Image::from_u8(w, h, ChannelLayout::Rgba, data).unwrap() } @@ -163,7 +166,9 @@ fn block_compressed_textures_round_trip_through_the_reader() { assert!(colour <= 24, "{compression:?} {w}x{h}: colour off by {colour}"); let alpha = max_diff_channel(&img, &back, 3); match compression { - DdsCompression::Bc1 => assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + DdsCompression::Bc1 => { + assert!(back.data().chunks(4).zip(img.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })) + } _ => assert!(alpha <= 8, "{compression:?} {w}x{h}: alpha off by {alpha}"), } } @@ -201,7 +206,9 @@ fn block_compression_error_on_gradients_is_bounded() { assert!(plane_err <= 10, "{compression:?}: 2D gradient off by {plane_err}"); assert!(p >= 38.0, "{compression:?}: 2D gradient PSNR {p:.1} dB"); match compression { - DdsCompression::Bc1 => assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })), + DdsCompression::Bc1 => { + assert!(line_back.data().chunks(4).zip(line.data().chunks(4)).all(|(b, i)| b[3] == if i[3] < 128 { 0 } else { 255 })) + } // Four bits: within half a step of 17. DdsCompression::Bc2 => assert!(alpha <= 8, "BC2 alpha off by {alpha}"), // Eight values across a block's few levels of alpha. diff --git a/crates/image-codecs/tests/it/decode_warnings.rs b/crates/image-codecs/tests/it/decode_warnings.rs index 5995de931..908b102b3 100644 --- a/crates/image-codecs/tests/it/decode_warnings.rs +++ b/crates/image-codecs/tests/it/decode_warnings.rs @@ -159,7 +159,8 @@ fn apng(frames: u32, sixteen: bool, separate_default: bool) -> Vec { } let mut w = enc.write_header().unwrap(); for c in COLOURS.iter().cycle().take(images.max(1) as usize) { - let px: Vec = if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; + let px: Vec = + if sixteen { c.iter().flat_map(|&v| [v, v]).collect::>().repeat((W * H) as usize) } else { c.repeat((W * H) as usize) }; w.write_image_data(&px).unwrap(); } w.finish().unwrap(); diff --git a/crates/image-codecs/tests/it/deep_exr.rs b/crates/image-codecs/tests/it/deep_exr.rs index a3c003f75..37fe315ad 100644 --- a/crates/image-codecs/tests/it/deep_exr.rs +++ b/crates/image-codecs/tests/it/deep_exr.rs @@ -383,8 +383,9 @@ fn opaque_front_hides_back() { fn hdr_colour_is_kept_and_edge_tiles_match_scanlines() { let (w, h) = (7, 5); let counts: Vec = (0..w * h).map(|p| (p as u32 * 3) % 4).collect(); - let sample = - |p: usize, ci: usize| -> Vec { (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() }; + let sample = |p: usize, ci: usize| -> Vec { + (0..(p as u32 * 3) % 4).map(|k| [1.0, 0.25 * k as f32, 0.5, 2.0 + p as f32 / 8.0, 10.0 - k as f32][ci]).collect() + }; for compression in [0u8, 1, 2] { let scan = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, None, &counts, &sample)); let tiled = rgba_flat(&gen_deep(w, h, &RGBAZ, compression, Some((3, 2)), &counts, &sample)); @@ -482,7 +483,8 @@ fn tiles_match_scanlines() { /// Half-float channels come back as F16 samples with exact values. #[test] fn half_channels_round_trip() { - let channels = [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; + let channels = + [Chan { name: "A", ty: 1 }, Chan { name: "B", ty: 1 }, Chan { name: "G", ty: 1 }, Chan { name: "R", ty: 1 }, Chan { name: "Z", ty: 1 }]; let bytes = gen_deep(4, 4, &channels, 2, None, COUNTS_ONE, &|p, _ci| { let v = f16::from_f32(0.5).to_f32(); vec![v * (p as f32 + 1.0) / 4.0] @@ -661,7 +663,11 @@ fn deep_tile_count_table_is_admitted_against_max_alloc() { // The table and its scratch copy are admitted together: 8 MiB. assert_eq!(decode_deep_exr(&big, &budget(2 * table)).expect("room for the table and its copy"), scan, "compression {compression}"); let got = decode_deep_exr(&big, &budget(2 * table - 1)); - assert!(matches!(got, Err(CodecError::LimitExceeded(_))), "compression {compression}: one byte short gave {:?}", got.map(|d| d.total_samples())); + assert!( + matches!(got, Err(CodecError::LimitExceeded(_))), + "compression {compression}: one byte short gave {:?}", + got.map(|d| d.total_samples()) + ); } // Ordinary tiles: full-size edge tables decode under the small budget, like clipped ones. let counts: Vec = (0..35).map(|p| (p * 3) % 4).collect(); @@ -729,7 +735,8 @@ fn clipped_count_tables_include_scratch_in_the_budget() { let required = if compression == 0 { 256 } else { 512 }; assert_eq!(decode_deep_exr(&bytes, &budget(required)).expect("table and any scratch fit exactly"), want, "{what}"); assert!(matches!(decode_deep_exr(&bytes, &budget(required - 1)), Err(CodecError::LimitExceeded(_))), "{what}"); - let flat = craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); + let flat = + craft_image_codecs::decode_with(&bytes, &craft_image_codecs::DecodeOptions { limits: budget(required - 1), ..Default::default() }); assert!(matches!(flat, Err(CodecError::LimitExceeded(_))), "{what}"); if compression == 0 { assert_eq!(decode_deep_exr(&bytes, &budget(511)).expect("raw table needs no scratch"), want, "{what}"); @@ -802,9 +809,10 @@ type RealFile = (&'static str, u64, u64, &'static [(&'static str, &'static [u32] #[cfg(feature = "corpus")] const MULTIVARIATE_IDS: &[u32] = &[ - 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, 1672208113, - 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, 2505186565, 2698909330, - 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, 3739501011, 3936563811, 4048943790, + 127035619, 147085119, 319332210, 409096490, 523461226, 678492822, 683859976, 1058109025, 1193275200, 1503246798, 1552124883, 1613501244, + 1672208113, 1687665471, 1741895317, 1841370478, 1863854195, 1865183722, 2056745377, 2202049807, 2303547305, 2369798380, 2433329986, 2439705590, + 2505186565, 2698909330, 2862333315, 2898564507, 2979371527, 3007860936, 3182339638, 3236172513, 3497437340, 3626859046, 3656820528, 3693645783, + 3739501011, 3936563811, 4048943790, ]; #[cfg(feature = "corpus")] @@ -813,7 +821,10 @@ const REAL_FILES: &[RealFile] = &[ "11.deep.exr", 6056, 2, - &[("id", &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011])], + &[( + "id", + &[523461226, 683859976, 1552124883, 2202049807, 2303547305, 2433329986, 2698909330, 3182339638, 3236172513, 3626859046, 3739501011], + )], ), ( "42.deep.exr", diff --git a/crates/image-codecs/tests/it/dpx.rs b/crates/image-codecs/tests/it/dpx.rs index 4d4835cc7..9b4d42e18 100644 --- a/crates/image-codecs/tests/it/dpx.rs +++ b/crates/image-codecs/tests/it/dpx.rs @@ -346,7 +346,10 @@ fn saving_sixteen_bits_at_ten_or_twelve_warns_with_the_stored_bits() { } assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits16))), []); assert_eq!(depth_warnings(&img, Format::Dpx, &opts(None)), []); - assert_eq!(depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }]); + assert_eq!( + depth_warnings(&img, Format::Dpx, &opts(Some(DpxBits::Bits8))), + [FidelityWarning::DepthReduced { from: SampleType::U16, to: SampleType::U8 }] + ); assert_eq!(depth_warnings(&img, Format::Cineon, &EncodeOptions::default()), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); } @@ -360,7 +363,10 @@ fn a_frame_opened_at_ten_or_twelve_bits_saves_back_without_a_warning() { } // A 12-bit frame saved at 10 bits does lose its two low bits. let twelve = decode(&encode(&img, Format::Dpx, &opts(Some(DpxBits::Bits12))).unwrap()).unwrap(); - assert_eq!(depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }]); + assert_eq!( + depth_warnings(&twelve, Format::Dpx, &opts(Some(DpxBits::Bits10))), + [FidelityWarning::BitsReduced { from: SampleType::U16, bits: 10 }] + ); let cineon = decode(&cineon_bytes(3, 4, 2, &cineon_codes(24))).unwrap(); assert_eq!(depth_warnings(&cineon, Format::Cineon, &EncodeOptions::default()), []); assert_eq!(depth_warnings(&cineon, Format::Dpx, &opts(Some(DpxBits::Bits10))), []); diff --git a/crates/image-codecs/tests/it/exr_cryptomatte.rs b/crates/image-codecs/tests/it/exr_cryptomatte.rs index 80c843492..dbf4f7010 100644 --- a/crates/image-codecs/tests/it/exr_cryptomatte.rs +++ b/crates/image-codecs/tests/it/exr_cryptomatte.rs @@ -4,11 +4,12 @@ //! the manifest round trip and the per-pixel pairs. Malformed inputs must error or //! degrade, never panic. +use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; use exr::meta::attribute::{AttributeValue, Text}; use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, WritableImage, + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, }; -use craft_image_codecs::{Limits, cryptomatte_id, cryptomatte_key, cryptomatte_layers, cryptomatte_preview_color, decode_cryptomatte}; // ------------------------------------------------------------------------------------------- // The official hash test vectors (cryptomatte_utilities_tests.py, CryptoHashing). @@ -77,7 +78,12 @@ fn cryptomatte_file(name: &str) -> Vec { let s1: Vec = (0..w * h).map(|p| if p % w == 0 { sphere } else { 0.0 }).collect(); let s1c: Vec = (0..w * h).map(|p| if p % w == 0 { 0.25 } else { 0.0 }).collect(); let mk = |n: String, v: Vec| AnyChannel::new(n.as_str(), FlatSamples::F32(v)); - let list = vec![mk(format!("{name}00.red"), s0), mk(format!("{name}00.green"), s0c), mk(format!("{name}01.red"), s1), mk(format!("{name}01.green"), s1c)]; + let list = vec![ + mk(format!("{name}00.red"), s0), + mk(format!("{name}00.green"), s0c), + mk(format!("{name}01.red"), s1), + mk(format!("{name}01.green"), s1c), + ]; let key = cryptomatte_key("CryptoAsset"); // attributes always use the canonical name let manifest = format!("{{\"cube\":\"{:08x}\",\"sphere\":\"{:08x}\"}}", cube.to_bits(), sphere.to_bits()); let mut attrs = LayerAttributes::named(name); diff --git a/crates/image-codecs/tests/it/exr_multipart.rs b/crates/image-codecs/tests/it/exr_multipart.rs index 246b3d9ed..b98c045f7 100644 --- a/crates/image-codecs/tests/it/exr_multipart.rs +++ b/crates/image-codecs/tests/it/exr_multipart.rs @@ -3,14 +3,14 @@ //! listing, the colour-ranked auto-pick and part-by-index decoding, and malformed //! inputs must error, never panic. -use exr::prelude::{ - AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, - WritableImage, -}; use craft_image_codecs::f16; use craft_image_codecs::{ ChannelLayout, DecodeOptions, DecodeWarning, Format, Limits, SampleType, decode, decode_as_with, decode_exr_part, exr_info, probe_size, }; +use exr::prelude::{ + AnyChannel, AnyChannels, Blocks, Compression, Encoding, FlatSamples, Image, ImageAttributes, IntegerBounds, Layer, LayerAttributes, LineOrder, + WritableImage, +}; fn layer_f32(name: &str, comp: Compression, chans: &[(&str, Vec)]) -> Layer> { let n = chans.iter().find(|(_, v)| !v.is_empty()).map_or(0, |(_, v)| v.len()); @@ -49,7 +49,11 @@ fn aov_file() -> Vec { let diffuse: [Vec; 3] = std::array::from_fn(|c| (0..n).map(|_| [0.3, 0.6, 0.9][c]).collect()); let layers = vec![ layer_f32("Z", Compression::Uncompressed, &[("Z", z)]), - layer_f32("beauty", Compression::ZIP1, &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())]), + layer_f32( + "beauty", + Compression::ZIP1, + &[("R", beauty[0].clone()), ("G", beauty[1].clone()), ("B", beauty[2].clone()), ("A", beauty[3].clone())], + ), layer_f32("diffuse", Compression::RLE, &[("R", diffuse[0].clone()), ("G", diffuse[1].clone()), ("B", diffuse[2].clone())]), ]; write_image(layers) @@ -291,8 +295,15 @@ fn auto_pick_and_probe_skip_incoherent_rgb_parts() { #[test] fn flat_decode_prefers_the_highest_ranked_complete_group() { - let bytes = - channel_group_file(&[("aaa.R", 9.0), ("aaa.G", 8.0), ("aaa.B", 7.0), ("beauty.R", 0.25), ("beauty.G", 0.5), ("beauty.B", 2.0), ("beauty.A", 0.75)]); + let bytes = channel_group_file(&[ + ("aaa.R", 9.0), + ("aaa.G", 8.0), + ("aaa.B", 7.0), + ("beauty.R", 0.25), + ("beauty.G", 0.5), + ("beauty.B", 2.0), + ("beauty.A", 0.75), + ]); assert_channel_group(&bytes, ChannelLayout::Rgba, &[0.25, 0.5, 2.0, 0.75]); let bytes = channel_group_file(&[("aaa.Y", 9.0), ("beauty.Y", 0.5), ("beauty.A", 0.75)]); assert_channel_group(&bytes, ChannelLayout::GrayA, &[0.5, 0.75]); diff --git a/crates/image-codecs/tests/it/exr_stream.rs b/crates/image-codecs/tests/it/exr_stream.rs index 5d6ee4158..a3a6503f4 100644 --- a/crates/image-codecs/tests/it/exr_stream.rs +++ b/crates/image-codecs/tests/it/exr_stream.rs @@ -125,8 +125,12 @@ fn mixed_samples_and_offset_data_window_keep_coordinates_and_values() { .collect(), ); let attributes = LayerAttributes { layer_position: exr::math::Vec2(-15, 40), ..Default::default() }; - let layer = - Layer::new((31, 35), attributes, Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, channels); + let layer = Layer::new( + (31, 35), + attributes, + Encoding { compression: Compression::ZIP16, blocks: Blocks::ScanLines, line_order: LineOrder::Decreasing }, + channels, + ); let mut bytes = Cursor::new(Vec::new()); exr::image::Image::from_layer(layer).write().to_buffered(&mut bytes).unwrap(); let mut decoder = Decoder::new(bytes, &|| false).unwrap(); diff --git a/crates/image-codecs/tests/it/fidelity.rs b/crates/image-codecs/tests/it/fidelity.rs index 37c7aba0c..98f362698 100644 --- a/crates/image-codecs/tests/it/fidelity.rs +++ b/crates/image-codecs/tests/it/fidelity.rs @@ -92,8 +92,13 @@ fn icc_warning_suppressed_when_not_embedding() { #[test] fn exif_xmp_dpi_text_dropped_for_qoi() { let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); - img.meta = - Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some("x".into()), + dpi: Some((72.0, 72.0)), + text: vec![("a".into(), "b".into())], + ..Default::default() + }; let w = warns(&img, Format::Qoi); for x in [W::ExifDropped, W::XmpDropped, W::DpiDropped, W::TextDropped] { assert!(w.contains(&x), "{x:?}"); @@ -103,8 +108,13 @@ fn exif_xmp_dpi_text_dropped_for_qoi() { #[test] fn webp_drops_text_only() { let mut img = test_image(ChannelLayout::Rgb, SampleType::U8); - img.meta = - Metadata { exif: Some(sample_exif()), xmp: Some("x".into()), dpi: Some((72.0, 72.0)), text: vec![("a".into(), "b".into())], ..Default::default() }; + img.meta = Metadata { + exif: Some(sample_exif()), + xmp: Some("x".into()), + dpi: Some((72.0, 72.0)), + text: vec![("a".into(), "b".into())], + ..Default::default() + }; let w = warns(&img, Format::WebP); assert_eq!(w, vec![W::TextDropped]); } diff --git a/crates/image-codecs/tests/it/limits.rs b/crates/image-codecs/tests/it/limits.rs index 69e510151..e590aa568 100644 --- a/crates/image-codecs/tests/it/limits.rs +++ b/crates/image-codecs/tests/it/limits.rs @@ -393,7 +393,9 @@ fn pnm_header_alone_reserves_nothing_without_limits() { // 2⁶² bytes before finding the raster missing (an abort). let none = opts(Limits::none()); let side = 1u32 << 31; - for f in [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] { + for f in + [format!("P1\n{side} {side}\n"), format!("P2\n{side} {side}\n255\n"), format!("P3\n{side} {side}\n255\n"), format!("P4\n{side} {side}\n")] + { assert!(decode_with(f.as_bytes(), &none).is_err(), "{f:?}"); } } @@ -440,7 +442,8 @@ fn jpeg_bomb_header_rejected() { fn tiff_bomb_header_rejected() { // Minimal little-endian TIFF claiming 1_000_000 x 1_000_000 8-bit gray. let mut b = b"II*\0\x08\0\0\0".to_vec(); - let entries: [(u16, u16, u32, u32); 6] = [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; + let entries: [(u16, u16, u32, u32); 6] = + [(256, 4, 1, 1_000_000), (257, 4, 1, 1_000_000), (258, 3, 1, 8), (262, 3, 1, 1), (273, 4, 1, 0), (279, 4, 1, 0)]; b.extend_from_slice(&(entries.len() as u16).to_le_bytes()); for (tag, ty, count, val) in entries { b.extend_from_slice(&tag.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/metadata.rs b/crates/image-codecs/tests/it/metadata.rs index d6d292557..ed5855a3f 100644 --- a/crates/image-codecs/tests/it/metadata.rs +++ b/crates/image-codecs/tests/it/metadata.rs @@ -177,7 +177,11 @@ fn jpeg_multi_segment_icc() { fn jpeg_with_oversized_metadata_still_encodes() { let ancestors: String = (0..3000).map(|i| format!("xmp.did:{i:032x}")).collect(); let texts: String = (0..400) - .map(|i| format!("Layer {i}Words on layer {i}")) + .map(|i| { + format!( + "Layer {i}Words on layer {i}" + ) + }) .collect(); let xmp = format!( "\n{ancestors}{texts}" @@ -218,7 +222,9 @@ fn embed_flags_disable_metadata() { assert!(back.icc.is_none(), "{f:?}"); assert!(back.meta.exif.is_none() && back.meta.xmp.is_none(), "{f:?}"); assert!(back.meta.text.is_empty(), "{f:?}"); - assert!(fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped))); + assert!( + fidelity_warnings_with(&img, Format::Bmp, &opts).iter().all(|w| !matches!(w, FidelityWarning::IccDropped | FidelityWarning::ExifDropped)) + ); } } @@ -322,7 +328,8 @@ fn webp_keeps_dpi_without_source_exif() { let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); assert!(back.meta.exif.is_some()); // ...unless the XMP written beside it records the resolution, which readers look at first. - img.meta.xmp = Some(r#""#.into()); + img.meta.xmp = + Some(r#""#.into()); assert!(!fidelity_warnings(&img, Format::WebP).contains(&FidelityWarning::DpiDropped)); let back = decode(&encode(&img, Format::WebP, &EncodeOptions::default()).unwrap()).unwrap(); let (x, y) = back.meta.dpi.expect("dpi from XMP"); diff --git a/crates/image-codecs/tests/it/orientation.rs b/crates/image-codecs/tests/it/orientation.rs index c278f36f6..d5d3ad90e 100644 --- a/crates/image-codecs/tests/it/orientation.rs +++ b/crates/image-codecs/tests/it/orientation.rs @@ -364,7 +364,9 @@ fn jpeg_opens_upright_for_every_orientation() { #[test] fn rgb_jpeg_with_dpi_swaps_resolution_on_a_quarter_turn() { - let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]).unwrap().with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); + let img = Image::from_u8(16, 8, ChannelLayout::Rgb, vec![128; 16 * 8 * 3]) + .unwrap() + .with_meta(Metadata { dpi: Some((300.0, 150.0)), ..Default::default() }); let jpeg = encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); let back = decode(&jpeg_with_exif(&jpeg, &exif(6, false, false))).unwrap(); assert_eq!(back.dimensions(), (8, 16)); @@ -584,7 +586,11 @@ fn upright_exif_keeps_every_other_byte() { tail.extend_from_slice(&u32b(4)); tail.extend_from_slice(b"0232"); tail.extend_from_slice(&u32b(0)); - let e = ifd(big, &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], &tail); + let e = ifd( + big, + &[(0x010F, 2, 12, u32b(make_at)), (0x0112, 3, 1, short(6, big)), (0x0131, 2, 4, *b"PC1\0"), (0x8769, 4, 1, u32b(sub_at))], + &tail, + ); let mut prefixed = b"Exif\0\0".to_vec(); prefixed.extend_from_slice(&e); let fixed = upright_exif(&prefixed).into_owned(); diff --git a/crates/image-codecs/tests/it/pcx.rs b/crates/image-codecs/tests/it/pcx.rs index b112a3af1..fbd0e7e57 100644 --- a/crates/image-codecs/tests/it/pcx.rs +++ b/crates/image-codecs/tests/it/pcx.rs @@ -112,7 +112,8 @@ fn rejects_truncated_and_overshooting_rle_without_panicking() { fn refuses_headers_that_exceed_allocation_budget() { let mut b = header(8, 3, 3000, 3000, 3000); // The default decoder rejects the allocation before reading any scanline. - let opts = craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; + let opts = + craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_alloc: 1024, ..Default::default() }, ..Default::default() }; assert!(matches!(craft_image_codecs::decode_as_with(Format::Pcx, &b, &opts), Err(CodecError::LimitExceeded(_)))); b[2] = 0; assert_eq!(detect(&b), None, "unsupported uncompressed encoding"); diff --git a/crates/image-codecs/tests/it/png_jpeg.rs b/crates/image-codecs/tests/it/png_jpeg.rs index e98b0ae09..0c7bc03d4 100644 --- a/crates/image-codecs/tests/it/png_jpeg.rs +++ b/crates/image-codecs/tests/it/png_jpeg.rs @@ -278,7 +278,8 @@ fn png_rows_take_one_call_per_row_and_report_progress() { #[test] fn png_rows_share_their_bytes_without_copying_them() { - let bytes = std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); + let bytes = + std::sync::Arc::new(encode(&synth(20, 20, ChannelLayout::Rgba, SampleType::U8, 3, 0.2), Format::Png, &EncodeOptions::default()).unwrap()); let mut rows = PngDecodeRows::new(SharedBytes(bytes.clone()), &Limits::default()).unwrap(); let img = rows.step(&mut || true).unwrap().unwrap(); assert_eq!(img, decode(&bytes).unwrap()); diff --git a/crates/image-codecs/tests/it/resolution.rs b/crates/image-codecs/tests/it/resolution.rs index fe2c51c8e..eadecbaa3 100644 --- a/crates/image-codecs/tests/it/resolution.rs +++ b/crates/image-codecs/tests/it/resolution.rs @@ -280,7 +280,9 @@ fn truncated_metadata_never_panics_and_falls_back() { assert_eq!(photoshop_resolution(&r[..n]), None, "{n}"); near(dpi_of(&jpeg_with(&[jfif(1, 72, 72), app13(&r[..n])])), 72.0); } - for x in ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] { + for x in + ["tiff:XResolution=", "tiff:XResolution=\"", "tiff:XResolution=\"\" tiff:YResolution=\"5/0\"", "", "9<"] + { assert_eq!(xmp_resolution(x), None, "{x}"); let _ = xmp_with_resolution(x, (1.0, 1.0)); } @@ -375,7 +377,10 @@ fn xmp_resolution_is_rewritten() { r#""# ); let elem = "300/1300/1"; - assert_eq!(xmp_with_resolution(elem, (72.5, 72.5)), "72500/100072500/1000"); + assert_eq!( + xmp_with_resolution(elem, (72.5, 72.5)), + "72500/100072500/1000" + ); for same in [r#"tiff:XResolution="240" tiff:YResolution="240/1""#, ""] { assert!(matches!(xmp_with_resolution(same, (240.0, 240.0)), Cow::Borrowed(_)), "{same}"); } diff --git a/crates/image-codecs/tests/it/roundtrip.rs b/crates/image-codecs/tests/it/roundtrip.rs index c9b179bc8..6d5d38bca 100644 --- a/crates/image-codecs/tests/it/roundtrip.rs +++ b/crates/image-codecs/tests/it/roundtrip.rs @@ -268,7 +268,8 @@ fn jpeg_format_options_round_trip() { let sof = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]); for subsampled in [false, true] { let enc = |f| { - encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }).unwrap() + encode(&img, Format::Jpeg, &EncodeOptions { jpeg_format: f, jpeg_chroma_subsampling: subsampled, jpeg_quality: 90, ..Default::default() }) + .unwrap() }; let (base, opt, prog) = (enc(JpegFormat::Baseline), enc(JpegFormat::Optimized), enc(JpegFormat::Progressive)); // The scan layout only changes how the same quantized coefficients are stored, so every @@ -309,7 +310,8 @@ fn webp_lossy_request_is_a_lossy_file() { let back = decode(&bytes).unwrap(); assert_eq!(back.dimensions(), img.dimensions()); assert!( - fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }).contains(&FidelityWarning::LossyCompression) + fidelity_warnings_with(&img, Format::WebP, &EncodeOptions { webp_lossless: false, ..Default::default() }) + .contains(&FidelityWarning::LossyCompression) ); } diff --git a/crates/image-codecs/tests/it/source_settings.rs b/crates/image-codecs/tests/it/source_settings.rs index 07b20471a..5923d562d 100644 --- a/crates/image-codecs/tests/it/source_settings.rs +++ b/crates/image-codecs/tests/it/source_settings.rs @@ -26,8 +26,8 @@ fn jpeg_quality_of_hand_built_tables() { // A minimal JPEG prefix: SOI, one DQT holding the Annex K luminance table (quality 50) in // zig-zag order, then SOS. let luma: [u8; 64] = [ - 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, - 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, + 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, + 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, ]; let mut b = vec![0xFF, 0xD8, 0xFF, 0xDB, 0x00, 67, 0x00]; b.extend_from_slice(&luma); diff --git a/crates/image-codecs/tests/it/tiff_bands.rs b/crates/image-codecs/tests/it/tiff_bands.rs index a449ae2b7..fe61178dd 100644 --- a/crates/image-codecs/tests/it/tiff_bands.rs +++ b/crates/image-codecs/tests/it/tiff_bands.rs @@ -103,7 +103,11 @@ fn every_layout_matches_the_whole_image_decoder() { ), ( "palette 4-bit", - strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag(320, SHORT, &(0..48u64).map(|i| i * 1365).collect::>()), + strips(W, H, 4, 1, 3, rows, &noise((W as usize).div_ceil(2) * H as usize, 8)).tag( + 320, + SHORT, + &(0..48u64).map(|i| i * 1365).collect::>(), + ), row(3, 1) * rows as usize, ), ("gray 1-bit", strips(W, H, 1, 1, 1, rows, &noise((W as usize).div_ceil(8) * H as usize, 9)), row(1, 1) * rows as usize), @@ -122,11 +126,9 @@ fn tiles_and_planar_data_match() { let (w, h) = (W as usize, H as usize); let px = noise(w * h * 3 * 2, 11); let tiled = |planar: bool| { - let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true }.tag(322, SHORT, &[16]).tag( - 323, - SHORT, - &[16], - ); + let mut d = Dir { entries: base(W, H, 16, 3, 2), chunks: tile_split(&px, w, h, 3, 2, 16, 16, planar), tiled: true } + .tag(322, SHORT, &[16]) + .tag(323, SHORT, &[16]); if planar { d = d.tag(284, SHORT, &[2]); } @@ -135,7 +137,8 @@ fn tiles_and_planar_data_match() { let band = w * 3 * 2 * 16; assert_same(&build(true, false, &[tiled(false)], &[0]).bytes, band, "chunky tiles"); assert_same(&build(true, true, &[tiled(true)], &[0]).bytes, band, "planar tiles, BigTIFF"); - let planar = Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); + let planar = + Dir { entries: base(W, H, 16, 3, 2), chunks: planar_strips(&px, w, h, 3, 2, 5), tiled: false }.tag(278, LONG, &[5]).tag(284, SHORT, &[2]); assert_same(&build(true, false, &[planar], &[0]).bytes, w * 3 * 2 * 5, "planar strips"); } diff --git a/crates/image-codecs/tests/it/tiff_bench.rs b/crates/image-codecs/tests/it/tiff_bench.rs index 55cff55ca..99ed4f5d6 100644 --- a/crates/image-codecs/tests/it/tiff_bench.rs +++ b/crates/image-codecs/tests/it/tiff_bench.rs @@ -78,9 +78,11 @@ fn time(name: &str, bytes: &[u8]) { fn decode_30mp() { let rgb = pixels(3); let img = Image::from_u8(W, H, ChannelLayout::Rgb, rgb.clone()).unwrap(); - for (name, c) in - [("strips, none", TiffCompression::None), ("strips, LZW + predictor", TiffCompression::Lzw), ("strips, Deflate + predictor", TiffCompression::Deflate)] - { + for (name, c) in [ + ("strips, none", TiffCompression::None), + ("strips, LZW + predictor", TiffCompression::Lzw), + ("strips, Deflate + predictor", TiffCompression::Deflate), + ] { let bytes = encode(&img, Format::Tiff, &EncodeOptions { tiff_compression: c, ..Default::default() }).unwrap(); time(&format!("RGB 8 {name}"), &bytes); } diff --git a/crates/image-codecs/tests/it/tiff_exif.rs b/crates/image-codecs/tests/it/tiff_exif.rs index a1ad5c542..6c08c091f 100644 --- a/crates/image-codecs/tests/it/tiff_exif.rs +++ b/crates/image-codecs/tests/it/tiff_exif.rs @@ -185,9 +185,18 @@ fn overlapping_tiff(entries: usize, count: u32) -> Vec { out.extend(42u16.to_le_bytes()); out.extend(8u32.to_le_bytes()); out.extend(10u16.to_le_bytes()); - for (tag, ty, value) in - [(256, 3, 1), (257, 3, 1), (258, 3, 8), (259, 3, 1), (262, 3, 1), (273, 4, 0), (277, 3, 1), (278, 3, 1), (279, 4, 1), (34665, 4, at_exif as u32)] - { + for (tag, ty, value) in [ + (256, 3, 1), + (257, 3, 1), + (258, 3, 8), + (259, 3, 1), + (262, 3, 1), + (273, 4, 0), + (277, 3, 1), + (278, 3, 1), + (279, 4, 1), + (34665, 4, at_exif as u32), + ] { out.extend(entry(tag, ty, 1, value)); } out.extend(0u32.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/tiff_pages.rs b/crates/image-codecs/tests/it/tiff_pages.rs index fbe4ffb8b..0e589786f 100644 --- a/crates/image-codecs/tests/it/tiff_pages.rs +++ b/crates/image-codecs/tests/it/tiff_pages.rs @@ -338,7 +338,8 @@ fn alpha_after_an_unspecified_extra_sample_is_kept() { // The colour samples, then the second extra sample (the alpha). let want: Vec = want_all.chunks(spp * bps).flat_map(|p| [&p[..color * bps], &p[(color + 1) * bps..]].concat()).collect(); for planar in [false, true] { - let chunks = if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; + let chunks = + if planar { planar_strips(&file, w, h, spp, bps, 2) } else { file.chunks(w * spp * bps * 2).map(<[u8]>::to_vec).collect() }; let mut d = Dir { entries: base(w as u32, h as u32, bits, spp as u16, photometric), chunks, tiled: false } .tag(278, LONG, &[2]) .tag(339, SHORT, &vec![u64::from(format); spp]) @@ -509,7 +510,14 @@ fn every_ifd_and_subifd_is_listed_and_decodable() { use TiffPageKind::*; assert_eq!( summary, - [(4, None, ReducedResolution), (9, None, Page), (3, Some(1), ReducedResolution), (2, Some(1), ReducedResolution), (9, None, Mask), (5, None, Page)] + [ + (4, None, ReducedResolution), + (9, None, Page), + (3, Some(1), ReducedResolution), + (2, Some(1), ReducedResolution), + (9, None, Mask), + (5, None, Page) + ] ); assert_eq!(info.pages[1].ifd_offset, b.ifd_at[1]); assert_eq!((info.default_page(), info.page_count()), (Some(1), 2)); diff --git a/crates/image-codecs/tests/it/tiff_photoshop.rs b/crates/image-codecs/tests/it/tiff_photoshop.rs index 698a4296d..d413ccc34 100644 --- a/crates/image-codecs/tests/it/tiff_photoshop.rs +++ b/crates/image-codecs/tests/it/tiff_photoshop.rs @@ -8,7 +8,8 @@ use craft_image_codecs::*; fn with_tags(layout: ChannelLayout, sample: SampleType) -> Image { let mut img = synth(9, 7, layout, sample, 5, 0.3); - img.meta.photoshop_resources = Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); + img.meta.photoshop_resources = + Some(b"8BIM\x03\xed\x00\x00\x00\x00\x00\x10\x00\x48\x00\x00\x00\x01\x00\x01\x00\x48\x00\x00\x00\x01\x00\x01".to_vec()); let mut layers = b"Adobe Photoshop Document Data Block\0".to_vec(); layers.extend_from_slice(b"MIB8ryaL"); layers.extend_from_slice(&4u32.to_le_bytes()); diff --git a/crates/image-codecs/tests/it/tiff_pyramid.rs b/crates/image-codecs/tests/it/tiff_pyramid.rs index 48c846944..adecd3fc7 100644 --- a/crates/image-codecs/tests/it/tiff_pyramid.rs +++ b/crates/image-codecs/tests/it/tiff_pyramid.rs @@ -51,8 +51,15 @@ fn off_by_default_and_none_for_small_images() { #[test] fn every_depth_layout_and_compression() { - let layouts = - [ChannelLayout::Gray, ChannelLayout::GrayA, ChannelLayout::Rgb, ChannelLayout::Rgba, ChannelLayout::Cmyk, ChannelLayout::CmykA, ChannelLayout::Lab]; + let layouts = [ + ChannelLayout::Gray, + ChannelLayout::GrayA, + ChannelLayout::Rgb, + ChannelLayout::Rgba, + ChannelLayout::Cmyk, + ChannelLayout::CmykA, + ChannelLayout::Lab, + ]; for layout in layouts { for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { for compression in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { @@ -100,7 +107,10 @@ fn first_level_pixel(layout: ChannelLayout, tile: [&[u8]; 4]) -> Vec { fn colour_is_weighted_by_alpha() { // One opaque pixel and three transparent ones of another colour: the level keeps the opaque // colour at a quarter alpha. Unweighted, the transparent colour would bleed in (pink, not red). - assert_eq!(first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), [255, 0, 0, 64]); + assert_eq!( + first_level_pixel(ChannelLayout::Rgba, [&[255, 0, 0, 255], &[255, 255, 255, 0], &[255, 255, 255, 0], &[255, 255, 255, 0]]), + [255, 0, 0, 64] + ); // Gray and CMYK reach the same path with other channel counts. assert_eq!(first_level_pixel(ChannelLayout::GrayA, [&[40, 255], &[255, 0], &[255, 0], &[255, 0]]), [40, 64]); assert_eq!(first_level_pixel(ChannelLayout::CmykA, [&[200, 10, 30, 90, 255], &[0; 5], &[0; 5], &[0; 5]]), [200, 10, 30, 90, 64]); diff --git a/crates/image-codecs/tests/it/tiff_ycbcr.rs b/crates/image-codecs/tests/it/tiff_ycbcr.rs index 15cac9410..0f456bf84 100644 --- a/crates/image-codecs/tests/it/tiff_ycbcr.rs +++ b/crates/image-codecs/tests/it/tiff_ycbcr.rs @@ -141,11 +141,10 @@ fn sixteen_bit_ycbcr_bands_match_whole_decode_with_a_missing_last_strip() { let icc = sample_icc(512); for little in [true, false] { for missing in [false, true] { - let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)).tag(SUBSAMPLING, SHORT, &[1, 1]).tag(YCBCR_COEFFICIENTS, RATIONAL, &weights).bytes( - ICC_PROFILE, - UNDEFINED, - &icc, - ); + let dir = strips(w, h, 16, 3, 6, rows, &u16s(&samples, little)) + .tag(SUBSAMPLING, SHORT, &[1, 1]) + .tag(YCBCR_COEFFICIENTS, RATIONAL, &weights) + .bytes(ICC_PROFILE, UNDEFINED, &icc); let mut file = build(little, false, &[dir], &[0]); if missing { let past = file.bytes.len() as u64 + 4096; @@ -222,7 +221,8 @@ fn tiled_jpeg_ycbcr_keeps_its_icc_profile() { assert_eq!((img.width(), img.height(), img.layout()), (w, h, ChannelLayout::Rgb)); assert_eq!(img.icc.as_deref(), Some(icc.as_slice()), "the embedded profile is kept"); // The oracle: each tile's stream decoded by an independent decoder, placed side by side. - let tiles: Vec> = streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); + let tiles: Vec> = + streams.iter().map(|s| image::load_from_memory_with_format(s, image::ImageFormat::Jpeg).unwrap().to_rgb8().into_raw()).collect(); let row = (t * 3) as usize; let want: Vec = (0..t as usize).flat_map(|y| [&tiles[0][y * row..(y + 1) * row], &tiles[1][y * row..(y + 1) * row]].concat()).collect(); let worst = rgb(&img).iter().zip(&want).map(|(g, o)| g.abs_diff(*o)).max().unwrap(); @@ -248,11 +248,10 @@ fn planar_ycbcr_reads_as_rgb_in_both_byte_orders() { // The pixels of the default-conversion test, one plane per component. let px = [0u8, 128, 128, 255, 128, 128, 128, 128, 128, 76, 85, 255]; for little in [true, false] { - let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false }.tag(278, LONG, &[1]).tag(284, SHORT, &[2]).tag( - SUBSAMPLING, - SHORT, - &[1, 1], - ); + let dir = Dir { entries: base(4, 1, 8, 3, 6), chunks: planar_strips(&px, 4, 1, 3, 1, 1), tiled: false } + .tag(278, LONG, &[1]) + .tag(284, SHORT, &[2]) + .tag(SUBSAMPLING, SHORT, &[1, 1]); let img = decode(&build(little, false, &[dir], &[0]).bytes).unwrap(); assert_eq!(rgb(&img), [0, 0, 0, 255, 255, 255, 128, 128, 128, 254, 0, 0], "little endian: {little}"); } diff --git a/crates/image-codecs/tests/it/webp_frames.rs b/crates/image-codecs/tests/it/webp_frames.rs index c099ec981..2c8e54e98 100644 --- a/crates/image-codecs/tests/it/webp_frames.rs +++ b/crates/image-codecs/tests/it/webp_frames.rs @@ -1,8 +1,8 @@ //! `decode_webp_frames`: every frame of an animated WebP, composited, with its duration (#1198). use craft_image_codecs::{ - AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, encode, - webp_is_animated, + AnimationFrame, ChannelLayout, CodecError, DecodeOptions, DecodeWarning, EncodeOptions, Format, Image, Limits, decode, decode_webp_frames, + encode, webp_is_animated, }; const RED: [u8; 4] = [255, 0, 0, 255]; diff --git a/crates/image-codecs/tests/jxl_export.rs b/crates/image-codecs/tests/jxl_export.rs index 61659f55a..57a91143c 100644 --- a/crates/image-codecs/tests/jxl_export.rs +++ b/crates/image-codecs/tests/jxl_export.rs @@ -23,7 +23,8 @@ fn jpeg_xl_is_export_only_and_detects_both_signatures() { #[test] fn jpeg_xl_encoding_is_deterministic_and_converts_float_to_integer() { let input = - Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]).unwrap(); + Image::from_u8(3, 2, ChannelLayout::Rgba, vec![0, 255, 17, 127, 20, 40, 80, 255, 255, 0, 2, 0, 10, 20, 30, 64, 0, 0, 0, 1, 90, 80, 70, 128]) + .unwrap(); let opts = EncodeOptions::default(); let one = encode(&input, Format::Jxl, &opts).unwrap(); let two = encode(&input, Format::Jxl, &opts).unwrap(); From 8bac629c0fe569a5c2f5e79e62f09dbf4020805b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:46:33 -0500 Subject: [PATCH 03/15] Preserve CFA anchor in shared raw metadata --- crates/raw/src/arw.rs | 4 +++- crates/raw/src/dng.rs | 8 ++++++-- crates/raw/src/erf.rs | 4 +++- crates/raw/src/kodak.rs | 4 +++- crates/raw/src/lib.rs | 19 ++++++++++++++----- crates/raw/src/mef.rs | 4 +++- crates/raw/src/nef.rs | 4 +++- crates/raw/src/pef.rs | 4 +++- crates/raw/src/raf.rs | 4 +++- crates/raw/src/srw.rs | 4 +++- 10 files changed, 44 insertions(+), 15 deletions(-) diff --git a/crates/raw/src/arw.rs b/crates/raw/src/arw.rs index e95d9a0bc..639fc0032 100644 --- a/crates/raw/src/arw.rs +++ b/crates/raw/src/arw.rs @@ -721,7 +721,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= (w * h * 2) as u64 { 14 } else { out_bits }; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), }; let default_black = if scale_bits >= 14 { 512.0 } else { 128.0 }; diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 04b0b8e66..87f23f81f 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -410,9 +410,13 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f // sensor origin (the crop to the active area shifts it back), so move the anchor out by the area's offset // (DNGs that LightCraft wrote before its writer followed the spec carry the sensor-origin pattern; they are marked // by a bare "LightCraft" Software tag and keep their original reading) - let at_origin = Cfa { width: cols, height: rows, pattern }; + let mut at_origin = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; let legacy_writer = [raw, ifd0].iter().any(|i| i.string(t::SOFTWARE).as_deref().map(str::trim) == Some("LightCraft")); - Some(if legacy_writer { at_origin } else { at_origin.shifted(cols - active_area.x % cols, rows - active_area.y % rows) }) + if !legacy_writer { + at_origin.origin_x = active_area.x; + at_origin.origin_y = active_area.y; + } + Some(at_origin) } else { None }; diff --git a/crates/raw/src/erf.rs b/crates/raw/src/erf.rs index 1779f19ad..40190ca20 100644 --- a/crates/raw/src/erf.rs +++ b/crates/raw/src/erf.rs @@ -78,7 +78,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Erf raw without a 2x2 CFAPattern".into())), }; let mn = maker_note(bytes, &tiff); diff --git a/crates/raw/src/kodak.rs b/crates/raw/src/kodak.rs index 40e186d6c..bd517225c 100644 --- a/crates/raw/src/kodak.rs +++ b/crates/raw/src/kodak.rs @@ -68,7 +68,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Kodak raw without a 2x2 CFAPattern".into())), }; let curve: Vec = raw diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 016185675..9ba94db04 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -785,6 +785,11 @@ pub struct Cfa { pub height: usize, /// `width × height` colour indices, row-major, anchored at image pixel (0, 0). pub pattern: Vec, + /// The sensor-space coordinate where this periodic pattern is anchored. + #[serde(default)] + pub origin_x: usize, + #[serde(default)] + pub origin_y: usize, } impl Cfa { @@ -801,13 +806,13 @@ impl Cfa { }) }) .collect::>()?; - (p.len() == 4).then_some(Cfa { width: 2, height: 2, pattern: p }) + (p.len() == 4).then_some(Cfa { width: 2, height: 2, pattern: p, origin_x: 0, origin_y: 0 }) } /// A 2×2 Bayer layout named in code (`"RGGB"`, `"BGGR"`, `"GRBG"`, `"GBRG"`). Only for literal /// names (tested below); anything else falls back to RGGB instead of failing. #[cfg_attr(not(feature = "crw"), allow(dead_code))] pub(crate) fn bayer_static(s: &'static str) -> Cfa { - Cfa::bayer(s).unwrap_or(Cfa { width: 2, height: 2, pattern: vec![0, 1, 1, 2] }) + Cfa::bayer(s).unwrap_or(Cfa { width: 2, height: 2, pattern: vec![0, 1, 1, 2], origin_x: 0, origin_y: 0 }) } /// The Fujifilm X-Trans 6×6 layout (as commonly documented), anchored at (0, 0). pub fn xtrans() -> Cfa { @@ -822,16 +827,20 @@ impl Cfa { }) }) .collect(); - Cfa { width: 6, height: 6, pattern } + Cfa { width: 6, height: 6, pattern, origin_x: 0, origin_y: 0 } } #[inline] pub fn color_at(&self, x: usize, y: usize) -> u8 { - self.pattern[(y % self.height) * self.width + (x % self.width)] + let relative = |v: usize, origin: usize, period: usize| { + let (v, origin) = (v % period, origin % period); + if v >= origin { v - origin } else { period - (origin - v) } + }; + self.pattern[relative(y, self.origin_y, self.height) * self.width + relative(x, self.origin_x, self.width)] } /// The same pattern anchored at `(dx, dy)` of the current anchor (e.g. after cropping to an active area). pub fn shifted(&self, dx: usize, dy: usize) -> Cfa { let pattern = (0..self.height).flat_map(|y| (0..self.width).map(move |x| (x, y))).map(|(x, y)| self.color_at(x + dx, y + dy)).collect(); - Cfa { width: self.width, height: self.height, pattern } + Cfa { width: self.width, height: self.height, pattern, origin_x: 0, origin_y: 0 } } pub fn is_bayer(&self) -> bool { if self.width != 2 || self.height != 2 { diff --git a/crates/raw/src/mef.rs b/crates/raw/src/mef.rs index 90849a7bd..5cd1fb705 100644 --- a/crates/raw/src/mef.rs +++ b/crates/raw/src/mef.rs @@ -70,7 +70,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result 0 && *n <= limits.samples()).ok_or(RawError::Limit("image too large"))?; let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Mef raw without a 2x2 CFAPattern".into())), }; let preview = rgb_preview(&tiff, bytes); diff --git a/crates/raw/src/nef.rs b/crates/raw/src/nef.rs index d378d5cbe..bd880ebb8 100644 --- a/crates/raw/src/nef.rs +++ b/crates/raw/src/nef.rs @@ -281,7 +281,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result= 2 && v[0] > 0.1 && v[1] > 0.1 && v[0] < 10.0 && v[1] < 10.0) .map(|v| [v[0] as f32, 1.0, v[1] as f32]); let cfa = match (raw.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), raw.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), }; let model = ifd0.string(t::MODEL).unwrap_or_default(); diff --git a/crates/raw/src/pef.rs b/crates/raw/src/pef.rs index d5f14bd9a..a33750db5 100644 --- a/crates/raw/src/pef.rs +++ b/crates/raw/src/pef.rs @@ -195,7 +195,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result dark_trimmed(samples, w, hgt, black_point.as_deref().map(|v| v.iter().sum::() / 4.0), white), }; let cfa = match (ifd0.u64s(t::CFA_REPEAT_PATTERN_DIM).as_deref(), ifd0.bytes(t::CFA_PATTERN_EP)) { - (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } // Every body states its layout in the Exif `CFAPattern`, read from the first sample of the stored array, not // of the image area: the *ist D (origin 19, 11) and the 645D (80, 59) only fit unshifted. The 12-bit 10 MP // bodies and everything from the K-3 on say RGGB; the K-7 / K-5 / 645D generation says BGGR. Files without the diff --git a/crates/raw/src/raf.rs b/crates/raw/src/raf.rs index b92f7568f..f75fa160b 100644 --- a/crates/raw/src/raf.rs +++ b/crates/raw/src/raf.rs @@ -125,7 +125,9 @@ fn camera_name(b: &[u8]) -> String { /// The CFA: X-Trans from the (reversed) layout record, else RGGB. fn cfa(h: &Header) -> Cfa { match record(h, XTRANS_LAYOUT) { - Some(l) if l.len() == 36 && l.iter().all(|&c| c <= 2) => Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect() }, + Some(l) if l.len() == 36 && l.iter().all(|&c| c <= 2) => { + Cfa { width: 6, height: 6, pattern: l.iter().rev().copied().collect(), origin_x: 0, origin_y: 0 } + } _ => Cfa::bayer_static("RGGB"), } } diff --git a/crates/raw/src/srw.rs b/crates/raw/src/srw.rs index 3946cbf79..c78ef1f6f 100644 --- a/crates/raw/src/srw.rs +++ b/crates/raw/src/srw.rs @@ -134,7 +134,9 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result Cfa { width: 2, height: 2, pattern: p.to_vec() }, + (Some([2, 2]), Some(p)) if p.len() == 4 && p.iter().all(|&c| c <= 2) => { + Cfa { width: 2, height: 2, pattern: p.to_vec(), origin_x: 0, origin_y: 0 } + } _ => return Err(RawError::Unsupported("Samsung raw without a 2x2 CFAPattern".into())), }; // saturation levels and black levels (R, G1, G2, B), in the file's own words From 85145d8dfd52966eacd045683a86a3737883aeea Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 00:58:35 -0500 Subject: [PATCH 04/15] Expose shared raster source and tone inspection --- crates/image-codecs/src/codecs/via_image.rs | 2 +- crates/image-codecs/src/inspection.rs | 196 ++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 + 3 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 crates/image-codecs/src/inspection.rs diff --git a/crates/image-codecs/src/codecs/via_image.rs b/crates/image-codecs/src/codecs/via_image.rs index 02efc0287..9e5e47b85 100644 --- a/crates/image-codecs/src/codecs/via_image.rs +++ b/crates/image-codecs/src/codecs/via_image.rs @@ -83,7 +83,7 @@ pub(crate) fn encode_bmp_indexed(width: u32, height: u32, indices: &[u8], palett } /// The `image` decoder for `f` with our limits, its header read and its size checked. -fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { +pub(crate) fn open<'a>(f: Format, bytes: &'a [u8], limits: &Limits) -> Result { let fmt = image_format(f).ok_or_else(|| CodecError::unsupported(f, "not handled by image backend"))?; let mut reader = image::ImageReader::with_format(Cursor::new(bytes), fmt); let mut il = image::Limits::default(); diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs new file mode 100644 index 000000000..922cc5ce1 --- /dev/null +++ b/crates/image-codecs/src/inspection.rs @@ -0,0 +1,196 @@ +//! Bounded source-header and tone inspection for raster documents. + +use crate::error::CodecError; +use crate::{ChannelLayout, DecodeOptions, Format, Limits, SampleType, decode_with}; + +/// The source pixel model reported by an image container, before decode normalizes it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum RasterColorType { + Bitmap, + Gray, + GrayAlpha, + Indexed, + Rgb, + Rgba, + Cmyk, + CmykAlpha, + Lab, + Other, +} + +/// Useful source facts, including palette entries when an indexed container exposes them. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct RasterInspection { + pub width: u32, + pub height: u32, + pub channels: u8, + pub bits_per_sample: u8, + pub color_type: RasterColorType, + pub palette: Option>, + pub transparent_index: Option, +} + +/// A grayscale source suitable for single-swatch tinting. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ToneClass { + Bitmap, + Gray, +} + +/// Inspect PSD, PNG, JPEG, TIFF, GIF and BMP source headers without expanding pixel data. +pub fn inspect_raster(bytes: &[u8]) -> Result, CodecError> { + if bytes.starts_with(b"8BPS") { + if bytes.len() < 26 || !matches!(u16::from_be_bytes([bytes[4], bytes[5]]), 1 | 2) { + return Ok(None); + } + let u16_at = |i| u16::from_be_bytes([bytes[i], bytes[i + 1]]); + let height = u32::from_be_bytes(bytes[14..18].try_into().map_err(|_| CodecError::InvalidImage("short PSD header".into()))?); + let width = u32::from_be_bytes(bytes[18..22].try_into().map_err(|_| CodecError::InvalidImage("short PSD header".into()))?); + let color_type = match u16_at(24) { + 0 => RasterColorType::Bitmap, + 1 => RasterColorType::Gray, + 2 => RasterColorType::Indexed, + 3 => RasterColorType::Rgb, + 4 => RasterColorType::Cmyk, + 9 => RasterColorType::Lab, + _ => RasterColorType::Other, + }; + return Ok(Some(RasterInspection { + width, + height, + channels: u16_at(12).min(255) as u8, + bits_per_sample: u16_at(22).min(255) as u8, + color_type, + palette: None, + transparent_index: None, + })); + } + let Some(format) = crate::detect(bytes) else { return Ok(None) }; + let i = match format { + Format::Png => { + let rows = crate::PngDecodeRows::new(bytes, &Limits::default())?; + let h = rows.header(); + let depth = *bytes.get(24).ok_or_else(|| CodecError::malformed(format, "short PNG IHDR"))?; + let ct = *bytes.get(25).ok_or_else(|| CodecError::malformed(format, "short PNG IHDR"))?; + let color = match (ct, depth) { + (0, 1) => RasterColorType::Bitmap, + (0, _) => RasterColorType::Gray, + (2, _) => RasterColorType::Rgb, + (3, _) => RasterColorType::Indexed, + (4, _) => RasterColorType::GrayAlpha, + (6, _) => RasterColorType::Rgba, + _ => RasterColorType::Other, + }; + let palette = crate::png_palette(bytes)?.map(|p| (p.colors, p.transparent)); + let (palette, transparent_index) = palette.map_or((None, None), |(c, t)| (Some(c), t)); + RasterInspection { + width: h.width, + height: h.height, + channels: match ct { + 0 | 3 => 1, + 2 => 3, + 4 => 2, + 6 => 4, + _ => h.layout.channels().min(255) as u8, + }, + bits_per_sample: depth, + color_type: color, + palette, + transparent_index, + } + } + Format::Tiff => { + let info = crate::tiff_info(bytes)?; + let Some(p) = info.default_page().and_then(|n| info.pages.get(n)) else { return Ok(None) }; + let color = match (p.photometric, p.bits_per_sample) { + (Some(0 | 1), 1) => RasterColorType::Bitmap, + (Some(0 | 1), _) => RasterColorType::Gray, + (Some(2 | 6), _) => RasterColorType::Rgb, + (Some(3), _) => RasterColorType::Indexed, + (Some(5), _) => RasterColorType::Cmyk, + (Some(8), _) => RasterColorType::Lab, + _ => RasterColorType::Other, + }; + RasterInspection { + width: p.width, + height: p.height, + channels: p.samples_per_pixel.min(255) as u8, + bits_per_sample: p.bits_per_sample.min(255) as u8, + color_type: color, + palette: None, + transparent_index: None, + } + } + Format::Jpeg => { + let Some((w, h, channels)) = crate::codecs::jpeg::scan_metadata(bytes).frame else { return Ok(None) }; + let depth = craft_jpeg::frame(bytes).map_or(8, |f| f.precision); + let color = match channels { + 1 => RasterColorType::Gray, + 3 => RasterColorType::Rgb, + 4 => RasterColorType::Cmyk, + _ => RasterColorType::Other, + }; + RasterInspection { width: w, height: h, channels, bits_per_sample: depth, color_type: color, palette: None, transparent_index: None } + } + Format::Bmp | Format::Gif => { + use image::ImageDecoder; + let d = crate::codecs::via_image::open(format, bytes, &Limits::default())?; + let (width, height) = d.dimensions(); + let (channels, bits, color) = match d.original_color_type() { + image::ExtendedColorType::L1 => (1, 1, RasterColorType::Bitmap), + image::ExtendedColorType::L2 => (1, 2, RasterColorType::Gray), + image::ExtendedColorType::L4 => (1, 4, RasterColorType::Gray), + image::ExtendedColorType::L8 => (1, 8, RasterColorType::Gray), + image::ExtendedColorType::L16 => (1, 16, RasterColorType::Gray), + image::ExtendedColorType::La8 => (2, 8, RasterColorType::GrayAlpha), + image::ExtendedColorType::La16 => (2, 16, RasterColorType::GrayAlpha), + image::ExtendedColorType::Rgb8 => (3, 8, RasterColorType::Rgb), + image::ExtendedColorType::Rgb16 => (3, 16, RasterColorType::Rgb), + image::ExtendedColorType::Rgba8 => (4, 8, RasterColorType::Rgba), + image::ExtendedColorType::Rgba16 => (4, 16, RasterColorType::Rgba), + _ => (4, 8, RasterColorType::Other), + }; + let (palette, transparent_index) = if format == Format::Gif { + crate::gif_palette(bytes).map_or((None, None), |(p, t)| (Some(p), t)) + } else { + (crate::bmp_palette(bytes)?, None) + }; + let (channels, color) = if format == Format::Gif { (1, RasterColorType::Indexed) } else { (channels, color) }; + RasterInspection { width, height, channels, bits_per_sample: bits, color_type: color, palette, transparent_index } + } + _ => return Ok(None), + }; + Ok(Some(i)) +} + +/// Classify grayscale sources for single-swatch tinting. GIF checks visible decoded pixels and +/// exposes its source palette through [`inspect_raster`]; all other formats use source headers. +pub fn classify_tone(bytes: &[u8]) -> Option { + let i = inspect_raster(bytes).ok()??; + if i.color_type == RasterColorType::Indexed && bytes.starts_with(b"GIF8") { + i.palette.as_ref()?; + let decoded = decode_with(bytes, &DecodeOptions::default()).ok()?; + let rgba = decoded.convert(ChannelLayout::Rgba, SampleType::U8); + let mut opaque = true; + let mut two_level = true; + for p in rgba.data().chunks_exact(4) { + match p[3] { + 0 => opaque = false, + 255 if p[0] == p[1] && p[1] == p[2] => two_level &= p[0] == 0 || p[0] == 255, + _ => return None, + } + } + return if two_level { + Some(ToneClass::Bitmap) + } else if opaque { + Some(ToneClass::Gray) + } else { + None + }; + } + match i.color_type { + RasterColorType::Bitmap => Some(ToneClass::Bitmap), + RasterColorType::Gray => Some(ToneClass::Gray), + _ => None, + } +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 974ff205b..335692a47 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -22,6 +22,7 @@ pub mod exr_stream; mod fidelity; mod format; mod image; +mod inspection; mod lab; mod options; pub mod orientation; @@ -47,6 +48,7 @@ pub use crate::image::{ ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; +pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, inspect_raster}; pub use crate::options::{ DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, TiffCompression, From c6ae97474351732c9e7c2c07eae6abfce9bd9882 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:09:49 -0500 Subject: [PATCH 05/15] Expose shared decoded DNG JPEG XL previews --- crates/raw/src/lib.rs | 9 ++++- crates/raw/src/preview.rs | 85 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 9ba94db04..8c8dd227d 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -146,8 +146,8 @@ pub use opcodes::{Opcode, OpcodeLists}; #[cfg(feature = "arw")] pub use preview::embedded_preview_dynamic_range_optimized; pub use preview::{ - PREVIEW_PREFIX, PreviewColorSpace, PreviewRange, bounded_preview, embedded_preview, embedded_preview_color_space, embedded_preview_covering, - embedded_preview_from_range, + DecodedPreview, PREVIEW_PREFIX, PreviewColorSpace, PreviewRange, bounded_preview, embedded_jpeg_preview, embedded_jpeg_preview_covering, + embedded_jxl_preview, embedded_preview, embedded_preview_color_space, embedded_preview_covering, embedded_preview_from_range, }; #[cfg(feature = "dng")] pub use semantic::{SemanticMask, semantic_masks}; @@ -636,6 +636,11 @@ pub fn probe_info(bytes: &[u8]) -> Result { probe_info_with(bytes, &Limits::default()) } +/// Orientation of a supported raw, read through the same format parser as [`probe_info`]. +pub fn orientation(bytes: &[u8]) -> Option { + probe_info(bytes).ok().map(|info| info.orientation.to_exif()) +} + /// [`probe_info`] within `limits`: a file that declares more than they allow fails as [`decode_with`] would. pub fn probe_info_with(bytes: &[u8], limits: &Limits) -> Result { decode_in(bytes, Mode::Header, limits).map(RawImage::into_info) diff --git a/crates/raw/src/preview.rs b/crates/raw/src/preview.rs index 6917d2588..48fa4e4b1 100644 --- a/crates/raw/src/preview.rs +++ b/crates/raw/src/preview.rs @@ -12,6 +12,73 @@ use craft_tiff::{Ifd, Tiff, makernote}; pub enum PreviewColorSpace { Srgb, AdobeRgb, + ProPhoto, +} + +/// A decoded DNG JPEG XL preview. Pixel samples use the declared bit depth and colour space. +#[derive(Clone, Debug, PartialEq)] +pub struct DecodedPreview { + pub width: u32, + pub height: u32, + pub bits: u32, + pub rgb: Vec, + pub color_space: Option, +} + +/// Decode the largest usable rendered JPEG XL preview in a DNG, within `limits`. +#[cfg(feature = "jxl")] +pub fn embedded_jxl_preview(bytes: &[u8], limits: &crate::Limits) -> Option { + use craft_tiff::image::chunk_bytes; + use craft_tiff::tags as t; + let tiff = Tiff::parse(bytes).ok()?; + let ifd0 = tiff.ifds.first()?; + if !ifd0.contains(t::DNG_VERSION) { + return None; + } + let mut candidates: Vec<_> = tiff + .all_ifds() + .into_iter() + .filter_map(|ifd| { + if ifd.u32(t::NEW_SUBFILE_TYPE)? & 1 == 0 + || ifd.u16(t::COMPRESSION)? != t::compression::JPEG_XL + || ifd.u16(t::SAMPLES_PER_PIXEL)? != 3 + || !matches!(ifd.u16(t::PHOTOMETRIC), Some(t::photometric::RGB | t::photometric::YCBCR)) + { + return None; + } + let info = ifd.image().ok()?; + let chunk = info.single_chunk(bytes.len() as u64)?; + chunk_bytes(bytes, &chunk)?; + Some((u64::from(info.width) * u64::from(info.height), ifd, info)) + }) + .collect(); + candidates.sort_by_key(|(area, _, _)| std::cmp::Reverse(*area)); + for (_, ifd, info) in candidates { + let samples = usize::from(info.samples_per_pixel); + let pixels = usize::try_from(info.width).ok()?.checked_mul(usize::try_from(info.height).ok()?)?; + if pixels.checked_mul(samples)? > limits.samples() { + continue; + } + let Ok(crate::RawData::U16(rgb)) = crate::tiffraw::read_image(bytes, &info, tiff.order, crate::tiffraw::Packing::Msb, limits) else { + continue; + }; + if rgb.len() != pixels.checked_mul(3)? { + continue; + } + let color_space = match ifd.u16(50970).or_else(|| ifd0.u16(50970)) { + Some(2) => Some(PreviewColorSpace::Srgb), + Some(3) => Some(PreviewColorSpace::AdobeRgb), + Some(4) => Some(PreviewColorSpace::ProPhoto), + _ => None, + }; + return Some(DecodedPreview { width: info.width, height: info.height, bits: u32::from(info.bits()), rgb, color_space }); + } + None +} + +#[cfg(not(feature = "jxl"))] +pub fn embedded_jxl_preview(_: &[u8], _: &crate::Limits) -> Option { + None } /// The colour space the raw says its embedded JPEG is in, for a JPEG that declares none itself (no ICC profile, @@ -243,7 +310,17 @@ fn candidates<'a>(data: &'a [u8], ifd: &Ifd, base: u64, out: &mut Vec<&'a [u8]>, /// The largest embedded preview, if any: a JPEG, or (DNG 1.7) a JPEG XL file, or for a Hasselblad 3FR / FFF, which /// has no JPEG, its uncompressed RGB preview wrapped as a baseline TIFF. pub fn embedded_preview(bytes: &[u8]) -> Option> { - preview_covering(bytes, None) + preview_covering(bytes, None, false) +} + +/// Largest embedded JPEG preview, excluding DNG JPEG XL previews. +pub fn embedded_jpeg_preview(bytes: &[u8]) -> Option> { + preview_covering(bytes, None, true) +} + +/// Smallest embedded JPEG of the largest preview's shape whose long edge covers `min_edge`, or the largest JPEG. +pub fn embedded_jpeg_preview_covering(bytes: &[u8], min_edge: u32) -> Option> { + preview_covering(bytes, Some(min_edge), true) } /// The smallest embedded JPEG whose long edge is at least `min_edge` and that looks like a copy of the largest preview @@ -251,7 +328,7 @@ pub fn embedded_preview(bytes: &[u8]) -> Option> { /// grey mask or depth map), else [`embedded_preview`]. Only TIFF-based raws keep several: an A7R VI's 1616 × 1080 /// preview is 0.3 MB beside its 9984 × 6656 one (5.6 MB), so a stand-in up to 1616 px decodes about a twentieth of the data. pub fn embedded_preview_covering(bytes: &[u8], min_edge: u32) -> Option> { - preview_covering(bytes, Some(min_edge)) + preview_covering(bytes, Some(min_edge), false) } /// Of the DCT JPEGs in `found`, the one with the shortest long edge of at least `min_edge` with `largest`'s aspect @@ -273,7 +350,7 @@ fn smallest_covering<'a>(found: &[&'a [u8]], largest: &[u8], min_edge: u32) -> O .map(|(s, _)| s) } -fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { +fn preview_covering(bytes: &[u8], min_edge: Option, jpeg_only: bool) -> Option> { if bytes.starts_with(b"FUJIFILMCCD-RAW") { return raf_jpeg(bytes).filter(|j| is_dct_jpeg(j)).map(|j| trim_eoi(j).to_vec()); } @@ -326,7 +403,7 @@ fn preview_covering(bytes: &[u8], min_edge: Option) -> Option> { found.push(p); } bound_candidates(&mut found, bytes.len()); - let largest = found.iter().copied().filter(|s| is_dct_jpeg(s) || is_jxl(s)).max_by_key(|s| s.len()); + let largest = found.iter().copied().filter(|s| is_dct_jpeg(s) || (!jpeg_only && is_jxl(s))).max_by_key(|s| s.len()); let best = min_edge .zip(largest) .and_then(|(min, big)| smallest_covering(&found, big, min)) From a47b78256c6acf3d7fdc4bc7cfbc25c373eac522 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:15:30 -0500 Subject: [PATCH 06/15] Expose shared embedded ICC extraction --- crates/image-codecs/src/inspection.rs | 8 ++++++++ crates/image-codecs/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs index 922cc5ce1..e263eea3a 100644 --- a/crates/image-codecs/src/inspection.rs +++ b/crates/image-codecs/src/inspection.rs @@ -3,6 +3,14 @@ use crate::error::CodecError; use crate::{ChannelLayout, DecodeOptions, Format, Limits, SampleType, decode_with}; +/// Return an embedded ICC profile using the selected format decoder. +/// +/// This fully decodes the image; callers that already need the pixels should read `Image::meta.icc` +/// from that decode instead of invoking this helper separately. +pub fn embedded_icc(bytes: &[u8]) -> Result>, CodecError> { + Ok(decode_with(bytes, &DecodeOptions::default())?.icc) +} + /// The source pixel model reported by an image container, before decode normalizes it. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum RasterColorType { diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 335692a47..865e59865 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -48,7 +48,7 @@ pub use crate::image::{ ChannelLayout, CryptomatteBuffer, CryptomatteLayer, DecodeWarning, DeepChannel, DeepImage, ExrChannelInfo, ExrPartInfo, ExtraSample, Image, MAX_EXTRA_SAMPLES, Metadata, SampleType, convert_samples, }; -pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, inspect_raster}; +pub use crate::inspection::{RasterColorType, RasterInspection, ToneClass, classify_tone, embedded_icc, inspect_raster}; pub use crate::options::{ DdsCompression, DdsOptions, DecodeOptions, DpxBits, EncodeOptions, ExrCompression, JpegFormat, Limits, PngCompression, PngStereo, TIFF_PYRAMID_MIN, TiffCompression, From fdfb18885507e60604d3188078e3a34df82128ec Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:24:36 -0500 Subject: [PATCH 07/15] Use array chunks for raster pixels --- crates/image-codecs/src/inspection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/image-codecs/src/inspection.rs b/crates/image-codecs/src/inspection.rs index e263eea3a..b5dc0c95b 100644 --- a/crates/image-codecs/src/inspection.rs +++ b/crates/image-codecs/src/inspection.rs @@ -181,7 +181,7 @@ pub fn classify_tone(bytes: &[u8]) -> Option { let rgba = decoded.convert(ChannelLayout::Rgba, SampleType::U8); let mut opaque = true; let mut two_level = true; - for p in rgba.data().chunks_exact(4) { + for p in rgba.data().as_chunks::<4>().0 { match p[3] { 0 => opaque = false, 255 if p[0] == p[1] && p[1] == p[2] => two_level &= p[0] == 0 || p[0] == 255, From bdba99322ea1eb4807aab17ac4cffe53d62a4b89 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:28:04 -0500 Subject: [PATCH 08/15] Use canonical winprint repository URL --- Cargo.lock | 20 ++++++++++---------- crates/print/Cargo.toml | 5 ++--- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5ff17d87..3c0f9c7e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3034,7 +3034,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -3581,7 +3581,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -5804,7 +5804,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7118,7 +7118,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7688,7 +7688,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -7757,7 +7757,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -8930,10 +8930,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.4.3", + "getrandom 0.3.4", "once_cell", "rustix 1.1.5", - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -10085,7 +10085,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -10355,7 +10355,7 @@ dependencies = [ [[package]] name = "winprint" version = "0.2.1" -source = "git+https://github.com/dotCooCoo/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" +source = "git+https://github.com/storytold/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" dependencies = [ "flate2", "fmt-derive", diff --git a/crates/print/Cargo.toml b/crates/print/Cargo.toml index 50fbb43d6..36b8813f2 100644 --- a/crates/print/Cargo.toml +++ b/crates/print/Cargo.toml @@ -35,9 +35,8 @@ log = "0.4" # ticket (winprint.rs#12, storytold/pdfcraft#3823), and reads whitespace-padded integers in # PrintCapabilities (winprint.rs#3, storytold/pdfcraft#2430). Pinned to an immutable revision; # review before bumping. The settings chosen in a driver's own dialog become a ticket through -# `PrintTicket::from_dev_mode`: until it is merged into storytold/winprint.rs, the pin is the -# fork commit that adds it on top of storytold/winprint.rs main (e90c444). -winprint = { git = "https://github.com/dotCooCoo/winprint.rs", rev = "72951d7455ebcab569fbd9386c513d3d9f53fb02", default-features = false } +# `PrintTicket::from_dev_mode`. +winprint = { git = "https://github.com/storytold/winprint.rs", rev = "72951d7455ebcab569fbd9386c513d3d9f53fb02", default-features = false } # GetDefaultPrinter (MIT). winsafe = { version = "0.0.29", default-features = false, features = ["winspool"] } # The job file's private folder. From e356ff84a4b5009518b1995e559675866e611d8f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:38:06 -0500 Subject: [PATCH 09/15] Initialize raster decode limits directly --- crates/raster-codecs/src/common.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index ec61aeedd..2d25f9f41 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -31,11 +31,13 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result limits.max_file_bytes { return Err(CodecError::LimitExceeded(format!("{} encoded bytes exceed max {}", bytes.len(), limits.max_file_bytes))); } - let mut decode_limits = craft_image_codecs::Limits::default(); - decode_limits.max_width = limits.max_width; - decode_limits.max_height = limits.max_height; - decode_limits.max_pixels = limits.max_pixels; - decode_limits.max_alloc = limits.max_alloc_bytes; + let decode_limits = craft_image_codecs::Limits { + max_width: limits.max_width, + max_height: limits.max_height, + max_pixels: limits.max_pixels, + max_alloc: limits.max_alloc_bytes, + ..Default::default() + }; let decoded = craft_image_codecs::decode_with(bytes, &craft_image_codecs::DecodeOptions { limits: decode_limits, ..Default::default() }) .map_err(map_codec_error)?; let output_bytes = u64::from(decoded.width()).saturating_mul(u64::from(decoded.height())).saturating_mul(4); From 80762908548a215dd450b53b781fcd562105ff95 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 01:44:36 -0500 Subject: [PATCH 10/15] Avoid needless raster limit update --- crates/raster-codecs/src/common.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/raster-codecs/src/common.rs b/crates/raster-codecs/src/common.rs index 2d25f9f41..925935871 100644 --- a/crates/raster-codecs/src/common.rs +++ b/crates/raster-codecs/src/common.rs @@ -36,7 +36,6 @@ pub fn decode_rgba(bytes: &[u8], limits: DecodeLimits) -> Result Date: Fri, 13 Nov 2026 01:51:42 -0500 Subject: [PATCH 11/15] Probe maximum raster frame allocation bounds --- crates/image-codecs/src/allocation.rs | 194 ++++++++++++++++++++++++++ crates/image-codecs/src/lib.rs | 2 + 2 files changed, 196 insertions(+) create mode 100644 crates/image-codecs/src/allocation.rs diff --git a/crates/image-codecs/src/allocation.rs b/crates/image-codecs/src/allocation.rs new file mode 100644 index 000000000..5425a0c24 --- /dev/null +++ b/crates/image-codecs/src/allocation.rs @@ -0,0 +1,194 @@ +//! Predecode bounds for raster containers whose frames may exceed their declared canvas. + +use crate::codecs::webp; +use crate::error::CodecError; +use crate::format::Format; +use crate::image::{ChannelLayout, SampleType}; +use crate::options::Limits; + +/// The largest width and height any decoded frame can declare, without allocating pixel data. +/// Animated GIF and WebP frames are inspected individually, including frames larger than the +/// animation canvas. Other recognized raster formats use their container's image dimensions. +pub fn probe_allocation_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let format = crate::detect(bytes).ok_or(CodecError::UnknownFormat)?; + let (width, height) = match format { + Format::Gif => gif_bounds(bytes)?, + Format::WebP => { + let (width, height, _) = webp::probe(bytes, &Limits::default())?; + let (frame_width, frame_height) = webp_bounds(bytes)?; + (width.max(frame_width), height.max(frame_height)) + } + _ => { + if let Some(info) = crate::inspect_raster(bytes)? { + (info.width, info.height) + } else { + crate::probe_size(bytes)? + } + } + }; + Limits::default().check(width, height, ChannelLayout::Rgba, SampleType::U8)?; + Ok((width, height)) +} + +fn malformed(format: Format, message: &'static str) -> CodecError { + CodecError::malformed(format, message) +} + +fn u16_le(bytes: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(bytes.get(at..at.checked_add(2)?)?.try_into().ok()?)) +} + +fn gif_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::Gif, "truncated or malformed GIF frame table"); + if !bytes.starts_with(b"GIF87a") && !bytes.starts_with(b"GIF89a") { + return Err(bad()); + } + let packed = *bytes.get(10).ok_or_else(bad)?; + let (mut width, mut height) = (u32::from(u16_le(bytes, 6).ok_or_else(bad)?), u32::from(u16_le(bytes, 8).ok_or_else(bad)?)); + let table_len = + if packed & 0x80 != 0 { 3usize.checked_mul(1usize.checked_shl(u32::from((packed & 7) + 1)).ok_or_else(bad)?).ok_or_else(bad)? } else { 0 }; + let mut at = 13usize.checked_add(table_len).ok_or_else(bad)?; + if bytes.get(..at).is_none() || width == 0 || height == 0 { + return Err(bad()); + } + let mut frames = 0usize; + loop { + match bytes.get(at).copied().ok_or_else(bad)? { + 0x3b => break, + 0x21 => { + at = at.checked_add(2).ok_or_else(bad)?; + loop { + let length = usize::from(*bytes.get(at).ok_or_else(bad)?); + at = at.checked_add(1).ok_or_else(bad)?; + if length == 0 { + break; + } + at = at.checked_add(length).ok_or_else(bad)?; + if bytes.get(..at).is_none() { + return Err(bad()); + } + } + } + 0x2c => { + let left = u32::from(u16_le(bytes, at + 1).ok_or_else(bad)?); + let top = u32::from(u16_le(bytes, at + 3).ok_or_else(bad)?); + let frame_width = u32::from(u16_le(bytes, at + 5).ok_or_else(bad)?); + let frame_height = u32::from(u16_le(bytes, at + 7).ok_or_else(bad)?); + let descriptor = bytes.get(at..at.checked_add(10).ok_or_else(bad)?).ok_or_else(bad)?; + let flags = descriptor[9]; + let mut body = at.checked_add(10).ok_or_else(bad)?; + if flags & 0x80 != 0 { + let table = 3usize.checked_mul(1usize.checked_shl(u32::from((flags & 7) + 1)).ok_or_else(bad)?).ok_or_else(bad)?; + body = body.checked_add(table).ok_or_else(bad)?; + } + body = body.checked_add(1).ok_or_else(bad)?; // LZW minimum code size. + if bytes.get(..body).is_none() || frame_width == 0 || frame_height == 0 { + return Err(bad()); + } + width = width.max(left.saturating_add(frame_width)); + height = height.max(top.saturating_add(frame_height)); + at = body; + loop { + let length = usize::from(*bytes.get(at).ok_or_else(bad)?); + at = at.checked_add(1).ok_or_else(bad)?; + if length == 0 { + break; + } + at = at.checked_add(length).ok_or_else(bad)?; + if bytes.get(..at).is_none() { + return Err(bad()); + } + } + frames = frames.saturating_add(1); + } + _ => return Err(bad()), + } + } + if frames == 0 { + return Err(bad()); + } + Ok((width, height)) +} + +fn webp_bounds(bytes: &[u8]) -> Result<(u32, u32), CodecError> { + let bad = || malformed(Format::WebP, "truncated or malformed WebP frame table"); + if bytes.get(0..4) != Some(b"RIFF") || bytes.get(8..12) != Some(b"WEBP") { + return Err(bad()); + } + let riff_len = usize::try_from(u32::from_le_bytes(bytes.get(4..8).ok_or_else(bad)?.try_into().map_err(|_| bad())?)).map_err(|_| bad())?; + let end = 8usize.checked_add(riff_len).ok_or_else(bad)?; + if end > bytes.len() || end < 12 { + return Err(bad()); + } + let mut max_width = 0u32; + let mut max_height = 0u32; + webp_chunks(bytes, 12, end, false, &mut max_width, &mut max_height)?; + if max_width == 0 || max_height == 0 { + return Err(bad()); + } + Ok((max_width, max_height)) +} + +fn webp_chunks(bytes: &[u8], mut at: usize, end: usize, nested: bool, max_width: &mut u32, max_height: &mut u32) -> Result<(), CodecError> { + let bad = || malformed(Format::WebP, "truncated or malformed WebP frame table"); + while at < end { + let header_end = at.checked_add(8).ok_or_else(bad)?; + let header = bytes.get(at..header_end).filter(|_| header_end <= end).ok_or_else(bad)?; + let size = usize::try_from(u32::from_le_bytes(header.get(4..8).ok_or_else(bad)?.try_into().map_err(|_| bad())?)).map_err(|_| bad())?; + let from = header_end; + let to = from.checked_add(size).ok_or_else(bad)?; + let payload = bytes.get(from..to).filter(|_| to <= end).ok_or_else(bad)?; + match header.get(..4).ok_or_else(bad)? { + b"VP8 " => { + if payload.get(3..6) != Some(&[0x9d, 0x01, 0x2a]) || payload.len() < 10 { + return Err(bad()); + } + let w = u32::from(u16::from_le_bytes([payload[6], payload[7]]) & 0x3fff); + let h = u32::from(u16::from_le_bytes([payload[8], payload[9]]) & 0x3fff); + *max_width = (*max_width).max(w); + *max_height = (*max_height).max(h); + } + b"VP8L" => { + if payload.len() < 5 || payload[0] != 0x2f { + return Err(bad()); + } + let bits = u32::from_le_bytes([payload[1], payload[2], payload[3], payload[4]]); + *max_width = (*max_width).max((bits & 0x3fff) + 1); + *max_height = (*max_height).max(((bits >> 14) & 0x3fff) + 1); + } + b"VP8X" => { + if nested || payload.len() < 10 { + return Err(bad()); + } + *max_width = (*max_width).max(read_le24(payload, 4).ok_or_else(bad)?.saturating_add(1)); + *max_height = (*max_height).max(read_le24(payload, 7).ok_or_else(bad)?.saturating_add(1)); + } + b"ANMF" => { + if nested || payload.len() < 16 { + return Err(bad()); + } + let x = read_le24(payload, 0).ok_or_else(bad)?.saturating_mul(2); + let y = read_le24(payload, 3).ok_or_else(bad)?.saturating_mul(2); + let w = read_le24(payload, 6).ok_or_else(bad)?.saturating_add(1); + let h = read_le24(payload, 9).ok_or_else(bad)?.saturating_add(1); + *max_width = (*max_width).max(x.saturating_add(w)); + *max_height = (*max_height).max(y.saturating_add(h)); + webp_chunks(payload, 16, payload.len(), true, max_width, max_height)?; + } + _ => {} + } + at = to.checked_add(size & 1).ok_or_else(bad)?; + if at > end { + return Err(bad()); + } + } + if at != end { + return Err(bad()); + } + Ok(()) +} + +fn read_le24(bytes: &[u8], at: usize) -> Option { + let b = bytes.get(at..at.checked_add(3)?)?; + Some(u32::from_le_bytes([b[0], b[1], b[2], 0])) +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 865e59865..e02c438ec 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -14,6 +14,7 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +mod allocation; pub mod cicp; mod codecs; mod error; @@ -29,6 +30,7 @@ pub mod orientation; pub mod resolution; pub mod source_settings; pub mod web; +pub use crate::allocation::probe_allocation_size; pub use crate::cicp::Cicp; pub use crate::codecs::exr_cryptomatte::{cryptomatte_id, cryptomatte_key, cryptomatte_preview_color}; pub use crate::codecs::gif_frames::{AnimationFrame, MAX_GIF_FRAMES}; From b9992dbebd531c7362c087c93910b5113cc88ac8 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 02:55:56 -0500 Subject: [PATCH 12/15] Canonicalize DNG CFA at sensor origin --- crates/raw/src/dng.rs | 17 +++++++---------- crates/raw/src/lib.rs | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index 87f23f81f..7c5b361cd 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -406,17 +406,14 @@ fn decode_ifd(bytes: &[u8], tiff: &Tiff, pixels: &Ifd, main: &Ifd, mode: Mode, f if cpp != 1 { return Err(RawError::Unsupported("CFA data with several samples per pixel".into())); } - // the tag is anchored at the top-left of the ActiveArea (DNG spec); `RawImage::cfa` is anchored at the - // sensor origin (the crop to the active area shifts it back), so move the anchor out by the area's offset - // (DNGs that LightCraft wrote before its writer followed the spec carry the sensor-origin pattern; they are marked - // by a bare "LightCraft" Software tag and keep their original reading) - let mut at_origin = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; + // DNG's CFAPattern is anchored at ActiveArea. Store one canonical CFA anchored at the + // sensor origin instead: invert the ActiveArea phase before building RawImage::cfa. + // Older LightCraft files wrote the pattern at the sensor origin and are identified by + // their bare Software tag, so those keep the bytes as written. + let at_area = Cfa { width: cols, height: rows, pattern, origin_x: 0, origin_y: 0 }; let legacy_writer = [raw, ifd0].iter().any(|i| i.string(t::SOFTWARE).as_deref().map(str::trim) == Some("LightCraft")); - if !legacy_writer { - at_origin.origin_x = active_area.x; - at_origin.origin_y = active_area.y; - } - Some(at_origin) + let cfa = if legacy_writer { at_area } else { at_area.shifted((cols - active_area.x % cols) % cols, (rows - active_area.y % rows) % rows) }; + Some(cfa) } else { None }; diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 8c8dd227d..c9c072122 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -862,7 +862,7 @@ impl Cfa { } /// Pattern name like `"RGGB"` for 2×2 patterns. pub fn name(&self) -> String { - self.pattern.iter().map(|&c| ['R', 'G', 'B', '?'][c.min(3) as usize]).collect() + self.shifted(0, 0).pattern.iter().map(|&c| ['R', 'G', 'B', '?'][c.min(3) as usize]).collect() } fn valid(&self) -> bool { self.width > 0 && self.height > 0 && self.width <= 16 && self.height <= 16 && self.pattern.len() == self.width * self.height From 34f2d55dc0ba5278f7499170b368b2ce55265f68 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 13 Nov 2026 04:36:50 -0500 Subject: [PATCH 13/15] Match Apple HEIF chroma reconstruction --- crates/image-codecs/src/codecs/heif.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/image-codecs/src/codecs/heif.rs b/crates/image-codecs/src/codecs/heif.rs index 775891ed8..c3fb33508 100644 --- a/crates/image-codecs/src/codecs/heif.rs +++ b/crates/image-codecs/src/codecs/heif.rs @@ -55,7 +55,14 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R if !keep_orientation && (info.coded_width, info.coded_height) != (w, h) { limits.check(info.coded_width, info.coded_height, layout, sample)?; } - let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, ..Default::default() }; + let options = craft_heif::Options { + max_pixels: limits.max_pixels, + apply_transforms: !keep_orientation, + // PhotoCraft's HEIF fixtures and export expectations match Apple's decoder. In + // particular, its left-sited bilinear chroma reconstruction differs at sharp edges + // from the libheif default, even though both produce the same RGB strip centres. + chroma: craft_heif::Chroma::Apple, + }; let decoded = craft_heif::decode(bytes, &options).map_err(err)?; // The decoder re-reads the container; trust its output's shape, not the probe's. let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb }; From c82c83043abee59cafbc6241844bf95b7da92ed2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 03:12:15 +0000 Subject: [PATCH 14/15] Update lockfile after main merge --- Cargo.lock | 418 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 402 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a4d8e0e9..3f5257951 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,24 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aligned" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4508988c62edf04abd8d92897fca0c2995d907ce1dfeaf369dac3716a40685" +dependencies = [ + "as-slice", +] + +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + [[package]] name = "alloc-no-stdlib" version = "2.0.4" @@ -204,6 +222,23 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "argon2" version = "0.5.3" @@ -238,6 +273,15 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -390,6 +434,49 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "av-scenechange" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f321d77c20e19b92c39e7471cf986812cbb46659d2af674adc4331ef3f18394" +dependencies = [ + "aligned", + "anyhow", + "arg_enum_proc_macro", + "arrayvec", + "log", + "num-rational", + "num-traits", + "pastey", + "rayon", + "thiserror 2.0.21", + "v_frame", + "y4m", +] + +[[package]] +name = "av1-grain" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cfddb07216410377231960af4fcab838eaa12e013417781b78bd95ee22077f8" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom 8.0.0", + "num-rational", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7178fe5f7d460b13895ebb9dcb28a3a6216d2df2574a0806cb51b555d297f38" +dependencies = [ + "arrayvec", +] + [[package]] name = "aws-lc-rs" version = "1.18.1" @@ -517,6 +604,15 @@ version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" +[[package]] +name = "bitstream-io" +version = "4.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eff00be299a18769011411c9def0d827e8f2d7bf0c3dbf53633147a8867fd1f" +dependencies = [ + "no_std_io2", +] + [[package]] name = "bitvec" version = "1.1.1" @@ -727,6 +823,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "built" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0e531d93d39c34eef561e929e8a7f86d77a5af08aac4f6d6e39976c51858e9" + [[package]] name = "bumpalo" version = "3.20.3" @@ -1727,6 +1829,30 @@ dependencies = [ "zip", ] +[[package]] +name = "craft-image-codecs" +version = "0.1.0" +dependencies = [ + "craft-heif", + "craft-jpeg", + "exr", + "flate2", + "half", + "image", + "image-webp", + "jpeg-encoder 0.6.1", + "jxl-oxide", + "png 0.18.1", + "proptest", + "rayon", + "serde_json", + "thiserror 2.0.21", + "tiff", + "zune-core", + "zune-jpeg", + "zune-jpegxl", +] + [[package]] name = "craft-indd" version = "0.1.0" @@ -2255,6 +2381,7 @@ name = "craft-raster-codecs" version = "0.1.0" dependencies = [ "base64 0.23.1", + "craft-image-codecs", "image", "rayon", "thiserror 2.0.21", @@ -3341,7 +3468,7 @@ dependencies = [ "bitflags 2.13.2", "emath", "epaint", - "itertools", + "itertools 0.15.0", "log", "nohash-hasher", "profiling", @@ -3398,7 +3525,7 @@ dependencies = [ "ahash", "egui", "enum-map", - "itertools", + "itertools 0.15.0", "log", "profiling", "resvg", @@ -3627,6 +3754,26 @@ name = "epaint_default_fonts" version = "0.37.0" source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -3683,6 +3830,7 @@ dependencies = [ "lebe", "miniz_oxide 0.8.9", "multiversion", + "rayon-core", "smallvec", "zune-inflate", ] @@ -4930,6 +5078,9 @@ dependencies = [ "moxcms", "num-traits", "png 0.18.1", + "qoi", + "ravif", + "rgb", "tiff", "zune-core", "zune-jpeg", @@ -4963,6 +5114,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65b27460c2c92b037f3f94c538ed9a3342f3fdf923606781629ccb35f82d042a" +[[package]] +name = "imgref" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e44b0a4eaa4c82f441d50a963f2d5f05a787240aeee097597033e72accfd22f" + [[package]] name = "indd" version = "0.1.0" @@ -4997,6 +5154,17 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "io-extras" version = "0.19.0" @@ -5044,6 +5212,15 @@ dependencies = [ "once_cell", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.15.0" @@ -5314,6 +5491,8 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25f15eb830aa77a7f21148d72e153562a26bfe570139bd4922eab1908dd499d3" dependencies = [ + "rayon", + "rayon-core", "tracing", ] @@ -5474,6 +5653,16 @@ version = "0.2.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + [[package]] name = "libloading" version = "0.8.9" @@ -5561,6 +5750,15 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" +[[package]] +name = "loop9" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fae87c125b03c1d2c0150c90365d7d6bcc53fb73a9acaef207d2d065860f062" +dependencies = [ + "imgref", +] + [[package]] name = "lru-slab" version = "0.1.3" @@ -5573,6 +5771,15 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4facc753ae494aeb6e3c22f839b158aebd4f9270f55cd3c79906c45476c47ab4" +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", +] + [[package]] name = "md-5" version = "0.10.6" @@ -5794,6 +6001,12 @@ dependencies = [ "jni-sys 0.3.1", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nix" version = "0.31.3" @@ -5806,6 +6019,15 @@ dependencies = [ "libc", ] +[[package]] +name = "no_std_io2" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" +dependencies = [ + "memchr", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -5831,6 +6053,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -5840,6 +6068,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint-dig" version = "0.8.6" @@ -5872,6 +6110,17 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "num-integer" version = "0.1.47" @@ -5891,6 +6140,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -6466,6 +6726,18 @@ dependencies = [ "subtle", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "pastey" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec" + [[package]] name = "pbkdf2" version = "0.13.0" @@ -7010,6 +7282,19 @@ name = "profiling" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" +dependencies = [ + "quote", + "syn 2.0.119", +] [[package]] name = "proptest" @@ -7037,6 +7322,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" @@ -7158,10 +7452,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" dependencies = [ "libc", - "rand_chacha", + "rand_chacha 0.3.1", "rand_core 0.6.4", ] +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + [[package]] name = "rand" version = "0.10.3" @@ -7183,6 +7487,16 @@ dependencies = [ "rand_core 0.6.4", ] +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -7192,6 +7506,15 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + [[package]] name = "rand_core" version = "0.10.1" @@ -7222,6 +7545,55 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca45419789ae5a7899559e9512e58ca889e41f04f1f2445e9f4b290ceccd1d08" +[[package]] +name = "rav1e" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43b6dd56e85d9483277cde964fd1bdb0428de4fec5ebba7540995639a21cb32b" +dependencies = [ + "aligned-vec", + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av-scenechange", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.14.0", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "paste", + "profiling", + "rand 0.9.5", + "rand_chacha 0.9.0", + "simd_helpers", + "thiserror 2.0.21", + "v_frame", + "wasm-bindgen", +] + +[[package]] +name = "ravif" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e52310197d971b0f5be7fe6b57530dcd27beb35c1b013f29d66c1ad73fbbcc45" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error 2.0.1", + "rav1e", + "rgb", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -8270,6 +8642,15 @@ dependencies = [ "simdutf8", ] +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "simdutf8" version = "0.1.5" @@ -9503,6 +9884,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "v_frame" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + [[package]] name = "vectorcraft-brush" version = "0.18.0" @@ -9718,18 +10110,6 @@ dependencies = [ "vectorcraft-pathops", ] -[[package]] -name = "v_frame" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "666b7727c8875d6ab5db9533418d7c764233ac9c0cff1d469aec8fa127597be2" -dependencies = [ - "aligned-vec", - "num-traits", - "wasm-bindgen", -] - - [[package]] name = "vello_common" version = "0.0.8" @@ -10571,7 +10951,7 @@ dependencies = [ [[package]] name = "winprint" version = "0.2.1" -source = "git+https://github.com/dotCooCoo/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" +source = "git+https://github.com/storytold/winprint.rs?rev=72951d7455ebcab569fbd9386c513d3d9f53fb02#72951d7455ebcab569fbd9386c513d3d9f53fb02" dependencies = [ "flate2", "fmt-derive", @@ -10806,6 +11186,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9440ea3e5aeabb0ac63af70daf835274065238cdd0cec83418f417eae38bacee" +[[package]] +name = "y4m" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" + [[package]] name = "yeslogic-fontconfig-sys" version = "6.0.1" From 49056e5408186909e23f4d1eae35b047b1242586 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 14 Nov 2026 03:21:46 +0000 Subject: [PATCH 15/15] image-codecs: align JPEG tests with progressive support --- crates/image-codecs/src/codecs/jpeg.rs | 50 ++++++++++++++++++++------ 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index e2e854622..8e68451ef 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -245,9 +245,8 @@ fn without_extended_xmp(b: &[u8]) -> Option> { } /// Whether `craft-jpeg` (rather than `zune-jpeg`) decodes the file: arithmetic coding -/// (SOF9–SOF11) or a DCT frame whose precision isn't 8 bits (#4934, #5003). Lossless (SOF3) and -/// 12-bit progressive Huffman (SOF2) go there too: craft-jpeg reports them unsupported, where -/// zune-jpeg's failure would read as a damaged file. +/// (SOF9–SOF11), lossless (SOF3), or a DCT frame whose precision isn't 8 bits (#4934, #5003). +/// This includes 12-bit progressive Huffman (SOF2), which `craft-jpeg` supports. fn uses_craft_jpeg(bytes: &[u8]) -> bool { match craft_jpeg::frame(bytes) { Some(f) if (0xC9..=0xCB).contains(&f.marker) || f.marker == 0xC3 => true, @@ -800,6 +799,24 @@ mod tests { gray_block(0xC1, 12, Some(&dht), &word.to_be_bytes()) } + /// A valid 12-bit progressive DC-first scan with a one-bit zero-DC Huffman code. + fn twelve_bit_progressive_dc_block() -> Vec { + let mut s = vec![0xFF, 0xD8]; + let mut dqt = vec![0u8]; + dqt.extend([1u8; 64]); + segment(&mut s, 0xDB, &dqt); + segment(&mut s, 0xC2, &[12, 0, 8, 0, 8, 1, 1, 0x11, 0]); + let mut dht = vec![0x00, 1]; + dht.extend([0u8; 15]); + dht.push(0); + segment(&mut s, 0xC4, &dht); + // One component, DC table 0, DC coefficients only, first approximation. + segment(&mut s, 0xDA, &[1, 1, 0, 0, 0, 0]); + s.push(0x7F); // DC category 0 followed by one padding bits. + s.extend([0xFF, 0xD9]); + s + } + fn twelve_bit(v: f32) -> u16 { (v * 65535.0 / 4095.0).round() as u16 } @@ -818,13 +835,26 @@ mod tests { } #[test] - fn lossless_and_twelve_bit_progressive_are_unsupported_not_damaged() { - for marker in [0xC3, 0xC2] { - let mut bytes = vec![0xFF, 0xD8, 0xFF, marker, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0]; - bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0x12, 0x34, 0xFF, 0xD9]); - let e = crate::decode(&bytes).unwrap_err(); - assert!(matches!(e, CodecError::Unsupported { .. }), "{marker:#x}: {e:?}"); - } + fn lossless_is_unsupported_not_damaged() { + let bytes = gray_block(0xC3, 12, None, &[0x12, 0x34]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(matches!(e, CodecError::Unsupported { .. }), "{e:?}"); + } + + #[test] + fn twelve_bit_progressive_dc_scan_decodes() { + let img = crate::decode(&twelve_bit_progressive_dc_block()).unwrap(); + assert_eq!((img.dimensions(), img.sample_type(), img.layout()), ((8, 8), SampleType::U16, ChannelLayout::Gray)); + assert!(img.warnings.is_empty()); + } + + #[test] + fn malformed_twelve_bit_progressive_scan_is_malformed_not_damaged() { + // A DC-first progressive scan must have Se=0, not the sequential scan's Se=63. + let mut bytes = vec![0xFF, 0xD8, 0xFF, 0xC2, 0, 11, 12, 0, 8, 0, 8, 1, 1, 0x11, 0]; + bytes.extend_from_slice(&[0xFF, 0xDA, 0, 8, 1, 1, 0, 0, 0x3F, 0, 0x12, 0x34, 0xFF, 0xD9]); + let e = crate::decode(&bytes).unwrap_err(); + assert!(matches!(e, CodecError::Malformed { .. }), "{e:?}"); } #[test]