PhotoCraft: An open-source, photo editor written by scratch in pure Rust
refactor: use photocraft-color's srgb transfer functions in algo and proof simulation (#7604)
Omar Saidahmed6385fced3df9
diff --git a/crates/algo/src/camera_raw.rs b/crates/algo/src/camera_raw.rsindex f855b08bf..3cadd1d20 100644--- a/crates/algo/src/camera_raw.rs+++ b/crates/algo/src/camera_raw.rs@@ -67,22 +67,16 @@ impl Encoding {pub(crate) fn of(gamma: f32) -> Self {Encoding(if gamma.is_finite() && gamma > 0.0 { gamma.clamp(0.5, 4.0) } else { 0.0 })}+ /// A value in this encoding as linear light.pub(crate) fn decode(self, v: f32) -> f32 {if self.0 > 0.0 { v.max(0.0).powf(self.0) } else { srgb_to_linear(v) }}+ /// Linear light back into this encoding, the inverse of [`Encoding::decode`].pub(crate) fn encode(self, v: f32) -> f32 {if self.0 > 0.0 { v.max(0.0).powf(1.0 / self.0) } else { linear_to_srgb(v) }}}-fn srgb_encode(v: f32) -> f32 {- linear_to_srgb(v)-}--fn srgb_decode(v: f32) -> f32 {- srgb_to_linear(v)-}-fn luma_linear(c: [f32; 3]) -> f32 {luma(c)}diff --git a/crates/algo/src/camera_raw/whites_blacks.rs b/crates/algo/src/camera_raw/whites_blacks.rsindex 382ade50e..93bb9cff3 100644--- a/crates/algo/src/camera_raw/whites_blacks.rs+++ b/crates/algo/src/camera_raw/whites_blacks.rs@@ -369,14 +369,14 @@ pub(crate) fn apply(px: &mut [[f32; 4]], width: usize, w: f32, b: f32, enc: Encoif per_channel {for c in 0..3 {let over = (lin[c] - 1.0).max(0.0);- q[c] = enc.encode(super::srgb_decode(grey(super::srgb_encode(lin[c].min(1.0)))) + over);+ q[c] = enc.encode(super::srgb_to_linear(grey(super::linear_to_srgb(lin[c].min(1.0)))) + over);}} else {let y = super::luma_linear(lin).max(0.0);if y <= 1e-7 {continue;}- let y2 = super::srgb_decode(grey(super::srgb_encode(y.min(1.0)))) + (y - 1.0).max(0.0);+ let y2 = super::srgb_to_linear(grey(super::linear_to_srgb(y.min(1.0)))) + (y - 1.0).max(0.0);let k = y2 / y;for c in 0..3 {q[c] = enc.encode(lin[c] * k);diff --git a/crates/algo/src/photo_util.rs b/crates/algo/src/photo_util.rsindex bf65bc445..b29612769 100644--- a/crates/algo/src/photo_util.rs+++ b/crates/algo/src/photo_util.rs@@ -183,15 +183,7 @@ pub(crate) fn hash01(x: i64, y: i64, seed: u64) -> f32 {(z >> 40) as f32 / (1u64 << 24) as f32}-/// sRGB transfer function, decode (extended linearly below 0 and as a power above 1).-pub(crate) fn srgb_to_linear(v: f32) -> f32 {- if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }-}--/// sRGB transfer function, encode.-pub(crate) fn linear_to_srgb(v: f32) -> f32 {- if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }-}+pub(crate) use photocraft_color::convert::{linear_to_srgb, srgb_to_linear};/// Bilinear sample of channel `c` of a `w × h × ch` buffer, clamped to the edges.pub(crate) fn bilinear(buf: &[f32], w: usize, h: usize, ch: usize, c: usize, x: f32, y: f32) -> f32 {@@ -217,6 +209,18 @@ pub(crate) fn catmull_rom(t: f32) -> [f32; 4] {mod tests {use super::*;+ /// The shared sRGB functions match the removed copies bit for bit.+ #[test]+ fn srgb_transfer_output_is_unchanged() {+ let decode = |v: f32| if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) };+ let encode = |v: f32| if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 };+ for i in -2000..=6000 {+ let v = i as f32 / 4000.0;+ assert_eq!(srgb_to_linear(v).to_bits(), decode(v).to_bits(), "decode {v}");+ assert_eq!(linear_to_srgb(v).to_bits(), encode(v).to_bits(), "encode {v}");+ }+ }+#[test]fn dense_and_sparse_solvers_agree() {// Overdetermined line fit y = 2x + 1.diff --git a/crates/engine/src/proof_sim.rs b/crates/engine/src/proof_sim.rsindex f225a5a7a..6c7afc15d 100644--- a/crates/engine/src/proof_sim.rs+++ b/crates/engine/src/proof_sim.rs@@ -18,6 +18,7 @@use photocraft_cms::{Builtin, Intent, Lut3d, Transform};use photocraft_color::SampleType;+use photocraft_color::convert::{linear_to_srgb, srgb_to_linear};use photocraft_doc::Document;use serde::{Deserialize, Serialize};use serde_json::{Value, json};@@ -91,14 +92,16 @@ impl Default for HdrPreview {}impl HdrPreview {+ /// Whether the preview passes values through unchanged.pub fn is_identity(&self) -> bool {self.highlight_compression || (self.exposure == 0.0 && self.gamma == 1.0)}+ /// One encoded channel value through the 32-bit preview's exposure and gamma.fn apply(&self, v: f32) -> f32 {if self.is_identity() {return v;}- let lin = srgb_decode(v) * 2f32.powf(self.exposure);+ let lin = srgb_to_linear(v) * 2f32.powf(self.exposure);srgb_encode(lin.max(0.0).powf(1.0 / self.gamma).min(1.0))}}@@ -115,13 +118,9 @@ pub fn signature(c: &ColorState, doc: &Document) -> String {format!("{:?} {:?}", pv.setup.kind, h)}-fn srgb_decode(v: f32) -> f32 {- if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }-}-+/// sRGB-encodes linear light after clamping it to 0..=1.fn srgb_encode(v: f32) -> f32 {- let v = v.clamp(0.0, 1.0);- if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }+ linear_to_srgb(v.clamp(0.0, 1.0))}/// Viénot et al. (1999): linear RGB → LMS.@@ -201,7 +200,7 @@ pub fn display_lut_with(c: &ColorState, doc: &Document, size: usize, include_hdrlet protan = kind == ProofKind::Protanopia;Box::new(move |v| {let s = eval3(&to_srgb, &v);- let sim = dichromat(s.map(srgb_decode), protan).map(srgb_encode);+ let sim = dichromat(s.map(srgb_to_linear), protan).map(srgb_encode);eval3(&to_mon, &sim)})}@@ -262,6 +261,7 @@ fn run_kind(s: &mut Session, id: &str) -> Result<Value> {set_kind(s, kind)}+/// View › 32-bit Preview Options for the active document.fn preview_32(s: &mut Session, p: &Value) -> Result<Value> {const CMD: &str = "view.thirtyTwoBitPreviewOptions";let id = s.active().ok_or(EngineError::NoDocument)?.doc.id;@@ -298,6 +298,7 @@ macro_rules! kind_spec {};}+/// The View › Proof Setup commands.pub fn specs() -> Vec<CommandSpec> {vec![kind_spec!("view.proofSetup.workingCyanPlate", "Working Cyan Plate"),@@ -325,6 +326,21 @@ pub fn specs() -> Vec<CommandSpec> {mod tests {use super::*;+ /// The shared sRGB functions match the removed copies bit for bit.+ #[test]+ fn srgb_transfer_output_is_unchanged() {+ let decode = |v: f32| if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) };+ let encode = |v: f32| {+ let v = v.clamp(0.0, 1.0);+ if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }+ };+ for i in -2000..=6000 {+ let v = i as f32 / 4000.0;+ assert_eq!(srgb_to_linear(v).to_bits(), decode(v).to_bits(), "decode {v}");+ assert_eq!(srgb_encode(v).to_bits(), encode(v).to_bits(), "encode {v}");+ }+ }+fn session(mode: &str, depth: u32) -> Session {let mut s = Session::new();s.execute("file.new", json!({"width": 8, "height": 8, "mode": mode, "depth": depth})).unwrap();