forked from cyd01/KiTTY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitty_image.c
1503 lines (1272 loc) · 46.3 KB
/
kitty_image.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef FLJ
#undef MOD_BACKGROUNDIMAGE
#endif
#include <setjmp.h>
#include "jpeg/jpeglib.h"
#include <stdio.h>
#include <windows.h>
#ifdef MOD_BACKGROUNDIMAGE
#include <math.h>
#include "putty.h"
#include "terminal.h"
#include "kitty_image.h"
extern Conf *conf ;// extern Config cfg;
//extern int offset_width, offset_height ;
//extern int font_width, font_height ;
#ifndef NCFGCOLOURS
#define NCFGCOLOURS 24
#endif
#ifndef NEXTCOLOURS
#define NEXTCOLOURS 240
#endif
#ifndef NALLCOLOURS
#define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
#endif
//extern COLORREF colours[NALLCOLOURS] ;
extern HWND MainHwnd ;
int stricmp(const char *s1, const char *s2) ;
int GetSessionField( const char * session_in, const char * folder_in, const char * field, char * result ) ;
int get_param( const char * val ) ;
int return_offset_height(void) ;
int return_offset_width(void) ;
int return_font_height(void) ;
int return_font_width(void) ;
COLORREF return_colours258(void) ;
static BOOL (WINAPI * pAlphaBlend)( HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION ) = 0 ;
//static HWND hwnd;
HDC textdc = NULL ;
HBITMAP textbm = NULL ;
COLORREF colorinpixel;
HDC colorinpixeldc = NULL ;
HBITMAP colorinpixelbm = NULL;
HDC backgrounddc = NULL ;
HBITMAP backgroundbm = NULL ;
HDC backgroundblenddc = NULL ;
HBITMAP backgroundblendbm = NULL;
BOOL bBgRelToTerm;
bool resizing;
RECT size_before;
#ifdef DLL
#define TARGET extern __declspec(dllexport)
#else
#define TARGET
#endif
static int ShrinkBitmapEnable = 1 ;
void SetShrinkBitmapEnable( int v ) {
if( v ) ShrinkBitmapEnable = 1 ;
else ShrinkBitmapEnable = 0 ;
}
//
// Fonctions de shrink de bitmap
//
#define Alloc(p,t) (t *)malloc((p)*sizeof(t))
#define For(i,n) for ((i)=0;(i)<(n);(i)++)
#define iFor(n) For (i,n)
#define jFor(n) For (j,n)
typedef struct {
WORD x, y ; // dimensions
WORD l ; // bytes per scan-line (32-bit allignment)
BYTE *b ; // bits of bitmap,3 bytes/pixel, BGR
} tWorkBMP ; // 24-bit working bitmap
void CreateWorkingBitmap( WORD dx, WORD dy, tWorkBMP *w ) {
w->x=dx ;
w->y=dy ;
w->l=(dx+1)*3&0xfffc ;
w->b=Alloc( w->l*dy, BYTE ) ;
}
HBITMAP CreateEmptyBitmap( WORD dx, WORD dy ) {
HDC h = GetDC( NULL ) ;
HBITMAP b = CreateCompatibleBitmap( h, dx, dy ) ;
ReleaseDC( NULL, h ) ;
return(b) ;
}
void SetBMIHeader( BITMAPINFO *b, short dx, short dy ) {
b->bmiHeader.biSize = sizeof(BITMAPINFOHEADER) ;
b->bmiHeader.biWidth = dx ;
b->bmiHeader.biHeight = -dy ;
b->bmiHeader.biPlanes = 1 ;
b->bmiHeader.biBitCount = 24 ;
b->bmiHeader.biCompression = BI_RGB ;
b->bmiHeader.biSizeImage = 0 ;
b->bmiHeader.biXPelsPerMeter = 1 ;
b->bmiHeader.biYPelsPerMeter = 1 ;
b->bmiHeader.biClrUsed = 0 ;
b->bmiHeader.biClrImportant = 0 ;
}
POINT GetBitmapSize( HBITMAP h ) {
POINT p ;
BITMAP o ;
GetObject( h, sizeof(o), &o ) ;
p.x = o.bmWidth ;
p.y = o.bmHeight ;
return(p) ;
}
void OpenBitmapForWork( HBITMAP b, tWorkBMP *w ) {
BITMAPINFO s ;
HDC h = GetDC( NULL ) ;
POINT v = GetBitmapSize( b ) ;
CreateWorkingBitmap( v.x, v.y, w ) ;
SetBMIHeader( &s,w->x, w->y ) ;
GetDIBits( h, b, 0, w->y, w->b, &s, DIB_RGB_COLORS ) ;
ReleaseDC( NULL, h ) ;
}
void SaveWorkingBitmap( tWorkBMP *w, HBITMAP b ) {
BITMAPINFO s ;
HDC h = GetDC( NULL ) ;
SetBMIHeader( &s, w->x, w->y ) ;
SetDIBits( h, b, 0, w->y, w->b, &s, DIB_RGB_COLORS ) ;
ReleaseDC( NULL, h ) ;
}
void ShrinkWorkingBitmap( tWorkBMP *a, tWorkBMP *b, WORD bx, WORD by ) {
BYTE *uy = a->b, *ux, i ;
WORD x, y, nx, ny = 0 ;
DWORD df = 3*bx, nf = df*by, j ;
float k, qx[2], qy[2], q[4], *f = Alloc( nf, float ) ;
CreateWorkingBitmap( bx, by, b) ;
jFor (nf) f[j]=0;
j=0;
For( y, a->y ) {
ux=uy;
uy+=a->l;
nx=0;
ny+=by;
if (ny>a->y) {
qy[0]=1-(qy[1]=(ny-a->y)/(float)by);
For (x,a->x) {
nx+=bx;
if (nx>a->x) {
qx[0]=1-(qx[1]=(nx-a->x)/(float)bx);
iFor (4) q[i]=qx[i&1]*qy[i>>1];
iFor (3) {
f[j]+=(*ux)*q[0];
f[j+3]+=(*ux)*q[1];
f[j+df]+=(*ux)*q[2];
f[(j++)+df+3]+=(*(ux++))*q[3];
}
} else iFor (3) {
f[j+i]+=(*ux)*qy[0];
f[j+df+i]+=(*(ux++))*qy[1];
}
if (nx>=a->x) nx-=a->x;
if (!nx) j+=3;
}
} else {
For (x,a->x) {
nx+=bx;
if (nx>a->x) {
qx[0]=1-(qx[1]=(nx-a->x)/(float)bx);
iFor (3) {
f[j]+=(*ux)*qx[0];
f[(j++)+3]+=(*(ux++))*qx[1];
}
}
else iFor (3) f[j+i]+=*(ux++);
if (nx>=a->x) nx-=a->x;
if (!nx) j+=3;
}
if (ny<a->y) j-=df;
}
if (ny>=a->y) ny-=a->y;
}
nf=0;
k=bx*by/(float)(a->x*a->y);
uy=b->b;
For (y,by) {
jFor (df) uy[j]=(unsigned char)(f[nf++]*k+.5);
uy+=b->l;
}
free (f);
}
TARGET HBITMAP ShrinkBitmap( HBITMAP a, WORD bx, WORD by )
// creates and returns new bitmap with dimensions of
// [bx,by] by shrinking bitmap a both [bx,by] must be less or equal
// than the dims of a, unless the result is nonsense
{
tWorkBMP in, out ;
HBITMAP b=CreateEmptyBitmap( bx, by ) ;
OpenBitmapForWork( a, &in ) ;
ShrinkWorkingBitmap( &in, &out, bx, by ) ;
free( in.b ) ;
SaveWorkingBitmap( &out, b ) ;
free( out.b ) ;
return( b ) ;
}
HBITMAP ResizeBmp( HBITMAP hBmpSrc, WORD bx, WORD by ) {
SIZE newSize ;
newSize.cx = bx;
newSize.cy = by;
// taille actuelle
BITMAP bmpInfo;
GetObject(hBmpSrc, sizeof(BITMAP), &bmpInfo);
SIZE oldSize;
oldSize.cx = bmpInfo.bmWidth;
oldSize.cy = bmpInfo.bmHeight;
// selection source ds un DC
HDC hdc = GetDC(NULL);
HDC hDCSrc = CreateCompatibleDC(hdc);
HBITMAP hOldBmpSrc = (HBITMAP)SelectObject(hDCSrc, hBmpSrc);
// création bitmap dest et sélection ds un DC
HDC hDCDst = CreateCompatibleDC(hdc);
HBITMAP hBmpDst = CreateCompatibleBitmap(hdc, newSize.cx, newSize.cy);
HBITMAP hOldBmpDst = (HBITMAP)SelectObject(hDCDst, hBmpDst);
// resize
StretchBlt(hDCDst, 0, 0, newSize.cx, newSize.cy, hDCSrc, 0, 0, oldSize.cx, oldSize.cy, SRCCOPY);
// libération ressources
SelectObject(hDCSrc, hOldBmpSrc);
SelectObject(hDCDst, hOldBmpDst);
DeleteDC(hDCSrc);
DeleteDC(hDCDst);
ReleaseDC(NULL, hdc);
return hBmpDst;
}
static void fill_dc(HDC dc, int width, int height, COLORREF color)
{
HBRUSH clrBrush = CreateSolidBrush(color);
HPEN clrPen = CreatePen(PS_SOLID, 0, color);
HBRUSH oldBrush;
HPEN oldPen;
oldBrush = SelectObject(dc, clrBrush);
oldPen = SelectObject(dc, clrPen);
Rectangle(dc, 0, 0, width, height);
SelectObject(dc, oldBrush);
SelectObject(dc, oldPen);
}
static BOOL load_wallpaper_bmp(HBITMAP* rawImage, int* style, int* x, int* y)
{
LONG lRes;
HKEY kDesktop;
DWORD pathLen = MAX_PATH;
DWORD numBufLen = 10;
char wpPath[MAX_PATH];
char wpStyleBuf[10];
char wpTileBuf[10];
int wpStyle = -1;
int wpTile = -1;
// NOTE: In the non-wallpaper case (i.e., load_file_bmp), we load parameters
// like x, y and style from cfg, but in the wallpaper case we ignore our
// stored configuration and get that information from the system.
// ENHANCE: It's possible to set WallpaperOriginX and WallpaperOriginY to
// specify an exact position for the start of the wallpaper, but this
// function doesn't support that yet. I don't think it's possible to set
// it through the normal UI anyway, you have to hack the registry to do it.
// For now, we'll never return an (x,y) positioning request to the caller.
*x = *y = 0;
lRes = RegOpenKeyEx(
HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &kDesktop
);
if(lRes != ERROR_SUCCESS)
{
RegCloseKey(kDesktop);
return FALSE; // TODO: Should the error be reported to the user here?
}
lRes = RegQueryValueEx(kDesktop, "Wallpaper", NULL, NULL, (LPBYTE)wpPath, &pathLen);
if(lRes != ERROR_SUCCESS)
{
RegCloseKey(kDesktop);
return FALSE; // TODO: Should the error be reported to the user here?
}
lRes = RegQueryValueEx( kDesktop, "WallpaperStyle", NULL, NULL, (LPBYTE)wpStyleBuf, &numBufLen );
if(lRes == ERROR_SUCCESS)
wpStyle = atoi(wpStyleBuf);
lRes = RegQueryValueEx( kDesktop, "TileWallpaper", NULL, NULL, (LPBYTE)wpTileBuf, &numBufLen );
if(lRes == ERROR_SUCCESS)
wpTile = atoi(wpTileBuf);
if(wpStyle < 0 && wpStyle > 3)
wpStyle = 0; // Default to tile.
else if(wpTile > 0)
wpStyle = 0; // Force tile.
else if(wpStyle == 0 && wpTile == 0)
wpStyle = 1; // For Explorer, wpStyle == wpTile == 0 means center, and
// it doesn't ever set wpStyle to 1. We call wpStyle == 1
// center and don't use wpTile, to simplify things after
// this point.
RegCloseKey(kDesktop);
if( *rawImage!=NULL ) { DeleteObject( *rawImage ) ; *rawImage=NULL ; }
*rawImage = LoadImage(
NULL, wpPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE
);
if(*rawImage == 0)
return FALSE; // TODO: Should the error be reported to the user here?
*style = wpStyle;
return TRUE;
}
static BOOL load_file_bmp(HBITMAP* rawImage, int* style, int* x, int* y)
{
*x = conf_get_int( conf,CONF_bg_image_abs_x);
*y = conf_get_int( conf,CONF_bg_image_abs_y );
*style = conf_get_int( conf,CONF_bg_image_style);
if( *rawImage!=NULL ) { DeleteObject( *rawImage ) ; *rawImage=NULL ; }
*rawImage = LoadImage(
NULL, conf_get_filename( conf, CONF_bg_image_filename )->path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE
);
if(*rawImage == 0)
return FALSE; // TODO: Should the error be reported to the user here?
return TRUE;
}
jmp_buf JPEG_bailout;
int usePalette = 0;
char *loadError = NULL;
COLORREF skycolour = RGB(0, 0, 255);
HBITMAP CreateHBitmap(int w, int h, LPVOID *lpBits)
{
HBITMAP bitmap;
BITMAPINFOHEADER BIH ;
int iSize = sizeof(BITMAPINFOHEADER) ;
memset(&BIH, 0, iSize);
// Fill in the header info.
BIH.biSize = iSize;
BIH.biWidth = w;
BIH.biHeight = h;
BIH.biPlanes = 1;
BIH.biBitCount = 24;
BIH.biCompression = BI_RGB;
HDC hDC = CreateCompatibleDC(NULL);
bitmap = CreateDIBSection(hDC,
(BITMAPINFO*)&BIH,
DIB_RGB_COLORS,
lpBits,
NULL,
0);
DeleteDC(hDC);
return bitmap;
}
// LOADJPEGIMAGE -- Load JPEG image into memory
HBITMAP loadJPEGimage(FILE *input_file, HGLOBAL *LimageBitmap, int *LsizeX, int *LsizeY)
{
int i;
LPBITMAPINFOHEADER bh;
DWORD bmpsize;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPARRAY colormap;
LPBYTE pix;
int linewid;
int pixbytes;
static LPBYTE sl = NULL;
static HGLOBAL imageBitmap = NULL; // In-memory bitmap
sl = NULL;
imageBitmap = NULL;
if (setjmp(JPEG_bailout) != 0) {
/* Since we arrive here via longjmp() from
parts unknown, we may have allocated
the line buffer or bitmap prior to bailing
out. If they've been allocated, release them. */
if (sl != NULL) {
GlobalFree(sl);
sl = NULL;
}
if (imageBitmap != NULL) {
GlobalFree(imageBitmap);
imageBitmap = NULL;
}
return NULL;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, input_file);
jpeg_read_header(&cinfo, TRUE);
cinfo.desired_number_of_colors = 254;
cinfo.quantize_colors = usePalette;
jpeg_start_decompress(&cinfo);
pixbytes = (usePalette ? 1 : 3);
sl = GlobalAlloc(GMEM_FIXED, cinfo.output_width * pixbytes);
if (sl == NULL) {
loadError = "Cannot allocate JPEG decoder row buffer";
return NULL;
}
linewid = ((((cinfo.output_width * pixbytes) + (sizeof(LONG) - 1)) / sizeof(LONG)) * sizeof(LONG));
bmpsize = sizeof(BITMAPINFOHEADER) +
(usePalette ? (256 * sizeof(RGBQUAD)) : 0) +
(linewid * cinfo.output_height);
imageBitmap = GlobalAlloc(GMEM_FIXED, bmpsize);
if (imageBitmap == NULL) {
loadError = "Cannot allocate bitmap for decoded JPEG image";
GlobalFree(sl);
return NULL;
}
// Plug in header fields with information from cinfo
bh = (LPBITMAPINFOHEADER) imageBitmap;
pix = ((LPBYTE) imageBitmap) + sizeof(BITMAPINFOHEADER) +
(usePalette ? (256 * sizeof(RGBQUAD)) : 0);
pix = pix + 0 ; // Pour eviter un warning de compilation
bh->biSize = sizeof(BITMAPINFOHEADER);
bh->biWidth = cinfo.output_width;
bh->biHeight = cinfo.output_height;
bh->biPlanes = 1;
bh->biBitCount = usePalette ? 8 : 24;
bh->biCompression = BI_RGB;
bh->biSizeImage = 0;
bh->biXPelsPerMeter = bh->biYPelsPerMeter = 2835;
bh->biClrUsed = 0;
bh->biClrImportant = 0;
/* Construct the palette from the colour map optimised
for the JPEG file. */
if (usePalette) {
colormap = cinfo.colormap;
for (i = 0; i < cinfo.actual_number_of_colors; i++) {
if (cinfo.num_components == 1) {
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbRed =
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbGreen =
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbBlue = GETJSAMPLE(colormap[0][i]);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbReserved = 0;
} else {
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbRed = GETJSAMPLE(colormap[0][i]);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbGreen = GETJSAMPLE(colormap[1][i]);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbBlue = GETJSAMPLE(colormap[2][i]);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[i])->rgbReserved = 0;
}
}
/* Plug black and our text colour in the last two slots
of the palette so we canbe sure they're available. */
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[254])->rgbRed =
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[254])->rgbGreen =
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[254])->rgbBlue =
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[254])->rgbReserved = 0;
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[255])->rgbRed = GetRValue(skycolour);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[255])->rgbGreen = GetGValue(skycolour);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[255])->rgbBlue = GetBValue(skycolour);
((LPRGBQUAD) &((LPBITMAPINFO) bh)->bmiColors[255])->rgbReserved = 0;
}
// Return scan lines and transfer to the pixel array
BYTE *pDst = NULL;
HBITMAP hBitmap = CreateHBitmap(bh->biWidth, bh->biHeight, (void**)&pDst);
int nStorageWidth = ((bh->biWidth * 24 + 31) & ~31) >> 3; //dword alignment
JSAMPARRAY ppDst = &pDst;
/*
LPBYTE px = pix + (linewid * (bh->biHeight - 1));
for (i = 0; i < (int) cinfo.output_height; i++) {
unsigned char *slp[1] = { sl };
jpeg_read_scanlines(&cinfo, slp, 1);
if (usePalette) {
memcpy(px, sl, cinfo.output_width);
} else {
int j, k;
if (cinfo.num_components == 3) {
for (j = k = 0; j < (int) cinfo.output_width; j++, k += 3) {
px[k] = sl[k + 2];
px[k + 1] = sl[k + 1];
px[k + 2] = sl[k];
}
} else {
for (j = k = 0; j < (int) cinfo.output_width; j++, k += 3) {
px[k] = sl[j];
px[k + 1] = sl[j];
px[k + 2] = sl[j];
}
}
}
px -= linewid;
}
*/
pDst = pDst + (cinfo.output_height-1)*nStorageWidth ;
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines (&cinfo, ppDst, 1);
if(cinfo.out_color_components==3) {
//swap R & B
BYTE* p = pDst;
int i ;
for( i=0;i<bh->biWidth;i++) {
BYTE r = p[0];
p[0] = p[2];
p[2] = r;
p += 3;
}
}
//pDst += nStorageWidth;
pDst -= nStorageWidth;
}
GlobalFree(sl);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
*LsizeX = (int) bh->biWidth;
*LsizeY = (int) bh->biHeight;
*LimageBitmap = imageBitmap;
return hBitmap;
}
static BOOL load_file_jpeg(HBITMAP* rawImage, int* style, int* x, int* y) {
*x = conf_get_int( conf, CONF_bg_image_abs_x );
*y = conf_get_int( conf, CONF_bg_image_abs_y );
*style = conf_get_int( conf, CONF_bg_image_style );
int res=TRUE, LsizeX, LsizeY ;
FILE *fp ;
HGLOBAL LimageBitmap = NULL ;
if( ( fp=fopen( conf_get_filename( conf,CONF_bg_image_filename)->path, "rb" ) ) == NULL ) return FALSE ;
if( *rawImage!=NULL ) { DeleteObject( *rawImage ) ; *rawImage=NULL ; }
*rawImage = loadJPEGimage(fp, &LimageBitmap,&LsizeX, &LsizeY) ;
if( rawImage == NULL ) res =FALSE ;
fclose( fp ) ;
GlobalFree(LimageBitmap);
return res;
}
static HBITMAP CreateDIBSectionWithFileMapping(HDC dc, int width, int height, HANDLE fmap)
{
BITMAPINFOHEADER BMI;
BMI.biSize = sizeof(BITMAPINFOHEADER);
BMI.biWidth = width;
BMI.biHeight = height;
BMI.biPlanes = 1;
BMI.biBitCount = 32;
BMI.biCompression = BI_RGB;
BMI.biSizeImage = 0;
BMI.biXPelsPerMeter = 0;
BMI.biYPelsPerMeter = 0;
BMI.biClrUsed = 0;
BMI.biClrImportant = 0;
return(CreateDIBSection(dc, (BITMAPINFO *)&BMI, DIB_RGB_COLORS, 0, fmap, 0));
}
/***********SCREEN CAPTURE*******************/
/******************************/
void init_dc_blend(void) {
//HMODULE * msimg32_dll = LoadLibrary("msimg32.dll");
HMODULE msimg32_dll = LoadLibrary("msimg32.dll");
if(msimg32_dll)
pAlphaBlend = GetProcAddress(msimg32_dll, "AlphaBlend");
if(pAlphaBlend) {
HDC hdc = GetDC(MainHwnd);
// Create one pixel size bitmap for use in color_blend.
if( colorinpixeldc !=NULL ) { DeleteDC(colorinpixeldc ) ; } colorinpixeldc = CreateCompatibleDC(hdc);
if( colorinpixelbm!=NULL ) { DeleteObject(colorinpixelbm) ; } colorinpixelbm = CreateCompatibleBitmap(hdc, 1, 1);
SelectObject(colorinpixeldc, colorinpixelbm);
colorinpixel = 0;
SetPixelV(colorinpixeldc, 0, 0, colorinpixel);
ReleaseDC(MainHwnd, hdc);
}
}
void color_blend(
HDC destDc, int x, int y, int width, int height,
COLORREF alphacolor, int opacity)
{
if(pAlphaBlend) {
// Fast alpha blending for Win98&2000 and newer...
BLENDFUNCTION blender;
// Create one pixel size bitmap for use in color_blend.
if(colorinpixel != alphacolor)
{
colorinpixel = alphacolor;
SetPixelV(colorinpixeldc, 0, 0, alphacolor);
}
blender.BlendOp = AC_SRC_OVER;
blender.BlendFlags = 0;
blender.SourceConstantAlpha = (0xff * opacity) / 100;
blender.AlphaFormat = 0;
(*pAlphaBlend)(destDc, x, y, width, height, colorinpixeldc, 0, 0, 1, 1, blender);
}
else
{
// Slow alpha blending for Win95&NT...
// Note: Only tested with WinXP, should work on 95/NT.. probably.
int i, alphacolorR, alphacolorG, alphacolorB, bk_opacity;
HBITMAP tmpbm;
HDC tmpdc;
static HANDLE fmap;
static int fmap_size;
static unsigned char * pRGB;
if(fmap_size < width * height * 4)
{
if(fmap) {
UnmapViewOfFile(pRGB);
CloseHandle(fmap);
}
fmap_size = width * height * 4;
fmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, fmap_size, NULL);
pRGB = MapViewOfFile(fmap, FILE_MAP_ALL_ACCESS, 0, 0, fmap_size);
}
// Create DIBSection so we get pixels easily.
tmpdc = CreateCompatibleDC(destDc);
tmpbm = CreateDIBSectionWithFileMapping(destDc, width, height, fmap);
SelectObject(tmpdc, tmpbm);
// Copy bitmap to temporary bitmap for easy pixel access.
BitBlt(tmpdc, 0, 0, width, height, destDc, x, y, SRCCOPY);
// Moved stuff out from the loop
alphacolorR = GetRValue(alphacolor) * opacity;
alphacolorG = GetGValue(alphacolor) * opacity;
alphacolorB = GetBValue(alphacolor) * opacity;
bk_opacity = 100 - opacity;
for(i=0; i<width*height*4; i+=4)
{
pRGB[i + 0] = (pRGB[i + 0] * bk_opacity + alphacolorB) / 100;
pRGB[i + 1] = (pRGB[i + 1] * bk_opacity + alphacolorG) / 100;
pRGB[i + 2] = (pRGB[i + 2] * bk_opacity + alphacolorR) / 100;
}
// Copy temporary bitmap back to original
BitBlt(destDc, x, y, width, height, tmpdc, 0, 0, SRCCOPY);
DeleteObject(tmpbm);
DeleteDC(tmpdc);
}
}
static void color_opacity_gradient( HDC destDc, int x, int y, int width, int height, COLORREF alphacolor, int style ) {
int i, alphacolorR, alphacolorG, alphacolorB, bk_opacity, opacity, h, w ;
double l ;
HBITMAP tmpbm;
HDC tmpdc;
static HANDLE fmap;
static int fmap_size;
static unsigned char * pRGB;
int OpacityMin = 0, OpacityMax = 100 ;
char buf[256] = "" ;
if( GetSessionField( conf_get_str(conf,CONF_sessionname), conf_get_str(conf,CONF_folder), "BgOpacityRange", buf ) ) {
sscanf( buf, "%d-%d", &OpacityMin, &OpacityMax ) ;
if( OpacityMin == OpacityMax ) { OpacityMin = 0 ; OpacityMax = 100 ; }
}
if(fmap_size < width * height * 4) {
if(fmap) { UnmapViewOfFile(pRGB) ; CloseHandle(fmap) ; }
fmap_size = width * height * 4;
fmap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, fmap_size, NULL);
pRGB = MapViewOfFile(fmap, FILE_MAP_ALL_ACCESS, 0, 0, fmap_size);
}
// Create DIBSection so we get pixels easily.
tmpdc = CreateCompatibleDC(destDc);
tmpbm = CreateDIBSectionWithFileMapping(destDc, width, height, fmap);
SelectObject(tmpdc, tmpbm);
// Copy bitmap to temporary bitmap for easy pixel access.
BitBlt(tmpdc, 0, 0, width, height, destDc, x, y, SRCCOPY);
opacity = 0 ; bk_opacity = 100 ; alphacolorR = 0 ; alphacolorG = 0 ; alphacolorB = 0 ;
w = 0 ; h = 0 ;
for(i=0; i<width*height*4; i+=4) {
switch( style ) {
case 2: // De bas en haut
if( (i%(4*width)) == 0 ) {
h++ ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * (1.0*h)/(1.0*height) ;
opacity = 100 - opacity ;
}
break ;
case 3: // De gauche a droite
w++ ; if( w >= width ) { w = 0 ; }
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * (1.0*w)/(1.0*width) ;
break ;
case 4: // De droite a gauche
w++ ; if( w >= width ) { w = 0 ; }
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * (1.0*w)/(1.0*width) ;
opacity = 100 - opacity ;
break ;
case 5: // Du centre vers l'exterieur
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*width/2.0-w,2.0)+pow(1.0*height/2.0-h,2.0) ) /
sqrt( pow(1.0*width/2.0,2.0)+pow(1.0*height/2.0,2.0) );
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
break ;
case 6: // De l'exterieur vers le centre
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*width/2.0-w,2.0)+pow(1.0*height/2.0-h,2.0) ) /
sqrt( pow(1.0*width/2.0,2.0)+pow(1.0*height/2.0,2.0) );
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
opacity = 100 - opacity ;
break ;
case 7:
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*w,2.0)+pow(1.0*h,2.0) )/sqrt( pow(1.0*width,2.0)+pow(1.0*height,2.0) ) ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
break ;
case 8:
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*w,2.0)+pow(1.0*h,2.0) )/sqrt( pow(1.0*width,2.0)+pow(1.0*height,2.0) ) ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
opacity = 100 - opacity ;
break ;
case 9:
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*(width-w),2.0)+pow(1.0*h,2.0) )/sqrt( pow(1.0*width,2.0)+pow(1.0*height,2.0) ) ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
break ;
case 10:
if( (i%(4*width)) == 0 ) { h++ ; }
w++ ; if( w >= width ) { w = 0 ; }
l = sqrt( pow(1.0*(width-w),2.0)+pow(1.0*h,2.0) )/sqrt( pow(1.0*width,2.0)+pow(1.0*height,2.0) ) ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * l ;
opacity = 100 - opacity ;
break ;
default: // De haut en bas
if( (i%(4*width)) == 0 ) {
h++ ;
opacity = OpacityMin + 1.0*( OpacityMax-OpacityMin ) * (1.0*h)/(1.0*height) ;
}
break ;
}
if( opacity < 0 ) opacity = 0 ;
if( opacity > 100 ) opacity = 100 ;
alphacolorR = GetRValue(alphacolor) * opacity;
alphacolorG = GetGValue(alphacolor) * opacity;
alphacolorB = GetBValue(alphacolor) * opacity;
bk_opacity = 100 - opacity ;
pRGB[i + 0] = (pRGB[i + 0] * bk_opacity + alphacolorB) / 100;
pRGB[i + 1] = (pRGB[i + 1] * bk_opacity + alphacolorG) / 100;
pRGB[i + 2] = (pRGB[i + 2] * bk_opacity + alphacolorR) / 100;
}
// Copy temporary bitmap back to original
BitBlt(destDc, x, y, width, height, tmpdc, 0, 0, SRCCOPY);
DeleteObject(tmpbm);
DeleteDC(tmpdc);
}
void CreateBlankBitmap( HBITMAP * rawImage, const int width, const int height ) {
HDC dc= CreateCompatibleDC(NULL);
BITMAPINFO bi;
ZeroMemory( &bi.bmiHeader, sizeof(BITMAPINFOHEADER) );
bi.bmiHeader.biWidth=width; // Set size you need
bi.bmiHeader.biHeight=height; // Set size you need
bi.bmiHeader.biPlanes=1;
bi.bmiHeader.biBitCount=24; // Can be 8, 16, 32 bpp or
bi.bmiHeader.biSizeImage=0;
bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biClrUsed= 0;
bi.bmiHeader.biClrImportant= 0;
VOID *pvBits;
if( *rawImage!=NULL ) { DeleteObject( *rawImage ) ; *rawImage=NULL ; }
*rawImage = CreateDIBSection( dc,&bi,DIB_RGB_COLORS,&pvBits,NULL,0 );
ReleaseDC(NULL, dc) ;
HDC hDC = GetDC(MainHwnd);
HDC hDCDst = CreateCompatibleDC(hDC); // memory device context for dest bitmap
ReleaseDC(NULL, hDC);
// hdcMem contains your rendered particle
HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0));
// Paints the rectangular band with the brush
RECT Rect = {0, 0, width-1, height-1};
FillRect(hDCDst, &Rect, hBrush);
// Deletes the brush
DeleteObject(hBrush);
HGDIOBJ holdDIBDst = SelectObject(hDCDst, *rawImage);
// transfer the image to your DIB bitmap
BitBlt(hDCDst, 0, 0 ,width, height, hDC, 0, 0, SRCCOPY);
// clean up
SelectObject(hDCDst, holdDIBDst);
DeleteDC(hDCDst);
}
BOOL load_bg_bmp()
{
HBITMAP rawImage = NULL;
BITMAP rawImageInfo;
HDC hdcPrimary;
HDC bmpdc;
int deskWidth, deskHeight, clientWidth, clientHeight;
int x, y;
int style;
//COLORREF backgroundcolor = colours[258]; // Default Background
COLORREF backgroundcolor = return_colours258() ;
COLORREF alphacolor = backgroundcolor;
// Start off assuming this is true.
bBgRelToTerm = conf_get_int( conf,CONF_bg_image_abs_fixed);
RECT clientRect;
GetWindowRect( MainHwnd, &clientRect ) ;
clientWidth = clientRect.right-clientRect.left+1 ;
clientHeight = clientRect.bottom-clientRect.top+1 ;
switch( conf_get_int( conf,CONF_bg_type) )
{
// Solid
case 0:
// No bitmap file to load. We'll handle this case below.
break;
// Wallpaper
case 1:
backgroundcolor = GetSysColor(COLOR_BACKGROUND);
bBgRelToTerm = FALSE; // Wallpaper is never positioned relative to term.
if(!load_wallpaper_bmp(&rawImage, &style, &x, &y))
rawImage = NULL; // Make sure rawImage is still NULL.
break;
// Image
case 2:
{
backgroundcolor = GetSysColor(COLOR_BACKGROUND) ;
if( conf_get_filename(conf,CONF_bg_image_filename)->path[0] == '#' ) {
int r=0,g=0,b=0;
sscanf( conf_get_filename(conf,CONF_bg_image_filename)->path, "#%02X%02X%02X", &r, &g, &b ) ;
backgroundcolor = RGB( r, g, b ) ;
BYTE *pDst = NULL;
rawImage = CreateHBitmap(10, 10, (void**)&pDst);
style = 4 ;
}
else if( !stricmp( conf_get_filename(conf,CONF_bg_image_filename)->path+strlen(conf_get_filename(conf,CONF_bg_image_filename)->path)-4, ".jpg" ) ) {
if(!load_file_jpeg(&rawImage, &style, &x, &y))
rawImage = NULL; // Make sure rawImage is still NULL.
}
else if( !stricmp( conf_get_filename(conf,CONF_bg_image_filename)->path+strlen(conf_get_filename(conf,CONF_bg_image_filename)->path)-5, ".jpeg" ) ) {
if(!load_file_jpeg(&rawImage, &style, &x, &y))
rawImage = NULL; // Make sure rawImage is still NULL.
}
else
if(!load_file_bmp(&rawImage, &style, &x, &y))
rawImage = NULL; // Make sure rawImage is still NULL.
break;
}
}
hdcPrimary = GetDC(MainHwnd);
deskWidth = GetDeviceCaps(hdcPrimary, HORZRES);
deskHeight = GetDeviceCaps(hdcPrimary, VERTRES);
// Securite pour ne pas depacer les limites de l'ecran principal
if( (bBgRelToTerm == 0)
&&((clientRect.right>deskWidth)||(clientRect.bottom>deskHeight)) ) {
//DeleteObject(rawImage); rawImage = NULL ;
bBgRelToTerm = 1 ;
}
if(rawImage == NULL)
{
// Create a solid bitmap. We'll get here in two cases: either the user
// selected the Solid background option, or the attempt to do something
// fancier (use the system wallpaper or an image file) failed.
/*
if( backgrounddc!=NULL ) DeleteDC(backgrounddc) ; backgrounddc = CreateCompatibleDC(hdcPrimary);
if( backgroundbm!=NULL ) DeleteObject(backgroundbm); backgroundbm
= CreateCompatibleBitmap(hdcPrimary, deskWidth, deskHeight);
SelectObject(backgrounddc, backgroundbm);
*/
// Do not create anything, use default 'no background'-code instead.
}
else
{
// We've managed to load a good image. Now, we need to manipulate it to
// the right size, location, etc.
// Find the width and height of the image we just loaded.
GetObject(rawImage, sizeof(rawImageInfo), &rawImageInfo);
// Create a temporary DC to wrap the raw image.
bmpdc = CreateCompatibleDC(hdcPrimary);
SelectObject(bmpdc, rawImage);
// Create a memory DC that has a new bitmap of the appropriate final
// image size.
if( textdc!=NULL ) { DeleteDC(textdc) ; } textdc = CreateCompatibleDC(hdcPrimary);
if( textbm!= NULL ) { DeleteObject(textbm) ; } textbm = CreateCompatibleBitmap(hdcPrimary, deskWidth, deskHeight);
SelectObject(textdc, textbm);
if( backgrounddc!=NULL ) { DeleteDC(backgrounddc) ; } backgrounddc = CreateCompatibleDC(hdcPrimary);
if( backgroundbm!=NULL ) { DeleteObject(backgroundbm) ; } backgroundbm = CreateCompatibleBitmap(hdcPrimary, deskWidth, deskHeight) ;
SelectObject(backgrounddc, backgroundbm);
switch(style)
{
case 0: { // Tile
for(y = 0; y < deskHeight; y += rawImageInfo.bmHeight)
{
for(x = 0; x < deskWidth; x += rawImageInfo.bmWidth)
{
BitBlt(
backgrounddc,
x, y, rawImageInfo.bmWidth, rawImageInfo.bmHeight,
bmpdc, 0, 0, SRCCOPY
);
}