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 2923

Summary: Add SDL_PIXELFORMAT_RGBA32 for byte-wise 32bit RGBA data
Product: SDL Reporter: Daniel Gibson <metalcaedes>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: enhancement    
Priority: P2 CC: metalcaedes
Version: HG 2.0   
Hardware: All   
OS: All   
Attachments: patch that adds SDL_PIXELFORMAT_RGBA32
patch that adds SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR
add SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR as aliases
test program (for latest SDL2 hg + my patch), needs stb_image.h
one of the test images I used

Description Daniel Gibson 2015-03-26 03:47:21 UTC
Created attachment 2088 [details]
patch that adds SDL_PIXELFORMAT_RGBA32

Byte-Wise 32bit RGBA pixeldata is quite common, both as output from image decoding libs (like stb_image) and as input for OpenGL.

SDL2 has SDL_PIXELFORMAT_RGBA8888, but it assumes that red is in the (endian specific) least significant byte of an Uint32, while the data I'm talking about always has red in the lowest byte, regardless of endianess.

According to https://wiki.libsdl.org/SDL_CreateRGBSurface and
https://wiki.libsdl.org/SDL_CreateTextureFromSurface to I'm expected to define the RGBA masks for that format myself, differently for little and big endian.
I think a format this common should be less painful to use.

So I added SDL_PIXELFORMAT_RGBA32. The name is analogous to SDL_PIXELFORMAT_RGB24 which does the same for 24bit RGB data.

There's some more discussion about this in https://forums.libsdl.org/viewtopic.php?t=11123
Comment 1 Daniel Gibson 2015-03-26 03:58:56 UTC
This is totally useful with https://bugzilla.libsdl.org/show_bug.cgi?id=2924
Comment 2 Sam Lantinga 2016-10-07 23:55:57 UTC
This seems reasonable, however wouldn't you need the common variations... RGBA, ARGB, BGRA, ABGR, etc?
Comment 3 Sam Lantinga 2016-10-07 23:56:43 UTC
Also you'd probably need to update the various other code that handles the appropriate packed formats to treat these as equivalent.
Comment 4 Daniel Gibson 2016-10-08 00:23:40 UTC
Hmm yeah, adding other common variations sounds useful, are there any more than the ones you listed?

I'm not sure what other code you mean, I thought this was all that's needed?
Note that I intentionally didn't touch any code that maps masks back to pixelformats, to make sure masks still map to the same formats as before (RGBA8888/ABGR8888 depending on endianess) for backwards compatibility.
Comment 5 Daniel Gibson 2016-10-08 01:16:54 UTC
Created attachment 2577 [details]
patch that adds SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR

ok, I updated the patch, now there's SDL_PIXELFORMAT_RGBA32, SDL_PIXELFORMAT_ARGB32, SDL_PIXELFORMAT_BGRA32, SDL_PIXELFORMAT_ABGR32
That's all the available 4 channel SDL_ARRAYORDERs.

/Maybe/ SDL_ARRAYORDER_RGBX etc would be useful, but currently they don't exist so I didn't create SDL_PIXELFORMAT_RGBX32 etc.

Anyway, if I should have modified more places in the code to support the new formats (without breaking backwards compatibility), please tell me and I'll update the patch accordingly.
Comment 6 Sam Lantinga 2016-10-08 01:47:59 UTC
Look for any code that references the packed pixel formats like SDL_PIXELFORMAT_RGBA8888 - they might also need to handle the new formats.
Comment 7 Daniel Gibson 2016-10-08 02:17:58 UTC
Ok, I looked SDL_PIXELFORMAT_RGB24 (which doesn't have a packed equivalent), so it looked like there wasn't that much to do.

There's things like the blitting table which could be done by mapping SDL_PIXELFORMAT_*32 to the corresponding (depending on endianess) SDL_PIXELFORMAT_*8888 in SDL_ChooseBlitFunc()..

But then there's RendererInfo::texture_formats and everything related like the format passed to SDL_CreateTexture(), (and possibly other places that need to decide whether a format of a surface or supplied by the user is valid).. that needs some more thought to get right.

So.. well.. damn, doing this properly doesn't seem trivial :-/
Comment 8 Sam Lantinga 2016-10-08 02:20:54 UTC
*grin*

You've got a good start! :)
Comment 9 Daniel Gibson 2016-10-08 02:46:39 UTC
Of course, there's also the easy way:
in SDL_pixels.h
enum {
	...

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_RGBA8888,
	SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_ARGB8888,
	SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_BGRA8888,
	SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_ABGR8888,
#else
	SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_ABGR8888,
	SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_BGRA8888,
	SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_ARGB8888,
	SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_RGBA8888,
#endif
	...
};

This might be a bit surprising when setting one format and printing it yields the string of the other, but sooner or later the user will stumble upon the equivalence anyway
Comment 10 Sam Lantinga 2016-10-08 08:31:50 UTC
That might actually be a better approach. Can you try it out and create a patch if that works?
Comment 11 Daniel Gibson 2016-10-09 00:24:44 UTC
Created attachment 2578 [details]
add SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR as aliases

Ok, I followed the simple approach of just making SDL_PIXELFORMAT_RGBA32 an alias of SDL_PIXELFORMAT_RGBA8888/SDL_PIXELFORMAT_ABGR8888, depending on endianess. And I did the same for SDL_PIXELFORMAT_ARGB32, .._BGRA, .._ABGR.


Seems to work fine with my little test program.

SDL_GetPixelFormatName() will of course return SDL_PIXELFORMAT_RGBA8888 (or SDL_PIXELFORMAT_ABGR8888) instead of SDL_PIXELFORMAT_RGBA32, but as long as that's mentioned in the docs it shouldn't be a problem.

I documented them in the header like
/**< endianess-specific alias for byte-wise 32bit RGBA data */
I hope the wording is clear enough, otherwise I'm open for suggestions of course :)
Comment 12 Daniel Gibson 2016-10-09 00:26:46 UTC
Created attachment 2579 [details]
test program (for latest SDL2 hg + my patch), needs stb_image.h
Comment 13 Daniel Gibson 2016-10-09 00:27:23 UTC
Created attachment 2580 [details]
one of the test images I used
Comment 14 Sam Lantinga 2016-10-12 06:19:41 UTC
Okay, this is in!
https://hg.libsdl.org/SDL/rev/c6d79a1bec47

It also cleaned up a few places where arrays of color bytes is exactly what we wanted. :)
Comment 15 Daniel Gibson 2016-10-13 00:32:47 UTC
Cool, thanks for getting this into 2.0.5! :-)
Comment 16 Sam Lantinga 2016-10-13 01:49:06 UTC
You're welcome, thanks for contributing! :)