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.42 by greg, Wed Dec 13 09:52:38 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1995 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 +
81   VIEW  ourview = STDVIEW;                /* image view parameters */
82   int  gotview = 0;                       /* got parameters from file */
83  
# Line 102 | Line 116 | char  *progname;
116  
117   char  errmsg[128];
118  
119 < extern BYTE  clrtab[256][3];            /* global color map */
119 > BYTE  clrtab[256][3];                   /* global color map */
120  
121   extern long  ftell();
122  
123 + extern char  *getenv();
124 +
125   Display  *thedisplay;
126   Atom  closedownAtom, wmProtocolsAtom;
127  
128 + int  sigrecv;
129  
130 + int  onsig() { sigrecv++; }
131 +
132 +
133   main(argc, argv)
134   int  argc;
135   char  *argv[];
136   {
117        extern char  *getenv();
118        char  *gv;
137          int  headline();
138          int  i;
139 +        int  pid;
140          
141          progname = argv[0];
123        if ((gv = getenv("GAMMA")) != NULL)
124                gamcor = atof(gv);
142  
143          for (i = 1; i < argc; i++)
144                  if (argv[i][0] == '-')
145                          switch (argv[i][1]) {
146 <                        case 'c':
146 >                        case 'c':                       /* number of colors */
147                                  maxcolors = atoi(argv[++i]);
148                                  break;
149 <                        case 'b':
149 >                        case 'b':                       /* greyscale only */
150                                  greyscale = !greyscale;
151                                  break;
152 <                        case 'm':
152 >                        case 'm':                       /* monochrome */
153 >                                greyscale = 1;
154                                  maxcolors = 2;
155                                  break;
156 <                        case 'd':
156 >                        case 'd':                       /* display or dither */
157                                  if (argv[i][2] == 'i')
158                                          dispname = argv[++i];
159                                  else
160                                          dither = !dither;
161                                  break;
162 <                        case 'f':
162 >                        case 'f':                       /* save pixmap */
163                                  fast = !fast;
164                                  break;
165 <                        case 'e':
165 >                        case 's':                       /* one at a time */
166 >                                sequential = !sequential;
167 >                                break;
168 >                        case 'o':                       /* 't' output */
169 >                                tout = argv[i]+2;
170 >                                break;
171 >                        case 'e':                       /* exposure comp. */
172                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
173                                          goto userr;
174                                  scale = atoi(argv[++i]);
175                                  break;
176 <                        case 'g':
176 >                        case 'g':                       /* gamma comp. */
177                                  if (argv[i][2] == 'e')
178                                          geometry = argv[++i];
179                                  else
180 <                                        gamcor = atof(argv[++i]);
180 >                                        gamstr = argv[++i];
181                                  break;
182                          default:
183                                  goto userr;
# Line 163 | Line 187 | char  *argv[];
187                  else
188                          break;
189  
190 <        if (i == argc-1) {
190 >        if (i > argc)
191 >                goto userr;
192 >        while (i < argc-1) {
193 >                sigrecv = 0;
194 >                signal(SIGCONT, onsig);
195 >                if ((pid=fork()) == 0) {        /* a child for each picture */
196 >                        parent = -1;
197 >                        break;
198 >                }
199 >                if (pid < 0)
200 >                        quiterr("fork failed");
201 >                parent++;
202 >                while (!sigrecv)
203 >                        pause();        /* wait for wake-up call */
204 >                i++;
205 >        }
206 >        if (i < argc) {                 /* open picture file */
207                  fname = argv[i];
208                  fin = fopen(fname, "r");
209 <                if (fin == NULL) {
210 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
211 <                        quiterr(errmsg);
172 <                }
173 <        } else if (i != argc)
174 <                goto userr;
209 >                if (fin == NULL)
210 >                        quiterr("cannot open picture file");
211 >        }
212                                  /* get header */
213          getheader(fin, headline, NULL);
214                                  /* get picture dimensions */
# Line 191 | Line 228 | char  *argv[];
228                  getevent();             /* main loop */
229   userr:
230          fprintf(stderr,
231 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
231 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s] pic ..\n",
232                          progname);
233          exit(1);
234   }
# Line 241 | Line 278 | char **argv;
278          name += i+1;
279          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
280                  quiterr("cannot open display");
281 +                                /* set gamma value */
282 +        if (gamstr == NULL)             /* get it from the X server */
283 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
284 +        if (gamstr == NULL)             /* get it from the environment */
285 +                gamstr = getenv("DISPLAY_GAMMA");
286 +        if (gamstr != NULL)
287 +                gamcor = atof(gamstr);
288                                  /* get best visual for default screen */
289          getbestvis();
290                                  /* store image */
# Line 267 | Line 311 | char **argv;
311                  }
312          }
313          /* open window */
314 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
315          ourwinattr.border_pixel = ourwhite;
316          ourwinattr.background_pixel = ourblack;
317 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
318 <                        ourvis.visual, AllocNone);
317 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
318 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
319 >                i |= CWColormap;
320 >        }
321          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
322                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
323          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
324          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
325                          xszhints.width, xszhints.height, BORWIDTH,
326 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
327 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
326 >                        ourvis.depth, InputOutput, ourvis.visual,
327 >                        i, &ourwinattr);
328          if (wind == 0)
329                  quiterr("cannot create window");
330          width = xmax;
331          height = ymax;
332 <        xgcv.foreground = ourblack;
286 <        xgcv.background = ourwhite;
332 >        /* prepare graphics drawing context */
333          if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
334                  quiterr("cannot get font");
335 +        xgcv.foreground = ourblack;
336 +        xgcv.background = ourwhite;
337          ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
338                          GCFont, &xgcv);
339          xgcv.function = GXinvert;
# Line 314 | Line 362 | char **argv;
362          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
363  
364          XMapWindow(thedisplay, wind);
317        return;
365   } /* end of init */
366  
367  
368   quiterr(err)            /* print message and exit */
369   char  *err;
370   {
371 <        if (err != NULL) {
372 <                fprintf(stderr, "%s: %s\n", progname, err);
373 <                exit(1);
371 >        register int  es;
372 >        int  cs;
373 >
374 >        if (es = err != NULL)
375 >                fprintf(stderr, "%s: %s: %s\n", progname,
376 >                                fname==NULL?"<stdin>":fname, err);
377 >        if (thedisplay != NULL)
378 >                XCloseDisplay(thedisplay);
379 >        if (parent < 0 & sigrecv == 0)
380 >                kill(getppid(), SIGCONT);
381 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
382 >                if (es == 0)
383 >                        es = cs>>8 & 0xff;
384 >                parent--;
385          }
386 <        exit(0);
386 >        exit(es);
387   }
388  
389  
# Line 466 | Line 524 | getras()                               /* get raster file */
524                          goto fail;
525                  getmono();
526          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
527 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
527 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
528                  if (ourdata == NULL)
529                          goto fail;
530 <                ourras = make_raster(thedisplay, &ourvis, 32,
530 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
531                                  ourdata, xmax, ymax, 32);
532                  if (ourras == NULL)
533                          goto fail;
# Line 512 | Line 570 | getevent()                             /* process the next event */
570                  map_rcolors(ourras, wind);
571                  if (fast)
572                          make_rpixmap(ourras, wind);
573 +                if (!sequential & parent < 0 & sigrecv == 0) {
574 +                        kill(getppid(), SIGCONT);
575 +                        sigrecv--;
576 +                }
577                  break;
578          case UnmapNotify:
579                  if (!fast)
# Line 529 | Line 591 | getevent()                             /* process the next event */
591                  else
592                          getbox(&xev.xbutton);
593                  break;
594 <   case ClientMessage:
594 >        case ClientMessage:
595                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
596                                  (xev.xclient.data.l[0] == closedownAtom))
597                          quiterr(NULL);
# Line 538 | Line 600 | getevent()                             /* process the next event */
600   }
601  
602  
603 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
603 > traceray(xpos, ypos)                    /* print requested pixel data */
604   int  xpos, ypos;
605   {
606 +        extern char  *index();
607          FLOAT  hv[2];
608          FVECT  rorg, rdir;
609 +        COLOR  cval;
610 +        register char  *cp;
611  
612 <        if (!gotview) {         /* no view, no can do */
613 <                XBell(thedisplay, 0);
614 <                return(-1);
615 <        }
612 >        box.xmin = xpos; box.xsiz = 1;
613 >        box.ymin = ypos; box.ysiz = 1;
614 >        avgbox(cval);
615 >        scalecolor(cval, 1./exposure);
616          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
617 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
618 <                return(-1);
619 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
620 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
617 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
618 >                rorg[0] = rorg[1] = rorg[2] =
619 >                rdir[0] = rdir[1] = rdir[2] = 0.;
620 >
621 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
622 >                switch (*cp) {
623 >                case 'o':                       /* origin */
624 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
625 >                        break;
626 >                case 'd':                       /* direction */
627 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
628 >                        break;
629 >                case 'v':                       /* radiance value */
630 >                        printf("%e %e %e ", colval(cval,RED),
631 >                                        colval(cval,GRN), colval(cval,BLU));
632 >                        break;
633 >                case 'l':                       /* luminance */
634 >                        printf("%e ", luminance(cval));
635 >                        break;
636 >                case 'p':                       /* pixel position */
637 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
638 >                                        (int)(hv[1]*inpres.yr));
639 >                        break;
640 >                }
641 >        putchar('\n');
642          fflush(stdout);
643          return(0);
644   }
# Line 627 | Line 713 | XKeyPressedEvent  *ekey;
713          case '@':                               /* adaptation level */
714                  if (avgbox(cval) == -1)
715                          return(-1);
716 <                comp = com=='@'
717 <                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
718 <                : .5/bright(cval) ;
716 >                comp = bright(cval);
717 >                if (comp < 1e-20) {
718 >                        XBell(thedisplay, 0);
719 >                        return(-1);
720 >                }
721 >                if (com == '@')
722 >                        comp = 106./exposure/
723 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
724 >                else
725 >                        comp = .5/comp;
726                  comp = log(comp)/.69315 - scale;
727                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
728                  if (n == 0)
# Line 653 | Line 746 | XKeyPressedEvent  *ekey;
746                          make_rpixmap(ourras, wind);
747                  redraw(0, 0, width, height);
748                  return(0);
749 +        case 'f':                               /* turn on fast redraw */
750 +                fast = 1;
751 +                make_rpixmap(ourras, wind);
752 +                return(0);
753 +        case 'F':                               /* turn off fast redraw */
754 +                fast = 0;
755 +                free_rpixmap(ourras);
756 +                return(0);
757          case '0':                               /* recenter origin */
758                  if (xoff == 0 & yoff == 0)
759                          return(0);
# Line 868 | Line 969 | COLR  *scan;
969   getfull()                       /* get full (24-bit) data */
970   {
971          int     y;
972 <        register unsigned long  *dp;
972 >        register unsigned int4  *dp;
973          register int    x;
974                                          /* set gamma correction */
975          setcolrgam(gamcor);
976                                          /* read and convert file */
977 <        dp = (unsigned long *)ourdata;
977 >        dp = (unsigned int4 *)ourdata;
978          for (y = 0; y < ymax; y++) {
979                  getscan(y);
980                  add2icon(y, scanline);
# Line 882 | Line 983 | getfull()                      /* get full (24-bit) data */
983                  colrs_gambs(scanline, xmax);
984                  if (ourras->image->blue_mask & 1)
985                          for (x = 0; x < xmax; x++)
986 <                                *dp++ = scanline[x][RED] << 16 |
987 <                                        scanline[x][GRN] << 8 |
988 <                                        scanline[x][BLU] ;
986 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
987 >                                        (unsigned int4)scanline[x][GRN] << 8 |
988 >                                        (unsigned int4)scanline[x][BLU] ;
989                  else
990                          for (x = 0; x < xmax; x++)
991 <                                *dp++ = scanline[x][RED] |
992 <                                        scanline[x][GRN] << 8 |
993 <                                        scanline[x][BLU] << 16 ;
991 >                                *dp++ = (unsigned int4)scanline[x][RED] |
992 >                                        (unsigned int4)scanline[x][GRN] << 8 |
993 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
994          }
995   }
996  
# Line 928 | Line 1029 | getgrey()                      /* get greyscale data */
1029   getmapped()                     /* get color-mapped data */
1030   {
1031          int     y;
1032 +                                        /* make sure we can do it first */
1033 +        if (fname == NULL)
1034 +                quiterr("cannot map colors from standard input");
1035                                          /* set gamma correction */
1036          setcolrgam(gamcor);
1037                                          /* make histogram */
1038 <        new_histo();
1038 >        if (new_histo((long)xmax*ymax) == -1)
1039 >                quiterr("cannot initialize histogram");
1040          for (y = 0; y < ymax; y++) {
1041                  if (getscan(y) < 0)
1042 <                        quiterr("seek error in getmapped");
1042 >                        break;
1043                  add2icon(y, scanline);
1044                  if (scale)
1045                          shiftcolrs(scanline, xmax, scale);
# Line 945 | Line 1050 | getmapped()                    /* get color-mapped data */
1050          if (!new_clrtab(maxcolors))
1051                  quiterr("cannot create color map");
1052          for (y = 0; y < ymax; y++) {
1053 <                if (getscan(y) < 0)
949 <                        quiterr("seek error in getmapped");
1053 >                getscan(y);
1054                  if (scale)
1055                          shiftcolrs(scanline, xmax, scale);
1056                  colrs_gambs(scanline, xmax);
# Line 989 | Line 1093 | double sf;
1093   getscan(y)
1094   int  y;
1095   {
1096 +        static int  trunced = -1;               /* truncated file? */
1097 + skipit:
1098 +        if (trunced >= 0 && y >= trunced) {
1099 +                bzero(scanline, xmax*sizeof(COLR));
1100 +                return(-1);
1101 +        }
1102          if (y != cury) {
1103                  if (scanpos == NULL || scanpos[y] == -1)
1104                          return(-1);
# Line 998 | Line 1108 | int  y;
1108          } else if (scanpos != NULL && scanpos[y] == -1)
1109                  scanpos[y] = ftell(fin);
1110  
1111 <        if (freadcolrs(scanline, xmax, fin) < 0)
1112 <                quiterr("read error");
1113 <
1111 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1112 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1113 >                                progname, fname==NULL?"<stdin>":fname);
1114 >                trunced = y;
1115 >                goto skipit;
1116 >        }
1117          cury++;
1118          return(0);
1119   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines