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