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