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 |
greg |
1.2 |
GXcopy, AllPlanes); |
52 |
greg |
1.1 |
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 |
greg |
1.2 |
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
patch_raster(d, xsrc, ysrc, xdst, ydst, width, height, xr) /* redraw */ |
164 |
|
|
Drawable d; |
165 |
|
|
int xsrc, ysrc, xdst, ydst; |
166 |
|
|
int width, height; |
167 |
|
|
register XRASTER *xr; |
168 |
|
|
{ |
169 |
|
|
if (xsrc >= xr->image->width || ysrc >= xr->image->height) |
170 |
|
|
return; |
171 |
|
|
if (xsrc < 0) { |
172 |
|
|
xdst -= xsrc; width += xsrc; |
173 |
|
|
xsrc = 0; |
174 |
|
|
} |
175 |
|
|
if (ysrc < 0) { |
176 |
|
|
ydst -= ysrc; height += ysrc; |
177 |
|
|
ysrc = 0; |
178 |
|
|
} |
179 |
|
|
if (width <= 0 || height <= 0) |
180 |
|
|
return; |
181 |
|
|
if (xsrc + width > xr->image->width) |
182 |
|
|
width = xr->image->width - xsrc; |
183 |
|
|
if (ysrc + height > xr->image->height) |
184 |
|
|
height = xr->image->height - ysrc; |
185 |
|
|
|
186 |
|
|
if (xr->pm == 0) |
187 |
|
|
XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc, |
188 |
|
|
xdst, ydst, width, height); |
189 |
|
|
else |
190 |
|
|
XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc, |
191 |
|
|
width, height, xdst, ydst); |
192 |
greg |
1.1 |
} |
193 |
|
|
|
194 |
|
|
|
195 |
|
|
unmap_rcolors(xr) /* free colors */ |
196 |
|
|
register XRASTER *xr; |
197 |
|
|
{ |
198 |
|
|
if (xr->pixels == NULL) |
199 |
|
|
return; |
200 |
|
|
XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0); |
201 |
|
|
if (xr->cmap != DefaultColormap(xr->disp, xr->screen)) |
202 |
|
|
XFreeColormap(xr->disp, xr->cmap); |
203 |
|
|
free((char *)xr->pixels); |
204 |
|
|
xr->pixels = NULL; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
|
208 |
|
|
free_rpixmap(xr) /* release Pixmap */ |
209 |
|
|
register XRASTER *xr; |
210 |
|
|
{ |
211 |
|
|
if (xr->pm == 0) |
212 |
|
|
return; |
213 |
|
|
XFreePixmap(xr->disp, xr->pm); |
214 |
|
|
xr->pm = 0; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
|
218 |
|
|
free_raster(xr) /* free raster data */ |
219 |
|
|
register XRASTER *xr; |
220 |
|
|
{ |
221 |
|
|
free_rpixmap(xr); |
222 |
|
|
if (xr->ncolors > 0) { |
223 |
|
|
unmap_rcolors(xr); |
224 |
|
|
free((char *)xr->pmap); |
225 |
|
|
free((char *)xr->cdefs); |
226 |
|
|
} |
227 |
|
|
XDestroyImage(xr->image); |
228 |
|
|
XFreeGC(xr->disp, xr->gc); |
229 |
|
|
free((char *)xr); |
230 |
|
|
} |