winemac: Use GetDIBits to retrieve the bitmap header when exporting a bitmap.

Signed-off-by: Ken Thomases <ken@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Ken Thomases 2016-10-23 13:03:27 -05:00 committed by Alexandre Julliard
parent 12ea83acb5
commit 1837210159
1 changed files with 17 additions and 66 deletions

View File

@ -410,77 +410,28 @@ static int bitmap_info_size(const BITMAPINFO *info, WORD coloruse)
* *
* Allocates a packed DIB and copies the bitmap data into it. * Allocates a packed DIB and copies the bitmap data into it.
*/ */
static HGLOBAL create_dib_from_bitmap(HBITMAP hBmp) static HGLOBAL create_dib_from_bitmap(HBITMAP bitmap)
{ {
BITMAP bmp; HANDLE ret = 0;
HDC hdc; BITMAPINFOHEADER header;
HGLOBAL hPackedDIB; HDC hdc = GetDC(0);
LPBYTE pPackedDIB; DWORD header_size;
LPBITMAPINFOHEADER pbmiHeader; BITMAPINFO *bmi;
unsigned int cDataSize, cPackedSize, OffsetBits;
int nLinesCopied;
if (!GetObjectW(hBmp, sizeof(bmp), &bmp)) return 0; memset(&header, 0, sizeof(header));
header.biSize = sizeof(header);
if (!GetDIBits(hdc, bitmap, 0, 0, NULL, (BITMAPINFO *)&header, DIB_RGB_COLORS)) goto done;
/* header_size = bitmap_info_size((BITMAPINFO *)&header, DIB_RGB_COLORS);
* A packed DIB contains a BITMAPINFO structure followed immediately by if (!(ret = GlobalAlloc(GMEM_FIXED, header_size + header.biSizeImage))) goto done;
* an optional color palette and the pixel data. bmi = (BITMAPINFO *)ret;
*/ memset(bmi, 0, header_size);
memcpy(bmi, &header, header.biSize);
GetDIBits(hdc, bitmap, 0, abs(header.biHeight), (char *)bmi + header_size, bmi, DIB_RGB_COLORS);
/* Calculate the size of the packed DIB */ done:
cDataSize = abs(bmp.bmHeight) * (((bmp.bmWidth * bmp.bmBitsPixel + 31) / 8) & ~3);
cPackedSize = sizeof(BITMAPINFOHEADER)
+ ((bmp.bmBitsPixel <= 8) ? (sizeof(RGBQUAD) * (1 << bmp.bmBitsPixel)) : 0)
+ cDataSize;
/* Get the offset to the bits */
OffsetBits = cPackedSize - cDataSize;
/* Allocate the packed DIB */
TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
hPackedDIB = GlobalAlloc(GMEM_FIXED, cPackedSize);
if (!hPackedDIB)
{
WARN("Could not allocate packed DIB!\n");
return 0;
}
/* A packed DIB starts with a BITMAPINFOHEADER */
pPackedDIB = GlobalLock(hPackedDIB);
pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
/* Init the BITMAPINFOHEADER */
pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
pbmiHeader->biWidth = bmp.bmWidth;
pbmiHeader->biHeight = bmp.bmHeight;
pbmiHeader->biPlanes = 1;
pbmiHeader->biBitCount = bmp.bmBitsPixel;
pbmiHeader->biCompression = BI_RGB;
pbmiHeader->biSizeImage = 0;
pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
pbmiHeader->biClrUsed = 0;
pbmiHeader->biClrImportant = 0;
/* Retrieve the DIB bits from the bitmap and fill in the
* DIB color table if present */
hdc = GetDC(0);
nLinesCopied = GetDIBits(hdc, /* Handle to device context */
hBmp, /* Handle to bitmap */
0, /* First scan line to set in dest bitmap */
bmp.bmHeight, /* Number of scan lines to copy */
pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
(LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
0); /* RGB or palette index */
GlobalUnlock(hPackedDIB);
ReleaseDC(0, hdc); ReleaseDC(0, hdc);
return ret;
/* Cleanup if GetDIBits failed */
if (nLinesCopied != bmp.bmHeight)
{
TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, bmp.bmHeight);
GlobalFree(hPackedDIB);
hPackedDIB = 0;
}
return hPackedDIB;
} }