]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image_png.c
do not use non-standard strnlen function
[xonotic/darkplaces.git] / image_png.c
index 6cf182a1e94a958f2491754940c60d1bed702ecc..ca090b2c57ff53113369cab8deba020deb0ae7bf 100644 (file)
@@ -32,7 +32,7 @@
 
 static void                            (*qpng_set_sig_bytes)           (void*, int);
 static int                             (*qpng_sig_cmp)                         (const unsigned char*, size_t, size_t);
-static void*                   (*qpng_create_read_struct)      (const char*, void*, void*, 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_info_struct)      (void*);
 static void                            (*qpng_read_info)                       (void*, void*);
 static void                            (*qpng_set_expand)                      (void*);
@@ -45,7 +45,7 @@ static void                           (*qpng_read_update_info)        (void*, void*);
 static void                            (*qpng_read_image)                      (void*, unsigned char**);
 static void                            (*qpng_read_end)                        (void*, void*);
 static void                            (*qpng_destroy_read_struct)     (void**, void**, void**);
-static void                            (*qpng_set_read_fn)                     (void*, void*, void*);
+static void                            (*qpng_set_read_fn)                     (void*, void*, void(*)(void *png, unsigned char *data, size_t length));
 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*);
@@ -121,14 +121,7 @@ qboolean PNG_OpenLibrary (void)
                return true;
 
        // Load the DLL
-       if (! Sys_LoadLibrary (dllnames, &png_dll, pngfuncs))
-       {
-               Con_Printf ("PNG support disabled\n");
-               return false;
-       }
-
-       Con_Printf ("PNG support enabled\n");
-       return true;
+       return Sys_LoadLibrary (dllnames, &png_dll, pngfuncs);
 }
 
 
@@ -229,8 +222,9 @@ void PNG_warning_fn(void *png, const char *message)
 extern int     image_width;
 extern int     image_height;
 
-unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchwidth, int matchheight)
+unsigned char *PNG_LoadImage_BGRA (const unsigned char *raw, int filesize)
 {
+       unsigned int c;
        unsigned int    y;
        void *png, *pnginfo;
        unsigned char *imagedata = NULL;
@@ -244,7 +238,7 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
 
        if(qpng_sig_cmp(raw, 0, filesize))
                return NULL;
-       png = (void *)qpng_create_read_struct(PNG_LIBPNG_VER_STRING, 0, (void *)PNG_error_fn, (void *)PNG_warning_fn);
+       png = (void *)qpng_create_read_struct(PNG_LIBPNG_VER_STRING, 0, PNG_error_fn, PNG_warning_fn);
        if(!png)
                return NULL;
 
@@ -255,7 +249,7 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        // 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 defined(MACOSX) || defined(WIN32)
        if (setjmp((int *)png))
 #else
        if (setjmp((__jmp_buf_tag *)png))
@@ -294,7 +288,7 @@ 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, (void *)PNG_fReadData);
+       qpng_set_read_fn(png, ioBuffer, 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);
 
@@ -311,11 +305,6 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        }
 #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;
-       }
        if (my_png.ColorType == PNG_COLOR_TYPE_PALETTE)
                qpng_set_palette_to_rgb(png);
        if (my_png.ColorType == PNG_COLOR_TYPE_GRAY || my_png.ColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
@@ -351,12 +340,21 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
                        qpng_read_image(png, my_png.FRowPtrs);
                }
                else
-                       Con_DPrintf("PNG_LoadImage : not enough memory\n");
+               {
+                       Con_Printf("PNG_LoadImage : not enough memory\n");
+                       qpng_destroy_read_struct(&png, &pnginfo, 0);
+                       Mem_Free(my_png.FRowPtrs);
+                       return NULL;
+               }
                Mem_Free(my_png.FRowPtrs);
                my_png.FRowPtrs = NULL;
        }
        else
-               Con_DPrintf("PNG_LoadImage : not enough memory\n");
+       {
+               Con_Printf("PNG_LoadImage : not enough memory\n");
+               qpng_destroy_read_struct(&png, &pnginfo, 0);
+               return NULL;
+       }
 
        qpng_read_end(png, pnginfo);
        qpng_destroy_read_struct(&png, &pnginfo, 0);
@@ -368,7 +366,15 @@ unsigned char *PNG_LoadImage (const unsigned char *raw, int filesize, int matchw
        {
                Con_Printf ("PNG_LoadImage : bad color depth\n");
                Mem_Free(imagedata);
-               imagedata = NULL;
+               return NULL;
+       }
+
+       // swizzle RGBA to BGRA
+       for (y = 0;y < (unsigned int)(image_width*image_height*4);y += 4)
+       {
+               c = imagedata[y+0];
+               imagedata[y+0] = imagedata[y+2];
+               imagedata[y+2] = c;
        }
 
        return imagedata;