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.22 by greg, Tue Mar 2 10:54:08 1993 UTC vs.
Revision 2.32 by greg, Thu Oct 28 11:39:59 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1993 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>
28 + #include  <X11/Xatom.h>
29  
30   #include  "color.h"
31   #include  "view.h"
# Line 31 | Line 33 | static char SCCSid[] = "$SunId$ LBL";
33   #include  "random.h"
34   #include  "resolu.h"
35  
36 + #ifdef  __alpha
37 + #define  int4           int
38 + #endif
39 + #ifndef  int4
40 + #define  int4           long
41 + #endif
42 +
43   #define  FONTNAME       "8x13"          /* text font we'll use */
44  
45   #define  CTRL(c)        ((c)-'@')
# Line 55 | Line 64 | char   *dispname = NULL;               /* our display name */
64  
65   Window  wind = 0;                       /* our output window */
66   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58 Font  fontid;                           /* our font */
59
67   int  maxcolors = 0;                     /* maximum colors */
68   int  greyscale = 0;                     /* in grey */
69  
# Line 65 | Line 72 | int  scale = 0;                                /* scalefactor; power of two */
72   int  xoff = 0;                          /* x image offset */
73   int  yoff = 0;                          /* y image offset */
74  
75 + int  parent = 0;                        /* number of children, -1 if child */
76 +
77   VIEW  ourview = STDVIEW;                /* image view parameters */
78   int  gotview = 0;                       /* got parameters from file */
79  
# Line 107 | Line 116 | extern BYTE  clrtab[256][3];           /* global color map */
116  
117   extern long  ftell();
118  
110 extern char  *malloc(), *calloc();
111
119   Display  *thedisplay;
120 + Atom  closedownAtom, wmProtocolsAtom;
121  
122 + int  sigrecv;
123  
124 + int  onsig() { sigrecv++; }
125 +
126 +
127   main(argc, argv)
128   int  argc;
129   char  *argv[];
# Line 120 | Line 132 | char  *argv[];
132          char  *gv;
133          int  headline();
134          int  i;
135 +        int  pid;
136          
137          progname = argv[0];
138          if ((gv = getenv("GAMMA")) != NULL)
# Line 165 | Line 178 | char  *argv[];
178                  else
179                          break;
180  
181 <        if (i == argc-1) {
181 >        if (i > argc)
182 >                goto userr;
183 >        while (i < argc-1) {
184 >                sigrecv = 0;
185 >                signal(SIGCONT, onsig);
186 >                if ((pid=fork()) == 0) {        /* a child for each picture */
187 >                        parent = -1;
188 >                        break;
189 >                }
190 >                if (pid < 0)
191 >                        quiterr("fork failed");
192 >                parent++;
193 >                while (!sigrecv)
194 >                        pause();        /* wait for wake-up call */
195 >                i++;
196 >        }
197 >        if (i < argc) {                 /* open picture file */
198                  fname = argv[i];
199                  fin = fopen(fname, "r");
200                  if (fin == NULL) {
201                          sprintf(errmsg, "cannot open file \"%s\"", fname);
202                          quiterr(errmsg);
203                  }
204 <        } else if (i != argc)
176 <                goto userr;
204 >        }
205                                  /* get header */
206          getheader(fin, headline, NULL);
207                                  /* get picture dimensions */
# Line 187 | Line 215 | char  *argv[];
215          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
216                  quiterr("out of memory");
217  
218 <        init();                 /* get file and open window */
218 >        init(argc, argv);                       /* get file and open window */
219  
220 +        if (parent < 0) {
221 +                kill(getppid(), SIGCONT);       /* signal parent if child */
222 +                sigrecv--;
223 +        }
224          for ( ; ; )
225                  getevent();             /* main loop */
226   userr:
227          fprintf(stderr,
228 < "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
228 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n",
229                          progname);
230          exit(1);
231   }
# Line 214 | Line 246 | char  *s;
246   }
247  
248  
249 < init()                  /* get data and open window */
249 > init(argc, argv)                        /* get data and open window */
250 > int argc;
251 > char **argv;
252   {
219        XWMHints        ourxwmhints;
253          XSetWindowAttributes    ourwinattr;
254 <        XSizeHints      oursizhints;
254 >        XClassHint      xclshints;
255 >        XWMHints        xwmhints;
256 >        XSizeHints      xszhints;
257 >        XTextProperty   windowName, iconName;
258 >        XGCValues       xgcv;
259 >        char    *name;
260          register int    i;
261          
262          if (fname != NULL) {
263                  scanpos = (long *)malloc(ymax*sizeof(long));
264                  if (scanpos == NULL)
265 <                        goto memerr;
265 >                        quiterr("out of memory");
266                  for (i = 0; i < ymax; i++)
267                          scanpos[i] = -1;
268 <        }
268 >                name = fname;
269 >        } else
270 >                name = progname;
271 >                                /* remove directory prefix from name */
272 >        for (i = strlen(name); i-- > 0; )
273 >                if (name[i] == '/')
274 >                        break;
275 >        name += i+1;
276          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
277                  quiterr("cannot open display");
278                                  /* get best visual for default screen */
# Line 235 | Line 280 | init()                 /* get data and open window */
280                                  /* store image */
281          getras();
282                                  /* get size and position */
283 <        bzero((char *)&oursizhints, sizeof(oursizhints));
284 <        oursizhints.width = xmax; oursizhints.height = ymax;
283 >        xszhints.flags = 0;
284 >        xszhints.width = xmax; xszhints.height = ymax;
285          if (geometry != NULL) {
286 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
287 <                                (unsigned *)&oursizhints.width,
288 <                                (unsigned *)&oursizhints.height);
286 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
287 >                                (unsigned *)&xszhints.width,
288 >                                (unsigned *)&xszhints.height);
289                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
290 <                        oursizhints.flags |= USSize;
290 >                        xszhints.flags |= USSize;
291                  else
292 <                        oursizhints.flags |= PSize;
292 >                        xszhints.flags |= PSize;
293                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
294 <                        oursizhints.flags |= USPosition;
294 >                        xszhints.flags |= USPosition;
295                          if (i & XNegative)
296 <                                oursizhints.x += DisplayWidth(thedisplay,
297 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
296 >                                xszhints.x += DisplayWidth(thedisplay,
297 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
298                          if (i & YNegative)
299 <                                oursizhints.y += DisplayHeight(thedisplay,
300 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
299 >                                xszhints.y += DisplayHeight(thedisplay,
300 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
301                  }
302          }
303 <                                /* open window */
304 <        ourwinattr.border_pixel = ourblack;
305 <        ourwinattr.background_pixel = ourwhite;
303 >        /* open window */
304 >        ourwinattr.border_pixel = ourwhite;
305 >        ourwinattr.background_pixel = ourblack;
306          ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
307                          ourvis.visual, AllocNone);
308 <        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
309 <                        oursizhints.width, oursizhints.height, BORWIDTH,
310 <                        ourvis.depth, InputOutput, ourvis.visual,
311 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
308 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
309 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
310 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
311 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
312 >                        xszhints.width, xszhints.height, BORWIDTH,
313 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
314 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
315          if (wind == 0)
316                  quiterr("cannot create window");
269        XFreeColormap(thedisplay, ourwinattr.colormap);
317          width = xmax;
318          height = ymax;
319 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
320 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
274 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
275 <        XSetFunction(thedisplay, revgc, GXinvert);
276 <        fontid = XLoadFont(thedisplay, FONTNAME);
277 <        if (fontid == 0)
319 >        /* prepare graphics drawing context */
320 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
321                  quiterr("cannot get font");
322 <        XSetFont(thedisplay, ourgc, fontid);
323 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
324 <                        XC_diamond_cross));
325 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
326 <        if (oursizhints.flags)
327 <                XSetNormalHints(thedisplay, wind, &oursizhints);
328 <        ourxwmhints.flags = InputHint|IconPixmapHint;
329 <        ourxwmhints.input = True;
330 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
322 >        xgcv.foreground = ourblack;
323 >        xgcv.background = ourwhite;
324 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
325 >                        GCFont, &xgcv);
326 >        xgcv.function = GXinvert;
327 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
328 >                        GCFunction, &xgcv);
329 >
330 >        /* set up the window manager */
331 >        xwmhints.flags = InputHint|IconPixmapHint;
332 >        xwmhints.input = True;
333 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
334                          wind, icondata, iconwidth, iconheight);
335 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
336 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
337 <                        |ButtonMotionMask|StructureNotifyMask
338 <                        |KeyPressMask|ExposureMask);
335 >
336 >        windowName.encoding = iconName.encoding = XA_STRING;
337 >        windowName.format = iconName.format = 8;
338 >        windowName.value = (u_char *)name;
339 >        windowName.nitems = strlen(windowName.value);
340 >        iconName.value = (u_char *)name;
341 >        iconName.nitems = strlen(windowName.value);
342 >
343 >        xclshints.res_name = NULL;
344 >        xclshints.res_class = "Ximage";
345 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
346 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
347 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
348 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
349 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
350 >
351          XMapWindow(thedisplay, wind);
352          return;
295 memerr:
296        quiterr("out of memory");
353   } /* end of init */
354  
355  
356   quiterr(err)            /* print message and exit */
357   char  *err;
358   {
359 <        if (err != NULL) {
360 <                fprintf(stderr, "%s: %s\n", progname, err);
361 <                exit(1);
359 >        register int  es;
360 >        int  cs;
361 >
362 >        if (es = err != NULL)
363 >                fprintf(stderr, "%s: %s: %s\n", progname,
364 >                                fname==NULL?"<stdin>":fname, err);
365 >        if (parent > 0 & wind != 0) {
366 >                XDestroyWindow(thedisplay, wind);
367 >                XFlush(thedisplay);
368 >        } else if (parent < 0 & sigrecv == 0)
369 >                kill(getppid(), SIGCONT);
370 >        while (parent > 0 && wait(&cs) != -1) { /* wait for any children */
371 >                if (es == 0)
372 >                        es = cs>>8 & 0xff;
373 >                parent--;
374          }
375 <        exit(0);
375 >        exit(es);
376   }
377  
378  
# Line 445 | Line 513 | getras()                               /* get raster file */
513                          goto fail;
514                  getmono();
515          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
516 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
516 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
517                  if (ourdata == NULL)
518                          goto fail;
519 <                ourras = make_raster(thedisplay, &ourvis, 32,
519 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
520                                  ourdata, xmax, ymax, 32);
521                  if (ourras == NULL)
522                          goto fail;
# Line 476 | Line 544 | fail:
544  
545   getevent()                              /* process the next event */
546   {
547 <        union {
480 <                XEvent  u;
481 <                XConfigureEvent  c;
482 <                XExposeEvent  e;
483 <                XButtonPressedEvent  b;
484 <                XKeyPressedEvent  k;
485 <        } e;
547 >        XEvent xev;
548  
549 <        XNextEvent(thedisplay, &e.u);
550 <        switch (e.u.type) {
549 >        XNextEvent(thedisplay, &xev);
550 >        switch ((int)xev.type) {
551          case KeyPress:
552 <                docom(&e.k);
552 >                docom(&xev.xkey);
553                  break;
554          case ConfigureNotify:
555 <                width = e.c.width;
556 <                height = e.c.height;
555 >                width = xev.xconfigure.width;
556 >                height = xev.xconfigure.height;
557                  break;
558          case MapNotify:
559                  map_rcolors(ourras, wind);
# Line 503 | Line 565 | getevent()                             /* process the next event */
565                          unmap_rcolors(ourras);
566                  break;
567          case Expose:
568 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
568 >                redraw(xev.xexpose.x, xev.xexpose.y,
569 >                                xev.xexpose.width, xev.xexpose.height);
570                  break;
571          case ButtonPress:
572 <                if (e.b.state & (ShiftMask|ControlMask))
573 <                        moveimage(&e.b);
574 <                else if (e.b.button == Button2)
575 <                        traceray(e.b.x, e.b.y);
572 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
573 >                        moveimage(&xev.xbutton);
574 >                else if (xev.xbutton.button == Button2)
575 >                        traceray(xev.xbutton.x, xev.xbutton.y);
576                  else
577 <                        getbox(&e.b);
577 >                        getbox(&xev.xbutton);
578                  break;
579 +        case ClientMessage:
580 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
581 +                                (xev.xclient.data.l[0] == closedownAtom))
582 +                        quiterr(NULL);
583 +                break;
584          }
585   }
586  
# Line 603 | Line 671 | XKeyPressedEvent  *ekey;
671          case 't':                               /* trace */
672                  return(traceray(ekey->x, ekey->y));
673          case '=':                               /* adjust exposure */
674 +        case '@':                               /* adaptation level */
675                  if (avgbox(cval) == -1)
676                          return(-1);
677 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
677 >                comp = com=='@'
678 >                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
679 >                : .5/bright(cval) ;
680 >                comp = log(comp)/.69315 - scale;
681 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
682                  if (n == 0)
683                          return(0);
684                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 627 | Line 700 | XKeyPressedEvent  *ekey;
700                          make_rpixmap(ourras, wind);
701                  redraw(0, 0, width, height);
702                  return(0);
703 +        case 'f':                               /* turn on fast redraw */
704 +                fast = 1;
705 +                make_rpixmap(ourras, wind);
706 +                return(0);
707 +        case 'F':                               /* turn off fast redraw */
708 +                fast = 0;
709 +                free_rpixmap(ourras);
710 +                return(0);
711          case '0':                               /* recenter origin */
712                  if (xoff == 0 & yoff == 0)
713                          return(0);
# Line 647 | Line 728 | XKeyPressedEvent  *ekey;
728   moveimage(ebut)                         /* shift the image */
729   XButtonPressedEvent  *ebut;
730   {
731 <        union {
651 <                XEvent  u;
652 <                XButtonReleasedEvent  b;
653 <                XPointerMovedEvent  m;
654 <        }  e;
731 >        XEvent  e;
732          int     mxo, myo;
733  
734 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
735 <        while (e.u.type == MotionNotify) {
736 <                mxo = e.m.x;
737 <                myo = e.m.y;
734 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
735 >        while (e.type == MotionNotify) {
736 >                mxo = e.xmotion.x;
737 >                myo = e.xmotion.y;
738                  revline(ebut->x, ebut->y, mxo, myo);
739                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
740                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
741 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
741 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
742                  revline(ebut->x, ebut->y, mxo, myo);
743                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
744                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
745          }
746 <        xoff += e.b.x - ebut->x;
747 <        yoff += e.b.y - ebut->y;
746 >        xoff += e.xbutton.x - ebut->x;
747 >        yoff += e.xbutton.y - ebut->y;
748          XClearWindow(thedisplay, wind);
749          redraw(0, 0, width, height);
750   }
# Line 676 | Line 753 | XButtonPressedEvent  *ebut;
753   getbox(ebut)                            /* get new box */
754   XButtonPressedEvent  *ebut;
755   {
756 <        union {
680 <                XEvent  u;
681 <                XButtonReleasedEvent  b;
682 <                XPointerMovedEvent  m;
683 <        }  e;
756 >        XEvent  e;
757  
758 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
759 <        while (e.u.type == MotionNotify) {
760 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
761 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
758 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
759 >        while (e.type == MotionNotify) {
760 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
761 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
762                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
763          }
764 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
765 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
764 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
765 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
766          if (box.xmin > ebut->x) {
767                  box.xsiz = box.xmin - ebut->x + 1;
768                  box.xmin = ebut->x;
# Line 850 | Line 923 | COLR  *scan;
923   getfull()                       /* get full (24-bit) data */
924   {
925          int     y;
926 <        register unsigned long  *dp;
926 >        register unsigned int4  *dp;
927          register int    x;
928                                          /* set gamma correction */
929          setcolrgam(gamcor);
930                                          /* read and convert file */
931 <        dp = (unsigned long *)ourdata;
931 >        dp = (unsigned int4 *)ourdata;
932          for (y = 0; y < ymax; y++) {
933                  getscan(y);
934                  add2icon(y, scanline);
# Line 910 | Line 983 | getgrey()                      /* get greyscale data */
983   getmapped()                     /* get color-mapped data */
984   {
985          int     y;
986 +                                        /* make sure we can do it first */
987 +        if (fname == NULL)
988 +                quiterr("cannot map colors from standard input");
989                                          /* set gamma correction */
990          setcolrgam(gamcor);
991                                          /* make histogram */
992          new_histo();
993          for (y = 0; y < ymax; y++) {
994                  if (getscan(y) < 0)
995 <                        quiterr("seek error in getmapped");
995 >                        break;
996                  add2icon(y, scanline);
997                  if (scale)
998                          shiftcolrs(scanline, xmax, scale);
# Line 927 | Line 1003 | getmapped()                    /* get color-mapped data */
1003          if (!new_clrtab(maxcolors))
1004                  quiterr("cannot create color map");
1005          for (y = 0; y < ymax; y++) {
1006 <                if (getscan(y) < 0)
931 <                        quiterr("seek error in getmapped");
1006 >                getscan(y);
1007                  if (scale)
1008                          shiftcolrs(scanline, xmax, scale);
1009                  colrs_gambs(scanline, xmax);
# Line 971 | Line 1046 | double sf;
1046   getscan(y)
1047   int  y;
1048   {
1049 +        static int  trunced = -1;               /* truncated file? */
1050 + skipit:
1051 +        if (trunced >= 0 && y >= trunced) {
1052 +                bzero(scanline, xmax*sizeof(COLR));
1053 +                return(-1);
1054 +        }
1055          if (y != cury) {
1056                  if (scanpos == NULL || scanpos[y] == -1)
1057                          return(-1);
# Line 980 | Line 1061 | int  y;
1061          } else if (scanpos != NULL && scanpos[y] == -1)
1062                  scanpos[y] = ftell(fin);
1063  
1064 <        if (freadcolrs(scanline, xmax, fin) < 0)
1065 <                quiterr("read error");
1066 <
1064 >        if (freadcolrs(scanline, xmax, fin) < 0) {
1065 >                fprintf(stderr, "%s: %s: unfinished picture\n",
1066 >                                progname, fname==NULL?"<stdin>":fname);
1067 >                trunced = y;
1068 >                goto skipit;
1069 >        }
1070          cury++;
1071          return(0);
1072   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines