This PR massively speeds up mask.from_surface() when dealing with the threshold argument (for 24 and 32 bit surfaces, about a 12% improvement for 16 and lower).
It achieves so by directly accessing the pixel's alpha channels instead of using SDL's functionality which is not only slow but actually did more work that we actually need for this operation by also returning the rgb channels.
With this new strategy we just jump from pixel's alpha to pixel's alpha and check that against the threshold.
I've also added a fast path for 24-bit surfaces bringing an astonishing 737 times improved runtime since we now use bitmask_fill.
I've also added a validity check for the threshold argument which wasn't there before.
This PR massively speeds up
mask.from_surface()
when dealing with the threshold argument (for 24 and 32 bit surfaces, about a 12% improvement for 16 and lower).It achieves so by directly accessing the pixel's alpha channels instead of using SDL's functionality which is not only slow but actually did more work that we actually need for this operation by also returning the rgb channels.
With this new strategy we just jump from pixel's alpha to pixel's alpha and check that against the threshold.
I've also added a fast path for 24-bit surfaces bringing an astonishing 737 times improved runtime since we now use
bitmask_fill
.I've also added a validity check for the threshold argument which wasn't there before.
here are the results:![image](https://github.com/pygame-community/pygame-ce/assets/103119829/aa6311bd-35dc-4ce6-a84c-2199a950a4b2)
made with this test program:
The data utils file (as txt): data_utils.txt