| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
schorsch |
2.13 |
static const char RCSid[] = "$Id: x11raster.c,v 2.12 2003/07/27 22:12:03 schorsch Exp $";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* x11raster.c - routines to handle images for X windows.
|
| 6 |
|
|
*
|
| 7 |
|
|
* 2/18/88
|
| 8 |
|
|
*/
|
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h>
|
| 11 |
|
|
#include <X11/Xlib.h>
|
| 12 |
|
|
#include <X11/Xutil.h>
|
| 13 |
|
|
|
| 14 |
greg |
2.7 |
#include "color.h"
|
| 15 |
greg |
1.1 |
#include "x11raster.h"
|
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
schorsch |
2.13 |
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 |
greg |
1.1 |
{
|
| 29 |
greg |
2.5 |
static long swaptest = 1;
|
| 30 |
greg |
1.1 |
register XRASTER *xr;
|
| 31 |
greg |
2.5 |
|
| 32 |
greg |
1.1 |
if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL)
|
| 33 |
|
|
return(NULL);
|
| 34 |
|
|
xr->disp = disp;
|
| 35 |
greg |
2.5 |
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 |
greg |
2.4 |
xr->image->bitmap_bit_order = MSBFirst;
|
| 44 |
greg |
2.6 |
xr->image->bitmap_unit = bm_pad;
|
| 45 |
greg |
2.5 |
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 |
greg |
1.4 |
}
|
| 56 |
greg |
2.5 |
xr->gc = 0;
|
| 57 |
greg |
1.1 |
return(xr);
|
| 58 |
|
|
}
|
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
schorsch |
2.13 |
extern int
|
| 62 |
|
|
init_rcolors( /* initialize colors */
|
| 63 |
|
|
register XRASTER *xr,
|
| 64 |
|
|
BYTE cmap[][3]
|
| 65 |
|
|
)
|
| 66 |
greg |
1.1 |
{
|
| 67 |
|
|
register unsigned char *p;
|
| 68 |
|
|
register int i;
|
| 69 |
|
|
|
| 70 |
schorsch |
2.12 |
if ((xr->image->depth > 8) | (xr->ncolors != 0))
|
| 71 |
greg |
1.1 |
return(xr->ncolors);
|
| 72 |
|
|
xr->pmap = (short *)malloc(256*sizeof(short));
|
| 73 |
|
|
if (xr->pmap == NULL)
|
| 74 |
|
|
return(0);
|
| 75 |
|
|
xr->cdefs = (XColor *)malloc(256*sizeof(XColor));
|
| 76 |
|
|
if (xr->cdefs == NULL)
|
| 77 |
|
|
return(0);
|
| 78 |
|
|
for (i = 0; i < 256; i++)
|
| 79 |
|
|
xr->pmap[i] = -1;
|
| 80 |
|
|
for (p = (unsigned char *)xr->image->data,
|
| 81 |
|
|
i = xr->image->width*xr->image->height;
|
| 82 |
|
|
i--; p++)
|
| 83 |
|
|
if (xr->pmap[*p] == -1) {
|
| 84 |
greg |
2.7 |
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 |
greg |
1.1 |
xr->cdefs[xr->ncolors].pixel = *p;
|
| 88 |
|
|
xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
|
| 89 |
|
|
xr->pmap[*p] = xr->ncolors++;
|
| 90 |
|
|
}
|
| 91 |
greg |
2.11 |
xr->cdefs = (XColor *)realloc((void *)xr->cdefs,
|
| 92 |
greg |
1.1 |
xr->ncolors*sizeof(XColor));
|
| 93 |
|
|
if (xr->cdefs == NULL)
|
| 94 |
|
|
return(0);
|
| 95 |
|
|
return(xr->ncolors);
|
| 96 |
|
|
}
|
| 97 |
|
|
|
| 98 |
greg |
2.5 |
|
| 99 |
schorsch |
2.13 |
extern Colormap
|
| 100 |
|
|
newcmap( /* get colormap and fix b & w */
|
| 101 |
|
|
Display *disp,
|
| 102 |
|
|
int scrn,
|
| 103 |
|
|
Visual *vis
|
| 104 |
|
|
)
|
| 105 |
greg |
1.3 |
{
|
| 106 |
|
|
XColor thiscolor;
|
| 107 |
|
|
unsigned long *pixels;
|
| 108 |
|
|
Colormap cmap;
|
| 109 |
|
|
int n;
|
| 110 |
|
|
register int i, j;
|
| 111 |
greg |
1.1 |
|
| 112 |
greg |
2.9 |
cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone);
|
| 113 |
|
|
if (cmap == 0 || vis->class != PseudoColor)
|
| 114 |
|
|
return(cmap);
|
| 115 |
greg |
1.3 |
pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long));
|
| 116 |
|
|
if (pixels == NULL)
|
| 117 |
|
|
return(0);
|
| 118 |
|
|
for (n = vis->map_entries; n > 0; n--)
|
| 119 |
|
|
if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0)
|
| 120 |
|
|
break;
|
| 121 |
|
|
if (n == 0)
|
| 122 |
|
|
return(0);
|
| 123 |
|
|
/* reset black and white */
|
| 124 |
|
|
for (i = 0; i < n; i++) {
|
| 125 |
|
|
if (pixels[i] != BlackPixel(disp,scrn)
|
| 126 |
|
|
&& pixels[i] != WhitePixel(disp,scrn))
|
| 127 |
|
|
continue;
|
| 128 |
|
|
thiscolor.pixel = pixels[i];
|
| 129 |
|
|
thiscolor.flags = DoRed|DoGreen|DoBlue;
|
| 130 |
|
|
XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor);
|
| 131 |
|
|
XStoreColor(disp, cmap, &thiscolor);
|
| 132 |
|
|
for (j = i; j+1 < n; j++)
|
| 133 |
|
|
pixels[j] = pixels[j+1];
|
| 134 |
|
|
n--;
|
| 135 |
|
|
i--;
|
| 136 |
|
|
}
|
| 137 |
|
|
XFreeColors(disp, cmap, pixels, n, 0);
|
| 138 |
greg |
2.10 |
free((void *)pixels);
|
| 139 |
greg |
1.3 |
return(cmap);
|
| 140 |
|
|
}
|
| 141 |
|
|
|
| 142 |
|
|
|
| 143 |
schorsch |
2.13 |
extern unsigned long *
|
| 144 |
|
|
map_rcolors( /* get and assign pixels */
|
| 145 |
|
|
register XRASTER *xr,
|
| 146 |
|
|
Window w
|
| 147 |
|
|
)
|
| 148 |
greg |
1.1 |
{
|
| 149 |
|
|
register int i;
|
| 150 |
|
|
register unsigned char *p;
|
| 151 |
|
|
|
| 152 |
greg |
2.6 |
if (xr->ncolors == 0 || xr->image->depth > 8)
|
| 153 |
greg |
1.1 |
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 |
greg |
2.9 |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
|
| 163 |
greg |
1.3 |
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 |
2.9 |
xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
|
| 167 |
greg |
1.3 |
else {
|
| 168 |
greg |
2.10 |
free((void *)xr->pixels);
|
| 169 |
greg |
1.3 |
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 |
schorsch |
2.13 |
extern Pixmap
|
| 195 |
|
|
make_rpixmap( /* make pixmap for raster */
|
| 196 |
|
|
register XRASTER *xr,
|
| 197 |
|
|
Window w
|
| 198 |
|
|
)
|
| 199 |
greg |
1.1 |
{
|
| 200 |
greg |
2.6 |
XWindowAttributes xwattr;
|
| 201 |
greg |
1.1 |
Pixmap pm;
|
| 202 |
|
|
|
| 203 |
|
|
if (xr->pm != 0)
|
| 204 |
|
|
return(xr->pm);
|
| 205 |
greg |
2.6 |
XGetWindowAttributes(xr->disp, w, &xwattr);
|
| 206 |
|
|
pm = XCreatePixmap(xr->disp, w,
|
| 207 |
greg |
1.3 |
xr->image->width, xr->image->height,
|
| 208 |
greg |
2.6 |
xwattr.depth);
|
| 209 |
greg |
1.1 |
if (pm == 0)
|
| 210 |
|
|
return(0);
|
| 211 |
|
|
put_raster(pm, 0, 0, xr);
|
| 212 |
|
|
return(xr->pm = pm);
|
| 213 |
greg |
1.2 |
}
|
| 214 |
|
|
|
| 215 |
schorsch |
2.13 |
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 |
greg |
1.2 |
{
|
| 227 |
|
|
if (xsrc >= xr->image->width || ysrc >= xr->image->height)
|
| 228 |
|
|
return;
|
| 229 |
|
|
if (xsrc < 0) {
|
| 230 |
|
|
xdst -= xsrc; width += xsrc;
|
| 231 |
|
|
xsrc = 0;
|
| 232 |
|
|
}
|
| 233 |
|
|
if (ysrc < 0) {
|
| 234 |
|
|
ydst -= ysrc; height += ysrc;
|
| 235 |
|
|
ysrc = 0;
|
| 236 |
|
|
}
|
| 237 |
|
|
if (width <= 0 || height <= 0)
|
| 238 |
|
|
return;
|
| 239 |
|
|
if (xsrc + width > xr->image->width)
|
| 240 |
|
|
width = xr->image->width - xsrc;
|
| 241 |
|
|
if (ysrc + height > xr->image->height)
|
| 242 |
|
|
height = xr->image->height - ysrc;
|
| 243 |
|
|
|
| 244 |
greg |
2.5 |
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 |
greg |
1.2 |
if (xr->pm == 0)
|
| 250 |
|
|
XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc,
|
| 251 |
|
|
xdst, ydst, width, height);
|
| 252 |
|
|
else
|
| 253 |
|
|
XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc,
|
| 254 |
|
|
width, height, xdst, ydst);
|
| 255 |
greg |
1.1 |
}
|
| 256 |
|
|
|
| 257 |
|
|
|
| 258 |
schorsch |
2.13 |
extern void
|
| 259 |
|
|
unmap_rcolors( /* free colors */
|
| 260 |
|
|
register XRASTER *xr
|
| 261 |
|
|
)
|
| 262 |
greg |
1.1 |
{
|
| 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 |
greg |
2.10 |
free((void *)xr->pixels);
|
| 269 |
greg |
1.1 |
xr->pixels = NULL;
|
| 270 |
|
|
}
|
| 271 |
|
|
|
| 272 |
|
|
|
| 273 |
schorsch |
2.13 |
extern void
|
| 274 |
|
|
free_rpixmap( /* release Pixmap */
|
| 275 |
|
|
register XRASTER *xr
|
| 276 |
|
|
)
|
| 277 |
greg |
1.1 |
{
|
| 278 |
|
|
if (xr->pm == 0)
|
| 279 |
|
|
return;
|
| 280 |
|
|
XFreePixmap(xr->disp, xr->pm);
|
| 281 |
|
|
xr->pm = 0;
|
| 282 |
|
|
}
|
| 283 |
|
|
|
| 284 |
|
|
|
| 285 |
schorsch |
2.13 |
extern void
|
| 286 |
|
|
free_raster( /* free raster data */
|
| 287 |
|
|
register XRASTER *xr
|
| 288 |
|
|
)
|
| 289 |
greg |
1.1 |
{
|
| 290 |
|
|
free_rpixmap(xr);
|
| 291 |
|
|
if (xr->ncolors > 0) {
|
| 292 |
|
|
unmap_rcolors(xr);
|
| 293 |
greg |
2.10 |
free((void *)xr->pmap);
|
| 294 |
|
|
free((void *)xr->cdefs);
|
| 295 |
greg |
1.1 |
}
|
| 296 |
|
|
XDestroyImage(xr->image);
|
| 297 |
greg |
2.5 |
if (xr->gc != 0)
|
| 298 |
|
|
XFreeGC(xr->disp, xr->gc);
|
| 299 |
greg |
2.10 |
free((void *)xr);
|
| 300 |
greg |
1.1 |
}
|