From 6d2f9a023792af127e53d90d3f34aeecd33fbb3b Mon Sep 17 00:00:00 2001 From: julianknodt Date: Wed, 21 Jul 2021 18:14:47 -0700 Subject: [PATCH] Add example for quantization This adds an example for the result of quantizing, which can be used to manually verify correctness. --- examples/quantize.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 examples/quantize.rs diff --git a/examples/quantize.rs b/examples/quantize.rs new file mode 100644 index 0000000..b7109eb --- /dev/null +++ b/examples/quantize.rs @@ -0,0 +1,66 @@ +#![feature(array_chunks)] + +extern crate color_quant; + +use color_quant::NeuQuant; +use std::fs::write; + +fn main() { + let header = b"P6\n256\n256\n255\n"; + let mut pixels = vec![0; 4 * 256 * 256]; + let mut r: u8 = 0; + let mut g: u8 = 0; + let mut b: u8 = 0; + + for (i, p) in pixels.iter_mut().enumerate() { + match i % 4 { + 0 => *p = r, + 1 => *p = g, + 2 => { + *p = b; + if let Some(next_r) = r.checked_add(1) { + r = next_r; + continue; + } + r = 0; + if let Some(next_g) = g.checked_add(1) { + g = next_g; + continue; + } + g = 0; + b += 1 + } + 3 => *p = 255, + _ => unreachable!(), + } + } + let raw = &header + .into_iter() + .chain( + pixels + .array_chunks::<4>() + .map(|[r, g, b, _a]| [r, g, b]) + .flatten(), + ) + .copied() + .collect::>(); + write("img.ppm", raw).expect("Failed to write"); + let nq = NeuQuant::new(10, 256, &pixels); + + let quantized = &header + .into_iter() + .copied() + .chain( + pixels + .array_chunks::<4>() + .map(|&[r, g, b, a]| { + let mut color = [r, g, b, a]; + nq.map_pixel(&mut color[..]); + let [r, g, b, _a] = color; + [r, g, b] + }) + .flatten(), + ) + .collect::>(); + write("quantized.ppm", quantized).expect("Failed to write"); +}