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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#include ".graphics.h"
#include "ml.h"
#include <math.h>
HBITMAP CreateBitmapMask(HBITMAP originalBmp, int cx, int cy)
{
return CreateBitmapMask(originalBmp, NULL, cx, cy);
}
HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor)
{
return CreateBitmapMask(originalBmp, transColor, 0, 0);
}
HBITMAP CreateBitmapMask(HBITMAP originalBmp, COLORREF transColor, int cx, int cy)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObjectW(originalBmp, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
HBITMAP obmp = (HBITMAP)SelectObject(hdcMem, originalBmp);
HBITMAP omsk = (HBITMAP)SelectObject(hdcMem2, hbmMask);
if (transColor == NULL) transColor = GetPixel(hdcMem, cx, cy);
COLORREF oldColorBK = SetBkColor(hdcMem, transColor);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
SetBkColor(hdcMem, oldColorBK);
SelectObject(hdcMem, obmp);
SelectObject(hdcMem2, omsk);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
HBITMAP ConvertTo24bpp(HBITMAP bmp, int bpp)
{
HDC hdcMem, hdcMem2;
HBITMAP hbm24;
BITMAP bm;
GetObjectW(bmp, sizeof(BITMAP), &bm);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
void *bits;
BITMAPINFOHEADER bi;
ZeroMemory (&bi, sizeof (bi));
bi.biSize = sizeof (bi);
bi.biWidth= bm.bmWidth;
bi.biHeight = -bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount= (WORD)(0xFF & bpp);
hbm24 = CreateDIBSection(hdcMem2, (BITMAPINFO *)&bi, DIB_RGB_COLORS, &bits, NULL, NULL);
HBITMAP oBmp = (HBITMAP)SelectObject(hdcMem, bmp);
HBITMAP oBmp24 = (HBITMAP)SelectObject(hdcMem2, hbm24);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oBmp);
SelectObject(hdcMem2, oBmp24);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbm24;
}
// works only with DIB
HBITMAP PatchBitmapColors24(HBITMAP bitmap, COLORREF color1, COLORREF color2, BMPFILTERPROC filterProc)
{
BITMAP bm;
COLOR24 clrBG, clrFG;
INT x, y;
if (!filterProc) return NULL;
if (!GetObjectW(bitmap, sizeof(BITMAP), &bm) || !bm.bmBits || 24 != bm.bmBitsPixel) return NULL;
clrBG.rgbRed = (BYTE)(0xFF & color1);
clrFG.rgbRed = (BYTE)(0xFF & color2);
clrBG.rgbGreen = (BYTE)(0xFF & (color1>>8));
clrFG.rgbGreen = (BYTE)(0xFF & (color2>>8));
clrBG.rgbBlue = (BYTE)(0xFF & (color1>>16));
clrFG.rgbBlue = (BYTE)(0xFF & (color2>>16));
for (y = 0; y < bm.bmHeight; y++)
{
LONG width = (bm.bmWidthBytes%4)?bm.bmWidth*4:bm.bmWidthBytes;
// bm.bmWidthBytes can lie so is safer to go with bm.bmWidth if the dword alignment cbeck fails
// http://blogs.msdn.com/oldnewthing/archive/2004/10/26/247918.aspx#248529
COLOR24 *cursor = (COLOR24*)(((BYTE*)bm.bmBits) + (width*y));
for (x = 0; x < bm.bmWidth; x++, cursor++) filterProc(&clrBG, &clrFG, cursor);
}
return bitmap;
}
void Filter1(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel)
{
pixel->rgbBlue = (BYTE)(color1->rgbBlue - (int)((1.f - (pixel->rgbBlue /255.f))* (color1->rgbBlue - color2->rgbBlue)));
pixel->rgbGreen = (BYTE)(color1->rgbGreen - (int)((1.f - (pixel->rgbGreen /255.f))* (color1->rgbGreen - color2->rgbGreen)));
pixel->rgbRed = (BYTE)(color1->rgbRed - (int)((1.f - (pixel->rgbRed /255.f))* (color1->rgbRed - color2->rgbRed)));
}
void Filter2(const COLOR24 *color1, const COLOR24 *color2, COLOR24 *pixel)
{
float chrom = (float)pixel->rgbBlue / 255.f;
pixel->rgbBlue = (BYTE)(color1->rgbBlue * (1.f - chrom) + color2->rgbBlue * chrom);
pixel->rgbGreen = (BYTE)(color1->rgbGreen * (1.f - chrom) + color2->rgbGreen * chrom);
pixel->rgbRed = (BYTE)(color1->rgbRed * (1.f - chrom) + color2->rgbRed * chrom);
}
|