Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Add linear floating-point EXR Quick Export

jonathnwxh3ad7333a1631
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index ed8b7f148..28bde21c0 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -86,7 +86,7 @@ choice!(UiFontSize { Tiny = "tiny", Small = "small", Medium = "medium", Large =
choice!(LogDestination { Metadata = "metadata", TextFile = "textFile", Both = "both" } default Metadata);
choice!(LogDetail { SessionsOnly = "sessionsOnly", Concise = "concise", Detailed = "detailed" } default Concise);
choice!(Ask { Always = "always", Never = "never", Ask = "ask" } default Always);
-choice!(QuickExportFormat { Png = "png", Jpg = "jpg", Gif = "gif", Webp = "webp" } default Png);
+choice!(QuickExportFormat { Png = "png", Jpg = "jpg", Gif = "gif", Webp = "webp", Exr = "exr" } default Png);
choice!(ExportLocation { Ask = "ask", SameFolder = "sameFolder" } default Ask);
choice!(ExportMetadata { None = "none", Copyright = "copyright", All = "all" } default Copyright);
choice!(
diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rs
index 755ba0732..333b2b073 100644
--- a/crates/engine/src/web_cmds.rs
+++ b/crates/engine/src/web_cmds.rs
@@ -1099,6 +1099,15 @@ fn quick_export(s: &mut Session, p: &Value) -> Result<Value> {
};
let d = s.active().ok_or(EngineError::NoDocument)?;
let doc = d.doc.clone();
+ if fmt == "exr" {
+ // EXR keeps floating-point scene values. The ordinary exporter converts to linear
+ // light; the web/sRGB pipeline would instead clamp HDR and negative values.
+ let opts = photocraft_io::ExportOptions { xmp: photocraft_io::XmpEmbed::None, ..Default::default() };
+ let out = photocraft_io::export(&doc, "exr", &opts).map_err(other)?;
+ write_file(&path, &out.bytes)?;
+ crate::automate_cmds::fire_event(s, "export");
+ return Ok(json!({"path": path, "format": fmt, "warnings": out.warnings}));
+ }
let metadata = serde_json::to_value(prefs.metadata).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_default();
let mut o = serde_json::Map::new();
o.insert(
diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rs
index 5791e5f27..6352293ee 100644
--- a/crates/engine/src/web_cmds/tests.rs
+++ b/crates/engine/src/web_cmds/tests.rs
@@ -977,3 +977,37 @@ fn web_exif_that_does_not_fit_is_reported_and_bad_exif_is_harmless() {
assert_eq!(decode(&path).dimensions(), (64, 48));
}
}
+
+#[test]
+fn quick_export_exr_preserves_linear_hdr_and_alpha() {
+ use photocraft_codecs::{ChannelLayout, Format, Image, SampleType};
+ let dir = tmp("quick_exr_hdr");
+ let samples = [-0.25, 2.0, 0.125, 1.0, 4.0, 0.25, 0.5, 0.5];
+ let image = Image::from_f32(2, 1, ChannelLayout::Rgba, &samples).unwrap();
+ let bytes = photocraft_codecs::encode(&image, Format::OpenExr, &Default::default()).unwrap();
+ let doc = photocraft_io::import("hdr.exr", &bytes).unwrap().document;
+ let mut s = Session::new();
+ s.add_document(doc, Some(format!("{dir}/source.psd")));
+ for convert in [true, false] {
+ s.execute("file.export.exportPreferences", json!({"quickExportFormat": "exr", "quickExportLocation": "sameFolder", "convertToSrgb": convert})).unwrap();
+ let result = s.execute("file.export.quickExport", json!({})).unwrap();
+ assert_eq!(result["format"], "exr");
+ let path = format!("{dir}/source.exr");
+ assert_eq!(result["path"], path);
+ let decoded = decode(&path);
+ assert_eq!(decoded.sample_type(), SampleType::F32);
+ let actual = decoded.convert(ChannelLayout::Rgba, SampleType::F32).to_f32_samples().unwrap();
+ for (got, expected) in actual.iter().zip(samples) {
+ assert!((got - expected).abs() < 0.0001, "EXR preserves scene values: {actual:?}");
+ }
+ assert_eq!(actual.len(), samples.len());
+ }
+ // The format preference must never silently overwrite the source EXR.
+ let own = format!("{dir}/source.exr");
+ s.active_mut().unwrap().path = Some(own.clone());
+ let before = std::fs::read(&own).unwrap();
+ assert!(s.execute("file.export.quickExport", json!({})).is_err());
+ assert_eq!(std::fs::read(&own).unwrap(), before);
+ s.execute("file.export.quickExport", json!({"path": format!("{dir}/explicit.exr")})).unwrap();
+ assert_eq!(decode(&format!("{dir}/explicit.exr")).dimensions(), (2, 1));
+}
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 63b46f442..cfe28ef02 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1000,6 +1000,7 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
QuickExportFormat::Jpg => "JPG",
QuickExportFormat::Gif => "GIF",
QuickExportFormat::Webp => "WebP",
+ QuickExportFormat::Exr => "EXR",
}
};
item.label = format!("Quick Export as {format}");
@@ -2168,7 +2169,7 @@ mod quick_export_label_tests {
#[test]
fn file_quick_export_menu_reflects_selected_format_without_renaming_layer_export() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
- for (value, expected) in [("png", "PNG"), ("jpg", "JPG"), ("gif", "GIF"), ("webp", "WebP")] {
+ for (value, expected) in [("png", "PNG"), ("jpg", "JPG"), ("gif", "GIF"), ("webp", "WebP"), ("exr", "EXR")] {
app.run("prefs.set", json!({"values": {"export.quickExportFormat": value}})).unwrap();
let items = menu_items(&app);
let quick: Vec<_> = items.iter().filter(|i| i.id == "file.export.quickExportAsPng").collect();
diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rs
index 5cd2e547d..5d2b01115 100644
--- a/crates/ui-egui/src/prefs_ui.rs
+++ b/crates/ui-egui/src/prefs_ui.rs
@@ -1435,6 +1435,7 @@ fn has_visible_fields(values: &Value, section: &str) -> bool {
fn export_field_visible(obj: &Map<String, Value>, key: &str) -> bool {
let format = obj.get("quickExportFormat").and_then(Value::as_str).unwrap_or("png");
match key {
+ "convertToSrgb" | "metadata" => format != "exr",
"jpegQuality" => format == "jpg",
"webpLossless" => format == "webp",
"webpQuality" => format == "webp" && obj.get("webpLossless").and_then(Value::as_bool) != Some(true),
@@ -2640,6 +2641,10 @@ mod tests {
assert!(export_field_visible(&obj, "webpQuality"));
assert_eq!(humanize("webpLossless"), "Lossless");
assert_eq!(humanize("webpQuality"), "Quality");
+ obj.insert("quickExportFormat".into(), json!("exr"));
+ for key in ["jpegQuality", "webpLossless", "webpQuality", "convertToSrgb", "metadata"] {
+ assert!(!export_field_visible(&obj, key), "EXR must not offer ignored web settings: {key}");
+ }
}
#[test]