use crate::math;
pub trait Channel: Copy + PartialEq + PartialOrd {
const ZERO: Self;
const ONE: Self;
const HALF: Self;
#[must_use]
fn scale(self, rhs: Self) -> Self;
#[must_use]
fn sum_of_products(a: Self, b: Self, c: Self, d: Self) -> Self;
#[must_use]
fn div_normalized(self, rhs: Self) -> Self;
#[must_use]
fn sqrt_normalized(self) -> Self;
#[must_use]
fn soft_light_d(self) -> Self;
#[must_use]
fn complement(self) -> Self;
#[must_use]
fn saturating_add(self, rhs: Self) -> Self;
#[must_use]
fn double_minus_one(self) -> Self {
self.saturating_sub(self.complement())
}
#[must_use]
fn saturating_sub(self, rhs: Self) -> Self;
#[must_use]
fn abs_diff(self, rhs: Self) -> Self;
#[must_use]
fn to_f32(self) -> f32;
#[must_use]
fn from_f32(value: f32) -> Self;
#[must_use]
fn lerp(self, to: Self, t: Self) -> Self {
Self::sum_of_products(self, t.complement(), to, t)
}
#[must_use]
fn min_channel(self, rhs: Self) -> Self {
if self < rhs { self } else { rhs }
}
#[must_use]
fn max_channel(self, rhs: Self) -> Self {
if self > rhs { self } else { rhs }
}
#[must_use]
fn is_zero(self) -> bool {
self == Self::ZERO
}
#[must_use]
fn is_one(self) -> bool {
self == Self::ONE
}
}
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn div255(v: u32) -> u8 {
debug_assert!(v <= 2 * 255 * 255);
let q = (v + 127) / 255;
if q > 255 { 255 } else { q as u8 }
}
const fn sqrt_round(v: u32) -> u32 {
(4 * v).isqrt().div_ceil(2)
}
impl Channel for u8 {
const ZERO: Self = 0;
const ONE: Self = 255;
const HALF: Self = 127;
fn scale(self, rhs: Self) -> Self {
div255(u32::from(self) * u32::from(rhs))
}
fn sum_of_products(a: Self, b: Self, c: Self, d: Self) -> Self {
div255(u32::from(a) * u32::from(b) + u32::from(c) * u32::from(d))
}
#[allow(clippy::cast_possible_truncation)]
fn div_normalized(self, rhs: Self) -> Self {
if rhs == 0 {
return Self::ONE;
}
let rhs = u32::from(rhs);
let q = (u32::from(self) * 255 + rhs / 2) / rhs;
if q > 255 { 255 } else { q as Self }
}
#[allow(clippy::cast_possible_truncation)]
fn sqrt_normalized(self) -> Self {
sqrt_round(u32::from(self) * 255) as Self
}
fn soft_light_d(self) -> Self {
if self > 63 {
return self.sqrt_normalized();
}
let v = u32::from(self);
let num = 16 * v * v * v + 260_100 * v - 3060 * v * v;
#[allow(clippy::cast_possible_truncation)]
{
((2 * num + 65025) / (2 * 65025)) as Self
}
}
fn complement(self) -> Self {
255 - self
}
#[allow(clippy::cast_possible_truncation)]
fn saturating_add(self, rhs: Self) -> Self {
let sum = u16::from(self) + u16::from(rhs);
if sum > 255 { 255 } else { sum as Self }
}
fn saturating_sub(self, rhs: Self) -> Self {
Self::saturating_sub(self, rhs)
}
fn abs_diff(self, rhs: Self) -> Self {
Self::abs_diff(self, rhs)
}
fn to_f32(self) -> f32 {
f32::from(self) / 255.0
}
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn from_f32(value: f32) -> Self {
if value <= 0.0 {
return 0;
}
if value >= 1.0 {
return 255;
}
math::round(value * 255.0) as Self
}
}
const fn saturate(v: f32) -> f32 {
if v < 0.0 {
0.0
} else if v > 1.0 {
1.0
} else {
v
}
}
impl Channel for f32 {
const ZERO: Self = 0.0;
const ONE: Self = 1.0;
const HALF: Self = 0.5;
fn scale(self, rhs: Self) -> Self {
self * rhs
}
#[allow(clippy::suboptimal_flops)]
fn sum_of_products(a: Self, b: Self, c: Self, d: Self) -> Self {
saturate(a * b + c * d)
}
fn div_normalized(self, rhs: Self) -> Self {
if rhs == 0.0 {
return Self::ONE;
}
saturate(self / rhs)
}
fn sqrt_normalized(self) -> Self {
math::sqrt(self)
}
#[allow(clippy::suboptimal_flops)]
fn soft_light_d(self) -> Self {
if self > 0.25 {
return self.sqrt_normalized();
}
((16.0 * self - 12.0) * self + 4.0) * self
}
fn complement(self) -> Self {
1.0 - self
}
fn saturating_add(self, rhs: Self) -> Self {
saturate(self + rhs)
}
fn saturating_sub(self, rhs: Self) -> Self {
saturate(self - rhs)
}
fn abs_diff(self, rhs: Self) -> Self {
if self > rhs { self - rhs } else { rhs - self }
}
fn to_f32(self) -> f32 {
self
}
fn from_f32(value: f32) -> Self {
saturate(value)
}
}
#[cfg(test)]
#[allow(
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::float_cmp,
clippy::suboptimal_flops
)]
mod tests {
use super::*;
const fn div255_reference(v: u32) -> u32 {
(2 * v + 255) / 510
}
#[test]
fn div255_is_exactly_round_to_nearest() {
for v in 0..=(2 * 255 * 255) {
assert_eq!(
u32::from(div255(v)).min(255),
div255_reference(v).min(255),
"v = {v}",
);
}
}
#[test]
fn shift_trick_diverges_above_65662() {
const fn shift_trick(v: u32) -> u32 {
let p = v + 128;
(p + (p >> 8)) >> 8
}
for v in 0..=65662 {
assert_eq!(shift_trick(v), div255_reference(v), "v = {v}");
}
assert_ne!(shift_trick(65663), div255_reference(65663));
}
#[test]
fn legacy_source_over_trick_was_truncating() {
const fn legacy(v: u32) -> u32 {
(v + (v >> 8) + 1) >> 8
}
let mut differ = 0;
for v in 0..=65025u32 {
assert_eq!(legacy(v), v / 255, "legacy is exactly floor; v = {v}");
if legacy(v) != div255_reference(v) {
differ += 1;
}
}
assert_eq!(differ, 32385);
}
#[test]
fn u8_scale_matches_exact_rational() {
for a in 0..=255u8 {
for b in 0..=255u8 {
let expected = div255_reference(a as u32 * b as u32) as u8;
assert_eq!(a.scale(b), expected, "{a} * {b}");
}
}
}
#[test]
fn u8_scale_endpoints_are_exact() {
for v in 0..=255u8 {
assert_eq!(v.scale(255), v, "MAX is the multiplicative identity");
assert_eq!(v.scale(0), 0);
}
}
#[test]
fn u8_scale_by_max_never_drifts() {
let mut v = 200u8;
for _ in 0..1000 {
v = v.scale(255);
}
assert_eq!(v, 200);
}
#[test]
fn u8_sum_of_products_rounds_once() {
for a in (0..=255u8).step_by(17) {
for b in (0..=255u8).step_by(13) {
for c in (0..=255u8).step_by(19) {
for d in (0..=255u8).step_by(11) {
let wide = a as u32 * b as u32 + c as u32 * d as u32;
let expected = div255_reference(wide).min(255) as u8;
assert_eq!(
<u8 as Channel>::sum_of_products(a, b, c, d),
expected,
"{a}*{b} + {c}*{d}",
);
}
}
}
}
}
#[test]
fn u8_sum_of_products_saturates() {
assert_eq!(<u8 as Channel>::sum_of_products(255, 255, 255, 255), 255);
}
#[test]
fn u8_lerp_endpoints_are_exact() {
for a in 0..=255u8 {
for b in (0..=255u8).step_by(5) {
assert_eq!(a.lerp(b, 0), a, "t = MIN returns self");
assert_eq!(a.lerp(b, 255), b, "t = MAX returns to");
}
}
}
#[test]
fn u8_lerp_midpoint() {
assert_eq!(0u8.lerp(255, 127), 127);
assert_eq!(0u8.lerp(255, 128), 128);
assert_eq!(100u8.lerp(200, 128), 150);
}
#[test]
fn u8_div_normalized() {
assert_eq!(128u8.div_normalized(255), 128);
assert_eq!(
255u8.div_normalized(128),
255,
"saturates instead of wrapping"
);
assert_eq!(64u8.div_normalized(128), 128);
assert_eq!(1u8.div_normalized(0), 255, "divide by zero is ONE");
assert_eq!(0u8.div_normalized(0), 255);
}
#[test]
fn double_minus_one() {
assert_eq!(255u8.double_minus_one(), 255);
assert_eq!(128u8.double_minus_one(), 1);
assert_eq!(127u8.double_minus_one(), 0, "saturates at ZERO");
assert_eq!(0u8.double_minus_one(), 0);
assert_eq!(1.0f32.double_minus_one(), 1.0);
assert_eq!(0.75f32.double_minus_one(), 0.5);
assert_eq!(0.25f32.double_minus_one(), 0.0);
}
#[test]
fn double_minus_one_does_not_saturate_the_intermediate() {
for v in 128..=255u8 {
let naive = Channel::saturating_sub(Channel::saturating_add(v, v), 255u8);
assert_eq!(
naive, 0,
"the doubled intermediate always clamps to ONE first"
);
assert_eq!(
v.double_minus_one(),
(2 * u16::from(v) - 255) as u8,
"v={v}"
);
}
}
#[test]
fn u8_div_normalized_inverts_scale() {
for a in (0..=255u8).step_by(7) {
for b in (1..=255u8).step_by(7) {
let round_trip = a.scale(b).div_normalized(b);
let expected = a;
let tolerance = 255 / u16::from(b);
assert!(
u16::from(round_trip).abs_diff(u16::from(expected)) <= tolerance + 1,
"{a} scaled by {b} then divided back gave {round_trip}",
);
}
}
}
#[test]
fn u8_sqrt_normalized_matches_float() {
for v in 0..=255u8 {
let expected = math::round(math::sqrt(f32::from(v) / 255.0) * 255.0) as u8;
assert_eq!(v.sqrt_normalized(), expected, "sqrt({v})");
}
}
#[test]
fn u8_sqrt_normalized_endpoints() {
assert_eq!(0u8.sqrt_normalized(), 0);
assert_eq!(255u8.sqrt_normalized(), 255);
assert_eq!(64u8.sqrt_normalized(), 128, "sqrt(0.25) == 0.5");
}
#[test]
fn u8_soft_light_d_matches_float_reference() {
for v in 0..=255u8 {
let x = f32::from(v) / 255.0;
let expected = if x <= 0.25 {
((16.0 * x - 12.0) * x + 4.0) * x
} else {
math::sqrt(x)
};
let expected = math::round(expected * 255.0) as u8;
assert!(
v.soft_light_d().abs_diff(expected) <= 1,
"D({v}) = {}, expected {expected}",
v.soft_light_d(),
);
}
}
#[test]
fn u8_soft_light_d_is_continuous_at_the_boundary() {
assert_eq!(63u8.soft_light_d(), 127);
assert_eq!(64u8.soft_light_d(), 128);
}
#[test]
fn u8_soft_light_d_never_darkens() {
for v in 0..=255u8 {
assert!(v.soft_light_d() >= v, "D({v}) must be >= {v}");
}
}
#[test]
fn u8_complement_and_saturation() {
assert_eq!(0u8.complement(), 255);
assert_eq!(255u8.complement(), 0);
assert_eq!(200u8.saturating_add(100), 255);
assert_eq!(50u8.saturating_sub(100), 0);
assert_eq!(50u8.abs_diff(100), 50);
assert_eq!(100u8.abs_diff(50), 50);
}
#[test]
fn u8_f32_round_trip() {
for v in 0..=255u8 {
assert_eq!(u8::from_f32(v.to_f32()), v);
}
assert_eq!(u8::from_f32(-1.0), 0, "clamps below");
assert_eq!(u8::from_f32(2.0), 255, "clamps above");
}
#[test]
fn u8_half_straddles_one_half() {
assert!(f32::from(<u8 as Channel>::HALF) / 255.0 <= 0.5);
assert!(f32::from(<u8 as Channel>::HALF + 1) / 255.0 > 0.5);
}
#[test]
fn f32_basics() {
assert_eq!(0.5f32.scale(0.5), 0.25);
assert_eq!(<f32 as Channel>::sum_of_products(0.5, 0.5, 0.25, 1.0), 0.5);
assert_eq!(
<f32 as Channel>::sum_of_products(1.0, 1.0, 1.0, 1.0),
1.0,
"saturates",
);
assert_eq!(0.25f32.div_normalized(0.5), 0.5);
assert_eq!(0.5f32.div_normalized(0.0), 1.0);
assert_eq!(0.25f32.sqrt_normalized(), 0.5);
assert_eq!(0.25f32.complement(), 0.75);
assert_eq!(0.75f32.saturating_add(0.75), 1.0);
assert_eq!(0.25f32.saturating_sub(0.75), 0.0);
assert_eq!(0.25f32.abs_diff(0.75), 0.5);
assert_eq!(Channel::min_channel(0.25f32, 0.75), 0.25);
assert_eq!(Channel::max_channel(0.25f32, 0.75), 0.75);
assert!(0.0f32.is_zero());
assert!(1.0f32.is_one());
}
#[test]
fn f32_conversion_is_the_identity_but_clamps() {
assert_eq!(0.25f32.to_f32(), 0.25);
assert_eq!(f32::from_f32(0.25), 0.25);
assert_eq!(f32::from_f32(-1.0), 0.0);
assert_eq!(f32::from_f32(2.0), 1.0);
}
#[test]
fn f32_lerp_endpoints_are_exact() {
assert_eq!(0.25f32.lerp(0.75, 0.0), 0.25);
assert_eq!(0.25f32.lerp(0.75, 1.0), 0.75);
assert_eq!(0.0f32.lerp(1.0, 0.5), 0.5);
}
#[test]
fn f32_soft_light_d() {
assert_eq!(0.25f32.soft_light_d(), 0.5, "polynomial meets sqrt at 0.25");
assert_eq!(1.0f32.soft_light_d(), 1.0);
assert_eq!(0.0f32.soft_light_d(), 0.0);
}
#[test]
fn u8_and_f32_agree_within_storage_precision() {
for a in (0..=255u8).step_by(3) {
for b in (0..=255u8).step_by(3) {
let int = a.scale(b).to_f32();
let flt = a.to_f32().scale(b.to_f32());
assert!(
(int - flt).abs() <= 0.5 / 255.0,
"{a} * {b}: {int} vs {flt}"
);
}
}
}
}