SDL: Fixed the colorspace for YUV textures using native RGB representations

From 16fb8e54cb9757c5cf3310cf7f3a7bee11eaa88a Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 29 Aug 2024 20:51:03 -0700
Subject: [PATCH] Fixed the colorspace for YUV textures using native RGB
 representations

Fixes https://github.com/libsdl-org/SDL/issues/10624
---
 src/render/SDL_render.c   | 2 +-
 src/render/SDL_yuv_sw.c   | 6 ++++--
 src/render/SDL_yuv_sw_c.h | 3 ++-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index d00257fa063f4..1f757cdd1d201 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -1453,7 +1453,7 @@ SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_Propert
 
         if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
 #if SDL_HAVE_YUV
-            texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
+            texture->yuv = SDL_SW_CreateYUVTexture(texture->format, texture->colorspace, w, h);
 #else
             SDL_SetError("SDL not built with YUV support");
 #endif
diff --git a/src/render/SDL_yuv_sw.c b/src/render/SDL_yuv_sw.c
index 18eaba20bc9bc..2b08df2192571 100644
--- a/src/render/SDL_yuv_sw.c
+++ b/src/render/SDL_yuv_sw.c
@@ -28,7 +28,7 @@
 #include "../video/SDL_blit.h"
 #include "../video/SDL_yuv_c.h"
 
-SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, int w, int h)
+SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, SDL_Colorspace colorspace, int w, int h)
 {
     SDL_SW_YUVTexture *swdata;
 
@@ -52,6 +52,7 @@ SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, int w, int h)
     }
 
     swdata->format = format;
+    swdata->colorspace = colorspace;
     swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
     swdata->w = w;
     swdata->h = h;
@@ -368,6 +369,7 @@ bool SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect, SDL
             if (!swdata->display) {
                 return false;
             }
+            swdata->target_format = target_format;
         }
         if (!swdata->stretch) {
             swdata->stretch = SDL_CreateSurface(swdata->w, swdata->h, target_format);
@@ -378,7 +380,7 @@ bool SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect, SDL
         pixels = swdata->stretch->pixels;
         pitch = swdata->stretch->pitch;
     }
-    if (!SDL_ConvertPixels(swdata->w, swdata->h, swdata->format, swdata->planes[0], swdata->pitches[0], target_format, pixels, pitch)) {
+    if (!SDL_ConvertPixelsAndColorspace(swdata->w, swdata->h, swdata->format, swdata->colorspace, 0, swdata->planes[0], swdata->pitches[0], target_format, SDL_COLORSPACE_SRGB, 0, pixels, pitch)) {
         return false;
     }
     if (stretch) {
diff --git a/src/render/SDL_yuv_sw_c.h b/src/render/SDL_yuv_sw_c.h
index 026989983a83c..76f2e5708371b 100644
--- a/src/render/SDL_yuv_sw_c.h
+++ b/src/render/SDL_yuv_sw_c.h
@@ -29,6 +29,7 @@
 struct SDL_SW_YUVTexture
 {
     SDL_PixelFormat format;
+    SDL_Colorspace colorspace;
     SDL_PixelFormat target_format;
     int w, h;
     Uint8 *pixels;
@@ -44,7 +45,7 @@ struct SDL_SW_YUVTexture
 
 typedef struct SDL_SW_YUVTexture SDL_SW_YUVTexture;
 
-extern SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, int w, int h);
+extern SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, SDL_Colorspace colorspace, int w, int h);
 extern bool SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture *swdata, void **pixels, int *pitch);
 extern bool SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect, const void *pixels, int pitch);
 extern bool SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,