eliminate the global variables

This commit is contained in:
Bill Currie 2003-09-04 21:24:20 +00:00
parent 2b5a3753ca
commit a0875ea909

View file

@ -54,21 +54,16 @@ typedef unsigned char uch;
typedef unsigned short ush; typedef unsigned short ush;
typedef unsigned long ulg; typedef unsigned long ulg;
static png_structp png_ptr = NULL;
static png_infop info_ptr = NULL;
static QFile *pngfile = NULL;
char tmpBuffer[8192];
/* Qread wrapper for libpng */ /* Qread wrapper for libpng */
static void static void
user_read_data (png_structp png_ptr, png_bytep data, png_size_t length) { user_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
Qread (pngfile, data, length); {
Qread ((QFile *) png_get_io_ptr (png_ptr), data, length);
} }
/* Basicly taken from the libpng example rpng-x */ /* Basicly taken from the libpng example rpng-x */
static int static int
readpng_init (QFile *infile) readpng_init (QFile *infile, png_structp *png_ptr, png_infop *info_ptr)
{ {
uch sig[8]; uch sig[8];
@ -79,31 +74,30 @@ readpng_init (QFile *infile)
return (1); return (1);
} }
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); *png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) if (!*png_ptr)
return (2); /* Out of memory! */ return (2); /* Out of memory! */
info_ptr = png_create_info_struct (png_ptr); *info_ptr = png_create_info_struct (*png_ptr);
if (!info_ptr) { if (!*info_ptr) {
png_destroy_read_struct (&png_ptr, NULL, NULL); png_destroy_read_struct (png_ptr, NULL, NULL);
return (3); /* Out of memory! */ return (3); /* Out of memory! */
} }
/* setjmp() must be called in every function that calls a PNG-reading /* setjmp() must be called in every function that calls a PNG-reading
* libpng function */ * libpng function */
if (setjmp(png_jmpbuf(png_ptr))) { if (setjmp(png_jmpbuf(*png_ptr))) {
png_destroy_read_struct (&png_ptr, &info_ptr, NULL); png_destroy_read_struct (png_ptr, info_ptr, NULL);
return (4); return (4);
} }
pngfile = infile; png_set_read_fn (*png_ptr, infile, user_read_data);
png_set_read_fn (png_ptr, tmpBuffer, user_read_data);
/* Is png_set_sig_bytes needed? */ /* Is png_set_sig_bytes needed? */
png_set_sig_bytes (png_ptr, 8); /* We allready read the 8 signiture bytes */ png_set_sig_bytes (*png_ptr, 8); // We allready read the 8 signiture bytes
png_read_info (png_ptr, info_ptr); /* read all png info upto the image data */ png_read_info (*png_ptr, *info_ptr);//read all png info upto the image data
return (0); return (0);
} }
@ -113,13 +107,15 @@ tex_t *
LoadPNG (QFile *infile) LoadPNG (QFile *infile)
{ {
double gamma; double gamma;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_uint_32 i, rowbytes; png_uint_32 i, rowbytes;
png_bytepp row_pointers = NULL; png_bytepp row_pointers = NULL;
png_uint_32 width, height; png_uint_32 width, height;
int bit_depth, color_type; int bit_depth, color_type;
tex_t *tex; tex_t *tex;
if (readpng_init(infile) != 0) if (readpng_init(infile, &png_ptr, &info_ptr) != 0)
return (NULL); return (NULL);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);