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