This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvcolor.c
1556 lines (1196 loc) · 41 KB
/
xvcolor.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
/*
* xvcolor.c - color allocation/sorting/freeing code
*
* Author: John Bradley, University of Pennsylvania
*
* Contains:
* void SortColormap()
* void ColorCompress8(trans)
* void AllocColors()
* void FreeColors()
* Status xvAllocColor()
* void xvFreeColors()
* void ApplyEditColor();
* int MakeStdCmaps();
* void MakeBrowCmap();
* void ChangeCmapMode();
*/
#include "copyright.h"
#include "xv.h"
static void allocROColors PARM((void));
static void allocRWColors PARM((void));
static void putECfirst PARM((void));
static void diverseOrder PARM((byte *, byte *, byte *, int, byte *));
static void freeStdCmaps PARM((void));
static int highbit PARM((unsigned long));
static char stdCmapSuccess[80];
/************************************************/
/* structure and routine used in SortColormap() */
/************************************************/
typedef struct thing {
byte r,g,b, n; /* actual value of color + alignment */
int oldindex; /* its index in the old colormap */
int use; /* # of pixels of this color */
int mindist; /* min distance to a selected color */
} CMAPENT;
/***********************************/
void SortColormap(pic, pwide, phigh, pnumcols, rmap,gmap,bmap, order, trans)
byte *pic, *rmap, *gmap, *bmap, *order, *trans;
int pwide, phigh, *pnumcols;
{
/* operates on 8-bit images. sorts the colormap into 'best' order
* 'order' is the 'best' order to allocate the colors. 'trans' is a
* transformation to be done to pic, cpic, and epic (in PIC8 mode) to
* compress the colormap
*/
byte *p;
int i, j, mdist, entry, d, hist[256], ncols;
static CMAPENT c[256], c1[256], *cp, *cj, *ck;
/* init some stuff */
for (i=0; i<256; i++) order[i]=i;
/* initialize histogram and compute it */
for (i=0; i<256; i++) hist[i]=0;
for (i=pwide*phigh, p=pic; i; i--, p++) hist[*p]++;
if (DEBUG>1) {
fprintf(stderr,"%s: Desired colormap\n",cmd);
for (i=0; i<256; i++)
if (hist[i]) fprintf(stderr,"(%3d %02x,%02x,%02x %d)\n",
i,rmap[i],gmap[i],bmap[i], hist[i]);
fprintf(stderr,"\n\n");
}
/* put the actually-used colors into the 'c' array in the order they occur
also, while we're at it, calculate ncols, and close up gaps in
colortable */
for (i=ncols=0; i<256; i++) {
if (hist[i]) {
rmap[ncols] = rmap[i];
gmap[ncols] = gmap[i];
bmap[ncols] = bmap[i];
trans[i] = (byte) ncols;
cp = &c[ncols];
cp->r = rmap[i]; cp->g = gmap[i]; cp->b = bmap[i];
cp->use = hist[i]; cp->oldindex = ncols;
cp->mindist = 1000000; /* 255^2 * 3 = 195075 */
ncols++;
}
}
/* find most-used color, put that in c1[0] */
entry = -1; mdist = -1;
for (i=0; i<ncols; i++) {
if (c[i].use > mdist) { mdist = c[i].use; entry=i; }
}
xvbcopy((char *) &c[entry], (char *) &c1[0], sizeof(CMAPENT));
c[entry].use = 0; /* dealt with */
/* sort rest of colormap. Half of the entries are allocated on the
basis of distance from already allocated colors, and half on the
basis of usage. (NB: 'taxicab' distance is used throughout this file.)
Mod: pick first 10 colors based on maximum distance. pick remaining
colors half by distance and half by usage -- JHB
To obtain O(n^2) performance, we keep each unselected color
(in c[], with use>0) marked with the minimum distance to any of
the selected colors (in c1[]). Each time we select a color, we
can update the minimum distances in O(n) time.
mod by Tom Lane [email protected] */
for (i=1; i<ncols; i++) {
int ckR, ckG, ckB;
/* Get RGB of color last selected and choose selection method */
ck = &c1[i-1]; /* point to just-selected color */
ckR = ck->r; ckG = ck->g; ckB = ck->b;
/* Now find the i'th most different color */
if (i&1 || i<10) {
/* select the unused color that has the greatest mindist */
entry = -1; mdist = -1;
for (j=0, cj=c; j<ncols; j++,cj++) {
if (cj->use) { /* this color has not been marked already */
/* update mindist */
d = (cj->r - ckR)*(cj->r - ckR) +
(cj->g - ckG)*(cj->g - ckG) +
(cj->b - ckB)*(cj->b - ckB);
if (cj->mindist > d) cj->mindist = d;
if (cj->mindist > mdist) { mdist = cj->mindist; entry = j; }
}
}
}
else {
/* select the unused color that has the greatest usage */
entry = -1; mdist = -1;
for (j=0, cj=c; j<ncols; j++,cj++) {
if (cj->use) { /* this color has not been marked already */
/* update mindist */
d = (cj->r - ckR)*(cj->r - ckR) +
(cj->g - ckG)*(cj->g - ckG) +
(cj->b - ckB)*(cj->b - ckB);
if (cj->mindist > d) cj->mindist = d;
if (cj->use > mdist) { mdist = cj->use; entry = j; }
}
}
}
/* c[entry] is the next color to put in the map. do so */
xvbcopy((char *) &c[entry], (char *) &c1[i], sizeof(CMAPENT));
c[entry].use = 0;
}
for (i=0; i<ncols; i++) order[i] = (byte) c1[i].oldindex;
if (DEBUG>1) {
fprintf(stderr,"%s: result of sorting colormap\n",cmd);
for (i=0; i<ncols; i++)
fprintf(stderr,"(%3d %02x,%02x,%02x) ",i,rmap[i],gmap[i],bmap[i]);
fprintf(stderr,"\n\n");
fprintf(stderr,"%s: allocation order table\n",cmd);
for (i=0; i<ncols; i++)
fprintf(stderr,"order[%d] = -> %d\n", i, order[i]);
fprintf(stderr,"\n");
}
*pnumcols = ncols;
}
/***********************************/
void ColorCompress8(trans)
byte *trans;
{
/* modify pic,cpic,epic to reflect new (compressed) colormap */
int i;
byte *p, j;
for (i=pWIDE*pHIGH, p=pic; i; i--, p++) { j = trans[*p]; *p = j; }
if (cpic && cpic != pic) {
for (i=cWIDE*cHIGH, p=cpic; i; i--, p++) { j = trans[*p]; *p = j; }
}
if (epic && epic != cpic) {
for (i=eWIDE*eHIGH, p=epic; i; i--, p++) { j = trans[*p]; *p = j; }
}
}
/***********************************/
void AllocColors()
{
int i;
nfcols = 0;
if (ncols == 0) {
for (i=0; i<numcols; i++) {
rdisp[i] = rMap[i];
gdisp[i] = gMap[i];
bdisp[i] = bMap[i];
}
SetISTR(ISTR_COLOR,"Dithering with 'black' & 'white'.");
SetISTR(ISTR_COLOR2,"");
rwthistime = 0;
RedrawCMap();
return;
}
if (colorMapMode == CM_STDCMAP) {
/* map desired image colors to closest standard colors */
if (picType == PIC24 &&
(theVisual->class == TrueColor || theVisual->class == DirectColor)) {
SetISTR(ISTR_COLOR,"Using %s visual.",
(theVisual->class == TrueColor) ? "TrueColor" : "DirectColor");
SetISTR(ISTR_COLOR2,"");
}
else {
SetISTR(ISTR_COLOR,"Using %s colormap.",
(haveStdCmap == STD_111 ? "2x2x2" :
haveStdCmap == STD_222 ? "4x4x4" :
haveStdCmap == STD_232 ? "4x8x4" :
haveStdCmap == STD_666 ? "6x6x6" : "8x8x4"));
if (ncols>0) SetISTR(ISTR_COLOR2,stdCmapSuccess);
else SetISTR(ISTR_COLOR2,"Dithering with 'black' & 'white'.");
}
rwthistime = 0;
for (i=0; i<numcols; i++) {
int i332;
i332 = ((int)rMap[i]&0xe0) | (((int)gMap[i]&0xe0)>>3) |
(((int)bMap[i]&0xc0)>>6);
cols[i] = stdcols[i332];
rdisp[i] = stdrdisp[i332];
gdisp[i] = stdgdisp[i332];
bdisp[i] = stdbdisp[i332];
}
}
else if (allocMode == AM_READWRITE) allocRWColors();
else allocROColors();
RedrawCMap();
}
/********************************/
void FreeColors()
{
int i;
/* frees all colors allocated by 'AllocColors()'. Doesn't touch stdcmap */
/* Note: might be called multiple times. Must not free colors once it
has done so */
if (LocalCmap) {
XSetWindowAttributes xswa;
xswa.colormap = None;
XChangeWindowAttributes(theDisp,mainW,CWColormap,&xswa);
if (cmapInGam) XChangeWindowAttributes(theDisp,gamW,CWColormap,&xswa);
XFreeColormap(theDisp,LocalCmap);
LocalCmap = 0;
nfcols = 0;
}
else {
for (i=0; i<nfcols; i++)
xvFreeColors(theDisp, theCmap, &freecols[i], 1, 0L);
nfcols = 0;
XFlush(theDisp);
}
}
/***********************************/
static void allocROColors()
{
int i, j, c, unique, p2alloc;
Colormap cmap;
XColor defs[256];
XColor ctab[256];
int failed[256];
int dc;
unique = p2alloc = 0;
rwthistime = 0;
/* FIRST PASS COLOR ALLOCATION:
for each color in the 'desired colormap', try to get it via
xvAllocColor(). If for any reason it fails, mark that pixel
'unallocated' and worry about it later. Repeat. */
/* attempt to allocate first ncols entries in colormap
note: On displays with less than 8 bits per RGB gun, it's quite
possible that different colors in the original picture will be
mapped to the same color on the screen. X does this for you
silently. However, this is not-desirable for this application,
because when I say 'allocate me 32 colors' I want it to allocate
32 different colors, not 32 instances of the same 4 shades... */
for (i=0; i<256; i++) failed[i] = 1;
cmap = (LocalCmap) ? LocalCmap : theCmap;
for (i=0; i<numcols && unique<ncols; i++) {
c = colAllocOrder[i];
if (mono) {
int intens = MONO(rMap[c], gMap[c], bMap[c]);
defs[c].red = defs[c].green = defs[c].blue = intens<<8;
}
else {
defs[c].red = rMap[c]<<8;
defs[c].green = gMap[c]<<8;
defs[c].blue = bMap[c]<<8;
}
defs[c].flags = DoRed | DoGreen | DoBlue;
if (!(colorMapMode==CM_OWNCMAP && cmap==theCmap && CMAPVIS(theVisual))
&& xvAllocColor(theDisp,cmap,&defs[c])) {
unsigned long pixel, *fcptr;
pixel = cols[c] = defs[c].pixel;
rdisp[c] = defs[c].red >> 8;
gdisp[c] = defs[c].green >> 8;
bdisp[c] = defs[c].blue >> 8;
failed[c]= 0;
/* see if the newly allocated color is new and different */
for (j=0, fcptr=freecols; j<nfcols && *fcptr!=pixel; j++,fcptr++);
if (j==nfcols) unique++;
fc2pcol[nfcols] = c;
freecols[nfcols++] = pixel;
}
else {
/* the allocation failed. If we want 'perfect' color, and we haven't
already created our own colormap, we'll want to do so */
if ((colorMapMode == CM_PERFECT || colorMapMode == CM_OWNCMAP)
&& !LocalCmap && CMAPVIS(theVisual)) {
LocalCmap = XCreateColormap(theDisp, vrootW, theVisual, AllocNone);
if (LocalCmap) { /* succeeded, presumably */
/* free all colors that were allocated, and try again with the
new colormap. This is necessary because 'XCopyColormapAndFree()'
has the unpleasant side effect of freeing up the various
colors I need for the control panel, etc. */
for (i=0; i<nfcols; i++)
xvFreeColors(theDisp, theCmap, &freecols[i], 1, 0L);
if (mainW && !useroot) XSetWindowColormap(theDisp,mainW, LocalCmap);
if (mainW && !useroot && cmapInGam)
XSetWindowColormap(theDisp,gamW, LocalCmap);
cmap = LocalCmap;
/* redo ALL allocation requests */
for (i=0; i<256; i++) failed[i] = 1;
nfcols = unique = 0;
i = -1;
}
}
else {
/* either we don't care about perfect color, or we do care, have
allocated our own colormap, and have STILL run out of colors
(possible, even on an 8 bit display), just mark pixel as
unallocated. We'll deal with it later */
failed[c] = 1;
}
}
} /* FIRST PASS */
if (nfcols==numcols) {
if (numcols != unique)
SetISTR(ISTR_COLOR,"Got all %d colors. (%d unique)", numcols,
unique);
else
SetISTR(ISTR_COLOR,"Got all %d colors.", numcols);
SetISTR(ISTR_COLOR2,"");
return;
}
/* SECOND PASS COLOR ALLOCATION:
Allocating 'exact' colors failed. Now try to allocate 'closest'
colors.
Read entire X colormap (or first 256 entries) in from display.
for each unallocated pixel, find the closest color that actually
is in the X colormap. Try to allocate that color (read only).
If that fails, the THIRD PASS will deal with it */
SetISTR(ISTR_COLOR,"Got %d of %d colors. (%d unique)",
nfcols,numcols,unique);
/* read entire colormap (or first 256 entries) into 'ctab' */
dc = (ncells<256) ? ncells : 256;
if (dc>0) { /* only do SECOND PASS if there IS a colormap to read */
for (i=0; i<dc; i++) ctab[i].pixel = (unsigned long) i;
XQueryColors(theDisp, cmap, ctab, dc);
for (i=0; i<numcols && unique<ncols; i++) {
c = colAllocOrder[i];
if (failed[c]) { /* an unallocated pixel */
int d, mdist, close;
int rd, gd, bd, ri, gi, bi;
mdist = 1000000; close = -1;
ri = rMap[c]; gi = gMap[c]; bi = bMap[c];
for (j=0; j<dc; j++) {
rd = ri - (ctab[j].red >>8);
gd = gi - (ctab[j].green>>8);
bd = bi - (ctab[j].blue >>8);
d = rd*rd + gd*gd + bd*bd;
if (d<mdist) { mdist=d; close=j; }
}
if (close<0) FatalError("This Can't Happen! (How reassuring.)");
if (xvAllocColor(theDisp, cmap, &ctab[close])) {
xvbcopy((char *) &ctab[close], (char *) &defs[c], sizeof(XColor));
failed[c]= 0;
cols[c] = ctab[close].pixel;
rdisp[c] = ctab[close].red >> 8;
gdisp[c] = ctab[close].green >> 8;
bdisp[c] = ctab[close].blue >> 8;
fc2pcol[nfcols] = c;
freecols[nfcols++] = cols[c];
p2alloc++;
unique++;
}
}
}
}
/* THIRD PASS COLOR ALLOCATION:
We've alloc'ed all the colors we can. Now, we have to map any
remaining unalloced pixels into either the colors that we DID get */
for (i=0; i<numcols; i++) {
c = colAllocOrder[i];
if (failed[c]) { /* an unallocated pixel */
int d, k, mdist, close;
int rd,gd,bd, ri,gi,bi;
mdist = 1000000; close = -1;
ri = rMap[c]; gi = gMap[c]; bi = bMap[c];
/* search the alloc'd colors */
for (j=0; j<nfcols; j++) {
k = fc2pcol[j];
rd = ri - (defs[k].red >>8);
gd = gi - (defs[k].green>>8);
bd = bi - (defs[k].blue >>8);
d = rd*rd + gd*gd + bd*bd;
if (d<mdist) { mdist=d; close=k; }
}
if (close<0) FatalError("Pass3: Failed to alloc ANY colors!\n");
xvbcopy((char *) &defs[close], (char *) &defs[c], sizeof(XColor));
failed[c]= 0;
cols[c] = defs[c].pixel;
rdisp[c] = defs[c].red >> 8;
gdisp[c] = defs[c].green >> 8;
bdisp[c] = defs[c].blue >> 8;
}
} /* THIRD PASS */
if (p2alloc) SetISTR(ISTR_COLOR2,"Got %d 'close' color%s.",
p2alloc, (p2alloc>1) ? "s" : "");
}
/***********************************/
static void allocRWColors()
{
int i,j,c, failed[256];
Colormap cmap;
XColor defs[256];
rwthistime = 1;
cmap = (LocalCmap) ? LocalCmap : theCmap;
for (i=0; i<numcols; i++) failed[colAllocOrder[i]] = 1;
for (i=0; i<numcols && i<ncols; i++) {
unsigned long pmr[1], pix[1];
c = colAllocOrder[i];
if (cellgroup[c]) {
int n;
/* this color is part of a group. see if its group's
been seen already, and if so, skip this */
for (n=0; n<i && cellgroup[c] != cellgroup[colAllocOrder[n]]; n++);
if (n<i) { /* found one */
cols[c] = cols[colAllocOrder[n]];
rwpc2pc[c] = colAllocOrder[n];
failed[c] = 0;
continue;
}
}
if (!(colorMapMode==CM_OWNCMAP && cmap==theCmap && CMAPVIS(theVisual)) &&
XAllocColorCells(theDisp, cmap, False, pmr, 0, pix, 1)) {
defs[c].pixel = cols[c] = pix[0];
failed[c] = 0;
if (mono) {
int intens = MONO(rMap[c], gMap[c], bMap[c]);
defs[c].red = defs[c].green = defs[c].blue = intens<<8;
}
else {
defs[c].red = rMap[c]<<8;
defs[c].green = gMap[c]<<8;
defs[c].blue = bMap[c]<<8;
}
defs[c].flags = DoRed | DoGreen | DoBlue;
rdisp[c] = rMap[c];
gdisp[c] = gMap[c];
bdisp[c] = bMap[c];
fc2pcol[nfcols] = c;
rwpc2pc[c] = c;
freecols[nfcols++] = pix[0];
}
else {
if ((colorMapMode == CM_PERFECT || colorMapMode == CM_OWNCMAP)
&& !LocalCmap && CMAPVIS(theVisual)) {
LocalCmap = XCreateColormap(theDisp, vrootW, theVisual, AllocNone);
/* free all colors that were allocated, and try again with the
new colormap. This is necessary because 'XCopyColormapAndFree()'
has the unpleasant side effect of freeing up the various
colors I need for the control panel, etc. */
for (i=0; i<nfcols; i++)
xvFreeColors(theDisp, theCmap, &freecols[i], 1, 0L);
if (mainW && !useroot) XSetWindowColormap(theDisp,mainW, LocalCmap);
if (mainW && !useroot && cmapInGam)
XSetWindowColormap(theDisp,gamW, LocalCmap);
cmap = LocalCmap;
/* redo ALL allocation requests */
for (i=0; i<numcols; i++) failed[colAllocOrder[i]] = 1;
nfcols = 0;
i = -1;
}
else failed[c] = 1;
}
} /* for (i=0; ... */
if (nfcols==numcols) {
SetISTR(ISTR_COLOR,"Got all %d colors.", numcols);
SetISTR(ISTR_COLOR2,"");
}
else {
/* Failed to allocate all colors in picture. Map remaining desired
colors into closest allocated desired colors */
if (nfcols==0 && !LocalCmap) {
char tstr[128], *tmp;
const char *foo = "No r/w cells available. Using r/o color.";
tmp = GetISTR(ISTR_WARNING);
if (strlen(tmp) > (size_t) 0) sprintf(tstr, "%s %s", tmp, foo);
else sprintf(tstr, "%s", foo);
SetISTR(ISTR_WARNING,tstr);
allocROColors();
return;
}
SetISTR(ISTR_COLOR,"Got %d of %d colors.", nfcols,numcols);
for (i=0; i<numcols; i++) {
c = colAllocOrder[i];
if (failed[c]) { /* an unallocated pixel */
int k, d, mdist, close;
int rd, gd, bd, ri, gi, bi;
mdist = 1000000; close = -1;
ri = rMap[c]; gi = gMap[c]; bi = bMap[c];
for (j=0; j<nfcols; j++) {
k = fc2pcol[j];
rd = ri - (defs[k].red >>8);
gd = gi - (defs[k].green>>8);
bd = bi - (defs[k].blue >>8);
d = rd*rd + gd*gd + bd*bd;
if (d<mdist) { mdist=d; close=k; }
}
if (close<0) FatalError("This Can't Happen! (How reassuring.)");
xvbcopy((char *) &defs[close], (char *) &defs[c], sizeof(XColor));
failed[c]= 0;
cols[c] = defs[c].pixel;
rdisp[c] = defs[c].red >> 8;
gdisp[c] = defs[c].green >> 8;
bdisp[c] = defs[c].blue >> 8;
rwpc2pc[c] = close;
}
}
}
/* load up the allocated colorcells */
for (i=0; i<nfcols; i++) {
j = fc2pcol[i];
defs[j].pixel = freecols[i];
if (mono) {
int intens = MONO(rMap[j], gMap[j], bMap[j]);
defs[j].red = defs[j].green = defs[j].blue = intens<<8;
}
else {
defs[j].red = rMap[j]<<8;
defs[j].green = gMap[j]<<8;
defs[j].blue = bMap[j]<<8;
}
defs[j].flags = DoRed | DoGreen | DoBlue;
XStoreColor(theDisp, cmap, &defs[j]);
}
}
/*******************************************************/
/* 24/32-bit TrueColor display color 'allocation' code */
/*******************************************************/
static int highbit(ul)
unsigned long ul;
{
/* returns position of highest set bit in 'ul' as an integer (0-31),
or -1 if none */
int i; unsigned long hb;
hb = 0x8000; hb = (hb<<16); /* hb = 0x80000000UL */
for (i=31; ((ul & hb) == 0) && i>=0; i--, ul<<=1);
return i;
}
Status xvAllocColor(dp, cm, cdef)
Display *dp;
Colormap cm;
XColor *cdef;
{
if (theVisual->class == TrueColor || theVisual->class == DirectColor) {
unsigned long r, g, b, rmask, gmask, bmask, origr, origg, origb;
int rshift, gshift, bshift;
/* shift r,g,b so that high bit of 16-bit color specification is
* aligned with high bit of r,g,b-mask in visual,
* AND each component with its mask,
* and OR the three components together
*/
origr = r = cdef->red; origg = g = cdef->green; origb = b = cdef->blue;
if (theVisual->class == DirectColor) {
/* r,g,b are 16-bit values */
int maplen = theVisual->map_entries;
if (maplen>256) maplen = 256;
rshift = 15 - highbit((unsigned long) (maplen-1));
r = (u_long) directConv[(r>>rshift) & 0xff];
g = (u_long) directConv[(g>>rshift) & 0xff];
b = (u_long) directConv[(b>>rshift) & 0xff];
/* shift the bits back up */
r = r << rshift;
g = g << rshift;
b = b << rshift;
}
rmask = theVisual->red_mask;
gmask = theVisual->green_mask;
bmask = theVisual->blue_mask;
rshift = 15 - highbit(rmask);
gshift = 15 - highbit(gmask);
bshift = 15 - highbit(bmask);
/* shift the bits around */
if (rshift<0) r = r << (-rshift);
else r = r >> rshift;
if (gshift<0) g = g << (-gshift);
else g = g >> gshift;
if (bshift<0) b = b << (-bshift);
else b = b >> bshift;
r = r & rmask;
g = g & gmask;
b = b & bmask;
cdef->pixel = r | g | b;
/* put 'exact' colors into red,green,blue fields */
/* shift the bits BACK to where they were, now that they've been masked */
if (rshift<0) r = r >> (-rshift);
else r = r << rshift;
if (gshift<0) g = g >> (-gshift);
else g = g << gshift;
if (bshift<0) b = b >> (-bshift);
else b = b << bshift;
cdef->red = r; cdef->green = g; cdef->blue = b;
if (DEBUG > 1) {
fprintf(stderr,
"xvAlloc: col=%04lx,%04lx,%04lx -> exact=%04x,%04x,%04x\n",
origr, origg, origb, cdef->red, cdef->green, cdef->blue);
fprintf(stderr,
" mask=%04lx,%04lx,%04lx pix=%08lx\n",
rmask, gmask, bmask, cdef->pixel);
}
return 1;
}
else {
return (XAllocColor(dp,cm,cdef));
}
}
void xvFreeColors(dp, cm, pixels, npixels, planes)
Display *dp;
Colormap cm;
unsigned long *pixels;
int npixels;
unsigned long planes;
{
if (theVisual->class != TrueColor && theVisual->class != DirectColor)
XFreeColors(dp, cm, pixels, npixels, planes);
}
/********************************/
void ApplyEditColor(regroup)
int regroup;
{
int i, j;
/* if regroup is set, we *must* do a full realloc, as the cols[] array
isn't correct anymore. (cell groupings changed) */
ApplyECctrls(); /* set {r,g,b}cmap[editColor] based on dial settings */
Gammify1(editColor);
if (curgroup) { /* do the same to all its friends */
for (i=0; i<numcols; i++) {
if (cellgroup[i] == curgroup) {
rcmap[i] = rcmap[editColor];
gcmap[i] = gcmap[editColor];
bcmap[i] = bcmap[editColor];
rMap[i] = rMap[editColor];
gMap[i] = gMap[editColor];
bMap[i] = bMap[editColor];
}
}
}
/* do something clever if we're using R/W color and this colorcell isn't
shared */
if (!regroup && allocMode==AM_READWRITE && rwthistime) {
/* let's try to be clever */
/* determine if the editColor cell is unique, or shared (among
non-group members, that is) */
for (i=j=0; i<numcols; i++)
if (rwpc2pc[i] == rwpc2pc[editColor]) j++;
/* if this is a group, subtract off the non-this-one pixels from group */
if (curgroup) {
for (i=0; i<numcols; i++) {
if (cellgroup[i] == curgroup && i!=editColor) j--;
}
}
if (j==1) { /* we can be way cool about this one */
XColor ctab;
ctab.pixel = cols[editColor];
if (mono) {
int intens = MONO(rMap[editColor], gMap[editColor], bMap[editColor]);
ctab.red = ctab.green = ctab.blue = intens<<8;
}
else {
ctab.red = rMap[editColor]<<8;
ctab.green = gMap[editColor]<<8;
ctab.blue = bMap[editColor]<<8;
}
ctab.flags = DoRed | DoGreen | DoBlue;
XStoreColor(theDisp, LocalCmap ? LocalCmap : theCmap, &ctab);
rdisp[editColor] = rMap[editColor];
gdisp[editColor] = gMap[editColor];
bdisp[editColor] = bMap[editColor];
return;
}
}
/* either we aren't using R/W color, or we are, but this particular color
cell isn't mapped a unique X colorcell. Either way... */
FreeColors();
putECfirst(); /* make certain this one gets alloc'd */
AllocColors();
DrawEpic();
SetCursors(-1);
}
/**************************************/
static void putECfirst()
{
/* called after all colors have been freed up, but before reallocating.
moves color #editColor to first in 'colAllocOrder' list, so that it
is most-likely to get its desired color */
int i;
/* find it in the list */
for (i=0; i<numcols; i++) {
if (editColor == colAllocOrder[i]) break;
}
if (i==numcols || i==0) { /* didn't find it, or it's first already */
return;
}
/* shift 0..i-1 down one position */
xvbcopy((char *) colAllocOrder, (char *) colAllocOrder+1,
i * sizeof(colAllocOrder[0]));
colAllocOrder[0] = editColor;
}
#define CDIST(x,y,z) ((x)*(x) + (y)*(y) + (z)*(z))
/***************************************************************/
int MakeStdCmaps()
{
/* produces many things:
* stdr,stdg,stdb[256] - a 256-entry, desired 3/3/2 colormap
* stdcols[256] - 256-entry, maps 3/3/2 colors into X pixel values
* stdrdisp,stdgdisp, stdbdisp[256]
* - for a given 3/3/2 color, gives the rgb values
* actually displayed. Since we're not going to
* successfully allocate all 256 colors (generally),
* several 3/3/2 colors may be mapped into the
* same X pixel (and hence, same rgb value)
* stdfreecols[256] - list of colors to free on exit
* stdnfcols - # of colors to free
*
* possibly modifies browR, browG, browB, and browcols arrays
* (if !browPerfect)
*/
/* returns '1' if the colors were reallocated, '0' otherwise */
/*
* if we're on a TrueColor/DirectColor visual, it will attempt to alloc
* 256 colors
* otherwise, if colorMapMode == CM_STDCMAP, it will attempt to alloc
* 6*6*6 = 216 colors, or 4*4*4 = 64, or 2*2*2 = 8 colors, depending
* on 'ncols' variable
*/
/* note:
* if (ncols==0) (ie, we're either on, or emulating a b/w display),
* build std*[], std*disp[], colormaps, but don't actually
* allocate any colors.
*/
int i, r,g,b, desMode, screwed;
XColor def;
byte rmap[256],gmap[256],bmap[256],order[256];
unsigned long descols[256];
int des2got[256], failed[256];
int maplen, exactCnt, nearCnt;
/* generate stdr,stdg,stdb cmap. Same in all cases */
for (r=0, i=0; r<8; r++)
for (g=0; g<8; g++)
for (b=0; b<4; b++,i++) {
stdr[i] = (r*255)/7;
stdg[i] = (g*255)/7;
stdb[i] = (b*255)/3;
}
/* determine what size cmap we should build */
if (theVisual->class == TrueColor ||
theVisual->class == DirectColor) desMode = STD_332;
else if (colorMapMode == CM_STDCMAP) desMode = STD_232;
else desMode = STD_222;
/* make sure that we're not exceeding 'ncols' (ignore ncols==0) */
if (ncols > 0) {
if (ncols < 64) desMode = STD_111;
else if (ncols < 128) desMode = STD_222;
else if (ncols < 256 && desMode == STD_332) desMode = STD_232;
}
if (DEBUG) fprintf(stderr,"MakeStdCmaps: have=%d, des=%d, ncols=%d\n",
haveStdCmap, desMode, ncols);
if (haveStdCmap != STD_NONE && haveStdCmap == desMode) return 0;
freeStdCmaps();
/* init some stuff */
screwed = 0;
stdnfcols = 0;
for (i=0; i<256; i++) stdcols[i] = stdfreecols[i] = 0;
for (i=0; i<256; i++) des2got[i] = i;
exactCnt = nearCnt = 0;