forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_windows.c
35 lines (30 loc) · 1.04 KB
/
image_windows.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 16 august 2014
#include "winapi_windows.h"
#include "_cgo_export.h"
// TODO rename to images_windows.c?
HBITMAP toBitmap(void *i, intptr_t dx, intptr_t dy)
{
BITMAPINFO bi;
VOID *ppvBits;
HBITMAP bitmap;
ZeroMemory(&bi, sizeof (BITMAPINFO));
bi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
bi.bmiHeader.biWidth = (LONG) dx;
bi.bmiHeader.biHeight = -((LONG) dy); // negative height to force top-down drawing;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = (DWORD) (dx * dy * 4);
bitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &ppvBits, 0, 0);
if (bitmap == NULL)
xpanic("error creating HBITMAP in toBitmap()", GetLastError());
// image lists use non-premultiplied RGBA - see http://stackoverflow.com/a/25578789/3408572
// the TRUE here does the conversion
dotoARGB(i, (void *) ppvBits, TRUE);
return bitmap;
}
void freeBitmap(uintptr_t bitmap)
{
if (DeleteObject((HBITMAP) bitmap) == 0)
xpanic("error deleting bitmap in freeBitmap()", GetLastError());
}