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