/
scan.c
3676 lines (3231 loc) · 85.9 KB
/
scan.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
/*
Copyright (C) 2002-2010 Karl J. Runge <runge@karlrunge.com>
All rights reserved.
This file is part of x11vnc.
x11vnc 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.
x11vnc 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 x11vnc; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
or see <http://www.gnu.org/licenses/>.
In addition, as a special exception, Karl J. Runge
gives permission to link the code of its release of x11vnc with the
OpenSSL project's "OpenSSL" library (or with modified versions of it
that use the same license as the "OpenSSL" library), and distribute
the linked executables. You must obey the GNU General Public License
in all respects for all of the code used other than "OpenSSL". If you
modify this file, you may extend this exception to your version of the
file, but you are not obligated to do so. If you do not wish to do
so, delete this exception statement from your version.
*/
/* -- scan.c -- */
#include "x11vnc.h"
#include "xinerama.h"
#include "xwrappers.h"
#include "xdamage.h"
#include "xrandr.h"
#include "win_utils.h"
#include "8to24.h"
#include "screen.h"
#include "pointer.h"
#include "cleanup.h"
#include "unixpw.h"
#include "screen.h"
#include "macosx.h"
#include "userinput.h"
/*
* routines for scanning and reading the X11 display for changes, and
* for doing all the tile work (shm, etc).
*/
void initialize_tiles(void);
void free_tiles(void);
void shm_delete(XShmSegmentInfo *shm);
void shm_clean(XShmSegmentInfo *shm, XImage *xim);
void initialize_polling_images(void);
void scale_rect(double factor_x, double factor_y, int blend, int interpolate, int Bpp,
char *src_fb, int src_bytes_per_line, char *dst_fb, int dst_bytes_per_line,
int Nx, int Ny, int nx, int ny, int X1, int Y1, int X2, int Y2, int mark);
void scale_and_mark_rect(int X1, int Y1, int X2, int Y2, int mark);
void mark_rect_as_modified(int x1, int y1, int x2, int y2, int force);
int copy_screen(void);
int copy_snap(void);
void nap_sleep(int ms, int split);
void set_offset(void);
int scan_for_updates(int count_only);
void rotate_curs(char *dst_0, char *src_0, int Dx, int Dy, int Bpp);
void rotate_coords(int x, int y, int *xo, int *yo, int dxi, int dyi);
void rotate_coords_inverse(int x, int y, int *xo, int *yo, int dxi, int dyi);
static void set_fs_factor(int max);
static char *flip_ximage_byte_order(XImage *xim);
static int shm_create(XShmSegmentInfo *shm, XImage **ximg_ptr, int w, int h,
char *name);
static void create_tile_hint(int x, int y, int tw, int th, hint_t *hint);
static void extend_tile_hint(int x, int y, int tw, int th, hint_t *hint);
static void save_hint(hint_t hint, int loc);
static void hint_updates(void);
static void mark_hint(hint_t hint);
static int copy_tiles(int tx, int ty, int nt);
static int copy_all_tiles(void);
static int copy_all_tile_runs(void);
static int copy_tiles_backward_pass(void);
static int copy_tiles_additional_pass(void);
static int gap_try(int x, int y, int *run, int *saw, int along_x);
static int fill_tile_gaps(void);
static int island_try(int x, int y, int u, int v, int *run);
static int grow_islands(void);
static void blackout_regions(void);
static void nap_set(int tile_cnt);
static void nap_check(int tile_cnt);
static void ping_clients(int tile_cnt);
static int blackout_line_skip(int n, int x, int y, int rescan,
int *tile_count);
static int blackout_line_cmpskip(int n, int x, int y, char *dst, char *src,
int w, int pixelsize);
static int scan_display(int ystart, int rescan);
/* array to hold the hints: */
static hint_t *hint_list;
/* nap state */
int nap_ok = 0;
static int nap_diff_count = 0;
static int scan_count = 0; /* indicates which scan pattern we are on */
static int scan_in_progress = 0;
typedef struct tile_change_region {
/* start and end lines, along y, of the changed area inside a tile. */
unsigned short first_line, last_line;
short first_x, last_x;
/* info about differences along edges. */
unsigned short left_diff, right_diff;
unsigned short top_diff, bot_diff;
} region_t;
/* array to hold the tiles region_t-s. */
static region_t *tile_region;
/*
* setup tile numbers and allocate the tile and hint arrays:
*/
void initialize_tiles(void) {
ntiles_x = (dpy_x - 1)/tile_x + 1;
ntiles_y = (dpy_y - 1)/tile_y + 1;
ntiles = ntiles_x * ntiles_y;
tile_has_diff = (unsigned char *)
calloc((size_t) (ntiles * sizeof(unsigned char)), 1);
tile_has_xdamage_diff = (unsigned char *)
calloc((size_t) (ntiles * sizeof(unsigned char)), 1);
tile_row_has_xdamage_diff = (unsigned char *)
calloc((size_t) (ntiles_y * sizeof(unsigned char)), 1);
tile_tried = (unsigned char *)
calloc((size_t) (ntiles * sizeof(unsigned char)), 1);
tile_copied = (unsigned char *)
calloc((size_t) (ntiles * sizeof(unsigned char)), 1);
tile_blackout = (tile_blackout_t *)
calloc((size_t) (ntiles * sizeof(tile_blackout_t)), 1);
tile_region = (region_t *) calloc((size_t) (ntiles * sizeof(region_t)), 1);
tile_row = (XImage **)
calloc((size_t) ((ntiles_x + 1) * sizeof(XImage *)), 1);
tile_row_shm = (XShmSegmentInfo *)
calloc((size_t) ((ntiles_x + 1) * sizeof(XShmSegmentInfo)), 1);
/* there will never be more hints than tiles: */
hint_list = (hint_t *) calloc((size_t) (ntiles * sizeof(hint_t)), 1);
}
void free_tiles(void) {
if (tile_has_diff) {
free(tile_has_diff);
tile_has_diff = NULL;
}
if (tile_has_xdamage_diff) {
free(tile_has_xdamage_diff);
tile_has_xdamage_diff = NULL;
}
if (tile_row_has_xdamage_diff) {
free(tile_row_has_xdamage_diff);
tile_row_has_xdamage_diff = NULL;
}
if (tile_tried) {
free(tile_tried);
tile_tried = NULL;
}
if (tile_copied) {
free(tile_copied);
tile_copied = NULL;
}
if (tile_blackout) {
free(tile_blackout);
tile_blackout = NULL;
}
if (tile_region) {
free(tile_region);
tile_region = NULL;
}
if (tile_row) {
free(tile_row);
tile_row = NULL;
}
if (tile_row_shm) {
free(tile_row_shm);
tile_row_shm = NULL;
}
if (hint_list) {
free(hint_list);
hint_list = NULL;
}
}
/*
* silly function to factor dpy_y until fullscreen shm is not bigger than max.
* should always work unless dpy_y is a large prime or something... under
* failure fs_factor remains 0 and no fullscreen updates will be tried.
*/
static int fs_factor = 0;
static void set_fs_factor(int max) {
int f, fac = 1, n = dpy_y;
fs_factor = 0;
if ((bpp/8) * dpy_x * dpy_y <= max) {
fs_factor = 1;
return;
}
for (f=2; f <= 101; f++) {
while (n % f == 0) {
n = n / f;
fac = fac * f;
if ( (bpp/8) * dpy_x * (dpy_y/fac) <= max ) {
fs_factor = fac;
return;
}
}
}
}
static char *flip_ximage_byte_order(XImage *xim) {
char *order;
if (xim->byte_order == LSBFirst) {
order = "MSBFirst";
xim->byte_order = MSBFirst;
xim->bitmap_bit_order = MSBFirst;
} else {
order = "LSBFirst";
xim->byte_order = LSBFirst;
xim->bitmap_bit_order = LSBFirst;
}
return order;
}
/*
* set up an XShm image, or if not using shm just create the XImage.
*/
static int shm_create(XShmSegmentInfo *shm, XImage **ximg_ptr, int w, int h,
char *name) {
XImage *xim;
static int reported_flip = 0;
int db = 0;
shm->shmid = -1;
shm->shmaddr = (char *) -1;
*ximg_ptr = NULL;
if (nofb) {
return 1;
}
X_LOCK;
if (! using_shm || xform24to32 || raw_fb) {
/* we only need the XImage created */
xim = XCreateImage_wr(dpy, default_visual, depth, ZPixmap,
0, NULL, w, h, raw_fb ? 32 : BitmapPad(dpy), 0);
X_UNLOCK;
if (xim == NULL) {
rfbErr("XCreateImage(%s) failed.\n", name);
if (quiet) {
fprintf(stderr, "XCreateImage(%s) failed.\n",
name);
}
return 0;
}
if (db) fprintf(stderr, "shm_create simple %d %d\t%p %s\n", w, h, (void *)xim, name);
xim->data = (char *) malloc(xim->bytes_per_line * xim->height);
if (xim->data == NULL) {
rfbErr("XCreateImage(%s) data malloc failed.\n", name);
if (quiet) {
fprintf(stderr, "XCreateImage(%s) data malloc"
" failed.\n", name);
}
return 0;
}
if (flip_byte_order) {
char *order = flip_ximage_byte_order(xim);
if (! reported_flip && ! quiet) {
rfbLog("Changing XImage byte order"
" to %s\n", order);
reported_flip = 1;
}
}
*ximg_ptr = xim;
return 1;
}
if (! dpy) {
X_UNLOCK;
return 0;
}
xim = XShmCreateImage_wr(dpy, default_visual, depth, ZPixmap, NULL,
shm, w, h);
if (xim == NULL) {
rfbErr("XShmCreateImage(%s) failed.\n", name);
if (quiet) {
fprintf(stderr, "XShmCreateImage(%s) failed.\n", name);
}
X_UNLOCK;
return 0;
}
*ximg_ptr = xim;
#if HAVE_XSHM
shm->shmid = shmget(IPC_PRIVATE,
xim->bytes_per_line * xim->height, IPC_CREAT | 0600);
if (shm->shmid == -1) {
rfbErr("shmget(%s) failed.\n", name);
rfbLogPerror("shmget");
XDestroyImage(xim);
*ximg_ptr = NULL;
X_UNLOCK;
return 0;
}
shm->shmaddr = xim->data = (char *) shmat(shm->shmid, 0, 0);
if (shm->shmaddr == (char *)-1) {
rfbErr("shmat(%s) failed.\n", name);
rfbLogPerror("shmat");
XDestroyImage(xim);
*ximg_ptr = NULL;
shmctl(shm->shmid, IPC_RMID, 0);
shm->shmid = -1;
X_UNLOCK;
return 0;
}
shm->readOnly = False;
if (! XShmAttach_wr(dpy, shm)) {
rfbErr("XShmAttach(%s) failed.\n", name);
XDestroyImage(xim);
*ximg_ptr = NULL;
shmdt(shm->shmaddr);
shm->shmaddr = (char *) -1;
shmctl(shm->shmid, IPC_RMID, 0);
shm->shmid = -1;
X_UNLOCK;
return 0;
}
#endif
X_UNLOCK;
return 1;
}
void shm_delete(XShmSegmentInfo *shm) {
#if HAVE_XSHM
if (getenv("X11VNC_SHM_DEBUG")) fprintf(stderr, "shm_delete: %p\n", (void *) shm);
if (shm != NULL && shm->shmaddr != (char *) -1) {
shmdt(shm->shmaddr);
}
if (shm != NULL && shm->shmid != -1) {
shmctl(shm->shmid, IPC_RMID, 0);
}
if (shm != NULL) {
shm->shmaddr = (char *) -1;
shm->shmid = -1;
}
#else
if (!shm) {}
#endif
}
void shm_clean(XShmSegmentInfo *shm, XImage *xim) {
int db = 0;
if (db) fprintf(stderr, "shm_clean: called: %p\n", (void *)xim);
X_LOCK;
#if HAVE_XSHM
if (shm != NULL && shm->shmid != -1 && dpy) {
if (db) fprintf(stderr, "shm_clean: XShmDetach_wr\n");
XShmDetach_wr(dpy, shm);
}
#endif
if (xim != NULL) {
if (! raw_fb_back_to_X) { /* raw_fb hack */
if (xim->bitmap_unit != -1) {
if (db) fprintf(stderr, "shm_clean: XDestroyImage %p\n", (void *)xim);
XDestroyImage(xim);
} else {
if (xim->data) {
if (db) fprintf(stderr, "shm_clean: free xim->data %p %p\n", (void *)xim, (void *)(xim->data));
free(xim->data);
xim->data = NULL;
}
}
}
xim = NULL;
}
X_UNLOCK;
shm_delete(shm);
}
void initialize_polling_images(void) {
int i, MB = 1024 * 1024;
/* set all shm areas to "none" before trying to create any */
scanline_shm.shmid = -1;
scanline_shm.shmaddr = (char *) -1;
scanline = NULL;
fullscreen_shm.shmid = -1;
fullscreen_shm.shmaddr = (char *) -1;
fullscreen = NULL;
snaprect_shm.shmid = -1;
snaprect_shm.shmaddr = (char *) -1;
snaprect = NULL;
for (i=1; i<=ntiles_x; i++) {
tile_row_shm[i].shmid = -1;
tile_row_shm[i].shmaddr = (char *) -1;
tile_row[i] = NULL;
}
/* the scanline (e.g. 1280x1) shared memory area image: */
if (! shm_create(&scanline_shm, &scanline, dpy_x, 1, "scanline")) {
clean_up_exit(1);
}
/*
* the fullscreen (e.g. 1280x1024/fs_factor) shared memory area image:
* (we cut down the size of the shm area to try avoid and shm segment
* limits, e.g. the default 1MB on Solaris)
*/
if (UT.sysname && strstr(UT.sysname, "Linux")) {
set_fs_factor(10 * MB);
} else {
set_fs_factor(1 * MB);
}
if (fs_frac >= 1.0) {
fs_frac = 1.1;
fs_factor = 0;
}
if (! fs_factor) {
rfbLog("warning: fullscreen updates are disabled.\n");
} else {
if (! shm_create(&fullscreen_shm, &fullscreen, dpy_x,
dpy_y/fs_factor, "fullscreen")) {
clean_up_exit(1);
}
}
if (use_snapfb) {
if (! fs_factor) {
rfbLog("warning: disabling -snapfb mode.\n");
use_snapfb = 0;
} else if (! shm_create(&snaprect_shm, &snaprect, dpy_x,
dpy_y/fs_factor, "snaprect")) {
clean_up_exit(1);
}
}
/*
* for copy_tiles we need a lot of shared memory areas, one for
* each possible run length of changed tiles. 32 for 1024x768
* and 40 for 1280x1024, etc.
*/
tile_shm_count = 0;
for (i=1; i<=ntiles_x; i++) {
if (! shm_create(&tile_row_shm[i], &tile_row[i], tile_x * i,
tile_y, "tile_row")) {
if (i == 1) {
clean_up_exit(1);
}
rfbLog("shm: Error creating shared memory tile-row for"
" len=%d,\n", i);
rfbLog("shm: reverting to -onetile mode. If this"
" problem persists\n");
rfbLog("shm: try using the -onetile or -noshm options"
" to limit\n");
rfbLog("shm: shared memory usage, or run ipcrm(1)"
" to manually\n");
rfbLog("shm: delete unattached shm segments.\n");
single_copytile_count = i;
single_copytile = 1;
}
tile_shm_count++;
if (single_copytile && i >= 1) {
/* only need 1x1 tiles */
break;
}
}
if (verbose) {
if (using_shm && ! xform24to32) {
rfbLog("created %d tile_row shm polling images.\n",
tile_shm_count);
} else {
rfbLog("created %d tile_row polling images.\n",
tile_shm_count);
}
}
}
/*
* A hint is a rectangular region built from 1 or more adjacent tiles
* glued together. Ultimately, this information in a single hint is sent
* to libvncserver rather than sending each tile separately.
*/
static void create_tile_hint(int x, int y, int tw, int th, hint_t *hint) {
int w = dpy_x - x;
int h = dpy_y - y;
if (w > tw) {
w = tw;
}
if (h > th) {
h = th;
}
hint->x = x;
hint->y = y;
hint->w = w;
hint->h = h;
}
static void extend_tile_hint(int x, int y, int tw, int th, hint_t *hint) {
int w = dpy_x - x;
int h = dpy_y - y;
if (w > tw) {
w = tw;
}
if (h > th) {
h = th;
}
if (hint->x > x) { /* extend to the left */
hint->w += hint->x - x;
hint->x = x;
}
if (hint->y > y) { /* extend upward */
hint->h += hint->y - y;
hint->y = y;
}
if (hint->x + hint->w < x + w) { /* extend to the right */
hint->w = x + w - hint->x;
}
if (hint->y + hint->h < y + h) { /* extend downward */
hint->h = y + h - hint->y;
}
}
static void save_hint(hint_t hint, int loc) {
/* simply copy it to the global array for later use. */
hint_list[loc].x = hint.x;
hint_list[loc].y = hint.y;
hint_list[loc].w = hint.w;
hint_list[loc].h = hint.h;
}
/*
* Glue together horizontal "runs" of adjacent changed tiles into one big
* rectangle change "hint" to be passed to the vnc machinery.
*/
static void hint_updates(void) {
hint_t hint;
int x, y, i, n, ty, th, tx, tw;
int hint_count = 0, in_run = 0;
hint.x = hint.y = hint.w = hint.h = 0;
for (y=0; y < ntiles_y; y++) {
for (x=0; x < ntiles_x; x++) {
n = x + y * ntiles_x;
if (tile_has_diff[n]) {
ty = tile_region[n].first_line;
th = tile_region[n].last_line - ty + 1;
tx = tile_region[n].first_x;
tw = tile_region[n].last_x - tx + 1;
if (tx < 0) {
tx = 0;
tw = tile_x;
}
if (! in_run) {
create_tile_hint( x * tile_x + tx,
y * tile_y + ty, tw, th, &hint);
in_run = 1;
} else {
extend_tile_hint( x * tile_x + tx,
y * tile_y + ty, tw, th, &hint);
}
} else {
if (in_run) {
/* end of a row run of altered tiles: */
save_hint(hint, hint_count++);
in_run = 0;
}
}
}
if (in_run) { /* save the last row run */
save_hint(hint, hint_count++);
in_run = 0;
}
}
for (i=0; i < hint_count; i++) {
/* pass update info to vnc: */
mark_hint(hint_list[i]);
}
}
/*
* kludge, simple ceil+floor for non-negative doubles:
*/
#define CEIL(x) ( (double) ((int) (x)) == (x) ? \
(double) ((int) (x)) : (double) ((int) (x) + 1) )
#define FLOOR(x) ( (double) ((int) (x)) )
/*
* Scaling:
*
* For shrinking, a destination (scaled) pixel will correspond to more
* than one source (i.e. main fb) pixel. Think of an x-y plane made with
* graph paper. Each unit square in the graph paper (i.e. collection of
* points (x,y) such that N < x < N+1 and M < y < M+1, N and M integers)
* corresponds to one pixel in the unscaled fb. There is a solid
* color filling the inside of such a square. A scaled pixel has width
* 1/scale_fac, e.g. for "-scale 3/4" the width of the scaled pixel
* is 1.333. The area of this scaled pixel is 1.333 * 1.333 (so it
* obviously overlaps more than one source pixel, each which have area 1).
*
* We take the weight an unscaled pixel (source) contributes to a
* scaled pixel (destination) as simply proportional to the overlap area
* between the two pixels. One can then think of the value of the scaled
* pixel as an integral over the portion of the graph paper it covers.
* The thing being integrated is the color value of the unscaled source.
* That color value is constant over a graph paper square (source pixel),
* and changes discontinuously from one unit square to the next.
*
Here is an example for -scale 3/4, the solid lines are the source pixels
(graph paper unit squares), while the dotted lines denote the scaled
pixels (destination pixels):
0 1 4/3 2 8/3 3 4=12/3
|---------|--.------|------.--|---------|.
| | . | . | |.
| A | . B | . | |.
| | . | . | |.
| | . | . | |.
1 |---------|--.------|------.--|---------|.
4/3|.........|.........|.........|.........|.
| | . | . | |.
| C | . D | . | |.
| | . | . | |.
2 |---------|--.------|------.--|---------|.
| | . | . | |.
| | . | . | |.
8/3|.........|.........|.........|.........|.
| | . | . | |.
3 |---------|--.------|------.--|---------|.
So we see the first scaled pixel (0 < x < 4/3 and 0 < y < 4/3) mostly
overlaps with unscaled source pixel "A". The integration (averaging)
weights for this scaled pixel are:
A 1
B 1/3
C 1/3
D 1/9
*
* The Red, Green, and Blue color values must be averaged over separately
* otherwise you can get a complete mess (except in solid regions),
* because high order bits are averaged differently from the low order bits.
*
* So the algorithm is roughly:
*
* - Given as input a rectangle in the unscaled source fb with changes,
* find the rectangle of pixels this affects in the scaled destination fb.
*
* - For each of the affected scaled (dest) pixels, determine all of the
* unscaled (source) pixels it overlaps with.
*
* - Average those unscaled source values together, weighted by the area
* overlap with the destination pixel. Average R, G, B separately.
*
* - Take this average value and convert to a valid pixel value if
* necessary (e.g. rounding, shifting), and then insert it into the
* destination framebuffer as the pixel value.
*
* - On to the next destination pixel...
*
* ========================================================================
*
* For expanding, e.g. -scale 1.1 (which we don't think people will do
* very often... or at least so we hope, the framebuffer can become huge)
* the situation is reversed and the destination pixel is smaller than a
* "graph paper" unit square (source pixel). Some destination pixels
* will be completely within a single unscaled source pixel.
*
* What we do here is a simple 4 point interpolation scheme:
*
* Let P00 be the source pixel closest to the destination pixel but with
* x and y values less than or equal to those of the destination pixel.
* (for simplicity, think of the upper left corner of a pixel defining the
* x,y location of the pixel, the center would work just as well). So it
* is the source pixel immediately to the upper left of the destination
* pixel. Let P10 be the source pixel one to the right of P00. Let P01
* be one down from P00. And let P11 be one down and one to the right
* of P00. They form a 2x2 square we will interpolate inside of.
*
* Let V00, V10, V01, and V11 be the color values of those 4 source
* pixels. Let dx be the displacement along x the destination pixel is
* from P00. Note: 0 <= dx < 1 by definition of P00. Similarly let
* dy be the displacement along y. The weighted average for the
* interpolation is:
*
* V_ave = V00 * (1 - dx) * (1 - dy)
* + V10 * dx * (1 - dy)
* + V01 * (1 - dx) * dy
* + V11 * dx * dy
*
* Note that the weights (1-dx)*(1-dy) + dx*(1-dy) + (1-dx)*dy + dx*dy
* automatically add up to 1. It is also nice that all the weights are
* positive (unsigned char stays unsigned char). The above formula can
* be motivated by doing two 1D interpolations along x:
*
* VA = V00 * (1 - dx) + V10 * dx
* VB = V01 * (1 - dx) + V11 * dx
*
* and then interpolating VA and VB along y:
*
* V_ave = VA * (1 - dy) + VB * dy
*
* VA
* v |<-dx->|
* -- V00 ------ V10
* dy | |
* -- | o...|... "o" denotes the position of the desired
* ^ | . | . destination pixel relative to the P00
* | . | . source pixel.
* V10 ----.- V11 .
* ........
* |
* VB
*
*
* Of course R, G, B averages are done separately as in the shrinking
* case. This gives reasonable results, and the implementation for
* shrinking can simply be used with different choices for weights for
* the loop over the 4 pixels.
*/
void scale_rect(double factor_x, double factor_y, int blend, int interpolate, int Bpp,
char *src_fb, int src_bytes_per_line, char *dst_fb, int dst_bytes_per_line,
int Nx, int Ny, int nx, int ny, int X1, int Y1, int X2, int Y2, int mark) {
/*
* Notation:
* "i" an x pixel index in the destination (scaled) framebuffer
* "j" a y pixel index in the destination (scaled) framebuffer
* "I" an x pixel index in the source (un-scaled, i.e. main) framebuffer
* "J" a y pixel index in the source (un-scaled, i.e. main) framebuffer
*
* Similarly for nx, ny, Nx, Ny, etc. Lowercase: dest, Uppercase: source.
*/
int i, j, i1, i2, j1, j2; /* indices for scaled fb (dest) */
int I, J, I1, I2, J1, J2; /* indices for main fb (source) */
double w, wx, wy, wtot; /* pixel weights */
double x1, y1, x2, y2; /* x-y coords for destination pixels edges */
double dx, dy; /* size of destination pixel */
double ddx=0, ddy=0; /* for interpolation expansion */
char *src, *dest; /* pointers to the two framebuffers */
unsigned short us = 0;
unsigned char uc = 0;
unsigned int ui = 0;
int use_noblend_shortcut = 1;
int shrink; /* whether shrinking or expanding */
static int constant_weights = -1, mag_int = -1;
static int last_Nx = -1, last_Ny = -1, cnt = 0;
static double last_factor = -1.0;
int b, k;
double pixave[4]; /* for averaging pixel values */
if (factor_x <= 1.0 && factor_y <= 1.0) {
shrink = 1;
} else {
shrink = 0;
}
/*
* N.B. width and height (real numbers) of a scaled pixel.
* both are > 1 (e.g. 1.333 for -scale 3/4)
* they should also be equal but we don't assume it.
*
* This new way is probably the best we can do, take the inverse
* of the scaling factor to double precision.
*/
dx = 1.0/factor_x;
dy = 1.0/factor_y;
/*
* There is some speedup if the pixel weights are constant, so
* let's special case these.
*
* If scale = 1/n and n divides Nx and Ny, the pixel weights
* are constant (e.g. 1/2 => equal on 2x2 square).
*/
if (factor_x != last_factor || Nx != last_Nx || Ny != last_Ny) {
constant_weights = -1;
mag_int = -1;
last_Nx = Nx;
last_Ny = Ny;
last_factor = factor_x;
}
if (constant_weights < 0 && factor_x != factor_y) {
constant_weights = 0;
mag_int = 0;
} else if (constant_weights < 0) {
int n = 0;
constant_weights = 0;
mag_int = 0;
for (i = 2; i<=128; i++) {
double test = ((double) 1)/ i;
double diff, eps = 1.0e-7;
diff = factor_x - test;
if (-eps < diff && diff < eps) {
n = i;
break;
}
}
if (! blend || ! shrink || interpolate) {
;
} else if (n != 0) {
if (Nx % n == 0 && Ny % n == 0) {
static int didmsg = 0;
if (mark && ! didmsg) {
didmsg = 1;
rfbLog("scale_and_mark_rect: using "
"constant pixel weight speedup "
"for 1/%d\n", n);
}
constant_weights = 1;
}
}
n = 0;
for (i = 2; i<=32; i++) {
double test = (double) i;
double diff, eps = 1.0e-7;
diff = factor_x - test;
if (-eps < diff && diff < eps) {
n = i;
break;
}
}
if (! blend && factor_x > 1.0 && n) {
mag_int = n;
}
}
if (mark && factor_x > 1.0 && blend) {
/*
* kludge: correct for interpolating blurring leaking
* up or left 1 destination pixel.
*/
if (X1 > 0) X1--;
if (Y1 > 0) Y1--;
}
/*
* find the extent of the change the input rectangle induces in
* the scaled framebuffer.
*/
/* Left edges: find largest i such that i * dx <= X1 */
i1 = FLOOR(X1/dx);
/* Right edges: find smallest i such that (i+1) * dx >= X2+1 */
i2 = CEIL( (X2+1)/dx ) - 1;
/* To be safe, correct any overflows: */
i1 = nfix(i1, nx);
i2 = nfix(i2, nx) + 1; /* add 1 to make a rectangle upper boundary */
/* Repeat above for y direction: */
j1 = FLOOR(Y1/dy);
j2 = CEIL( (Y2+1)/dy ) - 1;
j1 = nfix(j1, ny);
j2 = nfix(j2, ny) + 1;
/*
* special case integer magnification with no blending.
* vision impaired magnification usage is interested in this case.
*/
if (mark && ! blend && mag_int && Bpp != 3) {
int jmin, jmax, imin, imax;
/* outer loop over *source* pixels */
for (J=Y1; J < Y2; J++) {
jmin = J * mag_int;
jmax = jmin + mag_int;
for (I=X1; I < X2; I++) {
/* extract value */
src = src_fb + J*src_bytes_per_line + I*Bpp;
if (Bpp == 4) {
ui = *((unsigned int *)src);
} else if (Bpp == 2) {
us = *((unsigned short *)src);
} else if (Bpp == 1) {
uc = *((unsigned char *)src);
}
imin = I * mag_int;
imax = imin + mag_int;
/* inner loop over *dest* pixels */
for (j=jmin; j<jmax; j++) {
dest = dst_fb + j*dst_bytes_per_line + imin*Bpp;
for (i=imin; i<imax; i++) {
if (Bpp == 4) {
*((unsigned int *)dest) = ui;
} else if (Bpp == 2) {
*((unsigned short *)dest) = us;
} else if (Bpp == 1) {
*((unsigned char *)dest) = uc;
}
dest += Bpp;
}
}
}
}
goto markit;
}
/* set these all to 1.0 to begin with */
wx = 1.0;
wy = 1.0;
w = 1.0;
/*
* Loop over destination pixels in scaled fb:
*/
for (j=j1; j<j2; j++) {
y1 = j * dy; /* top edge */
if (y1 > Ny - 1) {
/* can go over with dy = 1/scale_fac */
y1 = Ny - 1;
}
y2 = y1 + dy; /* bottom edge */
/* Find main fb indices covered by this dest pixel: */
J1 = (int) FLOOR(y1);
J1 = nfix(J1, Ny);
if (shrink && ! interpolate) {
J2 = (int) CEIL(y2) - 1;
J2 = nfix(J2, Ny);
} else {
J2 = J1 + 1; /* simple interpolation */
ddy = y1 - J1;
}
/* destination char* pointer: */
dest = dst_fb + j*dst_bytes_per_line + i1*Bpp;
for (i=i1; i<i2; i++) {
x1 = i * dx; /* left edge */
if (x1 > Nx - 1) {