ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 2.6
Committed: Thu May 28 09:39:22 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +9 -6 lines
Log Message:
further refinements for grayscale and odd devices

File Contents

# User Rev Content
1 greg 1.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 greg 2.5 make_raster(disp, vis, npixbits, data, width, height, bm_pad)
22 greg 1.1 Display *disp;
23 greg 2.5 XVisualInfo *vis;
24     int npixbits;
25 greg 1.1 char *data;
26     int width, height;
27     int bm_pad;
28     {
29 greg 2.5 static long swaptest = 1;
30 greg 1.1 register XRASTER *xr;
31 greg 2.5
32 greg 1.1 if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL)
33     return(NULL);
34     xr->disp = disp;
35 greg 2.5 xr->screen = vis->screen;
36     xr->visual = vis->visual;
37     if (npixbits == 1)
38     xr->image = XCreateImage(disp,vis->visual,1,
39     XYBitmap,0,data,width,height,bm_pad,0);
40     else
41     xr->image = XCreateImage(disp,vis->visual,vis->depth,
42     ZPixmap,0,data,width,height,bm_pad,0);
43 greg 2.4 xr->image->bitmap_bit_order = MSBFirst;
44 greg 2.6 xr->image->bitmap_unit = bm_pad;
45 greg 2.5 xr->image->byte_order = *(char *)&swaptest ? LSBFirst : MSBFirst;
46     if (vis->depth >= 24 && (xr->image->red_mask != 0xff ||
47     xr->image->green_mask != 0xff00 ||
48     xr->image->blue_mask != 0xff0000) &&
49     (xr->image->red_mask != 0xff0000 ||
50     xr->image->green_mask != 0xff00 ||
51     xr->image->blue_mask != 0xff)) {
52     xr->image->red_mask = 0xff;
53     xr->image->green_mask = 0xff00;
54     xr->image->blue_mask = 0xff0000;
55 greg 1.4 }
56 greg 2.5 xr->gc = 0;
57 greg 1.1 return(xr);
58     }
59    
60    
61     int
62     init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */
63     register XRASTER *xr;
64 greg 1.3 int rmap[256], gmap[256], bmap[256];
65 greg 1.1 {
66     register unsigned char *p;
67     register int i;
68    
69 greg 2.6 if (xr->image->depth > 8 || xr->ncolors != 0)
70 greg 1.1 return(xr->ncolors);
71     xr->pmap = (short *)malloc(256*sizeof(short));
72     if (xr->pmap == NULL)
73     return(0);
74     xr->cdefs = (XColor *)malloc(256*sizeof(XColor));
75     if (xr->cdefs == NULL)
76     return(0);
77     for (i = 0; i < 256; i++)
78     xr->pmap[i] = -1;
79     for (p = (unsigned char *)xr->image->data,
80     i = xr->image->width*xr->image->height;
81     i--; p++)
82     if (xr->pmap[*p] == -1) {
83     xr->cdefs[xr->ncolors].red = rmap[*p] << 8;
84     xr->cdefs[xr->ncolors].green = gmap[*p] << 8;
85     xr->cdefs[xr->ncolors].blue = bmap[*p] << 8;
86     xr->cdefs[xr->ncolors].pixel = *p;
87     xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
88     xr->pmap[*p] = xr->ncolors++;
89     }
90     xr->cdefs = (XColor *)realloc((char *)xr->cdefs,
91     xr->ncolors*sizeof(XColor));
92     if (xr->cdefs == NULL)
93     return(0);
94     return(xr->ncolors);
95     }
96    
97 greg 2.5
98 greg 1.3 Colormap
99     newcmap(disp, scrn, w, vis) /* get colormap and fix b & w */
100     Display *disp;
101     int scrn;
102     Window w;
103     Visual *vis;
104     {
105     XColor thiscolor;
106     unsigned long *pixels;
107     Colormap cmap;
108     int n;
109     register int i, j;
110 greg 1.1
111 greg 1.3 cmap = XCreateColormap(disp, w, vis, AllocNone);
112     if (cmap == 0)
113     return(0);
114     pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long));
115     if (pixels == NULL)
116     return(0);
117     for (n = vis->map_entries; n > 0; n--)
118     if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0)
119     break;
120     if (n == 0)
121     return(0);
122     /* reset black and white */
123     for (i = 0; i < n; i++) {
124     if (pixels[i] != BlackPixel(disp,scrn)
125     && pixels[i] != WhitePixel(disp,scrn))
126     continue;
127     thiscolor.pixel = pixels[i];
128     thiscolor.flags = DoRed|DoGreen|DoBlue;
129     XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor);
130     XStoreColor(disp, cmap, &thiscolor);
131     for (j = i; j+1 < n; j++)
132     pixels[j] = pixels[j+1];
133     n--;
134     i--;
135     }
136     XFreeColors(disp, cmap, pixels, n, 0);
137     free((char *)pixels);
138     return(cmap);
139     }
140    
141    
142 greg 1.1 unsigned long *
143     map_rcolors(xr, w) /* get and assign pixels */
144     register XRASTER *xr;
145     Window w;
146     {
147     register int i;
148     register unsigned char *p;
149    
150 greg 2.6 if (xr->ncolors == 0 || xr->image->depth > 8)
151 greg 1.1 return(NULL);
152     if (xr->pixels != NULL)
153     return(xr->pixels);
154     xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long));
155     if (xr->pixels == NULL)
156     return(NULL);
157 greg 1.3 if (xr->visual == DefaultVisual(xr->disp, xr->screen))
158 greg 1.1 xr->cmap = DefaultColormap(xr->disp, xr->screen);
159 greg 1.3 else
160     xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual);
161     while (XAllocColorCells(xr->disp, xr->cmap, 0,
162     NULL, 0, xr->pixels, xr->ncolors) == 0)
163 greg 1.1 if (xr->cmap == DefaultColormap(xr->disp, xr->screen))
164 greg 1.3 xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual);
165     else {
166     free((char *)xr->pixels);
167     xr->pixels = NULL;
168     return(NULL);
169     }
170 greg 1.1 for (i = 0; i < xr->ncolors; i++)
171     if (xr->pmap[xr->pixels[i]] == -1)
172     break;
173     if (i < xr->ncolors) { /* different pixels */
174     for (p = (unsigned char *)xr->image->data,
175     i = xr->image->width*xr->image->height;
176     i--; p++)
177     *p = xr->pixels[xr->pmap[*p]];
178     for (i = 0; i < 256; i++)
179     xr->pmap[i] = -1;
180     for (i = 0; i < xr->ncolors; i++) {
181     xr->cdefs[i].pixel = xr->pixels[i];
182     xr->pmap[xr->pixels[i]] = i;
183     }
184     free_rpixmap(xr); /* Pixmap invalid */
185     }
186     XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors);
187     XSetWindowColormap(xr->disp, w, xr->cmap);
188     return(xr->pixels);
189     }
190    
191    
192     Pixmap
193 greg 2.6 make_rpixmap(xr, w) /* make pixmap for raster */
194 greg 1.1 register XRASTER *xr;
195 greg 2.6 Window w;
196 greg 1.1 {
197 greg 2.6 XWindowAttributes xwattr;
198 greg 1.1 Pixmap pm;
199    
200     if (xr->pm != 0)
201     return(xr->pm);
202 greg 2.6 XGetWindowAttributes(xr->disp, w, &xwattr);
203     pm = XCreatePixmap(xr->disp, w,
204 greg 1.3 xr->image->width, xr->image->height,
205 greg 2.6 xwattr.depth);
206 greg 1.1 if (pm == 0)
207     return(0);
208     put_raster(pm, 0, 0, xr);
209     return(xr->pm = pm);
210 greg 1.2 }
211    
212    
213     patch_raster(d, xsrc, ysrc, xdst, ydst, width, height, xr) /* redraw */
214     Drawable d;
215     int xsrc, ysrc, xdst, ydst;
216     int width, height;
217     register XRASTER *xr;
218     {
219     if (xsrc >= xr->image->width || ysrc >= xr->image->height)
220     return;
221     if (xsrc < 0) {
222     xdst -= xsrc; width += xsrc;
223     xsrc = 0;
224     }
225     if (ysrc < 0) {
226     ydst -= ysrc; height += ysrc;
227     ysrc = 0;
228     }
229     if (width <= 0 || height <= 0)
230     return;
231     if (xsrc + width > xr->image->width)
232     width = xr->image->width - xsrc;
233     if (ysrc + height > xr->image->height)
234     height = xr->image->height - ysrc;
235    
236 greg 2.5 if (xr->gc == 0) {
237     xr->gc = XCreateGC(xr->disp, d, 0, 0);
238     XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen),
239     WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes);
240     }
241 greg 1.2 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 greg 1.1 }
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 greg 2.5 if (xr->gc != 0)
284     XFreeGC(xr->disp, xr->gc);
285 greg 1.1 free((char *)xr);
286     }