Saves a bitmap to an open file. Saves the bitmap as an OUTRAGE_COMPRESSED_OGF. Returns -1 if something is wrong.
| 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 { |
no test coverage detected