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.42 by greg, Wed Dec 13 09:52:38 1995 UTC vs.
Revision 2.49 by greg, Thu Apr 24 17:29:53 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1995 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 77 | Line 76 | 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 168 | Line 173 | char  *argv[];
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 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
180 >                                i++;
181 >                                if (argv[i][0] == 'a') {
182 >                                        tmflags = TM_F_CAMERA;
183 >                                        break;
184 >                                }
185 >                                if (argv[i][0] == 'h') {
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':                       /* gamma comp. */
194                                  if (argv[i][2] == 'e')
# Line 228 | Line 245 | char  *argv[];
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][-s] 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 241 | Line 258 | char  *s;
258  
259          if (isexpos(s))
260                  exposure *= exposval(s);
261 <        else if (isformat(s)) {
245 <                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 540 | 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 586 | Line 602 | getevent()                             /* process the next event */
602          case ButtonPress:
603                  if (xev.xbutton.state & (ShiftMask|ControlMask))
604                          moveimage(&xev.xbutton);
589                else if (xev.xbutton.button == Button2)
590                        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 709 | 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 725 | 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 824 | 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 882 | Line 942 | COLOR  clr;
942   }
943  
944  
945 + make_tonemap()                  /* initialize tone mapping */
946 + {
947 +        int  flags, 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) {            /* linear with clamping */
955 +                setcolrcor(pow, 1.0/gamcor);
956 +                return;
957 +        }
958 +        flags = tmflags;                /* histogram adjustment */
959 +        if (greyscale) flags |= TM_F_BW;
960 +        if (tmTop != NULL) {            /* reuse old histogram if one */
961 +                tmTop->flags = flags;
962 +        } else {                        /* else initialize */
963 +                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
964 +                        goto memerr;
965 +                if (greyscale) {
966 +                        cscan = TM_NOCHROM;
967 +                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
968 +                                goto memerr;
969 +                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
970 +                                == NULL)
971 +                        goto memerr;
972 +                                                /* initialize tm library */
973 +                if (tmInit(flags, stdprims, gamcor) == NULL)
974 +                        goto memerr;
975 +                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
976 +                        goto tmerr;
977 +                                                /* compute picture histogram */
978 +                for (y = 0; y < ymax; y++) {
979 +                        getscan(y);
980 +                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
981 +                                goto tmerr;
982 +                        if (tmAddHisto(lscan, xmax, 1))
983 +                                goto tmerr;
984 +                }
985 +        }
986 +                                        /* (re)compute tone mapping */
987 +        if (tmComputeMapping(gamcor, 0., 0.))
988 +                goto tmerr;
989 +        return;
990 + memerr:
991 +        quiterr("out of memory in make_tonemap");
992 + tmerr:
993 +        quiterr("tone mapping error");
994 + }
995 +
996 +
997 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
998 + register COLR  *scn;
999 + int  len;
1000 + {
1001 +        register BYTE  *ps;
1002 +
1003 +        if (tmflags == -1) {
1004 +                if (scale)
1005 +                        shiftcolrs(scn, len, scale);
1006 +                colrs_gambs(scn, len);
1007 +                return;
1008 +        }
1009 +        if (len > xmax)
1010 +                quiterr("code error 1 in tmap_colrs");
1011 +        if (tmCvColrs(lscan, cscan, scn, len))
1012 +                goto tmerr;
1013 +        if (tmMapPixels(pscan, lscan, cscan, len))
1014 +                goto tmerr;
1015 +        ps = pscan;
1016 +        if (greyscale)
1017 +                while (len--) {
1018 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1019 +                        scn[0][EXP] = COLXS;
1020 +                        scn++;
1021 +                }
1022 +        else
1023 +                while (len--) {
1024 +                        scn[0][RED] = *ps++;
1025 +                        scn[0][GRN] = *ps++;
1026 +                        scn[0][BLU] = *ps++;
1027 +                        scn[0][EXP] = COLXS;
1028 +                        scn++;
1029 +                }
1030 +        return;
1031 + tmerr:
1032 +        quiterr("tone mapping error");
1033 + }
1034 +
1035 +
1036   getmono()                       /* get monochrome data */
1037   {
1038          register unsigned char  *dp;
# Line 971 | Line 1122 | getfull()                      /* get full (24-bit) data */
1122          int     y;
1123          register unsigned int4  *dp;
1124          register int    x;
1125 <                                        /* set gamma correction */
1126 <        setcolrgam(gamcor);
1125 >                                        /* initialize tone mapping */
1126 >        make_tonemap();
1127                                          /* read and convert file */
1128          dp = (unsigned int4 *)ourdata;
1129          for (y = 0; y < ymax; y++) {
1130                  getscan(y);
1131                  add2icon(y, scanline);
1132 <                if (scale)
982 <                        shiftcolrs(scanline, xmax, scale);
983 <                colrs_gambs(scanline, xmax);
1132 >                tmap_colrs(scanline, xmax);
1133                  if (ourras->image->blue_mask & 1)
1134                          for (x = 0; x < xmax; x++)
1135                                  *dp++ = (unsigned int4)scanline[x][RED] << 16 |
# Line 1000 | Line 1149 | getgrey()                      /* get greyscale data */
1149          int     y;
1150          register unsigned char  *dp;
1151          register int    x;
1152 <                                        /* set gamma correction */
1153 <        setcolrgam(gamcor);
1152 >                                        /* initialize tone mapping */
1153 >        make_tonemap();
1154                                          /* read and convert file */
1155          dp = ourdata;
1156          for (y = 0; y < ymax; y++) {
1157                  getscan(y);
1158                  add2icon(y, scanline);
1159 <                if (scale)
1011 <                        shiftcolrs(scanline, xmax, scale);
1012 <                for (x = 0; x < xmax; x++)
1013 <                        scanline[x][GRN] = normbright(scanline[x]);
1014 <                colrs_gambs(scanline, xmax);
1159 >                tmap_colrs(scanline, xmax);
1160                  if (maxcolors < 256)
1161                          for (x = 0; x < xmax; x++)
1162 <                                *dp++ = ((long)scanline[x][GRN] *
1162 >                                *dp++ = ((int4)scanline[x][GRN] *
1163                                          maxcolors + maxcolors/2) >> 8;
1164                  else
1165                          for (x = 0; x < xmax; x++)
# Line 1022 | Line 1167 | getgrey()                      /* get greyscale data */
1167          }
1168          for (x = 0; x < maxcolors; x++)
1169                  clrtab[x][RED] = clrtab[x][GRN] =
1170 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1170 >                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1171   }
1172  
1173  
# Line 1032 | Line 1177 | getmapped()                    /* get color-mapped data */
1177                                          /* make sure we can do it first */
1178          if (fname == NULL)
1179                  quiterr("cannot map colors from standard input");
1180 <                                        /* set gamma correction */
1181 <        setcolrgam(gamcor);
1180 >                                        /* initialize tone mapping */
1181 >        make_tonemap();
1182                                          /* make histogram */
1183 <        if (new_histo((long)xmax*ymax) == -1)
1183 >        if (new_histo((int4)xmax*ymax) == -1)
1184                  quiterr("cannot initialize histogram");
1185          for (y = 0; y < ymax; y++) {
1186                  if (getscan(y) < 0)
1187                          break;
1188                  add2icon(y, scanline);
1189 <                if (scale)
1045 <                        shiftcolrs(scanline, xmax, scale);
1046 <                colrs_gambs(scanline, xmax);
1189 >                tmap_colrs(scanline, xmax);
1190                  cnt_colrs(scanline, xmax);
1191          }
1192                                          /* map pixels */
# Line 1051 | Line 1194 | getmapped()                    /* get color-mapped data */
1194                  quiterr("cannot create color map");
1195          for (y = 0; y < ymax; y++) {
1196                  getscan(y);
1197 <                if (scale)
1055 <                        shiftcolrs(scanline, xmax, scale);
1056 <                colrs_gambs(scanline, xmax);
1197 >                tmap_colrs(scanline, xmax);
1198                  if (dither)
1199                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1200                  else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines