--- ray/src/px/x11image.c 1991/05/03 09:54:31 1.19 +++ ray/src/px/x11image.c 1992/05/19 14:22:46 2.5 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -30,10 +30,11 @@ static char SCCSid[] = "$SunId$ LBL"; #include "pic.h" #include "x11raster.h" #include "random.h" +#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ -#define CTRL(c) ('c'-'@') +#define CTRL(c) ((c)-'@') #define BORWIDTH 5 /* border width */ @@ -70,7 +71,8 @@ int gotview = 0; /* got parameters from file */ COLR *scanline; /* scan line buffer */ -int xmax, ymax; /* picture resolution */ +RESOLU inpres; /* input resolution and ordering */ +int xmax, ymax; /* picture dimensions */ int width, height; /* window size */ char *fname = NULL; /* input file name */ FILE *fin = stdin; /* input file */ @@ -103,7 +105,7 @@ extern long ftell(); extern char *malloc(), *calloc(); -extern double atof(), pow(), log(); +extern double pow(), log(); Display *thedisplay; @@ -165,8 +167,10 @@ char *argv[]; /* get header */ getheader(fin, headline, NULL); /* get picture dimensions */ - if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) + if (wrongformat || !fgetsresolu(&inpres, fin)) quiterr("bad picture format"); + xmax = scanlen(&inpres); + ymax = numscans(&inpres); /* set view parameters */ if (gotview && setview(&ourview) != NULL) gotview = 0; @@ -188,8 +192,6 @@ userr: headline(s) /* get relevant info from header */ char *s; { - static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; - register char **an; char fmt[32]; if (isexpos(s)) @@ -197,13 +199,8 @@ char *s; else if (isformat(s)) { formatval(fmt, s); wrongformat = strcmp(fmt, COLRFMT); - } else - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) > 0) - gotview++; - return; - } + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; } @@ -248,6 +245,7 @@ init() /* get data and open window */ XSetFunction(thedisplay, revgc, GXinvert); 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, @@ -280,8 +278,6 @@ init() /* get data and open window */ |ButtonMotionMask|StructureNotifyMask |KeyPressMask|ExposureMask); XMapWindow(thedisplay, wind); - /* store name last as ready signal */ - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); return; memerr: quiterr("out of memory"); @@ -407,6 +403,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; + FLOAT hv[2]; FVECT rorg, rdir; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); @@ -415,7 +412,7 @@ XKeyPressedEvent *ekey; com = buf[0]; switch (com) { /* interpret command */ case 'q': - case CTRL(D): /* quit */ + case CTRL('D'): /* quit */ quit(0); case '\n': case '\r': @@ -455,7 +452,9 @@ XKeyPressedEvent *ekey; XStoreColor(thedisplay, ourras->cmap, &cvx); return(0); case 'p': /* position */ - sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); + pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); + sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr), + (int)(hv[1]*inpres.yr)); XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, buf, strlen(buf)); return(0); @@ -464,9 +463,8 @@ XKeyPressedEvent *ekey; XBell(thedisplay, 0); return(-1); } - if (viewray(rorg, rdir, &ourview, - (ekey->x-xoff+.5)/xmax, - (ymax-1-ekey->y+yoff+.5)/ymax) < 0) + 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]); @@ -488,8 +486,8 @@ XKeyPressedEvent *ekey; free_raster(ourras); getras(); /* fall through */ - case CTRL(R): /* redraw */ - case CTRL(L): + case CTRL('R'): /* redraw */ + case CTRL('L'): unmap_rcolors(ourras); XClearWindow(thedisplay, wind); map_rcolors(ourras, wind); @@ -581,6 +579,8 @@ int x0, y0, x1, y1; avgbox(clr) /* average color over current box */ COLOR clr; { + static COLOR lc; + static int ll, lr, lt, lb; int left, right, top, bottom; int y; double d; @@ -604,6 +604,10 @@ COLOR clr; bottom = ymax; if (top >= bottom) return(-1); + if (left == ll && right == lr && top == lt && bottom == lb) { + copycolor(clr, lc); + return; + } for (y = top; y < bottom; y++) { if (getscan(y) == -1) return(-1); @@ -614,6 +618,8 @@ COLOR clr; } d = 1.0/((right-left)*(bottom-top)); scalecolor(clr, d); + ll = left; lr = right; lt = top; lb = bottom; + copycolor(lc, clr); return(0); } @@ -622,7 +628,7 @@ getmono() /* get monochrome data */ { register unsigned char *dp; register int x, err; - int y; + int y, errp; short *cerr; if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) @@ -637,12 +643,14 @@ getmono() /* get monochrome data */ for (x = 0; x < xmax; x++) { if (!(x&7)) *++dp = 0; + errp = err; err += normbright(scanline[x]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(7-(x&07)); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } } free((char *)cerr); @@ -653,10 +661,12 @@ add2icon(y, scan) /* add a scanline to our icon data int y; COLR *scan; { - static char *dp = NULL; static short cerr[ICONSIZ]; + static int ynext; + static char *dp; register int err; - register int x, xi; + register int x, ti; + int errp; if (iconheight == 0) { /* initialize */ if (xmax <= ICONSIZ && ymax <= ICONSIZ) { @@ -665,30 +675,34 @@ COLR *scan; } else if (xmax > ymax) { iconwidth = ICONSIZ; iconheight = ICONSIZ*ymax/xmax; + if (iconheight < 1) + iconheight = 1; } else { iconwidth = ICONSIZ*xmax/ymax; + if (iconwidth < 1) + iconwidth = 1; iconheight = ICONSIZ; } + ynext = 0; dp = icondata - 1; } - if (dp == NULL) /* done already */ + if (y < ynext*ymax/iconheight) /* skip this one */ return; - if (y % (ymax/iconheight)) /* skip this one */ - return; err = 0; for (x = 0; x < iconwidth; x++) { if (!(x&7)) *++dp = 0; - xi = x*xmax/iconwidth; - err += normbright(scan[xi]) + cerr[x]; + errp = err; + ti = x*xmax/iconwidth; + err += normbright(scan[ti]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(x&07); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } - if (y >= ymax - ymax/iconheight) /* all done */ - dp = NULL; + ynext++; } @@ -708,10 +722,10 @@ getfull() /* get full (24-bit) data */ shiftcolrs(scanline, xmax, scale); colrs_gambs(scanline, xmax); add2icon(y, scanline); - for (x = 0; x < xmax; x++) { - *dp++ = scanline[x][RED]; - *dp++ = scanline[x][GRN]; + for (x = 0; x < xmax; x++) { /* BGR byte ordering */ *dp++ = scanline[x][BLU]; + *dp++ = scanline[x][GRN]; + *dp++ = scanline[x][RED]; } } }