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) |
20 |
< |
Display *disp; |
21 |
< |
int scrn; |
22 |
< |
int depth; |
23 |
< |
char *data; |
24 |
< |
int width, height; |
25 |
< |
int bm_pad; |
18 |
> |
extern XRASTER * |
19 |
> |
make_raster( |
20 |
> |
Display *disp, |
21 |
> |
XVisualInfo *vis, |
22 |
> |
int npixbits, |
23 |
> |
char *data, |
24 |
> |
int width, |
25 |
> |
int height, |
26 |
> |
int bm_pad |
27 |
> |
) |
28 |
|
{ |
29 |
+ |
static long swaptest = 1; |
30 |
|
register XRASTER *xr; |
31 |
< |
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 == 24) { |
38 |
< |
if (!XMatchVisualInfo(disp,scrn,24,TrueColor,&ourvinfo) && |
39 |
< |
!XMatchVisualInfo(disp,scrn,24,DirectColor,&ourvinfo)) |
40 |
< |
return(NULL); |
41 |
< |
} else |
42 |
< |
return(NULL); |
31 |
> |
|
32 |
|
if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) |
33 |
|
return(NULL); |
34 |
|
xr->disp = disp; |
35 |
< |
xr->screen = scrn; |
36 |
< |
xr->visual = ourvinfo.visual; |
37 |
< |
xr->image = XCreateImage(disp,ourvinfo.visual,depth, |
38 |
< |
depth==1 ? XYBitmap : ZPixmap, |
39 |
< |
0,data,width,height,bm_pad,0); |
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 |
< |
if (xr->image->bits_per_pixel == 32) { |
45 |
< |
xr->image->bytes_per_line = xr->image->bytes_per_line*24/32; |
46 |
< |
xr->image->bits_per_pixel = 24; |
44 |
> |
xr->image->bitmap_unit = bm_pad; |
45 |
> |
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 |
|
} |
56 |
< |
xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0); |
57 |
< |
XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn), |
58 |
< |
GXcopy, AllPlanes); |
56 |
> |
xr->gc = 0; |
57 |
|
return(xr); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
< |
int |
62 |
< |
init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */ |
63 |
< |
register XRASTER *xr; |
64 |
< |
int rmap[256], gmap[256], bmap[256]; |
61 |
> |
extern int |
62 |
> |
init_rcolors( /* initialize colors */ |
63 |
> |
register XRASTER *xr, |
64 |
> |
uby8 cmap[][3] |
65 |
> |
) |
66 |
|
{ |
67 |
|
register unsigned char *p; |
68 |
|
register int i; |
69 |
|
|
70 |
< |
if (xr->image->depth != 8 || xr->ncolors != 0) |
70 |
> |
if ((xr->image->depth > 8) | (xr->ncolors != 0)) |
71 |
|
return(xr->ncolors); |
72 |
|
xr->pmap = (short *)malloc(256*sizeof(short)); |
73 |
|
if (xr->pmap == NULL) |
81 |
|
i = xr->image->width*xr->image->height; |
82 |
|
i--; p++) |
83 |
|
if (xr->pmap[*p] == -1) { |
84 |
< |
xr->cdefs[xr->ncolors].red = rmap[*p] << 8; |
85 |
< |
xr->cdefs[xr->ncolors].green = gmap[*p] << 8; |
86 |
< |
xr->cdefs[xr->ncolors].blue = bmap[*p] << 8; |
84 |
> |
xr->cdefs[xr->ncolors].red = cmap[*p][RED] << 8; |
85 |
> |
xr->cdefs[xr->ncolors].green = cmap[*p][GRN] << 8; |
86 |
> |
xr->cdefs[xr->ncolors].blue = cmap[*p][BLU] << 8; |
87 |
|
xr->cdefs[xr->ncolors].pixel = *p; |
88 |
|
xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue; |
89 |
|
xr->pmap[*p] = xr->ncolors++; |
90 |
|
} |
91 |
< |
xr->cdefs = (XColor *)realloc((char *)xr->cdefs, |
91 |
> |
xr->cdefs = (XColor *)realloc((void *)xr->cdefs, |
92 |
|
xr->ncolors*sizeof(XColor)); |
93 |
|
if (xr->cdefs == NULL) |
94 |
|
return(0); |
95 |
|
return(xr->ncolors); |
96 |
|
} |
97 |
|
|
98 |
< |
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; |
98 |
> |
|
99 |
> |
extern Colormap |
100 |
> |
newcmap( /* get colormap and fix b & w */ |
101 |
> |
Display *disp, |
102 |
> |
int scrn, |
103 |
> |
Visual *vis |
104 |
> |
) |
105 |
|
{ |
106 |
|
XColor thiscolor; |
107 |
|
unsigned long *pixels; |
109 |
|
int n; |
110 |
|
register int i, j; |
111 |
|
|
112 |
< |
cmap = XCreateColormap(disp, w, vis, AllocNone); |
113 |
< |
if (cmap == 0) |
114 |
< |
return(0); |
112 |
> |
cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone); |
113 |
> |
if (cmap == 0 || vis->class != PseudoColor) |
114 |
> |
return(cmap); |
115 |
|
pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long)); |
116 |
|
if (pixels == NULL) |
117 |
|
return(0); |
135 |
|
i--; |
136 |
|
} |
137 |
|
XFreeColors(disp, cmap, pixels, n, 0); |
138 |
< |
free((char *)pixels); |
138 |
> |
free((void *)pixels); |
139 |
|
return(cmap); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
< |
unsigned long * |
144 |
< |
map_rcolors(xr, w) /* get and assign pixels */ |
145 |
< |
register XRASTER *xr; |
146 |
< |
Window w; |
143 |
> |
extern unsigned long * |
144 |
> |
map_rcolors( /* get and assign pixels */ |
145 |
> |
register XRASTER *xr, |
146 |
> |
Window w |
147 |
> |
) |
148 |
|
{ |
149 |
|
register int i; |
150 |
|
register unsigned char *p; |
151 |
|
|
152 |
< |
if (xr->ncolors == 0 || xr->image->depth != 8) |
152 |
> |
if (xr->ncolors == 0 || xr->image->depth > 8) |
153 |
|
return(NULL); |
154 |
|
if (xr->pixels != NULL) |
155 |
|
return(xr->pixels); |
159 |
|
if (xr->visual == DefaultVisual(xr->disp, xr->screen)) |
160 |
|
xr->cmap = DefaultColormap(xr->disp, xr->screen); |
161 |
|
else |
162 |
< |
xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); |
162 |
> |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); |
163 |
|
while (XAllocColorCells(xr->disp, xr->cmap, 0, |
164 |
|
NULL, 0, xr->pixels, xr->ncolors) == 0) |
165 |
|
if (xr->cmap == DefaultColormap(xr->disp, xr->screen)) |
166 |
< |
xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); |
166 |
> |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); |
167 |
|
else { |
168 |
< |
free((char *)xr->pixels); |
168 |
> |
free((void *)xr->pixels); |
169 |
|
xr->pixels = NULL; |
170 |
|
return(NULL); |
171 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
< |
Pixmap |
195 |
< |
make_rpixmap(xr) /* make pixmap for raster */ |
196 |
< |
register XRASTER *xr; |
194 |
> |
extern Pixmap |
195 |
> |
make_rpixmap( /* make pixmap for raster */ |
196 |
> |
register XRASTER *xr, |
197 |
> |
Window w |
198 |
> |
) |
199 |
|
{ |
200 |
+ |
XWindowAttributes xwattr; |
201 |
|
Pixmap pm; |
202 |
|
|
203 |
|
if (xr->pm != 0) |
204 |
|
return(xr->pm); |
205 |
< |
pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen), |
205 |
> |
XGetWindowAttributes(xr->disp, w, &xwattr); |
206 |
> |
pm = XCreatePixmap(xr->disp, w, |
207 |
|
xr->image->width, xr->image->height, |
208 |
< |
DisplayPlanes(xr->disp, xr->screen)); |
208 |
> |
xwattr.depth); |
209 |
|
if (pm == 0) |
210 |
|
return(0); |
211 |
|
put_raster(pm, 0, 0, xr); |
212 |
|
return(xr->pm = pm); |
213 |
|
} |
214 |
|
|
215 |
< |
|
216 |
< |
patch_raster(d, xsrc, ysrc, xdst, ydst, width, height, xr) /* redraw */ |
217 |
< |
Drawable d; |
218 |
< |
int xsrc, ysrc, xdst, ydst; |
219 |
< |
int width, height; |
220 |
< |
register XRASTER *xr; |
215 |
> |
extern void |
216 |
> |
patch_raster( /* redraw */ |
217 |
> |
Drawable d, |
218 |
> |
int xsrc, |
219 |
> |
int ysrc, |
220 |
> |
int xdst, |
221 |
> |
int ydst, |
222 |
> |
int width, |
223 |
> |
int height, |
224 |
> |
register XRASTER *xr |
225 |
> |
) |
226 |
|
{ |
227 |
|
if (xsrc >= xr->image->width || ysrc >= xr->image->height) |
228 |
|
return; |
241 |
|
if (ysrc + height > xr->image->height) |
242 |
|
height = xr->image->height - ysrc; |
243 |
|
|
244 |
+ |
if (xr->gc == 0) { |
245 |
+ |
xr->gc = XCreateGC(xr->disp, d, 0, 0); |
246 |
+ |
XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen), |
247 |
+ |
WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes); |
248 |
+ |
} |
249 |
|
if (xr->pm == 0) |
250 |
|
XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc, |
251 |
|
xdst, ydst, width, height); |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
< |
unmap_rcolors(xr) /* free colors */ |
259 |
< |
register XRASTER *xr; |
258 |
> |
extern void |
259 |
> |
unmap_rcolors( /* free colors */ |
260 |
> |
register XRASTER *xr |
261 |
> |
) |
262 |
|
{ |
263 |
|
if (xr->pixels == NULL) |
264 |
|
return; |
265 |
|
XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0); |
266 |
|
if (xr->cmap != DefaultColormap(xr->disp, xr->screen)) |
267 |
|
XFreeColormap(xr->disp, xr->cmap); |
268 |
< |
free((char *)xr->pixels); |
268 |
> |
free((void *)xr->pixels); |
269 |
|
xr->pixels = NULL; |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
< |
free_rpixmap(xr) /* release Pixmap */ |
274 |
< |
register XRASTER *xr; |
273 |
> |
extern void |
274 |
> |
free_rpixmap( /* release Pixmap */ |
275 |
> |
register XRASTER *xr |
276 |
> |
) |
277 |
|
{ |
278 |
|
if (xr->pm == 0) |
279 |
|
return; |
282 |
|
} |
283 |
|
|
284 |
|
|
285 |
< |
free_raster(xr) /* free raster data */ |
286 |
< |
register XRASTER *xr; |
285 |
> |
extern void |
286 |
> |
free_raster( /* free raster data */ |
287 |
> |
register XRASTER *xr |
288 |
> |
) |
289 |
|
{ |
290 |
|
free_rpixmap(xr); |
291 |
|
if (xr->ncolors > 0) { |
292 |
|
unmap_rcolors(xr); |
293 |
< |
free((char *)xr->pmap); |
294 |
< |
free((char *)xr->cdefs); |
293 |
> |
free((void *)xr->pmap); |
294 |
> |
free((void *)xr->cdefs); |
295 |
|
} |
296 |
|
XDestroyImage(xr->image); |
297 |
< |
XFreeGC(xr->disp, xr->gc); |
298 |
< |
free((char *)xr); |
297 |
> |
if (xr->gc != 0) |
298 |
> |
XFreeGC(xr->disp, xr->gc); |
299 |
> |
free((void *)xr); |
300 |
|
} |