Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: "smearing" of gifs with transparent pixels #207

Merged
merged 2 commits into from
Dec 16, 2024
Merged

Conversation

salarkhan
Copy link
Contributor

@salarkhan salarkhan commented Dec 16, 2024

Fixes: #84

Problem

I tried this with a number of different gifs, seems like it only happened with these stark black/white ones. In the specific case from the linked issue, white pixels from the witch silhouette were being incorrectly marked as transparent when they should remain opaque. The transparency is causing the previous frame's content to show through, creating the smearing effect.

in out
in out

Fix

So, we have a palette lookup table comprised of bit-crushed RGB values. For extreme colors like pure white (255,255,255), the crushed center calculation: uint32_t R_center = (R & 0xf8) | 4; meant taht for R=255, we'd get 252, aka a slightly different color than what we actually had.

When comparing colors for transparency optimization, the least_dist we got from palette matching might have been larger than it should have been (because we used approximated colors), making it more likely for the transparency choice to "win" the comparison.

By using the actual RGB values instead, we get more accurate color matching against the palette, which means least_dist is more likely to be small for exact color matches. This makes it less likely for the transparency optimization to incorrectly override a good palette color match, especially for extreme colors like pure white or black.

@salarkhan salarkhan requested a review from skidder December 16, 2024 18:26
@skidder
Copy link
Contributor

skidder commented Dec 16, 2024

lgtm!

@salarkhan salarkhan merged commit c2b5911 into master Dec 16, 2024
2 checks passed
@salarkhan salarkhan deleted the sk/gif-smear-crush branch December 16, 2024 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some .gifs are incorrectly "smearing"
2 participants