-
Notifications
You must be signed in to change notification settings - Fork 1
/
dib.cpp
668 lines (567 loc) · 16.8 KB
/
dib.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
* PROJECT: PAINT for ReactOS
* LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
* PURPOSE: Some DIB related functions
* COPYRIGHT: Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
*/
#include "precomp.h"
INT g_fileSize = 0;
float g_xDpi = 96;
float g_yDpi = 96;
SYSTEMTIME g_fileTime;
#define WIDTHBYTES(i) (((i) + 31) / 32 * 4)
struct BITMAPINFOEX : BITMAPINFO
{
RGBQUAD bmiColorsExtra[256 - 1];
};
/* FUNCTIONS ********************************************************/
// Convert DPI (dots per inch) into PPCM (pixels per centimeter)
float PpcmFromDpi(float dpi)
{
// 1 DPI is 0.0254 meter. 1 centimeter is 1/100 meter.
return dpi / (0.0254f * 100.0f);
}
HBITMAP
CreateDIBWithProperties(int width, int height)
{
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
}
HBITMAP
CreateMonoBitmap(int width, int height, BOOL bWhite)
{
HBITMAP hbm = CreateBitmap(width, height, 1, 1, NULL);
if (hbm == NULL)
return NULL;
if (bWhite)
{
HDC hdc = CreateCompatibleDC(NULL);
HGDIOBJ hbmOld = SelectObject(hdc, hbm);
RECT rc = { 0, 0, width, height };
FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
SelectObject(hdc, hbmOld);
DeleteDC(hdc);
}
return hbm;
}
HBITMAP
CreateColorDIB(int width, int height, COLORREF rgb)
{
HBITMAP ret = CreateDIBWithProperties(width, height);
if (!ret)
return NULL;
if (rgb)
{
HDC hdc = CreateCompatibleDC(NULL);
HGDIOBJ hbmOld = SelectObject(hdc, ret);
RECT rc;
SetRect(&rc, 0, 0, width, height);
HBRUSH hbr = CreateSolidBrush(rgb);
FillRect(hdc, &rc, hbr);
DeleteObject(hbr);
SelectObject(hdc, hbmOld);
DeleteDC(hdc);
}
return ret;
}
HBITMAP CopyMonoImage(HBITMAP hbm, INT cx, INT cy)
{
BITMAP bm;
if (!::GetObjectW(hbm, sizeof(bm), &bm))
return NULL;
if (cx == 0 || cy == 0)
{
cx = bm.bmWidth;
cy = bm.bmHeight;
}
HBITMAP hbmNew = ::CreateBitmap(cx, cy, 1, 1, NULL);
if (!hbmNew)
return NULL;
HDC hdc1 = ::CreateCompatibleDC(NULL);
HDC hdc2 = ::CreateCompatibleDC(NULL);
HGDIOBJ hbm1Old = ::SelectObject(hdc1, hbm);
HGDIOBJ hbm2Old = ::SelectObject(hdc2, hbmNew);
::StretchBlt(hdc2, 0, 0, cx, cy, hdc1, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);
::SelectObject(hdc1, hbm1Old);
::SelectObject(hdc2, hbm2Old);
::DeleteDC(hdc1);
::DeleteDC(hdc2);
return hbmNew;
}
HBITMAP CachedBufferDIB(HBITMAP hbm, int minimalWidth, int minimalHeight)
{
if (minimalWidth <= 0)
minimalWidth = 1;
if (minimalHeight <= 0)
minimalHeight = 1;
BITMAP bm;
if (!GetObjectW(hbm, sizeof(bm), &bm))
hbm = NULL;
if (hbm && minimalWidth <= bm.bmWidth && minimalHeight <= bm.bmHeight)
return hbm;
if (hbm)
DeleteObject(hbm);
return CreateDIBWithProperties((minimalWidth * 3) / 2, (minimalHeight * 3) / 2);
}
int
GetDIBWidth(HBITMAP hBitmap)
{
BITMAP bm;
::GetObjectW(hBitmap, sizeof(BITMAP), &bm);
return bm.bmWidth;
}
int
GetDIBHeight(HBITMAP hBitmap)
{
BITMAP bm;
::GetObjectW(hBitmap, sizeof(BITMAP), &bm);
return bm.bmHeight;
}
BOOL SaveDIBToFile(HBITMAP hBitmap, LPCWSTR FileName, BOOL fIsMainFile, REFGUID guidFileType)
{
CWaitCursor waitCursor;
CImageDx img;
img.Attach(hBitmap);
HRESULT hr = img.SaveDx(FileName, guidFileType, g_xDpi, g_yDpi);
img.Detach();
if (FAILED(hr))
{
ShowError(IDS_SAVEERROR, FileName);
return FALSE;
}
if (!fIsMainFile)
return TRUE;
WIN32_FIND_DATAW find;
HANDLE hFind = ::FindFirstFileW(FileName, &find);
if (hFind == INVALID_HANDLE_VALUE)
{
ShowError(IDS_SAVEERROR, FileName);
return FALSE;
}
::FindClose(hFind);
SetFileInfo(FileName, &find, TRUE);
g_imageSaved = TRUE;
return TRUE;
}
void SetFileInfo(LPCWSTR name, LPWIN32_FIND_DATAW pFound, BOOL isAFile)
{
// update file time and size
if (pFound)
{
FILETIME ft;
::FileTimeToLocalFileTime(&pFound->ftLastWriteTime, &ft);
::FileTimeToSystemTime(&ft, &g_fileTime);
g_fileSize = pFound->nFileSizeLow;
}
else
{
ZeroMemory(&g_fileTime, sizeof(g_fileTime));
g_fileSize = 0;
}
// update g_szFileName
if (name && name[0])
{
CStringW strName = name;
::GetFullPathNameW(strName, _countof(g_szFileName), g_szFileName, NULL);
// The following code won't work correctly when (name == g_szFileName):
// ::GetFullPathNameW(name, _countof(g_szFileName), g_szFileName, NULL);
}
else
{
::LoadStringW(g_hinstExe, IDS_DEFAULTFILENAME, g_szFileName, _countof(g_szFileName));
}
// set title
CStringW strTitle;
strTitle.Format(IDS_WINDOWTITLE, PathFindFileNameW(g_szFileName));
mainWindow.SetWindowText(strTitle);
// update file info and recent
g_isAFile = isAFile;
if (g_isAFile)
registrySettings.SetMostRecentFile(g_szFileName);
g_imageSaved = TRUE;
}
HBITMAP InitializeImage(LPCWSTR name, LPWIN32_FIND_DATAW pFound, BOOL isFile)
{
COLORREF white = RGB(255, 255, 255);
HBITMAP hBitmap = CreateColorDIB(registrySettings.BMPWidth, registrySettings.BMPHeight, white);
if (hBitmap == NULL)
{
ShowOutOfMemory();
return NULL;
}
HDC hScreenDC = ::GetDC(NULL);
g_xDpi = (float)::GetDeviceCaps(hScreenDC, LOGPIXELSX);
g_yDpi = (float)::GetDeviceCaps(hScreenDC, LOGPIXELSY);
::ReleaseDC(NULL, hScreenDC);
return SetBitmapAndInfo(hBitmap, name, pFound, isFile);
}
HBITMAP SetBitmapAndInfo(HBITMAP hBitmap, LPCWSTR name, LPWIN32_FIND_DATAW pFound, BOOL isFile)
{
// update image
canvasWindow.updateScrollPos();
imageModel.PushImageForUndo(hBitmap);
imageModel.ClearHistory();
SetFileInfo(name, pFound, isFile);
g_imageSaved = TRUE;
return hBitmap;
}
HBITMAP DoLoadImageFile(HWND hwnd, LPCWSTR name, BOOL fIsMainFile)
{
CWaitCursor waitCursor;
// find the file
WIN32_FIND_DATAW find;
HANDLE hFind = ::FindFirstFileW(name, &find);
if (hFind == INVALID_HANDLE_VALUE) // does not exist
{
ShowError(IDS_LOADERRORTEXT, name);
return NULL;
}
::FindClose(hFind);
// is file empty?
if (find.nFileSizeLow == 0 && find.nFileSizeHigh == 0)
{
if (fIsMainFile)
return InitializeImage(name, &find, TRUE);
}
// load the image
CImageDx img;
float xDpi = 0, yDpi = 0;
HRESULT hr = img.LoadDx(name, &xDpi, &yDpi);
if (FAILED(hr) && fIsMainFile)
{
imageModel.ClearHistory();
hr = img.LoadDx(name, &xDpi, &yDpi);
}
if (FAILED(hr))
{
ATLTRACE("hr: 0x%08lX\n", hr);
ShowError(IDS_LOADERRORTEXT, name);
return NULL;
}
HBITMAP hBitmap = img.Detach();
if (!fIsMainFile)
return hBitmap;
if (xDpi <= 0 || yDpi <= 0)
{
HDC hDC = ::GetDC(NULL);
xDpi = (float)::GetDeviceCaps(hDC, LOGPIXELSX);
yDpi = (float)::GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(NULL, hDC);
}
g_xDpi = xDpi;
g_yDpi = yDpi;
SetBitmapAndInfo(hBitmap, name, &find, TRUE);
return hBitmap;
}
HBITMAP Rotate90DegreeBlt(HDC hDC1, INT cx, INT cy, BOOL bRight, BOOL bMono)
{
HBITMAP hbm2;
if (bMono)
hbm2 = ::CreateBitmap(cy, cx, 1, 1, NULL);
else
hbm2 = CreateDIBWithProperties(cy, cx);
if (!hbm2)
return NULL;
HDC hDC2 = CreateCompatibleDC(NULL);
HGDIOBJ hbm2Old = SelectObject(hDC2, hbm2);
if (bRight)
{
for (INT y = 0; y < cy; ++y)
{
for (INT x = 0; x < cx; ++x)
{
COLORREF rgb = GetPixel(hDC1, x, y);
SetPixelV(hDC2, cy - (y + 1), x, rgb);
}
}
}
else
{
for (INT y = 0; y < cy; ++y)
{
for (INT x = 0; x < cx; ++x)
{
COLORREF rgb = GetPixel(hDC1, x, y);
SetPixelV(hDC2, y, cx - (x + 1), rgb);
}
}
}
SelectObject(hDC2, hbm2Old);
DeleteDC(hDC2);
return hbm2;
}
HBITMAP SkewDIB(HDC hDC1, HBITMAP hbm, INT nDegree, BOOL bVertical, BOOL bMono)
{
CWaitCursor waitCursor;
if (nDegree == 0)
return CopyDIBImage(hbm);
const double eTan = tan(abs(nDegree) * M_PI / 180);
BITMAP bm;
::GetObjectW(hbm, sizeof(bm), &bm);
INT cx = bm.bmWidth, cy = bm.bmHeight, dx = 0, dy = 0;
if (bVertical)
dy = INT(cx * eTan);
else
dx = INT(cy * eTan);
if (dx == 0 && dy == 0)
return CopyDIBImage(hbm);
HBITMAP hbmNew;
if (bMono)
hbmNew = CreateMonoBitmap(cx + dx, cy + dy, FALSE);
else
hbmNew = CreateColorDIB(cx + dx, cy + dy, RGB(255, 255, 255));
if (!hbmNew)
return NULL;
HDC hDC2 = CreateCompatibleDC(NULL);
HGDIOBJ hbm2Old = SelectObject(hDC2, hbmNew);
if (bVertical)
{
for (INT x = 0; x < cx; ++x)
{
INT delta = INT(x * eTan);
if (nDegree > 0)
::BitBlt(hDC2, x, (dy - delta), 1, cy, hDC1, x, 0, SRCCOPY);
else
::BitBlt(hDC2, x, delta, 1, cy, hDC1, x, 0, SRCCOPY);
}
}
else
{
for (INT y = 0; y < cy; ++y)
{
INT delta = INT(y * eTan);
if (nDegree > 0)
::BitBlt(hDC2, (dx - delta), y, cx, 1, hDC1, 0, y, SRCCOPY);
else
::BitBlt(hDC2, delta, y, cx, 1, hDC1, 0, y, SRCCOPY);
}
}
SelectObject(hDC2, hbm2Old);
DeleteDC(hDC2);
return hbmNew;
}
HBITMAP getSubImage(HBITMAP hbmWhole, const RECT& rcPartial)
{
CRect rc = rcPartial;
HBITMAP hbmPart = CreateDIBWithProperties(rc.Width(), rc.Height());
if (!hbmPart)
return NULL;
HDC hDC1 = ::CreateCompatibleDC(NULL);
HDC hDC2 = ::CreateCompatibleDC(NULL);
HGDIOBJ hbm1Old = ::SelectObject(hDC1, hbmWhole);
HGDIOBJ hbm2Old = ::SelectObject(hDC2, hbmPart);
::BitBlt(hDC2, 0, 0, rc.Width(), rc.Height(), hDC1, rc.left, rc.top, SRCCOPY);
::SelectObject(hDC1, hbm1Old);
::SelectObject(hDC2, hbm2Old);
::DeleteDC(hDC1);
::DeleteDC(hDC2);
return hbmPart;
}
void putSubImage(HBITMAP hbmWhole, const RECT& rcPartial, HBITMAP hbmPart)
{
CRect rc = rcPartial;
HDC hDC1 = ::CreateCompatibleDC(NULL);
HDC hDC2 = ::CreateCompatibleDC(NULL);
HGDIOBJ hbm1Old = ::SelectObject(hDC1, hbmWhole);
HGDIOBJ hbm2Old = ::SelectObject(hDC2, hbmPart);
::BitBlt(hDC1, rc.left, rc.top, rc.Width(), rc.Height(), hDC2, 0, 0, SRCCOPY);
::SelectObject(hDC1, hbm1Old);
::SelectObject(hDC2, hbm2Old);
::DeleteDC(hDC1);
::DeleteDC(hDC2);
}
struct BITMAPINFODX : BITMAPINFO
{
RGBQUAD bmiColorsAdditional[256 - 1];
};
HGLOBAL BitmapToClipboardDIB(HBITMAP hBitmap)
{
CWaitCursor waitCursor;
BITMAP bm;
if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
return NULL;
BITMAPINFODX bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = bm.bmBitsPixel;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = bm.bmWidthBytes * bm.bmHeight;
INT cColors;
if (bm.bmBitsPixel < 16)
cColors = 1 << bm.bmBitsPixel;
else
cColors = 0;
HDC hDC = CreateCompatibleDC(NULL);
if (cColors)
{
HGDIOBJ hbmOld = SelectObject(hDC, hBitmap);
cColors = GetDIBColorTable(hDC, 0, cColors, bmi.bmiColors);
SelectObject(hDC, hbmOld);
}
DWORD cbColors = cColors * sizeof(RGBQUAD);
DWORD dwSize = sizeof(BITMAPINFOHEADER) + cbColors + bmi.bmiHeader.biSizeImage;
HGLOBAL hGlobal = GlobalAlloc(GHND | GMEM_SHARE, dwSize);
if (hGlobal)
{
LPBYTE pb = (LPBYTE)GlobalLock(hGlobal);
if (pb)
{
CopyMemory(pb, &bmi, sizeof(BITMAPINFOHEADER));
pb += sizeof(BITMAPINFOHEADER);
CopyMemory(pb, bmi.bmiColors, cbColors);
pb += cbColors;
GetDIBits(hDC, hBitmap, 0, bm.bmHeight, pb, &bmi, DIB_RGB_COLORS);
GlobalUnlock(hGlobal);
}
else
{
GlobalFree(hGlobal);
hGlobal = NULL;
}
}
DeleteDC(hDC);
return hGlobal;
}
HBITMAP BitmapFromClipboardDIB(HGLOBAL hGlobal)
{
CWaitCursor waitCursor;
LPBYTE pb = (LPBYTE)GlobalLock(hGlobal);
if (!pb)
return NULL;
LPBITMAPINFO pbmi = (LPBITMAPINFO)pb;
pb += pbmi->bmiHeader.biSize;
INT cColors = 0, cbColors = 0;
if (pbmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
{
LPBITMAPCOREINFO pbmci = (LPBITMAPCOREINFO)pbmi;
WORD BitCount = pbmci->bmciHeader.bcBitCount;
if (BitCount < 16)
{
cColors = (1 << BitCount);
cbColors = cColors * sizeof(RGBTRIPLE);
pb += cbColors;
}
}
else if (pbmi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER))
{
WORD BitCount = pbmi->bmiHeader.biBitCount;
if (BitCount < 16)
{
cColors = (1 << BitCount);
cbColors = cColors * sizeof(RGBQUAD);
pb += cbColors;
}
}
HDC hDC = CreateCompatibleDC(NULL);
HBITMAP hBitmap = CreateDIBSection(hDC, pbmi, DIB_RGB_COLORS, NULL, NULL, 0);
if (hBitmap)
{
SetDIBits(hDC, hBitmap, 0, labs(pbmi->bmiHeader.biHeight), pb, pbmi, DIB_RGB_COLORS);
}
DeleteDC(hDC);
GlobalUnlock(hGlobal);
return hBitmap;
}
HBITMAP BitmapFromHEMF(HENHMETAFILE hEMF)
{
CWaitCursor waitCursor;
ENHMETAHEADER header;
if (!GetEnhMetaFileHeader(hEMF, sizeof(header), &header))
return NULL;
CRect rc = *(LPRECT)&header.rclBounds;
INT cx = rc.Width(), cy = rc.Height();
HBITMAP hbm = CreateColorDIB(cx, cy, RGB(255, 255, 255));
if (!hbm)
return NULL;
HDC hDC = CreateCompatibleDC(NULL);
HGDIOBJ hbmOld = SelectObject(hDC, hbm);
PlayEnhMetaFile(hDC, hEMF, &rc);
SelectObject(hDC, hbmOld);
DeleteDC(hDC);
return hbm;
}
BOOL IsBitmapBlackAndWhite(HBITMAP hbm)
{
CWaitCursor waitCursor;
BITMAP bm;
if (!::GetObjectW(hbm, sizeof(bm), &bm))
return FALSE;
if (bm.bmBitsPixel == 1)
return TRUE;
BITMAPINFOEX bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
DWORD widthbytes = WIDTHBYTES(24 * bm.bmWidth);
DWORD cbBits = widthbytes * bm.bmHeight;
LPBYTE pbBits = new BYTE[cbBits];
HDC hdc = ::CreateCompatibleDC(NULL);
::GetDIBits(hdc, hbm, 0, bm.bmHeight, pbBits, &bmi, DIB_RGB_COLORS);
::DeleteDC(hdc);
BOOL bBlackAndWhite = TRUE;
for (LONG y = 0; y < bm.bmHeight; ++y)
{
LPBYTE pbLine = &pbBits[widthbytes * y];
for (LONG x = 0; x < bm.bmWidth; ++x)
{
BYTE Blue = *pbLine++;
BYTE Green = *pbLine++;
BYTE Red = *pbLine++;
COLORREF rgbColor = RGB(Red, Green, Blue);
if (rgbColor != RGB(0, 0, 0) && rgbColor != RGB(255, 255, 255))
{
bBlackAndWhite = FALSE;
goto Finish;
}
}
}
Finish:
delete[] pbBits;
return bBlackAndWhite;
}
HBITMAP ConvertToBlackAndWhite(HBITMAP hbm)
{
CWaitCursor waitCursor;
BITMAP bm;
if (!::GetObjectW(hbm, sizeof(bm), &bm))
return NULL;
BITMAPINFOEX bmi;
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bm.bmWidth;
bmi.bmiHeader.biHeight = bm.bmHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 1;
bmi.bmiColors[1].rgbBlue = 255;
bmi.bmiColors[1].rgbGreen = 255;
bmi.bmiColors[1].rgbRed = 255;
HDC hdc = ::CreateCompatibleDC(NULL);
LPVOID pvMonoBits;
HBITMAP hMonoBitmap = ::CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pvMonoBits, NULL, 0);
if (!hMonoBitmap)
{
::DeleteDC(hdc);
return NULL;
}
HBITMAP hNewBitmap = CreateDIBWithProperties(bm.bmWidth, bm.bmHeight);
if (hNewBitmap)
{
::GetDIBits(hdc, hbm, 0, bm.bmHeight, pvMonoBits, &bmi, DIB_RGB_COLORS);
::SetDIBits(hdc, hNewBitmap, 0, bm.bmHeight, pvMonoBits, &bmi, DIB_RGB_COLORS);
}
::DeleteObject(hMonoBitmap);
::DeleteDC(hdc);
return hNewBitmap;
}