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.49 by greg, Thu Apr 24 17:29:53 1997 UTC vs.
Revision 2.56 by greg, Sat Feb 22 02:07:28 2003 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 27 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   #include  <X11/Xutil.h>
25   #include  <X11/Xatom.h>
26  
27 < #undef  NOPROTO
31 < #define NOPROTO 1
27 > #include  "color.h"
28   #include  "tonemap.h"
29   #include  "view.h"
30   #include  "x11raster.h"
31   #include  "random.h"
36 #include  "resolu.h"
32  
38 #ifndef  int4
39 #define  int4           int             /* most int's are 32-bit */
40 #endif
41
33   #define  FONTNAME       "8x13"          /* text font we'll use */
34  
35   #define  CTRL(c)        ((c)-'@')
# Line 47 | Line 38 | static char SCCSid[] = "$SunId$ LBL";
38  
39   #define  ICONSIZ        (8*10)          /* maximum icon dimension (even 8) */
40  
41 + #define  FIXWEIGHT      20              /* weight to add for fixation points */
42 +
43   #define  ourscreen      DefaultScreen(thedisplay)
44   #define  ourroot        RootWindow(thedisplay,ourscreen)
45  
# Line 78 | Line 71 | int  sequential = 0;                   /* display images in sequence *
71   char  *tout = "od";                     /* output of 't' command */
72   int  tinterv = 0;                       /* interval between mouse reports */
73  
74 < int  tmflags = -1;                      /* tone mapping flags (-1 for none) */
74 > int  tmflags = TM_F_LINEAR;             /* tone mapping flags */
75  
76   VIEW  ourview = STDVIEW;                /* image view parameters */
77   int  gotview = 0;                       /* got parameters from file */
# Line 92 | Line 85 | RESOLU  inpres;                                /* input resolution and ordering */
85   int  xmax, ymax;                        /* picture dimensions */
86   int  width, height;                     /* window size */
87   char  *fname = NULL;                    /* input file name */
88 < FILE  *fin = stdin;                     /* input file */
88 > FILE  *fin = NULL;                      /* input file */
89   long  *scanpos = NULL;                  /* scan line positions in file */
90   int  cury = 0;                          /* current scan location */
91  
# Line 110 | Line 103 | unsigned char  *ourdata;               /* our image data */
103  
104   struct {
105          int  xmin, ymin, xsiz, ysiz;
106 < }  box = {0, 0, 0, 0};                  /* current box */
106 > }  bbox = {0, 0, 0, 0};                 /* current bbox */
107  
108   char  *geometry = NULL;                 /* geometry specification */
109  
# Line 144 | Line 137 | char  *argv[];
137          int  pid;
138          
139          progname = argv[0];
140 +        fin = stdin;
141  
142          for (i = 1; i < argc; i++)
143                  if (argv[i][0] == '-')
# Line 251 | Line 245 | userr:
245   }
246  
247  
248 + int
249   headline(s)             /* get relevant info from header */
250   char  *s;
251   {
# Line 262 | Line 257 | char  *s;
257                  wrongformat = strcmp(fmt, COLRFMT);
258          else if (isview(s) && sscanview(&ourview, s) > 0)
259                  gotview++;
260 +        return(0);
261   }
262  
263  
# Line 417 | Line 413 | register XVisualInfo   *v1, *v2;
413                                  return(-1);
414                          if (v1->depth == 32 && v2->depth == 24)
415                                  return(1);
416 <                        return(0);
416 >                                        /* go for maximum depth otherwise */
417 >                        return(v2->depth - v1->depth);
418                  }
419                                          /* don't be too greedy */
420                  if (maxcolors <= 1<<v1->depth && maxcolors <= 1<<v2->depth)
421                          return(v1->depth - v2->depth);
422                  return(v2->depth - v1->depth);
423          }
424 <                                        /* prefer Pseudo when < 24-bit */
424 >                                        /* prefer Pseudo when < 15-bit */
425          if ((v1->class == TrueColor || v1->class == DirectColor) &&
426 <                        v1->depth < 24)
426 >                        v1->depth < 15)
427                  bad1 = 1;
428          if ((v2->class == TrueColor || v2->class == DirectColor) &&
429 <                        v2->depth < 24)
429 >                        v2->depth < 15)
430                  bad2 = -1;
431          if (bad1 | bad2)
432                  return(bad1+bad2);
# Line 540 | Line 537 | getras()                               /* get raster file */
537                          goto fail;
538                  getmono();
539          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
540 <                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
540 >                int  datsiz = ourvis.depth>16 ? sizeof(int4) : sizeof(int2);
541 >                ourdata = (unsigned char *)malloc(datsiz*xmax*ymax);
542                  if (ourdata == NULL)
543                          goto fail;
544 <                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
545 <                                ourdata, xmax, ymax, 32);
544 >                ourras = make_raster(thedisplay, &ourvis, datsiz*8,
545 >                                ourdata, xmax, ymax, datsiz*8);
546                  if (ourras == NULL)
547                          goto fail;
548                  getfull();
# Line 633 | Line 631 | int  xpos, ypos;
631          COLOR  cval;
632          register char  *cp;
633  
634 <        box.xmin = xpos; box.xsiz = 1;
635 <        box.ymin = ypos; box.ysiz = 1;
634 >        bbox.xmin = xpos; bbox.xsiz = 1;
635 >        bbox.ymin = ypos; bbox.ysiz = 1;
636          avgbox(cval);
637          scalecolor(cval, 1./exposure);
638          pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
# Line 691 | Line 689 | XKeyPressedEvent  *ekey;
689          case '\r':
690          case 'l':
691          case 'c':                               /* value */
692 <                if (avgbox(cval) == -1)
692 >                if (!avgbox(cval))
693                          return(-1);
694                  switch (com) {
695                  case '\n':
# Line 699 | Line 697 | XKeyPressedEvent  *ekey;
697                          sprintf(buf, "%.3f", intens(cval)/exposure);
698                          break;
699                  case 'l':                               /* luminance */
700 <                        sprintf(buf, "%.0fL", luminance(cval)/exposure);
700 >                        sprintf(buf, "%.1fL", luminance(cval)/exposure);
701                          break;
702                  case 'c':                               /* color */
703                          comp = pow(2.0, (double)scale);
# Line 710 | Line 708 | XKeyPressedEvent  *ekey;
708                          break;
709                  }
710                  XDrawImageString(thedisplay, wind, ourgc,
711 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
711 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
712                  return(0);
713          case 'i':                               /* identify (contour) */
714                  if (ourras->pixels == NULL)
# Line 734 | Line 732 | XKeyPressedEvent  *ekey;
732          case 't':                               /* trace */
733                  return(traceray(ekey->x, ekey->y));
734          case 'a':                               /* auto exposure */
735 <                if (tmflags == TM_F_CAMERA)
736 <                        return(0);
735 >                if (fname == NULL)
736 >                        return(-1);
737                  tmflags = TM_F_CAMERA;
738                  strcpy(buf, "auto exposure...");
739                  goto remap;
740          case 'h':                               /* human response */
741 <                if (tmflags == TM_F_HUMAN)
742 <                        return(0);
741 >                if (fname == NULL)
742 >                        return(-1);
743                  tmflags = TM_F_HUMAN;
744                  strcpy(buf, "human exposure...");
745                  goto remap;
746          case '=':                               /* adjust exposure */
747          case '@':                               /* adaptation level */
748 <                if (avgbox(cval) == -1)
748 >                if (!avgbox(cval))
749                          return(-1);
750                  comp = bright(cval);
751                  if (comp < 1e-20) {
# Line 761 | Line 759 | XKeyPressedEvent  *ekey;
759                          comp = .5/comp;
760                  comp = log(comp)/.69315 - scale;
761                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
762 <                if (tmflags != -1)
763 <                        tmflags = -1;           /* turn off tone mapping */
762 >                if (tmflags != TM_F_LINEAR)
763 >                        tmflags = TM_F_LINEAR;  /* turn off tone mapping */
764                  else {
765                          if (n == 0)             /* else check if any change */
766                                  return(0);
# Line 772 | Line 770 | XKeyPressedEvent  *ekey;
770                  sprintf(buf, "%+d", scale);
771          remap:
772                  XDrawImageString(thedisplay, wind, ourgc,
773 <                                box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
773 >                                bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf));
774                  XFlush(thedisplay);
775 <                free(ourdata);
775 >                /* free(ourdata);       This is done in XDestroyImage()! */
776                  free_raster(ourras);
777                  getras();
778          /* fall through */
# Line 803 | Line 801 | XKeyPressedEvent  *ekey;
801                  redraw(0, 0, width, height);
802                  return(0);
803          case ' ':                               /* clear */
804 <                redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
804 >                redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz);
805                  return(0);
806          default:
807                  XBell(thedisplay, 0);
# Line 837 | Line 835 | XButtonPressedEvent  *ebut;
835   }
836  
837  
838 < getbox(ebut)                            /* get new box */
838 > getbox(ebut)                            /* get new bbox */
839   XButtonPressedEvent  *ebut;
840   {
841          XEvent  e;
842  
843          XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
844          while (e.type == MotionNotify) {
845 <                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
845 >                revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y);
846                  XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
847 <                revbox(ebut->x, ebut->y, box.xmin, box.ymin);
847 >                revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin);
848          }
849 <        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
850 <        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
851 <        if (box.xmin > ebut->x) {
852 <                box.xsiz = box.xmin - ebut->x + 1;
853 <                box.xmin = ebut->x;
849 >        bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
850 >        bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
851 >        if (bbox.xmin > ebut->x) {
852 >                bbox.xsiz = bbox.xmin - ebut->x + 1;
853 >                bbox.xmin = ebut->x;
854          } else {
855 <                box.xsiz = ebut->x - box.xmin + 1;
855 >                bbox.xsiz = ebut->x - bbox.xmin + 1;
856          }
857 <        if (box.ymin > ebut->y) {
858 <                box.ysiz = box.ymin - ebut->y + 1;
859 <                box.ymin = ebut->y;
857 >        if (bbox.ymin > ebut->y) {
858 >                bbox.ysiz = bbox.ymin - ebut->y + 1;
859 >                bbox.ymin = ebut->y;
860          } else {
861 <                box.ysiz = ebut->y - box.ymin + 1;
861 >                bbox.ysiz = ebut->y - bbox.ymin + 1;
862          }
863   }
864  
# Line 884 | Line 882 | XButtonPressedEvent  *ebut;
882   }
883  
884  
885 < revbox(x0, y0, x1, y1)                  /* draw box with reversed lines */
885 > revbox(x0, y0, x1, y1)                  /* draw bbox with reversed lines */
886   int  x0, y0, x1, y1;
887   {
888          revline(x0, y0, x1, y0);
# Line 894 | Line 892 | int  x0, y0, x1, y1;
892   }
893  
894  
895 < avgbox(clr)                             /* average color over current box */
896 < COLOR  clr;
895 > int
896 > colavg(scn, n, cavg)
897 > register COLR   *scn;
898 > register int    n;
899 > COLOR   cavg;
900   {
901 <        static COLOR  lc;
902 <        static int  ll, lr, lt, lb;
901 >        COLOR   col;
902 >
903 >        while (n--) {
904 >                colr_color(col, *scn++);
905 >                addcolor(cavg, col);
906 >        }
907 > }
908 >
909 >
910 > int
911 > avgbox(cavg)                            /* average color over current bbox */
912 > COLOR   cavg;
913 > {
914 >        double  d;
915 >        register int    rval;
916 >
917 >        setcolor(cavg, 0., 0., 0.);
918 >        rval = dobox(colavg, (char *)cavg);
919 >        if (rval > 0) {
920 >                d = 1./rval;
921 >                scalecolor(cavg, d);
922 >        }
923 >        return(rval);
924 > }
925 >
926 >
927 > int
928 > dobox(f, p)                             /* run function over bbox */
929 > int     (*f)();                 /* function to call for each subscan */
930 > char    *p;                     /* pointer to private data */
931 > {
932          int  left, right, top, bottom;
933          int  y;
904        double  d;
905        COLOR  ctmp;
906        register int  x;
934  
935 <        setcolor(clr, 0.0, 0.0, 0.0);
936 <        left = box.xmin - xoff;
910 <        right = left + box.xsiz;
935 >        left = bbox.xmin - xoff;
936 >        right = left + bbox.xsiz;
937          if (left < 0)
938                  left = 0;
939          if (right > xmax)
940                  right = xmax;
941          if (left >= right)
942 <                return(-1);
943 <        top = box.ymin - yoff;
944 <        bottom = top + box.ysiz;
942 >                return(0);
943 >        top = bbox.ymin - yoff;
944 >        bottom = top + bbox.ysiz;
945          if (top < 0)
946                  top = 0;
947          if (bottom > ymax)
948                  bottom = ymax;
949          if (top >= bottom)
924                return(-1);
925        if (left == ll && right == lr && top == lt && bottom == lb) {
926                copycolor(clr, lc);
950                  return(0);
928        }
951          for (y = top; y < bottom; y++) {
952                  if (getscan(y) == -1)
953                          return(-1);
954 <                for (x = left; x < right; x++) {
933 <                        colr_color(ctmp, scanline[x]);
934 <                        addcolor(clr, ctmp);
935 <                }
954 >                (*f)(scanline+left, right-left, p);
955          }
956 <        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);
956 >        return((right-left)*(bottom-top));
957   }
958  
959  
960 + int
961 + addfix(scn, n)                  /* add fixation points to histogram */
962 + COLR    *scn;
963 + int     n;
964 + {
965 +        if (tmCvColrs(lscan, TM_NOCHROM, scn, n))
966 +                goto tmerr;
967 +        if (tmAddHisto(lscan, n, FIXWEIGHT))
968 +                goto tmerr;
969 +        return;
970 + tmerr:
971 +        quiterr("tone mapping error");
972 + }
973 +
974 +
975   make_tonemap()                  /* initialize tone mapping */
976   {
977          int  flags, y;
978  
979 <        if (tmflags != -1 && fname == NULL) {
979 >        if (tmflags != TM_F_LINEAR && fname == NULL) {
980                  fprintf(stderr, "%s: cannot adjust tone of standard input\n",
981                                  progname);
982 <                tmflags = -1;
982 >                tmflags = TM_F_LINEAR;
983          }
984 <        if (tmflags == -1) {            /* linear with clamping */
984 >        if (tmflags == TM_F_LINEAR) {   /* linear with clamping */
985                  setcolrcor(pow, 1.0/gamcor);
986                  return;
987          }
988          flags = tmflags;                /* histogram adjustment */
989          if (greyscale) flags |= TM_F_BW;
990          if (tmTop != NULL) {            /* reuse old histogram if one */
991 +                tmDone(tmTop);
992                  tmTop->flags = flags;
993          } else {                        /* else initialize */
994                  if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL)
# Line 983 | Line 1014 | make_tonemap()                 /* initialize tone mapping */
1014                                  goto tmerr;
1015                  }
1016          }
1017 +        tmDup();                        /* add fixations to duplicate map */
1018 +        dobox(addfix, NULL);
1019                                          /* (re)compute tone mapping */
1020          if (tmComputeMapping(gamcor, 0., 0.))
1021                  goto tmerr;
# Line 1000 | Line 1033 | int  len;
1033   {
1034          register BYTE  *ps;
1035  
1036 <        if (tmflags == -1) {
1036 >        if (tmflags == TM_F_LINEAR) {
1037                  if (scale)
1038                          shiftcolrs(scn, len, scale);
1039                  colrs_gambs(scn, len);
# Line 1061 | Line 1094 | getmono()                      /* get monochrome data */
1094                          cerr[x] = err + errp;
1095                  }
1096          }
1097 <        free((char *)cerr);
1097 >        free((void *)cerr);
1098   }
1099  
1100  
# Line 1104 | Line 1137 | COLR  *scan;
1137                  errp = err;
1138                  ti = x*xmax/iconwidth;
1139                  copycolr(clr, scan[ti]);
1140 <                normcolrs(clr, 1, scale);
1140 >                normcolrs(&clr, 1, scale);
1141                  err += normbright(clr) + cerr[x];
1142                  if (err > 127)
1143                          err -= 255;
# Line 1121 | Line 1154 | getfull()                      /* get full (24-bit) data */
1154   {
1155          int     y;
1156          register unsigned int4  *dp;
1157 +        register unsigned int2  *dph;
1158          register int    x;
1159                                          /* initialize tone mapping */
1160          make_tonemap();
1161                                          /* read and convert file */
1162          dp = (unsigned int4 *)ourdata;
1163 +        dph = (unsigned int2 *)ourdata;
1164          for (y = 0; y < ymax; y++) {
1165                  getscan(y);
1166                  add2icon(y, scanline);
1167                  tmap_colrs(scanline, xmax);
1168 <                if (ourras->image->blue_mask & 1)
1168 >                switch (ourras->image->blue_mask) {
1169 >                case 0xff:              /* 24-bit RGB */
1170                          for (x = 0; x < xmax; x++)
1171                                  *dp++ = (unsigned int4)scanline[x][RED] << 16 |
1172                                          (unsigned int4)scanline[x][GRN] << 8 |
1173                                          (unsigned int4)scanline[x][BLU] ;
1174 <                else
1174 >                        break;
1175 >                case 0xff0000:          /* 24-bit BGR */
1176                          for (x = 0; x < xmax; x++)
1177                                  *dp++ = (unsigned int4)scanline[x][RED] |
1178                                          (unsigned int4)scanline[x][GRN] << 8 |
1179                                          (unsigned int4)scanline[x][BLU] << 16 ;
1180 +                        break;
1181 + #if 0
1182 +                case 0x1f:              /* 15-bit RGB */
1183 +                        for (x = 0; x < xmax; x++)
1184 +                                *dph++ =        (scanline[x][RED] << 7 & 0x7c00) |
1185 +                                        (scanline[x][GRN] << 2 & 0x3e0) |
1186 +                                        (unsigned)scanline[x][BLU] >> 3 ;
1187 +                        break;
1188 +                case 0x7c00:            /* 15-bit BGR */
1189 +                        for (x = 0; x < xmax; x++)
1190 +                                *dph++ =        (unsigned)scanline[x][RED] >> 3 |
1191 +                                        (scanline[x][GRN] << 2 & 0x3e0) |
1192 +                                        (scanline[x][BLU] << 7 & 0x7c00) ;
1193 +                        break;
1194 + #endif
1195 +                default:                /* unknown */
1196 +                        if (ourvis.depth > 16)
1197 +                                for (x = 0; x < xmax; x++) {
1198 +                                        *dp = ourras->image->red_mask &
1199 +                                                ourras->image->red_mask*scanline[x][RED]/255;
1200 +                                        *dp |= ourras->image->green_mask &
1201 +                                                ourras->image->green_mask*scanline[x][GRN]/255;
1202 +                                        *dp++ |= ourras->image->blue_mask &
1203 +                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1204 +                                }
1205 +                        else
1206 +                                for (x = 0; x < xmax; x++) {
1207 +                                        *dph = ourras->image->red_mask &
1208 +                                                ourras->image->red_mask*scanline[x][RED]/255;
1209 +                                        *dph |= ourras->image->green_mask &
1210 +                                                ourras->image->green_mask*scanline[x][GRN]/255;
1211 +                                        *dph++ |= ourras->image->blue_mask &
1212 +                                                ourras->image->blue_mask*scanline[x][BLU]/255;
1213 +                                }
1214 +                        break;
1215 +                }
1216          }
1217   }
1218  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines