SDL_image: Replace SDL_CreateRGBSurface by SDL_CreateRGBSurfaceWithFormat (#318) (bb9d2)

From bb9d216a3b25a0819a450f20349fceeea7bf4c9d Mon Sep 17 00:00:00 2001
From: Sylvain Becker <[EMAIL REDACTED]>
Date: Fri, 2 Dec 2022 22:28:04 +0100
Subject: [PATCH] Replace SDL_CreateRGBSurface by
 SDL_CreateRGBSurfaceWithFormat (#318)

---
 IMG.c         |  6 +++---
 IMG_ImageIO.m | 17 +++++----------
 IMG_WIC.c     | 11 +---------
 IMG_bmp.c     |  3 +--
 IMG_gif.c     |  2 +-
 IMG_jpg.c     | 17 ++-------------
 IMG_lbm.c     | 17 +++++++++++++--
 IMG_pcx.c     | 20 ++++-------------
 IMG_png.c     | 59 +++++++++++++++++++++++++++++----------------------
 IMG_pnm.c     | 11 ++--------
 IMG_tga.c     | 32 ++++++----------------------
 IMG_tif.c     |  8 +------
 IMG_webp.c    | 59 ++++++++++++---------------------------------------
 IMG_xcf.c     |  9 +++-----
 IMG_xpm.c     |  6 ++----
 IMG_xv.c      |  2 +-
 16 files changed, 96 insertions(+), 183 deletions(-)

diff --git a/IMG.c b/IMG.c
index 4f7bb3ca..aba05f28 100644
--- a/IMG.c
+++ b/IMG.c
@@ -185,7 +185,7 @@ SDL_Surface *IMG_Load(const char *file)
 
     data = emscripten_get_preloaded_image_data(file, &w, &h);
     if (data != NULL) {
-        surf = SDL_CreateRGBSurface(0, w, h, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
+        surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_ABGR8888);
         if (surf != NULL) {
             memcpy(surf->pixels, data, w * h * 4);
         }
@@ -258,7 +258,7 @@ SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
 
         if (data)
         {
-            surf = SDL_CreateRGBSurface(0, w, h, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
+            surf = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_ABGR8888);
             if (surf != NULL) {
                 memcpy(surf->pixels, data, w * h * 4);
             }
@@ -267,7 +267,7 @@ SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
             if (freesrc)
                 SDL_RWclose(src);
 
-            /* If SDL_CreateRGBSurface returns NULL, it has set the error message for us */
+            /* If SDL_CreateRGBSurfaceWithFormat returns NULL, it has set the error message for us */
             return surf;
         }
     }
diff --git a/IMG_ImageIO.m b/IMG_ImageIO.m
index 314564a2..85290c76 100644
--- a/IMG_ImageIO.m
+++ b/IMG_ImageIO.m
@@ -199,10 +199,7 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
     size_t bits_per_component = 8;
 
     SDL_Surface* surface;
-    Uint32 Amask;
-    Uint32 Rmask;
-    Uint32 Gmask;
-    Uint32 Bmask;
+    Uint32 format;
 
     CGContextRef bitmap_context;
     CGBitmapInfo bitmap_info;
@@ -212,19 +209,15 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
         alpha == kCGImageAlphaNoneSkipFirst ||
         alpha == kCGImageAlphaNoneSkipLast) {
         bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
-        Amask = 0x00000000;
+        format = SDL_PIXELFORMAT_RGB888;
     } else {
         /* kCGImageAlphaFirst isn't supported */
         //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
         bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
-        Amask = 0xFF000000;
+        format = SDL_PIXELFORMAT_ARGB8888;
     }
 
-    Rmask = 0x00FF0000;
-    Gmask = 0x0000FF00;
-    Bmask = 0x000000FF;
-
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)w, (int)h, 32, Rmask, Gmask, Bmask, Amask);
+    surface = SDL_CreateRGBSurfaceWithFormat(0, (int)w, (int)h, 0, format);
     if (surface)
     {
         // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
@@ -308,7 +301,7 @@ static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
     }
 
     CGColorSpaceGetColorTable(color_space, entries);
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)w, (int)h, (int)bits_per_pixel, 0, 0, 0, 0);
+    surface = SDL_CreateRGBSurfaceWithFormat(0, (int)w, (int)h, 0, SDL_PIXELFORMAT_INDEX8);
     if (surface) {
         uint8_t* pixels = (uint8_t*)surface->pixels;
         CGDataProviderRef provider = CGImageGetDataProvider(image_ref);
diff --git a/IMG_WIC.c b/IMG_WIC.c
index 4b65d977..867b62e0 100644
--- a/IMG_WIC.c
+++ b/IMG_WIC.c
@@ -252,16 +252,7 @@ static SDL_Surface* WIC_LoadImage(SDL_RWops *src)
     DONE_IF_FAILED(IWICBitmapFrameDecode_GetSize(bitmapFrame, &width, &height));
 #undef DONE_IF_FAILED
 
-    surface = SDL_CreateRGBSurface(
-        0,
-        width,
-        height,
-        32,
-        0x000000FF,
-        0x0000FF00,
-        0x00FF0000,
-        0xFF000000
-    );
+    surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_ABGR8888);
     IWICFormatConverter_CopyPixels(
         formatConverter,
         NULL,
diff --git a/IMG_bmp.c b/IMG_bmp.c
index 08833790..ad60ff43 100644
--- a/IMG_bmp.c
+++ b/IMG_bmp.c
@@ -289,8 +289,7 @@ LoadICOCUR_RW(SDL_RWops * src, int type, int freesrc)
     biHeight = biHeight >> 1;
     //printf("%d x %d\n", biWidth, biHeight);
     surface =
-        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
-                             0x0000FF00, 0x000000FF, 0xFF000000);
+        SDL_CreateRGBSurfaceWithFormat(0, biWidth, biHeight, 0, SDL_PIXELFORMAT_ARGB8888);
     if (surface == NULL) {
         was_error = SDL_TRUE;
         goto done;
diff --git a/IMG_gif.c b/IMG_gif.c
index a92f3767..b513605c 100644
--- a/IMG_gif.c
+++ b/IMG_gif.c
@@ -48,7 +48,7 @@
 
 #define Image           SDL_Surface
 #define RWSetMsg        IMG_SetError
-#define ImageNewCmap(w, h, s)   SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,8,0,0,0,0)
+#define ImageNewCmap(w, h, s)   SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, 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; \
diff --git a/IMG_jpg.c b/IMG_jpg.c
index 341b85da..c1f2c4a0 100644
--- a/IMG_jpg.c
+++ b/IMG_jpg.c
@@ -385,13 +385,7 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
         lib.jpeg_calc_output_dimensions(&cinfo);
 
         /* Allocate an output surface to hold the image */
-        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                cinfo.output_width, cinfo.output_height, 32,
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-                           0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
-#else
-                           0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF);
-#endif
+        surface = SDL_CreateRGBSurfaceWithFormat(0, cinfo.output_width, cinfo.output_height, 0, SDL_PIXELFORMAT_BGRA32);
     } else {
         /* Set 24-bit RGB output */
         cinfo.out_color_space = JCS_RGB;
@@ -405,14 +399,7 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
         lib.jpeg_calc_output_dimensions(&cinfo);
 
         /* Allocate an output surface to hold the image */
-        surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
-                cinfo.output_width, cinfo.output_height, 24,
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-                           0x0000FF, 0x00FF00, 0xFF0000,
-#else
-                           0xFF0000, 0x00FF00, 0x0000FF,
-#endif
-                           0);
+        surface = SDL_CreateRGBSurfaceWithFormat(0, cinfo.output_width, cinfo.output_height, 0, SDL_PIXELFORMAT_RGB24);
     }
 
     if ( surface == NULL ) {
diff --git a/IMG_lbm.c b/IMG_lbm.c
index 9bf3a743..eccc1289 100644
--- a/IMG_lbm.c
+++ b/IMG_lbm.c
@@ -250,8 +250,21 @@ SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
         goto done;
     }
 
-    if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (nbplanes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
-       goto done;
+    {
+       Uint32 format = SDL_PIXELFORMAT_INDEX8;
+       if (nbplanes == 24 || flagHAM == 1) {
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+          format = SDL_PIXELFORMAT_RGB24;
+#else
+          format = SDL_PIXELFORMAT_BGR24;
+#endif
+       }
+       if (nbplanes == 24 || flagHAM == 1) {
+          if ((Image = SDL_CreateRGBSurfaceWithFormat(0, width, bmhd.h, 0, format)) == NULL ){
+             goto done;
+          }
+       }
+    }
 
     if ( bmhd.mask & 2 )               /* There is a transparent color */
         SDL_SetColorKey( Image, SDL_TRUE, bmhd.tcolor );
diff --git a/IMG_pcx.c b/IMG_pcx.c
index 59af5924..334d9b7e 100644
--- a/IMG_pcx.c
+++ b/IMG_pcx.c
@@ -88,10 +88,6 @@ SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
 {
     Sint64 start;
     struct PCXheader pcxh;
-    Uint32 Rmask;
-    Uint32 Gmask;
-    Uint32 Bmask;
-    Uint32 Amask;
     SDL_Surface *surface = NULL;
     int width, height;
     int y, bpl;
@@ -100,6 +96,7 @@ SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
     int bits, src_bits;
     int count = 0;
     Uint8 ch;
+    Uint32 format;
 
     if ( !src ) {
         /* The error message has been set in SDL_RWFromFile */
@@ -134,28 +131,19 @@ SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
     /* Create the surface of the appropriate type */
     width = (pcxh.Xmax - pcxh.Xmin) + 1;
     height = (pcxh.Ymax - pcxh.Ymin) + 1;
-    Rmask = Gmask = Bmask = Amask = 0;
     src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
     if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
        || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
         bits = 8;
+        format = SDL_PIXELFORMAT_INDEX8;
     } else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
         bits = 24;
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-        Rmask = 0x000000FF;
-        Gmask = 0x0000FF00;
-        Bmask = 0x00FF0000;
-#else
-        Rmask = 0xFF0000;
-        Gmask = 0x00FF00;
-        Bmask = 0x0000FF;
-#endif
+        format = SDL_PIXELFORMAT_RGB24;
     } else {
         error = "unsupported PCX format";
         goto done;
     }
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
-                   bits, Rmask, Gmask, Bmask, Amask);
+    surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format);
     if ( surface == NULL ) {
         goto done;
     }
diff --git a/IMG_png.c b/IMG_png.c
index a55d4f9a..ba168e9f 100644
--- a/IMG_png.c
+++ b/IMG_png.c
@@ -241,10 +241,7 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
     png_infop info_ptr;
     png_uint_32 width, height;
     int bit_depth, color_type, interlace_type, num_channels;
-    Uint32 Rmask;
-    Uint32 Gmask;
-    Uint32 Bmask;
-    Uint32 Amask;
+    Uint32 format = SDL_PIXELFORMAT_UNKNOWN;
     SDL_Palette *palette;
     png_bytep *volatile row_pointers;
     int row, i;
@@ -363,24 +360,40 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
             &color_type, &interlace_type, NULL, NULL);
 
     /* Allocate the SDL surface to hold the image */
-    Rmask = Gmask = Bmask = Amask = 0 ;
     num_channels = lib.png_get_channels(png_ptr, info_ptr);
-    if ( num_channels >= 3 ) {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-        Rmask = 0x000000FF;
-        Gmask = 0x0000FF00;
-        Bmask = 0x00FF0000;
-        Amask = (num_channels == 4) ? 0xFF000000 : 0;
-#else
-        int s = (num_channels == 4) ? 0 : 8;
-        Rmask = 0xFF000000 >> s;
-        Gmask = 0x00FF0000 >> s;
-        Bmask = 0x0000FF00 >> s;
-        Amask = 0x000000FF >> s;
-#endif
+   
+    if (num_channels == 3) {
+       format = SDL_PIXELFORMAT_RGB24;
+    } else if (num_channels == 4) {
+       format = SDL_PIXELFORMAT_RGBA32;
+    } else {
+       /* Not sure they are all supported by png */
+       switch (bit_depth * num_channels) {
+          case 1:
+             format = SDL_PIXELFORMAT_INDEX1MSB;
+             break;
+          case 4:
+             format = SDL_PIXELFORMAT_INDEX4MSB;
+             break;
+          case 8:
+             format = SDL_PIXELFORMAT_INDEX8;
+             break;
+          case 12:
+             format = SDL_PIXELFORMAT_RGB444;
+             break;
+          case 15:
+             format = SDL_PIXELFORMAT_RGB555;
+             break;
+          case 16:
+             format = SDL_PIXELFORMAT_RGB565;
+             break;
+
+          default:
+             break;
+       }
     }
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,
-            bit_depth*num_channels, Rmask,Gmask,Bmask,Amask);
+
+    surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, format);
     if ( surface == NULL ) {
         error = SDL_GetError();
         goto done;
@@ -542,11 +555,7 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
 
 #if SDL_IMAGE_SAVE_PNG
 
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-static const Uint32 png_format = SDL_PIXELFORMAT_ABGR8888;
-#else
-static const Uint32 png_format = SDL_PIXELFORMAT_RGBA8888;
-#endif
+static const Uint32 png_format = SDL_PIXELFORMAT_RGBA32;
 
 #ifdef USE_LIBPNG
 
diff --git a/IMG_pnm.c b/IMG_pnm.c
index 2f42a373..4bb2b8f2 100644
--- a/IMG_pnm.c
+++ b/IMG_pnm.c
@@ -151,17 +151,10 @@ SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
 
     if(kind == PPM) {
         /* 24-bit surface in R,G,B byte order */
-        surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-                       0x000000ff, 0x0000ff00, 0x00ff0000,
-#else
-                       0x00ff0000, 0x0000ff00, 0x000000ff,
-#endif
-                       0);
+        surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_RGB24);
     } else {
         /* load PBM/PGM as 8-bit indexed images */
-        surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
-                       0, 0, 0, 0);
+        surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, 0, SDL_PIXELFORMAT_INDEX8);
     }
     if ( surface == NULL )
         ERROR("Out of memory");
diff --git a/IMG_tga.c b/IMG_tga.c
index 71939202..302eface 100644
--- a/IMG_tga.c
+++ b/IMG_tga.c
@@ -87,13 +87,12 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
     const char *error = NULL;
     struct TGAheader hdr;
     int rle = 0;
-    int alpha = 0;
     int indexed = 0;
     int grey = 0;
     int ckey = -1;
     int ncols, w, h;
     SDL_Surface *img = NULL;
-    Uint32 rmask, gmask, bmask, amask;
+    Uint32 format;
     Uint8 *dst;
     int i;
     int bpp;
@@ -144,41 +143,26 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
     }
 
     bpp = (hdr.pixel_bits + 7) >> 3;
-    rmask = gmask = bmask = amask = 0;
     switch(hdr.pixel_bits) {
     case 8:
         if (!indexed) {
                 goto unsupported;
         }
+        format = SDL_PIXELFORMAT_INDEX8;
         break;
 
     case 15:
     case 16:
         /* 15 and 16bpp both seem to use 5 bits/plane. The extra alpha bit
            is ignored for now. */
-        rmask = 0x7c00;
-        gmask = 0x03e0;
-        bmask = 0x001f;
+        format = SDL_PIXELFORMAT_RGB555;
         break;
 
     case 32:
-        alpha = 1;
-        /* fallthrough */
+        format = SDL_PIXELFORMAT_BGRA32;
+        break;
     case 24:
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-        {
-        int s = alpha ? 0 : 8;
-        amask = 0x000000ff >> s;
-        rmask = 0x0000ff00 >> s;
-        gmask = 0x00ff0000 >> s;
-        bmask = 0xff000000 >> s;
-        }
-#else
-        amask = alpha ? 0xff000000 : 0;
-        rmask = 0x00ff0000;
-        gmask = 0x0000ff00;
-        bmask = 0x000000ff;
-#endif
+        format = SDL_PIXELFORMAT_BGR24;
         break;
 
     default:
@@ -194,9 +178,7 @@ SDL_Surface *IMG_LoadTGA_RW(SDL_RWops *src)
 
     w = LE16(hdr.width);
     h = LE16(hdr.height);
-    img = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h,
-                   bpp * 8,
-                   rmask, gmask, bmask, amask);
+    img = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, format);
     if (img == NULL) {
         error = "Out of memory";
         goto error;
diff --git a/IMG_tif.c b/IMG_tif.c
index dfda5da0..e5901b85 100644
--- a/IMG_tif.c
+++ b/IMG_tif.c
@@ -163,7 +163,6 @@ SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
     TIFF* tiff = NULL;
     SDL_Surface* surface = NULL;
     Uint32 img_width, img_height;
-    Uint32 Rmask, Gmask, Bmask, Amask;
 
     if ( !src ) {
         /* The error message has been set in SDL_RWFromFile */
@@ -185,12 +184,7 @@ SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
     lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
     lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
 
-    Rmask = 0x000000FF;
-    Gmask = 0x0000FF00;
-    Bmask = 0x00FF0000;
-    Amask = 0xFF000000;
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, img_width, img_height, 32,
-        Rmask, Gmask, Bmask, Amask);
+    surface = SDL_CreateRGBSurfaceWithFormat(0, img_width, img_height, 0, SDL_PIXELFORMAT_ABGR8888);
     if(!surface)
         goto error;
 
diff --git a/IMG_webp.c b/IMG_webp.c
index 52889c29..79ea8928 100644
--- a/IMG_webp.c
+++ b/IMG_webp.c
@@ -154,10 +154,7 @@ SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
     Sint64 start;
     const char *error = NULL;
     SDL_Surface *volatile surface = NULL;
-    Uint32 Rmask;
-    Uint32 Gmask;
-    Uint32 Bmask;
-    Uint32 Amask;
+    Uint32 format;
     WebPBitstreamFeatures features;
     int raw_data_size;
     uint8_t *raw_data = NULL;
@@ -207,26 +204,13 @@ SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
         goto error;
     }
 
-    /* Check if it's ok !*/
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-    Rmask = 0x000000FF;
-    Gmask = 0x0000FF00;
-    Bmask = 0x00FF0000;
-    Amask = (features.has_alpha) ? 0xFF000000 : 0;
-#else
-    {
-        int s = (features.has_alpha) ? 0 : 8;
-        Rmask = 0xFF000000 >> s;
-        Gmask = 0x00FF0000 >> s;
-        Bmask = 0x0000FF00 >> s;
-        Amask = 0x000000FF >> s;
+    if (features.has_alpha) {
+       format = SDL_PIXELFORMAT_RGBA32;
+    } else {
+       format = SDL_PIXELFORMAT_RGB24;
     }
-#endif
-
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
-            features.width, features.height,
-            features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
-
+    
+    surface = SDL_CreateRGBSurfaceWithFormat(0, features.width, features.height, 0, format);
     if (surface == NULL) {
         error = "Failed to allocate SDL_Surface";
         goto error;
@@ -271,10 +255,7 @@ IMG_Animation *IMG_LoadWEBPAnimation_RW(SDL_RWops *src)
 {
     Sint64 start;
     const char *error = NULL;
-    Uint32 Rmask;
-    Uint32 Gmask;
-    Uint32 Bmask;
-    Uint32 Amask;
+    Uint32 format;
     WebPBitstreamFeatures features;
     struct WebPDemuxer* dmuxer = NULL;
     WebPIterator iter;
@@ -318,22 +299,12 @@ IMG_Animation *IMG_LoadWEBPAnimation_RW(SDL_RWops *src)
         goto error;
     }
 
-    /* Check if it's ok !*/
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
-    Rmask = 0x000000FF;
-    Gmask = 0x0000FF00;
-    Bmask = 0x00FF0000;
-    Amask = (features.has_alpha) ? 0xFF000000 : 0;
-#else
-    {
-        int s = (features.has_alpha) ? 0 : 8;
-        Rmask = 0xFF000000 >> s;
-        Gmask = 0x00FF0000 >> s;
-        Bmask = 0x0000FF00 >> s;
-        Amask = 0x000000FF >> s;
+   if (features.has_alpha) {
+       format = SDL_PIXELFORMAT_RGBA32;
+    } else {
+       format = SDL_PIXELFORMAT_RGB24;
     }
-#endif
-
+    
     wd.size = raw_data_size;
     wd.bytes = raw_data;
     dmuxer = lib.WebPDemuxInternal(&wd, 0, NULL, WEBP_DEMUX_ABI_VERSION);
@@ -348,9 +319,7 @@ IMG_Animation *IMG_LoadWEBPAnimation_RW(SDL_RWops *src)
         if (lib.WebPDemuxGetFrame(dmuxer, frame_idx, &iter) == 0) {
             break;
         }
-        curr = SDL_CreateRGBSurface(SDL_SWSURFACE,
-            features.width, features.height,
-            features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
+        curr = SDL_CreateRGBSurfaceWithFormat(0, features.width, features.height, 0, format);
         if (curr == NULL) {
             error = "Failed to allocate SDL_Surface";
             goto error;
diff --git a/IMG_xcf.c b/IMG_xcf.c
index 98521b8c..a264e822 100644
--- a/IMG_xcf.c
+++ b/IMG_xcf.c
@@ -836,8 +836,7 @@ SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
   }
 
   /* Create the surface of the appropriate type */
-  surface = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
-                 0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
+  surface = SDL_CreateRGBSurfaceWithFormat(0, head->width, head->height, 0, SDL_PIXELFORMAT_ARGB8888);
 
   if ( surface == NULL ) {
     error = "Out of memory";
@@ -853,8 +852,7 @@ SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
   }
   fp = SDL_RWtell (src);
 
-  lays = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
-              0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
+  lays = SDL_CreateRGBSurfaceWithFormat(0, head->width, head->height, 0, SDL_PIXELFORMAT_ARGB8888);
 
   if ( lays == NULL ) {
     error = "Out of memory";
@@ -904,8 +902,7 @@ SDL_Surface *IMG_LoadXCF_RW(SDL_RWops *src)
   if (chnls) {
     SDL_Surface * chs;
 
-    chs = SDL_CreateRGBSurface(SDL_SWSURFACE, head->width, head->height, 32,
-               0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
+    chs = SDL_CreateRGBSurfaceWithFormat(0, head->width, head->height, 0, SDL_PIXELFORMAT_ARGB8888);
 
     if (chs == NULL) {
       error = "Out of memory";
diff --git a/IMG_xpm.c b/IMG_xpm.c
index cbe0271d..993c0376 100644
--- a/IMG_xpm.c
+++ b/IMG_xpm.c
@@ -1053,14 +1053,12 @@ static SDL_Surface *load_xpm(char **xpm, SDL_RWops *src, SDL_bool force_32bit)
     /* Create the new surface */
     if (ncolors <= 256 && !force_32bit) {
         indexed = 1;
-        image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8,
-                         0, 0, 0, 0);
+        image = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_INDEX8);
         im_colors = image->format->palette->colors;
         image->format->palette->ncolors = ncolors;
     } else {
         indexed = 0;
-        image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
-                         0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
+        image = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_ARGB8888);
     }
     if (!image) {
         /* Hmm, some SDL error (out of memory?) */
diff --git a/IMG_xv.c b/IMG_xv.c
index 571915a1..ae589d93 100644
--- a/IMG_xv.c
+++ b/IMG_xv.c
@@ -118,7 +118,7 @@ SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
     }
 
     /* Create the 3-3-2 indexed palette surface */
-    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
+    surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 0, SDL_PIXELFORMAT_RGB332);
     if ( surface == NULL ) {
         error = "Out of memory";
         goto done;