Skip to content

Commit

Permalink
Add JxlEncoderUseBoxes for metadata encode
Browse files Browse the repository at this point in the history
- Add metadata test

- test encoder.use_box

- add xmp test
  • Loading branch information
Isotr0py authored and inflation committed May 18, 2024
1 parent 58956f0 commit 522973c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
20 changes: 19 additions & 1 deletion jpegxl-rs/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)]
Expand All @@ -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(())
}
}
2 changes: 2 additions & 0 deletions jpegxl-rs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
15 changes: 14 additions & 1 deletion jpegxl-rs/src/tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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<u8> =
encoder.encode(sample.as_raw(), sample.width(), sample.height())?;

Ok(())
}

#[test]
#[cfg(feature = "threads")]
fn builder() -> TestResult {
Expand Down
2 changes: 1 addition & 1 deletion jpegxl-sys/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Binary file added samples/sample.exif
Binary file not shown.
1 change: 1 addition & 0 deletions samples/sample.xmp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 4.4.0-Exiv2"> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:stEvt="http://ns.adobe.com/xap/1.0/sType/ResourceEvent#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:GIMP="http://www.gimp.org/xmp/" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmpMM:DocumentID="gimp:docid:gimp:10093e7e-33ea-4901-a7ce-a3c04b8b6480" xmpMM:InstanceID="xmp.iid:737430f3-5b4d-49cf-9873-c10684618917" xmpMM:OriginalDocumentID="xmp.did:f7302aac-e69b-4ad8-ac19-58a76e2fc4f7" dc:Format="image/jpeg" GIMP:API="2.0" GIMP:Platform="Linux" GIMP:TimeStamp="1635353959433667" GIMP:Version="2.10.28" xmp:CreatorTool="GIMP 2.10"> <xmpMM:History> <rdf:Seq> <rdf:li stEvt:action="saved" stEvt:changed="/metadata" stEvt:instanceID="xmp.iid:735c4bc4-11be-4e71-8aad-0ddd90c9607d" stEvt:softwareAgent="Gimp 2.10 (Linux)" stEvt:when="2021-10-27T18:58:46+02:00"/> <rdf:li stEvt:action="saved" stEvt:changed="/" stEvt:instanceID="xmp.iid:70d81176-171d-43b8-b687-1eb80d302337" stEvt:softwareAgent="Gimp 2.10 (Linux)" stEvt:when="2021-10-27T18:59:19+02:00"/> </rdf:Seq> </xmpMM:History> <dc:title> <rdf:Alt> <rdf:li xml:lang="x-default">test</rdf:li> </rdf:Alt> </dc:title> </rdf:Description> </rdf:RDF> </x:xmpmeta> <?xpacket end="w"?>

0 comments on commit 522973c

Please sign in to comment.