use crate::dec::alphai_dec::ALPHDecoder;
use crate::dec::vp8_dec::{VP8Io, vp8_init_io, vp8_set_error};
use crate::dec::vp8i_dec::{VP8AlphaPlane, VP8Decoder};
use crate::dec::vp8l_dec::{vp8l_decode_alpha_header, vp8l_decode_alpha_image_stream};
use crate::dsp::dsp::WEBP_FILTER_TYPE;
use crate::dsp::filters::webp_unfilters;
use crate::utils::quant_levels_dec_utils::webp_dequantize_levels;
use crate::utils::utils::webp_safe_malloc;
use crate::webp::decode::VP8StatusCode;
use crate::webp::format_constants::{
ALPHA_HEADER_LEN, ALPHA_LOSSLESS_COMPRESSION, ALPHA_NO_COMPRESSION, ALPHA_PREPROCESSED_LEVELS,
};
const HEADER_LEN: usize = ALPHA_HEADER_LEN.unsigned_abs() as usize;
fn index(value: i32) -> usize {
usize::try_from(value).expect("a count or position is not negative")
}
fn alpha_plane_mut(dec: &mut VP8Decoder) -> &mut [u8] {
match dec.alpha_plane {
VP8AlphaPlane::Memory => &mut dec.alpha_plane_mem,
VP8AlphaPlane::Frame => &mut dec.frame_alpha,
VP8AlphaPlane::None => panic!("the decoder has no alpha plane"),
}
}
#[expect(
clippy::unnecessary_box_returns,
reason = "C returns the heap object that dec->alph_dec holds"
)]
fn alph_new() -> Box<ALPHDecoder> {
Box::<ALPHDecoder>::default()
}
fn alph_delete(dec: Option<Box<ALPHDecoder>>) {
drop(dec);
}
fn alph_init(dec: &mut ALPHDecoder, data: &[u8], src_io: &VP8Io) -> bool {
dec.width = src_io.width;
dec.height = src_io.height;
debug_assert!(dec.width > 0 && dec.height > 0);
if data.len() <= HEADER_LEN {
return false;
}
let alpha_data = &data[HEADER_LEN..];
dec.method = i32::from(data[0] & 0x03);
dec.filter = WEBP_FILTER_TYPE(u32::from((data[0] >> 2) & 0x03));
dec.pre_processing = i32::from((data[0] >> 4) & 0x03);
let rsrv = (data[0] >> 6) & 0x03;
if dec.method < ALPHA_NO_COMPRESSION
|| dec.method > ALPHA_LOSSLESS_COMPRESSION
|| dec.filter >= WEBP_FILTER_TYPE::WEBP_FILTER_LAST
|| dec.pre_processing > ALPHA_PREPROCESSED_LEVELS
|| rsrv != 0
{
return false;
}
let io = &mut dec.io;
if !vp8_init_io(io) {
return false;
}
io.width = src_io.width;
io.height = src_io.height;
io.use_cropping = src_io.use_cropping;
io.crop_left = src_io.crop_left;
io.crop_right = src_io.crop_right;
io.crop_top = src_io.crop_top;
io.crop_bottom = src_io.crop_bottom;
if dec.method == ALPHA_NO_COMPRESSION {
let alpha_decoded_size = index(dec.width * dec.height);
alpha_data.len() >= alpha_decoded_size
} else {
debug_assert_eq!(dec.method, ALPHA_LOSSLESS_COMPRESSION);
vp8l_decode_alpha_header(dec, alpha_data)
}
}
fn alph_decode(dec: &mut VP8Decoder, alpha_data: &[u8], row: i32, num_rows: i32) -> bool {
let mut alph_dec = dec.alph_dec.take().expect("the alpha decoder is set up");
let width = alph_dec.width;
let height = alph_dec.io.crop_bottom;
let ok = if alph_dec.method == ALPHA_NO_COMPRESSION {
let mut prev_line = dec.alpha_prev_line;
let mut deltas = HEADER_LEN + index(row * width);
let mut dst = index(row * width);
let w = index(width);
debug_assert!(deltas <= alpha_data.len());
let filter = alph_dec.filter;
let plane = alpha_plane_mut(dec);
for _ in 0..num_rows {
let (before, after) = plane.split_at_mut(dst);
webp_unfilters(
filter,
prev_line.map(|prev| &before[prev..]),
&alpha_data[deltas..],
&mut after[..w],
width,
);
prev_line = Some(dst);
dst += w;
deltas += w;
}
dec.alpha_prev_line = prev_line;
true
} else {
debug_assert!(alph_dec.vp8l_dec.is_some());
vp8l_decode_alpha_image_stream(
&mut alph_dec,
row + num_rows,
&alpha_data[HEADER_LEN..],
alpha_plane_mut(dec),
)
.is_ok()
};
dec.alph_dec = Some(alph_dec);
if !ok {
return false;
}
if row + num_rows >= height {
dec.is_alpha_decoded = true;
}
true
}
fn allocate_alpha_plane(dec: &mut VP8Decoder, io: &VP8Io) -> Result<(), VP8StatusCode> {
let stride = io.width;
let height = io.crop_bottom;
let alpha_size = u64::from(stride.unsigned_abs()) * u64::from(height.unsigned_abs());
debug_assert_eq!(dec.alpha_plane_mem.len(), 0);
let Some(mut plane) = webp_safe_malloc::<u8>(alpha_size, 1) else {
return Err(vp8_set_error(
dec,
VP8StatusCode::VP8_STATUS_OUT_OF_MEMORY,
"Alpha decoder initialization failed.",
));
};
plane.resize(
usize::try_from(alpha_size).expect("an accepted size fits"),
0,
);
dec.alpha_plane_mem = plane;
dec.alpha_plane = VP8AlphaPlane::Memory;
dec.alpha_prev_line = None;
Ok(())
}
pub fn webp_deallocate_alpha_memory(dec: &mut VP8Decoder) {
dec.alpha_plane_mem = Vec::new();
dec.alpha_plane = VP8AlphaPlane::None;
alph_delete(dec.alph_dec.take());
}
#[expect(clippy::if_not_else, reason = "the C condition")]
pub fn vp8_decompress_alpha_rows(
dec: &mut VP8Decoder,
io: &VP8Io,
alpha_data: &[u8],
row: i32,
num_rows: i32,
) -> Option<usize> {
let width = io.width;
let height = io.crop_bottom;
let mut num_rows = num_rows;
if row < 0 || num_rows <= 0 || row + num_rows > height {
return None;
}
if !dec.is_alpha_decoded {
if dec.alph_dec.is_none() {
dec.alph_dec = Some(alph_new());
if allocate_alpha_plane(dec, io).is_err() {
webp_deallocate_alpha_memory(dec);
return None;
}
let alph_dec = dec.alph_dec.as_deref_mut().expect("just set");
if !alph_init(alph_dec, alpha_data, io) {
let status = alph_dec
.vp8l_dec
.as_ref()
.map_or(VP8StatusCode::VP8_STATUS_OUT_OF_MEMORY, |vp8l_dec| {
vp8l_dec.status
});
let _ = vp8_set_error(dec, status, "Alpha decoder initialization failed.");
webp_deallocate_alpha_memory(dec);
return None;
}
if alph_dec.pre_processing != ALPHA_PREPROCESSED_LEVELS {
dec.alpha_dithering = 0; } else {
num_rows = height - row; }
}
debug_assert!(dec.alph_dec.is_some());
debug_assert!(row + num_rows <= height);
if !alph_decode(dec, alpha_data, row, num_rows) {
webp_deallocate_alpha_memory(dec);
return None;
}
if dec.is_alpha_decoded {
alph_delete(dec.alph_dec.take());
if dec.alpha_dithering > 0 {
let alpha = index(io.crop_top * width + io.crop_left);
let strength = dec.alpha_dithering;
if !webp_dequantize_levels(
Some(&mut alpha_plane_mut(dec)[alpha..]),
io.crop_right - io.crop_left,
io.crop_bottom - io.crop_top,
width,
strength,
) {
webp_deallocate_alpha_memory(dec);
return None;
}
}
}
}
Some(index(row * width))
}