--- ray/src/px/pcomb.c 1989/07/11 17:13:44 1.3 +++ ray/src/px/pcomb.c 1993/04/29 14:32:07 2.10 @@ -1,4 +1,4 @@ -/* Copyright (c) 1989 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -10,89 +10,110 @@ static char SCCSid[] = "$SunId$ LBL"; * 1/4/89 */ -#include +#include "standard.h" -#include - #include "color.h" +#include "resolu.h" + #include "calcomp.h" -#define MAXINP 16 /* maximum number of input files */ +#include "view.h" +#define MAXINP 32 /* maximum number of input files */ +#define WINSIZ 9 /* scanline window size */ +#define MIDSCN 4 /* current scan position */ + struct { - char *name; /* file name */ + char *name; /* file or command name */ FILE *fp; /* stream pointer */ - COLOR *scan; /* input scanline */ + VIEW vw; /* view for picture */ + RESOLU rs; /* image resolution and orientation */ + COLOR *scan[WINSIZ]; /* input scanline window */ COLOR coef; /* coefficient */ + COLOR expos; /* recorded exposure */ } input[MAXINP]; /* input pictures */ int nfiles; /* number of input files */ -char *vcolin[3] = {"ri", "gi", "bi"}; -char *vcolout[3] = {"ro", "go", "bo"}; +char Command[] = ""; +char vcolin[3][4] = {"ri", "gi", "bi"}; +char vcolout[3][4] = {"ro", "go", "bo"}; +char vbrtin[] = "li"; +char vbrtout[] = "lo"; +char vcolexp[3][4] = {"re", "ge", "be"}; +char vbrtexp[] = "le"; -#define vxpos "x" -#define vypos "y" +char vray[6][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz"}; +char vnfiles[] = "nfiles"; +char vxmax[] = "xmax"; +char vymax[] = "ymax"; +char vxres[] = "xres"; +char vyres[] = "yres"; +char vxpos[] = "x"; +char vypos[] = "y"; + int nowarn = 0; /* no warning messages? */ -int xres=0, yres=0; /* picture resolution */ +int xmax = 0, ymax = 0; /* input resolution */ -int xpos, ypos; /* picture position */ +int xscan, yscan; /* input position */ +int xres, yres; /* output resolution */ -tputs(s) /* put out string preceded by a tab */ -char *s; -{ - putchar('\t'); - fputs(s, stdout); -} +int xpos, ypos; /* output position */ +char *progname; /* global argv[0] */ +int wrongformat = 0; +int gotview; + +FILE *popen(); + +extern char *emalloc(); + + main(argc, argv) int argc; char *argv[]; { - extern double l_redin(), l_grnin(), l_bluin(), atof(); + int original; double f; - int a; - - funset(vcolin[RED], 1, l_redin); - funset(vcolin[GRN], 1, l_grnin); - funset(vcolin[BLU], 1, l_bluin); - - for (a = 1; a < argc; a++) + int a, i; +#ifdef MSDOS + extern int _fmode; + _fmode = O_BINARY; + setmode(fileno(stdin), O_BINARY); + setmode(fileno(stdout), O_BINARY); +#endif + progname = argv[0]; + /* scan options */ + for (a = 1; a < argc; a++) { if (argv[a][0] == '-') switch (argv[a][1]) { - case '\0': - case 's': - case 'c': - goto getfiles; case 'x': - xres = atoi(argv[++a]); - break; case 'y': - yres = atoi(argv[++a]); - break; + a++; + continue; case 'w': nowarn = !nowarn; - break; + continue; case 'f': - fcompile(argv[++a]); - break; case 'e': - scompile(NULL, argv[++a]); - break; - default: - goto usage; + a++; + continue; } - else - break; -getfiles: - for (nfiles = 0; nfiles < MAXINP; nfiles++) + break; + } + /* process files */ + for (nfiles = 0; nfiles < MAXINP; nfiles++) { setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); + setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); + copystruct(&input[nfiles].vw, &stdview); + } nfiles = 0; + original = 0; for ( ; a < argc; a++) { if (nfiles >= MAXINP) { eputs(argv[0]); @@ -105,6 +126,9 @@ getfiles: input[nfiles].name = ""; input[nfiles].fp = stdin; break; + case 'o': + original++; + continue; case 's': f = atof(argv[++a]); scalecolor(input[nfiles].coef, f); @@ -118,123 +142,363 @@ getfiles: goto usage; } else { - input[nfiles].name = argv[a]; - input[nfiles].fp = fopen(argv[a], "r"); + if (argv[a][0] == '!') { + input[nfiles].name = Command; + input[nfiles].fp = popen(argv[a]+1, "r"); + } else { + input[nfiles].name = argv[a]; + input[nfiles].fp = fopen(argv[a], "r"); + } if (input[nfiles].fp == NULL) { - eputs(argv[a]); - eputs(": cannot open\n"); + perror(argv[a]); quit(1); } } - fputs(input[nfiles].name, stdout); - fputs(":\n", stdout); - getheader(input[nfiles].fp, tputs); - if (fscanf(input[nfiles].fp, "-Y %d +X %d\n", &ypos, &xpos) != 2) { - eputs(input[nfiles].name); - eputs(": bad picture size\n"); - quit(1); + checkfile(); + if (original) { + colval(input[nfiles].coef,RED) /= + colval(input[nfiles].expos,RED); + colval(input[nfiles].coef,GRN) /= + colval(input[nfiles].expos,GRN); + colval(input[nfiles].coef,BLU) /= + colval(input[nfiles].expos,BLU); } - if (xres == 0 && yres == 0) { - xres = xpos; - yres = ypos; - } else if (xpos != xres || ypos != yres) { - eputs(argv[0]); - eputs(": resolution mismatch\n"); - quit(1); - } - input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR)); nfiles++; + original = 0; } + init(); /* set constants */ + /* go back and get expressions */ + for (a = 1; a < argc; a++) { + if (argv[a][0] == '-') + switch (argv[a][1]) { + case 'x': + varset(vxres, ':', eval(argv[++a])); + continue; + case 'y': + varset(vyres, ':', eval(argv[++a])); + continue; + case 'w': + continue; + case 'f': + fcompile(argv[++a]); + continue; + case 'e': + scompile(argv[++a], NULL, 0); + continue; + } + break; + } + /* set/get output resolution */ + if (!vardefined(vxres)) + varset(vxres, ':', (double)xmax); + if (!vardefined(vyres)) + varset(vyres, ':', (double)ymax); + xres = varvalue(vxres) + .5; + yres = varvalue(vyres) + .5; + if (xres <= 0 || yres <= 0) { + eputs(argv[0]); + eputs(": illegal output resolution\n"); + quit(1); + } + /* complete header */ printargs(argc, argv, stdout); + fputformat(COLRFMT, stdout); putchar('\n'); - printf("-Y %d +X %d\n", yres, xres); + fprtresolu(xres, yres, stdout); + /* combine pictures */ combine(); quit(0); usage: eputs("Usage: "); eputs(argv[0]); -eputs(" [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] picture ..]\n"); + eputs( +" [-w][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] pic ..]\n"); quit(1); } +tputs(s) /* put out string preceded by a tab */ +char *s; +{ + char fmt[32]; + double d; + COLOR ctmp; + + if (isformat(s)) { /* check format */ + formatval(fmt, s); + wrongformat = strcmp(fmt, COLRFMT); + return; /* don't echo */ + } + if (isexpos(s)) { /* exposure */ + d = exposval(s); + scalecolor(input[nfiles].expos, d); + } else if (iscolcor(s)) { /* color correction */ + colcorval(ctmp, s); + multcolor(input[nfiles].expos, ctmp); + } else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) + gotview++; + /* echo line */ + putchar('\t'); + fputs(s, stdout); +} + + +checkfile() /* ready a file */ +{ + register int i; + /* process header */ + gotview = 0; + fputs(input[nfiles].name, stdout); + fputs(":\n", stdout); + getheader(input[nfiles].fp, tputs, NULL); + if (wrongformat) { + eputs(input[nfiles].name); + eputs(": not in Radiance picture format\n"); + quit(1); + } + if (!gotview || setview(&input[nfiles].vw) != NULL) + input[nfiles].vw.type = 0; + if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) { + eputs(input[nfiles].name); + eputs(": bad picture size\n"); + quit(1); + } + if (xmax == 0 && ymax == 0) { + xmax = scanlen(&input[nfiles].rs); + ymax = numscans(&input[nfiles].rs); + } else if (scanlen(&input[nfiles].rs) != xmax || + numscans(&input[nfiles].rs) != ymax) { + eputs(input[nfiles].name); + eputs(": resolution mismatch\n"); + quit(1); + } + /* allocate scanlines */ + for (i = 0; i < WINSIZ; i++) + input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR)); +} + + +init() /* perform final setup */ +{ + double l_colin(), l_expos(), l_ray(); + register int i; + /* define constants */ + varset(vnfiles, ':', (double)nfiles); + varset(vxmax, ':', (double)xmax); + varset(vymax, ':', (double)ymax); + /* set functions */ + for (i = 0; i < 3; i++) { + funset(vcolexp[i], 1, ':', l_expos); + funset(vcolin[i], 1, '=', l_colin); + } + funset(vbrtexp, 1, ':', l_expos); + funset(vbrtin, 1, '=', l_colin); + for (i = 0; i < 6; i++) + funset(vray[i], 1, '=', l_ray); +} + + combine() /* combine pictures */ { - int coldef[3]; + EPNODE *coldef[3], *brtdef; COLOR *scanout; + double d; register int i, j; /* check defined variables */ - for (j = 0; j < 3; j++) - coldef[j] = vardefined(vcolout[j]); + for (j = 0; j < 3; j++) { + if (vardefined(vcolout[j])) + coldef[j] = eparse(vcolout[j]); + else + coldef[j] = NULL; + } + if (vardefined(vbrtout)) + brtdef = eparse(vbrtout); + else + brtdef = NULL; /* allocate scanline */ scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); + /* set input position */ + yscan = ymax+MIDSCN; /* combine files */ for (ypos = yres-1; ypos >= 0; ypos--) { - for (i = 0; i < nfiles; i++) - if (freadscan(input[i].scan, xres, input[i].fp) < 0) { - eputs(input[i].name); - eputs(": read error\n"); - quit(1); + advance(); + varset(vypos, '=', (double)ypos); + for (xpos = 0; xpos < xres; xpos++) { + xscan = (long)xpos*xmax/xres; + varset(vxpos, '=', (double)xpos); + eclock++; + if (brtdef != NULL) { + d = evalue(brtdef); + if (d < 0.0) + d = 0.0; + setcolor(scanout[xpos], d, d, d); + } else { + for (j = 0; j < 3; j++) { + if (coldef[j] != NULL) { + d = evalue(coldef[j]); + } else { + d = 0.0; + for (i = 0; i < nfiles; i++) + d += colval(input[i].scan[MIDSCN][xscan],j); } - varset(vypos, (double)ypos); - for (xpos = 0; xpos < xres; xpos++) { - varset(vxpos, (double)xpos); - eclock++; - for (j = 0; j < 3; j++) - if (coldef[j]) { - colval(scanout[xpos],j) = - varvalue(vcolout[j]); - if (colval(scanout[xpos],j) < 0.0) - colval(scanout[xpos],j) = 0.0; - } else { - colval(scanout[xpos],j) = 0.0; - for (i = 0; i < nfiles; i++) - colval(scanout[xpos],j) += - colval(input[i].coef,j) * - colval(input[i].scan[xpos],j); - } + if (d < 0.0) + d = 0.0; + colval(scanout[xpos],j) = d; + } } - if (fwritescan(scanout, xres, stdout) < 0) { - eputs("write error\n"); - quit(1); - } + } + if (fwritescan(scanout, xres, stdout) < 0) { + perror("write error"); + quit(1); + } } efree(scanout); } -double -colin(fn, ci) /* return color value for picture */ -register int fn, ci; +advance() /* read in data for next scanline */ { - if (fn == 0) - return((double)nfiles); - if (fn < 1 || fn > nfiles) { - errno = EDOM; - return(0.0); - } - return(colval(input[fn-1].scan[xpos],ci)); + int ytarget; + register COLOR *st; + register int i, j; + + for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--) + for (i = 0; i < nfiles; i++) { + st = input[i].scan[WINSIZ-1]; + for (j = WINSIZ-1; j > 0; j--) /* rotate window */ + input[i].scan[j] = input[i].scan[j-1]; + input[i].scan[0] = st; + if (yscan <= MIDSCN) /* hit bottom? */ + continue; + if (freadscan(st, xmax, input[i].fp) < 0) { /* read */ + eputs(input[i].name); + eputs(": read error\n"); + quit(1); + } + if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 || + fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 || + fabs(colval(input[i].coef,BLU)-1.0) > 1e-3) + for (j = 0; j < xmax; j++) /* adjust color */ + multcolor(st[j], input[i].coef); + } } double -l_redin() /* get red color */ +l_expos(nam) /* return picture exposure */ +register char *nam; { - return(colin((int)(argument(1)+.5), RED)); + register int fn, n; + + fn = argument(1) - .5; + if (fn < 0 || fn >= nfiles) + return(1.0); + if (nam == vbrtexp) + return(bright(input[fn].expos)); + n = 3; + while (n--) + if (nam == vcolexp[n]) + return(colval(input[fn].expos,n)); + eputs("Bad call to l_expos()!\n"); + quit(1); } double -l_grnin() /* get green color */ +l_colin(nam) /* return color value for picture */ +register char *nam; { - return(colin((int)(argument(1)+.5), GRN)); + int fn; + register int n, xoff, yoff; + double d; + + d = argument(1); + if (d > -.5 && d < .5) + return((double)nfiles); + fn = d - .5; + if (fn < 0 || fn >= nfiles) { + errno = EDOM; + return(0.0); + } + xoff = yoff = 0; + n = nargum(); + if (n >= 2) { + d = argument(2); + if (d < 0.0) { + xoff = d-.5; + if (xscan+xoff < 0) + xoff = -xscan; + } else { + xoff = d+.5; + if (xscan+xoff >= xmax) + xoff = xmax-1-xscan; + } + } + if (n >= 3) { + d = argument(3); + if (d < 0.0) { + yoff = d-.5; + if (yoff+MIDSCN < 0) + yoff = -MIDSCN; + if (yscan+yoff < 0) + yoff = -yscan; + } else { + yoff = d+.5; + if (yoff+MIDSCN >= WINSIZ) + yoff = WINSIZ-1-MIDSCN; + if (yscan+yoff >= ymax) + yoff = ymax-1-yscan; + } + } + if (nam == vbrtin) + return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff])); + n = 3; + while (n--) + if (nam == vcolin[n]) + return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n)); + eputs("Bad call to l_colin()!\n"); + quit(1); } double -l_bluin() /* get blue color */ +l_ray(nam) /* return ray origin or direction */ +register char *nam; { - return(colin((int)(argument(1)+.5), BLU)); + static long ltick[MAXINP]; + static FVECT lorg[MAXINP], ldir[MAXINP]; + FLOAT loc[2]; + double d; + int fn; + register int i; + + d = argument(1); + if (d > -.5 && d < .5) + return((double)nfiles); + fn = d - .5; + if (fn < 0 || fn >= nfiles) { + errno = EDOM; + return(0.0); + } + if (ltick[fn] < eclock) { /* need to compute? */ + lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0; + ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0; + if (input[fn].vw.type == 0) + errno = ERANGE; + else { + pix2loc(loc, &input[fn].rs, xpos, ypos); + if (viewray(lorg[fn], ldir[fn], + &input[fn].vw, loc[0], loc[1]) < 0) + errno = ERANGE; + } + ltick[fn] = eclock; + } + i = 6; + while (i--) + if (nam == vray[i]) + return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]); + eputs("Bad call to l_ray()!\n"); + quit(1); } @@ -253,8 +517,15 @@ char *msg; } -quit(code) -int code; +quit(code) /* exit gracefully */ +int code; { + register int i; + /* close input files */ + for (i = 0; i < nfiles; i++) + if (input[i].name == Command) + pclose(input[i].fp); + else + fclose(input[i].fp); exit(code); }