mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 20:46:22 +01:00
Merge main into feat/save-for-web-transparency-dither
This commit is contained in:
commit
c5ca6d4273
141 files changed
+11227
-1777
No files matched your search
@@ -217,14 +217,15 @@ pub fn quantize_to(rgba: &[u8], width: u32, height: u32, o: &PaletteOptions, pal
|
||||
/// For each pixel of `rgba` (`width` pixels a row), the entry of `palette` nearest its color, as
|
||||
/// [`quantize_to`] measures it (the matte, transparency and `o.reduction`'s distance weights)
|
||||
/// without dither; `None` for a pixel that becomes transparent ([`clear_mask`]), and for every
|
||||
/// pixel when `palette` is empty.
|
||||
pub fn nearest_entries(rgba: &[u8], width: u32, o: &PaletteOptions, palette: &[[u8; 3]]) -> Vec<Option<usize>> {
|
||||
let px = rgba.as_chunks::<4>().0;
|
||||
let colors = opaque_colors(px, o, false);
|
||||
/// pixel when `palette` is empty. `f` gets each pixel's entry in pixel order, worked out one pixel
|
||||
/// at a time.
|
||||
pub fn nearest_entries(rgba: &[u8], width: u32, o: &PaletteOptions, palette: &[[u8; 3]], mut f: impl FnMut(Option<usize>)) {
|
||||
let clear = clear_mask(rgba, width, o);
|
||||
let pal: Vec<[f32; 3]> = palette.iter().map(|c| c.map(f32::from)).collect();
|
||||
let index = PaletteIndex::new(&pal, o.reduction.weights());
|
||||
colors.iter().zip(&clear).map(|(c, &clear)| if pal.is_empty() || clear { None } else { Some(index.nearest(c.map(f32::from))) }).collect()
|
||||
for (p, &clear) in rgba.as_chunks::<4>().0.iter().zip(&clear) {
|
||||
f(if pal.is_empty() || clear { None } else { Some(index.nearest(opaque_color(p, o, false).map(f32::from))) });
|
||||
}
|
||||
}
|
||||
|
||||
/// Which pixels of `rgba` (`width` pixels a row) become the transparent entry. Without
|
||||
@@ -292,15 +293,16 @@ pub fn clear_mask(rgba: &[u8], width: u32, o: &PaletteOptions) -> Vec<bool> {
|
||||
|
||||
/// The color of each pixel when opaque: blended over the matte, and gray when `gray`.
|
||||
fn opaque_colors(px: &[[u8; 4]], o: &PaletteOptions, gray: bool) -> Vec<[u8; 3]> {
|
||||
px.iter().map(|p| opaque_color(p, o, gray)).collect()
|
||||
}
|
||||
|
||||
/// The color of the pixel `p` when opaque, as [`opaque_colors`] gives it.
|
||||
fn opaque_color(p: &[u8; 4], o: &PaletteOptions, gray: bool) -> [u8; 3] {
|
||||
let matte = o.matte.unwrap_or([255; 3]);
|
||||
let blend = o.matte.is_some() || !o.transparency;
|
||||
px.iter()
|
||||
.map(|p| {
|
||||
let a = if blend { p[3] as u32 } else { 255 };
|
||||
let c = [0, 1, 2].map(|i| ((p[i] as u32 * a + matte[i] as u32 * (255 - a) + 127) / 255) as u8);
|
||||
if gray { [luma(c); 3] } else { c }
|
||||
})
|
||||
.collect()
|
||||
let a = if blend { p[3] as u32 } else { 255 };
|
||||
let c = [0, 1, 2].map(|i| ((p[i] as u32 * a + matte[i] as u32 * (255 - a) + 127) / 255) as u8);
|
||||
if gray { [luma(c); 3] } else { c }
|
||||
}
|
||||
|
||||
/// Map the pixels (their opaque `colors`, transparent where `clear` says) to `palette` with
|
||||
|
||||
@@ -286,7 +286,8 @@ fn transparency_dither_keeps_about_as_many_pixels_as_their_opacity() {
|
||||
// A fixed palette and the nearest entries make the same pixels transparent.
|
||||
let fixed = quantize_to(&px, w, h, &alpha_dither(d, 100), &[[255, 0, 0], [0, 0, 255]]);
|
||||
assert_eq!(opaque_share(&fixed), share, "{d:?} at {alpha}, fixed palette");
|
||||
let near = nearest_entries(&px, w, &alpha_dither(d, 100), &[[255, 0, 0]]);
|
||||
let mut near = Vec::new();
|
||||
nearest_entries(&px, w, &alpha_dither(d, 100), &[[255, 0, 0]], |n| near.push(n));
|
||||
let mask = clear_mask(&px, w, &alpha_dither(d, 100));
|
||||
assert_eq!(near.iter().map(Option::is_none).collect::<Vec<_>>(), mask, "{d:?} at {alpha}");
|
||||
}
|
||||
|
||||
@@ -93,6 +93,13 @@ fn lossy_makes_runs_and_smaller_gifs() {
|
||||
let mut kept = ix.clone();
|
||||
lossy(&mut kept, &px, 100, &fixed);
|
||||
assert_eq!(kept, ix);
|
||||
// With one fixed entry of three, no pixel moves into or out of it, and the others still merge.
|
||||
let palette: Vec<[u8; 3]> = vec![[100, 0, 0], [110, 0, 0], [120, 0, 0]];
|
||||
let indices: Vec<u8> = vec![1, 0, 0, 1, 1, 2];
|
||||
let px: Vec<u8> = indices.iter().flat_map(|&i| palette[usize::from(i)].into_iter().chain([255])).collect();
|
||||
let mut part = Indexed { width: 2, height: 3, palette, transparent: None, indices };
|
||||
lossy(&mut part, &px, 100, &[0]);
|
||||
assert_eq!(part.indices, vec![1, 0, 0, 1, 1, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user