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.28 by greg, Mon Jul 19 15:18:05 1993 UTC vs.
Revision 2.41 by greg, Wed Jan 25 10:53:02 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1993 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 21 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21  
22   #include  "standard.h"
23  
24 + #include  <signal.h>
25   #include  <X11/Xlib.h>
26   #include  <X11/cursorfont.h>
27   #include  <X11/Xutil.h>
# Line 55 | Line 56 | static char SCCSid[] = "$SunId$ LBL";
56   #define  redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
57  
58   double  gamcor = 2.2;                   /* gamma correction */
59 + char  *gamstr = NULL;                   /* gamma value override */
60  
61   int  dither = 1;                        /* dither colors? */
62   int  fast = 0;                          /* keep picture in Pixmap? */
# Line 71 | Line 73 | int  scale = 0;                                /* scalefactor; power of two */
73   int  xoff = 0;                          /* x image offset */
74   int  yoff = 0;                          /* y image offset */
75  
76 + int  parent = 0;                        /* number of children, -1 if child */
77 + int  sequential = 0;                    /* display images in sequence */
78 +
79   VIEW  ourview = STDVIEW;                /* image view parameters */
80   int  gotview = 0;                       /* got parameters from file */
81  
# Line 109 | Line 114 | char  *progname;
114  
115   char  errmsg[128];
116  
117 < extern BYTE  clrtab[256][3];            /* global color map */
117 > BYTE  clrtab[256][3];                   /* global color map */
118  
119   extern long  ftell();
120  
121 + extern char  *getenv();
122 +
123   Display  *thedisplay;
124   Atom  closedownAtom, wmProtocolsAtom;
125  
126 + int  sigrecv;
127  
128 + int  onsig() { sigrecv++; }
129 +
130 +
131   main(argc, argv)
132   int  argc;
133   char  *argv[];
134   {
124        extern char  *getenv();
125        char  *gv;
135          int  headline();
136          int  i;
137 +        int  pid;
138          
139          progname = argv[0];
130        if ((gv = getenv("GAMMA")) != NULL)
131                gamcor = atof(gv);
140  
141          for (i = 1; i < argc; i++)
142                  if (argv[i][0] == '-')
# Line 140 | Line 148 | char  *argv[];
148                                  greyscale = !greyscale;
149                                  break;
150                          case 'm':
151 +                                greyscale = 1;
152                                  maxcolors = 2;
153                                  break;
154                          case 'd':
# Line 151 | Line 160 | char  *argv[];
160                          case 'f':
161                                  fast = !fast;
162                                  break;
163 +                        case 's':
164 +                                sequential = !sequential;
165 +                                break;
166                          case 'e':
167                                  if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
168                                          goto userr;
# Line 160 | Line 172 | char  *argv[];
172                                  if (argv[i][2] == 'e')
173                                          geometry = argv[++i];
174                                  else
175 <                                        gamcor = atof(argv[++i]);
175 >                                        gamstr = argv[++i];
176                                  break;
177                          default:
178                                  goto userr;
# Line 170 | Line 182 | char  *argv[];
182                  else
183                          break;
184  
185 <        if (i == argc-1) {
185 >        if (i > argc)
186 >                goto userr;
187 >        while (i < argc-1) {
188 >                sigrecv = 0;
189 >                signal(SIGCONT, onsig);
190 >                if ((pid=fork()) == 0) {        /* a child for each picture */
191 >                        parent = -1;
192 >                        break;
193 >                }
194 >                if (pid < 0)
195 >                        quiterr("fork failed");
196 >                parent++;
197 >                while (!sigrecv)
198 >                        pause();        /* wait for wake-up call */
199 >                i++;
200 >        }
201 >        if (i < argc) {                 /* open picture file */
202                  fname = argv[i];
203                  fin = fopen(fname, "r");
204 <                if (fin == NULL) {
205 <                        sprintf(errmsg, "cannot open file \"%s\"", fname);
206 <                        quiterr(errmsg);
179 <                }
180 <        } else if (i != argc)
181 <                goto userr;
204 >                if (fin == NULL)
205 >                        quiterr("cannot open picture file");
206 >        }
207                                  /* get header */
208          getheader(fin, headline, NULL);
209                                  /* get picture dimensions */
# Line 198 | Line 223 | char  *argv[];
223                  getevent();             /* main loop */
224   userr:
225          fprintf(stderr,
226 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
226 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s] pic ..\n",
227                          progname);
228          exit(1);
229   }
# Line 248 | Line 273 | char **argv;
273          name += i+1;
274          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
275                  quiterr("cannot open display");
276 +                                /* set gamma value */
277 +        if (gamstr == NULL)             /* get it from the X server */
278 +                gamstr = XGetDefault(thedisplay, "radiance", "gamma");
279 +        if (gamstr == NULL)             /* get it from the environment */
280 +                gamstr = getenv("DISPLAY_GAMMA");
281 +        if (gamstr != NULL)
282 +                gamcor = atof(gamstr);
283                                  /* get best visual for default screen */
284          getbestvis();
285                                  /* store image */
# Line 274 | Line 306 | char **argv;
306                  }
307          }
308          /* open window */
309 +        i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel;
310          ourwinattr.border_pixel = ourwhite;
311          ourwinattr.background_pixel = ourblack;
312 <        ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
313 <                        ourvis.visual, AllocNone);
312 >        if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) {
313 >                ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual);
314 >                i |= CWColormap;
315 >        }
316          ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
317                          ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
318          ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
319          wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
320                          xszhints.width, xszhints.height, BORWIDTH,
321 <                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
322 <                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
321 >                        ourvis.depth, InputOutput, ourvis.visual,
322 >                        i, &ourwinattr);
323          if (wind == 0)
324                  quiterr("cannot create window");
325          width = xmax;
326          height = ymax;
327 <        xgcv.foreground = ourblack;
293 <        xgcv.background = ourwhite;
327 >        /* prepare graphics drawing context */
328          if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
329                  quiterr("cannot get font");
330 +        xgcv.foreground = ourblack;
331 +        xgcv.background = ourwhite;
332          ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
333                          GCFont, &xgcv);
334          xgcv.function = GXinvert;
# Line 321 | Line 357 | char **argv;
357          XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
358  
359          XMapWindow(thedisplay, wind);
324        return;
360   } /* end of init */
361  
362  
363   quiterr(err)            /* print message and exit */
364   char  *err;
365   {
366 <        if (err != NULL) {
367 <                fprintf(stderr, "%s: %s\n", progname, err);
368 <                exit(1);
366 >        register int  es;
367 >        int  cs;
368 >
369 >        if (es = err != NULL)
370 >                fprintf(stderr, "%s: %s: %s\n", progname,
371 >                                fname==NULL?"<stdin>":fname, err);
372 >        if (thedisplay != NULL)
373 >                XCloseDisplay(thedisplay);
374 >        if (parent < 0 & sigrecv == 0)
375 >                kill(getppid(), SIGCONT);
376 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
377 >                if (es == 0)
378 >                        es = cs>>8 & 0xff;
379 >                parent--;
380          }
381 <        exit(0);
381 >        exit(es);
382   }
383  
384  
# Line 519 | Line 565 | getevent()                             /* process the next event */
565                  map_rcolors(ourras, wind);
566                  if (fast)
567                          make_rpixmap(ourras, wind);
568 +                if (!sequential & parent < 0 & sigrecv == 0) {
569 +                        kill(getppid(), SIGCONT);
570 +                        sigrecv--;
571 +                }
572                  break;
573          case UnmapNotify:
574                  if (!fast)
# Line 536 | Line 586 | getevent()                             /* process the next event */
586                  else
587                          getbox(&xev.xbutton);
588                  break;
589 <   case ClientMessage:
589 >        case ClientMessage:
590                  if ((xev.xclient.message_type == wmProtocolsAtom) &&
591                                  (xev.xclient.data.l[0] == closedownAtom))
592                          quiterr(NULL);
# Line 634 | Line 684 | XKeyPressedEvent  *ekey;
684          case '@':                               /* adaptation level */
685                  if (avgbox(cval) == -1)
686                          return(-1);
687 <                comp = com=='@'
688 <                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
689 <                : .5/bright(cval) ;
687 >                comp = bright(cval);
688 >                if (comp < 1e-20) {
689 >                        XBell(thedisplay, 0);
690 >                        return(-1);
691 >                }
692 >                if (com == '@')
693 >                        comp = 106./exposure/
694 >                        pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5);
695 >                else
696 >                        comp = .5/comp;
697                  comp = log(comp)/.69315 - scale;
698                  n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
699                  if (n == 0)
# Line 897 | Line 954 | getfull()                      /* get full (24-bit) data */
954                  colrs_gambs(scanline, xmax);
955                  if (ourras->image->blue_mask & 1)
956                          for (x = 0; x < xmax; x++)
957 <                                *dp++ = scanline[x][RED] << 16 |
958 <                                        scanline[x][GRN] << 8 |
959 <                                        scanline[x][BLU] ;
957 >                                *dp++ = (unsigned int4)scanline[x][RED] << 16 |
958 >                                        (unsigned int4)scanline[x][GRN] << 8 |
959 >                                        (unsigned int4)scanline[x][BLU] ;
960                  else
961                          for (x = 0; x < xmax; x++)
962 <                                *dp++ = scanline[x][RED] |
963 <                                        scanline[x][GRN] << 8 |
964 <                                        scanline[x][BLU] << 16 ;
962 >                                *dp++ = (unsigned int4)scanline[x][RED] |
963 >                                        (unsigned int4)scanline[x][GRN] << 8 |
964 >                                        (unsigned int4)scanline[x][BLU] << 16 ;
965          }
966   }
967  
# Line 943 | Line 1000 | getgrey()                      /* get greyscale data */
1000   getmapped()                     /* get color-mapped data */
1001   {
1002          int     y;
1003 +                                        /* make sure we can do it first */
1004 +        if (fname == NULL)
1005 +                quiterr("cannot map colors from standard input");
1006                                          /* set gamma correction */
1007          setcolrgam(gamcor);
1008                                          /* make histogram */
1009 <        new_histo();
1009 >        if (new_histo((long)xmax*ymax) == -1)
1010 >                quiterr("cannot initialize histogram");
1011          for (y = 0; y < ymax; y++) {
1012                  if (getscan(y) < 0)
1013 <                        quiterr("seek error in getmapped");
1013 >                        break;
1014                  add2icon(y, scanline);
1015                  if (scale)
1016                          shiftcolrs(scanline, xmax, scale);
# Line 960 | Line 1021 | getmapped()                    /* get color-mapped data */
1021          if (!new_clrtab(maxcolors))
1022                  quiterr("cannot create color map");
1023          for (y = 0; y < ymax; y++) {
1024 <                if (getscan(y) < 0)
964 <                        quiterr("seek error in getmapped");
1024 >                getscan(y);
1025                  if (scale)
1026                          shiftcolrs(scanline, xmax, scale);
1027                  colrs_gambs(scanline, xmax);
# Line 1004 | Line 1064 | double sf;
1064   getscan(y)
1065   int  y;
1066   {
1067 +        static int  trunced = -1;               /* truncated file? */
1068 + skipit:
1069 +        if (trunced >= 0 && y >= trunced) {
1070 +                bzero(scanline, xmax*sizeof(COLR));
1071 +                return(-1);
1072 +        }
1073          if (y != cury) {
1074                  if (scanpos == NULL || scanpos[y] == -1)
1075                          return(-1);
# Line 1013 | Line 1079 | int  y;
1079          } else if (scanpos != NULL && scanpos[y] == -1)
1080                  scanpos[y] = ftell(fin);
1081  
1082 <        if (freadcolrs(scanline, xmax, fin) < 0)
1083 <                quiterr("read error");
1084 <
1082 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1083 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1084 >                                progname, fname==NULL?"<stdin>":fname);
1085 >                trunced = y;
1086 >                goto skipit;
1087 >        }
1088          cury++;
1089          return(0);
1090   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines