--- ray/src/px/pinterp.c 1995/09/15 22:04:04 2.25 +++ ray/src/px/pinterp.c 2012/02/08 13:43:21 2.43 @@ -1,25 +1,23 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pinterp.c,v 2.43 2012/02/08 13:43:21 greg Exp $"; #endif - /* * Interpolate and extrapolate pictures with different view parameters. * * Greg Ward 09Dec89 */ -#include "standard.h" +#include "copyright.h" #include +#include +#include "platform.h" +#include "standard.h" +#include "rtprocess.h" /* Windows: must come before color.h */ #include "view.h" - #include "color.h" -#include "resolu.h" - #define LOG2 0.69314718055994530942 #define pscan(y) (ourpict+(y)*hresolu) @@ -30,7 +28,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define averaging (ourweigh != NULL) #define blurring (ourbpict != NULL) #define usematrix (hasmatrix & !averaging) -#define zisnorm (!usematrix | ourview.type != VT_PER) +#define zisnorm ((!usematrix) | (ourview.type != VT_PER)) #define MAXWT 1000. /* maximum pixel weight (averaging) */ @@ -39,7 +37,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define PACKSIZ 256 /* max. calculation packet size */ -#define RTCOM "rtrace -h- -ovl -fff " +#define RTCOM "rtrace -h- -ovl -fff -ld- -i- -I- " #define ABS(x) ((x)>0?(x):-(x)) @@ -70,12 +68,11 @@ char *progname; int fillo = F_FORE|F_BACK; /* selected fill options */ int fillsamp = 0; /* sample separation (0 == inf) */ -extern int backfill(), rcalfill(); /* fill functions */ -int (*fillfunc)() = backfill; /* selected fill function */ COLR backcolr = BLKCOLR; /* background color */ COLOR backcolor = BLKCOLOR; /* background color (float) */ double backz = 0.0; /* background z value */ int normdist = 1; /* i/o normalized distance? */ +char ourfmt[LPICFMT+1] = PICFMT; /* original picture format */ double ourexp = -1; /* original picture exposure */ int expadj = 0; /* exposure adjustment (f-stops) */ double rexpadj = 1; /* real exposure adjustment */ @@ -88,18 +85,44 @@ double theirexp; /* input picture exposure */ MAT4 theirs2ours; /* transformation matrix */ int hasmatrix = 0; /* has transformation matrix */ -int PDesc[3] = {-1,-1,-1}; /* rtrace process descriptor */ -#define childpid (PDesc[2]) +static SUBPROC PDesc = SP_INACTIVE; /* rtrace process descriptor */ unsigned short queue[PACKSIZ][2]; /* pending pixels */ int packsiz; /* actual packet size */ -int queuesiz; /* number of pixels pending */ +int queuesiz = 0; /* number of pixels pending */ -extern double movepixel(); +typedef void fillfunc_t(int x, int y); +static gethfunc headline; +static int nextview(FILE *fp); +static void compavgview(void); +static void addpicture(char *pfile, char *zspec); +static int pixform(MAT4 xfmat, VIEW *vw1, VIEW *vw2); +static void addscanline(struct bound *xl, int y, + COLR *pline, float *zline, struct position *lasty); +static void addpixel(struct position *p0, struct position *p1, + struct position *p2, COLR pix, double w, double z); +static double movepixel(FVECT pos); +static int getperim(struct bound *xl, struct bound *yl, float *zline, int zfd); +static void backpicture(fillfunc_t *fill, int samp); +static void fillpicture(fillfunc_t *fill); +static int clipaft(void); +static int addblur(void); +static void writepicture(void); +static void writedistance(char *fname); +static fillfunc_t backfill; +static fillfunc_t rcalfill; +static void calstart(char *prog, char *args); +static void caldone(void); +static void clearqueue(void); +static void syserror(char *s); -main(argc, argv) /* interpolate pictures */ -int argc; -char *argv[]; +fillfunc_t *fillfunc = backfill; /* selected fill function */ + +int +main( /* interpolate pictures */ + int argc, + char *argv[] +) { #define check(ol,al) if (argv[an][ol] || \ badarg(argc-an-1,argv+an+1,al)) \ @@ -111,6 +134,9 @@ char *argv[]; char *expcomp = NULL; int i, an, rval; + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdout); + progname = argv[0]; for (an = 1; an < argc && argv[an][0] == '-'; an++) { @@ -212,7 +238,7 @@ char *argv[]; if (argv[an][2] != 'f') goto badopt; check(3,"s"); - gotvfile = viewfile(argv[++an], &ourview, 0, 0); + gotvfile = viewfile(argv[++an], &ourview, NULL); if (gotvfile < 0) syserror(argv[an]); else if (gotvfile == 0) { @@ -235,8 +261,8 @@ char *argv[]; fillo &= ~F_BACK; if (doavg < 0) doavg = (argc-an) > 2; - if (expcomp != NULL) - if (expcomp[0] == '+' | expcomp[0] == '-') { + if (expcomp != NULL) { + if ((expcomp[0] == '+') | (expcomp[0] == '-')) { expadj = atof(expcomp) + (expcomp[0]=='+' ? .5 : -.5); if (doavg | doblur) rexpadj = pow(2.0, atof(expcomp)); @@ -250,8 +276,9 @@ char *argv[]; if (!(doavg | doblur)) rexpadj = pow(2.0, (double)expadj); } + } /* set view */ - if (nextview(doblur ? stdin : NULL) == EOF) { + if (nextview(doblur ? stdin : (FILE *)NULL) == EOF) { fprintf(stderr, "%s: no view on standard input!\n", progname); exit(1); @@ -261,7 +288,7 @@ char *argv[]; if (doavg) { ourspict = (COLOR *)bmalloc(hresolu*vresolu*sizeof(COLOR)); ourweigh = (float *)bmalloc(hresolu*vresolu*sizeof(float)); - if (ourspict == NULL | ourweigh == NULL) + if ((ourspict == NULL) | (ourweigh == NULL)) syserror(progname); } else { ourpict = (COLR *)bmalloc(hresolu*vresolu*sizeof(COLR)); @@ -278,9 +305,10 @@ char *argv[]; syserror(progname); /* new header */ newheader("RADIANCE", stdout); + fputnow(stdout); /* run pictures */ do { - bzero((char *)ourzbuf, hresolu*vresolu*sizeof(float)); + memset((char *)ourzbuf, '\0', hresolu*vresolu*sizeof(float)); for (i = an; i < argc; i += 2) addpicture(argv[i], argv[i+1]); if (fillo&F_BACK) /* fill in spaces */ @@ -300,15 +328,16 @@ char *argv[]; fprintview(&avgview, stdout); putc('\n', stdout); } - if (pixaspect < .99 | pixaspect > 1.01) + if ((pixaspect < .99) | (pixaspect > 1.01)) fputaspect(pixaspect, stdout); if (ourexp > 0) ourexp *= rexpadj; else ourexp = rexpadj; - if (ourexp < .995 | ourexp > 1.005) + if ((ourexp < .995) | (ourexp > 1.005)) fputexpos(ourexp, stdout); - fputformat(COLRFMT, stdout); + if (strcmp(ourfmt, PICFMT)) /* print format if known */ + fputformat(ourfmt, stdout); putc('\n', stdout); /* write picture */ writepicture(); @@ -326,16 +355,23 @@ userr: } -headline(s) /* process header string */ -char *s; +static int +headline( /* process header string */ + char *s, + void *p +) { char fmt[32]; if (isheadid(s)) - return; + return(0); if (formatval(fmt, s)) { - wrongformat = strcmp(fmt, COLRFMT); - return; + if (globmatch(ourfmt, fmt)) { + wrongformat = 0; + strcpy(ourfmt, fmt); + } else + wrongformat = 1; + return(0); } if (nvavg < 2) { putc('\t', stdout); @@ -343,15 +379,18 @@ char *s; } if (isexpos(s)) { theirexp *= exposval(s); - return; + return(0); } if (isview(s) && sscanview(&theirview, s) > 0) gotview++; + return(0); } -nextview(fp) /* get and set next view */ -FILE *fp; +static int +nextview( /* get and set next view */ + FILE *fp +) { char linebuf[256]; char *err; @@ -369,7 +408,7 @@ FILE *fp; exit(1); } if (!nvavg) { /* first view */ - copystruct(&avgview, &ourview); + avgview = ourview; return(nvavg++); } /* add to average view */ @@ -378,6 +417,7 @@ FILE *fp; avgview.vdir[i] += ourview.vdir[i]; avgview.vup[i] += ourview.vup[i]; } + avgview.vdist += ourview.vdist; avgview.horiz += ourview.horiz; avgview.vert += ourview.vert; avgview.hoff += ourview.hoff; @@ -388,7 +428,8 @@ FILE *fp; } -compavgview() /* compute average view */ +static void +compavgview(void) /* compute average view */ { register int i; double f; @@ -401,6 +442,7 @@ compavgview() /* compute average view */ avgview.vdir[i] *= f; avgview.vup[i] *= f; } + avgview.vdist *= f; avgview.horiz *= f; avgview.vert *= f; avgview.hoff *= f; @@ -408,13 +450,16 @@ compavgview() /* compute average view */ avgview.vfore *= f; avgview.vaft *= f; if (setview(&avgview) != NULL) /* in case of emergency... */ - copystruct(&avgview, &ourview); + avgview = ourview; pixaspect = viewaspect(&avgview) * hresolu / vresolu; } -addpicture(pfile, zspec) /* add picture to output */ -char *pfile, *zspec; +static void +addpicture( /* add picture to output */ + char *pfile, + char *zspec +) { FILE *pfp; int zfd; @@ -429,7 +474,7 @@ char *pfile, *zspec; syserror(pfile); /* get header with exposure and view */ theirexp = 1.0; - copystruct(&theirview, &stdview); + theirview = stdview; gotview = 0; if (nvavg < 2) printf("%s:\n", pfile); @@ -442,7 +487,7 @@ char *pfile, *zspec; ourexp = theirexp; else if (ABS(theirexp-ourexp) > .01*ourexp) fprintf(stderr, "%s: different exposure (warning)\n", pfile); - if (err = setview(&theirview)) { + if ( (err = setview(&theirview)) ) { fprintf(stderr, "%s: %s\n", pfile, err); exit(1); } @@ -465,8 +510,8 @@ char *pfile, *zspec; if (xlim == NULL) syserror(progname); if (!getperim(xlim, &ylim, zin, zfd)) { /* overlapping area? */ - free((char *)zin); - free((char *)xlim); + free((void *)zin); + free((void *)xlim); if (zfd != -1) close(zfd); fclose(pfp); @@ -476,7 +521,7 @@ char *pfile, *zspec; scanin = (COLR *)malloc(scanlen(&tresolu)*sizeof(COLR)); plast = (struct position *)calloc(scanlen(&tresolu), sizeof(struct position)); - if (scanin == NULL | plast == NULL) + if ((scanin == NULL) | (plast == NULL)) syserror(progname); /* skip to starting point */ for (y = 0; y < ylim.min; y++) @@ -485,7 +530,8 @@ char *pfile, *zspec; exit(1); } if (zfd != -1 && lseek(zfd, - (long)ylim.min*scanlen(&tresolu)*sizeof(float), 0) < 0) + (off_t)ylim.min*scanlen(&tresolu)*sizeof(float), + SEEK_SET) < 0) syserror(zspec); /* load image */ for (y = ylim.min; y <= ylim.max; y++) { @@ -500,25 +546,28 @@ char *pfile, *zspec; addscanline(xlim+y, y, scanin, zin, plast); } /* clean up */ - free((char *)xlim); - free((char *)scanin); - free((char *)zin); - free((char *)plast); + free((void *)xlim); + free((void *)scanin); + free((void *)zin); + free((void *)plast); fclose(pfp); if (zfd != -1) close(zfd); } -pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */ -register MAT4 xfmat; -register VIEW *vw1, *vw2; +static int +pixform( /* compute view1 to view2 matrix */ + register MAT4 xfmat, + register VIEW *vw1, + register VIEW *vw2 +) { double m4t[4][4]; - if (vw1->type != VT_PER & vw1->type != VT_PAR) + if ((vw1->type != VT_PER) & (vw1->type != VT_PAR)) return(0); - if (vw2->type != VT_PER & vw2->type != VT_PAR) + if ((vw2->type != VT_PER) & (vw2->type != VT_PAR)) return(0); setident4(xfmat); xfmat[0][0] = vw1->hvec[0]; @@ -551,12 +600,14 @@ register VIEW *vw1, *vw2; } -addscanline(xl, y, pline, zline, lasty) /* add scanline to output */ -struct bound *xl; -int y; -COLR *pline; -float *zline; -struct position *lasty; /* input/output */ +static void +addscanline( /* add scanline to output */ + struct bound *xl, + int y, + COLR *pline, + float *zline, + struct position *lasty /* input/output */ +) { FVECT pos; struct position lastx, newpos; @@ -583,11 +634,15 @@ struct position *lasty; /* input/output */ } -addpixel(p0, p1, p2, pix, w, z) /* fill in pixel parallelogram */ -struct position *p0, *p1, *p2; -COLR pix; -double w; -double z; +static void +addpixel( /* fill in pixel parallelogram */ + struct position *p0, + struct position *p1, + struct position *p2, + COLR pix, + double w, + double z +) { double zt = 2.*zeps*p0->z; /* threshold */ COLOR pval; /* converted+weighted pixel */ @@ -602,7 +657,7 @@ double z; s1x = p1->x - p0->x; s1y = p1->y - p0->y; l1 = ABS(s1x); - if (p1isy = (ABS(s1y) > l1)) + if ( (p1isy = (ABS(s1y) > l1)) ) l1 = ABS(s1y); else if (l1 < 1) l1 = 1; @@ -635,10 +690,10 @@ double z; y1 = p0->y + c1*s1y/l1; for (c2 = l2; c2-- > 0; ) { x = x1 + c2*s2x/l2; - if (x < 0 | x >= hresolu) + if ((x < 0) | (x >= hresolu)) continue; y = y1 + c2*s2y/l2; - if (y < 0 | y >= vresolu) + if ((y < 0) | (y >= vresolu)) continue; if (averaging) { if (zscan(y)[x] <= 0 || zscan(y)[x]-z @@ -660,20 +715,20 @@ double z; } -double -movepixel(pos) /* reposition image point */ -register FVECT pos; +static double +movepixel( /* reposition image point */ + register FVECT pos +) { FVECT pt, tdir, odir; double d; - register int i; if (pos[2] <= 0) /* empty pixel */ return(0); if (usematrix) { pos[0] += theirview.hoff - .5; pos[1] += theirview.voff - .5; - if (normdist & theirview.type == VT_PER) + if (normdist & (theirview.type == VT_PER)) d = sqrt(1. + pos[0]*pos[0]*theirview.hn2 + pos[1]*pos[1]*theirview.vn2); else @@ -697,7 +752,7 @@ register FVECT pos; } else { if (viewray(pt, tdir, &theirview, pos[0], pos[1]) < -FTINY) return(0); - if (!normdist & theirview.type == VT_PER) /* adjust */ + if ((!normdist) & (theirview.type == VT_PER)) /* adjust */ pos[2] *= sqrt(1. + pos[0]*pos[0]*theirview.hn2 + pos[1]*pos[1]*theirview.vn2); pt[0] += tdir[0]*pos[2]; @@ -707,27 +762,32 @@ register FVECT pos; if (pos[2] <= 0) return(0); } - if (pos[0] < 0 | pos[0] >= 1-FTINY | pos[1] < 0 | pos[1] >= 1-FTINY) + if ((pos[0] < 0) | (pos[0] >= 1-FTINY) | (pos[1] < 0) | (pos[1] >= 1-FTINY)) return(0); if (!averaging) return(1); - if (ourview.type == VT_PAR) /* compute our direction */ - VCOPY(odir, ourview.vdir); - else - for (i = 0; i < 3; i++) - odir[i] = (pt[i] - ourview.vp[i])/pos[2]; - d = DOT(odir,tdir); /* compute pixel weight */ - if (d >= 1.-1./MAXWT/MAXWT) + /* compute pixel weight */ + if (ourview.type == VT_PAR) { + d = DOT(ourview.vdir,tdir); + d = 1. - d*d; + } else { + VSUB(odir, pt, ourview.vp); + d = DOT(odir,tdir); + d = 1. - d*d/DOT(odir,odir); + } + if (d <= 1./MAXWT/MAXWT) return(MAXWT); /* clip to maximum weight */ - return(1./sqrt(1.-d)); + return(1./sqrt(d)); } -getperim(xl, yl, zline, zfd) /* compute overlapping image area */ -register struct bound *xl; -struct bound *yl; -float *zline; -int zfd; +static int +getperim( /* compute overlapping image area */ + register struct bound *xl, + struct bound *yl, + float *zline, + int zfd +) { int step; FVECT pos; @@ -750,8 +810,8 @@ int zfd; yl->min = 32000; yl->max = 0; /* search for points on image */ for (y = step - 1; y < numscans(&tresolu); y += step) { if (zfd != -1) { - if (lseek(zfd, (long)y*scanlen(&tresolu)*sizeof(float), - 0) < 0) + if (lseek(zfd, (off_t)y*scanlen(&tresolu)*sizeof(float), + SEEK_SET) < 0) syserror("lseek"); if (read(zfd, (char *)zline, scanlen(&tresolu)*sizeof(float)) @@ -796,20 +856,22 @@ int zfd; xl[y-1].max = xl[y-step].max; } for (x = 2; x < step; x++) - copystruct(xl+y-x, xl+y-1); + *(xl+y-x) = *(xl+y-1); } if (yl->max >= numscans(&tresolu)) yl->max = numscans(&tresolu) - 1; y -= step; for (x = numscans(&tresolu) - 1; x > y; x--) /* fill bottom rows */ - copystruct(xl+x, xl+y); + *(xl+x) = *(xl+y); return(yl->max >= yl->min); } -backpicture(fill, samp) /* background fill algorithm */ -int (*fill)(); -int samp; +static void +backpicture( /* background fill algorithm */ + fillfunc_t *fill, + int samp +) { int *yback, xback; int y; @@ -911,12 +973,14 @@ int samp; xback = -2; } } - free((char *)yback); + free((void *)yback); } -fillpicture(fill) /* paint in empty pixels using fill */ -int (*fill)(); +static void +fillpicture( /* paint in empty pixels using fill */ + fillfunc_t *fill +) { register int x, y; @@ -924,13 +988,16 @@ int (*fill)(); for (x = 0; x < hresolu; x++) if (zscan(y)[x] <= 0) (*fill)(x,y); + if (fill == rcalfill) + clearqueue(); } -clipaft() /* perform aft clipping as indicated */ +static int +clipaft(void) /* perform aft clipping as indicated */ { register int x, y; - int adjtest = ourview.type == VT_PER & zisnorm; + int adjtest = (ourview.type == VT_PER) & zisnorm; double tstdist; double yzn2, vx; @@ -953,9 +1020,9 @@ clipaft() /* perform aft clipping as indicated */ continue; } if (averaging) - bzero(sscan(y)[x], sizeof(COLOR)); + memset(sscan(y)[x], '\0', sizeof(COLOR)); else - bzero(pscan(y)[x], sizeof(COLR)); + memset(pscan(y)[x], '\0', sizeof(COLR)); zscan(y)[x] = 0.0; } } @@ -963,7 +1030,8 @@ clipaft() /* perform aft clipping as indicated */ } -addblur() /* add to blurred picture */ +static int +addblur(void) /* add to blurred picture */ { COLOR cval; double d; @@ -1003,7 +1071,8 @@ addblur() /* add to blurred picture */ } -writepicture() /* write out picture (alters buffer) */ +static void +writepicture(void) /* write out picture (alters buffer) */ { int y; register int x; @@ -1034,11 +1103,13 @@ writepicture() /* write out picture (alters buffer) } -writedistance(fname) /* write out z file (alters buffer) */ -char *fname; +static void +writedistance( /* write out z file (alters buffer) */ + char *fname +) { int donorm = normdist & !zisnorm ? 1 : - ourview.type == VT_PER & !normdist & zisnorm ? -1 : 0; + (ourview.type == VT_PER) & !normdist & zisnorm ? -1 : 0; int fd; int y; @@ -1067,8 +1138,11 @@ char *fname; } -backfill(x, y) /* fill pixel with background */ -int x, y; +static void +backfill( /* fill pixel with background */ + int x, + int y +) { if (averaging) { copycolor(sscan(y)[x], backcolor); @@ -1079,15 +1153,18 @@ int x, y; } -calstart(prog, args) /* start fill calculation */ -char *prog, *args; +static void +calstart( /* start fill calculation */ + char *prog, + char *args +) { char combuf[512]; char *argv[64]; int rval; register char **wp, *cp; - if (childpid != -1) { + if (PDesc.running) { fprintf(stderr, "%s: too many calculations\n", progname); exit(1); } @@ -1106,7 +1183,7 @@ char *prog, *args; } *wp = NULL; /* start process */ - if ((rval = open_process(PDesc, argv)) < 0) + if ((rval = open_process(&PDesc, argv)) < 0) syserror(progname); if (rval == 0) { fprintf(stderr, "%s: command not found\n", argv[0]); @@ -1119,18 +1196,21 @@ char *prog, *args; } -caldone() /* done with calculation */ +static void +caldone(void) /* done with calculation */ { - if (childpid == -1) + if (!PDesc.running) return; clearqueue(); - close_process(PDesc); - childpid = -1; + close_process(&PDesc); } -rcalfill(x, y) /* fill with ray-calculated pixel */ -int x, y; +static void +rcalfill( /* fill with ray-calculated pixel */ + int x, + int y +) { if (queuesiz >= packsiz) /* flush queue if needed */ clearqueue(); @@ -1141,7 +1221,8 @@ int x, y; } -clearqueue() /* process queue */ +static void +clearqueue(void) /* process queue */ { FVECT orig, dir; float fbuf[6*(PACKSIZ+1)]; @@ -1160,10 +1241,11 @@ clearqueue() /* process queue */ *fbp++ = dir[0]; *fbp++ = dir[1]; *fbp++ = dir[2]; } /* mark end and get results */ - bzero((char *)fbp, 6*sizeof(float)); - if (process(PDesc, fbuf, fbuf, 4*sizeof(float)*queuesiz, + memset((char *)fbp, '\0', 6*sizeof(float)); + if (process(&PDesc, (char *)fbuf, (char *)fbuf, + 4*sizeof(float)*(queuesiz+1), 6*sizeof(float)*(queuesiz+1)) != - 4*sizeof(float)*queuesiz) { + 4*sizeof(float)*(queuesiz+1)) { fprintf(stderr, "%s: error reading from rtrace process\n", progname); exit(1); @@ -1178,7 +1260,7 @@ clearqueue() /* process queue */ if (averaging) { setcolor(sscan(queue[i][1])[queue[i][0]], fbp[0], fbp[1], fbp[2]); - wscan(queue[i][1])[queue[i][0]] = MAXWT; + wscan(queue[i][1])[queue[i][0]] = 1; } else setcolr(pscan(queue[i][1])[queue[i][0]], fbp[0], fbp[1], fbp[2]); @@ -1196,8 +1278,10 @@ clearqueue() /* process queue */ } -syserror(s) /* report error and exit */ -char *s; +static void +syserror( /* report error and exit */ + char *s +) { perror(s); exit(1);