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.30 by greg, Wed Oct 27 16:57:28 1993 UTC vs.
Revision 2.53 by gwlarson, Tue Oct 27 09:08:28 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4 < static char SCCSid[] = "$SunId$ LBL";
4 > static char SCCSid[] = "$SunId$ SGI";
5   #endif
6  
7   /*
# Line 27 | Line 27 | static char SCCSid[] = "$SunId$ LBL";
27   #include  <X11/Xutil.h>
28   #include  <X11/Xatom.h>
29  
30 + #undef  NOPROTO
31 + #define NOPROTO 1
32   #include  "color.h"
33 + #include  "tonemap.h"
34   #include  "view.h"
35   #include  "x11raster.h"
36   #include  "random.h"
37   #include  "resolu.h"
38  
36 #ifdef  __alpha
37 #define  int4           int
38 #endif
39 #ifndef  int4
40 #define  int4           long
41 #endif
42
39   #define  FONTNAME       "8x13"          /* text font we'll use */
40  
41   #define  CTRL(c)        ((c)-'@')
# Line 48 | Line 44 | static char SCCSid[] = "$SunId$ LBL";
44  
45   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
46  
47 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
48 +
49   #define  ourscreen      DefaultScreen(thedisplay)
50   #define  ourroot        RootWindow(thedisplay,ourscreen)
51  
# Line 56 | Line 54 | static char SCCSid[] = "$SunId$ LBL";
54   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
55  
56   double  gamcor = 2.2;                   /* gamma correction */
57 + char  *gamstr = NULL;                   /* gamma value override */
58  
59   int  dither = 1;                        /* dither colors? */
60   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 73 | Line 72 | int  xoff = 0;                         /* x image offset */
72   int  yoff = 0;                          /* y image offset */
73  
74   int  parent = 0;                        /* number of children, -1 if child */
75 + int  sequential = 0;                    /* display images in sequence */
76  
77 + char  *tout = "od";                     /* output of 't' command */
78 + int  tinterv = 0;                       /* interval between mouse reports */
79 +
80 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
81 +
82   VIEW  ourview = STDVIEW;                /* image view parameters */
83   int  gotview = 0;                       /* got parameters from file */
84  
85   COLR  *scanline;                        /* scan line buffer */
86 + TMbright  *lscan;                       /* encoded luminance scanline */
87 + BYTE  *cscan;                           /* encoded chroma scanline */
88 + BYTE  *pscan;                           /* compute pixel scanline */
89  
90   RESOLU  inpres;                         /* input resolution and ordering */
91   int  xmax, ymax;                        /* picture dimensions */
# Line 112 | Line 120 | char  *progname;
120  
121   char  errmsg[128];
122  
123 < extern BYTE  clrtab[256][3];            /* global color map */
123 > BYTE  clrtab[256][3];                   /* global color map */
124  
125   extern long  ftell();
126  
127 + extern char  *getenv();
128 +
129   Display  *thedisplay;
130   Atom  closedownAtom, wmProtocolsAtom;
131  
132 < int  noop() {}
132 > int  sigrecv;
133  
134 + int  onsig() { sigrecv++; }
135  
136 +
137   main(argc, argv)
138   int  argc;
139   char  *argv[];
140   {
129        extern char  *getenv();
130        char  *gv;
141          int  headline();
142          int  i;
143          int  pid;
144          
145          progname = argv[0];
136        if ((gv = getenv("GAMMA")) != NULL)
137                gamcor = atof(gv);
146  
147          for (i = 1; i < argc; i++)
148                  if (argv[i][0] == '-')
149                          switch (argv[i][1]) {
150 <                        case 'c':
150 >                        case 'c':                       /* number of colors */
151                                  maxcolors = atoi(argv[++i]);
152                                  break;
153 <                        case 'b':
153 >                        case 'b':                       /* greyscale only */
154                                  greyscale = !greyscale;
155                                  break;
156 <                        case 'm':
156 >                        case 'm':                       /* monochrome */
157 >                                greyscale = 1;
158                                  maxcolors = 2;
159                                  break;
160 <                        case 'd':
160 >                        case 'd':                       /* display or dither */
161                                  if (argv[i][2] == 'i')
162                                          dispname = argv[++i];
163                                  else
164                                          dither = !dither;
165                                  break;
166 <                        case 'f':
166 >                        case 'f':                       /* save pixmap */
167                                  fast = !fast;
168                                  break;
169 <                        case 'e':
170 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
169 >                        case 's':                       /* one at a time */
170 >                                sequential = !sequential;
171 >                                break;
172 >                        case 'o':                       /* 't' output */
173 >                                tout = argv[i]+2;
174 >                                break;
175 >                        case 't':                       /* msec interval */
176 >                                tinterv = atoi(argv[++i]);
177 >                                break;
178 >                        case 'e':                       /* exposure comp. */
179 >                                i++;
180 >                                if (argv[i][0] == 'a') {
181 >                                        tmflags = TM_F_CAMERA;
182 >                                        break;
183 >                                }
184 >                                if (argv[i][0] == 'h') {
185 >                                        tmflags = TM_F_HUMAN;
186 >                                        break;
187 >                                }
188 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
189                                          goto userr;
190 <                                scale = atoi(argv[++i]);
190 >                                scale = atoi(argv[i]);
191                                  break;
192 <                        case 'g':
192 >                        case 'g':                       /* gamma comp. */
193                                  if (argv[i][2] == 'e')
194                                          geometry = argv[++i];
195                                  else
196 <                                        gamcor = atof(argv[++i]);
196 >                                        gamstr = argv[++i];
197                                  break;
198                          default:
199                                  goto userr;
# Line 179 | Line 206 | char  *argv[];
206          if (i > argc)
207                  goto userr;
208          while (i < argc-1) {
209 +                sigrecv = 0;
210 +                signal(SIGCONT, onsig);
211                  if ((pid=fork()) == 0) {        /* a child for each picture */
212                          parent = -1;
213                          break;
# Line 186 | Line 215 | char  *argv[];
215                  if (pid < 0)
216                          quiterr("fork failed");
217                  parent++;
218 <                signal(SIGCONT, noop);
219 <                pause();                /* wait for wake-up call */
218 >                while (!sigrecv)
219 >                        pause();        /* wait for wake-up call */
220                  i++;
221          }
222          if (i < argc) {                 /* open picture file */
223                  fname = argv[i];
224                  fin = fopen(fname, "r");
225 <                if (fin == NULL) {
226 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
198 <                        quiterr(errmsg);
199 <                }
225 >                if (fin == NULL)
226 >                        quiterr("cannot open picture file");
227          }
228                                  /* get header */
229          getheader(fin, headline, NULL);
# Line 213 | Line 240 | char  *argv[];
240  
241          init(argc, argv);                       /* get file and open window */
242  
216        if (parent < 0)
217                kill(getppid(), SIGCONT);       /* signal parent if child */
218
243          for ( ; ; )
244                  getevent();             /* main loop */
245   userr:
246          fprintf(stderr,
247 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n",
247 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
248                          progname);
249          exit(1);
250   }
251  
252  
253 + int
254   headline(s)             /* get relevant info from header */
255   char  *s;
256   {
# Line 233 | Line 258 | char  *s;
258  
259          if (isexpos(s))
260                  exposure *= exposval(s);
261 <        else if (isformat(s)) {
237 <                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 +        return(0);
266   }
267  
268  
# Line 270 | Line 295 | char **argv;
295          name += i+1;
296          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
297                  quiterr("cannot open display");
298 +                                /* set gamma value */
299 +        if (gamstr == NULL)             /* get it from the X server */
300 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
301 +        if (gamstr == NULL)             /* get it from the environment */
302 +                gamstr = getenv("DISPLAY_GAMMA");
303 +        if (gamstr != NULL)
304 +                gamcor = atof(gamstr);
305                                  /* get best visual for default screen */
306          getbestvis();
307                                  /* store image */
# Line 296 | Line 328 | char **argv;
328                  }
329          }
330          /* open window */
331 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
332          ourwinattr.border_pixel = ourwhite;
333          ourwinattr.background_pixel = ourblack;
334 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
335 <                        ourvis.visual, AllocNone);
334 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
335 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
336 >                i |= CWColormap;
337 >        }
338          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
339                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
340          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
341          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
342                          xszhints.width, xszhints.height, BORWIDTH,
343 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
344 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
343 >                        ourvis.depth, InputOutput, ourvis.visual,
344 >                        i, &ourwinattr);
345          if (wind == 0)
346                  quiterr("cannot create window");
347          width = xmax;
348          height = ymax;
349 <        xgcv.foreground = ourblack;
315 <        xgcv.background = ourwhite;
349 >        /* prepare graphics drawing context */
350          if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
351                  quiterr("cannot get font");
352 +        xgcv.foreground = ourblack;
353 +        xgcv.background = ourwhite;
354          ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
355                          GCFont, &xgcv);
356          xgcv.function = GXinvert;
# Line 343 | Line 379 | char **argv;
379          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
380  
381          XMapWindow(thedisplay, wind);
346        return;
382   } /* end of init */
383  
384  
385   quiterr(err)            /* print message and exit */
386   char  *err;
387   {
388 <        if (err != NULL)
388 >        register int  es;
389 >        int  cs;
390 >
391 >        if (es = err != NULL)
392                  fprintf(stderr, "%s: %s: %s\n", progname,
393                                  fname==NULL?"<stdin>":fname, err);
394 <        if (wind) {
395 <                XDestroyWindow(thedisplay, wind);
396 <                XFlush(thedisplay);
397 <        }
398 <        while (parent > 0 && wait(0) != -1)     /* wait for any children */
394 >        if (thedisplay != NULL)
395 >                XCloseDisplay(thedisplay);
396 >        if (parent < 0 & sigrecv == 0)
397 >                kill(getppid(), SIGCONT);
398 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
399 >                if (es == 0)
400 >                        es = cs>>8 & 0xff;
401                  parent--;
402 <        exit(err != NULL);
402 >        }
403 >        exit(es);
404   }
405  
406  
# Line 516 | Line 557 | getras()                               /* get raster file */
557                                  xmax, ymax, 8);
558                  if (ourras == NULL)
559                          goto fail;
560 <                if (greyscale | ourvis.class == StaticGray)
560 >                if (greyscale)
561                          getgrey();
562                  else
563                          getmapped();
# Line 546 | Line 587 | getevent()                             /* process the next event */
587                  map_rcolors(ourras, wind);
588                  if (fast)
589                          make_rpixmap(ourras, wind);
590 +                if (!sequential & parent < 0 & sigrecv == 0) {
591 +                        kill(getppid(), SIGCONT);
592 +                        sigrecv--;
593 +                }
594                  break;
595          case UnmapNotify:
596                  if (!fast)
# Line 558 | Line 603 | getevent()                             /* process the next event */
603          case ButtonPress:
604                  if (xev.xbutton.state & (ShiftMask|ControlMask))
605                          moveimage(&xev.xbutton);
561                else if (xev.xbutton.button == Button2)
562                        traceray(xev.xbutton.x, xev.xbutton.y);
606                  else
607 <                        getbox(&xev.xbutton);
607 >                        switch (xev.xbutton.button) {
608 >                        case Button1:
609 >                                getbox(&xev.xbutton);
610 >                                break;
611 >                        case Button2:
612 >                                traceray(xev.xbutton.x, xev.xbutton.y);
613 >                                break;
614 >                        case Button3:
615 >                                trackrays(&xev.xbutton);
616 >                                break;
617 >                        }
618                  break;
619          case ClientMessage:
620                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
# Line 572 | Line 625 | getevent()                             /* process the next event */
625   }
626  
627  
628 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
628 > traceray(xpos, ypos)                    /* print requested pixel data */
629   int  xpos, ypos;
630   {
631 +        extern char  *index();
632          FLOAT  hv[2];
633          FVECT  rorg, rdir;
634 +        COLOR  cval;
635 +        register char  *cp;
636  
637 <        if (!gotview) {         /* no view, no can do */
638 <                XBell(thedisplay, 0);
639 <                return(-1);
640 <        }
637 >        box.xmin = xpos; box.xsiz = 1;
638 >        box.ymin = ypos; box.ysiz = 1;
639 >        avgbox(cval);
640 >        scalecolor(cval, 1./exposure);
641          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
642 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
643 <                return(-1);
644 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
645 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
642 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
643 >                rorg[0] = rorg[1] = rorg[2] =
644 >                rdir[0] = rdir[1] = rdir[2] = 0.;
645 >
646 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
647 >                switch (*cp) {
648 >                case 'o':                       /* origin */
649 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
650 >                        break;
651 >                case 'd':                       /* direction */
652 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
653 >                        break;
654 >                case 'v':                       /* radiance value */
655 >                        printf("%e %e %e ", colval(cval,RED),
656 >                                        colval(cval,GRN), colval(cval,BLU));
657 >                        break;
658 >                case 'l':                       /* luminance */
659 >                        printf("%e ", luminance(cval));
660 >                        break;
661 >                case 'p':                       /* pixel position */
662 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
663 >                                        (int)(hv[1]*inpres.yr));
664 >                        break;
665 >                }
666 >        putchar('\n');
667          fflush(stdout);
668          return(0);
669   }
# Line 615 | Line 692 | XKeyPressedEvent  *ekey;
692          case '\r':
693          case 'l':
694          case 'c':                               /* value */
695 <                if (avgbox(cval) == -1)
695 >                if (!avgbox(cval))
696                          return(-1);
697                  switch (com) {
698                  case '\n':
# Line 657 | Line 734 | XKeyPressedEvent  *ekey;
734                  return(0);
735          case 't':                               /* trace */
736                  return(traceray(ekey->x, ekey->y));
737 +        case 'a':                               /* auto exposure */
738 +                if (fname == NULL)
739 +                        return(-1);
740 +                tmflags = TM_F_CAMERA;
741 +                strcpy(buf, "auto exposure...");
742 +                goto remap;
743 +        case 'h':                               /* human response */
744 +                if (fname == NULL)
745 +                        return(-1);
746 +                tmflags = TM_F_HUMAN;
747 +                strcpy(buf, "human exposure...");
748 +                goto remap;
749          case '=':                               /* adjust exposure */
750          case '@':                               /* adaptation level */
751 <                if (avgbox(cval) == -1)
751 >                if (!avgbox(cval))
752                          return(-1);
753 <                comp = com=='@'
754 <                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
755 <                : .5/bright(cval) ;
753 >                comp = bright(cval);
754 >                if (comp < 1e-20) {
755 >                        XBell(thedisplay, 0);
756 >                        return(-1);
757 >                }
758 >                if (com == '@')
759 >                        comp = 106./exposure/
760 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
761 >                else
762 >                        comp = .5/comp;
763                  comp = log(comp)/.69315 - scale;
764                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
765 <                if (n == 0)
766 <                        return(0);
767 <                scale_rcolors(ourras, pow(2.0, (double)n));
765 >                if (tmflags != TM_F_LINEAR)
766 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
767 >                else {
768 >                        if (n == 0)             /* else check if any change */
769 >                                return(0);
770 >                        scale_rcolors(ourras, pow(2.0, (double)n));
771 >                }
772                  scale += n;
773                  sprintf(buf, "%+d", scale);
774 +        remap:
775                  XDrawImageString(thedisplay, wind, ourgc,
776                                  box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
777                  XFlush(thedisplay);
# Line 765 | Line 866 | XButtonPressedEvent  *ebut;
866   }
867  
868  
869 + trackrays(ebut)                         /* trace rays as mouse moves */
870 + XButtonPressedEvent  *ebut;
871 + {
872 +        XEvent  e;
873 +        unsigned long   lastrept;
874 +
875 +        traceray(ebut->x, ebut->y);
876 +        lastrept = ebut->time;
877 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
878 +        while (e.type == MotionNotify) {
879 +                if (e.xmotion.time >= lastrept + tinterv) {
880 +                        traceray(e.xmotion.x, e.xmotion.y);
881 +                        lastrept = e.xmotion.time;
882 +                }
883 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
884 +        }
885 + }
886 +
887 +
888   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
889   int  x0, y0, x1, y1;
890   {
# Line 775 | Line 895 | int  x0, y0, x1, y1;
895   }
896  
897  
898 < avgbox(clr)                             /* average color over current box */
899 < COLOR  clr;
898 > int
899 > colavg(scn, n, cavg)
900 > register COLR   *scn;
901 > register int    n;
902 > COLOR   cavg;
903   {
904 <        static COLOR  lc;
905 <        static int  ll, lr, lt, lb;
904 >        COLOR   col;
905 >
906 >        while (n--) {
907 >                colr_color(col, scn++);
908 >                addcolor(cavg, col);
909 >        }
910 > }
911 >
912 >
913 > int
914 > avgbox(cavg)                            /* average color over current box */
915 > COLOR   cavg;
916 > {
917 >        double  d;
918 >        register int    rval;
919 >
920 >        setcolor(cavg, 0., 0., 0.);
921 >        rval = dobox(colavg, (char *)cavg);
922 >        if (rval > 0) {
923 >                d = 1./rval;
924 >                scalecolor(cavg, d);
925 >        }
926 >        return(rval);
927 > }
928 >
929 >
930 > int
931 > dobox(f, p)                             /* run function over box */
932 > int     (*f)();                 /* function to call for each subscan */
933 > char    *p;                     /* pointer to private data */
934 > {
935          int  left, right, top, bottom;
936          int  y;
785        double  d;
786        COLOR  ctmp;
787        register int  x;
937  
789        setcolor(clr, 0.0, 0.0, 0.0);
938          left = box.xmin - xoff;
939          right = left + box.xsiz;
940          if (left < 0)
# Line 794 | Line 942 | COLOR  clr;
942          if (right > xmax)
943                  right = xmax;
944          if (left >= right)
945 <                return(-1);
945 >                return(0);
946          top = box.ymin - yoff;
947          bottom = top + box.ysiz;
948          if (top < 0)
# Line 802 | Line 950 | COLOR  clr;
950          if (bottom > ymax)
951                  bottom = ymax;
952          if (top >= bottom)
805                return(-1);
806        if (left == ll && right == lr && top == lt && bottom == lb) {
807                copycolor(clr, lc);
953                  return(0);
809        }
954          for (y = top; y < bottom; y++) {
955                  if (getscan(y) == -1)
956                          return(-1);
957 <                for (x = left; x < right; x++) {
958 <                        colr_color(ctmp, scanline[x]);
959 <                        addcolor(clr, ctmp);
957 >                (*f)(scanline+left, right-left, p);
958 >        }
959 >        return((right-left)*(bottom-top));
960 > }
961 >
962 >
963 > int
964 > addfix(scn, n)                  /* add fixation points to histogram */
965 > COLR    *scn;
966 > int     n;
967 > {
968 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
969 >                goto tmerr;
970 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
971 >                goto tmerr;
972 >        return;
973 > tmerr:
974 >        quiterr("tone mapping error");
975 > }
976 >
977 >
978 > make_tonemap()                  /* initialize tone mapping */
979 > {
980 >        int  flags, y;
981 >
982 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
983 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
984 >                                progname);
985 >                tmflags = TM_F_LINEAR;
986 >        }
987 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
988 >                setcolrcor(pow, 1.0/gamcor);
989 >                return;
990 >        }
991 >        flags = tmflags;                /* histogram adjustment */
992 >        if (greyscale) flags |= TM_F_BW;
993 >        if (tmTop != NULL) {            /* reuse old histogram if one */
994 >                tmDone(tmTop);
995 >                tmTop->flags = flags;
996 >        } else {                        /* else initialize */
997 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
998 >                        goto memerr;
999 >                if (greyscale) {
1000 >                        cscan = TM_NOCHROM;
1001 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1002 >                                goto memerr;
1003 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1004 >                                == NULL)
1005 >                        goto memerr;
1006 >                                                /* initialize tm library */
1007 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1008 >                        goto memerr;
1009 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1010 >                        goto tmerr;
1011 >                                                /* compute picture histogram */
1012 >                for (y = 0; y < ymax; y++) {
1013 >                        getscan(y);
1014 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1015 >                                goto tmerr;
1016 >                        if (tmAddHisto(lscan, xmax, 1))
1017 >                                goto tmerr;
1018                  }
1019          }
1020 <        d = 1.0/((right-left)*(bottom-top));
1021 <        scalecolor(clr, d);
1022 <        ll = left; lr = right; lt = top; lb = bottom;
1023 <        copycolor(lc, clr);
1024 <        return(0);
1020 >        tmDup();                        /* add fixations to duplicate map */
1021 >        dobox(addfix, NULL);
1022 >                                        /* (re)compute tone mapping */
1023 >        if (tmComputeMapping(gamcor, 0., 0.))
1024 >                goto tmerr;
1025 >        return;
1026 > memerr:
1027 >        quiterr("out of memory in make_tonemap");
1028 > tmerr:
1029 >        quiterr("tone mapping error");
1030   }
1031  
1032  
1033 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1034 + register COLR  *scn;
1035 + int  len;
1036 + {
1037 +        register BYTE  *ps;
1038 +
1039 +        if (tmflags == TM_F_LINEAR) {
1040 +                if (scale)
1041 +                        shiftcolrs(scn, len, scale);
1042 +                colrs_gambs(scn, len);
1043 +                return;
1044 +        }
1045 +        if (len > xmax)
1046 +                quiterr("code error 1 in tmap_colrs");
1047 +        if (tmCvColrs(lscan, cscan, scn, len))
1048 +                goto tmerr;
1049 +        if (tmMapPixels(pscan, lscan, cscan, len))
1050 +                goto tmerr;
1051 +        ps = pscan;
1052 +        if (greyscale)
1053 +                while (len--) {
1054 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1055 +                        scn[0][EXP] = COLXS;
1056 +                        scn++;
1057 +                }
1058 +        else
1059 +                while (len--) {
1060 +                        scn[0][RED] = *ps++;
1061 +                        scn[0][GRN] = *ps++;
1062 +                        scn[0][BLU] = *ps++;
1063 +                        scn[0][EXP] = COLXS;
1064 +                        scn++;
1065 +                }
1066 +        return;
1067 + tmerr:
1068 +        quiterr("tone mapping error");
1069 + }
1070 +
1071 +
1072   getmono()                       /* get monochrome data */
1073   {
1074          register unsigned char  *dp;
# Line 912 | Line 1158 | getfull()                      /* get full (24-bit) data */
1158          int     y;
1159          register unsigned int4  *dp;
1160          register int    x;
1161 <                                        /* set gamma correction */
1162 <        setcolrgam(gamcor);
1161 >                                        /* initialize tone mapping */
1162 >        make_tonemap();
1163                                          /* read and convert file */
1164          dp = (unsigned int4 *)ourdata;
1165          for (y = 0; y < ymax; y++) {
1166                  getscan(y);
1167                  add2icon(y, scanline);
1168 <                if (scale)
923 <                        shiftcolrs(scanline, xmax, scale);
924 <                colrs_gambs(scanline, xmax);
1168 >                tmap_colrs(scanline, xmax);
1169                  if (ourras->image->blue_mask & 1)
1170                          for (x = 0; x < xmax; x++)
1171 <                                *dp++ = scanline[x][RED] << 16 |
1172 <                                        scanline[x][GRN] << 8 |
1173 <                                        scanline[x][BLU] ;
1171 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1172 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1173 >                                        (unsigned int4)scanline[x][BLU] ;
1174                  else
1175                          for (x = 0; x < xmax; x++)
1176 <                                *dp++ = scanline[x][RED] |
1177 <                                        scanline[x][GRN] << 8 |
1178 <                                        scanline[x][BLU] << 16 ;
1176 >                                *dp++ = (unsigned int4)scanline[x][RED] |
1177 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1178 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
1179          }
1180   }
1181  
# Line 941 | Line 1185 | getgrey()                      /* get greyscale data */
1185          int     y;
1186          register unsigned char  *dp;
1187          register int    x;
1188 <                                        /* set gamma correction */
1189 <        setcolrgam(gamcor);
1188 >                                        /* initialize tone mapping */
1189 >        make_tonemap();
1190                                          /* read and convert file */
1191          dp = ourdata;
1192          for (y = 0; y < ymax; y++) {
1193                  getscan(y);
1194                  add2icon(y, scanline);
1195 <                if (scale)
952 <                        shiftcolrs(scanline, xmax, scale);
953 <                for (x = 0; x < xmax; x++)
954 <                        scanline[x][GRN] = normbright(scanline[x]);
955 <                colrs_gambs(scanline, xmax);
1195 >                tmap_colrs(scanline, xmax);
1196                  if (maxcolors < 256)
1197                          for (x = 0; x < xmax; x++)
1198 <                                *dp++ = ((long)scanline[x][GRN] *
1198 >                                *dp++ = ((int4)scanline[x][GRN] *
1199                                          maxcolors + maxcolors/2) >> 8;
1200                  else
1201                          for (x = 0; x < xmax; x++)
# Line 963 | Line 1203 | getgrey()                      /* get greyscale data */
1203          }
1204          for (x = 0; x < maxcolors; x++)
1205                  clrtab[x][RED] = clrtab[x][GRN] =
1206 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1206 >                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1207   }
1208  
1209  
# Line 973 | Line 1213 | getmapped()                    /* get color-mapped data */
1213                                          /* make sure we can do it first */
1214          if (fname == NULL)
1215                  quiterr("cannot map colors from standard input");
1216 <                                        /* set gamma correction */
1217 <        setcolrgam(gamcor);
1216 >                                        /* initialize tone mapping */
1217 >        make_tonemap();
1218                                          /* make histogram */
1219 <        new_histo();
1219 >        if (new_histo((int4)xmax*ymax) == -1)
1220 >                quiterr("cannot initialize histogram");
1221          for (y = 0; y < ymax; y++) {
1222                  if (getscan(y) < 0)
1223                          break;
1224                  add2icon(y, scanline);
1225 <                if (scale)
985 <                        shiftcolrs(scanline, xmax, scale);
986 <                colrs_gambs(scanline, xmax);
1225 >                tmap_colrs(scanline, xmax);
1226                  cnt_colrs(scanline, xmax);
1227          }
1228                                          /* map pixels */
# Line 991 | Line 1230 | getmapped()                    /* get color-mapped data */
1230                  quiterr("cannot create color map");
1231          for (y = 0; y < ymax; y++) {
1232                  getscan(y);
1233 <                if (scale)
995 <                        shiftcolrs(scanline, xmax, scale);
996 <                colrs_gambs(scanline, xmax);
1233 >                tmap_colrs(scanline, xmax);
1234                  if (dither)
1235                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1236                  else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines