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.8 by greg, Fri May 29 14:21:12 1992 UTC vs.
Revision 2.51 by gwlarson, Mon Feb 9 14:37:09 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 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 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 > #undef  NOPROTO
31 > #define NOPROTO 1
32 > #include  "tonemap.h"
33   #include  "view.h"
30 #include  "pic.h"
34   #include  "x11raster.h"
35   #include  "random.h"
36   #include  "resolu.h"
37  
38 + #ifndef  int4
39 + #define  int4           int             /* most int's are 32-bit */
40 + #endif
41 +
42   #define  FONTNAME       "8x13"          /* text font we'll use */
43  
44   #define  CTRL(c)        ((c)-'@')
# Line 40 | Line 47 | static char SCCSid[] = "$SunId$ LBL";
47  
48   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
49  
50 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
51 +
52   #define  ourscreen      DefaultScreen(thedisplay)
53   #define  ourroot        RootWindow(thedisplay,ourscreen)
54  
# Line 48 | Line 57 | static char SCCSid[] = "$SunId$ LBL";
57   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
58  
59   double  gamcor = 2.2;                   /* gamma correction */
60 + char  *gamstr = NULL;                   /* gamma value override */
61  
62   int  dither = 1;                        /* dither colors? */
63   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 55 | Line 65 | int  fast = 0;                         /* keep picture in Pixmap? */
65   char    *dispname = NULL;               /* our display name */
66  
67   Window  wind = 0;                       /* our output window */
68 < unsigned long  ourblack=1, ourwhite=0;  /* black and white for this visual */
59 < Font  fontid;                           /* our font */
60 <
68 > unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
69   int  maxcolors = 0;                     /* maximum colors */
70   int  greyscale = 0;                     /* in grey */
71  
# Line 66 | Line 74 | int  scale = 0;                                /* scalefactor; power of two */
74   int  xoff = 0;                          /* x image offset */
75   int  yoff = 0;                          /* y image offset */
76  
77 + int  parent = 0;                        /* number of children, -1 if child */
78 + int  sequential = 0;                    /* display images in sequence */
79 +
80 + char  *tout = "od";                     /* output of 't' command */
81 + int  tinterv = 0;                       /* interval between mouse reports */
82 +
83 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
84 +
85   VIEW  ourview = STDVIEW;                /* image view parameters */
86   int  gotview = 0;                       /* got parameters from file */
87  
88   COLR  *scanline;                        /* scan line buffer */
89 + TMbright  *lscan;                       /* encoded luminance scanline */
90 + BYTE  *cscan;                           /* encoded chroma scanline */
91 + BYTE  *pscan;                           /* compute pixel scanline */
92  
93   RESOLU  inpres;                         /* input resolution and ordering */
94   int  xmax, ymax;                        /* picture dimensions */
# Line 104 | Line 123 | char  *progname;
123  
124   char  errmsg[128];
125  
126 + BYTE  clrtab[256][3];                   /* global color map */
127 +
128   extern long  ftell();
129  
130 < extern char  *malloc(), *calloc();
130 > extern char  *getenv();
131  
111 extern double  pow(), log();
112
132   Display  *thedisplay;
133 + Atom  closedownAtom, wmProtocolsAtom;
134  
135 + int  sigrecv;
136 +
137 + int  onsig() { sigrecv++; }
138 +
139 +
140   main(argc, argv)
141   int  argc;
142   char  *argv[];
143   {
144          int  headline();
145          int  i;
146 +        int  pid;
147          
148          progname = argv[0];
149  
150          for (i = 1; i < argc; i++)
151                  if (argv[i][0] == '-')
152                          switch (argv[i][1]) {
153 <                        case 'c':
153 >                        case 'c':                       /* number of colors */
154                                  maxcolors = atoi(argv[++i]);
155                                  break;
156 <                        case 'b':
156 >                        case 'b':                       /* greyscale only */
157                                  greyscale = !greyscale;
158                                  break;
159 <                        case 'm':
159 >                        case 'm':                       /* monochrome */
160 >                                greyscale = 1;
161                                  maxcolors = 2;
162                                  break;
163 <                        case 'd':
163 >                        case 'd':                       /* display or dither */
164                                  if (argv[i][2] == 'i')
165                                          dispname = argv[++i];
166                                  else
167                                          dither = !dither;
168                                  break;
169 <                        case 'f':
169 >                        case 'f':                       /* save pixmap */
170                                  fast = !fast;
171                                  break;
172 <                        case 'e':
173 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
172 >                        case 's':                       /* one at a time */
173 >                                sequential = !sequential;
174 >                                break;
175 >                        case 'o':                       /* 't' output */
176 >                                tout = argv[i]+2;
177 >                                break;
178 >                        case 't':                       /* msec interval */
179 >                                tinterv = atoi(argv[++i]);
180 >                                break;
181 >                        case 'e':                       /* exposure comp. */
182 >                                i++;
183 >                                if (argv[i][0] == 'a') {
184 >                                        tmflags = TM_F_CAMERA;
185 >                                        break;
186 >                                }
187 >                                if (argv[i][0] == 'h') {
188 >                                        tmflags = TM_F_HUMAN;
189 >                                        break;
190 >                                }
191 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
192                                          goto userr;
193 <                                scale = atoi(argv[++i]);
193 >                                scale = atoi(argv[i]);
194                                  break;
195 <                        case 'g':
195 >                        case 'g':                       /* gamma comp. */
196                                  if (argv[i][2] == 'e')
197                                          geometry = argv[++i];
198                                  else
199 <                                        gamcor = atof(argv[++i]);
199 >                                        gamstr = argv[++i];
200                                  break;
201                          default:
202                                  goto userr;
# Line 161 | Line 206 | char  *argv[];
206                  else
207                          break;
208  
209 <        if (i == argc-1) {
209 >        if (i > argc)
210 >                goto userr;
211 >        while (i < argc-1) {
212 >                sigrecv = 0;
213 >                signal(SIGCONT, onsig);
214 >                if ((pid=fork()) == 0) {        /* a child for each picture */
215 >                        parent = -1;
216 >                        break;
217 >                }
218 >                if (pid < 0)
219 >                        quiterr("fork failed");
220 >                parent++;
221 >                while (!sigrecv)
222 >                        pause();        /* wait for wake-up call */
223 >                i++;
224 >        }
225 >        if (i < argc) {                 /* open picture file */
226                  fname = argv[i];
227                  fin = fopen(fname, "r");
228 <                if (fin == NULL) {
229 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
230 <                        quiterr(errmsg);
170 <                }
171 <        } else if (i != argc)
172 <                goto userr;
228 >                if (fin == NULL)
229 >                        quiterr("cannot open picture file");
230 >        }
231                                  /* get header */
232          getheader(fin, headline, NULL);
233                                  /* get picture dimensions */
# Line 183 | Line 241 | char  *argv[];
241          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
242                  quiterr("out of memory");
243  
244 <        init();                 /* get file and open window */
244 >        init(argc, argv);                       /* get file and open window */
245  
246          for ( ; ; )
247                  getevent();             /* main loop */
248   userr:
249          fprintf(stderr,
250 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
250 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
251                          progname);
252 <        quit(1);
252 >        exit(1);
253   }
254  
255  
# Line 202 | Line 260 | char  *s;
260  
261          if (isexpos(s))
262                  exposure *= exposval(s);
263 <        else if (isformat(s)) {
206 <                formatval(fmt, s);
263 >        else if (formatval(fmt, s))
264                  wrongformat = strcmp(fmt, COLRFMT);
265 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
265 >        else if (isview(s) && sscanview(&ourview, s) > 0)
266                  gotview++;
267   }
268  
269  
270 < init()                  /* get data and open window */
270 > init(argc, argv)                        /* get data and open window */
271 > int argc;
272 > char **argv;
273   {
215        XWMHints        ourxwmhints;
274          XSetWindowAttributes    ourwinattr;
275 <        XSizeHints      oursizhints;
275 >        XClassHint      xclshints;
276 >        XWMHints        xwmhints;
277 >        XSizeHints      xszhints;
278 >        XTextProperty   windowName, iconName;
279 >        XGCValues       xgcv;
280 >        char    *name;
281          register int    i;
282          
283          if (fname != NULL) {
284                  scanpos = (long *)malloc(ymax*sizeof(long));
285                  if (scanpos == NULL)
286 <                        goto memerr;
286 >                        quiterr("out of memory");
287                  for (i = 0; i < ymax; i++)
288                          scanpos[i] = -1;
289 <        }
289 >                name = fname;
290 >        } else
291 >                name = progname;
292 >                                /* remove directory prefix from name */
293 >        for (i = strlen(name); i-- > 0; )
294 >                if (name[i] == '/')
295 >                        break;
296 >        name += i+1;
297          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
298                  quiterr("cannot open display");
299 +                                /* set gamma value */
300 +        if (gamstr == NULL)             /* get it from the X server */
301 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
302 +        if (gamstr == NULL)             /* get it from the environment */
303 +                gamstr = getenv("DISPLAY_GAMMA");
304 +        if (gamstr != NULL)
305 +                gamcor = atof(gamstr);
306                                  /* get best visual for default screen */
307          getbestvis();
308                                  /* store image */
309          getras();
310 <                                /* open window */
311 <        ourwinattr.border_pixel = ourblack;
312 <        ourwinattr.background_pixel = ourwhite;
236 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
237 <                        ourvis.visual, AllocNone);
238 <        wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH,
239 <                        ourvis.depth, InputOutput, ourvis.visual,
240 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
241 <        if (wind == 0)
242 <                quiterr("cannot create window");
243 <        width = xmax;
244 <        height = ymax;
245 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
246 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
247 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
248 <        XSetFunction(thedisplay, revgc, GXinvert);
249 <        fontid = XLoadFont(thedisplay, FONTNAME);
250 <        if (fontid == 0)
251 <                quiterr("cannot get font");
252 <        XSetFont(thedisplay, ourgc, fontid);
253 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
254 <                        XC_diamond_cross));
255 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
310 >                                /* get size and position */
311 >        xszhints.flags = 0;
312 >        xszhints.width = xmax; xszhints.height = ymax;
313          if (geometry != NULL) {
314 <                bzero((char *)&oursizhints, sizeof(oursizhints));
315 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
316 <                                (unsigned *)&oursizhints.width,
260 <                                (unsigned *)&oursizhints.height);
314 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
315 >                                (unsigned *)&xszhints.width,
316 >                                (unsigned *)&xszhints.height);
317                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
318 <                        oursizhints.flags |= USSize;
319 <                else {
320 <                        oursizhints.width = xmax;
265 <                        oursizhints.height = ymax;
266 <                        oursizhints.flags |= PSize;
267 <                }
318 >                        xszhints.flags |= USSize;
319 >                else
320 >                        xszhints.flags |= PSize;
321                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
322 <                        oursizhints.flags |= USPosition;
322 >                        xszhints.flags |= USPosition;
323                          if (i & XNegative)
324 <                                oursizhints.x += DisplayWidth(thedisplay,
325 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
324 >                                xszhints.x += DisplayWidth(thedisplay,
325 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
326                          if (i & YNegative)
327 <                                oursizhints.y += DisplayHeight(thedisplay,
328 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
327 >                                xszhints.y += DisplayHeight(thedisplay,
328 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
329                  }
277                XSetNormalHints(thedisplay, wind, &oursizhints);
330          }
331 <        ourxwmhints.flags = InputHint|IconPixmapHint;
332 <        ourxwmhints.input = True;
333 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
331 >        /* open window */
332 >        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
333 >        ourwinattr.border_pixel = ourwhite;
334 >        ourwinattr.background_pixel = ourblack;
335 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
336 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
337 >                i |= CWColormap;
338 >        }
339 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
340 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
341 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
342 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
343 >                        xszhints.width, xszhints.height, BORWIDTH,
344 >                        ourvis.depth, InputOutput, ourvis.visual,
345 >                        i, &ourwinattr);
346 >        if (wind == 0)
347 >                quiterr("cannot create window");
348 >        width = xmax;
349 >        height = ymax;
350 >        /* prepare graphics drawing context */
351 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
352 >                quiterr("cannot get font");
353 >        xgcv.foreground = ourblack;
354 >        xgcv.background = ourwhite;
355 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
356 >                        GCFont, &xgcv);
357 >        xgcv.function = GXinvert;
358 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
359 >                        GCFunction, &xgcv);
360 >
361 >        /* set up the window manager */
362 >        xwmhints.flags = InputHint|IconPixmapHint;
363 >        xwmhints.input = True;
364 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
365                          wind, icondata, iconwidth, iconheight);
366 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
367 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
368 <                        |ButtonMotionMask|StructureNotifyMask
369 <                        |KeyPressMask|ExposureMask);
366 >
367 >        windowName.encoding = iconName.encoding = XA_STRING;
368 >        windowName.format = iconName.format = 8;
369 >        windowName.value = (u_char *)name;
370 >        windowName.nitems = strlen(windowName.value);
371 >        iconName.value = (u_char *)name;
372 >        iconName.nitems = strlen(windowName.value);
373 >
374 >        xclshints.res_name = NULL;
375 >        xclshints.res_class = "Ximage";
376 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
377 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
378 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
379 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
380 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
381 >
382          XMapWindow(thedisplay, wind);
288        return;
289 memerr:
290        quiterr("out of memory");
383   } /* end of init */
384  
385  
386   quiterr(err)            /* print message and exit */
387   char  *err;
388   {
389 <        if (err != NULL) {
390 <                fprintf(stderr, "%s: %s\n", progname, err);
391 <                exit(1);
389 >        register int  es;
390 >        int  cs;
391 >
392 >        if (es = err != NULL)
393 >                fprintf(stderr, "%s: %s: %s\n", progname,
394 >                                fname==NULL?"<stdin>":fname, err);
395 >        if (thedisplay != NULL)
396 >                XCloseDisplay(thedisplay);
397 >        if (parent < 0 & sigrecv == 0)
398 >                kill(getppid(), SIGCONT);
399 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
400 >                if (es == 0)
401 >                        es = cs>>8 & 0xff;
402 >                parent--;
403          }
404 <        exit(0);
404 >        exit(es);
405   }
406  
407  
305 eputs(s)
306 char    *s;
307 {
308        fputs(s, stderr);
309 }
310
311
312 quit(code)
313 int  code;
314 {
315        exit(code);
316 }
317
318
408   static int
409   viscmp(v1,v2)           /* compare visual to see which is better, descending */
410   register XVisualInfo    *v1, *v2;
# Line 419 | Line 508 | static char  vistype[][12] = {
508                  greyscale = 1;
509          if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors)
510                  maxcolors = ourvis.colormap_size;
422        if (maxcolors > 4)
423                maxcolors -= 2;
511          if (ourvis.class == StaticGray) {
512                  ourblack = 0;
513                  ourwhite = 255;
514          } else if (ourvis.class == PseudoColor) {
515                  ourblack = BlackPixel(thedisplay,ourscreen);
516                  ourwhite = WhitePixel(thedisplay,ourscreen);
517 +                if ((ourblack|ourwhite) & ~255L) {
518 +                        ourblack = 0;
519 +                        ourwhite = 1;
520 +                }
521 +                if (maxcolors > 4)
522 +                        maxcolors -= 2;
523          } else {
524                  ourblack = 0;
525 <                ourwhite = ~0;
525 >                ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask;
526          }
527          XFree((char *)xvi);
528   }
# Line 437 | Line 530 | static char  vistype[][12] = {
530  
531   getras()                                /* get raster file */
532   {
440        colormap        ourmap;
533          XVisualInfo     vinfo;
534  
535          if (maxcolors <= 2) {           /* monochrome */
# Line 449 | Line 541 | getras()                               /* get raster file */
541                  if (ourras == NULL)
542                          goto fail;
543                  getmono();
544 <        } else if (ourvis.class == TrueColor || ourvis.class == DirectColor) {
545 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
544 >        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
545 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
546                  if (ourdata == NULL)
547                          goto fail;
548 <                ourras = make_raster(thedisplay, &ourvis, 32,
548 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
549                                  ourdata, xmax, ymax, 32);
550                  if (ourras == NULL)
551                          goto fail;
# Line 466 | Line 558 | getras()                               /* get raster file */
558                                  xmax, ymax, 8);
559                  if (ourras == NULL)
560                          goto fail;
561 <                if (ourvis.class == StaticGray)
561 >                if (greyscale)
562                          getgrey();
563 <                else {
564 <                        if (greyscale)
565 <                                biq(dither,maxcolors,1,ourmap);
566 <                        else
475 <                                ciq(dither,maxcolors,1,ourmap);
476 <                        if (init_rcolors(ourras, ourmap[0],
477 <                                        ourmap[1], ourmap[2]) == 0)
478 <                                goto fail;
479 <                }
563 >                else
564 >                        getmapped();
565 >                if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab))
566 >                        goto fail;
567          }
568 <                return;
568 >        return;
569   fail:
570          quiterr("could not create raster image");
571   }
# Line 486 | Line 573 | fail:
573  
574   getevent()                              /* process the next event */
575   {
576 <        union {
490 <                XEvent  u;
491 <                XConfigureEvent  c;
492 <                XExposeEvent  e;
493 <                XButtonPressedEvent  b;
494 <                XKeyPressedEvent  k;
495 <        } e;
576 >        XEvent xev;
577  
578 <        XNextEvent(thedisplay, &e.u);
579 <        switch (e.u.type) {
578 >        XNextEvent(thedisplay, &xev);
579 >        switch ((int)xev.type) {
580          case KeyPress:
581 <                docom(&e.k);
581 >                docom(&xev.xkey);
582                  break;
583          case ConfigureNotify:
584 <                width = e.c.width;
585 <                height = e.c.height;
584 >                width = xev.xconfigure.width;
585 >                height = xev.xconfigure.height;
586                  break;
587          case MapNotify:
588                  map_rcolors(ourras, wind);
589                  if (fast)
590                          make_rpixmap(ourras, wind);
591 +                if (!sequential & parent < 0 & sigrecv == 0) {
592 +                        kill(getppid(), SIGCONT);
593 +                        sigrecv--;
594 +                }
595                  break;
596          case UnmapNotify:
597                  if (!fast)
598                          unmap_rcolors(ourras);
599                  break;
600          case Expose:
601 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
601 >                redraw(xev.xexpose.x, xev.xexpose.y,
602 >                                xev.xexpose.width, xev.xexpose.height);
603                  break;
604          case ButtonPress:
605 <                if (e.b.state & (ShiftMask|ControlMask))
606 <                        moveimage(&e.b);
605 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
606 >                        moveimage(&xev.xbutton);
607                  else
608 <                        getbox(&e.b);
608 >                        switch (xev.xbutton.button) {
609 >                        case Button1:
610 >                                getbox(&xev.xbutton);
611 >                                break;
612 >                        case Button2:
613 >                                traceray(xev.xbutton.x, xev.xbutton.y);
614 >                                break;
615 >                        case Button3:
616 >                                trackrays(&xev.xbutton);
617 >                                break;
618 >                        }
619                  break;
620 +        case ClientMessage:
621 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
622 +                                (xev.xclient.data.l[0] == closedownAtom))
623 +                        quiterr(NULL);
624 +                break;
625          }
626   }
627  
628  
629 < docom(ekey)                                     /* execute command */
629 > traceray(xpos, ypos)                    /* print requested pixel data */
630 > int  xpos, ypos;
631 > {
632 >        extern char  *index();
633 >        FLOAT  hv[2];
634 >        FVECT  rorg, rdir;
635 >        COLOR  cval;
636 >        register char  *cp;
637 >
638 >        box.xmin = xpos; box.xsiz = 1;
639 >        box.ymin = ypos; box.ysiz = 1;
640 >        avgbox(cval);
641 >        scalecolor(cval, 1./exposure);
642 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
643 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
644 >                rorg[0] = rorg[1] = rorg[2] =
645 >                rdir[0] = rdir[1] = rdir[2] = 0.;
646 >
647 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
648 >                switch (*cp) {
649 >                case 'o':                       /* origin */
650 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
651 >                        break;
652 >                case 'd':                       /* direction */
653 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
654 >                        break;
655 >                case 'v':                       /* radiance value */
656 >                        printf("%e %e %e ", colval(cval,RED),
657 >                                        colval(cval,GRN), colval(cval,BLU));
658 >                        break;
659 >                case 'l':                       /* luminance */
660 >                        printf("%e ", luminance(cval));
661 >                        break;
662 >                case 'p':                       /* pixel position */
663 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
664 >                                        (int)(hv[1]*inpres.yr));
665 >                        break;
666 >                }
667 >        putchar('\n');
668 >        fflush(stdout);
669 >        return(0);
670 > }
671 >
672 >
673 > docom(ekey)                             /* execute command */
674   XKeyPressedEvent  *ekey;
675   {
676          char  buf[80];
# Line 534 | Line 679 | XKeyPressedEvent  *ekey;
679          int  com, n;
680          double  comp;
681          FLOAT  hv[2];
537        FVECT  rorg, rdir;
682  
683          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
684          if (n == 0)
# Line 542 | Line 686 | XKeyPressedEvent  *ekey;
686          com = buf[0];
687          switch (com) {                  /* interpret command */
688          case 'q':
689 +        case 'Q':
690          case CTRL('D'):                         /* quit */
691 <                quit(0);
691 >                quiterr(NULL);
692          case '\n':
693          case '\r':
694          case 'l':
695          case 'c':                               /* value */
696 <                if (avgbox(cval) == -1)
696 >                if (!avgbox(cval))
697                          return(-1);
698                  switch (com) {
699                  case '\n':
# Line 589 | Line 734 | XKeyPressedEvent  *ekey;
734                                          buf, strlen(buf));
735                  return(0);
736          case 't':                               /* trace */
737 <                if (!gotview) {
738 <                        XBell(thedisplay, 0);
737 >                return(traceray(ekey->x, ekey->y));
738 >        case 'a':                               /* auto exposure */
739 >                if (fname == NULL)
740                          return(-1);
741 <                }
742 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
743 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
741 >                tmflags = TM_F_CAMERA;
742 >                strcpy(buf, "auto exposure...");
743 >                goto remap;
744 >        case 'h':                               /* human response */
745 >                if (fname == NULL)
746                          return(-1);
747 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
748 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
749 <                fflush(stdout);
602 <                return(0);
747 >                tmflags = TM_F_HUMAN;
748 >                strcpy(buf, "human exposure...");
749 >                goto remap;
750          case '=':                               /* adjust exposure */
751 <                if (avgbox(cval) == -1)
751 >        case '@':                               /* adaptation level */
752 >                if (!avgbox(cval))
753                          return(-1);
754 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
755 <                if (n == 0)
756 <                        return(0);
757 <                scale_rcolors(ourras, pow(2.0, (double)n));
754 >                comp = bright(cval);
755 >                if (comp < 1e-20) {
756 >                        XBell(thedisplay, 0);
757 >                        return(-1);
758 >                }
759 >                if (com == '@')
760 >                        comp = 106./exposure/
761 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
762 >                else
763 >                        comp = .5/comp;
764 >                comp = log(comp)/.69315 - scale;
765 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
766 >                if (tmflags != TM_F_LINEAR)
767 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
768 >                else {
769 >                        if (n == 0)             /* else check if any change */
770 >                                return(0);
771 >                        scale_rcolors(ourras, pow(2.0, (double)n));
772 >                }
773                  scale += n;
774                  sprintf(buf, "%+d", scale);
775 +        remap:
776                  XDrawImageString(thedisplay, wind, ourgc,
777                                  box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
778                  XFlush(thedisplay);
# Line 622 | Line 786 | XKeyPressedEvent  *ekey;
786                  XClearWindow(thedisplay, wind);
787                  map_rcolors(ourras, wind);
788                  if (fast)
789 <                        make_rpixmap(ourras);
789 >                        make_rpixmap(ourras, wind);
790                  redraw(0, 0, width, height);
791                  return(0);
792 +        case 'f':                               /* turn on fast redraw */
793 +                fast = 1;
794 +                make_rpixmap(ourras, wind);
795 +                return(0);
796 +        case 'F':                               /* turn off fast redraw */
797 +                fast = 0;
798 +                free_rpixmap(ourras);
799 +                return(0);
800 +        case '0':                               /* recenter origin */
801 +                if (xoff == 0 & yoff == 0)
802 +                        return(0);
803 +                xoff = yoff = 0;
804 +                XClearWindow(thedisplay, wind);
805 +                redraw(0, 0, width, height);
806 +                return(0);
807          case ' ':                               /* clear */
808                  redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
809                  return(0);
# Line 638 | Line 817 | XKeyPressedEvent  *ekey;
817   moveimage(ebut)                         /* shift the image */
818   XButtonPressedEvent  *ebut;
819   {
820 <        union {
642 <                XEvent  u;
643 <                XButtonReleasedEvent  b;
644 <                XPointerMovedEvent  m;
645 <        }  e;
820 >        XEvent  e;
821          int     mxo, myo;
822  
823 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
824 <        while (e.u.type == MotionNotify) {
825 <                mxo = e.m.x;
826 <                myo = e.m.y;
823 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
824 >        while (e.type == MotionNotify) {
825 >                mxo = e.xmotion.x;
826 >                myo = e.xmotion.y;
827                  revline(ebut->x, ebut->y, mxo, myo);
828                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
829                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
830 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
830 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
831                  revline(ebut->x, ebut->y, mxo, myo);
832                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
833                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
834          }
835 <        xoff += e.b.x - ebut->x;
836 <        yoff += e.b.y - ebut->y;
835 >        xoff += e.xbutton.x - ebut->x;
836 >        yoff += e.xbutton.y - ebut->y;
837          XClearWindow(thedisplay, wind);
838          redraw(0, 0, width, height);
839   }
# Line 667 | Line 842 | XButtonPressedEvent  *ebut;
842   getbox(ebut)                            /* get new box */
843   XButtonPressedEvent  *ebut;
844   {
845 <        union {
671 <                XEvent  u;
672 <                XButtonReleasedEvent  b;
673 <                XPointerMovedEvent  m;
674 <        }  e;
845 >        XEvent  e;
846  
847 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
848 <        while (e.u.type == MotionNotify) {
849 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
850 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
847 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
848 >        while (e.type == MotionNotify) {
849 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
850 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
851                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
852          }
853 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
854 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
853 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
854 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
855          if (box.xmin > ebut->x) {
856                  box.xsiz = box.xmin - ebut->x + 1;
857                  box.xmin = ebut->x;
# Line 696 | Line 867 | XButtonPressedEvent  *ebut;
867   }
868  
869  
870 + trackrays(ebut)                         /* trace rays as mouse moves */
871 + XButtonPressedEvent  *ebut;
872 + {
873 +        XEvent  e;
874 +        unsigned long   lastrept;
875 +
876 +        traceray(ebut->x, ebut->y);
877 +        lastrept = ebut->time;
878 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
879 +        while (e.type == MotionNotify) {
880 +                if (e.xmotion.time >= lastrept + tinterv) {
881 +                        traceray(e.xmotion.x, e.xmotion.y);
882 +                        lastrept = e.xmotion.time;
883 +                }
884 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
885 +        }
886 + }
887 +
888 +
889   revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
890   int  x0, y0, x1, y1;
891   {
# Line 706 | Line 896 | int  x0, y0, x1, y1;
896   }
897  
898  
899 < avgbox(clr)                             /* average color over current box */
900 < COLOR  clr;
899 > int
900 > colavg(scn, n, cavg)
901 > register COLR   *scn;
902 > register int    n;
903 > COLOR   cavg;
904   {
905 <        static COLOR  lc;
906 <        static int  ll, lr, lt, lb;
905 >        COLOR   col;
906 >
907 >        while (n--) {
908 >                colr_color(col, scn++);
909 >                addcolor(cavg, col);
910 >        }
911 > }
912 >
913 >
914 > int
915 > avgbox(cavg)                            /* average color over current box */
916 > COLOR   cavg;
917 > {
918 >        double  d;
919 >        register int    rval;
920 >
921 >        setcolor(cavg, 0., 0., 0.);
922 >        rval = dobox(colavg, (char *)cavg);
923 >        if (rval > 0) {
924 >                d = 1./rval;
925 >                scalecolor(cavg, d);
926 >        }
927 >        return(rval);
928 > }
929 >
930 >
931 > int
932 > dobox(f, p)                             /* run function over box */
933 > int     (*f)();                 /* function to call for each subscan */
934 > char    *p;                     /* pointer to private data */
935 > {
936          int  left, right, top, bottom;
937          int  y;
716        double  d;
717        COLOR  ctmp;
718        register int  x;
938  
720        setcolor(clr, 0.0, 0.0, 0.0);
939          left = box.xmin - xoff;
940          right = left + box.xsiz;
941          if (left < 0)
# Line 725 | Line 943 | COLOR  clr;
943          if (right > xmax)
944                  right = xmax;
945          if (left >= right)
946 <                return(-1);
946 >                return(0);
947          top = box.ymin - yoff;
948          bottom = top + box.ysiz;
949          if (top < 0)
# Line 733 | Line 951 | COLOR  clr;
951          if (bottom > ymax)
952                  bottom = ymax;
953          if (top >= bottom)
954 <                return(-1);
737 <        if (left == ll && right == lr && top == lt && bottom == lb) {
738 <                copycolor(clr, lc);
739 <                return;
740 <        }
954 >                return(0);
955          for (y = top; y < bottom; y++) {
956                  if (getscan(y) == -1)
957                          return(-1);
958 <                for (x = left; x < right; x++) {
959 <                        colr_color(ctmp, scanline[x]);
960 <                        addcolor(clr, ctmp);
958 >                (*f)(scanline+left, right-left, p);
959 >        }
960 >        return((right-left)*(bottom-top));
961 > }
962 >
963 >
964 > int
965 > addfix(scn, n)                  /* add fixation points to histogram */
966 > COLR    *scn;
967 > int     n;
968 > {
969 >        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
970 >                goto tmerr;
971 >        if (tmAddHisto(lscan, n, FIXWEIGHT))
972 >                goto tmerr;
973 >        return;
974 > tmerr:
975 >        quiterr("tone mapping error");
976 > }
977 >
978 >
979 > make_tonemap()                  /* initialize tone mapping */
980 > {
981 >        int  flags, y;
982 >
983 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
984 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
985 >                                progname);
986 >                tmflags = TM_F_LINEAR;
987 >        }
988 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
989 >                setcolrcor(pow, 1.0/gamcor);
990 >                return;
991 >        }
992 >        flags = tmflags;                /* histogram adjustment */
993 >        if (greyscale) flags |= TM_F_BW;
994 >        if (tmTop != NULL) {            /* reuse old histogram if one */
995 >                tmDone(tmTop);
996 >                tmTop->flags = flags;
997 >        } else {                        /* else initialize */
998 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
999 >                        goto memerr;
1000 >                if (greyscale) {
1001 >                        cscan = TM_NOCHROM;
1002 >                        if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1003 >                                goto memerr;
1004 >                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1005 >                                == NULL)
1006 >                        goto memerr;
1007 >                                                /* initialize tm library */
1008 >                if (tmInit(flags, stdprims, gamcor) == NULL)
1009 >                        goto memerr;
1010 >                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1011 >                        goto tmerr;
1012 >                                                /* compute picture histogram */
1013 >                for (y = 0; y < ymax; y++) {
1014 >                        getscan(y);
1015 >                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1016 >                                goto tmerr;
1017 >                        if (tmAddHisto(lscan, xmax, 1))
1018 >                                goto tmerr;
1019                  }
1020          }
1021 <        d = 1.0/((right-left)*(bottom-top));
1022 <        scalecolor(clr, d);
1023 <        ll = left; lr = right; lt = top; lb = bottom;
1024 <        copycolor(lc, clr);
1025 <        return(0);
1021 >        tmDup();                        /* add fixations to duplicate map */
1022 >        dobox(addfix, NULL);
1023 >                                        /* (re)compute tone mapping */
1024 >        if (tmComputeMapping(gamcor, 0., 0.))
1025 >                goto tmerr;
1026 >        return;
1027 > memerr:
1028 >        quiterr("out of memory in make_tonemap");
1029 > tmerr:
1030 >        quiterr("tone mapping error");
1031   }
1032  
1033  
1034 + tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1035 + register COLR  *scn;
1036 + int  len;
1037 + {
1038 +        register BYTE  *ps;
1039 +
1040 +        if (tmflags == TM_F_LINEAR) {
1041 +                if (scale)
1042 +                        shiftcolrs(scn, len, scale);
1043 +                colrs_gambs(scn, len);
1044 +                return;
1045 +        }
1046 +        if (len > xmax)
1047 +                quiterr("code error 1 in tmap_colrs");
1048 +        if (tmCvColrs(lscan, cscan, scn, len))
1049 +                goto tmerr;
1050 +        if (tmMapPixels(pscan, lscan, cscan, len))
1051 +                goto tmerr;
1052 +        ps = pscan;
1053 +        if (greyscale)
1054 +                while (len--) {
1055 +                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1056 +                        scn[0][EXP] = COLXS;
1057 +                        scn++;
1058 +                }
1059 +        else
1060 +                while (len--) {
1061 +                        scn[0][RED] = *ps++;
1062 +                        scn[0][GRN] = *ps++;
1063 +                        scn[0][BLU] = *ps++;
1064 +                        scn[0][EXP] = COLXS;
1065 +                        scn++;
1066 +                }
1067 +        return;
1068 + tmerr:
1069 +        quiterr("tone mapping error");
1070 + }
1071 +
1072 +
1073   getmono()                       /* get monochrome data */
1074   {
1075          register unsigned char  *dp;
# Line 765 | Line 1081 | getmono()                      /* get monochrome data */
1081                  quiterr("out of memory in getmono");
1082          dp = ourdata - 1;
1083          for (y = 0; y < ymax; y++) {
1084 <                if (getscan(y) < 0)
769 <                        quiterr("seek error in getmono");
770 <                normcolrs(scanline, xmax, scale);
1084 >                getscan(y);
1085                  add2icon(y, scanline);
1086 +                normcolrs(scanline, xmax, scale);
1087                  err = 0;
1088                  for (x = 0; x < xmax; x++) {
1089                          if (!(x&7))
# Line 794 | Line 1109 | COLR  *scan;
1109          static short  cerr[ICONSIZ];
1110          static int  ynext;
1111          static char  *dp;
1112 +        COLR  clr;
1113          register int  err;
1114          register int    x, ti;
1115          int  errp;
# Line 824 | Line 1140 | COLR  *scan;
1140                          *++dp = 0;
1141                  errp = err;
1142                  ti = x*xmax/iconwidth;
1143 <                err += normbright(scan[ti]) + cerr[x];
1143 >                copycolr(clr, scan[ti]);
1144 >                normcolrs(clr, 1, scale);
1145 >                err += normbright(clr) + cerr[x];
1146                  if (err > 127)
1147                          err -= 255;
1148                  else
# Line 839 | Line 1157 | COLR  *scan;
1157   getfull()                       /* get full (24-bit) data */
1158   {
1159          int     y;
1160 <        register unsigned long  *dp;
1160 >        register unsigned int4  *dp;
1161          register int    x;
1162 <                                        /* set gamma correction */
1163 <        setcolrgam(gamcor);
1162 >                                        /* initialize tone mapping */
1163 >        make_tonemap();
1164                                          /* read and convert file */
1165 <        dp = (unsigned long *)ourdata;
1165 >        dp = (unsigned int4 *)ourdata;
1166          for (y = 0; y < ymax; y++) {
1167 <                if (getscan(y) < 0)
850 <                        quiterr("seek error in getfull");
851 <                if (scale)
852 <                        shiftcolrs(scanline, xmax, scale);
853 <                colrs_gambs(scanline, xmax);
1167 >                getscan(y);
1168                  add2icon(y, scanline);
1169 +                tmap_colrs(scanline, xmax);
1170                  if (ourras->image->blue_mask & 1)
1171                          for (x = 0; x < xmax; x++)
1172 <                                *dp++ = scanline[x][RED] << 16 |
1173 <                                        scanline[x][GRN] << 8 |
1174 <                                        scanline[x][BLU] ;
1172 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1173 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1174 >                                        (unsigned int4)scanline[x][BLU] ;
1175                  else
1176                          for (x = 0; x < xmax; x++)
1177 <                                *dp++ = scanline[x][RED] |
1178 <                                        scanline[x][GRN] << 8 |
1179 <                                        scanline[x][BLU] << 16 ;
1177 >                                *dp++ = (unsigned int4)scanline[x][RED] |
1178 >                                        (unsigned int4)scanline[x][GRN] << 8 |
1179 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
1180          }
1181   }
1182  
# Line 871 | Line 1186 | getgrey()                      /* get greyscale data */
1186          int     y;
1187          register unsigned char  *dp;
1188          register int    x;
1189 <                                        /* set gamma correction */
1190 <        setcolrgam(gamcor);
1189 >                                        /* initialize tone mapping */
1190 >        make_tonemap();
1191                                          /* read and convert file */
1192          dp = ourdata;
1193          for (y = 0; y < ymax; y++) {
1194 <                if (getscan(y) < 0)
880 <                        quiterr("seek error in getfull");
881 <                if (scale)
882 <                        shiftcolrs(scanline, xmax, scale);
883 <                colrs_gambs(scanline, xmax);
1194 >                getscan(y);
1195                  add2icon(y, scanline);
1196 <                if (ourvis.colormap_size < 256)
1196 >                tmap_colrs(scanline, xmax);
1197 >                if (maxcolors < 256)
1198                          for (x = 0; x < xmax; x++)
1199 <                                *dp++ = ((long)normbright(scanline[x]) *
1200 <                                        ourvis.colormap_size + 128) >> 8;
1199 >                                *dp++ = ((int4)scanline[x][GRN] *
1200 >                                        maxcolors + maxcolors/2) >> 8;
1201                  else
1202                          for (x = 0; x < xmax; x++)
1203 <                                *dp++ = normbright(scanline[x]);
1203 >                                *dp++ = scanline[x][GRN];
1204          }
1205 +        for (x = 0; x < maxcolors; x++)
1206 +                clrtab[x][RED] = clrtab[x][GRN] =
1207 +                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1208   }
1209  
1210  
1211 + getmapped()                     /* get color-mapped data */
1212 + {
1213 +        int     y;
1214 +                                        /* make sure we can do it first */
1215 +        if (fname == NULL)
1216 +                quiterr("cannot map colors from standard input");
1217 +                                        /* initialize tone mapping */
1218 +        make_tonemap();
1219 +                                        /* make histogram */
1220 +        if (new_histo((int4)xmax*ymax) == -1)
1221 +                quiterr("cannot initialize histogram");
1222 +        for (y = 0; y < ymax; y++) {
1223 +                if (getscan(y) < 0)
1224 +                        break;
1225 +                add2icon(y, scanline);
1226 +                tmap_colrs(scanline, xmax);
1227 +                cnt_colrs(scanline, xmax);
1228 +        }
1229 +                                        /* map pixels */
1230 +        if (!new_clrtab(maxcolors))
1231 +                quiterr("cannot create color map");
1232 +        for (y = 0; y < ymax; y++) {
1233 +                getscan(y);
1234 +                tmap_colrs(scanline, xmax);
1235 +                if (dither)
1236 +                        dith_colrs(ourdata+y*xmax, scanline, xmax);
1237 +                else
1238 +                        map_colrs(ourdata+y*xmax, scanline, xmax);
1239 +        }
1240 + }
1241 +
1242 +
1243   scale_rcolors(xr, sf)                   /* scale color map */
1244   register XRASTER        *xr;
1245   double  sf;
# Line 924 | Line 1271 | double sf;
1271   getscan(y)
1272   int  y;
1273   {
1274 +        static int  trunced = -1;               /* truncated file? */
1275 + skipit:
1276 +        if (trunced >= 0 && y >= trunced) {
1277 +                bzero(scanline, xmax*sizeof(COLR));
1278 +                return(-1);
1279 +        }
1280          if (y != cury) {
1281                  if (scanpos == NULL || scanpos[y] == -1)
1282                          return(-1);
1283                  if (fseek(fin, scanpos[y], 0) == -1)
1284                          quiterr("fseek error");
1285                  cury = y;
1286 <        } else if (scanpos != NULL)
1286 >        } else if (scanpos != NULL && scanpos[y] == -1)
1287                  scanpos[y] = ftell(fin);
1288  
1289 <        if (freadcolrs(scanline, xmax, fin) < 0)
1290 <                quiterr("read error");
1291 <
1289 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1290 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1291 >                                progname, fname==NULL?"<stdin>":fname);
1292 >                trunced = y;
1293 >                goto skipit;
1294 >        }
1295          cury++;
1296          return(0);
941 }
942
943
944 picreadline3(y, l3)                     /* read in 3-byte scanline */
945 int  y;
946 register rgbpixel  *l3;
947 {
948        register int    i;
949                                                        /* read scanline */
950        if (getscan(y) < 0)
951                quiterr("cannot seek for picreadline");
952                                                        /* convert scanline */
953        normcolrs(scanline, xmax, scale);
954        add2icon(y, scanline);
955        for (i = 0; i < xmax; i++) {
956                l3[i].r = scanline[i][RED];
957                l3[i].g = scanline[i][GRN];
958                l3[i].b = scanline[i][BLU];
959        }
960 }
961
962
963 picwriteline(y, l)              /* add 8-bit scanline to image */
964 int  y;
965 pixel  *l;
966 {
967        bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
968 }
969
970
971 picreadcm(map)                  /* do gamma correction */
972 colormap  map;
973 {
974        extern double  pow();
975        register int  i, val;
976
977        for (i = 0; i < 256; i++) {
978                val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0;
979                map[0][i] = map[1][i] = map[2][i] = val;
980        }
981 }
982
983
984 picwritecm(map)                 /* handled elsewhere */
985 colormap  map;
986 {
987 #if 0
988        register int i;
989
990        for (i = 0; i < 256; i++)
991                printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
992 #endif
1297   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines