Keep the JPEG XL encoder API available without the codec (#4618)

This commit is contained in:
Terra854
2026-11-21 13:00:31 -05:00
committed by GitHub
parent 556a84c349
commit 8a04ebe5d1
2 changed files with 23 additions and 1 deletions
+10 -1
View File
@@ -1,6 +1,8 @@
//! JPEG XL encoder adapter to the shared `craft-jxl` codec.
use crate::encode::{EncodeImage, EncodeMeta, Samples};
#[cfg(feature = "jxl")]
use crate::encode::Samples;
use crate::encode::{EncodeImage, EncodeMeta};
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
@@ -60,6 +62,7 @@ impl Default for JxlOptions {
}
/// Encode JPEG XL while preserving LightCraft's public option and metadata types.
#[cfg(feature = "jxl")]
pub fn encode_jxl(image: &EncodeImage, options: &JxlOptions, metadata: &EncodeMeta) -> Result<Vec<u8>> {
image.validate()?;
let layout = match image.channels {
@@ -97,3 +100,9 @@ pub fn encode_jxl(image: &EncodeImage, options: &JxlOptions, metadata: &EncodeMe
};
craft_jxl::encode(&image, options, &metadata).map_err(|error| Error::Encode(error.to_string()))
}
/// Return an encoding error when the JPEG XL codec was disabled at build time.
#[cfg(not(feature = "jxl"))]
pub fn encode_jxl(_image: &EncodeImage, _options: &JxlOptions, _metadata: &EncodeMeta) -> Result<Vec<u8>> {
Err(Error::Encode("JPEG XL encoding is not included in this build".into()))
}
+13
View File
@@ -870,10 +870,23 @@ fn pq_png_rejects_short_buffers_and_survives_hostile_samples() {
}
/// Largest per-channel difference between two linear images.
#[cfg(feature = "jxl")]
fn max_diff(a: &lightcraft_raster::Rgb32f, b: &lightcraft_raster::Rgb32f) -> f32 {
a.data.iter().zip(&b.data).flat_map(|(p, q)| (0..3).map(move |c| (p[c] - q[c]).abs())).fold(0.0, f32::max)
}
#[test]
#[cfg(not(feature = "jxl"))]
fn jpeg_xl_disabled_reports_unavailable_codec() {
let image = EncodeImage::new(1, 1, 3, Samples::U8(&[17, 23, 83]));
let result = encode_jxl(&image, &JxlOptions::default(), &EncodeMeta::default());
assert!(matches!(result, Err(Error::Encode(message)) if message.contains("JPEG XL encoding is not included")));
let jxl_prefix = b"\xff\x0a\0\0";
assert_eq!(sniff(jxl_prefix), Some(Format::Jxl));
assert!(matches!(decode(jxl_prefix, DecodeOptions::default()), Err(Error::Unsupported(Format::Jxl, _))));
}
#[test]
#[cfg(feature = "jxl")]
fn jxl_lossless_exact_with_metadata() {