| 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 | greg | 2.7 | #include "color.h" | 
| 18 | greg | 1.1 | #include "x11raster.h" | 
| 19 |  |  |  | 
| 20 | greg | 2.8 | extern char  *malloc(), *realloc(), *calloc(); | 
| 21 |  |  |  | 
| 22 | greg | 1.1 |  | 
| 23 |  |  | XRASTER * | 
| 24 | greg | 2.5 | make_raster(disp, vis, npixbits, data, width, height, bm_pad) | 
| 25 | greg | 1.1 | Display *disp; | 
| 26 | greg | 2.5 | XVisualInfo     *vis; | 
| 27 |  |  | int     npixbits; | 
| 28 | greg | 1.1 | char    *data; | 
| 29 |  |  | int     width, height; | 
| 30 |  |  | int     bm_pad; | 
| 31 |  |  | { | 
| 32 | greg | 2.5 | static long     swaptest = 1; | 
| 33 | greg | 1.1 | register XRASTER        *xr; | 
| 34 | greg | 2.5 |  | 
| 35 | greg | 1.1 | if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) | 
| 36 |  |  | return(NULL); | 
| 37 |  |  | xr->disp = disp; | 
| 38 | greg | 2.5 | xr->screen = vis->screen; | 
| 39 |  |  | xr->visual = vis->visual; | 
| 40 |  |  | if (npixbits == 1) | 
| 41 |  |  | xr->image = XCreateImage(disp,vis->visual,1, | 
| 42 |  |  | XYBitmap,0,data,width,height,bm_pad,0); | 
| 43 |  |  | else | 
| 44 |  |  | xr->image = XCreateImage(disp,vis->visual,vis->depth, | 
| 45 |  |  | ZPixmap,0,data,width,height,bm_pad,0); | 
| 46 | greg | 2.4 | xr->image->bitmap_bit_order = MSBFirst; | 
| 47 | greg | 2.6 | xr->image->bitmap_unit = bm_pad; | 
| 48 | greg | 2.5 | xr->image->byte_order = *(char *)&swaptest ? LSBFirst : MSBFirst; | 
| 49 |  |  | if (vis->depth >= 24 && (xr->image->red_mask != 0xff || | 
| 50 |  |  | xr->image->green_mask != 0xff00 || | 
| 51 |  |  | xr->image->blue_mask != 0xff0000) && | 
| 52 |  |  | (xr->image->red_mask != 0xff0000 || | 
| 53 |  |  | xr->image->green_mask != 0xff00 || | 
| 54 |  |  | xr->image->blue_mask != 0xff)) { | 
| 55 |  |  | xr->image->red_mask = 0xff; | 
| 56 |  |  | xr->image->green_mask = 0xff00; | 
| 57 |  |  | xr->image->blue_mask = 0xff0000; | 
| 58 | greg | 1.4 | } | 
| 59 | greg | 2.5 | xr->gc = 0; | 
| 60 | greg | 1.1 | return(xr); | 
| 61 |  |  | } | 
| 62 |  |  |  | 
| 63 |  |  |  | 
| 64 |  |  | int | 
| 65 | greg | 2.7 | init_rcolors(xr, cmap)                  /* initialize colors */ | 
| 66 | greg | 1.1 | register XRASTER        *xr; | 
| 67 | greg | 2.7 | BYTE    cmap[][3]; | 
| 68 | greg | 1.1 | { | 
| 69 |  |  | register unsigned char  *p; | 
| 70 |  |  | register int    i; | 
| 71 |  |  |  | 
| 72 | greg | 2.7 | if (xr->image->depth > 8 | xr->ncolors != 0) | 
| 73 | greg | 1.1 | 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 | greg | 2.7 | xr->cdefs[xr->ncolors].red = cmap[*p][RED] << 8; | 
| 87 |  |  | xr->cdefs[xr->ncolors].green = cmap[*p][GRN] << 8; | 
| 88 |  |  | xr->cdefs[xr->ncolors].blue = cmap[*p][BLU] << 8; | 
| 89 | greg | 1.1 | 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 | 2.5 |  | 
| 101 | greg | 1.3 | Colormap | 
| 102 |  |  | newcmap(disp, scrn, w, vis)             /* get colormap and fix b & w */ | 
| 103 |  |  | Display *disp; | 
| 104 |  |  | int     scrn; | 
| 105 |  |  | Window  w; | 
| 106 |  |  | Visual  *vis; | 
| 107 |  |  | { | 
| 108 |  |  | XColor  thiscolor; | 
| 109 |  |  | unsigned long   *pixels; | 
| 110 |  |  | Colormap        cmap; | 
| 111 |  |  | int     n; | 
| 112 |  |  | register int    i, j; | 
| 113 | greg | 1.1 |  | 
| 114 | greg | 1.3 | cmap = XCreateColormap(disp, w, vis, AllocNone); | 
| 115 |  |  | if (cmap == 0) | 
| 116 |  |  | return(0); | 
| 117 |  |  | pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long)); | 
| 118 |  |  | if (pixels == NULL) | 
| 119 |  |  | return(0); | 
| 120 |  |  | for (n = vis->map_entries; n > 0; n--) | 
| 121 |  |  | if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0) | 
| 122 |  |  | break; | 
| 123 |  |  | if (n == 0) | 
| 124 |  |  | return(0); | 
| 125 |  |  | /* reset black and white */ | 
| 126 |  |  | for (i = 0; i < n; i++) { | 
| 127 |  |  | if (pixels[i] != BlackPixel(disp,scrn) | 
| 128 |  |  | && pixels[i] != WhitePixel(disp,scrn)) | 
| 129 |  |  | continue; | 
| 130 |  |  | thiscolor.pixel = pixels[i]; | 
| 131 |  |  | thiscolor.flags = DoRed|DoGreen|DoBlue; | 
| 132 |  |  | XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor); | 
| 133 |  |  | XStoreColor(disp, cmap, &thiscolor); | 
| 134 |  |  | for (j = i; j+1 < n; j++) | 
| 135 |  |  | pixels[j] = pixels[j+1]; | 
| 136 |  |  | n--; | 
| 137 |  |  | i--; | 
| 138 |  |  | } | 
| 139 |  |  | XFreeColors(disp, cmap, pixels, n, 0); | 
| 140 |  |  | free((char *)pixels); | 
| 141 |  |  | return(cmap); | 
| 142 |  |  | } | 
| 143 |  |  |  | 
| 144 |  |  |  | 
| 145 | greg | 1.1 | unsigned long * | 
| 146 |  |  | map_rcolors(xr, w)                              /* get and assign pixels */ | 
| 147 |  |  | register XRASTER        *xr; | 
| 148 |  |  | Window  w; | 
| 149 |  |  | { | 
| 150 |  |  | register int    i; | 
| 151 |  |  | register unsigned char  *p; | 
| 152 |  |  |  | 
| 153 | greg | 2.6 | if (xr->ncolors == 0 || xr->image->depth > 8) | 
| 154 | greg | 1.1 | return(NULL); | 
| 155 |  |  | if (xr->pixels != NULL) | 
| 156 |  |  | return(xr->pixels); | 
| 157 |  |  | xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long)); | 
| 158 |  |  | if (xr->pixels == NULL) | 
| 159 |  |  | return(NULL); | 
| 160 | greg | 1.3 | if (xr->visual == DefaultVisual(xr->disp, xr->screen)) | 
| 161 | greg | 1.1 | xr->cmap = DefaultColormap(xr->disp, xr->screen); | 
| 162 | greg | 1.3 | else | 
| 163 |  |  | xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); | 
| 164 |  |  | while (XAllocColorCells(xr->disp, xr->cmap, 0, | 
| 165 |  |  | NULL, 0, xr->pixels, xr->ncolors) == 0) | 
| 166 | greg | 1.1 | if (xr->cmap == DefaultColormap(xr->disp, xr->screen)) | 
| 167 | greg | 1.3 | xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); | 
| 168 |  |  | else { | 
| 169 |  |  | free((char *)xr->pixels); | 
| 170 |  |  | xr->pixels = NULL; | 
| 171 |  |  | return(NULL); | 
| 172 |  |  | } | 
| 173 | greg | 1.1 | for (i = 0; i < xr->ncolors; i++) | 
| 174 |  |  | if (xr->pmap[xr->pixels[i]] == -1) | 
| 175 |  |  | break; | 
| 176 |  |  | if (i < xr->ncolors) {                  /* different pixels */ | 
| 177 |  |  | for (p = (unsigned char *)xr->image->data, | 
| 178 |  |  | i = xr->image->width*xr->image->height; | 
| 179 |  |  | i--; p++) | 
| 180 |  |  | *p = xr->pixels[xr->pmap[*p]]; | 
| 181 |  |  | for (i = 0; i < 256; i++) | 
| 182 |  |  | xr->pmap[i] = -1; | 
| 183 |  |  | for (i = 0; i < xr->ncolors; i++) { | 
| 184 |  |  | xr->cdefs[i].pixel = xr->pixels[i]; | 
| 185 |  |  | xr->pmap[xr->pixels[i]] = i; | 
| 186 |  |  | } | 
| 187 |  |  | free_rpixmap(xr);               /* Pixmap invalid */ | 
| 188 |  |  | } | 
| 189 |  |  | XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors); | 
| 190 |  |  | XSetWindowColormap(xr->disp, w, xr->cmap); | 
| 191 |  |  | return(xr->pixels); | 
| 192 |  |  | } | 
| 193 |  |  |  | 
| 194 |  |  |  | 
| 195 |  |  | Pixmap | 
| 196 | greg | 2.6 | make_rpixmap(xr, w)                     /* make pixmap for raster */ | 
| 197 | greg | 1.1 | register XRASTER        *xr; | 
| 198 | greg | 2.6 | Window  w; | 
| 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 |  |  |  | 
| 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; | 
| 221 |  |  | { | 
| 222 |  |  | if (xsrc >= xr->image->width || ysrc >= xr->image->height) | 
| 223 |  |  | return; | 
| 224 |  |  | if (xsrc < 0) { | 
| 225 |  |  | xdst -= xsrc; width += xsrc; | 
| 226 |  |  | xsrc = 0; | 
| 227 |  |  | } | 
| 228 |  |  | if (ysrc < 0) { | 
| 229 |  |  | ydst -= ysrc; height += ysrc; | 
| 230 |  |  | ysrc = 0; | 
| 231 |  |  | } | 
| 232 |  |  | if (width <= 0 || height <= 0) | 
| 233 |  |  | return; | 
| 234 |  |  | if (xsrc + width > xr->image->width) | 
| 235 |  |  | width = xr->image->width - xsrc; | 
| 236 |  |  | if (ysrc + height > xr->image->height) | 
| 237 |  |  | height = xr->image->height - ysrc; | 
| 238 |  |  |  | 
| 239 | greg | 2.5 | if (xr->gc == 0) { | 
| 240 |  |  | xr->gc = XCreateGC(xr->disp, d, 0, 0); | 
| 241 |  |  | XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen), | 
| 242 |  |  | WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes); | 
| 243 |  |  | } | 
| 244 | greg | 1.2 | if (xr->pm == 0) | 
| 245 |  |  | XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc, | 
| 246 |  |  | xdst, ydst, width, height); | 
| 247 |  |  | else | 
| 248 |  |  | XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc, | 
| 249 |  |  | width, height, xdst, ydst); | 
| 250 | greg | 1.1 | } | 
| 251 |  |  |  | 
| 252 |  |  |  | 
| 253 |  |  | unmap_rcolors(xr)                       /* free colors */ | 
| 254 |  |  | register XRASTER        *xr; | 
| 255 |  |  | { | 
| 256 |  |  | if (xr->pixels == NULL) | 
| 257 |  |  | return; | 
| 258 |  |  | XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0); | 
| 259 |  |  | if (xr->cmap != DefaultColormap(xr->disp, xr->screen)) | 
| 260 |  |  | XFreeColormap(xr->disp, xr->cmap); | 
| 261 |  |  | free((char *)xr->pixels); | 
| 262 |  |  | xr->pixels = NULL; | 
| 263 |  |  | } | 
| 264 |  |  |  | 
| 265 |  |  |  | 
| 266 |  |  | free_rpixmap(xr)                        /* release Pixmap */ | 
| 267 |  |  | register XRASTER        *xr; | 
| 268 |  |  | { | 
| 269 |  |  | if (xr->pm == 0) | 
| 270 |  |  | return; | 
| 271 |  |  | XFreePixmap(xr->disp, xr->pm); | 
| 272 |  |  | xr->pm = 0; | 
| 273 |  |  | } | 
| 274 |  |  |  | 
| 275 |  |  |  | 
| 276 |  |  | free_raster(xr)                         /* free raster data */ | 
| 277 |  |  | register XRASTER        *xr; | 
| 278 |  |  | { | 
| 279 |  |  | free_rpixmap(xr); | 
| 280 |  |  | if (xr->ncolors > 0) { | 
| 281 |  |  | unmap_rcolors(xr); | 
| 282 |  |  | free((char *)xr->pmap); | 
| 283 |  |  | free((char *)xr->cdefs); | 
| 284 |  |  | } | 
| 285 |  |  | XDestroyImage(xr->image); | 
| 286 | greg | 2.5 | if (xr->gc != 0) | 
| 287 |  |  | XFreeGC(xr->disp, xr->gc); | 
| 288 | greg | 1.1 | free((char *)xr); | 
| 289 |  |  | } |