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

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;
        !            25: extern VOID ErrorOut();
        !            26: BOOL LoadBitmapFile(HDC, PINFO, PSTR);
        !            27: 
        !            28: /******************************Public*Routine******************************\
        !            29: *
        !            30: * LoadBitmapFile
        !            31: *
        !            32: * Effects:  Loads the bitmap from file and put into pInfo->hBmpSaved
        !            33: *
        !            34: * Warnings: pszFileName contains the full path
        !            35: *
        !            36: * History:
        !            37: *  09-Jan-1992 -by- Petrus Wong
        !            38: * Wrote it.
        !            39: \**************************************************************************/
        !            40: 
        !            41: BOOL LoadBitmapFile(HDC hDC, PINFO pInfo, PSTR pszFileName)
        !            42: {
        !            43:     BOOL bSuccess;
        !            44:     HANDLE hFile, hMapFile;
        !            45:     LPVOID pMapFile;
        !            46:     LPBITMAPINFOHEADER pbmh;
        !            47:     LPBITMAPINFO  pbmi;
        !            48:     PBYTE pjTmp;
        !            49:     ULONG sizBMI;
        !            50: 
        !            51:     bSuccess = TRUE;
        !            52: 
        !            53:     if ((hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
        !            54:             OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL)) == (HANDLE)-1) {
        !            55:         ErrorOut("Fail in file open");
        !            56:         bSuccess = FALSE;
        !            57:         goto ErrExit1;
        !            58:     }
        !            59: 
        !            60:     //
        !            61:     // Create a map file of the opened file
        !            62:     //
        !            63:     if ((hMapFile = CreateFileMapping(hFile, NULL,
        !            64:                              PAGE_READONLY, 0, 0, "MapF")) == (HANDLE)-1) {
        !            65:         ErrorOut("Fail in creating map file");
        !            66:         bSuccess = FALSE;
        !            67:         goto ErrExit2;
        !            68: 
        !            69:     }
        !            70: 
        !            71:     //
        !            72:     // Map a view of the whole file
        !            73:     //
        !            74:     if ((pMapFile = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0)) == NULL) {
        !            75:         ErrorOut("Fail in mapping view of the Map File object");
        !            76:         bSuccess = FALSE;
        !            77:         goto ErrExit3;
        !            78:     }
        !            79: 
        !            80:     //
        !            81:     // First check that it is a bitmap file
        !            82:     //
        !            83:     if (*((PWORD)pMapFile) != 0x4d42) {              // 'BM'
        !            84:         MessageBox(ghwndMain, "This is not a DIB bitmap file!", "Error", MB_OK);
        !            85:         bSuccess = FALSE;
        !            86:         goto ErrExit3;
        !            87:     }
        !            88: 
        !            89:     pbmh = (LPBITMAPINFOHEADER)((PBYTE)pMapFile + sizeof(BITMAPFILEHEADER));
        !            90: 
        !            91:     //
        !            92:     // Use the size to determine if it is a BitmapCoreHeader or
        !            93:     // BitmapInfoHeader
        !            94:     //
        !            95:     if (pbmh->biSize == sizeof(BITMAPCOREHEADER))
        !            96:     {
        !            97:         sizBMI = sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*
        !            98:             ((((LPBITMAPCOREHEADER)pbmh)->bcBitCount == 24) ? 0 : (1 << ((LPBITMAPCOREHEADER)pbmh)->bcBitCount));
        !            99:     }
        !           100:     else            // BITMAPINFOHEADER
        !           101:     {
        !           102:         sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*
        !           103:                  ((pbmh->biBitCount == 24) ? 0 : (1 << pbmh->biBitCount));
        !           104:     }
        !           105: 
        !           106:     if ((pbmi = (LPBITMAPINFO) LocalAlloc(LMEM_FIXED,sizBMI)) == NULL) {
        !           107:         MessageBox(ghwndMain, "Fail in Memory Allocation!", "Error", MB_OK);
        !           108:         bSuccess = FALSE;
        !           109:         goto ErrExit3;
        !           110:     }
        !           111: 
        !           112:     //
        !           113:     // Make sure we pass in a DWORD aligned BitmapInfo to CreateDIBitmap
        !           114:     // Otherwise, exception on the MIPS platform
        !           115:     // CR!!!  Equivalent to memcpy
        !           116:     //
        !           117:     pjTmp = (PBYTE)pbmi;
        !           118: 
        !           119:     while(sizBMI--)
        !           120:     {
        !           121:         *(((PBYTE)pjTmp)++) = *(((PBYTE)pbmh)++);
        !           122:     }
        !           123: 
        !           124:     pMapFile = (PBYTE)pMapFile + ((BITMAPFILEHEADER *)pMapFile)->bfOffBits;
        !           125: 
        !           126:     if ((pInfo->hBmpSaved = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)pbmi,
        !           127:                         CBM_INIT, pMapFile, pbmi, DIB_RGB_COLORS)) == NULL) {
        !           128:         ErrorOut("Fail in creating DIB bitmap from file!");
        !           129:         bSuccess = FALSE;
        !           130:         goto ErrExit4;
        !           131:     }
        !           132: 
        !           133: 
        !           134: 
        !           135: ErrExit4:
        !           136:     LocalFree(pbmi);
        !           137: ErrExit3:
        !           138:     CloseHandle(hMapFile);
        !           139: ErrExit2:
        !           140:     CloseHandle(hFile);
        !           141: ErrExit1:
        !           142: 
        !           143:     return (bSuccess);
        !           144: 
        !           145: }

unix.superglobalmegacorp.com

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