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.37 by greg, Fri Jun 10 12:51:21 1994 UTC vs.
Revision 2.46 by greg, Wed Apr 16 20:28:47 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 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27   #include  <X11/Xutil.h>
28   #include  <X11/Xatom.h>
29  
30 < #include  "color.h"
30 > #undef  NOPROTO
31 > #define NOPROTO 1
32 > #include  "tonemap.h"
33   #include  "view.h"
34   #include  "x11raster.h"
35   #include  "random.h"
36   #include  "resolu.h"
37  
36 #ifdef  __alpha
37 #define  int4           int
38 #endif
38   #ifndef  int4
39 < #define  int4           long
39 > #define  int4           int             /* most int's are 32-bit */
40   #endif
41  
42   #define  FONTNAME       "8x13"          /* text font we'll use */
# Line 74 | Line 73 | int  xoff = 0;                         /* x image offset */
73   int  yoff = 0;                          /* y image offset */
74  
75   int  parent = 0;                        /* number of children, -1 if child */
76 + int  sequential = 0;                    /* display images in sequence */
77  
78 + char  *tout = "od";                     /* output of 't' command */
79 + int  tinterv = 0;                       /* interval between mouse reports */
80 +
81 + int  tmflags = -1;                      /* tone mapping flags (-1 for none) */
82 +
83   VIEW  ourview = STDVIEW;                /* image view parameters */
84   int  gotview = 0;                       /* got parameters from file */
85  
86   COLR  *scanline;                        /* scan line buffer */
87 + TMbright  *lscan;                       /* encoded luminance scanline */
88 + BYTE  *cscan;                           /* encoded chroma scanline */
89 + BYTE  *pscan;                           /* compute pixel scanline */
90  
91   RESOLU  inpres;                         /* input resolution and ordering */
92   int  xmax, ymax;                        /* picture dimensions */
# Line 140 | Line 148 | char  *argv[];
148          for (i = 1; i < argc; i++)
149                  if (argv[i][0] == '-')
150                          switch (argv[i][1]) {
151 <                        case 'c':
151 >                        case 'c':                       /* number of colors */
152                                  maxcolors = atoi(argv[++i]);
153                                  break;
154 <                        case 'b':
154 >                        case 'b':                       /* greyscale only */
155                                  greyscale = !greyscale;
156                                  break;
157 <                        case 'm':
157 >                        case 'm':                       /* monochrome */
158 >                                greyscale = 1;
159                                  maxcolors = 2;
160                                  break;
161 <                        case 'd':
161 >                        case 'd':                       /* display or dither */
162                                  if (argv[i][2] == 'i')
163                                          dispname = argv[++i];
164                                  else
165                                          dither = !dither;
166                                  break;
167 <                        case 'f':
167 >                        case 'f':                       /* save pixmap */
168                                  fast = !fast;
169                                  break;
170 <                        case 'e':
171 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
170 >                        case 's':                       /* one at a time */
171 >                                sequential = !sequential;
172 >                                break;
173 >                        case 'o':                       /* 't' output */
174 >                                tout = argv[i]+2;
175 >                                break;
176 >                        case 't':                       /* msec interval */
177 >                                tinterv = atoi(argv[++i]);
178 >                                break;
179 >                        case 'e':                       /* exposure comp. */
180 >                                i++;
181 >                                if (!strcmp(argv[i], "auto")) {
182 >                                        tmflags = TM_F_CAMERA;
183 >                                        break;
184 >                                }
185 >                                if (!strcmp(argv[i], "human")) {
186 >                                        tmflags = TM_F_HUMAN;
187 >                                        break;
188 >                                }
189 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
190                                          goto userr;
191 <                                scale = atoi(argv[++i]);
191 >                                scale = atoi(argv[i]);
192                                  break;
193 <                        case 'g':
193 >                        case 'g':                       /* gamma comp. */
194                                  if (argv[i][2] == 'e')
195                                          geometry = argv[++i];
196                                  else
# Line 214 | Line 241 | char  *argv[];
241  
242          init(argc, argv);                       /* get file and open window */
243  
217        if (parent < 0) {
218                kill(getppid(), SIGCONT);       /* signal parent if child */
219                sigrecv--;
220        }
244          for ( ; ; )
245                  getevent();             /* main loop */
246   userr:
247          fprintf(stderr,
248 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n",
248 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
249                          progname);
250          exit(1);
251   }
# Line 235 | Line 258 | char  *s;
258  
259          if (isexpos(s))
260                  exposure *= exposval(s);
261 <        else if (isformat(s)) {
239 <                formatval(fmt, s);
261 >        else if (formatval(fmt, s))
262                  wrongformat = strcmp(fmt, COLRFMT);
263 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
263 >        else if (isview(s) && sscanview(&ourview, s) > 0)
264                  gotview++;
265   }
266  
# Line 276 | Line 298 | char **argv;
298          if (gamstr == NULL)             /* get it from the X server */
299                  gamstr = XGetDefault(thedisplay, "radiance", "gamma");
300          if (gamstr == NULL)             /* get it from the environment */
301 <                gamstr = getenv("GAMMA");
301 >                gamstr = getenv("DISPLAY_GAMMA");
302          if (gamstr != NULL)
303                  gamcor = atof(gamstr);
304                                  /* get best visual for default screen */
# Line 305 | Line 327 | char **argv;
327                  }
328          }
329          /* open window */
330 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
331          ourwinattr.border_pixel = ourwhite;
332          ourwinattr.background_pixel = ourblack;
333 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
334 <                        ourvis.visual, AllocNone);
333 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
334 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
335 >                i |= CWColormap;
336 >        }
337          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
338                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
339          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
340          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
341                          xszhints.width, xszhints.height, BORWIDTH,
342 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
343 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
342 >                        ourvis.depth, InputOutput, ourvis.visual,
343 >                        i, &ourwinattr);
344          if (wind == 0)
345                  quiterr("cannot create window");
346          width = xmax;
# Line 353 | Line 378 | char **argv;
378          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
379  
380          XMapWindow(thedisplay, wind);
356        return;
381   } /* end of init */
382  
383  
# Line 532 | Line 556 | getras()                               /* get raster file */
556                                  xmax, ymax, 8);
557                  if (ourras == NULL)
558                          goto fail;
559 <                if (greyscale | ourvis.class == StaticGray)
559 >                if (greyscale)
560                          getgrey();
561                  else
562                          getmapped();
# Line 562 | Line 586 | getevent()                             /* process the next event */
586                  map_rcolors(ourras, wind);
587                  if (fast)
588                          make_rpixmap(ourras, wind);
589 +                if (!sequential & parent < 0 & sigrecv == 0) {
590 +                        kill(getppid(), SIGCONT);
591 +                        sigrecv--;
592 +                }
593                  break;
594          case UnmapNotify:
595                  if (!fast)
# Line 574 | Line 602 | getevent()                             /* process the next event */
602          case ButtonPress:
603                  if (xev.xbutton.state & (ShiftMask|ControlMask))
604                          moveimage(&xev.xbutton);
577                else if (xev.xbutton.button == Button2)
578                        traceray(xev.xbutton.x, xev.xbutton.y);
605                  else
606 <                        getbox(&xev.xbutton);
606 >                        switch (xev.xbutton.button) {
607 >                        case Button1:
608 >                                getbox(&xev.xbutton);
609 >                                break;
610 >                        case Button2:
611 >                                traceray(xev.xbutton.x, xev.xbutton.y);
612 >                                break;
613 >                        case Button3:
614 >                                trackrays(&xev.xbutton);
615 >                                break;
616 >                        }
617                  break;
618          case ClientMessage:
619                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
# Line 588 | Line 624 | getevent()                             /* process the next event */
624   }
625  
626  
627 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
627 > traceray(xpos, ypos)                    /* print requested pixel data */
628   int  xpos, ypos;
629   {
630 +        extern char  *index();
631          FLOAT  hv[2];
632          FVECT  rorg, rdir;
633 +        COLOR  cval;
634 +        register char  *cp;
635  
636 <        if (!gotview) {         /* no view, no can do */
637 <                XBell(thedisplay, 0);
638 <                return(-1);
639 <        }
636 >        box.xmin = xpos; box.xsiz = 1;
637 >        box.ymin = ypos; box.ysiz = 1;
638 >        avgbox(cval);
639 >        scalecolor(cval, 1./exposure);
640          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
641 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
642 <                return(-1);
643 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
644 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
641 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
642 >                rorg[0] = rorg[1] = rorg[2] =
643 >                rdir[0] = rdir[1] = rdir[2] = 0.;
644 >
645 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
646 >                switch (*cp) {
647 >                case 'o':                       /* origin */
648 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
649 >                        break;
650 >                case 'd':                       /* direction */
651 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
652 >                        break;
653 >                case 'v':                       /* radiance value */
654 >                        printf("%e %e %e ", colval(cval,RED),
655 >                                        colval(cval,GRN), colval(cval,BLU));
656 >                        break;
657 >                case 'l':                       /* luminance */
658 >                        printf("%e ", luminance(cval));
659 >                        break;
660 >                case 'p':                       /* pixel position */
661 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
662 >                                        (int)(hv[1]*inpres.yr));
663 >                        break;
664 >                }
665 >        putchar('\n');
666          fflush(stdout);
667          return(0);
668   }
# Line 673 | Line 733 | XKeyPressedEvent  *ekey;
733                  return(0);
734          case 't':                               /* trace */
735                  return(traceray(ekey->x, ekey->y));
736 +        case 'a':                               /* auto exposure */
737 +                if (tmflags == TM_F_CAMERA)
738 +                        return(0);
739 +                tmflags = TM_F_CAMERA;
740 +                strcpy(buf, "auto exposure...");
741 +                goto remap;
742 +        case 'h':                               /* human response */
743 +                if (tmflags == TM_F_HUMAN)
744 +                        return(0);
745 +                tmflags = TM_F_HUMAN;
746 +                strcpy(buf, "human exposure...");
747 +                goto remap;
748          case '=':                               /* adjust exposure */
749          case '@':                               /* adaptation level */
750                  if (avgbox(cval) == -1)
# Line 689 | Line 761 | XKeyPressedEvent  *ekey;
761                          comp = .5/comp;
762                  comp = log(comp)/.69315 - scale;
763                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
764 <                if (n == 0)
765 <                        return(0);
766 <                scale_rcolors(ourras, pow(2.0, (double)n));
764 >                if (tmflags != -1)
765 >                        tmflags = -1;           /* turn off tone mapping */
766 >                else {
767 >                        if (n == 0)             /* else check if any change */
768 >                                return(0);
769 >                        scale_rcolors(ourras, pow(2.0, (double)n));
770 >                }
771                  scale += n;
772                  sprintf(buf, "%+d", scale);
773 +        remap:
774                  XDrawImageString(thedisplay, wind, ourgc,
775                                  box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
776                  XFlush(thedisplay);
# Line 788 | Line 865 | XButtonPressedEvent  *ebut;
865   }
866  
867  
868 + trackrays(ebut)                         /* trace rays as mouse moves */
869 + XButtonPressedEvent  *ebut;
870 + {
871 +        XEvent  e;
872 +        unsigned long   lastrept;
873 +
874 +        traceray(ebut->x, ebut->y);
875 +        lastrept = ebut->time;
876 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
877 +        while (e.type == MotionNotify) {
878 +                if (e.xmotion.time >= lastrept + tinterv) {
879 +                        traceray(e.xmotion.x, e.xmotion.y);
880 +                        lastrept = e.xmotion.time;
881 +                }
882 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
883 +        }
884 + }
885 +
886 +
887   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
888   int  x0, y0, x1, y1;
889   {
# Line 846 | Line 942 | COLOR  clr;
942   }
943  
944  
945 + make_tonemap()                  /* initialize tone mapping */
946 + {
947 +        int  y;
948 +
949 +        if (tmflags != -1 && fname == NULL) {
950 +                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
951 +                                progname);
952 +                tmflags = -1;
953 +        }
954 +        if (tmflags == -1) {
955 +                setcolrcor(pow, 1.0/gamcor);
956 +                return;
957 +        }
958 +        if (greyscale)
959 +                tmflags |= TM_F_BW;
960 +                                        /* initialize tm library */
961 +        if (tmInit(tmflags, stdprims, gamcor) == NULL)
962 +                goto memerr;
963 +        if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
964 +                goto tmerr;
965 +                                        /* allocate encoding buffers */
966 +        if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
967 +                goto memerr;
968 +        if (tmflags & TM_F_BW) {
969 +                cscan = TM_NOCHROM;
970 +                if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
971 +                        goto memerr;
972 +        } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL)
973 +                goto memerr;
974 +                                        /* compute picture histogram */
975 +        for (y = 0; y < ymax; y++) {
976 +                getscan(y);
977 +                if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
978 +                        goto tmerr;
979 +                if (tmAddHisto(lscan, xmax, 1))
980 +                        goto tmerr;
981 +        }
982 +                                        /* compute tone mapping */
983 +        if (tmComputeMapping(gamcor, 0., 0.))
984 +                goto tmerr;
985 +        free((char *)lscan);
986 +        return;
987 + memerr:
988 +        quiterr("out of memory in make_tonemap");
989 + tmerr:
990 +        quiterr("tone mapping error");
991 + }
992 +
993 +
994 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
995 + register COLR  *scn;
996 + int  len;
997 + {
998 +        register BYTE  *ps;
999 +
1000 +        if (tmflags == -1) {
1001 +                if (scale)
1002 +                        shiftcolrs(scn, len, scale);
1003 +                colrs_gambs(scn, len);
1004 +                return;
1005 +        }
1006 +        if (len > xmax)
1007 +                quiterr("code error 1 in tmap_colrs");
1008 +        if (tmCvColrs(lscan, cscan, scn, len))
1009 +                goto tmerr;
1010 +        if (tmMapPixels(pscan, lscan, cscan, len))
1011 +                goto tmerr;
1012 +        ps = pscan;
1013 +        if (tmflags & TM_F_BW)
1014 +                while (len--) {
1015 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1016 +                        scn[0][EXP] = COLXS;
1017 +                        scn++;
1018 +                }
1019 +        else
1020 +                while (len--) {
1021 +                        scn[0][RED] = *ps++;
1022 +                        scn[0][GRN] = *ps++;
1023 +                        scn[0][BLU] = *ps++;
1024 +                        scn[0][EXP] = COLXS;
1025 +                        scn++;
1026 +                }
1027 +        return;
1028 + tmerr:
1029 +        quiterr("tone mapping error");
1030 + }
1031 +
1032 +
1033 + done_tonemap()                  /* clean up after tone mapping is done */
1034 + {
1035 +        if (tmflags == -1 || tmTop == NULL)
1036 +                return;
1037 +        tmDone(tmTop);                  /* clear old mapping */
1038 +        free((char *)lscan);            /* free memory */
1039 +        free((char *)pscan);
1040 + }
1041 +
1042 +
1043   getmono()                       /* get monochrome data */
1044   {
1045          register unsigned char  *dp;
# Line 935 | Line 1129 | getfull()                      /* get full (24-bit) data */
1129          int     y;
1130          register unsigned int4  *dp;
1131          register int    x;
1132 <                                        /* set gamma correction */
1133 <        setcolrgam(gamcor);
1132 >                                        /* initialize tone mapping */
1133 >        make_tonemap();
1134                                          /* read and convert file */
1135          dp = (unsigned int4 *)ourdata;
1136          for (y = 0; y < ymax; y++) {
1137                  getscan(y);
1138                  add2icon(y, scanline);
1139 <                if (scale)
946 <                        shiftcolrs(scanline, xmax, scale);
947 <                colrs_gambs(scanline, xmax);
1139 >                tmap_colrs(scanline, xmax);
1140                  if (ourras->image->blue_mask & 1)
1141                          for (x = 0; x < xmax; x++)
1142                                  *dp++ = (unsigned int4)scanline[x][RED] << 16 |
# Line 956 | Line 1148 | getfull()                      /* get full (24-bit) data */
1148                                          (unsigned int4)scanline[x][GRN] << 8 |
1149                                          (unsigned int4)scanline[x][BLU] << 16 ;
1150          }
1151 +        done_tonemap();
1152   }
1153  
1154  
# Line 964 | Line 1157 | getgrey()                      /* get greyscale data */
1157          int     y;
1158          register unsigned char  *dp;
1159          register int    x;
1160 <                                        /* set gamma correction */
1161 <        setcolrgam(gamcor);
1160 >                                        /* initialize tone mapping */
1161 >        make_tonemap();
1162                                          /* read and convert file */
1163          dp = ourdata;
1164          for (y = 0; y < ymax; y++) {
1165                  getscan(y);
1166                  add2icon(y, scanline);
1167 <                if (scale)
975 <                        shiftcolrs(scanline, xmax, scale);
976 <                for (x = 0; x < xmax; x++)
977 <                        scanline[x][GRN] = normbright(scanline[x]);
978 <                colrs_gambs(scanline, xmax);
1167 >                tmap_colrs(scanline, xmax);
1168                  if (maxcolors < 256)
1169                          for (x = 0; x < xmax; x++)
1170 <                                *dp++ = ((long)scanline[x][GRN] *
1170 >                                *dp++ = ((int4)scanline[x][GRN] *
1171                                          maxcolors + maxcolors/2) >> 8;
1172                  else
1173                          for (x = 0; x < xmax; x++)
# Line 986 | Line 1175 | getgrey()                      /* get greyscale data */
1175          }
1176          for (x = 0; x < maxcolors; x++)
1177                  clrtab[x][RED] = clrtab[x][GRN] =
1178 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1178 >                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1179 >        done_tonemap();
1180   }
1181  
1182  
# Line 996 | Line 1186 | getmapped()                    /* get color-mapped data */
1186                                          /* make sure we can do it first */
1187          if (fname == NULL)
1188                  quiterr("cannot map colors from standard input");
1189 <                                        /* set gamma correction */
1190 <        setcolrgam(gamcor);
1189 >                                        /* initialize tone mapping */
1190 >        make_tonemap();
1191                                          /* make histogram */
1192 <        if (new_histo((long)xmax*ymax) == -1)
1192 >        if (new_histo((int4)xmax*ymax) == -1)
1193                  quiterr("cannot initialize histogram");
1194          for (y = 0; y < ymax; y++) {
1195                  if (getscan(y) < 0)
1196                          break;
1197                  add2icon(y, scanline);
1198 <                if (scale)
1009 <                        shiftcolrs(scanline, xmax, scale);
1010 <                colrs_gambs(scanline, xmax);
1198 >                tmap_colrs(scanline, xmax);
1199                  cnt_colrs(scanline, xmax);
1200          }
1201                                          /* map pixels */
# Line 1015 | Line 1203 | getmapped()                    /* get color-mapped data */
1203                  quiterr("cannot create color map");
1204          for (y = 0; y < ymax; y++) {
1205                  getscan(y);
1206 <                if (scale)
1019 <                        shiftcolrs(scanline, xmax, scale);
1020 <                colrs_gambs(scanline, xmax);
1206 >                tmap_colrs(scanline, xmax);
1207                  if (dither)
1208                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1209                  else
1210                          map_colrs(ourdata+y*xmax, scanline, xmax);
1211          }
1212 +        done_tonemap();
1213   }
1214  
1215  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines