From 48d0e13a44523c0c28f7b964df89ac07ff0d9d9a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 25 Mar 2025 10:15:44 -0700
Subject: [PATCH] Get the pixel format from masks if not specified
Fixes https://github.com/libsdl-org/sdl2-compat/issues/429
---
src/sdl2_compat.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index 11809d3..f50a4d6 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -9696,10 +9696,19 @@ SDL_SetPixelFormatPalette(SDL2_PixelFormat *format, SDL_Palette *palette)
return 0;
}
+static const SDL_PixelFormatDetails *GetPixelFormatDetails(const SDL2_PixelFormat *format2)
+{
+ SDL_PixelFormat format = (SDL_PixelFormat)format2->format;
+ if (format == SDL_PIXELFORMAT_UNKNOWN) {
+ format = SDL3_GetPixelFormatForMasks(format2->BitsPerPixel, format2->Rmask, format2->Gmask, format2->Bmask, format2->Amask);
+ }
+ return SDL3_GetPixelFormatDetails(format);
+}
+
SDL_DECLSPEC Uint32 SDLCALL
SDL_MapRGB(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b)
{
- const SDL_PixelFormatDetails *format = SDL3_GetPixelFormatDetails((SDL_PixelFormat)format2->format);
+ const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
if (!format) {
return 0;
}
@@ -9709,7 +9718,7 @@ SDL_MapRGB(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b)
SDL_DECLSPEC Uint32 SDLCALL
SDL_MapRGBA(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
- const SDL_PixelFormatDetails *format = SDL3_GetPixelFormatDetails((SDL_PixelFormat)format2->format);
+ const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
if (!format) {
return 0;
}
@@ -9719,7 +9728,7 @@ SDL_MapRGBA(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
SDL_DECLSPEC void SDLCALL
SDL_GetRGB(Uint32 pixel, const SDL2_PixelFormat *format2, Uint8 * r, Uint8 * g, Uint8 * b)
{
- const SDL_PixelFormatDetails *format = SDL3_GetPixelFormatDetails((SDL_PixelFormat)format2->format);
+ const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
if (!format) {
*r = *g = *b = 0;
return;
@@ -9730,7 +9739,7 @@ SDL_GetRGB(Uint32 pixel, const SDL2_PixelFormat *format2, Uint8 * r, Uint8 * g,
SDL_DECLSPEC void SDLCALL
SDL_GetRGBA(Uint32 pixel, const SDL2_PixelFormat *format2, Uint8 * r, Uint8 * g, Uint8 * b, Uint8 *a)
{
- const SDL_PixelFormatDetails *format = SDL3_GetPixelFormatDetails((SDL_PixelFormat)format2->format);
+ const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
if (!format) {
*r = *g = *b = *a = 0;
return;