#[cfg(all(test, feature = "yuv-planar"))]
mod tests;
#[cfg(all(test, feature = "frame"))]
mod rgb_u16_tests;
#[cfg(all(
test,
any(feature = "std", feature = "alloc"),
any(
feature = "xyz",
feature = "mono",
feature = "yuv-planar",
feature = "yuv-semi-planar",
feature = "yuv-packed",
feature = "yuv-444-packed",
feature = "y2xx",
feature = "v210",
feature = "yuva",
feature = "rgb",
feature = "rgb-float",
feature = "rgb-legacy",
feature = "gray",
feature = "gbr",
)
))]
mod source_coverage;
use crate::{
SourceFormat,
resample::{AreaResampler, FilterKernel, FilteredResampler, NoopResampler, Resampler},
sinker::{MixedSinker, MixedSinkerError},
walker::{ColorSpec, YuvOptions},
};
#[cfg(feature = "yuv-planar")]
use crate::{ChromaLocation, sinker::St428Interpretation};
#[allow(dead_code)]
#[derive(Clone, Copy)]
pub(crate) enum Faceless {}
#[allow(dead_code)]
pub(crate) trait FacelessSink {
fn set_rgb_u16(&mut self, faceless: Faceless) {
match faceless {}
}
fn with_rgb_u16(self, faceless: Faceless) -> Self
where
Self: Sized,
{
match faceless {}
}
}
impl<T: ?Sized> FacelessSink for T {}
pub(crate) mod sealed {
pub trait Sealed {}
pub trait SealedOptions {}
}
pub trait FromSpec: Default + sealed::SealedOptions {
fn from_spec(spec: &ColorSpec) -> Result<Self, crate::UnsupportedKernelMatrixError>;
}
impl sealed::SealedOptions for YuvOptions {}
impl FromSpec for YuvOptions {
#[inline]
fn from_spec(spec: &ColorSpec) -> Result<Self, crate::UnsupportedKernelMatrixError> {
Ok(Self::from_color_spec(spec))
}
}
impl sealed::SealedOptions for () {}
impl FromSpec for () {
#[inline]
fn from_spec(_spec: &ColorSpec) -> Result<Self, crate::UnsupportedKernelMatrixError> {
Ok(())
}
}
pub trait Source: sealed::Sealed {
type Marker: SourceFormat;
type Options: FromSpec;
fn dimensions(&self) -> (usize, usize);
#[doc(hidden)]
fn walk_mixed<R>(
&self,
opts: &Self::Options,
sink: &mut MixedSinker<'_, Self::Marker, R>,
) -> Result<(), MixedSinkerError>
where
R: Resampler;
#[doc(hidden)]
fn attach_rgba<'sink, R>(
sink: &mut MixedSinker<'sink, Self::Marker, R>,
buf: &'sink mut [u8],
) -> Result<(), MixedSinkerError>
where
R: Resampler;
#[doc(hidden)]
fn attach_rgb_u16<'sink, R>(
sink: &mut MixedSinker<'sink, Self::Marker, R>,
buf: &'sink mut [u16],
) -> Result<(), MixedSinkerError>
where
R: Resampler;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RgbDepths {
red: u32,
green: u32,
blue: u32,
}
impl RgbDepths {
#[cfg_attr(not(tarpaulin), inline(always))]
#[must_use]
pub const fn new(red: u32, green: u32, blue: u32) -> Self {
Self { red, green, blue }
}
#[cfg_attr(not(tarpaulin), inline(always))]
#[must_use]
pub const fn splat(depth: u32) -> Self {
Self::new(depth, depth, depth)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn red(self) -> u32 {
self.red
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn green(self) -> u32 {
self.green
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub const fn blue(self) -> u32 {
self.blue
}
#[cfg_attr(not(tarpaulin), inline(always))]
#[must_use]
pub const fn uniform(self) -> Option<u32> {
if self.red == self.green && self.green == self.blue {
Some(self.red)
} else {
None
}
}
}
pub trait RgbU16Source: Source {
const RGB_U16_DEPTHS: RgbDepths;
}
pub struct Convert<'a, Fr: Source, R = NoopResampler> {
frame: &'a Fr,
resampler: R,
rgb: Option<&'a mut [u8]>,
rgba: Option<&'a mut [u8]>,
luma: Option<&'a mut [u8]>,
rgb_u16: Option<&'a mut [u16]>,
#[cfg(feature = "yuv-planar")]
hsv: Option<(&'a mut [u8], &'a mut [u8], &'a mut [u8])>,
spec: Option<ColorSpec>,
format_options: Option<Fr::Options>,
simd: bool,
#[cfg(feature = "yuv-planar")]
chroma_location: Option<ChromaLocation>,
#[cfg(feature = "yuv-planar")]
st428: Option<St428Interpretation>,
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
native: Option<bool>,
#[cfg(feature = "xyz")]
target_gamut: Option<crate::DcpTargetGamut>,
}
impl<'a, Fr: Source> From<&'a Fr> for Convert<'a, Fr, NoopResampler> {
#[inline]
fn from(frame: &'a Fr) -> Self {
Convert {
frame,
resampler: NoopResampler,
rgb: None,
rgba: None,
luma: None,
rgb_u16: None,
#[cfg(feature = "yuv-planar")]
hsv: None,
spec: None,
format_options: None,
simd: true,
#[cfg(feature = "yuv-planar")]
chroma_location: None,
#[cfg(feature = "yuv-planar")]
st428: None,
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
native: None,
#[cfg(feature = "xyz")]
target_gamut: None,
}
}
}
#[allow(dead_code)]
pub(crate) fn convert_is_covariant<'short, 'long: 'short, Fr: Source + 'long, R>(
convert: Convert<'long, Fr, R>,
) -> Convert<'short, Fr, R> {
convert
}
#[cfg(all(feature = "yuv-planar", any(feature = "std", feature = "alloc")))]
#[allow(dead_code)]
pub(crate) fn faced_and_faceless_convert_shorten<'short, 'long: 'short, 'frame: 'long>(
faced: Convert<'long, crate::frame::Yuv420p10Frame<'frame>>,
faceless: Convert<'long, crate::frame::Yuv420pFrame<'frame>>,
) -> (
Convert<'short, crate::frame::Yuv420p10Frame<'frame>>,
Convert<'short, crate::frame::Yuv420pFrame<'frame>>,
) {
(faced, faceless)
}
impl<'a, Fr: Source, R> Convert<'a, Fr, R> {
#[inline]
fn rebind_resampler<R2>(self, resampler: R2) -> Convert<'a, Fr, R2> {
Convert {
frame: self.frame,
resampler,
rgb: self.rgb,
rgba: self.rgba,
luma: self.luma,
rgb_u16: self.rgb_u16,
#[cfg(feature = "yuv-planar")]
hsv: self.hsv,
spec: self.spec,
format_options: self.format_options,
simd: self.simd,
#[cfg(feature = "yuv-planar")]
chroma_location: self.chroma_location,
#[cfg(feature = "yuv-planar")]
st428: self.st428,
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
native: self.native,
#[cfg(feature = "xyz")]
target_gamut: self.target_gamut,
}
}
#[must_use]
#[inline]
pub fn spec(mut self, spec: ColorSpec) -> Self {
self.spec = Some(spec);
self
}
#[must_use]
#[inline]
pub fn format_options(mut self, options: Fr::Options) -> Self {
self.format_options = Some(options);
self
}
#[must_use]
#[inline]
pub fn rgb(mut self, buf: &'a mut [u8]) -> Self {
self.rgb = Some(buf);
self
}
#[must_use]
#[inline]
pub fn rgba(mut self, buf: &'a mut [u8]) -> Self {
self.rgba = Some(buf);
self
}
#[must_use]
#[inline]
pub fn luma(mut self, buf: &'a mut [u8]) -> Self {
self.luma = Some(buf);
self
}
#[cfg(feature = "yuv-planar")]
#[must_use]
#[inline]
pub fn hsv(mut self, h: &'a mut [u8], s: &'a mut [u8], v: &'a mut [u8]) -> Self {
self.hsv = Some((h, s, v));
self
}
#[must_use]
#[inline]
pub fn simd(mut self, simd: bool) -> Self {
self.simd = simd;
self
}
#[cfg(feature = "yuv-planar")]
#[must_use]
#[inline]
pub fn chroma_location(mut self, loc: ChromaLocation) -> Self {
self.chroma_location = Some(loc);
self
}
#[cfg(feature = "yuv-planar")]
#[must_use]
#[inline]
pub fn st428_interpretation(mut self, interp: St428Interpretation) -> Self {
self.st428 = Some(interp);
self
}
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
#[must_use]
#[inline]
pub fn native(mut self, native: bool) -> Self {
self.native = Some(native);
self
}
}
impl<'a, Fr: RgbU16Source, R> Convert<'a, Fr, R> {
#[must_use]
#[inline]
pub fn rgb_u16(mut self, buf: &'a mut [u16]) -> Self {
self.rgb_u16 = Some(buf);
self
}
}
#[cfg(all(feature = "xyz", any(feature = "std", feature = "alloc")))]
impl<const BE: bool, R> Convert<'_, crate::frame::Xyz12Frame<'_, BE>, R> {
#[must_use]
#[inline]
pub fn target_gamut(mut self, gamut: crate::DcpTargetGamut) -> Self {
self.target_gamut = Some(gamut);
self
}
}
impl<'a, Fr: Source> Convert<'a, Fr, NoopResampler> {
#[must_use]
#[inline]
pub fn resize(self, out_width: usize, out_height: usize) -> Convert<'a, Fr, AreaResampler> {
self.rebind_resampler(AreaResampler::to(out_width, out_height))
}
#[must_use]
#[inline]
pub fn resize_with<K: FilterKernel>(
self,
kernel: K,
out_width: usize,
out_height: usize,
) -> Convert<'a, Fr, FilteredResampler<K>> {
self.rebind_resampler(FilteredResampler::new(out_width, out_height, kernel))
}
}
impl<Fr: Source, R: Resampler> Convert<'_, Fr, R> {
pub fn run(self) -> Result<(), MixedSinkerError> {
let (w, h) = self.frame.dimensions();
let mut sink = MixedSinker::<Fr::Marker, R>::with_resampler(w, h, self.resampler)?;
if let Some(buf) = self.rgb {
sink.set_rgb(buf)?;
}
if let Some(buf) = self.luma {
sink.set_luma(buf)?;
}
#[cfg(feature = "yuv-planar")]
if let Some((h_plane, s_plane, v_plane)) = self.hsv {
sink.set_hsv(h_plane, s_plane, v_plane)?;
}
if let Some(buf) = self.rgba {
Fr::attach_rgba(&mut sink, buf)?;
}
if let Some(buf) = self.rgb_u16 {
Fr::attach_rgb_u16(&mut sink, buf)?;
}
sink.set_simd(self.simd);
if let Some(spec) = self.spec.as_ref() {
sink.set_color_spec(spec)?;
}
#[cfg(feature = "xyz")]
if let Some(gamut) = self.target_gamut.as_ref() {
sink.set_target_gamut(gamut)?;
}
#[cfg(feature = "yuv-planar")]
{
if let Some(loc) = self.chroma_location {
sink.set_chroma_location(loc.clone());
}
if let Some(interp) = self.st428 {
sink.set_st428_interpretation(interp);
}
}
#[cfg(any(feature = "yuv-planar", feature = "yuv-semi-planar"))]
if let Some(native) = self.native {
sink.set_native(native);
}
let options = match self.format_options {
Some(options) => options,
None => match self.spec.as_ref() {
Some(spec) => Fr::Options::from_spec(spec)?,
None => Fr::Options::default(),
},
};
self.frame.walk_mixed(&options, &mut sink)
}
}