Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract (potentially Brotli-compressed) Exif metadata #389

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement ImageDecoder::exif_metadata
tirr-c committed Nov 16, 2024

Verified

This commit was signed with the committer’s verified signature.
tirr-c Wonwoo Choi
commit 9422d455a06f4ce98259e0c4b98b3129b6939b54
7 changes: 7 additions & 0 deletions crates/jxl-oxide/examples/image-integration.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,13 @@ fn main() {

#[allow(unused)]
let icc = decoder.icc_profile().unwrap();
let exif = decoder
.exif_metadata()
.expect("cannot decode Exif metadata");
if let Some(exif) = exif {
println!("Exif metadata found ({} byte(s))", exif.len());
}

let image = DynamicImage::from_decoder(decoder).expect("cannot decode image");

let output_file = std::fs::File::create(output_path).expect("cannot open output file");
32 changes: 30 additions & 2 deletions crates/jxl-oxide/src/integration/image.rs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ use crate::{CropInfo, InitializeResult, JxlImage};
/// - Returning images of 8-bit, 16-bit integer and 32-bit float samples
/// - RGB or luma-only images, with or without alpha
/// - Returning ICC profiles via `icc_profile`
/// - Returning Exif metadata via `exif_metadata`
/// - Setting decoder limits (caveat: memory limits are not strict)
/// - Cropped decoding with [`ImageDecoderRect`][image::ImageDecoderRect]
/// - (When `lcms2` feature is enabled) Converting CMYK images to sRGB color space
@@ -154,8 +155,11 @@ impl<R: Read> JxlDecoder<R> {
Ok(image)
}

fn load_until_first_keyframe(&mut self) -> crate::Result<()> {
while self.image.ctx.loaded_keyframes() == 0 {
fn load_until_condition(
&mut self,
mut predicate: impl FnMut(&JxlImage) -> crate::Result<bool>,
) -> crate::Result<()> {
while !predicate(&self.image)? {
let count = self.reader.read(&mut self.buf[self.buf_valid..])?;
if count == 0 {
break;
@@ -166,6 +170,12 @@ impl<R: Read> JxlDecoder<R> {
self.buf_valid -= consumed;
}

Ok(())
}

fn load_until_first_keyframe(&mut self) -> crate::Result<()> {
self.load_until_condition(|image| Ok(image.ctx.loaded_frames() > 0))?;

if self.image.frame_by_keyframe(0).is_none() {
return Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
@@ -177,6 +187,10 @@ impl<R: Read> JxlDecoder<R> {
Ok(())
}

fn load_until_exif(&mut self) -> crate::Result<()> {
self.load_until_condition(|image| Ok(image.raw_exif_data()?.is_some()))
}

#[inline]
fn is_float(&self) -> bool {
use crate::BitDepth;
@@ -305,6 +319,20 @@ impl<R: Read> image::ImageDecoder for JxlDecoder<R> {
Ok(Some(self.image.rendered_icc()))
}

fn exif_metadata(&mut self) -> ImageResult<Option<Vec<u8>>> {
self.load_until_exif().map_err(|e| {
ImageError::Decoding(DecodingError::new(
ImageFormatHint::PathExtension("jxl".into()),
e,
))
})?;

let Some(exif) = self.image.raw_exif_data().unwrap() else {
return Ok(None);
};
Ok(exif.payload().map(|v| v.to_vec()))
}

fn set_limits(&mut self, limits: image::Limits) -> ImageResult<()> {
use image::error::{LimitError, LimitErrorKind};