comparison utils/frminfo/png_write.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 90005975cdbb
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2006 by the FIFE Team *
3 * fife-public@lists.sourceforge.net *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <cstdio>
24
25 // 3rd party library includes
26 #include <png.h>
27 #include <SDL.h>
28
29 // FIFE includes
30
31 int write_png(const char *filename, SDL_Surface& surface) {
32 FILE *file = fopen(filename, "wb");
33 if (!file)
34 return -1;
35
36 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
37 if (!png_ptr)
38 return -1;
39
40 png_infop info_ptr = png_create_info_struct(png_ptr);
41 if (!info_ptr) {
42 png_destroy_write_struct(&png_ptr, NULL);
43 fclose(file);
44 return -1;
45 }
46
47 if (setjmp(png_jmpbuf(png_ptr))) {
48 png_destroy_write_struct(&png_ptr, &info_ptr);
49 fclose(file);
50 return -1;
51 }
52
53 png_init_io(png_ptr, file);
54
55 png_set_IHDR(png_ptr, info_ptr, surface.w, surface.h, 8, PNG_COLOR_TYPE_RGB_ALPHA ,
56 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
57
58 /* does this leak memory? */
59 png_byte** row_pointers = (png_byte**)png_malloc(png_ptr, surface.h * sizeof(png_bytep));
60
61 png_byte* p = (png_byte*)surface.pixels;
62 for (int i = 0; i < surface.h; ++i) {
63 row_pointers[i] = p;
64 p += surface.pitch;
65 }
66
67 png_set_rows(png_ptr, info_ptr, row_pointers);
68
69 png_write_png(png_ptr, info_ptr, 0, NULL);
70
71 return 0;
72 }