diff --git a/crates/codecs/src/codecs/png.rs b/crates/codecs/src/codecs/png.rs index 2544a1066d..885af6e29f 100644 --- a/crates/codecs/src/codecs/png.rs +++ b/crates/codecs/src/codecs/png.rs @@ -17,6 +17,15 @@ fn err(e: impl std::fmt::Display) -> CodecError { CodecError::malformed(F, e) } +/// Pixels per inch for `pHYs` pixels per metre. A whole number of ppi is stored rounded to whole +/// pixels per metre (300 ppi = 11811), so reading it back gives 299.9994: the whole number that +/// rounds to the stored value is the resolution that was meant, as Photoshop reads it. +pub(crate) fn dpi_of_ppm(ppm: u32) -> f32 { + let dpi = f64::from(ppm) * f64::from(METERS_PER_INCH); + let whole = dpi.round(); + if (whole / f64::from(METERS_PER_INCH)).round() == f64::from(ppm) { whole as f32 } else { dpi as f32 } +} + pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result { let mut input = Cursor::new(bytes); let mut decoder = png::Decoder::new_with_limits(&mut input, png::Limits { bytes: limits.alloc_usize() }); @@ -74,7 +83,7 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits) -> Result && d.xppu > 0 && d.yppu > 0 { - meta.dpi = Some((d.xppu as f32 * METERS_PER_INCH, d.yppu as f32 * METERS_PER_INCH)); + meta.dpi = Some((dpi_of_ppm(d.xppu), dpi_of_ppm(d.yppu))); } drop(reader); let end = usize::try_from(input.position()).map_err(|_| err("invalid PNG reader position"))?; diff --git a/crates/codecs/tests/resolution.rs b/crates/codecs/tests/resolution.rs index 5eb6acad3a..15ddc95749 100644 --- a/crates/codecs/tests/resolution.rs +++ b/crates/codecs/tests/resolution.rs @@ -425,16 +425,38 @@ fn png_phys_outranks_its_exif() { img.meta.exif = Some(camera_exif(&LE)); let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); near(decode(&png).unwrap().meta.dpi, 300.0); + near(decode(&with_phys(&png, 5906)).unwrap().meta.dpi, 150.0); +} + +/// `png` with a `pHYs` chunk of `ppm` pixels per metre after IHDR. +fn with_phys(png: &[u8], ppm: u32) -> Vec { let mut body = b"pHYs".to_vec(); - body.extend_from_slice(&5906u32.to_be_bytes()); - body.extend_from_slice(&5906u32.to_be_bytes()); + body.extend_from_slice(&ppm.to_be_bytes()); + body.extend_from_slice(&ppm.to_be_bytes()); body.push(1); let mut chunk = 9u32.to_be_bytes().to_vec(); chunk.extend_from_slice(&body); chunk.extend_from_slice(&crc32(&body).to_be_bytes()); let ihdr_end = 8 + 8 + 13 + 4; - let with_phys = [&png[..ihdr_end], &chunk, &png[ihdr_end..]].concat(); - near(decode(&with_phys).unwrap().meta.dpi, 150.0114); + [&png[..ihdr_end], &chunk, &png[ihdr_end..]].concat() +} + +#[test] +fn png_phys_of_a_whole_ppi_reads_back_whole() { + // pHYs holds whole pixels per metre: 300 ppi is stored as 11811 (299.9994 ppi), 72 as 2835 + // (72.009). The whole number that rounds to the stored value is read, as Photoshop does; a + // value no whole ppi rounds to stays as it is. + let img = Image::from_raw(4, 4, ChannelLayout::Rgb, SampleType::U8, vec![90; 48]).unwrap(); + let png = encode(&img, Format::Png, &EncodeOptions { embed_metadata: false, ..Default::default() }).unwrap(); + for (ppm, want) in [(11811, 300.0), (2835, 72.0), (3937, 100.0), (11812, 300.0248)] { + let (x, y) = decode(&with_phys(&png, ppm)).unwrap().meta.dpi.unwrap(); + assert!((x - want).abs() < 1e-4 && (y - want).abs() < 1e-4, "{ppm} px/m: {x} × {y}, wanted {want}"); + } + // And a 300 ppi PNG written by the encoder opens at exactly 300. + let mut img = img; + img.meta.dpi = Some((300.0, 300.0)); + let png = encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + assert_eq!(decode(&png).unwrap().meta.dpi, Some((300.0, 300.0))); } /// `camera_exif` plus IFD1, the embedded thumbnail a camera (or the last editor) wrote: diff --git a/crates/io/tests/resolution_axes.rs b/crates/io/tests/resolution_axes.rs index bc069d7f30..58b67d93c4 100644 --- a/crates/io/tests/resolution_axes.rs +++ b/crates/io/tests/resolution_axes.rs @@ -27,7 +27,7 @@ fn png_with_unequal_axes_reports_the_dropped_vertical_resolution() { assert!((r.document.resolution_dpi - 300.0).abs() < 0.01, "{}", r.document.resolution_dpi); let notes = resolution_notes(&r); assert_eq!(notes.len(), 1, "{:?}", r.warnings); - assert!(notes[0].contains("150.01") && notes[0].contains("300.00"), "{notes:?}"); + assert!(notes[0].contains("150.00") && notes[0].contains("300.00"), "{notes:?}"); } #[test]