| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/bitmap/tga.cpp $ |
| 21 | * $Revision: 28 $ |
| 22 | * $Date: 9/06/01 10:25a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Functions to read TGA files |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/bitmap/tga.cpp $ |
| 28 | * |
| 29 | * 28 9/06/01 10:25a Matt |
| 30 | * Took out annoying Int3 |
| 31 | * |
| 32 | * 27 2/01/00 2:37a Jason |
| 33 | * Fixed problem with bad tga files |
| 34 | * |
| 35 | * 26 10/21/99 9:28p Jeff |
| 36 | * B.A. Macintosh code merge |
| 37 | * |
| 38 | * 25 4/14/99 1:07a Jeff |
| 39 | * fixed case mismatched #includes |
| 40 | * |
| 41 | * 24 2/16/99 12:42p Kevin |
| 42 | * Improvements to the paging data progress indicator |
| 43 | * |
| 44 | * 23 2/09/99 7:01p Kevin |
| 45 | * First work for new and improved progress screen while loading a level. |
| 46 | * Note that this is a hack at this point, while I get the details worked |
| 47 | * out, then I'll make it cleaner. |
| 48 | * |
| 49 | * 22 1/14/99 2:26p Jason |
| 50 | * made data tracking more reliable |
| 51 | * |
| 52 | * 21 12/18/98 3:16p Jason |
| 53 | * added specularity to mine walls |
| 54 | * |
| 55 | * 20 10/20/98 1:04p Jason |
| 56 | * fixed import problem |
| 57 | * |
| 58 | * 19 10/19/98 4:22p Jason |
| 59 | * more fixes for Beta5 |
| 60 | * |
| 61 | * 18 10/09/98 3:32p Kevin |
| 62 | * New memory library |
| 63 | * |
| 64 | * 17 10/08/98 4:23p Kevin |
| 65 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 66 | * , mem_free and mem_strdup |
| 67 | * |
| 68 | * 16 8/14/98 2:55p Jason |
| 69 | * fixed reading of 24 bit TGA files |
| 70 | * |
| 71 | * 15 5/24/98 1:52a Jeff |
| 72 | * no changes, checked out and checked back in so people would recompile |
| 73 | * it, because it is now to be compiled optimized |
| 74 | * |
| 75 | * 14 4/23/98 6:38p Jason |
| 76 | * made bitmaps use 1555 format |
| 77 | * |
| 78 | * 13 3/31/98 3:48p Jason |
| 79 | * added memory lib |
| 80 | * |
| 81 | * 12 2/12/98 1:32p Jason |
| 82 | * got mipmapping working |
| 83 | * |
| 84 | * 11 1/12/98 3:00p Keneta |
| 85 | * Changed assert to Int3 (Jason on MattL's machine) |
| 86 | * |
| 87 | * 10 12/31/97 11:11a Samir |
| 88 | * FROM JASON: Added the ability to read NEW_COMPRESS_MIPPED |
| 89 | * |
| 90 | * 9 12/22/97 7:34p Samir |
| 91 | * Removed instances of gr.h include. Replaced with grdefs.h |
| 92 | * |
| 93 | * 8 12/22/97 3:25p Keneta |
| 94 | * fixed bug with new texture paging |
| 95 | * |
| 96 | * 7 12/19/97 3:36p Jason |
| 97 | * bug fixes for bitmap paging stuff |
| 98 | * |
| 99 | * 6 12/19/97 2:46p Jason |
| 100 | * implemented bitmap paging routines |
| 101 | * |
| 102 | * 5 11/03/97 6:21p Jason |
| 103 | * fixed some problems with animating textures being remapped improperly |
| 104 | * |
| 105 | * 4 10/16/97 4:55p Jason |
| 106 | * fixed stupid off-by-one bug |
| 107 | * |
| 108 | * 3 10/15/97 5:20p Jason |
| 109 | * did a HUGE overhaul of the bitmap system |
| 110 | * |
| 111 | * 2 8/29/97 1:22p Jason |
| 112 | * added tga screenshots |
| 113 | * |
| 114 | * 13 5/29/97 6:21p Jason |
| 115 | * made tga's better handle pixmap info |
| 116 | * |
| 117 | * 12 5/19/97 5:10p Jason |
| 118 | * changes for our new abstracted renderer code |
| 119 | * |
| 120 | * 11 4/30/97 3:15p Jason |
| 121 | * changes to support both 8bit and 16bit rendering in software |
| 122 | * |
| 123 | * 10 3/07/97 4:03p Samir |
| 124 | * Took out malloc.h for ANSI compliance. |
| 125 | * |
| 126 | * 9 3/07/97 1:02p Jason |
| 127 | * added the ability to read 24 bit tgas as well |
| 128 | * |
| 129 | * 8 3/03/97 6:20p Matt |
| 130 | * Changed cfile functions to use D3 naming convention |
| 131 | * |
| 132 | * $NoKeywords: $ |
| 133 | */ |
| 134 | |
| 135 | #include "cfile.h" |
| 136 | #include "pserror.h" |
| 137 | #include "pstypes.h" |
| 138 | #include "bitmap.h" |
| 139 | #include "mono.h" |
| 140 | #include "grdefs.h" |
| 141 | #include "byteswap.h" |
| 142 | #include <string.h> |
| 143 | #include "mem.h" |
| 144 | #include "gamesequence.h" |
| 145 | |
| 146 | #include <stdlib.h> |
| 147 | |
| 148 | static char *Tga_file_data = NULL; |
| 149 | static int Fake_pos = 0; |
| 150 | static int Bad_tga = 0; |
| 151 | static int Fake_file_size = 0; |
| 152 | |
| 153 | static inline char tga_read_byte(); |
| 154 | static inline int tga_read_int(); |
| 155 | static inline int16_t tga_read_short(); |
| 156 | static uint16_t bm_tga_translate_pixel(int pixel, int format); |
| 157 | static int bm_tga_read_outrage_compressed16(CFILE *infile, int n, int num_mips, int type); |
| 158 | |
| 159 | inline char tga_read_byte() { |
| 160 | // Check for bad file |
| 161 | if (Fake_pos + 1 > Fake_file_size) { |
| 162 | Bad_tga = 1; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | return Tga_file_data[Fake_pos++]; |
| 167 | } |
| 168 | inline int tga_read_int() { |
| 169 | int i; |
| 170 | |
| 171 | // Check for bad file |
| 172 | if (Fake_pos + 4 > Fake_file_size) { |
| 173 | Bad_tga = 1; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | i = *(int *)(Tga_file_data + Fake_pos); |
| 178 | Fake_pos += 4; |
| 179 | |
| 180 | return INTEL_INT(i); |
| 181 | } |
| 182 | inline int16_t tga_read_short() { |
| 183 | int16_t i; |
| 184 | |
| 185 | // Check for bad file |
| 186 | if (Fake_pos + 2 > Fake_file_size) { |
| 187 | Bad_tga = 1; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | i = *(int16_t *)(Tga_file_data + Fake_pos); |
| 192 | Fake_pos += 2; |
| 193 | |
| 194 | return INTEL_SHORT(i); |
| 195 | } |
| 196 | |
| 197 | uint16_t bm_tga_translate_pixel(int pixel, int format) { |
| 198 | int red = ((pixel >> 16) & 0xFF); |
| 199 | int green = ((pixel >> 8) & 0xFF); |
| 200 | int blue = ((pixel) & 0xFF); |
| 201 | int alpha = ((pixel >> 24) & 0xFF); |
| 202 | uint16_t newpix; |
| 203 | |
| 204 | if (format == BITMAP_FORMAT_4444) { |
| 205 | int newred = red >> 4; |
| 206 | int newgreen = green >> 4; |
| 207 | int newblue = blue >> 4; |
| 208 | int newalpha = alpha >> 4; |
| 209 | newpix = (newalpha << 12) | (newred << 8) | (newgreen << 4) | (newblue); |
| 210 | } else { |
| 211 | int newred = red >> 3; |
| 212 | int newgreen = green >> 3; |
| 213 | int newblue = blue >> 3; |
| 214 | |
| 215 | newpix = OPAQUE_FLAG | (newred << 10) | (newgreen << 5) | (newblue); |
| 216 | |
| 217 | if (alpha == 0) |
| 218 | newpix = NEW_TRANSPARENT_COLOR; |
| 219 | } |
| 220 | |
| 221 | return newpix; |
| 222 | } |
| 223 | |
| 224 | int bm_tga_read_outrage_compressed16(CFILE *infile, int n, int num_mips, int type) { |
| 225 | uint16_t *dest_data; |
| 226 | uint16_t pixel; |
| 227 | int width, height; |
| 228 | int m; |
| 229 | |
| 230 | for (m = 0; m < num_mips; m++) { |
| 231 | |
| 232 | width = bm_w(n, m); |
| 233 | height = bm_h(n, m); |
| 234 | |
| 235 | int total = height * width; |
| 236 | int count = 0; |
| 237 | |
| 238 | dest_data = (uint16_t *)bm_data(n, m); |
| 239 | |
| 240 | while (count != total) { |
| 241 | ASSERT(count < total); |
| 242 | |
| 243 | uint8_t command = tga_read_byte(); |
| 244 | |
| 245 | if (Bad_tga) |
| 246 | return 0; |
| 247 | |
| 248 | if (command == 0) // next pixel is raw |
| 249 | { |
| 250 | pixel = tga_read_short(); |
| 251 | |
| 252 | if (Bad_tga) |
| 253 | return 0; |
| 254 | |
| 255 | if (type != OUTRAGE_1555_COMPRESSED_MIPPED && type != OUTRAGE_4444_COMPRESSED_MIPPED) { |
| 256 | if (pixel == 0x07e0) |
| 257 | pixel = NEW_TRANSPARENT_COLOR; |
| 258 | else { |
| 259 | int r = ((pixel & 0xF800) >> 11) << 3; |
| 260 | int g = ((pixel & 0x07e0) >> 5) << 2; |
| 261 | int b = (pixel & 0x001f) << 3; |
| 262 | |
| 263 | pixel = OPAQUE_FLAG | GR_RGB16(r, g, b); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | int i = count / width; |
| 268 | int t = count % width; |
| 269 | dest_data[i * width + t] = pixel; |
| 270 | count++; |
| 271 | } else if (command >= 2 && command <= 250) // next pixel is run of pixels |
| 272 | { |
| 273 | pixel = tga_read_short(); |
| 274 | |
| 275 | if (Bad_tga) |
| 276 | return 0; |
| 277 | |
| 278 | if (type != OUTRAGE_1555_COMPRESSED_MIPPED && type != OUTRAGE_4444_COMPRESSED_MIPPED) { |
| 279 | if (pixel == 0x07e0) |
| 280 | pixel = NEW_TRANSPARENT_COLOR; |
| 281 | else { |
| 282 | int r = ((pixel & 0xF800) >> 11) << 3; |
| 283 | int g = ((pixel & 0x07e0) >> 5) << 2; |
| 284 | int b = (pixel & 0x001f) << 3; |
| 285 | |
| 286 | pixel = OPAQUE_FLAG | GR_RGB16(r, g, b); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | for (int k = 0; k < command; k++) { |
| 291 | int i = count / width; |
| 292 | int t = count % width; |
| 293 | dest_data[i * width + t] = pixel; |
| 294 | count++; |
| 295 | } |
| 296 | } else |
| 297 | Int3(); // bad compression run |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // DAJ added to fill out the mip maps down to the 1x1 size (memory is already there) |
| 302 | // does not average since we are only a pixel or two in size |
| 303 | if (num_mips > 1) { |
| 304 | GameBitmaps[n].mip_levels = bm_miplevels(n); |
| 305 | for (m = num_mips; m < bm_miplevels(n); m++) { |
| 306 | width = bm_w(n, m); |
| 307 | height = bm_h(n, m); |
| 308 | |
| 309 | uint16_t w_prev = bm_w(n, m - 1); |
| 310 | uint16_t *dst = bm_data(n, m); |
| 311 | uint16_t *src = bm_data(n, m - 1); |
| 312 | |
| 313 | for (int h_inc = 0; h_inc < height; h_inc++) { |
| 314 | for (int w_inc = 0; w_inc < width; w_inc++) { |
| 315 | dst[h_inc * width + w_inc] = src[2 * (h_inc * w_prev + w_inc)]; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | // Loads a tga or ogf file into a bitmap...returns handle to bm or -1 on error |
| 325 | int bm_tga_alloc_file(CFILE *infile, char *name, int format) { |
| 326 | uint8_t image_id_len, color_map_type, image_type, pixsize, descriptor; |
| 327 | uint8_t upside_down = 0; |
| 328 | uint16_t width, height; |
| 329 | uint32_t pixel; |
| 330 | int i, t, n, data8bit = 0, savepos = 0; |
| 331 | int mipped = 0; |
| 332 | int num_mips = 1; |
| 333 | int read_ok = 1; |
| 334 | |
| 335 | image_id_len = cf_ReadByte(infile); |
| 336 | color_map_type = cf_ReadByte(infile); |
| 337 | image_type = cf_ReadByte(infile); |
| 338 | |
| 339 | if (color_map_type != 0 || |
| 340 | (image_type != 10 && image_type != 2 && image_type != OUTRAGE_TGA_TYPE && image_type != OUTRAGE_COMPRESSED_OGF && |
| 341 | image_type != OUTRAGE_COMPRESSED_MIPPED && image_type != OUTRAGE_NEW_COMPRESSED_MIPPED && |
| 342 | image_type != OUTRAGE_1555_COMPRESSED_MIPPED && image_type != OUTRAGE_4444_COMPRESSED_MIPPED)) { |
| 343 | mprintf(0, "bm_tga: Can't read this type of TGA.\n"); |
| 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | if (image_type == OUTRAGE_COMPRESSED_OGF_8BIT) |
| 348 | data8bit = 1; |
| 349 | |
| 350 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 351 | image_type == OUTRAGE_NEW_COMPRESSED_MIPPED || image_type == OUTRAGE_TGA_TYPE || |
| 352 | image_type == OUTRAGE_COMPRESSED_MIPPED || image_type == OUTRAGE_COMPRESSED_OGF || |
| 353 | image_type == OUTRAGE_COMPRESSED_OGF_8BIT) { |
| 354 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_NEW_COMPRESSED_MIPPED || |
| 355 | image_type == OUTRAGE_1555_COMPRESSED_MIPPED) { |
| 356 | cf_ReadString(name, BITMAP_NAME_LEN - 1, infile); |
| 357 | } else { |
| 358 | for (i = 0; i < BITMAP_NAME_LEN; i++) |
| 359 | name[i] = cf_ReadByte(infile); |
| 360 | } |
| 361 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 362 | image_type == OUTRAGE_COMPRESSED_MIPPED || image_type == OUTRAGE_NEW_COMPRESSED_MIPPED) |
| 363 | num_mips = cf_ReadByte(infile); |
| 364 | else |
| 365 | num_mips = 1; |
| 366 | |
| 367 | if (num_mips > 1) |
| 368 | mipped = 1; |
| 369 | } |
| 370 | |
| 371 | for (i = 0; i < 9; i++) // ingore next 9 bytes |
| 372 | cf_ReadByte(infile); |
| 373 | |
| 374 | width = cf_ReadShort(infile); |
| 375 | height = cf_ReadShort(infile); |
| 376 | pixsize = cf_ReadByte(infile); |
| 377 | |
| 378 | if (pixsize != 32 && pixsize != 24) { |
| 379 | mprintf(0, "bm_tga: This file has a pixsize of field of %d, it should be 32. ", pixsize); |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | descriptor = cf_ReadByte(infile); |
| 384 | if (((descriptor & 0x0F) != 8) && ((descriptor & 0x0F) != 0)) { |
| 385 | mprintf(0, "bm_tga: Descriptor field & 0x0F must be 8 or 0, but this is %d.", descriptor & 0x0F); |
| 386 | return -1; |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < image_id_len; i++) |
| 390 | cf_ReadByte(infile); |
| 391 | |
| 392 | n = bm_AllocBitmap(width, height, mipped * ((width * height * 2) / 3)); |
| 393 | |
| 394 | if (format == BITMAP_FORMAT_4444 || image_type == OUTRAGE_4444_COMPRESSED_MIPPED) |
| 395 | GameBitmaps[n].format = BITMAP_FORMAT_4444; |
| 396 | |
| 397 | // Copy the name |
| 398 | strcpy(GameBitmaps[n].name, name); |
| 399 | |
| 400 | if (mipped) |
| 401 | GameBitmaps[n].flags |= BF_MIPMAPPED; |
| 402 | |
| 403 | if (n < 0) { |
| 404 | mprintf(0, "bm_tga: Failed to allocate memory.\n"); |
| 405 | Int3(); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | upside_down = (descriptor & 0x20) >> 5; |
| 410 | upside_down = 1 - upside_down; |
| 411 | |
| 412 | // Load the actual bitmap data in, converting it from 32 bit to 16 bit, and replacing |
| 413 | // that pesky transparency color without our replacement |
| 414 | |
| 415 | if (image_type == 10 || image_type == 2) { |
| 416 | if (image_type == 10) // compressed tga |
| 417 | { |
| 418 | int total = 0; |
| 419 | |
| 420 | while (total < (height * width)) { |
| 421 | uint8_t command = cf_ReadByte(infile); |
| 422 | uint8_t len = (command & 127) + 1; |
| 423 | |
| 424 | if (command & 128) // rle chunk |
| 425 | { |
| 426 | if (pixsize == 32) |
| 427 | pixel = cf_ReadInt(infile); |
| 428 | else { |
| 429 | int r, g, b; |
| 430 | r = cf_ReadByte(infile); |
| 431 | g = cf_ReadByte(infile); |
| 432 | b = cf_ReadByte(infile); |
| 433 | pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
| 434 | } |
| 435 | |
| 436 | uint16_t newpix = bm_tga_translate_pixel(pixel, format); |
| 437 | |
| 438 | for (int k = 0; k < len; k++, total++) { |
| 439 | i = total / width; |
| 440 | t = total % width; |
| 441 | |
| 442 | if (upside_down) |
| 443 | GameBitmaps[n].data16[((height - 1) - i) * width + t] = newpix; |
| 444 | else |
| 445 | GameBitmaps[n].data16[i * width + t] = newpix; |
| 446 | } |
| 447 | } else // raw chunk |
| 448 | { |
| 449 | for (int k = 0; k < len; k++, total++) { |
| 450 | if (pixsize == 32) |
| 451 | pixel = cf_ReadInt(infile); |
| 452 | else { |
| 453 | int r, g, b; |
| 454 | b = (uint8_t)cf_ReadByte(infile); |
| 455 | g = (uint8_t)cf_ReadByte(infile); |
| 456 | r = (uint8_t)cf_ReadByte(infile); |
| 457 | pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
| 458 | } |
| 459 | uint16_t newpix = bm_tga_translate_pixel(pixel, format); |
| 460 | |
| 461 | i = total / width; |
| 462 | t = total % width; |
| 463 | |
| 464 | if (upside_down) |
| 465 | GameBitmaps[n].data16[((height - 1) - i) * width + t] = newpix; |
| 466 | else |
| 467 | GameBitmaps[n].data16[i * width + t] = newpix; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } else // uncompressed TGA |
| 472 | { |
| 473 | for (i = 0; i < height; i++) { |
| 474 | for (t = 0; t < width; t++) { |
| 475 | if (pixsize == 32) |
| 476 | pixel = cf_ReadInt(infile); |
| 477 | else { |
| 478 | int r, g, b; |
| 479 | b = (uint8_t)cf_ReadByte(infile); |
| 480 | g = (uint8_t)cf_ReadByte(infile); |
| 481 | r = (uint8_t)cf_ReadByte(infile); |
| 482 | pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
| 483 | } |
| 484 | |
| 485 | uint16_t newpix = bm_tga_translate_pixel(pixel, format); |
| 486 | |
| 487 | if (upside_down) |
| 488 | GameBitmaps[n].data16[((height - 1) - i) * width + t] = newpix; |
| 489 | else |
| 490 | GameBitmaps[n].data16[i * width + t] = newpix; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } else if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 495 | image_type == OUTRAGE_NEW_COMPRESSED_MIPPED || image_type == OUTRAGE_COMPRESSED_MIPPED || |
| 496 | image_type == OUTRAGE_COMPRESSED_OGF || image_type == OUTRAGE_COMPRESSED_OGF_8BIT) // COMPRESSED OGF |
| 497 | { |
| 498 | // read this ogf in all at once (much faster) |
| 499 | |
| 500 | savepos = cftell(infile); |
| 501 | cfseek(infile, 0, SEEK_END); |
| 502 | int lastpos = cftell(infile); |
| 503 | int numleft = lastpos - savepos; |
| 504 | |
| 505 | cfseek(infile, savepos, SEEK_SET); |
| 506 | |
| 507 | Tga_file_data = (char *)mem_malloc(numleft); |
| 508 | ASSERT(Tga_file_data != NULL); |
| 509 | Fake_pos = 0; |
| 510 | Bad_tga = 0; |
| 511 | Fake_file_size = numleft; |
| 512 | |
| 513 | cf_ReadBytes((uint8_t *)Tga_file_data, numleft, infile); |
| 514 | |
| 515 | read_ok = bm_tga_read_outrage_compressed16(infile, n, num_mips, image_type); |
| 516 | } |
| 517 | |
| 518 | else |
| 519 | Int3(); // Get Jason |
| 520 | |
| 521 | if (image_type == OUTRAGE_COMPRESSED_OGF_8BIT) |
| 522 | Int3(); |
| 523 | // bm_tga_read_outrage_compressed8 (infile,n); |
| 524 | |
| 525 | if (Tga_file_data != NULL) { |
| 526 | mem_free(Tga_file_data); |
| 527 | Tga_file_data = NULL; |
| 528 | cfseek(infile, savepos + Fake_pos, SEEK_SET); |
| 529 | } |
| 530 | |
| 531 | if (!read_ok) |
| 532 | return -1; |
| 533 | else |
| 534 | return (n); |
| 535 | } |
| 536 | |
| 537 | // Pages in bitmap index n. Returns 1 if successful, 0 if not |
| 538 | int bm_page_in_file(int n) { |
| 539 | uint8_t image_id_len, color_map_type, image_type, pixsize, descriptor; |
| 540 | uint8_t upside_down = 0; |
| 541 | uint16_t width, height; |
| 542 | int i, data8bit = 0, savepos = 0; |
| 543 | int mipped = 0, file_mipped = 0; |
| 544 | int num_mips = 1; |
| 545 | char name[BITMAP_NAME_LEN]; |
| 546 | CFILE *infile; |
| 547 | |
| 548 | ASSERT((GameBitmaps[n].flags & BF_NOT_RESIDENT)); |
| 549 | |
| 550 | infile = (CFILE *)cfopen(GameBitmaps[n].name, "rb"); |
| 551 | if (!infile) { |
| 552 | mprintf(0, "Couldn't page in bitmap %s!\n", GameBitmaps[n].name); |
| 553 | return 0; |
| 554 | } |
| 555 | // Used for progress bar when loading the level |
| 556 | paged_in_count += cfilelength(infile); |
| 557 | paged_in_num++; |
| 558 | image_id_len = cf_ReadByte(infile); |
| 559 | color_map_type = cf_ReadByte(infile); |
| 560 | image_type = cf_ReadByte(infile); |
| 561 | |
| 562 | if (color_map_type != 0 || |
| 563 | (image_type != 10 && image_type != 2 && image_type != OUTRAGE_TGA_TYPE && image_type != OUTRAGE_COMPRESSED_OGF && |
| 564 | image_type != OUTRAGE_COMPRESSED_MIPPED && image_type != OUTRAGE_NEW_COMPRESSED_MIPPED && |
| 565 | image_type != OUTRAGE_1555_COMPRESSED_MIPPED && image_type != OUTRAGE_4444_COMPRESSED_MIPPED)) { |
| 566 | mprintf(0, "bm_tga: Can't read this type of TGA.\n"); |
| 567 | return -1; |
| 568 | } |
| 569 | |
| 570 | if (image_type == OUTRAGE_COMPRESSED_OGF_8BIT) |
| 571 | data8bit = 1; |
| 572 | |
| 573 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 574 | image_type == OUTRAGE_NEW_COMPRESSED_MIPPED || image_type == OUTRAGE_TGA_TYPE || |
| 575 | image_type == OUTRAGE_COMPRESSED_MIPPED || image_type == OUTRAGE_COMPRESSED_OGF || |
| 576 | image_type == OUTRAGE_COMPRESSED_OGF_8BIT) { |
| 577 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 578 | image_type == OUTRAGE_NEW_COMPRESSED_MIPPED) { |
| 579 | cf_ReadString(name, BITMAP_NAME_LEN - 1, infile); |
| 580 | } else { |
| 581 | for (i = 0; i < BITMAP_NAME_LEN; i++) |
| 582 | name[i] = cf_ReadByte(infile); |
| 583 | } |
| 584 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 585 | image_type == OUTRAGE_COMPRESSED_MIPPED || image_type == OUTRAGE_NEW_COMPRESSED_MIPPED) |
| 586 | num_mips = cf_ReadByte(infile); |
| 587 | else |
| 588 | num_mips = 1; |
| 589 | |
| 590 | if (num_mips > 1) |
| 591 | file_mipped = 1; |
| 592 | } |
| 593 | |
| 594 | for (i = 0; i < 9; i++) // ingore next 9 bytes |
| 595 | cf_ReadByte(infile); |
| 596 | |
| 597 | width = cf_ReadShort(infile); |
| 598 | height = cf_ReadShort(infile); |
| 599 | pixsize = cf_ReadByte(infile); |
| 600 | |
| 601 | if (pixsize != 32 && pixsize != 24) { |
| 602 | mprintf(0, "bm_tga: This file has a pixsize of field of %d, it should be 32. ", pixsize); |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | descriptor = cf_ReadByte(infile); |
| 607 | if (((descriptor & 0x0F) != 8) && ((descriptor & 0x0F) != 0)) { |
| 608 | mprintf(0, "bm_tga: Descriptor field & 0x0F must be 8 or 0, but this is %d.", descriptor & 0x0F); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | for (i = 0; i < image_id_len; i++) |
| 613 | cf_ReadByte(infile); |
| 614 | |
| 615 | if ((GameBitmaps[n].flags & BF_WANTS_MIP) || file_mipped) |
| 616 | mipped = 1; |
| 617 | |
| 618 | int size = (width * height * 2) + (mipped * ((width * height * 2) / 3)) + 2; |
| 619 | GameBitmaps[n].data16 = (uint16_t *)mem_malloc(size); |
| 620 | if (!GameBitmaps[n].data16) { |
| 621 | mprintf(0, "Out of memory in bm_page_in_file!\n"); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | Bitmap_memory_used += size; |
| 626 | |
| 627 | if ((GameBitmaps[n].flags & BF_WANTS_4444) || image_type == OUTRAGE_4444_COMPRESSED_MIPPED) |
| 628 | GameBitmaps[n].format = BITMAP_FORMAT_4444; |
| 629 | else |
| 630 | GameBitmaps[n].format = BITMAP_FORMAT_STANDARD; |
| 631 | |
| 632 | GameBitmaps[n].width = width; |
| 633 | GameBitmaps[n].height = height; |
| 634 | GameBitmaps[n].flags &= ~BF_NOT_RESIDENT; |
| 635 | |
| 636 | // Copy the name |
| 637 | // if ((stricmp(GameBitmaps[n].name,name))) |
| 638 | // Int3(); //Get Jason! |
| 639 | |
| 640 | strcpy(GameBitmaps[n].name, name); |
| 641 | |
| 642 | mprintf(0, "Paging in bitmap %s!\n", GameBitmaps[n].name); |
| 643 | |
| 644 | if (file_mipped) |
| 645 | GameBitmaps[n].flags |= BF_MIPMAPPED; |
| 646 | |
| 647 | upside_down = (descriptor & 0x20) >> 5; |
| 648 | upside_down = 1 - upside_down; |
| 649 | |
| 650 | // Load the actual bitmap data in, converting it from 32 bit to 16 bit, and replacing |
| 651 | // that pesky transparency color without our replacement |
| 652 | if (image_type == OUTRAGE_4444_COMPRESSED_MIPPED || image_type == OUTRAGE_1555_COMPRESSED_MIPPED || |
| 653 | image_type == OUTRAGE_NEW_COMPRESSED_MIPPED || image_type == OUTRAGE_COMPRESSED_MIPPED || |
| 654 | image_type == OUTRAGE_COMPRESSED_OGF || image_type == OUTRAGE_COMPRESSED_OGF_8BIT) // COMPRESSED OGF |
| 655 | { |
| 656 | // read this ogf in all at once (much faster) |
| 657 | |
| 658 | savepos = cftell(infile); |
| 659 | cfseek(infile, 0, SEEK_END); |
| 660 | int lastpos = cftell(infile); |
| 661 | int numleft = lastpos - savepos; |
| 662 | |
| 663 | cfseek(infile, savepos, SEEK_SET); |
| 664 | |
| 665 | Tga_file_data = (char *)mem_malloc(numleft); |
| 666 | ASSERT(Tga_file_data != NULL); |
| 667 | Fake_pos = 0; |
| 668 | Bad_tga = 0; |
| 669 | Fake_file_size = numleft; |
| 670 | |
| 671 | cf_ReadBytes((uint8_t *)Tga_file_data, numleft, infile); |
| 672 | |
| 673 | bm_tga_read_outrage_compressed16(infile, n, num_mips, image_type); |
| 674 | } |
| 675 | |
| 676 | else |
| 677 | Int3(); // Get Jason |
| 678 | |
| 679 | if (image_type == OUTRAGE_COMPRESSED_OGF_8BIT) |
| 680 | Int3(); |
| 681 | // bm_tga_read_outrage_compressed8 (infile,n); |
| 682 | |
| 683 | if (Tga_file_data != NULL) { |
| 684 | mem_free(Tga_file_data); |
| 685 | Tga_file_data = NULL; |
| 686 | cfseek(infile, savepos + Fake_pos, SEEK_SET); |
| 687 | } |
| 688 | |
| 689 | cfclose(infile); |
| 690 | |
| 691 | if ((GameBitmaps[n].flags & BF_WANTS_MIP) && !file_mipped) { |
| 692 | bm_GenerateMipMaps(n); |
| 693 | } |
| 694 | |
| 695 | return 1; |
| 696 | } |