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.48 by greg, Tue Apr 22 10:01:43 1997 UTC vs.
Revision 2.80 by greg, Tue Jun 3 21:31:51 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 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 < #undef  NOPROTO
31 < #define NOPROTO 1
31 > #include  "color.h"
32   #include  "tonemap.h"
33 + #include  "clrtab.h"
34   #include  "view.h"
35   #include  "x11raster.h"
36   #include  "random.h"
36 #include  "resolu.h"
37  
38 #ifndef  int4
39 #define  int4           int             /* most int's are 32-bit */
40 #endif
41
38   #define  FONTNAME       "8x13"          /* text font we'll use */
39  
40   #define  CTRL(c)        ((c)-'@')
# Line 47 | 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 78 | Line 76 | int  sequential = 0;                   /* display images in sequence *
76   char  *tout = "od";                     /* output of 't' command */
77   int  tinterv = 0;                       /* interval between mouse reports */
78  
79 < int  tmflags = -1;                      /* tone mapping flags (-1 for none) */
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 < BYTE  *cscan;                           /* encoded chroma scanline */
87 < BYTE  *pscan;                           /* compute pixel 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 100 | 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 110 | 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  
118   char  icondata[ICONSIZ*ICONSIZ/8];      /* icon bitmap data */
119   int  iconwidth = 0, iconheight = 0;
120  
120 char  *progname;
121
121   char  errmsg[128];
122  
123 < BYTE  clrtab[256][3];                   /* global color map */
123 > uby8  clrtab[256][3];                   /* global color map */
124  
126 extern long  ftell();
125  
128 extern char  *getenv();
129
126   Display  *thedisplay;
127   Atom  closedownAtom, wmProtocolsAtom;
128  
129   int  sigrecv;
130  
131 < int  onsig() { sigrecv++; }
131 > void  onsig(int i) { sigrecv++; }
132  
133 + typedef void doboxf_t(COLR *scn, int n, void *);
134  
135 < main(argc, argv)
136 < int  argc;
137 < char  *argv[];
135 > static gethfunc headline;
136 > static void init(int argc, char **argv);
137 > static void quiterr(char *err);
138 > static int viscmp(XVisualInfo *v1, XVisualInfo *v2);
139 > static void getbestvis(void);
140 > static void getras(void);
141 > static void getevent(void);
142 > static int traceray(int xpos, int ypos);
143 > static int docom(XKeyPressedEvent *ekey);
144 > static void moveimage(XButtonPressedEvent *ebut);
145 > static void getbox(XButtonPressedEvent *ebut);
146 > static void trackrays(XButtonPressedEvent *ebut);
147 > static void revbox(int x0, int y0, int x1, int y1);
148 > static doboxf_t colavg;
149 > static doboxf_t addfix;
150 > static int avgbox(COLOR cavg);
151 > static int dobox(doboxf_t *f, void *p);
152 > static void make_tonemap(void);
153 > static void tmap_colrs(COLR *scn, int len);
154 > static void getmono(void);
155 > static void add2icon(int y, COLR *scan);
156 > static void getfull(void);
157 > static void getgrey(void);
158 > static void getmapped(void);
159 > static void scale_rcolors(XRASTER *xr, double sf);
160 > static int getscan(int y);
161 >
162 >
163 > int
164 > main(int  argc, char  *argv[])
165   {
142        int  headline();
166          int  i;
167          int  pid;
168          
169 <        progname = argv[0];
169 >        fixargv0(argv[0]);              /* sets global progname */
170 >        fin = stdin;
171  
172          for (i = 1; i < argc; i++)
173                  if (argv[i][0] == '-')
# Line 245 | Line 269 | char  *argv[];
269                  getevent();             /* main loop */
270   userr:
271          fprintf(stderr,
272 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n",
272 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] hdr ..\n",
273                          progname);
274          exit(1);
275   }
276  
277  
278 < headline(s)             /* get relevant info from header */
279 < char  *s;
278 > static int
279 > headline(               /* get relevant info from header */
280 >        char    *s,
281 >        void    *p
282 > )
283   {
284 <        char  fmt[32];
284 >        char  fmt[MAXFMTLEN];
285  
286          if (isexpos(s))
287                  exposure *= exposval(s);
288          else if (formatval(fmt, s))
289 <                wrongformat = strcmp(fmt, COLRFMT);
290 <        else if (isview(s) && sscanview(&ourview, s) > 0)
291 <                gotview++;
289 >                wrongformat = strcmp(fmt, COLRFMT) && strcmp(fmt, SPECFMT);
290 >        else if (isview(s))
291 >                gotview += sscanview(&ourview, s);
292 >        else if (isncomp(s))
293 >                NCSAMP = ncompval(s);
294 >        else if (iswlsplit(s))
295 >                wlsplitval(WLPART, s);
296 >        return(0);
297   }
298  
299  
300 < init(argc, argv)                        /* get data and open window */
301 < int argc;
302 < char **argv;
300 > static void
301 > init(                   /* get data and open window */
302 >        int argc,
303 >        char **argv
304 > )
305   {
306          XSetWindowAttributes    ourwinattr;
307          XClassHint      xclshints;
# Line 276 | Line 310 | char **argv;
310          XTextProperty   windowName, iconName;
311          XGCValues       xgcv;
312          char    *name;
313 <        register int    i;
313 >        int     i;
314          
315          if (fname != NULL) {
316                  scanpos = (long *)malloc(ymax*sizeof(long));
# Line 365 | Line 399 | char **argv;
399          windowName.encoding = iconName.encoding = XA_STRING;
400          windowName.format = iconName.format = 8;
401          windowName.value = (u_char *)name;
402 <        windowName.nitems = strlen(windowName.value);
402 >        windowName.nitems = strlen((char *)windowName.value);
403          iconName.value = (u_char *)name;
404 <        iconName.nitems = strlen(windowName.value);
404 >        iconName.nitems = strlen((char *)windowName.value);
405  
406          xclshints.res_name = NULL;
407          xclshints.res_class = "Ximage";
# Line 381 | Line 415 | char **argv;
415   } /* end of init */
416  
417  
418 < quiterr(err)            /* print message and exit */
419 < char  *err;
418 > static void
419 > quiterr(                /* print message and exit */
420 >        char  *err
421 > )
422   {
423 <        register int  es;
423 >        int  es;
424          int  cs;
425  
426 <        if (es = err != NULL)
426 >        if ( (es = (err != NULL)) )
427                  fprintf(stderr, "%s: %s: %s\n", progname,
428                                  fname==NULL?"<stdin>":fname, err);
429          if (thedisplay != NULL)
430                  XCloseDisplay(thedisplay);
431 <        if (parent < 0 & sigrecv == 0)
431 >        if ((parent < 0) & (sigrecv == 0))
432                  kill(getppid(), SIGCONT);
433          while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
434                  if (es == 0)
# Line 404 | Line 440 | char  *err;
440  
441  
442   static int
443 < viscmp(v1,v2)           /* compare visual to see which is better, descending */
444 < register XVisualInfo    *v1, *v2;
443 > viscmp(         /* compare visual to see which is better, descending */
444 >        XVisualInfo     *v1,
445 >        XVisualInfo     *v2
446 > )
447   {
448          int     bad1 = 0, bad2 = 0;
449 <        register int  *rp;
449 >        int  *rp;
450  
451          if (v1->class == v2->class) {
452 <                if (v1->class == TrueColor || v1->class == DirectColor) {
453 <                                        /* prefer 24-bit to 32-bit */
454 <                        if (v1->depth == 24 && v2->depth == 32)
452 >                if ((v1->class == TrueColor) | (v1->class == DirectColor)) {
453 >                                        /* prefer 24-bit */
454 >                        if ((v1->depth == 24) & (v2->depth > 24))
455                                  return(-1);
456 <                        if (v1->depth == 32 && v2->depth == 24)
456 >                        if ((v1->depth > 24) & (v2->depth == 24))
457                                  return(1);
458 <                        return(0);
458 >                                        /* go for maximum depth otherwise */
459 >                        return(v2->depth - v1->depth);
460                  }
461                                          /* don't be too greedy */
462 <                if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
462 >                if ((maxcolors <= 1<<v1->depth) & (maxcolors <= 1<<v2->depth))
463                          return(v1->depth - v2->depth);
464                  return(v2->depth - v1->depth);
465          }
466 <                                        /* prefer Pseudo when < 24-bit */
467 <        if ((v1->class == TrueColor || v1->class == DirectColor) &&
468 <                        v1->depth < 24)
466 >                                        /* prefer Pseudo when < 15-bit */
467 >        if ((v1->class == TrueColor) | (v1->class == DirectColor) &&
468 >                        v1->depth < 15)
469                  bad1 = 1;
470 <        if ((v2->class == TrueColor || v2->class == DirectColor) &&
471 <                        v2->depth < 24)
470 >        if ((v2->class == TrueColor) | (v2->class == DirectColor) &&
471 >                        v2->depth < 15)
472                  bad2 = -1;
473          if (bad1 | bad2)
474                  return(bad1+bad2);
# Line 444 | Line 483 | register XVisualInfo   *v1, *v2;
483   }
484  
485  
486 < getbestvis()                    /* get the best visual for this screen */
486 > static void
487 > getbestvis(void)                        /* get the best visual for this screen */
488   {
489   #ifdef DEBUG
490   static char  vistype[][12] = {
# Line 463 | Line 503 | static char  vistype[][12] = {
503          };
504          XVisualInfo     *xvi;
505          int     vismatched;
506 <        register int    i, j;
506 >        int     i, j;
507  
508          if (greyscale) {
509                  ourrank = rankings[2];
# Line 496 | Line 536 | static char  vistype[][12] = {
536          if (viscmp(&xvi[i],&ourvis) > 0)
537                  quiterr("inadequate visuals on this screen");
538                                          /* OK, we'll use it */
539 <        copystruct(&ourvis, &xvi[i]);
539 >        ourvis = xvi[i];
540   #ifdef DEBUG
541          fprintf(stderr, "Selected visual type %s, depth %d\n",
542                          vistype[ourvis.class], ourvis.depth);
# Line 526 | Line 566 | static char  vistype[][12] = {
566   }
567  
568  
569 < getras()                                /* get raster file */
569 > static void
570 > getras(void)                            /* get raster file */
571   {
531        XVisualInfo     vinfo;
532
572          if (maxcolors <= 2) {           /* monochrome */
573                  ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
574                  if (ourdata == NULL)
575                          goto fail;
576 <                ourras = make_raster(thedisplay, &ourvis, 1, ourdata,
576 >                ourras = make_raster(thedisplay, &ourvis, 1, (char *)ourdata,
577                                  xmax, ymax, 8);
578                  if (ourras == NULL)
579                          goto fail;
580                  getmono();
581 <        } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
582 <                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
581 >        } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) {
582 >                int  datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16);
583 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
584                  if (ourdata == NULL)
585                          goto fail;
586 <                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
587 <                                ourdata, xmax, ymax, 32);
586 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
587 >                                (char *)ourdata, xmax, ymax, datsiz*8);
588                  if (ourras == NULL)
589                          goto fail;
590                  getfull();
# Line 552 | Line 592 | getras()                               /* get raster file */
592                  ourdata = (unsigned char *)malloc(xmax*ymax);
593                  if (ourdata == NULL)
594                          goto fail;
595 <                ourras = make_raster(thedisplay, &ourvis, 8, ourdata,
595 >                ourras = make_raster(thedisplay, &ourvis, 8, (char *)ourdata,
596                                  xmax, ymax, 8);
597                  if (ourras == NULL)
598                          goto fail;
# Line 569 | Line 609 | fail:
609   }
610  
611  
612 < getevent()                              /* process the next event */
612 > static void
613 > getevent(void)                          /* process the next event */
614   {
615          XEvent xev;
616  
# Line 586 | Line 627 | getevent()                             /* process the next event */
627                  map_rcolors(ourras, wind);
628                  if (fast)
629                          make_rpixmap(ourras, wind);
630 <                if (!sequential & parent < 0 & sigrecv == 0) {
630 >                if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
631                          kill(getppid(), SIGCONT);
632                          sigrecv--;
633                  }
# Line 624 | Line 665 | getevent()                             /* process the next event */
665   }
666  
667  
668 < traceray(xpos, ypos)                    /* print requested pixel data */
669 < int  xpos, ypos;
668 > static int
669 > traceray(                       /* print requested pixel data */
670 >        int  xpos,
671 >        int  ypos
672 > )
673   {
674 <        extern char  *index();
631 <        FLOAT  hv[2];
674 >        RREAL  hv[2];
675          FVECT  rorg, rdir;
676          COLOR  cval;
677 <        register char  *cp;
677 >        char  *cp;
678  
679 <        box.xmin = xpos; box.xsiz = 1;
680 <        box.ymin = ypos; box.ysiz = 1;
679 >        bbox.xmin = xpos; bbox.xsiz = 1;
680 >        bbox.ymin = ypos; bbox.ysiz = 1;
681          avgbox(cval);
682          scalecolor(cval, 1./exposure);
683          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
# Line 668 | Line 711 | int  xpos, ypos;
711   }
712  
713  
714 < docom(ekey)                             /* execute command */
715 < XKeyPressedEvent  *ekey;
714 > static int
715 > docom(                          /* execute command */
716 >        XKeyPressedEvent  *ekey
717 > )
718   {
719          char  buf[80];
720          COLOR  cval;
721          XColor  cvx;
722          int  com, n;
723          double  comp;
724 <        FLOAT  hv[2];
724 >        RREAL  hv[2];
725  
726          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
727          if (n == 0)
# Line 691 | Line 736 | XKeyPressedEvent  *ekey;
736          case '\r':
737          case 'l':
738          case 'c':                               /* value */
739 <                if (avgbox(cval) == -1)
739 >                if (!avgbox(cval))
740                          return(-1);
741                  switch (com) {
742                  case '\n':
# Line 699 | Line 744 | XKeyPressedEvent  *ekey;
744                          sprintf(buf, "%.3f", intens(cval)/exposure);
745                          break;
746                  case 'l':                               /* luminance */
747 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
747 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
748                          break;
749                  case 'c':                               /* color */
750                          comp = pow(2.0, (double)scale);
# Line 710 | Line 755 | XKeyPressedEvent  *ekey;
755                          break;
756                  }
757                  XDrawImageString(thedisplay, wind, ourgc,
758 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
758 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
759                  return(0);
760          case 'i':                               /* identify (contour) */
761                  if (ourras->pixels == NULL)
# Line 734 | Line 779 | XKeyPressedEvent  *ekey;
779          case 't':                               /* trace */
780                  return(traceray(ekey->x, ekey->y));
781          case 'a':                               /* auto exposure */
782 <                if (tmflags == TM_F_CAMERA)
783 <                        return(0);
782 >                if (fname == NULL)
783 >                        return(-1);
784                  tmflags = TM_F_CAMERA;
785                  strcpy(buf, "auto exposure...");
786                  goto remap;
787          case 'h':                               /* human response */
788 <                if (tmflags == TM_F_HUMAN)
789 <                        return(0);
788 >                if (fname == NULL)
789 >                        return(-1);
790                  tmflags = TM_F_HUMAN;
791                  strcpy(buf, "human exposure...");
792                  goto remap;
793          case '=':                               /* adjust exposure */
794          case '@':                               /* adaptation level */
795 <                if (avgbox(cval) == -1)
795 >                if (!avgbox(cval))
796                          return(-1);
797                  comp = bright(cval);
798                  if (comp < 1e-20) {
# Line 761 | Line 806 | XKeyPressedEvent  *ekey;
806                          comp = .5/comp;
807                  comp = log(comp)/.69315 - scale;
808                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
809 <                if (tmflags != -1)
810 <                        tmflags = -1;           /* turn off tone mapping */
809 >                if (tmflags != TM_F_LINEAR)
810 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
811                  else {
812                          if (n == 0)             /* else check if any change */
813                                  return(0);
# Line 772 | Line 817 | XKeyPressedEvent  *ekey;
817                  sprintf(buf, "%+d", scale);
818          remap:
819                  XDrawImageString(thedisplay, wind, ourgc,
820 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
820 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
821                  XFlush(thedisplay);
822 <                free(ourdata);
822 >                /* free(ourdata);       This is done in XDestroyImage()! */
823                  free_raster(ourras);
824                  getras();
825          /* fall through */
# Line 796 | Line 841 | XKeyPressedEvent  *ekey;
841                  free_rpixmap(ourras);
842                  return(0);
843          case '0':                               /* recenter origin */
844 <                if (xoff == 0 & yoff == 0)
844 >                if ((xoff == 0) & (yoff == 0))
845                          return(0);
846                  xoff = yoff = 0;
847                  XClearWindow(thedisplay, wind);
848                  redraw(0, 0, width, height);
849                  return(0);
850          case ' ':                               /* clear */
851 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
851 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
852                  return(0);
853          default:
854                  XBell(thedisplay, 0);
# Line 812 | Line 857 | XKeyPressedEvent  *ekey;
857   }
858  
859  
860 < moveimage(ebut)                         /* shift the image */
861 < XButtonPressedEvent  *ebut;
860 > static void
861 > moveimage(                              /* shift the image */
862 >        XButtonPressedEvent  *ebut
863 > )
864   {
865          XEvent  e;
866          int     mxo, myo;
# Line 837 | Line 884 | XButtonPressedEvent  *ebut;
884   }
885  
886  
887 < getbox(ebut)                            /* get new box */
888 < XButtonPressedEvent  *ebut;
887 > static void
888 > getbox(                         /* get new bbox */
889 >        XButtonPressedEvent  *ebut
890 > )
891   {
892          XEvent  e;
893  
894          XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
895          while (e.type == MotionNotify) {
896 <                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
896 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
897                  XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
898 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
898 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
899          }
900 <        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
901 <        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
902 <        if (box.xmin > ebut->x) {
903 <                box.xsiz = box.xmin - ebut->x + 1;
904 <                box.xmin = ebut->x;
900 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
901 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
902 >        if (bbox.xmin > ebut->x) {
903 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
904 >                bbox.xmin = ebut->x;
905          } else {
906 <                box.xsiz = ebut->x - box.xmin + 1;
906 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
907          }
908 <        if (box.ymin > ebut->y) {
909 <                box.ysiz = box.ymin - ebut->y + 1;
910 <                box.ymin = ebut->y;
908 >        if (bbox.ymin > ebut->y) {
909 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
910 >                bbox.ymin = ebut->y;
911          } else {
912 <                box.ysiz = ebut->y - box.ymin + 1;
912 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
913          }
914   }
915  
916  
917 < trackrays(ebut)                         /* trace rays as mouse moves */
918 < XButtonPressedEvent  *ebut;
917 > static void
918 > trackrays(                              /* trace rays as mouse moves */
919 >        XButtonPressedEvent  *ebut
920 > )
921   {
922          XEvent  e;
923          unsigned long   lastrept;
# Line 884 | Line 935 | XButtonPressedEvent  *ebut;
935   }
936  
937  
938 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
939 < int  x0, y0, x1, y1;
938 > static void
939 > revbox(                 /* draw bbox with reversed lines */
940 >        int  x0,
941 >        int  y0,
942 >        int  x1,
943 >        int  y1
944 > )
945   {
946          revline(x0, y0, x1, y0);
947          revline(x0, y1, x1, y1);
# Line 894 | Line 950 | int  x0, y0, x1, y1;
950   }
951  
952  
953 < avgbox(clr)                             /* average color over current box */
954 < COLOR  clr;
953 > static void
954 > colavg(
955 >        COLR    *scn,
956 >        int     n,
957 >        void *cavg
958 > )
959   {
960 <        static COLOR  lc;
961 <        static int  ll, lr, lt, lb;
960 >        COLOR   col;
961 >
962 >        while (n--) {
963 >                colr_color(col, *scn);
964 >                addcolor((COLORV*)cavg, col);
965 >                scn++;
966 >        }
967 > }
968 >
969 >
970 > static int
971 > avgbox(                         /* average color over current bbox */
972 >        COLOR   cavg
973 > )
974 > {
975 >        double  d;
976 >        int     rval;
977 >
978 >        setcolor(cavg, 0., 0., 0.);
979 >        rval = dobox(colavg, cavg);
980 >        if (rval > 0) {
981 >                d = 1./rval;
982 >                scalecolor(cavg, d);
983 >        }
984 >        return(rval);
985 > }
986 >
987 >
988 > static int
989 > dobox(                          /* run function over bbox */
990 >        doboxf_t *f,                    /* function to call for each subscan */
991 >        void    *p                      /* pointer to private data */
992 > )
993 > {
994          int  left, right, top, bottom;
995          int  y;
904        double  d;
905        COLOR  ctmp;
906        register int  x;
996  
997 <        setcolor(clr, 0.0, 0.0, 0.0);
998 <        left = box.xmin - xoff;
910 <        right = left + box.xsiz;
997 >        left = bbox.xmin - xoff;
998 >        right = left + bbox.xsiz;
999          if (left < 0)
1000                  left = 0;
1001          if (right > xmax)
1002                  right = xmax;
1003          if (left >= right)
1004 <                return(-1);
1005 <        top = box.ymin - yoff;
1006 <        bottom = top + box.ysiz;
1004 >                return(0);
1005 >        top = bbox.ymin - yoff;
1006 >        bottom = top + bbox.ysiz;
1007          if (top < 0)
1008                  top = 0;
1009          if (bottom > ymax)
1010                  bottom = ymax;
1011          if (top >= bottom)
924                return(-1);
925        if (left == ll && right == lr && top == lt && bottom == lb) {
926                copycolor(clr, lc);
1012                  return(0);
928        }
1013          for (y = top; y < bottom; y++) {
1014 <                if (getscan(y) == -1)
1014 >                if (getscan(y) < 0)
1015                          return(-1);
1016 <                for (x = left; x < right; x++) {
933 <                        colr_color(ctmp, scanline[x]);
934 <                        addcolor(clr, ctmp);
935 <                }
1016 >                (*f)(scanline+left, right-left, p);
1017          }
1018 <        d = 1.0/((right-left)*(bottom-top));
938 <        scalecolor(clr, d);
939 <        ll = left; lr = right; lt = top; lb = bottom;
940 <        copycolor(lc, clr);
941 <        return(0);
1018 >        return((right-left)*(bottom-top));
1019   }
1020  
1021  
1022 < make_tonemap()                  /* initialize tone mapping */
1022 > static void
1023 > addfix(                 /* add fixation points to histogram */
1024 >        COLR    *scn,
1025 >        int     n,
1026 >        void    *p
1027 > )
1028   {
1029 +        TMstruct *      tms = (TMstruct *)p;
1030 +        
1031 +        if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n))
1032 +                goto tmerr;
1033 +        if (tmAddHisto(tms, lscan, n, FIXWEIGHT))
1034 +                goto tmerr;
1035 +        return;
1036 + tmerr:
1037 +        quiterr("tone mapping error");
1038 + }
1039 +
1040 +
1041 + static void
1042 + make_tonemap(void)                      /* initialize tone mapping */
1043 + {
1044          int  flags, y;
1045  
1046 <        if (tmflags != -1 && fname == NULL) {
1046 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
1047                  fprintf(stderr, "%s: cannot adjust tone of standard input\n",
1048                                  progname);
1049 <                tmflags = -1;
1049 >                tmflags = TM_F_LINEAR;
1050          }
1051 <        if (tmflags == -1) {
1051 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
1052                  setcolrcor(pow, 1.0/gamcor);
1053                  return;
1054          }
1055 <        if (tmTop != NULL)              /* already initialized? */
959 <                return;
960 <        flags = tmflags;
1055 >        flags = tmflags;                /* histogram adjustment */
1056          if (greyscale) flags |= TM_F_BW;
1057 <                                        /* initialize tm library */
1058 <        if (tmInit(flags, stdprims, gamcor) == NULL)
1059 <                goto memerr;
1060 <        if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1061 <                goto tmerr;
967 <                                        /* allocate encoding buffers */
968 <        if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
969 <                goto memerr;
970 <        if (greyscale) {
971 <                cscan = TM_NOCHROM;
972 <                if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1057 >        if (tmGlobal != NULL) {         /* reuse old histogram if one */
1058 >                tmDone(tmCurrent); tmCurrent = NULL;
1059 >                tmGlobal->flags = flags;
1060 >        } else {                        /* else initialize */
1061 >                if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
1062                          goto memerr;
1063 <        } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL)
1064 <                goto memerr;
1065 <                                        /* compute picture histogram */
1066 <        for (y = 0; y < ymax; y++) {
1067 <                getscan(y);
1068 <                if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1063 >                if (greyscale) {
1064 >                        cscan = TM_NOCHROM;
1065 >                        if ((pscan = (uby8 *)malloc(sizeof(uby8)*xmax)) == NULL)
1066 >                                goto memerr;
1067 >                } else if ((pscan=cscan = (uby8 *)malloc(3*sizeof(uby8)*xmax))
1068 >                                == NULL)
1069 >                        goto memerr;
1070 >                                                /* initialize tm library */
1071 >                tmGlobal = tmInit(flags, stdprims, gamcor);
1072 >                if (tmGlobal == NULL)
1073 >                        goto memerr;
1074 >                if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure))
1075                          goto tmerr;
1076 <                if (tmAddHisto(lscan, xmax, 1))
1077 <                        goto tmerr;
1076 >                                                /* compute picture histogram */
1077 >                for (y = 0; y < ymax; y++) {
1078 >                        getscan(y);
1079 >                        if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM,
1080 >                                        scanline, xmax))
1081 >                                goto tmerr;
1082 >                        if (tmAddHisto(tmGlobal, lscan, xmax, 1))
1083 >                                goto tmerr;
1084 >                }
1085          }
1086 <                                        /* compute tone mapping */
1087 <        if (tmComputeMapping(gamcor, 0., 0.))
1086 >        tmCurrent = tmDup(tmGlobal);    /* add fixations to duplicate map */
1087 >        dobox(addfix, tmCurrent);
1088 >                                        /* (re)compute tone mapping */
1089 >        if (tmComputeMapping(tmCurrent, gamcor, 0., 0.))
1090                  goto tmerr;
1091          return;
1092   memerr:
# Line 992 | Line 1096 | tmerr:
1096   }
1097  
1098  
1099 < tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1100 < register COLR  *scn;
1101 < int  len;
1099 > static void
1100 > tmap_colrs(             /* apply tone mapping to scanline */
1101 >        COLR  *scn,
1102 >        int  len
1103 > )
1104   {
1105 <        register BYTE  *ps;
1105 >        uby8  *ps;
1106  
1107 <        if (tmflags == -1) {
1107 >        if (tmflags == TM_F_LINEAR) {
1108                  if (scale)
1109                          shiftcolrs(scn, len, scale);
1110                  colrs_gambs(scn, len);
# Line 1006 | Line 1112 | int  len;
1112          }
1113          if (len > xmax)
1114                  quiterr("code error 1 in tmap_colrs");
1115 <        if (tmCvColrs(lscan, cscan, scn, len))
1115 >        if (tmCvColrs(tmCurrent, lscan, cscan, scn, len))
1116                  goto tmerr;
1117 <        if (tmMapPixels(pscan, lscan, cscan, len))
1117 >        if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len))
1118                  goto tmerr;
1119          ps = pscan;
1120          if (greyscale)
# Line 1031 | Line 1137 | tmerr:
1137   }
1138  
1139  
1140 < getmono()                       /* get monochrome data */
1140 > static void
1141 > getmono(void)                   /* get monochrome data */
1142   {
1143 <        register unsigned char  *dp;
1144 <        register int    x, err;
1143 >        unsigned char   *dp;
1144 >        int     x, err;
1145          int     y, errp;
1146          short   *cerr;
1147  
# Line 1059 | Line 1166 | getmono()                      /* get monochrome data */
1166                          cerr[x] = err + errp;
1167                  }
1168          }
1169 <        free((char *)cerr);
1169 >        free(cerr);
1170   }
1171  
1172  
1173 < add2icon(y, scan)               /* add a scanline to our icon data */
1174 < int  y;
1175 < COLR  *scan;
1173 > static void
1174 > add2icon(               /* add a scanline to our icon data */
1175 >        int  y,
1176 >        COLR  *scan
1177 > )
1178   {
1179          static short  cerr[ICONSIZ];
1180          static int  ynext;
1181          static char  *dp;
1182          COLR  clr;
1183 <        register int  err;
1184 <        register int    x, ti;
1183 >        int  err;
1184 >        int     x, ti;
1185          int  errp;
1186  
1187          if (iconheight == 0) {          /* initialize */
# Line 1102 | Line 1211 | COLR  *scan;
1211                  errp = err;
1212                  ti = x*xmax/iconwidth;
1213                  copycolr(clr, scan[ti]);
1214 <                normcolrs(clr, 1, scale);
1214 >                normcolrs(&clr, 1, scale);
1215                  err += normbright(clr) + cerr[x];
1216                  if (err > 127)
1217                          err -= 255;
# Line 1115 | Line 1224 | COLR  *scan;
1224   }
1225  
1226  
1227 < getfull()                       /* get full (24-bit) data */
1227 > static void
1228 > getfull(void)                   /* get full (24-bit) data */
1229   {
1230          int     y;
1231 <        register unsigned int4  *dp;
1232 <        register int    x;
1231 >        uint32  *dp;
1232 >        uint16  *dph;
1233 >        int     x;
1234                                          /* initialize tone mapping */
1235          make_tonemap();
1236                                          /* read and convert file */
1237 <        dp = (unsigned int4 *)ourdata;
1237 >        dp = (uint32 *)ourdata;
1238 >        dph = (uint16 *)ourdata;
1239          for (y = 0; y < ymax; y++) {
1240                  getscan(y);
1241                  add2icon(y, scanline);
1242                  tmap_colrs(scanline, xmax);
1243 <                if (ourras->image->blue_mask & 1)
1243 >                switch (ourras->image->blue_mask) {
1244 >                case 0xff:              /* 24-bit RGB */
1245                          for (x = 0; x < xmax; x++)
1246 <                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1247 <                                        (unsigned int4)scanline[x][GRN] << 8 |
1248 <                                        (unsigned int4)scanline[x][BLU] ;
1249 <                else
1246 >                                *dp++ = (uint32)scanline[x][RED] << 16 |
1247 >                                        (uint32)scanline[x][GRN] << 8 |
1248 >                                        (uint32)scanline[x][BLU] ;
1249 >                        break;
1250 >                case 0xff0000:          /* 24-bit BGR */
1251                          for (x = 0; x < xmax; x++)
1252 <                                *dp++ = (unsigned int4)scanline[x][RED] |
1253 <                                        (unsigned int4)scanline[x][GRN] << 8 |
1254 <                                        (unsigned int4)scanline[x][BLU] << 16 ;
1252 >                                *dp++ = (uint32)scanline[x][RED] |
1253 >                                        (uint32)scanline[x][GRN] << 8 |
1254 >                                        (uint32)scanline[x][BLU] << 16 ;
1255 >                        break;
1256 > #if 0
1257 >                case 0x1f:              /* 15-bit RGB */
1258 >                        for (x = 0; x < xmax; x++)
1259 >                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1260 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1261 >                                        (unsigned)scanline[x][BLU] >> 3 ;
1262 >                        break;
1263 >                case 0x7c00:            /* 15-bit BGR */
1264 >                        for (x = 0; x < xmax; x++)
1265 >                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1266 >                                        (scanline[x][GRN] << 2 & 0x3e0) |
1267 >                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1268 >                        break;
1269 > #endif
1270 >                default:                /* unknown */
1271 >                        if (ourvis.depth > 16)
1272 >                                for (x = 0; x < xmax; x++) {
1273 >                                        *dp = ourras->image->red_mask &
1274 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1275 >                                        *dp |= ourras->image->green_mask &
1276 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1277 >                                        *dp++ |= ourras->image->blue_mask &
1278 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1279 >                                }
1280 >                        else
1281 >                                for (x = 0; x < xmax; x++) {
1282 >                                        *dph = ourras->image->red_mask &
1283 >                                                ourras->image->red_mask*scanline[x][RED]/255;
1284 >                                        *dph |= ourras->image->green_mask &
1285 >                                                ourras->image->green_mask*scanline[x][GRN]/255;
1286 >                                        *dph++ |= ourras->image->blue_mask &
1287 >                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1288 >                                }
1289 >                        break;
1290 >                }
1291          }
1292   }
1293  
1294  
1295 < getgrey()                       /* get greyscale data */
1295 > static void
1296 > getgrey(void)                   /* get greyscale data */
1297   {
1298          int     y;
1299 <        register unsigned char  *dp;
1300 <        register int    x;
1299 >        unsigned char   *dp;
1300 >        int     x;
1301                                          /* initialize tone mapping */
1302          make_tonemap();
1303                                          /* read and convert file */
# Line 1157 | Line 1308 | getgrey()                      /* get greyscale data */
1308                  tmap_colrs(scanline, xmax);
1309                  if (maxcolors < 256)
1310                          for (x = 0; x < xmax; x++)
1311 <                                *dp++ = ((int4)scanline[x][GRN] *
1311 >                                *dp++ = ((int32)scanline[x][GRN] *
1312                                          maxcolors + maxcolors/2) >> 8;
1313                  else
1314                          for (x = 0; x < xmax; x++)
# Line 1165 | Line 1316 | getgrey()                      /* get greyscale data */
1316          }
1317          for (x = 0; x < maxcolors; x++)
1318                  clrtab[x][RED] = clrtab[x][GRN] =
1319 <                        clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors;
1319 >                        clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors;
1320   }
1321  
1322  
1323 < getmapped()                     /* get color-mapped data */
1323 > static void
1324 > getmapped(void)                 /* get color-mapped data */
1325   {
1326          int     y;
1327                                          /* make sure we can do it first */
# Line 1178 | Line 1330 | getmapped()                    /* get color-mapped data */
1330                                          /* initialize tone mapping */
1331          make_tonemap();
1332                                          /* make histogram */
1333 <        if (new_histo((int4)xmax*ymax) == -1)
1333 >        if (new_histo((int32)xmax*ymax) == -1)
1334                  quiterr("cannot initialize histogram");
1335          for (y = 0; y < ymax; y++) {
1336                  if (getscan(y) < 0)
# Line 1201 | Line 1353 | getmapped()                    /* get color-mapped data */
1353   }
1354  
1355  
1356 < scale_rcolors(xr, sf)                   /* scale color map */
1357 < register XRASTER        *xr;
1358 < double  sf;
1356 > static void
1357 > scale_rcolors(                  /* scale color map */
1358 >        XRASTER *xr,
1359 >        double  sf
1360 > )
1361   {
1362 <        register int    i;
1362 >        int     i;
1363          long    maxv;
1364  
1365          if (xr->pixels == NULL)
# Line 1229 | Line 1383 | double sf;
1383   }
1384  
1385  
1386 < getscan(y)
1387 < int  y;
1386 > static int
1387 > getscan(
1388 >        int  y
1389 > )
1390   {
1391          static int  trunced = -1;               /* truncated file? */
1392   skipit:
1393 <        if (trunced >= 0 && y >= trunced) {
1394 <                bzero(scanline, xmax*sizeof(COLR));
1393 >        if (y == cury-1)                        /* already got it? */
1394 >                return(0);
1395 >        if ((trunced >= 0) & (y >= trunced)) {
1396 >                memset(scanline, 0, xmax*sizeof(COLR));
1397                  return(-1);
1398          }
1399          if (y != cury) {
1400 <                if (scanpos == NULL || scanpos[y] == -1)
1400 >                if (scanpos == NULL || scanpos[y] < 0)
1401                          return(-1);
1402 <                if (fseek(fin, scanpos[y], 0) == -1)
1402 >                if (fseek(fin, scanpos[y], 0) < 0)
1403                          quiterr("fseek error");
1404                  cury = y;
1405 <        } else if (scanpos != NULL && scanpos[y] == -1)
1405 >        } else if (scanpos != NULL && scanpos[y] < 0)
1406                  scanpos[y] = ftell(fin);
1407  
1408 <        if (freadcolrs(scanline, xmax, fin) < 0) {
1408 >        if (fread2colrs(scanline, xmax, fin, NCSAMP, WLPART) < 0) {
1409                  fprintf(stderr, "%s: %s: unfinished picture\n",
1410                                  progname, fname==NULL?"<stdin>":fname);
1411                  trunced = y;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines