Integrate JPEG 2000 feature with backward-compatible codec defaults

This commit is contained in:
Codex committed 2026-11-29 19:33:24 +01:00
commit 17a339cd8f
8 files changed
+58 -5

No files matched your search

+1
View File
@@ -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"]
+4 -2
View File
@@ -15,7 +15,7 @@ corpus = []
# Every codec below is on by default; a build that never reads or writes a format can turn
# `default-features` off and list the ones it needs. A format left out is still detected, and
# reading or writing it returns `CodecError::Unsupported` naming the feature.
default = ["dds", "dicom", "dpx", "exr", "fits", "iff", "jpegls", "jxl-encode", "pnm", "tga", "webp", "xpm"]
default = ["dds", "dicom", "dpx", "exr", "fits", "iff", "jp2", "jpegls", "jxl-encode", "pnm", "tga", "webp", "xpm"]
dds = []
dicom = []
# DPX and Cineon.
@@ -46,6 +46,8 @@ avif-decode = ["dep:craft-video", "craft-video/av1-decode"]
# `Image::source_profile` and `Image::to_display_rgba8`: convert an image's colors from its ICC
# profile or CICP description into a display profile with craft-cms.
cms = ["dep:craft-cms"]
# JPEG 2000 decode via hayro-jpeg2000; default-on to preserve existing app support.
jp2 = ["dep:hayro-jpeg2000"]
[dependencies]
craft-dds = { workspace = true }
@@ -72,7 +74,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.
+1
View File
@@ -58,6 +58,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 | 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 |
+1
View File
@@ -21,6 +21,7 @@ pub(crate) mod gif_palette;
pub(crate) mod heif;
#[cfg(feature = "iff")]
pub(crate) mod iff;
#[cfg(feature = "jp2")]
pub(crate) mod jp2;
pub(crate) mod jpeg;
#[cfg(feature = "jpegls")]
+8 -1
View File
@@ -286,6 +286,7 @@ pub(crate) fn missing_feature(format: Format) -> Option<&'static str> {
Format::OpenExr => ("exr", cfg!(feature = "exr")),
Format::Fits => ("fits", cfg!(feature = "fits")),
Format::Iff => ("iff", cfg!(feature = "iff")),
Format::Jp2 => ("jp2", cfg!(feature = "jp2")),
Format::JpegLs => ("jpegls", cfg!(feature = "jpegls")),
Format::Pnm => ("pnm", cfg!(feature = "pnm")),
Format::Sgi => ("sgi", cfg!(feature = "sgi")),
@@ -383,7 +384,13 @@ fn format_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
},
}
}
+13 -2
View File
@@ -99,6 +99,8 @@ use crate::codecs::iff;
use crate::codecs::jpegls;
#[cfg(feature = "pnm")]
use crate::codecs::pnm;
#[cfg(feature = "jp2")]
use crate::codecs::jp2;
#[cfg(feature = "sgi")]
use crate::codecs::sgi;
#[cfg(feature = "sunraster")]
@@ -107,7 +109,7 @@ use crate::codecs::sunraster;
use crate::codecs::webp;
#[cfg(feature = "xpm")]
use crate::codecs::xpm;
use crate::codecs::{bmp, heif, jp2, jpeg, jxl, pcx, png, tiff, via_image};
use crate::codecs::{bmp, heif, jpeg, jxl, pcx, png, tiff, via_image};
/// `$codec` in a build with the Cargo feature `$feature`, else the error naming that feature.
macro_rules! gated {
@@ -187,7 +189,16 @@ pub fn decode_as_with(format: Format, bytes: &[u8], opts: &DecodeOptions) -> Res
Format::JpegLs => gated!("jpegls", format, jpegls::decode(bytes, l)),
Format::OpenExr => gated!("exr", format, 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),
@@ -0,0 +1,29 @@
//! 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");
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:?}"),
}
}
}
+1
View File
@@ -24,6 +24,7 @@ mod exr_stream;
mod fidelity;
mod gif_frames;
mod heif;
mod jp2_feature;
mod jpeg_inks;
#[cfg(feature = "jpegls")]
mod jpegls_oracle;