Skip to content

Commit

Permalink
Added PNG save support based on miniz.c by Rich Geldreich: IMG_SavePN…
Browse files Browse the repository at this point in the history
…G(), IMG_SavePNG_RW()
  • Loading branch information
slouken committed Jun 3, 2013
1 parent 70a37c0 commit 7d93b33
Show file tree
Hide file tree
Showing 5 changed files with 4,858 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
@@ -1,4 +1,7 @@
2.0.0:
Sam Lantinga - Sun Jun 2 22:25:31 PDT 2013
* Added PNG save support based on miniz.c by Rich Geldreich
IMG_SavePNG(), IMG_SavePNG_RW()
Sam Lantinga - Sat Jun 1 19:11:26 PDT 2013
* Updated for SDL 2.0 release
Sam Lantinga - Sat Mar 23 13:36:51 PDT 2013
Expand Down
65 changes: 60 additions & 5 deletions IMG_png.c
Expand Up @@ -19,15 +19,12 @@
3. This notice may not be removed or altered from any source distribution.
*/

#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)

/* This is a PNG image file loading framework */

#include <stdlib.h>
#include <stdio.h>

#include "SDL_image.h"

#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)

#ifdef LOAD_PNG

/*=============================================================================
Expand Down Expand Up @@ -589,3 +586,61 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src)
#endif /* LOAD_PNG */

#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */

/* We'll always have PNG save support */
#define SAVE_PNG

#ifdef SAVE_PNG

#include "miniz.c"

int IMG_SavePNG(SDL_Surface *surface, const char *file)
{
SDL_RWops *dst = SDL_RWFromFile(file, "wb");
if (dst) {
return IMG_SavePNG_RW(surface, dst, 1);
} else {
return -1;
}
}

int IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
int result = -1;

if (dst) {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
static const Uint32 png_format = SDL_PIXELFORMAT_ABGR8888;
#else
static const Uint32 png_format = SDL_PIXELFORMAT_RGBA8888;
#endif
size_t size;
void *png;

if (surface->format->format == png_format) {
png = tdefl_write_image_to_png_file_in_memory(surface->pixels, surface->w, surface->h, surface->pitch, surface->format->BytesPerPixel, &size);
} else {
SDL_Surface *cvt = SDL_ConvertSurfaceFormat(surface, png_format, 0);
if (cvt) {
png = tdefl_write_image_to_png_file_in_memory(cvt->pixels, cvt->w, cvt->h, cvt->pitch, cvt->format->BytesPerPixel, &size);
SDL_FreeSurface(cvt);
}
}
if (png) {
if (SDL_RWwrite(dst, png, size, 1)) {
result = 0;
}
SDL_free(png);
} else {
SDL_SetError("Failed to convert and save image");
}
if (freedst) {
SDL_RWclose(dst);
}
} else {
SDL_SetError("Passed NULL dst");
}
return result;
}

#endif /* SAVE_PNG */
4 changes: 4 additions & 0 deletions SDL_image.h
Expand Up @@ -131,6 +131,10 @@ extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadWEBP_RW(SDL_RWops *src);

extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm);

/* Individual saving functions */
extern DECLSPEC int SDLCALL IMG_SavePNG(SDL_Surface *surface, const char *file);
extern DECLSPEC int SDLCALL IMG_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst);

/* We'll use SDL for reporting errors */
#define IMG_SetError SDL_SetError
#define IMG_GetError SDL_GetError
Expand Down

0 comments on commit 7d93b33

Please sign in to comment.