summaryrefslogtreecommitdiff
path: root/plugins/MenuItemEx/images.c
blob: 51903294bba6f4e0dcddbba5e9d763a20b4b4455 (plain)
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
#include "menuex.h"
#include "images.h"

extern HINSTANCE hinstance;


void HalfBitmap32Alpha(HBITMAP hBitmap)
{
	BITMAP bmp;
	DWORD dwLen;
	BYTE *p;
	int x, y;

	GetObject(hBitmap, sizeof(bmp), &bmp);

	if (bmp.bmBitsPixel != 32)
		return;

	dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
	p = (BYTE *)malloc(dwLen);
	if (p == NULL)
		return;
	memset(p, 0, dwLen);

	GetBitmapBits(hBitmap, dwLen, p);

	for (y = 0; y < bmp.bmHeight; ++y) {
        BYTE *px = p + bmp.bmWidth * 4 * y;

        for (x = 0; x < bmp.bmWidth; ++x) 
		{
			px[3]>>=1;
			px += 4;
		}
	}

	SetBitmapBits(hBitmap, dwLen, p);

	free(p);
}


// Make a bitmap all transparent, but only if it is a 32bpp
//void MakeBmpTransparent(HBITMAP hBitmap)
//{
//	BITMAP bmp;
//	DWORD dwLen;
//	BYTE *p;
//
//	GetObject(hBitmap, sizeof(bmp), &bmp);
//
//	if (bmp.bmBitsPixel != 32)
//		return;
//
//	dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
//	p = (BYTE *)malloc(dwLen);
//	if (p == NULL)
//		return;
//
//	memset(p, 0, dwLen);
//	SetBitmapBits(hBitmap, dwLen, p);
//
//	free(p);
//}


// Correct alpha from bitmaps loaded without it (it cames with 0 and should be 255)
void CorrectBitmap32Alpha(HBITMAP hBitmap, BOOL force)
{
	BITMAP bmp;
	DWORD dwLen;
	BYTE *p;
	int x, y;
	BOOL fixIt;

	GetObject(hBitmap, sizeof(bmp), &bmp);

	if (bmp.bmBitsPixel != 32)
		return;

	dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
	p = (BYTE *)malloc(dwLen);
	if (p == NULL)
		return;
	memset(p, 0, dwLen);

	GetBitmapBits(hBitmap, dwLen, p);

	fixIt = TRUE;
	for (y = 0; fixIt && y < bmp.bmHeight; ++y) {
        BYTE *px = p + bmp.bmWidth * 4 * y;

        for (x = 0; fixIt && x < bmp.bmWidth; ++x) 
		{
			if (px[3] != 0 && !force) 
			{
				fixIt = FALSE;
			}
			else
			{
				if (px[0] != 0 || px[1] != 0 || px[2] != 0)
					px[3] = 255;
			}

			px += 4;
		}
	}

	if (fixIt)
		SetBitmapBits(hBitmap, dwLen, p);

	free(p);
}


HBITMAP CopyBitmapTo32(HBITMAP hBitmap)
{
	BITMAPINFO RGB32BitsBITMAPINFO; 
	BYTE * ptPixels;
	HBITMAP hDirectBitmap;

	BITMAP bmp;
	DWORD dwLen;
	BYTE *p;

	GetObject(hBitmap, sizeof(bmp), &bmp);

	dwLen = bmp.bmWidth * bmp.bmHeight * 4;
	p = (BYTE *)malloc(dwLen);
	if (p == NULL)
		return NULL;

	// Create bitmap
	ZeroMemory(&RGB32BitsBITMAPINFO, sizeof(BITMAPINFO));
	RGB32BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	RGB32BitsBITMAPINFO.bmiHeader.biWidth = bmp.bmWidth;
	RGB32BitsBITMAPINFO.bmiHeader.biHeight = bmp.bmHeight;
	RGB32BitsBITMAPINFO.bmiHeader.biPlanes = 1;
	RGB32BitsBITMAPINFO.bmiHeader.biBitCount = 32;

	hDirectBitmap = CreateDIBSection(NULL, 
									(BITMAPINFO *)&RGB32BitsBITMAPINFO, 
									DIB_RGB_COLORS,
									(void **)&ptPixels, 
									NULL, 0);

	// Copy data
	if (bmp.bmBitsPixel != 32)
	{
		HDC hdcOrig, hdcDest;
		HBITMAP oldOrig, oldDest;
		
		hdcOrig = CreateCompatibleDC(NULL);
		oldOrig = (HBITMAP) SelectObject(hdcOrig, hBitmap);

		hdcDest = CreateCompatibleDC(NULL);
		oldDest = (HBITMAP) SelectObject(hdcDest, hDirectBitmap);

		BitBlt(hdcDest, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcOrig, 0, 0, SRCCOPY);

		SelectObject(hdcDest, oldDest);
		DeleteObject(hdcDest);
		SelectObject(hdcOrig, oldOrig);
		DeleteObject(hdcOrig);

		// Set alpha
		CorrectBitmap32Alpha(hDirectBitmap, FALSE);
	}
	else
	{
		GetBitmapBits(hBitmap, dwLen, p);
		SetBitmapBits(hDirectBitmap, dwLen, p);
	}

	free(p);

	return hDirectBitmap;
}


//HBITMAP CreateBitmap32(int cx, int cy)
//{
//	BITMAPINFO RGB32BitsBITMAPINFO; 
//	UINT * ptPixels;
//	HBITMAP DirectBitmap;
//
//	ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
//	RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
//	RGB32BitsBITMAPINFO.bmiHeader.biWidth=cx;//bm.bmWidth;
//	RGB32BitsBITMAPINFO.bmiHeader.biHeight=cy;//bm.bmHeight;
//	RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
//	RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;
//
//	DirectBitmap = CreateDIBSection(NULL, 
//									(BITMAPINFO *)&RGB32BitsBITMAPINFO, 
//									DIB_RGB_COLORS,
//									(void **)&ptPixels, 
//									NULL, 0);
//	return DirectBitmap;
//}


BOOL MakeBitmap32(HBITMAP *hBitmap)
{
    BITMAP bmp;

	GetObject(*hBitmap, sizeof(bmp), &bmp);

	if (bmp.bmBitsPixel != 32) {
		// Convert to 32 bpp
		HBITMAP hBmpTmp = CopyBitmapTo32(*hBitmap);
		DeleteObject(*hBitmap);
		*hBitmap = hBmpTmp;
	} 

	return TRUE;
}


//#define GET_PIXEL(__P__, __X__, __Y__) ( __P__ + width * 4 * (__Y__) + 4 * (__X__))

//BOOL MakeGrayscale(HBITMAP *hBitmap)
//{
//	BYTE *p = NULL;
//	BYTE *p1;
//	DWORD dwLen;
//    int width, height, x, y;
//    BITMAP bmp;
//
//	GetObject(*hBitmap, sizeof(bmp), &bmp);
//    width = bmp.bmWidth;
//	height = bmp.bmHeight;
//
//	dwLen = width * height * 4;
//	p = (BYTE *)malloc(dwLen);
//    if (p == NULL) 
//	{
//		return FALSE;
//	}
//
//	if (bmp.bmBitsPixel != 32)
//	{
//		// Convert to 32 bpp
//		HBITMAP hBmpTmp = CopyBitmapTo32(*hBitmap);
//		DeleteObject(*hBitmap);
//		*hBitmap = hBmpTmp;
//	} 
//	GetBitmapBits(*hBitmap, dwLen, p);
//
//	// Make grayscale
//	for (y = 0 ; y < height ; y++)
//	{
//		for (x = 0 ; x < width ; x++)
//		{
//			p1 = GET_PIXEL(p, x, y);
//			p1[0] = p1[1] = p1[2] = ( p1[0] + p1[1] + p1[2] ) / 3;
//		}
//	}
//
//    dwLen = SetBitmapBits(*hBitmap, dwLen, p);
//    free(p);
//
//	return TRUE;
//}


HICON MakeHalfAlphaIcon(HICON SourceIcon)
{
	ICONINFO TargetIconInfo;
	BITMAP TargetBitmapInfo;
	HICON TargetIcon, TempIcon;

	TempIcon = CopyIcon(SourceIcon);
	if ( !GetIconInfo(TempIcon, &TargetIconInfo))
		return NULL;

	if ( !GetObject(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo))
		return NULL;

	MakeBitmap32(&TargetIconInfo.hbmColor);
	HalfBitmap32Alpha(TargetIconInfo.hbmColor);
	TargetIcon = CreateIconIndirect(&TargetIconInfo);

	DestroyIcon(TempIcon);
	DeleteObject(TargetIconInfo.hbmColor);
	DeleteObject(TargetIconInfo.hbmMask);
	return TargetIcon;
}


//HICON MakeGrayscaleIcon(HICON SourceIcon)
//{
//	ICONINFO TargetIconInfo;
//	BITMAP TargetBitmapInfo;
//	HICON TargetIcon, TempIcon;
//
//	TempIcon = CopyIcon(SourceIcon);
//	if (! GetIconInfo(TempIcon, &TargetIconInfo) || GetObject(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo)==0) return NULL;
//
//	MakeGrayscale(&TargetIconInfo.hbmColor);
//
//	TargetIcon = CreateIconIndirect(&TargetIconInfo);
//	DestroyIcon(TempIcon);
//
//	return TargetIcon;
//}


HICON BindOverlayIcon(HICON SourceIcon,LPCSTR OverlayIconName)
{
	ICONINFO OverlayIconInfo, TargetIconInfo;
	BITMAP OverlayBitmapInfo, TargetBitmapInfo;
	HBITMAP OldOverlayBitmap, OldTargetBitmap;
	HICON OverlayIcon, TargetIcon, TempIcon;
	HDC OverlayDC, TargetDC;
	BLENDFUNCTION bf = {0,0,255,1};

	TempIcon = CopyIcon(SourceIcon);
	if ( !GetIconInfo( TempIcon, &TargetIconInfo ))
		return NULL;

	MakeBitmap32(&TargetIconInfo.hbmColor);
	CorrectBitmap32Alpha(TargetIconInfo.hbmColor, FALSE);
	GetObject(TargetIconInfo.hbmColor, sizeof(BITMAP), &TargetBitmapInfo);

	OverlayIcon = (HICON)CallService( MS_SKIN2_GETICON, 0, (LPARAM)OverlayIconName);
	if ( !GetIconInfo(OverlayIcon, &OverlayIconInfo) || !GetObject(OverlayIconInfo.hbmColor, sizeof(BITMAP), &OverlayBitmapInfo))
		return NULL;

	TargetDC = CreateCompatibleDC(NULL);
	OldTargetBitmap = (HBITMAP)SelectObject(TargetDC, TargetIconInfo.hbmColor);

	OverlayDC = CreateCompatibleDC(NULL);
	OldOverlayBitmap = (HBITMAP)SelectObject(OverlayDC, OverlayIconInfo.hbmColor);

	AlphaBlend(TargetDC, 0, 0, TargetBitmapInfo.bmWidth, TargetBitmapInfo.bmHeight,
			   OverlayDC, 0, 0, OverlayBitmapInfo.bmWidth, OverlayBitmapInfo.bmHeight, bf);

	SelectObject(TargetDC, TargetIconInfo.hbmMask);
	SelectObject(OverlayDC, OverlayIconInfo.hbmMask);

	BitBlt(TargetDC, 0, 0, TargetBitmapInfo.bmWidth, TargetBitmapInfo.bmHeight,
	       OverlayDC, 0, 0, SRCCOPY);

	TargetIcon = CreateIconIndirect(&TargetIconInfo);
	DestroyIcon(TempIcon);
	//DestroyIcon(OverlayIcon);

	SelectObject(TargetDC, OldTargetBitmap);
	DeleteObject(TargetIconInfo.hbmColor);
	DeleteObject(TargetIconInfo.hbmMask);
	DeleteDC(TargetDC);

	SelectObject(OverlayDC, OldOverlayBitmap);
	DeleteObject(OverlayIconInfo.hbmColor);
	DeleteObject(OverlayIconInfo.hbmMask);
	DeleteDC(OverlayDC);

	return TargetIcon;
}


// EOF