From 522973c8471eb419156980f252c0335bd37011fe Mon Sep 17 00:00:00 2001 From: Isotr0py <2037008807@qq.com> Date: Thu, 9 May 2024 22:15:20 +0800 Subject: [PATCH] Add JxlEncoderUseBoxes for metadata encode - Add metadata test - test encoder.use_box - add xmp test --- jpegxl-rs/src/encode.rs | 20 +++++++++++++++++++- jpegxl-rs/src/tests.rs | 2 ++ jpegxl-rs/src/tests/encode.rs | 15 ++++++++++++++- jpegxl-sys/src/encode.rs | 2 +- samples/sample.exif | Bin 0 -> 260 bytes samples/sample.xmp | 1 + 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 samples/sample.exif create mode 100644 samples/sample.xmp diff --git a/jpegxl-rs/src/encode.rs b/jpegxl-rs/src/encode.rs index b1d86c4..6014804 100644 --- a/jpegxl-rs/src/encode.rs +++ b/jpegxl-rs/src/encode.rs @@ -123,6 +123,9 @@ pub struct JxlEncoder<'prl, 'mm> { /// Default: `None`, indicating single thread execution pub parallel_runner: Option<&'prl dyn JxlParallelRunner>, + /// Whether box is used in encoder + use_box: bool, + /// Set memory manager #[allow(dead_code)] memory_manager: Option<&'mm dyn MemoryManager>, @@ -165,6 +168,7 @@ impl<'prl, 'mm> JxlEncoderBuilder<'prl, 'mm> { init_buffer_size, color_encoding: self.color_encoding.unwrap_or(ColorEncoding::Srgb), parallel_runner: self.parallel_runner.flatten(), + use_box: self.use_box.unwrap_or_default(), memory_manager: mm, }) } @@ -392,10 +396,14 @@ impl<'prl, 'mm> JxlEncoder<'prl, 'mm> { Metadata::Jumb(data) => (b"jumb", data), Metadata::Custom(t, data) => (t, data), }; + if !self.use_box { + self.check_enc_status(unsafe { JxlEncoderUseBoxes(self.enc) })?; + self.use_box = true; + } self.check_enc_status(unsafe { JxlEncoderAddBox( self.enc, - Metadata::box_type(t), + &Metadata::box_type(t), data.as_ptr().cast(), data.len(), compress.into(), @@ -480,6 +488,7 @@ pub fn encoder_builder<'prl, 'mm>() -> JxlEncoderBuilder<'prl, 'mm> { #[cfg(test)] mod tests { use super::*; + use testresult::TestResult; #[test] #[allow(clippy::clone_on_copy)] @@ -492,4 +501,13 @@ mod tests { _ = encoder_builder().clone(); } + + #[test] + fn test_usebox() -> TestResult { + let mut encoder = encoder_builder().build()?; + let metadata = Metadata::Exif(&[0, 1, 2, 3]); + encoder.add_metadata(&metadata, true)?; + assert!(encoder.use_box); + Ok(()) + } } diff --git a/jpegxl-rs/src/tests.rs b/jpegxl-rs/src/tests.rs index b95b73f..4803570 100644 --- a/jpegxl-rs/src/tests.rs +++ b/jpegxl-rs/src/tests.rs @@ -3,6 +3,8 @@ mod encode; pub const SAMPLE_PNG: &[u8] = include_bytes!("../../samples/sample.png"); const SAMPLE_JPEG: &[u8] = include_bytes!("../../samples/sample.jpg"); +const SAMPLE_EXIF: &[u8] = include_bytes!("../../samples/sample.exif"); +const SAMPLE_XMP: &[u8] = include_bytes!("../../samples/sample.xmp"); pub const SAMPLE_JXL: &[u8] = include_bytes!("../../samples/sample.jxl"); const SAMPLE_JXL_JPEG: &[u8] = include_bytes!("../../samples/sample_jpg.jxl"); pub const SAMPLE_JXL_GRAY: &[u8] = include_bytes!("../../samples/sample_grey.jxl"); diff --git a/jpegxl-rs/src/tests/encode.rs b/jpegxl-rs/src/tests/encode.rs index 21bd287..8b5707e 100644 --- a/jpegxl-rs/src/tests/encode.rs +++ b/jpegxl-rs/src/tests/encode.rs @@ -23,7 +23,7 @@ use testresult::TestResult; use crate::decode::Data; use crate::{ decoder_builder, - encode::{ColorEncoding, EncoderFrame, EncoderResult}, + encode::{ColorEncoding, EncoderFrame, EncoderResult, Metadata}, encoder_builder, Endianness, }; #[cfg(feature = "threads")] @@ -66,6 +66,19 @@ fn jpeg() -> TestResult { Ok(()) } +#[test] +fn metadata() -> TestResult { + let sample = get_sample().to_rgb8(); + let mut encoder = encoder_builder().build()?; + encoder.add_metadata(&Metadata::Exif(super::SAMPLE_EXIF), true)?; + encoder.add_metadata(&Metadata::Xmp(super::SAMPLE_XMP), true)?; + + let _res: EncoderResult = + encoder.encode(sample.as_raw(), sample.width(), sample.height())?; + + Ok(()) +} + #[test] #[cfg(feature = "threads")] fn builder() -> TestResult { diff --git a/jpegxl-sys/src/encode.rs b/jpegxl-sys/src/encode.rs index 5d71d39..76415ee 100644 --- a/jpegxl-sys/src/encode.rs +++ b/jpegxl-sys/src/encode.rs @@ -242,7 +242,7 @@ extern "C" { pub fn JxlEncoderAddBox( enc: *mut JxlEncoder, - box_type: JxlBoxType, + box_type: *const JxlBoxType, contents: *const u8, size: usize, compress_box: JxlBool, diff --git a/samples/sample.exif b/samples/sample.exif new file mode 100644 index 0000000000000000000000000000000000000000..0d040a68c50d58b5846f599e7f1e820aaa188846 GIT binary patch literal 260 zcmebD)MDUZU;tu1MkWR!AS(}ur5ITm7=d&H5KBYZ9YD4QBQsc>3CK1Cs^znK zGc?dMvH+TEU}R`zXkcYzu3%_kWol_ test \ No newline at end of file