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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines