Debian Codegraph @

bitmap/bitmain.cpp

raw symbols SCIP
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/bitmain.cpp $
21 * $Revision: 70 $
22 * $Date: 5/10/00 5:09p $
23 * $Author: Matt $
24 *
25 * Bitmap handling functions
26 *
27 * $Log: /DescentIII/Main/bitmap/bitmain.cpp $
28 *
29 * 70 5/10/00 5:09p Matt
30 * Added casts so code would compile under Visual C++.
31 *
32 * 69 5/10/00 4:34p Jeff
33 * handle buggy tablefile editors that put the full path name to a
34 * primative instead of just the filename
35 *
36 * 68 4/19/00 5:29p Matt
37 * From Duane for 1.4
38 * Added Mac-only error handling
39 * Added checks & asserts for error return values
40 *
41 * 67 4/04/00 6:09p Matt
42 * Fixed two problems in the hash code: the hash values were
43 * case-sensitive, which caused misses with mixed-case filenames, and hash
44 * table nodes were not freed when a bitmap was freed.
45 *
46 * 66 3/20/00 12:23p Matt
47 * Merge of Duane's post-1.3 changes.
48 * Small optimization
49 * Error check
50 *
51 * 65 2/06/00 3:41a Jason
52 * fixed memory overrun error in bm_ChangeEndName function
53 *
54 * 64 10/21/99 9:27p Jeff
55 * B.A. Macintosh code merge
56 *
57 * 63 8/10/99 5:10p Jeff
58 * delete the hash table on bitmap lib shutdown...just to be clean
59 *
60 * 62 7/28/99 5:18p Kevin
61 * Mac merge fixes
62 *
63 * 61 7/28/99 3:37p Kevin
64 * Mac!
65 *
66 * 60 4/29/99 4:59p Jason
67 * fixed some preuploaded texture problems
68 *
69 * 59 4/23/99 10:45p Jeff
70 * fixed crash with freeing hash table, set it to NULL to prevent double
71 * free
72 *
73 * 58 4/21/99 11:05a Kevin
74 * new ps_rand and ps_srand to replace rand & srand
75 *
76 * 57 4/16/99 11:51p Jeff
77 * renamed strcmpi to stricmp to be linux nice
78 *
79 * 56 4/15/99 2:56p Kevin
80 * Removed unneeded mprintf and unused variable
81 *
82 * 55 4/15/99 12:36p Kevin
83 * Changed bm_FindBitmapName to use a hash table.
84 *
85 * 54 4/14/99 1:07a Jeff
86 * fixed case mismatched #includes
87 *
88 * 53 3/23/99 10:24a Samir
89 * NULL pointer in bm_DestroyChunkedBitmap.
90 *
91 * 52 2/12/99 12:14p Jason
92 * fixed memory scaling problem
93 *
94 * 51 1/19/99 11:17a Jason
95 * fixed another scaling problem
96 *
97 * 50 1/14/99 2:26p Jason
98 * made data tracking more reliable
99 *
100 * 49 1/04/99 6:19p Jason
101 * fixed bitmap format problem
102 *
103 * 48 12/21/98 11:47a Jason
104 * fixed bitmap problem with 4444 mipping
105 *
106 * 47 12/21/98 11:21a Josh
107 * FROM JASON: Fixed format/mip problem
108 *
109 * 46 11/30/98 5:50p Jason
110 * added 4444 bitmap support
111 *
112 * 45 10/21/98 4:36p Jason
113 * more changes for renderering speedups
114 *
115 * 44 10/19/98 4:22p Jason
116 * more fixes for Beta5
117 *
118 * 43 10/08/98 2:27p Jason
119 * sped up table file loading
120 *
121 * 42 9/25/98 12:01p Samir
122 * for ndebug builds, removed the return immediately case in
123 * bm_AllocLoadFileBitmap.
124 *
125 * 41 8/21/98 5:14p Jason
126 * made better memory use of primitives
127 *
128 * 40 7/14/98 10:58a Jason
129 * fixed transparency bug in error texture
130 *
131 * 39 5/27/98 5:17p Jason
132 * fixed some bugs for the E3 Demo
133 *
134 * 38 5/20/98 5:43p Jason
135 * incremental checkin for bumpmapping
136 *
137 * 37 5/07/98 3:30p Jeff
138 * optimized CreateChunkBitmap a bit
139 *
140 * 36 5/05/98 1:01p Jeff
141 * improved on Chunk bitmaps
142 *
143 * 35 4/23/98 6:38p Jason
144 * made bitmaps use 1555 format
145 *
146 * 34 4/22/98 12:10p Chris
147 * Fixed path length problems
148 *
149 * 33 4/03/98 12:23p Jason
150 * dealt with overlay types being loaded from disk more than once
151 *
152 * 32 3/31/98 3:48p Jason
153 * added memory lib
154 *
155 * 31 3/23/98 4:42p Jason
156 * took out dumb ifdef
157 *
158 * 30 3/19/98 3:18p Samir
159 * enforce constant char* arguments when needed. done in CFILE and bitmap
160 * libraries as well as ddio.
161 *
162 * 29 3/17/98 4:33p Jason
163 * Added size changing to bitmaps/textures
164 *
165 * 28 2/17/98 4:57p Jason
166 * upped bitmap counts
167 *
168 * 27 2/13/98 7:23p Brent
169 * fixed mipping bug
170 *
171 * 26 2/12/98 1:32p Jason
172 * got mipmapping working
173 *
174 * 25 2/11/98 7:08p Jason
175 * took out dumb mprintf
176 *
177 * 24 2/09/98 3:38p Jason
178 * fixed potential problem with overlay bitmaps
179 *
180 * 23 2/06/98 7:20p Jason
181 * made duplicate bitmaps replace themselves in memory
182 *
183 * 22 1/26/98 4:32p Jason
184 * took out some goofy mprintfs
185 *
186 * 21 1/16/98 3:14p Samir
187 * Now store pixel width and height of chunked bitmap.
188 *
189 * 20 1/16/98 11:46a Samir
190 * Added functions to allocate and destroy chunked bitmaps.
191 *
192 * 19 1/14/98 12:45p Jeff
193 * Added a 'clear bitmap' function. Clears to transparent color.
194 *
195 * 18 12/23/97 6:32p Mark
196 * fixed problem with trying to save a bitmap that hasn't been paged in.
197 *
198 * 17 12/19/97 5:59p Jason
199 * sped up bitmap loading
200 *
201 * 16 12/19/97 3:36p Jason
202 * bug fixes for bitmap paging stuff
203 *
204 * 15 12/19/97 2:46p Jason
205 * implemented bitmap paging routines
206 *
207 * 14 12/18/97 4:03p Jason
208 * added error checking for bitmaps initting
209 *
210 * 13 11/11/97 1:07p Jason
211 * fixed multiple names problem in bitmap naming
212 *
213 * 12 11/10/97 4:53p Jason
214 * added warning for names that are too long
215 *
216 * 11 10/16/97 4:55p Jason
217 * fixed stupid off-by-one bug
218 *
219 * 10 10/16/97 10:48a Jason
220 * added bm_set_priority
221 *
222 * 9 10/15/97 5:20p Jason
223 * did a HUGE overhaul of the bitmap system
224 *
225 * 8 9/17/97 10:48a Jason
226 * added cache_slot variable
227 *
228 * 7 9/16/97 5:04p Matt
229 * Changed conditional for debug code
230 *
231 * 6 9/12/97 6:06p Samir
232 * Added function to get bitmap pixel.
233 *
234 * 5 9/10/97 12:13p Samir
235 * Added function to check if a pixel is transparent.
236 *
237 * 4 9/02/97 11:18a Jason
238 * got rid of compiler warnings
239 *
240 * 3 8/29/97 1:22p Jason
241 * added tga screenshots
242 *
243 * 2 8/05/97 10:18a Jason
244 * added lightmap system
245 *
246 * 46 6/24/97 12:41p Jason
247 * checked in for safety
248 *
249 * 45 6/06/97 3:57p Jason
250 * implemented changes for small textures and automatic tmap2 recognition
251 *
252 * 44 6/03/97 12:19p Jason
253 * cleaned up the bitmap library a bit
254 *
255 * 43 5/19/97 5:10p Jason
256 * changes for our new abstracted renderer code
257 *
258 * 42 5/12/97 11:41a Jason
259 * made game work (default) to 16bit no mip maps mode
260 * Saves us alot of memory
261 *
262 * 41 5/08/97 1:16p Jason
263 * made ChangeEndName work with device independant calls
264 *
265 * 40 5/02/97 5:22p Jason
266 * added bm_rowsize to return bytes per row
267 *
268 * 39 5/01/97 4:37p Jason
269 * made bitmaps clear their settings when alloced (bug fixed)
270 *
271 * 38 4/30/97 3:15p Jason
272 * changes to support both 8bit and 16bit rendering in software
273 *
274 * 37 4/25/97 3:31p Jason
275 * implemented better memory management for vclips and bitmaps
276 *
277 * 36 4/8/97 5:23 PM Jeremy
278 * #ifdef editor around calls to decrement bitmap_memory_used by checking
279 * _msize of an array location. _msize is microsoft specific, not cross
280 * platform, but this is only used by the editor anyway, thus the ifdef
281 * editor.
282 *
283 * 35 3/31/97 7:18p Jason
284 * made bitmaps easier on memory if not in editor mode
285 *
286 * 34 3/28/97 12:22p Jason
287 * implemented memory sharing scheme between 8 bit bitmaps
288 *
289 * 33 3/13/97 1:05p Matt
290 * Fixed extra-stupid bug
291 *
292 * 32 3/13/97 12:35p Matt
293 * Look for error1.ogf in current directory (explicitely)
294 *
295 * 31 3/03/97 6:20p Matt
296 * Changed cfile functions to use D3 naming convention
297 *
298 * $NoKeywords: $
299 */
300
301 #include <algorithm>
302 #include <cstdlib>
303 #include <cstring>
304 #include <cctype>
305 #include <filesystem>
306
307 #include "cfile.h"
308 #include "bitmap.h"
309 #include "pserror.h"
310 #include "mono.h"
311 #include "iff.h"
312 #include "ddio.h"
313 #include "lightmap.h"
314 #include "bumpmap.h"
315 #include "mem.h"
316 #include "psrand.h"
317 #include "grdefs.h"
318
319 #define BM_FILETYPE_TGA 1
320 #define BM_FILETYPE_PCX 2
321 #define BM_FILETYPE_IFF 3
322 int Num_of_bitmaps = 0;
323 bms_bitmap GameBitmaps[MAX_BITMAPS];
324 uint32_t Bitmap_memory_used = 0;
325 uint8_t Bitmaps_initted = 0;
326 /* modify these lines to establish data type */
327 typedef bms_bitmap *bm_T; /* type of item to be stored */
328 typedef int bm_hashTableIndex; /* index into hash table */
329 #define compEQ(a, b) (stricmp((a)->name, (b)->name) == 0)
330 struct bm_Node {
331 struct bm_Node *next; /* next bm_Node */
332 bm_T data; /* data stored in bm_Node */
333 };
334 static bm_Node *bm_findNode(bm_T data);
335 static void bm_deleteNode(bm_T data);
336 static bm_Node *bm_insertNode(bm_T data);
337 static bm_hashTableIndex bm_hash(bm_T data);
338 static bm_Node **bm_hashTable = NULL;
339 static const int bm_hashTableSize = (MAX_BITMAPS / 2);
340 static void bm_InitHashTable();
341 static void bm_DeleteHashTable();
342 static int bm_TestName(const char *src);
343
344 void bm_InitHashTable() {
345 bm_hashTable = (bm_Node **)mem_malloc(bm_hashTableSize * sizeof(bm_Node *));
346 for (int a = 0; a < bm_hashTableSize; a++) {
347 bm_hashTable[a] = NULL;
348 }
349 }
350 void bm_DeleteHashTable() {
351 if (bm_hashTable) {
352 int idx;
353 for (idx = 0; idx < bm_hashTableSize; idx++) {
354 if (bm_hashTable[idx]) {
355 bm_Node *curr, *next;
356 curr = bm_hashTable[idx];
357 while (curr) {
358 next = curr->next;
359 mem_free(curr);
360 curr = next;
361 }
362 bm_hashTable[idx] = NULL;
363 }
364 }
365
366 mem_free(bm_hashTable);
367 bm_hashTable = NULL;
368 }
369 }
370
371 bm_hashTableIndex bm_hash(bm_T data) {
372 /***********************************
373 * hash function applied to data *
374 ***********************************/
375 char *p = data->name;
376 uint32_t hval = strlen(p);
377
378 while (*p) {
379 hval += tolower(*p);
380 p++;
381 }
382
383 return (hval % (MAX_BITMAPS / 2));
384 }
385 bm_Node *bm_insertNode(bm_T data) {
386 bm_Node *p, *p0;
387 bm_hashTableIndex bucket;
388 /************************************************
389 * allocate bm_Node for data and insert in table *
390 ************************************************/
391 /* insert bm_Node at beginning of list */
392 bucket = bm_hash(data);
393 if ((p = (bm_Node *)mem_malloc(sizeof(bm_Node))) == 0) {
394 exit(1);
395 }
396 p0 = bm_hashTable[bucket];
397 bm_hashTable[bucket] = p;
398 p->next = p0;
399 p->data = data;
400 return p;
401 }
402 void bm_deleteNode(bm_T data) {
403 bm_Node *p0, *p;
404 bm_hashTableIndex bucket;
405 /********************************************
406 * delete bm_Node containing data from table *
407 ********************************************/
408 /* find bm_Node */
409 p0 = 0;
410 bucket = bm_hash(data);
411 p = bm_hashTable[bucket];
412 while (p && !compEQ(p->data, data)) {
413 p0 = p;
414 p = p->next;
415 }
416 if (!p)
417 return;
418 /* p designates bm_Node to delete, remove it from list */
419 if (p0)
420 /* not first bm_Node, p0 points to previous bm_Node */
421 p0->next = p->next;
422 else
423 /* first bm_Node on chain */
424 bm_hashTable[bucket] = p->next;
425 mem_free(p);
426 }
427 bm_Node *bm_findNode(bm_T data) {
428 bm_Node *p;
429 if (!bm_hashTable)
430 return NULL;
431 /*******************************
432 * find bm_Node containing data *
433 *******************************/
434 p = bm_hashTable[bm_hash(data)];
435 while (p && !compEQ(p->data, data))
436 p = p->next;
437 return p;
438 }
439 // simply frees up a bitmap
440 void bm_FreeBitmapMain(int handle);
441 // Sets all the bitmaps to unused
442 void bm_InitBitmaps() {
443 int i, ret;
444 bm_InitHashTable();
445 Bitmaps_initted = 1;
446 for (i = 0; i < MAX_BITMAPS; i++) {
447 GameBitmaps[i].used = 0;
448 GameBitmaps[i].data16 = NULL;
449 GameBitmaps[i].format = BITMAP_FORMAT_STANDARD;
450 GameBitmaps[i].cache_slot = -1;
451 GameBitmaps[i].flags = 0;
452 }
453 int bm = bm_AllocBitmap(128, 128, 0);
454 ASSERT(bm == BAD_BITMAP_HANDLE);
455 if (cfexist(".\\error1.ogf"))
456 ret = bm_AllocLoadFileBitmap(".\\error1.ogf", 0);
457 else
458 ret = -1;
459 if (ret == -1)
460 bm_MakeBad(bm);
461 else {
462 bm_ScaleBitmapToBitmap(bm, ret);
463 bm_FreeBitmap(ret);
464 }
465
466 // bm_GenerateMipMaps (bm);
467 atexit(bm_ShutdownBitmaps);
468 // Initialize lightmaps and bumpmaps
469 lm_InitLightmaps();
470 bump_InitBumpmaps();
471 }
472 void bm_ShutdownBitmaps(void) {
473 int i;
474 mprintf(0, "Freeing all bitmap memory.\n");
475 bm_FreeBitmapMain(0);
476 for (i = 0; i < MAX_BITMAPS; i++) {
477 while (GameBitmaps[i].used > 0)
478 bm_FreeBitmap(i);
479 }
480 bm_DeleteHashTable();
481 }
482 int bm_AllocateMemoryForIndex(int n, int w, int h, int add_mem) {
483 // If no go on the malloc, bail out with -1
484
485 int size = (w * h * 2) + (add_mem) + 2;
486 GameBitmaps[n].data16 = (uint16_t *)mem_malloc(size);
487 if (!GameBitmaps[n].data16) {
488 Int3(); // Ran out of memory!
489 return -1;
490 }
491 Bitmap_memory_used += (size);
492 memset(GameBitmaps[n].data16, 0xAA, size);
493
494 GameBitmaps[n].format = BITMAP_FORMAT_STANDARD;
495 GameBitmaps[n].width = w;
496 GameBitmaps[n].height = h;
497 GameBitmaps[n].flags = BF_CHANGED | BF_BRAND_NEW;
498
499 #ifdef USE_OPENGL
500 for (int tmp = w; tmp > 0; tmp = tmp >> 1)
501 GameBitmaps[n].mip_levels++;
502 #else
503 GameBitmaps[n].mip_levels = NUM_MIP_LEVELS;
504 #endif
505
506 Num_of_bitmaps++;
507 return n;
508 }
509 // Allocs a bitmap of w x h size
510 // If add_mem is nonzero, adds that to the amount alloced
511 // Returns bitmap handle if successful, -1 if otherwise
512 int bm_AllocBitmap(int w, int h, int add_mem) {
513 int n = 0, i;
514 if (!Bitmaps_initted) {
515 Int3();
516 mprintf(0, "Bitmaps not initted!!!\n");
517 return -1;
518 }
519
520 for (i = 0; i < MAX_BITMAPS; i++) {
521 if (GameBitmaps[i].used == 0) {
522 n = i;
523 break;
524 }
525 }
526 // If we can't find a free slot in which to alloc, bail out
527 if (i == MAX_BITMAPS) {
528 Int3();
529 mprintf(0, "ERROR! Couldn't find a free bitmap to alloc!\n");
530 return -1;
531 }
532 memset(&GameBitmaps[n], 0, sizeof(bms_bitmap));
533 int ret = bm_AllocateMemoryForIndex(n, w, h, add_mem);
534 if (ret >= 0) {
535 GameBitmaps[n].used = 1;
536 GameBitmaps[n].cache_slot = -1;
537 }
538 return ret;
539 }
540 // Just like bm_AllocBitmap but doesn't actually allocate memory. Useful for paging!
541 int bm_AllocNoMemBitmap(int w, int h) {
542 int n = 0, i;
543 if (!Bitmaps_initted) {
544 Int3();
545 mprintf(0, "Bitmaps not initted!!!\n");
546 return -1;
547 }
548
549 for (i = 0; i < MAX_BITMAPS; i++) {
550 if (GameBitmaps[i].used == 0) {
551 n = i;
552 break;
553 }
554 }
555 // If we can't find a free slot in which to alloc, bail out
556 if (i == MAX_BITMAPS) {
557 Int3();
558 mprintf(0, "ERROR! Couldn't find a free bitmap to alloc!\n");
559 return -1;
560 }
561 // If no go on the malloc, bail out with -1
562
563 memset(&GameBitmaps[n], 0, sizeof(bms_bitmap));
564
565 GameBitmaps[n].width = w;
566 GameBitmaps[n].height = h;
567 GameBitmaps[n].used = 1;
568 GameBitmaps[n].format = BITMAP_FORMAT_STANDARD;
569 GameBitmaps[n].flags = BF_NOT_RESIDENT | BF_CHANGED | BF_BRAND_NEW;
570 GameBitmaps[n].cache_slot = -1;
571
572 Num_of_bitmaps++;
573 return n;
574 }
575 // Searches thru all bitmaps for a specific name, returns -1 if not found
576 // or index of bitmap with name
577 int bm_FindBitmapName(const char *name) {
578 int num_counted = 0;
579
580 bms_bitmap fbmp;
581 strcpy(fbmp.name, name);
582 bm_Node *fnode = bm_findNode(&fbmp);
583 if (fnode) {
584 // mprintf(0,"Hash table found bitmap %d\n",fnode->data - GameBitmaps);
585 return fnode->data - GameBitmaps;
586 } else
587 return -1;
588 }
589 // Given a handle, frees the bitmap memory and flags this bitmap as unused
590 void bm_FreeBitmap(int handle) {
591 if (!Bitmaps_initted) {
592 Int3();
593 mprintf(0, "Bitmaps not initted!!!\n");
594 return;
595 }
596 if (handle == BAD_BITMAP_HANDLE)
597 return;
598 if (GameBitmaps[handle].used < 1)
599 return;
600 GameBitmaps[handle].used--;
601 if (GameBitmaps[handle].used == 0) {
602 bm_FreeBitmapMain(handle);
603 }
604 }
605 // simply frees up a bitmap
606 void bm_FreeBitmapData(int handle) {
607 if (GameBitmaps[handle].data16 != NULL && !(GameBitmaps[handle].flags & BF_NOT_RESIDENT)) {
608 if (!(GameBitmaps[handle].flags & BF_NOT_RESIDENT)) {
609 int mem_used = (GameBitmaps[handle].width * GameBitmaps[handle].height * 2);
610 Bitmap_memory_used -= mem_used;
611 if (GameBitmaps[handle].flags & BF_MIPMAPPED)
612 Bitmap_memory_used -= (mem_used / 3);
613 }
614 mem_free(GameBitmaps[handle].data16);
615 }
616 GameBitmaps[handle].cache_slot = -1;
617 GameBitmaps[handle].flags |= BF_NOT_RESIDENT;
618 if (GameBitmaps[handle].flags & BF_MIPMAPPED)
619 GameBitmaps[handle].flags |= BF_WANTS_MIP;
620 GameBitmaps[handle].data16 = NULL;
621 }
622 // simply frees up a bitmap
623 void bm_FreeBitmapMain(int handle) {
624 bm_deleteNode(&GameBitmaps[handle]);
625 bm_FreeBitmapData(handle);
626 GameBitmaps[handle].data16 = NULL;
627 GameBitmaps[handle].cache_slot = -1;
628 GameBitmaps[handle].used = 0;
629 Num_of_bitmaps--;
630 }
631 // Returns -1 if this name is not already in use, else index of bitmap that is using name
632 int bm_TestName(const char *src) {
633 uint32_t i, limit;
634
635 char namedest[256];
636 char path[256], ext[256], filename[256];
637
638 ddio_SplitPath(src, path, filename, ext);
639 limit = BITMAP_NAME_LEN - 7;
640 // Make sure we don't go over our name length limit
641 strncpy(namedest, filename, limit);
642 namedest[limit] = 0;
643
644 int cur_len = strlen(namedest);
645
646 // Now, make sure there are no other bitmaps with this name
647 strcat(namedest, ".ogf");
648 if ((i = bm_FindBitmapName(namedest)) == -1)
649 return -1;
650 return i;
651 }
652 // gets the filename from a path, plus appends our .ogf extension
653 void bm_ChangeEndName(const char *src, char *dest) {
654 uint32_t i, limit;
655 int last = -1;
656 int curnum = -1;
657 char namedest[256 + 16];
658 char path[256], ext[256], filename[256];
659
660 ddio_SplitPath(src, path, filename, ext);
661 limit = BITMAP_NAME_LEN - 7;
662 // Make sure we don't go over our name length limit
663 filename[limit] = 0;
664 Start:
665 if (curnum != -1)
666 snprintf(namedest, sizeof(namedest), "%s%d", filename, curnum);
667 else
668 strcpy(namedest, filename);
669 // Now, make sure there are no other bitmaps with this name
670 strcat(namedest, ".ogf");
671 if ((i = bm_FindBitmapName(namedest)) != -1) {
672 curnum++;
673 // This name is already taken. Try same name with different number
674 // appended
675 goto Start;
676 }
677 strcpy(dest, namedest);
678 }
679 // Checks to see if this file is a kind we can read
680 int bm_GetFileType(CFILE *infile, const char *dest) {
681 char iffcheck[4];
682 int i;
683 // First, check if its a pcx
684 i = strlen(dest);
685 if (dest[i - 4] == '.' && (dest[i - 3] == 'p' || dest[i - 3] == 'P') && (dest[i - 2] == 'c' || dest[i - 2] == 'C') &&
686 (dest[i - 1] == 'x' || dest[i - 1] == 'X'))
687 return BM_FILETYPE_PCX;
688 // How about an IFF?
689 for (i = 0; i < 4; i++)
690 iffcheck[i] = cf_ReadByte(infile);
691 cfseek(infile, 0, SEEK_SET);
692 if (!strncmp("FORM", iffcheck, 4))
693 return BM_FILETYPE_IFF;
694 // Lastly, just default to possible TGA or OGF
695 return BM_FILETYPE_TGA;
696 }
697 // Allocs and loads a bitmap
698 // Returns the handle of the loaded bitmap
699 // Returns -1 if something is wrong
700 // If mipped is non-zero, allocs extra space for mips and computes them
701 int bm_AllocLoadFileBitmap(const char *fname, int mipped, int format) {
702 CFILE *infile;
703 if (!Bitmaps_initted) {
704 Int3();
705 mprintf(0, "Bitmaps not initted!!!\n");
706 return -1;
707 }
708
709 char name[BITMAP_NAME_LEN];
710 int n, src_bm = 0;
711 int old_used;
712 int overlay = 0;
713
714 char *filename = (char *)fname;
715
716 // due to a bug in some people's tablefile editors, we got to make sure there is
717 // no path if there shouldn't be
718 if (!cfexist(filename)) {
719 // generate a possible new filename
720 char *end_ptr, *start_ptr;
721 start_ptr = (char *)fname;
722 end_ptr = start_ptr + strlen(start_ptr) - 1;
723 while ((end_ptr >= start_ptr) && (*end_ptr != '\\'))
724 end_ptr--;
725 if (end_ptr < start_ptr) {
726 mprintf(0, "Unable to find bitmap %s\n", fname);
727 return -1;
728 }
729 ASSERT(*end_ptr == '\\');
730 end_ptr++;
731 filename = end_ptr;
732 mprintf(0, "Couldn't find %s, so gonna try %s\n", fname, filename);
733 }
734
735 // Check to see if this bitmap is already in memory, if so, just return that
736 // bitmaps handle
737 if ((n = bm_TestName(filename)) != -1) {
738 mprintf(1, "Found duplicate bitmap %s.\n", GameBitmaps[n].name);
739 old_used = GameBitmaps[n].used;
740 GameBitmaps[n].used = 1;
741 bm_FreeBitmap(n);
742 GameBitmaps[n].used = old_used + 1;
743
744 overlay = 1;
745 }
746 if (!overlay)
747 bm_ChangeEndName(filename, name);
748 else {
749 strcpy(name, GameBitmaps[n].name);
750 }
751 if (strlen(name) > 33) {
752 mprintf(0, "ERROR!! This bitmaps name is too long, try shortening it and retry!\n");
753 return -1;
754 }
755 // Try to open the file. If we can't load it from the network if possible
756 infile = (CFILE *)cfopen(filename, "rb");
757 if (!infile) {
758 mprintf(0, "bm_AllocLoadFileBitmap: Can't open file named %s.\n", filename);
759
760 return BAD_BITMAP_HANDLE; // return the bad texture
761 }
762 // Check to see if this is IFF. If so, call the IFF reader. If not,
763 // rewind the file and read as a TGA
764
765 int filetype = bm_GetFileType(infile, filename);
766
767 switch (filetype) {
768 case BM_FILETYPE_IFF:
769 src_bm = bm_iff_alloc_file(infile);
770 break;
771 case BM_FILETYPE_TGA:
772 // reads a tga or an outrage graphics file (ogf)
773 src_bm = bm_tga_alloc_file(infile, name, format);
774 break;
775 case BM_FILETYPE_PCX:
776 src_bm = bm_pcx_alloc_file(infile);
777 break;
778 default:
779 Int3(); // Can't read this type
780 break;
781 }
782 cfclose(infile);
783 if (src_bm < 0) {
784 mprintf(0, "Couldn't load %s.", filename);
785 return -1;
786 }
787
788 // Allocate space for our bitmap.
789 if (mipped) {
790 if ((bm_mipped(src_bm)) == 0) // If we want a mipped but we don't have one
791 {
792 int w = bm_w(src_bm, 0);
793 int h = bm_h(src_bm, 0);
794
795 if (overlay) {
796 bm_AllocateMemoryForIndex(n, w, h, mipped * (((w * h * 2) / 3)));
797 GameBitmaps[n].format = GameBitmaps[src_bm].format;
798 } else {
799 n = bm_AllocBitmap(w, h, mipped * (((w * h * 2) / 3)));
800 GameBitmaps[n].format = GameBitmaps[src_bm].format;
801 }
802
803 ASSERT(n >= 0);
804 bm_ScaleBitmapToBitmap(n, src_bm);
805 bm_FreeBitmap(src_bm);
806
807 bm_GenerateMipMaps(n);
808 } else {
809 if (!overlay)
810 n = src_bm;
811 else {
812 int w = bm_w(src_bm, 0);
813 int h = bm_h(src_bm, 0);
814 bm_AllocateMemoryForIndex(n, w, h, mipped * (((w * h * 2) / 3)));
815 GameBitmaps[n].flags |= BF_MIPMAPPED;
816 GameBitmaps[n].format = GameBitmaps[src_bm].format;
817 bm_ScaleBitmapToBitmap(n, src_bm);
818 bm_FreeBitmap(src_bm);
819 }
820 }
821 } else // If we don't want a mipped
822 {
823 if ((bm_mipped(src_bm)) == 0) {
824 if (!overlay)
825 n = src_bm;
826 else {
827 int w = bm_w(src_bm, 0);
828 int h = bm_h(src_bm, 0);
829 bm_AllocateMemoryForIndex(n, w, h, mipped * (((w * h * 2) / 3)));
830 GameBitmaps[n].format = GameBitmaps[src_bm].format;
831 bm_ScaleBitmapToBitmap(n, src_bm);
832 bm_FreeBitmap(src_bm);
833 }
834 } else // And this is already mipped
835 {
836 int w = bm_w(src_bm, 0);
837 int h = bm_h(src_bm, 0);
838 uint16_t *src_data, *dest_data;
839
840 if (overlay) {
841 bm_AllocateMemoryForIndex(n, w, h, mipped * (((w * h * 2) / 3)));
842 GameBitmaps[n].format = GameBitmaps[src_bm].format;
843 } else {
844 n = bm_AllocBitmap(w, h, mipped * (((w * h * 2) / 3)));
845 GameBitmaps[n].format = GameBitmaps[src_bm].format;
846 }
847 ASSERT(n >= 0);
848 src_data = (uint16_t *)bm_data(src_bm, 0);
849 dest_data = (uint16_t *)bm_data(n, 0);
850 memcpy(dest_data, src_data, w * h * 2);
851
852 bm_FreeBitmap(src_bm);
853 }
854 }
855
856 strcpy(GameBitmaps[n].name, name);
857
858 // Insert into the hash table!
859 bm_insertNode(&GameBitmaps[n]);
860 return n; // We made it!
861 }
862 // Allocs and loads a bitmap but doesn't actually load texel data!
863 // Returns the handle of the loaded bitmap
864 // Returns -1 if something is wrong
865 int bm_AllocLoadFileNoMemBitmap(const char *fname, int mipped, int format) {
866 if (!Bitmaps_initted) {
867 Int3();
868 mprintf(0, "Bitmaps not initted!!!\n");
869 return -1;
870 }
871
872 char name[BITMAP_NAME_LEN];
873 int n;
874 char *filename = (char *)fname;
875
876 // due to a bug in some people's tablefile editors, we got to make sure there is
877 // no path if there shouldn't be
878 if (!cfexist(filename)) {
879 // generate a possible new filename
880 char *end_ptr, *start_ptr;
881 start_ptr = (char *)fname;
882 end_ptr = start_ptr + strlen(start_ptr) - 1;
883 while ((end_ptr >= start_ptr) && (*end_ptr != '\\'))
884 end_ptr--;
885 if (end_ptr < start_ptr) {
886 mprintf(0, "Unable to find bitmap %s\n", fname);
887 return -1;
888 }
889 ASSERT(*end_ptr == '\\');
890 end_ptr++;
891 filename = end_ptr;
892 mprintf(0, "Couldn't find %s, so gonna try %s\n", fname, filename);
893 }
894
895 // Check to see if this bitmap is already in memory, if so, just return that
896 // bitmaps handle
897 if ((n = bm_TestName(filename)) != -1) {
898 GameBitmaps[n].used++;
899 mprintf(1, "Found duplicate bitmap %s.\n", GameBitmaps[n].name);
900 return n;
901 }
902
903 bm_ChangeEndName(filename, name);
904 if (strlen(name) > 33) {
905 mprintf(0, "ERROR!! This bitmaps name is too long, try shortening it and retry!\n");
906 return -1;
907 }
908 n = bm_AllocNoMemBitmap(1, 1);
909 strcpy(GameBitmaps[n].name, name);
910 if (mipped)
911 GameBitmaps[n].flags |= BF_WANTS_MIP;
912 if (format == BITMAP_FORMAT_4444)
913 GameBitmaps[n].flags |= BF_WANTS_4444;
914
915 return n; // We made it!
916 }
917 // Allocs and loads a bitmap from an open file
918 // Returns the handle of the loaded bitmap
919 // Returns -1 if something is wrong
920 // If mipped is non-zero, allocs extra space for mips and computes them
921 int bm_AllocLoadBitmap(CFILE *infile, int mipped, int format) {
922 char name[BITMAP_NAME_LEN];
923 int n, src_bm;
924 if (!Bitmaps_initted) {
925 Int3();
926 mprintf(0, "Bitmaps not initted!!!\n");
927 return -1;
928 }
929
930 // Assume this is an ogf
931
932 src_bm = bm_tga_alloc_file(infile, name, format);
933 if (src_bm < 0) {
934 mprintf(0, "Couldn't load %s.", name);
935 return -1;
936 }
937
938 // Allocate space for our bitmap
939 if (mipped) {
940 if ((bm_mipped(src_bm)) == 0) // If we want a mipped but we don't have one
941 {
942 int w = bm_w(src_bm, 0);
943 int h = bm_h(src_bm, 0);
944
945 n = bm_AllocBitmap(w, h, mipped * (((w * h * 2) / 3)));
946
947 ASSERT(n >= 0);
948 GameBitmaps[n].format = GameBitmaps[src_bm].format;
949 bm_ScaleBitmapToBitmap(n, src_bm);
950 bm_FreeBitmap(src_bm);
951
952 bm_GenerateMipMaps(n);
953 } else
954 n = src_bm;
955 } else // If we don't want a mipped
956 {
957 if ((bm_mipped(src_bm)) == 0)
958 n = src_bm;
959 else // And this is already mipped
960 {
961 int w = bm_w(src_bm, 0);
962 int h = bm_h(src_bm, 0);
963 uint16_t *src_data, *dest_data;
964
965 n = bm_AllocBitmap(w, h, mipped * (((w * h * 2) / 3)));
966 ASSERT(n >= 0);
967 src_data = (uint16_t *)bm_data(src_bm, 0);
968 dest_data = (uint16_t *)bm_data(n, 0);
969 memcpy(dest_data, src_data, w * h * 2);
970
971 bm_FreeBitmap(src_bm);
972 }
973 }
974 strcpy(GameBitmaps[n].name, name);
975 return n; // We made it!
976 }
977 // Given a handle, makes a big random shape to let you know you are screwed.
978 void bm_MakeBad(int handle) {
979 int i, t, limit;
980 uint16_t *dest;
981 ASSERT(GameBitmaps[handle].used);
982 if (handle != BAD_BITMAP_HANDLE)
983 Int3(); // hmm, you're assigning a random bitmap to something other than
984 // the default.
985 if (bm_mipped(handle))
986 limit = bm_miplevels(handle);
987 else
988 limit = 1;
989 for (i = 0; i < limit; i++) {
990 dest = bm_data(handle, i);
991
992 for (t = 0; t < bm_h(handle, i) * bm_w(handle, i); t++)
993 *dest++ = (OPAQUE_FLAG | ps_rand());
994 }
995 }
996
997 // Saves a bitmap to an open file. Saves the bitmap as an OUTRAGE_COMPRESSED_OGF.
998 // Returns -1 if something is wrong.
999 int bm_SaveBitmap(CFILE *fp, int handle) {
1000 uint8_t dumbbyte = 0, image_type = OUTRAGE_1555_COMPRESSED_MIPPED, pixsize = 32, desc = 8 + 32;
1001 int i, done = 0;
1002 int num_mips;
1003 mprintf(0, "Saving bitmap %s...\n", GameBitmaps[handle].name);
1004 if (!GameBitmaps[handle].used) {
1005 mprintf(0, "bm_SaveBitmap: Trying to save a bitmap that isn't used!\n");
1006 Int3();
1007 return -1;
1008 }
1009 if (handle == BAD_BITMAP_HANDLE) {
1010 Int3(); // Hey, you shouldn't be saving this bitmap!
1011 return -1;
1012 }
1013 if (GameBitmaps[handle].format == BITMAP_FORMAT_4444)
1014 image_type = OUTRAGE_4444_COMPRESSED_MIPPED;
1015 if (!fp) {
1016 mprintf(0, "bm_SaveBitmap: Trying to save a bitmap to a closed file!\n");
1017 Int3();
1018 return -1;
1019 }
1020 cf_WriteByte(fp, dumbbyte);
1021 cf_WriteByte(fp, dumbbyte);
1022 cf_WriteByte(fp, image_type);
1023 cf_WriteString(fp, GameBitmaps[handle].name);
1024
1025 if ((bm_mipped(handle)))
1026 num_mips = bm_miplevels(handle);
1027 else
1028 num_mips = 1;
1029 cf_WriteByte(fp, num_mips);
1030 for (i = 0; i < 9; i++)
1031 cf_WriteByte(fp, 0);
1032 cf_WriteShort(fp, GameBitmaps[handle].width);
1033 cf_WriteShort(fp, GameBitmaps[handle].height);
1034 cf_WriteByte(fp, pixsize);
1035 cf_WriteByte(fp, desc);
1036
1037 // write compressed info
1038 for (int m = 0; m < num_mips; m++) {
1039 int curptr = 0;
1040 int total = bm_w(handle, m) * bm_h(handle, m);
1041 uint16_t *src_data = (uint16_t *)bm_data(handle, m);
1042 done = 0;
1043 while (!done) {
1044 if (curptr == total) {
1045 done = 1;
1046 continue;
1047 }
1048 ASSERT(curptr < total);
1049 uint16_t curpix = src_data[curptr];
1050 uint8_t count = 1;
1051 while (src_data[curptr + count] == curpix && count < 250 && (curptr + count) < total)
1052 count++;
1053 if (count == 1) {
1054 cf_WriteByte(fp, 0);
1055 cf_WriteShort(fp, curpix);
1056 } else {
1057 cf_WriteByte(fp, count);
1058 cf_WriteShort(fp, curpix);
1059 }
1060 curptr += count;
1061 }
1062 }
1063
1064 return 1;
1065 }
1066 // Makes sure a bitmap is in memory...if not it attempts to page it in
1067 int bm_MakeBitmapResident(int handle) {
1068 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT) {
1069 if (bm_page_in_file(handle) > 0) // DAJ -1FIX
1070 {
1071 GameBitmaps[handle].flags &= ~BF_NOT_RESIDENT;
1072 GameBitmaps[handle].flags |= BF_CHANGED | BF_BRAND_NEW;
1073 } else {
1074 mprintf(0, "Error paging in bitmap %s!\n", GameBitmaps[handle].name);
1075 return 0;
1076 }
1077 }
1078 return 1;
1079 }
1080 // Saves a bitmap to a file. Saves the bitmap as an OUTRAGE_COMPRESSED_OGF.
1081 // Returns -1 if something is wrong.
1082 int bm_SaveFileBitmap(const std::filesystem::path &filename, int handle) {
1083 int ret;
1084 CFILE *fp;
1085 if (!GameBitmaps[handle].used) {
1086 mprintf(0, "bm_SaveBitmap: Trying to save a bitmap that isn't used!\n");
1087 Int3();
1088 return -1;
1089 }
1090 if (handle == BAD_BITMAP_HANDLE) {
1091 Int3(); // Hey, you shouldn't be saving this bitmap!
1092 return -1;
1093 }
1094 if (!bm_MakeBitmapResident(handle)) {
1095 mprintf(0, "There was a problem paging this bitmap in!\n");
1096 return -1;
1097 }
1098 fp = (CFILE *)cfopen(filename, "wb");
1099
1100 if (!fp) {
1101 mprintf(0, "bm_SaveBitmap: Trying to save a bitmap to a closed file!\n");
1102 Int3();
1103 return -1;
1104 }
1105 ret = bm_SaveBitmap(fp, handle);
1106
1107 cfclose(fp);
1108 return ret;
1109 }
1110 // returns a bitmaps width (based on miplevel), else -1 if something is wrong
1111 int bm_w(int handle, int miplevel) {
1112 int w;
1113 if (!GameBitmaps[handle].used) {
1114 Int3();
1115 return -1;
1116 }
1117 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1118 if (!bm_MakeBitmapResident(handle))
1119 return 0;
1120 if (!(GameBitmaps[handle].flags & BF_MIPMAPPED))
1121 miplevel = 0;
1122 w = GameBitmaps[handle].width;
1123 w >>= miplevel;
1124 return (w);
1125 }
1126 // returns a bitmaps height (based on miplevel), else -1 if something is wrong
1127 int bm_h(int handle, int miplevel) {
1128 int h;
1129 if (!GameBitmaps[handle].used) {
1130 Int3();
1131 return -1;
1132 }
1133 // If this bitmap is not page in, do so!
1134 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1135 if (!bm_MakeBitmapResident(handle))
1136 return 0;
1137 if (!(GameBitmaps[handle].flags & BF_MIPMAPPED))
1138 miplevel = 0;
1139 h = GameBitmaps[handle].height;
1140 h >>= miplevel;
1141 return (h);
1142 }
1143
1144 // returns the number of levels of mips a bitmap should have
1145 int bm_miplevels(int handle) {
1146 int levels = 0;
1147 if (!GameBitmaps[handle].used) {
1148 Int3();
1149 return -1;
1150 }
1151 if (GameBitmaps[handle].mip_levels)
1152 return GameBitmaps[handle].mip_levels;
1153
1154 // If this bitmap is not page in, do so!
1155 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1156 if (!bm_MakeBitmapResident(handle))
1157 return 0;
1158 if (GameBitmaps[handle].flags & BF_MIPMAPPED) {
1159 for (int tmp = GameBitmaps[handle].width; tmp > 0; tmp = tmp >> 1) {
1160 levels++;
1161 }
1162 return levels;
1163 }
1164 return 0;
1165 }
1166
1167 // returns a bitmaps mipped status, else -1 if something is wrong
1168 int bm_mipped(int handle) {
1169 if (!GameBitmaps[handle].used) {
1170 Int3();
1171 return -1;
1172 }
1173 // If this bitmap is not page in, do so!
1174 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1175 if (!bm_MakeBitmapResident(handle))
1176 return 0;
1177 if (GameBitmaps[handle].flags & BF_MIPMAPPED)
1178 return 1;
1179 return 0;
1180 }
1181 // returns a bitmaps data (based on given miplevel), else NULL if something is wrong
1182 uint16_t *bm_data(int handle, int miplevel) {
1183 uint16_t *d;
1184 int i;
1185 if (!GameBitmaps[handle].used) {
1186 Int3();
1187 return NULL;
1188 }
1189
1190 // If this bitmap is not page in, do so!
1191 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1192 if (!bm_MakeBitmapResident(handle))
1193 return NULL;
1194
1195 d = GameBitmaps[handle].data16;
1196 for (i = 0; i < miplevel; i++) {
1197 d += (GameBitmaps[handle].width >> i) * (GameBitmaps[handle].height >> i);
1198 }
1199 return (d);
1200 }
1201 // Given a source bitmap, generates mipmaps for it
1202 void bm_GenerateMipMaps(int handle) {
1203 int width = bm_w(handle, 0);
1204 int height = bm_h(handle, 0);
1205 int jump = 2; // how many pixels to jump in x and y on source
1206
1207 // mprintf(0,"We got a mipper! %d \n",handle);
1208
1209 ASSERT(GameBitmaps[handle].used);
1210 GameBitmaps[handle].flags |= BF_MIPMAPPED;
1211
1212 int levels = bm_miplevels(handle);
1213
1214 uint16_t *destdata;
1215 for (int miplevel = 1; miplevel < levels; miplevel++) {
1216 width /= 2;
1217 height /= 2;
1218
1219 for (int i = 0; i < height; i++) {
1220 int adjheight = (i * jump) * bm_w(handle, miplevel - 1); // find our y offset
1221
1222 for (int t = 0; t < width; t++) {
1223 int adjwidth = (t * jump); // find our x offset
1224 uint16_t *srcptr = bm_data(handle, miplevel - 1);
1225 srcptr += (adjheight + adjwidth);
1226
1227 int rsum, gsum, bsum, asum;
1228 rsum = gsum = bsum = asum = 0;
1229 uint16_t destpix = 0;
1230 if (GameBitmaps[handle].format == BITMAP_FORMAT_1555) {
1231 for (int y = 0; y < 2; y++)
1232 for (int x = 0; x < 2; x++) {
1233 uint16_t pix = srcptr[y * bm_w(handle, miplevel - 1) + x];
1234 int r = (pix >> 10) & 0x1f;
1235 int g = (pix >> 5) & 0x1f;
1236 int b = (pix & 0x1f);
1237
1238 if (!(pix & OPAQUE_FLAG))
1239 asum++;
1240 else {
1241 rsum += r;
1242 gsum += g;
1243 bsum += b;
1244 }
1245 }
1246 // Average our source pixels
1247 rsum /= 4;
1248 gsum /= 4;
1249 bsum /= 4;
1250 destpix = OPAQUE_FLAG | (rsum << 10) | (gsum << 5) | bsum;
1251
1252 if (asum > 2)
1253 destpix = NEW_TRANSPARENT_COLOR;
1254 } else if (GameBitmaps[handle].format == BITMAP_FORMAT_4444) {
1255 for (int y = 0; y < 2; y++)
1256 for (int x = 0; x < 2; x++) {
1257 uint16_t pix = srcptr[y * bm_w(handle, miplevel - 1) + x];
1258 int a = (pix >> 12) & 0x0f;
1259 int r = (pix >> 8) & 0x0f;
1260 int g = (pix >> 4) & 0x0f;
1261 int b = (pix & 0x0f);
1262
1263 asum += a;
1264 rsum += r;
1265 gsum += g;
1266 bsum += b;
1267 }
1268 // Average our source pixels
1269 rsum /= 4;
1270 gsum /= 4;
1271 bsum /= 4;
1272 asum /= 4;
1273 destpix = (asum << 12) | (rsum << 8) | (gsum << 4) | bsum;
1274 } else {
1275 Int3();
1276 }
1277 destdata = bm_data(handle, miplevel);
1278 int sw = bm_w(handle, miplevel);
1279 destdata[(i * sw) + t] = destpix;
1280 }
1281 }
1282 }
1283 }
1284 // Gets bits per pixel for a particular bitmap
1285 // As of 12/30/96 always returns 16
1286 int bm_bpp(int handle) { return BPP_16; }
1287 // Given two bitmaps, scales the data from src to the size of dest
1288 // Not a particularly fast implementation
1289 void bm_ScaleBitmapToBitmap(int dest, int src) {
1290 uint16_t *dp = bm_data(dest, 0);
1291 uint16_t *sp = bm_data(src, 0);
1292 ASSERT(GameBitmaps[dest].used && dp);
1293 ASSERT(GameBitmaps[src].used && sp);
1294 int smipped = bm_mipped(src);
1295 int dmipped = bm_mipped(dest);
1296 int limit;
1297 ASSERT(smipped == dmipped);
1298 ASSERT(GameBitmaps[dest].format == GameBitmaps[src].format);
1299
1300 int sw = bm_w(src, 0);
1301 int sh = bm_h(src, 0);
1302 int dw = bm_w(dest, 0);
1303 int dh = bm_h(dest, 0);
1304 int i, t;
1305 uint16_t *sdata;
1306 uint16_t *ddata;
1307 if (sw == dw && sh == dh) {
1308 if (smipped)
1309 limit = bm_miplevels(src);
1310 else
1311 limit = 1;
1312 for (i = 0; i < limit; i++) {
1313 sdata = (uint16_t *)bm_data(src, i);
1314 ddata = (uint16_t *)bm_data(dest, i);
1315 dw = bm_w(dest, i);
1316 dh = bm_h(dest, i);
1317 memcpy(ddata, sdata, dw * dh * sizeof(uint16_t));
1318 }
1319 return;
1320 }
1321 if (smipped)
1322 limit = bm_miplevels(src);
1323 else
1324 limit = 1;
1325 for (int m = 0; m < limit; m++) {
1326 sw = bm_w(src, m);
1327 sh = bm_h(src, m);
1328 dw = bm_w(dest, m);
1329 dh = bm_h(dest, m);
1330
1331 sdata = (uint16_t *)bm_data(src, m);
1332 ddata = (uint16_t *)bm_data(dest, m);
1333 // These are our interpolant variables
1334 float xstep = (float)sw / (float)dw;
1335 float ystep = (float)sh / (float)dh;
1336 float xoff = 0;
1337 float yoff = 0;
1338 for (i = 0; i < dh; i++, yoff += ystep) {
1339 for (xoff = 0, t = 0; t < dw; t++, xoff += xstep) {
1340 ddata[i * dw + t] = sdata[(int)yoff * sw + (int)xoff];
1341 }
1342 }
1343 }
1344 GameBitmaps[dest].flags |= BF_CHANGED;
1345 }
1346 // given a handle and a miplevel, returns the bytes per bitmap row
1347 int bm_rowsize(int handle, int miplevel) {
1348 int w;
1349 ASSERT(GameBitmaps[handle].used > 0);
1350 w = bm_w(handle, miplevel);
1351 w *= 2;
1352 return w;
1353 }
1354 // a function to determine if a pixel in a bitmap is transparent
1355 bool bm_pixel_transparent(int bm_handle, int x, int y) {
1356 if ((bm_data(bm_handle, 0)) == NULL)
1357 return 0; // only check 16bit stuff
1358 uint16_t *data = bm_data(bm_handle, 0);
1359 data = data + (bm_w(bm_handle, 0) * y) + x;
1360 if (GameBitmaps[bm_handle].format == BITMAP_FORMAT_4444) {
1361 int pix = *data;
1362 if (!(pix >> 12))
1363 return true;
1364 } else {
1365 if (!(*data & OPAQUE_FLAG))
1366 return true;
1367 }
1368 return false;
1369 }
1370 // a function to determine if a pixel in a bitmap is transparent
1371 uint16_t bm_pixel(int bm_handle, int x, int y) {
1372 if ((bm_data(bm_handle, 0)) == NULL)
1373 return 0; // only check 16bit stuff
1374 uint16_t *data = bm_data(bm_handle, 0);
1375 data = data + (bm_w(bm_handle, 0) * y) + x;
1376 return *data;
1377 }
1378 // Goes through the bitmap and sees if there is any transparency...if so, flag it!
1379 int bm_SetBitmapIfTransparent(int handle) {
1380 if ((bm_data(handle, 0)) == NULL)
1381 return 0; // only check 16bit stuff
1382 int w = bm_w(handle, 0);
1383 int h = bm_h(handle, 0);
1384 uint16_t *data = bm_data(handle, 0);
1385 if (GameBitmaps[handle].format == BITMAP_FORMAT_4444) {
1386 for (int i = 0; i < w * h; i++) {
1387 int pix = data[i] >> 12;
1388 if (!pix) {
1389 GameBitmaps[handle].flags |= BF_TRANSPARENT;
1390 return 1;
1391 }
1392 }
1393 } else {
1394 for (int i = 0; i < w * h; i++) {
1395 if (!(data[i] & OPAQUE_FLAG)) {
1396 GameBitmaps[handle].flags |= BF_TRANSPARENT;
1397 return 1;
1398 }
1399 }
1400 }
1401 return 0;
1402 }
1403
1404 // clears bitmap
1405 void bm_ClearBitmap(int handle) {
1406 int dx, dy;
1407 // DAJ int rowsize_w = bm_rowsize(handle,0) >> 1;
1408 uint16_t *bmpdata = bm_data(handle, 0);
1409 int w = bm_w(handle, 0);
1410 int h = bm_h(handle, 0);
1411 for (dy = 0; dy < h; dy++) {
1412 for (dx = 0; dx < w; dx++)
1413 bmpdata[dx] = NEW_TRANSPARENT_COLOR;
1414
1415 bmpdata += w;
1416 }
1417 }
1418 // Given a bitmap handle, breaks a bitmap into smaller pieces.
1419 // Note, this routine isn't terrible fast or efficient, and you
1420 // must free the bitmaps returned to you in the bm_array
1421 bool bm_CreateChunkedBitmap(int bm_handle, chunked_bitmap *chunk) {
1422 int i;
1423 int *bm_array;
1424 int bw = bm_w(bm_handle, 0);
1425 int bh = bm_h(bm_handle, 0);
1426 // determine optimal size of the square bitmaps
1427 float fopt = 128.0f;
1428 int iopt;
1429 // find the smallest dimension and base off that
1430 int smallest = std::min(bw, bh);
1431 if (smallest <= 32)
1432 fopt = 32;
1433 else if (smallest <= 64)
1434 fopt = 64;
1435 else
1436 fopt = 128;
1437 iopt = (int)fopt;
1438 // Get how many pieces we need across and down
1439 float temp = bw / fopt;
1440 int how_many_across = temp;
1441 if ((temp - how_many_across) > 0)
1442 how_many_across++;
1443 temp = bh / fopt;
1444 int how_many_down = temp;
1445 if ((temp - how_many_down) > 0)
1446 how_many_down++;
1447 ASSERT(how_many_across > 0);
1448 ASSERT(how_many_down > 0);
1449 // Allocate memory to hold our list of pieces
1450 bm_array = (int *)mem_malloc(how_many_down * how_many_across * sizeof(int));
1451 ASSERT(bm_array);
1452 for (i = 0; i < how_many_down * how_many_across; i++) {
1453 bm_array[i] = bm_AllocBitmap(iopt, iopt, 0);
1454 ASSERT(bm_array[i] > -1);
1455 // Fill our new pieces with transparency
1456 bm_ClearBitmap(bm_array[i]);
1457 }
1458 // Now go through our big bitmap and partition it into pieces
1459 uint16_t *src_data = bm_data(bm_handle, 0);
1460 uint16_t *sdata;
1461 uint16_t *ddata;
1462 int shift;
1463 switch (iopt) {
1464 case 32:
1465 shift = 5;
1466 break;
1467 case 64:
1468 shift = 6;
1469 break;
1470 case 128:
1471 shift = 7;
1472 break;
1473 default:
1474 Int3(); // Get Jeff
1475 break;
1476 }
1477 int maxx, maxy;
1478 int windex, hindex;
1479 int s_y, s_x, d_y, d_x;
1480 for (hindex = 0; hindex < how_many_down; hindex++) {
1481 for (windex = 0; windex < how_many_across; windex++) {
1482 // loop through the chunks
1483 // find end x and y
1484 if (windex < how_many_across - 1)
1485 maxx = iopt;
1486 else
1487 maxx = bw - (windex << shift);
1488 if (hindex < how_many_down - 1)
1489 maxy = iopt;
1490 else
1491 maxy = bh - (hindex << shift);
1492 // find the starting source x and y
1493 s_x = (windex << shift);
1494 s_y = (hindex << shift);
1495 // get the pointers pointing to the right spot
1496 ddata = bm_data(bm_array[hindex * how_many_across + windex], 0);
1497 sdata = &src_data[s_y * bw + s_x];
1498 // copy the data
1499 for (d_y = 0; d_y < maxy; d_y++) {
1500 for (d_x = 0; d_x < maxx; d_x++) {
1501 ddata[d_x] = sdata[d_x];
1502 } // end for d_x
1503 sdata += bw;
1504 ddata += iopt;
1505 } // end for d_y
1506 } // end for windex
1507 } // end for hindex
1508 // Sirrah, we're done!
1509 // Tell the calling function how many pieces we have in x and y
1510 chunk->pw = bw;
1511 chunk->ph = bh;
1512 chunk->w = how_many_across;
1513 chunk->h = how_many_down;
1514 chunk->bm_array = bm_array;
1515 return true;
1516 }
1517 // destroys a chunked bitmap.
1518 void bm_DestroyChunkedBitmap(chunked_bitmap *chunk) {
1519 int i, num_bmps = chunk->w * chunk->h;
1520 for (i = 0; i < num_bmps; i++)
1521 bm_FreeBitmap(chunk->bm_array[i]);
1522 mem_free(chunk->bm_array);
1523 chunk->bm_array = NULL;
1524 }
1525 // Changes the size of a bitmap to a new size
1526 void bm_ChangeSize(int handle, int new_w, int new_h) {
1527 int mipped = bm_mipped(handle);
1528 int n;
1529 int mem_used = (GameBitmaps[handle].width * GameBitmaps[handle].height * 2);
1530 n = bm_AllocBitmap(new_w, new_h, mipped * ((new_w * new_h * 2) / 3));
1531 ASSERT(n >= 0);
1532 if (mipped)
1533 GameBitmaps[n].flags |= BF_MIPMAPPED;
1534 if (GameBitmaps[handle].format == BITMAP_FORMAT_4444)
1535 GameBitmaps[n].format = BITMAP_FORMAT_4444;
1536
1537 bm_ScaleBitmapToBitmap(n, handle);
1538 Bitmap_memory_used -= mem_used + (mipped * (mem_used / 3));
1539 mem_free(GameBitmaps[handle].data16);
1540 GameBitmaps[handle].data16 = GameBitmaps[n].data16;
1541 GameBitmaps[handle].width = new_w;
1542 GameBitmaps[handle].height = new_h;
1543 GameBitmaps[n].used = 0;
1544 Num_of_bitmaps--;
1545 }
1546 // Returns the format of this bitmap
1547 int bm_format(int handle) {
1548 if (!GameBitmaps[handle].used) {
1549 Int3();
1550 return -1;
1551 }
1552 if (GameBitmaps[handle].flags & BF_NOT_RESIDENT)
1553 if (!bm_MakeBitmapResident(handle))
1554 return 0;
1555 return (GameBitmaps[handle].format);
1556 }