-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxfish.c
More file actions
1821 lines (1613 loc) · 46.8 KB
/
Copy pathxfish.c
File metadata and controls
1821 lines (1613 loc) · 46.8 KB
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
/*
* Original Author Unknow.
* 8/10/88 - Ported from X10 to X11R3 by:
Jonathan Greenblatt (jonnyg@rover.umd.edu)
* Cleaned up by Dave Lemke (lemke@sun.com)
* Ported to monocrome by Jonathan Greenblatt (jonnyg@rover.umd.edu)
* 05/02/1996 Added TrueColor support by TJ Phan (phan@aur.alcatel.com)
TODO:
Parameter parsing needs to be redone.
* Throughout 1991 improved for animation and color and multiple
fish types. Broke monocrome in the process.
Eric Bina (ebina@ncsa.uiuc.edu)
* 1992 added extra color remapping control options, as well as ways
to let the fish swim on the root window, or an image of the users
choice. Eric Bina (ebina@ncsa.uiuc.edu)
*/
#include <sys/types.h>
#ifndef hpux
#include <sys/time.h>
#else
#include <time.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifdef sgi
#define _BSD_SIGNALS
#endif
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <Imlib2.h>
#include "vroot.h"
#include "xfishy.h"
#include "bubbles.h"
#include "medcut.h"
/* constants are based on rand(3C) returning an integer between 0 and 32767 */
#if defined(ultrix) || defined(sun) || defined(linux) || defined(__APPLE__)
#define RAND_I_1_16 134217728
#define RAND_F_1_8 268435455.875
#define RAND_I_1_4 536870911
#define RAND_I_1_2 1073741823
#define RAND_I_3_4 1610612735
#define RAND_F_MAX 2147483647.0
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
#define RAND_I_1_16 (RAND_MAX>>4)
#define RAND_F_1_8 ((float)(RAND_MAX>>3))
#define RAND_I_1_4 (RAND_MAX>>2)
#define RAND_I_1_2 (RAND_MAX>>1)
#define RAND_I_3_4 ((RAND_MAX>>2)*3)
#define RAND_F_MAX ((float)RAND_MAX)
#else
#define RAND_I_1_16 2048
#define RAND_F_1_8 4096.0
#define RAND_I_1_4 8096
#define RAND_I_1_2 16384
#define RAND_I_3_4 24575
#define RAND_F_MAX 32767.0
#endif
extern unsigned char *ReadBitmap();
/* externals for pixmap and bimaps from xfishy.h */
/* typedefs for bubble and fish structures, also caddr_t (not used in X.h) */
typedef struct {
int x, y, s, erased, i;
} bubble;
typedef struct {
int x, y, d, frame, type, i;
} fish;
typedef unsigned char *caddrt;
/* bubble increment and yes check tables */
int binc[] = { 0, 64, 56, 48, 40, 32, 24, 16, 8 };
char *yess[] = { "yes", "Yes", "YES", "on", "On", "ON" };
char *pname, /* program name from argv[0] */
sname[64], /* host:display specification */
cname[64]; /* colorname specification */
char picname[256]; /* name of the background picture file */
int *Allocated; /* mark the used colors */
int AllocCnt; /* count number of colors used */
int mlimit = 0; /* num colors to median cut to. 0 = no limit */
int climit = 0; /* limit on color use. 0 = no limit */
int DoubleBuf = 0; /* Should we use double buffering */
int Overlap = 0; /* Should fish swim over each other */
int DoClipping = 0; /* Should clip masks be used. */
int blimit = 32, /* bubble limit */
flimit = 10, /* fish limit */
pmode = 1, /* pop mode, (1 for lower, 0 for raise) */
width, /* width of initial window in pixels */
height, /* height of initial window in pixels */
screen, /* Default screen of this display */
Init_B, *cmap; /* Initialize bubbles with random y value */
int Pwidth; /* width of background picture */
int Pheight; /* height of background picture */
int Pcnt; /* number of colors in background picture */
unsigned char *Pdata; /* data from background picture */
double rate = 0.2, /* update interval in seconds */
smooth = 0.2; /* smoothness increment multiplier */
bubble *binfo; /* bubble info structures, allocated
* dynamically */
fish *finfo; /* fish info structures, allocated dynamically */
Display *Dpy;
Window root_window;
XImage *xfishA[NUM_FISH][3]; /* fish pixmaps (1 is left-fish, 2 is
* right-fish) */
XImage *xfishB[NUM_FISH][3]; /* fish pixmaps (1 is left-fish, 2 is
* right-fish) */
Pixmap pfishA[NUM_FISH][3];
Pixmap pfishB[NUM_FISH][3];
Pixmap mfishA[NUM_FISH][3]; /* masking pixmaps for fish to use as */
Pixmap mfishB[NUM_FISH][3]; /* clipmasks */
Pixmap PicMap; /* pixmap for background picture */
Pixmap PixBuf; /* Pixmap buffer for double buffering */
Pixmap ClipBuf; /* Clipmask buffer for double buffering */
Pixmap xbubbles[9]; /* bubbles bitmaps (1 to 8, by size in pixels) */
Window wid; /* aqaurium window */
unsigned long white, black, bcolor;
Colormap colormap;
GC c0gc, cpgc; /* GCs to operateon the Clipmask buffer */
GC pgc;
GC gc, bgc;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Output desired error message and exit.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
msgdie(message)
char *message;
{
fprintf(stderr, "%s: %s\n", pname, message);
exit(1);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Set up program defaults, get X defaults, parse command line using getopts.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
parse(argc, argv)
int argc;
char **argv;
{
int c;
const char *display = getenv("DISPLAY");
extern int optind;
extern char *optarg;
extern double atof();
pname = argv[0];
if (display != NULL)
strncpy(sname, display, sizeof(sname) - 1);
strcpy(cname, "MediumAquamarine");
while ((c = getopt(argc, argv, "dDob:C:c:p:m:f:i:r:s")) != EOF) {
switch (c) {
case 'd':
DoClipping = 1;
break;
case 'D':
DoubleBuf = 1;
break;
case 'o':
Overlap = 1;
break;
case 'b':
blimit = atoi(optarg);
break;
case 'C':
climit = atoi(optarg);
break;
case 'm':
mlimit = atoi(optarg);
break;
case 'c':
strncpy(cname, optarg, sizeof(cname) - 1);
break;
case 'p':
strncpy(picname, optarg, sizeof(picname) - 1);
break;
case 'f':
flimit = atoi(optarg);
break;
case 'i':
smooth = atof(optarg);
break;
case 'r':
rate = atof(optarg);
break;
case 's':
pmode = 0;
break;
case '?':
fprintf(stderr, "usage: %s\n", pname);
fprintf(stderr, "\t\t[-c color] background color\n");
fprintf(stderr, "\t\t[-b limit] number of bubbles (default 32)\n");
fprintf(stderr, "\t\t[-f limit] number of fish (default 10)\n");
fprintf(stderr, "\t\t[-i mult] move interval (default 0.2)\n");
fprintf(stderr, "\t\t[-r rate] move frequency (default 0.2)\n");
fprintf(stderr, "\t\t[-m num] median cut to this many colors\n");
fprintf(stderr, "\t\t[-C num] use only this many color cells\n");
fprintf(stderr, "\t\t[-d] clip fish, swim on root window\n");
fprintf(stderr, "\t\t[-p file] fish swim on picture in file\n");
fprintf(stderr, "\t\t[host:display]\n");
exit(1);
}
}
if (optind < argc) {
char *display;
strncpy(sname, argv[optind], sizeof(sname) - 1);
display = (char *) malloc(strlen(sname) + 9);
snprintf(display, sizeof(display) - 1, "DISPLAY=%s", sname);
putenv(display);
}
}
void
erasefish(f, x, y, d)
fish *f;
int x, y, d;
{
/*
* for something as small as a bubble, it was never worth the
* effort of using clipmasks to only turn of the bubble itself, so
* we just clear the whole rectangle.
*/
XClearArea(Dpy, wid, x, y, rwidth[f->type], rheight[f->type], False);
#if 0
XGCValues gcv;
if (f->frame) {
gcv.foreground = cmap[0];
gcv.fill_style = FillTiled;
gcv.fill_style = FillSolid;
gcv.tile = pfishB[f->type][d];
gcv.ts_x_origin = f->x;
gcv.ts_y_origin = f->y;
gcv.clip_mask = mfishB[f->type][d];
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
XChangeGC(Dpy, gc, GCForeground | GCClipMask |
GCTile | GCTileStipXOrigin | GCTileStipYOrigin |
GCFillStyle | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyPlane(Dpy, mfishB[f->type][d], wid, gc, 0, 0,
rwidth[f->type], rheight[f->type], x, y, (unsigned long) 1);
} else {
gcv.foreground = cmap[0];
gcv.fill_style = FillTiled;
gcv.fill_style = FillSolid;
gcv.tile = pfishA[f->type][d];
gcv.ts_x_origin = f->x;
gcv.ts_y_origin = f->y;
gcv.clip_mask = mfishA[f->type][d];
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
XChangeGC(Dpy, gc, GCForeground | GCClipMask |
GCTile | GCTileStipXOrigin | GCTileStipYOrigin |
GCFillStyle | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyPlane(Dpy, mfishA[f->type][d], wid, gc, 0, 0,
rwidth[f->type], rheight[f->type], x, y, (unsigned long) 1);
}
#endif
}
/*
* Just places a fish. Normally this is all you need for animation, since
* placeing the fish places an entire rectangle which erases most of the old
* fish (the rest being cleaned up by the function that called putfish.
* If DoClipping is set, this function is only called when placing a new
* fish, otherwise newfish is called.
*/
void
putfish(f)
fish *f;
{
XGCValues gcv;
if (f->frame) {
/*
* If we have a pixmap of the fish use it, otherwise use
* the XImage of the fish. In reality we will never use
* the XImage since X dies if the pixmap create failed
*/
if (pfishA[f->type][f->d]) {
/*
* Clipping overrides background picture because
* the clipping prevents the drawing of any background
* anyway.
* DoClipping says just print a fish leaving the
* background unchanged.
* If there is a background picture, we use a buffer
* to prevent flashing, we combine the background
* picture and the fish, and then copy the
* whole rectangle in.
* Default is just copy in fish in with a background
* color.
*/
if (DoClipping) {
gcv.clip_mask = mfishA[f->type][f->d];
gcv.clip_x_origin = f->x;
gcv.clip_y_origin = f->y;
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, pfishA[f->type][f->d], wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
} else if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.ts_x_origin = -(f->x);
gcv.ts_y_origin = -(f->y);
gcv.clip_mask = None;
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask), &gcv);
XFillRectangle(Dpy, PixBuf, pgc, 0, 0, rwidth[f->type], rheight[f->type]);
gcv.clip_mask = mfishA[f->type][f->d];
gcv.clip_x_origin = 0;
gcv.clip_y_origin = 0;
XChangeGC(Dpy, pgc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, pfishA[f->type][f->d], PixBuf, pgc,
0, 0, rwidth[f->type], rheight[f->type], 0, 0);
XCopyArea(Dpy, PixBuf, wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
} else {
XCopyArea(Dpy, pfishA[f->type][f->d], wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
}
} else {
XPutImage(Dpy, wid, gc, xfishA[f->type][f->d], 0, 0,
f->x, f->y, rwidth[f->type], rheight[f->type]);
}
f->frame = 0;
} else {
/*
* same as the above, only for the second frame of animation
*/
if (pfishB[f->type][f->d]) {
if (DoClipping) {
gcv.clip_mask = mfishB[f->type][f->d];
gcv.clip_x_origin = f->x;
gcv.clip_y_origin = f->y;
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, pfishB[f->type][f->d], wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
} else if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.ts_x_origin = -(f->x);
gcv.ts_y_origin = -(f->y);
gcv.clip_mask = None;
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask), &gcv);
XFillRectangle(Dpy, PixBuf, pgc, 0, 0, rwidth[f->type], rheight[f->type]);
gcv.clip_mask = mfishB[f->type][f->d];
gcv.clip_x_origin = 0;
gcv.clip_y_origin = 0;
XChangeGC(Dpy, pgc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, pfishB[f->type][f->d], PixBuf, pgc,
0, 0, rwidth[f->type], rheight[f->type], 0, 0);
XCopyArea(Dpy, PixBuf, wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
} else {
XCopyArea(Dpy, pfishB[f->type][f->d], wid, gc,
0, 0, rwidth[f->type], rheight[f->type], f->x, f->y);
}
} else {
XPutImage(Dpy, wid, gc, xfishB[f->type][f->d], 0, 0,
f->x, f->y, rwidth[f->type], rheight[f->type]);
}
f->frame = 1;
}
}
/*
* This function can only be called if DoClipping is True. It is used to
* move a clipmasked fish. First the area under the fish is cleared,
* and then the new fish is masked in.
* The parameters x, y, amd d are from the old fish that is being
* erased before the new fish is drawn.
*/
void
movefish(f, x, y, d)
fish *f;
int x, y, d;
{
XGCValues gcv;
int bx, by, bw, bh;
/*
* If we are going to double buffer, we need to find the bounding
* rectangle of the overlap of the bounding rectangles of the old
* and the new fish.
*/
if (DoubleBuf) {
if (x < f->x) {
bx = x;
bw = f->x - x + rwidth[f->type];
} else {
bx = f->x;
bw = x - f->x + rwidth[f->type];
}
if (y < f->y) {
by = y;
bh = f->y - y + rheight[f->type];
} else {
by = f->y;
bh = y - f->y + rheight[f->type];
}
}
if (f->frame) {
/*
* If there is a pixmap use it.
* This branchis always taken since right now, if the pixmap
* allocation failed, the program dies.
*/
if (pfishA[f->type][f->d]) {
/*
* A pointless if, you now only come here if
* DoClipping is set, I've just been too lazy to
* clean up my code.
*/
if (DoClipping) {
/*
* Set up the masked gc for when we eventually
* draw the fish. Origin is different for
* whether we are drawing into the buffer
* or into the window
*/
gcv.clip_mask = mfishA[f->type][f->d];
if (DoubleBuf) {
gcv.clip_x_origin = f->x - bx;
gcv.clip_y_origin = f->y - by;
} else {
gcv.clip_x_origin = f->x;
gcv.clip_y_origin = f->y;
}
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
/*
* If we have a background picture we want to
* clear to that background, otherwise we just
* do an XCleararea, and let the root restore
* the background.
*/
if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.clip_mask = mfishB[f->type][d];
if (DoubleBuf) {
gcv.ts_x_origin = 0 - bx;
gcv.ts_y_origin = 0 - by;
gcv.clip_x_origin = x - bx;
gcv.clip_y_origin = y - by;
} else {
gcv.ts_x_origin = 0;
gcv.ts_y_origin = 0;
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
}
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask |
GCClipXOrigin | GCClipYOrigin), &gcv);
/*
* if bouble buffering we clear the buffer
* to the backgound picture, and then
* shape the clip buffer to the shape of
* the fish being erased.
*/
if (DoubleBuf) {
XFillRectangle(Dpy, PixBuf, pgc,
x - bx, y - by, rwidth[f->type], rheight[f->type]);
XFillRectangle(Dpy, ClipBuf, c0gc, 0, 0, 500, 500);
XCopyArea(Dpy, mfishB[f->type][d],
ClipBuf, cpgc, 0, 0,
rwidth[f->type], rheight[f->type], x - bx, y - by);
} else {
XFillRectangle(Dpy, wid, pgc, x, y, rwidth[f->type], rheight[f->type]);
}
} else {
XClearArea(Dpy, wid, x, y, rwidth[f->type], rheight[f->type], 0);
}
}
/*
* Now we just copy in the new fish with a clipmasked gc.
* But if we doublebuffered, we copy the new fish into
* the buffer, combine the new fishes clipmask in, and
* then mask the whole lot from the buffer to the window.
*/
if (DoubleBuf) {
XCopyArea(Dpy, pfishA[f->type][f->d], PixBuf, gc, 0, 0,
rwidth[f->type], rheight[f->type], f->x - bx, f->y - by);
XCopyArea(Dpy, mfishA[f->type][f->d], ClipBuf, cpgc,
0, 0, rwidth[f->type], rheight[f->type], f->x - bx, f->y - by);
gcv.clip_mask = ClipBuf;
gcv.clip_x_origin = bx;
gcv.clip_y_origin = by;
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, PixBuf, wid, gc, 0, 0, bw, bh, bx, by);
} else {
XCopyArea(Dpy, pfishA[f->type][f->d], wid, gc, 0, 0,
rwidth[f->type], rheight[f->type], f->x, f->y);
}
} else {
if (DoClipping) {
if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.ts_x_origin = 0;
gcv.ts_y_origin = 0;
gcv.clip_mask = mfishB[f->type][d];
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask |
GCClipXOrigin | GCClipYOrigin), &gcv);
XFillRectangle(Dpy, wid, pgc, x, y, rwidth[f->type], rheight[f->type]);
} else {
XClearArea(Dpy, wid, x, y, rwidth[f->type], rheight[f->type], 0);
}
}
XPutImage(Dpy, wid, gc, xfishA[f->type][f->d], 0, 0,
f->x, f->y, rwidth[f->type], rheight[f->type]);
}
f->frame = 0;
} else {
/*
* Same as above, only for the second frame of animation.
*/
if (pfishB[f->type][f->d]) {
if (DoClipping) {
gcv.clip_mask = mfishB[f->type][f->d];
if (DoubleBuf) {
gcv.clip_x_origin = f->x - bx;
gcv.clip_y_origin = f->y - by;
} else {
gcv.clip_x_origin = f->x;
gcv.clip_y_origin = f->y;
}
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.clip_mask = mfishA[f->type][d];
if (DoubleBuf) {
gcv.ts_x_origin = 0 - bx;
gcv.ts_y_origin = 0 - by;
gcv.clip_x_origin = x - bx;
gcv.clip_y_origin = y - by;
} else {
gcv.ts_x_origin = 0;
gcv.ts_y_origin = 0;
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
}
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask |
GCClipXOrigin | GCClipYOrigin), &gcv);
if (DoubleBuf) {
XFillRectangle(Dpy, PixBuf, pgc,
x - bx, y - by, rwidth[f->type], rheight[f->type]);
XFillRectangle(Dpy, ClipBuf, c0gc, 0, 0, 500, 500);
XCopyArea(Dpy, mfishA[f->type][d],
ClipBuf, cpgc, 0, 0,
rwidth[f->type], rheight[f->type], x - bx, y - by);
} else {
XFillRectangle(Dpy, wid, pgc, x, y, rwidth[f->type], rheight[f->type]);
}
} else {
XClearArea(Dpy, wid, x, y, rwidth[f->type], rheight[f->type], 0);
}
}
if (DoubleBuf) {
XCopyArea(Dpy, pfishB[f->type][f->d], PixBuf, gc, 0, 0,
rwidth[f->type], rheight[f->type], f->x - bx, f->y - by);
XCopyArea(Dpy, mfishB[f->type][f->d], ClipBuf, cpgc,
0, 0, rwidth[f->type], rheight[f->type], f->x - bx, f->y - by);
gcv.clip_mask = ClipBuf;
gcv.clip_x_origin = bx;
gcv.clip_y_origin = by;
XChangeGC(Dpy, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XCopyArea(Dpy, PixBuf, wid, gc, 0, 0, bw, bh, bx, by);
} else {
XCopyArea(Dpy, pfishB[f->type][f->d], wid, gc, 0, 0,
rwidth[f->type], rheight[f->type], f->x, f->y);
}
} else {
if (DoClipping) {
if (picname[0] != '\0') {
gcv.fill_style = FillTiled;
gcv.tile = PicMap;
gcv.ts_x_origin = 0;
gcv.ts_y_origin = 0;
gcv.clip_mask = mfishA[f->type][d];
gcv.clip_x_origin = x;
gcv.clip_y_origin = y;
XChangeGC(Dpy, pgc, (GCFillStyle |
GCTile | GCTileStipXOrigin |
GCTileStipYOrigin | GCClipMask |
GCClipXOrigin | GCClipYOrigin), &gcv);
XFillRectangle(Dpy, wid, pgc, x, y, rwidth[f->type], rheight[f->type]);
} else {
XClearArea(Dpy, wid, x, y, rwidth[f->type], rheight[f->type], 0);
}
}
XPutImage(Dpy, wid, gc, xfishB[f->type][f->d], 0, 0,
f->x, f->y, rwidth[f->type], rheight[f->type]);
}
f->frame = 1;
}
}
void
erasebubble(b, s)
bubble *b;
int s;
{
XClearArea(Dpy, wid, b->x, b->y, s, s, 0);
}
void
putbubble(b, s, c)
bubble *b;
int s;
unsigned long c;
{
XGCValues gcv;
gcv.foreground = c;
gcv.clip_mask = xbubbles[s];
gcv.clip_x_origin = b->x;
gcv.clip_y_origin = b->y;
XChangeGC(Dpy, bgc, GCForeground | GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
XFillRectangle(Dpy, wid, bgc, b->x, b->y, s, s);
}
/*
* Find the closest color by allocating it, or picking an already allocated
* color
*/
Visual(*visual_info) = NULL;
int r_mask, g_mask, b_mask;
int r_shift = 0, g_shift = 0, b_shift = 0;
int r_bits = 0, g_bits = 0, b_bits = 0;
void
FindColor(Dpy, colormap, colr)
Display *Dpy;
Colormap colormap;
XColor *colr;
{
int i, match;
double rd, gd, bd, dist, mindist;
int cindx;
XColor def_colrs[256];
int NumCells;
if (visual_info == NULL && DefaultDepth(Dpy, DefaultScreen(Dpy)) > 8) {
visual_info = DefaultVisual(Dpy, DefaultScreen(Dpy));
r_mask = visual_info->red_mask;
while (!(r_mask & 1)) {
r_mask >>= 1;
r_shift++;
}
while (r_mask & 1) {
r_mask >>= 1;
r_bits++;
}
g_mask = visual_info->green_mask;
while (!(g_mask & 1)) {
g_mask >>= 1;
g_shift++;
}
while (g_mask & 1) {
g_mask >>= 1;
g_bits++;
}
b_mask = visual_info->blue_mask;
while (!(b_mask & 1)) {
b_mask >>= 1;
b_shift++;
}
while (b_mask & 1) {
b_mask >>= 1;
b_bits++;
}
}
if (DefaultDepth(Dpy, DefaultScreen(Dpy)) > 8) {
colr->red >>= 16 - r_bits;
colr->green >>= 16 - g_bits;
colr->blue >>= 16 - b_bits;
colr->pixel = ((colr->red << r_shift) & visual_info->red_mask) |
((colr->green << g_shift) & visual_info->green_mask) |
((colr->blue << b_shift) & visual_info->blue_mask);
return;
}
if (AllocCnt < climit) {
match = XAllocColor(Dpy, colormap, colr);
} else {
match = 0;
}
if (match == 0) {
NumCells = DisplayCells(Dpy, DefaultScreen(Dpy));
for (i = 0; i < NumCells; i++) {
def_colrs[i].pixel = i;
}
XQueryColors(Dpy, colormap, def_colrs, NumCells);
mindist = 65536.0 * 65536.0;
cindx = colr->pixel;
for (i = 0; i < NumCells; i++) {
rd = (def_colrs[i].red - colr->red) / 256.0;
gd = (def_colrs[i].green - colr->green) / 256.0;
bd = (def_colrs[i].blue - colr->blue) / 256.0;
dist = (rd * rd * rd * rd) + (gd * gd * gd * gd) + (bd * bd * bd * bd);
if (dist < mindist) {
mindist = dist;
cindx = def_colrs[i].pixel;
}
}
colr->pixel = cindx;
colr->red = def_colrs[cindx].red;
colr->green = def_colrs[cindx].green;
colr->blue = def_colrs[cindx].blue;
} else {
if (Allocated[colr->pixel] == 0) {
Allocated[colr->pixel] = 1;
AllocCnt++;
}
}
}
int
ColorUsage(data, width, height, colrs)
unsigned char *data;
int width, height;
struct colr_data *colrs;
{
int mapping[256];
int i, size;
int cnt, indx;
unsigned char *ptr;
struct colr_data newcol[256];
for (i = 0; i < 256; i++) {
mapping[i] = -1;
}
size = width * height;
cnt = 0;
ptr = data;
for (i = 0; i < size; i++) {
indx = (int) *ptr;
if (mapping[indx] == -1) {
mapping[indx] = cnt;
newcol[cnt].red = colrs[indx].red;
newcol[cnt].green = colrs[indx].green;
newcol[cnt].blue = colrs[indx].blue;
cnt++;
}
ptr++;
}
ptr = data;
for (i = 0; i < size; i++) {
indx = (int) *ptr;
*ptr = (unsigned char) mapping[indx];
ptr++;
}
for (i = 0; i < cnt; i++) {
colrs[i].red = newcol[i].red;
colrs[i].green = newcol[i].green;
colrs[i].blue = newcol[i].blue;
}
for (i = cnt; i < 256; i++) {
colrs[i].red = 0;
colrs[i].green = 0;
colrs[i].blue = 0;
}
return (cnt);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Initialize colormap for background color and required fish colors.
The fish colors are coded in xfishy.h as a trio of tables.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
init_colormap()
{
int i, j, cnt;
int NumCells;
XColor hdef, edef;
struct colr_data *cdp;
struct colr_data colrs[256];
colormap = XDefaultColormap(Dpy, screen);
NumCells = DisplayCells(Dpy, DefaultScreen(Dpy));
Allocated = (int *) malloc(NumCells * sizeof(int));
for (i = 0; i < NumCells; i++) {
Allocated[i] = 0;
}
AllocCnt = 0;
if ((climit <= 0) || (climit > NumCells)) {
climit = NumCells;
}
Pcnt = 0;
if (picname[0] != '\0') {
Imlib_Image image = imlib_load_image(picname);
if (image == NULL) {
fprintf(stderr, "Cannot load image %s\n", picname);
picname[0] = 0;
} else {
imlib_context_set_image(image);
imlib_context_set_display(Dpy);
imlib_context_set_visual(DefaultVisual(Dpy, screen));
Pwidth = imlib_image_get_width();
Pheight = imlib_image_get_height();
DATA32 *image_data = imlib_image_get_data_for_reading_only();
Pdata = malloc(4 * Pwidth * Pheight);
for (i = 0; i < Pwidth * Pheight; i++) {
Pdata[4 * i] = image_data[i] & 0xFF;
Pdata[4 * i + 1] = image_data[i] & 0xFF00;
Pdata[4 * i + 2] = image_data[i] & 0xFF0000;
Pdata[4 * i + 3] = image_data[i] & 0xFF000000;
}
Pcnt = ColorUsage(Pdata, Pwidth, Pheight, colrs);
}
}
cnt = 0;
cnt += Pcnt;
for (i = 0; i < NUM_FISH; i++) {
cnt += rcolors[i];
}
cmap = (int *) malloc((cnt + 1) * sizeof(int));
XLookupColor(Dpy, colormap, cname, &hdef, &edef);
hdef.flags = DoRed | DoGreen | DoBlue;
FindColor(Dpy, colormap, &hdef);
cmap[0] = hdef.pixel;
if (mlimit > 0) {
MedianInit();
}
if (mlimit > 0) {
if (picname[0] != '\0') {
MedianCount(Pdata, Pwidth, Pheight, colrs);
}
for (j = 0; j < NUM_FISH; j++) {
int *rp, *gp, *bp;
cdp = (struct colr_data *) malloc(rcolors[j] * sizeof(struct colr_data));
rp = rreds[j];
gp = rgreens[j];
bp = rblues[j];
for (i = 0; i < rcolors[j]; i++) {
cdp[i].red = *rp++;
cdp[i].green = *gp++;
cdp[i].blue = *bp++;
}
MedianCount((unsigned char *) xfishRasterA[j],
(int) rwidth[j], (int) rheight[j], cdp);
free((char *) cdp);
}
MedianSplit(mlimit);
}
cnt = 1;
if (picname[0] != '\0') {
for (i = 0; i < Pcnt; i++) {
int rv, gv, bv;
rv = colrs[i].red;
gv = colrs[i].green;
bv = colrs[i].blue;
if (mlimit > 0) {
ConvertColor(&rv, &gv, &bv);
}
hdef.red = rv;
hdef.green = gv;
hdef.blue = bv;
hdef.flags = DoRed | DoGreen | DoBlue;
FindColor(Dpy, colormap, &hdef);
cmap[cnt] = hdef.pixel;
cnt++;
}
}
for (j = 0; j < NUM_FISH; j++) {
int *rp, *gp, *bp;
rp = rreds[j];
gp = rgreens[j];
bp = rblues[j];
for (i = 0; i < rcolors[j]; i++) {
int rv, gv, bv;
rv = *rp++;
gv = *gp++;
bv = *bp++;
if (mlimit > 0) {
ConvertColor(&rv, &gv, &bv);
}
hdef.red = rv;
hdef.green = gv;
hdef.blue = bv;
hdef.flags = DoRed | DoGreen | DoBlue;
FindColor(Dpy, colormap, &hdef);
cmap[cnt] = hdef.pixel;
if (i == rback[j]) {
cmap[cnt] = cmap[0];
}
cnt++;
}
}
bcolor = white;
}
/*
* Make am image of appropriate depth for display from image data.
*/
XImage *
MakeImage(data, width, height)
unsigned char *data;
int width, height;
{
int linepad, shiftnum;
int shiftstart, shiftstop, shiftinc;
int bytesperline;
int depth, temp;
int w, h;
XImage *newimage;
unsigned char *bit_data, *bitp, *datap;
depth = DefaultDepth(Dpy, DefaultScreen(Dpy));
if ((depth != 1) && (depth != 2) && (depth != 4) && (depth != 8)) {
fprintf(stderr, "Don't know how to format image for display of depth %d\n", depth);
exit(1);
}