]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/image.c
q3map2: make Smokin'Guns code not requiring a rebuild
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
index 4fb3f74e618b8efe731015d64df7e0f4c97557a4..e5f97a40c1dbc8d4d71531bbc237926ef033020f 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_FPrintf( SYS_WRN, "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
@@ -398,81 +426,76 @@ image_t *ImageLoad( const char *filename ){
        image->name = safe_malloc( strlen( name ) + 1 );
        strcpy( image->name, name );
 
-       do {
-               /* attempt to load tga */
-               StripExtension( name );
-               strcat( name, ".tga" );
-               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-               if ( size > 0 ) {
-                       LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
-                       break;
-               }
+       /* attempt to load tga */
+       StripExtension( name );
+       strcat( name, ".tga" );
+       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+       if ( size > 0 ) {
+               LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
+               goto image_load_success;
+       }
 
-               /* attempt to load png */
-               StripExtension( name );
-               strcat( name, ".png" );
-               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-               if ( size > 0 ) {
-                       LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
-                       break;
-               }
+       /* attempt to load png */
+       StripExtension( name );
+       strcat( name, ".png" );
+       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+       if ( size > 0 ) {
+               LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+               goto image_load_success;
+       }
 
-               /* attempt to load jpg */
-               StripExtension( name );
-               strcat( name, ".jpg" );
-               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-               if ( size > 0 ) {
-                       if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
-                               // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
-                               Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
-                       }
-                       alphaHack = qtrue;
-                       break;
+       /* attempt to load jpg */
+       StripExtension( name );
+       strcat( name, ".jpg" );
+       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+       if ( size > 0 ) {
+               if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
+                       // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
+                       Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
                }
+               alphaHack = qtrue;
+               goto image_load_success;
+       }
 
-               /* attempt to load dds */
-               StripExtension( name );
-               strcat( name, ".dds" );
-               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-               if ( size > 0 ) {
-                       LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
-
-                       /* debug code */
-                       #if 1
-                       {
-                               ddsPF_t pf;
-                               DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
-                               Sys_Printf( "pf = %d\n", pf );
-                               if ( image->width > 0 ) {
-                                       StripExtension( name );
-                                       strcat( name, "_converted.tga" );
-                                       WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
-                               }
-                       }
-                       #endif // debug
-                       break;
-               }
+       /* attempt to load dds */
+       StripExtension( name );
+       strcat( name, ".dds" );
+       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+       if ( size > 0 ) {
+               LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+               goto image_load_success;
+       }
 
-               /* attempt to load ktx */
-               StripExtension( name );
-               strcat( name, ".ktx" );
-               size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
-               if ( size > 0 ) {
-                       LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
-                       break;
-               }
+       /* attempt to load ktx */
+       StripExtension( name );
+       strcat( name, ".ktx" );
+       size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
+       if ( size > 0 ) {
+               LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
+               goto image_load_success;
+       }
 
-               #ifdef BUILD_CRUNCH
-               /* attempt to load crn */
-               StripExtension( name );
-               strcat( name, ".crn" );
-               size = vfsLoadFile( ( const char* ) name, ( void** ) &buffer, 0 );
-               if ( size > 0 ) {
-                       LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
-                       break;
-               }
-               #endif // BUILD_CRUNCH
-       } while (qfalse);
+       #ifdef BUILD_CRUNCH
+       /* attempt to load crn */
+       StripExtension( name );
+       strcat( name, ".crn" );
+       size = vfsLoadFile( ( const char* ) name, ( void** ) &buffer, 0 );
+       if ( size > 0 ) {
+               LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
+               goto image_load_success;
+       }
+       #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 );
+               goto image_load_success;
+       }
+
+       image_load_success:
 
        /* free file buffer */
        free( buffer );