Annotation of mstools/samples/mandel/loadbmp.c, revision 1.1.1.3

1.1       root        1: /******************************Module*Header*******************************\
                      2: * Module Name: loadbmp.c
                      3: *
                      4: * Contains function that loads a bitmap file
                      5: *
                      6: * Created: 08-Jan-1992 11:06:37
                      7: * Author: Petrus Wong
                      8: *
                      9: * Copyright (c) 1990 Microsoft Corporation
                     10: *
                     11: * Contains the main routine for loading a DI bitmap file.
                     12: *
                     13: * Dependencies:
                     14: *
                     15: *   (#defines)
                     16: *   (#includes)
                     17: *       #include <windows.h>
                     18: *       #include "jtypes.h"
                     19: *
                     20: \**************************************************************************/
                     21: #include <windows.h>
                     22: #include "jtypes.h"
                     23: 
                     24: extern HWND ghwndMain;
1.1.1.3 ! root       25: extern VOID ErrorOut(char errstring[30]);
        !            26: extern BOOL bSelectDIBPal(HDC, PINFO, LPBITMAPINFO, BOOL);
        !            27: extern BOOL bFreeRleFile(PINFO);
1.1       root       28: BOOL LoadBitmapFile(HDC, PINFO, PSTR);
                     29: 
                     30: /******************************Public*Routine******************************\
                     31: *
                     32: * LoadBitmapFile
                     33: *
                     34: * Effects:  Loads the bitmap from file and put into pInfo->hBmpSaved
                     35: *
                     36: * Warnings: pszFileName contains the full path
                     37: *
                     38: * History:
1.1.1.3 ! root       39: *  08-Feb-1993      Petrus Wong         Keep DIB around for HT
        !            40: *  22-Jan-1993      Petrus Wong         16,24,32 PM
1.1       root       41: *  09-Jan-1992 -by- Petrus Wong
                     42: * Wrote it.
                     43: \**************************************************************************/
                     44: 
                     45: BOOL LoadBitmapFile(HDC hDC, PINFO pInfo, PSTR pszFileName)
                     46: {
1.1.1.3 ! root       47:     BOOL            bSuccess;
        !            48:     HANDLE          hFile, hMapFile;
        !            49:     LPVOID          pMapFile;
1.1       root       50:     LPBITMAPINFOHEADER pbmh;
1.1.1.3 ! root       51:     LPBITMAPINFO    pbmi;
        !            52:     PBYTE           pjTmp;
        !            53:     ULONG           sizBMI;
        !            54:     INT             iNumClr;
        !            55:     BOOL            bCoreHdr;
        !            56:     PFILEINFO       pFileInfo;
        !            57: 
1.1       root       58: 
                     59:     bSuccess = TRUE;
                     60: 
                     61:     if ((hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                     62:             OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL)) == (HANDLE)-1) {
                     63:         ErrorOut("Fail in file open");
                     64:         bSuccess = FALSE;
                     65:         goto ErrExit1;
                     66:     }
                     67: 
                     68:     //
                     69:     // Create a map file of the opened file
                     70:     //
                     71:     if ((hMapFile = CreateFileMapping(hFile, NULL,
1.1.1.3 ! root       72:                              PAGE_READONLY, 0, 0, NULL)) == (HANDLE)-1) {
1.1       root       73:         ErrorOut("Fail in creating map file");
                     74:         bSuccess = FALSE;
                     75:         goto ErrExit2;
                     76: 
                     77:     }
                     78: 
                     79:     //
                     80:     // Map a view of the whole file
                     81:     //
                     82:     if ((pMapFile = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0)) == NULL) {
                     83:         ErrorOut("Fail in mapping view of the Map File object");
                     84:         bSuccess = FALSE;
                     85:         goto ErrExit3;
                     86:     }
                     87: 
                     88:     //
1.1.1.3 ! root       89:     // Saving the DIB file handle, etc in pInfo...
        !            90:     // freeing existing objects, if any
        !            91:     //
        !            92:     bFreeRleFile(pInfo);
        !            93:     pFileInfo = &(pInfo->RleData.rgFileInfo[0]);
        !            94:     pFileInfo->hFile      = hFile;
        !            95:     pFileInfo->hMapFile   = hMapFile;
        !            96:     pFileInfo->lpvMapView = pMapFile;
        !            97: 
        !            98:     //
1.1       root       99:     // First check that it is a bitmap file
                    100:     //
                    101:     if (*((PWORD)pMapFile) != 0x4d42) {              // 'BM'
                    102:         MessageBox(ghwndMain, "This is not a DIB bitmap file!", "Error", MB_OK);
                    103:         bSuccess = FALSE;
                    104:         goto ErrExit3;
                    105:     }
                    106: 
                    107:     //
1.1.1.2   root      108:     // The file header doesn't end on DWORD boundary...
1.1       root      109:     //
1.1.1.2   root      110:     pbmh = (LPBITMAPINFOHEADER)((PBYTE)pMapFile + sizeof(BITMAPFILEHEADER));
                    111: 
1.1       root      112:     {
1.1.1.2   root      113:         BITMAPCOREHEADER bmch, *pbmch;
                    114:         BITMAPINFOHEADER bmih, *pbmih;
                    115:         PBYTE            pjTmp;
                    116:         ULONG            ulSiz;
                    117: 
                    118:         pbmch = &bmch;
                    119:         pbmih = &bmih;
                    120: 
                    121:         pjTmp = (PBYTE)pbmh;
                    122:         ulSiz = sizeof(BITMAPCOREHEADER);
                    123:         while (ulSiz--) {
                    124:             *(((PBYTE)pbmch)++) = *(((PBYTE)pjTmp)++);
                    125:         }
                    126: 
                    127:         pjTmp = (PBYTE)pbmh;
                    128:         ulSiz = sizeof(BITMAPINFOHEADER);
                    129:         while (ulSiz--) {
                    130:             *(((PBYTE)pbmih)++) = *(((PBYTE)pjTmp)++);
                    131:         }
                    132: 
                    133:         //
                    134:         // Use the size to determine if it is a BitmapCoreHeader or
                    135:         // BitmapInfoHeader
                    136:         //
1.1.1.3 ! root      137:         // Does PM supports 16 and 32 bpp? How?
        !           138:         //
1.1.1.2   root      139:         if (bmch.bcSize == sizeof(BITMAPCOREHEADER))
                    140:         {
                    141:             WORD wBitCount;
                    142: 
                    143:             wBitCount = bmch.bcBitCount;
                    144:             iNumClr = ((wBitCount == 24) ? 0 : (1 << wBitCount));
                    145:             sizBMI = sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*iNumClr;
1.1.1.3 ! root      146:             bCoreHdr = TRUE;
1.1.1.2   root      147:         }
                    148:         else            // BITMAPINFOHEADER
                    149:         {
                    150:             WORD wBitCount;
                    151: 
                    152:             wBitCount = bmih.biBitCount;
1.1.1.3 ! root      153:             switch (wBitCount) {
        !           154:                 case 16:
        !           155:                 case 32:
        !           156:                     sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3;
        !           157:                     break;
        !           158:                 case 24:
        !           159:                     sizBMI = sizeof(BITMAPINFOHEADER);
        !           160:                     break;
        !           161:                 default:
        !           162:                     iNumClr = (1 << wBitCount);
        !           163:                     sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*iNumClr;
        !           164:                     break;
        !           165:             }
        !           166:             bCoreHdr = FALSE;
1.1.1.2   root      167:         }
                    168: 
1.1       root      169:     }
                    170: 
                    171:     if ((pbmi = (LPBITMAPINFO) LocalAlloc(LMEM_FIXED,sizBMI)) == NULL) {
                    172:         MessageBox(ghwndMain, "Fail in Memory Allocation!", "Error", MB_OK);
                    173:         bSuccess = FALSE;
                    174:         goto ErrExit3;
                    175:     }
                    176: 
                    177:     //
                    178:     // Make sure we pass in a DWORD aligned BitmapInfo to CreateDIBitmap
                    179:     // Otherwise, exception on the MIPS platform
                    180:     // CR!!!  Equivalent to memcpy
                    181:     //
                    182:     pjTmp = (PBYTE)pbmi;
                    183: 
                    184:     while(sizBMI--)
                    185:     {
                    186:         *(((PBYTE)pjTmp)++) = *(((PBYTE)pbmh)++);
                    187:     }
1.1.1.2   root      188: 
1.1.1.3 ! root      189:     //
        !           190:     // assuming CreateDIBitmap() is doing a byte fetch...
        !           191:     //
1.1       root      192:     pMapFile = (PBYTE)pMapFile + ((BITMAPFILEHEADER *)pMapFile)->bfOffBits;
                    193: 
1.1.1.3 ! root      194:     //
        !           195:     // Select the palette into the DC first before CreateDIBitmap()
        !           196:     //
        !           197:     bSelectDIBPal(hDC, pInfo, pbmi, bCoreHdr);
1.1       root      198:     if ((pInfo->hBmpSaved = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)pbmi,
                    199:                         CBM_INIT, pMapFile, pbmi, DIB_RGB_COLORS)) == NULL) {
                    200:         ErrorOut("Fail in creating DIB bitmap from file!");
                    201:         bSuccess = FALSE;
                    202:         goto ErrExit4;
                    203:     }
                    204: 
1.1.1.3 ! root      205:     //
        !           206:     // Saving the DIB...free memory when the windows is closed.
        !           207:     //
        !           208:     pInfo->RleData.rgpjFrame[0] = pMapFile;
        !           209:     pInfo->RleData.rgpbmi[0]    = pbmi;
        !           210:     pInfo->RleData.pbmi         = (PBITMAPINFO) &(pInfo->RleData.rgpbmi[0]);
        !           211:     pInfo->RleData.ulFrames     = 1;
        !           212:     pInfo->RleData.ulFiles      = 1;
1.1       root      213: 
1.1.1.3 ! root      214:     // set flag to use original DIB as source for blting so HT can be done
        !           215:     pInfo->bUseDIB = TRUE;
        !           216: 
        !           217:     pInfo->bCoreHdr = bCoreHdr;
        !           218: 
        !           219:     return (bSuccess);
1.1       root      220: 
                    221: ErrExit4:
                    222:     LocalFree(pbmi);
                    223: ErrExit3:
                    224:     CloseHandle(hMapFile);
                    225: ErrExit2:
                    226:     CloseHandle(hFile);
                    227: ErrExit1:
                    228: 
                    229:     return (bSuccess);
                    230: 
                    231: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.