|
@@ -48,12 +48,11 @@
|
|
|
#if defined(SDL_NEON_INTRINSICS)
|
|
|
#define STBI_NEON
|
|
|
#endif
|
|
|
+#define STBI_ONLY_PNG
|
|
|
#define STBI_ONLY_JPEG
|
|
|
#define STBI_NO_GIF
|
|
|
-#define STBI_NO_PNG
|
|
|
#define STBI_NO_HDR
|
|
|
#define STBI_NO_LINEAR
|
|
|
-#define STBI_NO_ZLIB
|
|
|
#define STBI_NO_STDIO
|
|
|
#define STBI_ASSERT SDL_assert
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
@@ -127,6 +126,252 @@ bool SDL_ConvertPixels_STB(int width, int height,
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
+#ifdef SDL_HAVE_STB
|
|
|
+static int IMG_LoadSTB_IO_read(void *user, char *data, int size)
|
|
|
+{
|
|
|
+ size_t amount = SDL_ReadIO((SDL_IOStream*)user, data, size);
|
|
|
+ return (int)amount;
|
|
|
+}
|
|
|
+
|
|
|
+static void IMG_LoadSTB_IO_skip(void *user, int n)
|
|
|
+{
|
|
|
+ SDL_SeekIO((SDL_IOStream*)user, n, SDL_IO_SEEK_CUR);
|
|
|
+}
|
|
|
+
|
|
|
+static int IMG_LoadSTB_IO_eof(void *user)
|
|
|
+{
|
|
|
+ SDL_IOStream *src = (SDL_IOStream*)user;
|
|
|
+ return SDL_GetIOStatus(src) == SDL_IO_STATUS_EOF;
|
|
|
+}
|
|
|
+
|
|
|
+static SDL_Surface *SDL_LoadSTB_IO(SDL_IOStream *src)
|
|
|
+{
|
|
|
+ Sint64 start;
|
|
|
+ Uint8 magic[26];
|
|
|
+ int w, h, format;
|
|
|
+ stbi_uc *pixels;
|
|
|
+ stbi_io_callbacks rw_callbacks;
|
|
|
+ SDL_Surface *surface = NULL;
|
|
|
+ bool use_palette = false;
|
|
|
+ unsigned int palette_colors[256];
|
|
|
+
|
|
|
+ // src has already been validated
|
|
|
+ start = SDL_TellIO(src);
|
|
|
+
|
|
|
+ if (SDL_ReadIO(src, magic, sizeof(magic)) == sizeof(magic)) {
|
|
|
+ const Uint8 PNG_COLOR_INDEXED = 3;
|
|
|
+ if (magic[0] == 0x89 &&
|
|
|
+ magic[1] == 'P' &&
|
|
|
+ magic[2] == 'N' &&
|
|
|
+ magic[3] == 'G' &&
|
|
|
+ magic[12] == 'I' &&
|
|
|
+ magic[13] == 'H' &&
|
|
|
+ magic[14] == 'D' &&
|
|
|
+ magic[15] == 'R' &&
|
|
|
+ magic[25] == PNG_COLOR_INDEXED) {
|
|
|
+ use_palette = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
|
|
|
+
|
|
|
+ /* Load the image data */
|
|
|
+ rw_callbacks.read = IMG_LoadSTB_IO_read;
|
|
|
+ rw_callbacks.skip = IMG_LoadSTB_IO_skip;
|
|
|
+ rw_callbacks.eof = IMG_LoadSTB_IO_eof;
|
|
|
+ w = h = format = 0; /* silence warning */
|
|
|
+ if (use_palette) {
|
|
|
+ /* Unused palette entries will be opaque white */
|
|
|
+ SDL_memset(palette_colors, 0xff, sizeof(palette_colors));
|
|
|
+
|
|
|
+ pixels = stbi_load_from_callbacks_with_palette(
|
|
|
+ &rw_callbacks,
|
|
|
+ src,
|
|
|
+ &w,
|
|
|
+ &h,
|
|
|
+ palette_colors,
|
|
|
+ SDL_arraysize(palette_colors)
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ pixels = stbi_load_from_callbacks(
|
|
|
+ &rw_callbacks,
|
|
|
+ src,
|
|
|
+ &w,
|
|
|
+ &h,
|
|
|
+ &format,
|
|
|
+ STBI_default
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (!pixels) {
|
|
|
+ SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (use_palette) {
|
|
|
+ surface = SDL_CreateSurfaceFrom(
|
|
|
+ w,
|
|
|
+ h,
|
|
|
+ SDL_PIXELFORMAT_INDEX8,
|
|
|
+ pixels,
|
|
|
+ w
|
|
|
+ );
|
|
|
+ if (surface) {
|
|
|
+ bool has_colorkey = false;
|
|
|
+ int colorkey_index = -1;
|
|
|
+ bool has_alpha = false;
|
|
|
+ SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
|
|
|
+ if (palette) {
|
|
|
+ int i;
|
|
|
+ Uint8 *palette_bytes = (Uint8 *)palette_colors;
|
|
|
+
|
|
|
+ for (i = 0; i < palette->ncolors; i++) {
|
|
|
+ palette->colors[i].r = *palette_bytes++;
|
|
|
+ palette->colors[i].g = *palette_bytes++;
|
|
|
+ palette->colors[i].b = *palette_bytes++;
|
|
|
+ palette->colors[i].a = *palette_bytes++;
|
|
|
+ if (palette->colors[i].a != SDL_ALPHA_OPAQUE) {
|
|
|
+ if (palette->colors[i].a == SDL_ALPHA_TRANSPARENT && !has_colorkey) {
|
|
|
+ has_colorkey = true;
|
|
|
+ colorkey_index = i;
|
|
|
+ } else {
|
|
|
+ /* Partial opacity or multiple colorkeys */
|
|
|
+ has_alpha = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (has_alpha) {
|
|
|
+ SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
|
|
|
+ } else if (has_colorkey) {
|
|
|
+ SDL_SetSurfaceColorKey(surface, true, colorkey_index);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* FIXME: This sucks. It'd be better to allocate the surface first, then
|
|
|
+ * write directly to the pixel buffer:
|
|
|
+ * https://github.com/nothings/stb/issues/58
|
|
|
+ * -flibit
|
|
|
+ */
|
|
|
+ surface->flags &= ~SDL_SURFACE_PREALLOCATED;
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (format == STBI_grey || format == STBI_rgb || format == STBI_rgb_alpha) {
|
|
|
+ surface = SDL_CreateSurfaceFrom(
|
|
|
+ w,
|
|
|
+ h,
|
|
|
+ (format == STBI_rgb_alpha) ? SDL_PIXELFORMAT_RGBA32 :
|
|
|
+ (format == STBI_rgb) ? SDL_PIXELFORMAT_RGB24 :
|
|
|
+ SDL_PIXELFORMAT_INDEX8,
|
|
|
+ pixels,
|
|
|
+ w * format
|
|
|
+ );
|
|
|
+ if (surface) {
|
|
|
+ /* Set a grayscale palette for gray images */
|
|
|
+ if (surface->format == SDL_PIXELFORMAT_INDEX8) {
|
|
|
+ SDL_Palette *palette = SDL_CreateSurfacePalette(surface);
|
|
|
+ if (palette) {
|
|
|
+ int i;
|
|
|
+
|
|
|
+ for (i = 0; i < palette->ncolors; i++) {
|
|
|
+ palette->colors[i].r = (Uint8)i;
|
|
|
+ palette->colors[i].g = (Uint8)i;
|
|
|
+ palette->colors[i].b = (Uint8)i;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* FIXME: This sucks. It'd be better to allocate the surface first, then
|
|
|
+ * write directly to the pixel buffer:
|
|
|
+ * https://github.com/nothings/stb/issues/58
|
|
|
+ * -flibit
|
|
|
+ */
|
|
|
+ surface->flags &= ~SDL_SURFACE_PREALLOCATED;
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (format == STBI_grey_alpha) {
|
|
|
+ surface = SDL_CreateSurface(w, h, SDL_PIXELFORMAT_RGBA32);
|
|
|
+ if (surface) {
|
|
|
+ Uint8 *src_ptr = pixels;
|
|
|
+ Uint8 *dst = (Uint8 *)surface->pixels;
|
|
|
+ int skip = surface->pitch - (surface->w * 4);
|
|
|
+ int row, col;
|
|
|
+
|
|
|
+ for (row = 0; row < h; ++row) {
|
|
|
+ for (col = 0; col < w; ++col) {
|
|
|
+ Uint8 c = *src_ptr++;
|
|
|
+ Uint8 a = *src_ptr++;
|
|
|
+ *dst++ = c;
|
|
|
+ *dst++ = c;
|
|
|
+ *dst++ = c;
|
|
|
+ *dst++ = a;
|
|
|
+ }
|
|
|
+ dst += skip;
|
|
|
+ }
|
|
|
+ stbi_image_free(pixels);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ SDL_SetError("Unknown image format: %d", format);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!surface) {
|
|
|
+ /* The error message should already be set */
|
|
|
+ stbi_image_free(pixels); /* calls SDL_free() */
|
|
|
+ SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
|
|
|
+ }
|
|
|
+ return surface;
|
|
|
+}
|
|
|
+#endif // SDL_HAVE_STB
|
|
|
+
|
|
|
+SDL_Surface *SDL_LoadPNG_IO(SDL_IOStream *src, bool closeio)
|
|
|
+{
|
|
|
+ Sint64 start;
|
|
|
+ Uint8 magic[4];
|
|
|
+ bool is_PNG;
|
|
|
+ SDL_Surface *surface = NULL;
|
|
|
+
|
|
|
+ CHECK_PARAM(!src) {
|
|
|
+ SDL_InvalidParamError("src");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+ start = SDL_TellIO(src);
|
|
|
+ is_PNG = false;
|
|
|
+ if (SDL_ReadIO(src, magic, sizeof(magic)) == sizeof(magic)) {
|
|
|
+ if (magic[0] == 0x89 &&
|
|
|
+ magic[1] == 'P' &&
|
|
|
+ magic[2] == 'N' &&
|
|
|
+ magic[3] == 'G') {
|
|
|
+ is_PNG = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ SDL_SeekIO(src, start, SDL_IO_SEEK_SET);
|
|
|
+
|
|
|
+ if (!is_PNG) {
|
|
|
+ SDL_SetError("File is not a PNG file");
|
|
|
+ goto done;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef SDL_HAVE_STB
|
|
|
+ surface = SDL_LoadSTB_IO(src);
|
|
|
+#else
|
|
|
+ SDL_SetError("SDL not built with STB image support");
|
|
|
+#endif // SDL_HAVE_STB
|
|
|
+
|
|
|
+done:
|
|
|
+ if (src && closeio) {
|
|
|
+ SDL_CloseIO(src);
|
|
|
+ }
|
|
|
+ return surface;
|
|
|
+}
|
|
|
+
|
|
|
+SDL_Surface *SDL_LoadPNG(const char *file)
|
|
|
+{
|
|
|
+ SDL_IOStream *stream = SDL_IOFromFile(file, "rb");
|
|
|
+ if (!stream) {
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ return SDL_LoadPNG_IO(stream, true);
|
|
|
+}
|
|
|
+
|
|
|
#ifdef SDL_HAVE_STB
|
|
|
static void SDL_STBWriteFunc(void *context, void *data, int size)
|
|
|
{
|
|
@@ -136,46 +381,46 @@ static void SDL_STBWriteFunc(void *context, void *data, int size)
|
|
|
|
|
|
bool SDL_SavePNG_IO(SDL_Surface *surface, SDL_IOStream *dst, bool closeio)
|
|
|
{
|
|
|
-#ifdef SDL_HAVE_STB
|
|
|
- bool retval = true;
|
|
|
+ bool retval = false;
|
|
|
|
|
|
// Make sure we have somewhere to save
|
|
|
CHECK_PARAM(!SDL_SurfaceValid(surface)) {
|
|
|
- retval = SDL_InvalidParamError("surface");
|
|
|
+ SDL_InvalidParamError("surface");
|
|
|
goto done;
|
|
|
}
|
|
|
CHECK_PARAM(!dst) {
|
|
|
- retval = SDL_InvalidParamError("dst");
|
|
|
+ SDL_InvalidParamError("dst");
|
|
|
goto done;
|
|
|
}
|
|
|
|
|
|
+#ifdef SDL_HAVE_STB
|
|
|
bool free_surface = false;
|
|
|
- if (surface->format != SDL_PIXELFORMAT_ABGR8888) {
|
|
|
- surface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ABGR8888);
|
|
|
+ if (surface->format != SDL_PIXELFORMAT_RGBA32) {
|
|
|
+ surface = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA32);
|
|
|
if (!surface) {
|
|
|
- retval = false;
|
|
|
goto done;
|
|
|
}
|
|
|
free_surface = true;
|
|
|
}
|
|
|
|
|
|
- if (!stbi_write_png_to_func(SDL_STBWriteFunc, dst, surface->w, surface->h, 4, surface->pixels, surface->pitch)) {
|
|
|
- retval = SDL_SetError("Failed to write PNG");
|
|
|
+ if (stbi_write_png_to_func(SDL_STBWriteFunc, dst, surface->w, surface->h, 4, surface->pixels, surface->pitch)) {
|
|
|
+ retval = true;
|
|
|
+ } else {
|
|
|
+ SDL_SetError("Failed to write PNG");
|
|
|
}
|
|
|
|
|
|
if (free_surface) {
|
|
|
SDL_DestroySurface(surface);
|
|
|
}
|
|
|
+#else
|
|
|
+ SDL_SetError("SDL not built with STB image support");
|
|
|
+#endif
|
|
|
|
|
|
done:
|
|
|
if (dst && closeio) {
|
|
|
- retval = SDL_CloseIO(dst);
|
|
|
+ retval &= SDL_CloseIO(dst);
|
|
|
}
|
|
|
-
|
|
|
return retval;
|
|
|
-#else
|
|
|
- return SDL_SetError("SDL not built with STB image write support");
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
bool SDL_SavePNG(SDL_Surface *surface, const char *file)
|
|
@@ -188,6 +433,6 @@ bool SDL_SavePNG(SDL_Surface *surface, const char *file)
|
|
|
|
|
|
return SDL_SavePNG_IO(surface, stream, true);
|
|
|
#else
|
|
|
- return SDL_SetError("SDL not built with STB image write support");
|
|
|
+ return SDL_SetError("SDL not built with STB image support");
|
|
|
#endif
|
|
|
}
|