--- ray/src/px/ximage.c 1989/11/29 16:50:39 1.15 +++ ray/src/px/ximage.c 1991/05/01 12:40:03 1.30 @@ -58,7 +58,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 +72,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 */ @@ -141,17 +143,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"); @@ -172,29 +170,23 @@ char *s; { static char *altname[] = {"rview","rpict",VIEWSTR,NULL}; register char **an; + char fmt[32]; - if (!strncmp(s, "EXPOSURE=", 9)) - exposure *= atof(s+9); - else + if (isexpos(s)) + exposure *= exposval(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) + if (sscanview(&ourview, s+strlen(*an)) > 0) gotview++; return; } } -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; @@ -301,7 +293,7 @@ getras() /* get raster file */ } return; memerr: - quit("out of memory"); + quiterr("out of memory"); } @@ -384,10 +376,10 @@ 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); @@ -401,7 +393,7 @@ XKeyEvent *ekey; fontid, BlackPixel, WhitePixel); return(0); case 'i': /* identify (contour) */ - if (ourras->ncolors == 0) + if (ourras->pixels == NULL) return(-1); n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)]; n = ourras->pmap[n]; @@ -421,8 +413,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); @@ -444,8 +437,8 @@ XKeyEvent *ekey; /* fall through */ case CTRL(R): /* redraw */ case CTRL(L): + unmap_rcolors(ourras); XClear(wind); - XStoreColors(ourras->ncolors, ourras->cdefs); return(redraw(0, 0, width, height)); case ' ': /* clear */ return(redraw(box.xmin, box.ymin, box.xsiz, box.ysiz)); @@ -556,20 +549,20 @@ getmono() /* get monochrome data */ register unsigned short *dp; register int x, err; int y; - rgbpixel *inline; + 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]; + err += rgb_bright(&inl[x]) + cerr[x]; if (err > 127) err -= 255; else @@ -577,7 +570,7 @@ getmono() /* get monochrome data */ cerr[x] = err >>= 1; } } - free((char *)inline); + free((char *)inl); free((char *)cerr); } @@ -618,24 +611,26 @@ register XRASTER *xr; double sf; { register int i; - int maxv; + long maxv; + if (xr->pixels == NULL) + return; + sf = pow(sf, 1.0/gamcor); - maxv = (1<<16) / sf; + maxv = 65535/sf; for (i = xr->ncolors; i--; ) { - xr->cdefs[i].red = xr->cdefs[i].red >= maxv ? - (1<<16)-1 : + xr->cdefs[i].red = xr->cdefs[i].red > maxv ? + 65535 : xr->cdefs[i].red * sf; - xr->cdefs[i].green = xr->cdefs[i].green >= maxv ? - (1<<16)-1 : + xr->cdefs[i].green = xr->cdefs[i].green > maxv ? + 65535 : xr->cdefs[i].green * sf; - xr->cdefs[i].blue = xr->cdefs[i].blue >= maxv ? - (1<<16)-1 : + xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ? + 65535 : xr->cdefs[i].blue * sf; } - if (xr->pixels != NULL) - XStoreColors(xr->ncolors, xr->cdefs); + XStoreColors(xr->ncolors, xr->cdefs); } @@ -646,7 +641,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); @@ -668,11 +663,7 @@ register rgbpixel *l3; if (getscan(y) < 0) quiterr("cannot seek for picreadline"); /* convert scanline */ - if (scale != 0) - for (i = 0; i < xmax; i++) - if (scanline[i][EXP]+scale >= 0) - 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]; @@ -685,7 +676,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)); } @@ -696,7 +687,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; } }