libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.33k stars 1.73k forks source link

Solve antialiasing issues applying alpha bleeding when loading / converting textures #6410

Open panreyes opened 1 year ago

panreyes commented 1 year ago

This is a suggestion / enhancement, and it might not even fit SDL but SDL_Image instead.

I am pretty sure you know about this kind of antialiasing issues (notice the grey rectangle of a texture rendered in the foreground): image

They occur when using SDL_HINT_RENDER_SCALE_QUALITY "linear" or "best". They occur because transparent pixels are affecting the edges of the non-transparent ones, because those transparent pixels are oftenly are stored by programs as RGBA(0,0,0,0) or RGBA(255,255,255,0), and antialiasing uses those pixels to calculate the smoothing of those edges.

These antialiasing issues are a natural thing to the feature, but they can be easily mitigated by using "alpha bleeding". Here's a good explanation of Alpha bleeding

So, my proposal/suggestion: Could SDL apply alpha bleeding to surfaces/textures when loading/converting them?

I currently use this small tool to solve this issue in my PNGs, but could be good if I could apply alpha bleeding directly in SDL, as sometimes I generate new bitmaps in runtime: alpha-bleeding-d

Here's the code and resources to reproduce the issue: antialiasing_bug.c.txt background.png foreground.png

panreyes commented 1 year ago

As an example, this is how Godot solves this when importing images: https://github.com/godotengine/godot/blob/master/core/io/image.cpp#L3619