#[inline(always)]
fn load_f16<const BE: bool>(raw: half::f16) -> f32 {
let bits = raw.to_bits();
half::f16::from_bits(if BE {
u16::from_be(bits)
} else {
u16::from_le(bits)
})
.to_f32()
}
#[inline(always)]
fn f32_to_u8(v: f32) -> u8 {
(v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}
#[inline(always)]
fn f32_to_u16(v: f32) -> u16 {
(v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_rgb_row<const BE: bool>(
packed: &[half::f16],
rgb_out: &mut [u8],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(rgb_out.len() >= width * 3, "rgb_out too short");
for x in 0..width {
let v = f32_to_u8(load_f16::<BE>(packed[x * 2]));
let i = x * 3;
rgb_out[i] = v;
rgb_out[i + 1] = v;
rgb_out[i + 2] = v;
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_rgba_row<const BE: bool>(
packed: &[half::f16],
rgba_out: &mut [u8],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(rgba_out.len() >= width * 4, "rgba_out too short");
for x in 0..width {
let y = f32_to_u8(load_f16::<BE>(packed[x * 2]));
let a = f32_to_u8(load_f16::<BE>(packed[x * 2 + 1]));
let i = x * 4;
rgba_out[i] = y;
rgba_out[i + 1] = y;
rgba_out[i + 2] = y;
rgba_out[i + 3] = a;
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_rgb_u16_row<const BE: bool>(
packed: &[half::f16],
rgb_u16_out: &mut [u16],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(rgb_u16_out.len() >= width * 3, "rgb_u16_out too short");
for x in 0..width {
let v = f32_to_u16(load_f16::<BE>(packed[x * 2]));
let i = x * 3;
rgb_u16_out[i] = v;
rgb_u16_out[i + 1] = v;
rgb_u16_out[i + 2] = v;
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_rgba_u16_row<const BE: bool>(
packed: &[half::f16],
rgba_u16_out: &mut [u16],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(rgba_u16_out.len() >= width * 4, "rgba_u16_out too short");
for x in 0..width {
let y = f32_to_u16(load_f16::<BE>(packed[x * 2]));
let a = f32_to_u16(load_f16::<BE>(packed[x * 2 + 1]));
let i = x * 4;
rgba_u16_out[i] = y;
rgba_u16_out[i + 1] = y;
rgba_u16_out[i + 2] = y;
rgba_u16_out[i + 3] = a;
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_rgb_f32_row<const BE: bool>(
packed: &[half::f16],
rgb_f32_out: &mut [f32],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(rgb_f32_out.len() >= width * 3, "rgb_f32_out too short");
for x in 0..width {
let y = load_f16::<BE>(packed[x * 2]);
let i = x * 3;
rgb_f32_out[i] = y;
rgb_f32_out[i + 1] = y;
rgb_f32_out[i + 2] = y;
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_luma_row<const BE: bool>(
packed: &[half::f16],
luma_out: &mut [u8],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(luma_out.len() >= width, "luma_out too short");
for x in 0..width {
luma_out[x] = f32_to_u8(load_f16::<BE>(packed[x * 2]));
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_luma_u16_row<const BE: bool>(
packed: &[half::f16],
luma_u16_out: &mut [u16],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(luma_u16_out.len() >= width, "luma_u16_out too short");
for x in 0..width {
luma_u16_out[x] = f32_to_u16(load_f16::<BE>(packed[x * 2]));
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_luma_f32_row<const BE: bool>(
packed: &[half::f16],
luma_f32_out: &mut [f32],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(luma_f32_out.len() >= width, "luma_f32_out too short");
for x in 0..width {
luma_f32_out[x] = load_f16::<BE>(packed[x * 2]);
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn yaf16_to_hsv_row<const BE: bool>(
packed: &[half::f16],
h_out: &mut [u8],
s_out: &mut [u8],
v_out: &mut [u8],
width: usize,
) {
debug_assert!(packed.len() >= width * 2, "packed too short");
debug_assert!(h_out.len() >= width, "h_out too short");
debug_assert!(s_out.len() >= width, "s_out too short");
debug_assert!(v_out.len() >= width, "v_out too short");
for x in 0..width {
h_out[x] = 0;
s_out[x] = 0;
v_out[x] = f32_to_u8(load_f16::<BE>(packed[x * 2]));
}
}
#[cfg(all(test, feature = "std"))]
mod tests;