SDL_image: Enable alpha blending for palettized images with alpha

From 2e5662a720f413ae823f7074b754437b62d5581d Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 11 Dec 2023 09:45:09 -0800
Subject: [PATCH] Enable alpha blending for palettized images with alpha

Fixes https://github.com/libsdl-org/SDL_image/issues/402
---
 src/IMG_stb.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/IMG_stb.c b/src/IMG_stb.c
index be3479a7..464b5e36 100644
--- a/src/IMG_stb.c
+++ b/src/IMG_stb.c
@@ -145,6 +145,7 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
             SDL_PIXELFORMAT_INDEX8
         );
         if (surface) {
+            SDL_bool has_alpha = SDL_FALSE;
             SDL_Palette *palette = surface->format->palette;
             if (palette) {
                 int i;
@@ -155,8 +156,14 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
                     palette->colors[i].g = *palette_bytes++;
                     palette->colors[i].b = *palette_bytes++;
                     palette->colors[i].a = *palette_bytes++;
+                    if (palette->colors[i].a != SDL_ALPHA_OPAQUE) {
+                        has_alpha = SDL_TRUE;
+                    }
                 }
             }
+            if (has_alpha) {
+                SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
+            }
 
             /* FIXME: This sucks. It'd be better to allocate the surface first, then
              * write directly to the pixel buffer: