--- ray/src/px/pinterp.c 1989/12/28 15:09:28 1.6 +++ ray/src/px/pinterp.c 1990/01/03 12:52:30 1.9 @@ -21,7 +21,7 @@ static char SCCSid[] = "$SunId$ LBL"; VIEW ourview = STDVIEW(512); /* desired view */ -double zeps = 0.001; /* allowed z epsilon */ +double zeps = .02; /* allowed z epsilon */ COLR *ourpict; /* output picture */ float *ourzbuf; /* corresponding z-buffer */ @@ -173,10 +173,10 @@ addpicture(pfile, zfile) /* add picture to output */ char *pfile, *zfile; { FILE *pfp, *zfp; - COLR *scanin; - float *zin; char *err; - int xres, yres; + COLR *scanin; + float *zin, *zlast; + int *plast; int y; /* open input files */ if ((pfp = fopen(pfile, "r")) == NULL) { @@ -191,12 +191,11 @@ char *pfile, *zfile; printf("%s:\n", pfile); gotview = 0; getheader(pfp, headline); - if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) { + if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) + != (YMAJOR|YDECR)) { fprintf(stderr, "%s: picture view error\n", pfile); exit(1); } - theirview.hresolu = xres; - theirview.vresolu = yres; if (err = setview(&theirview)) { fprintf(stderr, "%s: %s\n", pfile, err); exit(1); @@ -204,27 +203,32 @@ char *pfile, *zfile; /* compute transformation */ pixform(theirs2ours, &theirview, &ourview); /* allocate scanlines */ - scanin = (COLR *)malloc(xres*sizeof(COLR)); - zin = (float *)malloc(xres*sizeof(float)); - if (scanin == NULL || zin == NULL) { + 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) { perror(progname); exit(1); } /* load image */ - for (y = yres-1; y >= 0; y--) { - if (freadcolrs(scanin, xres, pfp) < 0) { + 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), xres, zfp) < xres) { + if (fread(zin, sizeof(float), theirview.hresolu, zfp) + < theirview.hresolu) { fprintf(stderr, "%s: read error\n", zfile); exit(1); } - addscanline(y, scanin, zin); + addscanline(y, scanin, zin, plast, zlast); } /* clean up */ free((char *)scanin); free((char *)zin); + free((char *)plast); + free((char *)zlast); fclose(pfp); fclose(zfp); } @@ -266,21 +270,31 @@ register VIEW *vw1, *vw2; } -addscanline(y, pline, zline) /* add scanline to output */ +addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ int y; COLR *pline; float *zline; +int *lasty; /* input/output */ +float *lastyz; /* input/output */ { - extern double sqrt(); + extern double sqrt(), fabs(); double pos[3]; + int lastx = 0; + double lastxz = 0; + double zt; + int xpos, ypos; register int x; - register int xpos, ypos; - for (x = 0; x < theirview.hresolu; x++) { + for (x = theirview.hresolu-1; x >= 0; x--) { pos[0] = x - .5*(theirview.hresolu-1); 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. + pos[0]*pos[0]*theirview.vhn2 + pos[1]*pos[1]*theirview.vvn2 ); @@ -288,7 +302,7 @@ float *zline; pos[1] *= pos[2]; } multp3(pos, pos, theirs2ours); - if (pos[2] <= 0.0) + if (pos[2] <= 0) continue; if (ourview.type == VT_PER) { pos[0] /= pos[2]; @@ -296,22 +310,51 @@ float *zline; } pos[0] += .5*ourview.hresolu; pos[1] += .5*ourview.vresolu; - if (pos[0] < 0 || pos[0] > ourview.hresolu - || pos[1] < 0 || pos[1] > ourview.vresolu) + if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu + || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) continue; - /* check current value at pos */ - xpos = pos[0]; - ypos = pos[1]; - if (zscan(ypos)[xpos] <= 0.0 - || zscan(ypos)[xpos] - pos[2] - > zeps*zscan(ypos)[xpos]) { - zscan(ypos)[xpos] = pos[2]; - copycolr(pscan(ypos)[xpos], pline[x]); - } + /* 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, + pline[x], pos[2]); + lastx = xpos; + lasty[x] = ypos; + lastxz = lastyz[x] = zline[x]; } } +addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ +int xstart, ystart; +int width, height; +COLR pix; +double z; +{ + register int x, y; + /* make width and height positive */ + if (width < 0) { + width = -width; + xstart = xstart-width+1; + } else if (width == 0) + width = 1; + if (height < 0) { + height = -height; + ystart = ystart-height+1; + } else if (height == 0) + height = 1; + /* fill pixel(s) within rectangle */ + for (y = ystart; y < ystart+height; y++) + for (x = xstart; x < xstart+width; x++) + if (zscan(y)[x] <= 0 + || zscan(y)[x]-z > zeps*zscan(y)[x]) { + zscan(y)[x] = z; + copycolr(pscan(y)[x], pix); + } +} + + fillpicture() /* fill in empty spaces */ { int *yback, xback; @@ -326,14 +369,24 @@ fillpicture() /* fill in empty spaces */ } for (x = 0; x < ourview.hresolu; x++) yback[x] = -2; + /* + * Xback and yback are the pixel locations of suitable + * background values in each direction. + * A value of -2 means unassigned, and -1 means + * that there is no suitable background in this direction. + */ /* fill image */ for (y = 0; y < ourview.vresolu; y++) { xback = -2; for (x = 0; x < ourview.hresolu; x++) - if (zscan(y)[x] <= 0.0) { /* empty pixel */ + if (zscan(y)[x] <= 0) { /* empty pixel */ + /* + * First, find background from above or below. + * (farthest assigned pixel) + */ if (yback[x] == -2) { for (i = y+1; i < ourview.vresolu; i++) - if (zscan(i)[x] > 0.0) + if (zscan(i)[x] > 0) break; if (i < ourview.vresolu && (y <= 0 || zscan(y-1)[x] < zscan(i)[x])) @@ -341,9 +394,12 @@ fillpicture() /* fill in empty spaces */ else yback[x] = y-1; } + /* + * Next, find background from left or right. + */ if (xback == -2) { for (i = x+1; x < ourview.hresolu; i++) - if (zscan(y)[i] > 0.0) + if (zscan(y)[i] > 0) break; if (i < ourview.hresolu && (x <= 0 || zscan(y)[x-1] < zscan(y)[i])) @@ -352,7 +408,11 @@ fillpicture() /* fill in empty spaces */ xback = x-1; } if (xback < 0 && yback[x] < 0) - continue; + continue; /* no background */ + /* + * Compare, and use the background that is + * farther, unless one of them is next to us. + */ if (yback[x] < 0 || ABS(x-xback) <= 1 || ( ABS(y-yback[x]) > 1 && zscan(yback[x])[x] < zscan(y)[xback] ))