-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgdi.c
478 lines (396 loc) · 12.1 KB
/
gdi.c
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
//? Marius Negrutiu (marius.negrutiu@protonmail.com) :: 2013/07/06
#include "main.h"
#define COBJMACROS
#include <olectl.h>
#include <ocidl.h>
int _fltused = 0; /// For some reason AlphaBlend is using this. The module won't link without it.
#define ALIGN_LEFT 1
#define ALIGN_TOP 1
#define ALIGN_CENTER 2
#define ALIGN_RIGHT 3
#define ALIGN_BOTTOM 3
//++ ColorsAlmostIdentical
BOOLEAN ColorsAlmostIdentical( __in COLORREF cl1, __in COLORREF cl2 )
{
#define ABS(x) ((x) >= 0 ? (x) : -(x))
const int iTolerance = 5;
return (
ABS((int)GetRValue(cl1) - (int)GetRValue(cl2)) <= iTolerance &&
ABS((int)GetGValue(cl1) - (int)GetGValue(cl2)) <= iTolerance &&
ABS((int)GetBValue(cl1) - (int)GetBValue(cl2)) <= iTolerance
);
}
//++ PickFillColor
COLORREF PickFillColor(
__in HDC hDC,
__in int x, __in int y,
__in int w, __in int h
)
{
const int xCenter = x + w / 2;
const int yCenter = y + h / 2;
const int xRight = x + w - 1;
const int yBottom = y + h - 1;
POINT SamplePts[9] = {
{ x, y }, /// left, top
{ xCenter, y }, /// center, top
{ xRight, y }, /// right, top
{ xRight, yCenter }, /// right, center
{ xRight, yBottom }, /// right, bottom
{ xCenter, yBottom }, /// center, bottom
{ x, yBottom }, /// left, bottom
{ x, yCenter }, /// left, center
{ xCenter, yCenter } /// center, center
};
struct { COLORREF cl; int hits; } SampleColors[9];
int i, j, iMaxHits;
COLORREF cl, clMaxHits;
// Pick some sample points from the image
// We'll identify the color with most hits
iMaxHits = 0;
clMaxHits = 0;
for ( i = 0; i < 9; i++ )
{
/// Clear this slot first
SampleColors[i].cl = 0;
SampleColors[i].hits = 0;
cl = GetPixel( hDC, SamplePts[i].x, SamplePts[i].y );
for ( j = 0; j < 9; j++ )
{
if ( SampleColors[j].hits == 0 ) {
/// Uninitialized slot
SampleColors[j].cl = cl;
SampleColors[j].hits = 1;
if ( iMaxHits < 1 ) {
iMaxHits = 1;
clMaxHits = cl;
}
break;
} else {
if ( ColorsAlmostIdentical( cl, SampleColors[j].cl )) {
SampleColors[j].hits++;
if ( iMaxHits < SampleColors[j].hits ) {
if ( SampleColors[j].hits == 5 ) {
return cl; /// Enough hits to stop the search...
}
iMaxHits = SampleColors[j].hits;
clMaxHits = cl;
}
break;
}
}
}
}
return clMaxHits;
}
//++ PrepareBitmapForAlphaBlend
BOOL PrepareBitmapForAlphaBlend( __inout HBITMAP hBmp )
{
BOOL bHasAlphaChannel = FALSE;
if ( hBmp ) {
BITMAP bm;
if ( GetObject( hBmp, sizeof( bm ), &bm ) > 0 ) {
// Make sure this is a 32bpp bitmap
// Otherwise, there's no alpha channel anyway...
if ( bm.bmBitsPixel == 32 ) {
int x, y;
LPRGBQUAD pBits;
// Make a quick pass and check whether the alpha channel is used at all.
pBits = (LPRGBQUAD)bm.bmBits;
for ( y = 0; y < bm.bmHeight && !bHasAlphaChannel; y++ ) {
for ( x = 0; x < bm.bmWidth; x++ ) {
if ( pBits->rgbReserved != 0 ) {
bHasAlphaChannel = TRUE;
break;
}
pBits++;
}
}
// Get direct access to bitmap's bits
pBits = (LPRGBQUAD)bm.bmBits;
if (bHasAlphaChannel) {
// Pre-multiply the color channels the way AlphBlend is expecting
for (y = 0; y < bm.bmHeight; y++) {
for (x = 0; x < bm.bmWidth; x++) {
pBits->rgbRed = (BYTE)(((LONG)pBits->rgbRed * pBits->rgbReserved) >> 8);
pBits->rgbGreen = (BYTE)(((LONG)pBits->rgbGreen * pBits->rgbReserved) >> 8);
pBits->rgbBlue = (BYTE)(((LONG)pBits->rgbBlue * pBits->rgbReserved) >> 8);
pBits++;
}
}
} else {
// Simply set the alpha channel to 255 (full opacity)
for (y = 0; y < bm.bmHeight; y++) {
for (x = 0; x < bm.bmWidth; x++) {
pBits->rgbReserved = 255;
pBits++;
}
}
}
} else {
//assert(!"not a 32bpp image sent to PrepareBitmapForAlphaBlend");
}
}
}
return bHasAlphaChannel;
}
//++ ResampleBitmap
DWORD ResampleBitmap(
__in HBITMAP hBitmap,
__in int iWidth,
__in int iHeight,
__in int iAlignHorz,
__in int iAlignVert,
__out HBITMAP *phBitmap
)
{
DWORD err = ERROR_SUCCESS;
if ( hBitmap && phBitmap && (iWidth > 0) && (iHeight > 0)) {
typedef BOOL( WINAPI *TfnAlphaBlend )(
_In_ HDC hdcDest, _In_ int xoriginDest, _In_ int yoriginDest, _In_ int wDest, _In_ int hDest,
_In_ HDC hdcSrc, _In_ int xoriginSrc, _In_ int yoriginSrc, _In_ int wSrc, _In_ int hSrc,
_In_ BLENDFUNCTION ftn
);
int iSize;
BITMAPINFO *pBmi;
HMODULE hImg32;
TfnAlphaBlend fnAlphaBlend = NULL;
TCHAR szPath[MAX_PATH];
// msimg32!AlphaBlend
// NOTE: msimg32.dll is not available in NT4
GetSystemDirectory( szPath, ARRAYSIZE( szPath ) );
lstrcat( szPath, _T( "\\msimg32.dll" ) );
hImg32 = LoadLibrary( szPath );
if (hImg32)
fnAlphaBlend = (TfnAlphaBlend)GetProcAddress( hImg32, "AlphaBlend" );
// Create a device independent bitmap
iSize = sizeof(BITMAPINFOHEADER) + 32 * iWidth * iHeight;
pBmi = (BITMAPINFO*)GlobalAlloc( GPTR, iSize );
if (pBmi) {
pBmi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
pBmi->bmiHeader.biWidth = iWidth;
pBmi->bmiHeader.biHeight = iHeight;
pBmi->bmiHeader.biPlanes = 1;
pBmi->bmiHeader.biBitCount = 32;
*phBitmap = CreateDIBSection( NULL, pBmi, DIB_RGB_COLORS, NULL, NULL, 0 );
GlobalFree( pBmi );
if ( *phBitmap ) {
int x, y;
int iSrcWidth, iSrcHeight;
BITMAP bm;
GetObject( hBitmap, sizeof(bm), &bm );
iSrcWidth = bm.bmWidth;
iSrcHeight = bm.bmHeight;
/// Horizontal alignment
switch ( iAlignHorz )
{
case ALIGN_LEFT: x = 0; break;
case ALIGN_RIGHT: x = iWidth - iSrcWidth; break;
default: x = ( iWidth - iSrcWidth ) / 2;
}
/// Vertical alignment
switch ( iAlignVert )
{
case ALIGN_TOP: y = 0; break;
case ALIGN_BOTTOM: y = iHeight - iSrcHeight; break;
default: y = ( iHeight - iSrcHeight ) / 2;
}
// Draw
if ( TRUE )
{
HDC hSrcDC = CreateCompatibleDC( NULL );
HDC hDstDC = CreateCompatibleDC( NULL );
HBITMAP hOldSrcBitmap = (HBITMAP)SelectObject( hSrcDC, hBitmap );
HBITMAP hOldDstBitmap = (HBITMAP)SelectObject( hDstDC, *phBitmap );
BOOL bHasAlphaChannel = FALSE;
bHasAlphaChannel = PrepareBitmapForAlphaBlend( hBitmap );
if (bHasAlphaChannel && fnAlphaBlend) {
BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
RECT rc = { 0, 0, iWidth, iHeight };
// White-fill the destination, and paint the (transparent) bitmap over it
FillRect( hDstDC, &rc, (HBRUSH)GetStockObject( WHITE_BRUSH ));
fnAlphaBlend( hDstDC, x, y, iSrcWidth, iSrcHeight, hSrcDC, 0, 0, iSrcWidth, iSrcHeight, bf );
} else {
// Draw the (opaque) bitmap, then fill the surrounding areas
BitBlt( hDstDC, x, y, iSrcWidth, iSrcHeight, hSrcDC, 0, 0, SRCCOPY );
if ( iWidth > iSrcWidth || iHeight > iSrcHeight )
{
COLORREF cl = PickFillColor( hSrcDC, 0, 0, iSrcWidth, iSrcHeight );
HBRUSH br = CreateSolidBrush( cl );
RECT rc;
if ( iWidth > iSrcWidth ) {
SetRect( &rc, 0, 0, x, iHeight ); /// Left side of the image
FillRect( hDstDC, &rc, br );
SetRect( &rc, x + iSrcWidth, 0, iWidth, iHeight ); /// Right side of the image
FillRect( hDstDC, &rc, br );
}
if ( iHeight > iSrcHeight ) {
SetRect( &rc, x, 0, x + iSrcWidth, y ); /// Top side of the image
FillRect( hDstDC, &rc, br );
SetRect( &rc, x, y + iSrcHeight, x + iSrcWidth, iHeight ); /// Bottom side of the image
FillRect( hDstDC, &rc, br );
}
DeleteObject( br );
}
}
/// Border
if ( TRUE ) {
HBRUSH br = CreateSolidBrush( RGB( 211, 212, 214 ));
RECT rc = { 0, 0, iWidth, iHeight };
FrameRect( hDstDC, &rc, br );
DeleteObject( br );
}
/// If the source image has no alpha channel -> force full opacity on destination image
if (!bHasAlphaChannel)
PrepareBitmapForAlphaBlend( *phBitmap );
SelectObject( hSrcDC, hOldSrcBitmap );
SelectObject( hDstDC, hOldDstBitmap );
DeleteDC( hSrcDC );
DeleteDC( hDstDC );
}
} else {
err = GetLastError(); /// CreateDIBSection
}
} else {
err = GetLastError(); /// GlobalAlloc
}
if (hImg32)
FreeLibrary( hImg32 );
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ LoadImageFileImpl
DWORD LoadImageFileImpl(
__in LPCTSTR pszFilePath,
__in int iWidth,
__in int iHeight,
__in int iAlignHorz,
__in int iAlignVert,
__out HBITMAP *phBitmap
)
{
DWORD err = ERROR_SUCCESS;
if ( pszFilePath && *pszFilePath && (iWidth > 0) && (iHeight > 0) && phBitmap ) {
// OLE initialize
HRESULT hrOleInit = OleInitialize( NULL );
// Load the image in memory
IPicture *pIPicture = NULL;
#if defined _UNICODE
LPCWSTR pszW = pszFilePath;
#else
WCHAR pszW[512];
pszW[0] = UNICODE_NULL;
MultiByteToWideChar( CP_ACP, 0, pszFilePath, -1, pszW, ARRAYSIZE(pszW));
#endif
err = OleLoadPicturePath((LPOLESTR)pszW, NULL, 0, 0, &IID_IPicture, (LPVOID)&pIPicture );
if ( SUCCEEDED( err )) {
// Make sure it's stored in memory as HBITMAP (as opposed to HICON, HMETAFILE, HENHMETAFILE, etc.)
SHORT iPicType;
err = IPicture_get_Type( pIPicture, &iPicType );
if ( SUCCEEDED( err )) {
if ( iPicType == PICTYPE_BITMAP ) {
// Get the HBITMAP
OLE_HANDLE hIPictureHandle;
err = (DWORD)IPicture_get_Handle( pIPicture, &hIPictureHandle );
if ( SUCCEEDED( err )) {
// Resize
err = ResampleBitmap( ULongToHandle( hIPictureHandle ), iWidth, iHeight, iAlignHorz, iAlignVert, phBitmap );
}
} else {
err = ERROR_UNSUPPORTED_TYPE;
}
}
IPicture_Release( pIPicture );
}
if ( SUCCEEDED( hrOleInit ))
OleUninitialize();
} else {
err = ERROR_INVALID_PARAMETER;
}
return err;
}
//++ [exported] LoadImageFile
// ----------------------------------------------------------------------
//+ Example:
// NSutils::LoadImageFile "$PLUGINSDIR\Image.jpg" 640 480 center center
// Pop $0
// ${If} $0 <> 0
// ;Success: $0 contains a valid HBITMAP
// ; ...
// ; Destroy the bitmap after no longer needed
// System::Call 'gdi32::DeleteObject( i $0 )'
// ${Else}
// ;Error
// ${EndIf}
void __declspec(dllexport) LoadImageFile(
HWND parent,
int string_size,
TCHAR *variables,
stack_t **stacktop,
extra_parameters *extra
)
{
LPTSTR pszBuf = NULL;
// Cache global structures
EXDLL_INIT();
EXDLL_VALIDATE();
// Retrieve NSIS parameters
/// Allocate memory large enough to store an NSIS string
pszBuf = (TCHAR*)GlobalAlloc( GPTR, sizeof(TCHAR) * string_size );
if ( pszBuf ) {
// TODO:
// If Width and Height are zero, load the image using its original dimensions
// Add "stretch" to horizontal and vertical alignments
// Support transparent images
TCHAR szPath[MAX_PATH];
int w, h;
int iAlignH = ALIGN_CENTER, iAlignV = ALIGN_CENTER;
/// Param1: Image file path or URL
*szPath = _T('\0');
if ( popstring( pszBuf ) == 0 ) {
lstrcpyn( szPath, pszBuf, ARRAYSIZE( szPath ));
}
/// Param2: Image width
w = popint();
/// Param3: Image height
h = popint();
/// Param4: Horizontal alignment (can be "left", "right" or "center")
if ( popstring( pszBuf ) == 0 ) {
if ( lstrcmpi( pszBuf, _T("left")) == 0 ) {
iAlignH = ALIGN_LEFT;
} else if ( lstrcmpi( pszBuf, _T("right")) == 0 ) {
iAlignH = ALIGN_RIGHT;
} else {
iAlignH = ALIGN_CENTER;
}
}
/// Param5: Vertical alignment (can be "top", "bottom" or "center")
if ( popstring( pszBuf ) == 0 ) {
if ( lstrcmpi( pszBuf, _T("top")) == 0 ) {
iAlignV = ALIGN_TOP;
} else if ( lstrcmpi( pszBuf, _T("bottom")) == 0 ) {
iAlignV = ALIGN_BOTTOM;
} else {
iAlignV = ALIGN_CENTER;
}
}
// Validate input
if ( *szPath && (w > 0) && (h > 0)) {
HBITMAP hBmp = NULL;
DWORD err = LoadImageFileImpl( szPath, w, h, iAlignH, iAlignV, &hBmp );
if ( SUCCEEDED( err )) {
// Return HBITMAP on the stack
// It must be destroyed by the caller (gdi32!DeleteObject) when no longer needed
pushintptr((INT_PTR)hBmp );
} else {
pushint( 0 ); /// Load error
}
} else {
pushint( 0 ); /// Invalid parameter
}
/// Free memory
GlobalFree( pszBuf );
}
}