From 50b6e5eb7e4a000fa98d673e1240278c34d06fcc Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 1 Jul 2023 23:59:21 -0700
Subject: [PATCH] Implemented SDL_LoadBMP() and SDL_SaveBMP() as functions
Fixes https://github.com/libsdl-org/SDL/issues/7902
---
include/SDL3/SDL_surface.h | 49 +++++++++++++++++++++----------
src/dynapi/SDL_dynapi.sym | 2 ++
src/dynapi/SDL_dynapi_overrides.h | 2 ++
src/dynapi/SDL_dynapi_procs.h | 2 ++
src/video/SDL_bmp.c | 10 +++++++
5 files changed, 49 insertions(+), 16 deletions(-)
diff --git a/include/SDL3/SDL_surface.h b/include/SDL3/SDL_surface.h
index 94a0c31c911b..b1304b018e08 100644
--- a/include/SDL3/SDL_surface.h
+++ b/include/SDL3/SDL_surface.h
@@ -227,10 +227,6 @@ extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface);
* The new surface should be freed with SDL_DestroySurface(). Not doing so
* will result in a memory leak.
*
- * src is an open SDL_RWops buffer, typically loaded with SDL_RWFromFile.
- * Alternitavely, you might also use the macro SDL_LoadBMP to load a bitmap
- * from a file, convert it to an SDL_Surface and then close the file.
- *
* \param src the data stream for the surface
* \param freesrc non-zero to close the stream after being read
* \returns a pointer to a new SDL_Surface structure or NULL if there was an
@@ -239,19 +235,28 @@ extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface);
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_DestroySurface
- * \sa SDL_RWFromFile
* \sa SDL_LoadBMP
* \sa SDL_SaveBMP_RW
*/
-extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops *src,
- int freesrc);
+extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops *src, int freesrc);
/**
- * Load a surface from a file.
+ * Load a BMP image from a file.
+ *
+ * The new surface should be freed with SDL_DestroySurface(). Not doing so
+ * will result in a memory leak.
+ *
+ * \param file the BMP file to load
+ * \returns a pointer to a new SDL_Surface structure or NULL if there was an
+ * error; call SDL_GetError() for more information.
*
- * Convenience macro.
+ * \since This function is available since SDL 3.0.0.
+ *
+ * \sa SDL_DestroySurface
+ * \sa SDL_LoadBMP_RW
+ * \sa SDL_SaveBMP
*/
-#define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1)
+extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP(const char *file);
/**
* Save a surface to a seekable SDL data stream in BMP format.
@@ -273,16 +278,28 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP_RW(SDL_RWops *src,
* \sa SDL_LoadBMP_RW
* \sa SDL_SaveBMP
*/
-extern DECLSPEC int SDLCALL SDL_SaveBMP_RW
- (SDL_Surface *surface, SDL_RWops *dst, int freedst);
+extern DECLSPEC int SDLCALL SDL_SaveBMP_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);
/**
- * Save a surface to a file.
+ * Save a surface to a file.
+ *
+ * Surfaces with a 24-bit, 32-bit and paletted 8-bit format get saved in the
+ * BMP directly. Other RGB formats with 8-bit or higher get converted to a
+ * 24-bit surface or, if they have an alpha mask or a colorkey, to a 32-bit
+ * surface before they are saved. YUV and paletted 1-bit and 4-bit formats are
+ * not supported.
*
- * Convenience macro.
+ * \param surface the SDL_Surface structure containing the image to be saved
+ * \param file a file to save to
+ * \returns 0 on success or a negative error code on failure; call
+ * SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 3.0.0.
+ *
+ * \sa SDL_LoadBMP
+ * \sa SDL_SaveBMP_RW
*/
-#define SDL_SaveBMP(surface, file) \
- SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1)
+extern DECLSPEC int SDLCALL SDL_SaveBMP(SDL_Surface *surface, const char *file);
/**
* Set the RLE acceleration hint for a surface.
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index 52f933f825c0..5479a364f3b8 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -867,6 +867,8 @@ SDL3_0.0.0 {
SDL_HasWindowSurface;
SDL_DestroyWindowSurface;
SDL_GetNaturalDisplayOrientation;
+ SDL_LoadBMP;
+ SDL_SaveBMP;
# extra symbols go here (don't modify this line)
local: *;
};
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index f0de22b5e29b..e0883df643d8 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -893,3 +893,5 @@
#define SDL_HasWindowSurface SDL_HasWindowSurface_REAL
#define SDL_DestroyWindowSurface SDL_DestroyWindowSurface_REAL
#define SDL_GetNaturalDisplayOrientation SDL_GetNaturalDisplayOrientation_REAL
+#define SDL_LoadBMP SDL_LoadBMP_REAL
+#define SDL_SaveBMP SDL_SaveBMP_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index c5e059bbd10e..d981b8498de4 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -938,3 +938,5 @@ SDL_DYNAPI_PROC(int,SDL_hid_get_report_descriptor,(SDL_hid_device *a, unsigned c
SDL_DYNAPI_PROC(SDL_bool,SDL_HasWindowSurface,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_DestroyWindowSurface,(SDL_Window *a),(a),return)
SDL_DYNAPI_PROC(SDL_DisplayOrientation,SDL_GetNaturalDisplayOrientation,(SDL_DisplayID a),(a),return)
+SDL_DYNAPI_PROC(SDL_Surface*,SDL_LoadBMP,(const char *a),(a),return)
+SDL_DYNAPI_PROC(int,SDL_SaveBMP,(SDL_Surface *a, const char *b),(a,b),return)
diff --git a/src/video/SDL_bmp.c b/src/video/SDL_bmp.c
index 5c33c72b6e20..4133f62d4f45 100644
--- a/src/video/SDL_bmp.c
+++ b/src/video/SDL_bmp.c
@@ -624,6 +624,11 @@ SDL_Surface *SDL_LoadBMP_RW(SDL_RWops *src, int freesrc)
return surface;
}
+SDL_Surface *SDL_LoadBMP(const char *file)
+{
+ return SDL_LoadBMP_RW(SDL_RWFromFile(file, "rb"), 1);
+}
+
int SDL_SaveBMP_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
/* !!! FIXME: this calls SDL_ClearError() and then checks if an error happened during this function to
@@ -869,3 +874,8 @@ int SDL_SaveBMP_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
}
return (SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1;
}
+
+int SDL_SaveBMP(SDL_Surface *surface, const char *file)
+{
+ return SDL_SaveBMP_RW(surface, SDL_RWFromFile(file, "wb"), 1);
+}