-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBitmap.cpp
430 lines (334 loc) · 11.7 KB
/
Bitmap.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Region.cpp: Converta bitmap to a Region
//
//////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Bitmap.h"
#include "Globals.h"
#include "Clock.h"
#include "Resource.h"
#include "ColorFunctions.h"
HRGN CreateRegionFromBitmap(HBITMAP hBitmap, COLORREF transparentColor)
{
HRGN hRgn = NULL;
HRGN hTempRgn = NULL;
// Check for valid bitmap handle
if ( hBitmap != NULL )
{
// Get bitmap object information
BITMAP bitmap;
BITMAPINFO sBmpInfo;
DWORD dwSize;
unsigned char* ucBytes;
int bitsPixel,iIndex,iResult;
unsigned char red, green, blue;
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
memset( &sBmpInfo, 0, sizeof(BITMAPINFO));
sBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
sBmpInfo.bmiHeader.biWidth = bitmap.bmWidth;
sBmpInfo.bmiHeader.biHeight = bitmap.bmHeight;
sBmpInfo.bmiHeader.biBitCount = bitmap.bmBitsPixel;
sBmpInfo.bmiHeader.biPlanes = 1;
sBmpInfo.bmiHeader.biCompression = BI_RGB;
//sBmpInfo.bmiHeader.biSizeImage = bitmap.bmBitsPixel*bitmap.bmWidth*bitmap.bmHeight/8;
sBmpInfo.bmiHeader.biSizeImage = 0;
sBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitsPixel= bitmap.bmBitsPixel / 8;
// Check bitmap color depth (only 24 or 32 bits per pixel allowed)
if ( ( bitsPixel == 3 ) || ( bitsPixel == 4 ) )
{
dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;
ucBytes = new unsigned char[dwSize];
memset(ucBytes, 0, dwSize);
//read the bitmap into buffer
HDC hScreenDC=GetDC(NULL);
SelectObject(hScreenDC, hBitmap);
//iResult=GetDIBits(hScreenDC,hBitmap,0,sBmpInfo.bmiHeader.biHeight,ucBytes,&sBmpInfo,DIB_RGB_COLORS);
iResult=GetBitmapBits(hBitmap, dwSize, ucBytes);
// iResult=GetLastError();
DeleteDC(hScreenDC);
//Consider Top Left corner pixel as transperent color if nothing is specified
if (transparentColor==-1)
transparentColor=RGB(ucBytes[2],ucBytes[1],ucBytes[0]);
// Create region from bitmap
for ( int y=0; y<bitmap.bmHeight; y++ )
{
for ( int x=0; x<bitmap.bmWidth; x++ )
{
// Get pixel color
iIndex=y*bitmap.bmWidthBytes + bitsPixel*x;
blue = ucBytes[iIndex];
green = ucBytes[iIndex+1];
red = ucBytes[iIndex+2];
// Check transparent color
if ( RGB(red,green,blue) != transparentColor )
{
// Combine regions
if ( hRgn == NULL )
hRgn = CreateRectRgn(x, y, x+1, y+1);
else
{
// Delete temporary region
if ( hTempRgn != NULL )
DeleteObject(hTempRgn);
// Create temporary region
hTempRgn = CreateRectRgn(x, y, x+1, y+1);
// Combine regions
CombineRgn(hRgn, hRgn, hTempRgn, RGN_OR);
}
}
}
}
// Free bitmap bits
delete ucBytes;
}
}
// Delete temporary region
if ( hTempRgn != NULL )
DeleteObject(hTempRgn);
return hRgn;
}
COLORREF GetColorAtPixel(HBITMAP hBitmap, POINT* PixelPos)
{
BITMAP bitmap;
DWORD dwSize;
unsigned char* ucBytes;
unsigned char red, green, blue;
int bitsPixel,iIndex;
POINT AtPixel;
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
//clean input pixel to be within the bitmap
if (PixelPos!=NULL)
{
AtPixel=*PixelPos;
if (AtPixel.x<0)
AtPixel.x=0;
else if(AtPixel.x >bitmap.bmWidth)
AtPixel.x=bitmap.bmWidth;
if (AtPixel.y<0)
AtPixel.y=0;
else if(AtPixel.y >bitmap.bmHeight)
AtPixel.y=bitmap.bmHeight;
}
else
AtPixel.x=AtPixel.y =0;
dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;
ucBytes = new unsigned char[dwSize];
GetBitmapBits(hBitmap, dwSize, ucBytes);
bitsPixel= bitmap.bmBitsPixel / 8;
// Get pixel color
iIndex=AtPixel.y *bitmap.bmWidthBytes + bitsPixel*AtPixel.x;
blue = ucBytes[iIndex];
green = ucBytes[iIndex+1];
red = ucBytes[iIndex+2];
// Free bitmap bits
delete ucBytes;
return RGB(red,green,blue);
}
void ResetBackGround(HWND hwnd)
{
HBRUSH hOldBackGnd;
hOldBackGnd=(HBRUSH)SetClassLong(hwnd,GCL_HBRBACKGROUND,(long) g_hBackGndBrush);
if (hOldBackGnd!=g_hBackGndBrush)
DeleteObject(hOldBackGnd);
}
HBRUSH GetTransBkGndBrush(HWND hwnd,HRGN hWndRgn)
{
/*adding some code to get the window back ground*/
HDC ScreenDC,ScreenShotDC;
// int ScreenWidth,ScreenHeight;
HBITMAP ScreenShotBitmap;
int Width,Height,Top,Left;
LOGBRUSH brBackGround;
HBRUSH hNewBackGnd;
RECT CurrentPos;
GetWindowRect(hwnd,&CurrentPos);
Left=CurrentPos.left;
Top=CurrentPos.top;
Width=CurrentPos.right-CurrentPos.left+1;
Height=CurrentPos.bottom-CurrentPos.top+1;
//get the screen shot
if (hWndRgn==NULL)
ScreenDC=GetDC(NULL);
else
ScreenDC = GetDCEx(NULL,hWndRgn,DCX_EXCLUDERGN);
ScreenShotDC = CreateCompatibleDC(ScreenDC);
ScreenShotBitmap= CreateCompatibleBitmap(ScreenDC, Width, Height);
SelectObject(ScreenShotDC, ScreenShotBitmap);
//get contents below window in to the bitmap
BitBlt(ScreenShotDC,0,0,Width, Height, ScreenDC,Left,Top, SRCCOPY);
//Make3DBorder(ScreenShotDC,g_hBitMap,hwnd);
Make3DBorder(ScreenShotDC,11,false);
brBackGround.lbStyle = BS_PATTERN;
brBackGround.lbHatch = (long) ScreenShotBitmap;
hNewBackGnd=CreateBrushIndirect(&brBackGround);
ReleaseDC(NULL,ScreenDC);
DeleteDC(ScreenDC);
DeleteObject(ScreenShotBitmap);
DeleteDC(ScreenShotDC);
return hNewBackGnd;
}
void Make3DBorder(HDC PaintDC,int iBorderThikness,bool bSink)
{
HPEN hHighlightPen, hShadowPen,hOldPen;
RECT sOuterRect,sInnerRect;
COLORREF clrHighlight, clrShadow;
clrHighlight=RGB(220,220,220);
clrShadow=RGB(128,128,128);
sInnerRect.left=CENTRE.x-RADIUS;
sInnerRect.top=CENTRE.y-RADIUS;
sInnerRect.right=CENTRE.x+RADIUS;
sInnerRect.bottom=CENTRE.y+RADIUS;
sOuterRect.left=CENTRE.x-RADIUS-iBorderThikness;
sOuterRect.top=CENTRE.y-RADIUS-iBorderThikness;
sOuterRect.right=CENTRE.x+RADIUS+iBorderThikness;
sOuterRect.bottom=CENTRE.y+RADIUS+iBorderThikness;
if (bSink)
{
COLORREF clrTemp;
clrTemp=clrHighlight;
clrHighlight=clrShadow;
clrShadow=clrTemp;
}
hHighlightPen=CreatePen(PS_SOLID, 1,clrHighlight);
hShadowPen=CreatePen(PS_SOLID, 1,clrShadow);
hOldPen=(HPEN)SelectObject(PaintDC,hHighlightPen);
Arc(PaintDC,sInnerRect.left,sInnerRect.top,sInnerRect.right,sInnerRect.bottom,CENTRE.x,sInnerRect.bottom,CENTRE.x,sInnerRect.top);
Arc(PaintDC,sOuterRect.left,sOuterRect.top,sOuterRect.right,sOuterRect.bottom,CENTRE.x,sOuterRect.top,CENTRE.x,sOuterRect.bottom);
SelectObject(PaintDC,hShadowPen);
Arc(PaintDC,sInnerRect.left,sInnerRect.top,sInnerRect.right,sInnerRect.bottom,CENTRE.x,sInnerRect.top,CENTRE.x,sInnerRect.bottom);
Arc(PaintDC,sOuterRect.left,sOuterRect.top,sOuterRect.right,sOuterRect.bottom,CENTRE.x,sOuterRect.bottom,CENTRE.x,sOuterRect.top);
SelectObject(PaintDC,hOldPen);
DeleteObject(hShadowPen);
DeleteObject(hHighlightPen);
}
/*
void Make3DBorder(HDC PaintDC,HBITMAP hBitmapMask,HWND hWnd, bool bSink)
{
BITMAP bitmap;
BITMAPINFO sBmpInfo;
DWORD dwSize;
unsigned char* ucBytes;
COLORREF lAvgColor,lPixelColor,lSumColor;
int iIndex,bitsPixel;
COLORREF clrHighlight, clrShadow;
bool bRaised,bPrevoiusNotEdge;
POINT pPixel;
RECT rtCurrentPos;
int GX[3][3];
int GY[3][3];
int dY,dX, iSumRx,iSumRy,iSumBx,iSumBy,iSumGx,iSumGy,iOffset=0;
lAvgColor=RGB(15,15,15);
//3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html
GX[0][0] = -1; GX[0][1] = -1; GX[0][2] = -1;
GX[1][0] = 2; GX[1][1] = 2; GX[1][2] = 2;
GX[2][0] = -1; GX[2][1] = -1; GX[2][2] = -1;
//3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html
GY[0][0] = -1; GY[0][1] = 2; GY[0][2] = -1;
GY[1][0] = -1; GY[1][1] = 2; GY[1][2] = -1;
GY[2][0] = -1; GY[2][1] = 2; GY[2][2] = -1;
//3x3 GX Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html
GX[0][0] = -1; GX[0][1] = -1; GX[0][2] = -1;
GX[1][0] = -1; GX[1][1] = 8; GX[1][2] = -1;
GX[2][0] = -1; GX[2][1] = -1; GX[2][2] = -1;
//3x3 GY Sobel mask. Ref: www.cee.hw.ac.uk/hipr/html/sobel.html
GY[0][0] = -1; GY[0][1] = -1; GY[0][2] = -1;
GY[1][0] = -1; GY[1][1] = 4; GY[1][2] = -1;
GY[2][0] = -1; GY[2][1] = 1; GY[2][2] = -1;
GetObject(hBitmapMask, sizeof(BITMAP), &bitmap);
memset( &sBmpInfo, 0, sizeof(BITMAPINFO));
sBmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
sBmpInfo.bmiHeader.biWidth = bitmap.bmWidth;
sBmpInfo.bmiHeader.biHeight = bitmap.bmHeight;
sBmpInfo.bmiHeader.biBitCount = bitmap.bmBitsPixel;
sBmpInfo.bmiHeader.biPlanes = 1;
sBmpInfo.bmiHeader.biCompression = BI_RGB;
sBmpInfo.bmiHeader.biSizeImage = bitmap.bmBitsPixel*bitmap.bmWidth*bitmap.bmHeight/8;
bitsPixel= bitmap.bmBitsPixel / 8;
dwSize = bitmap.bmHeight * bitmap.bmWidthBytes;
ucBytes = new unsigned char[dwSize];
//read the bitmap into buffer
GetDIBits(PaintDC,hBitmapMask,0,sBmpInfo.bmiHeader.biHeight,ucBytes,&sBmpInfo,DIB_RGB_COLORS );
clrHighlight=RGB(220,220,220);
clrShadow=RGB(125,125,125);
// clrHighlight=RGB(225,0,0);
// clrShadow=RGB(0,0,255);
if (bSink)
{
COLORREF clrTemp;
clrTemp=clrHighlight;
clrHighlight=clrShadow;
clrShadow=clrTemp;
}
GetWindowRect(hWnd,&rtCurrentPos);
//scan line by line, comapre contrast between two pixels, highlight/shadow accordingly if there is a edge
for ( int Y=0; Y<bitmap.bmHeight-1; Y++ )
{
for ( int X=0; X<bitmap.bmWidth-1; X++ )
{
iSumRx=iSumRy=iSumBx=iSumBy=iSumGx=iSumGy=0;
// image boundaries
if(Y==0 || Y==bitmap.bmHeight-1)
lSumColor= 0;
else if(X==0 || X==bitmap.bmWidth-1)
lSumColor = 0;
// Convolution starts here
else
{
//X & Y Gradient approximation
for(dX=-1; dX<=1; dX++)
{
for(dY=-1; dY<=1; dY++)
{
iIndex=(Y+dY)*bitmap.bmWidthBytes + bitsPixel*(X+dX);
lPixelColor=RGB(ucBytes[iIndex+2],ucBytes[iIndex+1],ucBytes[iIndex]);
iSumRx=iSumRx + ucBytes[iIndex+2]* GX[dX+1][dY+1];
iSumGx=iSumGx + ucBytes[iIndex+1]* GX[dX+1][dY+1];
iSumBx=iSumBx + ucBytes[iIndex] * GX[dX+1][dY+1];
iSumRy=iSumRy + ucBytes[iIndex+2]* GY[dX+1][dY+1];
iSumGy=iSumGy + ucBytes[iIndex+1]* GY[dX+1][dY+1];
iSumBy=iSumBy + ucBytes[iIndex] * GY[dX+1][dY+1];
//Offset
iSumRx=iSumRx + iOffset;
iSumGx=iSumGx + iOffset;
iSumBx=iSumBx + iOffset;
iSumRx=iSumRy + iOffset;
iSumGy=iSumGy + iOffset;
iSumBy=iSumBy + iOffset;
}
}
//Gradient magnitude approximation
iSumRx=(iSumRx>255?255:(iSumRx<0?0:iSumRx));
iSumGx=(iSumGx>255?255:(iSumGx<0?0:iSumGx));
iSumBx=(iSumBx>255?255:(iSumBx<0?0:iSumBx));
iSumRy=(iSumRy>255?255:(iSumRy<0?0:iSumRy));
iSumGy=(iSumGy>255?255:(iSumGy<0?0:iSumGy));
iSumBy=(iSumBy>255?255:(iSumBy<0?0:iSumBy));
lSumColor =RGB(iSumRx+iSumRy,iSumGx+iSumGy,iSumBx+iSumBy);
}
// SetPixel(PaintDC,X,Y,lSumColor);
//Check for contrast
// if ( abs(lAvgColor-lPixelColor)>CONTRASTTHREASHHOLD &&bPrevoiusNotEdge)
if (lSumColor>lAvgColor)
//if ((lAvgColor-lSumColor)>0 && lSumColor)
{
//x,y are relative to 0,0 of the window, but we need relative to 0,0 of the screen
pPixel.x=X;
pPixel.y=Y;
//ClientToScreen(hWnd,&pPixel);
//pPixel.x=(pPixel.x<rtCurrentPos.left?rtCurrentPos.left:(pPixel.x>rtCurrentPos.right?rtCurrentPos.right:pPixel.x));
//pPixel.y=(pPixel.x<rtCurrentPos.top?rtCurrentPos.top:(pPixel.y>rtCurrentPos.bottom?rtCurrentPos.bottom:pPixel.y));
//ScreenToClient(hWnd,&pPixel);
if (!bRaised)
SetPixel(PaintDC,pPixel.x,pPixel.y,clrHighlight);
else
SetPixel(PaintDC,pPixel.x,pPixel.y,clrShadow);
}
else
bRaised=!bRaised;
}
bRaised=false;
}
// Free bitmap bits
delete ucBytes;
}
*/