use crate::utils::{BigEndianReader, ycbcr_to_rgba};
#[derive(Debug, Clone)]
pub struct PaletteDefinitionSegment {
pub id: u8,
pub version: u8,
pub rgba: Vec<u32>,
}
impl PaletteDefinitionSegment {
pub fn parse(reader: &mut BigEndianReader, length: usize) -> Option<Self> {
let id = reader.read_u8()?;
let version = reader.read_u8()?;
let entry_count = (length - 2) / 5;
let mut rgba = vec![0u32; 256];
for _ in 0..entry_count {
let entry_id = reader.read_u8()? as usize;
let y = reader.read_u8()?;
let cr = reader.read_u8()?; let cb = reader.read_u8()?;
let a = reader.read_u8()?;
if entry_id < 256 {
rgba[entry_id] = ycbcr_to_rgba(y, cb, cr, a);
}
}
Some(Self { id, version, rgba })
}
pub fn empty() -> Self {
Self {
id: 0,
version: 0,
rgba: vec![0u32; 256],
}
}
}