2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
40 /* -------------------------------------------------------------------------------
42 this file contains image pool management with reference counting. note: it isn't
43 reentrant, so only call it from init/shutdown code or wrap calls in a mutex
45 ------------------------------------------------------------------------------- */
49 loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
52 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 )
68 if( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) )
70 Sys_Printf( "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 )
77 Sys_Printf( "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
81 /* create image pixel buffer */
84 *pixels = safe_malloc( w * h * 4 );
86 /* decompress the dds texture */
87 DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
94 callback function for libpng to read from a memory buffer
95 note: this function is a total hack, as it reads/writes the png struct directly!
98 typedef struct pngBuffer_s
105 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size )
107 pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
110 if( (pb->offset + size) > pb->size )
111 size = (pb->size - pb->offset);
112 memcpy( buffer, &pb->buffer[ pb->offset ], size );
114 //% 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 );
121 loads a png file buffer into a valid rgba image
124 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
127 png_info *info, *end;
129 int i, bitDepth, colorType, channels;
135 if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
143 /* determine if this is a png file */
144 if( png_sig_cmp( buffer, 0, 8 ) != 0 )
146 Sys_Printf( "WARNING: Invalid PNG file\n" );
150 /* create png structs */
151 png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
154 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
158 info = png_create_info_struct( png );
161 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
162 png_destroy_read_struct( &png, NULL, NULL );
166 end = png_create_info_struct( png );
169 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
170 png_destroy_read_struct( &png, &info, NULL );
174 /* set read callback */
178 png_set_read_fn( png, &pb, PNGReadData );
179 png->io_ptr = &pb; /* hack! */
181 /* set error longjmp */
182 if( setjmp( png->jmpbuf ) )
184 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
185 png_destroy_read_struct( &png, &info, &end );
189 /* fixme: add proper i/o stuff here */
192 png_read_info( png, info );
194 /* read image header chunk */
195 png_get_IHDR( png, info,
196 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
198 /* read number of channels */
199 channels = png_get_channels( png, info );
201 /* the following will probably bork on certain types of png images, but hey... */
203 /* force indexed/gray/trans chunk to rgb */
204 if( (colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
205 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) ||
206 png_get_valid( png, info, PNG_INFO_tRNS ) )
207 png_set_expand( png );
209 /* strip 16bpc -> 8bpc */
211 png_set_strip_16( png );
213 /* pad rgb to rgba */
214 if( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
215 png_set_filler( png, 255, PNG_FILLER_AFTER );
217 /* create image pixel buffer */
220 *pixels = safe_malloc( w * h * 4 );
222 /* create row pointers */
223 rowPointers = safe_malloc( h * sizeof( byte* ) );
224 for( i = 0; i < h; i++ )
225 rowPointers[ i ] = *pixels + (i * w * 4);
228 png_read_image( png, rowPointers );
232 png_destroy_read_struct( &png, &info, &end );
240 implicitly called by every function to set up image list
243 static void ImageInit( void )
250 /* clear images (fixme: this could theoretically leak) */
251 memset( images, 0, sizeof( images ) );
253 /* generate *bogus image */
254 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
255 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
256 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
257 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
258 images[ 0 ].width = 64;
259 images[ 0 ].height = 64;
260 images[ 0 ].refCount = 1;
261 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
262 for( i = 0; i < (64 * 64 * 4); i++ )
263 images[ 0 ].pixels[ i ] = 255;
274 void ImageFree( image_t *image )
280 /* decrement refcount */
284 if( image->refCount <= 0 )
286 if( image->name != NULL )
289 if( image->filename != NULL )
290 free( image->filename );
291 image->filename = NULL;
292 free( image->pixels );
303 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
306 image_t *ImageFind( const char *filename )
316 if( filename == NULL || filename[ 0 ] == '\0' )
319 /* strip file extension off name */
320 strcpy( name, filename );
321 StripExtension( name );
324 for( i = 0; i < MAX_IMAGES; i++ )
326 if( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) )
330 /* no matching image found */
338 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
341 image_t *ImageLoad( const char *filename )
354 if( filename == NULL || filename[ 0 ] == '\0' )
357 /* strip file extension off name */
358 strcpy( name, filename );
359 StripExtension( name );
361 /* try to find existing image */
362 image = ImageFind( name );
369 /* none found, so find first non-null image */
371 for( i = 0; i < MAX_IMAGES; i++ )
373 if( images[ i ].name == NULL )
375 image = &images[ i ];
380 /* too many images? */
382 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
385 image->name = safe_malloc( strlen( name ) + 1 );
386 strcpy( image->name, name );
388 /* attempt to load tga */
389 StripExtension( name );
390 strcat( name, ".tga" );
391 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
393 LoadTGABuffer( buffer, &image->pixels, &image->width, &image->height );
396 /* attempt to load png */
397 StripExtension( name );
398 strcat( name, ".png" );
399 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
401 LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
404 /* attempt to load jpg */
405 StripExtension( name );
406 strcat( name, ".jpg" );
407 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
410 if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
411 Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
415 /* attempt to load dds */
416 StripExtension( name );
417 strcat( name, ".dds" );
418 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
421 LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
427 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
428 Sys_Printf( "pf = %d\n", pf );
429 if( image->width > 0 )
431 StripExtension( name );
432 strcat( name, "_converted.tga" );
433 WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
442 /* free file buffer */
445 /* make sure everything's kosher */
446 if( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL )
448 //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
449 //% size, image->width, image->height, image->pixels, name );
456 image->filename = safe_malloc( strlen( name ) + 1 );
457 strcpy( image->filename, name );
463 /* return the image */