use crate::{consts, encoder::Enc};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u8)]
#[allow(dead_code)]
pub(crate) enum ColorType {
Grey = 0u8,
Rgb = 2,
Palette = 3,
GreyAlpha = 4,
Rgba = 6,
}
impl ColorType {
pub(crate) fn channels(self) -> u8 {
match self {
ColorType::Grey | ColorType::Palette => 1,
ColorType::GreyAlpha => 2,
ColorType::Rgb => 3,
ColorType::Rgba => 4,
}
}
pub(crate) fn bpp(self, bit_depth: u8) -> u8 {
assert!((1..=16).contains(&bit_depth));
let ch = self.channels();
ch * if ch > 1 {
if bit_depth == 8 {
8
} else {
16
}
} else {
bit_depth
}
}
}
#[derive(Copy, Clone, Debug)]
pub(crate) struct ImageHeader {
pub(crate) width: u32,
pub(crate) height: u32,
pub(crate) color_type: ColorType,
pub(crate) bit_depth: u8,
pub(crate) interlace: bool,
}
impl ImageHeader {
pub(crate) fn write(&self, enc: &mut Enc) {
enc.prepare(13, consts::IMAGE_HEADER);
enc.u32(self.width);
enc.u32(self.height);
enc.u8(self.bit_depth);
enc.u8(self.color_type as u8);
enc.u8(0);
enc.u8(0);
enc.u8(self.interlace as u8);
enc.write_crc();
}
}