]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/image.c
also load DDS files the way DarkPlaces stores them (does DDS loading even work?)
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22 ----------------------------------------------------------------------------------
23
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."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define IMAGE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* -------------------------------------------------------------------------------
42
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
45
46 ------------------------------------------------------------------------------- */
47
48 /*
49 LoadDDSBuffer()
50 loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
51 */
52
53 static void LoadDDSBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
54 {
55         int             w, h;
56         ddsPF_t pf;
57         
58         
59         /* dummy check */
60         if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
61                 return;
62         
63         /* null out */
64         *pixels = 0;
65         *width = 0;
66         *height = 0;
67         
68         /* get dds info */
69         if( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) )
70         {
71                 Sys_Printf( "WARNING: Invalid DDS texture\n" );
72                 return;
73         }
74         
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 )
77         {
78                 Sys_Printf( "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
79                 return;
80         }
81         
82         /* create image pixel buffer */
83         *width = w;
84         *height = h;
85         *pixels = safe_malloc( w * h * 4 );
86         
87         /* decompress the dds texture */
88         DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
89 }
90
91
92
93 /*
94 PNGReadData()
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!
97 */
98
99 typedef struct pngBuffer_s
100 {
101         byte    *buffer;
102         png_size_t              size, offset;
103 }
104 pngBuffer_t;
105
106 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size )
107 {
108         pngBuffer_t             *pb = (pngBuffer_t*) png_get_io_ptr( png );
109         
110         
111         if( (pb->offset + size) > pb->size )
112                 size = (pb->size - pb->offset);
113         memcpy( buffer, &pb->buffer[ pb->offset ], size );
114         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 );
116 }
117
118
119
120 /*
121 LoadPNGBuffer()
122 loads a png file buffer into a valid rgba image
123 */
124
125 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
126 {
127         png_struct      *png;
128         png_info        *info, *end;
129         pngBuffer_t     pb;
130         int                     bitDepth, colorType, channels;
131         png_uint_32     w, h, i;
132         byte            **rowPointers;
133         
134         
135         /* dummy check */
136         if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
137                 return;
138         
139         /* null out */
140         *pixels = 0;
141         *width = 0;
142         *height = 0;
143         
144         /* determine if this is a png file */
145         if( png_sig_cmp( buffer, 0, 8 ) != 0 )
146         {
147                 Sys_Printf( "WARNING: Invalid PNG file\n" );
148                 return;
149         }
150         
151         /* create png structs */
152         png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
153         if( png == NULL )
154         {
155                 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
156                 return;
157         }
158         
159         info = png_create_info_struct( png );
160         if( info == NULL )
161         {
162                 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
163                 png_destroy_read_struct( &png, NULL, NULL );
164                 return;
165         }
166         
167         end = png_create_info_struct( png );
168         if( end == NULL )
169         {
170                 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
171                 png_destroy_read_struct( &png, &info, NULL );
172                 return;
173         }
174         
175         /* set read callback */
176         pb.buffer = buffer;
177         pb.size = size;
178         pb.offset = 0;
179         png_set_read_fn( png, &pb, PNGReadData );
180         png->io_ptr = &pb; /* hack! */
181         
182         /* set error longjmp */
183         if( setjmp( png->jmpbuf ) )
184         {
185                 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
186                 png_destroy_read_struct( &png, &info, &end );
187                 return;
188         }
189         
190         /* fixme: add proper i/o stuff here */
191
192         /* read png info */
193         png_read_info( png, info );
194         
195         /* read image header chunk */
196         png_get_IHDR( png, info,
197                 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
198         
199         /* read number of channels */
200         channels = png_get_channels( png, info );
201         
202         /* the following will probably bork on certain types of png images, but hey... */
203
204         /* force indexed/gray/trans chunk to rgb */
205         if( (colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
206                 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) ||
207                 png_get_valid( png, info, PNG_INFO_tRNS ) )
208                 png_set_expand( png );
209         
210         /* strip 16bpc -> 8bpc */
211         if( bitDepth == 16 )
212                 png_set_strip_16( png );
213         
214         /* pad rgb to rgba */
215         if( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
216                 png_set_filler( png, 255, PNG_FILLER_AFTER );
217         
218         /* create image pixel buffer */
219         *width = w;
220         *height = h;
221         *pixels = safe_malloc( w * h * 4 );
222         
223         /* create row pointers */
224         rowPointers = safe_malloc( h * sizeof( byte* ) );
225         for( i = 0; i < h; i++ )
226                 rowPointers[ i ] = *pixels + (i * w * 4);
227         
228         /* read the png */
229         png_read_image( png, rowPointers );
230         
231         /* clean up */
232         free( rowPointers );
233         png_destroy_read_struct( &png, &info, &end );
234         
235 }
236
237
238
239 /*
240 ImageInit()
241 implicitly called by every function to set up image list
242 */
243
244 static void ImageInit( void )
245 {
246         int             i;
247         
248         
249         if( numImages <= 0 )
250         {
251                 /* clear images (fixme: this could theoretically leak) */
252                 memset( images, 0, sizeof( images ) );
253                 
254                 /* generate *bogus image */
255                 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
256                 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
257                 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
258                 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
259                 images[ 0 ].width = 64;
260                 images[ 0 ].height = 64;
261                 images[ 0 ].refCount = 1;
262                 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
263                 for( i = 0; i < (64 * 64 * 4); i++ )
264                         images[ 0 ].pixels[ i ] = 255;
265         }
266 }
267
268
269
270 /*
271 ImageFree()
272 frees an rgba image
273 */
274
275 void ImageFree( image_t *image )
276 {
277         /* dummy check */
278         if( image == NULL )
279                 return;
280         
281         /* decrement refcount */
282         image->refCount--;
283         
284         /* free? */
285         if( image->refCount <= 0 )
286         {
287                 if( image->name != NULL )
288                         free( image->name );
289                 image->name = NULL;
290                 if( image->filename != NULL )
291                         free( image->filename );
292                 image->filename = NULL;
293                 free( image->pixels );
294                 image->width = 0;
295                 image->height = 0;
296                 numImages--;
297         }
298 }
299
300
301
302 /*
303 ImageFind()
304 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
305 */
306
307 image_t *ImageFind( const char *filename )
308 {
309         int                     i;
310         char            name[ 1024 ];
311         
312         
313         /* init */
314         ImageInit();
315         
316         /* dummy check */
317         if( filename == NULL || filename[ 0 ] == '\0' )
318                 return NULL;
319         
320         /* strip file extension off name */
321         strcpy( name, filename );
322         StripExtension( name );
323         
324         /* search list */
325         for( i = 0; i < MAX_IMAGES; i++ )
326         {
327                 if( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) )
328                         return &images[ i ];
329         }
330         
331         /* no matching image found */
332         return NULL;
333 }
334
335
336
337 /*
338 ImageLoad()
339 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
340 */
341
342 image_t *ImageLoad( const char *filename )
343 {
344         int                     i;
345         image_t         *image;
346         char            name[ 1024 ];
347         int                     size;
348         byte            *buffer = NULL;
349         qboolean        alphaHack = qfalse;
350
351         
352         /* init */
353         ImageInit();
354         
355         /* dummy check */
356         if( filename == NULL || filename[ 0 ] == '\0' )
357                 return NULL;
358         
359         /* strip file extension off name */
360         strcpy( name, filename );
361         StripExtension( name );
362         
363         /* try to find existing image */
364         image = ImageFind( name );
365         if( image != NULL )
366         {
367                 image->refCount++;
368                 return image;
369         }
370         
371         /* none found, so find first non-null image */
372         image = NULL;
373         for( i = 0; i < MAX_IMAGES; i++ )
374         {
375                 if( images[ i ].name == NULL )
376                 {
377                         image = &images[ i ];
378                         break;
379                 }
380         }
381         
382         /* too many images? */
383         if( image == NULL )
384                 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
385         
386         /* set it up */
387         image->name = safe_malloc( strlen( name ) + 1 );
388         strcpy( image->name, name );
389         
390         /* attempt to load tga */
391         StripExtension( name );
392         strcat( name, ".tga" );
393         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
394         if( size > 0 )
395                 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
396         else
397         {
398                 /* attempt to load png */
399                 StripExtension( name );
400                 strcat( name, ".png" );
401                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
402                 if( size > 0 )
403                         LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
404                 else
405                 {
406                         /* attempt to load jpg */
407                         StripExtension( name );
408                         strcat( name, ".jpg" );
409                         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
410                         if( size > 0 )
411                         {
412                                 if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
413                                         Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
414                                 alphaHack = qtrue;
415                         }
416                         else
417                         {
418                                 /* attempt to load dds */
419                                 StripExtension( name );
420                                 strcat( name, ".dds" );
421                                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
422                                 if(size <= 0)
423                                 {
424                                         memmove(name + 4, name, strlen(name) + 1);
425                                         memcpy(name, "dds/", 4);
426                                         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
427                                 }
428                                 if( size > 0 )
429                                 {
430                                         LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
431                                         
432                                         /* debug code */
433                                         #if 1
434                                         {
435                                                 ddsPF_t pf;
436                                                 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
437                                                 Sys_Printf( "pf = %d\n", pf );
438                                                 if( image->width > 0 )
439                                                 {
440                                                         StripExtension( name );
441                                                         strcat( name, "_converted.tga" );
442                                                         WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
443                                                 }
444                                         }
445                                         #endif
446                                 }
447                         }
448                 }
449         }
450         
451         /* free file buffer */
452         free( buffer );
453         
454         /* make sure everything's kosher */
455         if( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL )
456         {
457                 //%     Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
458                 //%             size, image->width, image->height, image->pixels, name );
459                 free( image->name );
460                 image->name = NULL;
461                 return NULL;
462         }
463         
464         /* set filename */
465         image->filename = safe_malloc( strlen( name ) + 1 );
466         strcpy( image->filename, name );
467         
468         /* set count */
469         image->refCount = 1;
470         numImages++;
471
472         if(alphaHack)
473         {
474                 StripExtension( name );
475                 strcat( name, "_alpha.jpg" );
476                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
477                 if( size > 0 )
478                 {
479                         unsigned char *pixels;
480                         int width, height;
481                         if( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != NULL )
482                                 Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
483                         if(pixels && width == image->width && height == image->height)
484                         {
485                                 int i;
486                                 for(i = 0; i < width*height; ++i)
487                                         image->pixels[4*i+3] = pixels[4*i+2]; // copy alpha from blue channel
488                         }
489                         free(pixels);
490                         free(buffer);
491                 }
492         }
493         
494         /* return the image */
495         return image;
496 }
497
498