--- ray/src/px/ximage.c 1989/02/02 10:49:43 1.1 +++ ray/src/px/ximage.c 2003/06/30 14:59:12 2.12 @@ -1,9 +1,6 @@ -/* Copyright (c) 1987 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ximage.c,v 2.12 2003/06/30 14:59:12 schorsch Exp $"; #endif - /* * ximage.c - driver for X-windows * @@ -18,29 +15,29 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include - +#include #include +#include #include "color.h" - +#include "resolu.h" #include "xraster.h" - #include "view.h" - #include "pic.h" +#include "random.h" #define controlshift(e) (((XButtonEvent *)(e))->detail & (ShiftMask|ControlMask)) #define FONTNAME "9x15" /* text font we'll use */ -#define CTRL(c) ('c'-'@') +#define CTRL(c) ((c)-'@') #define BORWIDTH 5 /* border width */ #define BARHEIGHT 25 /* menu bar size */ -double gamcor = 2.0; /* gamcor correction */ +double gamcor = 2.2; /* gamma correction */ -XRASTER ourras; /* our stored raster image */ +XRASTER *ourras = NULL; /* our stored raster image */ int dither = 1; /* dither colors? */ int fast = 0; /* keep picture in Pixmap? */ @@ -51,10 +48,12 @@ Font fontid; /* our font */ int maxcolors = 0; /* maximum colors */ int greyscale = 0; /* in grey */ +int scale = 0; /* scalefactor; power of two */ + int xoff = 0; /* x image offset */ int yoff = 0; /* y image offset */ -VIEW ourview = STDVIEW(0); /* image view parameters */ +VIEW ourview = STDVIEW; /* image view parameters */ int gotview = 0; /* got parameters from file */ COLR *scanline; /* scan line buffer */ @@ -68,6 +67,8 @@ int cury = 0; /* current scan location */ double exposure = 1.0; /* exposure compensation used */ +int wrongformat = 0; /* input in another format */ + struct { int xmin, ymin, xsiz, ysiz; } box = {0, 0, 0, 0}; /* current box */ @@ -78,21 +79,19 @@ char *progname; char errmsg[128]; -extern long ftell(); -extern char *malloc(), *calloc(); - -extern double atof(); - - main(argc, argv) int argc; char *argv[]; { + extern char *getenv(); + char *gv; int headline(); int i; progname = argv[0]; + if ((gv = getenv("DISPLAY_GAMMA")) != NULL) + gamcor = atof(gv); for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -112,6 +111,11 @@ char *argv[]; case 'f': fast = !fast; break; + case 'e': + if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + goto userr; + scale = atoi(argv[++i]); + break; case 'g': gamcor = atof(argv[++i]); break; @@ -130,21 +134,15 @@ char *argv[]; sprintf(errmsg, "can't open file \"%s\"", fname); quiterr(errmsg); } - } else - goto userr; - + } /* get header */ - getheader(fin, headline); + getheader(fin, headline, NULL); /* get picture dimensions */ - if (fscanf(fin, "-Y %d +X %d\n", &ymax, &xmax) != 2) + if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) quiterr("bad picture size"); /* set view parameters */ - if (gotview) { - ourview.hresolu = xmax; - ourview.vresolu = ymax; - if (setview(&ourview) != NULL) - gotview = 0; - } + if (gotview && setview(&ourview) != NULL) + gotview = 0; if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) quiterr("out of memory"); @@ -154,44 +152,32 @@ char *argv[]; getevent(); /* main loop */ userr: fprintf(stderr, - "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors] file\n", + "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", progname); quit(1); } +int headline(s) /* get relevant info from header */ char *s; { - static char *altname[] = {"rview","rpict","VIEW=",NULL}; - register char **an; + char fmt[32]; - if (!strncmp(s, "EXPOSURE=", 9)) - exposure *= atof(s+9); - else - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) == 0) - gotview++; - return; - } + if (isexpos(s)) + exposure *= exposval(s); + else if (isformat(s)) { + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; + return(0); } -char * -sskip(s) /* skip a word */ -register char *s; -{ - while (isspace(*s)) s++; - while (*s && !isspace(*s)) s++; - return(s); -} - - init() /* get data and open window */ { register int i; - colormap ourmap; OpaqueFrame mainframe; char defgeom[32]; @@ -213,25 +199,9 @@ init() /* get data and open window */ maxcolors = 1< 4) maxcolors -= 2; } - ourras.width = xmax; - ourras.height = ymax; /* store image */ - if (maxcolors <= 2) { /* monochrome */ - ourras.data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); - if (ourras.data.m == NULL) - goto memerr; - getmono(); - } else { - ourras.data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); - if (ourras.data.bz == NULL) - goto memerr; - if (greyscale) - biq(dither,maxcolors,1,ourmap); - else - ciq(dither,maxcolors,1,ourmap); - if (init_rcolors(&ourras, ourmap) == 0) - goto memerr; - } + getras(); + mainframe.bdrwidth = BORWIDTH; mainframe.border = WhitePixmap; mainframe.background = BlackPixmap; @@ -270,6 +240,7 @@ char *err; } +void eputs(s) char *s; { @@ -277,6 +248,7 @@ char *s; } +void quit(code) int code; { @@ -284,6 +256,37 @@ int code; } +getras() /* get raster file */ +{ + colormap ourmap; + + ourras = (XRASTER *)calloc(1, sizeof(XRASTER)); + if (ourras == NULL) + goto memerr; + ourras->width = xmax; + ourras->height = ymax; + if (maxcolors <= 2) { /* monochrome */ + ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax)); + if (ourras->data.m == NULL) + goto memerr; + getmono(); + } else { + ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax)); + if (ourras->data.bz == NULL) + goto memerr; + if (greyscale) + biq(dither,maxcolors,1,ourmap); + else + ciq(dither,maxcolors,1,ourmap); + if (init_rcolors(ourras, ourmap) == 0) + goto memerr; + } + return; +memerr: + quiterr("out of memory"); +} + + getevent() /* process the next event */ { WindowInfo info; @@ -303,7 +306,7 @@ getevent() /* process the next event */ fixwindow(&e); break; case UnmapWindow: - unmap_rcolors(&ourras); + unmap_rcolors(ourras); break; case ButtonPressed: if (controlshift(&e)) @@ -326,10 +329,13 @@ redraw(x, y, w, h) /* redraw section of window */ int x, y; int w, h; { - map_rcolors(&ourras); + if (ourras->ncolors && map_rcolors(ourras) == NULL) { + fprintf(stderr, "%s: cannot allocate colors\n", progname); + return(-1); + } if (fast) - make_rpixmap(&ourras); - return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,&ourras) ? 0 : -1); + make_rpixmap(ourras); + return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1); } @@ -338,8 +344,10 @@ XKeyEvent *ekey; { char buf[80]; COLOR cval; + Color cvx; char *cp; int n; + double comp; FVECT rorg, rdir; cp = XLookupMapping(ekey, &n); @@ -347,7 +355,7 @@ XKeyEvent *ekey; return(0); switch (*cp) { /* interpret command */ case 'q': - case CTRL(D): /* quiterr */ + case CTRL('D'): /* quit */ quit(0); case '\n': case '\r': @@ -358,21 +366,33 @@ XKeyEvent *ekey; switch (*cp) { case '\n': case '\r': /* radiance */ - sprintf(buf, "%-3g", intens(cval)/exposure); + sprintf(buf, "%.3f", intens(cval)/exposure); break; case 'l': /* luminance */ - sprintf(buf, "%-3gL", bright(cval)*683.0/exposure); + sprintf(buf, "%.0fL", luminance(cval)/exposure); break; case 'c': /* color */ + comp = pow(2.0, (double)scale); sprintf(buf, "(%.2f,%.2f,%.2f)", - colval(cval,RED), - colval(cval,GRN), - colval(cval,BLU)); + colval(cval,RED)*comp, + colval(cval,GRN)*comp, + colval(cval,BLU)*comp); break; } XText(wind, box.xmin, box.ymin, buf, strlen(buf), fontid, BlackPixel, WhitePixel); return(0); + case 'i': /* identify (contour) */ + if (ourras->pixels == NULL) + return(-1); + n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; + n = ourras->pmap[n]; + cvx.pixel = ourras->cdefs[n].pixel; + cvx.red = random() & 65535; + cvx.green = random() & 65535; + cvx.blue = random() & 65535; + XStoreColor(&cvx); + return(0); case 'p': /* position */ sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); XText(wind, ekey->x, ekey->y, buf, strlen(buf), @@ -383,13 +403,31 @@ XKeyEvent *ekey; XFeep(0); return(-1); } - rayview(rorg, rdir, &ourview, - ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5); + if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, + (ymax-1-ekey->y+yoff+.5)/ymax) < 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); - case CTRL(R): /* redraw */ + case '=': /* adjust exposure */ + if (avgbox(cval) == -1) + return(-1); + n = log(.5/bright(cval))/.69315 - scale; /* truncate */ + if (n == 0) + return(0); + scale_rcolors(ourras, pow(2.0, (double)n)); + scale += n; + sprintf(buf, "%+d", scale); + XText(wind, box.xmin, box.ymin, buf, strlen(buf), + fontid, BlackPixel, WhitePixel); + XFlush(); + free_raster(ourras); + getras(); + /* fall through */ + case CTRL('R'): /* redraw */ + case CTRL('L'): + unmap_rcolors(ourras); XClear(wind); return(redraw(0, 0, width, height)); case ' ': /* clear */ @@ -500,34 +538,36 @@ getmono() /* get monochrome data */ { register unsigned short *dp; register int x, err; - int y; - rgbpixel *inline; + int y, errp; + rgbpixel *inl; short *cerr; - if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL + if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) - quit("out of memory in getmono"); - dp = ourras.data.m - 1; + quiterr("out of memory in getmono"); + dp = ourras->data.m - 1; for (y = 0; y < ymax; y++) { - picreadline3(y, inline); + picreadline3(y, inl); err = 0; for (x = 0; x < xmax; x++) { if (!(x&0xf)) *++dp = 0; - err += rgb_bright(&inline[x]) + cerr[x]; + errp = err; + err += rgb_bright(&inl[x]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(x&0xf); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } } - free((char *)inline); - free((char *)cerr); + free((void *)inl); + free((void *)cerr); } -init_rcolors(xr, cmap) /* assign color values */ +init_rcolors(xr, cmap) /* (re)assign color values */ register XRASTER *xr; colormap cmap; { @@ -551,13 +591,41 @@ colormap cmap; xr->cdefs[xr->ncolors].pixel = *p; xr->pmap[*p] = xr->ncolors++; } - xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color)); + xr->cdefs = (Color *)realloc((void *)xr->cdefs, xr->ncolors*sizeof(Color)); if (xr->cdefs == NULL) return(0); return(1); } +scale_rcolors(xr, sf) /* scale color map */ +register XRASTER *xr; +double sf; +{ + register int i; + long maxv; + + if (xr->pixels == NULL) + return; + + sf = pow(sf, 1.0/gamcor); + maxv = 65535/sf; + + for (i = xr->ncolors; i--; ) { + xr->cdefs[i].red = xr->cdefs[i].red > maxv ? + 65535 : + xr->cdefs[i].red * sf; + xr->cdefs[i].green = xr->cdefs[i].green > maxv ? + 65535 : + xr->cdefs[i].green * sf; + xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? + 65535 : + xr->cdefs[i].blue * sf; + } + XStoreColors(xr->ncolors, xr->cdefs); +} + + getscan(y) int y; { @@ -565,9 +633,9 @@ 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) + } else if (scanpos != NULL && scanpos[y] == -1) scanpos[y] = ftell(fin); if (freadcolrs(scanline, xmax, fin) < 0) @@ -582,35 +650,16 @@ picreadline3(y, l3) /* read in 3-byte scanline */ int y; register rgbpixel *l3; { - register BYTE *l4; - register int shift, c; - int i; - + register int i; + /* read scanline */ if (getscan(y) < 0) quiterr("cannot seek for picreadline"); /* convert scanline */ - for (l4=scanline[0], i=xmax; i--; l4+=4, l3++) { - shift = l4[EXP] - COLXS; - if (shift >= 8) { - l3->r = l3->g = l3->b = 255; - } else if (shift <= -8) { - l3->r = l3->g = l3->b = 0; - } else if (shift > 0) { - c = l4[RED] << shift; - l3->r = c > 255 ? 255 : c; - c = l4[GRN] << shift; - l3->g = c > 255 ? 255 : c; - c = l4[BLU] << shift; - l3->b = c > 255 ? 255 : c; - } else if (shift < 0) { - l3->r = l4[RED] >> -shift; - l3->g = l4[GRN] >> -shift; - l3->b = l4[BLU] >> -shift; - } else { - l3->r = l4[RED]; - l3->g = l4[GRN]; - l3->b = l4[BLU]; - } + normcolrs(scanline, xmax, scale); + for (i = 0; i < xmax; i++) { + l3[i].r = scanline[i][RED]; + l3[i].g = scanline[i][GRN]; + l3[i].b = scanline[i][BLU]; } } @@ -619,18 +668,18 @@ picwriteline(y, l) /* add 8-bit scanline to image */ int y; pixel *l; { - bcopy(l, ourras.data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); + memcpy((void *)ourras->data.bz+BZPixmapSize(xmax,y), (void *)l, BZPixmapSize(xmax,1)); } -picreadcm(map) /* do gamcor correction */ +picreadcm(map) /* do gamma correction */ colormap map; { extern double pow(); 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; } } @@ -639,4 +688,10 @@ colormap map; picwritecm(map) /* handled elsewhere */ colormap map; { +#ifdef DEBUG + register int i; + + for (i = 0; i < 256; i++) + printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); +#endif }