-
Notifications
You must be signed in to change notification settings - Fork 125
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
Additional palette sorting algorithm #514
Conversation
b9952d8
to
8ba2782
Compare
@ andrews05 - great addition!! I discovered a very simple heuristic that is cheap to check and quite effective:
PS - I have a bunch of 'palette order sensitive' images I used for testing. I could share a zip with you? |
And historic discussion here: |
@ace-dent Nice, thanks for that! I ran some tests on your files:
I'll see if I can incorporate your heuristic and do some further tests sometime... [edit] I've implemented your suggestion and see small but generally positive changes 🙂
And the set of files from my first post is now down to 18,539,101 bytes. |
@andrews05 thanks for implementing this! Wow :-D
Cheers. |
I did a quick survey of what other apps do. Have you considered testing chroma (/hue) sort? I certainly have test images that benefit from this. It seems reasonable to do (1) frequency – of some form, (2) chroma, (3) luma and (4) a more advanced statistical model (Battiato)... unless the 'Battiato' method is consistently equal or more performant than 1-3. pngrewrite (& imageworsener)
OptiPNG Pngoptimizer Pngcrush
PNGPalTest
|
Regarding other methods you mention: In my (albeit limited) experience, frequency (popularity) sort has been largely ineffective. I mean, it might look good compared to unsorted (or order of appearance) but pales in comparison to the luma sort. The zopflipng KrzyMod fork tries 120 combinations of these options, which were borrowed from TruePNG:
As I mentioned in my first post, the results were actually disappointing. That said, I would like to narrow these down and find out if there are one or two specific combinations that show modest improvement when added to the luma and battiato sorts. (edit: either ydsn or ydsd seems best) Finally, yes I do plan to try the mzeng method next! The reason I went with Battiato first was because 1. it has a much lower complexity (M2logM, vs M3 for mzeng), and 2. a later paper indicated it actually performed better for PNGs (most of the papers are testing with JPEG-LS). There's also a couple of others I hope to try, but they all take time to implement. |
I'm putting this on hold until #516 can be merged, as the sorting will be easier when we know we're always working in 8-bit. |
@andrews05 - Just thought: if the |
a04d639
to
c6724ac
Compare
@ace-dent I improved the co-occurrence matrix construction - your test files are now down to 261,712 bytes. I experimented a little with altering the position for a specific filter but it did not give positive results. (Possibly because the evaluator always runs with fixed filters: None & Bigrams) |
Re. filters: makes sense. Perhaps also diminishing returns -vs- added code complexity... |
Yeah, I already sorted that out so this is good to go 🙂 |
@andrews05 - was Mzeng useful? Curious about your findings :-) Update: I have also found images with very repeating patterns (so, e.g. filter=1 on all rows, etc.), where my edge color heuristic may fail. Please may you check for wins -vs- losses? https://github.com/ace-dent/8x8.me/tree/main/previews |
Oh yeah, I did experiment briefly with mzeng. On its own it averaged slightly better than battiato but when combined with the luma sort it averaged slightly worse. This was just on one set of files though (and the code was nowhere near production ready). I've just tried your "previews" folder - putting the popular edge colour first in the palette still comes out better than not doing that. |
9e3d15f
to
85b1fc2
Compare
Gah, there was a dumb bug I introduced in the clap update where fast mode was being disabled by default and reduced performance at level 2. I only found it while investigating a failing test here. All fixed now. |
This adds a new palette sorting algorithm that attempts to minimise entropy by an approximate solution to the Traveling Salesman Problem. The algorithm comes from "An efficient Re-indexing algorithm for color-mapped images" by Battiato et al (https://ieeexplore.ieee.org/document/1344033). It's fast and effective and works in addition to the luma sort (which remains the single most effective sort). In order to keep lower presets fast though, I've only enabled this for o3 and higher. Results on a set of 190 indexed images at `-o5`: 18,932,727 bytes - master 18,578,306 bytes - PR 18,559,863 bytes - PR + shssoichiro#509 (These images may be particularly suited to alternative sorting methods - the gains here are not necessarily what should be expected on average) Note I looked into the 120 different palette sorting methods from TruePNG, as mentioned in shssoichiro#74 (and seen in action in the Zopfli KrzYmod fork). They're... largely ineffective. The combination of all 120 methods are outperformed by just the existing luma sort plus this new one. That's not to say there's nothing further to be gained from them, but trying to brute force all the combinations definitely seems like a bad idea. There are other algorithms I hope to explore in future... @ace-dent Thought this might interest you UPDATE: I realised a quick tweak to alpha values in the luma sort can provide a great improvement on images with transparency. The following numbers were taken with PR shssoichiro#509 as base. `-o2`: 19,065,549 bytes - base (luma sort) 18,949,747 bytes - modified luma sort `-o5`: 18,922,165 bytes - base (luma sort) 18,559,863 bytes - new sorting algorithm + luma sort 18,544,813 bytes - new sorting algorithm + modified luma sort
This adds a new palette sorting algorithm that attempts to minimise entropy by an approximate solution to the Traveling Salesman Problem. The algorithm comes from "An efficient Re-indexing algorithm for color-mapped images" by Battiato et al (https://ieeexplore.ieee.org/document/1344033).
It's fast and effective and works in addition to the luma sort (which remains the single most effective sort). In order to keep lower presets fast though, I've only enabled this for o3 and higher.
Results on a set of 190 indexed images at
-o5
:18,932,727 bytes - master
18,578,306 bytes - PR
18,559,863 bytes - PR + #509
(These images may be particularly suited to alternative sorting methods - the gains here are not necessarily what should be expected on average)
Note I looked into the 120 different palette sorting methods from TruePNG, as mentioned in #74 (and seen in action in the Zopfli KrzYmod fork). They're... largely ineffective. The combination of all 120 methods are outperformed by just the existing luma sort plus this new one. That's not to say there's nothing further to be gained from them, but trying to brute force all the combinations definitely seems like a bad idea. There are other algorithms I hope to explore in future...
@ace-dent Thought this might interest you
UPDATE: I realised a quick tweak to alpha values in the luma sort can provide a great improvement on images with transparency. The following numbers were taken with PR #509 as base.
-o2
:19,065,549 bytes - base (luma sort)
18,949,747 bytes - modified luma sort
-o5
:18,922,165 bytes - base (luma sort)
18,559,863 bytes - new sorting algorithm + luma sort
18,544,813 bytes - new sorting algorithm + modified luma sort