use crate::math::powf;
#[inline]
pub fn scale_100_to_203(linear: f32) -> f32 {
linear * 2.03
}
#[inline]
pub fn scale_203_to_100(linear: f32) -> f32 {
linear / 2.03
}
#[inline]
pub fn ootf_gamma_adjust(linear: f32, gamma: f32) -> f32 {
if linear <= 0.0 {
return 0.0;
}
powf(linear, gamma)
}
#[inline]
pub fn gamma_203_to_100(linear: f32) -> f32 {
if linear <= 0.0 {
return 0.0;
}
powf(linear, 1.0 / 1.08)
}
#[inline]
pub fn gamma_100_to_203(linear: f32) -> f32 {
if linear <= 0.0 {
return 0.0;
}
powf(linear, 1.08)
}
pub const OOTF_GAMMA_BBC: f32 = 1.15;
pub const OOTF_GAMMA_ARIB: f32 = 1.16;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scale_roundtrip() {
let v = 0.5_f32;
let up = scale_100_to_203(v);
let back = scale_203_to_100(up);
assert!((back - v).abs() < 1e-6);
}
#[test]
fn scale_203_at_peak() {
let out = scale_100_to_203(1.0);
assert!((out - 2.03).abs() < 1e-5);
}
#[test]
fn ootf_at_unity() {
let out = ootf_gamma_adjust(1.0, OOTF_GAMMA_BBC);
assert!((out - 1.0).abs() < 1e-5);
}
#[test]
fn ootf_darkens_midtones() {
let out = ootf_gamma_adjust(0.5, OOTF_GAMMA_BBC);
assert!(out < 0.5, "OOTF should darken mid-gray: {out}");
}
#[test]
fn gamma_203_100_roundtrip() {
let v = 0.3_f32;
let down = gamma_203_to_100(v);
let back = gamma_100_to_203(down);
assert!((back - v).abs() < 1e-5, "roundtrip: {back} vs {v}");
}
#[test]
fn gamma_preserves_black() {
assert_eq!(gamma_203_to_100(0.0), 0.0);
assert_eq!(gamma_100_to_203(0.0), 0.0);
}
}