-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds an example for the result of quantizing, which can be used to manually verify correctness.
- Loading branch information
1 parent
6c302d2
commit 6d2f9a0
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<Vec<u8>>(); | ||
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::<Vec<u8>>(); | ||
write("quantized.ppm", quantized).expect("Failed to write"); | ||
} |