Save for Web: diffusion transparency dither keeps its error past fully clear pixels

Fully transparent and fully opaque pixels keep their state but now pass on
the opacity error they receive. Before, that error was dropped, so a row
alternating half-opaque and fully transparent pixels kept every
half-opaque pixel opaque at amount 100. About half of them stay opaque
now, in a single row and in a checkerboard.
This commit is contained in:
dotCooCoo committed 2026-11-21 00:08:27 -08:00
1 parent b2e390f18f
commit c8fb7729e5
2 files changed
+38 -13

No files matched your search

+20 -13
View File
@@ -232,8 +232,9 @@ pub fn nearest_entries(rgba: &[u8], width: u32, o: &PaletteOptions, palette: &[[
/// partly transparent pixel does under half opacity with no transparency dither, and else by
/// `o.transparency_dither`. With pattern, noise, or diffusion at amount 100, about as many of an
/// area's pixels stay opaque as its opacity says; a lower diffusion amount moves the result toward
/// the cut at half opacity. Diffusion spreads each pixel's opacity error to the pixels right of it
/// and below it (Floyd–Steinberg, scaled by `o.transparency_dither_amount`), pattern compares the
/// the cut at half opacity. Diffusion spreads each partly transparent pixel's opacity error to the
/// pixels right of it and below it (Floyd–Steinberg, scaled by `o.transparency_dither_amount`), and
/// fully transparent and opaque pixels pass on the error they receive; pattern compares the
/// opacity with the 8×8 ordered pattern and noise with a noise threshold. The result depends only
/// on the pixels and the options, so exports are reproducible.
pub fn clear_mask(rgba: &[u8], width: u32, o: &PaletteOptions) -> Vec<bool> {
@@ -264,18 +265,24 @@ pub fn clear_mask(rgba: &[u8], width: u32, o: &PaletteOptions) -> Vec<bool> {
(_, Dither::Pattern) => a < 255.0 * bayer(x % 8, y % 8),
// Another noise than the color dither's, so the two do not line up.
(_, Dither::Noise) => a < 255.0 * noise(x ^ 0x5a5a, y ^ 0xa5a5),
(_, Dither::Diffusion) => {
let v = a + cur[x + 1];
let clear = v < 127.5;
let e = (v - if clear { 0.0 } else { 255.0 }) * amount;
// 7/16 right, 3/16 below left, 5/16 below, 1/16 below right.
cur[x + 2] += e * 7.0 / 16.0;
next[x] += e * 3.0 / 16.0;
next[x + 1] += e * 5.0 / 16.0;
next[x + 2] += e / 16.0;
clear
}
(_, Dither::Diffusion) => a + cur[x + 1] < 127.5,
};
if dither == Dither::Diffusion {
// A partly transparent pixel spreads its own opacity error. A fully transparent or
// opaque pixel keeps its state and passes on the error it received unchanged, so
// the partly transparent pixels around it keep their share of opacity
// (VectorCraft's own rule).
let got = cur[x + 1];
let e = match p[3] {
0 | 255 => got,
_ => (a + got - if clear { 0.0 } else { 255.0 }) * amount,
};
// 7/16 right, 3/16 below left, 5/16 below, 1/16 below right.
cur[x + 2] += e * 7.0 / 16.0;
next[x] += e * 3.0 / 16.0;
next[x + 1] += e * 5.0 / 16.0;
next[x + 2] += e / 16.0;
}
out.push(clear);
}
cur.copy_from_slice(next);
+18
View File
@@ -328,3 +328,21 @@ fn transparency_dither_leaves_clear_and_opaque_pixels_and_needs_transparency() {
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);
}
#[test]
fn transparency_diffusion_passes_error_through_clear_and_opaque_pixels() {
// Half-opaque pixels between fully transparent or fully opaque ones, in one row and in a
// checkerboard: about half of them stay opaque, and the others keep their state.
for other in [0u8, 255] {
for (w, h, half) in [(64, 1, (|x, _| x % 2 == 0) as fn(u32, u32) -> bool), (32, 32, |x, y| (x + y) % 2 == 0)] {
let px = see_through(w, h, |x, y| if half(x, y) { 128 } else { other });
let mask = clear_mask(&px, w, &alpha_dither(Dither::Diffusion, 100));
let at = |x: u32, y: u32| mask[(y * w + x) as usize];
let cells: Vec<(u32, u32)> = (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).collect();
assert!(cells.iter().filter(|&&(x, y)| !half(x, y)).all(|&(x, y)| at(x, y) == (other == 0)), "{other} {w}x{h}");
let halves: Vec<_> = cells.iter().filter(|&&(x, y)| half(x, y)).collect();
let share = halves.iter().filter(|&&&(x, y)| !at(x, y)).count() as f64 / halves.len() as f64;
assert!((share - 128.0 / 255.0).abs() < 0.1, "{other} {w}x{h}: {share}");
}
}
}