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
|
diff -u old/colour.c colour.c
--- old/colour.c Fri Jun 3 14:39:39 1994
+++ colour.c Fri Jan 12 17:25:53 1996
@@ -19,7 +19,7 @@
static int allocate_colours(Colormap c);
-static unsigned long colours[6*6*6], greys[33], black, white;
+static unsigned long *colours, greys[33], black, white;
/*
* Create a colourmap with 6 levels each of red, green and blue,
@@ -27,12 +27,19 @@
* enough space, otherwise create a new one.
*/
+#define shift(x) \
+ for(i = 31, m = 0x80000000L; (m & v->x##_mask) == 0; i--, m >>= 1) \
+ ; \
+ x##_shift = i;
+
Colormap create_colourmap(void)
{
- int r, g, b, n;
+ int r, g, b, n, i, m;
XColor colour;
Colormap cmap;
-
+ Visual *v;
+ int red_shift, green_shift, blue_shift;
+
if(screendepth == 1)
{
black = BlackPixelOfScreen(screen);
@@ -51,42 +58,85 @@
XAllocColor(display, cmap, &colour);
}
- /* Allocate the colours */
+ if(screendepth <= 8)
+ {
+ /* Allocate the colours */
- n = 0;
- for(r=0; r<256; r+=51)
- for(g=0; g<256; g+=51)
- for(b=0; b<256; b+=51)
- {
- colour.red = r * 256;
- colour.green = g * 256;
- colour.blue = b * 256;
- if(XAllocColor(display, cmap, &colour) == 0)
+ colours = (unsigned long *)malloc(6*6*6 * sizeof(unsigned long));
+ if(colours == 0)
+ {
+ fprintf(stderr, "Out of memory allocating the colour table.\n");
+ exit(1);
+ }
+ n=0;
+ for(r=0; r<256; r+=51)
+ for(g=0; g<256; g+=51)
+ for(b=0; b<256; b+=51)
{
- fprintf(stderr, "using private colormap\n");
- cmap = XCopyColormapAndFree(display, cmap);
- XAllocColor(display, cmap, &colour);
+ colour.red = r * 256;
+ colour.green = g * 256;
+ colour.blue = b * 256;
+ if(XAllocColor(display, cmap, &colour) == 0)
+ {
+ fprintf(stderr, "using private colormap\n");
+ cmap = XCopyColormapAndFree(display, cmap);
+ XAllocColor(display, cmap, &colour);
+ }
+ colours[n++] = colour.pixel;
}
- colours[n++] = colour.pixel;
- }
- /* Allocate the greys */
+ /* Allocate the greys */
- for(n=0; n<33; n++)
- {
- colour.red = colour.green = colour.blue = n * 8 * 256 - (n == 32);
- if(XAllocColor(display, cmap, &colour) == 0)
+ for(n=0; n<33; n++)
{
- fprintf(stderr, "using private colormap\n");
- cmap = XCopyColormapAndFree(display, cmap);
- XAllocColor(display, cmap, &colour);
+ colour.red = colour.green = colour.blue = n * 8 * 256 - (n == 32);
+ if(XAllocColor(display, cmap, &colour) == 0)
+ {
+ fprintf(stderr, "using private colormap\n");
+ cmap = XCopyColormapAndFree(display, cmap);
+ XAllocColor(display, cmap, &colour);
+ }
+ greys[n] = colour.pixel;
}
- greys[n] = colour.pixel;
- }
- black = greys[0];
- white = greys[32];
+ black = greys[0];
+ white = greys[32];
+ }
+ else /* > 8 bit, assume truecolor display */
+ {
+ /* Use a 5*5*5 truecolor map */
+ colours = (unsigned long *)malloc(32768 * sizeof(unsigned long));
+ if(colours == 0)
+ {
+ fprintf(stderr, "Out of memory allocating the colour table.\n");
+ exit(1);
+ }
+
+ v = DefaultVisualOfScreen(screen);
+ shift(red);
+ shift(green);
+ shift(blue);
+ n=0;
+ for(r=0; r<32; r++)
+ for(g=0; g<32; g++)
+ for(b=0; b<32; b++)
+ {
+ /*
+ * We assume the default colormap has a certain
+ * format, so we can compute the pixel values
+ * without asking the XServer (which would take a
+ * tremendous amount of time for 32k colours).
+ */
+ colours[n++] =
+ (r << (red_shift - 4)) |
+ (g << (green_shift - 4)) |
+ (b << (blue_shift - 4));
+ }
+ black = BlackPixelOfScreen(screen);
+ white = WhitePixelOfScreen(screen);
+ }
+
return cmap;
}
@@ -112,7 +162,7 @@
pix_best = black;
}
}
- else
+ else if(screendepth <= 8)
{
int min = r < g ? (r < b ? r : b) : (g < b ? g : b);
int max = r > g ? (r > b ? r : b) : (g > b ? g : b);
@@ -131,6 +181,21 @@
b_best = ((b + 25) / 51) * 51;
pix_best = colours[(r_best/51)*36 + (g_best/51)*6 + (b_best/51)];
+ }
+ }
+ else /* > 8 bit */
+ {
+ if(r == 255 && g == 255 && b == 255)
+ pix_best = white;
+ else if(r == 0 && g == 0 && b == 0)
+ pix_best = black;
+ else
+ {
+ r_best = ((r + 4) / 8) * 8;
+ g_best = ((g + 4) / 8) * 8;
+ b_best = ((b + 4) / 8) * 8;
+
+ pix_best = colours[(r_best/8)*1024 + (g_best/8)*32 + (b_best/8)];
}
}
diff -u old/gui.c gui.c
--- old/gui.c Thu Jul 28 14:53:01 1994
+++ gui.c Fri Jan 12 17:25:53 1996
@@ -129,6 +129,7 @@
display = XtDisplay(shell);
screen = XtScreen(shell);
+ screendepth = DefaultDepthOfScreen(screen);
root = RootWindowOfScreen(screen);
form =
@@ -555,21 +556,39 @@
XImage *im;
unsigned char *newdata, *p;
int x, y;
+ int pad;
+ switch(screendepth)
+ {
+ case 1:
+ case 8:
+ pad = 8; break;
+ case 15:
+ case 16:
+ pad = 16; break;
+ case 24:
+ case 32:
+ pad = 32; break;
+ default:
+ fprintf(stderr, "Don't know how to handle depth %d\n", screendepth);
+ return 0;
+ }
+
if(!raw)
return 0;
switch(raw->type)
{
case 5: /* 24 bit colour */
- newdata = malloc(raw->width * raw->height);
+ newdata = malloc(raw->width * raw->height * (pad / 8));
if(!newdata)
{
fprintf(stderr, "Can't malloc %d bytes\n", raw->width*raw->height);
return 0;
}
- im = XCreateImage(display, visual, 8, ZPixmap, 0, newdata,
- raw->width, raw->height, 8, raw->width);
+ im = XCreateImage(display, visual, screendepth, ZPixmap, 0, newdata,
+ raw->width, raw->height, pad,
+ raw->width * (pad/8));
/* Convert to fixed colour map using simple error propagation */
|