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