PhotoCraft: An open-source, photo editor written by scratch in pure Rust
FITS: clamp 32-bit saves to 0..1 and warn, so they reopen as saved (#5627) (#5628)
David Mannd8d592b1bbab
diff --git a/crates/codecs/src/codecs/fits.rs b/crates/codecs/src/codecs/fits.rsindex 3aa769fb0..7e3f92ae6 100644--- a/crates/codecs/src/codecs/fits.rs+++ b/crates/codecs/src/codecs/fits.rs@@ -530,7 +530,11 @@ pub(crate) fn encode(src: &Image, plan: Plan, opts: &EncodeOptions) -> Result<Vefor p in 0..nc {for y in 0..hu {for x in 0..wu {- out.extend_from_slice(&d[idx(p, y, x)].to_be_bytes());+ // 0..1 as documented: a value above 2 would make readers (ours too)+ // take the data as ADU and scale it by 65535. NaN is written as 0.+ let v = d[idx(p, y, x)];+ let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) };+ out.extend_from_slice(&v.to_be_bytes());}}}@@ -632,6 +636,17 @@ mod tests {assert_eq!(img.to_f32_samples().unwrap(), vec![1.0, 0.0]);}+ #[test]+ fn hdr_float_saves_clamped_and_reopens_as_saved() {+ // One value above 2 used to make the reader scale everything by 65535 (all black).+ let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap();+ let opts = EncodeOptions::default();+ let bytes = encode(&img, crate::fidelity::plan(&img, F, &opts), &opts).unwrap();+ let back = decode(&bytes, &Limits::default()).unwrap().to_f32_samples().unwrap();+ assert!((back[0] - 0.5).abs() < 1e-6, "{back:?}");+ assert!((back[1] - 1.0).abs() < 1e-6, "{back:?}");+ }+#[test]fn signed_negative_data_falls_back_to_float() {let mut b = header(&["SIMPLE = T", "BITPIX = 16", "NAXIS = 2", "NAXIS1 = 2", "NAXIS2 = 1"]);diff --git a/crates/codecs/src/fidelity.rs b/crates/codecs/src/fidelity.rsindex d5654d27a..08153dfba 100644--- a/crates/codecs/src/fidelity.rs+++ b/crates/codecs/src/fidelity.rs@@ -195,7 +195,8 @@ pub fn fidelity_warnings_with(image: &Image, format: Format, opts: &EncodeOptionif lossy_depth {w.push(W::DepthReduced { from: ss, to: p.sample });}- if ss.is_float() && !p.sample.is_float() && image.has_out_of_range() {+ // FITS stores floats as 0..1 (see `codecs::fits`), so out-of-range values are clipped too.+ if ss.is_float() && (!p.sample.is_float() || format == Format::Fits) && image.has_out_of_range() {w.push(W::RangeClipped);}diff --git a/crates/codecs/tests/fidelity.rs b/crates/codecs/tests/fidelity.rsindex 32d5e21bf..6a7002fc2 100644--- a/crates/codecs/tests/fidelity.rs+++ b/crates/codecs/tests/fidelity.rs@@ -115,6 +115,12 @@ fn in_range_float_not_clipped() {assert!(!w.contains(&W::RangeClipped));}#[test]+fn hdr_float_to_fits_clips() {+ let img = Image::from_f32(2, 1, ChannelLayout::Gray, &[0.5, 3.0]).unwrap();+ assert!(warns(&img, Format::Fits).contains(&W::RangeClipped));+ assert!(!warns(&test_image(ChannelLayout::Gray, SampleType::F32), Format::Fits).contains(&W::RangeClipped));+}+#[test]fn float_to_exr_and_tiff_is_clean() {for f in [Format::OpenExr, Format::Tiff, Format::Pnm] {assert!(warns(&test_image(ChannelLayout::Rgb, SampleType::F32), f).is_empty(), "{f:?}");