--- mstools/samples/mandel/loadbmp.c 2018/08/09 18:20:41 1.1.1.1 +++ mstools/samples/mandel/loadbmp.c 2018/08/09 18:23:41 1.1.1.3 @@ -22,7 +22,9 @@ #include "jtypes.h" extern HWND ghwndMain; -extern VOID ErrorOut(); +extern VOID ErrorOut(char errstring[30]); +extern BOOL bSelectDIBPal(HDC, PINFO, LPBITMAPINFO, BOOL); +extern BOOL bFreeRleFile(PINFO); BOOL LoadBitmapFile(HDC, PINFO, PSTR); /******************************Public*Routine******************************\ @@ -34,19 +36,25 @@ BOOL LoadBitmapFile(HDC, PINFO, PSTR); * Warnings: pszFileName contains the full path * * History: +* 08-Feb-1993 Petrus Wong Keep DIB around for HT +* 22-Jan-1993 Petrus Wong 16,24,32 PM * 09-Jan-1992 -by- Petrus Wong * Wrote it. \**************************************************************************/ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo, PSTR pszFileName) { - BOOL bSuccess; - HANDLE hFile, hMapFile; - LPVOID pMapFile; + BOOL bSuccess; + HANDLE hFile, hMapFile; + LPVOID pMapFile; LPBITMAPINFOHEADER pbmh; - LPBITMAPINFO pbmi; - PBYTE pjTmp; - ULONG sizBMI; + LPBITMAPINFO pbmi; + PBYTE pjTmp; + ULONG sizBMI; + INT iNumClr; + BOOL bCoreHdr; + PFILEINFO pFileInfo; + bSuccess = TRUE; @@ -61,7 +69,7 @@ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo // Create a map file of the opened file // if ((hMapFile = CreateFileMapping(hFile, NULL, - PAGE_READONLY, 0, 0, "MapF")) == (HANDLE)-1) { + PAGE_READONLY, 0, 0, NULL)) == (HANDLE)-1) { ErrorOut("Fail in creating map file"); bSuccess = FALSE; goto ErrExit2; @@ -78,6 +86,16 @@ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo } // + // Saving the DIB file handle, etc in pInfo... + // freeing existing objects, if any + // + bFreeRleFile(pInfo); + pFileInfo = &(pInfo->RleData.rgFileInfo[0]); + pFileInfo->hFile = hFile; + pFileInfo->hMapFile = hMapFile; + pFileInfo->lpvMapView = pMapFile; + + // // First check that it is a bitmap file // if (*((PWORD)pMapFile) != 0x4d42) { // 'BM' @@ -86,21 +104,68 @@ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo goto ErrExit3; } - pbmh = (LPBITMAPINFOHEADER)((PBYTE)pMapFile + sizeof(BITMAPFILEHEADER)); - // - // Use the size to determine if it is a BitmapCoreHeader or - // BitmapInfoHeader + // The file header doesn't end on DWORD boundary... // - if (pbmh->biSize == sizeof(BITMAPCOREHEADER)) - { - sizBMI = sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)* - ((((LPBITMAPCOREHEADER)pbmh)->bcBitCount == 24) ? 0 : (1 << ((LPBITMAPCOREHEADER)pbmh)->bcBitCount)); - } - else // BITMAPINFOHEADER + pbmh = (LPBITMAPINFOHEADER)((PBYTE)pMapFile + sizeof(BITMAPFILEHEADER)); + { - sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)* - ((pbmh->biBitCount == 24) ? 0 : (1 << pbmh->biBitCount)); + BITMAPCOREHEADER bmch, *pbmch; + BITMAPINFOHEADER bmih, *pbmih; + PBYTE pjTmp; + ULONG ulSiz; + + pbmch = &bmch; + pbmih = &bmih; + + pjTmp = (PBYTE)pbmh; + ulSiz = sizeof(BITMAPCOREHEADER); + while (ulSiz--) { + *(((PBYTE)pbmch)++) = *(((PBYTE)pjTmp)++); + } + + pjTmp = (PBYTE)pbmh; + ulSiz = sizeof(BITMAPINFOHEADER); + while (ulSiz--) { + *(((PBYTE)pbmih)++) = *(((PBYTE)pjTmp)++); + } + + // + // Use the size to determine if it is a BitmapCoreHeader or + // BitmapInfoHeader + // + // Does PM supports 16 and 32 bpp? How? + // + if (bmch.bcSize == sizeof(BITMAPCOREHEADER)) + { + WORD wBitCount; + + wBitCount = bmch.bcBitCount; + iNumClr = ((wBitCount == 24) ? 0 : (1 << wBitCount)); + sizBMI = sizeof(BITMAPCOREHEADER)+sizeof(RGBTRIPLE)*iNumClr; + bCoreHdr = TRUE; + } + else // BITMAPINFOHEADER + { + WORD wBitCount; + + wBitCount = bmih.biBitCount; + switch (wBitCount) { + case 16: + case 32: + sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(DWORD)*3; + break; + case 24: + sizBMI = sizeof(BITMAPINFOHEADER); + break; + default: + iNumClr = (1 << wBitCount); + sizBMI = sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*iNumClr; + break; + } + bCoreHdr = FALSE; + } + } if ((pbmi = (LPBITMAPINFO) LocalAlloc(LMEM_FIXED,sizBMI)) == NULL) { @@ -121,8 +186,15 @@ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo *(((PBYTE)pjTmp)++) = *(((PBYTE)pbmh)++); } + // + // assuming CreateDIBitmap() is doing a byte fetch... + // pMapFile = (PBYTE)pMapFile + ((BITMAPFILEHEADER *)pMapFile)->bfOffBits; + // + // Select the palette into the DC first before CreateDIBitmap() + // + bSelectDIBPal(hDC, pInfo, pbmi, bCoreHdr); if ((pInfo->hBmpSaved = CreateDIBitmap(hDC, (LPBITMAPINFOHEADER)pbmi, CBM_INIT, pMapFile, pbmi, DIB_RGB_COLORS)) == NULL) { ErrorOut("Fail in creating DIB bitmap from file!"); @@ -130,7 +202,21 @@ BOOL LoadBitmapFile(HDC hDC, PINFO pInfo goto ErrExit4; } + // + // Saving the DIB...free memory when the windows is closed. + // + pInfo->RleData.rgpjFrame[0] = pMapFile; + pInfo->RleData.rgpbmi[0] = pbmi; + pInfo->RleData.pbmi = (PBITMAPINFO) &(pInfo->RleData.rgpbmi[0]); + pInfo->RleData.ulFrames = 1; + pInfo->RleData.ulFiles = 1; + + // set flag to use original DIB as source for blting so HT can be done + pInfo->bUseDIB = TRUE; + + pInfo->bCoreHdr = bCoreHdr; + return (bSuccess); ErrExit4: LocalFree(pbmi);