vapoursynth/
component.rs

1//! The pixel component trait.
2
3#[cfg(feature = "f16-pixel-type")]
4use half::f16;
5
6use crate::format::{Format, SampleType};
7
8/// A trait for possible pixel components.
9///
10/// # Safety
11/// Implementing this trait allows retrieving slices of pixel data from the frame for the target
12/// type, so the target type must be valid for the given format.
13pub unsafe trait Component {
14    /// Returns whether this component is valid for this format.
15    fn is_valid(format: Format) -> bool;
16}
17
18unsafe impl Component for u8 {
19    #[inline]
20    fn is_valid(format: Format) -> bool {
21        format.sample_type() == SampleType::Integer && format.bytes_per_sample() == 1
22    }
23}
24
25unsafe impl Component for u16 {
26    #[inline]
27    fn is_valid(format: Format) -> bool {
28        format.sample_type() == SampleType::Integer && format.bytes_per_sample() == 2
29    }
30}
31
32unsafe impl Component for u32 {
33    #[inline]
34    fn is_valid(format: Format) -> bool {
35        format.sample_type() == SampleType::Integer && format.bytes_per_sample() == 4
36    }
37}
38
39#[cfg(feature = "f16-pixel-type")]
40unsafe impl Component for f16 {
41    #[inline]
42    fn is_valid(format: Format) -> bool {
43        format.sample_type() == SampleType::Float && format.bytes_per_sample() == 2
44    }
45}
46
47unsafe impl Component for f32 {
48    #[inline]
49    fn is_valid(format: Format) -> bool {
50        format.sample_type() == SampleType::Float && format.bytes_per_sample() == 4
51    }
52}