use core::convert::TryFrom;
use core::num::NonZeroU8;
use mynd_math::{MathError, checked_add_u64, checked_mul_u64};
use crate::{
Dimensions, OutputLength, PixelLayoutError, PixelLayoutResult, PlaneLayout, SampleStorage,
checked_plane_output_len,
};
const ONE: NonZeroU8 = NonZeroU8::MIN;
const TWO: NonZeroU8 = NonZeroU8::MIN.saturating_add(1);
const THREE: NonZeroU8 = NonZeroU8::MIN.saturating_add(2);
const FOUR: NonZeroU8 = NonZeroU8::MIN.saturating_add(3);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum AlphaAssociation {
Straight,
Premultiplied,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(kani, derive(kani::Arbitrary))]
pub enum ChromaSubsampling {
Cs444,
Cs422,
Cs420,
Cs440,
Cs411,
Cs410,
}
impl ChromaSubsampling {
const fn factors(self) -> (NonZeroU8, NonZeroU8) {
match self {
Self::Cs444 => (ONE, ONE),
Self::Cs422 => (TWO, ONE),
Self::Cs420 => (TWO, TWO),
Self::Cs440 => (ONE, TWO),
Self::Cs411 => (FOUR, ONE),
Self::Cs410 => (FOUR, TWO),
}
}
#[must_use]
pub const fn horizontal_divisor(self) -> NonZeroU8 {
self.factors().0
}
#[must_use]
pub const fn vertical_divisor(self) -> NonZeroU8 {
self.factors().1
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum GrayAlphaOrder {
GrayAlpha,
AlphaGray,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum RgbOrder {
Rgb,
Bgr,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum RgbaOrder {
Rgba,
Bgra,
Argb,
Abgr,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ChromaOrder {
CbCr,
CrCb,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PixelPlane {
sample: SampleStorage,
channels: NonZeroU8,
horizontal_divisor: NonZeroU8,
vertical_divisor: NonZeroU8,
}
impl PixelPlane {
const fn new(
sample: SampleStorage,
channels: NonZeroU8,
horizontal_divisor: NonZeroU8,
vertical_divisor: NonZeroU8,
) -> Self {
Self {
sample,
channels,
horizontal_divisor,
vertical_divisor,
}
}
#[must_use]
pub const fn sample(self) -> SampleStorage {
self.sample
}
#[must_use]
pub const fn channels(self) -> NonZeroU8 {
self.channels
}
#[must_use]
pub const fn horizontal_divisor(self) -> NonZeroU8 {
self.horizontal_divisor
}
#[must_use]
pub const fn vertical_divisor(self) -> NonZeroU8 {
self.vertical_divisor
}
pub fn dimensions(self, image: Dimensions) -> PixelLayoutResult<Dimensions> {
let width = divided_ceil(image.width(), self.horizontal_divisor);
let height = divided_ceil(image.height(), self.vertical_divisor);
Dimensions::new(width, height).map_err(PixelLayoutError::Geometry)
}
pub fn row_bytes(self, image: Dimensions) -> PixelLayoutResult<u64> {
let samples = u64::from(divided_ceil(image.width(), self.horizontal_divisor));
let samples = checked_mul_u64(samples, u64::from(self.channels.get()))
.map_err(PixelLayoutError::Arithmetic)?;
let bits = checked_mul_u64(samples, u64::from(self.sample.storage_bits()))
.map_err(PixelLayoutError::Arithmetic)?;
let rounded = checked_add_u64(bits, 7).map_err(PixelLayoutError::Arithmetic)?;
Ok(rounded / 8)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PixelLayout {
Gray {
sample: SampleStorage,
},
GrayAlphaInterleaved {
sample: SampleStorage,
order: GrayAlphaOrder,
alpha: AlphaAssociation,
},
GrayAlphaPlanar {
gray: SampleStorage,
alpha_sample: SampleStorage,
alpha: AlphaAssociation,
},
RgbInterleaved {
sample: SampleStorage,
order: RgbOrder,
},
RgbPlanar {
sample: SampleStorage,
},
RgbaInterleaved {
sample: SampleStorage,
order: RgbaOrder,
alpha: AlphaAssociation,
},
RgbaPlanar {
color: SampleStorage,
alpha_sample: SampleStorage,
alpha: AlphaAssociation,
},
YcbcrPlanar {
luma: SampleStorage,
chroma: SampleStorage,
subsampling: ChromaSubsampling,
},
YcbcrSemiPlanar {
luma: SampleStorage,
chroma: SampleStorage,
subsampling: ChromaSubsampling,
order: ChromaOrder,
},
YcbcrAlphaPlanar {
luma: SampleStorage,
chroma: SampleStorage,
alpha_sample: SampleStorage,
subsampling: ChromaSubsampling,
alpha: AlphaAssociation,
},
YcbcrAlphaSemiPlanar {
luma: SampleStorage,
chroma: SampleStorage,
alpha_sample: SampleStorage,
subsampling: ChromaSubsampling,
order: ChromaOrder,
alpha: AlphaAssociation,
},
}
impl PixelLayout {
#[must_use]
pub const fn plane_count(self) -> u8 {
match self {
Self::Gray { .. }
| Self::GrayAlphaInterleaved { .. }
| Self::RgbInterleaved { .. }
| Self::RgbaInterleaved { .. } => 1,
Self::GrayAlphaPlanar { .. } | Self::YcbcrSemiPlanar { .. } => 2,
Self::RgbPlanar { .. }
| Self::YcbcrPlanar { .. }
| Self::YcbcrAlphaSemiPlanar { .. } => 3,
Self::RgbaPlanar { .. } | Self::YcbcrAlphaPlanar { .. } => 4,
}
}
#[must_use]
pub const fn alpha_association(self) -> Option<AlphaAssociation> {
match self {
Self::GrayAlphaInterleaved { alpha, .. }
| Self::GrayAlphaPlanar { alpha, .. }
| Self::RgbaInterleaved { alpha, .. }
| Self::RgbaPlanar { alpha, .. }
| Self::YcbcrAlphaPlanar { alpha, .. }
| Self::YcbcrAlphaSemiPlanar { alpha, .. } => Some(alpha),
Self::Gray { .. }
| Self::RgbInterleaved { .. }
| Self::RgbPlanar { .. }
| Self::YcbcrPlanar { .. }
| Self::YcbcrSemiPlanar { .. } => None,
}
}
#[must_use]
pub const fn chroma_subsampling(self) -> Option<ChromaSubsampling> {
match self {
Self::YcbcrPlanar { subsampling, .. }
| Self::YcbcrSemiPlanar { subsampling, .. }
| Self::YcbcrAlphaPlanar { subsampling, .. }
| Self::YcbcrAlphaSemiPlanar { subsampling, .. } => Some(subsampling),
Self::Gray { .. }
| Self::GrayAlphaInterleaved { .. }
| Self::GrayAlphaPlanar { .. }
| Self::RgbInterleaved { .. }
| Self::RgbPlanar { .. }
| Self::RgbaInterleaved { .. }
| Self::RgbaPlanar { .. } => None,
}
}
#[must_use]
pub const fn plane(self, index: u8) -> Option<PixelPlane> {
match self {
Self::Gray { sample } if index == 0 => Some(full(sample, ONE)),
Self::GrayAlphaInterleaved { sample, .. } if index == 0 => Some(full(sample, TWO)),
Self::GrayAlphaPlanar {
gray, alpha_sample, ..
} => planar_two(index, gray, alpha_sample),
Self::RgbInterleaved { sample, .. } if index == 0 => Some(full(sample, THREE)),
Self::RgbPlanar { sample } => same_full(index, sample, THREE),
Self::RgbaInterleaved { sample, .. } if index == 0 => Some(full(sample, FOUR)),
Self::RgbaPlanar {
color,
alpha_sample,
..
} => rgba_planar(index, color, alpha_sample),
Self::YcbcrPlanar {
luma,
chroma,
subsampling,
} => ycbcr_planar(index, luma, chroma, subsampling, None),
Self::YcbcrSemiPlanar {
luma,
chroma,
subsampling,
..
} => ycbcr_semi(index, luma, chroma, subsampling, None),
Self::YcbcrAlphaPlanar {
luma,
chroma,
alpha_sample,
subsampling,
..
} => ycbcr_planar(index, luma, chroma, subsampling, Some(alpha_sample)),
Self::YcbcrAlphaSemiPlanar {
luma,
chroma,
alpha_sample,
subsampling,
..
} => ycbcr_semi(index, luma, chroma, subsampling, Some(alpha_sample)),
_ => None,
}
}
pub fn validate_planes(
self,
image: Dimensions,
planes: &[PlaneLayout],
) -> PixelLayoutResult<OutputLength> {
if planes.len() != usize::from(self.plane_count()) {
return Err(PixelLayoutError::PlaneCountMismatch);
}
for (index, plane) in planes.iter().enumerate() {
let index = u8::try_from(index)
.map_err(|_| PixelLayoutError::Arithmetic(MathError::ConversionOutOfRange))?;
let expected = self
.plane(index)
.ok_or(PixelLayoutError::PlaneCountMismatch)?;
if plane.rows() != expected.dimensions(image)?.height() {
return Err(PixelLayoutError::PlaneRowsMismatch);
}
let row_bytes = u64::try_from(plane.row_bytes())
.map_err(|_| PixelLayoutError::Arithmetic(MathError::ConversionOutOfRange))?;
if row_bytes != expected.row_bytes(image)? {
return Err(PixelLayoutError::PlaneRowBytesMismatch);
}
}
checked_plane_output_len(planes).map_err(PixelLayoutError::Geometry)
}
}
const fn full(sample: SampleStorage, channels: NonZeroU8) -> PixelPlane {
PixelPlane::new(sample, channels, ONE, ONE)
}
const fn planar_two(index: u8, first: SampleStorage, second: SampleStorage) -> Option<PixelPlane> {
match index {
0 => Some(full(first, ONE)),
1 => Some(full(second, ONE)),
_ => None,
}
}
const fn same_full(index: u8, sample: SampleStorage, count: NonZeroU8) -> Option<PixelPlane> {
if index < count.get() {
Some(full(sample, ONE))
} else {
None
}
}
const fn rgba_planar(index: u8, color: SampleStorage, alpha: SampleStorage) -> Option<PixelPlane> {
match index {
0..=2 => Some(full(color, ONE)),
3 => Some(full(alpha, ONE)),
_ => None,
}
}
const fn ycbcr_planar(
index: u8,
luma: SampleStorage,
chroma: SampleStorage,
subsampling: ChromaSubsampling,
alpha: Option<SampleStorage>,
) -> Option<PixelPlane> {
let (horizontal, vertical) = subsampling.factors();
match index {
0 => Some(full(luma, ONE)),
1 | 2 => Some(PixelPlane::new(chroma, ONE, horizontal, vertical)),
3 => match alpha {
Some(sample) => Some(full(sample, ONE)),
None => None,
},
_ => None,
}
}
const fn ycbcr_semi(
index: u8,
luma: SampleStorage,
chroma: SampleStorage,
subsampling: ChromaSubsampling,
alpha: Option<SampleStorage>,
) -> Option<PixelPlane> {
let (horizontal, vertical) = subsampling.factors();
match index {
0 => Some(full(luma, ONE)),
1 => Some(PixelPlane::new(chroma, TWO, horizontal, vertical)),
2 => match alpha {
Some(sample) => Some(full(sample, ONE)),
None => None,
},
_ => None,
}
}
fn divided_ceil(value: u32, divisor: NonZeroU8) -> u32 {
value.div_ceil(u32::from(divisor.get()))
}