--- mstools/samples/mandel/savebmp.c 2018/08/09 18:20:41 1.1 +++ mstools/samples/mandel/savebmp.c 2018/08/09 18:23:42 1.1.1.3 @@ -21,7 +21,7 @@ #include #include "jtypes.h" - +extern HPALETTE ghPal; extern HWND ghwndMain; extern char gtext[256]; BOOL SaveBitmapFile(HDC, HBITMAP, PSTR); @@ -56,6 +56,12 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm bSuccess = TRUE; +#if 0 + if (ghPal) { + SelectPalette(hDC, ghPal, FALSE); + RealizePalette(hDC); + } +#endif if (!hBmp) { MessageBox(ghwndMain, "There's no Bitmap to save!", "Error", MB_OK); return FALSE; @@ -78,7 +84,7 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm // the actual bits // if ((pBits = (PBYTE)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, - bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight)) == NULL) { + bmi.bmiHeader.biSizeImage)) == NULL) { MessageBox(ghwndMain, "Failed in Memory Allocation for pBits!", "Error", MB_OK); return FALSE; } @@ -88,8 +94,26 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm // allocate memory for retrieving that. Otherwise, we do. // pbmi = &bmi; // assume no color table - if (bmi.bmiHeader.biBitCount != 24) { // has color table - sizBMI = sizeof(BITMAPINFO)+sizeof(RGBQUAD)*(1<bmiHeader.biSize = bmi.bmiHeader.biSize; - pbmi->bmiHeader.biWidth = bmi.bmiHeader.biWidth; - pbmi->bmiHeader.biHeight = bmi.bmiHeader.biHeight; - pbmi->bmiHeader.biPlanes = bmi.bmiHeader.biPlanes; - pbmi->bmiHeader.biBitCount = bmi.bmiHeader.biBitCount; - pbmi->bmiHeader.biCompression = bmi.bmiHeader.biCompression; - pbmi->bmiHeader.biSizeImage = bmi.bmiHeader.biSizeImage; - pbmi->bmiHeader.biXPelsPerMeter = bmi.bmiHeader.biXPelsPerMeter; - pbmi->bmiHeader.biYPelsPerMeter = bmi.bmiHeader.biYPelsPerMeter; - pbmi->bmiHeader.biClrUsed = bmi.bmiHeader.biClrUsed; - pbmi->bmiHeader.biClrImportant = bmi.bmiHeader.biClrImportant; -#endif // // Now that we've a bigger chunk of memory, let's copy the Bitmap // info header data over // pjTmp = (PBYTE)pbmi; pjTmpBmi = (PBYTE)&bmi; - sizBMI = sizeof(BITMAPINFOHEADER); + sizTmp = sizeof(BITMAPINFOHEADER); - while(sizBMI--) + while(sizTmp--) { *(((PBYTE)pjTmp)++) = *((pjTmpBmi)++); } - } // @@ -140,13 +150,11 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm // But first, fill in the info for the BitmapFileHeader // bfh.bfType = 0x4D42; // 'BM' - bfh.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)* - ((pbmi->bmiHeader.biBitCount == 24) ? 0 : (1<bmiHeader.biBitCount))+ + bfh.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizBMI+ pbmi->bmiHeader.biSizeImage; bfh.bfReserved1 = bfh.bfReserved2 = 0; - bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+ - sizeof(RGBQUAD)*((pbmi->bmiHeader.biBitCount == 24) ? 0 : (1<bmiHeader.biBitCount)); + bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizBMI; // // Write out the file header now @@ -178,8 +186,7 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm // // Now write out the BitmapInfoHeader and color table, if any // - if (_lwrite(hFile, (LPSTR)pbmi, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * - ((pbmi->bmiHeader.biBitCount == 24) ? 0 : (1 << pbmi->bmiHeader.biBitCount))) == -1) { + if (_lwrite(hFile, (LPSTR)pbmi, sizBMI) == -1) { MessageBox(ghwndMain, "Failed in WriteFile!", "Error", MB_OK); bSuccess = FALSE; goto ErrExit4; @@ -194,6 +201,7 @@ BOOL SaveBitmapFile(HDC hDC, HBITMAP hBm goto ErrExit4; } + ErrExit4: SelectObject(hDC, hBmpOld); DeleteObject(hTmpBmp);