| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: x11raster.c,v 2.11 2003/04/23 00:52:34 greg Exp $"; | 
| 3 | #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 | #include "color.h" | 
| 15 | #include "x11raster.h" | 
| 16 |  | 
| 17 |  | 
| 18 | XRASTER * | 
| 19 | make_raster(disp, vis, npixbits, data, width, height, bm_pad) | 
| 20 | Display *disp; | 
| 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 |  | 
| 30 | if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) | 
| 31 | return(NULL); | 
| 32 | xr->disp = disp; | 
| 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, cmap)                  /* initialize colors */ | 
| 61 | register XRASTER        *xr; | 
| 62 | BYTE    cmap[][3]; | 
| 63 | { | 
| 64 | register unsigned char  *p; | 
| 65 | register int    i; | 
| 66 |  | 
| 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) | 
| 71 | return(0); | 
| 72 | xr->cdefs = (XColor *)malloc(256*sizeof(XColor)); | 
| 73 | if (xr->cdefs == NULL) | 
| 74 | return(0); | 
| 75 | for (i = 0; i < 256; i++) | 
| 76 | xr->pmap[i] = -1; | 
| 77 | for (p = (unsigned char *)xr->image->data, | 
| 78 | i = xr->image->width*xr->image->height; | 
| 79 | i--; p++) | 
| 80 | if (xr->pmap[*p] == -1) { | 
| 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++; | 
| 87 | } | 
| 88 | xr->cdefs = (XColor *)realloc((void *)xr->cdefs, | 
| 89 | xr->ncolors*sizeof(XColor)); | 
| 90 | if (xr->cdefs == NULL) | 
| 91 | return(0); | 
| 92 | return(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 */ | 
| 141 | register XRASTER        *xr; | 
| 142 | Window  w; | 
| 143 | { | 
| 144 | register int    i; | 
| 145 | register unsigned char  *p; | 
| 146 |  | 
| 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)) | 
| 155 | xr->cmap = DefaultColormap(xr->disp, xr->screen); | 
| 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 | 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; | 
| 170 | if (i < xr->ncolors) {                  /* different pixels */ | 
| 171 | for (p = (unsigned char *)xr->image->data, | 
| 172 | i = xr->image->width*xr->image->height; | 
| 173 | i--; p++) | 
| 174 | *p = xr->pixels[xr->pmap[*p]]; | 
| 175 | for (i = 0; i < 256; i++) | 
| 176 | xr->pmap[i] = -1; | 
| 177 | for (i = 0; i < xr->ncolors; i++) { | 
| 178 | xr->cdefs[i].pixel = xr->pixels[i]; | 
| 179 | xr->pmap[xr->pixels[i]] = i; | 
| 180 | } | 
| 181 | free_rpixmap(xr);               /* Pixmap invalid */ | 
| 182 | } | 
| 183 | XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors); | 
| 184 | XSetWindowColormap(xr->disp, w, xr->cmap); | 
| 185 | return(xr->pixels); | 
| 186 | } | 
| 187 |  | 
| 188 |  | 
| 189 | Pixmap | 
| 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 | 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); | 
| 206 | return(xr->pm = pm); | 
| 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 | { | 
| 250 | if (xr->pixels == NULL) | 
| 251 | return; | 
| 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((void *)xr->pixels); | 
| 256 | xr->pixels = NULL; | 
| 257 | } | 
| 258 |  | 
| 259 |  | 
| 260 | free_rpixmap(xr)                        /* release Pixmap */ | 
| 261 | register XRASTER        *xr; | 
| 262 | { | 
| 263 | if (xr->pm == 0) | 
| 264 | return; | 
| 265 | XFreePixmap(xr->disp, xr->pm); | 
| 266 | xr->pm = 0; | 
| 267 | } | 
| 268 |  | 
| 269 |  | 
| 270 | free_raster(xr)                         /* free raster data */ | 
| 271 | register XRASTER        *xr; | 
| 272 | { | 
| 273 | free_rpixmap(xr); | 
| 274 | if (xr->ncolors > 0) { | 
| 275 | unmap_rcolors(xr); | 
| 276 | free((void *)xr->pmap); | 
| 277 | free((void *)xr->cdefs); | 
| 278 | } | 
| 279 | XDestroyImage(xr->image); | 
| 280 | if (xr->gc != 0) | 
| 281 | XFreeGC(xr->disp, xr->gc); | 
| 282 | free((void *)xr); | 
| 283 | } |