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 ){
59 if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
69 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 ) {
76 Sys_Printf( "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 );
93 callback function for libpng to read from a memory buffer
94 note: this function is a total hack, as it reads/writes the png struct directly!
97 typedef struct pngBuffer_s
103 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size ){
104 pngBuffer_t *pb = (pngBuffer_t*) png_get_io_ptr( png );
107 if ( ( pb->offset + size ) > pb->size ) {
108 size = ( pb->size - pb->offset );
110 memcpy( buffer, &pb->buffer[ pb->offset ], size );
112 //% 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 );
119 loads a png file buffer into a valid rgba image
122 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
124 png_info *info, *end;
126 int i, bitDepth, colorType, channels;
131 if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) {
140 /* determine if this is a png file */
141 if ( png_sig_cmp( buffer, 0, 8 ) != 0 ) {
142 Sys_Printf( "WARNING: Invalid PNG file\n" );
146 /* create png structs */
147 png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
149 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
153 info = png_create_info_struct( png );
154 if ( info == NULL ) {
155 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
156 png_destroy_read_struct( &png, NULL, NULL );
160 end = png_create_info_struct( png );
162 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
163 png_destroy_read_struct( &png, &info, NULL );
167 /* set read callback */
171 png_set_read_fn( png, (void*)&pb, PNGReadData );
173 /* set error longjmp */
174 if ( setjmp( png_jmpbuf(png) ) ) {
175 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
176 png_destroy_read_struct( &png, &info, &end );
180 /* fixme: add proper i/o stuff here */
183 png_read_info( png, info );
185 /* read image header chunk */
186 png_get_IHDR( png, info,
187 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
189 /* read number of channels */
190 channels = png_get_channels( png, info );
192 /* the following will probably bork on certain types of png images, but hey... */
194 /* force indexed/gray/trans chunk to rgb */
195 if ( ( colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8 ) ||
196 ( colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8 ) ||
197 png_get_valid( png, info, PNG_INFO_tRNS ) ) {
198 png_set_expand( png );
201 /* strip 16bpc -> 8bpc */
202 if ( bitDepth == 16 ) {
203 png_set_strip_16( png );
206 /* pad rgb to rgba */
207 if ( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB ) {
208 png_set_filler( png, 255, PNG_FILLER_AFTER );
211 /* create image pixel buffer */
214 *pixels = safe_malloc( w * h * 4 );
216 /* create row pointers */
217 rowPointers = safe_malloc( h * sizeof( byte* ) );
218 for ( i = 0; i < h; i++ )
219 rowPointers[ i ] = *pixels + ( i * w * 4 );
222 png_read_image( png, rowPointers );
226 png_destroy_read_struct( &png, &info, &end );
234 implicitly called by every function to set up image list
237 static void ImageInit( void ){
241 if ( numImages <= 0 ) {
242 /* clear images (fixme: this could theoretically leak) */
243 memset( images, 0, sizeof( images ) );
245 /* generate *bogus image */
246 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
247 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
248 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
249 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
250 images[ 0 ].width = 64;
251 images[ 0 ].height = 64;
252 images[ 0 ].refCount = 1;
253 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
254 for ( i = 0; i < ( 64 * 64 * 4 ); i++ )
255 images[ 0 ].pixels[ i ] = 255;
266 void ImageFree( image_t *image ){
268 if ( image == NULL ) {
272 /* decrement refcount */
276 if ( image->refCount <= 0 ) {
277 if ( image->name != NULL ) {
281 if ( image->filename != NULL ) {
282 free( image->filename );
284 image->filename = NULL;
285 free( image->pixels );
296 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
299 image_t *ImageFind( const char *filename ){
308 if ( filename == NULL || filename[ 0 ] == '\0' ) {
312 /* strip file extension off name */
313 strcpy( name, filename );
314 StripExtension( name );
317 for ( i = 0; i < MAX_IMAGES; i++ )
319 if ( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) ) {
324 /* no matching image found */
332 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
335 image_t *ImageLoad( const char *filename ){
347 if ( filename == NULL || filename[ 0 ] == '\0' ) {
351 /* strip file extension off name */
352 strcpy( name, filename );
353 StripExtension( name );
355 /* try to find existing image */
356 image = ImageFind( name );
357 if ( image != NULL ) {
362 /* none found, so find first non-null image */
364 for ( i = 0; i < MAX_IMAGES; i++ )
366 if ( images[ i ].name == NULL ) {
367 image = &images[ i ];
372 /* too many images? */
373 if ( image == NULL ) {
374 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
378 image->name = safe_malloc( strlen( name ) + 1 );
379 strcpy( image->name, name );
381 /* attempt to load tga */
382 StripExtension( name );
383 strcat( name, ".tga" );
384 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
386 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
390 /* attempt to load png */
391 StripExtension( name );
392 strcat( name, ".png" );
393 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
395 LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
399 /* attempt to load jpg */
400 StripExtension( name );
401 strcat( name, ".jpg" );
402 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
404 if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
405 Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
410 /* attempt to load dds */
411 StripExtension( name );
412 strcat( name, ".dds" );
413 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
415 LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
421 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
422 Sys_Printf( "pf = %d\n", pf );
423 if ( image->width > 0 ) {
424 StripExtension( name );
425 strcat( name, "_converted.tga" );
426 WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
435 /* free file buffer */
438 /* make sure everything's kosher */
439 if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) {
440 //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
441 //% size, image->width, image->height, image->pixels, name );
448 image->filename = safe_malloc( strlen( name ) + 1 );
449 strcpy( image->filename, name );
455 /* return the image */