--- ray/src/px/x11raster.c 1992/05/19 14:22:50 2.4 +++ ray/src/px/x11raster.c 2003/04/23 00:52:34 2.11 @@ -1,9 +1,6 @@ -/* Copyright 1990 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: x11raster.c,v 2.11 2003/04/23 00:52:34 greg Exp $"; #endif - /* * x11raster.c - routines to handle images for X windows. * @@ -14,68 +11,60 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include +#include "color.h" #include "x11raster.h" XRASTER * -make_raster(disp, scrn, depth, data, width, height, bm_pad) +make_raster(disp, vis, npixbits, data, width, height, bm_pad) Display *disp; -int scrn; -int depth; +XVisualInfo *vis; +int npixbits; char *data; int width, height; int bm_pad; { + static long swaptest = 1; register XRASTER *xr; - XVisualInfo ourvinfo; - /* Pick appropriate Visual */ - if (depth == 1) { - ourvinfo.visual = DefaultVisual(disp,scrn); - } else if (depth == 8) { - if (!XMatchVisualInfo(disp,scrn,8,PseudoColor,&ourvinfo)) - return(NULL); - } else if (depth == 24) { - if (!XMatchVisualInfo(disp,scrn,24,TrueColor,&ourvinfo) && - !XMatchVisualInfo(disp,scrn,24,DirectColor,&ourvinfo)) - return(NULL); - } else - return(NULL); + if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL) return(NULL); xr->disp = disp; - xr->screen = scrn; - xr->visual = ourvinfo.visual; - xr->image = XCreateImage(disp,ourvinfo.visual,depth, - depth==1 ? XYBitmap : ZPixmap, - 0,data,width,height,bm_pad,0); + xr->screen = vis->screen; + xr->visual = vis->visual; + if (npixbits == 1) + xr->image = XCreateImage(disp,vis->visual,1, + XYBitmap,0,data,width,height,bm_pad,0); + else + xr->image = XCreateImage(disp,vis->visual,vis->depth, + ZPixmap,0,data,width,height,bm_pad,0); xr->image->bitmap_bit_order = MSBFirst; - xr->image->byte_order = MSBFirst; - xr->image->red_mask = 0xff; - xr->image->green_mask = 0xff00; - xr->image->blue_mask = 0xff0000; - if (xr->image->bits_per_pixel == 32) { - xr->image->bytes_per_line = xr->image->bytes_per_line*24/32; - xr->image->bits_per_pixel = 24; - xr->image->bitmap_unit = 8; - while (xr->image->bytes_per_line % (bm_pad/8)) - xr->image->bytes_per_line++; + xr->image->bitmap_unit = bm_pad; + xr->image->byte_order = *(char *)&swaptest ? LSBFirst : MSBFirst; + if (vis->depth >= 24 && (xr->image->red_mask != 0xff || + xr->image->green_mask != 0xff00 || + xr->image->blue_mask != 0xff0000) && + (xr->image->red_mask != 0xff0000 || + xr->image->green_mask != 0xff00 || + xr->image->blue_mask != 0xff)) { + xr->image->red_mask = 0xff; + xr->image->green_mask = 0xff00; + xr->image->blue_mask = 0xff0000; } - xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0); - XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn), - GXcopy, AllPlanes); + xr->gc = 0; return(xr); } int -init_rcolors(xr, rmap, gmap, bmap) /* initialize colors */ +init_rcolors(xr, cmap) /* initialize colors */ register XRASTER *xr; -int rmap[256], gmap[256], bmap[256]; +BYTE cmap[][3]; { register unsigned char *p; register int i; - if (xr->image->depth != 8 || xr->ncolors != 0) + if (xr->image->depth > 8 | xr->ncolors != 0) return(xr->ncolors); xr->pmap = (short *)malloc(256*sizeof(short)); if (xr->pmap == NULL) @@ -89,25 +78,25 @@ int rmap[256], gmap[256], bmap[256]; i = xr->image->width*xr->image->height; i--; p++) if (xr->pmap[*p] == -1) { - xr->cdefs[xr->ncolors].red = rmap[*p] << 8; - xr->cdefs[xr->ncolors].green = gmap[*p] << 8; - xr->cdefs[xr->ncolors].blue = bmap[*p] << 8; + xr->cdefs[xr->ncolors].red = cmap[*p][RED] << 8; + xr->cdefs[xr->ncolors].green = cmap[*p][GRN] << 8; + xr->cdefs[xr->ncolors].blue = cmap[*p][BLU] << 8; xr->cdefs[xr->ncolors].pixel = *p; xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue; xr->pmap[*p] = xr->ncolors++; } - xr->cdefs = (XColor *)realloc((char *)xr->cdefs, + xr->cdefs = (XColor *)realloc((void *)xr->cdefs, xr->ncolors*sizeof(XColor)); if (xr->cdefs == NULL) return(0); return(xr->ncolors); } + Colormap -newcmap(disp, scrn, w, vis) /* get colormap and fix b & w */ +newcmap(disp, scrn, vis) /* get colormap and fix b & w */ Display *disp; int scrn; -Window w; Visual *vis; { XColor thiscolor; @@ -116,9 +105,9 @@ Visual *vis; int n; register int i, j; - cmap = XCreateColormap(disp, w, vis, AllocNone); - if (cmap == 0) - return(0); + cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone); + if (cmap == 0 || vis->class != PseudoColor) + return(cmap); pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long)); if (pixels == NULL) return(0); @@ -142,20 +131,20 @@ Visual *vis; i--; } XFreeColors(disp, cmap, pixels, n, 0); - free((char *)pixels); + free((void *)pixels); return(cmap); } unsigned long * -map_rcolors(xr, w) /* get and assign pixels */ +map_rcolors(xr, w) /* get and assign pixels */ register XRASTER *xr; Window w; { register int i; register unsigned char *p; - if (xr->ncolors == 0 || xr->image->depth != 8) + if (xr->ncolors == 0 || xr->image->depth > 8) return(NULL); if (xr->pixels != NULL) return(xr->pixels); @@ -165,13 +154,13 @@ Window w; if (xr->visual == DefaultVisual(xr->disp, xr->screen)) xr->cmap = DefaultColormap(xr->disp, xr->screen); else - xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); + xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); while (XAllocColorCells(xr->disp, xr->cmap, 0, NULL, 0, xr->pixels, xr->ncolors) == 0) if (xr->cmap == DefaultColormap(xr->disp, xr->screen)) - xr->cmap = newcmap(xr->disp, xr->screen, w, xr->visual); + xr->cmap = newcmap(xr->disp, xr->screen, xr->visual); else { - free((char *)xr->pixels); + free((void *)xr->pixels); xr->pixels = NULL; return(NULL); } @@ -198,16 +187,19 @@ Window w; Pixmap -make_rpixmap(xr) /* make pixmap for raster */ +make_rpixmap(xr, w) /* make pixmap for raster */ register XRASTER *xr; +Window w; { + XWindowAttributes xwattr; Pixmap pm; if (xr->pm != 0) return(xr->pm); - pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen), + XGetWindowAttributes(xr->disp, w, &xwattr); + pm = XCreatePixmap(xr->disp, w, xr->image->width, xr->image->height, - DisplayPlanes(xr->disp, xr->screen)); + xwattr.depth); if (pm == 0) return(0); put_raster(pm, 0, 0, xr); @@ -238,6 +230,11 @@ register XRASTER *xr; if (ysrc + height > xr->image->height) height = xr->image->height - ysrc; + if (xr->gc == 0) { + xr->gc = XCreateGC(xr->disp, d, 0, 0); + XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen), + WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes); + } if (xr->pm == 0) XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc, xdst, ydst, width, height); @@ -255,7 +252,7 @@ register XRASTER *xr; XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0); if (xr->cmap != DefaultColormap(xr->disp, xr->screen)) XFreeColormap(xr->disp, xr->cmap); - free((char *)xr->pixels); + free((void *)xr->pixels); xr->pixels = NULL; } @@ -276,10 +273,11 @@ register XRASTER *xr; free_rpixmap(xr); if (xr->ncolors > 0) { unmap_rcolors(xr); - free((char *)xr->pmap); - free((char *)xr->cdefs); + free((void *)xr->pmap); + free((void *)xr->cdefs); } XDestroyImage(xr->image); - XFreeGC(xr->disp, xr->gc); - free((char *)xr); + if (xr->gc != 0) + XFreeGC(xr->disp, xr->gc); + free((void *)xr); }