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.50 by gregl, Thu Jul 31 21:00:57 1997 UTC vs.
Revision 2.77 by greg, Tue Sep 10 20:24:42 2024 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
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$ SGI";
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 87 | Line 83 | 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 102 | 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 112 | 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 123 | 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  
128 extern long  ftell();
127  
130 extern char  *getenv();
131
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   {
144        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] == '-')
# Line 247 | Line 271 | char  *argv[];
271                  getevent();             /* main loop */
272   userr:
273          fprintf(stderr,
274 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] 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 (formatval(fmt, s))
291 <                wrongformat = strcmp(fmt, COLRFMT);
292 <        else if (isview(s) && sscanview(&ourview, s) > 0)
293 <                gotview++;
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 367 | 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 383 | Line 417 | char **argv;
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;
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 406 | 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 >        register XVisualInfo    *v1,
447 >        register XVisualInfo    *v2
448 > )
449   {
450          int     bad1 = 0, bad2 = 0;
451          register 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 446 | 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 498 | 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 528 | Line 568 | static char  vistype[][12] = {
568   }
569  
570  
571 < getras()                                /* get raster file */
571 > static void
572 > getras(void)                            /* get raster file */
573   {
533        XVisualInfo     vinfo;
534
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 554 | 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;
# Line 571 | 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 588 | 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) {
632 >                if ((!sequential) & (parent < 0) & (sigrecv == 0)) {
633                          kill(getppid(), SIGCONT);
634                          sigrecv--;
635                  }
# Line 626 | Line 667 | getevent()                             /* process the next event */
667   }
668  
669  
670 < traceray(xpos, ypos)                    /* print requested pixel data */
671 < int  xpos, ypos;
670 > static int
671 > traceray(                       /* print requested pixel data */
672 >        int  xpos,
673 >        int  ypos
674 > )
675   {
676 <        extern char  *index();
633 <        FLOAT  hv[2];
676 >        RREAL  hv[2];
677          FVECT  rorg, rdir;
678          COLOR  cval;
679          register char  *cp;
680  
681 <        box.xmin = xpos; box.xsiz = 1;
682 <        box.ymin = ypos; box.ysiz = 1;
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);
# Line 670 | Line 713 | int  xpos, ypos;
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 701 | 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 712 | 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 736 | Line 781 | XKeyPressedEvent  *ekey;
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;
# Line 770 | Line 819 | XKeyPressedEvent  *ekey;
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 794 | 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 810 | 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 835 | 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 < trackrays(ebut)                         /* trace rays as mouse moves */
920 < XButtonPressedEvent  *ebut;
919 > static void
920 > trackrays(                              /* trace rays as mouse moves */
921 >        XButtonPressedEvent  *ebut
922 > )
923   {
924          XEvent  e;
925          unsigned long   lastrept;
# Line 882 | Line 937 | XButtonPressedEvent  *ebut;
937   }
938  
939  
940 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
941 < int  x0, y0, x1, y1;
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);
# Line 892 | Line 952 | int  x0, y0, x1, y1;
952   }
953  
954  
955 < int
956 < colavg(scn, n, cavg)
957 < register COLR   *scn;
958 < register int    n;
959 < COLOR   cavg;
955 > static void
956 > colavg(
957 >        register COLR   *scn,
958 >        register int    n,
959 >        void *cavg
960 > )
961   {
962          COLOR   col;
963  
964          while (n--) {
965 <                colr_color(col, scn++);
966 <                addcolor(cavg, col);
965 >                colr_color(col, *scn++);
966 >                addcolor((COLORV*)cavg, col);
967          }
968   }
969  
970  
971 < int
972 < avgbox(cavg)                            /* average color over current box */
973 < COLOR   cavg;
971 > static int
972 > avgbox(                         /* average color over current bbox */
973 >        COLOR   cavg
974 > )
975   {
976          double  d;
977          register int    rval;
978  
979          setcolor(cavg, 0., 0., 0.);
980 <        rval = dobox(colavg, (char *)cavg);
980 >        rval = dobox(colavg, (void *)cavg);
981          if (rval > 0) {
982                  d = 1./rval;
983                  scalecolor(cavg, d);
# Line 924 | Line 986 | COLOR  cavg;
986   }
987  
988  
989 < int
990 < dobox(f, p)                             /* run function over box */
991 < int     (*f)();                 /* function to call for each subscan */
992 < char    *p;                     /* pointer to private data */
989 > static int
990 > dobox(                          /* run function over bbox */
991 >        //void  (*f)(),                 /* function to call for each subscan */
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;
998  
999 <        left = box.xmin - xoff;
1000 <        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(0);
1007 <        top = box.ymin - yoff;
1008 <        bottom = top + box.ysiz;
1007 >        top = bbox.ymin - yoff;
1008 >        bottom = top + bbox.ysiz;
1009          if (top < 0)
1010                  top = 0;
1011          if (bottom > ymax)
# Line 957 | Line 1021 | char   *p;                     /* pointer to private data */
1021   }
1022  
1023  
1024 < int
1025 < addfix(scn, n)                  /* add fixation points to histogram */
1026 < COLR    *scn;
1027 < int     n;
1024 > static void
1025 > addfix(                 /* add fixation points to histogram */
1026 >        COLR    *scn,
1027 >        int     n,
1028 >        void    *p
1029 > )
1030   {
1031 <        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
1031 >        TMstruct *      tms = (TMstruct *)p;
1032 >        
1033 >        if (tmCvColrs(tms, lscan, TM_NOCHROM, scn, n))
1034                  goto tmerr;
1035 <        if (tmAddHisto(lscan, n, FIXWEIGHT))
1035 >        if (tmAddHisto(tms, lscan, n, FIXWEIGHT))
1036                  goto tmerr;
1037          return;
1038   tmerr:
# Line 972 | Line 1040 | tmerr:
1040   }
1041  
1042  
1043 < make_tonemap()                  /* initialize tone mapping */
1043 > static void
1044 > make_tonemap(void)                      /* initialize tone mapping */
1045   {
1046          int  flags, y;
1047  
# Line 987 | Line 1056 | make_tonemap()                 /* initialize tone mapping */
1056          }
1057          flags = tmflags;                /* histogram adjustment */
1058          if (greyscale) flags |= TM_F_BW;
1059 <        if (tmTop != NULL) {            /* reuse old histogram if one */
1060 <                tmDone(tmTop);
1061 <                tmTop->flags = flags;
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 = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL)
1067 >                        if ((pscan = (uby8 *)malloc(sizeof(uby8)*xmax)) == NULL)
1068                                  goto memerr;
1069 <                } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax))
1069 >                } else if ((pscan=cscan = (uby8 *)malloc(3*sizeof(uby8)*xmax))
1070                                  == NULL)
1071                          goto memerr;
1072                                                  /* initialize tm library */
1073 <                if (tmInit(flags, stdprims, gamcor) == NULL)
1073 >                tmGlobal = tmInit(flags, stdprims, gamcor);
1074 >                if (tmGlobal == NULL)
1075                          goto memerr;
1076 <                if (tmSetSpace(stdprims, WHTEFFICACY/exposure))
1076 >                if (tmSetSpace(tmGlobal, stdprims, WHTEFFICACY/exposure, NULL))
1077                          goto tmerr;
1078                                                  /* compute picture histogram */
1079                  for (y = 0; y < ymax; y++) {
1080                          getscan(y);
1081 <                        if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax))
1081 >                        if (tmCvColrs(tmGlobal, lscan, TM_NOCHROM,
1082 >                                        scanline, xmax))
1083                                  goto tmerr;
1084 <                        if (tmAddHisto(lscan, xmax, 1))
1084 >                        if (tmAddHisto(tmGlobal, lscan, xmax, 1))
1085                                  goto tmerr;
1086                  }
1087          }
1088 <        tmDup();                        /* add fixations to duplicate map */
1089 <        dobox(addfix, NULL);
1088 >        tmCurrent = tmDup(tmGlobal);    /* add fixations to duplicate map */
1089 >        dobox(addfix, (void *)tmCurrent);
1090                                          /* (re)compute tone mapping */
1091 <        if (tmComputeMapping(gamcor, 0., 0.))
1091 >        if (tmComputeMapping(tmCurrent, gamcor, 0., 0.))
1092                  goto tmerr;
1093          return;
1094   memerr:
# Line 1027 | Line 1098 | tmerr:
1098   }
1099  
1100  
1101 < tmap_colrs(scn, len)            /* apply tone mapping to scanline */
1102 < register COLR  *scn;
1103 < int  len;
1101 > static void
1102 > tmap_colrs(             /* apply tone mapping to scanline */
1103 >        register COLR  *scn,
1104 >        int  len
1105 > )
1106   {
1107 <        register BYTE  *ps;
1107 >        register uby8  *ps;
1108  
1109          if (tmflags == TM_F_LINEAR) {
1110                  if (scale)
# Line 1041 | Line 1114 | int  len;
1114          }
1115          if (len > xmax)
1116                  quiterr("code error 1 in tmap_colrs");
1117 <        if (tmCvColrs(lscan, cscan, scn, len))
1117 >        if (tmCvColrs(tmCurrent, lscan, cscan, scn, len))
1118                  goto tmerr;
1119 <        if (tmMapPixels(pscan, lscan, cscan, len))
1119 >        if (tmMapPixels(tmCurrent, pscan, lscan, cscan, len))
1120                  goto tmerr;
1121          ps = pscan;
1122          if (greyscale)
# Line 1066 | Line 1139 | tmerr:
1139   }
1140  
1141  
1142 < getmono()                       /* get monochrome data */
1142 > static void
1143 > getmono(void)                   /* get monochrome data */
1144   {
1145          register unsigned char  *dp;
1146          register int    x, err;
# Line 1094 | Line 1168 | getmono()                      /* get monochrome data */
1168                          cerr[x] = err + errp;
1169                  }
1170          }
1171 <        free((char *)cerr);
1171 >        free((void *)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;
# Line 1137 | 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 1150 | 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;
1233 >        register uint32 *dp;
1234 >        register uint16 *dph;
1235          register 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                  tmap_colrs(scanline, xmax);
1245 <                if (ourras->image->blue_mask & 1)
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;
# Line 1192 | Line 1310 | getgrey()                      /* get greyscale data */
1310                  tmap_colrs(scanline, xmax);
1311                  if (maxcolors < 256)
1312                          for (x = 0; x < xmax; x++)
1313 <                                *dp++ = ((int4)scanline[x][GRN] *
1313 >                                *dp++ = ((int32)scanline[x][GRN] *
1314                                          maxcolors + maxcolors/2) >> 8;
1315                  else
1316                          for (x = 0; x < xmax; x++)
# Line 1200 | 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] = ((int4)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 */
# Line 1213 | Line 1332 | getmapped()                    /* get color-mapped data */
1332                                          /* initialize tone mapping */
1333          make_tonemap();
1334                                          /* make histogram */
1335 <        if (new_histo((int4)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)
# Line 1236 | 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 >        register XRASTER        *xr,
1361 >        double  sf
1362 > )
1363   {
1364          register int    i;
1365          long    maxv;
# Line 1264 | 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));
1396 >                memset(scanline, '\0', xmax*sizeof(COLR));
1397                  return(-1);
1398          }
1399          if (y != cury) {
# Line 1282 | Line 1405 | skipit:
1405          } else if (scanpos != NULL && scanpos[y] == -1)
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