ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
Revision: 1.1
Committed: Thu Mar 1 13:18:23 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

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     } else if (depth > 12) {
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     ZPixmap,0,data,width,height,bm_pad,0);
49     xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0);
50     XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn),
51     GXcopy, ~0L);
52     return(xr);
53     }
54    
55    
56     int
57     init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */
58     register XRASTER *xr;
59     unsigned char rmap[256], gmap[256], bmap[256];
60     {
61     register unsigned char *p;
62     register int i;
63    
64     if (xr->image->depth != 8 || xr->ncolors != 0)
65     return(xr->ncolors);
66     xr->pmap = (short *)malloc(256*sizeof(short));
67     if (xr->pmap == NULL)
68     return(0);
69     xr->cdefs = (XColor *)malloc(256*sizeof(XColor));
70     if (xr->cdefs == NULL)
71     return(0);
72     for (i = 0; i < 256; i++)
73     xr->pmap[i] = -1;
74     for (p = (unsigned char *)xr->image->data,
75     i = xr->image->width*xr->image->height;
76     i--; p++)
77     if (xr->pmap[*p] == -1) {
78     xr->cdefs[xr->ncolors].red = rmap[*p] << 8;
79     xr->cdefs[xr->ncolors].green = gmap[*p] << 8;
80     xr->cdefs[xr->ncolors].blue = bmap[*p] << 8;
81     xr->cdefs[xr->ncolors].pixel = *p;
82     xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
83     xr->pmap[*p] = xr->ncolors++;
84     }
85     xr->cdefs = (XColor *)realloc((char *)xr->cdefs,
86     xr->ncolors*sizeof(XColor));
87     if (xr->cdefs == NULL)
88     return(0);
89     return(xr->ncolors);
90     }
91    
92    
93     unsigned long *
94     map_rcolors(xr, w) /* get and assign pixels */
95     register XRASTER *xr;
96     Window w;
97     {
98     register int i;
99     register unsigned char *p;
100     int j;
101    
102     if (xr->ncolors == 0 || xr->image->depth != 8)
103     return(NULL);
104     if (xr->pixels != NULL)
105     return(xr->pixels);
106     xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long));
107     if (xr->pixels == NULL)
108     return(NULL);
109     if (xr->visual == DefaultVisual(xr->disp, xr->screen)) {
110     xr->cmap = DefaultColormap(xr->disp, xr->screen);
111     goto gotmap;
112     }
113     getmap:
114     xr->cmap = XCreateColormap(xr->disp, w, xr->visual, AllocNone);
115     gotmap:
116     if (XAllocColorCells(xr->disp, xr->cmap, 0,
117     &j, 0, xr->pixels, xr->ncolors) == 0) {
118     if (xr->cmap == DefaultColormap(xr->disp, xr->screen))
119     goto getmap;
120     free((char *)xr->pixels);
121     xr->pixels = NULL;
122     return(NULL);
123     }
124     for (i = 0; i < xr->ncolors; i++)
125     if (xr->pmap[xr->pixels[i]] == -1)
126     break;
127     if (i < xr->ncolors) { /* different pixels */
128     for (p = (unsigned char *)xr->image->data,
129     i = xr->image->width*xr->image->height;
130     i--; p++)
131     *p = xr->pixels[xr->pmap[*p]];
132     for (i = 0; i < 256; i++)
133     xr->pmap[i] = -1;
134     for (i = 0; i < xr->ncolors; i++) {
135     xr->cdefs[i].pixel = xr->pixels[i];
136     xr->pmap[xr->pixels[i]] = i;
137     }
138     free_rpixmap(xr); /* Pixmap invalid */
139     }
140     XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors);
141     XSetWindowColormap(xr->disp, w, xr->cmap);
142     return(xr->pixels);
143     }
144    
145    
146     Pixmap
147     make_rpixmap(xr) /* make pixmap for raster */
148     register XRASTER *xr;
149     {
150     Pixmap pm;
151    
152     if (xr->pm != 0)
153     return(xr->pm);
154     pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen),
155     xr->image->width, xr->image->height, xr->image->depth);
156     if (pm == 0)
157     return(0);
158     put_raster(pm, 0, 0, xr);
159     return(xr->pm = pm);
160     }
161    
162    
163     unmap_rcolors(xr) /* free colors */
164     register XRASTER *xr;
165     {
166     if (xr->pixels == NULL)
167     return;
168     XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0);
169     if (xr->cmap != DefaultColormap(xr->disp, xr->screen))
170     XFreeColormap(xr->disp, xr->cmap);
171     free((char *)xr->pixels);
172     xr->pixels = NULL;
173     }
174    
175    
176     free_rpixmap(xr) /* release Pixmap */
177     register XRASTER *xr;
178     {
179     if (xr->pm == 0)
180     return;
181     XFreePixmap(xr->disp, xr->pm);
182     xr->pm = 0;
183     }
184    
185    
186     free_raster(xr) /* free raster data */
187     register XRASTER *xr;
188     {
189     free_rpixmap(xr);
190     if (xr->ncolors > 0) {
191     unmap_rcolors(xr);
192     free((char *)xr->pmap);
193     free((char *)xr->cdefs);
194     }
195     XDestroyImage(xr->image);
196     XFreeGC(xr->disp, xr->gc);
197     free((char *)xr);
198     }