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.65 by schorsch, Fri Jan 2 12:47:01 2004 UTC vs.
Revision 2.78 by greg, Thu Nov 21 17:15:54 2024 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char RCSid[] = "$Id";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   *  x11image.c - driver for X-windows
# Line 20 | Line 20 | static const char RCSid[] = "$Id";
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>
# Line 27 | Line 30 | static const char RCSid[] = "$Id";
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"
# Line 79 | 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 */
# Line 94 | 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 115 | 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  
120 extern long  ftell();
127  
122 extern char  *getenv();
123
128   Display  *thedisplay;
129   Atom  closedownAtom, wmProtocolsAtom;
130  
# Line 128 | Line 132 | int  sigrecv;
132  
133   void  onsig(int i) { sigrecv++; }
134  
135 + typedef void doboxf_t(COLR *scn, int n, void *);
136 +
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 < main(argc, argv)
166 < int  argc;
136 < char  *argv[];
165 > int
166 > main(int  argc, char  *argv[])
167   {
168          int  i;
169          int  pid;
# Line 241 | 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   }
# Line 253 | Line 283 | headline(              /* get relevant info from header */
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 365 | 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 381 | 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)
# Line 404 | 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                                          /* 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 < 15-bit */
469 <        if ((v1->class == TrueColor || v1->class == DirectColor) &&
469 >        if ((v1->class == TrueColor) | (v1->class == DirectColor) &&
470                          v1->depth < 15)
471                  bad1 = 1;
472 <        if ((v2->class == TrueColor || v2->class == DirectColor) &&
472 >        if ((v2->class == TrueColor) | (v2->class == DirectColor) &&
473                          v2->depth < 15)
474                  bad2 = -1;
475          if (bad1 | bad2)
# Line 445 | 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 527 | Line 568 | static char  vistype[][12] = {
568   }
569  
570  
571 < getras()                                /* get raster file */
571 > static void
572 > getras(void)                            /* get raster file */
573   {
532        XVisualInfo     vinfo;
533
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;
# Line 546 | Line 586 | getras()                               /* get raster file */
586                  if (ourdata == NULL)
587                          goto fail;
588                  ourras = make_raster(thedisplay, &ourvis, datsiz*8,
589 <                                ourdata, xmax, ymax, 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 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          RREAL  hv[2];
677          FVECT  rorg, rdir;
# Line 669 | 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;
# Line 813 | 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 838 | Line 886 | XButtonPressedEvent  *ebut;
886   }
887  
888  
889 < getbox(ebut)                            /* get new bbox */
890 < XButtonPressedEvent  *ebut;
889 > static void
890 > getbox(                         /* get new bbox */
891 >        XButtonPressedEvent  *ebut
892 > )
893   {
894          XEvent  e;
895  
# Line 866 | Line 916 | XButtonPressedEvent  *ebut;
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 885 | Line 937 | XButtonPressedEvent  *ebut;
937   }
938  
939  
940 < revbox(x0, y0, x1, y1)                  /* draw bbox 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 895 | Line 952 | int  x0, y0, x1, y1;
952   }
953  
954  
955 < void
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);
966 >                addcolor((COLORV*)cavg, col);
967          }
968   }
969  
970  
971 < int
972 < avgbox(cavg)                            /* average color over current bbox */
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 927 | Line 986 | COLOR  cavg;
986   }
987  
988  
989 < int
990 < dobox(f, p)                             /* run function over bbox */
991 < void    (*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;
# Line 960 | Line 1021 | char   *p;                     /* pointer to private data */
1021   }
1022  
1023  
1024 < void
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 975 | 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 990 | 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))
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 1030 | 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 1044 | 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 1069 | 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 1101 | Line 1172 | getmono()                      /* get monochrome data */
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 1153 | 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 uint32 *dp;
# Line 1220 | Line 1294 | getfull()                      /* get full (24-bit) data */
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 1247 | Line 1322 | getgrey()                      /* get greyscale data */
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 1279 | 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 1307 | 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:
# Line 1325 | 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