2021-12-31 10:34:27 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::utils::unlikely;
|
|
|
|
|
|
|
|
/// Image color space.
|
|
|
|
///
|
|
|
|
/// Note: the color space is purely informative. Although it is saved to the
|
|
|
|
/// file header, it does not affect encoding/decoding in any way.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum ColorSpace {
|
|
|
|
/// sRGB with linear alpha
|
|
|
|
Srgb = 0,
|
|
|
|
/// All channels are linear
|
|
|
|
Linear = 1,
|
2021-11-29 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ColorSpace {
|
|
|
|
pub const fn is_srgb(self) -> bool {
|
2021-12-31 10:34:27 +00:00
|
|
|
matches!(self, Self::Srgb)
|
2021-11-29 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn is_linear(self) -> bool {
|
2021-12-31 10:34:27 +00:00
|
|
|
matches!(self, Self::Linear)
|
2021-11-29 22:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ColorSpace {
|
|
|
|
fn default() -> Self {
|
2021-12-31 10:34:27 +00:00
|
|
|
Self::Srgb
|
2021-11-29 22:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ColorSpace> for u8 {
|
2021-12-31 10:34:27 +00:00
|
|
|
#[inline]
|
|
|
|
fn from(colorspace: ColorSpace) -> Self {
|
2021-12-31 10:37:56 +00:00
|
|
|
colorspace as Self
|
2021-11-29 22:23:39 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-01 17:17:12 +00:00
|
|
|
|
2021-12-31 10:34:27 +00:00
|
|
|
impl TryFrom<u8> for ColorSpace {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn try_from(colorspace: u8) -> Result<Self> {
|
|
|
|
if unlikely(colorspace | 1 != 1) {
|
|
|
|
Err(Error::InvalidColorSpace { colorspace })
|
|
|
|
} else {
|
|
|
|
Ok(if colorspace == 0 { Self::Srgb } else { Self::Linear })
|
|
|
|
}
|
2021-12-01 17:17:12 +00:00
|
|
|
}
|
|
|
|
}
|