File: VBindColors.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (326 lines) | stat: -rw-r--r-- 7,971 bytes parent folder | download | duplicates (8)
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
#include "Vlib.h"
#ifdef HAVE_STRINGS_H
#include <strings.h>
#else
#include <string.h>
#endif

int _VProcessColor PARAMS((VWorkContext * cxt, Viewport * v, VColor * vc));

static char *errmsg = "Not enough pixel space for all colors\n";
static char *errmsg2 = "Unable to parse color \"%s\".\n";

/*
 *  We'll create the notion of a viewport-specific color tweaking
 *  procedure.  This procedure allows some user code to get control just
 *  before we hand the color name to XParseColor() so that we might
 *  alter the name a bit.  Why use it?  It allows us to shrink down the
 *  color space a bit on color-poor screens.
 */

void
VSetColorTweakProc(Viewport * v, void (*proc) ( /* ??? */ ))
{
	v->colorTweakProc = proc;
}

static int
pmap(long unsigned int *vec, int n)
{

	static int itbl[] =
	{1, 2, 4, 8};
	register int i, r = 0;

	for (i = 0; i < 4; ++i)
		if (itbl[i] & n)
			r += vec[i];
	return r;
}

int
VBindColors(Viewport * v, char *background)
{

	register int i, j, k, n, c;
	static int parseComplete = 0;
	unsigned int pixel;
	VColor   *p, *bg;
	XColor   *colorSet;
	Display  *dpy;
	unsigned long planemask[PLANECOUNT * 2];
	unsigned long pixels[1];
	int       maxpcolors;
	char      realColor[64];

	maxpcolors = (v->flags & VPMono) ? 256 : v->visual->map_entries;

	dpy = v->dpy;

	_VDefaultWorkContext->nextPixel = 0;

/*
 *  If this is a color or greyscale pixmap, allocate color cells for each
 *  color, and then set up the Viewport for drawing.
 *
 *  If this is a monochrome monitor, then the aPixel value for this color is
 *  an index into our standard pixmap table rather than being a Pixel value.
 */

	if (v->flags & (VPPixmap | VPFastAnimation | VPMono | VPDoubleBuffer)) {

		bg = VAllocColor(background);

		if (_VProcessColor(_VDefaultWorkContext, v, bg) != 0) {
			return -1;
		}

		for ((i = 0, p = _VDefaultWorkContext->VColorList); p; i++) {

			if (i > maxpcolors) {
				fprintf(stderr, "Too many colors selected.\n");
				return -1;
			}

			if (_VProcessColor(_VDefaultWorkContext, v, p) != 0) {
				return -1;
			}

			p = p->next;
		}

		v->colors = _VDefaultWorkContext->nextPixel;
		v->set = 0;
		v->pixel = v->aPixel;
		v->mask = AllPlanes;

		return 0;
	}

/*
 *  If we fall through to here, we are doing double buffering using plane
 *  masks.  This is obsolete code, but I'll leave it around for now.
 */

	colorSet = (XColor *) Vmalloc(sizeof(XColor) * maxpcolors);

	n = PLANECOUNT;
	c = 1 << n;

	if (XAllocColorCells(dpy, v->cmap, False, planemask, n * 2, pixels, 1) == 0) {
		fprintf(stderr, "Cannot allocate color cells\n");
		free((char *) colorSet);
		return -1;
	}

/*
 *  Parse background color
 */

	if ( /*parseComplete == 0 */ 1) {

		if (v->colorTweakProc) {
			(*v->colorTweakProc) (v, background, realColor);
		}
		else {
			strcpy(realColor, background);
		}

		if (XParseColor(dpy, v->cmap, realColor, &colorSet[0]) == 0) {
			fprintf(stderr, errmsg2, background);
			free(colorSet);
			return -1;
		}

/*
 *  Parse each color defined in the V Color List
 */

		for ((i = 0, p = _VDefaultWorkContext->VColorList); p; i++) {
			if (i > c) {
				fprintf(stderr, "Too many colors selected.\n");
				free(colorSet);
				return -1;
			}

			if (v->colorTweakProc) {
				(*v->colorTweakProc) (v, p->color_name, realColor);
			}
			else {
				strcpy(realColor, p->color_name);
			}

			if (XParseColor(dpy, v->cmap, realColor, &colorSet[i + 1]) == 0) {
				fprintf(stderr, errmsg2, p->color_name);
				free(colorSet);
				return -1;
			}
			p->cIndex = i + 1;
			p = p->next;
		}

		parseComplete = 1;
	}

	v->colors = i + 1;

#ifdef DEBUG
	fprintf(stderr, "%d colors defined in the V color list.\n", i);
#endif

/*
 *  PAY ATTENTION!
 *
 *  We will now create a two lists of XColors. Each will expose a particular
 *  drawing buffer (there are two drawing buffers created here).
 *  A drawing is exposed by passing one of these lists to the XSetColors
 *  procedure.
 *  We create a list by iterating through each possible combination of
 *  pixel values, based on the values returned in pixel and planemask.
 *  The pixel value is determined using a function called pmap.  Each pixel
 *  value is assigned the appropriate XColor.
 */

	k = 0;
	for (i = 0; i < v->colors; ++i) {
		pixel = v->aPixel[i] = (Color) (pmap(&planemask[0], i) | pixels[0]);
		for (j = 0; j < v->colors; ++j) {
			v->aColor[k] = colorSet[i];
			v->aColor[k++].pixel = pixel | pmap(&planemask[n], j);
		}
	}

	v->aMask = pmap(&planemask[0], (c - 1)) | pixels[0];

	k = 0;
	for (i = 0; i < v->colors; ++i) {
		pixel = v->bPixel[i] = (Color) (pmap(&planemask[n], i) | pixels[0]);
		for (j = 0; j < v->colors; ++j) {
			v->bColor[k] = colorSet[i];
			v->bColor[k++].pixel = pixel | pmap(&planemask[0], j);
		}
	}

	v->bMask = pmap(&planemask[n], (c - 1)) | pixels[0];

	free(colorSet);
	return 0;
}

int
_VProcessColor(VWorkContext * cxt, Viewport * v, VColor * vc)
{
	char      realColor[256];
	Display  *dpy = v->dpy;
	XColor    xcolor, xcolor2, hcolor;
	double    d;
	int       i, swap = 0;
	unsigned long temp;

/*
 *  First, if we are doing any depth cueing, insure that the haze color has
 *  been converted to RGB values.
 */

	if (cxt->depthCueSteps > 1 && (v->flags & VPDepthCueParsed) == 0) {

		if (v->colorTweakProc) {
			(*v->colorTweakProc) (v, cxt->depthCueColor->color_name,
								  realColor);
		}
		else {
			strcpy(realColor, cxt->depthCueColor->color_name);
		}

		if (XParseColor(dpy, v->cmap,
				realColor, &hcolor) == 0) {
			fprintf(stderr, errmsg2,
				cxt->depthCueColor->color_name);
			return -1;
		}

		if (v->AllocColor(v, v->cmap, &hcolor) == 0) {
			fprintf(stderr, errmsg);
			return -1;
	        }

		cxt->depthCueColor->cIndex = cxt->nextPixel;
		v->aPixel[cxt->nextPixel++] = hcolor.pixel;
		v->xdepthCueColor = hcolor;
		v->flags |= VPDepthCueParsed;
		swap = 1;
	}

/*
 *  Now parse this color.
 */

	if (v->colorTweakProc) {
		(*v->colorTweakProc) (v, vc->color_name, realColor);
	}
	else {
		strcpy(realColor, vc->color_name);
	}

	if (XParseColor(dpy, v->cmap, realColor, &xcolor) == 0) {
		fprintf(stderr, errmsg2, vc->color_name);
		return -1;
	}

	if (v->flags & VPMono) {
		vc->cIndex = cxt->nextPixel;
		v->aPixel[cxt->nextPixel++] = (
										  xcolor.red * 299L +
										  xcolor.green * 587L +
									 xcolor.blue * 114L) / (1000 * 8192);
	}
	else if (v->flags & VPDepthCueing && cxt->depthCueSteps > 1 &&
			 vc->flags & ColorEnableDepthCueing) {
		vc->cIndex = cxt->nextPixel;
		hcolor = v->xdepthCueColor;
		for (i = 0; i < cxt->depthCueSteps - 1; ++i) {
			d = (double) i / (double) cxt->depthCueSteps;
			xcolor2.red = (unsigned short) (xcolor.red * (1.0 - d) + hcolor.red * d);
			xcolor2.green = (unsigned short) (xcolor.green * (1.0 - d) + hcolor.green * d);
			xcolor2.blue = (unsigned short) (xcolor.blue * (1.0 - d) + hcolor.blue * d);
			xcolor2.flags = xcolor.flags;
			if (v->AllocColor(v, v->cmap, &xcolor2) == 0) {
				fprintf(stderr, errmsg);
				return -1;
			}
			v->aPixel[cxt->nextPixel++] = xcolor2.pixel;
	
		}
	}
	else {
		if (v->AllocColor(v, v->cmap, &xcolor) == 0) {
			fprintf(stderr, errmsg);
			return -1;
		}
		vc->cIndex = cxt->nextPixel;
		v->aPixel[cxt->nextPixel++] = xcolor.pixel;
	}

/*
 *  Sorry for the hack, but here goes ...
 *
 *  The background color in the V library is a bit of an orphan.  It is
 *  defined when VBindColors is called.  There are parts of the V library
 *  that assume that v->aPixel[0] is the pixel value of the background color,
 *  but that is not true at this point in time if we just got through parsing
 *  the depth cue color.  By swapping the aPixel[0] and aPixel[1] values
 *  (and updating the corresponding VColor entries that point to them), we
 *  an hack around the problem.
 */

	if (swap) {
		temp = v->aPixel[0];
		v->aPixel[0] = v->aPixel[1];
		v->aPixel[1] = (Color) temp;
		temp = cxt->depthCueColor->cIndex;
		cxt->depthCueColor->cIndex = vc->cIndex;
		vc->cIndex = (Color) temp;
	}

	return 0;
}