HBITMAP createDIB(HDC hdc, Image *inImage, const Rect& inRect)
{
HBITMAP hBitmap = NULL;
try
{
AutoPtr<Image> image = inImage;
if(image->getPhotometric() != pmBGR8)
image = ToSRGB(image, pmBGR8);
BITMAPINFOHEADER bi;
::ZeroMemory(&bi, sizeof(bi));
bi.biSize = sizeof(bi);
bi.biWidth = (LONG)inRect.width;
bi.biHeight = (LONG)inRect.height;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
void *pvBits;
hBitmap
= ::CreateDIBSection(
hdc, (CONST BITMAPINFO *)&bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
if(!hBitmap)
return NULL;
ssize_t rowStride = ((inImageRect.Width * bi.biBitCount / 8) + 3) & ~3;
u8 *firstLine = (u8 *)pvBits + rowStride * (inRect.height - 1);
AutoPtr<ImageLock> lock = image->lock();
lock->read(inRect, firstLine, -rowStride);
return hBitmap;
}
catch(...)
{
if(hBitmap) ::DeleteObject((HGDIOBJ)hBitmap);
return NULL;
}
}