ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 1.3
Committed: Fri Mar 2 12:22:42 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +61 -18 lines
Log Message:
final bug fixes, working version of x11image

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