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.20 by greg, Fri Feb 12 18:03:08 1993 UTC vs.
Revision 2.28 by greg, Mon Jul 19 15:18:05 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 24 | Line 24 | static char SCCSid[] = "$SunId$ LBL";
24   #include  <X11/Xlib.h>
25   #include  <X11/cursorfont.h>
26   #include  <X11/Xutil.h>
27 + #include  <X11/Xatom.h>
28  
29   #include  "color.h"
30   #include  "view.h"
# Line 31 | Line 32 | static char SCCSid[] = "$SunId$ LBL";
32   #include  "random.h"
33   #include  "resolu.h"
34  
35 + #ifdef  __alpha
36 + #define  int4           int
37 + #endif
38 + #ifndef  int4
39 + #define  int4           long
40 + #endif
41 +
42   #define  FONTNAME       "8x13"          /* text font we'll use */
43  
44   #define  CTRL(c)        ((c)-'@')
# Line 55 | Line 63 | char   *dispname = NULL;               /* our display name */
63  
64   Window  wind = 0;                       /* our output window */
65   unsigned long  ourblack=0, ourwhite=1;  /* black and white for this visual */
58 Font  fontid;                           /* our font */
59
66   int  maxcolors = 0;                     /* maximum colors */
67   int  greyscale = 0;                     /* in grey */
68  
# Line 107 | Line 113 | extern BYTE  clrtab[256][3];           /* global color map */
113  
114   extern long  ftell();
115  
110 extern char  *malloc(), *calloc();
111
116   Display  *thedisplay;
117 + Atom  closedownAtom, wmProtocolsAtom;
118  
119  
120   main(argc, argv)
# Line 187 | Line 192 | char  *argv[];
192          if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
193                  quiterr("out of memory");
194  
195 <        init();                 /* get file and open window */
195 >        init(argc, argv);                       /* get file and open window */
196  
197          for ( ; ; )
198                  getevent();             /* main loop */
199   userr:
200          fprintf(stderr,
201 <        "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
201 > "Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n",
202                          progname);
203 <        quit(1);
203 >        exit(1);
204   }
205  
206  
# Line 214 | Line 219 | char  *s;
219   }
220  
221  
222 < init()                  /* get data and open window */
222 > init(argc, argv)                        /* get data and open window */
223 > int argc;
224 > char **argv;
225   {
219        XWMHints        ourxwmhints;
226          XSetWindowAttributes    ourwinattr;
227 <        XSizeHints      oursizhints;
227 >        XClassHint      xclshints;
228 >        XWMHints        xwmhints;
229 >        XSizeHints      xszhints;
230 >        XTextProperty   windowName, iconName;
231 >        XGCValues       xgcv;
232 >        char    *name;
233          register int    i;
234          
235          if (fname != NULL) {
236                  scanpos = (long *)malloc(ymax*sizeof(long));
237                  if (scanpos == NULL)
238 <                        goto memerr;
238 >                        quiterr("out of memory");
239                  for (i = 0; i < ymax; i++)
240                          scanpos[i] = -1;
241 <        }
241 >                name = fname;
242 >        } else
243 >                name = progname;
244 >                                /* remove directory prefix from name */
245 >        for (i = strlen(name); i-- > 0; )
246 >                if (name[i] == '/')
247 >                        break;
248 >        name += i+1;
249          if ((thedisplay = XOpenDisplay(dispname)) == NULL)
250                  quiterr("cannot open display");
251                                  /* get best visual for default screen */
# Line 235 | Line 253 | init()                 /* get data and open window */
253                                  /* store image */
254          getras();
255                                  /* get size and position */
256 <        bzero((char *)&oursizhints, sizeof(oursizhints));
257 <        oursizhints.width = xmax; oursizhints.height = ymax;
256 >        xszhints.flags = 0;
257 >        xszhints.width = xmax; xszhints.height = ymax;
258          if (geometry != NULL) {
259 <                i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
260 <                                (unsigned *)&oursizhints.width,
261 <                                (unsigned *)&oursizhints.height);
259 >                i = XParseGeometry(geometry, &xszhints.x, &xszhints.y,
260 >                                (unsigned *)&xszhints.width,
261 >                                (unsigned *)&xszhints.height);
262                  if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
263 <                        oursizhints.flags |= USSize;
263 >                        xszhints.flags |= USSize;
264                  else
265 <                        oursizhints.flags |= PSize;
265 >                        xszhints.flags |= PSize;
266                  if ((i&(XValue|YValue)) == (XValue|YValue)) {
267 <                        oursizhints.flags |= USPosition;
267 >                        xszhints.flags |= USPosition;
268                          if (i & XNegative)
269 <                                oursizhints.x += DisplayWidth(thedisplay,
270 <                                ourscreen)-1-oursizhints.width-2*BORWIDTH;
269 >                                xszhints.x += DisplayWidth(thedisplay,
270 >                                ourscreen)-1-xszhints.width-2*BORWIDTH;
271                          if (i & YNegative)
272 <                                oursizhints.y += DisplayHeight(thedisplay,
273 <                                ourscreen)-1-oursizhints.height-2*BORWIDTH;
272 >                                xszhints.y += DisplayHeight(thedisplay,
273 >                                ourscreen)-1-xszhints.height-2*BORWIDTH;
274                  }
275          }
276 <                                /* open window */
277 <        ourwinattr.border_pixel = ourblack;
278 <        ourwinattr.background_pixel = ourwhite;
276 >        /* open window */
277 >        ourwinattr.border_pixel = ourwhite;
278 >        ourwinattr.background_pixel = ourblack;
279          ourwinattr.colormap = XCreateColormap(thedisplay, ourroot,
280                          ourvis.visual, AllocNone);
281 <        wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y,
282 <                        oursizhints.width, oursizhints.height, BORWIDTH,
283 <                        ourvis.depth, InputOutput, ourvis.visual,
284 <                        CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
281 >        ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask|
282 >                        ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask;
283 >        ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross);
284 >        wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y,
285 >                        xszhints.width, xszhints.height, BORWIDTH,
286 >                        ourvis.depth, InputOutput, ourvis.visual, CWEventMask|
287 >                        CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr);
288          if (wind == 0)
289                  quiterr("cannot create window");
269        XFreeColormap(thedisplay, ourwinattr.colormap);
290          width = xmax;
291          height = ymax;
292 <        ourgc = XCreateGC(thedisplay, wind, 0, 0);
293 <        XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes);
294 <        revgc = XCreateGC(thedisplay, wind, 0, 0);
275 <        XSetFunction(thedisplay, revgc, GXinvert);
276 <        fontid = XLoadFont(thedisplay, FONTNAME);
277 <        if (fontid == 0)
292 >        xgcv.foreground = ourblack;
293 >        xgcv.background = ourwhite;
294 >        if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0)
295                  quiterr("cannot get font");
296 <        XSetFont(thedisplay, ourgc, fontid);
297 <        XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
298 <                        XC_diamond_cross));
299 <        XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
300 <        if (oursizhints.flags)
301 <                XSetNormalHints(thedisplay, wind, &oursizhints);
302 <        ourxwmhints.flags = InputHint|IconPixmapHint;
303 <        ourxwmhints.input = True;
304 <        ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
296 >        ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
297 >                        GCFont, &xgcv);
298 >        xgcv.function = GXinvert;
299 >        revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground|
300 >                        GCFunction, &xgcv);
301 >
302 >        /* set up the window manager */
303 >        xwmhints.flags = InputHint|IconPixmapHint;
304 >        xwmhints.input = True;
305 >        xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay,
306                          wind, icondata, iconwidth, iconheight);
307 <        XSetWMHints(thedisplay, wind, &ourxwmhints);
308 <        XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
309 <                        |ButtonMotionMask|StructureNotifyMask
310 <                        |KeyPressMask|ExposureMask);
307 >
308 >        windowName.encoding = iconName.encoding = XA_STRING;
309 >        windowName.format = iconName.format = 8;
310 >        windowName.value = (u_char *)name;
311 >        windowName.nitems = strlen(windowName.value);
312 >        iconName.value = (u_char *)name;
313 >        iconName.nitems = strlen(windowName.value);
314 >
315 >        xclshints.res_name = NULL;
316 >        xclshints.res_class = "Ximage";
317 >        XSetWMProperties(thedisplay, wind, &windowName, &iconName,
318 >                        argv, argc, &xszhints, &xwmhints, &xclshints);
319 >        closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False);
320 >        wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False);
321 >        XSetWMProtocols(thedisplay, wind, &closedownAtom, 1);
322 >
323          XMapWindow(thedisplay, wind);
324          return;
295 memerr:
296        quiterr("out of memory");
325   } /* end of init */
326  
327  
# Line 445 | Line 473 | getras()                               /* get raster file */
473                          goto fail;
474                  getmono();
475          } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) {
476 <                ourdata = (unsigned char *)malloc(4*xmax*ymax);
476 >                ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax);
477                  if (ourdata == NULL)
478                          goto fail;
479 <                ourras = make_raster(thedisplay, &ourvis, 32,
479 >                ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8,
480                                  ourdata, xmax, ymax, 32);
481                  if (ourras == NULL)
482                          goto fail;
# Line 476 | Line 504 | fail:
504  
505   getevent()                              /* process the next event */
506   {
507 <        union {
480 <                XEvent  u;
481 <                XConfigureEvent  c;
482 <                XExposeEvent  e;
483 <                XButtonPressedEvent  b;
484 <                XKeyPressedEvent  k;
485 <        } e;
507 >        XEvent xev;
508  
509 <        XNextEvent(thedisplay, &e.u);
510 <        switch (e.u.type) {
509 >        XNextEvent(thedisplay, &xev);
510 >        switch ((int)xev.type) {
511          case KeyPress:
512 <                docom(&e.k);
512 >                docom(&xev.xkey);
513                  break;
514          case ConfigureNotify:
515 <                width = e.c.width;
516 <                height = e.c.height;
515 >                width = xev.xconfigure.width;
516 >                height = xev.xconfigure.height;
517                  break;
518          case MapNotify:
519                  map_rcolors(ourras, wind);
# Line 503 | Line 525 | getevent()                             /* process the next event */
525                          unmap_rcolors(ourras);
526                  break;
527          case Expose:
528 <                redraw(e.e.x, e.e.y, e.e.width, e.e.height);
528 >                redraw(xev.xexpose.x, xev.xexpose.y,
529 >                                xev.xexpose.width, xev.xexpose.height);
530                  break;
531          case ButtonPress:
532 <                if (e.b.state & (ShiftMask|ControlMask))
533 <                        moveimage(&e.b);
532 >                if (xev.xbutton.state & (ShiftMask|ControlMask))
533 >                        moveimage(&xev.xbutton);
534 >                else if (xev.xbutton.button == Button2)
535 >                        traceray(xev.xbutton.x, xev.xbutton.y);
536                  else
537 <                        getbox(&e.b);
537 >                        getbox(&xev.xbutton);
538                  break;
539 +   case ClientMessage:
540 +                if ((xev.xclient.message_type == wmProtocolsAtom) &&
541 +                                (xev.xclient.data.l[0] == closedownAtom))
542 +                        quiterr(NULL);
543 +                break;
544          }
545   }
546  
547  
548 < docom(ekey)                                     /* execute command */
548 > traceray(xpos, ypos)                    /* print ray corresponding to pixel */
549 > int  xpos, ypos;
550 > {
551 >        FLOAT  hv[2];
552 >        FVECT  rorg, rdir;
553 >
554 >        if (!gotview) {         /* no view, no can do */
555 >                XBell(thedisplay, 0);
556 >                return(-1);
557 >        }
558 >        pix2loc(hv, &inpres, xpos-xoff, ypos-yoff);
559 >        if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
560 >                return(-1);
561 >        printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
562 >        printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
563 >        fflush(stdout);
564 >        return(0);
565 > }
566 >
567 >
568 > docom(ekey)                             /* execute command */
569   XKeyPressedEvent  *ekey;
570   {
571          char  buf[80];
# Line 524 | Line 574 | XKeyPressedEvent  *ekey;
574          int  com, n;
575          double  comp;
576          FLOAT  hv[2];
527        FVECT  rorg, rdir;
577  
578          n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
579          if (n == 0)
# Line 532 | Line 581 | XKeyPressedEvent  *ekey;
581          com = buf[0];
582          switch (com) {                  /* interpret command */
583          case 'q':
584 +        case 'Q':
585          case CTRL('D'):                         /* quit */
586 <                quit(0);
586 >                quiterr(NULL);
587          case '\n':
588          case '\r':
589          case 'l':
# Line 579 | Line 629 | XKeyPressedEvent  *ekey;
629                                          buf, strlen(buf));
630                  return(0);
631          case 't':                               /* trace */
632 <                if (!gotview) {
583 <                        XBell(thedisplay, 0);
584 <                        return(-1);
585 <                }
586 <                pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff);
587 <                if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0)
588 <                        return(-1);
589 <                printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
590 <                printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
591 <                fflush(stdout);
592 <                return(0);
632 >                return(traceray(ekey->x, ekey->y));
633          case '=':                               /* adjust exposure */
634 +        case '@':                               /* adaptation level */
635                  if (avgbox(cval) == -1)
636                          return(-1);
637 <                n = log(.5/bright(cval))/.69315 - scale;        /* truncate */
637 >                comp = com=='@'
638 >                ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure
639 >                : .5/bright(cval) ;
640 >                comp = log(comp)/.69315 - scale;
641 >                n = comp < 0 ? comp-.5 : comp+.5 ;      /* round */
642                  if (n == 0)
643                          return(0);
644                  scale_rcolors(ourras, pow(2.0, (double)n));
# Line 615 | Line 660 | XKeyPressedEvent  *ekey;
660                          make_rpixmap(ourras, wind);
661                  redraw(0, 0, width, height);
662                  return(0);
663 +        case 'f':                               /* turn on fast redraw */
664 +                fast = 1;
665 +                make_rpixmap(ourras, wind);
666 +                return(0);
667 +        case 'F':                               /* turn off fast redraw */
668 +                fast = 0;
669 +                free_rpixmap(ourras);
670 +                return(0);
671          case '0':                               /* recenter origin */
672                  if (xoff == 0 & yoff == 0)
673                          return(0);
# Line 635 | Line 688 | XKeyPressedEvent  *ekey;
688   moveimage(ebut)                         /* shift the image */
689   XButtonPressedEvent  *ebut;
690   {
691 <        union {
639 <                XEvent  u;
640 <                XButtonReleasedEvent  b;
641 <                XPointerMovedEvent  m;
642 <        }  e;
691 >        XEvent  e;
692          int     mxo, myo;
693  
694 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
695 <        while (e.u.type == MotionNotify) {
696 <                mxo = e.m.x;
697 <                myo = e.m.y;
694 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
695 >        while (e.type == MotionNotify) {
696 >                mxo = e.xmotion.x;
697 >                myo = e.xmotion.y;
698                  revline(ebut->x, ebut->y, mxo, myo);
699                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
700                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
701 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
701 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
702                  revline(ebut->x, ebut->y, mxo, myo);
703                  revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y,
704                                  xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax);
705          }
706 <        xoff += e.b.x - ebut->x;
707 <        yoff += e.b.y - ebut->y;
706 >        xoff += e.xbutton.x - ebut->x;
707 >        yoff += e.xbutton.y - ebut->y;
708          XClearWindow(thedisplay, wind);
709          redraw(0, 0, width, height);
710   }
# Line 664 | Line 713 | XButtonPressedEvent  *ebut;
713   getbox(ebut)                            /* get new box */
714   XButtonPressedEvent  *ebut;
715   {
716 <        union {
668 <                XEvent  u;
669 <                XButtonReleasedEvent  b;
670 <                XPointerMovedEvent  m;
671 <        }  e;
716 >        XEvent  e;
717  
718 <        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
719 <        while (e.u.type == MotionNotify) {
720 <                revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
721 <                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
718 >        XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e);
719 >        while (e.type == MotionNotify) {
720 >                revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y);
721 >                XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e);
722                  revbox(ebut->x, ebut->y, box.xmin, box.ymin);
723          }
724 <        box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
725 <        box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
724 >        box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x);
725 >        box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y);
726          if (box.xmin > ebut->x) {
727                  box.xsiz = box.xmin - ebut->x + 1;
728                  box.xmin = ebut->x;
# Line 838 | Line 883 | COLR  *scan;
883   getfull()                       /* get full (24-bit) data */
884   {
885          int     y;
886 <        register unsigned long  *dp;
886 >        register unsigned int4  *dp;
887          register int    x;
888                                          /* set gamma correction */
889          setcolrgam(gamcor);
890                                          /* read and convert file */
891 <        dp = (unsigned long *)ourdata;
891 >        dp = (unsigned int4 *)ourdata;
892          for (y = 0; y < ymax; y++) {
893                  getscan(y);
894                  add2icon(y, scanline);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines