use crate::webp::encode::{WEBP_ENCODER_ABI_VERSION, WebPConfig, WebPImageHint, WebPPreset};
use crate::webp::types::webp_abi_is_incompatible;
pub fn webp_config_init_internal(
config: &mut WebPConfig,
preset: WebPPreset,
quality: f32,
version: i32,
) -> bool {
if webp_abi_is_incompatible(version, WEBP_ENCODER_ABI_VERSION) {
return false; }
config.quality = quality;
config.target_size = 0;
config.target_psnr = 0.0;
config.method = 4;
config.sns_strength = 50;
config.filter_strength = 60; config.filter_sharpness = 0;
config.filter_type = 1; config.partitions = 0;
config.segments = 4;
config.pass = 1;
config.qmin = 0;
config.qmax = 100;
config.show_compressed = 0;
config.preprocessing = 0;
config.autofilter = 0;
config.partition_limit = 0;
config.alpha_compression = 1;
config.alpha_filtering = 1;
config.alpha_quality = 100;
config.lossless = 0;
config.exact = 0;
config.image_hint = WebPImageHint::WEBP_HINT_DEFAULT;
config.emulate_jpeg_size = 0;
config.thread_level = 0;
config.low_memory = 0;
config.near_lossless = 100;
config.use_sharp_yuv = 0;
match preset {
WebPPreset::WEBP_PRESET_PICTURE => {
config.sns_strength = 80;
config.filter_sharpness = 4;
config.filter_strength = 35;
config.preprocessing &= !2; }
WebPPreset::WEBP_PRESET_PHOTO => {
config.sns_strength = 80;
config.filter_sharpness = 3;
config.filter_strength = 30;
config.preprocessing |= 2;
}
WebPPreset::WEBP_PRESET_DRAWING => {
config.sns_strength = 25;
config.filter_sharpness = 6;
config.filter_strength = 10;
}
WebPPreset::WEBP_PRESET_ICON => {
config.sns_strength = 0;
config.filter_strength = 0; config.preprocessing &= !2; }
WebPPreset::WEBP_PRESET_TEXT => {
config.sns_strength = 0;
config.filter_strength = 0; config.preprocessing &= !2; config.segments = 2;
}
_ => {}
}
webp_validate_config(config)
}
#[must_use]
pub fn webp_validate_config(config: &WebPConfig) -> bool {
if config.quality < 0.0 || config.quality > 100.0 {
return false;
}
if config.target_size < 0 {
return false;
}
if config.target_psnr < 0.0 {
return false;
}
if config.method < 0 || config.method > 6 {
return false;
}
if config.segments < 1 || config.segments > 4 {
return false;
}
if config.sns_strength < 0 || config.sns_strength > 100 {
return false;
}
if config.filter_strength < 0 || config.filter_strength > 100 {
return false;
}
if config.filter_sharpness < 0 || config.filter_sharpness > 7 {
return false;
}
if config.filter_type < 0 || config.filter_type > 1 {
return false;
}
if config.autofilter < 0 || config.autofilter > 1 {
return false;
}
if config.pass < 1 || config.pass > 10 {
return false;
}
if config.qmin < 0 || config.qmax > 100 || config.qmin > config.qmax {
return false;
}
if config.show_compressed < 0 || config.show_compressed > 1 {
return false;
}
if config.preprocessing < 0 || config.preprocessing > 7 {
return false;
}
if config.partitions < 0 || config.partitions > 3 {
return false;
}
if config.partition_limit < 0 || config.partition_limit > 100 {
return false;
}
if config.alpha_compression < 0 {
return false;
}
if config.alpha_filtering < 0 {
return false;
}
if config.alpha_quality < 0 || config.alpha_quality > 100 {
return false;
}
if config.lossless < 0 || config.lossless > 1 {
return false;
}
if config.near_lossless < 0 || config.near_lossless > 100 {
return false;
}
if config.image_hint >= WebPImageHint::WEBP_HINT_LAST {
return false;
}
if config.emulate_jpeg_size < 0 || config.emulate_jpeg_size > 1 {
return false;
}
if config.thread_level < 0 || config.thread_level > 1 {
return false;
}
if config.low_memory < 0 || config.low_memory > 1 {
return false;
}
if config.exact < 0 || config.exact > 1 {
return false;
}
if config.use_sharp_yuv < 0 || config.use_sharp_yuv > 1 {
return false;
}
true
}
const MAX_LEVEL: i32 = 9;
const LOSSLESS_PRESETS: [(u8, u8); (MAX_LEVEL + 1) as usize] = [
(0, 0),
(1, 20),
(2, 25),
(3, 30),
(3, 50),
(4, 50),
(4, 75),
(4, 90),
(5, 90),
(6, 100),
];
#[expect(
clippy::cast_sign_loss,
reason = "the C conversion keeps the bit pattern, as the source does"
)]
pub fn webp_config_lossless_preset(config: &mut WebPConfig, level: i32) -> bool {
if !(0..=MAX_LEVEL).contains(&level) {
return false;
}
config.lossless = 1;
let (method, quality) = LOSSLESS_PRESETS[level as usize];
config.method = i32::from(method);
config.quality = f32::from(quality);
true
}