Back to index

radiance  4R0+20100331
x11raster.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: x11raster.c,v 2.13 2004/03/28 20:33:14 schorsch Exp $";
00003 #endif
00004 /*
00005  * x11raster.c - routines to handle images for X windows.
00006  *
00007  *     2/18/88
00008  */
00009 
00010 #include <stdio.h>
00011 #include <X11/Xlib.h>
00012 #include <X11/Xutil.h>
00013 
00014 #include "color.h"
00015 #include "x11raster.h"
00016 
00017 
00018 extern XRASTER *
00019 make_raster(
00020        Display       *disp,
00021        XVisualInfo   *vis,
00022        int    npixbits,
00023        char   *data,
00024        int    width, 
00025        int height,
00026        int    bm_pad
00027 )
00028 {
00029        static long   swaptest = 1;
00030        register XRASTER     *xr;
00031 
00032        if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL)
00033               return(NULL);
00034        xr->disp = disp;
00035        xr->screen = vis->screen;
00036        xr->visual = vis->visual;
00037        if (npixbits == 1)
00038               xr->image = XCreateImage(disp,vis->visual,1,
00039                             XYBitmap,0,data,width,height,bm_pad,0);
00040        else
00041               xr->image = XCreateImage(disp,vis->visual,vis->depth,
00042                             ZPixmap,0,data,width,height,bm_pad,0);
00043        xr->image->bitmap_bit_order = MSBFirst;
00044        xr->image->bitmap_unit = bm_pad;
00045        xr->image->byte_order = *(char *)&swaptest ? LSBFirst : MSBFirst;
00046        if (vis->depth >= 24 && (xr->image->red_mask != 0xff ||
00047                      xr->image->green_mask != 0xff00 ||
00048                      xr->image->blue_mask != 0xff0000) &&
00049                      (xr->image->red_mask != 0xff0000 ||
00050                      xr->image->green_mask != 0xff00 ||
00051                      xr->image->blue_mask != 0xff)) {
00052               xr->image->red_mask = 0xff;
00053               xr->image->green_mask = 0xff00;
00054               xr->image->blue_mask = 0xff0000;
00055        }
00056        xr->gc = 0;
00057        return(xr);
00058 }
00059 
00060 
00061 extern int
00062 init_rcolors(               /* initialize colors */
00063        register XRASTER     *xr,
00064        BYTE   cmap[][3]
00065 )
00066 {
00067        register unsigned char      *p;
00068        register int  i;
00069 
00070        if ((xr->image->depth > 8) | (xr->ncolors != 0))
00071               return(xr->ncolors);
00072        xr->pmap = (short *)malloc(256*sizeof(short));
00073        if (xr->pmap == NULL)
00074               return(0);
00075        xr->cdefs = (XColor *)malloc(256*sizeof(XColor));
00076        if (xr->cdefs == NULL)
00077               return(0);
00078        for (i = 0; i < 256; i++)
00079               xr->pmap[i] = -1;
00080        for (p = (unsigned char *)xr->image->data,
00081                      i = xr->image->width*xr->image->height;
00082                      i--; p++)
00083               if (xr->pmap[*p] == -1) {
00084                      xr->cdefs[xr->ncolors].red = cmap[*p][RED] << 8;
00085                      xr->cdefs[xr->ncolors].green = cmap[*p][GRN] << 8;
00086                      xr->cdefs[xr->ncolors].blue = cmap[*p][BLU] << 8;
00087                      xr->cdefs[xr->ncolors].pixel = *p;
00088                      xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
00089                      xr->pmap[*p] = xr->ncolors++;
00090               }
00091        xr->cdefs = (XColor *)realloc((void *)xr->cdefs,
00092                      xr->ncolors*sizeof(XColor));
00093        if (xr->cdefs == NULL)
00094               return(0);
00095        return(xr->ncolors);
00096 }
00097 
00098 
00099 extern Colormap
00100 newcmap(             /* get colormap and fix b & w */
00101        Display       *disp,
00102        int    scrn,
00103        Visual *vis
00104 )
00105 {
00106        XColor thiscolor;
00107        unsigned long *pixels;
00108        Colormap      cmap;
00109        int    n;
00110        register int  i, j;
00111 
00112        cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone);
00113        if (cmap == 0 || vis->class != PseudoColor)
00114               return(cmap);
00115        pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long));
00116        if (pixels == NULL)
00117               return(0);
00118        for (n = vis->map_entries; n > 0; n--)
00119               if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0)
00120                      break;
00121        if (n == 0)
00122               return(0);
00123                                    /* reset black and white */
00124        for (i = 0; i < n; i++) {
00125               if (pixels[i] != BlackPixel(disp,scrn)
00126                             && pixels[i] != WhitePixel(disp,scrn))
00127                      continue;
00128               thiscolor.pixel = pixels[i];
00129               thiscolor.flags = DoRed|DoGreen|DoBlue;
00130               XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor);
00131               XStoreColor(disp, cmap, &thiscolor);
00132               for (j = i; j+1 < n; j++)
00133                      pixels[j] = pixels[j+1];
00134               n--;
00135               i--;
00136        }
00137        XFreeColors(disp, cmap, pixels, n, 0);
00138        free((void *)pixels);
00139        return(cmap);
00140 }
00141 
00142 
00143 extern unsigned long *
00144 map_rcolors(                /* get and assign pixels */
00145        register XRASTER     *xr,
00146        Window w
00147 )
00148 {
00149        register int  i;
00150        register unsigned char      *p;
00151 
00152        if (xr->ncolors == 0 || xr->image->depth > 8)
00153               return(NULL);
00154        if (xr->pixels != NULL)
00155               return(xr->pixels);
00156        xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long));
00157        if (xr->pixels == NULL)
00158               return(NULL);
00159        if (xr->visual == DefaultVisual(xr->disp, xr->screen))
00160               xr->cmap = DefaultColormap(xr->disp, xr->screen);
00161        else
00162               xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
00163        while (XAllocColorCells(xr->disp, xr->cmap, 0,
00164                      NULL, 0, xr->pixels, xr->ncolors) == 0)
00165               if (xr->cmap == DefaultColormap(xr->disp, xr->screen))
00166                      xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
00167               else {
00168                      free((void *)xr->pixels);
00169                      xr->pixels = NULL;
00170                      return(NULL);
00171               }
00172        for (i = 0; i < xr->ncolors; i++)
00173               if (xr->pmap[xr->pixels[i]] == -1)
00174                      break;
00175        if (i < xr->ncolors) {                    /* different pixels */
00176               for (p = (unsigned char *)xr->image->data,
00177                             i = xr->image->width*xr->image->height;
00178                             i--; p++)
00179                      *p = xr->pixels[xr->pmap[*p]];
00180               for (i = 0; i < 256; i++)
00181                      xr->pmap[i] = -1;
00182               for (i = 0; i < xr->ncolors; i++) {
00183                      xr->cdefs[i].pixel = xr->pixels[i];
00184                      xr->pmap[xr->pixels[i]] = i;
00185               }
00186               free_rpixmap(xr);           /* Pixmap invalid */
00187        }
00188        XStoreColors(xr->disp, xr->cmap, xr->cdefs, xr->ncolors);
00189        XSetWindowColormap(xr->disp, w, xr->cmap);
00190        return(xr->pixels);
00191 }
00192 
00193 
00194 extern Pixmap
00195 make_rpixmap(               /* make pixmap for raster */
00196        register XRASTER     *xr,
00197        Window w
00198 )
00199 {
00200        XWindowAttributes    xwattr;
00201        Pixmap pm;
00202 
00203        if (xr->pm != 0)
00204               return(xr->pm);
00205        XGetWindowAttributes(xr->disp, w, &xwattr);
00206        pm = XCreatePixmap(xr->disp, w,
00207                      xr->image->width, xr->image->height,
00208                      xwattr.depth);
00209        if (pm == 0)
00210               return(0);
00211        put_raster(pm, 0, 0, xr);
00212        return(xr->pm = pm);
00213 }
00214 
00215 extern void
00216 patch_raster( /* redraw */
00217        Drawable      d,
00218        int    xsrc,
00219        int    ysrc,
00220        int    xdst,
00221        int    ydst,
00222        int    width,
00223        int    height,
00224        register XRASTER     *xr
00225 )
00226 {
00227        if (xsrc >= xr->image->width || ysrc >= xr->image->height)
00228               return;
00229        if (xsrc < 0) {
00230               xdst -= xsrc; width += xsrc;
00231               xsrc = 0;
00232        }
00233        if (ysrc < 0) {
00234               ydst -= ysrc; height += ysrc;
00235               ysrc = 0;
00236        }
00237        if (width <= 0 || height <= 0)
00238               return;
00239        if (xsrc + width > xr->image->width)
00240               width = xr->image->width - xsrc;
00241        if (ysrc + height > xr->image->height)
00242               height = xr->image->height - ysrc;
00243 
00244        if (xr->gc == 0) {
00245               xr->gc = XCreateGC(xr->disp, d, 0, 0);
00246               XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen),
00247                      WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes);
00248        }
00249        if (xr->pm == 0)
00250               XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc,
00251                             xdst, ydst, width, height);
00252        else
00253               XCopyArea(xr->disp, xr->pm, d, xr->gc, xsrc, ysrc,
00254                             width, height, xdst, ydst);
00255 }
00256 
00257 
00258 extern void
00259 unmap_rcolors(                     /* free colors */
00260        register XRASTER     *xr
00261 )
00262 {
00263        if (xr->pixels == NULL)
00264               return;
00265        XFreeColors(xr->disp, xr->cmap, xr->pixels, xr->ncolors, 0);
00266        if (xr->cmap != DefaultColormap(xr->disp, xr->screen))
00267               XFreeColormap(xr->disp, xr->cmap);
00268        free((void *)xr->pixels);
00269        xr->pixels = NULL;
00270 }
00271 
00272 
00273 extern void
00274 free_rpixmap(               /* release Pixmap */
00275        register XRASTER     *xr
00276 )
00277 {
00278        if (xr->pm == 0)
00279               return;
00280        XFreePixmap(xr->disp, xr->pm);
00281        xr->pm = 0;
00282 }
00283 
00284 
00285 extern void
00286 free_raster(                       /* free raster data */
00287        register XRASTER     *xr
00288 )
00289 {
00290        free_rpixmap(xr);
00291        if (xr->ncolors > 0) {
00292               unmap_rcolors(xr);
00293               free((void *)xr->pmap);
00294               free((void *)xr->cdefs);
00295        }
00296        XDestroyImage(xr->image);
00297        if (xr->gc != 0)
00298               XFreeGC(xr->disp, xr->gc);
00299        free((void *)xr);
00300 }