EXR/HDR are linear: tag imports linear sRGB, linearise exports

Refs #46

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Brandon ThomasandClaude Opus 5.5 committed 2026-10-04 15:44:00 -04:00
1 parent f3439169f2
commit 6d0de7bb54
2 files changed
+51 -1

No files matched your search

+29
View File
@@ -46,6 +46,11 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result<ImportResult,
}
doc.layers.push(bg);
doc.icc_profile = img.icc.clone().map(Arc::new);
// OpenEXR and Radiance HDR hold linear, scene-referred values (Rec. 709 primaries unless
// stated otherwise): tag them linear sRGB so they display and convert correctly.
if doc.icc_profile.is_none() && mode == ColorMode::Rgb && matches!(codecs::detect(bytes), Some(Format::OpenExr | Format::Hdr)) {
doc.icc_profile = Some(photocraft_cms::Builtin::LinearSrgb.profile().to_bytes());
}
doc.metadata.exif = img.meta.exif.clone().map(Arc::new);
doc.metadata.xmp = img.meta.xmp.clone();
if let Some((x, _)) = img.meta.dpi {
@@ -178,6 +183,9 @@ pub fn export_flat(doc: &Document, format: Format, opts: &ExportOptions) -> 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<Image, IoError> {
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<Image, IoError> {
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<Option<ExportResult>, IoError> {
match doc.mode {
+22 -1
View File
@@ -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);