diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index 3372e5e0d80..d9ecaf9fd27 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -46,6 +46,11 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result Resu img = cmyk_image_to_srgb(&img)?; warnings.push(format!("CMYK converted to sRGB for {format:?} through the document's colour profile")); } + if matches!(format, Format::OpenExr | Format::Hdr) { + img = rgb_image_to_linear(img)?; + } for w in codecs::fidelity_warnings_with(&img, format, &opts.encode) { if w.is_fatal() { return Err(IoError::Unsupported(w.to_string())); @@ -214,6 +222,27 @@ fn cmyk_image_to_srgb(img: &Image) -> Result { Ok(Image::from_normalized(w, h, layout, sample, &out)?.with_icc(Some(dst.to_bytes().to_vec())).with_meta(img.meta.clone())) } +/// OpenEXR and Radiance HDR store linear light (read back as linear sRGB, see [`import_flat`]): +/// RGB pixels in another profile (sRGB when untagged) are converted to linear sRGB, unclamped. +fn rgb_image_to_linear(img: Image) -> Result { + use photocraft_cms::{Builtin, ColorSpace, Intent, Profile, Transform}; + if !matches!(img.layout(), ChannelLayout::Rgb | ChannelLayout::Rgba) { + return Ok(img); + } + let linear = Builtin::LinearSrgb.profile(); + let src = + img.icc.as_ref().and_then(|b| Profile::parse(b).ok()).filter(|p| p.color_space == ColorSpace::Rgb).unwrap_or_else(|| Builtin::Srgb.profile().clone()); + if src.content_hash() == linear.content_hash() { + return Ok(img); + } + let t = Transform::new(&src, linear, Intent::RelativeColorimetric, false).map_err(|e| IoError::Unsupported(e.to_string()))?; + let stride = img.layout().channels(); + let mut vals = img.to_normalized(); + t.apply(&mut vals, stride); + let (w, h) = img.dimensions(); + Ok(Image::from_normalized(w, h, img.layout(), CSample::F32, &vals)?.with_icc(None).with_meta(img.meta.clone())) +} + /// Indexed Color → PNG-8 with its colour table; Duotone → the inks rendered as RGB. fn export_mode_specific(doc: &Document, format: Format, opts: &ExportOptions) -> Result, IoError> { match doc.mode { diff --git a/crates/io/tests/flat.rs b/crates/io/tests/flat.rs index 4ccb1a7a6b9..7426c4b85c9 100644 --- a/crates/io/tests/flat.rs +++ b/crates/io/tests/flat.rs @@ -48,7 +48,28 @@ codec_rt!(tiff_rgb16, "tif", ColorMode::Rgb, SampleType::U16, false, 0.0); codec_rt!(tiff_cmyk8, "tiff", ColorMode::Cmyk, SampleType::U8, false, 0.0); codec_rt!(tiff_cmyka16, "tiff", ColorMode::Cmyk, SampleType::U16, true, 0.0); codec_rt!(tiff_gray8, "tiff", ColorMode::Grayscale, SampleType::U8, false, 0.0); -codec_rt!(exr_rgba32, "exr", ColorMode::Rgb, SampleType::F32, true, 0.0); + +/// EXR stores linear light: a linear document round-trips exactly; an sRGB (untagged) one is +/// linearised on export and comes back tagged linear sRGB with the same colours. +#[test] +fn exr_rgba32() { + let mut d = single(ColorMode::Rgb, SampleType::F32, true); + d.icc_profile = Some(photocraft_cms::Builtin::LinearSrgb.profile().to_bytes()); + let r = export(&d, "exr", &ExportOptions::default()).expect("export"); + let back = import("x.exr", &r.bytes).expect("import").document; + assert_eq!((back.size, back.mode, back.depth), (d.size, d.mode, d.depth)); + assert_eq!(back.icc_profile.as_deref(), d.icc_profile.as_deref(), "tagged linear sRGB"); + pixels_eq(&d, &back, 0.0); + // Untagged (sRGB) → linearised. + let srgb = single(ColorMode::Rgb, SampleType::F32, true); + let r = export(&srgb, "exr", &ExportOptions::default()).expect("export"); + let back = import("x.exr", &r.bytes).expect("import").document; + let (a, b) = (srgb.layers[0].surface().unwrap().pixel(2, 3), back.layers[0].surface().unwrap().pixel(2, 3)); + for c in 0..3 { + assert!((photocraft_color::convert::srgb_to_linear(a[c]) - b[c]).abs() < 1e-4, "{a:?} -> {b:?}"); + } + assert!((a[3] - b[3]).abs() < 1e-6, "alpha kept"); +} fn smooth(mode: ColorMode) -> photocraft_doc::Document { let mut d = photocraft_doc::Document::new("s", photocraft_geom::Size::new(32, 16), mode, SampleType::U8);