We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3657 - Color-key doesn't work when an alpha channel is present
Summary: Color-key doesn't work when an alpha channel is present
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: 2.0.1
Hardware: x86_64 macOS 10.12
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-26 21:30 UTC by Trevor Elliott
Modified: 2019-01-19 15:49 UTC (History)
1 user (show)

See Also:


Attachments
Ping with color-key transparency, and an alpha channel (2.58 KB, image/png)
2017-05-26 21:30 UTC, Trevor Elliott
Details
testcase (2.50 KB, text/x-c++src)
2017-09-14 11:53 UTC, Sylvain
Details
image (80.13 KB, image/bmp)
2017-09-14 11:53 UTC, Sylvain
Details
patch (978 bytes, patch)
2017-09-14 11:58 UTC, Sylvain
Details | Diff
test-case v2 (2.34 KB, text/x-csrc)
2019-01-19 15:38 UTC, Sylvain
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Trevor Elliott 2017-05-26 21:30:50 UTC
Created attachment 2746 [details]
Ping with color-key transparency, and an alpha channel

Color-key transparency doesn't seem to work if the loaded PNG has an alpha channel present. Oddly, this only seems to happen on OSX, with color-key transparency working fine on linux and windows, despite the presence of the alpha channel.

I've attached a PNG from the klystrack project (https://github.com/kometbomb/klystrack/) which is where I noticed the problem.

NOTE: their image loading code is in a separate repository:
https://github.com/kometbomb/klystron/blob/master/src/gfx/gfx.c#L113-L154
Comment 1 Sam Lantinga 2017-09-12 07:35:49 UTC
The attached image doesn't appear to have a colorkey, at least with libpng 1.6.32. Can you verify and attach another image if needed?
Comment 2 Sylvain 2017-09-12 12:17:52 UTC
I think, he meant that setting the colorkey isn't working on macosx, because his image has an alpha channel.

I tried, and actually it did not work on macosx: the pink colorkey is visible. but not on linux (pink became transparent).

setting the colorkey :

int ck = SDL_MapRGBA(surf->format, 0xff, 0x00, 0xff, 0x00);
SDL_SetColorKey(surf, SDL_TRUE, ck);
 
(on linux, whatever the alpha value I put, the colorkey is alway set).
Comment 3 Sylvain 2017-09-14 11:53:07 UTC
Created attachment 2941 [details]
testcase

In fact that issue can be reproduced on any platform.
It occurs if the surface (with alpha and colorkey) has the same format of the renderer.

here's a testcase.
Comment 4 Sylvain 2017-09-14 11:53:53 UTC
Created attachment 2942 [details]
image

BMP image for the test case.
Comment 5 Sylvain 2017-09-14 11:58:19 UTC
Created attachment 2943 [details]
patch

Here's a patch to force the path to Convert the surface, 
so that the internal SDL_ConvertColorkeyToAlpha() is called, without modifying the user surface.
Comment 6 Sylvain 2017-09-14 14:38:56 UTC
Some note, this patch may add some overhead, since surfaces with colorkey will always be "converted" by SDL_CreateTextureFromSurface, even if they have the same format as the renderer and the color-key is correct.
Comment 7 Sylvain 2017-10-22 13:22:57 UTC
this affects SDL and not SDL_image
Comment 8 Sylvain 2019-01-19 15:38:37 UTC
Created attachment 3578 [details]
test-case v2

Updated the test-case so that it doesn't need an image.

A surface with Alpha and colorkey is created. Then its Texture is rendered on screen.

Whether the surface format is A-RGB-8888 or A-BGR-8888, it's transparent or not.

In one case: surface format is the same format as renderer format. it's uploaded directly (and isn't transparent).

In the other case, surface and renderer formats are differents, so an intermediate conversion is done, which has also the benefit or converting colorkey to alpha (with SDL_ConvertColorKeyToAlpha())
Comment 9 Sylvain 2019-01-19 15:49:07 UTC
Fixed in https://hg.libsdl.org/SDL/rev/d4d5e1272239

When surface format is the same as renderer format, it still needs an
intermediate conversion to transform colorkey to alpha.