use crate::{_impl_init, RasterPackedChannels, RasterSampleFormat};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[allow(dead_code, reason = "Not exposed: Xrgb, Xbgr")]
pub(crate) enum RasterChannels {
#[default]
Unknown,
Gray,
GrayAlpha,
Rgb,
Rgba,
Argb,
Xrgb,
Rgbx,
Bgr,
Bgra,
Abgr,
Xbgr,
Bgrx,
Indexed,
Packed(RasterPackedChannels),
}
_impl_init![Self::Unknown => RasterChannels];
impl RasterChannels {
pub const fn is_unknown(self) -> bool {
match self {
Self::Unknown => true,
Self::Packed(packed) => packed.is_unknown(),
_ => false,
}
}
pub const fn channel_count(self) -> Option<u8> {
match self {
Self::Unknown => None,
Self::Gray | Self::Indexed => Some(1),
Self::GrayAlpha => Some(2),
Self::Rgb | Self::Bgr => Some(3),
Self::Rgba
| Self::Bgra
| Self::Argb
| Self::Abgr
| Self::Xrgb
| Self::Xbgr
| Self::Rgbx
| Self::Bgrx => Some(4),
Self::Packed(packed) => packed.channel_count(),
}
}
pub const fn color_channel_count(self) -> Option<u8> {
match self {
Self::Unknown | Self::Indexed => None,
Self::Gray | Self::GrayAlpha => Some(1),
Self::Rgb
| Self::Bgr
| Self::Rgba
| Self::Bgra
| Self::Argb
| Self::Abgr
| Self::Xrgb
| Self::Xbgr
| Self::Rgbx
| Self::Bgrx => Some(3),
Self::Packed(packed) => packed.color_channel_count(),
}
}
pub const fn has_alpha_field(self) -> bool {
match self {
Self::GrayAlpha | Self::Rgba | Self::Bgra | Self::Argb | Self::Abgr => true,
Self::Packed(packed) => packed.has_alpha(),
Self::Unknown
| Self::Gray
| Self::Rgb
| Self::Bgr
| Self::Xrgb
| Self::Xbgr
| Self::Rgbx
| Self::Bgrx
| Self::Indexed => false,
}
}
pub const fn has_padding_channel(self) -> bool {
match self {
Self::Xrgb | Self::Xbgr | Self::Rgbx | Self::Bgrx => true,
Self::Packed(packed) => packed.has_padding_channel(),
Self::Unknown
| Self::Gray
| Self::GrayAlpha
| Self::Rgb
| Self::Bgr
| Self::Rgba
| Self::Bgra
| Self::Argb
| Self::Abgr
| Self::Indexed => false,
}
}
pub const fn is_indexed(self) -> bool {
matches!(self, Self::Indexed)
}
pub const fn is_packed(self) -> bool {
matches!(self, Self::Packed(_))
}
pub const fn depth_bits(self, sample: RasterSampleFormat) -> Option<u16> {
match self {
Self::Unknown => None,
Self::Packed(packed) => packed.depth_bits(),
Self::Xrgb | Self::Xbgr | Self::Rgbx | Self::Bgrx => match sample.bits() {
Some(bits) => Some(3 * bits),
None => None,
},
_ => match (self.channel_count(), sample.bits()) {
(Some(channels), Some(bits)) => Some(channels as u16 * bits),
_ => None,
},
}
}
pub const fn bits_per_pixel(self, sample: RasterSampleFormat) -> Option<u16> {
match self {
Self::Unknown => None,
Self::Packed(packed) => packed.bits_per_pixel(),
_ => match (self.channel_count(), sample.bits()) {
(Some(channels), Some(bits)) => Some(channels as u16 * bits),
_ => None,
},
}
}
}