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.20 by greg, Fri Feb 12 18:03:08 1993 UTC vs.
Revision 2.44 by greg, Tue Apr 15 16:53:58 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1997 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
28 + #include  <X11/Xatom.h>
29  
30 < #include  "color.h"
30 > #include  "tonemap.h"
31   #include  "view.h"
32   #include  "x11raster.h"
33   #include  "random.h"
34   #include  "resolu.h"
35  
36 + #ifndef  int4
37 + #define  int4           int             /* most int's are 32-bit */
38 + #endif
39 +
40   #define  FONTNAME       "8x13"          /* text font we'll use */
41  
42   #define  CTRL(c)        ((c)-'@')
# Line 47 | Line 53 | static char SCCSid[] = "$SunId$ LBL";
53   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
54  
55   double  gamcor = 2.2;                   /* gamma correction */
56 + char  *gamstr = NULL;                   /* gamma value override */
57  
58   int  dither = 1;                        /* dither colors? */
59   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 55 | Line 62 | char   *dispname = NULL;               /* our display name */
62  
63   Window  wind = 0;                       /* our output window */
64   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58 Font  fontid;                           /* our font */
59
65   int  maxcolors = 0;                     /* maximum colors */
66   int  greyscale = 0;                     /* in grey */
67  
# Line 65 | Line 70 | int  scale = 0;                                /* scalefactor; power of two */
70   int  xoff = 0;                          /* x image offset */
71   int  yoff = 0;                          /* y image offset */
72  
73 + int  parent = 0;                        /* number of children, -1 if child */
74 + int  sequential = 0;                    /* display images in sequence */
75 +
76 + char  *tout = "od";                     /* output of 't' command */
77 + int  tinterv = 0;                       /* interval between mouse reports */
78 +
79 + int  tmflags = -1;                      /* tone mapping flags (-1 for none) */
80 +
81   VIEW  ourview = STDVIEW;                /* image view parameters */
82   int  gotview = 0;                       /* got parameters from file */
83  
84   COLR  *scanline;                        /* scan line buffer */
85 + TMbright  *lscan;                       /* encoded luminance scanline */
86 + BYTE  *cscan;                           /* encoded chroma scanline */
87 + BYTE  *pscan;                           /* compute pixel scanline */
88  
89   RESOLU  inpres;                         /* input resolution and ordering */
90   int  xmax, ymax;                        /* picture dimensions */
# Line 103 | Line 119 | char  *progname;
119  
120   char  errmsg[128];
121  
122 < extern BYTE  clrtab[256][3];            /* global color map */
122 > BYTE  clrtab[256][3];                   /* global color map */
123  
124   extern long  ftell();
125  
126 < extern char  *malloc(), *calloc();
126 > extern char  *getenv();
127  
128   Display  *thedisplay;
129 + Atom  closedownAtom, wmProtocolsAtom;
130  
131 + int  sigrecv;
132  
133 + int  onsig() { sigrecv++; }
134 +
135 +
136   main(argc, argv)
137   int  argc;
138   char  *argv[];
139   {
119        extern char  *getenv();
120        char  *gv;
140          int  headline();
141          int  i;
142 +        int  pid;
143          
144          progname = argv[0];
125        if ((gv = getenv("GAMMA")) != NULL)
126                gamcor = atof(gv);
145  
146          for (i = 1; i < argc; i++)
147                  if (argv[i][0] == '-')
148                          switch (argv[i][1]) {
149 <                        case 'c':
149 >                        case 'c':                       /* number of colors */
150                                  maxcolors = atoi(argv[++i]);
151                                  break;
152 <                        case 'b':
152 >                        case 'b':                       /* greyscale only */
153                                  greyscale = !greyscale;
154                                  break;
155 <                        case 'm':
155 >                        case 'm':                       /* monochrome */
156 >                                greyscale = 1;
157                                  maxcolors = 2;
158                                  break;
159 <                        case 'd':
159 >                        case 'd':                       /* display or dither */
160                                  if (argv[i][2] == 'i')
161                                          dispname = argv[++i];
162                                  else
163                                          dither = !dither;
164                                  break;
165 <                        case 'f':
165 >                        case 'f':                       /* save pixmap */
166                                  fast = !fast;
167                                  break;
168 <                        case 'e':
169 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
168 >                        case 's':                       /* one at a time */
169 >                                sequential = !sequential;
170 >                                break;
171 >                        case 'o':                       /* 't' output */
172 >                                tout = argv[i]+2;
173 >                                break;
174 >                        case 't':                       /* msec interval */
175 >                                tinterv = atoi(argv[++i]);
176 >                                break;
177 >                        case 'e':                       /* exposure comp. */
178 >                                i++;
179 >                                if (!strcmp(argv[i], "auto")) {
180 >                                        tmflags = TM_F_CAMERA;
181 >                                        break;
182 >                                }
183 >                                if (!strcmp(argv[i], "human")) {
184 >                                        tmflags = TM_F_HUMAN;
185 >                                        break;
186 >                                }
187 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
188                                          goto userr;
189 <                                scale = atoi(argv[++i]);
189 >                                scale = atoi(argv[i]);
190                                  break;
191 <                        case 'g':
191 >                        case 'g':                       /* gamma comp. */
192                                  if (argv[i][2] == 'e')
193                                          geometry = argv[++i];
194                                  else
195 <                                        gamcor = atof(argv[++i]);
195 >                                        gamstr = argv[++i];
196                                  break;
197                          default:
198                                  goto userr;
# Line 165 | Line 202 | char  *argv[];
202                  else
203                          break;
204  
205 <        if (i == argc-1) {
205 >        if (i > argc)
206 >                goto userr;
207 >        while (i < argc-1) {
208 >                sigrecv = 0;
209 >                signal(SIGCONT, onsig);
210 >                if ((pid=fork()) == 0) {        /* a child for each picture */
211 >                        parent = -1;
212 >                        break;
213 >                }
214 >                if (pid < 0)
215 >                        quiterr("fork failed");
216 >                parent++;
217 >                while (!sigrecv)
218 >                        pause();        /* wait for wake-up call */
219 >                i++;
220 >        }
221 >        if (i < argc) {                 /* open picture file */
222                  fname = argv[i];
223                  fin = fopen(fname, "r");
224 <                if (fin == NULL) {
225 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
226 <                        quiterr(errmsg);
174 <                }
175 <        } else if (i != argc)
176 <                goto userr;
224 >                if (fin == NULL)
225 >                        quiterr("cannot open picture file");
226 >        }
227                                  /* get header */
228          getheader(fin, headline, NULL);
229                                  /* get picture dimensions */
# Line 187 | Line 237 | char  *argv[];
237          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
238                  quiterr("out of memory");
239  
240 <        init();                 /* get file and open window */
240 >        init(argc, argv);                       /* get file and open window */
241  
242          for ( ; ; )
243                  getevent();             /* main loop */
244   userr:
245          fprintf(stderr,
246 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
246 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
247                          progname);
248 <        quit(1);
248 >        exit(1);
249   }
250  
251  
# Line 206 | Line 256 | char  *s;
256  
257          if (isexpos(s))
258                  exposure *= exposval(s);
259 <        else if (isformat(s)) {
210 <                formatval(fmt, s);
259 >        else if (formatval(fmt, s))
260                  wrongformat = strcmp(fmt, COLRFMT);
261 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
261 >        else if (isview(s) && sscanview(&ourview, s) > 0)
262                  gotview++;
263   }
264  
265  
266 < init()                  /* get data and open window */
266 > init(argc, argv)                        /* get data and open window */
267 > int argc;
268 > char **argv;
269   {
219        XWMHints        ourxwmhints;
270          XSetWindowAttributes    ourwinattr;
271 <        XSizeHints      oursizhints;
271 >        XClassHint      xclshints;
272 >        XWMHints        xwmhints;
273 >        XSizeHints      xszhints;
274 >        XTextProperty   windowName, iconName;
275 >        XGCValues       xgcv;
276 >        char    *name;
277          register int    i;
278          
279          if (fname != NULL) {
280                  scanpos = (long *)malloc(ymax*sizeof(long));
281                  if (scanpos == NULL)
282 <                        goto memerr;
282 >                        quiterr("out of memory");
283                  for (i = 0; i < ymax; i++)
284                          scanpos[i] = -1;
285 <        }
285 >                name = fname;
286 >        } else
287 >                name = progname;
288 >                                /* remove directory prefix from name */
289 >        for (i = strlen(name); i-- > 0; )
290 >                if (name[i] == '/')
291 >                        break;
292 >        name += i+1;
293          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
294                  quiterr("cannot open display");
295 +                                /* set gamma value */
296 +        if (gamstr == NULL)             /* get it from the X server */
297 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
298 +        if (gamstr == NULL)             /* get it from the environment */
299 +                gamstr = getenv("DISPLAY_GAMMA");
300 +        if (gamstr != NULL)
301 +                gamcor = atof(gamstr);
302                                  /* get best visual for default screen */
303          getbestvis();
304                                  /* store image */
305          getras();
306                                  /* get size and position */
307 <        bzero((char *)&oursizhints, sizeof(oursizhints));
308 <        oursizhints.width = xmax; oursizhints.height = ymax;
307 >        xszhints.flags = 0;
308 >        xszhints.width = xmax; xszhints.height = ymax;
309          if (geometry != NULL) {
310 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
311 <                                (unsigned *)&oursizhints.width,
312 <                                (unsigned *)&oursizhints.height);
310 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
311 >                                (unsigned *)&xszhints.width,
312 >                                (unsigned *)&xszhints.height);
313                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
314 <                        oursizhints.flags |= USSize;
314 >                        xszhints.flags |= USSize;
315                  else
316 <                        oursizhints.flags |= PSize;
316 >                        xszhints.flags |= PSize;
317                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
318 <                        oursizhints.flags |= USPosition;
318 >                        xszhints.flags |= USPosition;
319                          if (i & XNegative)
320 <                                oursizhints.x += DisplayWidth(thedisplay,
321 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
320 >                                xszhints.x += DisplayWidth(thedisplay,
321 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
322                          if (i & YNegative)
323 <                                oursizhints.y += DisplayHeight(thedisplay,
324 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
323 >                                xszhints.y += DisplayHeight(thedisplay,
324 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
325                  }
326          }
327 <                                /* open window */
328 <        ourwinattr.border_pixel = ourblack;
329 <        ourwinattr.background_pixel = ourwhite;
330 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
331 <                        ourvis.visual, AllocNone);
332 <        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
333 <                        oursizhints.width, oursizhints.height, BORWIDTH,
334 <                        ourvis.depth, InputOutput, ourvis.visual,
335 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
327 >        /* open window */
328 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
329 >        ourwinattr.border_pixel = ourwhite;
330 >        ourwinattr.background_pixel = ourblack;
331 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
332 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
333 >                i |= CWColormap;
334 >        }
335 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
336 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
337 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
338 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
339 >                        xszhints.width, xszhints.height, BORWIDTH,
340 >                        ourvis.depth, InputOutput, ourvis.visual,
341 >                        i, &ourwinattr);
342          if (wind == 0)
343                  quiterr("cannot create window");
269        XFreeColormap(thedisplay, ourwinattr.colormap);
344          width = xmax;
345          height = ymax;
346 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
347 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
274 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
275 <        XSetFunction(thedisplay, revgc, GXinvert);
276 <        fontid = XLoadFont(thedisplay, FONTNAME);
277 <        if (fontid == 0)
346 >        /* prepare graphics drawing context */
347 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
348                  quiterr("cannot get font");
349 <        XSetFont(thedisplay, ourgc, fontid);
350 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
351 <                        XC_diamond_cross));
352 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
353 <        if (oursizhints.flags)
354 <                XSetNormalHints(thedisplay, wind, &oursizhints);
355 <        ourxwmhints.flags = InputHint|IconPixmapHint;
356 <        ourxwmhints.input = True;
357 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
349 >        xgcv.foreground = ourblack;
350 >        xgcv.background = ourwhite;
351 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
352 >                        GCFont, &xgcv);
353 >        xgcv.function = GXinvert;
354 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
355 >                        GCFunction, &xgcv);
356 >
357 >        /* set up the window manager */
358 >        xwmhints.flags = InputHint|IconPixmapHint;
359 >        xwmhints.input = True;
360 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
361                          wind, icondata, iconwidth, iconheight);
362 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
363 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
364 <                        |ButtonMotionMask|StructureNotifyMask
365 <                        |KeyPressMask|ExposureMask);
362 >
363 >        windowName.encoding = iconName.encoding = XA_STRING;
364 >        windowName.format = iconName.format = 8;
365 >        windowName.value = (u_char *)name;
366 >        windowName.nitems = strlen(windowName.value);
367 >        iconName.value = (u_char *)name;
368 >        iconName.nitems = strlen(windowName.value);
369 >
370 >        xclshints.res_name = NULL;
371 >        xclshints.res_class = "Ximage";
372 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
373 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
374 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
375 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
376 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
377 >
378          XMapWindow(thedisplay, wind);
294        return;
295 memerr:
296        quiterr("out of memory");
379   } /* end of init */
380  
381  
382   quiterr(err)            /* print message and exit */
383   char  *err;
384   {
385 <        if (err != NULL) {
386 <                fprintf(stderr, "%s: %s\n", progname, err);
387 <                exit(1);
385 >        register int  es;
386 >        int  cs;
387 >
388 >        if (es = err != NULL)
389 >                fprintf(stderr, "%s: %s: %s\n", progname,
390 >                                fname==NULL?"<stdin>":fname, err);
391 >        if (thedisplay != NULL)
392 >                XCloseDisplay(thedisplay);
393 >        if (parent < 0 & sigrecv == 0)
394 >                kill(getppid(), SIGCONT);
395 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
396 >                if (es == 0)
397 >                        es = cs>>8 & 0xff;
398 >                parent--;
399          }
400 <        exit(0);
400 >        exit(es);
401   }
402  
403  
# Line 445 | Line 538 | getras()                               /* get raster file */
538                          goto fail;
539                  getmono();
540          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
541 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
541 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
542                  if (ourdata == NULL)
543                          goto fail;
544 <                ourras = make_raster(thedisplay, &ourvis, 32,
544 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
545                                  ourdata, xmax, ymax, 32);
546                  if (ourras == NULL)
547                          goto fail;
# Line 461 | Line 554 | getras()                               /* get raster file */
554                                  xmax, ymax, 8);
555                  if (ourras == NULL)
556                          goto fail;
557 <                if (greyscale | ourvis.class == StaticGray)
557 >                if (greyscale)
558                          getgrey();
559                  else
560                          getmapped();
# Line 476 | Line 569 | fail:
569  
570   getevent()                              /* process the next event */
571   {
572 <        union {
480 <                XEvent  u;
481 <                XConfigureEvent  c;
482 <                XExposeEvent  e;
483 <                XButtonPressedEvent  b;
484 <                XKeyPressedEvent  k;
485 <        } e;
572 >        XEvent xev;
573  
574 <        XNextEvent(thedisplay, &e.u);
575 <        switch (e.u.type) {
574 >        XNextEvent(thedisplay, &xev);
575 >        switch ((int)xev.type) {
576          case KeyPress:
577 <                docom(&e.k);
577 >                docom(&xev.xkey);
578                  break;
579          case ConfigureNotify:
580 <                width = e.c.width;
581 <                height = e.c.height;
580 >                width = xev.xconfigure.width;
581 >                height = xev.xconfigure.height;
582                  break;
583          case MapNotify:
584                  map_rcolors(ourras, wind);
585                  if (fast)
586                          make_rpixmap(ourras, wind);
587 +                if (!sequential & parent < 0 & sigrecv == 0) {
588 +                        kill(getppid(), SIGCONT);
589 +                        sigrecv--;
590 +                }
591                  break;
592          case UnmapNotify:
593                  if (!fast)
594                          unmap_rcolors(ourras);
595                  break;
596          case Expose:
597 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
597 >                redraw(xev.xexpose.x, xev.xexpose.y,
598 >                                xev.xexpose.width, xev.xexpose.height);
599                  break;
600          case ButtonPress:
601 <                if (e.b.state & (ShiftMask|ControlMask))
602 <                        moveimage(&e.b);
601 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
602 >                        moveimage(&xev.xbutton);
603                  else
604 <                        getbox(&e.b);
604 >                        switch (xev.xbutton.button) {
605 >                        case Button1:
606 >                                getbox(&xev.xbutton);
607 >                                break;
608 >                        case Button2:
609 >                                traceray(xev.xbutton.x, xev.xbutton.y);
610 >                                break;
611 >                        case Button3:
612 >                                trackrays(&xev.xbutton);
613 >                                break;
614 >                        }
615                  break;
616 +        case ClientMessage:
617 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
618 +                                (xev.xclient.data.l[0] == closedownAtom))
619 +                        quiterr(NULL);
620 +                break;
621          }
622   }
623  
624  
625 < docom(ekey)                                     /* execute command */
625 > traceray(xpos, ypos)                    /* print requested pixel data */
626 > int  xpos, ypos;
627 > {
628 >        extern char  *index();
629 >        FLOAT  hv[2];
630 >        FVECT  rorg, rdir;
631 >        COLOR  cval;
632 >        register char  *cp;
633 >
634 >        box.xmin = xpos; box.xsiz = 1;
635 >        box.ymin = ypos; box.ysiz = 1;
636 >        avgbox(cval);
637 >        scalecolor(cval, 1./exposure);
638 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
639 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
640 >                rorg[0] = rorg[1] = rorg[2] =
641 >                rdir[0] = rdir[1] = rdir[2] = 0.;
642 >
643 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
644 >                switch (*cp) {
645 >                case 'o':                       /* origin */
646 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
647 >                        break;
648 >                case 'd':                       /* direction */
649 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
650 >                        break;
651 >                case 'v':                       /* radiance value */
652 >                        printf("%e %e %e ", colval(cval,RED),
653 >                                        colval(cval,GRN), colval(cval,BLU));
654 >                        break;
655 >                case 'l':                       /* luminance */
656 >                        printf("%e ", luminance(cval));
657 >                        break;
658 >                case 'p':                       /* pixel position */
659 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
660 >                                        (int)(hv[1]*inpres.yr));
661 >                        break;
662 >                }
663 >        putchar('\n');
664 >        fflush(stdout);
665 >        return(0);
666 > }
667 >
668 >
669 > docom(ekey)                             /* execute command */
670   XKeyPressedEvent  *ekey;
671   {
672          char  buf[80];
# Line 524 | Line 675 | XKeyPressedEvent  *ekey;
675          int  com, n;
676          double  comp;
677          FLOAT  hv[2];
527        FVECT  rorg, rdir;
678  
679          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
680          if (n == 0)
# Line 532 | Line 682 | XKeyPressedEvent  *ekey;
682          com = buf[0];
683          switch (com) {                  /* interpret command */
684          case 'q':
685 +        case 'Q':
686          case CTRL('D'):                         /* quit */
687 <                quit(0);
687 >                quiterr(NULL);
688          case '\n':
689          case '\r':
690          case 'l':
# Line 579 | Line 730 | XKeyPressedEvent  *ekey;
730                                          buf, strlen(buf));
731                  return(0);
732          case 't':                               /* trace */
733 <                if (!gotview) {
583 <                        XBell(thedisplay, 0);
584 <                        return(-1);
585 <                }
586 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
587 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
588 <                        return(-1);
589 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
590 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
591 <                fflush(stdout);
592 <                return(0);
733 >                return(traceray(ekey->x, ekey->y));
734          case '=':                               /* adjust exposure */
735 +        case '@':                               /* adaptation level */
736                  if (avgbox(cval) == -1)
737                          return(-1);
738 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
739 <                if (n == 0)
740 <                        return(0);
741 <                scale_rcolors(ourras, pow(2.0, (double)n));
738 >                comp = bright(cval);
739 >                if (comp < 1e-20) {
740 >                        XBell(thedisplay, 0);
741 >                        return(-1);
742 >                }
743 >                if (com == '@')
744 >                        comp = 106./exposure/
745 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
746 >                else
747 >                        comp = .5/comp;
748 >                comp = log(comp)/.69315 - scale;
749 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
750 >                if (tmflags != -1)
751 >                        tmflags = -1;           /* turn off tone mapping */
752 >                else {
753 >                        if (n == 0)             /* else check if any change */
754 >                                return(0);
755 >                        scale_rcolors(ourras, pow(2.0, (double)n));
756 >                }
757                  scale += n;
758                  sprintf(buf, "%+d", scale);
759                  XDrawImageString(thedisplay, wind, ourgc,
# Line 615 | Line 772 | XKeyPressedEvent  *ekey;
772                          make_rpixmap(ourras, wind);
773                  redraw(0, 0, width, height);
774                  return(0);
775 +        case 'f':                               /* turn on fast redraw */
776 +                fast = 1;
777 +                make_rpixmap(ourras, wind);
778 +                return(0);
779 +        case 'F':                               /* turn off fast redraw */
780 +                fast = 0;
781 +                free_rpixmap(ourras);
782 +                return(0);
783          case '0':                               /* recenter origin */
784                  if (xoff == 0 & yoff == 0)
785                          return(0);
# Line 635 | Line 800 | XKeyPressedEvent  *ekey;
800   moveimage(ebut)                         /* shift the image */
801   XButtonPressedEvent  *ebut;
802   {
803 <        union {
639 <                XEvent  u;
640 <                XButtonReleasedEvent  b;
641 <                XPointerMovedEvent  m;
642 <        }  e;
803 >        XEvent  e;
804          int     mxo, myo;
805  
806 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
807 <        while (e.u.type == MotionNotify) {
808 <                mxo = e.m.x;
809 <                myo = e.m.y;
806 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
807 >        while (e.type == MotionNotify) {
808 >                mxo = e.xmotion.x;
809 >                myo = e.xmotion.y;
810                  revline(ebut->x, ebut->y, mxo, myo);
811                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
812                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
813 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
813 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
814                  revline(ebut->x, ebut->y, mxo, myo);
815                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
816                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
817          }
818 <        xoff += e.b.x - ebut->x;
819 <        yoff += e.b.y - ebut->y;
818 >        xoff += e.xbutton.x - ebut->x;
819 >        yoff += e.xbutton.y - ebut->y;
820          XClearWindow(thedisplay, wind);
821          redraw(0, 0, width, height);
822   }
# Line 664 | Line 825 | XButtonPressedEvent  *ebut;
825   getbox(ebut)                            /* get new box */
826   XButtonPressedEvent  *ebut;
827   {
828 <        union {
668 <                XEvent  u;
669 <                XButtonReleasedEvent  b;
670 <                XPointerMovedEvent  m;
671 <        }  e;
828 >        XEvent  e;
829  
830 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
831 <        while (e.u.type == MotionNotify) {
832 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
833 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
830 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
831 >        while (e.type == MotionNotify) {
832 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
833 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
834                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
835          }
836 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
837 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
836 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
837 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
838          if (box.xmin > ebut->x) {
839                  box.xsiz = box.xmin - ebut->x + 1;
840                  box.xmin = ebut->x;
# Line 693 | Line 850 | XButtonPressedEvent  *ebut;
850   }
851  
852  
853 + trackrays(ebut)                         /* trace rays as mouse moves */
854 + XButtonPressedEvent  *ebut;
855 + {
856 +        XEvent  e;
857 +        unsigned long   lastrept;
858 +
859 +        traceray(ebut->x, ebut->y);
860 +        lastrept = ebut->time;
861 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
862 +        while (e.type == MotionNotify) {
863 +                if (e.xmotion.time >= lastrept + tinterv) {
864 +                        traceray(e.xmotion.x, e.xmotion.y);
865 +                        lastrept = e.xmotion.time;
866 +                }
867 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
868 +        }
869 + }
870 +
871 +
872   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
873   int  x0, y0, x1, y1;
874   {
# Line 751 | Line 927 | COLOR  clr;
927   }
928  
929  
930 + make_tonemap()                  /* initialize tone mapping */
931 + {
932 +        int  y;
933 +
934 +        if (tmflags != -1 && fname == NULL) {
935 +                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
936 +                                progname);
937 +                tmflags = -1;
938 +        }
939 +        if (tmflags == -1) {
940 +                setcolrcor(pow, 1.0/gamcor);
941 +                return;
942 +        }
943 +        if (greyscale)
944 +                tmflags |= TM_F_BW;
945 +                                        /* initialize tm library */
946 +        if (tmInit(tmflags, stdprims, gamcor) == NULL)
947 +                goto memerr;
948 +        if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
949 +                goto tmerr;
950 +                                        /* allocate encoding buffers */
951 +        if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
952 +                goto memerr;
953 +        if (tmflags & TM_F_BW) {
954 +                cscan = TM_NOCHROM;
955 +                if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
956 +                        goto memerr;
957 +        } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL)
958 +                goto memerr;
959 +                                        /* compute picture histogram */
960 +        for (y = 0; y < ymax; y++) {
961 +                getscan(y);
962 +                if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
963 +                        goto tmerr;
964 +                if (tmAddHisto(lscan, xmax, 1))
965 +                        goto tmerr;
966 +        }
967 +                                        /* compute tone mapping */
968 +        if (tmComputeMapping(gamcor, 0., 0.))
969 +                goto tmerr;
970 +        free((char *)lscan);
971 +        return;
972 + memerr:
973 +        quiterr("out of memory in make_tonemap");
974 + tmerr:
975 +        quiterr("tone mapping error");
976 + }
977 +
978 +
979 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
980 + register COLR  *scn;
981 + int  len;
982 + {
983 +        register BYTE  *ps;
984 +
985 +        if (tmflags == -1) {
986 +                if (scale)
987 +                        shiftcolrs(scn, len, scale);
988 +                colrs_gambs(scn, len);
989 +                return;
990 +        }
991 +        if (len > xmax)
992 +                quiterr("code error 1 in tmap_colrs");
993 +        if (tmCvColrs(lscan, cscan, scn, len))
994 +                goto tmerr;
995 +        if (tmMapPixels(pscan, lscan, cscan, len))
996 +                goto tmerr;
997 +        ps = pscan;
998 +        if (tmflags & TM_F_BW)
999 +                while (len--) {
1000 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1001 +                        scn[0][EXP] = COLXS;
1002 +                        scn++;
1003 +                }
1004 +        else
1005 +                while (len--) {
1006 +                        scn[0][RED] = *ps++;
1007 +                        scn[0][GRN] = *ps++;
1008 +                        scn[0][BLU] = *ps++;
1009 +                        scn[0][EXP] = COLXS;
1010 +                        scn++;
1011 +                }
1012 +        return;
1013 + tmerr:
1014 +        quiterr("tone mapping error");
1015 + }
1016 +
1017 +
1018 + done_tonemap()                  /* clean up after tone mapping is done */
1019 + {
1020 +        if (tmflags == -1 || tmTop == NULL)
1021 +                return;
1022 +        tmDone(tmTop);                  /* clear old mapping */
1023 +        free((char *)lscan);            /* free memory */
1024 +        free((char *)pscan);
1025 + }
1026 +
1027 +
1028   getmono()                       /* get monochrome data */
1029   {
1030          register unsigned char  *dp;
# Line 838 | Line 1112 | COLR  *scan;
1112   getfull()                       /* get full (24-bit) data */
1113   {
1114          int     y;
1115 <        register unsigned long  *dp;
1115 >        register unsigned int4  *dp;
1116          register int    x;
1117 <                                        /* set gamma correction */
1118 <        setcolrgam(gamcor);
1117 >                                        /* initialize tone mapping */
1118 >        make_tonemap();
1119                                          /* read and convert file */
1120 <        dp = (unsigned long *)ourdata;
1120 >        dp = (unsigned int4 *)ourdata;
1121          for (y = 0; y < ymax; y++) {
1122                  getscan(y);
1123                  add2icon(y, scanline);
1124 <                if (scale)
851 <                        shiftcolrs(scanline, xmax, scale);
852 <                colrs_gambs(scanline, xmax);
1124 >                tmap_colrs(scanline, xmax);
1125                  if (ourras->image->blue_mask & 1)
1126                          for (x = 0; x < xmax; x++)
1127 <                                *dp++ = scanline[x][RED] << 16 |
1128 <                                        scanline[x][GRN] << 8 |
1129 <                                        scanline[x][BLU] ;
1127 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1128 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1129 >                                        (unsigned int4)scanline[x][BLU] ;
1130                  else
1131                          for (x = 0; x < xmax; x++)
1132 <                                *dp++ = scanline[x][RED] |
1133 <                                        scanline[x][GRN] << 8 |
1134 <                                        scanline[x][BLU] << 16 ;
1132 >                                *dp++ = (unsigned int4)scanline[x][RED] |
1133 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1134 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
1135          }
1136 +        done_tonemap();
1137   }
1138  
1139  
# Line 869 | Line 1142 | getgrey()                      /* get greyscale data */
1142          int     y;
1143          register unsigned char  *dp;
1144          register int    x;
1145 <                                        /* set gamma correction */
1146 <        setcolrgam(gamcor);
1145 >                                        /* initialize tone mapping */
1146 >        make_tonemap();
1147                                          /* read and convert file */
1148          dp = ourdata;
1149          for (y = 0; y < ymax; y++) {
1150                  getscan(y);
1151                  add2icon(y, scanline);
1152 <                if (scale)
880 <                        shiftcolrs(scanline, xmax, scale);
881 <                for (x = 0; x < xmax; x++)
882 <                        scanline[x][GRN] = normbright(scanline[x]);
883 <                colrs_gambs(scanline, xmax);
1152 >                tmap_colrs(scanline, xmax);
1153                  if (maxcolors < 256)
1154                          for (x = 0; x < xmax; x++)
1155 <                                *dp++ = ((long)scanline[x][GRN] *
1155 >                                *dp++ = ((int4)scanline[x][GRN] *
1156                                          maxcolors + maxcolors/2) >> 8;
1157                  else
1158                          for (x = 0; x < xmax; x++)
# Line 891 | Line 1160 | getgrey()                      /* get greyscale data */
1160          }
1161          for (x = 0; x < maxcolors; x++)
1162                  clrtab[x][RED] = clrtab[x][GRN] =
1163 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1163 >                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1164 >        done_tonemap();
1165   }
1166  
1167  
1168   getmapped()                     /* get color-mapped data */
1169   {
1170          int     y;
1171 <                                        /* set gamma correction */
1172 <        setcolrgam(gamcor);
1171 >                                        /* make sure we can do it first */
1172 >        if (fname == NULL)
1173 >                quiterr("cannot map colors from standard input");
1174 >                                        /* initialize tone mapping */
1175 >        make_tonemap();
1176                                          /* make histogram */
1177 <        new_histo();
1177 >        if (new_histo((int4)xmax*ymax) == -1)
1178 >                quiterr("cannot initialize histogram");
1179          for (y = 0; y < ymax; y++) {
1180                  if (getscan(y) < 0)
1181 <                        quiterr("seek error in getmapped");
1181 >                        break;
1182                  add2icon(y, scanline);
1183 <                if (scale)
910 <                        shiftcolrs(scanline, xmax, scale);
911 <                colrs_gambs(scanline, xmax);
1183 >                tmap_colrs(scanline, xmax);
1184                  cnt_colrs(scanline, xmax);
1185          }
1186                                          /* map pixels */
1187          if (!new_clrtab(maxcolors))
1188                  quiterr("cannot create color map");
1189          for (y = 0; y < ymax; y++) {
1190 <                if (getscan(y) < 0)
1191 <                        quiterr("seek error in getmapped");
920 <                if (scale)
921 <                        shiftcolrs(scanline, xmax, scale);
922 <                colrs_gambs(scanline, xmax);
1190 >                getscan(y);
1191 >                tmap_colrs(scanline, xmax);
1192                  if (dither)
1193                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1194                  else
1195                          map_colrs(ourdata+y*xmax, scanline, xmax);
1196          }
1197 +        done_tonemap();
1198   }
1199  
1200  
# Line 959 | Line 1229 | double sf;
1229   getscan(y)
1230   int  y;
1231   {
1232 +        static int  trunced = -1;               /* truncated file? */
1233 + skipit:
1234 +        if (trunced >= 0 && y >= trunced) {
1235 +                bzero(scanline, xmax*sizeof(COLR));
1236 +                return(-1);
1237 +        }
1238          if (y != cury) {
1239                  if (scanpos == NULL || scanpos[y] == -1)
1240                          return(-1);
# Line 968 | Line 1244 | int  y;
1244          } else if (scanpos != NULL && scanpos[y] == -1)
1245                  scanpos[y] = ftell(fin);
1246  
1247 <        if (freadcolrs(scanline, xmax, fin) < 0)
1248 <                quiterr("read error");
1249 <
1247 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1248 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1249 >                                progname, fname==NULL?"<stdin>":fname);
1250 >                trunced = y;
1251 >                goto skipit;
1252 >        }
1253          cury++;
1254          return(0);
1255   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines