ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 2.3
Committed: Tue May 19 13:10:07 1992 UTC (31 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.2: +5 -2 lines
Log Message:
fixed 24-bit byte ordering with hack for bitmaps

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