Texture blend artifacts

While experimenting with compositing images in SDL2 on Linux, I get a dark edge around my textures. In the image below, you can see the top example, where I composited the cyan image on top of the blue/magenta image in GIMP, looks correct but the bottom example, which is done using SDL_RenderCopy, is darker around the edges.

I tried premultiplying the the colors in the cyan image and setting the blend function as such…

SDL_SetTextureBlendMode(texture,
SDL_ComposeCustomBlendMode(
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE,
SDL_BLENDOPERATION_ADD));

…but that produces the exact same thing.

Can anyone tell me how to get proper alpha blending?

This looks like a color correction / gamma issue. Premultiplying the alpha channel only helps with issues caused by texture filtering (that can cause similar artifacts).

I tested same colors with gimp and funnily I’m actually getting the dark borders there too. Seems that support for linear color spaces is implemented in gimp 2.9 and I was using 2.8.x. Are you using 2.9?

See https://ninedegreesbelow.com/photography/linear-gamma-blur-normal-blend.html and http://ssp.impulsetrain.com/gamma-premult.html for more general info on the issue.

There doesn’t seem to be a super simple solution to the problem when using the SDL api for drawing. When drawing with OpenGL directly you can do color correction in a shader or for sRGB output just call:

glEnable(GL_FRAMEBUFFER_SRGB);

It’s probably possible to use GL_FRAMEBUFFER_SRGB with the SDL drawing api too, but you have to force SDL to use OpenGL as the rendering backend.

1 Like

Thanks for the info. I was using gimp 2.9.

I verified that is exactly the issue by doing a software blend and then another software blend using gamma corrected colors.