| 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 |
greg |
1.3 |
} else if (depth == 24) { |
| 38 |
greg |
1.4 |
if (!XMatchVisualInfo(disp,scrn,24,TrueColor,&ourvinfo) && |
| 39 |
|
|
!XMatchVisualInfo(disp,scrn,24,DirectColor,&ourvinfo)) |
| 40 |
greg |
1.1 |
return(NULL); |
| 41 |
|
|
} else |
| 42 |
|
|
return(NULL); |
| 43 |
|
|
if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) |
| 44 |
|
|
return(NULL); |
| 45 |
|
|
xr->disp = disp; |
| 46 |
|
|
xr->screen = scrn; |
| 47 |
|
|
xr->visual = ourvinfo.visual; |
| 48 |
|
|
xr->image = XCreateImage(disp,ourvinfo.visual,depth, |
| 49 |
greg |
1.3 |
depth==1 ? XYBitmap : ZPixmap, |
| 50 |
|
|
0,data,width,height,bm_pad,0); |
| 51 |
|
|
xr->image->bitmap_bit_order = MSBFirst; |
| 52 |
greg |
2.2 |
xr->image->byte_order = MSBFirst; |
| 53 |
greg |
1.4 |
if (xr->image->bits_per_pixel == 32) { |
| 54 |
|
|
xr->image->bytes_per_line = xr->image->bytes_per_line*24/32; |
| 55 |
|
|
xr->image->bits_per_pixel = 24; |
| 56 |
|
|
} |
| 57 |
greg |
1.1 |
xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0); |
| 58 |
|
|
XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn), |
| 59 |
greg |
1.2 |
GXcopy, AllPlanes); |
| 60 |
greg |
1.1 |
return(xr); |
| 61 |
|
|
} |
| 62 |
|
|
|
| 63 |
|
|
|
| 64 |
|
|
int |
| 65 |
|
|
init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */ |
| 66 |
|
|
register XRASTER *xr; |
| 67 |
greg |
1.3 |
int rmap[256], gmap[256], bmap[256]; |
| 68 |
greg |
1.1 |
{ |
| 69 |
|
|
register unsigned char *p; |
| 70 |
|
|
register int i; |
| 71 |
|
|
|
| 72 |
|
|
if (xr->image->depth != 8 || xr->ncolors != 0) |
| 73 |
|
|
return(xr->ncolors); |
| 74 |
|
|
xr->pmap = (short *)malloc(256*sizeof(short)); |
| 75 |
|
|
if (xr->pmap == NULL) |
| 76 |
|
|
return(0); |
| 77 |
|
|
xr->cdefs = (XColor *)malloc(256*sizeof(XColor)); |
| 78 |
|
|
if (xr->cdefs == NULL) |
| 79 |
|
|
return(0); |
| 80 |
|
|
for (i = 0; i < 256; i++) |
| 81 |
|
|
xr->pmap[i] = -1; |
| 82 |
|
|
for (p = (unsigned char *)xr->image->data, |
| 83 |
|
|
i = xr->image->width*xr->image->height; |
| 84 |
|
|
i--; p++) |
| 85 |
|
|
if (xr->pmap[*p] == -1) { |
| 86 |
|
|
xr->cdefs[xr->ncolors].red = rmap[*p] << 8; |
| 87 |
|
|
xr->cdefs[xr->ncolors].green = gmap[*p] << 8; |
| 88 |
|
|
xr->cdefs[xr->ncolors].blue = bmap[*p] << 8; |
| 89 |
|
|
xr->cdefs[xr->ncolors].pixel = *p; |
| 90 |
|
|
xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue; |
| 91 |
|
|
xr->pmap[*p] = xr->ncolors++; |
| 92 |
|
|
} |
| 93 |
|
|
xr->cdefs = (XColor *)realloc((char *)xr->cdefs, |
| 94 |
|
|
xr->ncolors*sizeof(XColor)); |
| 95 |
|
|
if (xr->cdefs == NULL) |
| 96 |
|
|
return(0); |
| 97 |
|
|
return(xr->ncolors); |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
greg |
1.3 |
Colormap |
| 101 |
|
|
newcmap(disp, scrn, w, vis) /* get colormap and fix b & w */ |
| 102 |
|
|
Display *disp; |
| 103 |
|
|
int scrn; |
| 104 |
|
|
Window w; |
| 105 |
|
|
Visual *vis; |
| 106 |
|
|
{ |
| 107 |
|
|
XColor thiscolor; |
| 108 |
|
|
unsigned long *pixels; |
| 109 |
|
|
Colormap cmap; |
| 110 |
|
|
int n; |
| 111 |
|
|
register int i, j; |
| 112 |
greg |
1.1 |
|
| 113 |
greg |
1.3 |
cmap = XCreateColormap(disp, w, vis, AllocNone); |
| 114 |
|
|
if (cmap == 0) |
| 115 |
|
|
return(0); |
| 116 |
|
|
pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long)); |
| 117 |
|
|
if (pixels == NULL) |
| 118 |
|
|
return(0); |
| 119 |
|
|
for (n = vis->map_entries; n > 0; n--) |
| 120 |
|
|
if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0) |
| 121 |
|
|
break; |
| 122 |
|
|
if (n == 0) |
| 123 |
|
|
return(0); |
| 124 |
|
|
/* reset black and white */ |
| 125 |
|
|
for (i = 0; i < n; i++) { |
| 126 |
|
|
if (pixels[i] != BlackPixel(disp,scrn) |
| 127 |
|
|
&& pixels[i] != WhitePixel(disp,scrn)) |
| 128 |
|
|
continue; |
| 129 |
|
|
thiscolor.pixel = pixels[i]; |
| 130 |
|
|
thiscolor.flags = DoRed|DoGreen|DoBlue; |
| 131 |
|
|
XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor); |
| 132 |
|
|
XStoreColor(disp, cmap, &thiscolor); |
| 133 |
|
|
for (j = i; j+1 < n; j++) |
| 134 |
|
|
pixels[j] = pixels[j+1]; |
| 135 |
|
|
n--; |
| 136 |
|
|
i--; |
| 137 |
|
|
} |
| 138 |
|
|
XFreeColors(disp, cmap, pixels, n, 0); |
| 139 |
|
|
free((char *)pixels); |
| 140 |
|
|
return(cmap); |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
|
|
| 144 |
greg |
1.1 |
unsigned long * |
| 145 |
|
|
map_rcolors(xr, w) /* get and assign pixels */ |
| 146 |
|
|
register XRASTER *xr; |
| 147 |
|
|
Window w; |
| 148 |
|
|
{ |
| 149 |
|
|
register int i; |
| 150 |
|
|
register unsigned char *p; |
| 151 |
|
|
|
| 152 |
|
|
if (xr->ncolors == 0 || xr->image->depth != 8) |
| 153 |
|
|
return(NULL); |
| 154 |
|
|
if (xr->pixels != NULL) |
| 155 |
|
|
return(xr->pixels); |
| 156 |
|
|
xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long)); |
| 157 |
|
|
if (xr->pixels == NULL) |
| 158 |
|
|
return(NULL); |
| 159 |
greg |
1.3 |
if (xr->visual == DefaultVisual(xr->disp, xr->screen)) |
| 160 |
greg |
1.1 |
xr->cmap = DefaultColormap(xr->disp, xr->screen); |
| 161 |
greg |
1.3 |
else |
| 162 |
|
|
xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); |
| 163 |
|
|
while (XAllocColorCells(xr->disp, xr->cmap, 0, |
| 164 |
|
|
NULL, 0, xr->pixels, xr->ncolors) == 0) |
| 165 |
greg |
1.1 |
if (xr->cmap == DefaultColormap(xr->disp, xr->screen)) |
| 166 |
greg |
1.3 |
xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); |
| 167 |
|
|
else { |
| 168 |
|
|
free((char *)xr->pixels); |
| 169 |
|
|
xr->pixels = NULL; |
| 170 |
|
|
return(NULL); |
| 171 |
|
|
} |
| 172 |
greg |
1.1 |
for (i = 0; i < xr->ncolors; i++) |
| 173 |
|
|
if (xr->pmap[xr->pixels[i]] == -1) |
| 174 |
|
|
break; |
| 175 |
|
|
if (i < xr->ncolors) { /* different pixels */ |
| 176 |
|
|
for (p = (unsigned char *)xr->image->data, |
| 177 |
|
|
i = xr->image->width*xr->image->height; |
| 178 |
|
|
i--; p++) |
| 179 |
|
|
*p = xr->pixels[xr->pmap[*p]]; |
| 180 |
|
|
for (i = 0; i < 256; i++) |
| 181 |
|
|
xr->pmap[i] = -1; |
| 182 |
|
|
for (i = 0; i < xr->ncolors; i++) { |
| 183 |
|
|
xr->cdefs[i].pixel = xr->pixels[i]; |
| 184 |
|
|
xr->pmap[xr->pixels[i]] = i; |
| 185 |
|
|
} |
| 186 |
|
|
free_rpixmap(xr); /* Pixmap invalid */ |
| 187 |
|
|
} |
| 188 |
|
|
XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors); |
| 189 |
|
|
XSetWindowColormap(xr->disp, w, xr->cmap); |
| 190 |
|
|
return(xr->pixels); |
| 191 |
|
|
} |
| 192 |
|
|
|
| 193 |
|
|
|
| 194 |
|
|
Pixmap |
| 195 |
|
|
make_rpixmap(xr) /* make pixmap for raster */ |
| 196 |
|
|
register XRASTER *xr; |
| 197 |
|
|
{ |
| 198 |
|
|
Pixmap pm; |
| 199 |
|
|
|
| 200 |
|
|
if (xr->pm != 0) |
| 201 |
|
|
return(xr->pm); |
| 202 |
|
|
pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen), |
| 203 |
greg |
1.3 |
xr->image->width, xr->image->height, |
| 204 |
|
|
DisplayPlanes(xr->disp, xr->screen)); |
| 205 |
greg |
1.1 |
if (pm == 0) |
| 206 |
|
|
return(0); |
| 207 |
|
|
put_raster(pm, 0, 0, xr); |
| 208 |
|
|
return(xr->pm = pm); |
| 209 |
greg |
1.2 |
} |
| 210 |
|
|
|
| 211 |
|
|
|
| 212 |
|
|
patch_raster(d, xsrc, ysrc, xdst, ydst, width, height, xr) /* redraw */ |
| 213 |
|
|
Drawable d; |
| 214 |
|
|
int xsrc, ysrc, xdst, ydst; |
| 215 |
|
|
int width, height; |
| 216 |
|
|
register XRASTER *xr; |
| 217 |
|
|
{ |
| 218 |
|
|
if (xsrc >= xr->image->width || ysrc >= xr->image->height) |
| 219 |
|
|
return; |
| 220 |
|
|
if (xsrc < 0) { |
| 221 |
|
|
xdst -= xsrc; width += xsrc; |
| 222 |
|
|
xsrc = 0; |
| 223 |
|
|
} |
| 224 |
|
|
if (ysrc < 0) { |
| 225 |
|
|
ydst -= ysrc; height += ysrc; |
| 226 |
|
|
ysrc = 0; |
| 227 |
|
|
} |
| 228 |
|
|
if (width <= 0 || height <= 0) |
| 229 |
|
|
return; |
| 230 |
|
|
if (xsrc + width > xr->image->width) |
| 231 |
|
|
width = xr->image->width - xsrc; |
| 232 |
|
|
if (ysrc + height > xr->image->height) |
| 233 |
|
|
height = xr->image->height - ysrc; |
| 234 |
|
|
|
| 235 |
|
|
if (xr->pm == 0) |
| 236 |
|
|
XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc, |
| 237 |
|
|
xdst, ydst, width, height); |
| 238 |
|
|
else |
| 239 |
|
|
XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc, |
| 240 |
|
|
width, height, xdst, ydst); |
| 241 |
greg |
1.1 |
} |
| 242 |
|
|
|
| 243 |
|
|
|
| 244 |
|
|
unmap_rcolors(xr) /* free colors */ |
| 245 |
|
|
register XRASTER *xr; |
| 246 |
|
|
{ |
| 247 |
|
|
if (xr->pixels == NULL) |
| 248 |
|
|
return; |
| 249 |
|
|
XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0); |
| 250 |
|
|
if (xr->cmap != DefaultColormap(xr->disp, xr->screen)) |
| 251 |
|
|
XFreeColormap(xr->disp, xr->cmap); |
| 252 |
|
|
free((char *)xr->pixels); |
| 253 |
|
|
xr->pixels = NULL; |
| 254 |
|
|
} |
| 255 |
|
|
|
| 256 |
|
|
|
| 257 |
|
|
free_rpixmap(xr) /* release Pixmap */ |
| 258 |
|
|
register XRASTER *xr; |
| 259 |
|
|
{ |
| 260 |
|
|
if (xr->pm == 0) |
| 261 |
|
|
return; |
| 262 |
|
|
XFreePixmap(xr->disp, xr->pm); |
| 263 |
|
|
xr->pm = 0; |
| 264 |
|
|
} |
| 265 |
|
|
|
| 266 |
|
|
|
| 267 |
|
|
free_raster(xr) /* free raster data */ |
| 268 |
|
|
register XRASTER *xr; |
| 269 |
|
|
{ |
| 270 |
|
|
free_rpixmap(xr); |
| 271 |
|
|
if (xr->ncolors > 0) { |
| 272 |
|
|
unmap_rcolors(xr); |
| 273 |
|
|
free((char *)xr->pmap); |
| 274 |
|
|
free((char *)xr->cdefs); |
| 275 |
|
|
} |
| 276 |
|
|
XDestroyImage(xr->image); |
| 277 |
|
|
XFreeGC(xr->disp, xr->gc); |
| 278 |
|
|
free((char *)xr); |
| 279 |
|
|
} |