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

Implement possible size optimizations from Palette sorting from TruePNG #74

Open
1 task
shssoichiro opened this issue Jul 21, 2017 · 3 comments
Open
1 task
Labels
I-Medium Issues that are breaking core functionality, but have a known workaround T-Compression Relates to improving the compression ratio of images T-Feature Requests for a new feature to be added T-Needs Tests

Comments

@shssoichiro
Copy link
Owner

shssoichiro commented Jul 21, 2017

https://css-ig.net/articles/truepng.php

the real good point is the order of entries in palette. it tries to optimize the sorting, sometimes againts logical way, prefering a heavy tRNS chunk — 256 transparent entries where 10 are really useful, but a smaller iDAT, mainly because of the filtering that is used.

TruePNG does some trials to sort the colors to get the smallest file. It tries to sort the colors by popularity, by colors or by luminance. all those trials are done in memory with fast settings (huffman only for compression, filter 0 and 5) and it picks the smallest output.

the first method that TruePNG can use is the ordering by popularity, also used by another programs. it counts the number of time that a color is used in the picture, then move it to the top of palette, and does the same with the next one. this is an effective way to store most of paletted PNG.

[luma] sorting is done according the following calculation 0,299R + 0,587G + 0,114B. colors which have most brightness are moved to the top of the palette. this method is also used by another programs, like PNGOptimizer or OptiPNG. this could be very effective on images which have a lot of gradients.

the palette can be sorted using another combination using the following transformation, consisting to collect the first bit of each RGB bytes of an entry in palette, and does the same with following bits to the end. this can be good for pictures that contains gradients.

according to the version of TruePNG, it also uses another trials with differents methods, that can be done in RGB, YUV or Lab* colorspace, with a different reference color at start.

Method Color space Color reference
Nearest RGB, YUV, Lab* most popular, most bright or most dark color
Nearest (W) RGB, YUV, Lab* most popular, most bright or most dark color
Nearest (N) RGB, YUV, Lab* most popular, most bright or most dark color

Nearest: each subsequent color in the palette is taken as the most similar to the previous one. it is done with Euclidean distance: sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2) [+sqr(A1-A2)]
Nearest (W): « W » means weight, and is the closest color value multiplied by the popularity of color which is indirectly correlated with the context of the image.
Nearest (N): « N » means neighbor, and is the value of the color multiplied by a factor of popularity in the immediate environment.

  • Trials to sort colors in palette for optimal filesize, accounting for transparency as below

TruePNG is considering the alpha values in a palette to sort colors. it is usually better to store all transparents colors at the top of the palette, because of unnecessary tRNS values affected for non-transparent colors.

but in some cases, it is better to have colors sorted, even if it makes a big tRNS chunk, because the sorting could reduce the iDAT with more efficiency. TruePNG adds combinations for sorting transparency values and selects the smaller output.

@shssoichiro shssoichiro added T-Needs Tests T-Feature Requests for a new feature to be added labels Jul 21, 2017
@shssoichiro shssoichiro added the I-Medium Issues that are breaking core functionality, but have a known workaround label Oct 1, 2017
@shssoichiro shssoichiro added the T-Compression Relates to improving the compression ratio of images label Dec 12, 2018
shssoichiro pushed a commit that referenced this issue Jul 11, 2023
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
@ace-dent
Copy link

ace-dent commented Sep 18, 2023

@andrews05 - here's a tough paletted image to add to your corpus!

notify3

@andrews05
Copy link
Collaborator

@ace-dent Nice. What's your analysis so far, how does oxipng fare?

@ace-dent
Copy link

@andrews05 - I'm collecting images that compress better with oxipng- but haven't analysed them yet. For palette sorting, there still doesn't seem to be one program that is consistently the best.

Pr0methean pushed a commit to Pr0methean/oxipng that referenced this issue Dec 1, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-Medium Issues that are breaking core functionality, but have a known workaround T-Compression Relates to improving the compression ratio of images T-Feature Requests for a new feature to be added T-Needs Tests
Projects
None yet
Development

No branches or pull requests

3 participants