--- ray/src/px/pinterp.c 1990/01/03 12:52:30 1.9 +++ ray/src/px/pinterp.c 1990/01/04 10:12:49 1.11 @@ -17,6 +17,9 @@ static char SCCSid[] = "$SunId$ LBL"; #define pscan(y) (ourpict+(y)*ourview.hresolu) #define zscan(y) (ourzbuf+(y)*ourview.hresolu) +#define F_FORE 1 /* fill foreground */ +#define F_BACK 2 /* fill background */ + #define ABS(x) ((x)>0?(x):-(x)) VIEW ourview = STDVIEW(512); /* desired view */ @@ -31,7 +34,11 @@ char *progname; VIEW theirview = STDVIEW(512); /* input view */ int gotview; /* got input view? */ +int fill = F_FORE|F_BACK; /* fill level */ +COLR backcolr = BLKCOLR; /* background color */ + double theirs2ours[4][4]; /* transformation matrix */ +int normdist = 1; /* normalized distance? */ main(argc, argv) /* interpolate pictures */ @@ -39,7 +46,9 @@ int argc; char *argv[]; { #define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt + extern double atof(); int gotvfile = 0; + char *zfile = NULL; char *err; int i; @@ -51,6 +60,42 @@ char *argv[]; check(2,1); zeps = atof(argv[++i]); break; + case 'n': /* dist. normalized? */ + check(2,0); + normdist = !normdist; + break; + case 'f': /* fill type */ + switch (argv[i][2]) { + case '0': /* none */ + check(3,0); + fill = 0; + break; + case 'f': /* foreground */ + check(3,0); + fill = F_FORE; + break; + case 'b': /* background */ + check(3,0); + fill = F_BACK; + break; + case 'a': /* all */ + check(3,0); + fill = F_FORE|F_BACK; + break; + case 'c': /* color */ + check(3,3); + setcolr(backcolr, atof(argv[i+1]), + atof(argv[i+2]), atof(argv[i+3])); + i += 3; + break; + default: + goto badopt; + } + break; + case 'z': /* z file */ + check(2,1); + zfile = argv[++i]; + break; case 'x': /* x resolution */ check(2,1); ourview.hresolu = atoi(argv[++i]); @@ -109,16 +154,13 @@ char *argv[]; break; default: badopt: - fprintf(stderr, "%s: unknown option '%s'\n", + fprintf(stderr, "%s: command line error at '%s'\n", progname, argv[i]); - exit(1); + goto userr; } /* check arguments */ - if (argc-i < 2 || (argc-i)%2) { - fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n", - progname); - exit(1); - } + if (argc-i < 2 || (argc-i)%2) + goto userr; /* set view */ if (err = setview(&ourview)) { fprintf(stderr, "%s: %s\n", progname, err); @@ -135,7 +177,10 @@ char *argv[]; for ( ; i < argc; i += 2) addpicture(argv[i], argv[i+1]); /* fill in spaces */ - fillpicture(); + if (fill&F_BACK) + fillpicture(); + else + backpicture(); /* add to header */ printargs(argc, argv, stdout); if (gotvfile) { @@ -144,10 +189,18 @@ char *argv[]; printf("\n"); } printf("\n"); - /* write output */ + /* write picture */ writepicture(); + /* write z file */ + if (zfile != NULL) + writedistance(zfile); exit(0); +userr: + fprintf(stderr, + "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n", + progname); + exit(1); #undef check } @@ -169,24 +222,21 @@ char *s; } -addpicture(pfile, zfile) /* add picture to output */ -char *pfile, *zfile; +addpicture(pfile, zspec) /* add picture to output */ +char *pfile, *zspec; { + extern double atof(); FILE *pfp, *zfp; char *err; COLR *scanin; float *zin, *zlast; int *plast; int y; - /* open input files */ + /* open picture file */ if ((pfp = fopen(pfile, "r")) == NULL) { perror(pfile); exit(1); } - if ((zfp = fopen(zfile, "r")) == NULL) { - perror(zfile); - exit(1); - } /* get header and view */ printf("%s:\n", pfile); gotview = 0; @@ -205,21 +255,33 @@ char *pfile, *zfile; /* allocate scanlines */ scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); zin = (float *)malloc(theirview.hresolu*sizeof(float)); - zlast = (float *)calloc(theirview.hresolu, sizeof(float)); plast = (int *)calloc(theirview.hresolu, sizeof(int)); - if (scanin == NULL || zin == NULL || zlast == NULL || plast == NULL) { + zlast = (float *)calloc(theirview.hresolu, sizeof(float)); + if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { perror(progname); exit(1); } + /* get z specification or file */ + if ((zfp = fopen(zspec, "r")) == NULL) { + double zvalue; + register int x; + if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) { + perror(zspec); + exit(1); + } + for (x = 0; x < theirview.hresolu; x++) + zin[x] = zvalue; + } /* load image */ for (y = theirview.vresolu-1; y >= 0; y--) { if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) { fprintf(stderr, "%s: read error\n", pfile); exit(1); } - if (fread(zin, sizeof(float), theirview.hresolu, zfp) + if (zfp != NULL + && fread(zin,sizeof(float),theirview.hresolu,zfp) < theirview.hresolu) { - fprintf(stderr, "%s: read error\n", zfile); + fprintf(stderr, "%s: read error\n", zspec); exit(1); } addscanline(y, scanin, zin, plast, zlast); @@ -230,7 +292,8 @@ char *pfile, *zfile; free((char *)plast); free((char *)zlast); fclose(pfp); - fclose(zfp); + if (zfp != NULL) + fclose(zfp); } @@ -277,7 +340,7 @@ float *zline; int *lasty; /* input/output */ float *lastyz; /* input/output */ { - extern double sqrt(), fabs(); + extern double sqrt(); double pos[3]; int lastx = 0; double lastxz = 0; @@ -290,12 +353,8 @@ float *lastyz; /* input/output */ pos[1] = y - .5*(theirview.vresolu-1); pos[2] = zline[x]; if (theirview.type == VT_PER) { - /* - * The following (single) statement can go - * if z is along the view direction rather - * than an eye ray. - */ - pos[2] /= sqrt( 1. + if (normdist) /* adjust for eye-ray distance */ + pos[2] /= sqrt( 1. + pos[0]*pos[0]*theirview.vhn2 + pos[1]*pos[1]*theirview.vvn2 ); pos[0] *= pos[2]; @@ -316,8 +375,10 @@ float *lastyz; /* input/output */ /* add pixel to our image */ zt = 2.*zeps*zline[x]; addpixel(xpos, ypos, - (fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1, - (fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1, + (fill&F_FORE && ABS(zline[x]-lastxz) <= zt) + ? lastx - xpos : 1, + (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) + ? lasty[x] - ypos : 1, pline[x], pos[2]); lastx = xpos; lasty[x] = ypos; @@ -398,7 +459,7 @@ fillpicture() /* fill in empty spaces */ * Next, find background from left or right. */ if (xback == -2) { - for (i = x+1; x < ourview.hresolu; i++) + for (i = x+1; i < ourview.hresolu; i++) if (zscan(y)[i] > 0) break; if (i < ourview.hresolu @@ -407,15 +468,23 @@ fillpicture() /* fill in empty spaces */ else xback = x-1; } - if (xback < 0 && yback[x] < 0) - continue; /* no background */ /* + * Check to see if we have no background for + * this pixel. If not, use background color. + */ + if (xback < 0 && yback[x] < 0) { + copycolr(pscan(y)[x], backcolr); + continue; + } + /* * Compare, and use the background that is * farther, unless one of them is next to us. */ - if (yback[x] < 0 || ABS(x-xback) <= 1 + if ( yback[x] < 0 + || (xback >= 0 && ABS(x-xback) <= 1) || ( ABS(y-yback[x]) > 1 - && zscan(yback[x])[x] < zscan(y)[xback] )) + && zscan(yback[x])[x] + < zscan(y)[xback] ) ) copycolr(pscan(y)[x],pscan(y)[xback]); else copycolr(pscan(y)[x],pscan(yback[x])[x]); @@ -428,6 +497,17 @@ fillpicture() /* fill in empty spaces */ } +backpicture() /* paint in empty pixels */ +{ + register int x, y; + + for (y = 0; y < ourview.vresolu; y++) + for (x = 0; x < ourview.hresolu; x++) + if (zscan(y)[x] <= 0) + copycolr(pscan(y)[x], backcolr); +} + + writepicture() /* write out picture */ { int y; @@ -438,4 +518,58 @@ writepicture() /* write out picture */ perror(progname); exit(1); } +} + + +writedistance(fname) /* write out z file */ +char *fname; +{ + extern double sqrt(); + int donorm = normdist && ourview.type == VT_PER; + FILE *fp; + int y; + float *zout; + + if ((fp = fopen(fname, "w")) == NULL) { + perror(fname); + exit(1); + } + if (donorm + && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { + perror(progname); + exit(1); + } + for (y = ourview.vresolu-1; y >= 0; y--) { + if (donorm) { + double vx, yzn2; + register int x; + yzn2 = y - .5*(ourview.vresolu-1); + yzn2 = 1. + yzn2*yzn2*ourview.vvn2; + for (x = 0; x < ourview.hresolu; x++) { + vx = x - .5*(ourview.hresolu-1); + zout[x] = zscan(y)[x] + * sqrt(vx*vx*ourview.vhn2 + yzn2); + } + } else + zout = zscan(y); + if (fwrite(zout, sizeof(float), ourview.hresolu, fp) + < ourview.hresolu) { + perror(fname); + exit(1); + } + } + if (donorm) + free((char *)zout); + fclose(fp); +} + + +isfloat(s) /* see if string is floating number */ +register char *s; +{ + for ( ; *s; s++) + if ((*s < '0' || *s > '9') && *s != '.' && *s != '-' + && *s != 'e' && *s != 'E' && *s != '+') + return(0); + return(1); }