1 |
– |
/* Copyright 1990 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* x11raster.c - routines to handle images for X windows. |
6 |
|
* |
11 |
|
#include <X11/Xlib.h> |
12 |
|
#include <X11/Xutil.h> |
13 |
|
|
14 |
+ |
#include "color.h" |
15 |
|
#include "x11raster.h" |
16 |
|
|
17 |
|
|
18 |
|
XRASTER * |
19 |
< |
make_raster(disp, scrn, depth, data, width, height, bm_pad) |
19 |
> |
make_raster(disp, vis, npixbits, data, width, height, bm_pad) |
20 |
|
Display *disp; |
21 |
< |
int scrn; |
22 |
< |
int depth; |
21 |
> |
XVisualInfo *vis; |
22 |
> |
int npixbits; |
23 |
|
char *data; |
24 |
|
int width, height; |
25 |
|
int bm_pad; |
26 |
|
{ |
27 |
+ |
static long swaptest = 1; |
28 |
|
register XRASTER *xr; |
29 |
< |
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); |
29 |
> |
|
30 |
|
if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) |
31 |
|
return(NULL); |
32 |
|
xr->disp = disp; |
33 |
< |
xr->screen = scrn; |
34 |
< |
xr->visual = ourvinfo.visual; |
35 |
< |
xr->image = XCreateImage(disp,ourvinfo.visual,depth, |
36 |
< |
ZPixmap,0,data,width,height,bm_pad,0); |
37 |
< |
xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0); |
38 |
< |
XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn), |
39 |
< |
GXcopy, ~0L); |
33 |
> |
xr->screen = vis->screen; |
34 |
> |
xr->visual = vis->visual; |
35 |
> |
if (npixbits == 1) |
36 |
> |
xr->image = XCreateImage(disp,vis->visual,1, |
37 |
> |
XYBitmap,0,data,width,height,bm_pad,0); |
38 |
> |
else |
39 |
> |
xr->image = XCreateImage(disp,vis->visual,vis->depth, |
40 |
> |
ZPixmap,0,data,width,height,bm_pad,0); |
41 |
> |
xr->image->bitmap_bit_order = MSBFirst; |
42 |
> |
xr->image->bitmap_unit = bm_pad; |
43 |
> |
xr->image->byte_order = *(char *)&swaptest ? LSBFirst : MSBFirst; |
44 |
> |
if (vis->depth >= 24 && (xr->image->red_mask != 0xff || |
45 |
> |
xr->image->green_mask != 0xff00 || |
46 |
> |
xr->image->blue_mask != 0xff0000) && |
47 |
> |
(xr->image->red_mask != 0xff0000 || |
48 |
> |
xr->image->green_mask != 0xff00 || |
49 |
> |
xr->image->blue_mask != 0xff)) { |
50 |
> |
xr->image->red_mask = 0xff; |
51 |
> |
xr->image->green_mask = 0xff00; |
52 |
> |
xr->image->blue_mask = 0xff0000; |
53 |
> |
} |
54 |
> |
xr->gc = 0; |
55 |
|
return(xr); |
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
|
int |
60 |
< |
init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */ |
60 |
> |
init_rcolors(xr, cmap) /* initialize colors */ |
61 |
|
register XRASTER *xr; |
62 |
< |
unsigned char rmap[256], gmap[256], bmap[256]; |
62 |
> |
BYTE cmap[][3]; |
63 |
|
{ |
64 |
|
register unsigned char *p; |
65 |
|
register int i; |
66 |
|
|
67 |
< |
if (xr->image->depth != 8 || xr->ncolors != 0) |
67 |
> |
if (xr->image->depth > 8 | xr->ncolors != 0) |
68 |
|
return(xr->ncolors); |
69 |
|
xr->pmap = (short *)malloc(256*sizeof(short)); |
70 |
|
if (xr->pmap == NULL) |
78 |
|
i = xr->image->width*xr->image->height; |
79 |
|
i--; p++) |
80 |
|
if (xr->pmap[*p] == -1) { |
81 |
< |
xr->cdefs[xr->ncolors].red = rmap[*p] << 8; |
82 |
< |
xr->cdefs[xr->ncolors].green = gmap[*p] << 8; |
83 |
< |
xr->cdefs[xr->ncolors].blue = bmap[*p] << 8; |
81 |
> |
xr->cdefs[xr->ncolors].red = cmap[*p][RED] << 8; |
82 |
> |
xr->cdefs[xr->ncolors].green = cmap[*p][GRN] << 8; |
83 |
> |
xr->cdefs[xr->ncolors].blue = cmap[*p][BLU] << 8; |
84 |
|
xr->cdefs[xr->ncolors].pixel = *p; |
85 |
|
xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue; |
86 |
|
xr->pmap[*p] = xr->ncolors++; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
+ |
Colormap |
97 |
+ |
newcmap(disp, scrn, vis) /* get colormap and fix b & w */ |
98 |
+ |
Display *disp; |
99 |
+ |
int scrn; |
100 |
+ |
Visual *vis; |
101 |
+ |
{ |
102 |
+ |
XColor thiscolor; |
103 |
+ |
unsigned long *pixels; |
104 |
+ |
Colormap cmap; |
105 |
+ |
int n; |
106 |
+ |
register int i, j; |
107 |
+ |
|
108 |
+ |
cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone); |
109 |
+ |
if (cmap == 0 || vis->class != PseudoColor) |
110 |
+ |
return(cmap); |
111 |
+ |
pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long)); |
112 |
+ |
if (pixels == NULL) |
113 |
+ |
return(0); |
114 |
+ |
for (n = vis->map_entries; n > 0; n--) |
115 |
+ |
if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0) |
116 |
+ |
break; |
117 |
+ |
if (n == 0) |
118 |
+ |
return(0); |
119 |
+ |
/* reset black and white */ |
120 |
+ |
for (i = 0; i < n; i++) { |
121 |
+ |
if (pixels[i] != BlackPixel(disp,scrn) |
122 |
+ |
&& pixels[i] != WhitePixel(disp,scrn)) |
123 |
+ |
continue; |
124 |
+ |
thiscolor.pixel = pixels[i]; |
125 |
+ |
thiscolor.flags = DoRed|DoGreen|DoBlue; |
126 |
+ |
XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor); |
127 |
+ |
XStoreColor(disp, cmap, &thiscolor); |
128 |
+ |
for (j = i; j+1 < n; j++) |
129 |
+ |
pixels[j] = pixels[j+1]; |
130 |
+ |
n--; |
131 |
+ |
i--; |
132 |
+ |
} |
133 |
+ |
XFreeColors(disp, cmap, pixels, n, 0); |
134 |
+ |
free((void *)pixels); |
135 |
+ |
return(cmap); |
136 |
+ |
} |
137 |
+ |
|
138 |
+ |
|
139 |
|
unsigned long * |
140 |
< |
map_rcolors(xr, w) /* get and assign pixels */ |
140 |
> |
map_rcolors(xr, w) /* get and assign pixels */ |
141 |
|
register XRASTER *xr; |
142 |
|
Window w; |
143 |
|
{ |
144 |
|
register int i; |
145 |
|
register unsigned char *p; |
100 |
– |
int j; |
146 |
|
|
147 |
< |
if (xr->ncolors == 0 || xr->image->depth != 8) |
147 |
> |
if (xr->ncolors == 0 || xr->image->depth > 8) |
148 |
|
return(NULL); |
149 |
|
if (xr->pixels != NULL) |
150 |
|
return(xr->pixels); |
151 |
|
xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long)); |
152 |
|
if (xr->pixels == NULL) |
153 |
|
return(NULL); |
154 |
< |
if (xr->visual == DefaultVisual(xr->disp, xr->screen)) { |
154 |
> |
if (xr->visual == DefaultVisual(xr->disp, xr->screen)) |
155 |
|
xr->cmap = DefaultColormap(xr->disp, xr->screen); |
156 |
< |
goto gotmap; |
157 |
< |
} |
158 |
< |
getmap: |
159 |
< |
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) { |
156 |
> |
else |
157 |
> |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); |
158 |
> |
while (XAllocColorCells(xr->disp, xr->cmap, 0, |
159 |
> |
NULL, 0, xr->pixels, xr->ncolors) == 0) |
160 |
|
if (xr->cmap == DefaultColormap(xr->disp, xr->screen)) |
161 |
< |
goto getmap; |
162 |
< |
free((char *)xr->pixels); |
163 |
< |
xr->pixels = NULL; |
164 |
< |
return(NULL); |
165 |
< |
} |
161 |
> |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); |
162 |
> |
else { |
163 |
> |
free((void *)xr->pixels); |
164 |
> |
xr->pixels = NULL; |
165 |
> |
return(NULL); |
166 |
> |
} |
167 |
|
for (i = 0; i < xr->ncolors; i++) |
168 |
|
if (xr->pmap[xr->pixels[i]] == -1) |
169 |
|
break; |
187 |
|
|
188 |
|
|
189 |
|
Pixmap |
190 |
< |
make_rpixmap(xr) /* make pixmap for raster */ |
190 |
> |
make_rpixmap(xr, w) /* make pixmap for raster */ |
191 |
|
register XRASTER *xr; |
192 |
+ |
Window w; |
193 |
|
{ |
194 |
+ |
XWindowAttributes xwattr; |
195 |
|
Pixmap pm; |
196 |
|
|
197 |
|
if (xr->pm != 0) |
198 |
|
return(xr->pm); |
199 |
< |
pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen), |
200 |
< |
xr->image->width, xr->image->height, xr->image->depth); |
199 |
> |
XGetWindowAttributes(xr->disp, w, &xwattr); |
200 |
> |
pm = XCreatePixmap(xr->disp, w, |
201 |
> |
xr->image->width, xr->image->height, |
202 |
> |
xwattr.depth); |
203 |
|
if (pm == 0) |
204 |
|
return(0); |
205 |
|
put_raster(pm, 0, 0, xr); |
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 |
|
{ |
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); |
255 |
> |
free((void *)xr->pixels); |
256 |
|
xr->pixels = NULL; |
257 |
|
} |
258 |
|
|
273 |
|
free_rpixmap(xr); |
274 |
|
if (xr->ncolors > 0) { |
275 |
|
unmap_rcolors(xr); |
276 |
< |
free((char *)xr->pmap); |
277 |
< |
free((char *)xr->cdefs); |
276 |
> |
free((void *)xr->pmap); |
277 |
> |
free((void *)xr->cdefs); |
278 |
|
} |
279 |
|
XDestroyImage(xr->image); |
280 |
< |
XFreeGC(xr->disp, xr->gc); |
281 |
< |
free((char *)xr); |
280 |
> |
if (xr->gc != 0) |
281 |
> |
XFreeGC(xr->disp, xr->gc); |
282 |
> |
free((void *)xr); |
283 |
|
} |