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 aa16ef9
Show file tree
Hide file tree
Showing 5 changed files with 26 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.
1 change: 1 addition & 0 deletions tests/results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ tests/pngsuite-extra/basi3p02_2.png: 419215269
tests/bugfixes/invalid_palette_index.png: 3040120786
tests/bugfixes/acid2.png: 2380843583
tests/bugfixes/cicp_pq.png: 3306910117
tests/bugfixes/F-exif-chunk-early.png: 4079196849
tests/bugfixes/gama-srgb-order-issue#304.png: 275287206
tests/bugfixes/issue#1825.png: 3386502267
tests/bugfixes/issue#430.png: 574381763
Expand Down
1 change: 1 addition & 0 deletions tests/results_alpha.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ tests/pngsuite-extra/basi3p01_2.png: 4023530527
tests/pngsuite-extra/basi3p02_2.png: 313298351
tests/bugfixes/acid2.png: 2380843583
tests/bugfixes/cicp_pq.png: 2316063598
tests/bugfixes/F-exif-chunk-early.png: 3035932940
tests/bugfixes/gama-srgb-order-issue#304.png: 275287206
tests/bugfixes/invalid_palette_index.png: 4178597885
tests/bugfixes/issue#202.png: 29900969
Expand Down
1 change: 1 addition & 0 deletions tests/results_identity.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ tests/pngsuite-extra/basi3p01_2.png: 3071936103
tests/pngsuite-extra/basi3p02_2.png: 1136045771
tests/bugfixes/acid2.png: 2051796287
tests/bugfixes/cicp_pq.png: 3306910117
tests/bugfixes/F-exif-chunk-early.png: 4079196849
tests/bugfixes/invalid_palette_index.png: 64128641
tests/bugfixes/gama-srgb-order-issue#304.png: 275287206
tests/bugfixes/issue#1825.png: 3386502267
Expand Down

0 comments on commit aa16ef9

Please sign in to comment.