--- ray/src/px/x11image.c 1990/08/30 11:21:34 1.7 +++ ray/src/px/x11image.c 1991/11/11 14:01:17 1.26 @@ -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,6 +30,7 @@ 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 */ @@ -37,6 +38,8 @@ static char SCCSid[] = "$SunId$ LBL"; #define BORWIDTH 5 /* border width */ +#define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ + #define ourscreen DefaultScreen(thedisplay) #define ourblack BlackPixel(thedisplay,ourscreen) #define ourwhite WhitePixel(thedisplay,ourscreen) @@ -68,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 */ @@ -77,6 +81,8 @@ int cury = 0; /* current scan location */ double exposure = 1.0; /* exposure compensation used */ +int wrongformat = 0; /* input in another format? */ + GC revgc; /* graphics context with GXinvert */ XRASTER *ourras; /* our stored image */ @@ -88,6 +94,9 @@ struct { char *geometry = NULL; /* geometry specification */ +char icondata[ICONSIZ*ICONSIZ/8]; /* icon bitmap data */ +int iconwidth = 0, iconheight = 0; + char *progname; char errmsg[128]; @@ -156,10 +165,12 @@ char *argv[]; } else if (i != argc) goto userr; /* get header */ - getheader(fin, headline); + getheader(fin, headline, NULL); /* get picture dimensions */ - if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) - quiterr("bad picture size"); + 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; @@ -183,10 +194,14 @@ char *s; { static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; register char **an; + char fmt[32]; if (isexpos(s)) exposure *= exposval(s); - else + 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) @@ -198,6 +213,7 @@ char *s; init() /* get data and open window */ { + XWMHints ourxwmhints; XSetWindowAttributes ourwinattr; XSizeHints oursizhints; register int i; @@ -234,13 +250,14 @@ init() /* get data and open window */ XSetFont(thedisplay, ourgc, fontid); revgc = XCreateGC(thedisplay, wind, 0, 0); XSetFunction(thedisplay, revgc, GXinvert); - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); 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, - &oursizhints.width, &oursizhints.height); + (unsigned *)&oursizhints.width, + (unsigned *)&oursizhints.height); if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) oursizhints.flags |= USSize; else { @@ -259,6 +276,11 @@ init() /* get data and open window */ } XSetNormalHints(thedisplay, wind, &oursizhints); } + ourxwmhints.flags = InputHint|IconPixmapHint; + ourxwmhints.input = True; + ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, + wind, icondata, iconwidth, iconheight); + XSetWMHints(thedisplay, wind, &ourxwmhints); XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |ButtonMotionMask|StructureNotifyMask |KeyPressMask|ExposureMask); @@ -336,7 +358,7 @@ getras() /* get raster file */ } return; fail: - quit("could not create raster image"); + quiterr("could not create raster image"); } @@ -388,6 +410,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; + FLOAT hv[2]; FVECT rorg, rdir; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); @@ -410,7 +433,7 @@ XKeyPressedEvent *ekey; sprintf(buf, "%.3f", intens(cval)/exposure); break; case 'l': /* luminance */ - sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); + sprintf(buf, "%.0fL", luminance(cval)/exposure); break; case 'c': /* color */ comp = pow(2.0, (double)scale); @@ -436,7 +459,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); @@ -445,9 +470,9 @@ XKeyPressedEvent *ekey; XBell(thedisplay, 0); return(-1); } - viewray(rorg, rdir, &ourview, - (ekey->x-xoff+.5)/xmax, - (ymax-1-ekey->y+yoff+.5)/ymax); + 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); @@ -561,6 +586,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; @@ -584,6 +611,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); @@ -594,6 +625,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); } @@ -602,39 +635,106 @@ getmono() /* get monochrome data */ { register unsigned char *dp; register int x, err; - int y; - rgbpixel *inl; + int y, errp; short *cerr; - if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL - || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) - quit("out of memory in getmono"); + if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) + quiterr("out of memory in getmono"); dp = ourdata - 1; for (y = 0; y < ymax; y++) { - picreadline3(y, inl); + if (getscan(y) < 0) + quiterr("seek error in getmono"); + normcolrs(scanline, xmax, scale); + add2icon(y, scanline); err = 0; for (x = 0; x < xmax; x++) { if (!(x&7)) *++dp = 0; - err += rgb_bright(&inl[x]) + cerr[x]; + 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 *)inl); free((char *)cerr); } +add2icon(y, scan) /* add a scanline to our icon data */ +int y; +COLR *scan; +{ + static short cerr[ICONSIZ]; + static int ynext; + static char *dp; + register int err; + register int x, ti; + int errp; + + if (iconheight == 0) { /* initialize */ + if (xmax <= ICONSIZ && ymax <= ICONSIZ) { + iconwidth = xmax; + iconheight = ymax; + } 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 (y < ynext*ymax/iconheight) /* skip this one */ + return; + err = 0; + for (x = 0; x < iconwidth; x++) { + if (!(x&7)) + *++dp = 0; + errp = err; + ti = x*xmax/iconwidth; + err += normbright(scan[ti]) + cerr[x]; + if (err > 127) + err -= 255; + else + *dp |= 1<<(x&07); + err /= 3; + cerr[x] = err + errp; + } + ynext++; +} + + getfull() /* get full (24-bit) data */ { int y; - - for (y = 0; y < ymax; y++) - picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); + register unsigned char *dp; + register int x; + /* set gamma correction */ + setcolrgam(gamcor); + /* read and convert file */ + dp = ourdata; + for (y = 0; y < ymax; y++) { + if (getscan(y) < 0) + quiterr("seek error in getfull"); + if (scale) + 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]; + *dp++ = scanline[x][BLU]; + } + } } @@ -673,7 +773,7 @@ int y; if (scanpos == NULL || scanpos[y] == -1) return(-1); if (fseek(fin, scanpos[y], 0) == -1) - quit("fseek error"); + quiterr("fseek error"); cury = y; } else if (scanpos != NULL) scanpos[y] = ftell(fin); @@ -696,6 +796,7 @@ register rgbpixel *l3; quiterr("cannot seek for picreadline"); /* convert scanline */ normcolrs(scanline, xmax, scale); + add2icon(y, scanline); for (i = 0; i < xmax; i++) { l3[i].r = scanline[i][RED]; l3[i].g = scanline[i][GRN]; @@ -719,7 +820,7 @@ colormap map; register int i, val; for (i = 0; i < 256; i++) { - val = pow(i/256.0, 1.0/gamcor) * 256.0; + val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; map[0][i] = map[1][i] = map[2][i] = val; } }