use crate::converter::{EncoderRegistry, ThreadBudget};
#[derive(Clone, Debug, serde::Serialize)]
pub struct EncoderInfo {
pub name: &'static str,
pub extension: &'static str,
pub supports_animation: bool,
pub supports_metadata: bool,
pub enabled: bool,
pub disabled_reason: Option<&'static str>,
pub description: String,
}
#[derive(Clone, Debug, serde::Serialize)]
pub struct Capabilities {
pub encoders: Vec<EncoderInfo>,
pub heif_input_enabled: bool,
}
fn live(name: &'static str, config: &crate::config::EncoderConfig) -> EncoderInfo {
let encoder = EncoderRegistry::build(
config,
ThreadBudget {
files: 1,
threads_per_encoder: Some(1),
},
);
EncoderInfo {
name,
extension: encoder.extension(),
supports_animation: encoder.supports_animation(),
supports_metadata: encoder.supports_metadata(),
enabled: true,
disabled_reason: None,
description: encoder.describe(),
}
}
#[cfg_attr(
all(
feature = "jxl",
feature = "opt-oxipng",
feature = "anim-webp",
feature = "anim-apng"
),
allow(dead_code)
)]
fn disabled(
name: &'static str,
extension: &'static str,
supports_animation: bool,
supports_metadata: bool,
reason: &'static str,
description: &str,
) -> EncoderInfo {
EncoderInfo {
name,
extension,
supports_animation,
supports_metadata,
enabled: false,
disabled_reason: Some(reason),
description: description.to_string(),
}
}
fn encoder_infos() -> Vec<EncoderInfo> {
use crate::config::EncoderConfig;
let mut infos = vec![
live(
"webp",
&EncoderConfig::Webp(crate::config::WebpOptions {
lossless: false,
quality: 90.0,
}),
),
live("webp-image", &EncoderConfig::WebpImage),
live(
"avif",
&EncoderConfig::Avif(crate::config::AvifOptions {
quality: 90.0,
speed: 3,
..crate::config::AvifOptions::default()
}),
),
live(
"png",
&EncoderConfig::Png(crate::config::PngOptions::default()),
),
live("jpeg", &EncoderConfig::Jpeg),
];
#[cfg(feature = "jxl")]
infos.push(live(
"jxl",
&EncoderConfig::Jxl(crate::config::JxlOptions::default()),
));
#[cfg(not(feature = "jxl"))]
infos.push(disabled(
"jxl",
"jxl",
true,
true,
"this build was compiled without the \"jxl\" feature",
"jpeg-xl encoder via libjxl (lossy, lossless, animation, EXIF boxes)",
));
#[cfg(feature = "opt-oxipng")]
infos.push(live(
"oxipng",
&EncoderConfig::Oxipng(crate::config::OxipngOptions::default()),
));
#[cfg(not(feature = "opt-oxipng"))]
infos.push(disabled(
"oxipng",
"png",
false,
true,
"this build was compiled without the \"opt-oxipng\" feature",
"png re-optimization / transcoding via oxipng (level 2)",
));
#[cfg(feature = "anim-webp")]
infos.push(live(
"webp-anim",
&EncoderConfig::WebpAnim(crate::config::WebpAnimOptions::default()),
));
#[cfg(not(feature = "anim-webp"))]
infos.push(disabled(
"webp-anim",
"webp",
true,
false,
"this build was compiled without the \"anim-webp\" feature",
"animated webp encoder via webp-animation (quality 90.0)",
));
#[cfg(feature = "anim-apng")]
infos.push(live(
"apng",
&EncoderConfig::Apng(crate::config::ApngOptions::default()),
));
#[cfg(not(feature = "anim-apng"))]
infos.push(disabled(
"apng",
"png",
true,
false,
"this build was compiled without the \"anim-apng\" feature",
"animated png (APNG) encoder via the png crate",
));
infos.push(live(
"gif",
&EncoderConfig::Gif(crate::config::GifOptions { speed: Some(10) }),
));
infos
}
#[must_use]
pub fn capabilities() -> Capabilities {
Capabilities {
encoders: encoder_infos(),
heif_input_enabled: cfg!(feature = "dec-heif"),
}
}