#[inline]
pub fn gamma_correct(x: f32, gamma: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
let g = gamma.max(1e-6);
x.powf(1.0 / g).clamp(0.0, 1.0)
}
#[inline]
pub fn srgb_gamma(x: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
if x <= 0.003_130_8_f32 {
12.92 * x
} else {
1.055 * x.powf(1.0 / 2.4) - 0.055
}
}
#[inline]
pub fn inverse_srgb_gamma(x: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
if x <= 0.04045_f32 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}
#[inline]
pub fn apply_gamma(value: f32, gamma: f32) -> f32 {
let g = gamma.max(1e-6);
value.max(0.0).powf(1.0 / g)
}
pub fn apply_gamma_image(img: &[f32], gamma: f32) -> Vec<f32> {
img.iter().map(|&v| apply_gamma(v, gamma)).collect()
}
#[inline]
pub fn hdr_linear_to_srgb(v: f32) -> f32 {
let v = v.clamp(0.0, 1.0);
if v <= 0.003_130_8_f32 {
12.92 * v
} else {
(1.055 * v.powf(1.0 / 2.4) - 0.055).clamp(0.0, 1.0)
}
}
pub fn image_hdr_linear_to_srgb(img: &[f32]) -> Vec<f32> {
img.iter().map(|&v| hdr_linear_to_srgb(v)).collect()
}
#[inline]
pub fn srgb_to_linear_hdr(v: f32) -> f32 {
let v = v.clamp(0.0, 1.0);
if v <= 0.04045_f32 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
}