SDL_image: Fixed decoding CMYK JPG images (6eb7e)

From 6eb7e31e595a47aee0be02a233448169542e7bb3 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 8 Jan 2026 11:30:37 -0800
Subject: [PATCH] Fixed decoding CMYK JPG images

Fixes https://github.com/libsdl-org/SDL_image/issues/410

(cherry picked from commit edddd6af4a9bc98658ac8d7264fef246d5fbaeae)
(thanks @s09bQ5!)
---
 src/IMG_jpg.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/IMG_jpg.c b/src/IMG_jpg.c
index 4b6b8f587..9b677b1a5 100644
--- a/src/IMG_jpg.c
+++ b/src/IMG_jpg.c
@@ -372,7 +372,7 @@ static SDL_bool LIBJPEG_LoadJPG_RW(SDL_RWops *src, struct loadjpeg_vars *vars)
         lib.jpeg_calc_output_dimensions(&vars->cinfo);
 
         /* Allocate an output surface to hold the image */
-        vars->surface = SDL_CreateRGBSurfaceWithFormat(0, vars->cinfo.output_width, vars->cinfo.output_height, 0, SDL_PIXELFORMAT_BGRA32);
+        vars->surface = SDL_CreateRGBSurfaceWithFormat(0, vars->cinfo.output_width, vars->cinfo.output_height, 0, SDL_PIXELFORMAT_RGBA32);
     } else {
         /* Set 24-bit RGB output */
         vars->cinfo.out_color_space = JCS_RGB;
@@ -404,6 +404,17 @@ static SDL_bool LIBJPEG_LoadJPG_RW(SDL_RWops *src, struct loadjpeg_vars *vars)
     lib.jpeg_finish_decompress(&vars->cinfo);
     lib.jpeg_destroy_decompress(&vars->cinfo);
 
+    if (vars->cinfo.num_components == 4) {
+        // The CMYK image is essentially RGBA composed over black
+        SDL_Surface *output = SDL_CreateRGBSurfaceWithFormat(0, vars->cinfo.output_width, vars->cinfo.output_height, 0, SDL_PIXELFORMAT_RGB24);
+        if (!output) {
+            return SDL_FALSE;
+        }
+
+        SDL_BlitSurface(vars->surface, NULL, output, NULL);
+        SDL_FreeSurface(vars->surface);
+        vars->surface = output;
+    }
     return SDL_TRUE;
 }