Code:
#pragma comment(lib, "shlwapi.lib")
#include <windows.h>
#include "shlwapi.h"
HBITMAP dBitmap;
void ColorizeBitmap(HWND hwnd)
{
BITMAP bitty;
GetObject(dBitmap,sizeof(BITMAP),&bitty);
HDC hdc = GetDC(hwnd);
HDC BitDC = CreateCompatibleDC(hdc);
HBITMAP oldBitmap = (HBITMAP)SelectObject(BitDC,dBitmap);
COLORREF col;
WORD hue,light,sat;
int CurBXpos = 0, CurBYpos = 0;
while(CurBYpos < bitty.bmHeight)
{
while(CurBXpos < bitty.bmWidth)
{
col = GetPixel(BitDC,CurBXpos,CurBYpos);
ColorRGBToHLS(col,&hue,&light,&sat);
hue= 0; //makes it red-ish
sat = 200;
col = ColorHLSToRGB(hue,light,sat);
SetPixel(BitDC,CurBXpos,CurBYpos,col);
CurBXpos++;
}
if(CurBXpos == bitty.bmWidth)
CurBXpos = 0;
CurBYpos++;
}
SelectObject(BitDC,oldBitmap);
DeleteDC(BitDC);
ReleaseDC(hwnd,hdc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
BeginPaint(hwnd,&ps);
HDC BitDC = CreateCompatibleDC(ps.hdc);
HBITMAP oldState = (HBITMAP)SelectObject(BitDC,dBitmap);
BitBlt(ps.hdc,0,0,200,200,BitDC,0,0,SRCCOPY);
SelectObject(BitDC,oldState);
EndPaint(hwnd,&ps);
return 0;
}
case WM_CLOSE:
DestroyWindow(hwnd); break;
case WM_DESTROY:
PostQuitMessage(0); break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WindowCls";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"WindowCls",
"Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
dBitmap = (HBITMAP)LoadImage(0,"Bitmap.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE);
ColorizeBitmap(hwnd);
InvalidateRect(hwnd,0,true);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
DeleteObject(dBitmap);
return Msg.wParam;
}
now, although this code 'works' it doesn't work very good. the conversion is quite slow (relatively speaking of course). so, my question to you is, is there any way to make it faster? if there is, would you please explain how.