Skip to content

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

32-bit documents: Exposure works on the stored linear values (#400)

floflo7778a5e89faa60e
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rs
index aaefacc47..7a39527b8 100644
--- a/crates/compose/src/adjust.rs
+++ b/crates/compose/src/adjust.rs
@@ -13,7 +13,7 @@ use crate::Buffer;
/// work in linear light (Exposure). Until ICC profiles are wired in (M8) this
/// approximates the Photoshop defaults: sRGB for colour documents and the
/// "Dot Gain 20%" grey profile (≈ gamma 1.73, fitted on the corpus) for
-/// grayscale documents.
+/// grayscale documents. 32-bit documents store linear light already.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Transfer {
/// sRGB piecewise curve.
@@ -23,8 +23,13 @@ pub enum Transfer {
}
impl Transfer {
- /// Default transfer for a document colour mode.
- pub fn for_mode(mode: photocraft_color::ColorMode) -> Self {
+ /// Default transfer for a document's colour mode and depth. 32-bit samples are linear, and
+ /// Photoshop's Exposure works on them as stored (photoshop corpus rgb32 and gray32
+ /// exposure.psd: within 0.5/255, against 69/255 through the 2.2 curve).
+ pub fn for_document(mode: photocraft_color::ColorMode, depth: photocraft_color::SampleType) -> Self {
+ if depth == photocraft_color::SampleType::F32 {
+ return Transfer::Gamma(1.0);
+ }
match mode {
photocraft_color::ColorMode::Grayscale | photocraft_color::ColorMode::Duotone | photocraft_color::ColorMode::Bitmap => Transfer::Gamma(1.732),
_ => Transfer::Srgb,
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 1e68fe7de..1591f953c 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -445,7 +445,7 @@ impl<'a> Ctx<'a> {
fn for_doc(doc: &Document, patterns: &'a pattern::PreparedPatterns<'a>) -> Self {
Self {
canvas: doc.bounds(),
- transfer: adjust::Transfer::for_mode(doc.mode),
+ transfer: adjust::Transfer::for_document(doc.mode, doc.depth),
light: doc.global_light,
patterns,
mode: doc.mode,
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index ae09ed9e7..279ff063e 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -1162,6 +1162,24 @@ fn exposure_offset_uses_gamma_2_2_in_rgb() {
assert_eq!(adjust::Transfer::Gamma(1.732).for_exposure(), adjust::Transfer::Gamma(1.732));
}
+// 32-bit samples are linear light: +1 stop doubles them as stored, where an 8-bit document goes
+// through its tone curve first (photoshop corpus rgb32 and gray32 exposure.psd).
+#[test]
+fn exposure_scales_32_bit_samples_as_stored() {
+ let plus_one_stop = Adjustment::Exposure { exposure: 1.0, offset: 0.0, gamma: 1.0 };
+ for (depth, fmt, want) in [(SampleType::F32, PixelFormat::RGBA32F, 0.4), (SampleType::U8, PixelFormat::RGBA8, 0.2744)] {
+ let mut d = Document::new("e", Size::new(1, 1), ColorMode::Rgb, depth);
+ let mut l = Layer::raster("px", fmt);
+ l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 1, 1), &[0.2, 0.2, 0.2, 1.0]);
+ d.layers = vec![l, Layer::new("exp", LayerContent::Adjustment(plus_one_stop.clone()))];
+ let got = px(&d, 0, 0)[0];
+ assert!((got - want).abs() < 2e-3, "{depth:?}: {got}");
+ }
+ for mode in [ColorMode::Rgb, ColorMode::Grayscale] {
+ assert_eq!(adjust::Transfer::for_document(mode, SampleType::F32), adjust::Transfer::Gamma(1.0));
+ }
+}
+
// A 30° Reflected gradient fill on a 4 × 4 canvas renders as Photoshop's (its end point snaps to
// the corner: t = |x − y| / 4) at every depth.
#[test]
diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rs
index 21dd7b537..b1b2365d0 100644
--- a/crates/engine/src/pixels.rs
+++ b/crates/engine/src/pixels.rs
@@ -18,7 +18,7 @@ pub fn adjust_surface(s: &mut Surface, adj: &Adjustment, selection: Option<&Surf
let raw = s.read_region(r);
let mut buf = Buffer { rect: r, px: raw.chunks_exact(n).map(|p| to_rgba(&fmt, p)).collect() };
let orig = buf.clone();
- adjust::apply_with(adj, &mut buf, adjust::Transfer::for_mode(mode));
+ adjust::apply_with(adj, &mut buf, adjust::Transfer::for_document(mode, fmt.sample));
let w = r.width() as usize;
let mut out = Vec::with_capacity(raw.len());
for (i, (a, o)) in buf.px.iter().zip(&orig.px).enumerate() {
diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs
index 86114cbff..98464fab0 100644
--- a/crates/gpu/src/plan.rs
+++ b/crates/gpu/src/plan.rs
@@ -298,7 +298,7 @@ impl<'a> DocCtx<'a> {
pub fn of(doc: &'a Document) -> Self {
DocCtx {
canvas: doc.bounds(),
- transfer: Transfer::for_mode(doc.mode),
+ transfer: Transfer::for_document(doc.mode, doc.depth),
light: doc.global_light,
patterns: &doc.patterns,
mode: doc.mode,
diff --git a/crates/io/tests/corpus.rs b/crates/io/tests/corpus.rs
index bb017a107..309a4762a 100644
--- a/crates/io/tests/corpus.rs
+++ b/crates/io/tests/corpus.rs
@@ -156,7 +156,7 @@ const PSD_TOOLS: Source = Source { label: "psd-tools corpus", dir: "corpus/psd-t
/// Our Photoshop-authored oracles (256 files; https://github.com/storytold/photocraft-corpus),
/// grouped by feature (`smart-filters`, `effects`, `text`, `adjustments/<mode><bits>`).
-const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 128, roundtrip_floor: 258 };
+const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 130, roundtrip_floor: 258 };
/// The corpus directory; a missing corpus fails the test.
fn locate(src: &Source) -> PathBuf {
diff --git a/docs/scorecard.md b/docs/scorecard.md
index 3c3b85205..f7ba16dda 100644
--- a/docs/scorecard.md
+++ b/docs/scorecard.md
@@ -7,7 +7,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js
| Area | Done | Partial | Missing | Key numbers |
|---|---:|---:|---:|---|
| [Performance](#performance) | 3 | 22 | 12 | 46 scenarios: 3 within budget, 22 over budget or failing, 13 tracked only, 4 not run, 8 not measurable yet (baseline 2026-10-05) |
-| [File compatibility](#file-compatibility) | 1 | 8 | 18 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 229/309, round trip ≥ 307/309; photoshop oracles: oracle ≥ 128/256, round trip ≥ 258/256 |
+| [File compatibility](#file-compatibility) | 1 | 8 | 18 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 229/309, round trip ≥ 307/309; photoshop oracles: oracle ≥ 130/256, round trip ≥ 258/256 |
| [Tools](#tools) | 3 | 14 | 13 | |
| [Workspace and UI](#workspace-and-ui) | 0 | 3 | 20 | settings that do nothing: 60 of 133 (target 0) |
| [Type](#type) | 0 | 3 | 6 | |
@@ -84,7 +84,7 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t
|---|---|---:|---:|---:|
| io corpus | `corpus/psd` | 170 | 146 | 169 |
| psd-tools corpus | `corpus/psd-tools` | 309 | 229 | 307 |
-| photoshop oracles | `corpus/photoshop` | 256 | 128 | 258 |
+| photoshop oracles | `corpus/photoshop` | 256 | 130 | 258 |
| Id | Target | Status | Issue | Note |
|---|---|---|---|---|