|
37 | 37 | #include "muzzle1.h" |
38 | 38 | #include "muzzle2.h" |
39 | 39 | #include "muzzle3.h" |
| 40 | +#include "imagelib.h" |
40 | 41 |
|
41 | 42 | extern bool g_bStopPlaying; |
42 | 43 | extern bool bUseWeaponOrigin; |
@@ -991,214 +992,82 @@ mxImage *GlWindow::readBmpFromBuffer(const byte * buffer, size_t size) |
991 | 992 | return image; |
992 | 993 | } |
993 | 994 |
|
994 | | - |
995 | | -mxImage *GlWindow::readBmpFromFile(const char *filename) |
| 995 | +bool ImageLib_Alloc(mxImage *image, int w, int h, int pixelSize) |
996 | 996 | { |
997 | | - int i; |
998 | | - FILE *pfile = 0; |
999 | | - mxBitmapFileHeader bmfh; |
1000 | | - mxBitmapInfoHeader bmih; |
1001 | | - mxBitmapRGBQuad rgrgbPalette[256]; |
1002 | | - int cbBmpBits; |
1003 | | - byte *pbBmpBits; |
1004 | | - byte *pb, *pbHold; |
1005 | | - int cbPalBytes; |
1006 | | - int biTrueWidth; |
1007 | | - mxImage *image = nullptr; |
| 997 | + if (image->data) |
| 998 | + Mem_Free(image->data); |
1008 | 999 |
|
1009 | | - // File exists? |
1010 | | - if ((pfile = fopen (filename, "rb")) == 0) |
1011 | | - return 0; |
1012 | | - |
1013 | | - // Read file header |
1014 | | - if (fread(&bmfh, sizeof bmfh, 1/*count*/, pfile) != 1) { |
1015 | | - if (pfile) { |
1016 | | - fclose(pfile); |
1017 | | - } |
1018 | | - return nullptr; |
1019 | | - } |
| 1000 | + if (image->palette) |
| 1001 | + Mem_Free(image->palette); |
1020 | 1002 |
|
1021 | | - // Bogus file header check |
1022 | | - if (!(bmfh.bfReserved1 == 0 && bmfh.bfReserved2 == 0)) { |
1023 | | - if (pfile) { |
1024 | | - fclose(pfile); |
1025 | | - } |
1026 | | - return nullptr; |
1027 | | - } |
| 1003 | + image->data = Mem_Alloc(w * h * pixelSize / 8); |
| 1004 | + if (!image->data) |
| 1005 | + return false; |
1028 | 1006 |
|
1029 | | - // Read info header |
1030 | | - if (fread(&bmih, sizeof bmih, 1/*count*/, pfile) != 1) { |
1031 | | - if (pfile) { |
1032 | | - fclose(pfile); |
| 1007 | + // allocate a palette for 8-bit images |
| 1008 | + if (pixelSize == 8) |
| 1009 | + { |
| 1010 | + image->palette = Mem_Alloc(3 * 256); |
| 1011 | + if (!image->palette) |
| 1012 | + { |
| 1013 | + //delete[] data; |
| 1014 | + Mem_Free(image->data); |
| 1015 | + return false; |
1033 | 1016 | } |
1034 | | - return nullptr; |
1035 | 1017 | } |
1036 | | - |
1037 | | - // Bogus info header check |
1038 | | - if (!(bmih.biSize == sizeof bmih && bmih.biPlanes == 1)) { |
1039 | | - if (pfile) { |
1040 | | - fclose(pfile); |
1041 | | - } |
1042 | | - return nullptr; |
| 1018 | + else { |
| 1019 | + image->palette = nullptr; |
1043 | 1020 | } |
1044 | 1021 |
|
1045 | | - // Bogus bit depth? Only 8-bit supported. |
1046 | | - if (bmih.biBitCount != 8) { |
1047 | | - if (pfile) { |
1048 | | - fclose(pfile); |
1049 | | - } |
1050 | | - return nullptr; |
1051 | | - } |
| 1022 | + image->width = w; |
| 1023 | + image->height = h; |
| 1024 | + image->bpp = pixelSize; |
| 1025 | + return true; |
| 1026 | +}; |
1052 | 1027 |
|
1053 | | - // Bogus compression? Only non-compressed supported. |
1054 | | - if (bmih.biCompression != 0) { //BI_RGB) |
1055 | | - if (pfile) { |
1056 | | - fclose(pfile); |
1057 | | - } |
1058 | | - return nullptr; |
1059 | | - } |
| 1028 | +mxImage *GlWindow::readTextureFromFile(const char *filename) |
| 1029 | +{ |
| 1030 | + mxImage *image = nullptr; |
| 1031 | + rgbdata_t *imageData = nullptr; |
1060 | 1032 |
|
1061 | | - // Figure out how many entires are actually in the table |
1062 | | - if (bmih.biClrUsed == 0) |
| 1033 | + imageData = COM_LoadImage(filename, true); |
| 1034 | + if (!imageData) |
1063 | 1035 | { |
1064 | | - bmih.biClrUsed = 256; |
1065 | | - cbPalBytes = (1 << bmih.biBitCount) * sizeof (mxBitmapRGBQuad); |
1066 | | - } |
1067 | | - else |
1068 | | - { |
1069 | | - cbPalBytes = bmih.biClrUsed * sizeof (mxBitmapRGBQuad); |
| 1036 | + return nullptr; // File didn't exist, or can't be reached |
1070 | 1037 | } |
1071 | 1038 |
|
1072 | | - // Read palette (bmih.biClrUsed entries) |
1073 | | - if (fread(rgrgbPalette, cbPalBytes, 1/*count*/, pfile) != 1) { |
1074 | | - if (pfile) { |
1075 | | - fclose(pfile); |
1076 | | - } |
1077 | | - return nullptr; |
| 1039 | + imageData = Image_Quantize(imageData); |
| 1040 | + if (!(imageData->flags & IMAGE_QUANTIZED)) |
| 1041 | + { |
| 1042 | + return nullptr; // Quantizer failed |
1078 | 1043 | } |
1079 | 1044 |
|
1080 | 1045 | image = (mxImage *)Mem_Alloc(sizeof(mxImage)); |
1081 | 1046 | new (image) mxImage(); |
1082 | 1047 |
|
1083 | | - if (!image) |
| 1048 | + if (!image) |
1084 | 1049 | { |
1085 | | - if (pfile) { |
1086 | | - fclose(pfile); |
1087 | | - } |
1088 | | - return nullptr; |
| 1050 | + return nullptr; // image can't be allocated for some reason |
1089 | 1051 | } |
1090 | 1052 |
|
1091 | | - auto imageAlloc = [](mxImage *image, int w, int h, int pixelSize) { |
1092 | | - if (image->data) |
1093 | | - Mem_Free(image->data); |
1094 | | - |
1095 | | - if (image->palette) |
1096 | | - Mem_Free(image->palette); |
1097 | | - |
1098 | | - image->data = Mem_Alloc(w * h * pixelSize / 8); |
1099 | | - if (!image->data) |
1100 | | - return false; |
1101 | | - |
1102 | | - // allocate a palette for 8-bit images |
1103 | | - if (pixelSize == 8) |
1104 | | - { |
1105 | | - image->palette = Mem_Alloc(3 * 256); |
1106 | | - if (!image->palette) |
1107 | | - { |
1108 | | - //delete[] data; |
1109 | | - Mem_Free(image->data); |
1110 | | - return false; |
1111 | | - } |
1112 | | - } |
1113 | | - else { |
1114 | | - image->palette = nullptr; |
1115 | | - } |
1116 | | - |
1117 | | - image->width = w; |
1118 | | - image->height = h; |
1119 | | - image->bpp = pixelSize; |
1120 | | - return true; |
1121 | | - }; |
1122 | | - |
1123 | | - if (!imageAlloc(image, bmih.biWidth, bmih.biHeight, 8)) |
| 1053 | + if (!ImageLib_Alloc(image, imageData->width, imageData->height, 8)) |
1124 | 1054 | { |
1125 | 1055 | imageFree(image); |
1126 | | - if (pfile) { |
1127 | | - fclose(pfile); |
1128 | | - } |
1129 | 1056 | return nullptr; |
1130 | 1057 | } |
1131 | 1058 |
|
1132 | | - pb = (byte *) image->palette; |
| 1059 | + byte *paletteBuffer = (byte *) image->palette; |
1133 | 1060 |
|
1134 | 1061 | // Copy over used entries |
1135 | | - for (i = 0; i < (int) bmih.biClrUsed; i++) |
1136 | | - { |
1137 | | - *pb++ = rgrgbPalette[i].rgbRed; |
1138 | | - *pb++ = rgrgbPalette[i].rgbGreen; |
1139 | | - *pb++ = rgrgbPalette[i].rgbBlue; |
1140 | | - } |
1141 | | - |
1142 | | - // Fill in unused entires will 0,0,0 |
1143 | | - for (i = bmih.biClrUsed; i < 256; i++) |
1144 | | - { |
1145 | | - *pb++ = 0; |
1146 | | - *pb++ = 0; |
1147 | | - *pb++ = 0; |
1148 | | - } |
1149 | | - |
1150 | | - // Read bitmap bits (remainder of file) |
1151 | | - cbBmpBits = bmfh.bfSize - ftell (pfile); |
1152 | | - |
1153 | | - pbHold = pb = (byte *) Mem_Alloc(cbBmpBits * sizeof (byte)); |
1154 | | - if (pb == 0) |
1155 | | - { |
1156 | | - imageFree(image); |
1157 | | - if (pfile) { |
1158 | | - fclose(pfile); |
1159 | | - } |
1160 | | - return nullptr; |
1161 | | - } |
1162 | | - |
1163 | | - if (fread (pb, cbBmpBits, 1/*count*/, pfile) != 1) |
| 1062 | + for (int i = 0; i < 256; i++) |
1164 | 1063 | { |
1165 | | - Mem_Free(pbHold); |
1166 | | - imageFree(image); |
1167 | | - if (pfile) { |
1168 | | - fclose(pfile); |
1169 | | - } |
1170 | | - return nullptr; |
| 1064 | + *paletteBuffer++ = imageData->palette[i * 4 + 0]; |
| 1065 | + *paletteBuffer++ = imageData->palette[i * 4 + 1]; |
| 1066 | + *paletteBuffer++ = imageData->palette[i * 4 + 2]; |
1171 | 1067 | } |
1172 | | -/* |
1173 | | - pbBmpBits = malloc(cbBmpBits); |
1174 | | - if (pbBmpBits == 0) |
1175 | | - { |
1176 | | - free (pb); |
1177 | | - free (pbPal); |
1178 | | - if (pfile) { |
1179 | | - fclose(pfile); |
1180 | | - } |
1181 | | - return nullptr; |
1182 | | - } |
1183 | | -*/ |
1184 | | - pbBmpBits = (byte *) image->data; |
1185 | | - |
1186 | | - // data is actually stored with the width being rounded up to a multiple of 4 |
1187 | | - biTrueWidth = (bmih.biWidth + 3) & ~3; |
1188 | | - |
1189 | | - // reverse the order of the data. |
1190 | | - pb += (bmih.biHeight - 1) * biTrueWidth; |
1191 | | - for(i = 0; i < bmih.biHeight; i++) |
1192 | | - { |
1193 | | - memmove (&pbBmpBits[bmih.biWidth * i], pb, bmih.biWidth); |
1194 | | - pb -= biTrueWidth; |
1195 | | - } |
1196 | | - |
1197 | | - //pb += biTrueWidth; |
1198 | | - Mem_Free(pbHold); |
1199 | 1068 |
|
1200 | | - if (pfile) |
1201 | | - fclose (pfile); |
| 1069 | + memcpy(image->data, imageData->buffer, imageData->width * imageData->height); |
| 1070 | + Image_Free(imageData); |
1202 | 1071 |
|
1203 | 1072 | return image; |
1204 | 1073 | } |
|
0 commit comments