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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
#include "stdafx.h"
#include "image_utils.h"
#define GET_PIXEL(__P__, __X__, __Y__) ( (__P__) + width * 4 * (__Y__) + 4 * (__X__))
// Make a bitmap all transparent, but only if it is a 32bpp
void MakeBmpTransparent(HBITMAP hBitmap)
{
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
if (bmp.bmBitsPixel != 32)
return;
DWORD dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
BYTE *p = (BYTE *)malloc(dwLen);
if (p == nullptr)
return;
memset(p, 0, dwLen);
SetBitmapBits(hBitmap, dwLen, p);
free(p);
}
/////////////////////////////////////////////////////////////////////////////////////////
HBITMAP CopyBitmapTo32(HBITMAP hBitmap)
{
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
DWORD dwLen = bmp.bmWidth * bmp.bmHeight * 4;
BYTE *p = (BYTE *)malloc(dwLen);
if (p == nullptr)
return nullptr;
// Create bitmap
BITMAPINFO RGB32BitsBITMAPINFO;
memset(&RGB32BitsBITMAPINFO, 0, 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;
BYTE *ptPixels;
HBITMAP hDirectBitmap = CreateDIBSection(nullptr, (BITMAPINFO *)&RGB32BitsBITMAPINFO, DIB_RGB_COLORS, (void **)&ptPixels, nullptr, 0);
// Copy data
if (bmp.bmBitsPixel != 32) {
HDC hdcOrig = CreateCompatibleDC(nullptr);
HBITMAP oldOrig = (HBITMAP)SelectObject(hdcOrig, hBitmap);
HDC hdcDest = CreateCompatibleDC(nullptr);
HBITMAP 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
FreeImage_CorrectBitmap32Alpha(hDirectBitmap, FALSE);
}
else {
GetBitmapBits(hBitmap, dwLen, p);
SetBitmapBits(hDirectBitmap, dwLen, p);
}
free(p);
return hDirectBitmap;
}
HBITMAP CreateBitmap32(int cx, int cy)
{
BITMAPINFO RGB32BitsBITMAPINFO;
memset(&RGB32BitsBITMAPINFO, 0, 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;
UINT *ptPixels;
return CreateDIBSection(nullptr, (BITMAPINFO *)&RGB32BitsBITMAPINFO, DIB_RGB_COLORS, (void **)&ptPixels, nullptr, 0);
}
// Set the color of points that are transparent
void SetTranspBkgColor(HBITMAP hBitmap, COLORREF color)
{
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
if (bmp.bmBitsPixel != 32)
return;
DWORD dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
BYTE *p = (BYTE *)malloc(dwLen);
if (p == nullptr)
return;
memset(p, 0, dwLen);
GetBitmapBits(hBitmap, dwLen, p);
bool changed = false;
for (int y = 0; y < bmp.bmHeight; ++y) {
BYTE *px = p + bmp.bmWidth * 4 * y;
for (int x = 0; x < bmp.bmWidth; ++x) {
if (px[3] == 0) {
px[0] = GetBValue(color);
px[1] = GetGValue(color);
px[2] = GetRValue(color);
changed = true;
}
px += 4;
}
}
if (changed)
SetBitmapBits(hBitmap, dwLen, p);
free(p);
}
#define HIMETRIC_INCH 2540 // HIMETRIC units per inch
void SetHIMETRICtoDP(HDC hdc, SIZE* sz)
{
POINT pt;
int nMapMode = GetMapMode(hdc);
if (nMapMode < MM_ISOTROPIC && nMapMode != MM_TEXT) {
// when using a constrained map mode, map against physical inch
SetMapMode(hdc, MM_HIMETRIC);
pt.x = sz->cx;
pt.y = sz->cy;
LPtoDP(hdc, &pt, 1);
sz->cx = pt.x;
sz->cy = pt.y;
SetMapMode(hdc, nMapMode);
}
else {
// map against logical inch for non-constrained mapping modes
int cxPerInch, cyPerInch;
cxPerInch = GetDeviceCaps(hdc, LOGPIXELSX);
cyPerInch = GetDeviceCaps(hdc, LOGPIXELSY);
sz->cx = MulDiv(sz->cx, cxPerInch, HIMETRIC_INCH);
sz->cy = MulDiv(sz->cy, cyPerInch, HIMETRIC_INCH);
}
pt.x = sz->cx;
pt.y = sz->cy;
DPtoLP(hdc, &pt, 1);
sz->cx = pt.x;
sz->cy = pt.y;
}
HBITMAP BmpFilterLoadBitmap(BOOL *bIsTransparent, const wchar_t *ptszFilename)
{
FIBITMAP *dib = (FIBITMAP*)Image_Load(ptszFilename, IMGL_RETURNDIB);
if (dib == nullptr)
return nullptr;
FIBITMAP *dib32 = nullptr;
if (FreeImage_GetBPP(dib) != 32) {
dib32 = FreeImage_ConvertTo32Bits(dib);
FreeImage_Unload(dib);
}
else dib32 = dib;
if (dib32 == nullptr)
return nullptr;
if (FreeImage_IsTransparent(dib32))
if (bIsTransparent)
*bIsTransparent = TRUE;
if (FreeImage_GetWidth(dib32) > 128 || FreeImage_GetHeight(dib32) > 128) {
FIBITMAP *dib_new = FreeImage_MakeThumbnail(dib32, 128, FALSE);
FreeImage_Unload(dib32);
if (dib_new == nullptr)
return nullptr;
dib32 = dib_new;
}
HBITMAP bitmap = FreeImage_CreateHBITMAPFromDIB(dib32);
FreeImage_Unload(dib32);
FreeImage_CorrectBitmap32Alpha(bitmap, FALSE);
return bitmap;
}
static HWND hwndClui = nullptr;
//
// Save ///////////////////////////////////////////////////////////////////////////////////////////
// PNG and BMP will be saved as 32bit images, jpg as 24bit with default quality (75)
// returns 1 on success, 0 on failure
int BmpFilterSaveBitmap(HBITMAP hBmp, const wchar_t *ptszFile, int flags)
{
wchar_t tszFilename[MAX_PATH];
if (!PathToAbsoluteW(ptszFile, tszFilename))
wcsncpy_s(tszFilename, ptszFile, _TRUNCATE);
if (mir_wstrlen(tszFilename) <= 4)
return -1;
IMGSRVC_INFO i = { 0 };
i.cbSize = sizeof(IMGSRVC_INFO);
i.pwszName = tszFilename;
i.hbm = hBmp;
i.dwMask = IMGI_HBITMAP;
i.fif = FIF_UNKNOWN;
return !Image_Save(&i, flags);
}
// Other utilities ////////////////////////////////////////////////////////////////////////////////
static BOOL ColorsAreTheSame(int colorDiff, BYTE *px1, BYTE *px2)
{
return abs(px1[0] - px2[0]) <= colorDiff
&& abs(px1[1] - px2[1]) <= colorDiff
&& abs(px1[2] - px2[2]) <= colorDiff;
}
void AddToStack(int *stack, int *topPos, int x, int y)
{
// Already is in stack?
for (int i = 0; i < *topPos; i += 2)
if (stack[i] == x && stack[i + 1] == y)
return;
stack[*topPos] = x;
(*topPos)++;
stack[*topPos] = y;
(*topPos)++;
}
BOOL GetColorForPoint(int colorDiff, BYTE *p, int width,
int x0, int y0, int x1, int y1, int x2, int y2, BOOL *foundBkg, BYTE colors[][3])
{
BYTE *px1 = GET_PIXEL(p, x0, y0);
BYTE *px2 = GET_PIXEL(p, x1, y1);
BYTE *px3 = GET_PIXEL(p, x2, y2);
// If any of the corners have transparency, forget about it
// Not using != 255 because some MSN bmps have 254 in some positions
if (px1[3] < 253 || px2[3] < 253 || px3[3] < 253)
return FALSE;
// See if is the same color
if (ColorsAreTheSame(colorDiff, px1, px2) && ColorsAreTheSame(colorDiff, px3, px2)) {
*foundBkg = TRUE;
memmove(colors, px1, 3);
}
else *foundBkg = FALSE;
return TRUE;
}
DWORD GetImgHash(HBITMAP hBitmap)
{
BITMAP bmp;
GetObject(hBitmap, sizeof(bmp), &bmp);
DWORD dwLen = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8);
WORD *p = (WORD *)malloc(dwLen);
if (p == nullptr)
return 0;
memset(p, 0, dwLen);
GetBitmapBits(hBitmap, dwLen, p);
DWORD ret = 0;
for (DWORD i = 0; i < dwLen / 2; i++)
ret += p[i];
free(p);
return ret;
}
/*
* Changes the handle to a grayscale image
*/
HBITMAP MakeGrayscale(HBITMAP hBitmap)
{
if (hBitmap) {
FIBITMAP *dib = FreeImage_CreateDIBFromHBITMAP(hBitmap);
if (dib) {
FIBITMAP *dib_new = FreeImage_ConvertToGreyscale(dib);
FreeImage_Unload(dib);
if (dib_new) {
DeleteObject(hBitmap);
HBITMAP hbm_new = FreeImage_CreateHBITMAPFromDIB(dib_new);
FreeImage_Unload(dib_new);
return hbm_new;
}
}
}
return hBitmap;
}
/*
* See if finds a transparent background in image, and set its transparency
* Return TRUE if found a transparent background
*/
BOOL MakeTransparentBkg(MCONTACT hContact, HBITMAP *hBitmap)
{
int i, j;
BITMAP bmp;
GetObject(*hBitmap, sizeof(bmp), &bmp);
int width = bmp.bmWidth;
int height = bmp.bmHeight;
int colorDiff = db_get_w(hContact, "ContactPhoto", "TranspBkgColorDiff", db_get_w(0, AVS_MODULE, "TranspBkgColorDiff", 10));
// Min 5x5 to easy things in loop
if (width <= 4 || height <= 4)
return FALSE;
DWORD dwLen = width * height * 4;
BYTE *p = (BYTE *)malloc(dwLen);
if (p == nullptr)
return FALSE;
HBITMAP hBmpTmp;
if (bmp.bmBitsPixel == 32)
hBmpTmp = *hBitmap;
else // Convert to 32 bpp
hBmpTmp = CopyBitmapTo32(*hBitmap);
GetBitmapBits(hBmpTmp, dwLen, p);
// **** Get corner colors
// Top left
BYTE colors[8][3];
BOOL foundBkg[8];
if (!GetColorForPoint(colorDiff, p, width, 0, 0, 0, 1, 1, 0, &foundBkg[0], &colors[0])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Top center
if (!GetColorForPoint(colorDiff, p, width, width / 2, 0, width / 2 - 1, 0, width / 2 + 1, 0, &foundBkg[1], &colors[1])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Top Right
if (!GetColorForPoint(colorDiff, p, width,
width - 1, 0, width - 1, 1, width - 2, 0, &foundBkg[2], &colors[2])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Center left
if (!GetColorForPoint(colorDiff, p, width, 0, height / 2, 0, height / 2 - 1, 0, height / 2 + 1, &foundBkg[3], &colors[3])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Center left
if (!GetColorForPoint(colorDiff, p, width, width - 1, height / 2, width - 1, height / 2 - 1, width - 1, height / 2 + 1, &foundBkg[4], &colors[4])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Bottom left
if (!GetColorForPoint(colorDiff, p, width, 0, height - 1, 0, height - 2, 1, height - 1, &foundBkg[5], &colors[5])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Bottom center
if (!GetColorForPoint(colorDiff, p, width, width / 2, height - 1, width / 2 - 1, height - 1, width / 2 + 1, height - 1, &foundBkg[6], &colors[6])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Bottom Right
if (!GetColorForPoint(colorDiff, p, width, width - 1, height - 1, width - 1, height - 2, width - 2, height - 1, &foundBkg[7], &colors[7])) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// **** X corners have to have the same color
int count = 0;
for (i = 0; i < 8; i++)
if (foundBkg[i])
count++;
if (count < db_get_w(hContact, "ContactPhoto", "TranspBkgNumPoints", db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5))) {
if (hBmpTmp != *hBitmap)
DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Ok, X corners at least have a color, lets compare then
int maxCount = 0, selectedColor = 0;
for (i = 0; i < 8; i++) {
if (foundBkg[i]) {
count = 0;
for (j = 0; j < 8; j++) {
if (foundBkg[j] && ColorsAreTheSame(colorDiff, (BYTE *)&colors[i], (BYTE *)&colors[j]))
count++;
}
if (count > maxCount) {
maxCount = count;
selectedColor = i;
}
}
}
if (maxCount < db_get_w(hContact, "ContactPhoto", "TranspBkgNumPoints",
db_get_w(0, AVS_MODULE, "TranspBkgNumPoints", 5))) {
// Not enought corners with the same color
if (hBmpTmp != *hBitmap) DeleteObject(hBmpTmp);
free(p);
return FALSE;
}
// Get bkg color as mean of colors
{
int bkgColor[3];
bkgColor[0] = 0;
bkgColor[1] = 0;
bkgColor[2] = 0;
for (i = 0; i < 8; i++) {
if (foundBkg[i] && ColorsAreTheSame(colorDiff, (BYTE *)&colors[i], (BYTE *)&colors[selectedColor])) {
bkgColor[0] += colors[i][0];
bkgColor[1] += colors[i][1];
bkgColor[2] += colors[i][2];
}
}
bkgColor[0] /= maxCount;
bkgColor[1] /= maxCount;
bkgColor[2] /= maxCount;
colors[selectedColor][0] = bkgColor[0];
colors[selectedColor][1] = bkgColor[1];
colors[selectedColor][2] = bkgColor[2];
}
// **** Set alpha for the background color, from the borders
if (hBmpTmp != *hBitmap) {
DeleteObject(*hBitmap);
*hBitmap = hBmpTmp;
GetObject(*hBitmap, sizeof(bmp), &bmp);
GetBitmapBits(*hBitmap, dwLen, p);
}
// Set alpha from borders
bool transpProportional = (db_get_b(NULL, AVS_MODULE, "MakeTransparencyProportionalToColorDiff", 0) != 0);
int *stack = (int *)malloc(width * height * 2 * sizeof(int));
if (stack == nullptr) {
free(p);
return FALSE;
}
// Put four corners
int topPos = 0;
AddToStack(stack, &topPos, 0, 0);
AddToStack(stack, &topPos, width / 2, 0);
AddToStack(stack, &topPos, width - 1, 0);
AddToStack(stack, &topPos, 0, height / 2);
AddToStack(stack, &topPos, width - 1, height / 2);
AddToStack(stack, &topPos, 0, height - 1);
AddToStack(stack, &topPos, width / 2, height - 1);
AddToStack(stack, &topPos, width - 1, height - 1);
int curPos = 0;
while (curPos < topPos) {
// Get pos
int x = stack[curPos]; curPos++;
int y = stack[curPos]; curPos++;
// Get pixel
BYTE *px1 = GET_PIXEL(p, x, y);
// It won't change the transparency if one exists
// (This avoid an endless loop too)
// Not using == 255 because some MSN bmps have 254 in some positions
if (px1[3] >= 253) {
if (ColorsAreTheSame(colorDiff, px1, (BYTE *)&colors[selectedColor])) {
px1[3] = (transpProportional) ? min(252,
(abs(px1[0] - colors[selectedColor][0])
+ abs(px1[1] - colors[selectedColor][1])
+ abs(px1[2] - colors[selectedColor][2])) / 3) : 0;
// Add 4 neighbours
if (x + 1 < width)
AddToStack(stack, &topPos, x + 1, y);
if (x - 1 >= 0)
AddToStack(stack, &topPos, x - 1, y);
if (y + 1 < height)
AddToStack(stack, &topPos, x, y + 1);
if (y - 1 >= 0)
AddToStack(stack, &topPos, x, y - 1);
}
}
}
free(stack);
SetBitmapBits(*hBitmap, dwLen, p);
free(p);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////////////////
// Other utils
int SaveAvatar(const char *protocol, const wchar_t *tszFileName)
{
INT_PTR result = CallProtoService(protocol, PS_SETMYAVATAR, 0, (LPARAM)tszFileName);
if (result == CALLSERVICE_NOTFOUND)
result = CallProtoService(protocol, PS_SETMYAVATAR, 0, _T2A(tszFileName));
return result;
}
|