[351] | 1 |
|
---|
| 2 | /***********************************************************************
|
---|
| 3 |
|
---|
| 4 | $Id: loadbmp.c 907 2008-01-06 07:26:17Z stevenhl $
|
---|
| 5 |
|
---|
| 6 | Load bitmaps
|
---|
| 7 |
|
---|
| 8 | Copyright (c) 1993-98 M. Kimes
|
---|
[549] | 9 | Copyright (c) 2006, 2007 Steven H.Levine
|
---|
[351] | 10 |
|
---|
| 11 | 22 Jul 06 SHL Check more run time errors
|
---|
[549] | 12 | 16 Jan 07 SHL Check more run time errors
|
---|
| 13 | 16 Jan 07 SHL Sync variable names for sanity
|
---|
| 14 | 16 Jan 07 SHL Open bitmap file binary - no wonder the code does not work
|
---|
| 15 | 16 Jan 07 SHL Beautify with indent -i2
|
---|
[351] | 16 |
|
---|
| 17 | ***********************************************************************/
|
---|
| 18 |
|
---|
[2] | 19 | #include <stdlib.h>
|
---|
| 20 | #include <stdio.h>
|
---|
| 21 | #include <string.h>
|
---|
| 22 | #include <share.h>
|
---|
[351] | 23 |
|
---|
[907] | 24 | #define INCL_DOS
|
---|
| 25 | #define INCL_WIN
|
---|
| 26 | #define INCL_GPI
|
---|
| 27 | #define INCL_LONGLONG // dircnrs.h
|
---|
| 28 |
|
---|
| 29 | #include "errutil.h" // Dos_Error...
|
---|
[2] | 30 | #include "fm3dll.h"
|
---|
| 31 |
|
---|
[351] | 32 | static PSZ pszSrcFile = __FILE__;
|
---|
| 33 |
|
---|
[549] | 34 | HBITMAP LoadBitmapFromFileNum(USHORT id)
|
---|
[351] | 35 | {
|
---|
[549] | 36 | char s[CCHMAXPATH];
|
---|
[2] | 37 |
|
---|
| 38 | save_dir2(s);
|
---|
[549] | 39 | sprintf(s + strlen(s), "\\%u.BMP", id);
|
---|
[2] | 40 | return LoadBitmapFromFile(s);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[549] | 43 | HBITMAP LoadBitmapFromFile(CHAR * pszFileName)
|
---|
[351] | 44 | {
|
---|
[549] | 45 | HBITMAP hBmp = (HBITMAP) 0;
|
---|
| 46 | FILE *pf;
|
---|
| 47 | ULONG rc;
|
---|
| 48 | USHORT usType;
|
---|
| 49 | PBITMAPARRAYFILEHEADER2 pbmafh2 = NULL; // Must init for xfree
|
---|
[551] | 50 | PBITMAPFILEHEADER2 pbmfh2; // No color table
|
---|
| 51 | PBITMAPINFOHEADER2 pbmih2; // No color table
|
---|
| 52 | PBITMAPINFO2 pbmi2; // Includes color table
|
---|
| 53 | BOOL is2x; // Format 1.x or 2.x
|
---|
[549] | 54 | ULONG ulColors;
|
---|
| 55 | ULONG ulRGBOffset;
|
---|
[551] | 56 | PBYTE pData = NULL; // Must init for xfree
|
---|
[549] | 57 | ULONG ulDataSize;
|
---|
| 58 | SIZEL sizel;
|
---|
| 59 | HPS hPS = WinGetPS(HWND_DESKTOP);
|
---|
[2] | 60 |
|
---|
[549] | 61 | if (!hPS) {
|
---|
[551] | 62 | Win_Error(HWND_DESKTOP, HWND_DESKTOP, pszSrcFile, __LINE__, "WinGetPS");
|
---|
[2] | 63 | goto ExitLoadBMP;
|
---|
[549] | 64 | }
|
---|
[2] | 65 |
|
---|
[549] | 66 | pf = _fsopen(pszFileName, "rb", SH_DENYWR);
|
---|
| 67 | if (!pf) {
|
---|
| 68 | // OK for file to not exist - enable following for debug as needed
|
---|
| 69 | // Runtime_Error(pszSrcFile, __LINE__, "_fsopen %s", pszFileName);
|
---|
[2] | 70 | goto ExitLoadBMP;
|
---|
[549] | 71 | }
|
---|
[2] | 72 |
|
---|
[549] | 73 | // Read image type
|
---|
| 74 | // fixme to just read type2 header - this is silly and wastes time
|
---|
| 75 | rc = fread(&usType, 1, sizeof(usType), pf);
|
---|
| 76 | if (rc != sizeof(usType)) {
|
---|
| 77 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
[2] | 78 | goto ExitLoadBMP;
|
---|
[549] | 79 | }
|
---|
| 80 |
|
---|
| 81 | /* Read bitmap info header
|
---|
| 82 | Allocate enough to hold a complete 2.x bitmap array file header
|
---|
| 83 | fixme to support > 256 colors?
|
---|
[551] | 84 | */
|
---|
[549] | 85 | pbmafh2 =
|
---|
| 86 | xmalloc(sizeof(*pbmafh2) + 256 * sizeof(RGB2), pszSrcFile, __LINE__);
|
---|
| 87 | if (!pbmafh2)
|
---|
| 88 | goto ExitLoadBMP;
|
---|
| 89 | /* Assign pointers to the file header and bitmap info header etc.
|
---|
| 90 | Both the 1.x and 2.x structures are assigned to simplify code
|
---|
| 91 | fixme to clean this up - aliased pointers are evil
|
---|
[551] | 92 | */
|
---|
[549] | 93 | pbmfh2 = &pbmafh2->bfh2;
|
---|
| 94 | pbmih2 = &pbmfh2->bmp2;
|
---|
[551] | 95 | pbmi2 = (PBITMAPINFO2) pbmih2;
|
---|
[549] | 96 |
|
---|
[2] | 97 | switch (usType) {
|
---|
[549] | 98 | case BFT_BMAP:
|
---|
| 99 | case BFT_ICON:
|
---|
| 100 | case BFT_POINTER:
|
---|
| 101 | case BFT_COLORICON:
|
---|
| 102 | case BFT_COLORPOINTER:
|
---|
| 103 | {
|
---|
| 104 | /* Assume image is a 2.0 image and read as a 2.x header
|
---|
[551] | 105 | OK for 1.x file - read will not fail unless file is corrupted
|
---|
| 106 | */
|
---|
[549] | 107 | rc = fseek(pf, 0, SEEK_SET);
|
---|
| 108 | if (rc) {
|
---|
| 109 | Runtime_Error(pszSrcFile, __LINE__, "fseek 0");
|
---|
| 110 | goto ExitLoadBMP;
|
---|
| 111 | }
|
---|
[2] | 112 |
|
---|
[549] | 113 | rc = fread(pbmfh2, 1, sizeof(*pbmfh2), pf);
|
---|
| 114 | if (rc != sizeof(*pbmfh2)) {
|
---|
| 115 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
| 116 | goto ExitLoadBMP;
|
---|
[2] | 117 | }
|
---|
| 118 |
|
---|
[549] | 119 | is2x = pbmih2->cbFix > sizeof(BITMAPINFOHEADER); // 1.x or 2.x bitmap
|
---|
| 120 | /* We will read the color table later
|
---|
[551] | 121 | Color table follows header but
|
---|
| 122 | location depends on the type of the bitmap (old vs new)
|
---|
| 123 | 1.x header is fixed size
|
---|
| 124 | 2.x header is variable sized, so offset must be calculated
|
---|
| 125 | cbFix contains actual size of BITMAPINFOHEADER2 in file
|
---|
| 126 | */
|
---|
[549] | 127 | ulRGBOffset = is2x ? sizeof(*pbmfh2) - sizeof(*pbmih2) + pbmih2->cbFix :
|
---|
[551] | 128 | sizeof(BITMAPFILEHEADER);
|
---|
[549] | 129 | }
|
---|
| 130 | break;
|
---|
[2] | 131 |
|
---|
[549] | 132 | case BFT_BITMAPARRAY:
|
---|
| 133 | {
|
---|
| 134 | /* Now we are dealing with a bitmap array which is a collection of bitmaps
|
---|
[551] | 135 | Each bitmap has its own file header
|
---|
| 136 | */
|
---|
[2] | 137 |
|
---|
[549] | 138 | ULONG ulCurOffset;
|
---|
| 139 | ULONG clScreenWidth;
|
---|
| 140 | ULONG clScreenHeight;
|
---|
| 141 | ULONG ulDeviceColors;
|
---|
| 142 | ULONG ulSizeDiff;
|
---|
| 143 | ULONG ulOffsetPicked = 0;
|
---|
| 144 | ULONG ulColorsPicked;
|
---|
| 145 | ULONG ulSizeDiffPicked;
|
---|
| 146 | HDC hdc;
|
---|
[2] | 147 |
|
---|
[549] | 148 | /* Scan the array and chose the bitmap best suited
|
---|
[551] | 149 | for the current display size and color capacities
|
---|
| 150 | */
|
---|
[549] | 151 | hdc = GpiQueryDevice(hPS);
|
---|
| 152 | if (!hdc) {
|
---|
| 153 | Win_Error(HWND_DESKTOP, HWND_DESKTOP, pszSrcFile, __LINE__,
|
---|
| 154 | "GpiQueryDevice");
|
---|
| 155 | goto ExitLoadBMP;
|
---|
| 156 | }
|
---|
[551] | 157 | DevQueryCaps(hdc, CAPS_COLORS, 1, (PLONG) & ulDeviceColors);
|
---|
| 158 | DevQueryCaps(hdc, CAPS_WIDTH, 1, (PLONG) & clScreenWidth);
|
---|
| 159 | DevQueryCaps(hdc, CAPS_HEIGHT, 1, (PLONG) & clScreenHeight);
|
---|
[549] | 160 | pbmafh2->offNext = 0;
|
---|
| 161 | do {
|
---|
| 162 | ulCurOffset = pbmafh2->offNext;
|
---|
| 163 | rc = fseek(pf, pbmafh2->offNext, SEEK_SET);
|
---|
| 164 | if (rc) {
|
---|
| 165 | Runtime_Error(pszSrcFile, __LINE__, "fseek %ld", pbmafh2->offNext);
|
---|
| 166 | goto ExitLoadBMP;
|
---|
| 167 | }
|
---|
| 168 | rc = fread(pbmafh2, 1, sizeof(*pbmafh2), pf);
|
---|
| 169 | if (rc != sizeof(*pbmafh2)) {
|
---|
| 170 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
| 171 | goto ExitLoadBMP;
|
---|
| 172 | }
|
---|
| 173 | is2x = pbmih2->cbFix > sizeof(BITMAPINFOHEADER);
|
---|
| 174 | if (is2x) {
|
---|
| 175 | ulColors = 1 << (pbmafh2->bfh2.bmp2.cBitCount *
|
---|
| 176 | pbmafh2->bfh2.bmp2.cPlanes);
|
---|
| 177 | }
|
---|
| 178 | else {
|
---|
[551] | 179 | ulColors =
|
---|
| 180 | 1 << (((PBITMAPARRAYFILEHEADER) pbmafh2)->bfh.bmp.cBitCount *
|
---|
| 181 | ((PBITMAPARRAYFILEHEADER) pbmafh2)->bfh.bmp.cPlanes);
|
---|
[549] | 182 | }
|
---|
| 183 | if (pbmafh2->cxDisplay == 0 && pbmafh2->cyDisplay == 0) {
|
---|
| 184 | // This is a device independant bitmap - process it as a VGA
|
---|
| 185 | pbmafh2->cxDisplay = 640;
|
---|
| 186 | pbmafh2->cyDisplay = 480;
|
---|
| 187 | }
|
---|
| 188 | ulSizeDiff = abs(pbmafh2->cxDisplay - clScreenWidth) +
|
---|
[551] | 189 | abs(pbmafh2->cyDisplay - clScreenHeight);
|
---|
[549] | 190 | if (ulDeviceColors == ulColors && ulSizeDiff == 0) {
|
---|
| 191 | // We found the perfect match
|
---|
| 192 | ulOffsetPicked = ulCurOffset;
|
---|
| 193 | break; // Stop scan
|
---|
| 194 | }
|
---|
| 195 | if (ulOffsetPicked == 0 || // First time thru
|
---|
| 196 | ulSizeDiff < ulSizeDiffPicked || // Better fit than any previous
|
---|
| 197 | (ulColors > ulColorsPicked && ulColors < ulDeviceColors) || // More colors than prev & less than device
|
---|
[551] | 198 | (ulColors < ulColorsPicked && ulColors > ulDeviceColors)) {
|
---|
[549] | 199 | ulOffsetPicked = ulCurOffset; // Make this our current pick
|
---|
| 200 | ulColorsPicked = ulColors;
|
---|
| 201 | ulSizeDiffPicked = ulSizeDiff;
|
---|
| 202 | }
|
---|
| 203 | } while (pbmafh2->offNext != 0);
|
---|
[2] | 204 |
|
---|
[549] | 205 | // Retrieve the selected bitmap
|
---|
| 206 | rc = fseek(pf, ulOffsetPicked, SEEK_SET);
|
---|
| 207 | if (rc) {
|
---|
| 208 | Runtime_Error(pszSrcFile, __LINE__, "fseek %ld", ulOffsetPicked);
|
---|
| 209 | goto ExitLoadBMP;
|
---|
[2] | 210 | }
|
---|
[549] | 211 | rc = fread(pbmafh2, 1, sizeof(*pbmafh2), pf);
|
---|
| 212 | if (rc != sizeof(*pbmafh2)) {
|
---|
| 213 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
| 214 | goto ExitLoadBMP;
|
---|
| 215 | }
|
---|
[2] | 216 |
|
---|
[549] | 217 | is2x = pbmih2->cbFix > sizeof(BITMAPINFOHEADER);
|
---|
| 218 | /* As before, we calculate offset in file stream to color table
|
---|
[551] | 219 | This code must match single bitmap logic
|
---|
| 220 | */
|
---|
[549] | 221 | ulRGBOffset = ulOffsetPicked;
|
---|
[551] | 222 | ulRGBOffset +=
|
---|
| 223 | is2x ? sizeof(*pbmafh2) - sizeof(*pbmih2) +
|
---|
| 224 | pbmih2->cbFix : sizeof(BITMAPARRAYFILEHEADER);
|
---|
[549] | 225 | }
|
---|
| 226 | break;
|
---|
[2] | 227 |
|
---|
[549] | 228 | default:
|
---|
| 229 | Runtime_Error(pszSrcFile, __LINE__, "Bad type %u", usType);
|
---|
[2] | 230 | goto ExitLoadBMP;
|
---|
[549] | 231 | } // endswitch
|
---|
[2] | 232 |
|
---|
[549] | 233 | // Position to color table
|
---|
| 234 | rc = fseek(pf, ulRGBOffset, SEEK_SET);
|
---|
| 235 | if (rc) {
|
---|
| 236 | Runtime_Error(pszSrcFile, __LINE__, "fseek %ld", ulRGBOffset);
|
---|
| 237 | goto ExitLoadBMP;
|
---|
| 238 | }
|
---|
[2] | 239 |
|
---|
[549] | 240 | // Read color table
|
---|
| 241 | if (is2x) {
|
---|
| 242 | /* For a 2.0 bitmap, read the color table as is
|
---|
| 243 | The bitmap info structure is header + color table
|
---|
| 244 | If we have 24 bits per pel, there is usually no color table, unless
|
---|
| 245 | pbmih2->cclrUsed or pbmih2->cclrImportant are non zero
|
---|
| 246 | fixme to test this
|
---|
[551] | 247 | */
|
---|
[549] | 248 | if (pbmih2->cBitCount < 24) {
|
---|
| 249 | ULONG ulRGBBytes;
|
---|
[551] | 250 |
|
---|
[549] | 251 | ulColors = 1L << pbmih2->cBitCount;
|
---|
[2] | 252 |
|
---|
[549] | 253 | if (ulColors > 256) {
|
---|
[551] | 254 | Runtime_Error(pszSrcFile, __LINE__, "RGB exceeds 256 colors: %lu",
|
---|
| 255 | ulColors);
|
---|
[549] | 256 | goto ExitLoadBMP;
|
---|
| 257 | }
|
---|
| 258 | ulRGBBytes = ulColors * sizeof(RGB2);
|
---|
| 259 | rc = fread(&pbmi2->argbColor[0], 1, ulRGBBytes, pf);
|
---|
| 260 | if (rc != ulRGBBytes) {
|
---|
| 261 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
| 262 | goto ExitLoadBMP;
|
---|
| 263 | }
|
---|
| 264 | } // endif
|
---|
| 265 | // Get pointer to bitmap info (header and color table)
|
---|
[551] | 266 | pbmi2 = (PBITMAPINFO2) pbmih2;
|
---|
[2] | 267 | }
|
---|
| 268 | else {
|
---|
[549] | 269 | /* This is a 1.x format bitmap
|
---|
| 270 | Since the current standard format is the 2.0
|
---|
| 271 | convert the header and color table to 2.x format
|
---|
[551] | 272 | */
|
---|
[549] | 273 | ULONG ul;
|
---|
| 274 | RGB rgb;
|
---|
[551] | 275 | PBITMAPINFOHEADER pbmih = &((PBITMAPARRAYFILEHEADER) pbmafh2)->bfh.bmp;
|
---|
[2] | 276 |
|
---|
[549] | 277 | if (pbmih->cBitCount < 24) {
|
---|
| 278 | ulColors = 1 << pbmih->cBitCount;
|
---|
| 279 | if (ulColors > 256) {
|
---|
[551] | 280 | Runtime_Error(pszSrcFile, __LINE__, "RGB exceeds 256 colors: %lu",
|
---|
| 281 | ulColors);
|
---|
[549] | 282 | goto ExitLoadBMP;
|
---|
| 283 | }
|
---|
| 284 | // Read in 1.x color table and reformat for 2.x
|
---|
| 285 | for (ul = 0; ul < ulColors; ul++) {
|
---|
| 286 | fread(&rgb, 1, sizeof(rgb), pf);
|
---|
| 287 | pbmi2->argbColor[ul].bRed = rgb.bRed;
|
---|
| 288 | pbmi2->argbColor[ul].bGreen = rgb.bGreen;
|
---|
| 289 | pbmi2->argbColor[ul].bBlue = rgb.bBlue;
|
---|
| 290 | pbmi2->argbColor[ul].fcOptions = 0; // initialize 2.x extra byte to 0
|
---|
[551] | 291 | } // for
|
---|
[549] | 292 | }
|
---|
[2] | 293 |
|
---|
[549] | 294 | // Convert the old style to the new header format
|
---|
| 295 | pbmi2->cbFix = sizeof(BITMAPINFOHEADER2);
|
---|
| 296 | pbmi2->cBitCount = pbmih->cBitCount;
|
---|
| 297 | pbmi2->cPlanes = pbmih->cPlanes;
|
---|
| 298 | pbmi2->cy = pbmih->cy;
|
---|
| 299 | pbmi2->cx = pbmih->cx;
|
---|
[2] | 300 | // set rest to zero
|
---|
[551] | 301 | memset((PCHAR) pbmi2 + 16, 0, sizeof(BITMAPINFOHEADER2) - 16);
|
---|
| 302 | } // if 1.x
|
---|
[2] | 303 |
|
---|
[549] | 304 | /* The 2.0 bitmap info structure set up
|
---|
| 305 | Position to start of the bitmap data
|
---|
[551] | 306 | */
|
---|
[549] | 307 | rc = fseek(pf, pbmfh2->offBits, SEEK_SET);
|
---|
| 308 | if (rc) {
|
---|
| 309 | Runtime_Error(pszSrcFile, __LINE__, "fseek %ld", pbmfh2->offBits);
|
---|
[2] | 310 | goto ExitLoadBMP;
|
---|
[549] | 311 | }
|
---|
[2] | 312 |
|
---|
[549] | 313 | /* Read the bitmap data
|
---|
| 314 | The read size is derived using the magic formula
|
---|
| 315 | Each bitmap scan line is aligned on a doubleword boundary
|
---|
| 316 | The size of the scan line is the number of pels times the bpp
|
---|
| 317 | After aligning it, we divide by 4 to get the number of bytes, and
|
---|
| 318 | multiply by the number of scan lines and the number of pel planes
|
---|
[551] | 319 | */
|
---|
[549] | 320 | if (pbmi2->ulCompression)
|
---|
[2] | 321 | ulDataSize = pbmi2->cbImage;
|
---|
| 322 | else
|
---|
| 323 | ulDataSize = (((pbmi2->cBitCount * pbmi2->cx) + 31) / 32) * 4 *
|
---|
[549] | 324 | pbmi2->cy * pbmi2->cPlanes;
|
---|
| 325 | pData = xmalloc(ulDataSize, pszSrcFile, __LINE__);
|
---|
| 326 | if (!pData)
|
---|
[2] | 327 | goto ExitLoadBMP;
|
---|
[549] | 328 | rc = fread(pData, 1, ulDataSize, pf);
|
---|
| 329 | if (rc != ulDataSize) {
|
---|
| 330 | Runtime_Error(pszSrcFile, __LINE__, "fread");
|
---|
[2] | 331 | goto ExitLoadBMP;
|
---|
[549] | 332 | }
|
---|
[2] | 333 |
|
---|
[549] | 334 | // Create the GPI bitmap image
|
---|
[2] | 335 | sizel.cx = pbmi2->cx;
|
---|
| 336 | sizel.cy = pbmi2->cy;
|
---|
| 337 |
|
---|
[549] | 338 | hBmp = GpiCreateBitmap(hPS, (PBITMAPINFOHEADER2) pbmi2, CBM_INIT,
|
---|
| 339 | pData, pbmi2);
|
---|
| 340 | if (!hBmp)
|
---|
| 341 | Win_Error(HWND_DESKTOP, HWND_DESKTOP, pszSrcFile, __LINE__,
|
---|
| 342 | "GpiCreateBitmap");
|
---|
[2] | 343 |
|
---|
| 344 | ExitLoadBMP:
|
---|
[351] | 345 |
|
---|
| 346 | xfree(pData);
|
---|
[549] | 347 | xfree(pbmafh2);
|
---|
| 348 | if (pf)
|
---|
| 349 | fclose(pf);
|
---|
| 350 | if (hPS)
|
---|
| 351 | WinReleasePS(hPS);
|
---|
[351] | 352 | return hBmp;
|
---|
[2] | 353 | }
|
---|
[793] | 354 |
|
---|
| 355 | #pragma alloc_text(LOADBITMAP,LoadBitmapFromFile,LoadBitmapFromFileNum)
|
---|