Skip to content

Commit

Permalink
Add support for parsing eXIf chunk.
Browse files Browse the repository at this point in the history
The `eXIf` chunk was already supported in `encoder.rs`, but this commit
also adds support to `decoder/stream.rs`.  This commit is needed for
parity with the existing PNG decoder in Blink / Chromium - see
https://crbug.com/390707316
  • Loading branch information
anforowicz committed Jan 17, 2025
1 parent 7ab5bb6 commit 643d290
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/decoder/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ impl StreamingDecoder {
chunk::cICP => Ok(self.parse_cicp()),
chunk::mDCV => Ok(self.parse_mdcv()),
chunk::cLLI => Ok(self.parse_clli()),
chunk::eXIf => Ok(self.parse_exif()),
chunk::bKGD => Ok(self.parse_bkgd()),
chunk::iCCP if !self.decode_options.ignore_iccp_chunk => self.parse_iccp(),
chunk::tEXt if !self.decode_options.ignore_text_chunk => self.parse_text(),
Expand Down Expand Up @@ -1487,6 +1488,16 @@ impl StreamingDecoder {
Decoded::Nothing
}

fn parse_exif(&mut self) -> Decoded {
// We ignore a second, duplicated eXIf chunk (if any).
let info = self.info.as_mut().unwrap();
if info.exif_metadata.is_none() {
info.exif_metadata = Some(self.current_chunk.raw_bytes.clone().into());
}

Decoded::Nothing
}

fn parse_iccp(&mut self) -> Result<Decoded, DecodingError> {
if self.have_idat {
Err(DecodingError::Format(
Expand Down Expand Up @@ -2125,7 +2136,7 @@ mod tests {
assert!(decoder.read_info().is_ok());
}

/// Test handling of `mDCV` and `cLLI` chunks.`
/// Test handling of `mDCV` and `cLLI` chunks.
#[test]
fn test_mdcv_and_clli_chunks() {
let decoder = crate::Decoder::new(File::open("tests/bugfixes/cicp_pq.png").unwrap());
Expand Down Expand Up @@ -2155,6 +2166,17 @@ mod tests {
assert_relative_eq!(clli.max_frame_average_light_level as f32 / 10_000.0, 2627.0);
}

/// Test handling of `eXIf` chunk.
#[test]
fn test_exif_chunk() {
let decoder =
crate::Decoder::new(File::open("tests/bugfixes/F-exif-chunk-early.png").unwrap());
let reader = decoder.read_info().unwrap();
let info = reader.info();
let exif = info.exif_metadata.as_ref().unwrap().as_ref();
assert_eq!(exif.len(), 90);
}

/// Tests what happens then [`Reader.finish`] is called twice.
#[test]
fn test_finishing_twice() {
Expand Down
Binary file added tests/bugfixes/F-exif-chunk-early.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 643d290

Please sign in to comment.