use crate::{
DynamicImageOwned, DynamicImageRef, GenericImageOwned, GenericImageRef, ImageProps, PixelData,
};
use super::{ImageSpec, Pipeline, PipelineError, Runner, Strategy};
pub trait Frame: ImageProps {
fn as_bytes(&self) -> &[u8];
}
impl Frame for DynamicImageRef<'_> {
fn as_bytes(&self) -> &[u8] {
self.as_raw_u8()
}
}
impl Frame for DynamicImageOwned {
fn as_bytes(&self) -> &[u8] {
self.as_raw_u8()
}
}
pub trait ApplyInput {
type Output;
#[doc(hidden)]
fn run_pipeline(&self, pipeline: &Pipeline) -> Result<Self::Output, PipelineError>;
}
fn apply_dynamic<F: Frame + ?Sized>(
pipeline: &Pipeline,
frame: &F,
) -> Result<DynamicImageOwned, PipelineError> {
let mut runner: Runner =
pipeline.compile(ImageSpec::from_dynamic(frame), Strategy::Sequential)?;
let out = runner.run(frame)?;
Ok(DynamicImageOwned::from(&out))
}
impl ApplyInput for DynamicImageRef<'_> {
type Output = DynamicImageOwned;
fn run_pipeline(&self, pipeline: &Pipeline) -> Result<Self::Output, PipelineError> {
apply_dynamic(pipeline, self)
}
}
impl ApplyInput for DynamicImageOwned {
type Output = DynamicImageOwned;
fn run_pipeline(&self, pipeline: &Pipeline) -> Result<Self::Output, PipelineError> {
apply_dynamic(pipeline, self)
}
}
impl ApplyInput for GenericImageRef<'_> {
type Output = GenericImageOwned;
fn run_pipeline(&self, pipeline: &Pipeline) -> Result<Self::Output, PipelineError> {
Ok(GenericImageOwned {
metadata: self.metadata.clone(),
image: apply_dynamic(pipeline, self.image())?,
})
}
}
impl ApplyInput for GenericImageOwned {
type Output = GenericImageOwned;
fn run_pipeline(&self, pipeline: &Pipeline) -> Result<Self::Output, PipelineError> {
Ok(GenericImageOwned {
metadata: self.metadata.clone(),
image: apply_dynamic(pipeline, self.image())?,
})
}
}