use super::error::Error;
use ::image::RgbaImage;
pub fn quantize_rgba(
mut img: RgbaImage,
) -> Result<(Vec<u8>, Vec<u32>, color_quant::NeuQuant), Error> {
let palette_size = 255; let sample_fact = 10;
for pix in img.pixels_mut() {
pix[3] = 0;
}
let nq = color_quant::NeuQuant::new(sample_fact, palette_size, img.as_raw());
let quantized: Vec<u8> = img.pixels().map(|pix| nq.index_of(&pix.0) as u8).collect();
let palette = nq
.color_map_rgb()
.chunks(3)
.map(|col| {
let red = col[0] as u32;
let green = (col[1] as u32) << 8;
let blue = (col[2] as u32) << 16;
red + green + blue
})
.collect();
Ok((quantized, palette, nq))
}
pub fn quantize_rgba_known(
mut img: RgbaImage,
nq: &color_quant::NeuQuant,
) -> Result<Vec<u8>, Error> {
for pix in img.pixels_mut() {
pix[3] = 0;
}
let quantized: Vec<u8> = img.pixels().map(|pix| nq.index_of(&pix.0) as u8).collect();
Ok(quantized)
}