use crate::dec::vp8li_dec::VP8LTransform;
use crate::dsp::alpha_processing::{webp_apply_alpha_multiply, webp_apply_alpha_multiply4444};
use crate::dsp::lossless_common::{
generate_predictor_add, vp8_get_alpha_index, vp8_get_alpha_value, vp8_get_argb_index,
vp8_get_argb_value, vp8l_add_pixels, vp8l_sub_sample_size,
};
use crate::utils::endian_inl_utils::b_swap32;
use crate::utils::utils::webp_uint32_to_mem;
use crate::webp::decode::WEBP_CSP_MODE;
use crate::webp::format_constants::{ARGB_BLACK, VP8LImageTransformType};
pub type VP8LPredictorFunc = fn(&[u32], usize, &[u32], usize) -> u32;
pub type VP8LPredictorAddSubFunc = fn(&mut [u32], usize, usize, i32, usize);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct VP8LMultipliers {
pub green_to_red: u8,
pub green_to_blue: u8,
pub red_to_blue: u8,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct VP8LStreaks {
pub counts: [i32; 2],
pub streaks: [[i32; 2]; 2],
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct VP8LBitEntropy {
pub entropy: u64,
pub sum: u32,
pub nonzeros: i32,
pub max_val: u32,
pub nonzero_code: u32,
}
#[inline]
fn pixel_count(num_pixels: i32) -> usize {
usize::try_from(num_pixels).unwrap_or(0)
}
#[inline]
fn position(value: i32) -> usize {
usize::try_from(value).expect("a transform position is not negative")
}
#[must_use]
#[inline]
fn average2(a0: u32, a1: u32) -> u32 {
(((a0 ^ a1) & 0xfefe_fefe) >> 1) + (a0 & a1)
}
#[must_use]
#[inline]
fn average3(a0: u32, a1: u32, a2: u32) -> u32 {
average2(average2(a0, a2), a1)
}
#[must_use]
#[inline]
fn average4(a0: u32, a1: u32, a2: u32, a3: u32) -> u32 {
average2(average2(a0, a1), average2(a2, a3))
}
#[must_use]
#[inline]
fn clip255(a: u32) -> u32 {
if a < 256 {
return a;
}
!a >> 24
}
#[must_use]
#[inline]
fn add_subtract_component_full(a: i32, b: i32, c: i32) -> i32 {
clip255((a + b - c).cast_unsigned()).cast_signed()
}
#[inline]
fn channel(value: u32) -> i32 {
value.cast_signed()
}
#[must_use]
#[inline]
fn clamped_add_subtract_full(c0: u32, c1: u32, c2: u32) -> u32 {
let a = add_subtract_component_full(channel(c0 >> 24), channel(c1 >> 24), channel(c2 >> 24));
let r = add_subtract_component_full(
channel((c0 >> 16) & 0xff),
channel((c1 >> 16) & 0xff),
channel((c2 >> 16) & 0xff),
);
let g = add_subtract_component_full(
channel((c0 >> 8) & 0xff),
channel((c1 >> 8) & 0xff),
channel((c2 >> 8) & 0xff),
);
let b = add_subtract_component_full(channel(c0 & 0xff), channel(c1 & 0xff), channel(c2 & 0xff));
(a.cast_unsigned() << 24) | ((r << 16) | (g << 8) | b).cast_unsigned()
}
#[must_use]
#[inline]
fn add_subtract_component_half(a: i32, b: i32) -> i32 {
clip255((a + (a - b) / 2).cast_unsigned()).cast_signed()
}
#[must_use]
#[inline]
fn clamped_add_subtract_half(c0: u32, c1: u32, c2: u32) -> u32 {
let ave = average2(c0, c1);
let a = add_subtract_component_half(channel(ave >> 24), channel(c2 >> 24));
let r = add_subtract_component_half(channel((ave >> 16) & 0xff), channel((c2 >> 16) & 0xff));
let g = add_subtract_component_half(channel((ave >> 8) & 0xff), channel((c2 >> 8) & 0xff));
let b = add_subtract_component_half(channel(ave & 0xff), channel(c2 & 0xff));
(a.cast_unsigned() << 24) | ((r << 16) | (g << 8) | b).cast_unsigned()
}
#[must_use]
#[inline]
fn sub3(a: i32, b: i32, c: i32) -> i32 {
let pb = b - c;
let pa = a - c;
pb.abs() - pa.abs()
}
#[must_use]
#[inline]
fn select(a: u32, b: u32, c: u32) -> u32 {
let pa_minus_pb = sub3(channel(a >> 24), channel(b >> 24), channel(c >> 24))
+ sub3(
channel((a >> 16) & 0xff),
channel((b >> 16) & 0xff),
channel((c >> 16) & 0xff),
)
+ sub3(
channel((a >> 8) & 0xff),
channel((b >> 8) & 0xff),
channel((c >> 8) & 0xff),
)
+ sub3(channel(a & 0xff), channel(b & 0xff), channel(c & 0xff));
if pa_minus_pb <= 0 { a } else { b }
}
fn vp8l_predictor0_c(_left: &[u32], _left_pos: usize, _top: &[u32], _top_pos: usize) -> u32 {
ARGB_BLACK
}
fn vp8l_predictor1_c(left: &[u32], left_pos: usize, _top: &[u32], _top_pos: usize) -> u32 {
left[left_pos]
}
#[must_use]
pub fn vp8l_predictor2_c(_left: &[u32], _left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
top[top_pos]
}
#[must_use]
pub fn vp8l_predictor3_c(_left: &[u32], _left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
top[top_pos + 1]
}
#[must_use]
pub fn vp8l_predictor4_c(_left: &[u32], _left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
top[top_pos - 1]
}
#[must_use]
pub fn vp8l_predictor5_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average3(left[left_pos], top[top_pos], top[top_pos + 1])
}
#[must_use]
pub fn vp8l_predictor6_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average2(left[left_pos], top[top_pos - 1])
}
#[must_use]
pub fn vp8l_predictor7_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average2(left[left_pos], top[top_pos])
}
#[must_use]
pub fn vp8l_predictor8_c(_left: &[u32], _left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average2(top[top_pos - 1], top[top_pos])
}
#[must_use]
pub fn vp8l_predictor9_c(_left: &[u32], _left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average2(top[top_pos], top[top_pos + 1])
}
#[must_use]
pub fn vp8l_predictor10_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
average4(
left[left_pos],
top[top_pos - 1],
top[top_pos],
top[top_pos + 1],
)
}
#[must_use]
pub fn vp8l_predictor11_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
select(top[top_pos], left[left_pos], top[top_pos - 1])
}
#[must_use]
pub fn vp8l_predictor12_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
clamped_add_subtract_full(left[left_pos], top[top_pos], top[top_pos - 1])
}
#[must_use]
pub fn vp8l_predictor13_c(left: &[u32], left_pos: usize, top: &[u32], top_pos: usize) -> u32 {
clamped_add_subtract_half(left[left_pos], top[top_pos], top[top_pos - 1])
}
fn predictor_add0_c(
data: &mut [u32],
in_pos: usize,
_upper_pos: usize,
num_pixels: i32,
out_pos: usize,
) {
for x in 0..pixel_count(num_pixels) {
data[out_pos + x] = vp8l_add_pixels(data[in_pos + x], ARGB_BLACK);
}
}
fn predictor_add1_c(
data: &mut [u32],
in_pos: usize,
_upper_pos: usize,
num_pixels: i32,
out_pos: usize,
) {
let mut left = data[out_pos - 1];
for i in 0..pixel_count(num_pixels) {
left = vp8l_add_pixels(data[in_pos + i], left);
data[out_pos + i] = left;
}
}
generate_predictor_add!(vp8l_predictor2_c, predictor_add2_c);
generate_predictor_add!(vp8l_predictor3_c, predictor_add3_c);
generate_predictor_add!(vp8l_predictor4_c, predictor_add4_c);
generate_predictor_add!(vp8l_predictor5_c, predictor_add5_c);
generate_predictor_add!(vp8l_predictor6_c, predictor_add6_c);
generate_predictor_add!(vp8l_predictor7_c, predictor_add7_c);
generate_predictor_add!(vp8l_predictor8_c, predictor_add8_c);
generate_predictor_add!(vp8l_predictor9_c, predictor_add9_c);
generate_predictor_add!(vp8l_predictor10_c, predictor_add10_c);
generate_predictor_add!(vp8l_predictor11_c, predictor_add11_c);
generate_predictor_add!(vp8l_predictor12_c, predictor_add12_c);
generate_predictor_add!(vp8l_predictor13_c, predictor_add13_c);
static VP8L_PREDICTORS: [VP8LPredictorFunc; 16] = [
vp8l_predictor0_c,
vp8l_predictor1_c,
vp8l_predictor2_c,
vp8l_predictor3_c,
vp8l_predictor4_c,
vp8l_predictor5_c,
vp8l_predictor6_c,
vp8l_predictor7_c,
vp8l_predictor8_c,
vp8l_predictor9_c,
vp8l_predictor10_c,
vp8l_predictor11_c,
vp8l_predictor12_c,
vp8l_predictor13_c,
vp8l_predictor0_c, vp8l_predictor0_c,
];
static VP8L_PREDICTORS_ADD: [VP8LPredictorAddSubFunc; 16] = [
predictor_add0_c,
predictor_add1_c,
predictor_add2_c,
predictor_add3_c,
predictor_add4_c,
predictor_add5_c,
predictor_add6_c,
predictor_add7_c,
predictor_add8_c,
predictor_add9_c,
predictor_add10_c,
predictor_add11_c,
predictor_add12_c,
predictor_add13_c,
predictor_add0_c, predictor_add0_c,
];
static VP8L_PREDICTORS_ADD_C: [VP8LPredictorAddSubFunc; 16] = VP8L_PREDICTORS_ADD;
#[must_use]
#[inline]
pub fn vp8l_predictors(
mode: usize,
left: &[u32],
left_pos: usize,
top: &[u32],
top_pos: usize,
) -> u32 {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
match mode {
5 => return crate::dsp::lossless_sse2::predictor5(left, left_pos, top, top_pos),
6 => return crate::dsp::lossless_sse2::predictor6(left, left_pos, top, top_pos),
7 => return crate::dsp::lossless_sse2::predictor7(left, left_pos, top, top_pos),
8 => return crate::dsp::lossless_sse2::predictor8(left, left_pos, top, top_pos),
9 => return crate::dsp::lossless_sse2::predictor9(left, left_pos, top, top_pos),
10 => return crate::dsp::lossless_sse2::predictor10(left, left_pos, top, top_pos),
11 => return crate::dsp::lossless_sse2::predictor11(left, left_pos, top, top_pos),
12 => return crate::dsp::lossless_sse2::predictor12(left, left_pos, top, top_pos),
13 => return crate::dsp::lossless_sse2::predictor13(left, left_pos, top, top_pos),
_ => {}
}
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
let neon: Option<VP8LPredictorFunc> = match mode {
5 => Some(crate::dsp::lossless_neon::predictor5),
6 => Some(crate::dsp::lossless_neon::predictor6),
7 => Some(crate::dsp::lossless_neon::predictor7),
13 => Some(crate::dsp::lossless_neon::predictor13),
_ => None,
};
if let Some(predictor) = neon {
return predictor(left, left_pos, top, top_pos);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
VP8L_PREDICTORS[mode](left, left_pos, top, top_pos)
}
#[inline]
pub fn vp8l_predictors_add(
mode: usize,
data: &mut [u32],
in_pos: usize,
upper_pos: usize,
num_pixels: i32,
out_pos: usize,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
use crate::dsp::lossless_avx2 as avx2;
use crate::dsp::lossless_sse2 as sse2;
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 && features.avx2 {
let add: Option<VP8LPredictorAddSubFunc> = match mode {
0 => Some(avx2::predictor_add0),
1 => Some(avx2::predictor_add1),
2 => Some(avx2::predictor_add2),
3 => Some(avx2::predictor_add3),
4 => Some(avx2::predictor_add4),
8 => Some(avx2::predictor_add8),
9 => Some(avx2::predictor_add9),
10 => Some(avx2::predictor_add10),
11 => Some(avx2::predictor_add11),
12 => Some(avx2::predictor_add12),
_ => None,
};
if let Some(add) = add {
return add(data, in_pos, upper_pos, num_pixels, out_pos);
}
}
if features.sse2 {
let add: Option<VP8LPredictorAddSubFunc> = match mode {
0 => Some(sse2::predictor_add0),
1 => Some(sse2::predictor_add1),
2 => Some(sse2::predictor_add2),
3 => Some(sse2::predictor_add3),
4 => Some(sse2::predictor_add4),
5 => Some(sse2::predictor_add5),
6 => Some(sse2::predictor_add6),
7 => Some(sse2::predictor_add7),
8 => Some(sse2::predictor_add8),
9 => Some(sse2::predictor_add9),
10 => Some(sse2::predictor_add10),
11 => Some(sse2::predictor_add11),
12 => Some(sse2::predictor_add12),
13 => Some(sse2::predictor_add13),
_ => None,
};
if let Some(add) = add {
return add(data, in_pos, upper_pos, num_pixels, out_pos);
}
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
let neon: Option<VP8LPredictorAddSubFunc> = match mode {
0 => Some(crate::dsp::lossless_neon::predictor_add0),
1 => Some(crate::dsp::lossless_neon::predictor_add1),
2 => Some(crate::dsp::lossless_neon::predictor_add2),
3 => Some(crate::dsp::lossless_neon::predictor_add3),
4 => Some(crate::dsp::lossless_neon::predictor_add4),
5 => Some(crate::dsp::lossless_neon::predictor_add5),
6 => Some(crate::dsp::lossless_neon::predictor_add6),
7 => Some(crate::dsp::lossless_neon::predictor_add7),
8 => Some(crate::dsp::lossless_neon::predictor_add8),
9 => Some(crate::dsp::lossless_neon::predictor_add9),
10 => Some(crate::dsp::lossless_neon::predictor_add10),
11 => Some(crate::dsp::lossless_neon::predictor_add11),
12 => Some(crate::dsp::lossless_neon::predictor_add12),
13 => Some(crate::dsp::lossless_neon::predictor_add13),
_ => None,
};
if let Some(predictor_add) = neon {
return predictor_add(data, in_pos, upper_pos, num_pixels, out_pos);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
VP8L_PREDICTORS_ADD[mode](data, in_pos, upper_pos, num_pixels, out_pos);
}
#[inline]
pub fn vp8l_predictors_add_c(
mode: usize,
data: &mut [u32],
in_pos: usize,
upper_pos: usize,
num_pixels: i32,
out_pos: usize,
) {
VP8L_PREDICTORS_ADD_C[mode](data, in_pos, upper_pos, num_pixels, out_pos);
}
fn predictor_inverse_transform_c(
transform: &VP8LTransform,
mut y_start: i32,
y_end: i32,
data: &mut [u32],
mut in_pos: usize,
mut out_pos: usize,
) {
let width = transform.xsize;
let row = position(width);
if y_start == 0 {
predictor_add0_c(data, in_pos, 0, 1, out_pos);
predictor_add1_c(data, in_pos + 1, 0, width - 1, out_pos + 1);
in_pos += row;
out_pos += row;
y_start += 1;
}
{
let mut y = y_start;
let tile_width: i32 = 1 << transform.bits;
let mask = tile_width - 1;
let tiles_per_row =
vp8l_sub_sample_size(width.cast_unsigned(), transform.bits.cast_unsigned());
let mut pred_mode_base =
(y >> transform.bits).cast_unsigned() as usize * tiles_per_row as usize;
while y < y_end {
let mut pred_mode_src = pred_mode_base;
let mut x: i32 = 1;
predictor_add2_c(data, in_pos, out_pos - row, 1, out_pos);
while x < width {
let mode = ((transform.data[pred_mode_src] >> 8) & 0xf) as usize;
pred_mode_src += 1;
let mut x_end = (x & !mask) + tile_width;
if x_end > width {
x_end = width;
}
let at = position(x);
vp8l_predictors_add(
mode,
data,
in_pos + at,
out_pos + at - row,
x_end - x,
out_pos + at,
);
x = x_end;
}
in_pos += row;
out_pos += row;
y += 1;
if (y & mask) == 0 {
pred_mode_base += tiles_per_row as usize;
}
}
}
}
pub fn vp8l_add_green_to_blue_and_red_c(
data: &mut [u32],
src_pos: usize,
num_pixels: i32,
dst_pos: usize,
) {
for i in 0..pixel_count(num_pixels) {
let argb = data[src_pos + i];
let green = (argb >> 8) & 0xff;
let mut red_blue = argb & 0x00ff_00ff;
red_blue = red_blue.wrapping_add((green << 16) | green);
red_blue &= 0x00ff_00ff;
data[dst_pos + i] = (argb & 0xff00_ff00) | red_blue;
}
}
#[inline]
pub fn vp8l_add_green_to_blue_and_red(
data: &mut [u32],
src_pos: usize,
num_pixels: i32,
dst_pos: usize,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 && features.avx2 {
return crate::dsp::lossless_avx2::add_green_to_blue_and_red(
data, src_pos, num_pixels, dst_pos,
);
}
if features.sse2 {
return crate::dsp::lossless_sse2::add_green_to_blue_and_red(
data, src_pos, num_pixels, dst_pos,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::lossless_neon::add_green_to_blue_and_red(
data, src_pos, num_pixels, dst_pos,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_add_green_to_blue_and_red_c(data, src_pos, num_pixels, dst_pos);
}
#[must_use]
#[inline]
fn color_transform_delta(color_pred: i8, color: i8) -> i32 {
(i32::from(color_pred) * i32::from(color)) >> 5
}
#[inline]
fn color_code_to_multipliers(color_code: u32, m: &mut VP8LMultipliers) {
m.green_to_red = (color_code & 0xff) as u8;
m.green_to_blue = ((color_code >> 8) & 0xff) as u8;
m.red_to_blue = ((color_code >> 16) & 0xff) as u8;
}
#[inline]
#[expect(
clippy::cast_possible_truncation,
reason = "the C (int8_t) cast keeps the low byte"
)]
fn low_int8(value: u32) -> i8 {
(value as u8).cast_signed()
}
pub fn vp8l_transform_color_inverse_c(
m: &VP8LMultipliers,
data: &mut [u32],
src_pos: usize,
num_pixels: i32,
dst_pos: usize,
) {
for i in 0..pixel_count(num_pixels) {
let argb = data[src_pos + i];
let green = low_int8(argb >> 8);
let red = argb >> 16;
let mut new_red: i32 = channel(red & 0xff);
let mut new_blue: i32 = channel(argb & 0xff);
new_red += color_transform_delta(m.green_to_red.cast_signed(), green);
new_red &= 0xff;
new_blue += color_transform_delta(m.green_to_blue.cast_signed(), green);
new_blue += color_transform_delta(
m.red_to_blue.cast_signed(),
low_int8(new_red.cast_unsigned()),
);
new_blue &= 0xff;
data[dst_pos + i] = (argb & 0xff00_ff00) | ((new_red << 16) | new_blue).cast_unsigned();
}
}
#[inline]
pub fn vp8l_transform_color_inverse(
m: &VP8LMultipliers,
data: &mut [u32],
src_pos: usize,
num_pixels: i32,
dst_pos: usize,
) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 && features.avx2 {
return crate::dsp::lossless_avx2::transform_color_inverse(
m, data, src_pos, num_pixels, dst_pos,
);
}
if features.sse2 && features.sse4_1 {
return crate::dsp::lossless_sse41::transform_color_inverse(
m, data, src_pos, num_pixels, dst_pos,
);
}
if features.sse2 {
return crate::dsp::lossless_sse2::transform_color_inverse(
m, data, src_pos, num_pixels, dst_pos,
);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::lossless_neon::transform_color_inverse(
m, data, src_pos, num_pixels, dst_pos,
);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_transform_color_inverse_c(m, data, src_pos, num_pixels, dst_pos);
}
fn color_space_inverse_transform_c(
transform: &VP8LTransform,
y_start: i32,
y_end: i32,
data: &mut [u32],
mut src: usize,
mut dst: usize,
) {
let width = transform.xsize;
let tile_width: i32 = 1 << transform.bits;
let mask = tile_width - 1;
let safe_width = width & !mask;
let remaining_width = width - safe_width;
let tiles_per_row =
vp8l_sub_sample_size(width.cast_unsigned(), transform.bits.cast_unsigned()) as usize;
let mut y = y_start;
let mut pred_row = (y >> transform.bits).cast_unsigned() as usize * tiles_per_row;
while y < y_end {
let mut pred = pred_row;
let mut m = VP8LMultipliers::default();
let src_safe_end = src + position(safe_width);
let src_end = src + position(width);
while src < src_safe_end {
color_code_to_multipliers(transform.data[pred], &mut m);
pred += 1;
vp8l_transform_color_inverse(&m, data, src, tile_width, dst);
src += position(tile_width);
dst += position(tile_width);
}
if src < src_end {
color_code_to_multipliers(transform.data[pred], &mut m);
vp8l_transform_color_inverse(&m, data, src, remaining_width, dst);
src += position(remaining_width);
dst += position(remaining_width);
}
y += 1;
if (y & mask) == 0 {
pred_row += tiles_per_row;
}
}
}
fn map_argb_c(
data: &mut [u32],
mut src: usize,
color_map: &[u32],
mut dst: usize,
y_start: i32,
y_end: i32,
width: i32,
) {
for _ in y_start..y_end {
for _ in 0..pixel_count(width) {
data[dst] = vp8_get_argb_value(color_map[vp8_get_argb_index(data[src]) as usize]);
dst += 1;
src += 1;
}
}
}
fn map_alpha_c(
src: &[u8],
color_map: &[u32],
dst: &mut [u8],
y_start: i32,
y_end: i32,
width: i32,
) {
let mut s = 0;
let mut d = 0;
for _ in y_start..y_end {
for _ in 0..pixel_count(width) {
dst[d] = vp8_get_alpha_value(color_map[usize::from(vp8_get_alpha_index(src[s]))]);
d += 1;
s += 1;
}
}
}
#[inline]
pub fn vp8l_map_color32b(
data: &mut [u32],
src: usize,
color_map: &[u32],
dst: usize,
y_start: i32,
y_end: i32,
width: i32,
) {
map_argb_c(data, src, color_map, dst, y_start, y_end, width);
}
#[inline]
pub fn vp8l_map_color8b(
src: &[u8],
color_map: &[u32],
dst: &mut [u8],
y_start: i32,
y_end: i32,
width: i32,
) {
map_alpha_c(src, color_map, dst, y_start, y_end, width);
}
fn color_index_inverse_transform_c(
transform: &VP8LTransform,
y_start: i32,
y_end: i32,
data: &mut [u32],
mut src: usize,
mut dst: usize,
) {
let bits_per_pixel = 8 >> transform.bits;
let width = transform.xsize;
let color_map = &transform.data[..];
if bits_per_pixel < 8 {
let pixels_per_byte = 1 << transform.bits;
let count_mask = pixels_per_byte - 1;
let bit_mask: u32 = ((1_i32 << bits_per_pixel) - 1).cast_unsigned();
for _ in y_start..y_end {
let mut packed_pixels: u32 = 0;
for x in 0..width {
if (x & count_mask) == 0 {
packed_pixels = vp8_get_argb_index(data[src]);
src += 1;
}
data[dst] = vp8_get_argb_value(color_map[(packed_pixels & bit_mask) as usize]);
dst += 1;
packed_pixels >>= bits_per_pixel;
}
}
} else {
vp8l_map_color32b(data, src, color_map, dst, y_start, y_end, width);
}
}
pub fn vp8l_color_index_inverse_transform_alpha(
transform: &VP8LTransform,
y_start: i32,
y_end: i32,
src: &[u8],
dst: &mut [u8],
) {
let bits_per_pixel = 8 >> transform.bits;
let width = transform.xsize;
let color_map = &transform.data[..];
if bits_per_pixel < 8 {
let pixels_per_byte = 1 << transform.bits;
let count_mask = pixels_per_byte - 1;
let bit_mask: u32 = ((1_i32 << bits_per_pixel) - 1).cast_unsigned();
let mut s = 0;
let mut d = 0;
for _ in y_start..y_end {
let mut packed_pixels: u32 = 0;
for x in 0..width {
if (x & count_mask) == 0 {
packed_pixels = u32::from(vp8_get_alpha_index(src[s]));
s += 1;
}
dst[d] = vp8_get_alpha_value(color_map[(packed_pixels & bit_mask) as usize]);
d += 1;
packed_pixels >>= bits_per_pixel;
}
}
} else {
vp8l_map_color8b(src, color_map, dst, y_start, y_end, width);
}
}
pub fn vp8l_inverse_transform(
transform: &VP8LTransform,
row_start: i32,
row_end: i32,
data: &mut [u32],
in_pos: usize,
out_pos: usize,
) {
let width = transform.xsize;
debug_assert!(row_start < row_end);
debug_assert!(row_end <= transform.ysize);
match transform.r#type {
VP8LImageTransformType::SUBTRACT_GREEN_TRANSFORM => {
vp8l_add_green_to_blue_and_red(data, in_pos, (row_end - row_start) * width, out_pos);
}
VP8LImageTransformType::PREDICTOR_TRANSFORM => {
predictor_inverse_transform_c(transform, row_start, row_end, data, in_pos, out_pos);
if row_end != transform.ysize {
let row = position(width);
let last = out_pos + position((row_end - row_start - 1) * width);
data.copy_within(last..last + row, out_pos - row);
}
}
VP8LImageTransformType::CROSS_COLOR_TRANSFORM => {
color_space_inverse_transform_c(transform, row_start, row_end, data, in_pos, out_pos);
}
VP8LImageTransformType::COLOR_INDEXING_TRANSFORM => {
if in_pos == out_pos && transform.bits > 0 {
let out_stride = position((row_end - row_start) * width);
let in_stride = position(row_end - row_start)
* vp8l_sub_sample_size(
transform.xsize.cast_unsigned(),
transform.bits.cast_unsigned(),
) as usize;
let src = out_pos + out_stride - in_stride;
data.copy_within(out_pos..out_pos + in_stride, src);
color_index_inverse_transform_c(transform, row_start, row_end, data, src, out_pos);
} else {
color_index_inverse_transform_c(
transform, row_start, row_end, data, in_pos, out_pos,
);
}
}
_ => {}
}
}
#[must_use]
#[inline]
fn is_big_endian() -> bool {
cfg!(target_endian = "big")
}
#[inline]
fn byte_at(argb: u32, shift: u32) -> u8 {
((argb >> shift) & 0xff) as u8
}
pub fn vp8l_convert_bgra_to_rgb_c(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
for (&argb, out) in src[..pixel_count(num_pixels)]
.iter()
.zip(dst.as_chunks_mut::<3>().0)
{
out[0] = byte_at(argb, 16);
out[1] = byte_at(argb, 8);
out[2] = byte_at(argb, 0);
}
}
pub fn vp8l_convert_bgra_to_rgba_c(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
for (&argb, out) in src[..pixel_count(num_pixels)]
.iter()
.zip(dst.as_chunks_mut::<4>().0)
{
out[0] = byte_at(argb, 16);
out[1] = byte_at(argb, 8);
out[2] = byte_at(argb, 0);
out[3] = byte_at(argb, 24);
}
}
pub fn vp8l_convert_bgra_to_rgba4444_c(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
for (&argb, out) in src[..pixel_count(num_pixels)]
.iter()
.zip(dst.as_chunks_mut::<2>().0)
{
let rg = byte_at(argb, 16) & 0xf0 | byte_at(argb, 12) & 0xf;
let ba = byte_at(argb, 0) & 0xf0 | byte_at(argb, 28) & 0xf;
out[0] = rg;
out[1] = ba;
}
}
pub fn vp8l_convert_bgra_to_rgb565_c(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
for (&argb, out) in src[..pixel_count(num_pixels)]
.iter()
.zip(dst.as_chunks_mut::<2>().0)
{
let rg = byte_at(argb, 16) & 0xf8 | byte_at(argb, 13) & 0x7;
let gb = byte_at(argb, 5) & 0xe0 | byte_at(argb, 3) & 0x1f;
out[0] = rg;
out[1] = gb;
}
}
pub fn vp8l_convert_bgra_to_bgr_c(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
for (&argb, out) in src[..pixel_count(num_pixels)]
.iter()
.zip(dst.as_chunks_mut::<3>().0)
{
out[0] = byte_at(argb, 0);
out[1] = byte_at(argb, 8);
out[2] = byte_at(argb, 16);
}
}
#[inline]
pub fn vp8l_convert_bgra_to_rgb(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::lossless_sse41::convert_bgra_to_rgb(src, num_pixels, dst);
}
if features.sse2 {
return crate::dsp::lossless_sse2::convert_bgra_to_rgb(src, num_pixels, dst);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::lossless_neon::convert_bgra_to_rgb(src, num_pixels, dst);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_convert_bgra_to_rgb_c(src, num_pixels, dst);
}
#[inline]
pub fn vp8l_convert_bgra_to_rgba(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 && features.avx2 {
return crate::dsp::lossless_avx2::convert_bgra_to_rgba(src, num_pixels, dst);
}
if features.sse2 {
return crate::dsp::lossless_sse2::convert_bgra_to_rgba(src, num_pixels, dst);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::lossless_neon::convert_bgra_to_rgba(src, num_pixels, dst);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_convert_bgra_to_rgba_c(src, num_pixels, dst);
}
#[inline]
pub fn vp8l_convert_bgra_to_rgba4444(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::lossless_sse2::convert_bgra_to_rgba4444(src, num_pixels, dst);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_convert_bgra_to_rgba4444_c(src, num_pixels, dst);
}
#[inline]
pub fn vp8l_convert_bgra_to_rgb565(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 {
return crate::dsp::lossless_sse2::convert_bgra_to_rgb565(src, num_pixels, dst);
}
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_convert_bgra_to_rgb565_c(src, num_pixels, dst);
}
#[inline]
pub fn vp8l_convert_bgra_to_bgr(src: &[u32], num_pixels: i32, dst: &mut [u8]) {
#[cfg(all(feature = "simd", any(target_arch = "x86", target_arch = "x86_64")))]
{
let features = crate::dsp::cpu::features();
if features.sse2 && features.sse4_1 {
return crate::dsp::lossless_sse41::convert_bgra_to_bgr(src, num_pixels, dst);
}
if features.sse2 {
return crate::dsp::lossless_sse2::convert_bgra_to_bgr(src, num_pixels, dst);
}
}
#[cfg(all(feature = "simd", target_arch = "aarch64"))]
{
return crate::dsp::lossless_neon::convert_bgra_to_bgr(src, num_pixels, dst);
}
#[allow(
unreachable_code,
reason = "the C path is reached only without a SIMD variant"
)]
vp8l_convert_bgra_to_bgr_c(src, num_pixels, dst);
}
fn copy_or_swap(src: &[u32], num_pixels: i32, dst: &mut [u8], swap_on_big_endian: bool) {
let src = &src[..pixel_count(num_pixels)];
if is_big_endian() == swap_on_big_endian {
for (&argb, out) in src.iter().zip(dst.as_chunks_mut::<4>().0) {
webp_uint32_to_mem(out, b_swap32(argb));
}
} else {
for (&argb, out) in src.iter().zip(dst.as_chunks_mut::<4>().0) {
out.copy_from_slice(&argb.to_ne_bytes());
}
}
}
pub fn vp8l_convert_from_bgra(
in_data: &[u32],
num_pixels: i32,
out_colorspace: WEBP_CSP_MODE,
rgba: &mut [u8],
) {
match out_colorspace {
WEBP_CSP_MODE::MODE_RGB => vp8l_convert_bgra_to_rgb(in_data, num_pixels, rgba),
WEBP_CSP_MODE::MODE_RGBA => vp8l_convert_bgra_to_rgba(in_data, num_pixels, rgba),
WEBP_CSP_MODE::MODE_rgbA => {
vp8l_convert_bgra_to_rgba(in_data, num_pixels, rgba);
webp_apply_alpha_multiply(rgba, false, num_pixels, 1, 0);
}
WEBP_CSP_MODE::MODE_BGR => vp8l_convert_bgra_to_bgr(in_data, num_pixels, rgba),
WEBP_CSP_MODE::MODE_BGRA => copy_or_swap(in_data, num_pixels, rgba, true),
WEBP_CSP_MODE::MODE_bgrA => {
copy_or_swap(in_data, num_pixels, rgba, true);
webp_apply_alpha_multiply(rgba, false, num_pixels, 1, 0);
}
WEBP_CSP_MODE::MODE_ARGB => copy_or_swap(in_data, num_pixels, rgba, false),
WEBP_CSP_MODE::MODE_Argb => {
copy_or_swap(in_data, num_pixels, rgba, false);
webp_apply_alpha_multiply(rgba, true, num_pixels, 1, 0);
}
WEBP_CSP_MODE::MODE_RGBA_4444 => vp8l_convert_bgra_to_rgba4444(in_data, num_pixels, rgba),
WEBP_CSP_MODE::MODE_rgbA_4444 => {
vp8l_convert_bgra_to_rgba4444(in_data, num_pixels, rgba);
webp_apply_alpha_multiply4444(rgba, num_pixels, 1, 0);
}
WEBP_CSP_MODE::MODE_RGB_565 => vp8l_convert_bgra_to_rgb565(in_data, num_pixels, rgba),
_ => debug_assert!(false, "Code flow should not reach here."),
}
}