craft-develop: preserve LightCraft raster sRGB rounding (#587) (#3017)

Carry over LightCraft's sqrt-domain quantizer from 08179b9b for the extracted raster conversion; leave craft-color's general transfer lookup unchanged.
This commit is contained in:
FredApps
2026-11-22 08:54:06 +01:00
committed by GitHub
parent 742af23a37
commit eda95dd6b4
+25 -2
View File
@@ -219,6 +219,22 @@ impl<T: Copy + Default> Image<T> {
}
}
// Keep the sqrt-spaced 14-bit lookup from LightCraft's raster conversion. The uniform
// craft-color byte lookup makes different quantization choices for some near-black inputs.
#[inline]
fn encode_raster_srgb8(v: f32) -> u8 {
const LAST: usize = (1 << 14) - 1;
static LUT: std::sync::OnceLock<[u8; LAST + 1]> = std::sync::OnceLock::new();
let lut = LUT.get_or_init(|| {
std::array::from_fn(|i| {
let t = i as f32 / LAST as f32;
(craft_color::linear_to_srgb(t * t) * 255.0 + 0.5) as u8
})
});
let i = (v.clamp(0.0, 1.0).sqrt() * LAST as f32 + 0.5) as usize;
lut[i.min(LAST)]
}
impl Rgb32f {
/// Bilinear sample at continuous pixel coordinates (pixel centres at +0.5).
#[inline]
@@ -250,8 +266,7 @@ impl Rgb32f {
/// Encode to 8-bit sRGB from linear sRGB values (no gamut conversion).
pub fn to_srgb8(&self) -> Rgba8 {
use craft_color::linear_to_srgb_u8;
self.map(|p| [linear_to_srgb_u8(p[0]), linear_to_srgb_u8(p[1]), linear_to_srgb_u8(p[2]), 255])
self.map(|p| [encode_raster_srgb8(p[0]), encode_raster_srgb8(p[1]), encode_raster_srgb8(p[2]), 255])
}
}
@@ -369,6 +384,14 @@ mod tests {
assert!((m[0] - 0.5).abs() < 1e-6);
}
#[test]
fn raster_srgb8_keeps_lightcraft_float_quantization() {
// Referenced to LightCraft's sqrt-domain conversion, not the 8-bit decode grid.
let samples = [[0.00013, 0.00016, 0.0005], [0.003_130_8, 0.18, 0.5], [-0.1, 1.0, 1.1]];
let img = Rgb32f::from_fn(samples.len(), 1, |x, _| samples[x]);
assert_eq!(img.to_srgb8().data, [[0, 1, 2, 255], [10, 118, 188, 255], [0, 255, 255, 255]]);
}
#[test]
fn srgb8_roundtrip() {
let img = Rgba8::from_fn(16, 16, |x, y| [(x * 16) as u8, (y * 16) as u8, 7, 255]);