2021-11-29 22:30:29 +00:00
|
|
|
use crate::colorspace::ColorSpace;
|
2021-11-28 12:36:12 +00:00
|
|
|
use crate::consts::{QOI_HEADER_SIZE, QOI_MAGIC};
|
2021-12-01 17:07:21 +00:00
|
|
|
use crate::error::{Error, Result};
|
|
|
|
use crate::utils::unlikely;
|
2021-11-28 12:36:12 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct Header {
|
2021-11-29 22:38:09 +00:00
|
|
|
pub magic: u32,
|
2021-11-28 12:36:12 +00:00
|
|
|
pub width: u32,
|
|
|
|
pub height: u32,
|
|
|
|
pub channels: u8,
|
2021-11-29 22:30:29 +00:00
|
|
|
pub colorspace: ColorSpace,
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Header {
|
|
|
|
fn default() -> Self {
|
2021-11-29 22:30:29 +00:00
|
|
|
Self {
|
|
|
|
magic: QOI_MAGIC,
|
2021-11-29 22:30:53 +00:00
|
|
|
width: 1,
|
|
|
|
height: 1,
|
2021-11-29 22:30:29 +00:00
|
|
|
channels: 3,
|
|
|
|
colorspace: ColorSpace::default(),
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn u32_to_be(v: u32) -> [u8; 4] {
|
|
|
|
[
|
|
|
|
((0xff000000 & v) >> 24) as u8,
|
|
|
|
((0xff0000 & v) >> 16) as u8,
|
|
|
|
((0xff00 & v) >> 8) as u8,
|
|
|
|
(0xff & v) as u8,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn u32_from_be(v: &[u8]) -> u32 {
|
|
|
|
((v[0] as u32) << 24) | ((v[1] as u32) << 16) | ((v[2] as u32) << 8) | (v[3] as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Header {
|
|
|
|
pub const SIZE: usize = QOI_HEADER_SIZE;
|
|
|
|
|
|
|
|
pub(crate) fn to_bytes(&self) -> [u8; QOI_HEADER_SIZE] {
|
|
|
|
let mut out = [0; QOI_HEADER_SIZE];
|
2021-11-29 22:38:09 +00:00
|
|
|
out[..4].copy_from_slice(&u32_to_be(self.magic));
|
2021-11-28 12:36:12 +00:00
|
|
|
out[4..8].copy_from_slice(&u32_to_be(self.width));
|
|
|
|
out[8..12].copy_from_slice(&u32_to_be(self.height));
|
|
|
|
out[12] = self.channels;
|
2021-11-29 22:30:29 +00:00
|
|
|
out[13] = self.colorspace.into();
|
2021-11-28 12:36:12 +00:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_bytes(v: [u8; QOI_HEADER_SIZE]) -> Self {
|
|
|
|
let mut out = Self::default();
|
2021-11-29 22:38:09 +00:00
|
|
|
out.magic = u32_from_be(&v[..4]);
|
2021-11-28 12:36:12 +00:00
|
|
|
out.width = u32_from_be(&v[4..8]);
|
|
|
|
out.height = u32_from_be(&v[8..12]);
|
|
|
|
out.channels = v[12];
|
2021-11-29 22:30:29 +00:00
|
|
|
out.colorspace = v[13].into();
|
2021-11-28 12:36:12 +00:00
|
|
|
out
|
|
|
|
}
|
2021-12-01 16:04:04 +00:00
|
|
|
|
|
|
|
pub const fn n_pixels(&self) -> usize {
|
|
|
|
(self.width as usize).saturating_mul(self.height as usize)
|
|
|
|
}
|
2021-12-01 17:07:21 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn validate(&self) -> Result<()> {
|
|
|
|
if unlikely(self.magic != QOI_MAGIC) {
|
|
|
|
return Err(Error::InvalidMagic { magic: self.magic });
|
|
|
|
} else if unlikely(self.height == 0 || self.width == 0) {
|
|
|
|
return Err(Error::EmptyImage { width: self.width, height: self.height });
|
|
|
|
} else if unlikely(self.channels < 3 || self.channels > 4) {
|
|
|
|
return Err(Error::InvalidChannels { channels: self.channels });
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-28 12:36:12 +00:00
|
|
|
}
|