diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c index 1575d99..9ac4cca 100644 --- a/src/sdl2_compat.c +++ b/src/sdl2_compat.c @@ -4232,6 +4232,7 @@ SDL_DECLSPEC SDL2_Surface * SDLCALL SDL_ConvertSurface(SDL2_Surface *surface, const SDL2_PixelFormat *format, Uint32 flags) { SDL_Palette *palette = NULL; + SDL_PixelFormat pixel_format; SDL2_Surface *result; (void) flags; /* SDL3 removed the (unused) `flags` argument */ @@ -4245,6 +4246,12 @@ SDL_ConvertSurface(SDL2_Surface *surface, const SDL2_PixelFormat *format, Uint32 return NULL; } + pixel_format = SDL3_GetPixelFormatForMasks(format->BitsPerPixel, format->Rmask, format->Gmask, format->Bmask, format->Amask); + if (pixel_format == SDL_PIXELFORMAT_UNKNOWN) { + SDL3_InvalidParamError("format"); + return NULL; + } + if (format->palette) { // This conversion is going to assign the new surface this palette, // but it might be on the stack, so always allocate a new one to be @@ -4257,7 +4264,7 @@ SDL_ConvertSurface(SDL2_Surface *surface, const SDL2_PixelFormat *format, Uint32 SDL3_SetPaletteColors(palette, format->palette->colors, 0, ncolors); } - result = Surface3to2(SDL3_ConvertSurfaceAndColorspace(Surface2to3(surface), (SDL_PixelFormat)format->format, palette, SDL_COLORSPACE_SRGB, 0)); + result = Surface3to2(SDL3_ConvertSurfaceAndColorspace(Surface2to3(surface), pixel_format, palette, SDL_COLORSPACE_SRGB, 0)); if (palette) { SDL3_DestroyPalette(palette);