Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!usc!ucsd!ucbvax!bloom-beacon!SABER.COM!jimf From: jimf@SABER.COM Newsgroups: comp.windows.x Subject: Re: XView questions.. Message-ID: <9005311425.AA13698@lance> Date: 31 May 90 14:25:59 GMT Sender: daemon@athena.mit.edu (Mr Background) Organization: The Internet Lines: 122 | Does anyone know where I can get the xviewsun source.? I'm pretty sure the xviewsun you're talking about was part of my original xbgsun distribution. Xloadimage was supposed to replace xbgsun/xviewsun since there were several flaws in its color handling and image transfer functions. If you really want the source I still have it archived but I don't think it'd be a good teacher. Xloadimage is much better, although it doesn't support TrueColor or DirectColor displays. The apropos code for color handling is all in send.c. There's really not very much. The idea is to try the following in sequence: * try to allocate all needed colors shareable. * if we can't do that and it's a dynamic visual type, create a new colormap. * allocate as many colors as we need or can allocate read/write. * if this fails, modify the image to fit the number of colors we allocated. The code which does this, simplified somewhat, follows (this hasn't been compiled and tested, don't be surprised if something isn't quite right). This doesn't do colormap reduction and assumes you want to use the default visual. -- cut here -- /* code to handle allocation of color entries. this assumes that * COLORMAPSIZE is the size of the colormap you want and you have * red[] green[] and blue[] arrays describing your colormap. disp * and scrn are the X display and screen indicators. */ Visual *visual; unsigned long xpixel[COLORMAPSIZE]; /* array of pixel values X gave us */ XColor xcolor; Colormap cmap; int pixel, b; visual= DefaultVisual(disp, scrn); cmap= DefaultColormap(disp, scrn); xcolor.flags= DoRed | DoGreen | DoBlue; /* these are the only visuals that this code supports */ switch(visual->class) { case PseudoColor: case GrayScale: case StaticColor: case StaticGray: break; default: printf("sendImageToX: unsupported display visual\n"); exit(1); } /* allocate colors shareable (if we can) */ for (pixel= 0; pixel < COLORMAPSIZE; pixel++) { xcolor.red= red[pixel]; xcolor.green= green[pixel]; xcolor.blue= blue[pixel]; if (! XAllocColor(disp, cmap, &xcolor)) if ((visual->class == StaticColor) || (visual->class == StaticGray)) { printf("sendImageToX: XAllocColor failed on a static visual\n"); exit(1); } else { /* shareable color allocation failed. free allocations we had made * and allocate a private colormap. */ for (b= 0; b < pixel; b++) XFreeColors(disp, cmap, xpixel[b], 1, 0); cmap= XCreateColormap(disp, RootWindow(disp, scrn), visual, AllocNone); /* find allocate read/write color cells and count how many we have */ for (pixel= 0; pixel < COLORMAPSIZE; pixel++) if (!XAllocColorCells(disp, cmap, False, NULL, 0, &xpixel[pixel], 1)) break; if (pixel < COLORMAPSIZE) { printf("Can't allocate enough colors!\n"); exit(1); } /* store image colors in private colormap */ for (b= 0; b < pixel; b++) { xcolor.pixel= xpixel[b]; xcolor.red= red[b]; xcolor.green= green[b]; xcolor.blue= blue[b]; XStoreColor(disp, cmap, &xcolor); } break; } xpixel[pixel]= xcolor.pixel; } /* at this point cmap will be the appropriate colormap and xpixel will * map the current pixel values to the pixel values you should use * when talking to the server. */ -- cut here -- If you're doing image processing, you have to modify your image's pixel values to match the new colormap. The code to do this and to transfer the image to a pixmap is also in send.c. I hope this is useful, jim frost saber software jimf@saber.com