Skip to content

Commit a9cda7b

Browse files
committed
Improve palette handling in STB backend
1 parent b9cf035 commit a9cda7b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/IMG_stb.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
169169
int i;
170170
Uint8 *palette_bytes = (Uint8 *)palette_colors;
171171

172+
/* Reduce palette->ncolors to minimum possible value */
173+
int palette_ncolors = 1;
174+
for (i = 0; i < w * h; i++) {
175+
int cur_best = pixels[i] + 1;
176+
if (palette_ncolors < cur_best) {
177+
palette_ncolors = cur_best;
178+
}
179+
}
180+
if (palette_ncolors < palette->ncolors) {
181+
palette->ncolors = palette_ncolors;
182+
}
183+
172184
for (i = 0; i < palette->ncolors; i++) {
173185
palette->colors[i].r = *palette_bytes++;
174186
palette->colors[i].g = *palette_bytes++;
@@ -191,6 +203,8 @@ SDL_Surface *IMG_LoadSTB_RW(SDL_RWops *src)
191203
SDL_FreeSurface(surface);
192204
surface = converted;
193205
} else if (has_colorkey) {
206+
/* remove redundant pixel alpha before setting colorkey */
207+
palette->colors[colorkey_index].a = SDL_ALPHA_OPAQUE;
194208
SDL_SetColorKey(surface, SDL_TRUE, colorkey_index);
195209
}
196210
}

0 commit comments

Comments
 (0)