use alloc::boxed::Box;
use crate::format::ImageFormat;
use crate::{EncodeCapabilities, Metadata, ResourceLimits};
use zenpixels::PixelDescriptor;
use super::BoxedError;
use super::dyn_encoding::{AnimationFrameEncoderShim, DynAnimationFrameEncoder, DynEncoder};
use super::encoder::{AnimationFrameEncoder, Encoder};
pub trait EncoderConfig: Clone + Send + Sync {
type Error: core::error::Error + Send + Sync + 'static;
type Job: EncodeJob<Error = Self::Error>;
fn format() -> ImageFormat;
fn supported_descriptors() -> &'static [PixelDescriptor];
fn capabilities() -> &'static EncodeCapabilities {
&EncodeCapabilities::EMPTY
}
fn with_generic_quality(self, _quality: f32) -> Self {
self
}
fn with_generic_effort(self, _effort: i32) -> Self {
self
}
fn with_lossless(self, _lossless: bool) -> Self {
self
}
fn with_alpha_quality(self, _quality: f32) -> Self {
self
}
fn generic_quality(&self) -> Option<f32> {
None
}
fn generic_effort(&self) -> Option<i32> {
None
}
fn is_lossless(&self) -> Option<bool> {
None
}
fn alpha_quality(&self) -> Option<f32> {
None
}
fn job(self) -> Self::Job;
}
pub trait EncodeJob: Sized {
type Error: core::error::Error + Send + Sync + 'static;
type Enc: Sized + 'static;
type AnimationFrameEnc: Sized + Send + 'static;
fn with_stop(self, stop: crate::StopToken) -> Self;
fn with_limits(self, limits: ResourceLimits) -> Self;
fn with_policy(self, _policy: crate::EncodePolicy) -> Self {
self
}
fn with_metadata(self, meta: Metadata) -> Self;
fn with_canvas_size(self, _width: u32, _height: u32) -> Self {
self
}
fn with_loop_count(self, _count: Option<u32>) -> Self {
self
}
fn extensions(&self) -> Option<&dyn core::any::Any> {
None
}
fn extensions_mut(&mut self) -> Option<&mut dyn core::any::Any> {
None
}
fn encoder(self) -> Result<Self::Enc, Self::Error>;
fn animation_frame_encoder(self) -> Result<Self::AnimationFrameEnc, Self::Error>;
fn dyn_encoder(self) -> Result<Box<dyn DynEncoder>, BoxedError>
where
Self::Enc: Encoder + Send,
{
let enc = self.encoder().map_err(|e| Box::new(e) as BoxedError)?;
Ok(Box::new(super::dyn_encoding::EncoderShim(enc)))
}
fn dyn_animation_frame_encoder(self) -> Result<Box<dyn DynAnimationFrameEncoder>, BoxedError>
where
Self::AnimationFrameEnc: AnimationFrameEncoder + Send,
{
let enc = self
.animation_frame_encoder()
.map_err(|e| Box::new(e) as BoxedError)?;
Ok(Box::new(AnimationFrameEncoderShim(enc)))
}
}