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 ------------------------------------------------------------------------------- */
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 )
60 if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
69 if( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) )
71 Sys_Printf( "WARNING: Invalid DDS texture\n" );
75 /* only certain types of dds textures are supported */
76 if( pf != DDS_PF_ARGB8888 && pf != DDS_PF_DXT1 && pf != DDS_PF_DXT3 && pf != DDS_PF_DXT5 )
78 Sys_Printf( "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
82 /* create image pixel buffer */
85 *pixels = safe_malloc( w * h * 4 );
87 /* decompress the dds texture */
88 DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
95 callback function for libpng to read from a memory buffer
96 note: this function is a total hack, as it reads/writes the png struct directly!
99 typedef struct pngBuffer_s
102 png_size_t size, offset;
106 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size )
108 pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
111 if( (pb->offset + size) > pb->size )
112 size = (pb->size - pb->offset);
113 memcpy( buffer, &pb->buffer[ pb->offset ], size );
115 //% 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 );
122 loads a png file buffer into a valid rgba image
125 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
128 png_info *info, *end;
130 int bitDepth, colorType;
136 if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
144 /* determine if this is a png file */
145 if( png_sig_cmp( buffer, 0, 8 ) != 0 )
147 Sys_Printf( "WARNING: Invalid PNG file\n" );
151 /* create png structs */
152 png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
155 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
159 info = png_create_info_struct( png );
162 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
163 png_destroy_read_struct( &png, NULL, NULL );
167 end = png_create_info_struct( png );
170 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
171 png_destroy_read_struct( &png, &info, NULL );
175 /* set read callback */
179 png_set_read_fn( png, &pb, PNGReadData );
180 png->io_ptr = &pb; /* hack! */
182 /* set error longjmp */
183 if( setjmp( png->jmpbuf ) )
185 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
186 png_destroy_read_struct( &png, &info, &end );
190 /* fixme: add proper i/o stuff here */
193 png_read_info( png, info );
195 /* read image header chunk */
196 png_get_IHDR( png, info,
197 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
199 /* the following will probably bork on certain types of png images, but hey... */
201 /* force indexed/gray/trans chunk to rgb */
202 if( (colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
203 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) ||
204 png_get_valid( png, info, PNG_INFO_tRNS ) )
205 png_set_expand( png );
207 /* strip 16bpc -> 8bpc */
209 png_set_strip_16( png );
211 /* pad rgb to rgba */
212 if( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
213 png_set_filler( png, 255, PNG_FILLER_AFTER );
215 /* create image pixel buffer */
218 *pixels = safe_malloc( w * h * 4 );
220 /* create row pointers */
221 rowPointers = safe_malloc( h * sizeof( byte* ) );
222 for( i = 0; i < h; i++ )
223 rowPointers[ i ] = *pixels + (i * w * 4);
226 png_read_image( png, rowPointers );
230 png_destroy_read_struct( &png, &info, &end );
238 implicitly called by every function to set up image list
241 static void ImageInit( void )
248 /* clear images (fixme: this could theoretically leak) */
249 memset( images, 0, sizeof( images ) );
251 /* generate *bogus image */
252 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
253 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
254 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
255 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
256 images[ 0 ].width = 64;
257 images[ 0 ].height = 64;
258 images[ 0 ].refCount = 1;
259 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
260 for( i = 0; i < (64 * 64 * 4); i++ )
261 images[ 0 ].pixels[ i ] = 255;
272 void ImageFree( image_t *image )
278 /* decrement refcount */
282 if( image->refCount <= 0 )
284 if( image->name != NULL )
287 if( image->filename != NULL )
288 free( image->filename );
289 image->filename = NULL;
290 free( image->pixels );
301 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
304 image_t *ImageFind( const char *filename )
314 if( filename == NULL || filename[ 0 ] == '\0' )
317 /* strip file extension off name */
318 strcpy( name, filename );
319 StripExtension( name );
322 for( i = 0; i < MAX_IMAGES; i++ )
324 if( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) )
328 /* no matching image found */
336 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
339 image_t *ImageLoad( const char *filename )
346 qboolean alphaHack = qfalse;
353 if( filename == NULL || filename[ 0 ] == '\0' )
356 /* strip file extension off name */
357 strcpy( name, filename );
358 StripExtension( name );
360 /* try to find existing image */
361 image = ImageFind( name );
368 /* none found, so find first non-null image */
370 for( i = 0; i < MAX_IMAGES; i++ )
372 if( images[ i ].name == NULL )
374 image = &images[ i ];
379 /* too many images? */
381 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
384 image->name = safe_malloc( strlen( name ) + 1 );
385 strcpy( image->name, name );
387 /* attempt to load tga */
388 StripExtension( name );
389 strcat( name, ".tga" );
390 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
392 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
395 /* attempt to load png */
396 StripExtension( name );
397 strcat( name, ".png" );
398 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
400 LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
403 /* attempt to load jpg */
404 StripExtension( name );
405 strcat( name, ".jpg" );
406 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
409 if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
410 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 );
465 StripExtension( name );
466 strcat( name, "_alpha.jpg" );
467 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
470 unsigned char *pixels;
472 if( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != NULL )
473 Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
474 if(pixels && width == image->width && height == image->height)
477 for(i = 0; i < width*height; ++i)
478 image->pixels[4*i+3] = pixels[4*i+2]; // copy alpha from blue channel
485 /* return the image */