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.11 by greg, Fri Sep 29 13:23:59 1989 UTC vs.
Revision 1.14 by greg, Tue Oct 3 14:06:56 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 96 | Line 90 | char  *name;
90                  stderr_v("not enough colors\n");
91                  return(NULL);
92          }
93 <        if (getmap() < 0)                       /* not fatal */
93 >        make_gmap(GAMMA);                       /* make color map */
94 >        if (getpixels() < 0)                    /* get pixels */
95                  stderr_v("cannot allocate colors\n");
96  
97          pickcursor = XCreateCursor(bcross_width, bcross_height,
# Line 130 | Line 125 | x_close()                      /* close our display */
125                  gwidth = gheight = 0;
126          }
127          XFreeCursor(pickcursor);
128 <        freemap();
128 >        freepixels();
129          XCloseDisplay(ourdisplay);
130          ourdisplay = NULL;
131   }
# Line 161 | Line 156 | int  xres, yres;
156                  gheight = yres;
157          } else                                          /* just clear */
158                  XClear(gwind);
159 <        newmap();
159 >        if (new_ctab(ncolors) == 0) {
160 >                stderr_v("Color allocation error\n");
161 >                quit(1);
162 >        }
163          checkinp();
164          return;
165   fail:
# Line 176 | 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 */
180        int  ndx;
179  
180          if (ncolors > 0) {
183                if ((ndx = colindex(col)) < 0) {
184                        colres >>= 1;
185                        redraw();
186                        return;
187                }
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 258 | Line 251 | int  *xp, *yp;
251   }
252  
253  
261 static int
262 colindex(col)                   /* return index for color */
263 COLOR  col;
264 {
265        static COLR  clr;
266        int  hval;
267        register int  ndx, i;
268
269        mapcolor(clr, col);
270
271        hval = ndx = hashcolr(clr);
272        
273        for (i = 1; i < ncolors; i++) {
274                if (colrtbl[ndx][EXP] == 0) {
275                        colrtbl[ndx][RED] = clr[RED];
276                        colrtbl[ndx][GRN] = clr[GRN];
277                        colrtbl[ndx][BLU] = clr[BLU];
278                        colrtbl[ndx][EXP] = COLXS;
279                        newcolr(ndx, clr);
280                        return(ndx);
281                }
282                if (            colrtbl[ndx][RED] == clr[RED] &&
283                                colrtbl[ndx][GRN] == clr[GRN] &&
284                                colrtbl[ndx][BLU] == clr[BLU]   )
285                        return(ndx);
286                ndx = (hval + i*i) % ncolors;
287        }
288        return(-1);
289 }
290
291
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 */
310 < COLR  clr;
311 < COLOR  col;
271 > getpixels()                             /* get the color map */
272   {
313        register int  i, p;
314                                        /* compute color table value */
315        for (i = 0; i < 3; i++) {
316                p = colval(col,i) * 255.0 + 0.5;
317                if (p < 0) p = 0;
318                else if (p > 255) p = 255;
319                clr[i] = colrmap[p][i];
320        }
321        clr[EXP] = COLXS;
322 }
323
324
325 static
326 getmap()                                /* get the color map */
327 {
273          int  planes;
274  
275          for (ncolors=(1<<DisplayPlanes())-3; ncolors>12; ncolors=ncolors*.937){
331                colrtbl = (COLR *)malloc(ncolors*sizeof(COLR));
276                  pixval = (int *)malloc(ncolors*sizeof(int));
277 <                if (colrtbl == NULL || pixval == NULL)
278 <                        return(-1);
277 >                if (pixval == NULL)
278 >                        break;
279                  if (XGetColorCells(0,ncolors,0,&planes,pixval) != 0)
280                          return(0);
337                free((char *)colrtbl);
281                  free((char *)pixval);
282          }
283          ncolors = 0;
# Line 343 | Line 286 | getmap()                               /* get the color map */
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);
351        free((char *)colrtbl);
294          free((char *)pixval);
295          ncolors = 0;
296   }
297  
298  
357 static
358 newmap()                                /* initialize the color map */
359 {
360        double  pow();
361        int  val;
362        register int  i;
363
364        for (i = 0; i < 256; i++) {
365                val = pow(i/256.0, 1.0/GAMMA) * colres;
366                val = (val*256 + 128) / colres;
367                colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val;
368                colrmap[i][EXP] = COLXS;
369        }
370        for (i = 0; i < ncolors; i++)
371                colrtbl[i][EXP] = 0;
372 }
373
374
299   static int
300   x_getc()                        /* get a command character */
301   {
# Line 393 | 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