-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIMG_savepng.c
281 lines (260 loc) · 7.95 KB
/
IMG_savepng.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Based on zlib license - see http://www.gzip.org/zlib/zlib_license.html
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
"Philip D. Bober" <[email protected]>
*/
/**
* 4/17/04 - IMG_SavePNG & IMG_SavePNG_RW - Philip D. Bober
* 11/08/2004 - Compr fix, levels -1,1-7 now work - Tyler Montbriand
*/
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_byteorder.h>
#include <png.h>
#include "IMG_savepng.h"
int IMG_SavePNG(const char *file, SDL_Surface *surf,int compression){
SDL_RWops *fp;
int ret;
fp=SDL_RWFromFile(file,"wb");
if( fp == NULL ) {
return (-1);
}
ret=IMG_SavePNG_RW(fp,surf,compression);
SDL_RWclose(fp);
return ret;
}
static void png_write_data(png_structp png_ptr,png_bytep data, png_size_t length){
SDL_RWops *rp = (SDL_RWops*) png_get_io_ptr(png_ptr);
SDL_RWwrite(rp,data,1,length);
}
int IMG_SavePNG_RW(SDL_RWops *src, SDL_Surface *surf,int compression){
png_structp png_ptr;
png_infop info_ptr;
SDL_PixelFormat *fmt=NULL;
SDL_Surface *tempsurf=NULL;
int ret,funky_format,used_alpha;
unsigned int i,temp_alpha;
png_colorp palette;
Uint8 *palette_alpha=NULL;
png_byte **row_pointers=NULL;
png_ptr=NULL;info_ptr=NULL;palette=NULL;ret=-1;
funky_format=0;
if( !src || !surf) {
goto savedone; /* Nothing to do. */
}
row_pointers=(png_byte **)malloc(surf->h * sizeof(png_byte*));
if (!row_pointers) {
SDL_SetError("Couldn't allocate memory for rowpointers");
goto savedone;
}
png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
if (!png_ptr){
SDL_SetError("Couldn't allocate memory for PNG file");
goto savedone;
}
info_ptr= png_create_info_struct(png_ptr);
if (!info_ptr){
SDL_SetError("Couldn't allocate image information for PNG file");
goto savedone;
}
/* setup custom writer functions */
png_set_write_fn(png_ptr,(voidp)src,png_write_data,NULL);
if (setjmp(png_jmpbuf(png_ptr))){
SDL_SetError("Unknown error writing PNG");
goto savedone;
}
if(compression>Z_BEST_COMPRESSION)
compression=Z_BEST_COMPRESSION;
if(compression == Z_NO_COMPRESSION) // No compression
{
png_set_filter(png_ptr,0,PNG_FILTER_NONE);
png_set_compression_level(png_ptr,Z_NO_COMPRESSION);
}
else if(compression<0) // Default compression
png_set_compression_level(png_ptr,Z_DEFAULT_COMPRESSION);
else
png_set_compression_level(png_ptr,compression);
fmt=surf->format;
if(fmt->BitsPerPixel==8){ /* Paletted */
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
palette=(png_colorp) malloc(fmt->palette->ncolors * sizeof(png_color));
if (!palette) {
SDL_SetError("Couldn't create memory for palette");
goto savedone;
}
for (i=0;i<fmt->palette->ncolors;i++) {
palette[i].red=fmt->palette->colors[i].r;
palette[i].green=fmt->palette->colors[i].g;
palette[i].blue=fmt->palette->colors[i].b;
}
png_set_PLTE(png_ptr,info_ptr,palette,fmt->palette->ncolors);
if (surf->flags&SDL_SRCCOLORKEY) {
palette_alpha=(Uint8 *)malloc((fmt->colorkey+1)*sizeof(Uint8));
if (!palette_alpha) {
SDL_SetError("Couldn't create memory for palette transparency");
goto savedone;
}
/* FIXME: memset? */
for (i=0;i<(fmt->colorkey+1);i++) {
palette_alpha[i]=255;
}
palette_alpha[fmt->colorkey]=0;
png_set_tRNS(png_ptr,info_ptr,palette_alpha,fmt->colorkey+1,NULL);
}
}else{ /* Truecolor */
if (fmt->Amask) {
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
} else {
png_set_IHDR(png_ptr,info_ptr,
surf->w,surf->h,8,PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
}
}
png_write_info(png_ptr, info_ptr);
if (fmt->BitsPerPixel==8) { /* Paletted */
for(i=0;i<surf->h;i++){
row_pointers[i]= ((png_byte*)surf->pixels) + i*surf->pitch;
}
if(SDL_MUSTLOCK(surf)){
SDL_LockSurface(surf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(surf)){
SDL_UnlockSurface(surf);
}
}else{ /* Truecolor */
if(fmt->BytesPerPixel==3){
if(fmt->Amask){ /* check for 24 bit with alpha */
funky_format=1;
}else{
/* Check for RGB/BGR/GBR/RBG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF0000
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0x0000FF){
#else
if(fmt->Rmask!=0x0000FF
|| fmt->Gmask!=0x00FF00
|| fmt->Bmask!=0xFF0000){
#endif
funky_format=1;
}
}
}else if (fmt->BytesPerPixel==4){
if (!fmt->Amask) { /* check for 32bit but no alpha */
funky_format=1;
}else{
/* Check for ARGB/ABGR/GBAR/RABG/etc surfaces.*/
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if(fmt->Rmask!=0xFF000000
|| fmt->Gmask!=0x00FF0000
|| fmt->Bmask!=0x0000FF00
|| fmt->Amask!=0x000000FF){
#else
if(fmt->Rmask!=0x000000FF
|| fmt->Gmask!=0x0000FF00
|| fmt->Bmask!=0x00FF0000
|| fmt->Amask!=0xFF000000){
#endif
funky_format=1;
}
}
}else{ /* 555 or 565 16 bit color */
funky_format=1;
}
if (funky_format) {
/* Allocate non-funky format, and copy pixeldata in*/
if(fmt->Amask){
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
}else{
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0xff0000, 0x00ff00, 0x0000ff, 0x00000000);
#else
tempsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 24,
0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000);
#endif
}
if(!tempsurf){
SDL_SetError("Couldn't allocate temp surface");
goto savedone;
}
if(surf->flags&SDL_SRCALPHA){
temp_alpha=fmt->alpha;
used_alpha=1;
SDL_SetAlpha(surf,0,255); /* Set for an opaque blit */
}else{
used_alpha=0;
}
if(SDL_BlitSurface(surf,NULL,tempsurf,NULL)!=0){
SDL_SetError("Couldn't blit surface to temp surface");
SDL_FreeSurface(tempsurf);
goto savedone;
}
if (used_alpha) {
SDL_SetAlpha(surf,SDL_SRCALPHA,(Uint8)temp_alpha); /* Restore alpha settings*/
}
for(i=0;i<tempsurf->h;i++){
row_pointers[i]= ((png_byte*)tempsurf->pixels) + i*tempsurf->pitch;
}
if(SDL_MUSTLOCK(tempsurf)){
SDL_LockSurface(tempsurf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(tempsurf)){
SDL_UnlockSurface(tempsurf);
}
SDL_FreeSurface(tempsurf);
} else {
for(i=0;i<surf->h;i++){
row_pointers[i]= ((png_byte*)surf->pixels) + i*surf->pitch;
}
if(SDL_MUSTLOCK(surf)){
SDL_LockSurface(surf);
}
png_write_image(png_ptr, row_pointers);
if(SDL_MUSTLOCK(surf)){
SDL_UnlockSurface(surf);
}
}
}
png_write_end(png_ptr, NULL);
ret=0; /* got here, so nothing went wrong. YAY! */
savedone: /* clean up and return */
png_destroy_write_struct(&png_ptr,&info_ptr);
if (palette) {
free(palette);
}
if (palette_alpha) {
free(palette_alpha);
}
if (row_pointers) {
free(row_pointers);
}
return ret;
}