24 |
|
|
25 |
|
#include "xtwind.h" |
26 |
|
|
27 |
< |
#define GAMMA 2.0 /* exponent for color correction */ |
27 |
> |
#define GAMMA 2.2 /* exponent for color correction */ |
28 |
|
|
29 |
|
#define BORWIDTH 5 /* border width */ |
30 |
|
#define BARHEIGHT 25 /* menu bar size */ |
39 |
|
#define WFLUSH 30 /* flush after this many rays */ |
40 |
|
#endif |
41 |
|
|
42 |
– |
#define hashcolr(c) ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors) |
43 |
– |
|
42 |
|
#define checkinp() while (XPending() > 0) getevent() |
43 |
|
|
44 |
|
#define levptr(etype) ((etype *)&thisevent) |
47 |
|
|
48 |
|
static XEvent thisevent; /* current event */ |
49 |
|
|
52 |
– |
static int colres = 128; /* color resolution */ |
53 |
– |
static COLR colrmap[256]; /* our color mapping */ |
54 |
– |
|
50 |
|
static int ncolors = 0; /* color table size */ |
51 |
< |
static COLR *colrtbl; /* our color table */ |
57 |
< |
static int *pixval; /* associated pixel values */ |
51 |
> |
static int *pixval; /* allocated pixel values */ |
52 |
|
|
53 |
|
static Display *ourdisplay = NULL; /* our display */ |
54 |
|
|
90 |
|
stderr_v("not enough colors\n"); |
91 |
|
return(NULL); |
92 |
|
} |
93 |
< |
if (getmap() < 0) /* not fatal */ |
93 |
> |
make_cmap(GAMMA); /* make color map */ |
94 |
> |
if (getpixels() < 0) /* get pixels */ |
95 |
|
stderr_v("cannot allocate colors\n"); |
96 |
|
|
97 |
|
pickcursor = XCreateCursor(bcross_width, bcross_height, |
125 |
|
gwidth = gheight = 0; |
126 |
|
} |
127 |
|
XFreeCursor(pickcursor); |
128 |
< |
freemap(); |
128 |
> |
freepixels(); |
129 |
|
XCloseDisplay(ourdisplay); |
130 |
|
ourdisplay = NULL; |
131 |
|
} |
156 |
|
gheight = yres; |
157 |
|
} else /* just clear */ |
158 |
|
XClear(gwind); |
159 |
< |
newmap(); |
159 |
> |
if (newtab() < 0) { |
160 |
> |
stderr_v("Color allocation error\n"); |
161 |
> |
quit(1); |
162 |
> |
} |
163 |
|
checkinp(); |
164 |
|
return; |
165 |
|
fail: |
175 |
|
{ |
176 |
|
extern long nrays; /* global ray count */ |
177 |
|
static long lastflush = 0; /* ray count at last flush */ |
180 |
– |
int ndx; |
178 |
|
|
179 |
|
if (ncolors > 0) { |
183 |
– |
if ((ndx = colindex(col)) < 0) { |
184 |
– |
colres >>= 1; |
185 |
– |
redraw(); |
186 |
– |
return; |
187 |
– |
} |
180 |
|
XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin, |
181 |
< |
pixval[ndx]); |
181 |
> |
pixval[get_pixel(col)]); |
182 |
|
} |
183 |
|
if (nrays - lastflush >= WFLUSH) { |
184 |
|
if (ncolors <= 0) /* output necessary for death */ |
250 |
|
} |
251 |
|
|
252 |
|
|
261 |
– |
static int |
262 |
– |
colindex(col) /* return index for color */ |
263 |
– |
COLOR col; |
264 |
– |
{ |
265 |
– |
static COLR clr; |
266 |
– |
int hval; |
267 |
– |
register int ndx, i; |
268 |
– |
|
269 |
– |
mapcolor(clr, col); |
270 |
– |
|
271 |
– |
hval = ndx = hashcolr(clr); |
272 |
– |
|
273 |
– |
for (i = 1; i < ncolors; i++) { |
274 |
– |
if (colrtbl[ndx][EXP] == 0) { |
275 |
– |
colrtbl[ndx][RED] = clr[RED]; |
276 |
– |
colrtbl[ndx][GRN] = clr[GRN]; |
277 |
– |
colrtbl[ndx][BLU] = clr[BLU]; |
278 |
– |
colrtbl[ndx][EXP] = COLXS; |
279 |
– |
newcolr(ndx, clr); |
280 |
– |
return(ndx); |
281 |
– |
} |
282 |
– |
if ( colrtbl[ndx][RED] == clr[RED] && |
283 |
– |
colrtbl[ndx][GRN] == clr[GRN] && |
284 |
– |
colrtbl[ndx][BLU] == clr[BLU] ) |
285 |
– |
return(ndx); |
286 |
– |
ndx = (hval + i*i) % ncolors; |
287 |
– |
} |
288 |
– |
return(-1); |
289 |
– |
} |
290 |
– |
|
291 |
– |
|
253 |
|
static |
254 |
|
newcolr(ndx, clr) /* enter a color into hardware table */ |
255 |
|
int ndx; |
267 |
|
|
268 |
|
|
269 |
|
static |
270 |
< |
mapcolor(clr, col) /* map to our color space */ |
310 |
< |
COLR clr; |
311 |
< |
COLOR col; |
270 |
> |
newtab() /* assign new color table */ |
271 |
|
{ |
272 |
< |
register int i, p; |
273 |
< |
/* compute color table value */ |
274 |
< |
for (i = 0; i < 3; i++) { |
275 |
< |
p = colval(col,i) * 255.0 + 0.5; |
276 |
< |
if (p < 0) p = 0; |
277 |
< |
else if (p > 255) p = 255; |
278 |
< |
clr[i] = colrmap[p][i]; |
272 |
> |
extern COLR *get_ctab(); |
273 |
> |
COLR *ctab; |
274 |
> |
Color *defs; |
275 |
> |
register int i; |
276 |
> |
|
277 |
> |
if ((ctab = get_ctab(ncolors)) == NULL) |
278 |
> |
return(-1); |
279 |
> |
if ((defs = (Color *)malloc(ncolors*sizeof(Color))) == NULL) |
280 |
> |
return(-1); |
281 |
> |
for (i = 0; i < ncolors; i++) { |
282 |
> |
defs[i].pixel = pixval[i]; |
283 |
> |
defs[i].red = ctab[i][RED] << 8; |
284 |
> |
defs[i].green = ctab[i][GRN] << 8; |
285 |
> |
defs[i].blue = ctab[i][BLU] << 8; |
286 |
|
} |
287 |
< |
clr[EXP] = COLXS; |
287 |
> |
XStoreColors(ncolors, defs); |
288 |
> |
free((char *)defs); |
289 |
> |
return(0); |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
|
static |
294 |
< |
getmap() /* get the color map */ |
294 |
> |
getpixels() /* get the color map */ |
295 |
|
{ |
296 |
|
int planes; |
297 |
|
|
298 |
|
for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){ |
331 |
– |
colrtbl = (COLR *)malloc(ncolors*sizeof(COLR)); |
299 |
|
pixval = (int *)malloc(ncolors*sizeof(int)); |
300 |
< |
if (colrtbl == NULL || pixval == NULL) |
301 |
< |
return(-1); |
300 |
> |
if (pixval == NULL) |
301 |
> |
break; |
302 |
|
if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0) |
303 |
|
return(0); |
337 |
– |
free((char *)colrtbl); |
304 |
|
free((char *)pixval); |
305 |
|
} |
306 |
|
ncolors = 0; |
309 |
|
|
310 |
|
|
311 |
|
static |
312 |
< |
freemap() /* free our color map */ |
312 |
> |
freepixels() /* free our pixels */ |
313 |
|
{ |
314 |
|
if (ncolors == 0) |
315 |
|
return; |
316 |
|
XFreeColors(pixval, ncolors, 0); |
351 |
– |
free((char *)colrtbl); |
317 |
|
free((char *)pixval); |
318 |
|
ncolors = 0; |
319 |
|
} |
320 |
|
|
321 |
|
|
357 |
– |
static |
358 |
– |
newmap() /* initialize the color map */ |
359 |
– |
{ |
360 |
– |
double pow(); |
361 |
– |
int val; |
362 |
– |
register int i; |
363 |
– |
|
364 |
– |
for (i = 0; i < 256; i++) { |
365 |
– |
val = pow(i/256.0, 1.0/GAMMA) * colres; |
366 |
– |
val = (val*256 + 128) / colres; |
367 |
– |
colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val; |
368 |
– |
colrmap[i][EXP] = COLXS; |
369 |
– |
} |
370 |
– |
for (i = 0; i < ncolors; i++) |
371 |
– |
colrtbl[i][EXP] = 0; |
372 |
– |
} |
373 |
– |
|
374 |
– |
|
322 |
|
static int |
323 |
|
x_getc() /* get a command character */ |
324 |
|
{ |
341 |
|
break; |
342 |
|
case ExposeWindow: |
343 |
|
if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0) |
344 |
< |
if (getmap() < 0) |
344 |
> |
if (getpixels() < 0) |
345 |
|
stderr_v("cannot grab colors\n"); |
346 |
|
else |
347 |
< |
newmap(); |
347 |
> |
newtab(); |
348 |
|
/* fall through */ |
349 |
|
case ExposeRegion: |
350 |
|
fixwindow(levptr(XExposeEvent)); |
351 |
|
break; |
352 |
|
case UnmapWindow: |
353 |
|
if (levptr(XUnmapEvent)->subwindow == 0) |
354 |
< |
freemap(); |
354 |
> |
freepixels(); |
355 |
|
break; |
356 |
|
case ButtonPressed: /* handled in x_getcur() */ |
357 |
|
break; |