2121// Standard library
2222#include < vector>
2323
24- HBITMAP getResourceBitmap (unsigned int id)
24+ namespace Bitmap
25+ {
26+
27+ HBITMAP getResource (unsigned int id)
2528{
2629 HINSTANCE hinstance = (HINSTANCE)GetModuleHandle (nullptr );
2730 HBITMAP bitmap = (HBITMAP)LoadImageA (hinstance, MAKEINTRESOURCEA (id), IMAGE_BITMAP, 0 , 0 , 0 );
@@ -35,17 +38,17 @@ HBITMAP getResourceBitmap(unsigned int id)
3538 return bitmap;
3639}
3740
38- bool replaceBitmapColor (HBITMAP hbmp , COLORREF oldColor, COLORREF newColor)
41+ bool replaceColor (HBITMAP hbitmap , COLORREF oldColor, COLORREF newColor)
3942{
40- if (!hbmp ) {
43+ if (!hbitmap ) {
4144 return false ;
4245 }
4346
4447 DeviceContextHandleWrapper hdc (GetDC (HWND_DESKTOP), DeviceContextHandleWrapper::Referenced);
4548
4649 BITMAP bitmap;
4750 memset (&bitmap, 0 , sizeof (bitmap));
48- if (!GetObject (hbmp , sizeof (bitmap), &bitmap)) {
51+ if (!GetObject (hbitmap , sizeof (bitmap), &bitmap)) {
4952 WARNING_PRINTF (" failed to get bitmap object, GetObject() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
5053 return false ;
5154 }
@@ -59,7 +62,7 @@ bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor)
5962 bitmapInfo.bmiHeader .biBitCount = 32 ;
6063
6164 std::vector<COLORREF> pixels (bitmap.bmWidth * bitmap.bmHeight );
62- if (!GetDIBits (hdc, hbmp , 0 , bitmap.bmHeight , &pixels[0 ], &bitmapInfo, DIB_RGB_COLORS)) {
65+ if (!GetDIBits (hdc, hbitmap , 0 , bitmap.bmHeight , &pixels[0 ], &bitmapInfo, DIB_RGB_COLORS)) {
6366 WARNING_PRINTF (" failed to get bitmap bits, GetDIBits() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
6467 return false ;
6568 }
@@ -71,76 +74,11 @@ bool replaceBitmapColor(HBITMAP hbmp, COLORREF oldColor, COLORREF newColor)
7174 }
7275 }
7376
74- if (!SetDIBits (hdc, hbmp , 0 , bitmap.bmHeight , &pixels[0 ], &bitmapInfo, DIB_RGB_COLORS)) {
77+ if (!SetDIBits (hdc, hbitmap , 0 , bitmap.bmHeight , &pixels[0 ], &bitmapInfo, DIB_RGB_COLORS)) {
7578 WARNING_PRINTF (" failed to set bitmap bits, SetDIBits() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
7679 }
7780
7881 return replaced;
7982}
8083
81- bool replaceBitmapMaskColor (HBITMAP hbmp, HBITMAP hmask, COLORREF newColor)
82- {
83- if (!hbmp || !hmask) {
84- return false ;
85- }
86-
87- BITMAP maskBitmap;
88- if (!GetObjectA (hmask, sizeof (BITMAP), &maskBitmap)) {
89- WARNING_PRINTF (
90- " failed to get mask bitmap object, GetObject() failed: %s\n " ,
91- StringUtility::lastErrorString ().c_str ());
92- return false ;
93- }
94-
95- // BITMAP colorBitmap;
96- // if (!GetObjectA(hbmp, sizeof(BITMAP), &colorBitmap)) {
97- // WARNING_PRINTF(
98- // "failed to get color bitmap object, GetObject() failed: %s\n",
99- // StringUtility::lastErrorString().c_str());
100- // return false;
101- // }
102-
103- DeviceContextHandleWrapper desktopDC (GetDC (HWND_DESKTOP), DeviceContextHandleWrapper::Referenced);
104- if (!desktopDC) {
105- WARNING_PRINTF (
106- " failed to get desktop device context, GetDC() failed: %s\n " ,
107- StringUtility::lastErrorString ().c_str ());
108- return false ;
109- }
110-
111- DeviceContextHandleWrapper maskDC (CreateCompatibleDC (desktopDC), DeviceContextHandleWrapper::Created);
112- DeviceContextHandleWrapper colorDC (CreateCompatibleDC (desktopDC), DeviceContextHandleWrapper::Created);
113- if (!maskDC || !colorDC) {
114- WARNING_PRINTF (
115- " failed to create compatible device contexts, CreateCompatibleDC() failed: %s\n " ,
116- StringUtility::lastErrorString ().c_str ());
117- return false ;
118- }
119-
120- if (!maskDC.selectObject (hmask) || !colorDC.selectObject (hbmp)) {
121- return false ;
122- }
123-
124- bool replaced = false ;
125- for (int y = 0 ; y < maskBitmap.bmHeight ; ++y) {
126- for (int x = 0 ; x < maskBitmap.bmWidth ; ++x) {
127- COLORREF maskPixel = GetPixel (maskDC, x, y);
128- if (maskPixel == RGB (255 , 255 , 255 )) {
129- SetPixel (colorDC, x, y, newColor);
130- replaced = true ;
131- }
132- }
133- }
134-
135- return replaced;
136- }
137-
138- HBITMAP scaleBitmap (HBITMAP hbmp, int width, int height)
139- {
140- HBITMAP scaledBitmap = (HBITMAP)CopyImage (hbmp, IMAGE_BITMAP, width, height, LR_COPYDELETEORG);
141- if (!scaledBitmap) {
142- WARNING_PRINTF (" failed to scale bitmap, CopyImage() failed: %s\n " , StringUtility::lastErrorString ().c_str ());
143- return nullptr ;
144- }
145- return scaledBitmap;
146- }
84+ } // namespace Bitmap
0 commit comments