home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / mandel / loadbmp.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  6KB  |  232 lines

  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. *
  8. * Copyright (C) 1993-1997 Microsoft Corporation
  9. *
  10. * Contains the main routine for loading a DI bitmap file.
  11. *
  12. * Dependencies:
  13. *
  14. *   (#defines)
  15. *   (#includes)
  16. *       #include <windows.h>
  17. *       #include "jtypes.h"
  18. *
  19. \**************************************************************************/
  20. #include <windows.h>
  21. #include "julia.h"
  22.  
  23. extern HWND ghwndMain;
  24. BOOL bSelectDIBPal(HDC, PINFO, LPBITMAPINFO, BOOL);
  25. BOOL bFreeRleFile(PINFO);
  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. \**************************************************************************/
  37.  
  38. BOOL LoadBitmapFile(HDC hDC, PINFO pInfo, PSTR pszFileName)
  39. {
  40.     BOOL            bSuccess;
  41.     HANDLE          hFile, hMapFile;
  42.     LPVOID          pMapFile;
  43.     LPBITMAPINFOHEADER pbmh;
  44.     LPBITMAPINFO    pbmi;
  45.     PBYTE           pjTmp;
  46.     ULONG           sizBMI;
  47.     INT             iNumClr;
  48.     BOOL            bCoreHdr;
  49.     PFILEINFO       pFileInfo;
  50.  
  51.  
  52.     bSuccess = TRUE;
  53.  
  54.     if ((hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  55.             OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL)) == (HANDLE)-1) 
  56.     {
  57.         OutputDebugString("Fail in file open");
  58.         bSuccess = FALSE;
  59.         goto ErrExit1;
  60.     }
  61.  
  62.     //
  63.     // Create a map file of the opened file
  64.     //
  65.     if ((hMapFile = CreateFileMapping(hFile, NULL,
  66.                              PAGE_READONLY, 0, 0, NULL)) == (HANDLE)-1) 
  67.     {
  68.         OutputDebugString("Fail in creating map file");
  69.         bSuccess = FALSE;
  70.         goto ErrExit2;
  71.  
  72.     }
  73.  
  74.     //
  75.     // Map a view of the whole file
  76.     //
  77.     if ((pMapFile = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0)) == NULL) 
  78.     {
  79.         OutputDebugString("Fail in mapping view of the Map File object");
  80.         bSuccess = FALSE;
  81.         goto ErrExit3;
  82.     }
  83.  
  84.     //
  85.     // Saving the DIB file handle, etc in pInfo...
  86.     // freeing existing objects, if any
  87.     //
  88.     bFreeRleFile(pInfo);
  89.     pFileInfo = &(pInfo->RleData.rgFileInfo[0]);
  90.     pFileInfo->hFile      = hFile;
  91.     pFileInfo->hMapFile   = hMapFile;
  92.     pFileInfo->lpvMapView = pMapFile;
  93.  
  94.     //
  95.     // First check that it is a bitmap file
  96.     //
  97.     if (*((PWORD)pMapFile) != 0x4d42)  // 'BM'
  98.     {              
  99.         MessageBox(ghwndMain, 
  100.                    GetStringRes (IDS_ERR_NOT_A_DIB),
  101.                    NULL, MB_OK);
  102.         bSuccess = FALSE;
  103.         goto ErrExit3;
  104.     }
  105.  
  106.     //
  107.     // The file header doesn't end on DWORD boundary...
  108.     //
  109.     pbmh = (LPBITMAPINFOHEADER)((PBYTE)pMapFile + sizeof(BITMAPFILEHEADER));
  110.  
  111.     {
  112.         BITMAPCOREHEADER bmch, *pbmch;
  113.         BITMAPINFOHEADER bmih, *pbmih;
  114.         PBYTE            pjTmp;
  115.         ULONG            ulSiz;
  116.  
  117.         pbmch = &bmch;
  118.         pbmih = &bmih;
  119.  
  120.         pjTmp = (PBYTE)pbmh;
  121.         ulSiz = sizeof(BITMAPCOREHEADER);
  122.         while (ulSiz--) {
  123.             *(((PBYTE)pbmch)++) = *(((PBYTE)pjTmp)++);
  124.         }
  125.  
  126.         pjTmp = (PBYTE)pbmh;
  127.         ulSiz = sizeof(BITMAPINFOHEADER);
  128.         while (ulSiz--) {
  129.             *(((PBYTE)pbmih)++) = *(((PBYTE)pjTmp)++);
  130.         }
  131.  
  132.         //
  133.         // Use the size to determine if it is a BitmapCoreHeader or
  134.         // BitmapInfoHeader
  135.         //
  136.         // Does PM supports 16 and 32 bpp? How?
  137.         //
  138.         if (bmch.bcSize == sizeof(BITMAPCOREHEADER))
  139.         {
  140.             WORD wBitCount;
  141.  
  142.             wBitCount = bmch.bcBitCount;
  143.             iNumClr = ((wBitCount == 24) ? 0 : (1 << wBitCount));
  144.             sizBMI = sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*iNumClr;
  145.             bCoreHdr = TRUE;
  146.         }
  147.         else            // BITMAPINFOHEADER
  148.         {
  149.             WORD wBitCount;
  150.  
  151.             wBitCount = bmih.biBitCount;
  152.             switch (wBitCount) {
  153.                 case 16:
  154.                 case 32:
  155.                     sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3;
  156.                     break;
  157.                 case 24:
  158.                     sizBMI = sizeof(BITMAPINFOHEADER);
  159.                     break;
  160.                 default:
  161.                     iNumClr = (1 << wBitCount);
  162.                     sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*iNumClr;
  163.                     break;
  164.             }
  165.             bCoreHdr = FALSE;
  166.         }
  167.  
  168.     }
  169.  
  170.     if ((pbmi = (LPBITMAPINFO) LocalAlloc(LMEM_FIXED,sizBMI)) == NULL) 
  171.     {
  172.         bSuccess = FALSE;
  173.         goto ErrExit3;
  174.     }
  175.  
  176.     //
  177.     // Make sure we pass in a DWORD aligned BitmapInfo to CreateDIBitmap
  178.     // Otherwise, exception on the MIPS platform
  179.     // CR!!!  Equivalent to memcpy
  180.     //
  181.     pjTmp = (PBYTE)pbmi;
  182.  
  183.     while(sizBMI--)
  184.     {
  185.         *(((PBYTE)pjTmp)++) = *(((PBYTE)pbmh)++);
  186.     }
  187.  
  188.     //
  189.     // assuming CreateDIBitmap() is doing a byte fetch...
  190.     //
  191.     pMapFile = (PBYTE)pMapFile + ((BITMAPFILEHEADER *)pMapFile)->bfOffBits;
  192.  
  193.     //
  194.     // Select the palette into the DC first before CreateDIBitmap()
  195.     //
  196.     bSelectDIBPal(hDC, pInfo, pbmi, bCoreHdr);
  197.     if ((pInfo->hBmpSaved = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)pbmi,
  198.                         CBM_INIT, pMapFile, pbmi, DIB_RGB_COLORS)) == NULL) 
  199.     {
  200.         OutputDebugString("Fail in creating DIB bitmap from file!");
  201.         bSuccess = FALSE;
  202.         goto ErrExit4;
  203.     }
  204.  
  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;
  213.  
  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);
  220.  
  221. ErrExit4:
  222.     LocalFree(pbmi);
  223. ErrExit3:
  224.     CloseHandle(hMapFile);
  225. ErrExit2:
  226.     CloseHandle(hFile);
  227. ErrExit1:
  228.  
  229.     return (bSuccess);
  230.  
  231. }
  232.