--- ray/src/px/x11image.c 1992/10/12 12:59:22 2.13 +++ ray/src/px/x11image.c 1993/05/05 10:27:58 2.26 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1993 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -24,6 +24,7 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include #include +#include #include "color.h" #include "view.h" @@ -55,8 +56,6 @@ char *dispname = NULL; /* our display name */ Window wind = 0; /* our output window */ unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ -Font fontid; /* our font */ - int maxcolors = 0; /* maximum colors */ int greyscale = 0; /* in grey */ @@ -107,12 +106,10 @@ extern BYTE clrtab[256][3]; /* global color map */ extern long ftell(); -extern char *malloc(), *calloc(); - -extern double pow(), log(); - Display *thedisplay; +Atom closedownAtom, wmProtocolsAtom; + main(argc, argv) int argc; char *argv[]; @@ -188,15 +185,15 @@ char *argv[]; if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) quiterr("out of memory"); - init(); /* get file and open window */ + init(argc, argv); /* get file and open window */ for ( ; ; ) getevent(); /* main loop */ userr: fprintf(stderr, - "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", +"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", progname); - quit(1); + exit(1); } @@ -215,85 +212,109 @@ char *s; } -init() /* get data and open window */ +init(argc, argv) /* get data and open window */ +int argc; +char **argv; { - XWMHints ourxwmhints; XSetWindowAttributes ourwinattr; - XSizeHints oursizhints; + XClassHint xclshints; + XWMHints xwmhints; + XSizeHints xszhints; + XTextProperty windowName, iconName; + XGCValues xgcv; + char *name; register int i; if (fname != NULL) { scanpos = (long *)malloc(ymax*sizeof(long)); if (scanpos == NULL) - goto memerr; + quiterr("out of memory"); for (i = 0; i < ymax; i++) scanpos[i] = -1; - } + name = fname; + } else + name = progname; + /* remove directory prefix from name */ + for (i = strlen(name); i-- > 0; ) + if (name[i] == '/') + break; + name += i+1; if ((thedisplay = XOpenDisplay(dispname)) == NULL) quiterr("cannot open display"); /* get best visual for default screen */ getbestvis(); /* store image */ getras(); - /* open window */ - ourwinattr.border_pixel = ourblack; - ourwinattr.background_pixel = ourwhite; - ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, - ourvis.visual, AllocNone); - wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, - ourvis.depth, InputOutput, ourvis.visual, - CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); - if (wind == 0) - quiterr("cannot create window"); - XFreeColormap(thedisplay, ourwinattr.colormap); - width = xmax; - height = ymax; - ourgc = XCreateGC(thedisplay, wind, 0, 0); - XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); - revgc = XCreateGC(thedisplay, wind, 0, 0); - XSetFunction(thedisplay, revgc, GXinvert); - fontid = XLoadFont(thedisplay, FONTNAME); - if (fontid == 0) - quiterr("cannot get font"); - XSetFont(thedisplay, ourgc, fontid); - XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, - XC_diamond_cross)); - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); + /* get size and position */ + xszhints.flags = 0; + xszhints.width = xmax; xszhints.height = ymax; if (geometry != NULL) { - bzero((char *)&oursizhints, sizeof(oursizhints)); - i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, - (unsigned *)&oursizhints.width, - (unsigned *)&oursizhints.height); + i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, + (unsigned *)&xszhints.width, + (unsigned *)&xszhints.height); if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) - oursizhints.flags |= USSize; - else { - oursizhints.width = xmax; - oursizhints.height = ymax; - oursizhints.flags |= PSize; - } + xszhints.flags |= USSize; + else + xszhints.flags |= PSize; if ((i&(XValue|YValue)) == (XValue|YValue)) { - oursizhints.flags |= USPosition; + xszhints.flags |= USPosition; if (i & XNegative) - oursizhints.x += DisplayWidth(thedisplay, - ourscreen)-1-oursizhints.width-2*BORWIDTH; + xszhints.x += DisplayWidth(thedisplay, + ourscreen)-1-xszhints.width-2*BORWIDTH; if (i & YNegative) - oursizhints.y += DisplayHeight(thedisplay, - ourscreen)-1-oursizhints.height-2*BORWIDTH; + xszhints.y += DisplayHeight(thedisplay, + ourscreen)-1-xszhints.height-2*BORWIDTH; } - XSetNormalHints(thedisplay, wind, &oursizhints); } - ourxwmhints.flags = InputHint|IconPixmapHint; - ourxwmhints.input = True; - ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, + /* open window */ + ourwinattr.border_pixel = ourwhite; + ourwinattr.background_pixel = ourblack; + ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, + ourvis.visual, AllocNone); + ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| + ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; + ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); + wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, + xszhints.width, xszhints.height, BORWIDTH, + ourvis.depth, InputOutput, ourvis.visual, CWEventMask| + CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); + if (wind == 0) + quiterr("cannot create window"); + width = xmax; + height = ymax; + xgcv.foreground = ourblack; + xgcv.background = ourwhite; + if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) + quiterr("cannot get font"); + ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| + GCFont, &xgcv); + xgcv.function = GXinvert; + revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| + GCFunction, &xgcv); + + /* set up the window manager */ + xwmhints.flags = InputHint|IconPixmapHint; + xwmhints.input = True; + xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, wind, icondata, iconwidth, iconheight); - XSetWMHints(thedisplay, wind, &ourxwmhints); - XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask - |ButtonMotionMask|StructureNotifyMask - |KeyPressMask|ExposureMask); + + windowName.encoding = iconName.encoding = XA_STRING; + windowName.format = iconName.format = 8; + windowName.value = (u_char *)name; + windowName.nitems = strlen(windowName.value); + iconName.value = (u_char *)name; + iconName.nitems = strlen(windowName.value); + + xclshints.res_name = NULL; + xclshints.res_class = "Ximage"; + XSetWMProperties(thedisplay, wind, &windowName, &iconName, + argv, argc, &xszhints, &xwmhints, &xclshints); + closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); + wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); + XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); + XMapWindow(thedisplay, wind); return; -memerr: - quiterr("out of memory"); } /* end of init */ @@ -308,20 +329,6 @@ char *err; } -eputs(s) -char *s; -{ - fputs(s, stderr); -} - - -quit(code) -int code; -{ - exit(code); -} - - static int viscmp(v1,v2) /* compare visual to see which is better, descending */ register XVisualInfo *v1, *v2; @@ -490,22 +497,16 @@ fail: getevent() /* process the next event */ { - union { - XEvent u; - XConfigureEvent c; - XExposeEvent e; - XButtonPressedEvent b; - XKeyPressedEvent k; - } e; + XEvent xev; - XNextEvent(thedisplay, &e.u); - switch (e.u.type) { + XNextEvent(thedisplay, &xev); + switch ((int)xev.type) { case KeyPress: - docom(&e.k); + docom(&xev.xkey); break; case ConfigureNotify: - width = e.c.width; - height = e.c.height; + width = xev.xconfigure.width; + height = xev.xconfigure.height; break; case MapNotify: map_rcolors(ourras, wind); @@ -517,19 +518,47 @@ getevent() /* process the next event */ unmap_rcolors(ourras); break; case Expose: - redraw(e.e.x, e.e.y, e.e.width, e.e.height); + redraw(xev.xexpose.x, xev.xexpose.y, + xev.xexpose.width, xev.xexpose.height); break; case ButtonPress: - if (e.b.state & (ShiftMask|ControlMask)) - moveimage(&e.b); + if (xev.xbutton.state & (ShiftMask|ControlMask)) + moveimage(&xev.xbutton); + else if (xev.xbutton.button == Button2) + traceray(xev.xbutton.x, xev.xbutton.y); else - getbox(&e.b); + getbox(&xev.xbutton); break; + case ClientMessage: + if ((xev.xclient.message_type == wmProtocolsAtom) && + (xev.xclient.data.l[0] == closedownAtom)) + quiterr(NULL); + break; } } -docom(ekey) /* execute command */ +traceray(xpos, ypos) /* print ray corresponding to pixel */ +int xpos, ypos; +{ + FLOAT hv[2]; + FVECT rorg, rdir; + + if (!gotview) { /* no view, no can do */ + XBell(thedisplay, 0); + return(-1); + } + pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); + if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) + return(-1); + printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); + printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); + fflush(stdout); + return(0); +} + + +docom(ekey) /* execute command */ XKeyPressedEvent *ekey; { char buf[80]; @@ -538,7 +567,6 @@ XKeyPressedEvent *ekey; int com, n; double comp; FLOAT hv[2]; - FVECT rorg, rdir; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); if (n == 0) @@ -546,8 +574,9 @@ XKeyPressedEvent *ekey; com = buf[0]; switch (com) { /* interpret command */ case 'q': + case 'Q': case CTRL('D'): /* quit */ - quit(0); + quiterr(NULL); case '\n': case '\r': case 'l': @@ -593,21 +622,16 @@ XKeyPressedEvent *ekey; buf, strlen(buf)); return(0); case 't': /* trace */ - if (!gotview) { - XBell(thedisplay, 0); - return(-1); - } - pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); - if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) - return(-1); - printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); - printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); - fflush(stdout); - return(0); + return(traceray(ekey->x, ekey->y)); case '=': /* adjust exposure */ + case '@': /* adaptation level */ if (avgbox(cval) == -1) return(-1); - n = log(.5/bright(cval))/.69315 - scale; /* truncate */ + comp = com=='@' + ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure + : .5/bright(cval) ; + comp = log(comp)/.69315 - scale; + n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ if (n == 0) return(0); scale_rcolors(ourras, pow(2.0, (double)n)); @@ -629,6 +653,13 @@ XKeyPressedEvent *ekey; make_rpixmap(ourras, wind); redraw(0, 0, width, height); return(0); + case '0': /* recenter origin */ + if (xoff == 0 & yoff == 0) + return(0); + xoff = yoff = 0; + XClearWindow(thedisplay, wind); + redraw(0, 0, width, height); + return(0); case ' ': /* clear */ redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); return(0); @@ -642,27 +673,23 @@ XKeyPressedEvent *ekey; moveimage(ebut) /* shift the image */ XButtonPressedEvent *ebut; { - union { - XEvent u; - XButtonReleasedEvent b; - XPointerMovedEvent m; - } e; + XEvent e; int mxo, myo; - XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); - while (e.u.type == MotionNotify) { - mxo = e.m.x; - myo = e.m.y; + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); + while (e.type == MotionNotify) { + mxo = e.xmotion.x; + myo = e.xmotion.y; revline(ebut->x, ebut->y, mxo, myo); revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); - XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); revline(ebut->x, ebut->y, mxo, myo); revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); } - xoff += e.b.x - ebut->x; - yoff += e.b.y - ebut->y; + xoff += e.xbutton.x - ebut->x; + yoff += e.xbutton.y - ebut->y; XClearWindow(thedisplay, wind); redraw(0, 0, width, height); } @@ -671,20 +698,16 @@ XButtonPressedEvent *ebut; getbox(ebut) /* get new box */ XButtonPressedEvent *ebut; { - union { - XEvent u; - XButtonReleasedEvent b; - XPointerMovedEvent m; - } e; + XEvent e; - XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); - while (e.u.type == MotionNotify) { - revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); - XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); + while (e.type == MotionNotify) { + revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); revbox(ebut->x, ebut->y, box.xmin, box.ymin); } - box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); - box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); + box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); + box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); if (box.xmin > ebut->x) { box.xsiz = box.xmin - ebut->x + 1; box.xmin = ebut->x; @@ -740,7 +763,7 @@ COLOR clr; return(-1); if (left == ll && right == lr && top == lt && bottom == lb) { copycolor(clr, lc); - return; + return(0); } for (y = top; y < bottom; y++) { if (getscan(y) == -1) @@ -769,10 +792,9 @@ getmono() /* get monochrome data */ quiterr("out of memory in getmono"); dp = ourdata - 1; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getmono"); - normcolrs(scanline, xmax, scale); + getscan(y); add2icon(y, scanline); + normcolrs(scanline, xmax, scale); err = 0; for (x = 0; x < xmax; x++) { if (!(x&7)) @@ -798,6 +820,7 @@ COLR *scan; static short cerr[ICONSIZ]; static int ynext; static char *dp; + COLR clr; register int err; register int x, ti; int errp; @@ -828,7 +851,9 @@ COLR *scan; *++dp = 0; errp = err; ti = x*xmax/iconwidth; - err += normbright(scan[ti]) + cerr[x]; + copycolr(clr, scan[ti]); + normcolrs(clr, 1, scale); + err += normbright(clr) + cerr[x]; if (err > 127) err -= 255; else @@ -850,12 +875,11 @@ getfull() /* get full (24-bit) data */ /* read and convert file */ dp = (unsigned long *)ourdata; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getfull"); + getscan(y); + add2icon(y, scanline); if (scale) shiftcolrs(scanline, xmax, scale); colrs_gambs(scanline, xmax); - add2icon(y, scanline); if (ourras->image->blue_mask & 1) for (x = 0; x < xmax; x++) *dp++ = scanline[x][RED] << 16 | @@ -880,23 +904,24 @@ getgrey() /* get greyscale data */ /* read and convert file */ dp = ourdata; for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getgrey"); + getscan(y); + add2icon(y, scanline); if (scale) shiftcolrs(scanline, xmax, scale); + for (x = 0; x < xmax; x++) + scanline[x][GRN] = normbright(scanline[x]); colrs_gambs(scanline, xmax); - add2icon(y, scanline); if (maxcolors < 256) for (x = 0; x < xmax; x++) - *dp++ = ((long)normbright(scanline[x]) * - maxcolors + 128) >> 8; + *dp++ = ((long)scanline[x][GRN] * + maxcolors + maxcolors/2) >> 8; else for (x = 0; x < xmax; x++) - *dp++ = normbright(scanline[x]); + *dp++ = scanline[x][GRN]; } for (x = 0; x < maxcolors; x++) clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = (x*256+maxcolors/2)/maxcolors; + clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; } @@ -910,10 +935,10 @@ getmapped() /* get color-mapped data */ for (y = 0; y < ymax; y++) { if (getscan(y) < 0) quiterr("seek error in getmapped"); + add2icon(y, scanline); if (scale) shiftcolrs(scanline, xmax, scale); colrs_gambs(scanline, xmax); - add2icon(y, scanline); cnt_colrs(scanline, xmax); } /* map pixels */