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
|
#include "./setupImage.h"
#include "../api__ml_online.h"
#include <shlwapi.h>
static BOOL SetupImage_CopyImage(HDC hdc, HBITMAP bitmapDst, INT x, INT y, INT cx, INT cy, HBITMAP bitmapSrc, INT srcX, INT srcY)
{
BOOL resultOk = FALSE;
HDC hdcDst = CreateCompatibleDC(hdc);
HDC hdcSrc = CreateCompatibleDC(hdc);
if (NULL != hdcDst && NULL != hdcSrc)
{
HBITMAP bitmapDstOrig = (HBITMAP)SelectObject(hdcDst, bitmapDst);
HBITMAP bitmapSrcOrig = (HBITMAP)SelectObject(hdcSrc, bitmapSrc);
resultOk = BitBlt(hdcDst, x, y, cx, cy, hdcSrc, srcX, srcY, SRCCOPY);
SelectObject(hdcDst, bitmapDstOrig);
SelectObject(hdcSrc, bitmapSrcOrig);
}
if (NULL != hdcDst) DeleteDC(hdcDst);
if (NULL != hdcSrc) DeleteDC(hdcSrc);
return resultOk;
}
static BOOL SetupImage_ColorizeImage(BYTE *pPixels, LONG x, LONG y, LONG cx, LONG cy, WORD bpp, LONG dstX, LONG dstY, COLORREF rgbBk, COLORREF rgbFg, BOOL removeAlpha)
{
LONG pitch;
INT step;
BYTE rFg, gFg, bFg;
LPBYTE srcCursor, srcLine;
LPBYTE dstLine;
if (bpp < 24) return FALSE;
step = (bpp>>3);
pitch = cx*step;
while (pitch%4) pitch++;
rFg = GetRValue(rgbFg); gFg = GetGValue(rgbFg); bFg = GetBValue(rgbFg);
INT bK = (bFg - GetBValue(rgbBk));
INT gK = (gFg - GetGValue(rgbBk));
INT rK = (rFg - GetRValue(rgbBk));
srcLine = pPixels + pitch * y + x*step;
dstLine = pPixels + pitch * dstY + dstX*step;
if (24 == bpp)
{
for (; cy-- != 0; srcLine += pitch, dstLine += pitch )
{
LONG i;
LPBYTE dstCursor;
for (i = cx, srcCursor = srcLine, dstCursor = dstLine ; i-- != 0; srcCursor += 3, dstCursor +=3)
{
dstCursor[0] = bFg - (bK*(255 - srcCursor[0])>>8);
dstCursor[1] = gFg - (gK*(255 - srcCursor[1])>>8);
dstCursor[2] = rFg - (rK*(255 - srcCursor[2])>>8);
}
}
}
else
{
// nothing for now
return FALSE;
}
return TRUE;
}
SetupImage::SetupImage(HDC hdc, HBITMAP bitmapSource, INT maxColors)
: ref(1), bitmap(NULL), pixels(NULL),
table(NULL), tableSize(0), tableCount(0), insertCursor(0), readCursor(0)
{
ZeroMemory(&header, sizeof(BITMAPINFOHEADER));
BITMAP bm;
if (sizeof(BITMAP) != GetObject(bitmapSource, sizeof(BITMAP), &bm))
return;
if (bm.bmHeight < 0)
bm.bmHeight = -bm.bmHeight;
header.biSize = sizeof(BITMAPINFOHEADER);
header.biCompression = BI_RGB;
header.biBitCount = 24;
header.biPlanes = 1;
header.biWidth = bm.bmWidth;
header.biHeight = -(bm.bmHeight * (maxColors + 1));
bitmap = CreateDIBSection(hdc, (LPBITMAPINFO)&header, DIB_RGB_COLORS, (void**)&pixels, NULL, 0);
if (NULL == bitmap)
return;
if (FALSE == SetupImage_CopyImage(hdc, bitmap, 0, 0, bm.bmWidth, bm.bmHeight, bitmapSource, 0, 0))
{
DeleteObject(bitmap);
bitmap = NULL;
return;
}
tableSize = maxColors;
table = (IMAGEINDEX*)calloc(tableSize, sizeof(IMAGEINDEX));
if (NULL == table)
{
DeleteObject(bitmap);
bitmap = NULL;
return;
}
}
SetupImage::~SetupImage()
{
if (NULL != bitmap)
{
DeleteObject(bitmap);
bitmap = NULL;
}
if (NULL != table)
{
free(table);
table = NULL;
}
}
SetupImage *SetupImage::CreateInstance(HDC hdc, HBITMAP bitmapSource, INT maxColors)
{
if (NULL == bitmapSource || maxColors < 1 || maxColors > 120)
return NULL;
SetupImage *instance = new SetupImage(hdc, bitmapSource, maxColors);
if (NULL == instance) return NULL;
if (NULL == instance->bitmap || NULL == instance->table)
{
instance->Release();
instance = NULL;
}
return instance;
}
SetupImage *SetupImage::CreateFromPluginBitmap(HDC hdc, LPCWSTR pszModuleName, LPCWSTR resourceName, INT maxColors)
{
SetupImage *instance = NULL;
WCHAR szPath[MAX_PATH] = {0};
if (0 != GetModuleFileName(WASABI_API_ORIG_HINST, szPath, ARRAYSIZE(szPath)))
{
PathRemoveFileSpec(szPath);
PathAppend(szPath, pszModuleName);
HMODULE hModule = LoadLibraryEx(szPath, NULL, LOAD_LIBRARY_AS_DATAFILE | 0x00000020/*LOAD_LIBRARY_AS_IMAGE_RESOURCE*/);
if (NULL != hModule)
{
HBITMAP bitmapSource = (HBITMAP)LoadImage(hModule, resourceName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
if (NULL != bitmapSource)
instance = CreateInstance(hdc, bitmapSource, maxColors);
FreeLibrary(hModule);
}
}
return instance;
}
ULONG SetupImage::AddRef()
{
return InterlockedIncrement((LONG*)&ref);
}
ULONG SetupImage::Release()
{
if (0 == ref)
return ref;
LONG r = InterlockedDecrement((LONG*)&ref);
if (0 == r)
delete(this);
return r;
}
BOOL SetupImage::GetSize(SIZE *pSize)
{
if (NULL == pSize) return FALSE;
INT cy = header.biHeight;
if (cy < 0) cy = -cy;
pSize->cx = header.biWidth;
pSize->cy = cy / (tableSize + 1);
return TRUE;
}
BOOL SetupImage::DrawImage(HDC hdc, INT x, INT y, INT cx, INT cy, INT srcX, INT srcY, COLORREF rgbBk, COLORREF rgbFg)
{
BYTE bitmapIndex = 0xFF;
SIZE imageSize;
if (!GetSize(&imageSize))
return FALSE;
for(BYTE i = readCursor; i < tableCount; i++)
{
if (table[i].rgbBk == rgbBk && table[i].rgbFg == rgbFg)
{
bitmapIndex = i;
break;
}
}
if (0xFF == bitmapIndex)
{
if (readCursor > tableCount)
readCursor = tableCount;
for(BYTE i = 0; i < readCursor; i++)
{
if (table[i].rgbBk == rgbBk && table[i].rgbFg == rgbFg)
{
bitmapIndex = i;
break;
}
}
if (0xFF == bitmapIndex)
{
if (tableCount < tableSize)
{
insertCursor = tableCount;
tableCount++;
}
else if (++insertCursor == tableCount)
insertCursor = 0;
INT targetY = (insertCursor + 1) * imageSize.cy;
if (!SetupImage_ColorizeImage(pixels, 0, 0, imageSize.cx, imageSize.cy,
header.biBitCount, 0, targetY, rgbBk, rgbFg, TRUE))
{
return FALSE;
}
table[insertCursor].rgbBk = rgbBk;
table[insertCursor].rgbFg = rgbFg;
bitmapIndex = insertCursor;
}
}
readCursor = bitmapIndex;
srcY += ((bitmapIndex + 1) * imageSize.cy);
INT dstY = y;
INT dstCY = cy;
INT imageHeight = header.biHeight;
if (imageHeight < 0)
{
header.biHeight = -imageHeight;
dstY += (cy - 1);
dstCY = -cy;
}
BOOL resultOk = StretchDIBits(hdc, x, dstY, cx, dstCY, srcX, srcY, cx, cy,
pixels, (BITMAPINFO*)&header, DIB_RGB_COLORS, SRCCOPY);
if (imageHeight < 0)
header.biHeight = imageHeight;
return resultOk;
}
BOOL SetupImage::ResetCache()
{
if (NULL != table)
ZeroMemory(&table, sizeof(IMAGEINDEX) * tableSize);
tableCount = 0;
insertCursor = 0;
readCursor = 0;
return TRUE;
}
|