ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 2.4
Committed: Tue May 19 14:22:50 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +8 -5 lines
Log Message:
Did I fix 24-bit color this time?  Who knows.

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