Debian Codegraph @

tools/quake3/q3map2/image.c

raw symbols SCIP
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 #include "webp/decode.h"
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 unsigned 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 /* initialize with zeros, this memory area may not be entirely rewritten */
232 *pixels = safe_malloc0( w * h * 4 );
233
234 /* create row pointers */
235 rowPointers = safe_malloc( h * sizeof( byte* ) );
236 for ( i = 0; i < h; i++ )
237 rowPointers[ i ] = *pixels + ( i * w * 4 );
238
239 /* read the png */
240 png_read_image( png, rowPointers );
241
242 /* clean up */
243 free( rowPointers );
244 png_destroy_read_struct( &png, &info, &end );
245 }
246
247
248
249 static void LoadWEBPBuffer( byte *buffer, int size, byte **pixels, int *width, int *height ){
250
251 int image_width;
252 int image_height;
253
254 if ( !WebPGetInfo( buffer, ( size_t) size, &image_width, &image_height ) )
255 {
256 Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image info\n" );
257 return;
258 }
259
260 /* create image pixel buffer */
261 *pixels = safe_malloc( image_width * image_height * 4 );
262 *width = image_width;
263 *height = image_height;
264
265 int out_stride = image_width * 4;
266 int out_size = image_height * out_stride;
267
268 if ( !WebPDecodeRGBAInto( buffer, (size_t) size, *pixels, out_size, out_stride ) )
269 {
270 Sys_FPrintf( SYS_WRN, "WARNING: An error occurred reading WEBP image\n" );
271 return;
272 }
273 }
274
275
276
277 /*
278 ImageInit()
279 implicitly called by every function to set up image list
280 */
281
282 static void ImageInit( void ){
283 int i;
284
285
286 if ( numImages <= 0 ) {
287 /* clear images (fixme: this could theoretically leak) */
288 memset( images, 0, sizeof( images ) );
289
290 /* generate *bogus image */
291 images[ 0 ].name = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
292 strcpy( images[ 0 ].name, DEFAULT_IMAGE );
293 images[ 0 ].filename = safe_malloc( strlen( DEFAULT_IMAGE ) + 1 );
294 strcpy( images[ 0 ].filename, DEFAULT_IMAGE );
295 images[ 0 ].width = 64;
296 images[ 0 ].height = 64;
297 images[ 0 ].refCount = 1;
298 images[ 0 ].pixels = safe_malloc( 64 * 64 * 4 );
299 for ( i = 0; i < ( 64 * 64 * 4 ); i++ )
300 images[ 0 ].pixels[ i ] = 255;
301 }
302 }
303
304
305
306 /*
307 ImageFree()
308 frees an rgba image
309 */
310
311 void ImageFree( image_t *image ){
312 /* dummy check */
313 if ( image == NULL ) {
314 return;
315 }
316
317 /* decrement refcount */
318 image->refCount--;
319
320 /* free? */
321 if ( image->refCount <= 0 ) {
322 if ( image->name != NULL ) {
323 free( image->name );
324 }
325 image->name = NULL;
326 if ( image->filename != NULL ) {
327 free( image->filename );
328 }
329 image->filename = NULL;
330 free( image->pixels );
331 image->width = 0;
332 image->height = 0;
333 numImages--;
334 }
335 }
336
337
338
339 /*
340 ImageFind()
341 finds an existing rgba image and returns a pointer to the image_t struct or NULL if not found
342 */
343
344 image_t *ImageFind( const char *filename ){
345 int i;
346 char name[ 1024 ];
347
348
349 /* init */
350 ImageInit();
351
352 /* dummy check */
353 if ( filename == NULL || filename[ 0 ] == '\0' ) {
354 return NULL;
355 }
356
357 /* strip file extension off name */
358 strcpy( name, filename );
359 StripExtension( name );
360
361 /* search list */
362 for ( i = 0; i < MAX_IMAGES; i++ )
363 {
364 if ( images[ i ].name != NULL && !strcmp( name, images[ i ].name ) ) {
365 return &images[ i ];
366 }
367 }
368
369 /* no matching image found */
370 return NULL;
371 }
372
373
374
375 /*
376 ImageLoad()
377 loads an rgba image and returns a pointer to the image_t struct or NULL if not found
378 */
379
380 image_t *ImageLoad( const char *filename ){
381 int i;
382 image_t *image;
383 char name[ 1024 ];
384 int size;
385 byte *buffer = NULL;
386 qboolean alphaHack = qfalse;
387
388
389 /* init */
390 ImageInit();
391
392 /* dummy check */
393 if ( filename == NULL || filename[ 0 ] == '\0' ) {
394 return NULL;
395 }
396
397 /* try with extension then without extension
398 so light.blend will be unfortunately first
399 tried as light.tga like it always did, but
400 then, light.blend.tga will be tried as well */
401 for ( i = 0; i < 2; i++ )
402 {
403 strcpy( name, filename );
404
405 if ( i == 0 )
406 {
407 /* strip file extension off name */
408 StripExtension( name );
409 }
410
411 /* try to find existing image */
412 image = ImageFind( name );
413 if ( image != NULL ) {
414 image->refCount++;
415 return image;
416 }
417
418 /* none found, so find first non-null image */
419 image = NULL;
420 for ( i = 0; i < MAX_IMAGES; i++ )
421 {
422 if ( images[ i ].name == NULL ) {
423 image = &images[ i ];
424 break;
425 }
426 }
427
428 /* too many images? */
429 if ( image == NULL ) {
430 Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
431 }
432
433 /* set it up */
434 image->name = safe_malloc( strlen( name ) + 1 );
435 strcpy( image->name, name );
436
437 /* add a dummy extension that can be removed */
438 strcat( name, ".tga" );
439
440 /* Sys_FPrintf( SYS_WRN, "==> Looking for %s\n", name ); */
441
442 /* attempt to load tga */
443 StripExtension( name );
444 strcat( name, ".tga" );
445 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
446 if ( size > 0 ) {
447 LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
448 break;
449 }
450
451 /* attempt to load png */
452 StripExtension( name );
453 strcat( name, ".png" );
454 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
455 if ( size > 0 ) {
456 LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
457 break;
458 }
459
460 /* attempt to load jpg */
461 StripExtension( name );
462 strcat( name, ".jpg" );
463 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
464 if ( size > 0 ) {
465 if ( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL ) {
466 // On error, LoadJPGBuff might store a pointer to the error message in image->pixels
467 Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
468 }
469 alphaHack = qtrue;
470 break;
471 }
472
473 /* attempt to load dds */
474 StripExtension( name );
475 strcat( name, ".dds" );
476 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
477
478 /* also look for .dds image in dds/ prefix like Doom3 or DarkPlaces */
479 if ( size <= 0 ) {
480 char ddsname[ 1024 ];
481 strcpy( ddsname, "dds/" );
482 strcat( ddsname, image->name );
483 StripExtension( ddsname );
484 strcat( ddsname, ".dds" );
485 size = vfsLoadFile( (const char*) ddsname, (void**) &buffer, 0 );
486 }
487
488 if ( size > 0 ) {
489 LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
490 break;
491 }
492
493 /* attempt to load ktx */
494 StripExtension( name );
495 strcat( name, ".ktx" );
496 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
497 if ( size > 0 ) {
498 LoadKTXBufferFirstImage( buffer, size, &image->pixels, &image->width, &image->height );
499 break;
500 }
501
502 /* attempt to load crn */
503 StripExtension( name );
504 strcat( name, ".crn" );
505 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
506 if ( size > 0 ) {
507 LoadCRNBuffer( buffer, size, &image->pixels, &image->width, &image->height );
508 break;
509 }
510
511 /* attempt to load webp */
512 StripExtension( name );
513 strcat( name, ".webp" );
514 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
515 if ( size > 0 ) {
516 LoadWEBPBuffer( buffer, size, &image->pixels, &image->width, &image->height );
517 break;
518 }
519 }
520
521 /* free file buffer */
522 free( buffer );
523
524 /* make sure everything's kosher */
525 if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) {
526 // Sys_FPrintf( SYS_WRN, "WARNING: Image not found: \"%s\"\n", filename );
527 //% Sys_Printf( "size = %d width = %d height = %d pixels = 0x%08x (%s)\n",
528 //% size, image->width, image->height, image->pixels, name );
529 free( image->name );
530 image->name = NULL;
531 return NULL;
532 }
533
534 /* tell user which image file is found for the given texture path */
535 Sys_FPrintf( SYS_VRB, "Loaded image: \"%s\"\n", name );
536
537 /* set filename */
538 image->filename = safe_malloc( strlen( name ) + 1 );
539 strcpy( image->filename, name );
540
541 /* set count */
542 image->refCount = 1;
543 numImages++;
544
545 if ( alphaHack ) {
546 StripExtension( name );
547 strcat( name, "_alpha.jpg" );
548 size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
549 if ( size > 0 ) {
550 unsigned char *pixels;
551 int width, height;
552 if ( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 ) {
553 if (pixels) {
554 // On error, LoadJPGBuff might store a pointer to the error message in pixels
555 Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff %s %s\n", name, (unsigned char*) pixels );
556 }
557 } else {
558 if ( width == image->width && height == image->height ) {
559 int i;
560 for ( i = 0; i < width * height; ++i )
561 image->pixels[4 * i + 3] = pixels[4 * i + 2]; // copy alpha from blue channel
562 }
563 free( pixels );
564 }
565 free( buffer );
566 }
567 }
568
569 /* return the image */
570 return image;
571 }