| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#define IMAGE_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
#include "webp/decode.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 59 |
if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
*pixels = 0; |
| 65 |
*width = 0; |
| 66 |
*height = 0; |
| 67 |
|
| 68 |
|
| 69 |
if ( DDSGetInfo( (ddsBuffer_t*) buffer, &w, &h, &pf ) ) { |
| 70 |
Sys_FPrintf( SYS_WRN, "WARNING: Invalid DDS texture\n" ); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 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 |
|
| 81 |
*width = w; |
| 82 |
*height = h; |
| 83 |
*pixels = safe_malloc( w * h * 4 ); |
| 84 |
|
| 85 |
|
| 86 |
DDSDecompress( (ddsBuffer_t*) buffer, *pixels ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void LoadCRNBuffer( byte *buffer, int size, byte **pixels, int *width, int *height) { |
| 94 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 151 |
if ( buffer == NULL || size <= 0 || pixels == NULL || width == NULL || height == NULL ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
*pixels = 0; |
| 157 |
*width = 0; |
| 158 |
*height = 0; |
| 159 |
|
| 160 |
|
| 161 |
if ( png_sig_cmp( buffer, 0, 8 ) != 0 ) { |
| 162 |
Sys_FPrintf( SYS_WRN, "WARNING: Invalid PNG file\n" ); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 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 |
|
| 188 |
pb.buffer = buffer; |
| 189 |
pb.size = size; |
| 190 |
pb.offset = 0; |
| 191 |
png_set_read_fn( png, &pb, PNGReadData ); |
| 192 |
|
| 193 |
|
| 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 |
|
| 201 |
|
| 202 |
|
| 203 |
png_read_info( png, info ); |
| 204 |
|
| 205 |
|
| 206 |
png_get_IHDR( png, info, |
| 207 |
&w, &h, &bitDepth, &colorType, NULL, NULL, NULL ); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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 |
|
| 219 |
if ( bitDepth == 16 ) { |
| 220 |
png_set_strip_16( png ); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if ( bitDepth == 8 && colorType == PNG_COLOR_TYPE_RGB ) { |
| 225 |
png_set_filler( png, 255, PNG_FILLER_AFTER ); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
*width = w; |
| 230 |
*height = h; |
| 231 |
|
| 232 |
*pixels = safe_malloc0( w * h * 4 ); |
| 233 |
|
| 234 |
|
| 235 |
rowPointers = safe_malloc( h * sizeof( byte* ) ); |
| 236 |
for ( i = 0; i < h; i++ ) |
| 237 |
rowPointers[ i ] = *pixels + ( i * w * 4 ); |
| 238 |
|
| 239 |
|
| 240 |
png_read_image( png, rowPointers ); |
| 241 |
|
| 242 |
|
| 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 |
|
| 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 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
static void ImageInit( void ){ |
| 283 |
int i; |
| 284 |
|
| 285 |
|
| 286 |
if ( numImages <= 0 ) { |
| 287 |
|
| 288 |
memset( images, 0, sizeof( images ) ); |
| 289 |
|
| 290 |
|
| 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 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
void ImageFree( image_t *image ){ |
| 312 |
|
| 313 |
if ( image == NULL ) { |
| 314 |
return; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
image->refCount--; |
| 319 |
|
| 320 |
|
| 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 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
image_t *ImageFind( const char *filename ){ |
| 345 |
int i; |
| 346 |
char name[ 1024 ]; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
ImageInit(); |
| 351 |
|
| 352 |
|
| 353 |
if ( filename == NULL || filename[ 0 ] == '\0' ) { |
| 354 |
return NULL; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
strcpy( name, filename ); |
| 359 |
StripExtension( name ); |
| 360 |
|
| 361 |
|
| 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 |
|
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 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 |
|
| 390 |
ImageInit(); |
| 391 |
|
| 392 |
|
| 393 |
if ( filename == NULL || filename[ 0 ] == '\0' ) { |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
for ( i = 0; i < 2; i++ ) |
| 402 |
{ |
| 403 |
strcpy( name, filename ); |
| 404 |
|
| 405 |
if ( i == 0 ) |
| 406 |
{ |
| 407 |
|
| 408 |
StripExtension( name ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
image = ImageFind( name ); |
| 413 |
if ( image != NULL ) { |
| 414 |
image->refCount++; |
| 415 |
return image; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 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 |
|
| 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 |
|
| 434 |
image->name = safe_malloc( strlen( name ) + 1 ); |
| 435 |
strcpy( image->name, name ); |
| 436 |
|
| 437 |
|
| 438 |
strcat( name, ".tga" ); |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 467 |
Sys_FPrintf( SYS_WRN, "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels ); |
| 468 |
} |
| 469 |
alphaHack = qtrue; |
| 470 |
break; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
StripExtension( name ); |
| 475 |
strcat( name, ".dds" ); |
| 476 |
size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 ); |
| 477 |
|
| 478 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 522 |
free( buffer ); |
| 523 |
|
| 524 |
|
| 525 |
if ( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL ) { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
free( image->name ); |
| 530 |
image->name = NULL; |
| 531 |
return NULL; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
Sys_FPrintf( SYS_VRB, "Loaded image: \"%s\"\n", name ); |
| 536 |
|
| 537 |
|
| 538 |
image->filename = safe_malloc( strlen( name ) + 1 ); |
| 539 |
strcpy( image->filename, name ); |
| 540 |
|
| 541 |
|
| 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 |
|
| 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]; |
| 562 |
} |
| 563 |
free( pixels ); |
| 564 |
} |
| 565 |
free( buffer ); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
return image; |
| 571 |
} |