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

Comparing ray/src/rt/x10.c (file contents):
Revision 1.10 by greg, Wed Jun 7 13:22:52 1989 UTC vs.
Revision 1.17 by greg, Wed Oct 25 17:17:09 1989 UTC

# Line 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24  
25   #include  "xtwind.h"
26  
27 < #define GAMMA           2.0             /* exponent for color correction */
27 > #define GAMMA           2.2             /* exponent for color correction */
28  
29   #define BORWIDTH        5               /* border width */
30   #define BARHEIGHT       25              /* menu bar size */
# Line 39 | Line 39 | static char SCCSid[] = "$SunId$ LBL";
39   #define WFLUSH          30              /* flush after this many rays */
40   #endif
41  
42 #define  hashcolr(c)    ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%ncolors)
43
42   #define  checkinp()     while (XPending() > 0) getevent()
43  
44   #define  levptr(etype)  ((etype *)&thisevent)
# Line 49 | Line 47 | static char  *clientname;              /* calling client's name */
47  
48   static XEvent  thisevent;               /* current event */
49  
52 static int  colres = 128;               /* color resolution */
53 static COLR  colrmap[256];              /* our color mapping */
54
50   static int  ncolors = 0;                /* color table size */
51 < static COLR  *colrtbl;                  /* our color table */
57 < static int  *pixval;                    /* associated pixel values */
51 > static int  *pixval;                    /* allocated pixel values */
52  
53   static Display  *ourdisplay = NULL;     /* our display */
54  
# Line 67 | Line 61 | static int  gheight = 0;               /* graphics window height */
61  
62   static TEXTWIND  *comline = NULL;       /* our command line */
63  
70 static int  c_erase, c_kill;            /* erase and kill characters */
71
64   static char  c_queue[64];               /* input queue */
65   static int  c_first = 0;                /* first character in queue */
66   static int  c_last = 0;                 /* last character in queue */
# Line 86 | Line 78 | static struct driver  x_driver = {
78  
79  
80   struct driver *
81 < x_init(name)                    /* initialize driver */
82 < char  *name;
81 > x_init(name, id)                /* initialize driver */
82 > char  *name, *id;
83   {
92        struct sgttyb  ttymode;
93        
94        if (isatty(0)) {
95                ioctl(0, TIOCGETP, &ttymode);
96                c_erase = ttymode.sg_erase;
97                c_kill = ttymode.sg_kill;
98        } else {
99                c_erase = 'H'-'@';
100                c_kill = 'U'-'@';
101        }
84          ourdisplay = XOpenDisplay(NULL);
85          if (ourdisplay == NULL) {
86                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 108 | Line 90 | char  *name;
90                  stderr_v("not enough colors\n");
91                  return(NULL);
92          }
93 <        if (getmap() < 0)                       /* not fatal */
112 <                stderr_v("cannot allocate colors\n");
93 >        make_gmap(GAMMA);                       /* make color map */
94  
95          pickcursor = XCreateCursor(bcross_width, bcross_height,
96                          bcross_bits, bcross_mask_bits,
97                          bcross_x_hot, bcross_y_hot,
98                          BlackPixel, WhitePixel, GXcopy);
99 <        clientname = name;
99 >        clientname = id;
100          x_driver.inpready = 0;
101          cmdvec = x_comout;                      /* set error vectors */
102          if (wrnvec != NULL)
# Line 142 | Line 123 | x_close()                      /* close our display */
123                  gwidth = gheight = 0;
124          }
125          XFreeCursor(pickcursor);
126 <        freemap();
126 >        freepixels();
127          XCloseDisplay(ourdisplay);
128          ourdisplay = NULL;
129   }
# Line 155 | Line 136 | int  xres, yres;
136          if (xres != gwidth || yres != gheight) {        /* new window */
137                  if (comline != NULL)
138                          xt_close(comline);
139 <                if (gwind != 0)
140 <                        XDestroyWindow(gwind);
141 <                gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
142 <                                xres, yres+COMHEIGHT, BORWIDTH,
143 <                                BlackPixmap, BlackPixmap);
144 <                if (gwind == 0)
145 <                        goto fail;
139 >                if (gwind == 0) {
140 >                        gwind = XCreateWindow(RootWindow, 0, BARHEIGHT,
141 >                                        xres, yres+COMHEIGHT, BORWIDTH,
142 >                                        BlackPixmap, BlackPixmap);
143 >                        if (gwind == 0)
144 >                                goto fail;
145 >                        XStoreName(gwind, clientname);
146 >                        XSelectInput(gwind, KeyPressed|ButtonPressed|
147 >                                        ExposeWindow|ExposeRegion|UnmapWindow);
148 >                        XMapWindow(gwind);
149 >                } else
150 >                        XChangeWindow(gwind, xres, yres+COMHEIGHT);
151                  comline = xt_open(gwind, 0, yres, xres, COMHEIGHT, 0, COMFN);
152                  if (comline == NULL)
153                          goto fail;
168                XMapWindow(gwind);
169                XSelectInput(gwind,
170        KeyPressed|ButtonPressed|ExposeWindow|ExposeRegion|UnmapWindow);
171                XStoreName(gwind, clientname);
154                  gwidth = xres;
155                  gheight = yres;
156          } else                                          /* just clear */
157                  XClear(gwind);
158 <        newmap();
159 <        checkinp();
158 >                                                /* reinitialize color table */
159 >        if (ncolors == 0 && getpixels() == 0)
160 >                stderr_v("cannot allocate colors\n");
161 >        else
162 >                new_ctab(ncolors);
163 >        XSync(1);                               /* discard input */
164          return;
165   fail:
166          stderr_v("Failure opening window in x_clear\n");
# Line 188 | Line 174 | COLOR  col;
174   int  xmin, ymin, xmax, ymax;
175   {
176          extern long  nrays;             /* global ray count */
177 +        extern int  xnewcolr();         /* pixel assignment routine */
178          static long  lastflush = 0;     /* ray count at last flush */
192        int  ndx;
179  
180          if (ncolors > 0) {
195                if ((ndx = colindex(col)) < 0) {
196                        colres >>= 1;
197                        redraw();
198                        return;
199                }
181                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
182 <                                pixval[ndx]);
182 >                                pixval[get_pixel(col, xnewcolr)]);
183          }
184          if (nrays - lastflush >= WFLUSH) {
185                  if (ncolors <= 0)       /* output necessary for death */
# Line 216 | Line 197 | char  *inp;
197          int  x_getc(), x_comout();
198  
199          xt_cursor(comline, TBLKCURS);
200 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
200 >        editline(inp, x_getc, x_comout);
201          xt_cursor(comline, TNOCURS);
202   }
203  
# Line 270 | Line 251 | int  *xp, *yp;
251   }
252  
253  
273 static int
274 colindex(col)                   /* return index for color */
275 COLOR  col;
276 {
277        static COLR  clr;
278        int  hval;
279        register int  ndx, i;
280
281        mapcolor(clr, col);
282
283        hval = ndx = hashcolr(clr);
284        
285        for (i = 1; i < ncolors; i++) {
286                if (colrtbl[ndx][EXP] == 0) {
287                        colrtbl[ndx][RED] = clr[RED];
288                        colrtbl[ndx][GRN] = clr[GRN];
289                        colrtbl[ndx][BLU] = clr[BLU];
290                        colrtbl[ndx][EXP] = COLXS;
291                        newcolr(ndx, clr);
292                        return(ndx);
293                }
294                if (            colrtbl[ndx][RED] == clr[RED] &&
295                                colrtbl[ndx][GRN] == clr[GRN] &&
296                                colrtbl[ndx][BLU] == clr[BLU]   )
297                        return(ndx);
298                ndx = (hval + i*i) % ncolors;
299        }
300        return(-1);
301 }
302
303
254   static
255 < newcolr(ndx, clr)               /* enter a color into hardware table */
255 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
256   int  ndx;
257 < COLR  clr;
257 > int  r, g, b;
258   {
259          Color  xcolor;
260  
261          xcolor.pixel = pixval[ndx];
262 <        xcolor.red = clr[RED] << 8;
263 <        xcolor.green = clr[GRN] << 8;
264 <        xcolor.blue = clr[BLU] << 8;
262 >        xcolor.red = r << 8;
263 >        xcolor.green = g << 8;
264 >        xcolor.blue = b << 8;
265  
266          XStoreColor(&xcolor);
267   }
268  
269  
270 < static
271 < mapcolor(clr, col)                      /* map to our color space */
322 < COLR  clr;
323 < COLOR  col;
270 > static int
271 > getpixels()                             /* get the color map */
272   {
325        register int  i, p;
326                                        /* compute color table value */
327        for (i = 0; i < 3; i++) {
328                p = colval(col,i) * 255.0 + 0.5;
329                if (p < 0) p = 0;
330                else if (p > 255) p = 255;
331                clr[i] = colrmap[p][i];
332        }
333        clr[EXP] = COLXS;
334 }
335
336
337 static
338 getmap()                                /* get the color map */
339 {
273          int  planes;
274  
275 +        freepixels();
276          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
343                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
277                  pixval = (int *)malloc(ncolors*sizeof(int));
278 <                if (colrtbl == NULL || pixval == NULL)
279 <                        return(-1);
278 >                if (pixval == NULL)
279 >                        break;
280                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
281 <                        return(0);
349 <                free((char *)colrtbl);
281 >                        return(ncolors);
282                  free((char *)pixval);
283          }
284 <        ncolors = 0;
353 <        return(-1);
284 >        return(ncolors = 0);
285   }
286  
287  
288   static
289 < freemap()                               /* free our color map */
289 > freepixels()                            /* free our pixels */
290   {
291          if (ncolors == 0)
292                  return;
293          XFreeColors(pixval, ncolors, 0);
363        free((char *)colrtbl);
294          free((char *)pixval);
295          ncolors = 0;
296   }
297  
298  
369 static
370 newmap()                                /* initialize the color map */
371 {
372        double  pow();
373        int  val;
374        register int  i;
375
376        for (i = 0; i < 256; i++) {
377                val = pow(i/256.0, 1.0/GAMMA) * colres;
378                val = (val*256 + 128) / colres;
379                colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
380                colrmap[i][EXP] = COLXS;
381        }
382        for (i = 0; i < ncolors; i++)
383                colrtbl[i][EXP] = 0;
384 }
385
386
299   static int
300   x_getc()                        /* get a command character */
301   {
# Line 405 | Line 317 | getevent()                     /* get next event */
317                  getkey(levptr(XKeyPressedEvent));
318                  break;
319          case ExposeWindow:
320 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
321 <                        if (getmap() < 0)
322 <                                stderr_v("cannot grab colors\n");
323 <                        else
324 <                                newmap();
320 >                if (levptr(XExposeEvent)->subwindow == 0) {
321 >                        if (ncolors == 0 && getpixels() == 0) {
322 >                                stderr_v("cannot allocate colors\n");
323 >                                break;
324 >                        }
325 >                        new_ctab(ncolors);
326 >                }
327                  /* fall through */
328          case ExposeRegion:
329                  fixwindow(levptr(XExposeEvent));
330                  break;
331          case UnmapWindow:
332                  if (levptr(XUnmapEvent)->subwindow == 0)
333 <                        freemap();
333 >                        freepixels();
334                  break;
335          case ButtonPressed:             /* handled in x_getcur() */
336                  break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines