]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/image.c
WebP support for radiant and q3map2.
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
index 4fb3f74e618b8efe731015d64df7e0f4c97557a4..c947085445def14e171f98983fe357a4213d11f5 100644 (file)
@@ -36,7 +36,7 @@
 /* dependencies */
 #include "q3map2.h"
 
-
+#include "webp/decode.h"
 
 /* -------------------------------------------------------------------------------
 
@@ -248,6 +248,34 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in
 
 
 
+static void LoadWEBPBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
+
+       int image_width;
+       int image_height;
+       
+       if ( !WebPGetInfo( buffer, ( size_t) size, &image_width, &image_height ) )
+       {
+               Sys_Printf( "WARNING: An error occurred reading WEBP image info\n" );
+               return;
+       }
+
+       /* create image pixel buffer */
+       *pixels = safe_malloc( image_width * image_height * 4 );
+       *width = image_width;
+       *height = image_height;
+
+       int out_stride = image_width  * 4;
+       int out_size =  image_height * out_stride;
+
+               if ( !WebPDecodeRGBAInto( buffer, (size_t) size, *pixels, out_size, out_stride ) )
+               {
+               Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image\n" );
+                       return;
+               }
+}
+
+
+
 /*
    ImageInit()
    implicitly called by every function to set up image list
@@ -472,6 +500,15 @@ image_t *ImageLoad( const char *filename ){
                        break;
                }
                #endif // BUILD_CRUNCH
+
+               /* attempt to load webp */
+               StripExtension( name );
+               strcat( name, ".webp" );
+               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+               if ( size > 0 ) {
+                       LoadWEBPBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+                       break;
+               }
        } while (qfalse);
 
        /* free file buffer */