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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
|
/*
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
* Copyright (C) 2002 Tim Jansen. All Rights Reserved.
* Copyright (C) 1999-2001 Anders Lindstr�m
*
*
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* tim@tjansen.de: - removed stuff for krdc
* - merged with shm.c and misc.c
* - added FillRectangle and Sync methods to draw only on
* the image
* - added Zoom functionality, based on rotation funcs from
* SGE by Anders Lindstr�m)
* - added support for softcursor encoding
*
*/
/*
* desktop.c - functions to deal with "desktop" window.
*/
#include <X11/Xlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
#include <math.h>
#include <limits.h>
#include "vncviewer.h"
static XShmSegmentInfo shminfo;
static Bool caughtShmError = False;
static Bool needShmCleanup = False;
static XShmSegmentInfo zoomshminfo;
static Bool caughtZoomShmError = False;
static Bool needZoomShmCleanup = False;
static Bool gcInited = False;
GC gc;
GC srcGC, dstGC; /* used for debugging copyrect */
Dimension dpyWidth, dpyHeight;
static XImage *image = NULL;
Bool useShm = True;
static Bool zoomActive = False;
static int zoomWidth, zoomHeight;
static XImage *zoomImage = NULL;
static Bool useZoomShm = True;
/* for softcursor */
static char *savedArea = NULL;
typedef enum {
SOFTCURSOR_UNDER,
SOFTCURSOR_PART_UNDER,
SOFTCURSOR_UNAFFECTED
} SoftCursorState;
typedef int Sint32;
typedef short Sint16;
typedef char Sint8;
typedef unsigned int Uint32;
typedef unsigned short Uint16;
typedef unsigned char Uint8;
typedef struct {
int w, h;
unsigned int pitch;
void *pixels;
int BytesPerPixel;
} Surface;
typedef struct {
Sint16 x, y;
Uint16 w, h;
} Rect;
static void bgr233cpy(CARD8 *dst, CARD8 *src, int len);
static void CopyDataToScreenRaw(char *buf, int x, int y, int width, int height);
static void CopyBGR233ToScreen(CARD8 *buf, int x, int y, int width,int height);
static void FillRectangleBGR233(CARD8 buf, int x, int y, int width,int height);
static int CheckRectangle(int x, int y, int width, int height);
static SoftCursorState getSoftCursorState(int x, int y, int width, int height);
static void discardCursorSavedArea(void);
static void saveCursorSavedArea(void);
static void ZoomInit(void);
static void transformZoomSrc(int six, int siy, int siw, int sih,
int *dix, int *diy, int *diw, int *dih,
int srcW, int dstW, int srcH, int dstH);
static void transformZoomDst(int *six, int *siy, int *siw, int *sih,
int dix, int diy, int diw, int dih,
int srcW, int dstW, int srcH, int dstH);
static void ZoomSurfaceSrcCoords(int x, int y, int w, int h,
int *dix, int *diy, int *diw, int *dih,
Surface * src, Surface * dst);
static void ZoomSurfaceCoords32(int sx, int sy, int sw, int sh,
int dx, int dy, Surface * src, Surface * dst);
static void sge_transform(Surface *src, Surface *dst, float xscale, float yscale,
Uint16 qx, Uint16 qy);
void
DesktopInit(Window win)
{
XGCValues gcv;
image = CreateShmImage();
if (!image) {
useShm = False;
image = XCreateImage(dpy, vis, visdepth, ZPixmap, 0, NULL,
si.framebufferWidth, si.framebufferHeight,
BitmapPad(dpy), 0);
image->data = calloc(image->bytes_per_line * image->height, 1);
if (!image->data) {
fprintf(stderr,"malloc failed\n");
exit(1);
}
}
gc = XCreateGC(dpy,win,0,NULL);
gcv.function = GXxor;
gcv.foreground = 0x0f0f0f0f;
srcGC = XCreateGC(dpy,win,GCFunction|GCForeground,&gcv);
gcv.foreground = 0xf0f0f0f0;
dstGC = XCreateGC(dpy,win,GCFunction|GCForeground,&gcv);
gcInited = True;
}
/*
* DrawScreenRegionX11Thread
* Never call from any other desktop.c function, only for X11 thread
*/
void
DrawScreenRegionX11Thread(Window win, int x, int y, int width, int height) {
zoomActive = False;
zoomWidth = 0;
zoomHeight = 0;
if (!image)
return;
if (useShm)
XShmPutImage(dpy, win, gc, image, x, y, x, y, width, height, False);
else
XPutImage(dpy, win, gc, image, x, y, x, y, width, height);
}
/*
* CheckRectangle
*/
static int CheckRectangle(int x, int y, int width, int height) {
if ((x < 0) || (y < 0))
return 0;
if (((x+width) > si.framebufferWidth) || ((y+height) > si.framebufferHeight))
return 0;
return 1;
}
static
void bgr233cpy(CARD8 *dst, CARD8 *src, int len) {
int i;
CARD16 *d16;
CARD32 *d32;
switch (visbpp) {
case 8:
for (i = 0; i < len; i++)
*(dst++) = (CARD8) BGR233ToPixel[*(src++)];
break;
case 16:
d16 = (CARD16*) dst;
for (i = 0; i < len; i++)
*(d16++) = (CARD16) BGR233ToPixel[*(src++)];
break;
case 32:
d32 = (CARD32*) dst;
for (i = 0; i < len; i++)
*(d32++) = (CARD32) BGR233ToPixel[*(src++)];
break;
default:
fprintf(stderr, "Unsupported softcursor depth %d\n", visbpp);
}
}
/*
* CopyDataToScreen.
*/
void
CopyDataToScreen(char *buf, int x, int y, int width, int height)
{
SoftCursorState s;
if (!CheckRectangle(x, y, width, height))
return;
LockFramebuffer();
s = getSoftCursorState(x, y, width, height);
if (s == SOFTCURSOR_PART_UNDER)
undrawCursor();
if (!appData.useBGR233)
CopyDataToScreenRaw(buf, x, y, width, height);
else
CopyBGR233ToScreen((CARD8 *)buf, x, y, width, height);
if (s != SOFTCURSOR_UNAFFECTED)
drawCursor();
UnlockFramebuffer();
SyncScreenRegion(x, y, width, height);
}
/*
* CopyDataToScreenRaw.
*/
static void
CopyDataToScreenRaw(char *buf, int x, int y, int width, int height)
{
int h;
int widthInBytes = width * visbpp / 8;
int scrWidthInBytes = image->bytes_per_line;
char *scr = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
for (h = 0; h < height; h++) {
memcpy(scr, buf, widthInBytes);
buf += widthInBytes;
scr += scrWidthInBytes;
}
}
/*
* CopyBGR233ToScreen.
*/
static void
CopyBGR233ToScreen(CARD8 *buf, int x, int y, int width, int height)
{
int p, q;
int xoff = 7 - (x & 7);
int xcur;
int fbwb = si.framebufferWidth / 8;
CARD8 *scr1 = ((CARD8 *)image->data) + y * fbwb + x / 8;
CARD8 *scrt;
CARD8 *scr8 = ((CARD8 *)image->data) + y * si.framebufferWidth + x;
CARD16 *scr16 = ((CARD16 *)image->data) + y * si.framebufferWidth + x;
CARD32 *scr32 = ((CARD32 *)image->data) + y * si.framebufferWidth + x;
switch (visbpp) {
/* thanks to Chris Hooper for single bpp support */
case 1:
for (q = 0; q < height; q++) {
xcur = xoff;
scrt = scr1;
for (p = 0; p < width; p++) {
*scrt = ((*scrt & ~(1 << xcur))
| (BGR233ToPixel[*(buf++)] << xcur));
if (xcur-- == 0) {
xcur = 7;
scrt++;
}
}
scr1 += fbwb;
}
break;
case 8:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr8++) = BGR233ToPixel[*(buf++)];
}
scr8 += si.framebufferWidth - width;
}
break;
case 16:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr16++) = BGR233ToPixel[*(buf++)];
}
scr16 += si.framebufferWidth - width;
}
break;
case 32:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr32++) = BGR233ToPixel[*(buf++)];
}
scr32 += si.framebufferWidth - width;
}
break;
}
}
/*
* FillRectangle8.
*/
void
FillRectangle8(CARD8 fg, int x, int y, int width, int height)
{
SoftCursorState s;
if (!CheckRectangle(x, y, width, height))
return;
s = getSoftCursorState(x, y, width, height);
if (s == SOFTCURSOR_PART_UNDER)
undrawCursor();
if (!appData.useBGR233) {
int h;
int widthInBytes = width * visbpp / 8;
int scrWidthInBytes = image->bytes_per_line;
char *scr = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
for (h = 0; h < height; h++) {
memset(scr, fg, widthInBytes);
scr += scrWidthInBytes;
}
} else {
FillRectangleBGR233(fg, x, y, width, height);
}
if (s != SOFTCURSOR_UNAFFECTED)
drawCursor();
}
/*
* FillRectangleBGR233.
*/
static void
FillRectangleBGR233(CARD8 fg, int x, int y, int width, int height)
{
int p, q;
int xoff = 7 - (x & 7);
int xcur;
int fbwb = si.framebufferWidth / 8;
CARD8 *scr1 = ((CARD8 *)image->data) + y * fbwb + x / 8;
CARD8 *scrt;
CARD8 *scr8 = ((CARD8 *)image->data) + y * si.framebufferWidth + x;
CARD16 *scr16 = ((CARD16 *)image->data) + y * si.framebufferWidth + x;
CARD32 *scr32 = ((CARD32 *)image->data) + y * si.framebufferWidth + x;
unsigned long fg233 = BGR233ToPixel[fg];
switch (visbpp) {
/* thanks to Chris Hooper for single bpp support */
case 1:
for (q = 0; q < height; q++) {
xcur = xoff;
scrt = scr1;
for (p = 0; p < width; p++) {
*scrt = ((*scrt & ~(1 << xcur))
| (fg233 << xcur));
if (xcur-- == 0) {
xcur = 7;
scrt++;
}
}
scr1 += fbwb;
}
break;
case 8:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr8++) = fg233;
}
scr8 += si.framebufferWidth - width;
}
break;
case 16:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr16++) = fg233;
}
scr16 += si.framebufferWidth - width;
}
break;
case 32:
for (q = 0; q < height; q++) {
for (p = 0; p < width; p++) {
*(scr32++) = fg233;
}
scr32 += si.framebufferWidth - width;
}
break;
}
}
/*
* FillRectangle16
*/
void
FillRectangle16(CARD16 fg, int x, int y, int width, int height)
{
int i, h;
int scrWidthInBytes = image->bytes_per_line;
char *scr = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
CARD16 *scr16;
SoftCursorState s;
if (!CheckRectangle(x, y, width, height))
return;
s = getSoftCursorState(x, y, width, height);
if (s == SOFTCURSOR_PART_UNDER)
undrawCursor();
for (h = 0; h < height; h++) {
scr16 = (CARD16*) scr;
for (i = 0; i < width; i++)
scr16[i] = fg;
scr += scrWidthInBytes;
}
if (s != SOFTCURSOR_UNAFFECTED)
drawCursor();
}
/*
* FillRectangle32
*/
void
FillRectangle32(CARD32 fg, int x, int y, int width, int height)
{
int i, h;
int scrWidthInBytes = image->bytes_per_line;
SoftCursorState s;
char *scr = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
CARD32 *scr32;
if (!CheckRectangle(x, y, width, height))
return;
s = getSoftCursorState(x, y, width, height);
if (s == SOFTCURSOR_PART_UNDER)
undrawCursor();
for (h = 0; h < height; h++) {
scr32 = (CARD32*) scr;
for (i = 0; i < width; i++)
scr32[i] = fg;
scr += scrWidthInBytes;
}
if (s != SOFTCURSOR_UNAFFECTED)
drawCursor();
}
/*
* CopyDataFromScreen.
*/
void
CopyDataFromScreen(char *buf, int x, int y, int width, int height)
{
int widthInBytes = width * visbpp / 8;
int scrWidthInBytes = image->bytes_per_line;
char *src = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
int h;
if (!CheckRectangle(x, y, width, height))
return;
for (h = 0; h < height; h++) {
memcpy(buf, src, widthInBytes);
src += scrWidthInBytes;
buf += widthInBytes;
}
}
/*
* CopyArea
*/
void
CopyArea(int srcX, int srcY, int width, int height, int x, int y)
{
int widthInBytes = width * visbpp / 8;
SoftCursorState sSrc, sDst;
LockFramebuffer();
sSrc = getSoftCursorState(srcX, srcY, width, height);
sDst = getSoftCursorState(x, y, width, height);
if ((sSrc != SOFTCURSOR_UNAFFECTED) ||
(sDst == SOFTCURSOR_PART_UNDER))
undrawCursor();
if ((srcY+height < y) || (y+height < srcY) ||
(srcX+width < x) || (x+width < srcX)) {
int scrWidthInBytes = image->bytes_per_line;
char *src = (image->data + srcY * scrWidthInBytes
+ srcX * visbpp / 8);
char *dst = (image->data + y * scrWidthInBytes
+ x * visbpp / 8);
int h;
if (!CheckRectangle(srcX, srcY, width, height)) {
UnlockFramebuffer();
return;
}
if (!CheckRectangle(x, y, width, height)) {
UnlockFramebuffer();
return;
}
for (h = 0; h < height; h++) {
memcpy(dst, src, widthInBytes);
src += scrWidthInBytes;
dst += scrWidthInBytes;
}
}
else {
char *buf = malloc(widthInBytes*height);
if (!buf) {
UnlockFramebuffer();
fprintf(stderr, "Out of memory, CopyArea impossible\n");
return;
}
CopyDataFromScreen(buf, srcX, srcY, width, height);
CopyDataToScreenRaw(buf, x, y, width, height);
free(buf);
}
if ((sSrc != SOFTCURSOR_UNAFFECTED) ||
(sDst != SOFTCURSOR_UNAFFECTED))
drawCursor();
UnlockFramebuffer();
SyncScreenRegion(x, y, width, height);
}
void SyncScreenRegion(int x, int y, int width, int height) {
int dx, dy, dw, dh;
if (zoomActive) {
Surface src, dest;
src.w = si.framebufferWidth;
src.h = si.framebufferHeight;
src.pitch = image->bytes_per_line;
src.pixels = image->data;
src.BytesPerPixel = visbpp / 8;
dest.w = zoomWidth;
dest.h = zoomHeight;
dest.pitch = zoomImage->bytes_per_line;
dest.pixels = zoomImage->data;
dest.BytesPerPixel = visbpp / 8;
ZoomSurfaceSrcCoords(x, y, width, height, &dx, &dy, &dw, &dh, &src, &dest);
}
else {
dx = x; dy = y;
dw = width; dh = height;
}
DrawScreenRegion(dx, dy, dw, dh);
}
void SyncScreenRegionX11Thread(int x, int y, int width, int height) {
int dx, dy, dw, dh;
if (zoomActive) {
Surface src, dest;
src.w = si.framebufferWidth;
src.h = si.framebufferHeight;
src.pitch = image->bytes_per_line;
src.pixels = image->data;
src.BytesPerPixel = visbpp / 8;
dest.w = zoomWidth;
dest.h = zoomHeight;
dest.pitch = zoomImage->bytes_per_line;
dest.pixels = zoomImage->data;
dest.BytesPerPixel = visbpp / 8;
ZoomSurfaceSrcCoords(x, y, width, height, &dx, &dy, &dw, &dh, &src, &dest);
}
else {
dx = x; dy = y;
dw = width; dh = height;
}
DrawAnyScreenRegionX11Thread(dx, dy, dw, dh);
}
/*
* ToplevelInitBeforeRealization sets the title, geometry and other resources
* on the toplevel window.
*/
void
ToplevelInit()
{
dpyWidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
dpyHeight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
}
/*
* Cleanup - perform shm cleanup operations prior to exiting.
*/
void
Cleanup()
{
if (useShm || useZoomShm)
ShmCleanup();
}
void
ShmCleanup()
{
fprintf(stderr,"ShmCleanup called\n");
if (needShmCleanup) {
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid, IPC_RMID, 0);
needShmCleanup = False;
}
if (needZoomShmCleanup) {
shmdt(zoomshminfo.shmaddr);
shmctl(zoomshminfo.shmid, IPC_RMID, 0);
needZoomShmCleanup = False;
}
}
static int
ShmCreationXErrorHandler(Display *d, XErrorEvent *e)
{
caughtShmError = True;
return 0;
}
XImage *
CreateShmImage()
{
XImage *_image;
XErrorHandler oldXErrorHandler;
if (!XShmQueryExtension(dpy))
return NULL;
_image = XShmCreateImage(dpy, vis, visdepth, ZPixmap, NULL, &shminfo,
si.framebufferWidth, si.framebufferHeight);
if (!_image) return NULL;
shminfo.shmid = shmget(IPC_PRIVATE,
_image->bytes_per_line * _image->height,
IPC_CREAT|0777);
if (shminfo.shmid == -1) {
XDestroyImage(_image);
return NULL;
}
shminfo.shmaddr = _image->data = shmat(shminfo.shmid, 0, 0);
if (shminfo.shmaddr == (char *)-1) {
XDestroyImage(_image);
shmctl(shminfo.shmid, IPC_RMID, 0);
return NULL;
}
shminfo.readOnly = True;
oldXErrorHandler = XSetErrorHandler(ShmCreationXErrorHandler);
XShmAttach(dpy, &shminfo);
XSync(dpy, False);
XSetErrorHandler(oldXErrorHandler);
if (caughtShmError) {
XDestroyImage(_image);
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid, IPC_RMID, 0);
return NULL;
}
needShmCleanup = True;
fprintf(stderr,"Using shared memory PutImage\n");
return _image;
}
void undrawCursor() {
int x, y, w, h;
if ((imageIndex < 0) || !savedArea)
return;
getBoundingRectCursor(cursorX, cursorY, imageIndex,
&x, &y, &w, &h);
if ((w < 1) || (h < 1))
return;
CopyDataToScreenRaw(savedArea, x, y, w, h);
discardCursorSavedArea();
}
static void drawCursorImage() {
int x, y, w, h, pw, pixelsLeft, processingMask;
int skipLeft, skipRight;
PointerImage *pi = &pointerImages[imageIndex];
CARD8 *img = (CARD8*) pi->image;
CARD8 *imgEnd = &img[pi->len];
CARD8 *fb;
/* check whether the source image has ended (image broken) */
#define CHECK_IMG(x) if (&img[x] > imgEnd) goto imgError
/* check whether the end of the framebuffer has been reached (last line) */
#define CHECK_END() if ((wl == 0) && (h == 1)) return
/* skip x pixels in the source (x must be < pixelsLeft!) */
#define SKIP_IMG(x) if ((x > 0) && !processingMask) { \
CHECK_END(); \
img += pw * x; \
CHECK_IMG(0); \
}
/* skip x pixels in source and destination */
#define SKIP_PIXELS(x) { int wl = x; \
while (pixelsLeft <= wl) { \
wl -= pixelsLeft; \
SKIP_IMG(pixelsLeft); \
CHECK_END(); \
pixelsLeft = *(img++); \
CHECK_IMG(0); \
processingMask = processingMask ? 0 : 1; \
} \
pixelsLeft -= wl; \
SKIP_IMG(wl); \
}
if (!img)
return;
x = cursorX - pi->hotX;
y = cursorY - pi->hotY;
w = pi->w;
h = pi->h;
if (!rectsIntersect(x, y, w, h,
0, 0, si.framebufferWidth, si.framebufferHeight)) {
fprintf(stderr, "intersect abort\n");
return;
}
pw = myFormat.bitsPerPixel / 8;
processingMask = 1;
pixelsLeft = *(img++);
/* at this point everything is initialized for the macros */
/* skip/clip bottom lines */
if ((y+h) > si.framebufferHeight)
h = si.framebufferHeight - y;
/* Skip invisible top lines */
while (y < 0) {
SKIP_PIXELS(w);
y++;
h--;
}
/* calculate left/right clipping */
if (x < 0) {
skipLeft = -x;
w += x;
x = 0;
}
else
skipLeft = 0;
if ((x+w) > si.framebufferWidth) {
skipRight = (x+w) - si.framebufferWidth;
w = si.framebufferWidth - x;
}
else
skipRight = 0;
fb = (CARD8*) image->data + y * image->bytes_per_line + x * visbpp / 8;
/* Paint the thing */
while (h > 0) {
SKIP_PIXELS(skipLeft);
{
CARD8 *fbx = fb;
int wl = w;
while (pixelsLeft <= wl) {
wl -= pixelsLeft;
if ((pixelsLeft > 0) && !processingMask) {
int pl = pw * pixelsLeft;
CHECK_IMG(pl);
if (!appData.useBGR233)
memcpy(fbx, img, pl);
else
bgr233cpy(fbx, img, pixelsLeft);
img += pl;
}
CHECK_END();
fbx += pixelsLeft * visbpp / 8;
pixelsLeft = *(img++);
CHECK_IMG(0);
processingMask = processingMask ? 0 : 1;
}
pixelsLeft -= wl;
if ((wl > 0) && !processingMask) {
int pl = pw * wl;
CHECK_IMG(pl);
if (!appData.useBGR233)
memcpy(fbx, img, pl);
else
bgr233cpy(fbx, img, wl);
img += pl;
}
}
SKIP_PIXELS(skipRight);
fb += image->bytes_per_line;
h--;
}
return;
imgError:
fprintf(stderr, "Error in softcursor image %d\n", imageIndex);
pointerImages[imageIndex].set = 0;
}
static void discardCursorSavedArea() {
if (savedArea)
free(savedArea);
savedArea = 0;
}
static void saveCursorSavedArea() {
int x, y, w, h;
if (imageIndex < 0)
return;
getBoundingRectCursor(cursorX, cursorY, imageIndex,
&x, &y, &w, &h);
if ((w < 1) || (h < 1))
return;
discardCursorSavedArea();
savedArea = malloc(h*image->bytes_per_line);
if (!savedArea) {
fprintf(stderr,"malloc failed, saving cursor not possible\n");
exit(1);
}
CopyDataFromScreen(savedArea, x, y, w, h);
}
void drawCursor() {
saveCursorSavedArea();
drawCursorImage();
}
void getBoundingRectCursor(int cx, int cy, int _imageIndex,
int *x, int *y, int *w, int *h) {
int nx, ny, nw, nh;
if ((_imageIndex < 0) || !pointerImages[_imageIndex].set) {
*x = 0;
*y = 0;
*w = 0;
*h = 0;
return;
}
nx = cx - pointerImages[_imageIndex].hotX;
ny = cy - pointerImages[_imageIndex].hotY;
nw = pointerImages[_imageIndex].w;
nh = pointerImages[_imageIndex].h;
if (nx < 0) {
nw += nx;
nx = 0;
}
if (ny < 0) {
nh += ny;
ny = 0;
}
if ((nx+nw) > si.framebufferWidth)
nw = si.framebufferWidth - nx;
if ((ny+nh) > si.framebufferHeight)
nh = si.framebufferHeight - ny;
if ((nw <= 0) || (nh <= 0)) {
*x = 0;
*y = 0;
*w = 0;
*h = 0;
return;
}
*x = nx;
*y = ny;
*w = nw;
*h = nh;
}
static SoftCursorState getSoftCursorState(int x, int y, int w, int h) {
int cx, cy, cw, ch;
if (imageIndex < 0)
return SOFTCURSOR_UNAFFECTED;
getBoundingRectCursor(cursorX, cursorY, imageIndex,
&cx, &cy, &cw, &ch);
if ((cw == 0) || (ch == 0))
return SOFTCURSOR_UNAFFECTED;
if (!rectsIntersect(x, y, w, h, cx, cy, cw, ch))
return SOFTCURSOR_UNAFFECTED;
if (rectContains(x, y, w, h, cx, cy, cw, ch))
return SOFTCURSOR_UNDER;
else
return SOFTCURSOR_PART_UNDER;
}
int rectsIntersect(int x, int y, int w, int h,
int x2, int y2, int w2, int h2) {
if (x2 >= (x+w))
return 0;
if (y2 >= (y+h))
return 0;
if ((x2+w2) <= x)
return 0;
if ((y2+h2) <= y)
return 0;
return 1;
}
int rectContains(int outX, int outY, int outW, int outH,
int inX, int inY, int inW, int inH) {
if (inX < outX)
return 0;
if (inY < outY)
return 0;
if ((inX+inW) > (outX+outW))
return 0;
if ((inY+inH) > (outY+outH))
return 0;
return 1;
}
void rectsJoin(int *nx1, int *ny1, int *nw1, int *nh1,
int x2, int y2, int w2, int h2) {
int ox, oy, ow, oh;
ox = *nx1;
oy = *ny1;
ow = *nw1;
oh = *nh1;
if (x2 < ox) {
ow += ox - x2;
ox = x2;
}
if (y2 < oy) {
oh += oy - y2;
oy = y2;
}
if ((x2+w2) > (ox+ow))
ow = (x2+w2) - ox;
if ((y2+h2) > (oy+oh))
oh = (y2+h2) - oy;
*nx1 = ox;
*ny1 = oy;
*nw1 = ow;
*nh1 = oh;
}
XImage *
CreateShmZoomImage()
{
XImage *_image;
XErrorHandler oldXErrorHandler;
if (!XShmQueryExtension(dpy))
return NULL;
_image = XShmCreateImage(dpy, vis, visdepth, ZPixmap, NULL, &zoomshminfo,
si.framebufferWidth, si.framebufferHeight);
if (!_image) return NULL;
zoomshminfo.shmid = shmget(IPC_PRIVATE,
_image->bytes_per_line * _image->height,
IPC_CREAT|0777);
if (zoomshminfo.shmid == -1) {
XDestroyImage(_image);
return NULL;
}
zoomshminfo.shmaddr = _image->data = shmat(zoomshminfo.shmid, 0, 0);
if (zoomshminfo.shmaddr == (char *)-1) {
XDestroyImage(_image);
shmctl(zoomshminfo.shmid, IPC_RMID, 0);
return NULL;
}
zoomshminfo.readOnly = True;
oldXErrorHandler = XSetErrorHandler(ShmCreationXErrorHandler);
XShmAttach(dpy, &zoomshminfo);
XSync(dpy, False);
XSetErrorHandler(oldXErrorHandler);
if (caughtZoomShmError) {
XDestroyImage(_image);
shmdt(zoomshminfo.shmaddr);
shmctl(zoomshminfo.shmid, IPC_RMID, 0);
return NULL;
}
needZoomShmCleanup = True;
fprintf(stderr,"Using shared memory PutImage\n");
return _image;
}
/*
* DrawZoomedScreenRegionX11Thread
* Never call from any other desktop.c function, only for X11 thread
*/
void
DrawZoomedScreenRegionX11Thread(Window win, int zwidth, int zheight,
int x, int y, int width, int height) {
if (!image)
return;
if (zwidth > si.framebufferWidth)
zwidth = si.framebufferWidth;
if (zheight > si.framebufferHeight)
zheight = si.framebufferHeight;
if (!zoomActive) {
ZoomInit();
zoomActive = True;
}
if ((zoomWidth != zwidth) || (zoomHeight != zheight)) {
Surface src, dest;
zoomWidth = zwidth;
zoomHeight = zheight;
src.w = si.framebufferWidth;
src.h = si.framebufferHeight;
src.pitch = image->bytes_per_line;
src.pixels = image->data;
src.BytesPerPixel = visbpp / 8;
dest.w = zwidth;
dest.h = zheight;
dest.pitch = zoomImage->bytes_per_line;
dest.pixels = zoomImage->data;
dest.BytesPerPixel = visbpp / 8;
sge_transform(&src, &dest,
(float)dest.w/(float)src.w, (float)dest.h/(float)src.h,
0, 0);
if (useZoomShm)
XShmPutImage(dpy, win, gc, zoomImage, 0, 0, 0, 0, zwidth, zheight, False);
else
XPutImage(dpy, win, gc, zoomImage, 0, 0, 0, 0, zwidth, zheight);
return;
}
if (useZoomShm)
XShmPutImage(dpy, win, gc, zoomImage, x, y, x, y, width, height, False);
else
XPutImage(dpy, win, gc, zoomImage, x, y, x, y, width, height);
}
static void
ZoomInit()
{
if (zoomImage)
return;
zoomImage = CreateShmZoomImage();
if (!zoomImage) {
useZoomShm = False;
zoomImage = XCreateImage(dpy, vis, visdepth, ZPixmap, 0, NULL,
si.framebufferWidth, si.framebufferHeight,
BitmapPad(dpy), 0);
zoomImage->data = calloc(zoomImage->bytes_per_line * zoomImage->height, 1);
if (!zoomImage->data) {
fprintf(stderr,"malloc failed\n");
exit(1);
}
}
}
static void transformZoomSrc(int six, int siy, int siw, int sih,
int *dix, int *diy, int *diw, int *dih,
int srcW, int dstW, int srcH, int dstH) {
double sx, sy, sw, sh;
double dx, dy, dw, dh;
double wq, hq;
sx = six; sy = siy;
sw = siw; sh = sih;
wq = ((double)dstW) / (double) srcW;
hq = ((double)dstH) / (double) srcH;
dx = sx * wq;
dy = sy * hq;
dw = sw * wq;
dh = sh * hq;
*dix = dx;
*diy = dy;
*diw = dw+(dx-(int)dx)+0.5;
*dih = dh+(dy-(int)dy)+0.5;
}
static void transformZoomDst(int *six, int *siy, int *siw, int *sih,
int dix, int diy, int diw, int dih,
int srcW, int dstW, int srcH, int dstH) {
double sx, sy, sw, sh;
double dx, dy, dw, dh;
double wq, hq;
dx = dix; dy = diy;
dw = diw; dh = dih;
wq = ((double)dstW) / (double) srcW;
hq = ((double)dstH) / (double) srcH;
sx = dx / wq;
sy = dy / hq;
sw = dw / wq;
sh = dh / hq;
*six = sx;
*siy = sy;
*siw = sw+(sx-(int)sx)+0.5;
*sih = sh+(sy-(int)sy)+0.5;
}
static void ZoomSurfaceSrcCoords(int six, int siy, int siw, int sih,
int *dix, int *diy, int *diw, int *dih,
Surface * src, Surface * dst)
{
int dx, dy, dw, dh;
int sx, sy, sw, sh;
transformZoomSrc(six, siy, siw, sih,
&dx, &dy, &dw, &dh,
src->w, dst->w, src->h, dst->h);
dx-=2;
dy-=2;
dw+=4;
dh+=4;
if (dx < 0)
dx = 0;
if (dy < 0)
dy = 0;
if (dx+dw > dst->w)
dw = dst->w - dx;
if (dy+dh > dst->h)
dh = dst->h - dy;
transformZoomDst(&sx, &sy, &sw, &sh,
dx, dy, dw, dh,
src->w, dst->w, src->h, dst->h);
if (sx+sw > src->w)
sw = src->w - sx;
if (sy+sh > src->h)
sh = src->h - sy;
ZoomSurfaceCoords32(sx, sy, sw, sh, dx, dy, src, dst);
*dix = dx;
*diy = dy;
*diw = dw;
*dih = dh;
}
static void ZoomSurfaceCoords32(int sx, int sy, int sw, int sh,
int dx, int dy,
Surface * src, Surface * dst)
{
Surface s2;
s2 = *src;
s2.pixels = ((char*)s2.pixels) + (sx * s2.BytesPerPixel) + (sy * src->pitch);
s2.w = sw;
s2.h = sh;
sge_transform(&s2, dst,
(float)dst->w/(float)src->w, (float)dst->h/(float)src->h,
dx, dy);
}
#define sge_clip_xmin(pnt) 0
#define sge_clip_xmax(pnt) pnt->w
#define sge_clip_ymin(pnt) 0
#define sge_clip_ymax(pnt) pnt->h
/*==================================================================================
// Helper function to sge_transform()
// Returns the bounding box
//==================================================================================
*/
static void _calcRect(Surface *src, Surface *dst, float xscale, float yscale,
Uint16 qx, Uint16 qy,
Sint16 *xmin, Sint16 *ymin, Sint16 *xmax, Sint16 *ymax)
{
Sint16 x, y, rx, ry;
int i;
/* Clip to src surface */
Sint16 sxmin = sge_clip_xmin(src);
Sint16 sxmax = sge_clip_xmax(src);
Sint16 symin = sge_clip_ymin(src);
Sint16 symax = sge_clip_ymax(src);
Sint16 sx[5];
Sint16 sy[4];
/* We don't really need fixed-point here
* but why not? */
Sint32 ictx = (Sint32) (xscale * 8192.0);
Sint32 icty = (Sint32) (yscale * 8192.0);
sx[0] = sxmin;
sx[1] = sxmax;
sx[2] = sxmin;
sx[3] = sxmax;
sy[0] = symin;
sy[1] = symax;
sy[2] = symax;
sy[3] = symin;
/* Calculate the four corner points */
for(i=0; i<4; i++){
rx = sx[i];
ry = sy[i];
x = (Sint16)(((ictx*rx) >> 13) + qx);
y = (Sint16)(((icty*ry) >> 13) + qy);
if(i==0){
*xmax = *xmin = x;
*ymax = *ymin = y;
}else{
if(x>*xmax)
*xmax=x;
else if(x<*xmin)
*xmin=x;
if(y>*ymax)
*ymax=y;
else if(y<*ymin)
*ymin=y;
}
}
/* Better safe than sorry...*/
*xmin -= 1;
*ymin -= 1;
*xmax += 1;
*ymax += 1;
/* Clip to dst surface */
if( !dst )
return;
if( *xmin < sge_clip_xmin(dst) )
*xmin = sge_clip_xmin(dst);
if( *xmax > sge_clip_xmax(dst) )
*xmax = sge_clip_xmax(dst);
if( *ymin < sge_clip_ymin(dst) )
*ymin = sge_clip_ymin(dst);
if( *ymax > sge_clip_ymax(dst) )
*ymax = sge_clip_ymax(dst);
}
/*==================================================================================
** Scale by scale and place at position (qx,qy).
**
**
** Developed with the help from Terry Hancock (hancock@earthlink.net)
**
**==================================================================================*/
/* First we need some macros to handle different bpp
* I'm sorry about this...
*/
#define TRANSFORM(UintXX, DIV) \
Sint32 src_pitch=src->pitch/DIV; \
Sint32 dst_pitch=dst->pitch/DIV; \
UintXX *src_row = (UintXX *)src->pixels; \
UintXX *dst_row; \
\
for (y=ymin; y<ymax; y++){ \
dy = y - qy; \
\
sx = (Sint32)ctdx; /* Compute source anchor points */ \
sy = (Sint32)(cty*dy); \
\
/* Calculate pointer to dst surface */ \
dst_row = (UintXX *)dst->pixels + y*dst_pitch; \
\
for (x=xmin; x<xmax; x++){ \
rx=(Sint16)(sx >> 13); /* Convert from fixed-point */ \
ry=(Sint16)(sy >> 13); \
\
/* Make sure the source pixel is actually in the source image. */ \
if( (rx>=sxmin) && (rx<sxmax) && (ry>=symin) && (ry<symax) ) \
*(dst_row + x) = *(src_row + ry*src_pitch + rx); \
\
sx += ctx; /* Incremental transformations */ \
} \
}
/* Interpolated transform */
#define TRANSFORM_AA(UintXX, DIV) \
Sint32 src_pitch=src->pitch/DIV; \
Sint32 dst_pitch=dst->pitch/DIV; \
UintXX *src_row = (UintXX *)src->pixels; \
UintXX *dst_row; \
UintXX c1, c2, c3, c4;\
Uint32 R, G, B, A=0; \
UintXX Rmask = image->red_mask;\
UintXX Gmask = image->green_mask;\
UintXX Bmask = image->blue_mask;\
UintXX Amask = 0;\
Uint32 wx, wy;\
Uint32 p1, p2, p3, p4;\
\
/*
* Interpolation:
* We calculate the distances from our point to the four nearest pixels, d1..d4.
* d(a,b) = sqrt(a�+b�) ~= 0.707(a+b) (Pythagoras (Taylor) expanded around (0.5;0.5))
*
* 1 wx 2
* *-|-* (+ = our point at (x,y))
* | | | (* = the four nearest pixels)
* wy --+ | wx = float(x) - int(x)
* | | wy = float(y) - int(y)
* *---*
* 3 4
* d1 = d(wx,wy) d2 = d(1-wx,wy) d3 = d(wx,1-wy) d4 = d(1-wx,1-wy)
* We now want to weight each pixels importance - it's vicinity to our point:
* w1=d4 w2=d3 w3=d2 w4=d1 (Yes it works... just think a bit about it)
*
* If the pixels have the colors c1..c4 then our point should have the color
* c = (w1*c1 + w2*c2 + w3*c3 + w4*c4)/(w1+w2+w3+w4) (the weighted average)
* but w1+w2+w3+w4 = 4*0.707 so we might as well write it as
* c = p1*c1 + p2*c2 + p3*c3 + p4*c4 where p1..p4 = (w1..w4)/(4*0.707)
*
* But p1..p4 are fixed point so we can just divide the fixed point constant!
* 8192/(4*0.71) = 2897 and we can skip 0.71 too (the division will cancel it everywhere)
* 8192/4 = 2048
*
* 020102: I changed the fixed-point representation for the variables in the weighted average
* to 24.7 to avoid problems with 32bit colors. Everything else is still 18.13. This
* does however not solve the problem with 32bit RGBA colors...
*/\
\
Sint32 one = 2048>>6; /* 1 in Fixed-point */ \
Sint32 two = 2*2048>>6; /* 2 in Fixed-point */ \
\
for (y=ymin; y<ymax; y++){ \
dy = y - qy; \
\
sx = (Sint32)(ctdx); /* Compute source anchor points */ \
sy = (Sint32)(cty*dy); \
\
/* Calculate pointer to dst surface */ \
dst_row = (UintXX *)dst->pixels + y*dst_pitch; \
\
for (x=xmin; x<xmax; x++){ \
rx=(Sint16)(sx >> 13); /* Convert from fixed-point */ \
ry=(Sint16)(sy >> 13); \
\
/* Make sure the source pixel is actually in the source image. */ \
if( (rx>=sxmin) && (rx+1<sxmax) && (ry>=symin) && (ry+1<symax) ){ \
wx = (sx & 0x00001FFF) >>8; /* (float(x) - int(x)) / 4 */ \
wy = (sy & 0x00001FFF) >>8;\
\
p4 = wx+wy;\
p3 = one-wx+wy;\
p2 = wx+one-wy;\
p1 = two-wx-wy;\
\
c1 = *(src_row + ry*src_pitch + rx);\
c2 = *(src_row + ry*src_pitch + rx+1);\
c3 = *(src_row + (ry+1)*src_pitch + rx);\
c4 = *(src_row + (ry+1)*src_pitch + rx+1);\
\
/* Calculate the average */\
R = ((p1*(c1 & Rmask) + p2*(c2 & Rmask) + p3*(c3 & Rmask) + p4*(c4 & Rmask))>>7) & Rmask;\
G = ((p1*(c1 & Gmask) + p2*(c2 & Gmask) + p3*(c3 & Gmask) + p4*(c4 & Gmask))>>7) & Gmask;\
B = ((p1*(c1 & Bmask) + p2*(c2 & Bmask) + p3*(c3 & Bmask) + p4*(c4 & Bmask))>>7) & Bmask;\
if(Amask)\
A = ((p1*(c1 & Amask) + p2*(c2 & Amask) + p3*(c3 & Amask) + p4*(c4 & Amask))>>7) & Amask;\
\
*(dst_row + x) = R | G | B | A;\
} \
sx += ctx; /* Incremental transformations */ \
} \
}
void sge_transform(Surface *src, Surface *dst, float xscale, float yscale, Uint16 qx, Uint16 qy)
{
Sint32 dy, sx, sy;
Sint16 x, y, rx, ry;
Rect r;
Sint32 ctx, cty;
Sint16 xmin, xmax, ymin, ymax;
Sint16 sxmin, sxmax, symin, symax;
Sint32 dx, ctdx;
/* Here we use 18.13 fixed point integer math
// Sint32 should have 31 usable bits and one for sign
// 2^13 = 8192
*/
/* Check scales */
Sint32 maxint = (Sint32)(pow(2, sizeof(Sint32)*8 - 1 - 13)); /* 2^(31-13) */
r.x = r.y = r.w = r.h = 0;
if( xscale == 0 || yscale == 0)
return;
if( 8192.0/xscale > maxint )
xscale = (float)(8192.0/maxint);
else if( 8192.0/xscale < -maxint )
xscale = (float)(-8192.0/maxint);
if( 8192.0/yscale > maxint )
yscale = (float)(8192.0/maxint);
else if( 8192.0/yscale < -maxint )
yscale = (float)(-8192.0/maxint);
/* Fixed-point equivalents */
ctx = (Sint32)(8192.0/xscale);
cty = (Sint32)(8192.0/yscale);
/* Compute a bounding rectangle */
xmin=0; xmax=dst->w; ymin=0; ymax=dst->h;
_calcRect(src, dst, xscale, yscale,
qx, qy, &xmin, &ymin, &xmax, &ymax);
/* Clip to src surface */
sxmin = sge_clip_xmin(src);
sxmax = sge_clip_xmax(src);
symin = sge_clip_ymin(src);
symax = sge_clip_ymax(src);
/* Some terms in the transform are constant */
dx = xmin - qx;
ctdx = ctx*dx;
/* Use the correct bpp */
if( src->BytesPerPixel == dst->BytesPerPixel){
switch( src->BytesPerPixel ){
case 1: { /* Assuming 8-bpp */
TRANSFORM(Uint8, 1)
}
break;
case 2: { /* Probably 15-bpp or 16-bpp */
TRANSFORM_AA(Uint16, 2)
}
break;
case 4: { /* Probably 32-bpp */
TRANSFORM_AA(Uint32, 4)
}
break;
}
}
}
void freeDesktopResources() {
Cleanup();
if (image) {
XDestroyImage(image);
}
if (zoomImage) {
XDestroyImage(zoomImage);
}
if (savedArea)
free(savedArea);
if (gcInited) {
XFreeGC(dpy, gc);
XFreeGC(dpy, srcGC);
XFreeGC(dpy, dstGC);
}
caughtShmError = False;
needShmCleanup = False;
caughtZoomShmError = False;
needZoomShmCleanup = False;
gcInited = False;
image = NULL;
useShm = True;
zoomActive = False;
zoomImage = NULL;
useZoomShm = True;
savedArea = NULL;
}
/*
* ColorRectangle32
* Only used for debugging / visualizing output
*/
/*
static void
ColorRectangle32(XImage *img, CARD32 fg, int x, int y, int width, int height)
{
int i, h;
int scrWidthInBytes = img->bytes_per_line;
char *scr;
CARD32 *scr32;
if ((!img) || (!img->data))
return;
scr = (img->data + y * scrWidthInBytes + x * 4);
if (!CheckRectangle(x, y, width, height))
return;
for (h = 0; h < height; h++) {
scr32 = (CARD32*) scr;
for (i = 0; i < width; i++) {
CARD32 n = 0;
CARD32 p = scr32[i];
if (0xff & fg)
n |= ((( 0xff & p)+( 0xff & fg)) >> 2) & 0xff;
else
n |= (0xff & p);
if (0xff00 & fg)
n |= ((( 0xff00 & p)+( 0xff00 & fg)) >> 2) & 0xff00;
else
n |= (0xff00 & p);
if (0xff0000 & fg)
n |= (((0xff0000 & p)+(0xff0000 & fg)) >> 2) & 0xff0000;
else
n |= (0xff0000 & p);
scr32[i] = n;
}
scr += scrWidthInBytes;
}
}
*/
|