use super::sharpyuv::SharpYuvConversionMatrix;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SharpYuvRange(pub u32);
impl SharpYuvRange {
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvRangeFull: Self = Self(0);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvRangeLimited: Self = Self(1);
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SharpYuvColorSpace {
pub kr: f32,
pub kb: f32,
pub bit_depth: i32,
pub range: SharpYuvRange,
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SharpYuvMatrixType(pub u32);
impl SharpYuvMatrixType {
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixWebp: Self = Self(0);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixRec601Limited: Self = Self(1);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixRec601Full: Self = Self(2);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixRec709Limited: Self = Self(3);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixRec709Full: Self = Self(4);
#[expect(non_upper_case_globals, reason = "C enumerator name")]
pub const kSharpYuvMatrixNum: Self = Self(5);
}
#[expect(
clippy::cast_possible_truncation,
reason = "C integer and float conversions, kept as in the source (ADR 0003)"
)]
fn to_fixed16(f: f32) -> i32 {
(f64::from(f * 65536.0 + 0.5)).floor() as i32
}
#[must_use]
#[expect(
clippy::cast_precision_loss,
reason = "C int to float conversions of the scale factors"
)]
pub fn sharp_yuv_compute_conversion_matrix(
yuv_color_space: &SharpYuvColorSpace,
) -> SharpYuvConversionMatrix {
let kr = yuv_color_space.kr;
let kb = yuv_color_space.kb;
let kg = 1.0_f32 - kr - kb;
let cb = 0.5_f32 / (1.0_f32 - kb);
let cr = 0.5_f32 / (1.0_f32 - kr);
let shift = yuv_color_space.bit_depth - 8;
debug_assert!(yuv_color_space.bit_depth >= 8);
let denom = ((1 << yuv_color_space.bit_depth) - 1) as f32;
let mut scale_y = 1.0_f32;
let mut add_y = 0.0_f32;
let mut scale_u = cb;
let mut scale_v = cr;
let add_uv = (128 << shift) as f32;
if yuv_color_space.range == SharpYuvRange::kSharpYuvRangeLimited {
scale_y *= (219 << shift) as f32 / denom;
scale_u *= (224 << shift) as f32 / denom;
scale_v *= (224 << shift) as f32 / denom;
add_y = (16 << shift) as f32;
}
SharpYuvConversionMatrix {
rgb_to_y: [
to_fixed16(kr * scale_y),
to_fixed16(kg * scale_y),
to_fixed16(kb * scale_y),
to_fixed16(add_y),
],
rgb_to_u: [
to_fixed16(-kr * scale_u),
to_fixed16(-kg * scale_u),
to_fixed16((1.0 - kb) * scale_u),
to_fixed16(add_uv),
],
rgb_to_v: [
to_fixed16((1.0 - kr) * scale_v),
to_fixed16(-kg * scale_v),
to_fixed16(-kb * scale_v),
to_fixed16(add_uv),
],
}
}
const WEBP_MATRIX: SharpYuvConversionMatrix = SharpYuvConversionMatrix {
rgb_to_y: [16839, 33059, 6420, 16 << 16],
rgb_to_u: [-9719, -19081, 28800, 128 << 16],
rgb_to_v: [28800, -24116, -4684, 128 << 16],
};
const REC601_LIMITED_MATRIX: SharpYuvConversionMatrix = SharpYuvConversionMatrix {
rgb_to_y: [16829, 33039, 6416, 16 << 16],
rgb_to_u: [-9714, -19071, 28784, 128 << 16],
rgb_to_v: [28784, -24103, -4681, 128 << 16],
};
const REC601_FULL_MATRIX: SharpYuvConversionMatrix = SharpYuvConversionMatrix {
rgb_to_y: [19595, 38470, 7471, 0],
rgb_to_u: [-11058, -21710, 32768, 128 << 16],
rgb_to_v: [32768, -27439, -5329, 128 << 16],
};
const REC709_LIMITED_MATRIX: SharpYuvConversionMatrix = SharpYuvConversionMatrix {
rgb_to_y: [11966, 40254, 4064, 16 << 16],
rgb_to_u: [-6596, -22189, 28784, 128 << 16],
rgb_to_v: [28784, -26145, -2639, 128 << 16],
};
const REC709_FULL_MATRIX: SharpYuvConversionMatrix = SharpYuvConversionMatrix {
rgb_to_y: [13933, 46871, 4732, 0],
rgb_to_u: [-7509, -25259, 32768, 128 << 16],
rgb_to_v: [32768, -29763, -3005, 128 << 16],
};
#[must_use]
pub fn sharp_yuv_get_conversion_matrix(
matrix_type: SharpYuvMatrixType,
) -> Option<&'static SharpYuvConversionMatrix> {
match matrix_type {
SharpYuvMatrixType::kSharpYuvMatrixWebp => Some(&WEBP_MATRIX),
SharpYuvMatrixType::kSharpYuvMatrixRec601Limited => Some(&REC601_LIMITED_MATRIX),
SharpYuvMatrixType::kSharpYuvMatrixRec601Full => Some(&REC601_FULL_MATRIX),
SharpYuvMatrixType::kSharpYuvMatrixRec709Limited => Some(&REC709_LIMITED_MATRIX),
SharpYuvMatrixType::kSharpYuvMatrixRec709Full => Some(&REC709_FULL_MATRIX),
_ => None,
}
}