]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - image_png.c
better handling of color tints by colormap
[xonotic/darkplaces.git] / image_png.c
index 6cf182a1e94a958f2491754940c60d1bed702ecc..593bb59419b63a2c495694a7f9780010ff1d7de5 100644 (file)
@@ -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;
@@ -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;