Files
craft-libs/crates/image-codecs/tests/jxl_decode.rs
T
Diego Chagas 7ff0f967db image-codecs: read JPEG XL through craft-jxl behind a jxl feature (#2135)
* image-codecs: read JPEG XL through craft-jxl behind a jxl feature

Format::Jxl reads when the non-default `jxl` feature is on (decode only, so the
encoder's jixel dependency is not built); without it JPEG XL is still detected and
opening one is an Unsupported error that says the build lacks it. Orientation is
applied by the decoder, so the Exif and XMP orientation become 1. ICC, Exif and XMP
come through; associated alpha is divided out; float files open as F32.

* image-codecs: skip JPEG XL in the one-pixel round trip

The encoder refuses images narrower than two pixels; with the jxl feature JPEG XL is a
read-and-write format, so the generic round trip reached it.
2026-11-18 15:26:57 -05:00

45 lines
1.9 KiB
Rust

//! JPEG XL decode through craft-jxl: lossless round trips, metadata, and hostile containers.
#![cfg(feature = "jxl")]
use craft_image_codecs::{ChannelLayout, CodecError, DecodeOptions, EncodeOptions, Format, Image, Limits, SampleType, decode, decode_with, encode};
fn rgba_8bit() -> Image {
let data = 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];
Image::from_u8(3, 2, ChannelLayout::Rgba, data).unwrap()
}
#[test]
fn an_eight_bit_export_decodes_back_to_the_same_pixels() {
let input = rgba_8bit();
let bytes = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap();
let out = decode(&bytes).unwrap();
assert_eq!((out.width(), out.height(), out.layout(), out.sample_type()), (3, 2, ChannelLayout::Rgba, SampleType::U8));
assert_eq!(out.data(), input.data());
}
#[test]
fn a_sixteen_bit_gray_export_decodes_back_to_the_same_samples() {
let samples: Vec<u16> = (0..12).map(|i| i * 5461).collect();
let input = Image::from_u16(4, 3, ChannelLayout::Gray, &samples).unwrap();
let bytes = encode(&input, Format::Jxl, &EncodeOptions::default()).unwrap();
let out = decode(&bytes).unwrap();
assert_eq!((out.layout(), out.sample_type()), (ChannelLayout::Gray, SampleType::U16));
assert_eq!(out.data(), input.data());
}
#[test]
fn the_pixel_limit_is_an_error_not_an_allocation() {
let bytes = encode(&rgba_8bit(), Format::Jxl, &EncodeOptions::default()).unwrap();
let opts = DecodeOptions { limits: Limits { max_pixels: 4, ..Limits::default() }, ..DecodeOptions::default() };
let err = decode_with(&bytes, &opts);
assert!(matches!(err, Err(CodecError::LimitExceeded(_))), "{err:?}");
}
#[test]
fn every_prefix_of_a_file_is_an_error_or_an_image_never_a_panic() {
let bytes = encode(&rgba_8bit(), Format::Jxl, &EncodeOptions::default()).unwrap();
for end in 0..bytes.len() {
let _ = decode(&bytes[..end]);
}
}