SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
SDL/save_png/libpng.c
1 #include <png.h>
2 #include <SDL.h>
3 #include <stdbool.h>
4 #include "save_png.h"
5
6 bool save_png(const char *filename, uint32_t width, uint32_t height, const void *pixels, SDL_PixelFormat *pixel_format)
7 {
8 FILE *f = fopen(filename, "wb");
9 if (!f) return false;
10
11 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
12 png_infop info = png_create_info_struct(png);
13 uint8_t *row = malloc(width * 3);
14
15 if (setjmp(png_jmpbuf(png))) {
16 png_destroy_write_struct(&png, &info);
17 fclose(f);
18 free(row);
19 return false;
20 }
21 png_init_io(png, f);
22
23 png_set_IHDR(png,
24 info,
25 width,
26 height,
27 8,
28 PNG_COLOR_TYPE_RGB,
29 PNG_INTERLACE_NONE,
30 PNG_COMPRESSION_TYPE_DEFAULT,
31 PNG_FILTER_TYPE_DEFAULT);
32
33 png_write_info(png, info);
34
35 for (uint32_t y = 0; y < height; y++) {
36 const uint32_t *src = (uint32_t *)pixels + y * width;
37 uint8_t *dest = row;
38
39 for (uint32_t x = 0; x < width; x++) {
40 uint8_t dummy;
41 SDL_GetRGBA(*(src++), pixel_format, &dest[0], &dest[1], &dest[2], &dummy);
42 dest += 3;
43 }
44
45 png_write_row(png, row);
46 }
47
48 png_write_end(png, NULL);
49 png_destroy_write_struct(&png, &info);
50 fclose(f);
51
52 return true;
53
54 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.