]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/image.c
Merge commit '515673c08f8718a237e90c2130a1f5294f966d6a'
[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;
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         
181         /* set error longjmp */
182         if( setjmp( png_jmpbuf(png) ) )
183         {
184                 Sys_Printf( "WARNING: An error occurred reading PNG image\n" );
185                 png_destroy_read_struct( &png, &info, &end );
186                 return;
187         }
188         
189         /* fixme: add proper i/o stuff here */
190
191         /* read png info */
192         png_read_info( png, info );
193         
194         /* read image header chunk */
195         png_get_IHDR( png, info,
196                 &w, &h, &bitDepth, &colorType, NULL, NULL, NULL );
197         
198         /* the following will probably bork on certain types of png images, but hey... */
199
200         /* force indexed/gray/trans chunk to rgb */
201         if( (colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
202                 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) ||
203                 png_get_valid( png, info, PNG_INFO_tRNS ) )
204                 png_set_expand( png );
205         
206         /* strip 16bpc -> 8bpc */
207         if( bitDepth == 16 )
208                 png_set_strip_16( png );
209         
210         /* pad rgb to rgba */
211         if( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
212                 png_set_filler( png, 255, PNG_FILLER_AFTER );
213         
214         /* create image pixel buffer */
215         *width = w;
216         *height = h;
217         *pixels = safe_malloc( w * h * 4 );
218         
219         /* create row pointers */
220         rowPointers = safe_malloc( h * sizeof( byte* ) );
221         for( i = 0; i < h; i++ )
222                 rowPointers[ i ] = *pixels + (i * w * 4);
223         
224         /* read the png */
225         png_read_image( png, rowPointers );
226         
227         /* clean up */
228         free( rowPointers );
229         png_destroy_read_struct( &png, &info, &end );
230         
231 }
232
233
234
235 /*
236 ImageInit()
237 implicitly called by every function to set up image list
238 */
239
240 static void ImageInit( void )
241 {
242         int             i;
243         
244         
245         if( numImages <= 0 )
246         {
247                 /* clear images (fixme: this could theoretically leak) */
248                 memset( images, 0, sizeof( images ) );
249                 
250                 /* generate *bogus image */
251                 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
252                 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
253                 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
254                 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
255                 images[ 0 ].width = 64;
256                 images[ 0 ].height = 64;
257                 images[ 0 ].refCount = 1;
258                 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
259                 for( i = 0; i < (64 * 64 * 4); i++ )
260                         images[ 0 ].pixels[ i ] = 255;
261         }
262 }
263
264
265
266 /*
267 ImageFree()
268 frees an rgba image
269 */
270
271 void ImageFree( image_t *image )
272 {
273         /* dummy check */
274         if( image == NULL )
275                 return;
276         
277         /* decrement refcount */
278         image->refCount--;
279         
280         /* free? */
281         if( image->refCount <= 0 )
282         {
283                 if( image->name != NULL )
284                         free( image->name );
285                 image->name = NULL;
286                 if( image->filename != NULL )
287                         free( image->filename );
288                 image->filename = NULL;
289                 free( image->pixels );
290                 image->width = 0;
291                 image->height = 0;
292                 numImages--;
293         }
294 }
295
296
297
298 /*
299 ImageFind()
300 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
301 */
302
303 image_t *ImageFind( const char *filename )
304 {
305         int                     i;
306         char            name[ 1024 ];
307         
308         
309         /* init */
310         ImageInit();
311         
312         /* dummy check */
313         if( filename == NULL || filename[ 0 ] == '\0' )
314                 return NULL;
315         
316         /* strip file extension off name */
317         strcpy( name, filename );
318         StripExtension( name );
319         
320         /* search list */
321         for( i = 0; i < MAX_IMAGES; i++ )
322         {
323                 if( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) )
324                         return &images[ i ];
325         }
326         
327         /* no matching image found */
328         return NULL;
329 }
330
331
332
333 /*
334 ImageLoad()
335 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
336 */
337
338 image_t *ImageLoad( const char *filename )
339 {
340         int                     i;
341         image_t         *image;
342         char            name[ 1024 ];
343         int                     size;
344         byte            *buffer = NULL;
345         qboolean        alphaHack = qfalse;
346
347         
348         /* init */
349         ImageInit();
350         
351         /* dummy check */
352         if( filename == NULL || filename[ 0 ] == '\0' )
353                 return NULL;
354         
355         /* strip file extension off name */
356         strcpy( name, filename );
357         StripExtension( name );
358         
359         /* try to find existing image */
360         image = ImageFind( name );
361         if( image != NULL )
362         {
363                 image->refCount++;
364                 return image;
365         }
366         
367         /* none found, so find first non-null image */
368         image = NULL;
369         for( i = 0; i < MAX_IMAGES; i++ )
370         {
371                 if( images[ i ].name == NULL )
372                 {
373                         image = &images[ i ];
374                         break;
375                 }
376         }
377         
378         /* too many images? */
379         if( image == NULL )
380                 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
381         
382         /* set it up */
383         image->name = safe_malloc( strlen( name ) + 1 );
384         strcpy( image->name, name );
385         
386         /* attempt to load tga */
387         StripExtension( name );
388         strcat( name, ".tga" );
389         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
390         if( size > 0 )
391                 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
392         else
393         {
394                 /* attempt to load png */
395                 StripExtension( name );
396                 strcat( name, ".png" );
397                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
398                 if( size > 0 )
399                         LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
400                 else
401                 {
402                         /* attempt to load jpg */
403                         StripExtension( name );
404                         strcat( name, ".jpg" );
405                         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
406                         if( size > 0 )
407                         {
408                                 if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
409                                         Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
410                                 alphaHack = qtrue;
411                         }
412                         else
413                         {
414                                 /* attempt to load dds */
415                                 StripExtension( name );
416                                 strcat( name, ".dds" );
417                                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
418                                 if( size > 0 )
419                                 {
420                                         LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
421                                         
422                                         /* debug code */
423                                         #if 1
424                                         {
425                                                 ddsPF_t pf;
426                                                 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
427                                                 Sys_Printf( "pf = %d\n", pf );
428                                                 if( image->width > 0 )
429                                                 {
430                                                         StripExtension( name );
431                                                         strcat( name, "_converted.tga" );
432                                                         WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
433                                                 }
434                                         }
435                                         #endif
436                                 }
437                         }
438                 }
439         }
440         
441         /* free file buffer */
442         free( buffer );
443         
444         /* make sure everything's kosher */
445         if( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL )
446         {
447                 //%     Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
448                 //%             size, image->width, image->height, image->pixels, name );
449                 free( image->name );
450                 image->name = NULL;
451                 return NULL;
452         }
453         
454         /* set filename */
455         image->filename = safe_malloc( strlen( name ) + 1 );
456         strcpy( image->filename, name );
457         
458         /* set count */
459         image->refCount = 1;
460         numImages++;
461
462         if(alphaHack)
463         {
464                 StripExtension( name );
465                 strcat( name, "_alpha.jpg" );
466                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
467                 if( size > 0 )
468                 {
469                         unsigned char *pixels;
470                         int width, height;
471                         if( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != NULL )
472                                 Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
473                         if(pixels && width == image->width && height == image->height)
474                         {
475                                 int i;
476                                 for(i = 0; i < width*height; ++i)
477                                         image->pixels[4*i+3] = pixels[4*i+2]; // copy alpha from blue channel
478                         }
479                         free(pixels);
480                         free(buffer);
481                 }
482         }
483         
484         /* return the image */
485         return image;
486 }
487
488