mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 12:21:08 +01:00
Save for Web: Black & White keeps to the color count with transparent pixels
With two colors and some pixels transparent, Black & White still built both black and white entries next to the transparent one, so the palette held three entries. When only one entry is left, Black & White now takes black or white, whichever leaves the opaque pixels nearer the palette with the locked colors included, and skips an end that is already locked.
This commit is contained in:
1 parent
c5ca6d4273
commit
7c376a58da
3 files changed
+32
-1
No files matched your search
@@ -178,6 +178,22 @@ pub fn quantize_locked(rgba: &[u8], width: u32, height: u32, o: &PaletteOptions,
|
||||
}
|
||||
let free = k - lock.len();
|
||||
let (mut palette, exact) = match o.reduction {
|
||||
// With one entry left (two colors and a transparent entry, or the others locked): black or
|
||||
// white, whichever leaves the opaque pixels nearer the palette, the locked colors included.
|
||||
Reduction::BlackWhite if free < 2 => {
|
||||
let mut hist = [0u64; 256];
|
||||
for c in opaque() {
|
||||
hist[c[0] as usize] += 1;
|
||||
}
|
||||
let w = o.reduction.weights();
|
||||
let cost = |end: [u8; 3]| -> f32 {
|
||||
let entries: Vec<[f32; 3]> = lock.iter().chain([&end]).map(|c| c.map(f32::from)).collect();
|
||||
let near = |g: f32| entries.iter().map(|&e| dist([g; 3], e, w)).fold(f32::INFINITY, f32::min);
|
||||
hist.iter().enumerate().filter(|(_, n)| **n > 0).map(|(g, &n)| n as f32 * near(g as f32)).sum()
|
||||
};
|
||||
let ends = [[0; 3], [255; 3]].into_iter().filter(|c| !lock.contains(c));
|
||||
(ends.min_by(|a, b| cost(*a).total_cmp(&cost(*b))).into_iter().collect(), false)
|
||||
}
|
||||
Reduction::BlackWhite => (vec![[0; 3], [255; 3]], false),
|
||||
Reduction::Gray => ((0..free).map(|i| [(i * 255 / (free - 1).max(1)) as u8; 3]).collect(), false),
|
||||
Reduction::Web => (web(opaque(), free), false),
|
||||
|
||||
@@ -71,7 +71,7 @@ fn colours_cap_the_palette() {
|
||||
let px = ramps(64, 48);
|
||||
for colors in [2, 16, 64, 256] {
|
||||
for dither in Dither::ALL {
|
||||
for reduction in [Reduction::Perceptual, Reduction::Selective, Reduction::Adaptive, Reduction::Web, Reduction::Gray] {
|
||||
for reduction in Reduction::ALL {
|
||||
let ix = quantize(&px, 64, 48, &PaletteOptions { colors, dither, reduction, ..Default::default() });
|
||||
assert!(ix.palette.len() <= colors as usize, "{colors} {dither:?} {reduction:?}: {}", ix.palette.len());
|
||||
assert!(ix.indices.iter().all(|&i| (i as usize) < ix.palette.len()));
|
||||
@@ -325,6 +325,12 @@ fn transparency_dither_leaves_clear_and_opaque_pixels_and_needs_transparency() {
|
||||
let o = PaletteOptions { colors: 2, ..alpha_dither(Dither::Pattern, 100) };
|
||||
let ix = quantize(&see_through(w, h, |_, _| 128), w, h, &o);
|
||||
assert_eq!((ix.palette.len(), ix.transparent), (2, Some(0)));
|
||||
// So does Black & White, with black and white pixels both left opaque.
|
||||
let bw = PaletteOptions { reduction: Reduction::BlackWhite, matte: None, ..o };
|
||||
let px: Vec<u8> = (0..64u32).flat_map(|i| if i % 8 < 4 { [0, 0, 0, 128] } else { [255, 255, 255, 128] }).collect();
|
||||
let ix = quantize(&px, 8, 8, &bw);
|
||||
assert_eq!(ix.transparent, Some(0));
|
||||
assert!(ix.palette.len() <= 2, "{:?}", ix.palette);
|
||||
// No pixels, and a width past the pixels there are.
|
||||
assert!(clear_mask(&[], 0, &alpha_dither(Dither::Diffusion, 100)).is_empty());
|
||||
assert_eq!(clear_mask(&see_through(3, 1, |_, _| 200), 1000, &alpha_dither(Dither::Diffusion, 100)).len(), 3);
|
||||
|
||||
@@ -22,6 +22,15 @@ fn locked_colours_survive_a_reduction() {
|
||||
let ix = quantize_locked(&px, 64, 16, &o, &[rare]);
|
||||
assert!(ix.palette.contains(&rare) && ix.palette.len() <= 4, "{:?}", ix.palette);
|
||||
assert_eq!(colours(&ix)[0], rare);
|
||||
// Black & White with black locked and one entry left: white takes it, though most pixels are black.
|
||||
let px = image(8, 8, |x, _| if x == 7 { [255; 3] } else { [0; 3] });
|
||||
let o = PaletteOptions { colors: 2, dither: Dither::None, reduction: Reduction::BlackWhite, ..Default::default() };
|
||||
let ix = quantize_locked(&px, 8, 8, &o, &[[0; 3]]);
|
||||
assert_eq!(ix.palette, [[0; 3], [255; 3]]);
|
||||
// With a light gray locked, black takes the entry left, though most pixels are white.
|
||||
let px = image(10, 1, |x, _| if x < 4 { [0; 3] } else { [255; 3] });
|
||||
let ix = quantize_locked(&px, 10, 1, &o, &[[200; 3]]);
|
||||
assert_eq!(ix.palette, [[200; 3], [0; 3]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user