PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(image): 32 Bits/Channel holds linear light, as in Photoshop (#3792)
MrCloudy204d2874e380d
diff --git a/crates/compose/src/adjust.rs b/crates/compose/src/adjust.rsindex cc96e697f..edba48826 100644--- a/crates/compose/src/adjust.rs+++ b/crates/compose/src/adjust.rs@@ -45,14 +45,16 @@ impl Transfer {t => t,}}- fn decode(self, v: f32) -> f32 {+ /// Tone-encoded value → linear light.+ pub fn decode(self, v: f32) -> f32 {match self {Transfer::Srgb => photocraft_color::convert::srgb_to_linear(v.max(0.0)),Transfer::Gamma(1.0) => v.max(0.0),Transfer::Gamma(g) => v.max(0.0).powf(g),}}- fn encode(self, v: f32) -> f32 {+ /// Linear light → tone-encoded value.+ pub fn encode(self, v: f32) -> f32 {match self {Transfer::Srgb => photocraft_color::convert::linear_to_srgb(v.max(0.0)),Transfer::Gamma(1.0) => v.max(0.0),diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rsindex 9ccca87ad..84474d57d 100644--- a/crates/engine/src/image_cmds.rs+++ b/crates/engine/src/image_cmds.rs@@ -650,7 +650,20 @@ fn convert_depth(s: &mut Session, depth: SampleType) -> Result<Value> {return Ok(Value::Null);}s.edit("Bit Depth", |doc, _| {+ // 32-bit documents hold linear light (as Photoshop's do: grey 128 becomes 0.2159, measured+ // in Photoshop 27.11), so the colour samples cross the document's tone curve when the+ // depth crosses 32 bits: decoded after widening, encoded before narrowing (so no linear+ // value is quantised to 8 or 16 bits). Masks, alpha and channels are not colour.+ let crosses = (current == SampleType::F32) != (depth == SampleType::F32) && matches!(doc.mode, ColorMode::Rgb | ColorMode::Grayscale);+ let to_linear = depth == SampleType::F32;+ let transfer = photocraft_compose::adjust::Transfer::for_document(doc.mode, if to_linear { current } else { depth });+ if crosses && !to_linear {+ for_each_surface(&mut doc.layers, false, &mut |surf, _| surf.map_color(|v| transfer.encode(v).clamp(0.0, 1.0)));+ }convert_layers_depth(&mut doc.layers, depth);+ if crosses && to_linear {+ for_each_surface(&mut doc.layers, false, &mut |surf, _| surf.map_color(|v| transfer.decode(v)));+ }for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) {let f = ch.surface.format().with_sample(depth);ch.surface = ch.surface.convert(f);@@ -1337,6 +1350,31 @@ mod tests {assert_eq!(doc(&s).size, Size::new(10, 10));}+ #[test]+ fn thirty_two_bit_holds_linear_light() {+ // Photoshop 27.11: an 8-bit sRGB grey 128 becomes 0.2159 in 32 bits (and 32 → 0.0144),+ // and comes back unchanged; alpha keeps its value.+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 4, "height": 1, "background": "white"})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.edit("paint", |doc, active| {+ let l = doc.layer_mut(active.unwrap()).unwrap();+ l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 2, 1), &[128.0 / 255.0, 32.0 / 255.0, 1.0, 0.5]);+ Ok(())+ })+ .unwrap();+ s.execute("image.mode.bits32", json!({})).unwrap();+ let px = doc(&s).layers[1].surface().unwrap().pixel(0, 0);+ for (got, want) in px.iter().zip([0.2159, 0.0144, 1.0, 128.0 / 255.0]) {+ assert!((got - want).abs() < 2e-4, "{px:?}");+ }+ s.execute("image.mode.bits8", json!({})).unwrap();+ let px = doc(&s).layers[1].surface().unwrap().pixel(0, 0);+ for (got, want) in px.iter().zip([128.0 / 255.0, 32.0 / 255.0, 1.0, 128.0 / 255.0]) {+ assert!((got - want).abs() < 0.6 / 255.0, "{px:?}");+ }+ }+#[test]fn mode_and_depth_conversions() {let mut s = session();diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rsindex c80dce82c..dbb6c0a10 100644--- a/crates/raster/src/lib.rs+++ b/crates/raster/src/lib.rs@@ -732,6 +732,31 @@ impl Surface {out}+ /// Applies `f` to every colour sample (not alpha), including the default pixel: a tone-curve+ /// change such as linearising when a document becomes 32-bit.+ pub fn map_color(&mut self, f: impl Fn(f32) -> f32) {+ let format = self.format;+ let color = format.mode.color_channels();+ let n = format.channels();+ let mut dp = decode_pixel(&format, &self.default_pixel);+ for v in dp.iter_mut().take(color) {+ *v = f(*v);+ }+ encode_pixel(&format, &dp, &mut self.default_pixel);+ let coords: Vec<TileCoord> = self.tiles.keys().copied().collect();+ let px_count = (TILE_SIZE * TILE_SIZE) as usize;+ for c in coords {+ let tile = self.tile_mut(c);+ for i in 0..px_count {+ for k in 0..color {+ let at = i * n + k;+ let v = read_sample(&tile.data, format.sample, at);+ write_sample(&mut tile.data, format.sample, at, f(v));+ }+ }+ }+ }+/// Convert to another pixel format (depth and/or model; colour models via [`convert_pixel`]).pub fn convert(&self, to: PixelFormat) -> Surface {let mut out = Surface::with_default(to, &convert_pixel(&self.format, &to, &self.default_pixel()));