diff --git a/Cargo.toml b/Cargo.toml index 5f7d3cb..37cbd1d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,7 +85,7 @@ yaneurao-itufuru = ["yaneurao"] # basic feature image = ["png"] image-jpg = ["mozjpeg"] -image-jxl = ["jpegxl-sys"] +image-jxl = ["image", "jpegxl-sys"] image-webp = ["webp"] lossless-audio = ["utils-pcm"] audio-flac = ["libflac-sys", "utils-pcm"] diff --git a/src/utils/jxl.rs b/src/utils/jxl.rs index 15ea23d..78ab80e 100644 --- a/src/utils/jxl.rs +++ b/src/utils/jxl.rs @@ -1,4 +1,5 @@ //! JPEG XL image support +use super::img::*; use crate::types::*; use anyhow::Result; use jpegxl_sys::common::types::*; @@ -144,7 +145,7 @@ pub fn decode_jxl(mut r: R) -> Result { } /// Encode image data to JXL format -pub fn encode_jxl(img: ImageData, _config: &ExtraConfig) -> Result> { +pub fn encode_jxl(mut img: ImageData, _config: &ExtraConfig) -> Result> { let encoder = unsafe { JxlEncoderCreate(std::ptr::null()) }; if encoder.is_null() { return Err(anyhow::anyhow!("Failed to create JXL encoder")); @@ -194,6 +195,15 @@ pub fn encode_jxl(img: ImageData, _config: &ExtraConfig) -> Result> { endianness: JxlEndianness::Little, align: 0, }; + match img.color_type { + ImageColorType::Bgr => { + convert_bgr_to_rgb(&mut img)?; + } + ImageColorType::Bgra => { + convert_bgra_to_rgba(&mut img)?; + } + _ => {} + }; check_encoder_status(unsafe { JxlEncoderAddImageFrame( options,