Added SDL_CreateSurfacePalette()

This commit is contained in:
Sam Lantinga
2024-07-13 14:18:35 -07:00
parent 9379e2eb8d
commit 650271af46
9 changed files with 87 additions and 18 deletions

View File

@@ -79,6 +79,7 @@ SDL3_0.0.0 {
SDL_CreateStorageDirectory;
SDL_CreateSurface;
SDL_CreateSurfaceFrom;
SDL_CreateSurfacePalette;
SDL_CreateSystemCursor;
SDL_CreateTLS;
SDL_CreateTexture;

View File

@@ -104,6 +104,7 @@
#define SDL_CreateStorageDirectory SDL_CreateStorageDirectory_REAL
#define SDL_CreateSurface SDL_CreateSurface_REAL
#define SDL_CreateSurfaceFrom SDL_CreateSurfaceFrom_REAL
#define SDL_CreateSurfacePalette SDL_CreateSurfacePalette_REAL
#define SDL_CreateSystemCursor SDL_CreateSystemCursor_REAL
#define SDL_CreateTLS SDL_CreateTLS_REAL
#define SDL_CreateTexture SDL_CreateTexture_REAL

View File

@@ -124,6 +124,7 @@ SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateSoftwareRenderer,(SDL_Surface *a),(a),re
SDL_DYNAPI_PROC(int,SDL_CreateStorageDirectory,(SDL_Storage *a, const char *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateSurface,(int a, int b, SDL_PixelFormat c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_Surface*,SDL_CreateSurfaceFrom,(int a, int b, SDL_PixelFormat c, void *d, int e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(SDL_Palette*,SDL_CreateSurfacePalette,(SDL_Surface *a),(a),return)
SDL_DYNAPI_PROC(SDL_Cursor*,SDL_CreateSystemCursor,(SDL_SystemCursor a),(a),return)
SDL_DYNAPI_PROC(SDL_TLSID,SDL_CreateTLS,(void),(),return)
SDL_DYNAPI_PROC(SDL_Texture*,SDL_CreateTexture,(SDL_Renderer *a, SDL_PixelFormat b, int c, int d, int e),(a,b,c,d,e),return)

View File

@@ -439,8 +439,10 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
/* Load the palette, if any */
if (SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
int max_colors = (1 << SDL_BITSPERPIXEL(surface->format));
SDL_Palette *palette;
SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
if (!palette) {
goto done;
}
if (SDL_SeekIO(src, fp_offset + 14 + biSize, SDL_IO_SEEK_SET) < 0) {
SDL_SetError("Error seeking in datastream");
@@ -456,21 +458,17 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
biClrUsed = 1 << biBitCount;
}
if (biClrUsed > (Uint32)max_colors) {
if (biClrUsed > (Uint32)palette->ncolors) {
biClrUsed = 1 << biBitCount; /* try forcing it? */
if (biClrUsed > (Uint32)max_colors) {
if (biClrUsed > (Uint32)palette->ncolors) {
SDL_SetError("Unsupported or incorrect biClrUsed field");
goto done;
}
}
palette = SDL_CreatePalette(biClrUsed);
if (!palette) {
goto done;
}
palette->ncolors = biClrUsed;
if (biSize == 12) {
for (i = 0; i < (int)biClrUsed; ++i) {
for (i = 0; i < palette->ncolors; ++i) {
if (!SDL_ReadU8(src, &palette->colors[i].b) ||
!SDL_ReadU8(src, &palette->colors[i].g) ||
!SDL_ReadU8(src, &palette->colors[i].r)) {
@@ -479,7 +477,7 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
palette->colors[i].a = SDL_ALPHA_OPAQUE;
}
} else {
for (i = 0; i < (int)biClrUsed; ++i) {
for (i = 0; i < palette->ncolors; ++i) {
if (!SDL_ReadU8(src, &palette->colors[i].b) ||
!SDL_ReadU8(src, &palette->colors[i].g) ||
!SDL_ReadU8(src, &palette->colors[i].r) ||
@@ -494,9 +492,6 @@ SDL_Surface *SDL_LoadBMP_IO(SDL_IOStream *src, SDL_bool closeio)
palette->colors[i].a = SDL_ALPHA_OPAQUE;
}
}
SDL_SetSurfacePalette(surface, palette);
SDL_DestroyPalette(palette);
}
/* Read the surface pixels. Note that the bmp image is upside down */

View File

@@ -367,6 +367,46 @@ float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace)
return 1.0f;
}
SDL_Palette *SDL_CreateSurfacePalette(SDL_Surface *surface)
{
SDL_Palette *palette;
if (!SDL_SurfaceValid(surface)) {
SDL_InvalidParamError("surface");
return NULL;
}
if (!SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
SDL_SetError("The surface is not indexed format");
return NULL;
}
palette = SDL_CreatePalette((1 << SDL_BITSPERPIXEL(surface->format)));
if (!palette) {
return NULL;
}
if (palette->ncolors == 2) {
/* Create a black and white bitmap palette */
palette->colors[0].r = 0xFF;
palette->colors[0].g = 0xFF;
palette->colors[0].b = 0xFF;
palette->colors[1].r = 0x00;
palette->colors[1].g = 0x00;
palette->colors[1].b = 0x00;
}
if (SDL_SetSurfacePalette(surface, palette) < 0) {
SDL_DestroyPalette(palette);
return NULL;
}
/* The surface has retained the palette, we can remove the reference here */
SDL_assert(palette->refcount == 2);
SDL_DestroyPalette(palette);
return palette;
}
int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
{
if (!SDL_SurfaceValid(surface)) {