summaryrefslogtreecommitdiff
path: root/src/modules/utils/imgconv.cpp
blob: 20550b12c7ec6dc0338a70d72dbad35e7e54a2cb (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "..\..\core\commonheaders.h"

typedef DWORD ARGB;

void InitBitmapInfo(BITMAPINFO &bmi,  const SIZE &size)
{
	ZeroMemory(&bmi, sizeof(BITMAPINFO));
	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biCompression = BI_RGB;
	bmi.bmiHeader.biBitCount = 32;

	bmi.bmiHeader.biWidth = size.cx;
	bmi.bmiHeader.biHeight = size.cy;
}

void ConvertToPARGB32(HDC hdc, ARGB *pargb, HBITMAP hbmp, SIZE& sizImage, int cxRow)
{
	BITMAPINFO bmi;
	InitBitmapInfo(bmi, sizImage);

	void *pvBits = malloc(sizImage.cx * 4 * sizImage.cy);
	if (GetDIBits(hdc, hbmp, 0, bmi.bmiHeader.biHeight, pvBits, &bmi, DIB_RGB_COLORS) == bmi.bmiHeader.biHeight) {
		ULONG cxDelta = cxRow - bmi.bmiHeader.biWidth;
		ARGB *pargbMask = (ARGB *)pvBits;

		for (ULONG y = bmi.bmiHeader.biHeight + 1; --y;) {
			for (ULONG x = bmi.bmiHeader.biWidth + 1; --x;) {
				if (*pargbMask++) {
					// transparent pixel
					*pargb++=0;
				}
				else {
					// opaque pixel
					*pargb++ |= 0xFF000000;
				}
			}

			pargb += cxDelta;
		}
	}
	free(pvBits);
}

bool HasAlpha(ARGB *pargb, SIZE& sizImage, int cxRow)
{
	ULONG cxDelta = cxRow - sizImage.cx;
	for (ULONG y = sizImage.cy; y--;) {
		for (ULONG x = sizImage.cx; x--;) {
			if (*pargb++ & 0xFF000000)
				return true;
		}
		pargb += cxDelta;
	}

	return false;
}

void ConvertBufferToPARGB32(HANDLE hPaintBuffer, HDC hdc, HICON hIcon, SIZE& sizIcon)
{
	RGBQUAD *prgbQuad;
	int cxRow;
	HRESULT hr = getBufferedPaintBits(hPaintBuffer, &prgbQuad, &cxRow);
	if (SUCCEEDED(hr)) {
		ARGB *pargb = (ARGB *)prgbQuad;
		if (!HasAlpha(pargb, sizIcon, cxRow)) {
			ICONINFO info;
			if (GetIconInfo(hIcon, &info)) {
				if (info.hbmMask)
					ConvertToPARGB32(hdc, pargb, info.hbmMask, sizIcon, cxRow);

				DeleteObject(info.hbmColor);
				DeleteObject(info.hbmMask);
			}
		}
	}
}

HBITMAP ConvertIconToBitmap(HICON hicon, HIMAGELIST hIml, int iconId)
{
	SIZE sizIcon;
	sizIcon.cx = GetSystemMetrics(SM_CXSMICON);
	sizIcon.cy = GetSystemMetrics(SM_CYSMICON);

	RECT rcIcon = { 0, 0, sizIcon.cx, sizIcon.cy };

	HDC hdc = CreateCompatibleDC(NULL);

	BITMAPINFO bmi;
	InitBitmapInfo(bmi, sizIcon);

	HBITMAP hbmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
	HBITMAP hbmpOld = (HBITMAP)SelectObject(hdc, hbmp);

	BLENDFUNCTION bfAlpha = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
	BP_PAINTPARAMS paintParams = {0};
	paintParams.cbSize = sizeof(paintParams);
	paintParams.dwFlags = BPPF_ERASE;
	paintParams.pBlendFunction = &bfAlpha;

	HDC hdcBuffer;
	HANDLE hPaintBuffer = beginBufferedPaint(hdc, &rcIcon, BPBF_DIB, &paintParams, &hdcBuffer);
	if (hPaintBuffer) {
		if (hIml)
			ImageList_Draw(hIml, iconId, hdc, 0, 0, ILD_TRANSPARENT);
		else
			DrawIconEx(hdcBuffer, 0, 0, hicon, sizIcon.cx, sizIcon.cy, 0, NULL, DI_NORMAL);

		// If icon did not have an alpha channel we need to convert buffer to PARGB
		ConvertBufferToPARGB32(hPaintBuffer, hdc, hicon, sizIcon);

		// This will write the buffer contents to the destination bitmap
		endBufferedPaint(hPaintBuffer, TRUE);
	}

	SelectObject(hdc, hbmpOld);
	DeleteDC(hdc);

	return hbmp;
}