ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 1.4
Committed: Tue Oct 29 16:21:37 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +6 -1 lines
Log Message:
put in a hack for 32-bit DirectColor servers

File Contents

# Content
1 /* Copyright 1990 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * x11raster.c - routines to handle images for X windows.
9 *
10 * 2/18/88
11 */
12
13 #include <stdio.h>
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16
17 #include "x11raster.h"
18
19
20 XRASTER *
21 make_raster(disp, scrn, depth, data, width, height, bm_pad)
22 Display *disp;
23 int scrn;
24 int depth;
25 char *data;
26 int width, height;
27 int bm_pad;
28 {
29 register XRASTER *xr;
30 XVisualInfo ourvinfo;
31 /* Pick appropriate Visual */
32 if (depth == 1) {
33 ourvinfo.visual = DefaultVisual(disp,scrn);
34 } else if (depth == 8) {
35 if (!XMatchVisualInfo(disp,scrn,8,PseudoColor,&ourvinfo))
36 return(NULL);
37 } else if (depth == 24) {
38 if (!XMatchVisualInfo(disp,scrn,24,TrueColor,&ourvinfo) &&
39 !XMatchVisualInfo(disp,scrn,24,DirectColor,&ourvinfo))
40 return(NULL);
41 } else
42 return(NULL);
43 if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL)
44 return(NULL);
45 xr->disp = disp;
46 xr->screen = scrn;
47 xr->visual = ourvinfo.visual;
48 xr->image = XCreateImage(disp,ourvinfo.visual,depth,
49 depth==1 ? XYBitmap : ZPixmap,
50 0,data,width,height,bm_pad,0);
51 xr->image->bitmap_bit_order = MSBFirst;
52 if (xr->image->bits_per_pixel == 32) {
53 xr->image->bytes_per_line = xr->image->bytes_per_line*24/32;
54 xr->image->bits_per_pixel = 24;
55 }
56 xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0);
57 XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn),
58 GXcopy, AllPlanes);
59 return(xr);
60 }
61
62
63 int
64 init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */
65 register XRASTER *xr;
66 int rmap[256], gmap[256], bmap[256];
67 {
68 register unsigned char *p;
69 register int i;
70
71 if (xr->image->depth != 8 || xr->ncolors != 0)
72 return(xr->ncolors);
73 xr->pmap = (short *)malloc(256*sizeof(short));
74 if (xr->pmap == NULL)
75 return(0);
76 xr->cdefs = (XColor *)malloc(256*sizeof(XColor));
77 if (xr->cdefs == NULL)
78 return(0);
79 for (i = 0; i < 256; i++)
80 xr->pmap[i] = -1;
81 for (p = (unsigned char *)xr->image->data,
82 i = xr->image->width*xr->image->height;
83 i--; p++)
84 if (xr->pmap[*p] == -1) {
85 xr->cdefs[xr->ncolors].red = rmap[*p] << 8;
86 xr->cdefs[xr->ncolors].green = gmap[*p] << 8;
87 xr->cdefs[xr->ncolors].blue = bmap[*p] << 8;
88 xr->cdefs[xr->ncolors].pixel = *p;
89 xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
90 xr->pmap[*p] = xr->ncolors++;
91 }
92 xr->cdefs = (XColor *)realloc((char *)xr->cdefs,
93 xr->ncolors*sizeof(XColor));
94 if (xr->cdefs == NULL)
95 return(0);
96 return(xr->ncolors);
97 }
98
99 Colormap
100 newcmap(disp, scrn, w, vis) /* get colormap and fix b & w */
101 Display *disp;
102 int scrn;
103 Window w;
104 Visual *vis;
105 {
106 XColor thiscolor;
107 unsigned long *pixels;
108 Colormap cmap;
109 int n;
110 register int i, j;
111
112 cmap = XCreateColormap(disp, w, vis, AllocNone);
113 if (cmap == 0)
114 return(0);
115 pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long));
116 if (pixels == NULL)
117 return(0);
118 for (n = vis->map_entries; n > 0; n--)
119 if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0)
120 break;
121 if (n == 0)
122 return(0);
123 /* reset black and white */
124 for (i = 0; i < n; i++) {
125 if (pixels[i] != BlackPixel(disp,scrn)
126 && pixels[i] != WhitePixel(disp,scrn))
127 continue;
128 thiscolor.pixel = pixels[i];
129 thiscolor.flags = DoRed|DoGreen|DoBlue;
130 XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor);
131 XStoreColor(disp, cmap, &thiscolor);
132 for (j = i; j+1 < n; j++)
133 pixels[j] = pixels[j+1];
134 n--;
135 i--;
136 }
137 XFreeColors(disp, cmap, pixels, n, 0);
138 free((char *)pixels);
139 return(cmap);
140 }
141
142
143 unsigned long *
144 map_rcolors(xr, w) /* get and assign pixels */
145 register XRASTER *xr;
146 Window w;
147 {
148 register int i;
149 register unsigned char *p;
150
151 if (xr->ncolors == 0 || xr->image->depth != 8)
152 return(NULL);
153 if (xr->pixels != NULL)
154 return(xr->pixels);
155 xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long));
156 if (xr->pixels == NULL)
157 return(NULL);
158 if (xr->visual == DefaultVisual(xr->disp, xr->screen))
159 xr->cmap = DefaultColormap(xr->disp, xr->screen);
160 else
161 xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual);
162 while (XAllocColorCells(xr->disp, xr->cmap, 0,
163 NULL, 0, xr->pixels, xr->ncolors) == 0)
164 if (xr->cmap == DefaultColormap(xr->disp, xr->screen))
165 xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual);
166 else {
167 free((char *)xr->pixels);
168 xr->pixels = NULL;
169 return(NULL);
170 }
171 for (i = 0; i < xr->ncolors; i++)
172 if (xr->pmap[xr->pixels[i]] == -1)
173 break;
174 if (i < xr->ncolors) { /* different pixels */
175 for (p = (unsigned char *)xr->image->data,
176 i = xr->image->width*xr->image->height;
177 i--; p++)
178 *p = xr->pixels[xr->pmap[*p]];
179 for (i = 0; i < 256; i++)
180 xr->pmap[i] = -1;
181 for (i = 0; i < xr->ncolors; i++) {
182 xr->cdefs[i].pixel = xr->pixels[i];
183 xr->pmap[xr->pixels[i]] = i;
184 }
185 free_rpixmap(xr); /* Pixmap invalid */
186 }
187 XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors);
188 XSetWindowColormap(xr->disp, w, xr->cmap);
189 return(xr->pixels);
190 }
191
192
193 Pixmap
194 make_rpixmap(xr) /* make pixmap for raster */
195 register XRASTER *xr;
196 {
197 Pixmap pm;
198
199 if (xr->pm != 0)
200 return(xr->pm);
201 pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen),
202 xr->image->width, xr->image->height,
203 DisplayPlanes(xr->disp, xr->screen));
204 if (pm == 0)
205 return(0);
206 put_raster(pm, 0, 0, xr);
207 return(xr->pm = pm);
208 }
209
210
211 patch_raster(d, xsrc, ysrc, xdst, ydst, width, height, xr) /* redraw */
212 Drawable d;
213 int xsrc, ysrc, xdst, ydst;
214 int width, height;
215 register XRASTER *xr;
216 {
217 if (xsrc >= xr->image->width || ysrc >= xr->image->height)
218 return;
219 if (xsrc < 0) {
220 xdst -= xsrc; width += xsrc;
221 xsrc = 0;
222 }
223 if (ysrc < 0) {
224 ydst -= ysrc; height += ysrc;
225 ysrc = 0;
226 }
227 if (width <= 0 || height <= 0)
228 return;
229 if (xsrc + width > xr->image->width)
230 width = xr->image->width - xsrc;
231 if (ysrc + height > xr->image->height)
232 height = xr->image->height - ysrc;
233
234 if (xr->pm == 0)
235 XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc,
236 xdst, ydst, width, height);
237 else
238 XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc,
239 width, height, xdst, ydst);
240 }
241
242
243 unmap_rcolors(xr) /* free colors */
244 register XRASTER *xr;
245 {
246 if (xr->pixels == NULL)
247 return;
248 XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0);
249 if (xr->cmap != DefaultColormap(xr->disp, xr->screen))
250 XFreeColormap(xr->disp, xr->cmap);
251 free((char *)xr->pixels);
252 xr->pixels = NULL;
253 }
254
255
256 free_rpixmap(xr) /* release Pixmap */
257 register XRASTER *xr;
258 {
259 if (xr->pm == 0)
260 return;
261 XFreePixmap(xr->disp, xr->pm);
262 xr->pm = 0;
263 }
264
265
266 free_raster(xr) /* free raster data */
267 register XRASTER *xr;
268 {
269 free_rpixmap(xr);
270 if (xr->ncolors > 0) {
271 unmap_rcolors(xr);
272 free((char *)xr->pmap);
273 free((char *)xr->cdefs);
274 }
275 XDestroyImage(xr->image);
276 XFreeGC(xr->disp, xr->gc);
277 free((char *)xr);
278 }