DarkPlaces
Game engine based on the Quake 1 engine by id Software, developed by LadyHavoc
 
image_png.c File Reference
#include "darkplaces.h"
#include "image.h"
#include "image_png.h"
+ Include dependency graph for image_png.c:

Go to the source code of this file.

Macros

#define PNG_ALL_FILTERS
 
#define PNG_COLOR_MASK_ALPHA   4
 
#define PNG_COLOR_MASK_COLOR   2
 
#define PNG_COLOR_MASK_PALETTE   1
 
#define PNG_COLOR_TYPE_GA   PNG_COLOR_TYPE_GRAY_ALPHA
 
#define PNG_COLOR_TYPE_GRAY   0
 
#define PNG_COLOR_TYPE_GRAY_ALPHA   (PNG_COLOR_MASK_ALPHA)
 
#define PNG_COLOR_TYPE_PALETTE   (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
 
#define PNG_COLOR_TYPE_RGB   (PNG_COLOR_MASK_COLOR)
 
#define PNG_COLOR_TYPE_RGB_ALPHA   (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
 
#define PNG_COLOR_TYPE_RGBA   PNG_COLOR_TYPE_RGB_ALPHA
 
#define PNG_COMPRESSION_TYPE_BASE   0
 
#define PNG_COMPRESSION_TYPE_DEFAULT   PNG_COMPRESSION_TYPE_BASE
 
#define PNG_FILTER_AVG   0x40
 
#define PNG_FILTER_NONE   0x08
 
#define PNG_FILTER_PAETH   0x80
 
#define PNG_FILTER_SUB   0x10
 
#define PNG_FILTER_TYPE_BASE   0
 
#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE
 
#define PNG_FILTER_UP   0x20
 
#define PNG_INFO_tRNS   0x0010
 
#define PNG_INTERLACE_ADAM7   1
 
#define PNG_INTERLACE_NONE   0
 
#define PNG_LIBPNG_VER_STRING_12   "1.2.4"
 
#define PNG_LIBPNG_VER_STRING_14   "1.4.0"
 
#define PNG_LIBPNG_VER_STRING_15   "1.5.0"
 
#define PNG_LIBPNG_VER_STRING_16   "1.6.0"
 
#define PNG_NO_FILTERS   0x00
 
#define qpng_jmpbuf(png_ptr)
 
#define qpng_jmpbuf_12(png_ptr)
 
#define qpng_jmpbuf_14(png_ptr)
 
#define Z_BEST_COMPRESSION   9
 
#define Z_BEST_SPEED   1
 

Typedefs

typedef void(* qpng_longjmp_ptr) (jmp_buf, int)
 

Functions

void PNG_CloseLibrary (void)
 
static void PNG_error_fn (void *png, const char *message)
 
static void PNG_fFlushData (void *png)
 
static void PNG_fReadData (void *png, unsigned char *data, size_t length)
 
static void PNG_fWriteData (void *png, unsigned char *data, size_t length)
 
unsigned char * PNG_LoadImage_BGRA (const unsigned char *raw, int filesize, int *miplevel)
 
qbool PNG_OpenLibrary (void)
 
qbool PNG_SaveImage_preflipped (const char *filename, int width, int height, qbool has_alpha, unsigned char *data)
 
static void PNG_warning_fn (void *png, const char *message)
 

Variables

struct { 
 
   int   BitDepth 
 
   int   BytesPerPixel 
 
   int   ColorType 
 
   int   Compression 
 
   unsigned char *   Data 
 
   int   Filter 
 
   unsigned int   FRowBytes 
 
   unsigned char **   FRowPtrs 
 
   unsigned long   Height 
 
   int   Interlace 
 
   qfile_t *   outfile 
 
   const unsigned char *   tmpBuf 
 
   int   tmpBuflength 
 
   int   tmpi 
 
   unsigned long   Width 
 
my_png 
 
dllhandle_t png14_dll = NULL
 
static dllfunction_t png14funcs []
 
dllhandle_t png_dll = NULL
 
static dllfunction_t pngfuncs []
 
static unsigned int(* qpng_access_version_number )(void)
 
static void *(* qpng_create_info_struct )(void *)
 
static void *(* qpng_create_read_struct )(const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message))
 
static void *(* qpng_create_write_struct )(const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message))
 
static void(* qpng_destroy_read_struct )(void **, void **, void **)
 
static void(* qpng_destroy_write_struct )(void **, void **)
 
static unsigned char(* qpng_get_bit_depth )(void *, void *)
 
static unsigned char(* qpng_get_channels )(void *, void *)
 
static unsigned int(* qpng_get_IHDR )(void *, void *, unsigned long *, unsigned long *, int *, int *, int *, int *, int *)
 
static unsigned int(* qpng_get_rowbytes )(void *, void *)
 
static unsigned int(* qpng_get_valid )(void *, void *, unsigned int)
 
static void(* qpng_read_end )(void *, void *)
 
static void(* qpng_read_image )(void *, unsigned char **)
 
static void(* qpng_read_info )(void *, void *)
 
static void(* qpng_read_update_info )(void *, void *)
 
static void(* qpng_set_bgr )(void *)
 
static void(* qpng_set_compression_level )(void *, int)
 
static void(* qpng_set_expand )(void *)
 
static void(* qpng_set_filler )(void *, unsigned int, int)
 
static void(* qpng_set_filter )(void *, int, int)
 
static void(* qpng_set_gray_to_rgb )(void *)
 
static void(* qpng_set_IHDR )(void *, void *, unsigned long, unsigned long, int, int, int, int, int)
 
static int(* qpng_set_interlace_handling )(void *)
 
static jmp_buf *(* qpng_set_longjmp_fn )(void *, qpng_longjmp_ptr, size_t)
 
static void(* qpng_set_packing )(void *)
 
static void(* qpng_set_palette_to_rgb )(void *)
 
static void(* qpng_set_read_fn )(void *, void *, void(*)(void *png, unsigned char *data, size_t length))
 
static void(* qpng_set_sig_bytes )(void *, int)
 
static void(* qpng_set_tRNS_to_alpha )(void *)
 
static void(* qpng_set_write_fn )(void *, void *, void(*)(void *png, unsigned char *data, size_t length), void(*)(void *png))
 
static int(* qpng_sig_cmp )(const unsigned char *, size_t, size_t)
 
static void(* qpng_write_end )(void *, void *)
 
static void(* qpng_write_info )(void *, void *)
 
static void(* qpng_write_row )(void *, unsigned char *)
 

Macro Definition Documentation

◆ PNG_ALL_FILTERS

#define PNG_ALL_FILTERS
Value:
#define PNG_FILTER_SUB
Definition image_png.c:463
#define PNG_FILTER_PAETH
Definition image_png.c:466
#define PNG_FILTER_NONE
Definition image_png.c:462
#define PNG_FILTER_AVG
Definition image_png.c:465
#define PNG_FILTER_UP
Definition image_png.c:464

Definition at line 467 of file image_png.c.

467#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \
468 PNG_FILTER_AVG | PNG_FILTER_PAETH)

◆ PNG_COLOR_MASK_ALPHA

#define PNG_COLOR_MASK_ALPHA   4

Definition at line 215 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_COLOR_MASK_COLOR

#define PNG_COLOR_MASK_COLOR   2

Definition at line 214 of file image_png.c.

◆ PNG_COLOR_MASK_PALETTE

#define PNG_COLOR_MASK_PALETTE   1

Definition at line 213 of file image_png.c.

◆ PNG_COLOR_TYPE_GA

#define PNG_COLOR_TYPE_GA   PNG_COLOR_TYPE_GRAY_ALPHA

Definition at line 224 of file image_png.c.

◆ PNG_COLOR_TYPE_GRAY

#define PNG_COLOR_TYPE_GRAY   0

Definition at line 217 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_COLOR_TYPE_GRAY_ALPHA

#define PNG_COLOR_TYPE_GRAY_ALPHA   (PNG_COLOR_MASK_ALPHA)

Definition at line 221 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_COLOR_TYPE_PALETTE

#define PNG_COLOR_TYPE_PALETTE   (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)

Definition at line 218 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_COLOR_TYPE_RGB

#define PNG_COLOR_TYPE_RGB   (PNG_COLOR_MASK_COLOR)

Definition at line 219 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_COLOR_TYPE_RGB_ALPHA

#define PNG_COLOR_TYPE_RGB_ALPHA   (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)

Definition at line 220 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ PNG_COLOR_TYPE_RGBA

#define PNG_COLOR_TYPE_RGBA   PNG_COLOR_TYPE_RGB_ALPHA

Definition at line 223 of file image_png.c.

◆ PNG_COMPRESSION_TYPE_BASE

#define PNG_COMPRESSION_TYPE_BASE   0

Definition at line 459 of file image_png.c.

◆ PNG_COMPRESSION_TYPE_DEFAULT

#define PNG_COMPRESSION_TYPE_DEFAULT   PNG_COMPRESSION_TYPE_BASE

Definition at line 460 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ PNG_FILTER_AVG

#define PNG_FILTER_AVG   0x40

Definition at line 465 of file image_png.c.

◆ PNG_FILTER_NONE

#define PNG_FILTER_NONE   0x08

Definition at line 462 of file image_png.c.

◆ PNG_FILTER_PAETH

#define PNG_FILTER_PAETH   0x80

Definition at line 466 of file image_png.c.

◆ PNG_FILTER_SUB

#define PNG_FILTER_SUB   0x10

Definition at line 463 of file image_png.c.

◆ PNG_FILTER_TYPE_BASE

#define PNG_FILTER_TYPE_BASE   0

Definition at line 457 of file image_png.c.

◆ PNG_FILTER_TYPE_DEFAULT

#define PNG_FILTER_TYPE_DEFAULT   PNG_FILTER_TYPE_BASE

Definition at line 458 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ PNG_FILTER_UP

#define PNG_FILTER_UP   0x20

Definition at line 464 of file image_png.c.

◆ PNG_INFO_tRNS

#define PNG_INFO_tRNS   0x0010

Definition at line 226 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_INTERLACE_ADAM7

#define PNG_INTERLACE_ADAM7   1

Definition at line 456 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ PNG_INTERLACE_NONE

#define PNG_INTERLACE_NONE   0

Definition at line 455 of file image_png.c.

◆ PNG_LIBPNG_VER_STRING_12

#define PNG_LIBPNG_VER_STRING_12   "1.2.4"

Definition at line 208 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_LIBPNG_VER_STRING_14

#define PNG_LIBPNG_VER_STRING_14   "1.4.0"

Definition at line 209 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_LIBPNG_VER_STRING_15

#define PNG_LIBPNG_VER_STRING_15   "1.5.0"

Definition at line 210 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_LIBPNG_VER_STRING_16

#define PNG_LIBPNG_VER_STRING_16   "1.6.0"

Definition at line 211 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_NO_FILTERS

#define PNG_NO_FILTERS   0x00

Definition at line 461 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_jmpbuf

#define qpng_jmpbuf ( png_ptr)
Value:
static jmp_buf *(* qpng_set_longjmp_fn)(void *, qpng_longjmp_ptr, size_t)
Definition image_png.c:70
#define qpng_jmpbuf_14(png_ptr)
Definition image_png.c:71
#define qpng_jmpbuf_12(png_ptr)
Definition image_png.c:74

Definition at line 77 of file image_png.c.

77#define qpng_jmpbuf(png_ptr) \
78 (qpng_set_longjmp_fn ? qpng_jmpbuf_14(png_ptr) : qpng_jmpbuf_12(png_ptr))

Referenced by PNG_LoadImage_BGRA().

◆ qpng_jmpbuf_12

#define qpng_jmpbuf_12 ( png_ptr)
Value:
(*((jmp_buf *) png_ptr))

Definition at line 74 of file image_png.c.

◆ qpng_jmpbuf_14

#define qpng_jmpbuf_14 ( png_ptr)
Value:
(*qpng_set_longjmp_fn((png_ptr), longjmp, sizeof (jmp_buf)))

Definition at line 71 of file image_png.c.

◆ Z_BEST_COMPRESSION

#define Z_BEST_COMPRESSION   9

Definition at line 454 of file image_png.c.

◆ Z_BEST_SPEED

#define Z_BEST_SPEED   1

Definition at line 453 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

Typedef Documentation

◆ qpng_longjmp_ptr

typedef void(* qpng_longjmp_ptr) (jmp_buf, int)

Definition at line 69 of file image_png.c.

Function Documentation

◆ PNG_CloseLibrary()

void PNG_CloseLibrary ( void )

Definition at line 194 of file image_png.c.

195{
198}
dllhandle_t png_dll
Definition image_png.c:124
dllhandle_t png14_dll
Definition image_png.c:125
void Sys_FreeLibrary(dllhandle_t *handle)
Definition sys_shared.c:245

References png14_dll, png_dll, and Sys_FreeLibrary().

◆ PNG_error_fn()

static void PNG_error_fn ( void * png,
const char * message )
static

Definition at line 284 of file image_png.c.

285{
286 Con_Printf(CON_ERROR "PNG_LoadImage: error: %s\n", message);
287}
void Con_Printf(const char *fmt,...)
Prints to all appropriate console targets.
Definition console.c:1514
#define CON_ERROR
Definition console.h:102
string message
Definition progsdefs.qc:205

References CON_ERROR, Con_Printf(), and message.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ PNG_fFlushData()

static void PNG_fFlushData ( void * png)
static

Definition at line 280 of file image_png.c.

281{
282}

Referenced by PNG_SaveImage_preflipped().

◆ PNG_fReadData()

static void PNG_fReadData ( void * png,
unsigned char * data,
size_t length )
static

Definition at line 258 of file image_png.c.

259{
260 size_t l;
261 l = my_png.tmpBuflength - my_png.tmpi;
262 if (l < length)
263 {
264 Con_Printf("PNG_fReadData: overrun by %i bytes\n", (int)(length - l));
265 // a read going past the end of the file, fill in the remaining bytes
266 // with 0 just to be consistent
267 memset(data + l, 0, length - l);
268 length = l;
269 }
270 memcpy(data, my_png.tmpBuf + my_png.tmpi, length);
271 my_png.tmpi += (int)length;
272 //Com_HexDumpToConsole(data, (int)length);
273}
static int(ZEXPORT *qz_inflate)(z_stream *strm
GLenum GLuint GLenum GLsizei length
Definition glquake.h:657
GLsizeiptr const GLvoid * data
Definition glquake.h:639
static struct @13 my_png

References Con_Printf(), data, int(), length, and my_png.

Referenced by PNG_LoadImage_BGRA().

◆ PNG_fWriteData()

static void PNG_fWriteData ( void * png,
unsigned char * data,
size_t length )
static

Definition at line 275 of file image_png.c.

276{
277 FS_Write(my_png.outfile, data, length);
278}
fs_offset_t FS_Write(qfile_t *file, const void *data, size_t datasize)
Definition fs.c:3019

References data, FS_Write(), length, and my_png.

Referenced by PNG_SaveImage_preflipped().

◆ PNG_LoadImage_BGRA()

unsigned char * PNG_LoadImage_BGRA ( const unsigned char * raw,
int filesize,
int * miplevel )

Definition at line 294 of file image_png.c.

295{
296 unsigned int c;
297 unsigned int y;
298 void *png, *pnginfo;
299 unsigned char *imagedata = NULL;
300 unsigned char ioBuffer[8192];
301
302 // FIXME: register an error handler so that abort() won't be called on error
303
304 // No DLL = no PNGs
305 if (!png_dll)
306 return NULL;
307
308 if(qpng_sig_cmp(raw, 0, filesize))
309 return NULL;
310 png = (void *)qpng_create_read_struct(
314 PNG_LIBPNG_VER_STRING_16, // nasty hack... whatever
316 );
317 if(!png)
318 return NULL;
319
320 // this must be memset before the setjmp error handler, because it relies
321 // on the fields in this struct for cleanup
322 memset(&my_png, 0, sizeof(my_png));
323
324 // NOTE: this relies on jmp_buf being the first thing in the png structure
325 // created by libpng! (this is correct for libpng 1.2.x)
326 if (setjmp(qpng_jmpbuf(png)))
327 {
328 if (my_png.Data)
329 Mem_Free(my_png.Data);
330 my_png.Data = NULL;
331 if (my_png.FRowPtrs)
332 Mem_Free(my_png.FRowPtrs);
333 my_png.FRowPtrs = NULL;
334 qpng_destroy_read_struct(&png, &pnginfo, 0);
335 return NULL;
336 }
337 //
338
339 pnginfo = qpng_create_info_struct(png);
340 if(!pnginfo)
341 {
342 qpng_destroy_read_struct(&png, &pnginfo, 0);
343 return NULL;
344 }
345 qpng_set_sig_bytes(png, 0);
346
347 my_png.tmpBuf = raw;
348 my_png.tmpBuflength = filesize;
349 my_png.tmpi = 0;
350 //my_png.Data = NULL;
351 //my_png.FRowPtrs = NULL;
352 //my_png.Height = 0;
353 //my_png.Width = 0;
354 my_png.ColorType = PNG_COLOR_TYPE_RGB;
355 //my_png.Interlace = 0;
356 //my_png.Compression = 0;
357 //my_png.Filter = 0;
358 qpng_set_read_fn(png, ioBuffer, PNG_fReadData);
359 qpng_read_info(png, pnginfo);
360 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);
361
362 // 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
363 // (if it's little endian the unwritten bytes are the most significant
364 // ones and we don't worry about that)
365 //
366 // this is only necessary because of retarded 64bit png_uint_32 types in libpng 1.2, which can (conceivably) vary by platform
367#if LONG_MAX > 4000000000
368 if (my_png.Width > LONG_MAX || my_png.Height > LONG_MAX)
369 {
370 my_png.Width >>= 32;
371 my_png.Height >>= 32;
372 }
373#endif
374
375 if (my_png.ColorType == PNG_COLOR_TYPE_PALETTE)
377 if (my_png.ColorType == PNG_COLOR_TYPE_GRAY || my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
379 if (qpng_get_valid(png, pnginfo, PNG_INFO_tRNS))
381 if (my_png.BitDepth == 8 && !(my_png.ColorType & PNG_COLOR_MASK_ALPHA))
382 qpng_set_filler(png, 255, 1);
383 if (( my_png.ColorType == PNG_COLOR_TYPE_GRAY) || (my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA ))
385 if (my_png.BitDepth < 8)
386 qpng_set_expand(png);
387
388 qpng_read_update_info(png, pnginfo);
389
390 my_png.FRowBytes = qpng_get_rowbytes(png, pnginfo);
391 my_png.BytesPerPixel = qpng_get_channels(png, pnginfo);
392
393 my_png.FRowPtrs = (unsigned char **)Mem_Alloc(tempmempool, my_png.Height * sizeof(*my_png.FRowPtrs));
394 if (my_png.FRowPtrs)
395 {
396 imagedata = (unsigned char *)Mem_Alloc(tempmempool, my_png.Height * my_png.FRowBytes);
397 if(imagedata)
398 {
399 my_png.Data = imagedata;
400 for(y = 0;y < my_png.Height;y++)
401 my_png.FRowPtrs[y] = my_png.Data + y * my_png.FRowBytes;
402 qpng_read_image(png, my_png.FRowPtrs);
403 }
404 else
405 {
406 Con_Printf("PNG_LoadImage : not enough memory\n");
407 qpng_destroy_read_struct(&png, &pnginfo, 0);
408 Mem_Free(my_png.FRowPtrs);
409 return NULL;
410 }
411 Mem_Free(my_png.FRowPtrs);
412 my_png.FRowPtrs = NULL;
413 }
414 else
415 {
416 Con_Printf("PNG_LoadImage : not enough memory\n");
417 qpng_destroy_read_struct(&png, &pnginfo, 0);
418 return NULL;
419 }
420
421 qpng_read_end(png, pnginfo);
422 qpng_destroy_read_struct(&png, &pnginfo, 0);
423
424 image_width = (int)my_png.Width;
425 image_height = (int)my_png.Height;
426
427 if (my_png.BitDepth != 8)
428 {
429 Con_Printf ("PNG_LoadImage : bad color depth\n");
430 Mem_Free(imagedata);
431 return NULL;
432 }
433
434 // swizzle RGBA to BGRA
435 for (y = 0;y < (unsigned int)(image_width*image_height*4);y += 4)
436 {
437 c = imagedata[y+0];
438 imagedata[y+0] = imagedata[y+2];
439 imagedata[y+2] = c;
440 }
441
442 return imagedata;
443}
GLint GLenum GLint GLint y
Definition glquake.h:651
int image_height
Definition image.c:10
int image_width
Definition image.c:9
static unsigned int(* qpng_get_IHDR)(void *, void *, unsigned long *, unsigned long *, int *, int *, int *, int *, int *)
Definition image_png.c:62
#define qpng_jmpbuf(png_ptr)
Definition image_png.c:77
static int(* qpng_sig_cmp)(const unsigned char *, size_t, size_t)
Definition image_png.c:35
static void(* qpng_set_gray_to_rgb)(void *)
Definition image_png.c:45
static void(* qpng_set_palette_to_rgb)(void *)
Definition image_png.c:43
static void(* qpng_read_info)(void *, void *)
Definition image_png.c:39
static unsigned int(* qpng_get_valid)(void *, void *, unsigned int)
Definition image_png.c:58
#define PNG_COLOR_TYPE_RGB
Definition image_png.c:219
#define PNG_LIBPNG_VER_STRING_15
Definition image_png.c:210
static void(* qpng_set_read_fn)(void *, void *, void(*)(void *png, unsigned char *data, size_t length))
Definition image_png.c:56
#define PNG_LIBPNG_VER_STRING_16
Definition image_png.c:211
#define PNG_LIBPNG_VER_STRING_12
Definition image_png.c:208
static unsigned int(* qpng_access_version_number)(void)
Definition image_png.c:63
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition image_png.c:221
static void(* qpng_set_sig_bytes)(void *, int)
Definition image_png.c:34
static void(* qpng_destroy_read_struct)(void **, void **, void **)
Definition image_png.c:54
static unsigned int(* qpng_get_rowbytes)(void *, void *)
Definition image_png.c:59
static void(* qpng_read_end)(void *, void *)
Definition image_png.c:53
static void PNG_error_fn(void *png, const char *message)
Definition image_png.c:284
static void PNG_warning_fn(void *png, const char *message)
Definition image_png.c:289
static void(* qpng_set_filler)(void *, unsigned int, int)
Definition image_png.c:46
#define PNG_COLOR_MASK_ALPHA
Definition image_png.c:215
#define PNG_INFO_tRNS
Definition image_png.c:226
static void(* qpng_set_expand)(void *)
Definition image_png.c:42
#define PNG_COLOR_TYPE_GRAY
Definition image_png.c:217
static void(* qpng_set_tRNS_to_alpha)(void *)
Definition image_png.c:44
static void *(* qpng_create_info_struct)(void *)
Definition image_png.c:38
static void(* qpng_read_image)(void *, unsigned char **)
Definition image_png.c:52
static unsigned char(* qpng_get_channels)(void *, void *)
Definition image_png.c:60
static void PNG_fReadData(void *png, unsigned char *data, size_t length)
Definition image_png.c:258
#define PNG_COLOR_TYPE_PALETTE
Definition image_png.c:218
static void(* qpng_read_update_info)(void *, void *)
Definition image_png.c:51
#define PNG_LIBPNG_VER_STRING_14
Definition image_png.c:209
static void *(* qpng_create_read_struct)(const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message))
Definition image_png.c:36
#define NULL
Definition qtypes.h:12
mempool_t * tempmempool
Definition zone.c:794
#define Mem_Free(mem)
Definition zone.h:96
#define Mem_Alloc(pool, size)
Definition zone.h:92

References Con_Printf(), image_height, image_width, int(), Mem_Alloc, Mem_Free, my_png, NULL, PNG_COLOR_MASK_ALPHA, PNG_COLOR_TYPE_GRAY, PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, png_dll, PNG_error_fn(), PNG_fReadData(), PNG_INFO_tRNS, PNG_LIBPNG_VER_STRING_12, PNG_LIBPNG_VER_STRING_14, PNG_LIBPNG_VER_STRING_15, PNG_LIBPNG_VER_STRING_16, PNG_warning_fn(), qpng_access_version_number, qpng_create_info_struct, qpng_create_read_struct, qpng_destroy_read_struct, qpng_get_channels, qpng_get_IHDR, qpng_get_rowbytes, qpng_get_valid, qpng_jmpbuf, qpng_read_end, qpng_read_image, qpng_read_info, qpng_read_update_info, qpng_set_expand, qpng_set_filler, qpng_set_gray_to_rgb, qpng_set_palette_to_rgb, qpng_set_read_fn, qpng_set_sig_bytes, qpng_set_tRNS_to_alpha, qpng_sig_cmp, tempmempool, and y.

Referenced by decode_image().

◆ PNG_OpenLibrary()

qbool PNG_OpenLibrary ( void )

Definition at line 143 of file image_png.c.

144{
145 const char* dllnames [] =
146 {
147#if WIN32
148 "libpng16.dll",
149 "libpng16-16.dll",
150 "libpng15-15.dll",
151 "libpng15.dll",
152 "libpng14-14.dll",
153 "libpng14.dll",
154 "libpng12.dll",
155#elif defined(MACOSX)
156 "libpng16.16.dylib",
157 "libpng15.15.dylib",
158 "libpng14.14.dylib",
159 "libpng12.0.dylib",
160#else
161 "libpng16.so.16",
162 "libpng15.so.15", // WTF libtool guidelines anyone?
163 "libpng14.so.14", // WTF libtool guidelines anyone?
164 "libpng12.so.0",
165 "libpng.so", // FreeBSD
166#endif
167 NULL
168 };
169
170 // Already loaded?
171 if (png_dll)
172 return true;
173
174 // Load the DLL
175 if(!Sys_LoadDependency (dllnames, &png_dll, pngfuncs))
176 return false;
177 if(qpng_access_version_number() / 100 >= 104)
178 if(!Sys_LoadDependency (dllnames, &png14_dll, png14funcs))
179 {
181 return false;
182 }
183 return true;
184}
static dllfunction_t pngfuncs[]
Definition image_png.c:80
static dllfunction_t png14funcs[]
Definition image_png.c:117
qbool Sys_LoadDependency(const char **dllnames, dllhandle_t *handle, const dllfunction_t *fcts)
Definition sys_shared.c:131

References NULL, png14_dll, png14funcs, png_dll, pngfuncs, qpng_access_version_number, Sys_FreeLibrary(), and Sys_LoadDependency().

Referenced by Image_Compress(), and r_textures_start().

◆ PNG_SaveImage_preflipped()

qbool PNG_SaveImage_preflipped ( const char * filename,
int width,
int height,
qbool has_alpha,
unsigned char * data )

Definition at line 477 of file image_png.c.

478{
479 unsigned int offset, linesize;
480 qfile_t* file = NULL;
481 void *png, *pnginfo;
482 unsigned char ioBuffer[8192];
483 int passes, i, j;
484
485 // No DLL = no JPEGs
486 if (!png_dll)
487 {
488 Con_Print("You need the libpng library to save PNG images\n");
489 return false;
490 }
491
492 png = (void *)qpng_create_write_struct(
496 PNG_LIBPNG_VER_STRING_16, // nasty hack... whatever
498 );
499 if(!png)
500 return false;
501 pnginfo = (void *)qpng_create_info_struct(png);
502 if(!pnginfo)
503 {
505 return false;
506 }
507
508 // this must be memset before the setjmp error handler, because it relies
509 // on the fields in this struct for cleanup
510 memset(&my_png, 0, sizeof(my_png));
511
512 // NOTE: this relies on jmp_buf being the first thing in the png structure
513 // created by libpng! (this is correct for libpng 1.2.x)
514#ifdef WIN64
515 if (setjmp((_JBTYPE *)png))
516#elif defined(MACOSX) || defined(WIN32)
517 if (setjmp((int *)png))
518#elif defined(__ANDROID__)
519 if (setjmp((long *)png))
520#else
521 if (setjmp((struct __jmp_buf_tag *)png))
522#endif
523 {
524 qpng_destroy_write_struct(&png, &pnginfo);
525 return false;
526 }
527
528 // Open the file
529 file = FS_OpenRealFile(filename, "wb", true);
530 if (!file)
531 return false;
532 my_png.outfile = file;
534
535 //qpng_set_compression_level(png, Z_BEST_COMPRESSION);
539 qpng_write_info(png, pnginfo);
540 qpng_set_packing(png);
541 qpng_set_bgr(png);
542
543 passes = qpng_set_interlace_handling(png);
544
545 linesize = width * (has_alpha ? 4 : 3);
546 offset = linesize * (height - 1);
547 for(i = 0; i < passes; ++i)
548 for(j = 0; j < height; ++j)
549 qpng_write_row(png, &data[offset - j * linesize]);
550
551 qpng_write_end(png, NULL);
552 qpng_destroy_write_struct(&png, &pnginfo);
553
554 FS_Close (file);
555
556 return true;
557}
void Con_Print(const char *msg)
Prints to all appropriate console targets, and adds timestamps.
Definition console.c:1504
qfile_t * FS_OpenRealFile(const char *filepath, const char *mode, qbool quiet)
Definition fs.c:2901
int FS_Close(qfile_t *file)
Definition fs.c:2970
GLenum GLsizei width
Definition glquake.h:622
GLenum GLsizei GLsizei height
Definition glquake.h:622
GLuint GLuint GLintptr offset
Definition glquake.h:632
static void PNG_fWriteData(void *png, unsigned char *data, size_t length)
Definition image_png.c:275
static void *(* qpng_create_write_struct)(const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message))
Definition image_png.c:37
#define PNG_COMPRESSION_TYPE_DEFAULT
Definition image_png.c:460
static void(* qpng_set_write_fn)(void *, void *, void(*)(void *png, unsigned char *data, size_t length), void(*)(void *png))
Definition image_png.c:57
static void(* qpng_write_info)(void *, void *)
Definition image_png.c:64
static void(* qpng_set_compression_level)(void *, int)
Definition image_png.c:40
static void(* qpng_write_end)(void *, void *)
Definition image_png.c:66
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition image_png.c:220
static void(* qpng_write_row)(void *, unsigned char *)
Definition image_png.c:65
#define PNG_INTERLACE_ADAM7
Definition image_png.c:456
static void(* qpng_set_packing)(void *)
Definition image_png.c:48
static int(* qpng_set_interlace_handling)(void *)
Definition image_png.c:50
static void(* qpng_destroy_write_struct)(void **, void **)
Definition image_png.c:55
#define Z_BEST_SPEED
Definition image_png.c:453
static void(* qpng_set_bgr)(void *)
Definition image_png.c:49
static void PNG_fFlushData(void *png)
Definition image_png.c:280
#define PNG_FILTER_TYPE_DEFAULT
Definition image_png.c:458
#define PNG_NO_FILTERS
Definition image_png.c:461
static void(* qpng_set_filter)(void *, int, int)
Definition image_png.c:41
static void(* qpng_set_IHDR)(void *, void *, unsigned long, unsigned long, int, int, int, int, int)
Definition image_png.c:47
int i

References Con_Print(), data, FS_Close(), FS_OpenRealFile(), height, i, my_png, NULL, offset, PNG_COLOR_TYPE_RGB, PNG_COLOR_TYPE_RGB_ALPHA, PNG_COMPRESSION_TYPE_DEFAULT, png_dll, PNG_error_fn(), PNG_fFlushData(), PNG_FILTER_TYPE_DEFAULT, PNG_fWriteData(), PNG_INTERLACE_ADAM7, PNG_LIBPNG_VER_STRING_12, PNG_LIBPNG_VER_STRING_14, PNG_LIBPNG_VER_STRING_15, PNG_LIBPNG_VER_STRING_16, PNG_NO_FILTERS, PNG_warning_fn(), qpng_access_version_number, qpng_create_info_struct, qpng_create_write_struct, qpng_destroy_write_struct, qpng_set_bgr, qpng_set_compression_level, qpng_set_filter, qpng_set_IHDR, qpng_set_interlace_handling, qpng_set_packing, qpng_set_write_fn, qpng_write_end, qpng_write_info, qpng_write_row, width, and Z_BEST_SPEED.

Referenced by SCR_ScreenShot().

◆ PNG_warning_fn()

static void PNG_warning_fn ( void * png,
const char * message )
static

Definition at line 289 of file image_png.c.

290{
291 Con_Printf(CON_WARN "PNG_LoadImage: warning: %s\n", message);
292}
#define CON_WARN
Definition console.h:101

References Con_Printf(), CON_WARN, and message.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

Variable Documentation

◆ BitDepth

int BitDepth

Definition at line 244 of file image_png.c.

◆ BytesPerPixel

int BytesPerPixel

Definition at line 245 of file image_png.c.

◆ ColorType

int ColorType

Definition at line 246 of file image_png.c.

◆ Compression

int Compression

Definition at line 250 of file image_png.c.

◆ Data

unsigned char* Data

Definition at line 240 of file image_png.c.

◆ Filter

int Filter

Definition at line 251 of file image_png.c.

◆ FRowBytes

unsigned int FRowBytes

Definition at line 236 of file image_png.c.

◆ FRowPtrs

unsigned char** FRowPtrs

Definition at line 239 of file image_png.c.

◆ Height

unsigned long Height

Definition at line 247 of file image_png.c.

◆ Interlace

int Interlace

Definition at line 249 of file image_png.c.

◆ [struct]

◆ outfile

qfile_t* outfile

Definition at line 254 of file image_png.c.

Referenced by CL_Locs_Save_f(), JPEG_FileDest(), and main().

◆ png14_dll

dllhandle_t png14_dll = NULL

Definition at line 125 of file image_png.c.

Referenced by PNG_CloseLibrary(), and PNG_OpenLibrary().

◆ png14funcs

dllfunction_t png14funcs[]
static
Initial value:
=
{
{"png_set_longjmp_fn", (void **) &qpng_set_longjmp_fn},
}

Definition at line 117 of file image_png.c.

118{
119 {"png_set_longjmp_fn", (void **) &qpng_set_longjmp_fn},
120 {NULL, NULL}
121};

Referenced by PNG_OpenLibrary().

◆ png_dll

◆ pngfuncs

dllfunction_t pngfuncs[]
static

Definition at line 80 of file image_png.c.

81{
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},
115 {NULL, NULL}
116};
static unsigned char(* qpng_get_bit_depth)(void *, void *)
Definition image_png.c:61

Referenced by PNG_OpenLibrary().

◆ qpng_access_version_number

unsigned int(* qpng_access_version_number) (void) ( void )
static

Definition at line 63 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), PNG_OpenLibrary(), and PNG_SaveImage_preflipped().

◆ qpng_create_info_struct

void *(* qpng_create_info_struct) (void *) ( void * )
static

Definition at line 38 of file image_png.c.

Referenced by PNG_LoadImage_BGRA(), and PNG_SaveImage_preflipped().

◆ qpng_create_read_struct

void *(* qpng_create_read_struct) (const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message)) ( const char * ,
void * ,
void(*  )(void *png, const char *message),
void(*  )(void *png, const char *message) )
static

Definition at line 36 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_create_write_struct

void *(* qpng_create_write_struct) (const char *, void *, void(*)(void *png, const char *message), void(*)(void *png, const char *message)) ( const char * ,
void * ,
void(*  )(void *png, const char *message),
void(*  )(void *png, const char *message) )
static

Definition at line 37 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_destroy_read_struct

void(* qpng_destroy_read_struct) (void **, void **, void **) ( void ** ,
void ** ,
void **  )
static

Definition at line 54 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_destroy_write_struct

void(* qpng_destroy_write_struct) (void **, void **) ( void ** ,
void **  )
static

Definition at line 55 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_get_bit_depth

unsigned char(* qpng_get_bit_depth) (void *, void *) ( void * ,
void *  )
static

Definition at line 61 of file image_png.c.

◆ qpng_get_channels

unsigned char(* qpng_get_channels) (void *, void *) ( void * ,
void *  )
static

Definition at line 60 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_get_IHDR

unsigned int(* qpng_get_IHDR) (void *, void *, unsigned long *, unsigned long *, int *, int *, int *, int *, int *) ( void * ,
void * ,
unsigned long * ,
unsigned long * ,
int * ,
int * ,
int * ,
int * ,
int *  )
static

Definition at line 62 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_get_rowbytes

unsigned int(* qpng_get_rowbytes) (void *, void *) ( void * ,
void *  )
static

Definition at line 59 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_get_valid

unsigned int(* qpng_get_valid) (void *, void *, unsigned int) ( void * ,
void * ,
unsigned int  )
static

Definition at line 58 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_read_end

void(* qpng_read_end) (void *, void *) ( void * ,
void *  )
static

Definition at line 53 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_read_image

void(* qpng_read_image) (void *, unsigned char **) ( void * ,
unsigned char **  )
static

Definition at line 52 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_read_info

void(* qpng_read_info) (void *, void *) ( void * ,
void *  )
static

Definition at line 39 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_read_update_info

void(* qpng_read_update_info) (void *, void *) ( void * ,
void *  )
static

Definition at line 51 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_bgr

void(* qpng_set_bgr) (void *) ( void * )
static

Definition at line 49 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_compression_level

void(* qpng_set_compression_level) (void *, int) ( void * ,
int  )
static

Definition at line 40 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_expand

void(* qpng_set_expand) (void *) ( void * )
static

Definition at line 42 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_filler

void(* qpng_set_filler) (void *, unsigned int, int) ( void * ,
unsigned int ,
int  )
static

Definition at line 46 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_filter

void(* qpng_set_filter) (void *, int, int) ( void * ,
int ,
int  )
static

Definition at line 41 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_gray_to_rgb

void(* qpng_set_gray_to_rgb) (void *) ( void * )
static

Definition at line 45 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_IHDR

void(* qpng_set_IHDR) (void *, void *, unsigned long, unsigned long, int, int, int, int, int) ( void * ,
void * ,
unsigned long ,
unsigned long ,
int ,
int ,
int ,
int ,
int  )
static

Definition at line 47 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_interlace_handling

int(* qpng_set_interlace_handling) (void *) ( void * )
static

Definition at line 50 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_longjmp_fn

jmp_buf *(* qpng_set_longjmp_fn) (void *, qpng_longjmp_ptr, size_t) ( void * ,
qpng_longjmp_ptr ,
size_t  )
static

Definition at line 70 of file image_png.c.

◆ qpng_set_packing

void(* qpng_set_packing) (void *) ( void * )
static

Definition at line 48 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_set_palette_to_rgb

void(* qpng_set_palette_to_rgb) (void *) ( void * )
static

Definition at line 43 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_read_fn

void(* qpng_set_read_fn) (void *, void *, void(*)(void *png, unsigned char *data, size_t length)) ( void * ,
void * ,
void(*  )(void *png, unsigned char *data, size_t length) )
static

Definition at line 56 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_sig_bytes

void(* qpng_set_sig_bytes) (void *, int) ( void * ,
int  )
static

Definition at line 34 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_tRNS_to_alpha

void(* qpng_set_tRNS_to_alpha) (void *) ( void * )
static

Definition at line 44 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_set_write_fn

void(* qpng_set_write_fn) (void *, void *, void(*)(void *png, unsigned char *data, size_t length), void(*)(void *png)) ( void * ,
void * ,
void(*  )(void *png, unsigned char *data, size_t length),
void(*  )(void *png) )
static

Definition at line 57 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_sig_cmp

int(* qpng_sig_cmp) (const unsigned char *, size_t, size_t) ( const unsigned char * ,
size_t ,
size_t  )
static

Definition at line 35 of file image_png.c.

Referenced by PNG_LoadImage_BGRA().

◆ qpng_write_end

void(* qpng_write_end) (void *, void *) ( void * ,
void *  )
static

Definition at line 66 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_write_info

void(* qpng_write_info) (void *, void *) ( void * ,
void *  )
static

Definition at line 64 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ qpng_write_row

void(* qpng_write_row) (void *, unsigned char *) ( void * ,
unsigned char *  )
static

Definition at line 65 of file image_png.c.

Referenced by PNG_SaveImage_preflipped().

◆ tmpBuf

const unsigned char* tmpBuf

Definition at line 231 of file image_png.c.

◆ tmpBuflength

int tmpBuflength

Definition at line 232 of file image_png.c.

◆ tmpi

int tmpi

Definition at line 233 of file image_png.c.

◆ Width

unsigned long Width

Definition at line 248 of file image_png.c.