SDL: When changing surface format, use the default colorspace for the new format

From ba86d9f235e55f3bee4724679b2ee5cf3b072d29 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 2 Feb 2024 17:56:33 -0800
Subject: [PATCH] When changing surface format, use the default colorspace for
 the new format

---
 src/video/SDL_surface.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index aa51e4e53616..8b1f21fcdb1f 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -1407,7 +1407,7 @@ static SDL_Surface *SDL_ConvertSurfaceWithPixelFormatAndColorspace(SDL_Surface *
             tmp->map->info.flags &= ~SDL_COPY_COLORKEY;
 
             /* Conversion of the colorkey */
-            tmp2 = SDL_ConvertSurface(tmp, format);
+            tmp2 = SDL_ConvertSurfaceWithPixelFormatAndColorspace(tmp, format, colorspace);
             if (!tmp2) {
                 SDL_DestroySurface(tmp);
                 SDL_DestroySurface(convert);
@@ -1461,12 +1461,35 @@ SDL_Surface *SDL_DuplicateSurface(SDL_Surface *surface)
 
 SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, const SDL_PixelFormat *format)
 {
-    return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, SDL_COLORSPACE_UNKNOWN);
+    SDL_Colorspace colorspace;
+
+    if (!surface) {
+        SDL_InvalidParamError("surface");
+        return NULL;
+    }
+
+    if (!format) {
+        SDL_InvalidParamError("format");
+        return NULL;
+    }
+
+    colorspace = SDL_GetDefaultColorspaceForFormat(format->format);
+
+    return SDL_ConvertSurfaceWithPixelFormatAndColorspace(surface, format, colorspace);
 }
 
 SDL_Surface *SDL_ConvertSurfaceFormat(SDL_Surface *surface, Uint32 pixel_format)
 {
-    return SDL_ConvertSurfaceFormatAndColorspace(surface, pixel_format, SDL_COLORSPACE_UNKNOWN);
+    SDL_Colorspace colorspace;
+
+    if (!surface) {
+        SDL_InvalidParamError("surface");
+        return NULL;
+    }
+
+    colorspace = SDL_GetDefaultColorspaceForFormat(pixel_format);
+
+    return SDL_ConvertSurfaceFormatAndColorspace(surface, pixel_format, colorspace);
 }
 
 SDL_Surface *SDL_ConvertSurfaceFormatAndColorspace(SDL_Surface *surface, Uint32 pixel_format, SDL_Colorspace colorspace)