ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11raster.c
(Generate patch)

Comparing ray/src/px/x11raster.c (file contents):
Revision 1.2 by greg, Thu Mar 1 18:13:34 1990 UTC vs.
Revision 2.9 by greg, Wed Nov 23 14:24:32 1994 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14   #include <X11/Xlib.h>
15   #include <X11/Xutil.h>
16  
17 + #include "color.h"
18   #include "x11raster.h"
19  
20 + extern char  *malloc(), *realloc(), *calloc();
21  
22 +
23   XRASTER *
24 < make_raster(disp, scrn, depth, data, width, height, bm_pad)
24 > make_raster(disp, vis, npixbits, data, width, height, bm_pad)
25   Display *disp;
26 < int     scrn;
27 < int     depth;
26 > XVisualInfo     *vis;
27 > int     npixbits;
28   char    *data;
29   int     width, height;
30   int     bm_pad;
31   {
32 +        static long     swaptest = 1;
33          register XRASTER        *xr;
34 <        XVisualInfo     ourvinfo;
31 <                                                /* Pick appropriate Visual */
32 <        if (depth == 1) {
33 <                ourvinfo.visual = DefaultVisual(disp,scrn);
34 <        } else if (depth == 8) {
35 <                if (!XMatchVisualInfo(disp,scrn,8,PseudoColor,&ourvinfo))
36 <                        return(NULL);
37 <        } else if (depth > 12) {
38 <                if (!XMatchVisualInfo(disp,scrn,24,TrueColor,&ourvinfo))
39 <                        return(NULL);
40 <        } else
41 <                return(NULL);
34 >
35          if ((xr = (XRASTER *)calloc(1, sizeof(XRASTER))) == NULL)
36                  return(NULL);
37          xr->disp = disp;
38 <        xr->screen = scrn;
39 <        xr->visual = ourvinfo.visual;
40 <        xr->image = XCreateImage(disp,ourvinfo.visual,depth,
41 <                        ZPixmap,0,data,width,height,bm_pad,0);
42 <        xr->gc = XCreateGC(disp, RootWindow(disp,scrn), 0, 0);
43 <        XSetState(disp, xr->gc, BlackPixel(disp,scrn), WhitePixel(disp,scrn),
44 <                        GXcopy, AllPlanes);
38 >        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 >        xr->image->bitmap_bit_order = MSBFirst;
47 >        xr->image->bitmap_unit = bm_pad;
48 >        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 >        }
59 >        xr->gc = 0;
60          return(xr);
61   }
62  
63  
64   int
65 < init_rcolors(xr, rmap, gmap, bmap)              /* initialize colors */
65 > init_rcolors(xr, cmap)                  /* initialize colors */
66   register XRASTER        *xr;
67 < unsigned char   rmap[256], gmap[256], bmap[256];
67 > BYTE    cmap[][3];
68   {
69          register unsigned char  *p;
70          register int    i;
71  
72 <        if (xr->image->depth != 8 || xr->ncolors != 0)
72 >        if (xr->image->depth > 8 | xr->ncolors != 0)
73                  return(xr->ncolors);
74          xr->pmap = (short *)malloc(256*sizeof(short));
75          if (xr->pmap == NULL)
# Line 75 | Line 83 | unsigned char  rmap[256], gmap[256], bmap[256];
83                          i = xr->image->width*xr->image->height;
84                          i--; p++)
85                  if (xr->pmap[*p] == -1) {
86 <                        xr->cdefs[xr->ncolors].red = rmap[*p] << 8;
87 <                        xr->cdefs[xr->ncolors].green = gmap[*p] << 8;
88 <                        xr->cdefs[xr->ncolors].blue = bmap[*p] << 8;
86 >                        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                          xr->cdefs[xr->ncolors].pixel = *p;
90                          xr->cdefs[xr->ncolors].flags = DoRed|DoGreen|DoBlue;
91                          xr->pmap[*p] = xr->ncolors++;
# Line 90 | Line 98 | unsigned char  rmap[256], gmap[256], bmap[256];
98   }
99  
100  
101 + Colormap
102 + newcmap(disp, scrn, vis)                /* get colormap and fix b & w */
103 + Display *disp;
104 + int     scrn;
105 + Visual  *vis;
106 + {
107 +        XColor  thiscolor;
108 +        unsigned long   *pixels;
109 +        Colormap        cmap;
110 +        int     n;
111 +        register int    i, j;
112 +
113 +        cmap = XCreateColormap(disp, RootWindow(disp,scrn), vis, AllocNone);
114 +        if (cmap == 0 || vis->class != PseudoColor)
115 +                return(cmap);
116 +        pixels=(unsigned long *)malloc(vis->map_entries*sizeof(unsigned long));
117 +        if (pixels == NULL)
118 +                return(0);
119 +        for (n = vis->map_entries; n > 0; n--)
120 +                if (XAllocColorCells(disp, cmap, 0, NULL, 0, pixels, n) != 0)
121 +                        break;
122 +        if (n == 0)
123 +                return(0);
124 +                                        /* reset black and white */
125 +        for (i = 0; i < n; i++) {
126 +                if (pixels[i] != BlackPixel(disp,scrn)
127 +                                && pixels[i] != WhitePixel(disp,scrn))
128 +                        continue;
129 +                thiscolor.pixel = pixels[i];
130 +                thiscolor.flags = DoRed|DoGreen|DoBlue;
131 +                XQueryColor(disp, DefaultColormap(disp,scrn), &thiscolor);
132 +                XStoreColor(disp, cmap, &thiscolor);
133 +                for (j = i; j+1 < n; j++)
134 +                        pixels[j] = pixels[j+1];
135 +                n--;
136 +                i--;
137 +        }
138 +        XFreeColors(disp, cmap, pixels, n, 0);
139 +        free((char *)pixels);
140 +        return(cmap);
141 + }
142 +
143 +
144   unsigned long *
145 < map_rcolors(xr, w)                              /* get and assign pixels */
145 > map_rcolors(xr, w)                      /* get and assign pixels */
146   register XRASTER        *xr;
147   Window  w;
148   {
149          register int    i;
150          register unsigned char  *p;
100        int     j;
151  
152 <        if (xr->ncolors == 0 || xr->image->depth != 8)
152 >        if (xr->ncolors == 0 || xr->image->depth > 8)
153                  return(NULL);
154          if (xr->pixels != NULL)
155                  return(xr->pixels);
156          xr->pixels = (unsigned long *)malloc(xr->ncolors*sizeof(unsigned long));
157          if (xr->pixels == NULL)
158                  return(NULL);
159 <        if (xr->visual == DefaultVisual(xr->disp, xr->screen)) {
159 >        if (xr->visual == DefaultVisual(xr->disp, xr->screen))
160                  xr->cmap = DefaultColormap(xr->disp, xr->screen);
161 <                goto gotmap;
162 <        }
163 < getmap:
164 <        xr->cmap = XCreateColormap(xr->disp, w, xr->visual, AllocNone);
115 < gotmap:
116 <        if (XAllocColorCells(xr->disp, xr->cmap, 0,
117 <                        &j, 0, xr->pixels, xr->ncolors) == 0) {
161 >        else
162 >                xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
163 >        while (XAllocColorCells(xr->disp, xr->cmap, 0,
164 >                        NULL, 0, xr->pixels, xr->ncolors) == 0)
165                  if (xr->cmap == DefaultColormap(xr->disp, xr->screen))
166 <                        goto getmap;
167 <                free((char *)xr->pixels);
168 <                xr->pixels = NULL;
169 <                return(NULL);
170 <        }
166 >                        xr->cmap = newcmap(xr->disp, xr->screen, xr->visual);
167 >                else {
168 >                        free((char *)xr->pixels);
169 >                        xr->pixels = NULL;
170 >                        return(NULL);
171 >                }
172          for (i = 0; i < xr->ncolors; i++)
173                  if (xr->pmap[xr->pixels[i]] == -1)
174                          break;
# Line 144 | Line 192 | gotmap:
192  
193  
194   Pixmap
195 < make_rpixmap(xr)                        /* make pixmap for raster */
195 > make_rpixmap(xr, w)                     /* make pixmap for raster */
196   register XRASTER        *xr;
197 + Window  w;
198   {
199 +        XWindowAttributes       xwattr;
200          Pixmap  pm;
201  
202          if (xr->pm != 0)
203                  return(xr->pm);
204 <        pm = XCreatePixmap(xr->disp, RootWindow(xr->disp, xr->screen),
205 <                        xr->image->width, xr->image->height, xr->image->depth);
204 >        XGetWindowAttributes(xr->disp, w, &xwattr);
205 >        pm = XCreatePixmap(xr->disp, w,
206 >                        xr->image->width, xr->image->height,
207 >                        xwattr.depth);
208          if (pm == 0)
209                  return(0);
210          put_raster(pm, 0, 0, xr);
# Line 183 | Line 235 | register XRASTER       *xr;
235          if (ysrc + height > xr->image->height)
236                  height = xr->image->height - ysrc;
237  
238 +        if (xr->gc == 0) {
239 +                xr->gc = XCreateGC(xr->disp, d, 0, 0);
240 +                XSetState(xr->disp, xr->gc, BlackPixel(xr->disp,xr->screen),
241 +                        WhitePixel(xr->disp,xr->screen), GXcopy, AllPlanes);
242 +        }
243          if (xr->pm == 0)
244                  XPutImage(xr->disp, d, xr->gc, xr->image, xsrc, ysrc,
245                                  xdst, ydst, width, height);
# Line 225 | Line 282 | register XRASTER       *xr;
282                  free((char *)xr->cdefs);
283          }
284          XDestroyImage(xr->image);
285 <        XFreeGC(xr->disp, xr->gc);
285 >        if (xr->gc != 0)
286 >                XFreeGC(xr->disp, xr->gc);
287          free((char *)xr);
288   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines