2 Copyright (C) 2006 Serge "(515)" Ziryukin, Forest "LordHavoc" Hale
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to:
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA
24 //[515]: png implemented into DP ONLY FOR TESTING 2d stuff with csqc
25 // so delete this bullshit :D
27 //LordHavoc: rewrote most of this.
31 #include "image_png.h"
34 static void (*qpng_set_sig_bytes) (void*, int);
35 static int (*qpng_sig_cmp) (const unsigned char*, size_t, size_t);
36 static void* (*qpng_create_read_struct) (const char*, void*, void(*)(void *png, const char *message), void(*)(void *png, const char *message));
37 static void* (*qpng_create_write_struct) (const char*, void*, void(*)(void *png, const char *message), void(*)(void *png, const char *message));
38 static void* (*qpng_create_info_struct) (void*);
39 static void (*qpng_read_info) (void*, void*);
40 static void (*qpng_set_compression_level) (void*, int);
41 static void (*qpng_set_filter) (void*, int, int);
42 static void (*qpng_set_expand) (void*);
43 static void (*qpng_set_palette_to_rgb) (void*);
44 static void (*qpng_set_tRNS_to_alpha) (void*);
45 static void (*qpng_set_gray_to_rgb) (void*);
46 static void (*qpng_set_filler) (void*, unsigned int, int);
47 static void (*qpng_set_IHDR) (void*, void*, unsigned long, unsigned long, int, int, int, int, int);
48 static void (*qpng_set_packing) (void*);
49 static void (*qpng_set_bgr) (void*);
50 static int (*qpng_set_interlace_handling) (void*);
51 static void (*qpng_read_update_info) (void*, void*);
52 static void (*qpng_read_image) (void*, unsigned char**);
53 static void (*qpng_read_end) (void*, void*);
54 static void (*qpng_destroy_read_struct) (void**, void**, void**);
55 static void (*qpng_destroy_write_struct) (void**, void**);
56 static void (*qpng_set_read_fn) (void*, void*, void(*)(void *png, unsigned char *data, size_t length));
57 static void (*qpng_set_write_fn) (void*, void*, void(*)(void *png, unsigned char *data, size_t length), void(*)(void *png));
58 static unsigned int (*qpng_get_valid) (void*, void*, unsigned int);
59 static unsigned int (*qpng_get_rowbytes) (void*, void*);
60 static unsigned char (*qpng_get_channels) (void*, void*);
61 static unsigned char (*qpng_get_bit_depth) (void*, void*);
62 static unsigned int (*qpng_get_IHDR) (void*, void*, unsigned long*, unsigned long*, int *, int *, int *, int *, int *);
63 static unsigned int (*qpng_access_version_number) (void); // FIXME is this return type right? It is a png_uint_32 in libpng
64 static void (*qpng_write_info) (void*, void*);
65 static void (*qpng_write_row) (void*, unsigned char*);
66 static void (*qpng_write_end) (void*, void*);
68 // libpng 1.4+ longjmp hack
69 typedef void (*qpng_longjmp_ptr) (jmp_buf, int);
70 static jmp_buf* (*qpng_set_longjmp_fn) (void *, qpng_longjmp_ptr, size_t);
71 #define qpng_jmpbuf_14(png_ptr) (*qpng_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf)))
73 // libpng 1.2 longjmp hack
74 #define qpng_jmpbuf_12(png_ptr) (*((jmp_buf *) png_ptr))
76 // all version support
77 #define qpng_jmpbuf(png_ptr) \
78 (qpng_set_longjmp_fn ? qpng_jmpbuf_14(png_ptr) : qpng_jmpbuf_12(png_ptr))
80 static dllfunction_t pngfuncs[] =
82 {"png_set_sig_bytes", (void **) &qpng_set_sig_bytes},
83 {"png_sig_cmp", (void **) &qpng_sig_cmp},
84 {"png_create_read_struct", (void **) &qpng_create_read_struct},
85 {"png_create_write_struct", (void **) &qpng_create_write_struct},
86 {"png_create_info_struct", (void **) &qpng_create_info_struct},
87 {"png_read_info", (void **) &qpng_read_info},
88 {"png_set_compression_level", (void **) &qpng_set_compression_level},
89 {"png_set_filter", (void **) &qpng_set_filter},
90 {"png_set_expand", (void **) &qpng_set_expand},
91 {"png_set_palette_to_rgb", (void **) &qpng_set_palette_to_rgb},
92 {"png_set_tRNS_to_alpha", (void **) &qpng_set_tRNS_to_alpha},
93 {"png_set_gray_to_rgb", (void **) &qpng_set_gray_to_rgb},
94 {"png_set_filler", (void **) &qpng_set_filler},
95 {"png_set_IHDR", (void **) &qpng_set_IHDR},
96 {"png_set_packing", (void **) &qpng_set_packing},
97 {"png_set_bgr", (void **) &qpng_set_bgr},
98 {"png_set_interlace_handling", (void **) &qpng_set_interlace_handling},
99 {"png_read_update_info", (void **) &qpng_read_update_info},
100 {"png_read_image", (void **) &qpng_read_image},
101 {"png_read_end", (void **) &qpng_read_end},
102 {"png_destroy_read_struct", (void **) &qpng_destroy_read_struct},
103 {"png_destroy_write_struct", (void **) &qpng_destroy_write_struct},
104 {"png_set_read_fn", (void **) &qpng_set_read_fn},
105 {"png_set_write_fn", (void **) &qpng_set_write_fn},
106 {"png_get_valid", (void **) &qpng_get_valid},
107 {"png_get_rowbytes", (void **) &qpng_get_rowbytes},
108 {"png_get_channels", (void **) &qpng_get_channels},
109 {"png_get_bit_depth", (void **) &qpng_get_bit_depth},
110 {"png_get_IHDR", (void **) &qpng_get_IHDR},
111 {"png_access_version_number", (void **) &qpng_access_version_number},
112 {"png_write_info", (void **) &qpng_write_info},
113 {"png_write_row", (void **) &qpng_write_row},
114 {"png_write_end", (void **) &qpng_write_end},
117 static dllfunction_t png14funcs[] =
119 {"png_set_longjmp_fn", (void **) &qpng_set_longjmp_fn},
123 // Handle for PNG DLL
124 dllhandle_t png_dll = NULL;
125 dllhandle_t png14_dll = NULL;
129 =================================================================
133 =================================================================
140 Try to load the PNG DLL
143 qboolean PNG_OpenLibrary (void)
145 const char* dllnames [] =
153 #elif defined(MACOSX)
158 "libpng15.so.15", // WTF libtool guidelines anyone?
159 "libpng14.so.14", // WTF libtool guidelines anyone?
161 "libpng.so", // FreeBSD
171 if(!Sys_LoadLibrary (dllnames, &png_dll, pngfuncs))
173 if(qpng_access_version_number() / 100 >= 104)
174 if(!Sys_LoadLibrary (dllnames, &png14_dll, png14funcs))
176 Sys_UnloadLibrary (&png_dll);
190 void PNG_CloseLibrary (void)
192 Sys_UnloadLibrary (&png14_dll);
193 Sys_UnloadLibrary (&png_dll);
197 =================================================================
201 =================================================================
204 #define PNG_LIBPNG_VER_STRING_12 "1.2.4"
205 #define PNG_LIBPNG_VER_STRING_14 "1.4.0"
206 #define PNG_LIBPNG_VER_STRING_15 "1.5.0"
208 #define PNG_COLOR_MASK_PALETTE 1
209 #define PNG_COLOR_MASK_COLOR 2
210 #define PNG_COLOR_MASK_ALPHA 4
212 #define PNG_COLOR_TYPE_GRAY 0
213 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
214 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
215 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
216 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
218 #define PNG_COLOR_TYPE_RGBA PNG_COLOR_TYPE_RGB_ALPHA
219 #define PNG_COLOR_TYPE_GA PNG_COLOR_TYPE_GRAY_ALPHA
221 #define PNG_INFO_tRNS 0x0010
223 // this struct is only used for status information during loading
226 const unsigned char *tmpBuf;
231 unsigned int FRowBytes;
233 //double FScreenGamma;
234 unsigned char **FRowPtrs;
242 unsigned long Height; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
243 unsigned long Width; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
247 //double LastModified;
252 //LordHavoc: removed __cdecl prefix, added overrun protection, and rewrote this to be more efficient
253 static void PNG_fReadData(void *png, unsigned char *data, size_t length)
256 l = my_png.tmpBuflength - my_png.tmpi;
259 Con_Printf("PNG_fReadData: overrun by %i bytes\n", (int)(length - l));
260 // a read going past the end of the file, fill in the remaining bytes
261 // with 0 just to be consistent
262 memset(data + l, 0, length - l);
265 memcpy(data, my_png.tmpBuf + my_png.tmpi, length);
266 my_png.tmpi += (int)length;
267 //Com_HexDumpToConsole(data, (int)length);
270 static void PNG_fWriteData(void *png, unsigned char *data, size_t length)
272 FS_Write(my_png.outfile, data, length);
275 static void PNG_fFlushData(void *png)
279 static void PNG_error_fn(void *png, const char *message)
281 Con_Printf("PNG_LoadImage: error: %s\n", message);
284 static void PNG_warning_fn(void *png, const char *message)
286 Con_Printf("PNG_LoadImage: warning: %s\n", message);
289 extern int image_width;
290 extern int image_height;
292 unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *miplevel)
297 unsigned char *imagedata = NULL;
298 unsigned char ioBuffer[8192];
300 // FIXME: register an error handler so that abort() won't be called on error
306 if(qpng_sig_cmp(raw, 0, filesize))
308 png = (void *)qpng_create_read_struct(
309 (qpng_access_version_number() / 100 == 102) ? PNG_LIBPNG_VER_STRING_12 :
310 (qpng_access_version_number() / 100 == 104) ? PNG_LIBPNG_VER_STRING_14 :
311 PNG_LIBPNG_VER_STRING_15, // nasty hack... whatever
312 0, PNG_error_fn, PNG_warning_fn
317 // this must be memset before the setjmp error handler, because it relies
318 // on the fields in this struct for cleanup
319 memset(&my_png, 0, sizeof(my_png));
321 // NOTE: this relies on jmp_buf being the first thing in the png structure
322 // created by libpng! (this is correct for libpng 1.2.x)
323 if (setjmp(qpng_jmpbuf(png)))
326 Mem_Free(my_png.Data);
329 Mem_Free(my_png.FRowPtrs);
330 my_png.FRowPtrs = NULL;
331 qpng_destroy_read_struct(&png, &pnginfo, 0);
336 pnginfo = qpng_create_info_struct(png);
339 qpng_destroy_read_struct(&png, &pnginfo, 0);
342 qpng_set_sig_bytes(png, 0);
345 my_png.tmpBuflength = filesize;
347 //my_png.Data = NULL;
348 //my_png.FRowPtrs = NULL;
351 my_png.ColorType = PNG_COLOR_TYPE_RGB;
352 //my_png.Interlace = 0;
353 //my_png.Compression = 0;
355 qpng_set_read_fn(png, ioBuffer, PNG_fReadData);
356 qpng_read_info(png, pnginfo);
357 qpng_get_IHDR(png, pnginfo, &my_png.Width, &my_png.Height,&my_png.BitDepth, &my_png.ColorType, &my_png.Interlace, &my_png.Compression, &my_png.Filter);
359 // this check guards against pngconf.h with unsigned int *width/height parameters on big endian systems by detecting the strange values and shifting them down 32bits
360 // (if it's little endian the unwritten bytes are the most significant
361 // ones and we don't worry about that)
363 // this is only necessary because of retarded 64bit png_uint_32 types in libpng 1.2, which can (conceivably) vary by platform
364 #if LONG_MAX > 4000000000
365 if (my_png.Width > LONG_MAX || my_png.Height > LONG_MAX)
368 my_png.Height >>= 32;
372 if (my_png.ColorType == PNG_COLOR_TYPE_PALETTE)
373 qpng_set_palette_to_rgb(png);
374 if (my_png.ColorType == PNG_COLOR_TYPE_GRAY || my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
375 qpng_set_gray_to_rgb(png);
376 if (qpng_get_valid(png, pnginfo, PNG_INFO_tRNS))
377 qpng_set_tRNS_to_alpha(png);
378 if (my_png.BitDepth == 8 && !(my_png.ColorType & PNG_COLOR_MASK_ALPHA))
379 qpng_set_filler(png, 255, 1);
380 if (( my_png.ColorType == PNG_COLOR_TYPE_GRAY) || (my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA ))
381 qpng_set_gray_to_rgb(png);
382 if (my_png.BitDepth < 8)
383 qpng_set_expand(png);
385 qpng_read_update_info(png, pnginfo);
387 my_png.FRowBytes = qpng_get_rowbytes(png, pnginfo);
388 my_png.BytesPerPixel = qpng_get_channels(png, pnginfo);
390 my_png.FRowPtrs = (unsigned char **)Mem_Alloc(tempmempool, my_png.Height * sizeof(*my_png.FRowPtrs));
393 imagedata = (unsigned char *)Mem_Alloc(tempmempool, my_png.Height * my_png.FRowBytes);
396 my_png.Data = imagedata;
397 for(y = 0;y < my_png.Height;y++)
398 my_png.FRowPtrs[y] = my_png.Data + y * my_png.FRowBytes;
399 qpng_read_image(png, my_png.FRowPtrs);
403 Con_Printf("PNG_LoadImage : not enough memory\n");
404 qpng_destroy_read_struct(&png, &pnginfo, 0);
405 Mem_Free(my_png.FRowPtrs);
408 Mem_Free(my_png.FRowPtrs);
409 my_png.FRowPtrs = NULL;
413 Con_Printf("PNG_LoadImage : not enough memory\n");
414 qpng_destroy_read_struct(&png, &pnginfo, 0);
418 qpng_read_end(png, pnginfo);
419 qpng_destroy_read_struct(&png, &pnginfo, 0);
421 image_width = (int)my_png.Width;
422 image_height = (int)my_png.Height;
424 if (my_png.BitDepth != 8)
426 Con_Printf ("PNG_LoadImage : bad color depth\n");
431 // swizzle RGBA to BGRA
432 for (y = 0;y < (unsigned int)(image_width*image_height*4);y += 4)
435 imagedata[y+0] = imagedata[y+2];
443 =================================================================
447 =================================================================
450 #define Z_BEST_SPEED 1
451 #define Z_BEST_COMPRESSION 9
452 #define PNG_INTERLACE_NONE 0
453 #define PNG_INTERLACE_ADAM7 1
454 #define PNG_FILTER_TYPE_BASE 0
455 #define PNG_FILTER_TYPE_DEFAULT PNG_FILTER_TYPE_BASE
456 #define PNG_COMPRESSION_TYPE_BASE 0
457 #define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE
458 #define PNG_NO_FILTERS 0x00
459 #define PNG_FILTER_NONE 0x08
460 #define PNG_FILTER_SUB 0x10
461 #define PNG_FILTER_UP 0x20
462 #define PNG_FILTER_AVG 0x40
463 #define PNG_FILTER_PAETH 0x80
464 #define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
465 PNG_FILTER_AVG | PNG_FILTER_PAETH)
469 PNG_SaveImage_preflipped
471 Save a preflipped PNG image to a file
474 qboolean PNG_SaveImage_preflipped (const char *filename, int width, int height, qboolean has_alpha, unsigned char *data)
476 unsigned int offset, linesize;
477 qfile_t* file = NULL;
479 unsigned char ioBuffer[8192];
485 Con_Print("You need the libpng library to save PNG images\n");
489 png = (void *)qpng_create_write_struct(
490 (qpng_access_version_number() / 100 == 102) ? PNG_LIBPNG_VER_STRING_12 :
491 (qpng_access_version_number() / 100 == 104) ? PNG_LIBPNG_VER_STRING_14 :
492 PNG_LIBPNG_VER_STRING_15, // nasty hack... whatever
493 0, PNG_error_fn, PNG_warning_fn
497 pnginfo = (void *)qpng_create_info_struct(png);
500 qpng_destroy_write_struct(&png, NULL);
504 // this must be memset before the setjmp error handler, because it relies
505 // on the fields in this struct for cleanup
506 memset(&my_png, 0, sizeof(my_png));
508 // NOTE: this relies on jmp_buf being the first thing in the png structure
509 // created by libpng! (this is correct for libpng 1.2.x)
512 if (setjmp((_JBTYPE *)png))
513 #elif defined(MACOSX) || defined(WIN32)
514 if (setjmp((int *)png))
516 if (setjmp((__jmp_buf_tag *)png))
522 qpng_destroy_write_struct(&png, &pnginfo);
527 file = FS_OpenRealFile(filename, "wb", true);
530 my_png.outfile = file;
531 qpng_set_write_fn(png, ioBuffer, PNG_fWriteData, PNG_fFlushData);
533 //qpng_set_compression_level(png, Z_BEST_COMPRESSION);
534 qpng_set_compression_level(png, Z_BEST_SPEED);
535 qpng_set_IHDR(png, pnginfo, width, height, 8, has_alpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB, PNG_INTERLACE_ADAM7, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
536 qpng_set_filter(png, 0, PNG_NO_FILTERS);
537 qpng_write_info(png, pnginfo);
538 qpng_set_packing(png);
541 passes = qpng_set_interlace_handling(png);
543 linesize = width * (has_alpha ? 4 : 3);
544 offset = linesize * (height - 1);
545 for(i = 0; i < passes; ++i)
546 for(j = 0; j < height; ++j)
547 qpng_write_row(png, &data[offset - j * linesize]);
549 qpng_write_end(png, NULL);
550 qpng_destroy_write_struct(&png, &pnginfo);