--- ray/src/px/ximage.c 1990/01/05 17:52:57 1.20 +++ ray/src/px/ximage.c 2003/04/23 00:52:34 2.10 @@ -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.10 2003/04/23 00:52:34 greg Exp $"; #endif - /* * ximage.c - driver for X-windows * @@ -21,8 +18,12 @@ static char SCCSid[] = "$SunId$ LBL"; #include +#include + #include "color.h" +#include "resolu.h" + #include "xraster.h" #include "view.h" @@ -35,7 +36,7 @@ static char SCCSid[] = "$SunId$ LBL"; #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 */ @@ -58,7 +59,7 @@ 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 */ @@ -72,6 +73,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 */ @@ -82,21 +85,19 @@ char *progname; char errmsg[128]; -extern long ftell(); -extern char *malloc(), *calloc(); - -extern double atof(), pow(), log(); - - 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] == '-') @@ -141,17 +142,13 @@ char *argv[]; } } /* get header */ - getheader(fin, headline); + getheader(fin, headline, NULL); /* get picture dimensions */ - if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) + 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"); @@ -167,21 +164,20 @@ userr: } +int headline(s) /* get relevant info from header */ char *s; { - static char *altname[] = {"rview","rpict",VIEWSTR,NULL}; - register char **an; + char fmt[32]; if (isexpos(s)) exposure *= exposval(s); - else - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) == 0) - gotview++; - return; - } + else if (isformat(s)) { + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; + return(0); } @@ -250,6 +246,7 @@ char *err; } +void eputs(s) char *s; { @@ -257,6 +254,7 @@ char *s; } +void quit(code) int code; { @@ -291,7 +289,7 @@ getras() /* get raster file */ } return; memerr: - quit("out of memory"); + quiterr("out of memory"); } @@ -363,7 +361,7 @@ XKeyEvent *ekey; return(0); switch (*cp) { /* interpret command */ case 'q': - case CTRL(D): /* quit */ + case CTRL('D'): /* quit */ quit(0); case '\n': case '\r': @@ -377,7 +375,7 @@ XKeyEvent *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); @@ -411,8 +409,9 @@ 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); @@ -432,8 +431,8 @@ XKeyEvent *ekey; free_raster(ourras); getras(); /* fall through */ - case CTRL(R): /* redraw */ - case CTRL(L): + case CTRL('R'): /* redraw */ + case CTRL('L'): unmap_rcolors(ourras); XClear(wind); return(redraw(0, 0, width, height)); @@ -545,30 +544,32 @@ 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"); + 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); } @@ -596,7 +597,7 @@ 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); @@ -638,9 +639,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) @@ -660,11 +661,7 @@ register rgbpixel *l3; if (getscan(y) < 0) quiterr("cannot seek for picreadline"); /* convert scanline */ - if (scale) - for (i = 0; i < xmax; i++) - if (scanline[i][EXP]) - scanline[i][EXP] += scale; - normcolrs(scanline, xmax); + normcolrs(scanline, xmax, scale); for (i = 0; i < xmax; i++) { l3[i].r = scanline[i][RED]; l3[i].g = scanline[i][GRN]; @@ -677,7 +674,7 @@ picwriteline(y, l) /* add 8-bit scanline to image */ int y; pixel *l; { - bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); + bcopy((char *)l, (char *)ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1)); } @@ -688,7 +685,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; } }