31 PRAGMA48(GCC diagnostic ignored
"-Wshadow")
32 #include <SDL_video.h>
38 const std::string &filename)
40 if (surface ==
nullptr)
44 if (SDL_MUSTLOCK(surface))
45 SDL_LockSurface(surface);
47 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
48 nullptr,
nullptr,
nullptr);
49 if (png_ptr ==
nullptr)
55 png_infop info_ptr = png_create_info_struct(png_ptr);
56 if (info_ptr ==
nullptr)
58 png_destroy_write_struct(&png_ptr,
static_cast<png_infopp
>(
nullptr));
63 if (setjmp(png_jmpbuf(png_ptr)))
65 png_destroy_write_struct(&png_ptr, &info_ptr);
70 FILE *
const fp = fopen(filename.c_str(),
"wb");
78 png_init_io(png_ptr, fp);
80 const int colortype = (surface->format->BitsPerPixel == 24) ?
81 PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
83 png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
84 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
85 PNG_FILTER_TYPE_DEFAULT);
87 png_write_info(png_ptr, info_ptr);
89 png_set_packing(png_ptr);
91 png_bytep *
const row_pointers
94 for (
int i = 0; i < surface->h; i++)
96 row_pointers[i] =
static_cast<png_bytep
>(
static_cast<uint8_t *
>(
97 surface->pixels) +
CAST_SIZE(i * surface->pitch));
100 png_write_image(png_ptr, row_pointers);
101 png_write_end(png_ptr, info_ptr);
105 delete [] row_pointers;
107 png_destroy_write_struct(&png_ptr,
110 if (SDL_MUSTLOCK(surface))
111 SDL_UnlockSurface(surface);
#define reportAlways(...)
bool writePNG(SDL_Surface *const surface, const std::string &filename)