1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
39 #include "webp/decode.h"
41 /* -------------------------------------------------------------------------------
43 this file contains image pool management with reference counting. note: it isn't
44 reentrant, so only call it from init/shutdown code or wrap calls in a mutex
46 ------------------------------------------------------------------------------- */
50 loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
53 static void LoadDDSBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
59 if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
69 if ( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) ) {
70 Sys_FPrintf( SYS_WRN, "WARNING: Invalid DDS texture\n" );
74 /* only certain types of dds textures are supported */
75 if ( pf != DDS_PF_ARGB8888 && pf != DDS_PF_DXT1 && pf != DDS_PF_DXT3 && pf != DDS_PF_DXT5 ) {
76 Sys_FPrintf( SYS_WRN, "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
80 /* create image pixel buffer */
83 *pixels = safe_malloc( w * h * 4 );
85 /* decompress the dds texture */
86 DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
92 loads a crn image into a valid rgba image
94 void LoadCRNBuffer( byte *buffer, int size, byte **pixels, int *width, int *height) {
96 if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
99 if ( !GetCRNImageSize( buffer, size, width, height ) ) {
100 Sys_FPrintf( SYS_WRN, "WARNING: Error getting crn imag dimensions.\n");;
103 int outBufSize = *width * *height * 4;
104 *pixels = safe_malloc( outBufSize );
105 if ( !ConvertCRNtoRGBA( buffer, size, outBufSize, *pixels) ) {
106 Sys_FPrintf( SYS_WRN, "WARNING: Error decoding crn image.\n", 0 );
110 #endif // BUILD_CRUNCH
115 callback function for libpng to read from a memory buffer
116 note: this function is a total hack, as it reads/writes the png struct directly!
119 typedef struct pngBuffer_s
122 png_size_t size, offset;
125 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
126 pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
129 if ( ( pb->offset + size ) > pb->size ) {
130 size = ( pb->size - pb->offset );
132 memcpy( buffer, &pb->buffer[ pb->offset ], size );
134 //% Sys_Printf( "Copying %d bytes from 0x%08X to 0x%08X (offset: %d of %d)\n", size, &pb->buffer[ pb->offset ], buffer, pb->offset, pb->size );
141 loads a png file buffer into a valid rgba image
144 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
146 png_info *info, *end;
148 int bitDepth, colorType;
153 if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
162 /* determine if this is a png file */
163 if ( png_sig_cmp( buffer, 0, 8 ) != 0 ) {
164 Sys_FPrintf( SYS_WRN, "WARNING: Invalid PNG file\n" );
168 /* create png structs */
169 png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
171 Sys_FPrintf( SYS_WRN, "WARNING: Unable to create PNG read struct\n" );
175 info = png_create_info_struct( png );
176 if ( info == NULL ) {
177 Sys_FPrintf( SYS_WRN, "WARNING: Unable to create PNG info struct\n" );
178 png_destroy_read_struct( &png, NULL, NULL );
182 end = png_create_info_struct( png );
184 Sys_FPrintf( SYS_WRN, "WARNING: Unable to create PNG end info struct\n" );
185 png_destroy_read_struct( &png, &info, NULL );
189 /* set read callback */
193 png_set_read_fn( png, &pb, PNGReadData );
195 /* set error longjmp */
196 if ( setjmp( png_jmpbuf(png) ) ) {
197 Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading PNG image\n" );
198 png_destroy_read_struct( &png, &info, &end );
202 /* fixme: add proper i/o stuff here */
205 png_read_info( png, info );
207 /* read image header chunk */
208 png_get_IHDR( png, info,
209 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
211 /* the following will probably bork on certain types of png images, but hey... */
213 /* force indexed/gray/trans chunk to rgb */
214 if ( ( colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8 ) ||
215 ( colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8 ) ||
216 png_get_valid( png, info, PNG_INFO_tRNS ) ) {
217 png_set_expand( png );
220 /* strip 16bpc -> 8bpc */
221 if ( bitDepth == 16 ) {
222 png_set_strip_16( png );
225 /* pad rgb to rgba */
226 if ( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB ) {
227 png_set_filler( png, 255, PNG_FILLER_AFTER );
230 /* create image pixel buffer */
233 /* initialize with zeros, this memory area may not be entirely rewritten */
234 *pixels = safe_malloc0( w * h * 4 );
236 /* create row pointers */
237 rowPointers = safe_malloc( h * sizeof( byte* ) );
238 for ( i = 0; i < h; i++ )
239 rowPointers[ i ] = *pixels + ( i * w * 4 );
242 png_read_image( png, rowPointers );
246 png_destroy_read_struct( &png, &info, &end );
251 static void LoadWEBPBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
256 if ( !WebPGetInfo( buffer, ( size_t) size, &image_width, &image_height ) )
258 Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image info\n" );
262 /* create image pixel buffer */
263 *pixels = safe_malloc( image_width * image_height * 4 );
264 *width = image_width;
265 *height = image_height;
267 int out_stride = image_width * 4;
268 int out_size = image_height * out_stride;
270 if ( !WebPDecodeRGBAInto( buffer, (size_t) size, *pixels, out_size, out_stride ) )
272 Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image\n" );
281 implicitly called by every function to set up image list
284 static void ImageInit( void ){
288 if ( numImages <= 0 ) {
289 /* clear images (fixme: this could theoretically leak) */
290 memset( images, 0, sizeof( images ) );
292 /* generate *bogus image */
293 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
294 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
295 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
296 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
297 images[ 0 ].width = 64;
298 images[ 0 ].height = 64;
299 images[ 0 ].refCount = 1;
300 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
301 for ( i = 0; i < ( 64 * 64 * 4 ); i++ )
302 images[ 0 ].pixels[ i ] = 255;
313 void ImageFree( image_t *image ){
315 if ( image == NULL ) {
319 /* decrement refcount */
323 if ( image->refCount <= 0 ) {
324 if ( image->name != NULL ) {
328 if ( image->filename != NULL ) {
329 free( image->filename );
331 image->filename = NULL;
332 free( image->pixels );
343 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
346 image_t *ImageFind( const char *filename ){
355 if ( filename == NULL || filename[ 0 ] == '\0' ) {
359 /* strip file extension off name */
360 strcpy( name, filename );
361 StripExtension( name );
364 for ( i = 0; i < MAX_IMAGES; i++ )
366 if ( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) ) {
371 /* no matching image found */
379 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
382 image_t *ImageLoad( const char *filename ){
388 qboolean alphaHack = qfalse;
395 if ( filename == NULL || filename[ 0 ] == '\0' ) {
399 /* try with extension then without extension
400 so light.blend will be unfortunately first
401 tried as light.tga like it always did, but
402 then, light.blend.tga will be tried as well */
403 for ( i = 0; i < 2; i++ )
405 strcpy( name, filename );
409 /* strip file extension off name */
410 StripExtension( name );
413 /* try to find existing image */
414 image = ImageFind( name );
415 if ( image != NULL ) {
420 /* none found, so find first non-null image */
422 for ( i = 0; i < MAX_IMAGES; i++ )
424 if ( images[ i ].name == NULL ) {
425 image = &images[ i ];
430 /* too many images? */
431 if ( image == NULL ) {
432 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
436 image->name = safe_malloc( strlen( name ) + 1 );
437 strcpy( image->name, name );
439 /* add a dummy extension that can be removed */
440 strcat( name, ".tga" );
442 /* Sys_FPrintf( SYS_WRN, "==> Looking for %s\n", name ); */
444 /* attempt to load tga */
445 StripExtension( name );
446 strcat( name, ".tga" );
447 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
449 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
453 /* attempt to load png */
454 StripExtension( name );
455 strcat( name, ".png" );
456 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
458 LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
462 /* attempt to load jpg */
463 StripExtension( name );
464 strcat( name, ".jpg" );
465 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
467 if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
468 // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
469 Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
475 /* attempt to load dds */
476 StripExtension( name );
477 strcat( name, ".dds" );
478 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
480 /* also look for .dds image in dds/ prefix like Doom3 or DarkPlaces */
482 char ddsname[ 1024 ];
483 strcpy( ddsname, "dds/" );
484 strcat( ddsname, image->name );
485 StripExtension( ddsname );
486 strcat( ddsname, ".dds" );
487 size = vfsLoadFile( (const char*) ddsname, (void**) &buffer, 0 );
491 LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
495 /* attempt to load ktx */
496 StripExtension( name );
497 strcat( name, ".ktx" );
498 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
500 LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
505 /* attempt to load crn */
506 StripExtension( name );
507 strcat( name, ".crn" );
508 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
510 LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
513 #endif // BUILD_CRUNCH
515 /* attempt to load webp */
516 StripExtension( name );
517 strcat( name, ".webp" );
518 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
520 LoadWEBPBuffer( buffer, size, &image->pixels, &image->width, &image->height );
525 /* free file buffer */
528 /* make sure everything's kosher */
529 if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) {
530 // Sys_FPrintf( SYS_WRN, "WARNING: Image not found: \"%s\"\n", filename );
531 //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
532 //% size, image->width, image->height, image->pixels, name );
538 /* tell user which image file is found for the given texture path */
539 Sys_FPrintf( SYS_VRB, "Loaded image: \"%s\"\n", name );
542 image->filename = safe_malloc( strlen( name ) + 1 );
543 strcpy( image->filename, name );
550 StripExtension( name );
551 strcat( name, "_alpha.jpg" );
552 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
554 unsigned char *pixels;
556 if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) {
558 // On error, LoadJPGBuff might store a pointer to the error message in pixels
559 Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff %s %s\n", name, (unsigned char*) pixels );
562 if ( width == image->width && height == image->height ) {
564 for ( i = 0; i < width * height; ++i )
565 image->pixels[4 * i + 3] = pixels[4 * i + 2]; // copy alpha from blue channel
573 /* return the image */