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