From 1b4dcb4afa10659855e86052b124e9a319ec75c5 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 20 Nov 2026 10:07:04 +0100 Subject: [PATCH 1/3] image-codecs: make JPEG 2000 decode an opt-in jp2 feature hayro-jpeg2000 was a plain dependency, so every crate that reached craft-image-codecs carried a JPEG 2000 decoder: craft-svg's tree feature, and through it any SVG or raster reader, including apps that export PDF and keep hayro out of their production graph with a guard. The decoder now sits behind jp2, off by default, in the shape sgi and sunraster already use: the module and the dependency are gated, caps().read reports the build, and a JP2 file without the feature is still detected and refused with the feature named. craft-file-formats gains the matching jp2 flag. Detection and the jp2_probe size reader need no decoder and stay on. --- crates/file-formats/Cargo.toml | 1 + crates/image-codecs/Cargo.toml | 5 ++++- crates/image-codecs/README.md | 1 + crates/image-codecs/src/codecs.rs | 1 + crates/image-codecs/src/format.rs | 8 ++++++- crates/image-codecs/src/lib.rs | 17 ++++++++++++--- crates/image-codecs/tests/it/jp2_feature.rs | 24 +++++++++++++++++++++ crates/image-codecs/tests/it/main.rs | 1 + 8 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 crates/image-codecs/tests/it/jp2_feature.rs diff --git a/crates/file-formats/Cargo.toml b/crates/file-formats/Cargo.toml index afe7c6391d..293bcfaeea 100644 --- a/crates/file-formats/Cargo.toml +++ b/crates/file-formats/Cargo.toml @@ -27,6 +27,7 @@ default = [] raster-codecs = ["dep:craft-image-codecs"] heif = ["raster-codecs", "craft-image-codecs/heif"] avif = ["raster-codecs", "craft-image-codecs/avif"] +jp2 = ["raster-codecs", "craft-image-codecs/jp2"] pdf = [] svg = [] ora = ["dep:craft-ora"] diff --git a/crates/image-codecs/Cargo.toml b/crates/image-codecs/Cargo.toml index 46d5103426..f1f969ba0c 100644 --- a/crates/image-codecs/Cargo.toml +++ b/crates/image-codecs/Cargo.toml @@ -25,6 +25,9 @@ jxl = ["dep:craft-jxl"] # AVIF decode: craft-video's clean-room pure-Rust AV1 decoder. Native targets only (on wasm32 a # decoder panic aborts instead of becoming an error), so wasm32 builds leave AVIF unreadable. avif-decode = ["dep:craft-video", "craft-video/av1-decode"] +# JPEG 2000 decode via hayro-jpeg2000. Off by default so a crate that only reads SVG or raster +# pixels does not carry a JPEG 2000 decoder (a PDF reader family) into its graph. +jp2 = ["dep:hayro-jpeg2000"] [dependencies] craft-dds = { workspace = true } @@ -50,7 +53,7 @@ craft-jxl = { workspace = true, optional = true, features = ["decode"] } ravif = { version = "0.13", default-features = false, optional = true } rav1e = { version = "0.8", default-features = false, optional = true } avif-serialize = { version = "0.8", optional = true } -hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"] } +hayro-jpeg2000 = { version = "=0.3.5", default-features = false, features = ["std", "simd"], optional = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # DDS encodes rows of blocks on several threads, as the other parallel codecs here do on native targets. diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 7d588336f1..27f74678d5 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -57,6 +57,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | 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 | +| JPEG 2000 (`.jp2`, `.j2k`) | with feature `jp2` | **no** | U8, U16 | Gray, GrayA, RGB, RGBA, CMYK | yes | no | no | no | no | no | no | `hayro-jpeg2000` (off by default, so an SVG or raster reader carries no PDF decoder family) | | Sun Raster (`.ras`) | with feature `sunraster` (incl. RLE and colour maps) | with feature `sunraster` (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | `sunraster` (off by default) | | SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | diff --git a/crates/image-codecs/src/codecs.rs b/crates/image-codecs/src/codecs.rs index 3fdf65ae34..817f63ce7e 100644 --- a/crates/image-codecs/src/codecs.rs +++ b/crates/image-codecs/src/codecs.rs @@ -13,6 +13,7 @@ pub(crate) mod gif_frames; pub(crate) mod gif_palette; pub(crate) mod heif; pub(crate) mod iff; +#[cfg(feature = "jp2")] pub(crate) mod jp2; pub(crate) mod jpeg; pub(crate) mod jpegls; diff --git a/crates/image-codecs/src/format.rs b/crates/image-codecs/src/format.rs index f43dae32c0..0e0ef4c6b2 100644 --- a/crates/image-codecs/src/format.rs +++ b/crates/image-codecs/src/format.rs @@ -350,7 +350,13 @@ pub fn caps(format: Format) -> FormatCaps { FormatCaps { read: cfg!(feature = "sgi"), write: cfg!(feature = "sgi"), depths: &[S::U8, S::U16], layouts: RGB_GRAY, alpha: true, ..base } } Format::JpegLs => FormatCaps { depths: &[S::U8, S::U16], layouts: &[L::Gray, L::Rgb], alpha: false, ..base }, - Format::Jp2 => FormatCaps { read: true, write: false, layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], icc: true, ..base }, + Format::Jp2 => FormatCaps { + read: cfg!(feature = "jp2"), + write: false, + layouts: &[L::Gray, L::GrayA, L::Rgb, L::Rgba, L::Cmyk, L::CmykA], + icc: true, + ..base + }, } } diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 2a91dc6ea8..707031a98c 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -75,11 +75,13 @@ pub use crate::resolution_probe::file_resolution; pub use crate::source_settings::{WebpEncoding, jpeg_quality, webp_encoding}; pub use half::f16; +#[cfg(feature = "jp2")] +use crate::codecs::jp2; #[cfg(feature = "sgi")] use crate::codecs::sgi; #[cfg(feature = "sunraster")] use crate::codecs::sunraster; -use crate::codecs::{bmp, dds, dicom, dpx, exr, fits, heif, iff, jp2, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; +use crate::codecs::{bmp, dds, dicom, dpx, exr, fits, heif, iff, jpeg, jpegls, jxl, pcx, png, pnm, tiff, via_image, webp, xpm}; /// Detect the format and decode with default [`Limits`]. pub fn decode(bytes: &[u8]) -> Result { @@ -100,7 +102,7 @@ pub fn decode_as(format: Format, bytes: &[u8]) -> Result { /// 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) { + if !caps(format).read && !matches!(format, Format::Heif | Format::Jxl | Format::Jp2) { return Err(CodecError::unsupported(format, "decoding is not available for this format")); } let l = &opts.limits; @@ -141,7 +143,16 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res Format::JpegLs => jpegls::decode(bytes, l), Format::OpenExr => exr::decode(bytes, l), Format::Heif => heif::decode(bytes, l, opts.keep_orientation), - Format::Jp2 => jp2::decode(bytes, l), + Format::Jp2 => { + #[cfg(feature = "jp2")] + { + jp2::decode(bytes, l) + } + #[cfg(not(feature = "jp2"))] + { + Err(CodecError::unsupported(format, "JPEG 2000 support requires the jp2 feature")) + } + } Format::Jxl => jxl::decode(bytes, l), #[cfg(all(feature = "avif-decode", not(target_arch = "wasm32")))] Format::Avif => codecs::avif_decode::decode(bytes, l, opts.keep_orientation), diff --git a/crates/image-codecs/tests/it/jp2_feature.rs b/crates/image-codecs/tests/it/jp2_feature.rs new file mode 100644 index 0000000000..3cc8facc28 --- /dev/null +++ b/crates/image-codecs/tests/it/jp2_feature.rs @@ -0,0 +1,24 @@ +//! JPEG 2000 decode is opt-in: without the `jp2` feature a JP2 file is detected and refused with a +//! named reason rather than decoded, so a crate that only reads SVG keeps no JPEG 2000 decoder. + +use craft_image_codecs::*; + +/// JP2 signature box and file type box, and nothing else. +const JP2_HEAD: &[u8] = b"\0\0\0\x0cjP \r\n\x87\n\0\0\0\x14ftypjp2 \0\0\0\0jp2 "; + +#[test] +fn a_jp2_file_is_detected_without_the_feature() { + assert_eq!(detect(JP2_HEAD), Some(Format::Jp2)); +} + +#[test] +fn a_jp2_file_is_refused_with_the_feature_named() { + let err = decode_with(JP2_HEAD, &DecodeOptions::default()).expect_err("no image in a bare header"); + // With the feature, the bare header is a decode error, never "unsupported". + if !cfg!(feature = "jp2") + && let CodecError::Unsupported { format, reason } = err + { + assert_eq!(format, Format::Jp2); + assert!(reason.contains("jp2 feature"), "reason names the feature: {reason}"); + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs index e3c0feaaf8..4a71f7c9ea 100644 --- a/crates/image-codecs/tests/it/main.rs +++ b/crates/image-codecs/tests/it/main.rs @@ -16,6 +16,7 @@ mod exr_stream; mod fidelity; mod gif_frames; mod heif; +mod jp2_feature; mod jpeg_inks; mod jpegls_oracle; mod limits; From 4dcf4fc13a36ac3c2ac9bf3c825b230f25486992 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 20 Nov 2026 13:20:29 +0100 Subject: [PATCH 2/3] image-codecs: fix the jp2 review findings The README promised native U16 samples, which jp2::decode does not produce and caps(Format::Jp2).depths does not advertise: list U8. The feature test used its error match as a guard, so a different variant would have skipped the assertions. Each feature branch now asserts what it expects and anything else fails. --- crates/image-codecs/README.md | 2 +- crates/image-codecs/tests/it/jp2_feature.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index 27f74678d5..d88722238a 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -57,7 +57,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | 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 | -| JPEG 2000 (`.jp2`, `.j2k`) | with feature `jp2` | **no** | U8, U16 | Gray, GrayA, RGB, RGBA, CMYK | yes | no | no | no | no | no | no | `hayro-jpeg2000` (off by default, so an SVG or raster reader carries no PDF decoder family) | +| JPEG 2000 (`.jp2`, `.j2k`) | with feature `jp2` | **no** | U8 | Gray, GrayA, RGB, RGBA, CMYK | yes | no | no | no | no | no | no | `hayro-jpeg2000` (off by default, so an SVG or raster reader carries no PDF decoder family) | | Sun Raster (`.ras`) | with feature `sunraster` (incl. RLE and colour maps) | with feature `sunraster` (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | `sunraster` (off by default) | | SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in | diff --git a/crates/image-codecs/tests/it/jp2_feature.rs b/crates/image-codecs/tests/it/jp2_feature.rs index 3cc8facc28..9d75731c5d 100644 --- a/crates/image-codecs/tests/it/jp2_feature.rs +++ b/crates/image-codecs/tests/it/jp2_feature.rs @@ -14,11 +14,16 @@ fn a_jp2_file_is_detected_without_the_feature() { #[test] fn a_jp2_file_is_refused_with_the_feature_named() { let err = decode_with(JP2_HEAD, &DecodeOptions::default()).expect_err("no image in a bare header"); - // With the feature, the bare header is a decode error, never "unsupported". - if !cfg!(feature = "jp2") - && let CodecError::Unsupported { format, reason } = err - { - assert_eq!(format, Format::Jp2); - assert!(reason.contains("jp2 feature"), "reason names the feature: {reason}"); + if cfg!(feature = "jp2") { + // With the feature the bare header reaches the decoder and fails as undecodable. + assert!(matches!(err, CodecError::Malformed { .. } | CodecError::InvalidImage(_)), "decodes as far as the decoder: {err:?}"); + } else { + match err { + CodecError::Unsupported { format, reason } => { + assert_eq!(format, Format::Jp2); + assert!(reason.contains("jp2 feature"), "reason names the feature: {reason}"); + } + other => panic!("expected an unsupported format error, got {other:?}"), + } } } From 2176367fd7c3b5c3ce3ec4ca3018031cb3b80b10 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 20 Nov 2026 15:17:06 +0100 Subject: [PATCH 3/3] docs(image-codecs): JPEG 2000 keeps its ICC profile The format row said no while jp2::decode carries the profile into out.icc and caps(Format::Jp2) reports icc: true. Say yes, so a colour-managed consumer does not skip the decoder on the table alone. --- crates/image-codecs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/image-codecs/README.md b/crates/image-codecs/README.md index d88722238a..054df4f314 100644 --- a/crates/image-codecs/README.md +++ b/crates/image-codecs/README.md @@ -57,7 +57,7 @@ craft-image-codecs = { git = "https://github.com/storytold/craft-libs", rev = "< | 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 | -| JPEG 2000 (`.jp2`, `.j2k`) | with feature `jp2` | **no** | U8 | Gray, GrayA, RGB, RGBA, CMYK | yes | no | no | no | no | no | no | `hayro-jpeg2000` (off by default, so an SVG or raster reader carries no PDF decoder family) | +| JPEG 2000 (`.jp2`, `.j2k`) | with feature `jp2` | **no** | U8 | Gray, GrayA, RGB, RGBA, CMYK | yes | yes | no | no | no | no | no | `hayro-jpeg2000` (off by default, so an SVG or raster reader carries no PDF decoder family) | | Sun Raster (`.ras`) | with feature `sunraster` (incl. RLE and colour maps) | with feature `sunraster` (uncompressed) | U8 | Gray, RGB, RGBA | yes | no | no | no | no | no | no | `sunraster` (off by default) | | SGI (`.sgi`, `.rgb`, `.bw`) | with feature `sgi` | with feature `sgi` (RLE) | U8, U16 | Gray, Gray+A, RGB, RGBA | yes | no | no | no | no | no | no | `sgi` (off by default) | | JPEG-LS (`.jls`) | yes (any NEAR; one scan per component) | yes (lossless) | U8, U16 | Gray, RGB | no | no | no | no | no | no | no | built in |