-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgdiu.cpp
294 lines (236 loc) · 9.05 KB
/
gdiu.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <windows.h>
#include "gdiu.h"
bool TransparentBltU(
HDC dcDest, // handle to Dest DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int nHeightDest, // height of destination rectangle
HDC dcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
UINT crTransparent // color to make transparent
)
{
if (nWidthDest < 1) return false;
if (nWidthSrc < 1) return false;
if (nHeightDest < 1) return false;
if (nHeightSrc < 1) return false;
HDC dc = CreateCompatibleDC(NULL);
HBITMAP bitmap = CreateBitmap(nWidthSrc, nHeightSrc, 1,
GetDeviceCaps(dc, BITSPIXEL), NULL);
if (bitmap == NULL)
return false;
HBITMAP oldBitmap = (HBITMAP)SelectObject(dc, bitmap);
if (!BitBlt(dc, 0, 0, nWidthSrc, nHeightSrc, dcSrc,
nXOriginSrc, nYOriginSrc, SRCCOPY))
return false;
HDC maskDC = CreateCompatibleDC(NULL);
HBITMAP maskBitmap = CreateBitmap(nWidthSrc,
nHeightSrc, 1, 1, NULL);
if (maskBitmap == NULL)
return false;
HBITMAP oldMask = (HBITMAP)SelectObject(maskDC,
maskBitmap);
SetBkColor(maskDC, RGB(0,0,0));
SetTextColor(maskDC, RGB(255,255,255));
if (!BitBlt(maskDC, 0,0,nWidthSrc,nHeightSrc,NULL,
0,0,BLACKNESS))
return false;
SetBkColor(dc, crTransparent);
BitBlt(maskDC, 0,0,nWidthSrc,nHeightSrc,dc,0,0,SRCINVERT);
SetBkColor(dc, RGB(0,0,0));
SetTextColor(dc, RGB(255,255,255));
BitBlt(dc, 0,0,nWidthSrc,nHeightSrc,maskDC,0,0,SRCAND);
HDC newMaskDC = CreateCompatibleDC(NULL);
HBITMAP newMask;
newMask = CreateBitmap(nWidthDest, nHeightDest, 1,
GetDeviceCaps(newMaskDC, BITSPIXEL), NULL);
if (newMask == NULL)
{
SelectObject(dc, oldBitmap);
DeleteDC(dc);
SelectObject(maskDC, oldMask);
DeleteDC(maskDC);
DeleteDC(newMaskDC);
return false;
}
SetStretchBltMode(newMaskDC, COLORONCOLOR);
HBITMAP oldNewMask = (HBITMAP) SelectObject(newMaskDC,
newMask);
StretchBlt(newMaskDC, 0, 0, nWidthDest, nHeightDest,
maskDC, 0, 0, nWidthSrc, nHeightSrc, SRCCOPY);
SelectObject(maskDC, oldMask);
DeleteDC(maskDC);
HDC newImageDC = CreateCompatibleDC(NULL);
HBITMAP newImage = CreateBitmap(nWidthDest, nHeightDest,
1, GetDeviceCaps(newMaskDC, BITSPIXEL), NULL);
if (newImage == NULL)
{
SelectObject(dc, oldBitmap);
DeleteDC(dc);
DeleteDC(newMaskDC);
return false;
}
HBITMAP oldNewImage = (HBITMAP)SelectObject(newImageDC,
newImage);
StretchBlt(newImageDC, 0, 0, nWidthDest, nHeightDest,
dc, 0, 0, nWidthSrc, nHeightSrc, SRCCOPY);
SelectObject(dc, oldBitmap);
DeleteDC(dc);
BitBlt( dcDest, nXOriginDest, nYOriginDest, nWidthDest,
nHeightDest, newMaskDC, 0, 0, SRCAND);
BitBlt( dcDest, nXOriginDest, nYOriginDest, nWidthDest,
nHeightDest, newImageDC, 0, 0, SRCPAINT);
SelectObject(newImageDC, oldNewImage);
DeleteDC(newImageDC);
SelectObject(newMaskDC, oldNewMask);
DeleteDC(newMaskDC);
return true;
}
bool AlphaBlendU(HDC dcDest, int x, int y, int cx, int cy,
HDC dcSrc, int sx, int sy, int scx, int scy, int alpha)
{
BITMAPINFOHEADER BMI;
// Fill in the header info.
BMI.biSize = sizeof(BITMAPINFOHEADER);
BMI.biWidth = cx;
BMI.biHeight = cy;
BMI.biPlanes = 1;
BMI.biBitCount = 32;
BMI.biCompression = BI_RGB; // No compression
BMI.biSizeImage = 0;
BMI.biXPelsPerMeter = 0;
BMI.biYPelsPerMeter = 0;
BMI.biClrUsed = 0; // Always use the whole palette.
BMI.biClrImportant = 0;
BYTE * pSrcBits;
HBITMAP hbmSrc;
// Create DIB section in shared memory
hbmSrc = CreateDIBSection (dcSrc, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pSrcBits, 0, 0l);
BYTE * pDestBits;
HBITMAP hbmDest;
// Create DIB section in shared memory
hbmDest = CreateDIBSection (dcDest, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pDestBits, 0, 0l);
// Copy our source and destination bitmaps onto our DIBSections,
// so we can get access to their bits using the BYTE *'s we
// passed into CreateDIBSection
HDC dc = CreateCompatibleDC(NULL);
HBITMAP dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!StretchBlt(dc, 0, 0, cx, cy, dcSrc, sx, sy,
scx, scy, SRCCOPY))
return false;
SelectObject(dc, hbmDest);
if(!StretchBlt(dc, 0, 0, cx, cy, dcDest, x, y, cx, cy, SRCCOPY))
return false;
SelectObject(dc, dcOld);
DeleteDC(dc);
for (int j = 0; j < cy; ++j)
{
LPBYTE pbDestRGB = (LPBYTE)&((DWORD*)pDestBits)[j * cx];
LPBYTE pbSrcRGB = (LPBYTE)&((DWORD*)pSrcBits)[j * cx];
for (int i = 0; i < cx; ++i)
{
pbSrcRGB[0]=(pbDestRGB[0] * (255-alpha) +
pbSrcRGB[0] * alpha)>>8;
pbSrcRGB[1]=(pbDestRGB[1] * (255-alpha) +
pbSrcRGB[1] * alpha)>>8;
pbSrcRGB[2]=(pbDestRGB[2] * (255-alpha) +
pbSrcRGB[2] * alpha)>>8;
pbSrcRGB += 4;
pbDestRGB += 4;
}
}
dc = CreateCompatibleDC(NULL);
dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!BitBlt(dcDest, x, y, cx, cy, dc, 0, 0, SRCCOPY))
return false;
DeleteDC(dc);
DeleteObject(hbmSrc);
DeleteObject(hbmDest);
return true;
}
bool AlphaBlendCK(HDC dcDest, int x, int y, int cx, int cy,
HDC dcSrc, int sx, int sy, int scx, int scy,
int alpha, COLORREF rgbMask)
{
BITMAPINFOHEADER BMI;
// Fill in the header info.
BMI.biSize = sizeof(BITMAPINFOHEADER);
BMI.biWidth = cx;
BMI.biHeight = cy;
BMI.biPlanes = 1;
BMI.biBitCount = 32;
BMI.biCompression = BI_RGB; // No compression
BMI.biSizeImage = 0;
BMI.biXPelsPerMeter = 0;
BMI.biYPelsPerMeter = 0;
BMI.biClrUsed = 0; // Always use the whole palette.
BMI.biClrImportant = 0;
BYTE * pSrcBits;
HBITMAP hbmSrc;
// Create DIB section in shared memory
hbmSrc = CreateDIBSection (dcSrc, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pSrcBits, 0, 0l);
BYTE * pDestBits;
HBITMAP hbmDest;
// Create DIB section in shared memory
hbmDest = CreateDIBSection (dcDest, (BITMAPINFO *)&BMI,
DIB_RGB_COLORS, (void **)&pDestBits, 0, 0l);
// Copy our source and destination bitmaps onto our DIBSections,
// so we can get access to their bits using the BYTE *'s we
// passed into CreateDIBSection
HDC dc = CreateCompatibleDC(NULL);
HBITMAP dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!StretchBlt(dc, 0, 0, cx, cy, dcSrc, sx, sy,
scx, scy, SRCCOPY))
return false;
SelectObject(dc, hbmDest);
if(!StretchBlt(dc, 0, 0, cx, cy, dcDest, x, x, cx, cy, SRCCOPY))
return false;
SelectObject(dc, dcOld);
DeleteDC(dc);
int red = GetRValue(rgbMask);
int green = GetGValue(rgbMask);
int blue = GetBValue(rgbMask);
for (int j = 0; j < cy; ++j)
{
LPBYTE pbDestRGB = (LPBYTE)&((DWORD*)pDestBits)[j * cx];
LPBYTE pbSrcRGB = (LPBYTE)&((DWORD*)pSrcBits)[j * cx];
for (int i = 0; i < cx; ++i)
{
if (pbSrcRGB[0] != blue ||
pbSrcRGB[1] != green ||
pbSrcRGB[2] != red)
{
pbSrcRGB[0]=(pbDestRGB[0] * (255-alpha) +
pbSrcRGB[0] * alpha)>>8;
pbSrcRGB[1]=(pbDestRGB[1] * (255-alpha) +
pbSrcRGB[1] * alpha)>>8;
pbSrcRGB[2]=(pbDestRGB[2] * (255-alpha) +
pbSrcRGB[2] * alpha)>>8;
}
else
{
pbSrcRGB[0]=pbDestRGB[0];
pbSrcRGB[1]=pbDestRGB[1];
pbSrcRGB[2]=pbDestRGB[2];
}
pbSrcRGB += 4;
pbDestRGB += 4;
}
}
dc = CreateCompatibleDC(NULL);
dcOld = (HBITMAP) SelectObject(dc, hbmSrc);
if (!BitBlt(dcDest, x, y, cx, cy, dc, 0, 0, SRCCOPY))
return false;
DeleteDC(dc);
DeleteObject(hbmSrc);
DeleteObject(hbmDest);
return true;
}
//End of File