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