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

Comparing ray/src/px/x11image.c (file contents):
Revision 2.26 by greg, Wed May 5 10:27:58 1993 UTC vs.
Revision 2.43 by greg, Mon Jan 27 15:07:03 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1997 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
# Line 32 | Line 33 | static char SCCSid[] = "$SunId$ LBL";
33   #include  "random.h"
34   #include  "resolu.h"
35  
36 + #ifdef  __alpha
37 + #define  int4           int
38 + #endif
39 + #ifndef  int4
40 + #define  int4           long
41 + #endif
42 +
43   #define  FONTNAME       "8x13"          /* text font we'll use */
44  
45   #define  CTRL(c)        ((c)-'@')
# Line 48 | Line 56 | static char SCCSid[] = "$SunId$ LBL";
56   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
57  
58   double  gamcor = 2.2;                   /* gamma correction */
59 + char  *gamstr = NULL;                   /* gamma value override */
60  
61   int  dither = 1;                        /* dither colors? */
62   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 64 | Line 73 | int  scale = 0;                                /* scalefactor; power of two */
73   int  xoff = 0;                          /* x image offset */
74   int  yoff = 0;                          /* y image offset */
75  
76 + int  parent = 0;                        /* number of children, -1 if child */
77 + int  sequential = 0;                    /* display images in sequence */
78 +
79 + char  *tout = "od";                     /* output of 't' command */
80 + int  tinterv = 0;                       /* interval between mouse reports */
81 +
82   VIEW  ourview = STDVIEW;                /* image view parameters */
83   int  gotview = 0;                       /* got parameters from file */
84  
# Line 102 | Line 117 | char  *progname;
117  
118   char  errmsg[128];
119  
120 < extern BYTE  clrtab[256][3];            /* global color map */
120 > BYTE  clrtab[256][3];                   /* global color map */
121  
122   extern long  ftell();
123  
124 + extern char  *getenv();
125 +
126   Display  *thedisplay;
127   Atom  closedownAtom, wmProtocolsAtom;
128  
129 + int  sigrecv;
130  
131 + int  onsig() { sigrecv++; }
132 +
133 +
134   main(argc, argv)
135   int  argc;
136   char  *argv[];
137   {
117        extern char  *getenv();
118        char  *gv;
138          int  headline();
139          int  i;
140 +        int  pid;
141          
142          progname = argv[0];
123        if ((gv = getenv("GAMMA")) != NULL)
124                gamcor = atof(gv);
143  
144          for (i = 1; i < argc; i++)
145                  if (argv[i][0] == '-')
146                          switch (argv[i][1]) {
147 <                        case 'c':
147 >                        case 'c':                       /* number of colors */
148                                  maxcolors = atoi(argv[++i]);
149                                  break;
150 <                        case 'b':
150 >                        case 'b':                       /* greyscale only */
151                                  greyscale = !greyscale;
152                                  break;
153 <                        case 'm':
153 >                        case 'm':                       /* monochrome */
154 >                                greyscale = 1;
155                                  maxcolors = 2;
156                                  break;
157 <                        case 'd':
157 >                        case 'd':                       /* display or dither */
158                                  if (argv[i][2] == 'i')
159                                          dispname = argv[++i];
160                                  else
161                                          dither = !dither;
162                                  break;
163 <                        case 'f':
163 >                        case 'f':                       /* save pixmap */
164                                  fast = !fast;
165                                  break;
166 <                        case 'e':
166 >                        case 's':                       /* one at a time */
167 >                                sequential = !sequential;
168 >                                break;
169 >                        case 'o':                       /* 't' output */
170 >                                tout = argv[i]+2;
171 >                                break;
172 >                        case 't':                       /* msec interval */
173 >                                tinterv = atoi(argv[++i]);
174 >                                break;
175 >                        case 'e':                       /* exposure comp. */
176                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
177                                          goto userr;
178                                  scale = atoi(argv[++i]);
179                                  break;
180 <                        case 'g':
180 >                        case 'g':                       /* gamma comp. */
181                                  if (argv[i][2] == 'e')
182                                          geometry = argv[++i];
183                                  else
184 <                                        gamcor = atof(argv[++i]);
184 >                                        gamstr = argv[++i];
185                                  break;
186                          default:
187                                  goto userr;
# Line 163 | Line 191 | char  *argv[];
191                  else
192                          break;
193  
194 <        if (i == argc-1) {
194 >        if (i > argc)
195 >                goto userr;
196 >        while (i < argc-1) {
197 >                sigrecv = 0;
198 >                signal(SIGCONT, onsig);
199 >                if ((pid=fork()) == 0) {        /* a child for each picture */
200 >                        parent = -1;
201 >                        break;
202 >                }
203 >                if (pid < 0)
204 >                        quiterr("fork failed");
205 >                parent++;
206 >                while (!sigrecv)
207 >                        pause();        /* wait for wake-up call */
208 >                i++;
209 >        }
210 >        if (i < argc) {                 /* open picture file */
211                  fname = argv[i];
212                  fin = fopen(fname, "r");
213 <                if (fin == NULL) {
214 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
215 <                        quiterr(errmsg);
172 <                }
173 <        } else if (i != argc)
174 <                goto userr;
213 >                if (fin == NULL)
214 >                        quiterr("cannot open picture file");
215 >        }
216                                  /* get header */
217          getheader(fin, headline, NULL);
218                                  /* get picture dimensions */
# Line 191 | Line 232 | char  *argv[];
232                  getevent();             /* main loop */
233   userr:
234          fprintf(stderr,
235 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
235 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
236                          progname);
237          exit(1);
238   }
# Line 241 | Line 282 | char **argv;
282          name += i+1;
283          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
284                  quiterr("cannot open display");
285 +                                /* set gamma value */
286 +        if (gamstr == NULL)             /* get it from the X server */
287 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
288 +        if (gamstr == NULL)             /* get it from the environment */
289 +                gamstr = getenv("DISPLAY_GAMMA");
290 +        if (gamstr != NULL)
291 +                gamcor = atof(gamstr);
292                                  /* get best visual for default screen */
293          getbestvis();
294                                  /* store image */
# Line 267 | Line 315 | char **argv;
315                  }
316          }
317          /* open window */
318 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
319          ourwinattr.border_pixel = ourwhite;
320          ourwinattr.background_pixel = ourblack;
321 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
322 <                        ourvis.visual, AllocNone);
321 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
322 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
323 >                i |= CWColormap;
324 >        }
325          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
326                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
327          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
328          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
329                          xszhints.width, xszhints.height, BORWIDTH,
330 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
331 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
330 >                        ourvis.depth, InputOutput, ourvis.visual,
331 >                        i, &ourwinattr);
332          if (wind == 0)
333                  quiterr("cannot create window");
334          width = xmax;
335          height = ymax;
336 <        xgcv.foreground = ourblack;
286 <        xgcv.background = ourwhite;
336 >        /* prepare graphics drawing context */
337          if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
338                  quiterr("cannot get font");
339 +        xgcv.foreground = ourblack;
340 +        xgcv.background = ourwhite;
341          ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
342                          GCFont, &xgcv);
343          xgcv.function = GXinvert;
# Line 314 | Line 366 | char **argv;
366          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
367  
368          XMapWindow(thedisplay, wind);
317        return;
369   } /* end of init */
370  
371  
372   quiterr(err)            /* print message and exit */
373   char  *err;
374   {
375 <        if (err != NULL) {
376 <                fprintf(stderr, "%s: %s\n", progname, err);
377 <                exit(1);
375 >        register int  es;
376 >        int  cs;
377 >
378 >        if (es = err != NULL)
379 >                fprintf(stderr, "%s: %s: %s\n", progname,
380 >                                fname==NULL?"<stdin>":fname, err);
381 >        if (thedisplay != NULL)
382 >                XCloseDisplay(thedisplay);
383 >        if (parent < 0 & sigrecv == 0)
384 >                kill(getppid(), SIGCONT);
385 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
386 >                if (es == 0)
387 >                        es = cs>>8 & 0xff;
388 >                parent--;
389          }
390 <        exit(0);
390 >        exit(es);
391   }
392  
393  
# Line 466 | Line 528 | getras()                               /* get raster file */
528                          goto fail;
529                  getmono();
530          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
531 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
531 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
532                  if (ourdata == NULL)
533                          goto fail;
534 <                ourras = make_raster(thedisplay, &ourvis, 32,
534 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
535                                  ourdata, xmax, ymax, 32);
536                  if (ourras == NULL)
537                          goto fail;
# Line 512 | Line 574 | getevent()                             /* process the next event */
574                  map_rcolors(ourras, wind);
575                  if (fast)
576                          make_rpixmap(ourras, wind);
577 +                if (!sequential & parent < 0 & sigrecv == 0) {
578 +                        kill(getppid(), SIGCONT);
579 +                        sigrecv--;
580 +                }
581                  break;
582          case UnmapNotify:
583                  if (!fast)
# Line 524 | Line 590 | getevent()                             /* process the next event */
590          case ButtonPress:
591                  if (xev.xbutton.state & (ShiftMask|ControlMask))
592                          moveimage(&xev.xbutton);
527                else if (xev.xbutton.button == Button2)
528                        traceray(xev.xbutton.x, xev.xbutton.y);
593                  else
594 <                        getbox(&xev.xbutton);
594 >                        switch (xev.xbutton.button) {
595 >                        case Button1:
596 >                                getbox(&xev.xbutton);
597 >                                break;
598 >                        case Button2:
599 >                                traceray(xev.xbutton.x, xev.xbutton.y);
600 >                                break;
601 >                        case Button3:
602 >                                trackrays(&xev.xbutton);
603 >                                break;
604 >                        }
605                  break;
606 <   case ClientMessage:
606 >        case ClientMessage:
607                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
608                                  (xev.xclient.data.l[0] == closedownAtom))
609                          quiterr(NULL);
# Line 538 | Line 612 | getevent()                             /* process the next event */
612   }
613  
614  
615 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
615 > traceray(xpos, ypos)                    /* print requested pixel data */
616   int  xpos, ypos;
617   {
618 +        extern char  *index();
619          FLOAT  hv[2];
620          FVECT  rorg, rdir;
621 +        COLOR  cval;
622 +        register char  *cp;
623  
624 <        if (!gotview) {         /* no view, no can do */
625 <                XBell(thedisplay, 0);
626 <                return(-1);
627 <        }
624 >        box.xmin = xpos; box.xsiz = 1;
625 >        box.ymin = ypos; box.ysiz = 1;
626 >        avgbox(cval);
627 >        scalecolor(cval, 1./exposure);
628          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
629 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
630 <                return(-1);
631 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
632 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
629 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
630 >                rorg[0] = rorg[1] = rorg[2] =
631 >                rdir[0] = rdir[1] = rdir[2] = 0.;
632 >
633 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
634 >                switch (*cp) {
635 >                case 'o':                       /* origin */
636 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
637 >                        break;
638 >                case 'd':                       /* direction */
639 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
640 >                        break;
641 >                case 'v':                       /* radiance value */
642 >                        printf("%e %e %e ", colval(cval,RED),
643 >                                        colval(cval,GRN), colval(cval,BLU));
644 >                        break;
645 >                case 'l':                       /* luminance */
646 >                        printf("%e ", luminance(cval));
647 >                        break;
648 >                case 'p':                       /* pixel position */
649 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
650 >                                        (int)(hv[1]*inpres.yr));
651 >                        break;
652 >                }
653 >        putchar('\n');
654          fflush(stdout);
655          return(0);
656   }
# Line 627 | Line 725 | XKeyPressedEvent  *ekey;
725          case '@':                               /* adaptation level */
726                  if (avgbox(cval) == -1)
727                          return(-1);
728 <                comp = com=='@'
729 <                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
730 <                : .5/bright(cval) ;
728 >                comp = bright(cval);
729 >                if (comp < 1e-20) {
730 >                        XBell(thedisplay, 0);
731 >                        return(-1);
732 >                }
733 >                if (com == '@')
734 >                        comp = 106./exposure/
735 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
736 >                else
737 >                        comp = .5/comp;
738                  comp = log(comp)/.69315 - scale;
739                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
740                  if (n == 0)
# Line 653 | Line 758 | XKeyPressedEvent  *ekey;
758                          make_rpixmap(ourras, wind);
759                  redraw(0, 0, width, height);
760                  return(0);
761 +        case 'f':                               /* turn on fast redraw */
762 +                fast = 1;
763 +                make_rpixmap(ourras, wind);
764 +                return(0);
765 +        case 'F':                               /* turn off fast redraw */
766 +                fast = 0;
767 +                free_rpixmap(ourras);
768 +                return(0);
769          case '0':                               /* recenter origin */
770                  if (xoff == 0 & yoff == 0)
771                          return(0);
# Line 723 | Line 836 | XButtonPressedEvent  *ebut;
836   }
837  
838  
839 + trackrays(ebut)                         /* trace rays as mouse moves */
840 + XButtonPressedEvent  *ebut;
841 + {
842 +        XEvent  e;
843 +        unsigned long   lastrept;
844 +
845 +        traceray(ebut->x, ebut->y);
846 +        lastrept = ebut->time;
847 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
848 +        while (e.type == MotionNotify) {
849 +                if (e.xmotion.time >= lastrept + tinterv) {
850 +                        traceray(e.xmotion.x, e.xmotion.y);
851 +                        lastrept = e.xmotion.time;
852 +                }
853 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
854 +        }
855 + }
856 +
857 +
858   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
859   int  x0, y0, x1, y1;
860   {
# Line 868 | Line 1000 | COLR  *scan;
1000   getfull()                       /* get full (24-bit) data */
1001   {
1002          int     y;
1003 <        register unsigned long  *dp;
1003 >        register unsigned int4  *dp;
1004          register int    x;
1005                                          /* set gamma correction */
1006          setcolrgam(gamcor);
1007                                          /* read and convert file */
1008 <        dp = (unsigned long *)ourdata;
1008 >        dp = (unsigned int4 *)ourdata;
1009          for (y = 0; y < ymax; y++) {
1010                  getscan(y);
1011                  add2icon(y, scanline);
# Line 882 | Line 1014 | getfull()                      /* get full (24-bit) data */
1014                  colrs_gambs(scanline, xmax);
1015                  if (ourras->image->blue_mask & 1)
1016                          for (x = 0; x < xmax; x++)
1017 <                                *dp++ = scanline[x][RED] << 16 |
1018 <                                        scanline[x][GRN] << 8 |
1019 <                                        scanline[x][BLU] ;
1017 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1018 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1019 >                                        (unsigned int4)scanline[x][BLU] ;
1020                  else
1021                          for (x = 0; x < xmax; x++)
1022 <                                *dp++ = scanline[x][RED] |
1023 <                                        scanline[x][GRN] << 8 |
1024 <                                        scanline[x][BLU] << 16 ;
1022 >                                *dp++ = (unsigned int4)scanline[x][RED] |
1023 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1024 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
1025          }
1026   }
1027  
# Line 928 | Line 1060 | getgrey()                      /* get greyscale data */
1060   getmapped()                     /* get color-mapped data */
1061   {
1062          int     y;
1063 +                                        /* make sure we can do it first */
1064 +        if (fname == NULL)
1065 +                quiterr("cannot map colors from standard input");
1066                                          /* set gamma correction */
1067          setcolrgam(gamcor);
1068                                          /* make histogram */
1069 <        new_histo();
1069 >        if (new_histo((long)xmax*ymax) == -1)
1070 >                quiterr("cannot initialize histogram");
1071          for (y = 0; y < ymax; y++) {
1072                  if (getscan(y) < 0)
1073 <                        quiterr("seek error in getmapped");
1073 >                        break;
1074                  add2icon(y, scanline);
1075                  if (scale)
1076                          shiftcolrs(scanline, xmax, scale);
# Line 945 | Line 1081 | getmapped()                    /* get color-mapped data */
1081          if (!new_clrtab(maxcolors))
1082                  quiterr("cannot create color map");
1083          for (y = 0; y < ymax; y++) {
1084 <                if (getscan(y) < 0)
949 <                        quiterr("seek error in getmapped");
1084 >                getscan(y);
1085                  if (scale)
1086                          shiftcolrs(scanline, xmax, scale);
1087                  colrs_gambs(scanline, xmax);
# Line 989 | Line 1124 | double sf;
1124   getscan(y)
1125   int  y;
1126   {
1127 +        static int  trunced = -1;               /* truncated file? */
1128 + skipit:
1129 +        if (trunced >= 0 && y >= trunced) {
1130 +                bzero(scanline, xmax*sizeof(COLR));
1131 +                return(-1);
1132 +        }
1133          if (y != cury) {
1134                  if (scanpos == NULL || scanpos[y] == -1)
1135                          return(-1);
# Line 998 | Line 1139 | int  y;
1139          } else if (scanpos != NULL && scanpos[y] == -1)
1140                  scanpos[y] = ftell(fin);
1141  
1142 <        if (freadcolrs(scanline, xmax, fin) < 0)
1143 <                quiterr("read error");
1144 <
1142 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1143 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1144 >                                progname, fname==NULL?"<stdin>":fname);
1145 >                trunced = y;
1146 >                goto skipit;
1147 >        }
1148          cury++;
1149          return(0);
1150   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines