--- ray/src/px/pcomb.c 1991/05/23 13:46:05 1.14 +++ ray/src/px/pcomb.c 2023/12/09 23:46:51 2.57 @@ -1,133 +1,149 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pcomb.c,v 2.57 2023/12/09 23:46:51 greg Exp $"; #endif - /* * Combine picture files according to calcomp functions. * * 1/4/89 */ -#include - -#include - +#include "platform.h" +#include "standard.h" +#include "paths.h" #include "color.h" - #include "calcomp.h" +#include "view.h" -#define MAXINP 32 /* maximum number of input files */ -#define WINSIZ 9 /* scanline window size */ -#define MIDSCN 4 /* current scan position */ +#define MAXINP 1024 /* maximum number of input files */ +#define WINSIZ 127 /* scanline window size */ +#define MIDSCN ((WINSIZ-1)/2+1) -#define BRT (-1) /* special index for brightness */ - struct { - char *name; /* file name */ + char *name; /* file or command name */ FILE *fp; /* stream pointer */ + VIEW vw; /* view for picture */ + RESOLU rs; /* image resolution and orientation */ + int infloat; /* input is floating point (#comp)? */ + float pa; /* pixel aspect ratio */ 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"}; -#define vbrtin "li" -#define vbrtout "lo" +VIEW *commvp = NULL; /* common view parameters */ -#define vnfiles "nfiles" -#define vxres "xres" -#define vyres "yres" -#define vxpos "x" -#define vypos "y" +char ourfmt[MAXFMTLEN] = PICFMT; /* input picture format */ +int outfloat = 0; /* #component float output? */ +char StandardInput[] = ""; +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"; +char vpixaspect[] = "pa"; + +char vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"}; + +char vpsize[] = "S"; + +char vnfiles[] = "nfiles"; +char vwhteff[] = "WE"; +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 original = 0; /* origninal values? */ +int xmax = 0, ymax = 0; /* input resolution */ -int xres=0, yres=0; /* picture resolution */ +int xscan, yscan; /* input position */ -int xpos, ypos; /* picture position */ +int xres, yres; /* output resolution */ -int wrongformat = 0; +int xpos, ypos; /* output position */ +char *progname; /* global argv[0] */ -tputs(s) /* put out string preceded by a tab */ -char *s; -{ - char fmt[32]; - double d; - COLOR ctmp; +int echoheader = 1; +int wrongformat = 0; +int gotview; - if (isformat(s)) { /* check format */ - formatval(fmt, s); - wrongformat = strcmp(fmt, COLRFMT); - } else if (original && isexpos(s)) { /* exposure */ - d = 1.0/exposval(s); - scalecolor(input[nfiles].coef, d); - } else if (original && iscolcor(s)) { /* color correction */ - colcorval(ctmp, s); - colval(input[nfiles].coef,RED) /= colval(ctmp,RED); - colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN); - colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU); - } else { /* echo unaffected line */ - putchar('\t'); - fputs(s, stdout); - } -} +static gethfunc headline; +static void checkfile(int orig); +static double rgb_bright(COLOR clr); +static double xyz_bright(COLOR clr); +static void init(void); +static void combine(void); +static void advance(void); +static double l_expos(char *nam); +static double l_pixaspect(char *nm); +static double l_colin(char *nam); +static double l_ray(char *nam); +static double l_psize(char *nm); -main(argc, argv) -int argc; -char *argv[]; +int +main( + int argc, + char *argv[] +) { - extern double l_redin(), l_grnin(), l_bluin(), l_brtin(), atof(); + int original; double f; - int a, i; - - funset(vcolin[RED], 1, '=', l_redin); - funset(vcolin[GRN], 1, '=', l_grnin); - funset(vcolin[BLU], 1, '=', l_bluin); - funset(vbrtin, 1, '=', l_brtin); - - for (a = 1; a < argc; a++) + int a; + + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); + progname = argv[0]; + esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; + esupport &= ~(E_OUTCHAN|E_INCHAN); + /* 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; - case 'o': - original = !original; - break; + continue; + case 'h': + echoheader = !echoheader; + continue; case 'f': - fcompile(argv[++a]); - break; + if (argv[a][2]) { + outfloat = (argv[a][2] == 'f'); + continue; + } + /* fall through */ case 'e': - scompile(argv[++a], NULL, 0); - break; - default: - goto usage; + a++; + continue; } - else - break; -getfiles: - for (nfiles = 0; nfiles < MAXINP; nfiles++) + break; + } + newheader("RADIANCE", stdout); /* start header */ + fputnow(stdout); + /* 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); + input[nfiles].vw = stdview; + input[nfiles].pa = 1.0; + } nfiles = 0; + original = 0; for ( ; a < argc; a++) { if (nfiles >= MAXINP) { eputs(argv[0]); @@ -137,9 +153,12 @@ getfiles: if (argv[a][0] == '-') switch (argv[a][1]) { case '\0': - input[nfiles].name = ""; + input[nfiles].name = StandardInput; input[nfiles].fp = stdin; break; + case 'o': + original++; + continue; case 's': f = atof(argv[++a]); scalecolor(input[nfiles].coef, f); @@ -153,61 +172,290 @@ 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) { perror(argv[a]); quit(1); } } - 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 (fgetresolu(&xpos, &ypos, input[nfiles].fp) != - (YMAJOR|YDECR)) { - eputs(input[nfiles].name); - eputs(": bad picture size\n"); - quit(1); - } - if (xres == 0 && yres == 0) { - xres = xpos; - yres = ypos; - } else if (xpos != xres || ypos != yres) { - eputs(argv[0]); - eputs(": resolution mismatch\n"); - quit(1); - } - for (i = 0; i < WINSIZ; i++) - input[nfiles].scan[i] = - (COLOR *)emalloc(xres*sizeof(COLOR)); + checkfile(original); nfiles++; + original = 0; } - printargs(argc, argv, stdout); - fputformat(COLRFMT, stdout); - putchar('\n'); - fputresolu(YMAJOR|YDECR, xres, yres, stdout); - combine(); + init(); /* set constants */ + /* go back and get expressions */ + for (a = 1; a < argc; a++) { + char *fpath; + 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 'h': + continue; + case 'f': + if (argv[a][2]) + continue; + fpath = getpath(argv[++a], getrlibpath(), 0); + if (fpath == NULL) { + eputs(argv[0]); + eputs(": cannot find file '"); + eputs(argv[a]); + eputs("'\n"); + quit(1); + } + fcompile(fpath); + continue; + case 'e': + scompile(argv[++a], NULL, 0); + continue; + } + break; + } + /* get output resolution */ + xres = varvalue(vxres) + .5; + yres = varvalue(vyres) + .5; + if (xres <= 0 || yres <= 0) { + eputs(argv[0]); + eputs(": illegal output resolution\n"); + quit(1); + } + if (!vardefined(vbrtout)) /* single or 3-channel? */ + outfloat *= 3; + + printargs(argc, argv, stdout); /* complete header */ + if (commvp != NULL) { + fputs(VIEWSTR, stdout); + fprintview(commvp, stdout); + fputc('\n', stdout); + } + if (outfloat) { /* print format if known */ + printf("NROWS=%d\nNCOLS=%d\n", yres, xres); + fputncomp(outfloat, stdout); + fputendian(stdout); + fputformat("float", stdout); + } else if (strcmp(ourfmt, PICFMT)) + fputformat(ourfmt, stdout); + fputc('\n', stdout); /* end header */ + if (!outfloat) + fprtresolu(xres, yres, stdout); + + combine(); /* combine pictures */ quit(0); usage: eputs("Usage: "); eputs(argv[0]); eputs( -" [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n"); +" [-w][-h][-ff][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n"); quit(1); + return 1; /* pro forma return */ } -combine() /* combine pictures */ +static int +headline( /* check header line & echo if requested */ + char *s, + void *p +) { + int orig = *(int *)p; + char fmt[MAXFMTLEN]; + double d; + int bigend; + COLOR ctmp; + + if (isheadid(s)) /* header id */ + return(0); /* don't echo */ + if (formatval(fmt, s)) { /* check format */ + if (globmatch(ourfmt, fmt)) { + wrongformat = 0; + strcpy(ourfmt, fmt); + } else if (!strcmp("float", fmt)) + input[nfiles].infloat *= -1; + else + wrongformat = globmatch(PICFMT, fmt) ? 1 : -1; + return(0); /* don't echo */ + } + if ((bigend = isbigendian(s)) >= 0) { + if (bigend != nativebigendian()) { + eputs(input[nfiles].name); + eputs(": unsupported input byte ordering\n"); + quit(1); + } + return(0); /* don't echo */ + } + if (!strncmp("NROWS=", s, 6)) { /* X-resolution */ + input[nfiles].rs.yr = atoi(s+6); + return(0); /* don't echo */ + } + if (!strncmp("NCOLS=", s, 6)) { /* Y-resolution */ + input[nfiles].rs.xr = atoi(s+6); + return(0); /* don't echo */ + } + if (isncomp(s)) /* # components */ + input[nfiles].infloat *= ncompval(s); + + if (orig) { /* undo exposure? */ + if (isexpos(s)) { + d = 1./exposval(s); + scalecolor(input[nfiles].coef, d); + return(0); /* don't echo */ + } + if (iscolcor(s)) { + int i; + colcorval(ctmp, s); + for (i = 3; i--; ) + colval(input[nfiles].coef,i) /= colval(ctmp,i); + return(0); /* don't echo */ + } + } else if (isexpos(s)) { /* record exposure? */ + d = exposval(s); + scalecolor(input[nfiles].expos, d); + } else if (iscolcor(s)) { + colcorval(ctmp, s); + multcolor(input[nfiles].expos, ctmp); + } + if (isaspect(s)) + input[nfiles].pa *= aspectval(s); + else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) + gotview++; + + if (echoheader) { /* echo line */ + putchar('\t'); + return(fputs(s, stdout)); + } + return(0); +} + + +static void +checkfile(int orig) /* ready a file */ +{ + int i; + /* process header */ + gotview = 0; + input[nfiles].infloat = -1; + input[nfiles].rs.rt = PIXSTANDARD; + input[nfiles].rs.xr = input[nfiles].rs.yr = 0; + if (echoheader) { + fputs(input[nfiles].name, stdout); + fputs(":\n", stdout); + } + getheader(input[nfiles].fp, headline, &orig); + + if (input[nfiles].infloat <= 0) + input[nfiles].infloat = 0; + else if ((input[nfiles].infloat != 3) & + (input[nfiles].infloat != 1)) { + eputs(input[nfiles].name); + eputs(": unsupported number of components\n"); + quit(1); + } + if (wrongformat < 0) { + eputs(input[nfiles].name); + eputs(": not a Radiance picture or float matrix\n"); + quit(1); + } + if (wrongformat > 0) { + wputs(input[nfiles].name); + wputs(": warning -- incompatible picture format\n"); + } + if (!gotview || setview(&input[nfiles].vw) != NULL) + input[nfiles].vw.type = 0; + else if (commvp == NULL) + commvp = &input[nfiles].vw; + if ((input[nfiles].rs.xr <= 0) | (input[nfiles].rs.yr <= 0) && + !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)); +} + + +static double +rgb_bright( + COLOR clr +) +{ + return(bright(clr)); +} + + +static double +xyz_bright( + COLOR clr +) +{ + return(clr[CIEY]); +} + + +double (*ourbright)() = rgb_bright; + + +static void +init(void) /* perform final setup */ +{ + int i; + /* define constants */ + varset("PI", ':', PI); + 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); + funset(vpixaspect, 1, ':', l_pixaspect); + for (i = 0; i < 7; i++) + funset(vray[i], 1, '=', l_ray); + funset(vpsize, 1, '=', l_psize); + /* set brightness function */ + if (!strcmp(ourfmt, CIEFMT)) { + varset(vwhteff, ':', 1.0); + ourbright = xyz_bright; + } else + varset(vwhteff, ':', WHTEFFICACY); + /* these may be overridden */ + varset(vxres, ':', (double)xmax); + varset(vyres, ':', (double)ymax); +} + + +static void +combine(void) /* combine pictures */ +{ EPNODE *coldef[3], *brtdef; COLOR *scanout; double d; - register int i, j; + int i, j; /* check defined variables */ for (j = 0; j < 3; j++) { if (vardefined(vcolout[j])) @@ -219,25 +467,21 @@ combine() /* combine pictures */ brtdef = eparse(vbrtout); else brtdef = NULL; - /* define constants */ - varset(vnfiles, ':', (double)nfiles); - varset(vxres, ':', (double)xres); - varset(vyres, ':', (double)yres); /* allocate scanline */ scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); - /* initialize input */ - initinp(); + /* set input position */ + yscan = ymax+MIDSCN; /* combine files */ for (ypos = yres-1; ypos >= 0; ypos--) { advance(); varset(vypos, '=', (double)ypos); for (xpos = 0; xpos < xres; xpos++) { + xscan = (xpos+.5)*xmax/xres; varset(vxpos, '=', (double)xpos); eclock++; if (brtdef != NULL) { d = evalue(brtdef); - if (d < 0.0) - d = 0.0; + d *= (outfloat > 0) | (d >= 0); setcolor(scanout[xpos], d, d, d); } else { for (j = 0; j < 3; j++) { @@ -246,80 +490,153 @@ combine() /* combine pictures */ } else { d = 0.0; for (i = 0; i < nfiles; i++) - d += colval(input[i].scan[MIDSCN][xpos],j); + d += colval(input[i].scan[MIDSCN][xscan],j); } - if (d < 0.0) - d = 0.0; + d *= (outfloat > 0) | (d >= 0); colval(scanout[xpos],j) = d; } } } - if (fwritescan(scanout, xres, stdout) < 0) { - perror("write error"); - quit(1); + switch (outfloat) { + case 3: /* writing out float triplets */ + if (fwrite(scanout, sizeof(float)*3, xres, stdout) != xres) + goto writerr; + break; + case 1: /* writing out gray float values */ + for (xpos = 0; xpos < xres; xpos++) + if (putbinary(&scanout[xpos][CIEY], + sizeof(float), 1, stdout) != 1) + goto writerr; + break; + default: /* writing out Radiance picture */ + if (fwritescan(scanout, xres, stdout) < 0) + goto writerr; + break; } } - efree(scanout); + efree((char *)scanout); + return; +writerr: + perror("write error"); + quit(1); } -initinp() /* initilize scan windows */ +static void +advance(void) /* read in data for next scanline */ { - for (ypos = yres+(MIDSCN-1); ypos >= yres; ypos--) - advance(); + int ytarget; + COLOR *st; + int i, j; + + for (ytarget = (ypos+.5)*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; + /* read scanline */ + if (input[i].infloat) { + if (fread(st, sizeof(float)*input[i].infloat, + xmax, input[i].fp) != xmax) + goto readerr; + if (input[i].infloat == 1) { + const COLORV *f = st[0] + xmax; + int x = xmax; + while (x-- > 0) + st[x][BLU] = st[x][GRN] = + st[x][RED] = *--f; + } + } else if (freadscan(st, xmax, input[i].fp) < 0) + goto readerr; + for (j = 0; j < xmax; j++) /* adjust color */ + multcolor(st[j], input[i].coef); + } + return; +readerr: + eputs(input[i].name); + eputs(": read error\n"); + quit(1); } -advance() /* read in next scanline */ +static double +l_expos( /* return picture exposure */ + char *nam +) { - register COLOR *st; - register int i, j; + int fn, n; + double d; - 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 (ypos < MIDSCN) /* hit bottom */ - continue; - if (freadscan(st, xres, input[i].fp) < 0) { - eputs(input[i].name); - eputs(": read error\n"); - quit(1); - } - for (j = 0; j < xres; j++) /* adjust color */ - multcolor(st[j], input[i].coef); + d = argument(1); + if (d <= -0.5 || d >= nfiles+0.5) { + errno = EDOM; + return(0.0); } + if (d < 0.5) + return((double)nfiles); + fn = d - 0.5; + if (nam == vbrtexp) + return((*ourbright)(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); + return 1; /* pro forma return */ } -double -colin(ci) /* return color value for picture */ -register int ci; +static double +l_pixaspect(char *nm) /* return pixel aspect ratio */ { - int n, fn; - register int xoff, yoff; + int fn; double d; - fn = (d = argument(1)) - .5; - if (d <= -.5 || fn >= nfiles) { + d = argument(1); + if (d <= -0.5 || d >= nfiles+0.5) { errno = EDOM; return(0.0); } - if (d < .5) + if (d < 0.5) return((double)nfiles); + fn = d - 0.5; + return(input[fn].pa); +} + + +static double +l_colin( /* return color value for picture */ + char *nam +) +{ + int fn; + int n, xoff, yoff; + double d; + + d = argument(1); + if (d <= -0.5 || d >= nfiles+0.5) { + errno = EDOM; + return(0.0); + } + if (d < 0.5) + return((double)nfiles); + fn = d - 0.5; xoff = yoff = 0; n = nargum(); if (n >= 2) { d = argument(2); if (d < 0.0) { xoff = d-.5; - if (xpos+xoff < 0) - xoff = -xpos; + if (xscan+xoff < 0) + xoff = -xscan; } else { xoff = d+.5; - if (xpos+xoff >= xres) - xoff = xres-1-xpos; + if (xscan+xoff >= xmax) + xoff = xmax-1-xscan; } } if (n >= 3) { @@ -328,67 +645,147 @@ register int ci; yoff = d-.5; if (yoff+MIDSCN < 0) yoff = -MIDSCN; - if (ypos+yoff < 0) - yoff = -ypos; + if (yscan+yoff < 0) + yoff = -yscan; } else { yoff = d+.5; if (yoff+MIDSCN >= WINSIZ) yoff = WINSIZ-1-MIDSCN; - if (ypos+yoff >= yres) - yoff = yres-1-ypos; + if (yscan+yoff >= ymax) + yoff = ymax-1-yscan; } } - if (ci == BRT) - return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff])); - return(colval(input[fn].scan[MIDSCN+yoff][xpos+xoff],ci)); + if (nam == vbrtin) + return((*ourbright)(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); + return 1; /* pro forma return */ } -double -l_redin() /* get red color */ +static double +l_ray( /* return ray origin or direction */ + char *nam +) { - return(colin(RED)); -} + static unsigned long ltick[MAXINP]; + static FVECT lorg[MAXINP], ldir[MAXINP]; + static double ldist[MAXINP]; + RREAL loc[2]; + double d; + int fn; + int i; - -double -l_grnin() /* get green color */ -{ - return(colin(GRN)); + d = argument(1); + if (d <= -0.5 || d >= nfiles+0.5) { + errno = EDOM; + return(0.0); + } + if (d < 0.5) + return((double)nfiles); + fn = d - 0.5; + if (ltick[fn] != eclock) { /* need to compute? */ + if (input[fn].vw.type == 0) { + lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0; + ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0; + ldist[fn] = -1.0; + errno = EDOM; + } else { + pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan); + ldist[fn] = viewray(lorg[fn], ldir[fn], + &input[fn].vw, loc[0], loc[1]); + if (ldist[fn] < -FTINY) + errno = EDOM; + } + ltick[fn] = eclock; + } + if (nam == vray[i=6]) + return(ldist[fn]); + 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); + return 1; /* pro forma return */ } -double -l_bluin() /* get blue color */ +static double +l_psize(char *nm) /* compute pixel size in steradians */ { - return(colin(BLU)); -} + static unsigned long ltick[MAXINP]; + static double psize[MAXINP]; + FVECT dir0, org, dirx, diry; + RREAL locx[2], locy[2]; + double d; + int fn; + int i; - -double -l_brtin() /* get brightness value */ -{ - return(colin(BRT)); + d = argument(1); + if (d <= -0.5 || d >= nfiles+0.5) { + errno = EDOM; + return(0.0); + } + if (d < 0.5) + return((double)nfiles); + fn = d - 0.5; + if (ltick[fn] != eclock) { /* need to compute? */ + psize[fn] = 0.0; + if (input[fn].vw.type == 0) + errno = EDOM; + else if (input[fn].vw.type != VT_PAR && + funvalue(vray[6], 1, &d) >= -FTINY) { + for (i = 0; i < 3; i++) + dir0[i] = funvalue(vray[3+i], 1, &d); + pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan); + pix2loc(locy, &input[fn].rs, xscan, ymax-yscan); + if (viewray(org, dirx, &input[fn].vw, + locx[0], locx[1]) >= -FTINY && + viewray(org, diry, &input[fn].vw, + locy[0], locy[1]) >= -FTINY) { + /* approximate solid angle */ + for (i = 0; i < 3; i++) { + dirx[i] -= dir0[i]; + diry[i] -= dir0[i]; + } + fcross(dir0, dirx, diry); + psize[fn] = VLEN(dir0); + } + } + ltick[fn] = eclock; + } + return(psize[fn]); } -wputs(msg) -char *msg; +extern void +wputs(const char *msg) { if (!nowarn) eputs(msg); } -eputs(msg) -char *msg; +extern void +eputs(const char *msg) { fputs(msg, stderr); } -quit(code) -int code; +extern void +quit(int code) /* exit gracefully */ { + 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); }