use image::DynamicImage;
use crate::Error;
use crate::config::EncoderConfig;
#[cfg(feature = "anim-apng")]
use crate::converter::apng::ApngEncoder;
use crate::converter::avif::AvifEncoder;
use crate::converter::gif::GifEncoder;
#[cfg(feature = "jxl")]
use crate::converter::jxl::JxlEncoder;
use crate::converter::mozjpeg::MozjpegEncoder;
#[cfg(feature = "opt-oxipng")]
use crate::converter::oxipng::OxipngEncoder;
use crate::converter::png::PngEncoder;
use crate::converter::webp::WebpEncoder;
#[cfg(feature = "anim-webp")]
use crate::converter::webp_anim::WebpAnimEncoder;
use crate::converter::webp_image::WebpImageEncoder;
use crate::format::ImageFormat;
use crate::input::SourceImage;
#[derive(Clone, Copy, Debug)]
pub struct ThreadBudget {
pub files: usize,
pub threads_per_encoder: Option<usize>,
}
impl ThreadBudget {
#[must_use]
pub fn global() -> Self {
let available = std::thread::available_parallelism().map_or(1, |n| n.get());
ThreadBudget {
files: available,
threads_per_encoder: None,
}
}
#[must_use]
pub fn per_files(total_files: usize) -> Self {
let mut budget = ThreadBudget::global();
let in_flight = total_files.min(budget.files).max(1);
budget.threads_per_encoder = Some((budget.files / in_flight).max(1));
budget
}
}
pub trait ImageEncoder: Send + Sync {
fn format(&self) -> ImageFormat;
fn extension(&self) -> &'static str;
fn describe(&self) -> String;
fn describe_options(&self) -> String;
fn encode_still_image(&self, image: &DynamicImage) -> Result<Vec<u8>, Error>;
fn encode_still_image_with_metadata(
&self,
image: &DynamicImage,
metadata: &crate::metadata::ImageMetadata,
) -> Result<Vec<u8>, Error> {
let _ = metadata;
self.encode_still_image(image)
}
fn encode(&self, input: &SourceImage) -> Result<Vec<u8>, Error> {
match &input.content {
crate::input::ImageContent::Still(image) => {
self.encode_still_image_with_metadata(image, &input.metadata)
}
crate::input::ImageContent::Animated(animation) => {
let first = animation.first_frame().ok_or_else(|| {
Error::from_string("Animation does not contain any frames".to_string())
})?;
println!(
"Warning: {} cannot encode animated input; encoding the first frame only (use --animated-input error to reject such files)",
self.describe()
);
self.encode_still_image_with_metadata(
&DynamicImage::ImageRgba8(first.buffer.clone()),
&input.metadata,
)
}
}
}
fn supports_animation(&self) -> bool {
false
}
fn huge_image_hint(&self) -> Option<&'static str> {
None
}
fn supports_metadata(&self) -> bool {
true
}
fn adjust_threading(&mut self, _budget: ThreadBudget) {}
}
pub struct EncoderRegistry;
impl EncoderRegistry {
#[must_use]
pub fn build(config: &EncoderConfig, budget: ThreadBudget) -> Box<dyn ImageEncoder> {
let mut encoder: Box<dyn ImageEncoder> = match config {
EncoderConfig::Webp(options) => Box::new(WebpEncoder::new(*options)),
EncoderConfig::WebpImage => Box::new(WebpImageEncoder::new()),
EncoderConfig::Avif(options) => Box::new(AvifEncoder::new(*options)),
EncoderConfig::Png(options) => Box::new(PngEncoder::new(*options)),
EncoderConfig::Jpeg => Box::new(MozjpegEncoder::new()),
#[cfg(feature = "jxl")]
EncoderConfig::Jxl(options) => Box::new(JxlEncoder::new(options.clone())),
#[cfg(feature = "opt-oxipng")]
EncoderConfig::Oxipng(options) => Box::new(OxipngEncoder::new(options.clone())),
#[cfg(feature = "anim-webp")]
EncoderConfig::WebpAnim(options) => Box::new(WebpAnimEncoder::new(*options)),
#[cfg(feature = "anim-apng")]
EncoderConfig::Apng(options) => Box::new(ApngEncoder::new(*options)),
EncoderConfig::Gif(options) => Box::new(GifEncoder::new(*options)),
};
encoder.adjust_threading(budget);
encoder
}
}