mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-21 20:38:15 +00:00
Keep the JPEG XL encoder API available without the codec (#4618)
This commit is contained in:
@@ -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()))
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user