use crate::dec::vp8_dec::VP8Io;
use crate::utils::rescaler_utils::webp_rescaler_get_scaled_dimensions;
use crate::webp::decode::{WEBP_CSP_MODE, WebPDecoderOptions, webp_is_rgb_mode};
fn webp_check_crop_dimensions_basic(x: i32, y: i32, w: i32, h: i32) -> bool {
!(x < 0 || y < 0 || w <= 0 || h <= 0)
}
#[must_use]
pub fn webp_check_crop_dimensions(
image_width: i32,
image_height: i32,
x: i32,
y: i32,
w: i32,
h: i32,
) -> bool {
webp_check_crop_dimensions_basic(x, y, w, h)
&& !(x >= image_width
|| w > image_width
|| w > image_width - x
|| y >= image_height
|| h > image_height
|| h > image_height - y)
}
#[must_use]
pub fn webp_io_init_from_options(
options: Option<&WebPDecoderOptions>,
io: &mut VP8Io,
src_colorspace: WEBP_CSP_MODE,
) -> bool {
let big_w = io.width;
let big_h = io.height;
let mut x = 0;
let mut y = 0;
let mut w = big_w;
let mut h = big_h;
io.use_cropping = options.is_some_and(|options| options.use_cropping != 0);
if let Some(options) = options.filter(|_| io.use_cropping) {
w = options.crop_width;
h = options.crop_height;
x = options.crop_left;
y = options.crop_top;
if !webp_is_rgb_mode(src_colorspace) {
x &= !1;
y &= !1;
}
if !webp_check_crop_dimensions(big_w, big_h, x, y, w, h) {
return false; }
}
io.crop_left = x;
io.crop_top = y;
io.crop_right = x + w;
io.crop_bottom = y + h;
io.mb_w = w;
io.mb_h = h;
io.use_scaling = options.is_some_and(|options| options.use_scaling != 0);
if let Some(options) = options.filter(|_| io.use_scaling) {
let mut scaled_width = options.scaled_width;
let mut scaled_height = options.scaled_height;
if !webp_rescaler_get_scaled_dimensions(w, h, &mut scaled_width, &mut scaled_height) {
return false;
}
io.scaled_width = scaled_width;
io.scaled_height = scaled_height;
}
io.bypass_filtering = options.is_some_and(|options| options.bypass_filtering != 0);
io.fancy_upsampling = options.is_none_or(|options| options.no_fancy_upsampling == 0);
if io.use_scaling {
io.bypass_filtering |=
(io.scaled_width < big_w * 3 / 4) && (io.scaled_height < big_h * 3 / 4);
io.fancy_upsampling = false;
}
true
}
use crate::dec::buffer_dec::{
webp_allocate_dec_buffer, webp_avoid_slow_memory, webp_copy_dec_buffer_pixels,
webp_flip_buffer, webp_free_dec_buffer,
};
use crate::dec::frame_dec::{vp8_get_thread_method, vp8_init_dithering};
use crate::dec::io_dec::webp_init_custom_io;
use crate::dec::vp8_dec::{vp8_decode, vp8_get_headers, vp8_get_info, vp8_init_io, vp8_new};
use crate::dec::vp8l_dec::{
vp8l_check_signature, vp8l_decode_header, vp8l_decode_image, vp8l_get_info, vp8l_new,
};
use crate::dec::webpi_dec::{WebPDecParams, WebPHeaderStructure};
use crate::utils::utils::{get_le24, get_le32};
use crate::webp::decode::{
VP8StatusCode, WEBP_DECODER_ABI_VERSION, WebPBitstreamFeatures, WebPDecBuffer,
WebPDecBufferPlanes, WebPDecoderConfig, WebPRGBABuffer, WebPYUVABuffer, webp_init_dec_buffer,
};
use crate::webp::format_constants::{
CHUNK_HEADER_SIZE, MAX_CHUNK_PAYLOAD, MAX_IMAGE_AREA, RIFF_HEADER_SIZE, TAG_SIZE,
VP8_FRAME_HEADER_SIZE, VP8L_FRAME_HEADER_SIZE, VP8X_CHUNK_SIZE,
};
use crate::webp::mux_types::WebPFeatureFlags;
use crate::webp::types::{PlaneOffset, PlaneStorage, webp_abi_is_incompatible};
const TAG: usize = TAG_SIZE.unsigned_abs() as usize;
const CHUNK_HEADER: usize = CHUNK_HEADER_SIZE.unsigned_abs() as usize;
const RIFF_HEADER: usize = RIFF_HEADER_SIZE.unsigned_abs() as usize;
fn status_result(status: VP8StatusCode) -> Result<(), VP8StatusCode> {
if status == VP8StatusCode::VP8_STATUS_OK {
Ok(())
} else {
Err(status)
}
}
fn has_tag(data: &[u8], pos: usize, tag: [u8; 4]) -> bool {
data[pos..pos + TAG] == tag[..]
}
fn parse_riff(
data: &[u8],
pos: &mut usize,
data_size: &mut usize,
have_all_data: bool,
riff_size: &mut usize,
) -> Result<(), VP8StatusCode> {
*riff_size = 0; if *data_size >= RIFF_HEADER && has_tag(data, *pos, *b"RIFF") {
if !has_tag(data, *pos + 8, *b"WEBP") {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
let size = get_le32(&data[*pos + TAG..]);
if size < (TAG_SIZE + CHUNK_HEADER_SIZE).cast_unsigned() {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
}
if size > MAX_CHUNK_PAYLOAD {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
}
if have_all_data && (size as usize > *data_size - CHUNK_HEADER) {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA); }
*riff_size = size as usize;
*pos += RIFF_HEADER;
*data_size -= RIFF_HEADER;
}
Ok(())
}
fn parse_vp8x(
data: &[u8],
pos: &mut usize,
data_size: &mut usize,
found_vp8x: &mut bool,
width_ptr: Option<&mut i32>,
height_ptr: Option<&mut i32>,
flags_ptr: Option<&mut u32>,
) -> Result<(), VP8StatusCode> {
let vp8x_size = (CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE).unsigned_abs() as usize;
*found_vp8x = false;
if *data_size < CHUNK_HEADER {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA); }
if has_tag(data, *pos, *b"VP8X") {
let chunk_size = get_le32(&data[*pos + TAG..]);
if chunk_size != VP8X_CHUNK_SIZE.cast_unsigned() {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
if *data_size < vp8x_size {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA); }
let flags = get_le32(&data[*pos + 8..]);
let width = 1 + get_le24(&data[*pos + 12..]);
let height = 1 + get_le24(&data[*pos + 15..]);
if i64::from(width).cast_unsigned() * i64::from(height).cast_unsigned() >= MAX_IMAGE_AREA {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
if let Some(flags_ptr) = flags_ptr {
*flags_ptr = flags;
}
if let Some(width_ptr) = width_ptr {
*width_ptr = width;
}
if let Some(height_ptr) = height_ptr {
*height_ptr = height;
}
*pos += vp8x_size;
*data_size -= vp8x_size;
*found_vp8x = true;
}
Ok(())
}
fn parse_optional_chunks(
data: &[u8],
pos: &mut usize,
data_size: &mut usize,
riff_size: usize,
alpha_data: &mut Option<usize>,
alpha_size: &mut usize,
) -> Result<(), VP8StatusCode> {
let mut buf = *pos;
let mut buf_size = *data_size;
let mut total_size: u32 = (TAG_SIZE + CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) .cast_unsigned();
*alpha_data = None;
*alpha_size = 0;
loop {
*pos = buf;
*data_size = buf_size;
if buf_size < CHUNK_HEADER {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
let chunk_size = get_le32(&data[buf + TAG..]);
if chunk_size > MAX_CHUNK_PAYLOAD {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
let disk_chunk_size = (CHUNK_HEADER_SIZE.cast_unsigned() + chunk_size + 1) & !1u32;
total_size = total_size.wrapping_add(disk_chunk_size);
if riff_size > 0 && (total_size as usize > riff_size) {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
if has_tag(data, buf, *b"VP8 ") || has_tag(data, buf, *b"VP8L") {
return Ok(());
}
if buf_size < disk_chunk_size as usize {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
if has_tag(data, buf, *b"ALPH") {
*alpha_data = Some(buf + CHUNK_HEADER);
*alpha_size = chunk_size as usize;
}
buf += disk_chunk_size as usize;
buf_size -= disk_chunk_size as usize;
}
}
fn parse_vp8_header(
data: &[u8],
pos: &mut usize,
data_size: &mut usize,
have_all_data: bool,
riff_size: usize,
chunk_size: &mut usize,
is_lossless: &mut bool,
) -> Result<(), VP8StatusCode> {
let is_vp8 = has_tag(data, *pos, *b"VP8 ");
let is_vp8l = has_tag(data, *pos, *b"VP8L");
let minimal_size = (TAG_SIZE + CHUNK_HEADER_SIZE).unsigned_abs() as usize;
if *data_size < CHUNK_HEADER {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA); }
if is_vp8 || is_vp8l {
let size = get_le32(&data[*pos + TAG..]) as usize;
if (riff_size >= minimal_size) && (size > riff_size - minimal_size) {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
if have_all_data && (size > *data_size - CHUNK_HEADER) {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA); }
*chunk_size = size;
*pos += CHUNK_HEADER;
*data_size -= CHUNK_HEADER;
*is_lossless = is_vp8l;
} else {
*is_lossless = vp8l_check_signature(&data[*pos..*pos + *data_size]);
*chunk_size = *data_size;
}
Ok(())
}
#[expect(clippy::too_many_lines, reason = "the C function")]
fn parse_headers_internal<'d>(
data: &'d [u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
has_alpha: Option<&mut i32>,
has_animation: Option<&mut i32>,
format: Option<&mut i32>,
headers: Option<&mut WebPHeaderStructure<'d>>,
) -> Result<(), VP8StatusCode> {
let mut has_alpha = has_alpha;
let mut format = format;
let mut canvas_width = 0;
let mut canvas_height = 0;
let mut image_width;
let mut image_height;
let mut found_vp8x = false;
let have_all_data = headers
.as_ref()
.is_some_and(|headers| headers.have_all_data);
let has_headers = headers.is_some();
if data.len() < RIFF_HEADER {
return Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
let mut hdrs = WebPHeaderStructure {
data,
..WebPHeaderStructure::default()
};
let mut pos = 0;
let mut data_size = data.len();
parse_riff(
data,
&mut pos,
&mut data_size,
have_all_data,
&mut hdrs.riff_size,
)?;
let found_riff = hdrs.riff_size > 0;
let status = 'return_width_height: {
let animation_present;
{
let mut flags = 0u32;
parse_vp8x(
data,
&mut pos,
&mut data_size,
&mut found_vp8x,
Some(&mut canvas_width),
Some(&mut canvas_height),
Some(&mut flags),
)?; animation_present = (flags & WebPFeatureFlags::ANIMATION_FLAG.0) != 0;
if !found_riff && found_vp8x {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
}
if let Some(has_alpha) = has_alpha.as_deref_mut() {
*has_alpha = i32::from((flags & WebPFeatureFlags::ALPHA_FLAG.0) != 0);
}
if let Some(has_animation) = has_animation {
*has_animation = i32::from(animation_present);
}
if let Some(format) = format.as_deref_mut() {
*format = 0; }
image_width = canvas_width;
image_height = canvas_height;
if found_vp8x && animation_present && !has_headers {
break 'return_width_height Ok(()); }
}
if data_size < TAG {
break 'return_width_height Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
if (found_riff && found_vp8x)
|| (!found_riff && !found_vp8x && has_tag(data, pos, *b"ALPH"))
{
let status = parse_optional_chunks(
data,
&mut pos,
&mut data_size,
hdrs.riff_size,
&mut hdrs.alpha_data,
&mut hdrs.alpha_data_size,
);
if status.is_err() {
break 'return_width_height status; }
}
let status = parse_vp8_header(
data,
&mut pos,
&mut data_size,
have_all_data,
hdrs.riff_size,
&mut hdrs.compressed_size,
&mut hdrs.is_lossless,
);
if status.is_err() {
break 'return_width_height status; }
if hdrs.compressed_size > MAX_CHUNK_PAYLOAD as usize {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
}
if let Some(format) = format
&& !animation_present
{
*format = if hdrs.is_lossless { 2 } else { 1 };
}
if hdrs.is_lossless {
if data_size < VP8L_FRAME_HEADER_SIZE.unsigned_abs() as usize {
break 'return_width_height Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
let Some((w, h, alpha)) = vp8l_get_info(&data[pos..pos + data_size]) else {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
};
image_width = w;
image_height = h;
if let Some(has_alpha) = has_alpha.as_deref_mut() {
*has_alpha = i32::from(alpha);
}
} else {
if data_size < VP8_FRAME_HEADER_SIZE.unsigned_abs() as usize {
break 'return_width_height Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA);
}
let Some((w, h)) = vp8_get_info(&data[pos..pos + data_size], hdrs.compressed_size)
else {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
};
image_width = w;
image_height = h;
}
if found_vp8x && (canvas_width != image_width || canvas_height != image_height) {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR);
}
if let Some(headers) = headers {
*headers = hdrs;
headers.offset = pos;
debug_assert!((pos as u64) < u64::from(MAX_CHUNK_PAYLOAD));
debug_assert_eq!(headers.offset, headers.data.len() - data_size);
}
Ok(())
};
if status.is_ok()
|| (status == Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA) && found_vp8x && !has_headers)
{
if let Some(has_alpha) = has_alpha {
*has_alpha |= i32::from(hdrs.alpha_data.is_some());
}
if let Some(width) = width {
*width = image_width;
}
if let Some(height) = height {
*height = image_height;
}
Ok(())
} else {
status
}
}
pub fn webp_parse_headers(headers: &mut WebPHeaderStructure<'_>) -> Result<(), VP8StatusCode> {
let mut has_animation = 0;
let data = headers.data;
let mut status = parse_headers_internal(
data,
None,
None,
None,
Some(&mut has_animation),
None,
Some(headers),
);
if status.is_ok() || status == Err(VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA) {
if has_animation != 0 {
status = Err(VP8StatusCode::VP8_STATUS_UNSUPPORTED_FEATURE);
}
}
status
}
pub fn webp_reset_dec_params(params: &mut WebPDecParams<'_>) {
*params = WebPDecParams::default();
}
fn decode_into(
data: &[u8],
params: &mut WebPDecParams<'_>,
output: &mut WebPDecBuffer<'_>,
) -> Result<(), VP8StatusCode> {
let mut headers = WebPHeaderStructure {
data,
have_all_data: true,
..WebPHeaderStructure::default()
};
webp_parse_headers(&mut headers)?;
let mut io = VP8Io::default();
if !vp8_init_io(&mut io) {
return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
}
let io_data = &headers.data[headers.offset..];
let mut status;
if headers.is_lossless {
let mut dec = vp8l_new();
if let Err(dec_status) = vp8l_decode_header(&mut dec, &mut io, io_data) {
status = Err(dec_status); } else {
status = webp_allocate_dec_buffer(io.width, io.height, params.options, output);
if status.is_ok() {
if let Err(dec_status) =
vp8l_decode_image(&mut dec, &mut io, io_data, params, output)
{
status = Err(dec_status);
}
}
}
} else {
let mut dec = vp8_new();
let alpha_data = headers
.alpha_data
.map(|alpha| &headers.data[alpha..alpha + headers.alpha_data_size]);
if vp8_get_headers(&mut dec, &mut io, io_data).is_err() {
status = status_result(dec.status); } else {
status = webp_allocate_dec_buffer(io.width, io.height, params.options, output);
if status.is_ok() {
debug_assert!(!headers.is_lossless); dec.mt_method = vp8_get_thread_method(params.options, io.width, io.height);
vp8_init_dithering(params.options, &mut dec);
let mut hooks = webp_init_custom_io(params, output, &mut io);
if !vp8_decode(&mut dec, &mut io, io_data, alpha_data, Some(&mut hooks)) {
status = status_result(dec.status);
}
}
}
}
if status.is_err() {
webp_free_dec_buffer(output);
} else if params.options.is_some_and(|options| options.flip != 0) {
status = webp_flip_buffer(output);
}
status
}
fn decode_into_rgba_buffer<'a>(
colorspace: WEBP_CSP_MODE,
data: &[u8],
rgba: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
let mut params = WebPDecParams::default();
let mut buf = WebPDecBuffer::default();
if !webp_init_dec_buffer(&mut buf) {
return None;
}
webp_reset_dec_params(&mut params);
buf.colorspace = colorspace;
buf.u = WebPDecBufferPlanes::RGBA(WebPRGBABuffer {
rgba: PlaneOffset::default(),
stride,
size: rgba.len(),
});
buf.storage = PlaneStorage::External(vec![rgba]);
if decode_into(data, &mut params, &mut buf).is_err() {
return None;
}
external_region(buf.storage)
}
fn external_region(storage: PlaneStorage<'_, u8>) -> Option<&mut [u8]> {
match storage {
PlaneStorage::External(slices) => slices.into_iter().next(),
PlaneStorage::Internal(_) | PlaneStorage::Shared(_) => None,
}
}
#[must_use]
pub fn webp_decode_rgb_into<'a>(
data: &[u8],
output: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
decode_into_rgba_buffer(WEBP_CSP_MODE::MODE_RGB, data, output, stride)
}
#[must_use]
pub fn webp_decode_rgba_into<'a>(
data: &[u8],
output: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
decode_into_rgba_buffer(WEBP_CSP_MODE::MODE_RGBA, data, output, stride)
}
#[must_use]
pub fn webp_decode_argb_into<'a>(
data: &[u8],
output: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
decode_into_rgba_buffer(WEBP_CSP_MODE::MODE_ARGB, data, output, stride)
}
#[must_use]
pub fn webp_decode_bgr_into<'a>(
data: &[u8],
output: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
decode_into_rgba_buffer(WEBP_CSP_MODE::MODE_BGR, data, output, stride)
}
#[must_use]
pub fn webp_decode_bgra_into<'a>(
data: &[u8],
output: &'a mut [u8],
stride: i32,
) -> Option<&'a mut [u8]> {
decode_into_rgba_buffer(WEBP_CSP_MODE::MODE_BGRA, data, output, stride)
}
#[must_use]
pub fn webp_decode_yuv_into<'a>(
data: &[u8],
luma: &'a mut [u8],
luma_stride: i32,
u: &'a mut [u8],
u_stride: i32,
v: &'a mut [u8],
v_stride: i32,
) -> Option<&'a mut [u8]> {
let mut params = WebPDecParams::default();
let mut output = WebPDecBuffer::default();
if !webp_init_dec_buffer(&mut output) {
return None;
}
webp_reset_dec_params(&mut params);
output.colorspace = WEBP_CSP_MODE::MODE_YUV;
output.u = WebPDecBufferPlanes::YUVA(WebPYUVABuffer {
y: PlaneOffset {
region: 0,
offset: 0,
},
y_stride: luma_stride,
y_size: luma.len(),
u: PlaneOffset {
region: 1,
offset: 0,
},
u_stride,
u_size: u.len(),
v: PlaneOffset {
region: 2,
offset: 0,
},
v_stride,
v_size: v.len(),
..WebPYUVABuffer::default()
});
output.storage = PlaneStorage::External(vec![luma, u, v]);
if decode_into(data, &mut params, &mut output).is_err() {
return None;
}
external_region(output.storage)
}
fn decode(
mode: WEBP_CSP_MODE,
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<WebPDecBuffer<'static>> {
let mut params = WebPDecParams::default();
let mut output = WebPDecBuffer::default();
if !webp_init_dec_buffer(&mut output) {
return None;
}
webp_reset_dec_params(&mut params);
output.colorspace = mode;
if !webp_get_info(data, Some(&mut output.width), Some(&mut output.height)) {
return None;
}
if let Some(width) = width {
*width = output.width;
}
if let Some(height) = height {
*height = output.height;
}
if decode_into(data, &mut params, &mut output).is_err() {
return None;
}
Some(output)
}
fn into_samples(buffer: WebPDecBuffer<'static>) -> Vec<u8> {
match buffer.storage {
PlaneStorage::Internal(memory) => memory,
PlaneStorage::External(_) | PlaneStorage::Shared(_) => {
panic!("Decode allocates the buffer's memory")
}
}
}
#[must_use]
pub fn webp_decode_rgb(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<Vec<u8>> {
decode(WEBP_CSP_MODE::MODE_RGB, data, width, height).map(into_samples)
}
#[must_use]
pub fn webp_decode_rgba(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<Vec<u8>> {
decode(WEBP_CSP_MODE::MODE_RGBA, data, width, height).map(into_samples)
}
#[must_use]
pub fn webp_decode_argb(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<Vec<u8>> {
decode(WEBP_CSP_MODE::MODE_ARGB, data, width, height).map(into_samples)
}
#[must_use]
pub fn webp_decode_bgr(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<Vec<u8>> {
decode(WEBP_CSP_MODE::MODE_BGR, data, width, height).map(into_samples)
}
#[must_use]
pub fn webp_decode_bgra(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<Vec<u8>> {
decode(WEBP_CSP_MODE::MODE_BGRA, data, width, height).map(into_samples)
}
#[must_use]
pub fn webp_decode_yuv(
data: &[u8],
width: Option<&mut i32>,
height: Option<&mut i32>,
) -> Option<WebPDecBuffer<'static>> {
let output = decode(WEBP_CSP_MODE::MODE_YUV, data, width, height)?;
if let WebPDecBufferPlanes::YUVA(buf) = &output.u {
debug_assert_eq!(buf.u_stride, buf.v_stride);
}
Some(output)
}
fn default_features(features: &mut WebPBitstreamFeatures) {
*features = WebPBitstreamFeatures::default();
}
fn get_features(data: &[u8], features: &mut WebPBitstreamFeatures) -> Result<(), VP8StatusCode> {
default_features(features);
parse_headers_internal(
data,
Some(&mut features.width),
Some(&mut features.height),
Some(&mut features.has_alpha),
Some(&mut features.has_animation),
Some(&mut features.format),
None,
)
}
#[must_use]
pub fn webp_get_info(data: &[u8], width: Option<&mut i32>, height: Option<&mut i32>) -> bool {
let mut features = WebPBitstreamFeatures::default();
if get_features(data, &mut features).is_err() {
return false;
}
if let Some(width) = width {
*width = features.width;
}
if let Some(height) = height {
*height = features.height;
}
true
}
#[must_use]
#[allow(clippy::needless_bool, reason = "the C early return")]
pub fn webp_init_decoder_config_internal(config: &mut WebPDecoderConfig<'_>, version: i32) -> bool {
if webp_abi_is_incompatible(version, WEBP_DECODER_ABI_VERSION) {
return false; }
*config = WebPDecoderConfig::default();
default_features(&mut config.input);
if !webp_init_dec_buffer(&mut config.output) {
return false;
}
true
}
#[must_use]
#[allow(clippy::needless_bool, reason = "the C early return")]
pub fn webp_validate_decoder_config(config: &WebPDecoderConfig<'_>) -> bool {
if !crate::dec::buffer_dec::is_valid_colorspace(config.output.colorspace.0.cast_signed()) {
return false;
}
let options = &config.options;
if options.use_cropping != 0
&& !webp_check_crop_dimensions_basic(
options.crop_left,
options.crop_top,
options.crop_width,
options.crop_height,
)
{
return false;
}
if options.use_scaling != 0
&& (options.scaled_width < 0
|| options.scaled_height < 0
|| (options.scaled_width == 0 && options.scaled_height == 0))
{
return false;
}
if config.input.width > 0 || config.input.height > 0 {
let mut scaled_width = options.scaled_width;
let mut scaled_height = options.scaled_height;
if options.use_cropping != 0
&& !webp_check_crop_dimensions(
config.input.width,
config.input.height,
options.crop_left,
options.crop_top,
options.crop_width,
options.crop_height,
)
{
return false;
}
if options.use_scaling != 0
&& !webp_rescaler_get_scaled_dimensions(
config.input.width,
config.input.height,
&mut scaled_width,
&mut scaled_height,
)
{
return false;
}
}
if options.dithering_strength < 0
|| options.dithering_strength > 100
|| options.alpha_dithering_strength < 0
|| options.alpha_dithering_strength > 100
{
return false;
}
true
}
pub fn webp_get_features_internal(
data: &[u8],
features: &mut WebPBitstreamFeatures,
version: i32,
) -> Result<(), VP8StatusCode> {
if webp_abi_is_incompatible(version, WEBP_DECODER_ABI_VERSION) {
return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM); }
get_features(data, features)
}
pub fn webp_decode(data: &[u8], config: &mut WebPDecoderConfig<'_>) -> Result<(), VP8StatusCode> {
let mut params = WebPDecParams::default();
if let Err(status) = get_features(data, &mut config.input) {
if status == VP8StatusCode::VP8_STATUS_NOT_ENOUGH_DATA {
return Err(VP8StatusCode::VP8_STATUS_BITSTREAM_ERROR); }
return Err(status);
}
webp_reset_dec_params(&mut params);
params.options = Some(&config.options);
if webp_avoid_slow_memory(&config.output, Some(&config.input)) {
let mut in_mem_buffer = WebPDecBuffer::default();
if !webp_init_dec_buffer(&mut in_mem_buffer) {
return Err(VP8StatusCode::VP8_STATUS_INVALID_PARAM);
}
in_mem_buffer.colorspace = config.output.colorspace;
in_mem_buffer.width = config.input.width;
in_mem_buffer.height = config.input.height;
let mut decoded = decode_into(data, &mut params, &mut in_mem_buffer);
if decoded.is_ok() {
decoded = webp_copy_dec_buffer_pixels(&in_mem_buffer, &mut config.output);
}
webp_free_dec_buffer(&mut in_mem_buffer);
decoded
} else {
decode_into(data, &mut params, &mut config.output)
}
}