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

Minor reduction improvements #502

Merged
merged 4 commits into from
May 7, 2023
Merged

Conversation

andrews05
Copy link
Collaborator

@andrews05 andrews05 commented May 4, 2023

This started off as minor a bug fix but then expanded into a larger refactor.

The bug occurred when a GrayscaleAlpha image with a bKGD chunk tried to reduce to Indexed. The bKGD conversion was assuming RGB and would fail the reduction entirely if the chunk wasn't 6 bytes. It now correctly reduces a Grayscale bKGD and thereby allows the reduction to Indexed. (My fault, since I added the GrayAlpha to Indexed support)

However, due to the new found ability to reduce such images to indexed, "issue-60.png" ended up regressing slightly as the indexed version was actually larger than the gray alpha. Ultimately I ended up completely refactoring the reduction and evaluation sequence, combining the perform_reductions function with reduce_color_type (I've put the code in the reduction mod, mainly to reduce the amount of code in lib).
The result is a bit tidier and has a few notable outcomes:

The end result of all this is... "issue-52-04.png" now compresses 3 bytes smaller 😂
Performance is essentially unchanged (I did measure a fractional improvement at level 0).

@andrews05 andrews05 force-pushed the evaluations branch 2 times, most recently from 1c477e3 to 3f936a0 Compare May 5, 2023 08:07
@andrews05
Copy link
Collaborator Author

Well, this turned into a larger refactor than I had planned 😆

@kornelski I ended up removing the extra 4-bit evaluation. I managed to find some images which did prefer 4-bit to 2-bit, but (perhaps due to improvements since that code was added) they were actually best at 8-bit (or even rgb). So I think it's not providing any benefit anymore, but do let me know if you disagree.

@andrews05 andrews05 force-pushed the evaluations branch 3 times, most recently from a2a5993 to 63f91b9 Compare May 5, 2023 22:05
if let Some(reduced) = reduce_to_palette(&png) {
png = Arc::new(reduced);
// Make sure the palette gets sorted (ideally, this should be done within reduce_to_palette)
if let Some(reduced) = reduced_palette(&png, opts.optimize_alpha) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can reduce_to_palette be moved before line 80 to try reduced_palette once?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two calls to reduced_palette are mutually exclusive: The earlier one will run if the image is already indexed (the function will simply return if it isn't), while this one will run if we've just converted to indexed.

@shssoichiro
Copy link
Owner

Thanks, looks good, just needs a rebase 👍

@andrews05
Copy link
Collaborator Author

@shssoichiro Thanks, done 🙂

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.

Implement possible size optimizations from Bit Depth from TruePNG
3 participants