Optimize the lib ColorThief
See original GitHub issueCollapse will use the method in ColorThief to calculate the theme color of the background image when switching game region. But the performance of this third-party library is extremely bad. It takes 169 ms when parsing the official launcher image (1280x730), and only more when parsing custom larger images.
Get palette list with colorCount=255
and quality=3
, Same parameters as collapse.
Ignore the time consumption and memory allocation for image decoding
ColorThief can be optimized as follows:
- Use pointer to get pixels with color instead of
Bitmap.GetPixel
- Use one-dimensional arrays
byte[]
to store pixels instead of interleaved arraysbyte[][]
- Directly specify the number of pixels when splitting the vbox, instead of repeated calculations
After I optimized the code, the time consumed and the amount of memory allocated were greatly reduced. To make it simpler to write the code, each pixel of the image will be included, so the parameters: colorCount=255
and quality=1
.
Could I remove the submodule ColorThief and commit code in this project?
Issue Analytics
- State:
- Created 4 months ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
Hi @Scighost,
Thank you for the information. As per your suggestion and some additional changes including:
int
.IEnumerable<int>
intoMmcq.Quantize()
and processing the pixels on-the-fly.By enumerating the pixel directly instead of pre-allocating it first with this “v2” code, we got a really significant reduction memory allocation from initially
352 MB
to21.65 MB
at “v1” changes and finally130 KB
at “v2” changes withquality=1
Note
The
quality=1
on this new “v2” code has a bit performance degradation compared to the last modification due to enumeration overhead. But the rest withquality > 1
got a bit performance improvement. Hope this can be improved in the future.That being said, thank you for your amazing contribution on this optimization! Please allow me to merge the changes into
ColorThief
Submodule repo and close this issue.Good job!
There are two more small areas that can be optimized.
Of course, it is very good now, and I will not pull request for ColorThief again.