Flatten transparent pixels to reduce palette size#224
Merged
197g merged 3 commits intoimage-rs:masterfrom Nov 23, 2025
Merged
Conversation
Contributor
|
I'd probably suggest either this: if pix[3] != 0 {
pix[3] = 0xFF;
} else {
match transparent {
...
}
}Or this: if pix[3] != 0 {
pix[3] = 0xFF;
continue;
}
match transparent {
...
} |
197g
reviewed
Nov 21, 2025
src/common.rs
Outdated
Comment on lines
+225
to
+231
| } else if transparent.is_none() { | ||
| transparent = Some([pix[0], pix[1], pix[2], pix[3]]); | ||
| } else if let Some([r, g, b, a]) = transparent { | ||
| pix[0] = r; | ||
| pix[1] = g; | ||
| pix[2] = b; | ||
| pix[3] = a; |
Member
There was a problem hiding this comment.
I'd use if let instead of match here since we only need contents from the Some-branch but follow #224 (comment) otherwise.
Suggested change
| } else if transparent.is_none() { | |
| transparent = Some([pix[0], pix[1], pix[2], pix[3]]); | |
| } else if let Some([r, g, b, a]) = transparent { | |
| pix[0] = r; | |
| pix[1] = g; | |
| pix[2] = b; | |
| pix[3] = a; | |
| continue; | |
| } | |
| if let Some([r, g, b, a]) = transparent { | |
| pix[0] = r; | |
| pix[1] = g; | |
| pix[2] = b; | |
| pix[3] = a; | |
| } else { | |
| transparent = Some([pix[0], pix[1], pix[2], pix[3]]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When creating frames from RGBA data with
Frame::from_rgba_speed(), all transparent pixels (alpha = 0) are now flattened to use the same RGB values. This ensures transparent pixels count as a single color entry instead of multiple entries in the palette.Benefit: Reduces palette size and helps avoid unnecessary NeuQuant quantization when the image has ≤256 unique colors.
The
BTreeSet<(u8, u8, u8, u8)>now counts: opaque colors + 1 transparent color.like suggested in #110
PS. to avoid .unwrap() i inserted a redundant Some() check. my rust knowledge is limited, any suggestion is appreciated.