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

Comparing ray/src/rt/x10.c (file contents):
Revision 1.3 by greg, Thu May 25 14:53:13 1989 UTC vs.
Revision 1.13 by greg, Tue Oct 3 13:02:57 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)
42 > #define  checkinp()     while (XPending() > 0) getevent()
43  
44 #define  flush()        while (XPending() > 0) getevent()
45
44   #define  levptr(etype)  ((etype *)&thisevent)
45  
46   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 */
67  
68   extern char  *malloc();
69  
70 + int  xnewcolr();
71 +
72   int  x_close(), x_clear(), x_paintr(), x_errout(),
73                  x_getcur(), x_comout(), x_comin();
74  
# Line 89 | Line 83 | struct driver *
83   x_init(name)                    /* initialize driver */
84   char  *name;
85   {
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        }
86          ourdisplay = XOpenDisplay(NULL);
87          if (ourdisplay == NULL) {
88                  stderr_v("cannot open X-windows; DISPLAY variable set?\n");
# Line 108 | Line 92 | char  *name;
92                  stderr_v("not enough colors\n");
93                  return(NULL);
94          }
95 <        if (getmap() < 0)                       /* not fatal */
95 >        make_cmap(GAMMA);                       /* make color map */
96 >        if (getpixels() < 0)                    /* get pixels */
97                  stderr_v("cannot allocate colors\n");
98  
99          pickcursor = XCreateCursor(bcross_width, bcross_height,
# Line 117 | Line 102 | char  *name;
102                          BlackPixel, WhitePixel, GXcopy);
103          clientname = name;
104          x_driver.inpready = 0;
105 <        errvec = x_errout;                      /* set error vectors */
121 <        cmdvec = x_comout;
105 >        cmdvec = x_comout;                      /* set error vectors */
106          if (wrnvec != NULL)
107                  wrnvec = x_errout;
108          return(&x_driver);
# Line 128 | Line 112 | char  *name;
112   static
113   x_close()                       /* close our display */
114   {
115 <        errvec = stderr_v;                      /* reset error vectors */
132 <        cmdvec = NULL;
115 >        cmdvec = NULL;                          /* reset error vectors */
116          if (wrnvec != NULL)
117                  wrnvec = stderr_v;
118          if (ourdisplay == NULL)
# Line 144 | Line 127 | x_close()                      /* close our display */
127                  gwidth = gheight = 0;
128          }
129          XFreeCursor(pickcursor);
130 <        freemap();
130 >        freepixels();
131          XCloseDisplay(ourdisplay);
132          ourdisplay = NULL;
133   }
# Line 175 | Line 158 | int  xres, yres;
158                  gheight = yres;
159          } else                                          /* just clear */
160                  XClear(gwind);
161 <        newmap();
162 <        flush();
161 >        if (new_ctab(ncolors, xnewcolr) == 0) {
162 >                stderr_v("Color allocation error\n");
163 >                quit(1);
164 >        }
165 >        checkinp();
166          return;
167   fail:
168          stderr_v("Failure opening window in x_clear\n");
# Line 191 | Line 177 | int  xmin, ymin, xmax, ymax;
177   {
178          extern long  nrays;             /* global ray count */
179          static long  lastflush = 0;     /* ray count at last flush */
194        int  ndx;
180  
181          if (ncolors > 0) {
197                if ((ndx = colindex(col)) < 0) {
198                        colres >>= 1;
199                        redraw();
200                        return;
201                }
182                  XPixSet(gwind, xmin, gheight-ymax, xmax-xmin, ymax-ymin,
183 <                                pixval[ndx]);
183 >                                pixval[get_pixel(col)]);
184          }
185          if (nrays - lastflush >= WFLUSH) {
186 <                flush();
186 >                if (ncolors <= 0)       /* output necessary for death */
187 >                        XPixSet(gwind,0,0,1,1,BlackPixel);
188 >                checkinp();
189                  lastflush = nrays;
190          }
191   }
# Line 216 | Line 198 | char  *inp;
198          int  x_getc(), x_comout();
199  
200          xt_cursor(comline, TBLKCURS);
201 <        editline(inp, x_getc, x_comout, c_erase, c_kill);
201 >        editline(inp, x_getc, x_comout);
202          xt_cursor(comline, TNOCURS);
203   }
204  
# Line 254 | Line 236 | int  *xp, *yp;
236          *yp = gheight-1 - levptr(XKeyOrButtonEvent)->y;
237          XFocusKeyboard(RootWindow);
238          XUngrabMouse();
239 <        flush();                                /* insure release */
239 >        XFlush();                               /* insure release */
240          if (c_last > c_first)                   /* key pressed */
241                  return(x_getc());
242                                                  /* button pressed */
# Line 270 | Line 252 | int  *xp, *yp;
252   }
253  
254  
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
255   static
256 < newcolr(ndx, clr)               /* enter a color into hardware table */
256 > xnewcolr(ndx, r, g, b)          /* enter a color into hardware table */
257   int  ndx;
258 < COLR  clr;
258 > int  r, g, b;
259   {
260          Color  xcolor;
261  
262          xcolor.pixel = pixval[ndx];
263 <        xcolor.red = clr[RED] << 8;
264 <        xcolor.green = clr[GRN] << 8;
265 <        xcolor.blue = clr[BLU] << 8;
263 >        xcolor.red = r << 8;
264 >        xcolor.green = g << 8;
265 >        xcolor.blue = b << 8;
266  
267          XStoreColor(&xcolor);
268   }
269  
270  
271   static
272 < mapcolor(clr, col)                      /* map to our color space */
322 < COLR  clr;
323 < COLOR  col;
272 > getpixels()                             /* get the color map */
273   {
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 {
274          int  planes;
275  
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);
282                  free((char *)pixval);
283          }
284          ncolors = 0;
# Line 355 | Line 287 | getmap()                               /* get the color map */
287  
288  
289   static
290 < freemap()                               /* free our color map */
290 > freepixels()                            /* free our pixels */
291   {
292          if (ncolors == 0)
293                  return;
294          XFreeColors(pixval, ncolors, 0);
363        free((char *)colrtbl);
295          free((char *)pixval);
296          ncolors = 0;
297   }
298  
299  
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
300   static int
301   x_getc()                        /* get a command character */
302   {
# Line 405 | Line 318 | getevent()                     /* get next event */
318                  getkey(levptr(XKeyPressedEvent));
319                  break;
320          case ExposeWindow:
321 <                if (ncolors == 0 && levptr(XExposeEvent)->subwindow == 0)
322 <                        if (getmap() < 0)
323 <                                stderr_v("cannot grab colors\n");
324 <                        else
325 <                                newmap();
321 >                if (levptr(XExposeEvent)->subwindow == 0) {
322 >                        if (ncolors == 0 && getpixels() < 0) {
323 >                                stderr_v("cannot allocate colors\n");
324 >                                break;
325 >                        }
326 >                        new_ctab(ncolors, xnewcolr);
327 >                }
328                  /* fall through */
329          case ExposeRegion:
330                  fixwindow(levptr(XExposeEvent));
331                  break;
332          case UnmapWindow:
333                  if (levptr(XUnmapEvent)->subwindow == 0)
334 <                        freemap();
334 >                        freepixels();
335                  break;
336          case ButtonPressed:             /* handled in x_getcur() */
337                  break;

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)