Replace stb_image_write with miniz for SDL_SavePNG()

Fixes https://github.com/libsdl-org/SDL/issues/14219
This commit is contained in:
Sam Lantinga
2025-10-11 19:37:05 -07:00
parent c89fed4eae
commit 70eceec77b
4 changed files with 5079 additions and 1753 deletions

View File

@@ -161,6 +161,7 @@ def find_symbols_in_file(file: pathlib.Path) -> int:
"src/libm", "src/libm",
"src/hidapi", "src/hidapi",
"src/video/khronos", "src/video/khronos",
"src/video/miniz.h",
"src/video/stb_image.h", "src/video/stb_image.h",
"include/SDL3", "include/SDL3",
"build-scripts/gen_audio_resampler_filter.c", "build-scripts/gen_audio_resampler_filter.c",

View File

@@ -23,8 +23,8 @@
#include "SDL_stb_c.h" #include "SDL_stb_c.h"
#include "SDL_surface_c.h" #include "SDL_surface_c.h"
// We currently only support JPEG, but we could add other image formats if we wanted
#ifdef SDL_HAVE_STB #ifdef SDL_HAVE_STB
////////////////////////////////////////////////////////////////////////////
#define malloc SDL_malloc #define malloc SDL_malloc
#define realloc SDL_realloc #define realloc SDL_realloc
#define free SDL_free #define free SDL_free
@@ -58,15 +58,23 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"
#undef memcpy ////////////////////////////////////////////////////////////////////////////
#define STB_IMAGE_WRITE_IMPLEMENTATION #define MZ_ASSERT(x) SDL_assert(x)
#define STB_IMAGE_WRITE_STATIC //#undef memcpy
#define STBI_WRITE_NO_STDIO //#define memcpy SDL_memcpy
#define STBIW_ASSERT SDL_assert //#undef memset
#include "stb_image_write.h" //#define memset SDL_memset
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define MINIZ_LITTLE_ENDIAN 1
#else
#define MINIZ_LITTLE_ENDIAN 0
#endif
#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 0
#define MINIZ_SDL_NOUNUSED
#include "miniz.h"
#undef memset #undef memset
#endif #endif // SDL_HAVE_STB
#ifdef SDL_HAVE_STB #ifdef SDL_HAVE_STB
static bool SDL_ConvertPixels_MJPG_to_NV12(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch) static bool SDL_ConvertPixels_MJPG_to_NV12(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
@@ -372,13 +380,6 @@ SDL_Surface *SDL_LoadPNG(const char *file)
return SDL_LoadPNG_IO(stream, true); return SDL_LoadPNG_IO(stream, true);
} }
#ifdef SDL_HAVE_STB
static void SDL_STBWriteFunc(void *context, void *data, int size)
{
SDL_WriteIO(context, data, size);
}
#endif
bool SDL_SavePNG_IO(SDL_Surface *surface, SDL_IOStream *dst, bool closeio) bool SDL_SavePNG_IO(SDL_Surface *surface, SDL_IOStream *dst, bool closeio)
{ {
bool retval = false; bool retval = false;
@@ -403,10 +404,15 @@ bool SDL_SavePNG_IO(SDL_Surface *surface, SDL_IOStream *dst, bool closeio)
free_surface = true; free_surface = true;
} }
if (stbi_write_png_to_func(SDL_STBWriteFunc, dst, surface->w, surface->h, 4, surface->pixels, surface->pitch)) { size_t size = 0;
retval = true; void *png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, SDL_BYTESPERPIXEL(surface->format), surface->pitch, &size);
if (png) {
if (SDL_WriteIO(dst, png, size)) {
retval = true;
}
mz_free(png); /* calls SDL_free() */
} else { } else {
SDL_SetError("Failed to write PNG"); SDL_SetError("Failed to convert and save image");
} }
if (free_surface) { if (free_surface) {

5054
src/video/miniz.h Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff