use crate::webp::decode::WEBP_CSP_MODE;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "C stores an int in a uint8_t, which keeps the low eight bits"
)]
#[must_use]
pub fn store_u8(value: i32) -> u8 {
value as u8
}
pub const YUV_FIX: i32 = 16;
pub const YUV_HALF: i32 = 1 << (YUV_FIX - 1);
pub const YUV_FIX2: i32 = 6;
pub const YUV_MASK2: i32 = (256 << YUV_FIX2) - 1;
#[must_use]
pub fn mult_hi(v: i32, coeff: i32) -> i32 {
(v * coeff) >> 8
}
#[must_use]
pub fn vp8_clip8(v: i32) -> i32 {
if (v & !YUV_MASK2) == 0 {
v >> YUV_FIX2
} else if v < 0 {
0
} else {
255
}
}
#[must_use]
pub fn vp8yuv_to_r(y: i32, v: i32) -> i32 {
vp8_clip8(mult_hi(y, 19077) + mult_hi(v, 26149) - 14234)
}
#[must_use]
pub fn vp8yuv_to_g(y: i32, u: i32, v: i32) -> i32 {
vp8_clip8(mult_hi(y, 19077) - mult_hi(u, 6419) - mult_hi(v, 13320) + 8708)
}
#[must_use]
pub fn vp8yuv_to_b(y: i32, u: i32) -> i32 {
vp8_clip8(mult_hi(y, 19077) + mult_hi(u, 33050) - 17685)
}
pub fn vp8_yuv_to_rgb(y: i32, u: i32, v: i32, rgb: &mut [u8]) {
rgb[0] = store_u8(vp8yuv_to_r(y, v));
rgb[1] = store_u8(vp8yuv_to_g(y, u, v));
rgb[2] = store_u8(vp8yuv_to_b(y, u));
}
pub fn vp8_yuv_to_bgr(y: i32, u: i32, v: i32, bgr: &mut [u8]) {
bgr[0] = store_u8(vp8yuv_to_b(y, u));
bgr[1] = store_u8(vp8yuv_to_g(y, u, v));
bgr[2] = store_u8(vp8yuv_to_r(y, v));
}
#[expect(
clippy::many_single_char_names,
reason = "the YUV and RGB component names of the C source"
)]
pub fn vp8_yuv_to_rgb565(y: i32, u: i32, v: i32, rgb: &mut [u8]) {
let r = vp8yuv_to_r(y, v); let g = vp8yuv_to_g(y, u, v); let b = vp8yuv_to_b(y, u); let rg = (r & 0xf8) | (g >> 5);
let gb = ((g << 3) & 0xe0) | (b >> 3);
rgb[0] = store_u8(rg);
rgb[1] = store_u8(gb);
}
#[expect(
clippy::many_single_char_names,
reason = "the YUV and RGB component names of the C source"
)]
pub fn vp8_yuv_to_rgba4444(y: i32, u: i32, v: i32, argb: &mut [u8]) {
let r = vp8yuv_to_r(y, v); let g = vp8yuv_to_g(y, u, v); let b = vp8yuv_to_b(y, u); let rg = (r & 0xf0) | (g >> 4);
let ba = (b & 0xf0) | 0x0f; argb[0] = store_u8(rg);
argb[1] = store_u8(ba);
}
pub fn vp8_yuv_to_argb(y: u8, u: u8, v: u8, argb: &mut [u8]) {
argb[0] = 0xff;
vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), &mut argb[1..]);
}
pub fn vp8_yuv_to_bgra(y: u8, u: u8, v: u8, bgra: &mut [u8]) {
vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), bgra);
bgra[3] = 0xff;
}
pub fn vp8_yuv_to_rgba(y: u8, u: u8, v: u8, rgba: &mut [u8]) {
vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), rgba);
rgba[3] = 0xff;
}
#[must_use]
pub fn vp8_clip_uv(uv: i32, rounding: i32) -> i32 {
let uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2);
if (uv & !0xff) == 0 {
uv
} else if uv < 0 {
0
} else {
255
}
}
#[must_use]
pub fn vp8rgb_to_y(r: i32, g: i32, b: i32, rounding: i32) -> i32 {
let luma = 16839 * r + 33059 * g + 6420 * b;
(luma + rounding + (16 << YUV_FIX)) >> YUV_FIX }
#[must_use]
pub fn vp8rgb_to_u(r: i32, g: i32, b: i32, rounding: i32) -> i32 {
let u = -9719 * r - 19081 * g + 28800 * b;
vp8_clip_uv(u, rounding)
}
#[must_use]
pub fn vp8rgb_to_v(r: i32, g: i32, b: i32, rounding: i32) -> i32 {
let v = 28800 * r - 24116 * g - 4684 * b;
vp8_clip_uv(v, rounding)
}
fn row_func(
y: &[u8],
u: &[u8],
v: &[u8],
dst: &mut [u8],
len: i32,
xstep: usize,
func: impl Fn(u8, u8, u8, &mut [u8]),
) {
let len = usize::try_from(len).expect("the row length is not negative");
let mut y_pos = 0;
let mut uv_pos = 0;
let mut dst_pos = 0;
for _ in 0..len / 2 {
func(y[y_pos], u[uv_pos], v[uv_pos], &mut dst[dst_pos..]);
func(
y[y_pos + 1],
u[uv_pos],
v[uv_pos],
&mut dst[dst_pos + xstep..],
);
y_pos += 2;
uv_pos += 1;
dst_pos += 2 * xstep;
}
if len & 1 != 0 {
func(y[y_pos], u[uv_pos], v[uv_pos], &mut dst[dst_pos..]);
}
}
fn yuv_to_rgb_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 3, |y, u, v, out| {
vp8_yuv_to_rgb(i32::from(y), i32::from(u), i32::from(v), out);
});
}
fn yuv_to_bgr_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 3, |y, u, v, out| {
vp8_yuv_to_bgr(i32::from(y), i32::from(u), i32::from(v), out);
});
}
fn yuv_to_rgba_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 4, vp8_yuv_to_rgba);
}
fn yuv_to_bgra_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 4, vp8_yuv_to_bgra);
}
fn yuv_to_argb_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 4, vp8_yuv_to_argb);
}
fn yuv_to_rgba4444_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 2, |y, u, v, out| {
vp8_yuv_to_rgba4444(i32::from(y), i32::from(u), i32::from(v), out);
});
}
fn yuv_to_rgb565_row(y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
row_func(y, u, v, dst, len, 2, |y, u, v, out| {
vp8_yuv_to_rgb565(i32::from(y), i32::from(u), i32::from(v), out);
});
}
pub type WebPSamplerRowFunc = fn(&[u8], &[u8], &[u8], &mut [u8], i32);
#[expect(
clippy::too_many_arguments,
reason = "mirrors the parameters of WebPSamplerProcessPlane"
)]
pub fn webp_sampler_process_plane(
y: &[u8],
y_stride: i32,
u: &[u8],
v: &[u8],
uv_stride: i32,
dst: &mut [u8],
dst_stride: i32,
width: i32,
height: i32,
func: impl Fn(&[u8], &[u8], &[u8], &mut [u8], i32),
) {
let row = |offset: isize| usize::try_from(offset).expect("a row starts inside the buffer");
let mut y_offset = 0_isize;
let mut uv_offset = 0_isize;
let mut dst_offset = 0_isize;
for j in 0..height {
func(
&y[row(y_offset)..],
&u[row(uv_offset)..],
&v[row(uv_offset)..],
&mut dst[row(dst_offset)..],
width,
);
y_offset += isize::try_from(y_stride).expect("the stride fits in isize");
if j & 1 != 0 {
uv_offset += isize::try_from(uv_stride).expect("the stride fits in isize");
}
dst_offset += isize::try_from(dst_stride).expect("the stride fits in isize");
}
}
static WEBP_SAMPLERS: [Option<WebPSamplerRowFunc>; 13] = [
Some(yuv_to_rgb_row), Some(yuv_to_rgba_row), Some(yuv_to_bgr_row), Some(yuv_to_bgra_row), Some(yuv_to_argb_row), Some(yuv_to_rgba4444_row), Some(yuv_to_rgb565_row), Some(yuv_to_rgba_row), Some(yuv_to_bgra_row), Some(yuv_to_argb_row), Some(yuv_to_rgba4444_row), None, None, ];
pub fn webp_samplers(mode: WEBP_CSP_MODE, y: &[u8], u: &[u8], v: &[u8], dst: &mut [u8], len: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
match mode {
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::yuv_sse41::yuv_to_rgb_row(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::yuv_sse41::yuv_to_bgr_row(y, u, v, dst, len);
}
_ => {}
}
}
if features.sse2 {
match mode {
WEBP_CSP_MODE::MODE_RGB => {
return crate::dsp::yuv_sse2::yuv_to_rgb_row(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_RGBA => {
return crate::dsp::yuv_sse2::yuv_to_rgba_row(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGR => {
return crate::dsp::yuv_sse2::yuv_to_bgr_row(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_BGRA => {
return crate::dsp::yuv_sse2::yuv_to_bgra_row(y, u, v, dst, len);
}
WEBP_CSP_MODE::MODE_ARGB => {
return crate::dsp::yuv_sse2::yuv_to_argb_row(y, u, v, dst, len);
}
_ => {}
}
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
let sampler = WEBP_SAMPLERS[mode.0 as usize].expect("YUV modes have no row sampler");
sampler(y, u, v, dst, len);
}
#[expect(
clippy::cast_possible_wrap,
reason = "the masked channels of an ARGB value are below 2^31"
)]
pub fn convert_argb_to_y_c(argb: &[u32], y: &mut [u8], width: i32) {
let width = usize::try_from(width).expect("the width is not negative");
for i in 0..width {
let p = argb[i];
y[i] = store_u8(vp8rgb_to_y(
((p >> 16) & 0xff) as i32,
((p >> 8) & 0xff) as i32,
(p & 0xff) as i32,
YUV_HALF,
));
}
}
#[expect(
clippy::cast_possible_wrap,
reason = "the masked channels of an ARGB value are below 2^31"
)]
#[expect(
clippy::many_single_char_names,
reason = "the YUV and RGB component names of the C source"
)]
pub fn webp_convert_argb_to_uv_c(
argb: &[u32],
u: &mut [u8],
v: &mut [u8],
src_width: i32,
do_store: i32,
) {
let uv_width = usize::try_from(src_width >> 1).expect("the width is not negative");
let mut i = 0_usize;
while i < uv_width {
let v0 = argb[2 * i];
let v1 = argb[2 * i + 1];
let r = (((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe)) as i32;
let g = (((v0 >> 7) & 0x1fe) + ((v1 >> 7) & 0x1fe)) as i32;
let b = (((v0 << 1) & 0x1fe) + ((v1 << 1) & 0x1fe)) as i32;
let tmp_u = vp8rgb_to_u(r, g, b, YUV_HALF << 2);
let tmp_v = vp8rgb_to_v(r, g, b, YUV_HALF << 2);
if do_store != 0 {
u[i] = store_u8(tmp_u);
v[i] = store_u8(tmp_v);
} else {
u[i] = store_u8((i32::from(u[i]) + tmp_u + 1) >> 1);
v[i] = store_u8((i32::from(v[i]) + tmp_v + 1) >> 1);
}
i += 1;
}
if src_width & 1 != 0 {
let v0 = argb[2 * i];
let r = ((v0 >> 14) & 0x3fc) as i32;
let g = ((v0 >> 6) & 0x3fc) as i32;
let b = ((v0 << 2) & 0x3fc) as i32;
let tmp_u = vp8rgb_to_u(r, g, b, YUV_HALF << 2);
let tmp_v = vp8rgb_to_v(r, g, b, YUV_HALF << 2);
if do_store != 0 {
u[i] = store_u8(tmp_u);
v[i] = store_u8(tmp_v);
} else {
u[i] = store_u8((i32::from(u[i]) + tmp_u + 1) >> 1);
v[i] = store_u8((i32::from(v[i]) + tmp_v + 1) >> 1);
}
}
}
pub fn convert_rgb24_to_y_c(rgb: &[u8], y: &mut [u8], width: i32) {
let width = usize::try_from(width).expect("the width is not negative");
for i in 0..width {
let px = &rgb[3 * i..3 * i + 3];
y[i] = store_u8(vp8rgb_to_y(
i32::from(px[0]),
i32::from(px[1]),
i32::from(px[2]),
YUV_HALF,
));
}
}
pub fn convert_bgr24_to_y_c(bgr: &[u8], y: &mut [u8], width: i32) {
let width = usize::try_from(width).expect("the width is not negative");
for i in 0..width {
let px = &bgr[3 * i..3 * i + 3];
y[i] = store_u8(vp8rgb_to_y(
i32::from(px[2]),
i32::from(px[1]),
i32::from(px[0]),
YUV_HALF,
));
}
}
#[expect(
clippy::many_single_char_names,
reason = "the YUV and RGB component names of the C source"
)]
pub fn webp_convert_rgba32_to_uv_c(rgb: &[u16], u: &mut [u8], v: &mut [u8], width: i32) {
let width = usize::try_from(width).expect("the width is not negative");
for i in 0..width {
let r = i32::from(rgb[4 * i]);
let g = i32::from(rgb[4 * i + 1]);
let b = i32::from(rgb[4 * i + 2]);
u[i] = store_u8(vp8rgb_to_u(r, g, b, YUV_HALF << 2));
v[i] = store_u8(vp8rgb_to_v(r, g, b, YUV_HALF << 2));
}
}
pub fn webp_convert_argb_to_y(argb: &[u32], y: &mut [u8], width: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
return crate::dsp::yuv_sse41::convert_argb_to_y(argb, y, width);
}
if features.sse2 {
return crate::dsp::yuv_sse2::convert_argb_to_y(argb, y, width);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::yuv_neon::convert_argb_to_y(argb, y, width);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
convert_argb_to_y_c(argb, y, width);
}
pub fn webp_convert_argb_to_uv(
argb: &[u32],
u: &mut [u8],
v: &mut [u8],
src_width: i32,
do_store: i32,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
return crate::dsp::yuv_sse41::convert_argb_to_uv(argb, u, v, src_width, do_store);
}
if features.sse2 {
return crate::dsp::yuv_sse2::convert_argb_to_uv(argb, u, v, src_width, do_store);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::yuv_neon::convert_argb_to_uv(argb, u, v, src_width, do_store);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
webp_convert_argb_to_uv_c(argb, u, v, src_width, do_store);
}
pub fn webp_convert_rgb24_to_y(rgb: &[u8], y: &mut [u8], width: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
return crate::dsp::yuv_sse41::convert_rgb24_to_y(rgb, y, width);
}
if features.sse2 {
return crate::dsp::yuv_sse2::convert_rgb24_to_y(rgb, y, width);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::yuv_neon::convert_rgb24_to_y(rgb, y, width);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
convert_rgb24_to_y_c(rgb, y, width);
}
pub fn webp_convert_bgr24_to_y(bgr: &[u8], y: &mut [u8], width: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
return crate::dsp::yuv_sse41::convert_bgr24_to_y(bgr, y, width);
}
if features.sse2 {
return crate::dsp::yuv_sse2::convert_bgr24_to_y(bgr, y, width);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::yuv_neon::convert_bgr24_to_y(bgr, y, width);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
convert_bgr24_to_y_c(bgr, y, width);
}
pub fn webp_convert_rgba32_to_uv(rgb: &[u16], u: &mut [u8], v: &mut [u8], width: i32) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse4_1 {
return crate::dsp::yuv_sse41::convert_rgba32_to_uv(rgb, u, v, width);
}
if features.sse2 {
return crate::dsp::yuv_sse2::convert_rgba32_to_uv(rgb, u, v, width);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::yuv_neon::convert_rgba32_to_uv(rgb, u, v, width);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
webp_convert_rgba32_to_uv_c(rgb, u, v, width);
}