use crate::{agnostic::ClumpFormat, error::Error as CacoError};
use anyhow::{Error, Result};
use image::{Pixel, Rgb};
use itertools::Itertools;
pub struct PaletteTint {
pub colors: [Rgb<u8>; 256]
}
pub struct Playpal {
pub tints: Vec<PaletteTint>
}
impl ClumpFormat for Playpal {
fn try_decode_bytes(data: &[u8]) -> Result<Self> {
if data.len() % 768 != 0 { return Err(Error::new(CacoError::FormatError)) }
let mut tints = vec!();
for t in data.chunks(768) {
let colors = *t.iter()
.tuples()
.map(|(r, g, b)| Rgb([*r, *g, *b]))
.collect::<Vec<Rgb<u8>>>()
.as_array()
.ok_or(CacoError::FormatError)?;
tints.push(PaletteTint { colors });
}
Ok(Playpal { tints })
}
fn try_encode_bytes(&self) -> Result<Box<[u8]>> {
Ok(self.tints.iter()
.map(|t| t.colors.iter()
.map(|c| c.channels())
.flatten()
.map(|n| *n) )
.flatten()
.collect::<Vec<u8>>()
.into_boxed_slice())
}
}