--- ray/src/px/x11image.c 1992/10/12 12:59:22 2.13 +++ ray/src/px/x11image.c 1993/03/02 10:54:08 2.22 @@ -109,10 +109,9 @@ extern long ftell(); extern char *malloc(), *calloc(); -extern double pow(), log(); - Display *thedisplay; + main(argc, argv) int argc; char *argv[]; @@ -194,9 +193,9 @@ char *argv[]; 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); } @@ -235,12 +234,34 @@ init() /* get data and open window */ getbestvis(); /* store image */ getras(); + /* get size and position */ + bzero((char *)&oursizhints, sizeof(oursizhints)); + oursizhints.width = xmax; oursizhints.height = ymax; + if (geometry != NULL) { + i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, + (unsigned *)&oursizhints.width, + (unsigned *)&oursizhints.height); + if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) + oursizhints.flags |= USSize; + else + oursizhints.flags |= PSize; + if ((i&(XValue|YValue)) == (XValue|YValue)) { + oursizhints.flags |= USPosition; + if (i & XNegative) + oursizhints.x += DisplayWidth(thedisplay, + ourscreen)-1-oursizhints.width-2*BORWIDTH; + if (i & YNegative) + oursizhints.y += DisplayHeight(thedisplay, + ourscreen)-1-oursizhints.height-2*BORWIDTH; + } + } /* 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, + wind = XCreateWindow(thedisplay, ourroot, oursizhints.x, oursizhints.y, + oursizhints.width, oursizhints.height, BORWIDTH, ourvis.depth, InputOutput, ourvis.visual, CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); if (wind == 0) @@ -259,29 +280,8 @@ init() /* get data and open window */ XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, XC_diamond_cross)); XStoreName(thedisplay, wind, fname == NULL ? progname : fname); - if (geometry != NULL) { - bzero((char *)&oursizhints, sizeof(oursizhints)); - i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, - (unsigned *)&oursizhints.width, - (unsigned *)&oursizhints.height); - if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) - oursizhints.flags |= USSize; - else { - oursizhints.width = xmax; - oursizhints.height = ymax; - oursizhints.flags |= PSize; - } - if ((i&(XValue|YValue)) == (XValue|YValue)) { - oursizhints.flags |= USPosition; - if (i & XNegative) - oursizhints.x += DisplayWidth(thedisplay, - ourscreen)-1-oursizhints.width-2*BORWIDTH; - if (i & YNegative) - oursizhints.y += DisplayHeight(thedisplay, - ourscreen)-1-oursizhints.height-2*BORWIDTH; - } + if (oursizhints.flags) XSetNormalHints(thedisplay, wind, &oursizhints); - } ourxwmhints.flags = InputHint|IconPixmapHint; ourxwmhints.input = True; ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, @@ -308,20 +308,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; @@ -522,6 +508,8 @@ getevent() /* process the next event */ case ButtonPress: if (e.b.state & (ShiftMask|ControlMask)) moveimage(&e.b); + else if (e.b.button == Button2) + traceray(e.b.x, e.b.y); else getbox(&e.b); break; @@ -529,7 +517,27 @@ getevent() /* process the next event */ } -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 +546,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 +553,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,17 +601,7 @@ 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 */ if (avgbox(cval) == -1) return(-1); @@ -629,6 +627,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); @@ -740,7 +745,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 +774,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 +802,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 +833,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 +857,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 +886,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 +917,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 */