# This bug report was migrated from our old Bugzilla tracker.
These attachments… are available in the static archive:
* ~~[patch that adds SDL_PIXELFORMAT_RGBA32 (sdl2_SDL_PIXELFORMAT_RGBA32.diff, text/plain, 2015-03-26 03:47:21 +0000, 1667 bytes)](https://bugzilla.libsdl.org/attachment.cgi?id=2088)~~
* ~~[patch that adds SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR (sdl2_SDL_PIXELFORMAT_RGBA32_andmore.diff, text/plain, 2016-10-08 01:16:54 +0000, 3332 bytes)](https://bugzilla.libsdl.org/attachment.cgi?id=2577)~~
* [add SDL_PIXELFORMAT_RGBA32, also ARGB, BGRA, ABGR as aliases (SDL_PIXELFORMAT_RGBA32_andmore_simple.diff, text/plain, 2016-10-09 00:24:44 +0000, 1790 bytes)](https://bugzilla.libsdl.org/attachment.cgi?id=2578)
* [test program (for latest SDL2 hg + my patch), needs stb_image.h (test.c, text/x-c, 2016-10-09 00:26:46 +0000, 3962 bytes)](https://bugzilla.libsdl.org/attachment.cgi?id=2579)
* [one of the test images I used (RGBA.png, image/png, 2016-10-09 00:27:23 +0000, 11495 bytes)](https://bugzilla.libsdl.org/attachment.cgi?id=2580)
**Reported in version:** HG 2.0
**Reported for operating system, platform:** All, All
# Comments on the original bug report:
On 2015-03-26 03:47:21 +0000, Daniel Gibson wrote:
> Created attachment 2088
> 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
On 2015-03-26 03:58:56 +0000, Daniel Gibson wrote:
> This is totally useful with https://bugzilla.libsdl.org/show_bug.cgi?id=2924
On 2016-10-07 23:55:57 +0000, Sam Lantinga wrote:
> This seems reasonable, however wouldn't you need the common variations... RGBA, ARGB, BGRA, ABGR, etc?
On 2016-10-07 23:56:43 +0000, Sam Lantinga wrote:
> Also you'd probably need to update the various other code that handles the appropriate packed formats to treat these as equivalent.
On 2016-10-08 00:23:40 +0000, Daniel Gibson wrote:
> 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.
On 2016-10-08 01:16:54 +0000, Daniel Gibson wrote:
> Created attachment 2577
> 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.
On 2016-10-08 01:47:59 +0000, Sam Lantinga wrote:
> Look for any code that references the packed pixel formats like SDL_PIXELFORMAT_RGBA8888 - they might also need to handle the new formats.
On 2016-10-08 02:17:58 +0000, Daniel Gibson wrote:
> 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 :-/
On 2016-10-08 02:20:54 +0000, Sam Lantinga wrote:
> *grin*
>
> You've got a good start! :)
On 2016-10-08 02:46:39 +0000, Daniel Gibson wrote:
> 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
On 2016-10-08 08:31:50 +0000, Sam Lantinga wrote:
> That might actually be a better approach. Can you try it out and create a patch if that works?
On 2016-10-09 00:24:44 +0000, Daniel Gibson wrote:
> Created attachment 2578
> 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 :)
On 2016-10-09 00:26:46 +0000, Daniel Gibson wrote:
> Created attachment 2579
> test program (for latest SDL2 hg + my patch), needs stb_image.h
On 2016-10-09 00:27:23 +0000, Daniel Gibson wrote:
> Created attachment 2580
> one of the test images I used
On 2016-10-12 06:19:41 +0000, Sam Lantinga wrote:
> 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. :)
On 2016-10-13 00:32:47 +0000, Daniel Gibson wrote:
> Cool, thanks for getting this into 2.0.5! :-)
On 2016-10-13 01:49:06 +0000, Sam Lantinga wrote:
> You're welcome, thanks for contributing! :)