X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=tools%2Fquake3%2Fq3map2%2Fimage.c;h=f5c0b1e90f7c4082bad7b6e41785e545087c282a;hb=414307f1eb6b2f46b87121ea71d63adf973cf571;hp=e5f97a40c1dbc8d4d71531bbc237926ef033020f;hpb=dc24f680c400b2325c649fe48ac1984887a1a966;p=xonotic%2Fnetradiant.git diff --git a/tools/quake3/q3map2/image.c b/tools/quake3/q3map2/image.c index e5f97a40..f5c0b1e9 100644 --- a/tools/quake3/q3map2/image.c +++ b/tools/quake3/q3map2/image.c @@ -230,7 +230,8 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in /* create image pixel buffer */ *width = w; *height = h; - *pixels = safe_malloc( w * h * 4 ); + /* initialize with zeros, this memory area may not be entirely rewritten */ + *pixels = safe_malloc0( w * h * 4 ); /* create row pointers */ rowPointers = safe_malloc( h * sizeof( byte* ) ); @@ -243,7 +244,6 @@ static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, in /* clean up */ free( rowPointers ); png_destroy_read_struct( &png, &info, &end ); - } @@ -396,112 +396,138 @@ image_t *ImageLoad( const char *filename ){ return NULL; } - /* strip file extension off name */ - strcpy( name, filename ); - StripExtension( name ); + /* try with extension then without extension + so light.blend will be unfortunately first + tried as light.tga like it always did, but + then, light.blend.tga will be tried as well */ + for ( i = 0; i < 2; i++ ) + { + strcpy( name, filename ); - /* try to find existing image */ - image = ImageFind( name ); - if ( image != NULL ) { - image->refCount++; - return image; - } + if ( i == 0 ) + { + /* strip file extension off name */ + StripExtension( name ); + } - /* none found, so find first non-null image */ - image = NULL; - for ( i = 0; i < MAX_IMAGES; i++ ) - { - if ( images[ i ].name == NULL ) { - image = &images[ i ]; - break; + /* try to find existing image */ + image = ImageFind( name ); + if ( image != NULL ) { + image->refCount++; + return image; } - } - /* too many images? */ - if ( image == NULL ) { - Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES ); - } + /* none found, so find first non-null image */ + image = NULL; + for ( i = 0; i < MAX_IMAGES; i++ ) + { + if ( images[ i ].name == NULL ) { + image = &images[ i ]; + break; + } + } + + /* too many images? */ + if ( image == NULL ) { + Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES ); + } - /* set it up */ - image->name = safe_malloc( strlen( name ) + 1 ); - strcpy( image->name, name ); + /* set it up */ + image->name = safe_malloc( strlen( name ) + 1 ); + strcpy( image->name, name ); - /* 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; - } + /* add a dummy extension that can be removed */ + strcat( name, ".tga" ); - /* 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; - } + /* Sys_FPrintf( SYS_WRN, "==> Looking for %s\n", name ); */ - /* 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 ); + /* 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; } - 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 ); - 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 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; - } + /* 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; + } - #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 dds */ + StripExtension( name ); + strcat( name, ".dds" ); + size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 ); - /* 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; - } + /* also look for .dds image in dds/ prefix like Doom3 or DarkPlaces */ + if ( size <= 0 ) { + char ddsname[ 1024 ]; + strcpy( ddsname, "dds/" ); + strcat( ddsname, image->name ); + StripExtension( ddsname ); + strcat( ddsname, ".dds" ); + size = vfsLoadFile( (const char*) ddsname, (void**) &buffer, 0 ); + } - image_load_success: + if ( size > 0 ) { + LoadDDSBuffer( 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 ); + break; + } + + #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 + + /* 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; + } + } /* free file buffer */ free( buffer ); /* make sure everything's kosher */ if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) { + // Sys_FPrintf( SYS_WRN, "WARNING: Image not found: \"%s\"\n", filename ); //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n", //% size, image->width, image->height, image->pixels, name ); free( image->name ); @@ -509,6 +535,9 @@ image_t *ImageLoad( const char *filename ){ return NULL; } + /* tell user which image file is found for the given texture path */ + Sys_FPrintf( SYS_VRB, "Loaded image: \"%s\"\n", name ); + /* set filename */ image->filename = safe_malloc( strlen( name ) + 1 ); strcpy( image->filename, name );