-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
359 lines (319 loc) · 11.4 KB
/
Copy pathMain.cpp
File metadata and controls
359 lines (319 loc) · 11.4 KB
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
// Windows Header Files:
#include <windows.h>
#include "resource.h"
#include "TChar.h"
#include "commctrl.h"
#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) | ((Color << 16) & 0xff0000)
TCHAR szClassName[] = _T("RPLCOLORTEST");
HINSTANCE g_hinstance; // not very clean but usefull for a sample
HBITMAP BlitReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC=NULL);
HBITMAP ReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC=NULL);
HBITMAP ReplaceColor (HINSTANCE Instance,WORD ResourseID,COLORREF cOldColor,COLORREF cNewColor);
//-----------------------------------------------------------------------------------
// BlitReplaceColor ( twice slower in my tests than ReplaceColor )
//
// hBmp : Source Bitmap
// cOldColor: Color to replace in hBmp
// cNewColor: Color used for replacement
// hBmpDC : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
//
// Retcode : HBITMAP of the modified bitmap or NULL for errors
//
//-----------------------------------------------------------------------------------
HBITMAP BlitReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC)
{
HBITMAP RetBmp=NULL;
if (hBmp)
{
HDC BufferDC=CreateCompatibleDC(NULL); // DC for Source Bitmap
if (BufferDC)
{
HBITMAP hTmpBitmap = (HBITMAP) NULL;
if (hBmpDC)
if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP))
{
hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);
SelectObject(hBmpDC, hTmpBitmap);
}
HGDIOBJ PreviousBufferObject=SelectObject(BufferDC,hBmp);
// here BufferDC contains the bitmap
HDC DirectDC=CreateCompatibleDC(NULL); // DC for working
if (DirectDC)
{
HDC MaskDC=CreateCompatibleDC(NULL); // DC for mask
if (MaskDC)
{
// Get bitmap size
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
// create a BITMAPINFO with minimal initilisation for the CreateDIBSection
BITMAPINFO RGB32BitsBITMAPINFO;
ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
RECT tmprect={0,0,bm.bmWidth,bm.bmHeight};
HBITMAP bitmapTrans=CreateBitmap(bm.bmWidth,bm.bmHeight,1,1,NULL);
DeleteObject(SelectObject (MaskDC,bitmapTrans));
SetBkColor(BufferDC,cOldColor);
RetBmp=CreateBitmap(bm.bmWidth,bm.bmHeight,1,32,NULL);
HGDIOBJ PreviousObject=SelectObject (DirectDC,RetBmp);
HBRUSH sb=CreateSolidBrush(cNewColor);
FillRect(DirectDC,&tmprect,sb);
DeleteObject(sb);
BitBlt(MaskDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCCOPY);
BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCINVERT);
BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,MaskDC,0,0,SRCAND);
BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCINVERT);
DeleteObject(PreviousObject);
DeleteObject (bitmapTrans);
DeleteDC(MaskDC);
}
// clean up
DeleteDC(DirectDC);
}
// BufferDC is now useless
if (hTmpBitmap)
{
SelectObject(hBmpDC, hBmp);
DeleteObject(hTmpBitmap);
}
SelectObject(BufferDC,PreviousBufferObject);
DeleteDC(BufferDC);
}
}
return RetBmp;
}
//-----------------------------------------------------------------------------------
// ReplaceColor
// ( 9 times faster in my test than loading and replacing with ReplaceColor)
//
// Simple mapping of the CreateMappedBitmap which loads a bitmap from resources
// and replace one color by another.
//
// Instance : Instance of the module where to find the resource
// ResourseID : Id of the resource to load
// cOldColor : Color to replace
// cNewColor : Color used for replacement
//
// Retcode : HBITMAP of the modified bitmap or NULL for errors
//
//-----------------------------------------------------------------------------------
HBITMAP ReplaceColor (HINSTANCE Instance,WORD ResourseID,COLORREF cOldColor,COLORREF cNewColor)
{
COLORMAP tabcolormap;
tabcolormap.from=cOldColor;
tabcolormap.to=cNewColor;
return CreateMappedBitmap(Instance,ResourseID,0,&tabcolormap,1);
}
//-----------------------------------------------------------------------------------
// ReplaceColor
//
// Author : Dimitri Rochette drochette@coldcat.fr
// Specials Thanks to Joe Woodbury for his comments and code corrections
//
// Includes : Only <windows.h>
//
// hBmp : Source Bitmap
// cOldColor: Color to replace in hBmp
// cNewColor: Color used for replacement
// hBmpDC : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
//
// Retcode : HBITMAP of the modified bitmap or NULL for errors
//
//-----------------------------------------------------------------------------------
HBITMAP ReplaceColor (HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC)
{
HBITMAP RetBmp=NULL;
if (hBmp)
{
HDC BufferDC=CreateCompatibleDC(NULL); // DC for Source Bitmap
if (BufferDC)
{
HBITMAP hTmpBitmap = (HBITMAP) NULL;
if (hBmpDC)
if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP))
{
hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);
SelectObject(hBmpDC, hTmpBitmap);
}
HGDIOBJ PreviousBufferObject=SelectObject(BufferDC,hBmp);
// here BufferDC contains the bitmap
HDC DirectDC=CreateCompatibleDC(NULL); // DC for working
if (DirectDC)
{
// Get bitmap size
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
// create a BITMAPINFO with minimal initilisation for the CreateDIBSection
BITMAPINFO RGB32BitsBITMAPINFO;
ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
UINT * ptPixels; // pointer used for direct Bitmap pixels access
HBITMAP DirectBitmap= CreateDIBSection(DirectDC, (BITMAPINFO *)&RGB32BitsBITMAPINFO, DIB_RGB_COLORS,(void **)&ptPixels, NULL, 0);
if (DirectBitmap)
{
// here DirectBitmap!=NULL so ptPixels!=NULL no need to test
HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
BitBlt(DirectDC,0,0,bm.bmWidth,bm.bmHeight,BufferDC,0,0,SRCCOPY);
// here the DirectDC contains the bitmap
// Convert COLORREF to RGB (Invert RED and BLUE)
cOldColor=COLORREF2RGB(cOldColor);
cNewColor=COLORREF2RGB(cNewColor);
// After all the inits we can do the job : Replace Color
for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
{
if (ptPixels[i]==cOldColor) ptPixels[i]=cNewColor;
}
// little clean up
// Don't delete the result of SelectObject because it's our modified bitmap (DirectBitmap)
SelectObject(DirectDC,PreviousObject);
// finish
RetBmp=DirectBitmap;
}
// clean up
DeleteDC(DirectDC);
}
if (hTmpBitmap)
{
SelectObject(hBmpDC, hBmp);
DeleteObject(hTmpBitmap);
}
SelectObject(BufferDC,PreviousBufferObject);
// BufferDC is now useless
DeleteDC(BufferDC);
}
}
return RetBmp;
}
/*---------------------------------------------------------------------------
// Short Sample of the usage of ReplaceColor
HBITMAP hBmp2 = LoadBitmap(g_hinstance,MAKEINTRESOURCE(IDB_SAMPLEBITMAP));
HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // replace blue by green
DeleteObject(hBmp2);
// Use your modified Bitmap here
DeleteObject(hBmp);
-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------------
// WndProc
//
// Sample window message loop
// This function is here only for illustrating the use of ReplaceColor
//-----------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_NCCREATE:
{
InitCommonControls();
// We will create an ImageList and use transparency on purple
HBITMAP hBmp2 = LoadBitmap(g_hinstance,MAKEINTRESOURCE(IDB_SAMPLEBITMAP));
HBITMAP hBmp = ReplaceColor(hBmp2,0xff0000,0x00ff00); // replace blue by green
BITMAP bm;
GetObject(hBmp, sizeof(bm), &bm);
HIMAGELIST ImgList=ImageList_Create(bm.bmWidth,bm.bmHeight,ILC_COLOR32|ILC_MASK,8,8);
ImageList_Add(ImgList,hBmp2,NULL); // original image
ImageList_AddMasked(ImgList,hBmp,0xff00ff);
DeleteObject(hBmp);
hBmp = ReplaceColor(hBmp2,0x0000ff,0x00ff00); // replace red by green
ImageList_AddMasked(ImgList,hBmp,0xff00ff);
DeleteObject(hBmp);
hBmp = ReplaceColor(hBmp2,0x000000,0x00ff00); // replace black by green
ImageList_AddMasked(ImgList,hBmp,0xff00ff);
DeleteObject(hBmp);
hBmp = ReplaceColor(hBmp2,0xff00ff,0x00ff00); // replace purple by green
ImageList_AddMasked(ImgList,hBmp,0xff0000); // use blue for mask this time
DeleteObject(hBmp);
DeleteObject(hBmp2);
// save the imagelistref on GWLP_USERDATA
SetWindowLongPtr(hWnd,GWLP_USERDATA,(LONG_PTR)ImgList);
return (DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_PAINT:
{
hdc = BeginPaint (hWnd, &ps);
RECT Rect;
GetClientRect(hWnd,&Rect);
HGDIOBJ PreviousObj=SelectObject(hdc,CreateHatchBrush(HS_BDIAGONAL,0x0));
PatBlt(hdc,Rect.left,Rect.top,Rect.right,Rect.bottom,PATCOPY);
SelectObject(hdc,PreviousObj);
HIMAGELIST ImgList=(HIMAGELIST)GetWindowLongPtr(hWnd,GWLP_USERDATA);
TCHAR szBuffer[512];
if (ImgList)
{
for (int i=0;i<5;i++)
{
ImageList_DrawEx(ImgList,i,hdc,i*48,0,0,0,CLR_NONE ,CLR_NONE,ILD_NORMAL );
LoadString(g_hinstance,IDS_HELPSTRING01+i,szBuffer,(sizeof(szBuffer)/sizeof(szBuffer[0])-1));
TextOut(hdc,0,64+i*16,szBuffer,static_cast<int>(_tcslen(szBuffer)) );
}
}
EndPaint (hWnd, &ps);
}
break;
case WM_KEYDOWN:
if (wParam != VK_ESCAPE)
break;
case WM_DESTROY:
{
HIMAGELIST ImgList=(HIMAGELIST)GetWindowLongPtr(hWnd,GWLP_USERDATA);
if (ImgList)
{
ImageList_Destroy(ImgList);
}
PostQuitMessage(0);
}
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
//-----------------------------------------------------------------------------------
// WinMain
//-----------------------------------------------------------------------------------
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
WNDCLASS wc;
HWND hWnd;
MSG msg;
g_hinstance=hInstance;
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (RegisterClass(&wc))
{
hWnd = CreateWindow(szClassName, _T("Replace color"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, 320, 240, NULL, NULL, hInstance, NULL);
if (hWnd)
{
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam!=0);
}
}
return -1;
}