]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image_png.c
view models no longer receive stencil shadows (only basic light visibility culling...
[xonotic/darkplaces.git] / image_png.c
index 8b9d1ffea45d5c23c9ae6d672543a6ae8c5eb500..084fb1c8e8268693c30f320abafe5f132e8a3f24 100644 (file)
@@ -50,7 +50,7 @@ static unsigned int           (*qpng_get_valid)                       (void*, void*, unsigned int);
 static unsigned int            (*qpng_get_rowbytes)            (void*, void*);
 static unsigned char   (*qpng_get_channels)            (void*, void*);
 static unsigned char   (*qpng_get_bit_depth)           (void*, void*);
-static unsigned int            (*qpng_get_IHDR)                        (void*, void*, unsigned int*, unsigned int*, int *, int *, int *, int *, int *);
+static unsigned int            (*qpng_get_IHDR)                        (void*, void*, unsigned long*, unsigned long*, int *, int *, int *, int *, int *);
 static char*                   (*qpng_get_libpng_ver)          (void*);
 
 static dllfunction_t pngfuncs[] =
@@ -103,12 +103,15 @@ qboolean PNG_OpenLibrary (void)
 {
        const char* dllnames [] =
        {
-#ifdef WIN32
+#ifdef WIN64
+               "libpng12_64.dll",
+#elif WIN32
                "libpng12.dll",
 #elif defined(MACOSX)
                "libpng12.0.dylib",
 #else
                "libpng12.so.0",
+               "libpng.so", // FreeBSD
 #endif
                NULL
        };
@@ -168,7 +171,7 @@ void PNG_CloseLibrary (void)
 #define PNG_INFO_tRNS 0x0010
 
 // this struct is only used for status information during loading
-struct
+static struct
 {
        const unsigned char     *tmpBuf;
        int             tmpBuflength;
@@ -186,8 +189,8 @@ struct
        int             BitDepth;
        int             BytesPerPixel;
        int             ColorType;
-       unsigned int    Height;
-       unsigned int    Width;
+       unsigned long   Height; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
+       unsigned long   Width; // retarded libpng 1.2 pngconf.h uses long (64bit/32bit depending on arch)
        int             Interlace;
        int             Compression;
        int             Filter;
@@ -202,15 +205,15 @@ void PNG_fReadData(void *png, unsigned char *data, size_t length)
        l = my_png.tmpBuflength - my_png.tmpi;
        if (l < length)
        {
-               Con_Printf("PNG_fReadData: overrun by %i bytes\n", length - l);
+               Con_Printf("PNG_fReadData: overrun by %i bytes\n", (int)(length - l));
                // a read going past the end of the file, fill in the remaining bytes
                // with 0 just to be consistent
                memset(data + l, 0, length - l);
                length = l;
        }
        memcpy(data, my_png.tmpBuf + my_png.tmpi, length);
-       my_png.tmpi += length;
-       Com_HexDumpToConsole(data, length);
+       my_png.tmpi += (int)length;
+       //Com_HexDumpToConsole(data, (int)length);
 }
 
 void PNG_error_fn(void *png, const char *message)
@@ -241,13 +244,21 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
 
        if(qpng_sig_cmp(raw, 0, filesize))
                return NULL;
-       png = qpng_create_read_struct(PNG_LIBPNG_VER_STRING, 0, PNG_error_fn, PNG_warning_fn);
+       png = (void *)qpng_create_read_struct(PNG_LIBPNG_VER_STRING, 0, (void *)PNG_error_fn, (void *)PNG_warning_fn);
        if(!png)
                return NULL;
 
        // NOTE: this relies on jmp_buf being the first thing in the png structure
        // created by libpng! (this is correct for libpng 1.2.x)
+#ifdef __cplusplus
+#ifdef MACOSX
+       if (setjmp((int *)png))
+#else
+       if (setjmp((__jmp_buf_tag *)png))
+#endif
+#else
        if (setjmp(png))
+#endif
        {
                qpng_destroy_read_struct(&png, &pnginfo, 0);
                return NULL;
@@ -274,10 +285,24 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        //my_png.Interlace      = 0;
        //my_png.Compression    = 0;
        //my_png.Filter         = 0;
-       qpng_set_read_fn(png, ioBuffer, PNG_fReadData);
+       qpng_set_read_fn(png, ioBuffer, (void *)PNG_fReadData);
        qpng_read_info(png, pnginfo);
        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);
-       if ((matchwidth && my_png.Width != (unsigned int)matchwidth) || (matchheight && my_png.Height != (unsigned int)matchheight))
+
+       // 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
+       // (if it's little endian the unwritten bytes are the most significant
+       //  ones and we don't worry about that)
+       //
+       // this is only necessary because of retarded 64bit png_uint_32 types in libpng 1.2, which can (conceivably) vary by platform
+#if LONG_MAX > 4000000000
+       if (my_png.Width > LONG_MAX || my_png.Height > LONG_MAX)
+       {
+               my_png.Width >>= 32;
+               my_png.Height >>= 32;
+       }
+#endif
+
+if ((matchwidth && my_png.Width != (unsigned long)matchwidth) || (matchheight && my_png.Height != (unsigned long)matchheight))
        {
                qpng_destroy_read_struct(&png, &pnginfo, 0);
                return NULL;
@@ -305,10 +330,10 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        my_png.FRowBytes = qpng_get_rowbytes(png, pnginfo);
        my_png.BytesPerPixel = qpng_get_channels(png, pnginfo);
 
-       my_png.FRowPtrs = Mem_Alloc(tempmempool, my_png.Height * sizeof(*my_png.FRowPtrs));
+       my_png.FRowPtrs = (unsigned char **)Mem_Alloc(tempmempool, my_png.Height * sizeof(*my_png.FRowPtrs));
        if (my_png.FRowPtrs)
        {
-               my_png.Data = Mem_Alloc(tempmempool, my_png.Height * my_png.FRowBytes);
+               my_png.Data = (unsigned char *)Mem_Alloc(tempmempool, my_png.Height * my_png.FRowBytes);
                if(my_png.Data)
                {
                        for(y = 0;y < my_png.Height;y++)
@@ -325,8 +350,8 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        qpng_read_end(png, pnginfo);
        qpng_destroy_read_struct(&png, &pnginfo, 0);
 
-       image_width = my_png.Width;
-       image_height = my_png.Height;
+       image_width = (int)my_png.Width;
+       image_height = (int)my_png.Height;
        imagedata = my_png.Data;
 
        if (my_png.BitDepth != 8)