--- ray/src/px/pcomb.c 2005/02/16 03:26:33 2.36 +++ ray/src/px/pcomb.c 2024/02/23 03:47:57 2.59 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pcomb.c,v 2.36 2005/02/16 03:26:33 greg Exp $"; +static const char RCSid[] = "$Id: pcomb.c,v 2.59 2024/02/23 03:47:57 greg Exp $"; #endif /* * Combine picture files according to calcomp functions. @@ -9,13 +9,13 @@ static const char RCSid[] = "$Id: pcomb.c,v 2.36 2005/ #include "platform.h" #include "standard.h" -#include "rtprocess.h" +#include "paths.h" #include "color.h" #include "calcomp.h" #include "view.h" -#define MAXINP 32 /* maximum number of input files */ -#define WINSIZ 64 /* scanline window size */ +#define MAXINP 1024 /* maximum number of input files */ +#define WINSIZ 127 /* scanline window size */ #define MIDSCN ((WINSIZ-1)/2+1) struct { @@ -23,6 +23,7 @@ struct { 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 */ @@ -31,8 +32,11 @@ struct { int nfiles; /* number of input files */ -char ourfmt[LPICFMT+1] = PICFMT; /* input picture format */ +VIEW *commvp = NULL; /* common view parameters */ +char ourfmt[MAXFMTLEN] = PICFMT; /* input picture format */ +int outfloat = 0; /* #component float output? */ + char StandardInput[] = ""; char Command[] = ""; char vcolin[3][4] = {"ri", "gi", "bi"}; @@ -68,12 +72,13 @@ int xpos, ypos; /* output position */ char *progname; /* global argv[0] */ +int echoheader = 1; int wrongformat = 0; int gotview; -static gethfunc tabputs; -static void checkfile(void); +static gethfunc headline; +static void checkfile(int orig); static double rgb_bright(COLOR clr); static double xyz_bright(COLOR clr); static void init(void); @@ -95,10 +100,13 @@ main( int original; double f; 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] == '-') @@ -110,7 +118,15 @@ main( case 'w': nowarn = !nowarn; continue; + case 'h': + echoheader = !echoheader; + continue; case 'f': + if (argv[a][2]) { + outfloat = (argv[a][2] == 'f'); + continue; + } + /* fall through */ case 'e': a++; continue; @@ -168,22 +184,14 @@ main( 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); - setcolor(input[nfiles].expos, 1.0, 1.0, 1.0); - } + checkfile(original); nfiles++; original = 0; } 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': @@ -194,8 +202,20 @@ main( continue; case 'w': continue; + case 'h': + continue; case 'f': - fcompile(argv[++a]); + 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); @@ -203,11 +223,7 @@ main( } break; } - /* set/get output resolution */ - if (!vardefined(vxres)) - varset(vxres, ':', (double)xmax); - if (!vardefined(vyres)) - varset(vyres, ':', (double)ymax); + /* get output resolution */ xres = varvalue(vxres) + .5; yres = varvalue(vyres) + .5; if (xres <= 0 || yres <= 0) { @@ -215,33 +231,49 @@ main( eputs(": illegal output resolution\n"); quit(1); } - /* complete header */ - printargs(argc, argv, stdout); - if (strcmp(ourfmt, PICFMT)) - fputformat(ourfmt, stdout); /* print format if known */ - putchar('\n'); - fprtresolu(xres, yres, stdout); - /* combine pictures */ - combine(); + 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); + + doptimize(1); /* optimize definitions */ + combine(); /* combine pictures */ quit(0); usage: eputs("Usage: "); eputs(argv[0]); eputs( -" [-w][-x xr][-y yr][-e expr][-f file] [ [-o][-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 */ } static int -tabputs( /* put out string preceded by a tab */ +headline( /* check header line & echo if requested */ char *s, void *p ) { - char fmt[32]; + int orig = *(int *)p; + char fmt[MAXFMTLEN]; double d; + int bigend; COLOR ctmp; if (isheadid(s)) /* header id */ @@ -250,38 +282,90 @@ tabputs( /* put out string preceded by a tab */ if (globmatch(ourfmt, fmt)) { wrongformat = 0; strcpy(ourfmt, fmt); - } else + } else if (!strcmp("float", fmt)) + input[nfiles].infloat *= -1; + else wrongformat = globmatch(PICFMT, fmt) ? 1 : -1; return(0); /* don't echo */ } - if (isexpos(s)) { /* exposure */ + 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)) { /* color correction */ + } else if (iscolcor(s)) { colcorval(ctmp, s); multcolor(input[nfiles].expos, ctmp); - } else if (isaspect(s)) + } + if (isaspect(s)) input[nfiles].pa *= aspectval(s); else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0) gotview++; - /* echo line */ - putchar('\t'); - return(fputs(s, stdout)); + + if (echoheader) { /* echo line */ + putchar('\t'); + return(fputs(s, stdout)); + } + return(0); } static void -checkfile(void) /* ready a file */ +checkfile(int orig) /* ready a file */ { - register int i; + int i; /* process header */ gotview = 0; - fputs(input[nfiles].name, stdout); - fputs(":\n", stdout); - getheader(input[nfiles].fp, tabputs, NULL); + 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\n"); + eputs(": not a Radiance picture or float matrix\n"); quit(1); } if (wrongformat > 0) { @@ -290,7 +374,10 @@ checkfile(void) /* ready a file */ } if (!gotview || setview(&input[nfiles].vw) != NULL) input[nfiles].vw.type = 0; - if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) { + 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); @@ -334,7 +421,7 @@ double (*ourbright)() = rgb_bright; static void init(void) /* perform final setup */ { - register int i; + int i; /* define constants */ varset("PI", ':', PI); varset(vnfiles, ':', (double)nfiles); @@ -357,6 +444,9 @@ init(void) /* perform final setup */ ourbright = xyz_bright; } else varset(vwhteff, ':', WHTEFFICACY); + /* these may be overridden */ + varset(vxres, ':', (double)xmax); + varset(vyres, ':', (double)ymax); } @@ -364,9 +454,10 @@ static void combine(void) /* combine pictures */ { EPNODE *coldef[3], *brtdef; + int set_x, set_y; COLOR *scanout; double d; - register int i, j; + int i, j; /* check defined variables */ for (j = 0; j < 3; j++) { if (vardefined(vcolout[j])) @@ -378,6 +469,9 @@ combine(void) /* combine pictures */ brtdef = eparse(vbrtout); else brtdef = NULL; + /* what to set */ + set_x = varlookup(vxpos) != NULL && !vardefined(vxpos); + set_y = varlookup(vypos) != NULL && !vardefined(vypos); /* allocate scanline */ scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); /* set input position */ @@ -385,15 +479,14 @@ combine(void) /* combine pictures */ /* combine files */ for (ypos = yres-1; ypos >= 0; ypos--) { advance(); - varset(vypos, '=', (double)ypos); + if (set_y) varset(vypos, '=', (double)ypos); for (xpos = 0; xpos < xres; xpos++) { - xscan = (long)xpos*xmax/xres; - varset(vxpos, '=', (double)xpos); + xscan = (xpos+.5)*xmax/xres; + if (set_x) 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++) { @@ -404,18 +497,33 @@ combine(void) /* combine pictures */ for (i = 0; i < nfiles; i++) 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((char *)scanout); + return; +writerr: + perror("write error"); + quit(1); } @@ -423,10 +531,10 @@ static void advance(void) /* read in data for next scanline */ { int ytarget; - register COLOR *st; - register int i, j; + COLOR *st; + int i, j; - for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--) + 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 */ @@ -434,30 +542,47 @@ advance(void) /* read in data for next scanline */ 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); + /* 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); } static double l_expos( /* return picture exposure */ - register char *nam + char *nam ) { - register int fn, n; + int fn, n; + double d; - fn = argument(1) - .5; - if (fn < 0 || fn >= nfiles) - return(1.0); + 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; @@ -473,29 +598,38 @@ l_expos( /* return picture exposure */ static double l_pixaspect(char *nm) /* return pixel aspect ratio */ { - register int fn; + int fn; + double d; - fn = argument(1) - .5; - if (fn < 0 || fn >= nfiles) - return(1.0); + 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; return(input[fn].pa); } static double l_colin( /* return color value for picture */ - register char *nam + char *nam ) { int fn; - register int n, xoff, yoff; + int n, xoff, yoff; double d; - fn = argument(1) - .5; - if (fn < 0 || fn >= nfiles) { + 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) { @@ -540,31 +674,37 @@ l_colin( /* return color value for picture */ static double l_ray( /* return ray origin or direction */ - register char *nam + char *nam ) { static unsigned long ltick[MAXINP]; static FVECT lorg[MAXINP], ldir[MAXINP]; static double ldist[MAXINP]; RREAL loc[2]; + double d; int fn; - register int i; + int i; - fn = argument(1) - .5; - if (fn < 0 || fn >= nfiles) { + 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? */ - 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) + 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 { + } 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; } @@ -588,14 +728,16 @@ l_psize(char *nm) /* compute pixel size in steradians RREAL locx[2], locy[2]; double d; int fn; - register int i; + int i; d = argument(1); - if (d < .5 || d >= nfiles+.5) { + if (d <= -0.5 || d >= nfiles+0.5) { errno = EDOM; return(0.0); } - fn = d - .5; + 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) @@ -626,7 +768,7 @@ l_psize(char *nm) /* compute pixel size in steradians extern void -wputs(char *msg) +wputs(const char *msg) { if (!nowarn) eputs(msg); @@ -634,7 +776,7 @@ wputs(char *msg) extern void -eputs(char *msg) +eputs(const char *msg) { fputs(msg, stderr); } @@ -643,7 +785,7 @@ eputs(char *msg) extern void quit(int code) /* exit gracefully */ { - register int i; + int i; /* close input files */ for (i = 0; i < nfiles; i++) if (input[i].name == Command)