SDL_image: Updated to the latest version of SDL (9c8ba)

From 9c8ba95b49faf209bcc12a50fd41561072ab7de6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 10 Jul 2024 14:37:48 -0700
Subject: [PATCH] Updated to the latest version of SDL

---
 examples/showimage.c |  2 +-
 external/SDL         |  2 +-
 src/IMG_ImageIO.m    |  2 +-
 src/IMG_avif.c       | 22 ++++++++++------------
 src/IMG_gif.c        | 14 ++++++++------
 src/IMG_jpg.c        |  8 ++++----
 src/IMG_jxl.c        |  4 ++--
 src/IMG_lbm.c        | 21 +++++++++++----------
 src/IMG_pcx.c        |  5 +++--
 src/IMG_png.c        | 24 ++++++++++++------------
 src/IMG_pnm.c        | 12 +++++++-----
 src/IMG_qoi.c        | 10 +++++-----
 src/IMG_stb.c        | 20 ++++++++++----------
 src/IMG_tga.c        | 10 ++++++----
 src/IMG_webp.c       |  2 +-
 src/IMG_xpm.c        |  7 +++++--
 16 files changed, 87 insertions(+), 78 deletions(-)

diff --git a/examples/showimage.c b/examples/showimage.c
index 38aaf357..52e294c2 100644
--- a/examples/showimage.c
+++ b/examples/showimage.c
@@ -146,7 +146,7 @@ int main(int argc, char *argv[])
 
             /* Use the tonemap operator to convert to SDR output */
             SDL_SetStringProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING, tonemap);
-            temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32);
+            temp = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
             SDL_DestroySurface(surface);
             if (!temp) {
                 SDL_Log("Couldn't convert surface: %s\n", SDL_GetError());
diff --git a/external/SDL b/external/SDL
index f7877902..d5e6116f 160000
--- a/external/SDL
+++ b/external/SDL
@@ -1 +1 @@
-Subproject commit f7877902436eae72e3931b2d9ab9d213e095baab
+Subproject commit d5e6116f8e0775d94616e67e955705f281407eda
diff --git a/src/IMG_ImageIO.m b/src/IMG_ImageIO.m
index cadd5e98..95af3b09 100644
--- a/src/IMG_ImageIO.m
+++ b/src/IMG_ImageIO.m
@@ -319,7 +319,7 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
         const uint8_t* bytes = [data bytes];
         size_t i;
 
-        palette = surface->format->palette;
+        palette = SDL_GetSurfacePalette(surface);
         for (i = 0, entry = entries; i < num_entries; ++i) {
             palette->colors[i].r = entry[0];
             palette->colors[i].g = entry[1];
diff --git a/src/IMG_avif.c b/src/IMG_avif.c
index bb444672..9f12a00b 100644
--- a/src/IMG_avif.c
+++ b/src/IMG_avif.c
@@ -527,7 +527,6 @@ static int IMG_SaveAVIF_IO_libavif(SDL_Surface *surface, SDL_IOStream *dst, int
     avifEncoder *encoder = NULL;
     avifRWData avifOutput = AVIF_DATA_EMPTY;
     avifResult rc;
-    const Uint32 surface_format = surface->format->format;
     SDL_Colorspace colorspace;
     Uint16 maxCLL, maxFALL;
     SDL_PropertiesID props;
@@ -557,7 +556,7 @@ static int IMG_SaveAVIF_IO_libavif(SDL_Surface *surface, SDL_IOStream *dst, int
     SDL_zero(rgb);
     lib.avifRGBImageSetDefaults(&rgb, image);
 
-    if (SDL_ISPIXELFORMAT_10BIT(surface_format)) {
+    if (SDL_ISPIXELFORMAT_10BIT(surface->format)) {
         const Uint16 expand_alpha[] = {
             0, 0x155, 0x2aa, 0x3ff
         };
@@ -571,13 +570,13 @@ static int IMG_SaveAVIF_IO_libavif(SDL_Surface *surface, SDL_IOStream *dst, int
         image->depth = 10;
         image->matrixCoefficients = AVIF_MATRIX_COEFFICIENTS_IDENTITY;
 
-        if (SDL_PIXELORDER(surface_format) == SDL_PACKEDORDER_XRGB ||
-            SDL_PIXELORDER(surface_format) == SDL_PACKEDORDER_ARGB) {
+        if (SDL_PIXELORDER(surface->format) == SDL_PACKEDORDER_XRGB ||
+            SDL_PIXELORDER(surface->format) == SDL_PACKEDORDER_ARGB) {
             rgb.format = AVIF_RGB_FORMAT_RGBA;
         } else {
             rgb.format = AVIF_RGB_FORMAT_BGRA;
         }
-        rgb.ignoreAlpha = SDL_ISPIXELFORMAT_ALPHA(surface_format) ? SDL_FALSE : SDL_TRUE;
+        rgb.ignoreAlpha = SDL_ISPIXELFORMAT_ALPHA(surface->format) ? SDL_FALSE : SDL_TRUE;
         rgb.depth = 10;
         rgb.rowBytes = (uint32_t)image->width * 4 * sizeof(Uint16);
         rgb.pixels = (uint8_t *)SDL_malloc(image->height * rgb.rowBytes);
@@ -612,7 +611,7 @@ static int IMG_SaveAVIF_IO_libavif(SDL_Surface *surface, SDL_IOStream *dst, int
         SDL_Surface *temp = NULL;
 
         rgb.depth = 8;
-        switch (surface_format) {
+        switch (surface->format) {
         case SDL_PIXELFORMAT_RGBX32:
         case SDL_PIXELFORMAT_RGBA32:
             rgb.format = AVIF_RGB_FORMAT_RGBA;
@@ -636,17 +635,17 @@ static int IMG_SaveAVIF_IO_libavif(SDL_Surface *surface, SDL_IOStream *dst, int
         default:
             /* Need to convert to a format libavif understands */
             rgb.format = AVIF_RGB_FORMAT_RGBA;
-            if (SDL_ISPIXELFORMAT_ALPHA(surface_format)) {
-                temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32);
+            if (SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
+                temp = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
             } else {
-                temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBX32);
+                temp = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBX32);
             }
             if (!temp) {
                 goto done;
             }
             break;
         }
-        rgb.ignoreAlpha = SDL_ISPIXELFORMAT_ALPHA(surface_format) ? SDL_FALSE : SDL_TRUE;
+        rgb.ignoreAlpha = SDL_ISPIXELFORMAT_ALPHA(surface->format) ? SDL_FALSE : SDL_TRUE;
         rgb.pixels = (uint8_t *)temp->pixels;
         rgb.rowBytes = (uint32_t)temp->pitch;
 
@@ -755,11 +754,10 @@ int IMG_SaveAVIF_IO(SDL_Surface *surface, SDL_IOStream *dst, int closeio, int qu
         return IMG_SetError("Passed NULL dst");
     }
 
-#if SDL_IMAGE_SAVE_AVIF
+#ifdef SDL_IMAGE_SAVE_AVIF
     if (result < 0) {
         result = IMG_SaveAVIF_IO_libavif(surface, dst, quality);
     }
-
 #else
     (void) surface;
     (void) quality;
diff --git a/src/IMG_gif.c b/src/IMG_gif.c
index 85828342..17f4462f 100644
--- a/src/IMG_gif.c
+++ b/src/IMG_gif.c
@@ -51,9 +51,9 @@
 #define RWSetMsg        IMG_SetError
 #define ImageNewCmap(w, h, s)   SDL_CreateSurface(w, h, SDL_PIXELFORMAT_INDEX8)
 #define ImageSetCmap(s, i, R, G, B) do { \
-                s->format->palette->colors[i].r = R; \
-                s->format->palette->colors[i].g = G; \
-                s->format->palette->colors[i].b = B; \
+                palette->colors[i].r = R; \
+                palette->colors[i].g = G; \
+                palette->colors[i].b = B; \
             } while (0)
 /* * * * * */
 
@@ -149,15 +149,15 @@ static SDL_bool NormalizeFrames(Frame_t *frames, int count)
 
 
     if (SDL_SurfaceHasColorKey(frames[0].image)) {
-        image = SDL_ConvertSurfaceFormat(frames[0].image, SDL_PIXELFORMAT_ARGB8888);
+        image = SDL_ConvertSurface(frames[0].image, SDL_PIXELFORMAT_ARGB8888);
     } else {
-        image = SDL_ConvertSurfaceFormat(frames[0].image, SDL_PIXELFORMAT_XRGB8888);
+        image = SDL_ConvertSurface(frames[0].image, SDL_PIXELFORMAT_XRGB8888);
     }
     if (!image) {
         return SDL_FALSE;
     }
 
-    fill = SDL_MapRGBA(image->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
+    fill = SDL_MapSurfaceRGBA(image, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
 
     rect.x = 0;
     rect.y = 0;
@@ -614,6 +614,7 @@ ReadImage(SDL_IOStream * src, int len, int height, int cmapSize,
           int gray, int interlace, int ignore, State_t * state)
 {
     Image *image;
+    SDL_Palette *palette;
     unsigned char c;
     int i, v;
     int xpos = 0, ypos = 0, pass = 0;
@@ -647,6 +648,7 @@ ReadImage(SDL_IOStream * src, int len, int height, int cmapSize,
         SDL_DestroySurface(image);
         return NULL;
     }
+    palette = SDL_GetSurfacePalette(image);
 
     for (i = 0; i < cmapSize; i++)
         ImageSetCmap(image, i, cmap[CM_RED][i],
diff --git a/src/IMG_jpg.c b/src/IMG_jpg.c
index c7264cb6..e781c48c 100644
--- a/src/IMG_jpg.c
+++ b/src/IMG_jpg.c
@@ -539,8 +539,8 @@ static int IMG_SaveJPG_IO_jpeglib(SDL_Surface *surface, SDL_IOStream *dst, int q
     }
 
     /* Convert surface to format we can save */
-    if (surface->format->format != jpg_format) {
-        jpeg_surface = SDL_ConvertSurfaceFormat(surface, jpg_format);
+    if (surface->format != jpg_format) {
+        jpeg_surface = SDL_ConvertSurface(surface, jpg_format);
         if (!jpeg_surface) {
             return -1;
         }
@@ -715,8 +715,8 @@ static int IMG_SaveJPG_IO_tinyjpeg(SDL_Surface *surface, SDL_IOStream *dst, int
     int result = -1;
 
     /* Convert surface to format we can save */
-    if (surface->format->format != jpg_format) {
-        jpeg_surface = SDL_ConvertSurfaceFormat(surface, jpg_format);
+    if (surface->format != jpg_format) {
+        jpeg_surface = SDL_ConvertSurface(surface, jpg_format);
         if (!jpeg_surface) {
             return -1;
         }
diff --git a/src/IMG_jxl.c b/src/IMG_jxl.c
index 33d1920a..3d7d1580 100644
--- a/src/IMG_jxl.c
+++ b/src/IMG_jxl.c
@@ -219,11 +219,11 @@ SDL_Surface *IMG_LoadJXL_IO(SDL_IOStream *src)
             break;
         case JXL_DEC_SUCCESS:
             /* All done! */
-            surface = SDL_CreateSurfaceFrom(pixels, info.xsize, info.ysize, pitch, SDL_PIXELFORMAT_RGBA32);
+            surface = SDL_CreateSurfaceFrom(info.xsize, info.ysize, SDL_PIXELFORMAT_RGBA32, pixels, pitch);
             if (surface) {
                 /* Let SDL manage the memory now */
                 pixels = NULL;
-                surface->flags &= ~SDL_PREALLOC;
+                surface->flags &= ~SDL_SURFACE_PREALLOCATED;
             }
             goto done;
         default:
diff --git a/src/IMG_lbm.c b/src/IMG_lbm.c
index 9bfadec6..d4319041 100644
--- a/src/IMG_lbm.c
+++ b/src/IMG_lbm.c
@@ -273,15 +273,16 @@ SDL_Surface *IMG_LoadLBM_IO(SDL_IOStream *src )
     /* There is no palette in 24 bits ILBM file */
     if ( nbcolors>0 && flagHAM==0 )
     {
+        SDL_Palette *palette = SDL_GetSurfacePalette(Image);
         /* FIXME: Should this include the stencil? See comment below */
         int nbrcolorsfinal = 1 << (nbplanes + stencil);
         ptr = &colormap[0];
 
         for ( i=0; i<nbcolors; i++ )
         {
-            Image->format->palette->colors[i].r = *ptr++;
-            Image->format->palette->colors[i].g = *ptr++;
-            Image->format->palette->colors[i].b = *ptr++;
+            palette->colors[i].r = *ptr++;
+            palette->colors[i].g = *ptr++;
+            palette->colors[i].b = *ptr++;
         }
 
         /* Amiga EHB mode (Extra-Half-Bright) */
@@ -295,9 +296,9 @@ SDL_Surface *IMG_LoadLBM_IO(SDL_IOStream *src )
             ptr = &colormap[0];
             for ( i=32; i<64; i++ )
             {
-                Image->format->palette->colors[i].r = (*ptr++)/2;
-                Image->format->palette->colors[i].g = (*ptr++)/2;
-                Image->format->palette->colors[i].b = (*ptr++)/2;
+                palette->colors[i].r = (*ptr++)/2;
+                palette->colors[i].g = (*ptr++)/2;
+                palette->colors[i].b = (*ptr++)/2;
             }
         }
 
@@ -308,12 +309,12 @@ SDL_Surface *IMG_LoadLBM_IO(SDL_IOStream *src )
         }
         for ( i=nbcolors; i < (Uint32)nbrcolorsfinal; i++ )
         {
-            Image->format->palette->colors[i].r = Image->format->palette->colors[i%nbcolors].r;
-            Image->format->palette->colors[i].g = Image->format->palette->colors[i%nbcolors].g;
-            Image->format->palette->colors[i].b = Image->format->palette->colors[i%nbcolors].b;
+            palette->colors[i].r = palette->colors[i%nbcolors].r;
+            palette->colors[i].g = palette->colors[i%nbcolors].g;
+            palette->colors[i].b = palette->colors[i%nbcolors].b;
         }
         if ( !pbm )
-            Image->format->palette->ncolors = nbrcolorsfinal;
+            palette->ncolors = nbrcolorsfinal;
     }
 
     /* Get the bitmap */
diff --git a/src/IMG_pcx.c b/src/IMG_pcx.c
index dd17ad67..917cbf6d 100644
--- a/src/IMG_pcx.c
+++ b/src/IMG_pcx.c
@@ -233,11 +233,12 @@ SDL_Surface *IMG_LoadPCX_IO(SDL_IOStream *src)
     }
 
     if ( bits == 8 ) {
-        SDL_Color *colors = surface->format->palette->colors;
+        SDL_Palette *palette = SDL_GetSurfacePalette(surface);
+        SDL_Color *colors = palette->colors;
         int nc = 1 << src_bits;
         int i;
 
-        surface->format->palette->ncolors = nc;
+        palette->ncolors = nc;
         if ( src_bits == 8 ) {
             Uint8 pch;
             Uint8 colormap[768];
diff --git a/src/IMG_png.c b/src/IMG_png.c
index 7db2d5b2..4331125c 100644
--- a/src/IMG_png.c
+++ b/src/IMG_png.c
@@ -394,7 +394,7 @@ static void LIBPNG_LoadPNG_IO(SDL_IOStream *src, struct loadpng_vars *vars)
     if (ckey != -1) {
         if (color_type != PNG_COLOR_TYPE_PALETTE) {
             /* FIXME: Should these be truncated or shifted down? */
-            ckey = SDL_MapRGB(vars->surface->format,
+            ckey = SDL_MapSurfaceRGB(vars->surface,
                          (Uint8)transv->red,
                          (Uint8)transv->green,
                          (Uint8)transv->blue);
@@ -425,7 +425,7 @@ static void LIBPNG_LoadPNG_IO(SDL_IOStream *src, struct loadpng_vars *vars)
     */
 
     /* Load the palette, if any */
-    palette = vars->surface->format->palette;
+    palette = SDL_GetSurfacePalette(vars->surface);
     if ( palette ) {
         int png_num_palette;
         png_colorp png_palette;
@@ -621,7 +621,7 @@ static int LIBPNG_SavePNG_IO(struct savepng_vars *vars, SDL_Surface *surface, SD
         return -1;
     }
 
-    palette = surface->format->palette;
+    palette = SDL_GetSurfacePalette(surface);
     if (palette) {
         const int ncolors = palette->ncolors;
         int i;
@@ -650,21 +650,21 @@ static int LIBPNG_SavePNG_IO(struct savepng_vars *vars, SDL_Surface *surface, SD
             lib.png_set_tRNS(vars->png_ptr, vars->info_ptr, transparent_table, last_transparent + 1, NULL);
         }
     }
-    else if (surface->format->format == SDL_PIXELFORMAT_RGB24) {
+    else if (surface->format == SDL_PIXELFORMAT_RGB24) {
         /* If the surface is exactly the right RGB format it is just passed through */
         png_color_type = PNG_COLOR_TYPE_RGB;
     }
-    else if (!SDL_ISPIXELFORMAT_ALPHA(surface->format->format)) {
+    else if (!SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
         /* If the surface is not exactly the right RGB format but does not have alpha
            information, it should be converted to RGB24 before being passed through */
         png_color_type = PNG_COLOR_TYPE_RGB;
-        vars->source = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGB24);
+        vars->source = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGB24);
     }
-    else if (surface->format->format != png_format) {
+    else if (surface->format != png_format) {
         /* Otherwise, (surface has alpha data), and it is not in the exact right
            format , so it should be converted to that */
         png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-        vars->source = SDL_ConvertSurfaceFormat(surface, png_format);
+        vars->source = SDL_ConvertSurface(surface, png_format);
     } else {
         png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
     }
@@ -759,12 +759,12 @@ static int IMG_SavePNG_IO_miniz(SDL_Surface *surface, SDL_IOStream *dst)
         return IMG_SetError("Passed NULL dst");
     }
 
-    if (surface->format->format == png_format) {
-        png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, surface->format->bytes_per_pixel, surface->pitch, &size);
+    if (surface->format == png_format) {
+        png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, SDL_BYTESPERPIXEL(surface->format), surface->pitch, &size);
     } else {
-        SDL_Surface *cvt = SDL_ConvertSurfaceFormat(surface, png_format);
+        SDL_Surface *cvt = SDL_ConvertSurface(surface, png_format);
         if (cvt) {
-            png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, cvt->format->bytes_per_pixel, cvt->pitch, &size);
+            png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, SDL_BYTESPERPIXEL(cvt->format), cvt->pitch, &size);
             SDL_DestroySurface(cvt);
         }
     }
diff --git a/src/IMG_pnm.c b/src/IMG_pnm.c
index f4531508..e0d9b739 100644
--- a/src/IMG_pnm.c
+++ b/src/IMG_pnm.c
@@ -162,19 +162,21 @@ SDL_Surface *IMG_LoadPNM_IO(SDL_IOStream *src)
     }
     if ( surface == NULL )
         ERROR("Out of memory");
-    bpl = width * surface->format->bytes_per_pixel;
+    bpl = width * SDL_BYTESPERPIXEL(surface->format);
     if(kind == PGM) {
-        SDL_Color *c = surface->format->palette->colors;
+        SDL_Palette *palette = SDL_GetSurfacePalette(surface);
+        SDL_Color *c = palette->colors;
         int i;
         for(i = 0; i < 256; i++)
             c[i].r = c[i].g = c[i].b = i;
-        surface->format->palette->ncolors = 256;
+        palette->ncolors = 256;
     } else if(kind == PBM) {
         /* for some reason PBM has 1=black, 0=white */
-        SDL_Color *c = surface->format->palette->colors;
+        SDL_Palette *palette = SDL_GetSurfacePalette(surface);
+        SDL_Color *c = palette->colors;
         c[0].r = c[0].g = c[0].b = 255;
         c[1].r = c[1].g = c[1].b = 0;
-        surface->format->palette->ncolors = 2;
+        palette->ncolors = 2;
         bpl = (width + 7) >> 3;
         buf = (Uint8 *)SDL_malloc(bpl);
         if(buf == NULL)
diff --git a/src/IMG_qoi.c b/src/IMG_qoi.c
index 340d9920..ef1c07d1 100644
--- a/src/IMG_qoi.c
+++ b/src/IMG_qoi.c
@@ -89,11 +89,11 @@ SDL_Surface *IMG_LoadQOI_IO(SDL_IOStream *src)
         return NULL;
     }
 
-    surface = SDL_CreateSurfaceFrom(pixel_data,
-                                    image_info.width,
+    surface = SDL_CreateSurfaceFrom(image_info.width,
                                     image_info.height,
-                                    (image_info.width * 4),
-                                    SDL_PIXELFORMAT_RGBA32);
+                                    SDL_PIXELFORMAT_RGBA32,
+                                    pixel_data,
+                                    (image_info.width * 4));
     if ( !surface ) {
         QOI_FREE(pixel_data);
         IMG_SetError("Couldn't create SDL_Surface");
@@ -101,7 +101,7 @@ SDL_Surface *IMG_LoadQOI_IO(SDL_IOStream *src)
     }
 
     /* Let SDL manage the memory now */
-    surface->flags &= ~SDL_PREALLOC;
+    surface->flags &= ~SDL_SURFACE_PREALLOCATED;
 
     return surface;
 }
diff --git a/src/IMG_stb.c b/src/IMG_stb.c
index ecfeda97..dde629e6 100644
--- a/src/IMG_stb.c
+++ b/src/IMG_stb.c
@@ -141,17 +141,17 @@ SDL_Surface *IMG_LoadSTB_IO(SDL_IOStream *src)
 
     if (use_palette) {
         surface = SDL_CreateSurfaceFrom(
-            pixels,
             w,
             h,
-            w,
-            SDL_PIXELFORMAT_INDEX8
+            SDL_PIXELFORMAT_INDEX8,
+            pixels,
+            w
         );
         if (surface) {
             SDL_bool has_colorkey = SDL_FALSE;
             int colorkey_index = -1;
             SDL_bool has_alpha = SDL_FALSE;
-            SDL_Palette *palette = surface->format->palette;
+            SDL_Palette *palette = SDL_GetSurfacePalette(surface);
             if (palette) {
                 int i;
                 Uint8 *palette_bytes = (Uint8 *)palette_colors;
@@ -183,22 +183,22 @@ SDL_Surface *IMG_LoadSTB_IO(SDL_IOStream *src)
              * https://github.com/nothings/stb/issues/58
              * -flibit
              */
-            surface->flags &= ~SDL_PREALLOC;
+            surface->flags &= ~SDL_SURFACE_PREALLOCATED;
         }
 
     } else if (format == STBI_grey || format == STBI_rgb || format == STBI_rgb_alpha) {
         surface = SDL_CreateSurfaceFrom(
-            pixels,
             w,
             h,
-            w * format,
             (format == STBI_rgb_alpha) ? SDL_PIXELFORMAT_RGBA32 :
             (format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 :
-            SDL_PIXELFORMAT_INDEX8
+            SDL_PIXELFORMAT_INDEX8,
+            pixels,
+            w * format
         );
         if (surface) {
             /* Set a grayscale palette for gray images */
-            SDL_Palette *palette = surface->format->palette;
+            SDL_Palette *palette = SDL_GetSurfacePalette(surface);
             if (palette) {
                 int i;
 
@@ -214,7 +214,7 @@ SDL_Surface *IMG_LoadSTB_IO(SDL_IOStream *src)
              * https://github.com/nothings/stb/issues/58
              * -flibit
              */
-            surface->flags &= ~SDL_PREALLOC;
+            surface->flags &= ~SDL_SURFACE_PREALLOCATED;
         }
 
     } else if (format == STBI_grey_alpha) {
diff --git a/src/IMG_tga.c b/src/IMG_tga.c
index 9950e2e5..b3683390 100644
--- a/src/IMG_tga.c
+++ b/src/IMG_tga.c
@@ -189,8 +189,9 @@ SDL_Surface *IMG_LoadTGA_IO(SDL_IOStream *src)
         size_t palsiz = ncols * ((hdr.cmap_bits + 7) >> 3);
         if (indexed && !grey) {
             Uint8 *pal = (Uint8 *)SDL_malloc(palsiz), *p = pal;
-            SDL_Color *colors = img->format->palette->colors;
-            img->format->palette->ncolors = ncols;
+            SDL_Palette *palette = SDL_GetSurfacePalette(img);
+            SDL_Color *colors = palette->colors;
+            palette->ncolors = ncols;
             if (SDL_ReadIO(src, pal, palsiz) != palsiz) {
                 error = "Error reading TGA data";
                 goto error;
@@ -227,10 +228,11 @@ SDL_Surface *IMG_LoadTGA_IO(SDL_IOStream *src)
     }
 
     if (grey) {
-        SDL_Color *colors = img->format->palette->colors;
+        SDL_Palette *palette = SDL_GetSurfacePalette(img);
+        SDL_Color *colors = palette->colors;
         for(i = 0; i < 256; i++)
             colors[i].r = colors[i].g = colors[i].b = i;
-        img->format->palette->ncolors = 256;
+        palette->ncolors = 256;
     }
 
     if (hdr.flags & TGA_ORIGIN_UPPER) {
diff --git a/src/IMG_webp.c b/src/IMG_webp.c
index 3e03134b..6152916e 100644
--- a/src/IMG_webp.c
+++ b/src/IMG_webp.c
@@ -94,7 +94,7 @@ int IMG_InitWEBP(void)
         FUNCTION_LOADER_LIBWEBP(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
         FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxInternal, WebPDemuxer* (*)(const WebPData*, int, WebPDemuxState*, int))
         FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxGetFrame, int (*)(const WebPDemuxer* dmux, int frame_number, WebPIterator* iter))
-        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxGetI, uint32_t (*)(const WebPDemuxer* dmux, WebPFormatFeature feature));
+        FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxGetI, uint32_t (*)(const WebPDemuxer* dmux, WebPFormatFeature feature))
         FUNCTION_LOADER_LIBWEBPDEMUX(WebPDemuxDelete, void (*)(WebPDemuxer* dmux))
     }
     ++lib.loaded;
diff --git a/src/IMG_xpm.c b/src/IMG_xpm.c
index 6c7715bb..7fc0285e 100644
--- a/src/IMG_xpm.c
+++ b/src/IMG_xpm.c
@@ -1055,8 +1055,11 @@ static SDL_Surface *load_xpm(char **xpm, SDL_IOStream *src, SDL_bool force_32bit
     if (ncolors <= 256 && !force_32bit) {
         indexed = 1;
         image = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_INDEX8);
-        im_colors = image->format->palette->colors;
-        image->format->palette->ncolors = ncolors;
+        if (image) {
+            SDL_Palette *palette = SDL_GetSurfacePalette(image);
+            im_colors = palette->colors;
+            palette->ncolors = ncolors;
+        }
     } else {
         indexed = 0;
         image = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB8888);