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.38 by greg, Tue Sep 20 15:48:15 1994 UTC vs.
Revision 2.79 by greg, Mon Apr 28 01:37:07 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  x11image.c - driver for X-windows
6   *
# Line 21 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "standard.h"
20  
21 + #include  <string.h>
22   #include  <signal.h>
23 + #include  <unistd.h>
24 + #include  <sys/types.h>
25 + #include  <sys/wait.h>
26   #include  <X11/Xlib.h>
27   #include  <X11/cursorfont.h>
28   #include  <X11/Xutil.h>
29   #include  <X11/Xatom.h>
30  
31   #include  "color.h"
32 + #include  "tonemap.h"
33 + #include  "clrtab.h"
34   #include  "view.h"
35   #include  "x11raster.h"
36   #include  "random.h"
34 #include  "resolu.h"
37  
36 #ifdef  __alpha
37 #define  int4           int
38 #endif
39 #ifndef  int4
40 #define  int4           long
41 #endif
42
38   #define  FONTNAME       "8x13"          /* text font we'll use */
39  
40   #define  CTRL(c)        ((c)-'@')
# Line 48 | Line 43 | static char SCCSid[] = "$SunId$ LBL";
43  
44   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
45  
46 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
47 +
48   #define  ourscreen      DefaultScreen(thedisplay)
49   #define  ourroot        RootWindow(thedisplay,ourscreen)
50  
# Line 74 | Line 71 | int  xoff = 0;                         /* x image offset */
71   int  yoff = 0;                          /* y image offset */
72  
73   int  parent = 0;                        /* number of children, -1 if child */
74 + int  sequential = 0;                    /* display images in sequence */
75  
76 + char  *tout = "od";                     /* output of 't' command */
77 + int  tinterv = 0;                       /* interval between mouse reports */
78 +
79 + int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
80 +
81   VIEW  ourview = STDVIEW;                /* image view parameters */
82   int  gotview = 0;                       /* got parameters from file */
83  
84   COLR  *scanline;                        /* scan line buffer */
85 + TMbright  *lscan;                       /* encoded luminance scanline */
86 + uby8  *cscan;                           /* encoded chroma scanline */
87 + uby8  *pscan;                           /* compute pixel scanline */
88  
89   RESOLU  inpres;                         /* input resolution and ordering */
90   int  xmax, ymax;                        /* picture dimensions */
91   int  width, height;                     /* window size */
92   char  *fname = NULL;                    /* input file name */
93 < FILE  *fin = stdin;                     /* input file */
93 > FILE  *fin = NULL;                      /* input file */
94   long  *scanpos = NULL;                  /* scan line positions in file */
95   int  cury = 0;                          /* current scan location */
96  
# Line 92 | Line 98 | double  exposure = 1.0;                        /* exposure compensation use
98  
99   int  wrongformat = 0;                   /* input in another format? */
100  
101 + TMstruct        *tmGlobal;              /* base tone-mapping */
102 + TMstruct        *tmCurrent;             /* curren tone-mapping */
103 +
104   GC      ourgc;                          /* standard graphics context */
105   GC      revgc;                          /* graphics context with GXinvert */
106  
# Line 102 | Line 111 | unsigned char  *ourdata;               /* our image data */
111  
112   struct {
113          int  xmin, ymin, xsiz, ysiz;
114 < }  box = {0, 0, 0, 0};                  /* current box */
114 > }  bbox = {0, 0, 0, 0};                 /* current bbox */
115  
116   char  *geometry = NULL;                 /* geometry specification */
117  
# Line 113 | Line 122 | char  *progname;
122  
123   char  errmsg[128];
124  
125 < BYTE  clrtab[256][3];                   /* global color map */
125 > uby8  clrtab[256][3];                   /* global color map */
126  
118 extern long  ftell();
127  
120 extern char  *getenv();
121
128   Display  *thedisplay;
129   Atom  closedownAtom, wmProtocolsAtom;
130  
131   int  sigrecv;
132  
133 < int  onsig() { sigrecv++; }
133 > void  onsig(int i) { sigrecv++; }
134  
135 + typedef void doboxf_t(COLR *scn, int n, void *);
136  
137 < main(argc, argv)
138 < int  argc;
139 < char  *argv[];
137 > static gethfunc headline;
138 > static void init(int argc, char **argv);
139 > static void quiterr(char *err);
140 > static int viscmp(XVisualInfo *v1, XVisualInfo *v2);
141 > static void getbestvis(void);
142 > static void getras(void);
143 > static void getevent(void);
144 > static int traceray(int xpos, int ypos);
145 > static int docom(XKeyPressedEvent *ekey);
146 > static void moveimage(XButtonPressedEvent *ebut);
147 > static void getbox(XButtonPressedEvent *ebut);
148 > static void trackrays(XButtonPressedEvent *ebut);
149 > static void revbox(int x0, int y0, int x1, int y1);
150 > static doboxf_t colavg;
151 > static doboxf_t addfix;
152 > static int avgbox(COLOR cavg);
153 > static int dobox(doboxf_t *f, void *p);
154 > static void make_tonemap(void);
155 > static void tmap_colrs(COLR *scn, int len);
156 > static void getmono(void);
157 > static void add2icon(int y, COLR *scan);
158 > static void getfull(void);
159 > static void getgrey(void);
160 > static void getmapped(void);
161 > static void scale_rcolors(XRASTER *xr, double sf);
162 > static int getscan(int y);
163 >
164 >
165 > int
166 > main(int  argc, char  *argv[])
167   {
134        int  headline();
168          int  i;
169          int  pid;
170          
171          progname = argv[0];
172 +        fin = stdin;
173  
174          for (i = 1; i < argc; i++)
175                  if (argv[i][0] == '-')
176                          switch (argv[i][1]) {
177 <                        case 'c':
177 >                        case 'c':                       /* number of colors */
178                                  maxcolors = atoi(argv[++i]);
179                                  break;
180 <                        case 'b':
180 >                        case 'b':                       /* greyscale only */
181                                  greyscale = !greyscale;
182                                  break;
183 <                        case 'm':
183 >                        case 'm':                       /* monochrome */
184 >                                greyscale = 1;
185                                  maxcolors = 2;
186                                  break;
187 <                        case 'd':
187 >                        case 'd':                       /* display or dither */
188                                  if (argv[i][2] == 'i')
189                                          dispname = argv[++i];
190                                  else
191                                          dither = !dither;
192                                  break;
193 <                        case 'f':
193 >                        case 'f':                       /* save pixmap */
194                                  fast = !fast;
195                                  break;
196 <                        case 'e':
197 <                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
196 >                        case 's':                       /* one at a time */
197 >                                sequential = !sequential;
198 >                                break;
199 >                        case 'o':                       /* 't' output */
200 >                                tout = argv[i]+2;
201 >                                break;
202 >                        case 't':                       /* msec interval */
203 >                                tinterv = atoi(argv[++i]);
204 >                                break;
205 >                        case 'e':                       /* exposure comp. */
206 >                                i++;
207 >                                if (argv[i][0] == 'a') {
208 >                                        tmflags = TM_F_CAMERA;
209 >                                        break;
210 >                                }
211 >                                if (argv[i][0] == 'h') {
212 >                                        tmflags = TM_F_HUMAN;
213 >                                        break;
214 >                                }
215 >                                if (argv[i][0] != '+' && argv[i][0] != '-')
216                                          goto userr;
217 <                                scale = atoi(argv[++i]);
217 >                                scale = atoi(argv[i]);
218                                  break;
219 <                        case 'g':
219 >                        case 'g':                       /* gamma comp. */
220                                  if (argv[i][2] == 'e')
221                                          geometry = argv[++i];
222                                  else
# Line 214 | Line 267 | char  *argv[];
267  
268          init(argc, argv);                       /* get file and open window */
269  
217        if (parent < 0) {
218                kill(getppid(), SIGCONT);       /* signal parent if child */
219                sigrecv--;
220        }
270          for ( ; ; )
271                  getevent();             /* main loop */
272   userr:
273          fprintf(stderr,
274 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n",
274 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] hdr ..\n",
275                          progname);
276          exit(1);
277   }
278  
279  
280 < headline(s)             /* get relevant info from header */
281 < char  *s;
280 > static int
281 > headline(               /* get relevant info from header */
282 >        char    *s,
283 >        void    *p
284 > )
285   {
286 <        char  fmt[32];
286 >        char  fmt[MAXFMTLEN];
287  
288          if (isexpos(s))
289                  exposure *= exposval(s);
290 <        else if (isformat(s)) {
291 <                formatval(fmt, s);
292 <                wrongformat = strcmp(fmt, COLRFMT);
293 <        } else if (isview(s) && sscanview(&ourview, s) > 0)
294 <                gotview++;
290 >        else if (formatval(fmt, s))
291 >                wrongformat = strcmp(fmt, COLRFMT) && strcmp(fmt, SPECFMT);
292 >        else if (isview(s))
293 >                gotview += sscanview(&ourview, s);
294 >        else if (isncomp(s))
295 >                NCSAMP = ncompval(s);
296 >        else if (iswlsplit(s))
297 >                wlsplitval(WLPART, s);
298 >        return(0);
299   }
300  
301  
302 < init(argc, argv)                        /* get data and open window */
303 < int argc;
304 < char **argv;
302 > static void
303 > init(                   /* get data and open window */
304 >        int argc,
305 >        char **argv
306 > )
307   {
308          XSetWindowAttributes    ourwinattr;
309          XClassHint      xclshints;
# Line 254 | Line 312 | char **argv;
312          XTextProperty   windowName, iconName;
313          XGCValues       xgcv;
314          char    *name;
315 <        register int    i;
315 >        int     i;
316          
317          if (fname != NULL) {
318                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 305 | Line 363 | char **argv;
363                  }
364          }
365          /* open window */
366 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
367          ourwinattr.border_pixel = ourwhite;
368          ourwinattr.background_pixel = ourblack;
369 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
370 <                        ourvis.visual, AllocNone);
369 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
370 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
371 >                i |= CWColormap;
372 >        }
373          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
374                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
375          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
376          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
377                          xszhints.width, xszhints.height, BORWIDTH,
378 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
379 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
378 >                        ourvis.depth, InputOutput, ourvis.visual,
379 >                        i, &ourwinattr);
380          if (wind == 0)
381                  quiterr("cannot create window");
382          width = xmax;
# Line 340 | Line 401 | char **argv;
401          windowName.encoding = iconName.encoding = XA_STRING;
402          windowName.format = iconName.format = 8;
403          windowName.value = (u_char *)name;
404 <        windowName.nitems = strlen(windowName.value);
404 >        windowName.nitems = strlen((char *)windowName.value);
405          iconName.value = (u_char *)name;
406 <        iconName.nitems = strlen(windowName.value);
406 >        iconName.nitems = strlen((char *)windowName.value);
407  
408          xclshints.res_name = NULL;
409          xclshints.res_class = "Ximage";
# Line 353 | Line 414 | char **argv;
414          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
415  
416          XMapWindow(thedisplay, wind);
356        return;
417   } /* end of init */
418  
419  
420 < quiterr(err)            /* print message and exit */
421 < char  *err;
420 > static void
421 > quiterr(                /* print message and exit */
422 >        char  *err
423 > )
424   {
425 <        register int  es;
425 >        int  es;
426          int  cs;
427  
428 <        if (es = err != NULL)
428 >        if ( (es = (err != NULL)) )
429                  fprintf(stderr, "%s: %s: %s\n", progname,
430                                  fname==NULL?"<stdin>":fname, err);
431          if (thedisplay != NULL)
432                  XCloseDisplay(thedisplay);
433 <        if (parent < 0 & sigrecv == 0)
433 >        if ((parent < 0) & (sigrecv == 0))
434                  kill(getppid(), SIGCONT);
435          while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
436                  if (es == 0)
# Line 380 | Line 442 | char  *err;
442  
443  
444   static int
445 < viscmp(v1,v2)           /* compare visual to see which is better, descending */
446 < register XVisualInfo    *v1, *v2;
445 > viscmp(         /* compare visual to see which is better, descending */
446 >        XVisualInfo     *v1,
447 >        XVisualInfo     *v2
448 > )
449   {
450          int     bad1 = 0, bad2 = 0;
451 <        register int  *rp;
451 >        int  *rp;
452  
453          if (v1->class == v2->class) {
454 <                if (v1->class == TrueColor || v1->class == DirectColor) {
455 <                                        /* prefer 24-bit to 32-bit */
456 <                        if (v1->depth == 24 && v2->depth == 32)
454 >                if ((v1->class == TrueColor) | (v1->class == DirectColor)) {
455 >                                        /* prefer 24-bit */
456 >                        if ((v1->depth == 24) & (v2->depth > 24))
457                                  return(-1);
458 <                        if (v1->depth == 32 && v2->depth == 24)
458 >                        if ((v1->depth > 24) & (v2->depth == 24))
459                                  return(1);
460 <                        return(0);
460 >                                        /* go for maximum depth otherwise */
461 >                        return(v2->depth - v1->depth);
462                  }
463                                          /* don't be too greedy */
464 <                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
464 >                if ((maxcolors <= 1<<v1->depth) & (maxcolors <= 1<<v2->depth))
465                          return(v1->depth - v2->depth);
466                  return(v2->depth - v1->depth);
467          }
468 <                                        /* prefer Pseudo when < 24-bit */
469 <        if ((v1->class == TrueColor || v1->class == DirectColor) &&
470 <                        v1->depth < 24)
468 >                                        /* prefer Pseudo when < 15-bit */
469 >        if ((v1->class == TrueColor) | (v1->class == DirectColor) &&
470 >                        v1->depth < 15)
471                  bad1 = 1;
472 <        if ((v2->class == TrueColor || v2->class == DirectColor) &&
473 <                        v2->depth < 24)
472 >        if ((v2->class == TrueColor) | (v2->class == DirectColor) &&
473 >                        v2->depth < 15)
474                  bad2 = -1;
475          if (bad1 | bad2)
476                  return(bad1+bad2);
# Line 420 | Line 485 | register XVisualInfo   *v1, *v2;
485   }
486  
487  
488 < getbestvis()                    /* get the best visual for this screen */
488 > static void
489 > getbestvis(void)                        /* get the best visual for this screen */
490   {
491   #ifdef DEBUG
492   static char  vistype[][12] = {
# Line 439 | Line 505 | static char  vistype[][12] = {
505          };
506          XVisualInfo     *xvi;
507          int     vismatched;
508 <        register int    i, j;
508 >        int     i, j;
509  
510          if (greyscale) {
511                  ourrank = rankings[2];
# Line 472 | Line 538 | static char  vistype[][12] = {
538          if (viscmp(&xvi[i],&ourvis) > 0)
539                  quiterr("inadequate visuals on this screen");
540                                          /* OK, we'll use it */
541 <        copystruct(&ourvis, &xvi[i]);
541 >        ourvis = xvi[i];
542   #ifdef DEBUG
543          fprintf(stderr, "Selected visual type %s, depth %d\n",
544                          vistype[ourvis.class], ourvis.depth);
# Line 502 | Line 568 | static char  vistype[][12] = {
568   }
569  
570  
571 < getras()                                /* get raster file */
571 > static void
572 > getras(void)                            /* get raster file */
573   {
507        XVisualInfo     vinfo;
508
574          if (maxcolors <= 2) {           /* monochrome */
575                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
576                  if (ourdata == NULL)
577                          goto fail;
578 <                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
578 >                ourras = make_raster(thedisplay, &ourvis, 1, (char *)ourdata,
579                                  xmax, ymax, 8);
580                  if (ourras == NULL)
581                          goto fail;
582                  getmono();
583 <        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
584 <                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
583 >        } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) {
584 >                int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
585 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
586                  if (ourdata == NULL)
587                          goto fail;
588 <                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
589 <                                ourdata, xmax, ymax, 32);
588 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
589 >                                (char *)ourdata, xmax, ymax, datsiz*8);
590                  if (ourras == NULL)
591                          goto fail;
592                  getfull();
# Line 528 | Line 594 | getras()                               /* get raster file */
594                  ourdata = (unsigned char *)malloc(xmax*ymax);
595                  if (ourdata == NULL)
596                          goto fail;
597 <                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
597 >                ourras = make_raster(thedisplay, &ourvis, 8, (char *)ourdata,
598                                  xmax, ymax, 8);
599                  if (ourras == NULL)
600                          goto fail;
601 <                if (greyscale | ourvis.class == StaticGray)
601 >                if (greyscale)
602                          getgrey();
603                  else
604                          getmapped();
# Line 545 | Line 611 | fail:
611   }
612  
613  
614 < getevent()                              /* process the next event */
614 > static void
615 > getevent(void)                          /* process the next event */
616   {
617          XEvent xev;
618  
# Line 562 | Line 629 | getevent()                             /* process the next event */
629                  map_rcolors(ourras, wind);
630                  if (fast)
631                          make_rpixmap(ourras, wind);
632 +                if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
633 +                        kill(getppid(), SIGCONT);
634 +                        sigrecv--;
635 +                }
636                  break;
637          case UnmapNotify:
638                  if (!fast)
# Line 574 | Line 645 | getevent()                             /* process the next event */
645          case ButtonPress:
646                  if (xev.xbutton.state & (ShiftMask|ControlMask))
647                          moveimage(&xev.xbutton);
577                else if (xev.xbutton.button == Button2)
578                        traceray(xev.xbutton.x, xev.xbutton.y);
648                  else
649 <                        getbox(&xev.xbutton);
649 >                        switch (xev.xbutton.button) {
650 >                        case Button1:
651 >                                getbox(&xev.xbutton);
652 >                                break;
653 >                        case Button2:
654 >                                traceray(xev.xbutton.x, xev.xbutton.y);
655 >                                break;
656 >                        case Button3:
657 >                                trackrays(&xev.xbutton);
658 >                                break;
659 >                        }
660                  break;
661          case ClientMessage:
662                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
# Line 588 | Line 667 | getevent()                             /* process the next event */
667   }
668  
669  
670 < traceray(xpos, ypos)                    /* print ray corresponding to pixel */
671 < int  xpos, ypos;
670 > static int
671 > traceray(                       /* print requested pixel data */
672 >        int  xpos,
673 >        int  ypos
674 > )
675   {
676 <        FLOAT  hv[2];
676 >        RREAL  hv[2];
677          FVECT  rorg, rdir;
678 +        COLOR  cval;
679 +        char  *cp;
680  
681 <        if (!gotview) {         /* no view, no can do */
682 <                XBell(thedisplay, 0);
683 <                return(-1);
684 <        }
681 >        bbox.xmin = xpos; bbox.xsiz = 1;
682 >        bbox.ymin = ypos; bbox.ysiz = 1;
683 >        avgbox(cval);
684 >        scalecolor(cval, 1./exposure);
685          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
686 <        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
687 <                return(-1);
688 <        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
689 <        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
686 >        if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
687 >                rorg[0] = rorg[1] = rorg[2] =
688 >                rdir[0] = rdir[1] = rdir[2] = 0.;
689 >
690 >        for (cp = tout; *cp; cp++)      /* print what they asked for */
691 >                switch (*cp) {
692 >                case 'o':                       /* origin */
693 >                        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
694 >                        break;
695 >                case 'd':                       /* direction */
696 >                        printf("%e %e %e ", rdir[0], rdir[1], rdir[2]);
697 >                        break;
698 >                case 'v':                       /* radiance value */
699 >                        printf("%e %e %e ", colval(cval,RED),
700 >                                        colval(cval,GRN), colval(cval,BLU));
701 >                        break;
702 >                case 'l':                       /* luminance */
703 >                        printf("%e ", luminance(cval));
704 >                        break;
705 >                case 'p':                       /* pixel position */
706 >                        printf("%d %d ", (int)(hv[0]*inpres.xr),
707 >                                        (int)(hv[1]*inpres.yr));
708 >                        break;
709 >                }
710 >        putchar('\n');
711          fflush(stdout);
712          return(0);
713   }
714  
715  
716 < docom(ekey)                             /* execute command */
717 < XKeyPressedEvent  *ekey;
716 > static int
717 > docom(                          /* execute command */
718 >        XKeyPressedEvent  *ekey
719 > )
720   {
721          char  buf[80];
722          COLOR  cval;
723          XColor  cvx;
724          int  com, n;
725          double  comp;
726 <        FLOAT  hv[2];
726 >        RREAL  hv[2];
727  
728          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
729          if (n == 0)
# Line 631 | Line 738 | XKeyPressedEvent  *ekey;
738          case '\r':
739          case 'l':
740          case 'c':                               /* value */
741 <                if (avgbox(cval) == -1)
741 >                if (!avgbox(cval))
742                          return(-1);
743                  switch (com) {
744                  case '\n':
# Line 639 | Line 746 | XKeyPressedEvent  *ekey;
746                          sprintf(buf, "%.3f", intens(cval)/exposure);
747                          break;
748                  case 'l':                               /* luminance */
749 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
749 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
750                          break;
751                  case 'c':                               /* color */
752                          comp = pow(2.0, (double)scale);
# Line 650 | Line 757 | XKeyPressedEvent  *ekey;
757                          break;
758                  }
759                  XDrawImageString(thedisplay, wind, ourgc,
760 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
760 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
761                  return(0);
762          case 'i':                               /* identify (contour) */
763                  if (ourras->pixels == NULL)
# Line 673 | Line 780 | XKeyPressedEvent  *ekey;
780                  return(0);
781          case 't':                               /* trace */
782                  return(traceray(ekey->x, ekey->y));
783 +        case 'a':                               /* auto exposure */
784 +                if (fname == NULL)
785 +                        return(-1);
786 +                tmflags = TM_F_CAMERA;
787 +                strcpy(buf, "auto exposure...");
788 +                goto remap;
789 +        case 'h':                               /* human response */
790 +                if (fname == NULL)
791 +                        return(-1);
792 +                tmflags = TM_F_HUMAN;
793 +                strcpy(buf, "human exposure...");
794 +                goto remap;
795          case '=':                               /* adjust exposure */
796          case '@':                               /* adaptation level */
797 <                if (avgbox(cval) == -1)
797 >                if (!avgbox(cval))
798                          return(-1);
799                  comp = bright(cval);
800                  if (comp < 1e-20) {
# Line 689 | Line 808 | XKeyPressedEvent  *ekey;
808                          comp = .5/comp;
809                  comp = log(comp)/.69315 - scale;
810                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
811 <                if (n == 0)
812 <                        return(0);
813 <                scale_rcolors(ourras, pow(2.0, (double)n));
811 >                if (tmflags != TM_F_LINEAR)
812 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
813 >                else {
814 >                        if (n == 0)             /* else check if any change */
815 >                                return(0);
816 >                        scale_rcolors(ourras, pow(2.0, (double)n));
817 >                }
818                  scale += n;
819                  sprintf(buf, "%+d", scale);
820 +        remap:
821                  XDrawImageString(thedisplay, wind, ourgc,
822 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
822 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
823                  XFlush(thedisplay);
824 <                free(ourdata);
824 >                /* free(ourdata);       This is done in XDestroyImage()! */
825                  free_raster(ourras);
826                  getras();
827          /* fall through */
# Line 719 | Line 843 | XKeyPressedEvent  *ekey;
843                  free_rpixmap(ourras);
844                  return(0);
845          case '0':                               /* recenter origin */
846 <                if (xoff == 0 & yoff == 0)
846 >                if ((xoff == 0) & (yoff == 0))
847                          return(0);
848                  xoff = yoff = 0;
849                  XClearWindow(thedisplay, wind);
850                  redraw(0, 0, width, height);
851                  return(0);
852          case ' ':                               /* clear */
853 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
853 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
854                  return(0);
855          default:
856                  XBell(thedisplay, 0);
# Line 735 | Line 859 | XKeyPressedEvent  *ekey;
859   }
860  
861  
862 < moveimage(ebut)                         /* shift the image */
863 < XButtonPressedEvent  *ebut;
862 > static void
863 > moveimage(                              /* shift the image */
864 >        XButtonPressedEvent  *ebut
865 > )
866   {
867          XEvent  e;
868          int     mxo, myo;
# Line 760 | Line 886 | XButtonPressedEvent  *ebut;
886   }
887  
888  
889 < getbox(ebut)                            /* get new box */
890 < XButtonPressedEvent  *ebut;
889 > static void
890 > getbox(                         /* get new bbox */
891 >        XButtonPressedEvent  *ebut
892 > )
893   {
894          XEvent  e;
895  
896          XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
897          while (e.type == MotionNotify) {
898 <                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
898 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
899                  XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
900 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
900 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
901          }
902 <        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
903 <        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
904 <        if (box.xmin > ebut->x) {
905 <                box.xsiz = box.xmin - ebut->x + 1;
906 <                box.xmin = ebut->x;
902 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
903 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
904 >        if (bbox.xmin > ebut->x) {
905 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
906 >                bbox.xmin = ebut->x;
907          } else {
908 <                box.xsiz = ebut->x - box.xmin + 1;
908 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
909          }
910 <        if (box.ymin > ebut->y) {
911 <                box.ysiz = box.ymin - ebut->y + 1;
912 <                box.ymin = ebut->y;
910 >        if (bbox.ymin > ebut->y) {
911 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
912 >                bbox.ymin = ebut->y;
913          } else {
914 <                box.ysiz = ebut->y - box.ymin + 1;
914 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
915          }
916   }
917  
918  
919 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
920 < int  x0, y0, x1, y1;
919 > static void
920 > trackrays(                              /* trace rays as mouse moves */
921 >        XButtonPressedEvent  *ebut
922 > )
923   {
924 +        XEvent  e;
925 +        unsigned long   lastrept;
926 +
927 +        traceray(ebut->x, ebut->y);
928 +        lastrept = ebut->time;
929 +        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
930 +        while (e.type == MotionNotify) {
931 +                if (e.xmotion.time >= lastrept + tinterv) {
932 +                        traceray(e.xmotion.x, e.xmotion.y);
933 +                        lastrept = e.xmotion.time;
934 +                }
935 +                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
936 +        }
937 + }
938 +
939 +
940 + static void
941 + revbox(                 /* draw bbox with reversed lines */
942 +        int  x0,
943 +        int  y0,
944 +        int  x1,
945 +        int  y1
946 + )
947 + {
948          revline(x0, y0, x1, y0);
949          revline(x0, y1, x1, y1);
950          revline(x0, y0, x0, y1);
# Line 798 | Line 952 | int  x0, y0, x1, y1;
952   }
953  
954  
955 < avgbox(clr)                             /* average color over current box */
956 < COLOR  clr;
955 > static void
956 > colavg(
957 >        COLR    *scn,
958 >        int     n,
959 >        void *cavg
960 > )
961   {
962 <        static COLOR  lc;
963 <        static int  ll, lr, lt, lb;
962 >        COLOR   col;
963 >
964 >        while (n--) {
965 >                colr_color(col, *scn);
966 >                addcolor((COLORV*)cavg, col);
967 >                scn++;
968 >        }
969 > }
970 >
971 >
972 > static int
973 > avgbox(                         /* average color over current bbox */
974 >        COLOR   cavg
975 > )
976 > {
977 >        double  d;
978 >        int     rval;
979 >
980 >        setcolor(cavg, 0., 0., 0.);
981 >        rval = dobox(colavg, cavg);
982 >        if (rval > 0) {
983 >                d = 1./rval;
984 >                scalecolor(cavg, d);
985 >        }
986 >        return(rval);
987 > }
988 >
989 >
990 > static int
991 > dobox(                          /* run function over bbox */
992 >        doboxf_t *f,                    /* function to call for each subscan */
993 >        void    *p                      /* pointer to private data */
994 > )
995 > {
996          int  left, right, top, bottom;
997          int  y;
808        double  d;
809        COLOR  ctmp;
810        register int  x;
998  
999 <        setcolor(clr, 0.0, 0.0, 0.0);
1000 <        left = box.xmin - xoff;
814 <        right = left + box.xsiz;
999 >        left = bbox.xmin - xoff;
1000 >        right = left + bbox.xsiz;
1001          if (left < 0)
1002                  left = 0;
1003          if (right > xmax)
1004                  right = xmax;
1005          if (left >= right)
1006 <                return(-1);
1007 <        top = box.ymin - yoff;
1008 <        bottom = top + box.ysiz;
1006 >                return(0);
1007 >        top = bbox.ymin - yoff;
1008 >        bottom = top + bbox.ysiz;
1009          if (top < 0)
1010                  top = 0;
1011          if (bottom > ymax)
1012                  bottom = ymax;
1013          if (top >= bottom)
828                return(-1);
829        if (left == ll && right == lr && top == lt && bottom == lb) {
830                copycolor(clr, lc);
1014                  return(0);
832        }
1015          for (y = top; y < bottom; y++) {
1016 <                if (getscan(y) == -1)
1016 >                if (getscan(y) < 0)
1017                          return(-1);
1018 <                for (x = left; x < right; x++) {
1019 <                        colr_color(ctmp, scanline[x]);
1020 <                        addcolor(clr, ctmp);
1018 >                (*f)(scanline+left, right-left, p);
1019 >        }
1020 >        return((right-left)*(bottom-top));
1021 > }
1022 >
1023 >
1024 > static void
1025 > addfix(                 /* add fixation points to histogram */
1026 >        COLR    *scn,
1027 >        int     n,
1028 >        void    *p
1029 > )
1030 > {
1031 >        TMstruct *      tms = (TMstruct *)p;
1032 >        
1033 >        if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n))
1034 >                goto tmerr;
1035 >        if (tmAddHisto(tms, lscan, n, FIXWEIGHT))
1036 >                goto tmerr;
1037 >        return;
1038 > tmerr:
1039 >        quiterr("tone mapping error");
1040 > }
1041 >
1042 >
1043 > static void
1044 > make_tonemap(void)                      /* initialize tone mapping */
1045 > {
1046 >        int  flags, y;
1047 >
1048 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
1049 >                fprintf(stderr, "%s: cannot adjust tone of standard input\n",
1050 >                                progname);
1051 >                tmflags = TM_F_LINEAR;
1052 >        }
1053 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
1054 >                setcolrcor(pow, 1.0/gamcor);
1055 >                return;
1056 >        }
1057 >        flags = tmflags;                /* histogram adjustment */
1058 >        if (greyscale) flags |= TM_F_BW;
1059 >        if (tmGlobal != NULL) {         /* reuse old histogram if one */
1060 >                tmDone(tmCurrent); tmCurrent = NULL;
1061 >                tmGlobal->flags = flags;
1062 >        } else {                        /* else initialize */
1063 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1064 >                        goto memerr;
1065 >                if (greyscale) {
1066 >                        cscan = TM_NOCHROM;
1067 >                        if ((pscan = (uby8 *)malloc(sizeof(uby8)*xmax)) == NULL)
1068 >                                goto memerr;
1069 >                } else if ((pscan=cscan = (uby8 *)malloc(3*sizeof(uby8)*xmax))
1070 >                                == NULL)
1071 >                        goto memerr;
1072 >                                                /* initialize tm library */
1073 >                tmGlobal = tmInit(flags, stdprims, gamcor);
1074 >                if (tmGlobal == NULL)
1075 >                        goto memerr;
1076 >                if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure))
1077 >                        goto tmerr;
1078 >                                                /* compute picture histogram */
1079 >                for (y = 0; y < ymax; y++) {
1080 >                        getscan(y);
1081 >                        if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM,
1082 >                                        scanline, xmax))
1083 >                                goto tmerr;
1084 >                        if (tmAddHisto(tmGlobal, lscan, xmax, 1))
1085 >                                goto tmerr;
1086                  }
1087          }
1088 <        d = 1.0/((right-left)*(bottom-top));
1089 <        scalecolor(clr, d);
1090 <        ll = left; lr = right; lt = top; lb = bottom;
1091 <        copycolor(lc, clr);
1092 <        return(0);
1088 >        tmCurrent = tmDup(tmGlobal);    /* add fixations to duplicate map */
1089 >        dobox(addfix, tmCurrent);
1090 >                                        /* (re)compute tone mapping */
1091 >        if (tmComputeMapping(tmCurrent, gamcor, 0., 0.))
1092 >                goto tmerr;
1093 >        return;
1094 > memerr:
1095 >        quiterr("out of memory in make_tonemap");
1096 > tmerr:
1097 >        quiterr("tone mapping error");
1098   }
1099  
1100  
1101 < getmono()                       /* get monochrome data */
1101 > static void
1102 > tmap_colrs(             /* apply tone mapping to scanline */
1103 >        COLR  *scn,
1104 >        int  len
1105 > )
1106   {
1107 <        register unsigned char  *dp;
1108 <        register int    x, err;
1107 >        uby8  *ps;
1108 >
1109 >        if (tmflags == TM_F_LINEAR) {
1110 >                if (scale)
1111 >                        shiftcolrs(scn, len, scale);
1112 >                colrs_gambs(scn, len);
1113 >                return;
1114 >        }
1115 >        if (len > xmax)
1116 >                quiterr("code error 1 in tmap_colrs");
1117 >        if (tmCvColrs(tmCurrent, lscan, cscan, scn, len))
1118 >                goto tmerr;
1119 >        if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len))
1120 >                goto tmerr;
1121 >        ps = pscan;
1122 >        if (greyscale)
1123 >                while (len--) {
1124 >                        scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++;
1125 >                        scn[0][EXP] = COLXS;
1126 >                        scn++;
1127 >                }
1128 >        else
1129 >                while (len--) {
1130 >                        scn[0][RED] = *ps++;
1131 >                        scn[0][GRN] = *ps++;
1132 >                        scn[0][BLU] = *ps++;
1133 >                        scn[0][EXP] = COLXS;
1134 >                        scn++;
1135 >                }
1136 >        return;
1137 > tmerr:
1138 >        quiterr("tone mapping error");
1139 > }
1140 >
1141 >
1142 > static void
1143 > getmono(void)                   /* get monochrome data */
1144 > {
1145 >        unsigned char   *dp;
1146 >        int     x, err;
1147          int     y, errp;
1148          short   *cerr;
1149  
# Line 874 | Line 1168 | getmono()                      /* get monochrome data */
1168                          cerr[x] = err + errp;
1169                  }
1170          }
1171 <        free((char *)cerr);
1171 >        free(cerr);
1172   }
1173  
1174  
1175 < add2icon(y, scan)               /* add a scanline to our icon data */
1176 < int  y;
1177 < COLR  *scan;
1175 > static void
1176 > add2icon(               /* add a scanline to our icon data */
1177 >        int  y,
1178 >        COLR  *scan
1179 > )
1180   {
1181          static short  cerr[ICONSIZ];
1182          static int  ynext;
1183          static char  *dp;
1184          COLR  clr;
1185 <        register int  err;
1186 <        register int    x, ti;
1185 >        int  err;
1186 >        int     x, ti;
1187          int  errp;
1188  
1189          if (iconheight == 0) {          /* initialize */
# Line 917 | Line 1213 | COLR  *scan;
1213                  errp = err;
1214                  ti = x*xmax/iconwidth;
1215                  copycolr(clr, scan[ti]);
1216 <                normcolrs(clr, 1, scale);
1216 >                normcolrs(&clr, 1, scale);
1217                  err += normbright(clr) + cerr[x];
1218                  if (err > 127)
1219                          err -= 255;
# Line 930 | Line 1226 | COLR  *scan;
1226   }
1227  
1228  
1229 < getfull()                       /* get full (24-bit) data */
1229 > static void
1230 > getfull(void)                   /* get full (24-bit) data */
1231   {
1232          int     y;
1233 <        register unsigned int4  *dp;
1234 <        register int    x;
1235 <                                        /* set gamma correction */
1236 <        setcolrgam(gamcor);
1233 >        uint32  *dp;
1234 >        uint16  *dph;
1235 >        int     x;
1236 >                                        /* initialize tone mapping */
1237 >        make_tonemap();
1238                                          /* read and convert file */
1239 <        dp = (unsigned int4 *)ourdata;
1239 >        dp = (uint32 *)ourdata;
1240 >        dph = (uint16 *)ourdata;
1241          for (y = 0; y < ymax; y++) {
1242                  getscan(y);
1243                  add2icon(y, scanline);
1244 <                if (scale)
1245 <                        shiftcolrs(scanline, xmax, scale);
1246 <                colrs_gambs(scanline, xmax);
948 <                if (ourras->image->blue_mask & 1)
1244 >                tmap_colrs(scanline, xmax);
1245 >                switch (ourras->image->blue_mask) {
1246 >                case 0xff:              /* 24-bit RGB */
1247                          for (x = 0; x < xmax; x++)
1248 <                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1249 <                                        (unsigned int4)scanline[x][GRN] << 8 |
1250 <                                        (unsigned int4)scanline[x][BLU] ;
1251 <                else
1248 >                                *dp++ = (uint32)scanline[x][RED] << 16 |
1249 >                                        (uint32)scanline[x][GRN] << 8 |
1250 >                                        (uint32)scanline[x][BLU] ;
1251 >                        break;
1252 >                case 0xff0000:          /* 24-bit BGR */
1253                          for (x = 0; x < xmax; x++)
1254 <                                *dp++ = (unsigned int4)scanline[x][RED] |
1255 <                                        (unsigned int4)scanline[x][GRN] << 8 |
1256 <                                        (unsigned int4)scanline[x][BLU] << 16 ;
1254 >                                *dp++ = (uint32)scanline[x][RED] |
1255 >                                        (uint32)scanline[x][GRN] << 8 |
1256 >                                        (uint32)scanline[x][BLU] << 16 ;
1257 >                        break;
1258 > #if 0
1259 >                case 0x1f:              /* 15-bit RGB */
1260 >                        for (x = 0; x < xmax; x++)
1261 >                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1262 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1263 >                                        (unsigned)scanline[x][BLU] >> 3 ;
1264 >                        break;
1265 >                case 0x7c00:            /* 15-bit BGR */
1266 >                        for (x = 0; x < xmax; x++)
1267 >                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1268 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1269 >                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1270 >                        break;
1271 > #endif
1272 >                default:                /* unknown */
1273 >                        if (ourvis.depth > 16)
1274 >                                for (x = 0; x < xmax; x++) {
1275 >                                        *dp = ourras->image->red_mask &
1276 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1277 >                                        *dp |= ourras->image->green_mask &
1278 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1279 >                                        *dp++ |= ourras->image->blue_mask &
1280 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1281 >                                }
1282 >                        else
1283 >                                for (x = 0; x < xmax; x++) {
1284 >                                        *dph = ourras->image->red_mask &
1285 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1286 >                                        *dph |= ourras->image->green_mask &
1287 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1288 >                                        *dph++ |= ourras->image->blue_mask &
1289 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1290 >                                }
1291 >                        break;
1292 >                }
1293          }
1294   }
1295  
1296  
1297 < getgrey()                       /* get greyscale data */
1297 > static void
1298 > getgrey(void)                   /* get greyscale data */
1299   {
1300          int     y;
1301 <        register unsigned char  *dp;
1302 <        register int    x;
1303 <                                        /* set gamma correction */
1304 <        setcolrgam(gamcor);
1301 >        unsigned char   *dp;
1302 >        int     x;
1303 >                                        /* initialize tone mapping */
1304 >        make_tonemap();
1305                                          /* read and convert file */
1306          dp = ourdata;
1307          for (y = 0; y < ymax; y++) {
1308                  getscan(y);
1309                  add2icon(y, scanline);
1310 <                if (scale)
975 <                        shiftcolrs(scanline, xmax, scale);
976 <                for (x = 0; x < xmax; x++)
977 <                        scanline[x][GRN] = normbright(scanline[x]);
978 <                colrs_gambs(scanline, xmax);
1310 >                tmap_colrs(scanline, xmax);
1311                  if (maxcolors < 256)
1312                          for (x = 0; x < xmax; x++)
1313 <                                *dp++ = ((long)scanline[x][GRN] *
1313 >                                *dp++ = ((int32)scanline[x][GRN] *
1314                                          maxcolors + maxcolors/2) >> 8;
1315                  else
1316                          for (x = 0; x < xmax; x++)
# Line 986 | Line 1318 | getgrey()                      /* get greyscale data */
1318          }
1319          for (x = 0; x < maxcolors; x++)
1320                  clrtab[x][RED] = clrtab[x][GRN] =
1321 <                        clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors;
1321 >                        clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1322   }
1323  
1324  
1325 < getmapped()                     /* get color-mapped data */
1325 > static void
1326 > getmapped(void)                 /* get color-mapped data */
1327   {
1328          int     y;
1329                                          /* make sure we can do it first */
1330          if (fname == NULL)
1331                  quiterr("cannot map colors from standard input");
1332 <                                        /* set gamma correction */
1333 <        setcolrgam(gamcor);
1332 >                                        /* initialize tone mapping */
1333 >        make_tonemap();
1334                                          /* make histogram */
1335 <        if (new_histo((long)xmax*ymax) == -1)
1335 >        if (new_histo((int32)xmax*ymax) == -1)
1336                  quiterr("cannot initialize histogram");
1337          for (y = 0; y < ymax; y++) {
1338                  if (getscan(y) < 0)
1339                          break;
1340                  add2icon(y, scanline);
1341 <                if (scale)
1009 <                        shiftcolrs(scanline, xmax, scale);
1010 <                colrs_gambs(scanline, xmax);
1341 >                tmap_colrs(scanline, xmax);
1342                  cnt_colrs(scanline, xmax);
1343          }
1344                                          /* map pixels */
# Line 1015 | Line 1346 | getmapped()                    /* get color-mapped data */
1346                  quiterr("cannot create color map");
1347          for (y = 0; y < ymax; y++) {
1348                  getscan(y);
1349 <                if (scale)
1019 <                        shiftcolrs(scanline, xmax, scale);
1020 <                colrs_gambs(scanline, xmax);
1349 >                tmap_colrs(scanline, xmax);
1350                  if (dither)
1351                          dith_colrs(ourdata+y*xmax, scanline, xmax);
1352                  else
# Line 1026 | Line 1355 | getmapped()                    /* get color-mapped data */
1355   }
1356  
1357  
1358 < scale_rcolors(xr, sf)                   /* scale color map */
1359 < register XRASTER        *xr;
1360 < double  sf;
1358 > static void
1359 > scale_rcolors(                  /* scale color map */
1360 >        XRASTER *xr,
1361 >        double  sf
1362 > )
1363   {
1364 <        register int    i;
1364 >        int     i;
1365          long    maxv;
1366  
1367          if (xr->pixels == NULL)
# Line 1054 | Line 1385 | double sf;
1385   }
1386  
1387  
1388 < getscan(y)
1389 < int  y;
1388 > static int
1389 > getscan(
1390 >        int  y
1391 > )
1392   {
1393          static int  trunced = -1;               /* truncated file? */
1394   skipit:
1395 <        if (trunced >= 0 && y >= trunced) {
1396 <                bzero(scanline, xmax*sizeof(COLR));
1395 >        if (y == cury-1)                        /* already got it? */
1396 >                return(0);
1397 >        if ((trunced >= 0) & (y >= trunced)) {
1398 >                memset(scanline, 0, xmax*sizeof(COLR));
1399                  return(-1);
1400          }
1401          if (y != cury) {
1402 <                if (scanpos == NULL || scanpos[y] == -1)
1402 >                if (scanpos == NULL || scanpos[y] < 0)
1403                          return(-1);
1404 <                if (fseek(fin, scanpos[y], 0) == -1)
1404 >                if (fseek(fin, scanpos[y], 0) < 0)
1405                          quiterr("fseek error");
1406                  cury = y;
1407 <        } else if (scanpos != NULL && scanpos[y] == -1)
1407 >        } else if (scanpos != NULL && scanpos[y] < 0)
1408                  scanpos[y] = ftell(fin);
1409  
1410 <        if (freadcolrs(scanline, xmax, fin) < 0) {
1410 >        if (fread2colrs(scanline, xmax, fin, NCSAMP, WLPART) < 0) {
1411                  fprintf(stderr, "%s: %s: unfinished picture\n",
1412                                  progname, fname==NULL?"<stdin>":fname);
1413                  trunced = y;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines