]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/image.c
eol style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / image.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 ----------------------------------------------------------------------------------
22
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
25
26 ------------------------------------------------------------------------------- */
27
28
29
30 /* marker */
31 #define IMAGE_C
32
33
34
35 /* dependencies */
36 #include "q3map2.h"
37
38
39
40 /* -------------------------------------------------------------------------------
41
42 this file contains image pool management with reference counting. note: it isn't
43 reentrant, so only call it from init/shutdown code or wrap calls in a mutex
44
45 ------------------------------------------------------------------------------- */
46
47 /*
48 LoadDDSBuffer()
49 loads a dxtc (1, 3, 5) dds buffer into a valid rgba image
50 */
51
52 static void LoadDDSBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
53 {
54         int             w, h;
55         ddsPF_t pf;
56         
57         
58         /* dummy check */
59         if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
60                 return;
61         
62         /* null out */
63         *pixels = 0;
64         *width = 0;
65         *height = 0;
66         
67         /* get dds info */
68         if( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) )
69         {
70                 Sys_Printf( "WARNING: Invalid DDS texture\n" );
71                 return;
72         }
73         
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         {
77                 Sys_Printf( "WARNING: Only DDS texture formats ARGB8888, DXT1, DXT3, and DXT5 are supported (%d)\n", pf );
78                 return;
79         }
80         
81         /* create image pixel buffer */
82         *width = w;
83         *height = h;
84         *pixels = safe_malloc( w * h * 4 );
85         
86         /* decompress the dds texture */
87         DDSDecompress( (ddsBuffer_t*) buffer, *pixels );
88 }
89
90
91
92 /*
93 PNGReadData()
94 callback function for libpng to read from a memory buffer
95 note: this function is a total hack, as it reads/writes the png struct directly!
96 */
97
98 typedef struct pngBuffer_s
99 {
100         byte    *buffer;
101         int             size, offset;
102 }
103 pngBuffer_t;
104
105 void PNGReadData( png_struct *png, png_byte *buffer, png_size_t size )
106 {
107         pngBuffer_t             *pb = (pngBuffer_t*) png_get_io_ptr( png );
108         
109         
110         if( (pb->offset + size) > pb->size )
111                 size = (pb->size - pb->offset);
112         memcpy( buffer, &pb->buffer[ pb->offset ], size );
113         pb->offset += size;
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 );
115 }
116
117
118
119 /*
120 LoadPNGBuffer()
121 loads a png file buffer into a valid rgba image
122 */
123
124 static void LoadPNGBuffer( byte *buffer, int size, byte **pixels, int *width, int *height )
125 {
126         png_struct      *png;
127         png_info        *info, *end;
128         pngBuffer_t     pb;
129         int                     i, bitDepth, colorType, channels;
130         png_uint_32     w, h;
131         byte            **rowPointers;
132         
133         
134         /* dummy check */
135         if( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL )
136                 return;
137         
138         /* null out */
139         *pixels = 0;
140         *width = 0;
141         *height = 0;
142         
143         /* determine if this is a png file */
144         if( png_sig_cmp( buffer, 0, 8 ) != 0 )
145         {
146                 Sys_Printf( "WARNING: Invalid PNG file\n" );
147                 return;
148         }
149         
150         /* create png structs */
151         png = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
152         if( png == NULL )
153         {
154                 Sys_Printf( "WARNING: Unable to create PNG read struct\n" );
155                 return;
156         }
157         
158         info = png_create_info_struct( png );
159         if( info == NULL )
160         {
161                 Sys_Printf( "WARNING: Unable to create PNG info struct\n" );
162                 png_destroy_read_struct( &png, NULL, NULL );
163                 return;
164         }
165         
166         end = png_create_info_struct( png );
167         if( end == NULL )
168         {
169                 Sys_Printf( "WARNING: Unable to create PNG end info struct\n" );
170                 png_destroy_read_struct( &png, &info, NULL );
171                 return;
172         }
173         
174         /* set read callback */
175         pb.buffer = buffer;
176         pb.size = size;
177         pb.offset = 0;
178         png_set_read_fn( png, &pb, PNGReadData );
179         png->io_ptr = &pb; /* hack! */
180         
181         /* set error longjmp */
182         if( setjmp( png->jmpbuf ) )
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         /* read number of channels */
199         channels = png_get_channels( png, info );
200         
201         /* the following will probably bork on certain types of png images, but hey... */
202
203         /* force indexed/gray/trans chunk to rgb */
204         if( (colorType == PNG_COLOR_TYPE_PALETTE && bitDepth <= 8) ||
205                 (colorType == PNG_COLOR_TYPE_GRAY && bitDepth <= 8) ||
206                 png_get_valid( png, info, PNG_INFO_tRNS ) )
207                 png_set_expand( png );
208         
209         /* strip 16bpc -> 8bpc */
210         if( bitDepth == 16 )
211                 png_set_strip_16( png );
212         
213         /* pad rgb to rgba */
214         if( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB)
215                 png_set_filler( png, 255, PNG_FILLER_AFTER );
216         
217         /* create image pixel buffer */
218         *width = w;
219         *height = h;
220         *pixels = safe_malloc( w * h * 4 );
221         
222         /* create row pointers */
223         rowPointers = safe_malloc( h * sizeof( byte* ) );
224         for( i = 0; i < h; i++ )
225                 rowPointers[ i ] = *pixels + (i * w * 4);
226         
227         /* read the png */
228         png_read_image( png, rowPointers );
229         
230         /* clean up */
231         free( rowPointers );
232         png_destroy_read_struct( &png, &info, &end );
233         
234 }
235
236
237
238 /*
239 ImageInit()
240 implicitly called by every function to set up image list
241 */
242
243 static void ImageInit( void )
244 {
245         int             i;
246         
247         
248         if( numImages <= 0 )
249         {
250                 /* clear images (fixme: this could theoretically leak) */
251                 memset( images, 0, sizeof( images ) );
252                 
253                 /* generate *bogus image */
254                 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
255                 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
256                 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
257                 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
258                 images[ 0 ].width = 64;
259                 images[ 0 ].height = 64;
260                 images[ 0 ].refCount = 1;
261                 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
262                 for( i = 0; i < (64 * 64 * 4); i++ )
263                         images[ 0 ].pixels[ i ] = 255;
264         }
265 }
266
267
268
269 /*
270 ImageFree()
271 frees an rgba image
272 */
273
274 void ImageFree( image_t *image )
275 {
276         /* dummy check */
277         if( image == NULL )
278                 return;
279         
280         /* decrement refcount */
281         image->refCount--;
282         
283         /* free? */
284         if( image->refCount <= 0 )
285         {
286                 if( image->name != NULL )
287                         free( image->name );
288                 image->name = NULL;
289                 if( image->filename != NULL )
290                         free( image->filename );
291                 image->filename = NULL;
292                 free( image->pixels );
293                 image->width = 0;
294                 image->height = 0;
295                 numImages--;
296         }
297 }
298
299
300
301 /*
302 ImageFind()
303 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
304 */
305
306 image_t *ImageFind( const char *filename )
307 {
308         int                     i;
309         char            name[ 1024 ];
310         
311         
312         /* init */
313         ImageInit();
314         
315         /* dummy check */
316         if( filename == NULL || filename[ 0 ] == '\0' )
317                 return NULL;
318         
319         /* strip file extension off name */
320         strcpy( name, filename );
321         StripExtension( name );
322         
323         /* search list */
324         for( i = 0; i < MAX_IMAGES; i++ )
325         {
326                 if( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) )
327                         return &images[ i ];
328         }
329         
330         /* no matching image found */
331         return NULL;
332 }
333
334
335
336 /*
337 ImageLoad()
338 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
339 */
340
341 image_t *ImageLoad( const char *filename )
342 {
343         int                     i;
344         image_t         *image;
345         char            name[ 1024 ];
346         int                     size;
347         byte            *buffer = NULL;
348
349         
350         /* init */
351         ImageInit();
352         
353         /* dummy check */
354         if( filename == NULL || filename[ 0 ] == '\0' )
355                 return NULL;
356         
357         /* strip file extension off name */
358         strcpy( name, filename );
359         StripExtension( name );
360         
361         /* try to find existing image */
362         image = ImageFind( name );
363         if( image != NULL )
364         {
365                 image->refCount++;
366                 return image;
367         }
368         
369         /* none found, so find first non-null image */
370         image = NULL;
371         for( i = 0; i < MAX_IMAGES; i++ )
372         {
373                 if( images[ i ].name == NULL )
374                 {
375                         image = &images[ i ];
376                         break;
377                 }
378         }
379         
380         /* too many images? */
381         if( image == NULL )
382                 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
383         
384         /* set it up */
385         image->name = safe_malloc( strlen( name ) + 1 );
386         strcpy( image->name, name );
387         
388         /* attempt to load tga */
389         StripExtension( name );
390         strcat( name, ".tga" );
391         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
392         if( size > 0 )
393                 LoadTGABuffer( buffer, &image->pixels, &image->width, &image->height );
394         else
395         {
396                 /* attempt to load png */
397                 StripExtension( name );
398                 strcat( name, ".png" );
399                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
400                 if( size > 0 )
401                         LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
402                 else
403                 {
404                         /* attempt to load jpg */
405                         StripExtension( name );
406                         strcat( name, ".jpg" );
407                         size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
408                         if( size > 0 )
409                         {
410                                 if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
411                                         Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
412                         }
413                         else
414                         {
415                                 /* attempt to load dds */
416                                 StripExtension( name );
417                                 strcat( name, ".dds" );
418                                 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
419                                 if( size > 0 )
420                                 {
421                                         LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
422                                         
423                                         /* debug code */
424                                         #if 1
425                                         {
426                                                 ddsPF_t pf;
427                                                 DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
428                                                 Sys_Printf( "pf = %d\n", pf );
429                                                 if( image->width > 0 )
430                                                 {
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 );
434                                                 }
435                                         }
436                                         #endif
437                                 }
438                         }
439                 }
440         }
441         
442         /* free file buffer */
443         free( buffer );
444         
445         /* make sure everything's kosher */
446         if( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL )
447         {
448                 //%     Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
449                 //%             size, image->width, image->height, image->pixels, name );
450                 free( image->name );
451                 image->name = NULL;
452                 return NULL;
453         }
454         
455         /* set filename */
456         image->filename = safe_malloc( strlen( name ) + 1 );
457         strcpy( image->filename, name );
458         
459         /* set count */
460         image->refCount = 1;
461         numImages++;
462         
463         /* return the image */
464         return image;
465 }
466
467