--- ray/src/px/pinterp.c 1990/03/06 12:04:56 1.26 +++ ray/src/px/pinterp.c 1991/11/11 14:01:57 1.34 @@ -1,3 +1,5 @@ +/* Copyright (c) 1991 Regents of the University of California */ + #ifndef lint static char SCCSid[] = "$SunId$ LBL"; #endif @@ -16,6 +18,8 @@ static char SCCSid[] = "$SunId$ LBL"; #include "color.h" +#include "resolu.h" + #ifndef BSD #define vfork fork #endif @@ -57,9 +61,11 @@ double ourexp = -1; /* output picture exposure */ VIEW theirview = STDVIEW; /* input view */ int gotview; /* got input view? */ -int thresolu, tvresolu; /* input resolution */ +int wrongformat = 0; /* input in another format? */ +RESOLU tresolu; /* input resolution */ double theirexp; /* input picture exposure */ double theirs2ours[4][4]; /* transformation matrix */ +int hasmatrix = 0; /* has transformation matrix */ int childpid = -1; /* id of fill process */ FILE *psend, *precv; /* pipes to/from fill calculation */ @@ -210,6 +216,7 @@ char *argv[]; fputaspect(pixaspect, stdout); if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005)) fputexpos(ourexp, stdout); + fputformat(COLRFMT, stdout); putc('\n', stdout); /* write picture */ writepicture(); @@ -232,7 +239,13 @@ char *s; { static char *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL}; register char **an; + char fmt[32]; + if (isformat(s)) { + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + return; + } putc('\t', stdout); fputs(s, stdout); @@ -269,10 +282,9 @@ char *pfile, *zspec; theirexp = 1.0; gotview = 0; printf("%s:\n", pfile); - getheader(pfp, headline); - if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp) - != (YMAJOR|YDECR)) { - fprintf(stderr, "%s: picture view error\n", pfile); + getheader(pfp, headline, NULL); + if (wrongformat || !gotview || !fgetsresolu(&tresolu, pfp)) { + fprintf(stderr, "%s: picture format error\n", pfile); exit(1); } if (ourexp <= 0) @@ -284,11 +296,12 @@ char *pfile, *zspec; exit(1); } /* compute transformation */ - pixform(theirs2ours, &theirview, &ourview); + hasmatrix = pixform(theirs2ours, &theirview, &ourview); /* allocate scanlines */ - scanin = (COLR *)malloc(thresolu*sizeof(COLR)); - zin = (float *)malloc(thresolu*sizeof(float)); - plast = (struct position *)calloc(thresolu, sizeof(struct position)); + scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); + zin = (float *)malloc(scanlen(&tresolu)*sizeof(float)); + plast = (struct position *)calloc(scanlen(&tresolu), + sizeof(struct position)); if (scanin == NULL || zin == NULL || plast == NULL) syserror(); /* get z specification or file */ @@ -299,17 +312,18 @@ char *pfile, *zspec; perror(zspec); exit(1); } - for (x = 0; x < thresolu; x++) + for (x = scanlen(&tresolu); x-- > 0; ) zin[x] = zvalue; } /* load image */ - for (y = tvresolu-1; y >= 0; y--) { - if (freadcolrs(scanin, thresolu, pfp) < 0) { + for (y = 0; y < numscans(&tresolu); y++) { + if (freadcolrs(scanin, scanlen(&tresolu), pfp) < 0) { fprintf(stderr, "%s: read error\n", pfile); exit(1); } - if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float)) - < thresolu*sizeof(float)) { + if (zfd != -1 && read(zfd,(char *)zin, + scanlen(&tresolu)*sizeof(float)) + < scanlen(&tresolu)*sizeof(float)) { fprintf(stderr, "%s: read error\n", zspec); exit(1); } @@ -331,6 +345,10 @@ register VIEW *vw1, *vw2; { double m4t[4][4]; + if (vw1->type != VT_PER && vw1->type != VT_PAR) + return(0); + if (vw2->type != VT_PER && vw2->type != VT_PAR) + return(0); setident4(xfmat); xfmat[0][0] = vw1->hvec[0]; xfmat[0][1] = vw1->hvec[1]; @@ -358,6 +376,7 @@ register VIEW *vw1, *vw2; m4t[2][2] = vw2->vdir[2]; m4t[3][2] = -DOT(vw2->vp,vw2->vdir); multmat4(xfmat, xfmat, m4t); + return(1); } @@ -368,34 +387,18 @@ float *zline; struct position *lasty; /* input/output */ { extern double sqrt(); - double pos[3]; + FVECT pos; struct position lastx, newpos; register int x; lastx.z = 0; - for (x = thresolu-1; x >= 0; x--) { - pos[0] = (x+.5)/thresolu + theirview.hoff - .5; - pos[1] = (y+.5)/tvresolu + theirview.voff - .5; + for (x = scanlen(&tresolu); x-- > 0; ) { + pix2loc(pos, &tresolu, x, y); pos[2] = zline[x]; - if (theirview.type == VT_PER) { - if (normdist) /* adjust for eye-ray distance */ - pos[2] /= sqrt( 1. - + pos[0]*pos[0]*theirview.hn2 - + pos[1]*pos[1]*theirview.vn2 ); - pos[0] *= pos[2]; - pos[1] *= pos[2]; - } - multp3(pos, pos, theirs2ours); - if (pos[2] <= 0) { + if (movepixel(pos) < 0) { lasty[x].z = lastx.z = 0; /* mark invalid */ continue; } - if (ourview.type == VT_PER) { - pos[0] /= pos[2]; - pos[1] /= pos[2]; - } - pos[0] += .5 - ourview.hoff; - pos[1] += .5 - ourview.voff; newpos.x = pos[0] * hresolu; newpos.y = pos[1] * vresolu; newpos.z = zline[x]; @@ -454,7 +457,11 @@ double z; y1 = p0->y + c1*s1y/l1; for (c2 = l2; c2-- > 0; ) { x = x1 + c2*s2x/l2; + if (x < 0 || x >= hresolu) + continue; y = y1 + c2*s2y/l2; + if (y < 0 || y >= vresolu) + continue; if (zscan(y)[x] <= 0 || zscan(y)[x]-z > zeps*zscan(y)[x]) { zscan(y)[x] = z; @@ -465,13 +472,53 @@ double z; } +movepixel(pos) /* reposition image point */ +FVECT pos; +{ + FVECT pt, direc; + + if (pos[2] <= 0) /* empty pixel */ + return(-1); + if (hasmatrix) { + pos[0] += theirview.hoff - .5; + pos[1] += theirview.voff - .5; + if (theirview.type == VT_PER) { + if (normdist) /* adjust for eye-ray distance */ + pos[2] /= sqrt( 1. + + pos[0]*pos[0]*theirview.hn2 + + pos[1]*pos[1]*theirview.vn2 ); + pos[0] *= pos[2]; + pos[1] *= pos[2]; + } + multp3(pos, pos, theirs2ours); + if (pos[2] <= 0) + return(-1); + if (ourview.type == VT_PER) { + pos[0] /= pos[2]; + pos[1] /= pos[2]; + } + pos[0] += .5 - ourview.hoff; + pos[1] += .5 - ourview.voff; + return(0); + } + if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0) + return(-1); + pt[0] += direc[0]*pos[2]; + pt[1] += direc[1]*pos[2]; + pt[2] += direc[2]*pos[2]; + viewloc(pos, &ourview, pt); + if (pos[2] <= 0) + return(-1); + return(0); +} + + backpicture(fill, samp) /* background fill algorithm */ int (*fill)(); int samp; { int *yback, xback; int y; - COLR pfill; register int x, i; /* get back buffer */ yback = (int *)malloc(hresolu*sizeof(int)); @@ -519,35 +566,39 @@ int samp; } /* * If we have no background for this pixel, - * or if the background is too distant, * use the given fill function. */ - if ((xback < 0 && yback[x] < 0) - || (samp > 0 - && ABS(x-xback) >= samp - && ABS(y-yback[y]) >= samp)) { - (*fill)(x,y); - if (fill != backfill) { /* reuse */ - yback[x] = -2; - xback = -2; - } - continue; - } + if (xback < 0 && yback[x] < 0) + goto fillit; /* * Compare, and use the background that is * farther, unless one of them is next to us. + * If the background is too distant, call + * the fill function. */ if ( yback[x] < 0 || (xback >= 0 && ABS(x-xback) <= 1) || ( ABS(y-yback[x]) > 1 && zscan(yback[x])[x] < zscan(y)[xback] ) ) { + if (samp > 0 && ABS(x-xback) >= samp) + goto fillit; copycolr(pscan(y)[x],pscan(y)[xback]); zscan(y)[x] = zscan(y)[xback]; } else { + if (samp > 0 && ABS(y-yback[x]) > samp) + goto fillit; copycolr(pscan(y)[x],pscan(yback[x])[x]); zscan(y)[x] = zscan(yback[x])[x]; } + continue; + fillit: + (*fill)(x,y); + if (fill == rcalfill) { /* use it */ + clearqueue(); + xback = x; + yback[x] = y; + } } else { /* full pixel */ yback[x] = -2; xback = -2; @@ -573,7 +624,7 @@ writepicture() /* write out picture */ { int y; - fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); + fprtresolu(hresolu, vresolu, stdout); for (y = vresolu-1; y >= 0; y--) if (fwritecolrs(pscan(y), hresolu, stdout) < 0) syserror(); @@ -600,10 +651,10 @@ char *fname; if (donorm) { double vx, yzn2; register int x; - yzn2 = y - .5*(vresolu-1); + yzn2 = (y+.5)/vresolu + ourview.voff - .5; yzn2 = 1. + yzn2*yzn2*ourview.vn2; for (x = 0; x < hresolu; x++) { - vx = x - .5*(hresolu-1); + vx = (x+.5)/hresolu + ourview.hoff - .5; zout[x] = zscan(y)[x] * sqrt(vx*vx*ourview.hn2 + yzn2); }