14acdc2a6d
* Render: one sRGB transfer function, geom::srgb, for the hand-written copies (#7161) Twelve places wrote the sRGB encode or decode formula out by hand (doc's glass recipe, engine's render library, glTF import, the PNG film, photographic and presentation output, the ray tracer's image, the stage, the surface decoder, texture normalisation and the theme's contrast check). They now call solidcraft_geom::srgb: encode/decode on 0..1 values, encode8/decode8 for 8-bit codes. render::surface::srgb8_to_linear stays as a re-export, so its callers are unchanged. Same results: the module's tests compare it bit for bit with the formula as the callers wrote it, over all 256 codes and a dense sweep of linear values plus the knees, out-of-range values, infinities and NaN, for each way the callers clamped (after encoding, before it, or not at all), and decoded values either side of the decode knee, where no 8-bit code falls. One change: glTF import encoded in f32. Over every f32 in 0..1, 218 of 1,065,353,217 values sit on a rounding tie that f32 got wrong and now give the neighbouring code (f64 gives the nearer one). Left as they are: render/job/image.rs (replaced by exact tables in its own change), the tests and the starter-pack validator that keep their own copy as an independent reference, and the WGSL shaders (they can't call Rust). Fixes #7161 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: the sRGB codes of five callers that had no test of their output (#7161) The glass swatch, a library surface applied as a plain appearance, glTF's fallback for a colour channel that isn't a number, a stage panel and an empty pixel in presentation output each get an assertion with their sRGB codes worked out by hand. A planted wrong conversion at each is now caught by that test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
78 lines
3.6 KiB
Rust
78 lines
3.6 KiB
Rust
//! The sRGB transfer function (IEC 61966-2-1), once: linear light to an sRGB value and back, and the 8-bit codes most
|
|
//! callers want. Every encode and decode in Rust goes through here, so a change to the rule is one change. (Shaders
|
|
//! can't call Rust and keep their own copies.)
|
|
|
|
/// Linear light (0..1) to an sRGB value (0..1): straight below the knee at 0.0031308, a 1/2.4 power above it.
|
|
pub fn encode(linear: f64) -> f64 {
|
|
if linear <= 0.003_130_8 { 12.92 * linear } else { 1.055 * linear.powf(1.0 / 2.4) - 0.055 }
|
|
}
|
|
|
|
/// An sRGB value (0..1) to linear light: straight below the knee at 0.04045, a 2.4 power above it.
|
|
pub fn decode(encoded: f64) -> f64 {
|
|
if encoded <= 0.040_45 { encoded / 12.92 } else { ((encoded + 0.055) / 1.055).powf(2.4) }
|
|
}
|
|
|
|
/// Linear light to the nearest 8-bit sRGB code; out of range reads as the nearest end, and NaN as 0.
|
|
pub fn encode8(linear: f64) -> u8 {
|
|
(encode(linear).clamp(0.0, 1.0) * 255.0).round() as u8
|
|
}
|
|
|
|
/// An 8-bit sRGB code to linear light.
|
|
pub fn decode8(code: u8) -> f64 {
|
|
decode(f64::from(code) / 255.0)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// The formulas as each caller wrote them before this module: the module must give the same results.
|
|
fn encode_as_written(v: f64) -> f64 {
|
|
if v <= 0.0031308 { 12.92 * v } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }
|
|
}
|
|
fn decode_as_written(v: f64) -> f64 {
|
|
if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }
|
|
}
|
|
|
|
#[test]
|
|
fn every_code_decodes_as_before_and_encodes_back_to_itself() {
|
|
for code in 0..=255u8 {
|
|
let linear = decode8(code);
|
|
assert_eq!(linear.to_bits(), decode_as_written(f64::from(code) / 255.0).to_bits(), "{code}");
|
|
assert_eq!(encode8(linear), code, "{code}");
|
|
}
|
|
assert_eq!((decode8(0), decode8(255)), (0.0, 1.0));
|
|
assert!((decode8(10) - (10.0 / 255.0) / 12.92).abs() < 1e-15, "the straight part below the knee");
|
|
assert!((decode8(128) - 0.21586050011389926).abs() < 1e-14);
|
|
}
|
|
|
|
#[test]
|
|
fn values_decode_as_every_caller_did() {
|
|
// Either side of the knee (no 8-bit code falls between 0.04 and 0.04045), out of range, and a dense sweep.
|
|
let mut values = vec![0.0404, 0.040_45, 0.040_45_f64.next_up(), -0.5, 1.5];
|
|
values.extend((0..=100_000).map(|i| f64::from(i) / 100_000.0));
|
|
for v in values {
|
|
assert_eq!(decode(v).to_bits(), decode_as_written(v).to_bits(), "{v}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn linear_values_encode_as_every_caller_did() {
|
|
// Below zero, the knees, above one, the extremes and NaN, and a dense sweep in between.
|
|
let mut values = vec![-1.0, -0.0, 0.0, 0.003_130_8, 0.003_130_8_f64.next_up(), 0.04045, 1.0, 1.0f64.next_up(), 2.0];
|
|
values.extend([f64::MIN_POSITIVE, f64::MAX, f64::INFINITY, f64::NEG_INFINITY, f64::NAN]);
|
|
values.extend((0..=200_000).map(|i| f64::from(i) / 100_000.0 - 0.5));
|
|
for v in values {
|
|
assert_eq!(encode(v).to_bits(), encode_as_written(v).to_bits(), "{v}");
|
|
let byte = encode8(v);
|
|
// Clamped after encoding (film_png, raytrace, photographic, presentation, appearance_pack) ...
|
|
assert_eq!(byte, (encode_as_written(v).clamp(0.0, 1.0) * 255.0).round() as u8, "{v}");
|
|
// ... before it (engine's render library) ...
|
|
let first = (encode_as_written(v.clamp(0.0, 1.0)) * 255.0).round().clamp(0.0, 255.0) as u8;
|
|
// ... or not at all, the cast saturating (render's stage).
|
|
let none = (encode_as_written(v) * 255.0).round() as u8;
|
|
assert_eq!((byte, byte), (first, none), "{v}");
|
|
}
|
|
}
|
|
}
|