Fixed the colorspace for YUV textures using native RGB representations

Fixes https://github.com/libsdl-org/SDL/issues/10624
This commit is contained in:
Sam Lantinga 2024-08-29 20:51:03 -07:00
parent efc98062af
commit 16fb8e54cb
3 changed files with 7 additions and 4 deletions

View file

@ -1453,7 +1453,7 @@ SDL_Texture *SDL_CreateTextureWithProperties(SDL_Renderer *renderer, SDL_Propert
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
#if SDL_HAVE_YUV #if SDL_HAVE_YUV
texture->yuv = SDL_SW_CreateYUVTexture(format, w, h); texture->yuv = SDL_SW_CreateYUVTexture(texture->format, texture->colorspace, w, h);
#else #else
SDL_SetError("SDL not built with YUV support"); SDL_SetError("SDL not built with YUV support");
#endif #endif

View file

@ -28,7 +28,7 @@
#include "../video/SDL_blit.h" #include "../video/SDL_blit.h"
#include "../video/SDL_yuv_c.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; 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->format = format;
swdata->colorspace = colorspace;
swdata->target_format = SDL_PIXELFORMAT_UNKNOWN; swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
swdata->w = w; swdata->w = w;
swdata->h = h; swdata->h = h;
@ -368,6 +369,7 @@ bool SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect, SDL
if (!swdata->display) { if (!swdata->display) {
return false; return false;
} }
swdata->target_format = target_format;
} }
if (!swdata->stretch) { if (!swdata->stretch) {
swdata->stretch = SDL_CreateSurface(swdata->w, swdata->h, target_format); 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; pixels = swdata->stretch->pixels;
pitch = swdata->stretch->pitch; 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; return false;
} }
if (stretch) { if (stretch) {

View file

@ -29,6 +29,7 @@
struct SDL_SW_YUVTexture struct SDL_SW_YUVTexture
{ {
SDL_PixelFormat format; SDL_PixelFormat format;
SDL_Colorspace colorspace;
SDL_PixelFormat target_format; SDL_PixelFormat target_format;
int w, h; int w, h;
Uint8 *pixels; Uint8 *pixels;
@ -44,7 +45,7 @@ struct SDL_SW_YUVTexture
typedef struct SDL_SW_YUVTexture 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_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_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, extern bool SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,