From faf1ccf7ee0cdafc78ec822d83efaab35d58f320 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 d3aa3b15..118fcc12 100644
--- a/src/IMG_stb.c
+++ b/src/IMG_stb.c
@@ -150,6 +150,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;
@@ -160,8 +161,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: