Save for Web: GIF lossy compares pixels with the web-snapped colors

Web shift and web snap now run before lossy compression again, so a pixel
is compared with the color its neighbor's entry is written as. Colors
shifted by hand are written after lossy, which still moves no pixel into
or out of them.
This commit is contained in:
dotCooCoo
2026-11-17 18:57:53 -08:00
parent 8486bd1892
commit fc5460d18e
2 changed files with 32 additions and 17 deletions
+16 -15
View File
@@ -814,24 +814,11 @@ fn palette_image(rgba: &[u8], w: u32, h: u32, s: &WebSettings, matte: Option<[u8
*slot = *c;
}
}
// Lossy compares the pixels with the colors the reduction made, and moves no pixel into or out
// of a color shifted by hand.
if s.format == WebFormat::Gif {
let fixed: Vec<u8> = sources.iter().enumerate().filter(|(_, c)| shifted_to(c).is_some()).filter_map(|(i, _)| u8::try_from(i).ok()).collect();
web::lossy(&mut ix, rgba, s.lossy, &fixed);
}
// The colors shifted by hand, then web shift and web snap (locked colors stay as they are).
// Web shift and web snap (locked colors and colors shifted by hand stay as they are).
let clear = ix.transparent.map(usize::from);
for (i, c) in ix.palette.iter_mut().enumerate() {
let src = sources.get(i).copied().unwrap_or(*c);
if Some(i) == clear {
continue;
}
if let Some(to) = shifted_to(&src) {
*c = to;
continue;
}
if locked.contains(&src) {
if Some(i) == clear || shifted_to(&src).is_some() || locked.contains(&src) {
continue;
}
if shift.contains(&src) {
@@ -840,6 +827,20 @@ fn palette_image(rgba: &[u8], w: u32, h: u32, s: &WebSettings, matte: Option<[u8
*c = snapped;
}
}
// Lossy compares the pixels with the colors written for them, and moves no pixel into or out of
// a color shifted by hand.
if s.format == WebFormat::Gif {
let fixed: Vec<u8> = sources.iter().enumerate().filter(|(_, c)| shifted_to(c).is_some()).filter_map(|(i, _)| u8::try_from(i).ok()).collect();
web::lossy(&mut ix, rgba, s.lossy, &fixed);
}
// The colors shifted by hand.
for (i, c) in ix.palette.iter_mut().enumerate() {
if Some(i) != clear
&& let Some(to) = sources.get(i).and_then(shifted_to)
{
*c = to;
}
}
let order = web::sort(&mut ix, table.sort);
let sources: Vec<[u8; 3]> = order.iter().map(|&i| sources.get(i).copied().unwrap_or_default()).collect();
let colors = ix
+16 -2
View File
@@ -686,8 +686,8 @@ fn colour_table_shift_writes_the_picked_color_wherever_the_source_is() {
assert!(old.color_table.shifted.is_empty());
}
/// Lossy GIF compression compares the pixels with the colors the reduction made, so a color shifted
/// near another one does not take in that color's pixels.
/// Lossy GIF compression leaves the pixels of a color shifted by hand alone, so a color shifted near
/// another one does not take in that color's pixels.
#[test]
fn lossy_does_not_spread_a_shifted_color_into_another() {
let mut s = session(30.0, 10.0);
@@ -715,6 +715,20 @@ fn lossy_moves_no_pixel_into_or_out_of_a_shifted_color() {
}
}
/// Lossy GIF compression compares the pixels with the colors web snap writes, so a pixel does not
/// take a color that snaps far from it.
#[test]
fn lossy_compares_pixels_with_the_snapped_colors() {
let mut s = session(20.0, 10.0);
rect(&mut s, [0.0, 0.0, 10.0, 10.0], "#1a1a1a");
rect(&mut s, [10.0, 0.0, 20.0, 10.0], "#000000");
let img = pixels(&one(&mut s, json!({"format": "gif", "dither": "none", "webSnap": 100, "lossy": 50})));
for x in 0..20 {
let want = if x < 10 { [51, 51, 51, 255] } else { [0, 0, 0, 255] };
assert_eq!(img.get_pixel(x, 5).0, want, "x = {x}");
}
}
#[test]
fn presets_and_remembered_settings() {
let mut s = two_colours();