--- ray/src/px/pcomb.c 1993/04/29 14:32:07 2.10 +++ ray/src/px/pcomb.c 2003/06/08 12:03:10 2.24 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pcomb.c,v 2.24 2003/06/08 12:03:10 schorsch Exp $"; #endif - /* * Combine picture files according to calcomp functions. * @@ -11,24 +8,21 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include "standard.h" - +#include "platform.h" #include "color.h" - -#include "resolu.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 WINSIZ 64 /* scanline window size */ +#define MIDSCN ((WINSIZ-1)/2+1) struct { char *name; /* file or command name */ FILE *fp; /* stream pointer */ VIEW vw; /* view for picture */ RESOLU rs; /* image resolution and orientation */ + float pa; /* pixel aspect ratio */ COLOR *scan[WINSIZ]; /* input scanline window */ COLOR coef; /* coefficient */ COLOR expos; /* recorded exposure */ @@ -36,6 +30,8 @@ struct { int nfiles; /* number of input files */ +char ourfmt[LPICFMT+1] = PICFMT; /* input picture format */ + char Command[] = ""; char vcolin[3][4] = {"ri", "gi", "bi"}; char vcolout[3][4] = {"ro", "go", "bo"}; @@ -43,10 +39,14 @@ char vbrtin[] = "li"; char vbrtout[] = "lo"; char vcolexp[3][4] = {"re", "ge", "be"}; char vbrtexp[] = "le"; +char vpixaspect[] = "pa"; -char vray[6][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz"}; +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"; @@ -81,12 +81,9 @@ char *argv[]; int original; double f; int a, i; -#ifdef MSDOS - extern int _fmode; - _fmode = O_BINARY; - setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); -#endif + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); progname = argv[0]; /* scan options */ for (a = 1; a < argc; a++) { @@ -106,11 +103,14 @@ char *argv[]; } 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); copystruct(&input[nfiles].vw, &stdview); + input[nfiles].pa = 1.0; } nfiles = 0; original = 0; @@ -162,6 +162,7 @@ char *argv[]; colval(input[nfiles].expos,GRN); colval(input[nfiles].coef,BLU) /= colval(input[nfiles].expos,BLU); + setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); } nfiles++; original = 0; @@ -202,7 +203,8 @@ char *argv[]; } /* complete header */ printargs(argc, argv, stdout); - fputformat(COLRFMT, stdout); + if (strcmp(ourfmt, PICFMT)) + fputformat(ourfmt, stdout); /* print format if known */ putchar('\n'); fprtresolu(xres, yres, stdout); /* combine pictures */ @@ -217,17 +219,22 @@ usage: } -tputs(s) /* put out string preceded by a tab */ +tabputs(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 (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 + wrongformat = globmatch(PICFMT, fmt) ? 1 : -1; + return(0); /* don't echo */ } if (isexpos(s)) { /* exposure */ d = exposval(s); @@ -235,11 +242,13 @@ char *s; } else if (iscolcor(s)) { /* color correction */ colcorval(ctmp, s); multcolor(input[nfiles].expos, ctmp); - } else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) + } else if (isaspect(s)) + input[nfiles].pa *= aspectval(s); + else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) gotview++; /* echo line */ putchar('\t'); - fputs(s, stdout); + return(fputs(s, stdout)); } @@ -250,12 +259,16 @@ checkfile() /* ready a file */ gotview = 0; fputs(input[nfiles].name, stdout); fputs(":\n", stdout); - getheader(input[nfiles].fp, tputs, NULL); - if (wrongformat) { + getheader(input[nfiles].fp, tabputs, NULL); + if (wrongformat < 0) { eputs(input[nfiles].name); - eputs(": not in Radiance picture format\n"); + eputs(": not a Radiance picture\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; if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) { @@ -278,11 +291,31 @@ checkfile() /* ready a file */ } +double +rgb_bright(clr) +COLOR clr; +{ + return(bright(clr)); +} + + +double +xyz_bright(clr) +COLOR clr; +{ + return(clr[CIEY]); +} + + +double (*ourbright)() = rgb_bright; + + init() /* perform final setup */ { - double l_colin(), l_expos(), l_ray(); + double l_colin(), l_expos(), l_pixaspect(), l_ray(), l_psize(); register int i; /* define constants */ + varset("PI", ':', PI); varset(vnfiles, ':', (double)nfiles); varset(vxmax, ':', (double)xmax); varset(vymax, ':', (double)ymax); @@ -293,8 +326,16 @@ init() /* perform final setup */ } funset(vbrtexp, 1, ':', l_expos); funset(vbrtin, 1, '=', l_colin); - for (i = 0; i < 6; i++) + 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); } @@ -352,7 +393,7 @@ combine() /* combine pictures */ quit(1); } } - efree(scanout); + efree((char *)scanout); } @@ -394,7 +435,7 @@ register char *nam; if (fn < 0 || fn >= nfiles) return(1.0); if (nam == vbrtexp) - return(bright(input[fn].expos)); + return((*ourbright)(input[fn].expos)); n = 3; while (n--) if (nam == vcolexp[n]) @@ -405,6 +446,18 @@ register char *nam; double +l_pixaspect() /* return pixel aspect ratio */ +{ + register int fn; + + fn = argument(1) - .5; + if (fn < 0 || fn >= nfiles) + return(1.0); + return(input[fn].pa); +} + + +double l_colin(nam) /* return color value for picture */ register char *nam; { @@ -412,10 +465,7 @@ register char *nam; register int n, xoff, yoff; double d; - d = argument(1); - if (d > -.5 && d < .5) - return((double)nfiles); - fn = d - .5; + fn = argument(1) - .5; if (fn < 0 || fn >= nfiles) { errno = EDOM; return(0.0); @@ -451,7 +501,7 @@ register char *nam; } } if (nam == vbrtin) - return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff])); + return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff])); n = 3; while (n--) if (nam == vcolin[n]) @@ -465,35 +515,33 @@ double l_ray(nam) /* return ray origin or direction */ register char *nam; { - static long ltick[MAXINP]; + static unsigned long ltick[MAXINP]; static FVECT lorg[MAXINP], ldir[MAXINP]; + static double ldist[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; + fn = argument(1) - .5; if (fn < 0 || fn >= nfiles) { errno = EDOM; return(0.0); } - if (ltick[fn] < eclock) { /* need to compute? */ + 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; + ldist[fn] = -1.0; if (input[fn].vw.type == 0) - errno = ERANGE; + errno = EDOM; else { - pix2loc(loc, &input[fn].rs, xpos, ypos); - if (viewray(lorg[fn], ldir[fn], - &input[fn].vw, loc[0], loc[1]) < 0) - errno = ERANGE; + pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan); + ldist[fn] = viewray(lorg[fn], ldir[fn], + &input[fn].vw, loc[0], loc[1]); } ltick[fn] = eclock; } - i = 6; + if (nam == vray[i=6]) + return(ldist[fn]); while (i--) if (nam == vray[i]) return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]); @@ -502,6 +550,53 @@ register char *nam; } +double +l_psize() /* compute pixel size in steradians */ +{ + static unsigned long ltick[MAXINP]; + static double psize[MAXINP]; + FVECT dir0, org, dirx, diry; + FLOAT locx[2], locy[2]; + double d; + int fn; + register int i; + + d = argument(1); + if (d < .5 || d >= nfiles+.5) { + errno = EDOM; + return(0.0); + } + fn = d - .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) >= 0) { + 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]) >= 0 && + viewray(org, diry, &input[fn].vw, + locy[0], locy[1]) >= 0) { + /* approximate solid angle */ + for (i = 0; i < 3; i++) { + dirx[i] -= dir0[i]; + diry[i] -= dir0[i]; + } + fcross(dir0, dirx, diry); + psize[fn] = sqrt(DOT(dir0,dir0)); + } + } + ltick[fn] = eclock; + } + return(psize[fn]); +} + + +void wputs(msg) char *msg; { @@ -510,6 +605,7 @@ char *msg; } +void eputs(msg) char *msg; { @@ -517,6 +613,7 @@ char *msg; } +void quit(code) /* exit gracefully */ int code; {