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