1use core::convert::TryFrom;
2use core::num::NonZeroU8;
3
4use mynd_math::{MathError, checked_add_u64, checked_mul_u64};
5
6use crate::{
7 Dimensions, OutputLength, PixelLayoutError, PixelLayoutResult, PlaneLayout, SampleStorage,
8 checked_plane_output_len,
9};
10
11const ONE: NonZeroU8 = NonZeroU8::MIN;
12const TWO: NonZeroU8 = NonZeroU8::MIN.saturating_add(1);
13const THREE: NonZeroU8 = NonZeroU8::MIN.saturating_add(2);
14const FOUR: NonZeroU8 = NonZeroU8::MIN.saturating_add(3);
15
16#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
18pub enum AlphaAssociation {
19 Straight,
21 Premultiplied,
23}
24
25#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
27#[cfg_attr(kani, derive(kani::Arbitrary))]
28pub enum ChromaSubsampling {
29 Cs444,
31 Cs422,
33 Cs420,
35 Cs440,
37 Cs411,
39 Cs410,
41}
42
43impl ChromaSubsampling {
44 const fn factors(self) -> (NonZeroU8, NonZeroU8) {
45 match self {
46 Self::Cs444 => (ONE, ONE),
47 Self::Cs422 => (TWO, ONE),
48 Self::Cs420 => (TWO, TWO),
49 Self::Cs440 => (ONE, TWO),
50 Self::Cs411 => (FOUR, ONE),
51 Self::Cs410 => (FOUR, TWO),
52 }
53 }
54
55 #[must_use]
57 pub const fn horizontal_divisor(self) -> NonZeroU8 {
58 self.factors().0
59 }
60
61 #[must_use]
63 pub const fn vertical_divisor(self) -> NonZeroU8 {
64 self.factors().1
65 }
66}
67
68#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
70pub enum GrayAlphaOrder {
71 GrayAlpha,
73 AlphaGray,
75}
76
77#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
79pub enum RgbOrder {
80 Rgb,
82 Bgr,
84}
85
86#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
88pub enum RgbaOrder {
89 Rgba,
91 Bgra,
93 Argb,
95 Abgr,
97}
98
99#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
101pub enum ChromaOrder {
102 CbCr,
104 CrCb,
106}
107
108#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
110pub struct PixelPlane {
111 sample: SampleStorage,
112 channels: NonZeroU8,
113 horizontal_divisor: NonZeroU8,
114 vertical_divisor: NonZeroU8,
115}
116
117impl PixelPlane {
118 const fn new(
119 sample: SampleStorage,
120 channels: NonZeroU8,
121 horizontal_divisor: NonZeroU8,
122 vertical_divisor: NonZeroU8,
123 ) -> Self {
124 Self {
125 sample,
126 channels,
127 horizontal_divisor,
128 vertical_divisor,
129 }
130 }
131
132 #[must_use]
134 pub const fn sample(self) -> SampleStorage {
135 self.sample
136 }
137
138 #[must_use]
140 pub const fn channels(self) -> NonZeroU8 {
141 self.channels
142 }
143
144 #[must_use]
146 pub const fn horizontal_divisor(self) -> NonZeroU8 {
147 self.horizontal_divisor
148 }
149
150 #[must_use]
152 pub const fn vertical_divisor(self) -> NonZeroU8 {
153 self.vertical_divisor
154 }
155
156 pub fn dimensions(self, image: Dimensions) -> PixelLayoutResult<Dimensions> {
158 let width = divided_ceil(image.width(), self.horizontal_divisor);
159 let height = divided_ceil(image.height(), self.vertical_divisor);
160 Dimensions::new(width, height).map_err(PixelLayoutError::Geometry)
161 }
162
163 pub fn row_bytes(self, image: Dimensions) -> PixelLayoutResult<u64> {
172 let samples = u64::from(divided_ceil(image.width(), self.horizontal_divisor));
173 let samples = checked_mul_u64(samples, u64::from(self.channels.get()))
174 .map_err(PixelLayoutError::Arithmetic)?;
175 let bits = checked_mul_u64(samples, u64::from(self.sample.storage_bits()))
176 .map_err(PixelLayoutError::Arithmetic)?;
177 let rounded = checked_add_u64(bits, 7).map_err(PixelLayoutError::Arithmetic)?;
178 Ok(rounded / 8)
179 }
180}
181
182#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
185pub enum PixelLayout {
186 Gray {
188 sample: SampleStorage,
190 },
191 GrayAlphaInterleaved {
193 sample: SampleStorage,
195 order: GrayAlphaOrder,
197 alpha: AlphaAssociation,
199 },
200 GrayAlphaPlanar {
202 gray: SampleStorage,
204 alpha_sample: SampleStorage,
206 alpha: AlphaAssociation,
208 },
209 RgbInterleaved {
211 sample: SampleStorage,
213 order: RgbOrder,
215 },
216 RgbPlanar {
218 sample: SampleStorage,
220 },
221 RgbaInterleaved {
223 sample: SampleStorage,
225 order: RgbaOrder,
227 alpha: AlphaAssociation,
229 },
230 RgbaPlanar {
232 color: SampleStorage,
234 alpha_sample: SampleStorage,
236 alpha: AlphaAssociation,
238 },
239 YcbcrPlanar {
241 luma: SampleStorage,
243 chroma: SampleStorage,
245 subsampling: ChromaSubsampling,
247 },
248 YcbcrSemiPlanar {
250 luma: SampleStorage,
252 chroma: SampleStorage,
254 subsampling: ChromaSubsampling,
256 order: ChromaOrder,
258 },
259 YcbcrAlphaPlanar {
261 luma: SampleStorage,
263 chroma: SampleStorage,
265 alpha_sample: SampleStorage,
267 subsampling: ChromaSubsampling,
269 alpha: AlphaAssociation,
271 },
272 YcbcrAlphaSemiPlanar {
274 luma: SampleStorage,
276 chroma: SampleStorage,
278 alpha_sample: SampleStorage,
280 subsampling: ChromaSubsampling,
282 order: ChromaOrder,
284 alpha: AlphaAssociation,
286 },
287}
288
289impl PixelLayout {
290 #[must_use]
292 pub const fn plane_count(self) -> u8 {
293 match self {
294 Self::Gray { .. }
295 | Self::GrayAlphaInterleaved { .. }
296 | Self::RgbInterleaved { .. }
297 | Self::RgbaInterleaved { .. } => 1,
298 Self::GrayAlphaPlanar { .. } | Self::YcbcrSemiPlanar { .. } => 2,
299 Self::RgbPlanar { .. }
300 | Self::YcbcrPlanar { .. }
301 | Self::YcbcrAlphaSemiPlanar { .. } => 3,
302 Self::RgbaPlanar { .. } | Self::YcbcrAlphaPlanar { .. } => 4,
303 }
304 }
305
306 #[must_use]
308 pub const fn alpha_association(self) -> Option<AlphaAssociation> {
309 match self {
310 Self::GrayAlphaInterleaved { alpha, .. }
311 | Self::GrayAlphaPlanar { alpha, .. }
312 | Self::RgbaInterleaved { alpha, .. }
313 | Self::RgbaPlanar { alpha, .. }
314 | Self::YcbcrAlphaPlanar { alpha, .. }
315 | Self::YcbcrAlphaSemiPlanar { alpha, .. } => Some(alpha),
316 Self::Gray { .. }
317 | Self::RgbInterleaved { .. }
318 | Self::RgbPlanar { .. }
319 | Self::YcbcrPlanar { .. }
320 | Self::YcbcrSemiPlanar { .. } => None,
321 }
322 }
323
324 #[must_use]
326 pub const fn chroma_subsampling(self) -> Option<ChromaSubsampling> {
327 match self {
328 Self::YcbcrPlanar { subsampling, .. }
329 | Self::YcbcrSemiPlanar { subsampling, .. }
330 | Self::YcbcrAlphaPlanar { subsampling, .. }
331 | Self::YcbcrAlphaSemiPlanar { subsampling, .. } => Some(subsampling),
332 Self::Gray { .. }
333 | Self::GrayAlphaInterleaved { .. }
334 | Self::GrayAlphaPlanar { .. }
335 | Self::RgbInterleaved { .. }
336 | Self::RgbPlanar { .. }
337 | Self::RgbaInterleaved { .. }
338 | Self::RgbaPlanar { .. } => None,
339 }
340 }
341
342 #[must_use]
344 pub const fn plane(self, index: u8) -> Option<PixelPlane> {
345 match self {
346 Self::Gray { sample } if index == 0 => Some(full(sample, ONE)),
347 Self::GrayAlphaInterleaved { sample, .. } if index == 0 => Some(full(sample, TWO)),
348 Self::GrayAlphaPlanar {
349 gray, alpha_sample, ..
350 } => planar_two(index, gray, alpha_sample),
351 Self::RgbInterleaved { sample, .. } if index == 0 => Some(full(sample, THREE)),
352 Self::RgbPlanar { sample } => same_full(index, sample, THREE),
353 Self::RgbaInterleaved { sample, .. } if index == 0 => Some(full(sample, FOUR)),
354 Self::RgbaPlanar {
355 color,
356 alpha_sample,
357 ..
358 } => rgba_planar(index, color, alpha_sample),
359 Self::YcbcrPlanar {
360 luma,
361 chroma,
362 subsampling,
363 } => ycbcr_planar(index, luma, chroma, subsampling, None),
364 Self::YcbcrSemiPlanar {
365 luma,
366 chroma,
367 subsampling,
368 ..
369 } => ycbcr_semi(index, luma, chroma, subsampling, None),
370 Self::YcbcrAlphaPlanar {
371 luma,
372 chroma,
373 alpha_sample,
374 subsampling,
375 ..
376 } => ycbcr_planar(index, luma, chroma, subsampling, Some(alpha_sample)),
377 Self::YcbcrAlphaSemiPlanar {
378 luma,
379 chroma,
380 alpha_sample,
381 subsampling,
382 ..
383 } => ycbcr_semi(index, luma, chroma, subsampling, Some(alpha_sample)),
384 _ => None,
385 }
386 }
387
388 pub fn validate_planes(
399 self,
400 image: Dimensions,
401 planes: &[PlaneLayout],
402 ) -> PixelLayoutResult<OutputLength> {
403 if planes.len() != usize::from(self.plane_count()) {
404 return Err(PixelLayoutError::PlaneCountMismatch);
405 }
406 for (index, plane) in planes.iter().enumerate() {
407 let index = u8::try_from(index)
408 .map_err(|_| PixelLayoutError::Arithmetic(MathError::ConversionOutOfRange))?;
409 let expected = self
410 .plane(index)
411 .ok_or(PixelLayoutError::PlaneCountMismatch)?;
412 if plane.rows() != expected.dimensions(image)?.height() {
413 return Err(PixelLayoutError::PlaneRowsMismatch);
414 }
415 let row_bytes = u64::try_from(plane.row_bytes())
416 .map_err(|_| PixelLayoutError::Arithmetic(MathError::ConversionOutOfRange))?;
417 if row_bytes != expected.row_bytes(image)? {
418 return Err(PixelLayoutError::PlaneRowBytesMismatch);
419 }
420 }
421 checked_plane_output_len(planes).map_err(PixelLayoutError::Geometry)
422 }
423}
424
425const fn full(sample: SampleStorage, channels: NonZeroU8) -> PixelPlane {
426 PixelPlane::new(sample, channels, ONE, ONE)
427}
428
429const fn planar_two(index: u8, first: SampleStorage, second: SampleStorage) -> Option<PixelPlane> {
430 match index {
431 0 => Some(full(first, ONE)),
432 1 => Some(full(second, ONE)),
433 _ => None,
434 }
435}
436
437const fn same_full(index: u8, sample: SampleStorage, count: NonZeroU8) -> Option<PixelPlane> {
438 if index < count.get() {
439 Some(full(sample, ONE))
440 } else {
441 None
442 }
443}
444
445const fn rgba_planar(index: u8, color: SampleStorage, alpha: SampleStorage) -> Option<PixelPlane> {
446 match index {
447 0..=2 => Some(full(color, ONE)),
448 3 => Some(full(alpha, ONE)),
449 _ => None,
450 }
451}
452
453const fn ycbcr_planar(
454 index: u8,
455 luma: SampleStorage,
456 chroma: SampleStorage,
457 subsampling: ChromaSubsampling,
458 alpha: Option<SampleStorage>,
459) -> Option<PixelPlane> {
460 let (horizontal, vertical) = subsampling.factors();
461 match index {
462 0 => Some(full(luma, ONE)),
463 1 | 2 => Some(PixelPlane::new(chroma, ONE, horizontal, vertical)),
464 3 => match alpha {
465 Some(sample) => Some(full(sample, ONE)),
466 None => None,
467 },
468 _ => None,
469 }
470}
471
472const fn ycbcr_semi(
473 index: u8,
474 luma: SampleStorage,
475 chroma: SampleStorage,
476 subsampling: ChromaSubsampling,
477 alpha: Option<SampleStorage>,
478) -> Option<PixelPlane> {
479 let (horizontal, vertical) = subsampling.factors();
480 match index {
481 0 => Some(full(luma, ONE)),
482 1 => Some(PixelPlane::new(chroma, TWO, horizontal, vertical)),
483 2 => match alpha {
484 Some(sample) => Some(full(sample, ONE)),
485 None => None,
486 },
487 _ => None,
488 }
489}
490
491fn divided_ceil(value: u32, divisor: NonZeroU8) -> u32 {
492 value.div_ceil(u32::from(divisor.get()))
493}