Create 16/24BPP HBITMAP from pixel array
Hi. I'm interesting in creating 16/24BPP WinAPI Bitmaps manually for a custom
image file format. My CreateBitmap(...) code works for 32BPP, but not 16/24BPP.
Code:
/* Create 32BPP HBITMAP manually from pixel array */
#pragma optimize("t", on)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define name "Create HBITMAP"
int screenw, screenh;
HINSTANCE instance;
HWND window;
HDC sdc;
LRESULT CALLBACK wproc(HWND w, UINT m, WPARAM wp, LPARAM lp);
HBITMAP hbmp;
const unsigned w=0xFFFFFF, b=0, y=0xFFFF00; // colors: white, black, yellow
unsigned bmp[] = { // example 8x8 face image
w,w,b,b,b,b,w,w,
w,b,y,y,y,y,b,w,
b,y,b,y,y,b,y,b,
b,y,b,y,y,b,y,b,
b,y,y,y,y,y,y,b,
b,y,b,b,b,b,y,b,
w,b,y,y,y,y,b,w,
w,w,b,b,b,b,w,w };
int WINAPI WinMain(HINSTANCE hi, HINSTANCE na, PSTR c, int na2) {
instance = hi;
screenw = GetSystemMetrics(SM_CXSCREEN);
screenh = GetSystemMetrics(SM_CYSCREEN);
WNDCLASSEX wc = { 0x30, 0x23, wproc, 0, 0, hi, 0, (HCURSOR) LoadCursor(0,
IDC_ARROW), (HBRUSH) GetStockObject(WHITE_BRUSH), 0, name, 0 };
if (!RegisterClassEx(&wc) || !(window = CreateWindowEx(0, name, name,
WS_POPUP|WS_VISIBLE, screenw/2-4, screenh/2-4, 8, 8, 0, 0, hi, 0)))
return 1;
hbmp = CreateBitmap(8, 8, 32, 1, bmp);
MSG msg;
for (;;) {
if (!GetMessage(&msg, 0, 0, 0))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK wproc(HWND w, UINT m, WPARAM wp, LPARAM lp) {
if (m == WM_KEYDOWN && wp == VK_ESCAPE)
PostQuitMessage(0);
else if (m == WM_PAINT) {
HDC dc = GetDC(window),
mdc = CreateCompatibleDC(dc);
SelectObject(mdc, hbmp);
BitBlt(dc, 0, 0, 8, 8, mdc, 0, 0, SRCCOPY);
ReleaseDC(window, dc);
DeleteDC(mdc);
}
return DefWindowProc(w, m, wp, lp);
}
Here is a quote from MSDN:
Quote:
For a 16 bpp or 32 bpp non-palletized image, the color table must be three entries long; the entries must specify the value of the red, green, and blue (RGB) bitmasks. Because GDI ignores the color table for 24-bpp bitmaps, you should store the image pixels in RGB format.
This doesn't even make sense. 15/16/24/32BPP do not have a palette. I know what bitmasks are -- ie, 11111.000000.00000 = R16 -- but what does that have to do with creating an image?
Any help would be appreciated. Thank you.