--- ray/src/px/pfilt.c 1992/09/08 15:33:08 2.3 +++ ray/src/px/pfilt.c 2023/12/15 01:57:45 2.39 @@ -1,100 +1,186 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pfilt.c,v 2.39 2023/12/15 01:57:45 greg Exp $"; #endif - /* * pfilt.c - program to post-process picture file. * * 9/26/85 + * 6/23/93 Added additional buffers for value spreading */ -#include +#include "copyright.h" #include - -#include "color.h" - -#include "resolu.h" - +#include "pfilt.h" +#include "platform.h" #include "paths.h" +#include "view.h" -extern char *malloc(); -extern float *matchlamp(); +#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) -#define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) +double CHECKRAD = 2.0; /* radius to check for filtering */ -#define CHECKRAD 1.5 /* radius to check for filtering */ +#define THRESHRAD 5.0 /* maximum sample spread in output */ COLOR exposure = WHTCOLOR; /* exposure for the frame */ -double rad = 0.0; /* output pixel radius for filtering */ +double rad = 0.0; /* output pixel radius for filtering */ +double thresh = 0.0; /* maximum contribution for subpixel */ + int nrows = 0; /* number of rows for output */ int ncols = 0; /* number of columns for output */ -double x_c = 1.0; /* ratio of output x size to input */ -double y_r = 1.0; /* ratio of output y size to input */ +double x_c = 1.0; /* ratio of output x size to input */ +double y_r = 1.0; /* ratio of output y size to input */ int singlepass = 0; /* true means skip first pass */ int avghot = 0; /* true means average in bright spots */ -double hotlvl = 1000.0; /* level considered "hot" */ +double hotlvl = 100.0; /* level considered "hot" */ int npts = 0; /* (half) number of points for stars */ -double spread = 1e-4; /* spread for star points */ +double spread = 1e-4; /* spread for star points */ char *tfname = NULL; +char template[] = TEMPLATE; + char *lampdat = "lamp.tab"; /* lamp data file */ int order; /* scanline ordering of input */ int xres, yres; /* resolution of input */ -double inpaspect = 1.0; /* pixel aspect ratio of input */ +double inpaspect = 1.0; /* pixel aspect ratio of input */ int correctaspect = 0; /* aspect ratio correction? */ int wrongformat = 0; -int xrad; /* x window size */ -int yrad; /* y window size */ +VIEW ourview = STDVIEW; +int gotview = 0; +int wrapfilt = 0; /* wrap filter horizontally? */ +int estatus = 0; /* exit status (for non-fatal errors) */ + +int xrad; /* x search radius */ +int yrad; /* y search radius */ +int xbrad; /* x box size */ +int ybrad; /* y box size */ + int barsize; /* size of input scan bar */ -COLOR **scanin; /* input scan bar */ -COLOR *scanout; /* output scan line */ +COLORV **scanin; /* input scan bar */ +COLORV *scanout; /* output scan line */ +COLORV **scoutbar; /* output scan bar (if thresh > 0) */ +float **greybar; /* grey-averaged input values */ +int obarsize = 0; /* size of output scan bar */ +int orad = 0; /* output window radius */ char *progname; +static void copyfile(FILE *infp, FILE *out); +static void pass1(FILE *infp); +static void pass2(FILE *infp); +static void scan2init(void); +static void scan2sync(int r); +static void scan2flush(void); -main(argc, argv) -int argc; -char **argv; + +static double +rgb_bright( + SCOLOR clr +) { - extern double pow(); - extern long ftell(); - extern int quit(), headline(); + return(bright(clr)); +} + + +static double +xyz_bright( + SCOLOR clr +) +{ + return(colval(clr,CIEY)); +} + + +static double +spec_bright( + SCOLOR clr +) +{ + return(pbright(clr)); +} + + +brightfunc_t *ourbright = rgb_bright; + + +static int +headline( /* process line from header */ + char *s, + void *p +) +{ + char fmt[MAXFMTLEN]; + + fputs(s, stdout); /* copy to output */ + if (isaspect(s)) /* get aspect ratio */ + inpaspect *= aspectval(s); + else if (isexpos(s)) /* get exposure */ + hotlvl *= exposval(s); + else if (isncomp(s)) /* get #components (spectral) */ + NCSAMP = ncompval(s); + else if (iswlsplit(s)) /* get wavelength partitions */ + wlsplitval(WLPART, s); + else if (formatval(fmt, s)) { /* get format */ + wrongformat = 0; + if (!strcmp(COLRFMT, fmt)) + ourbright = rgb_bright; + else if (!strcmp(CIEFMT, fmt)) + ourbright = xyz_bright; + else if (!strcmp(SPECFMT, fmt)) + ourbright = spec_bright; + else + wrongformat = !globmatch(PICFMT, fmt); + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; + return(0); +} + + +int +main( + int argc, + char **argv +) +{ FILE *fin; float *lampcolor; char *lamptype = NULL; long fpos; - double outaspect = 0.0; - double d; + double outaspect = 0.0; + double d; int i, j; - + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); +#ifdef SIGHUP if (signal(SIGHUP, quit) == SIG_IGN) - signal(SIGINT, SIG_IGN); + signal(SIGHUP, SIG_IGN); +#endif signal(SIGTERM, quit); +#ifdef SIGPIPE signal(SIGPIPE, quit); -#ifdef SIGXCPU +#endif +#ifdef SIGXCPU signal(SIGXCPU, quit); signal(SIGXFSZ, quit); #endif - progname = argv[0]; + progname = argv[0] = fixargv0(argv[0]); for (i = 1; i < argc; i++) if (argv[i][0] == '-') @@ -131,7 +217,7 @@ char **argv; fprintf(stderr, "%s: exposure out of range\n", argv[0]); - exit(1); + quit(1); } switch (argv[i][2]) { case '\0': @@ -178,8 +264,13 @@ char **argv; case 'r': rad = atof(argv[++i]); break; + case 'm': + thresh = atof(argv[++i]); + if (rad <= FTINY) + rad = 0.6; + break; case 'b': - rad = 0.0; + rad = thresh = 0.0; break; default:; badopt: @@ -198,9 +289,9 @@ char **argv; fprintf(stderr, "%s: unknown lamp type\n", lamptype); quit(1); } - for (i = 0; i < 3; i++) - if (lampcolor[i] > 1e-4) - colval(exposure,i) /= lampcolor[i]; + for (j = 0; j < 3; j++) + if (lampcolor[j] > 1e-4) + colval(exposure,j) /= lampcolor[j]; freelamps(); } /* open input file */ @@ -208,8 +299,8 @@ char **argv; if (singlepass) fin = stdin; else { - tfname = mktemp(TEMPLATE); - if ((fin = fopen(tfname, "w+")) == NULL) { + tfname = mktemp(template); + if ((fin = fopen(tfname, "w+b")) == NULL) { fprintf(stderr, "%s: can't create ", progname); fprintf(stderr, "temp file \"%s\"\n", tfname); quit(1); @@ -221,7 +312,7 @@ char **argv; } } } else if (i == argc-1) { - if ((fin = fopen(argv[i], "r")) == NULL) { + if ((fin = fopen(argv[i], "rb")) == NULL) { fprintf(stderr, "%s: can't open file \"%s\"\n", progname, argv[i]); quit(1); @@ -237,6 +328,11 @@ char **argv; progname); quit(1); } + if ((ourbright == spec_bright) ^ (NCSAMP > 3) || + setspectrsamp(CNDX, WLPART) < 0) { + fprintf(stderr, "%s: bad number of components\n", progname); + quit(1); + } /* add new header info. */ printargs(i, argv, stdout); /* get picture size */ @@ -246,6 +342,9 @@ char **argv; } if (!(order & YMAJOR)) inpaspect = 1.0/inpaspect; + /* wrap around for cylindrical view? */ + wrapfilt = gotview && ourview.type == VT_CYL && + ourview.horiz >= 360.-FTINY && order & YMAJOR; /* compute output resolution */ if (ncols <= 0) ncols = x_c*xres + .5; @@ -277,31 +376,20 @@ char **argv; } pass2(fin); - quit(0); + quit(estatus); + return estatus; /* pro forma return */ } -headline(s) /* process line from header */ -char *s; +static void +copyfile( /* copy a file */ + FILE *infp, + FILE *out +) { - char fmt[32]; + int c; - fputs(s, stdout); /* copy to output */ - if (isaspect(s)) /* get aspect ratio */ - inpaspect *= aspectval(s); - else if (isformat(s)) { - formatval(fmt, s); - wrongformat = strcmp(fmt, COLRFMT); - } -} - - -copyfile(in, out) /* copy a file */ -register FILE *in, *out; -{ - register int c; - - while ((c = getc(in)) != EOF) + while ((c = getc(infp)) != EOF) putc(c, out); if (ferror(out)) { @@ -311,56 +399,61 @@ register FILE *in, *out; } -pass1(in) /* first pass of picture file */ -FILE *in; +static void +pass1( /* first pass of picture file */ + FILE *infp +) { int i; - COLOR *scan; + COLORV *scan; pass1init(); - scan = (COLOR *)malloc(xres*sizeof(COLOR)); + scan = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); if (scan == NULL) { fprintf(stderr, "%s: out of memory\n", progname); quit(1); } for (i = 0; i < yres; i++) { - if (freadscan(scan, xres, in) < 0) { - nrows = nrows * i / yres; /* adjust frame */ + if (freadsscan(scan, NCSAMP, xres, infp) < 0) { + nrows = (long)nrows * i / yres; /* adjust frame */ if (nrows <= 0) { fprintf(stderr, "%s: empty frame\n", progname); quit(1); } fprintf(stderr, "%s: warning - partial frame (%d%%)\n", - progname, 100*i/yres); + progname, (int)(100L*i/yres)); yres = i; y_r = (double)nrows/yres; + estatus++; break; } pass1scan(scan, i); } - free((char *)scan); + free(scan); } -pass2(in) /* last pass on file, write to stdout */ -FILE *in; +static void +pass2( /* last pass on file, write to stdout */ + FILE *infp +) { int yread; int ycent, xcent; int r, c; - + pass2init(); scan2init(); yread = 0; for (r = 0; r < nrows; r++) { - ycent = (long)r*yres/nrows; + ycent = (r+.5)*yres/nrows; while (yread <= ycent+yrad) { if (yread < yres) { - if (freadscan(scanin[yread%barsize], - xres, in) < 0) { + if (freadsscan(scanin[yread%barsize], + NCSAMP, xres, infp) < 0) { fprintf(stderr, - "%s: bad read (y=%d)\n", + "%s: truncated input (y=%d)\n", progname, yres-1-yread); quit(1); } @@ -368,59 +461,85 @@ FILE *in; } yread++; } + if (obarsize > 0) /* => thresh > FTINY */ + scan2sync(r); for (c = 0; c < ncols; c++) { - xcent = (long)c*xres/ncols; - if (rad <= 0.0) - dobox(scanout[c], xcent, ycent, c, r); + xcent = (c+.5)*xres/ncols; + if (thresh > FTINY) + dothresh(xcent, ycent, c, r); + else if (rad > FTINY) + dogauss(scanout+c*NCSAMP, xcent, ycent, c, r); else - dogauss(scanout[c], xcent, ycent, c, r); + dobox(scanout+c*NCSAMP, xcent, ycent, c, r); } - if (fwritescan(scanout, ncols, stdout) < 0) { + if (scanout != NULL && fwritesscan(scanout, NCSAMP, ncols, stdout) < 0) { fprintf(stderr, "%s: write error in pass2\n", progname); quit(1); } } - /* skip leftovers */ + /* skip leftover input */ while (yread < yres) { - if (freadscan(scanin[0], xres, in) < 0) + if (freadscolrs((uby8 *)tempbuffer(xres*(NCSAMP+1)), + NCSAMP, xres, infp) < 0) break; yread++; } + scan2flush(); /* flush output */ } -scan2init() /* prepare scanline arrays */ +static void +scan2init(void) /* prepare scanline arrays */ { COLOR ctmp; - double d; - register int i; + double d; + int i; - if (rad <= 0.0) { - xrad = xres/ncols/2 + 1; - yrad = yres/nrows/2 + 1; - } else { + xbrad = xres/ncols/2 + 1; + ybrad = yres/nrows/2 + 1; + if (rad > FTINY) { if (nrows >= yres && ncols >= xres) rad *= (y_r + x_c)/2.0; - xrad = CHECKRAD*rad/x_c + 1; - yrad = CHECKRAD*rad/y_r + 1; - + if (thresh > FTINY) { + orad = CHECKRAD*THRESHRAD*rad + 1; + xrad = orad/x_c + xbrad; + yrad = orad/y_r + ybrad; + obarsize = 2*orad + 1; + } else { + xrad = CHECKRAD*rad/x_c + 1; + yrad = CHECKRAD*rad/y_r + 1; + } initmask(); /* initialize filter table */ + } else { + xrad = xbrad; + yrad = ybrad; } barsize = 2*yrad + 1; - scanin = (COLOR **)malloc(barsize*sizeof(COLOR *)); + scanin = (COLORV **)malloc(barsize*sizeof(COLORV *)); + if (scanin == NULL) + goto memerr; for (i = 0; i < barsize; i++) { - scanin[i] = (COLOR *)malloc(xres*sizeof(COLOR)); - if (scanin[i] == NULL) { - fprintf(stderr, "%s: out of memory\n", progname); - quit(1); + scanin[i] = (COLORV *)malloc(xres*NCSAMP*sizeof(COLORV)); + if (scanin[i] == NULL) + goto memerr; + } + if (obarsize > 0) { + scoutbar = (COLORV **)malloc(obarsize*sizeof(COLORV *)); + greybar = (float **)malloc(obarsize*sizeof(float *)); + if ((scoutbar == NULL) | (greybar == NULL)) + goto memerr; + for (i = 0; i < obarsize; i++) { + scoutbar[i] = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); + greybar[i] = (float *)malloc(ncols*sizeof(float)); + if ((scoutbar[i] == NULL) | (greybar[i] == NULL)) + goto memerr; } + } else { + scanout = (COLORV *)malloc(ncols*NCSAMP*sizeof(COLORV)); + if (scanout == NULL) + goto memerr; } - scanout = (COLOR *)malloc(ncols*sizeof(COLOR)); - if (scanout == NULL) { - fprintf(stderr, "%s: out of memory\n", progname); - quit(1); - } /* record pixel aspect ratio */ if (!correctaspect) { d = order & YMAJOR ? x_c/y_r : y_r/x_c ; @@ -440,9 +559,57 @@ scan2init() /* prepare scanline arrays */ printf("\n"); /* write out resolution */ fputresolu(order, ncols, nrows, stdout); + return; +memerr: + fprintf(stderr, "%s: out of memory\n", progname); + quit(1); } +static void +scan2sync( /* synchronize grey averages and output scan */ + int r +) +{ + static int nextrow = 0; + SCOLOR ctmp; + int ybot; + int c; + /* average input scanlines */ + while (nextrow <= r+orad && nextrow < nrows) { + ybot = (nextrow+.5)*yres/nrows; + for (c = 0; c < ncols; c++) { + dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); + greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); + } + /* and zero output scanline */ + memset(scoutbar[nextrow%obarsize], 0, ncols*NCSAMP*sizeof(COLORV)); + nextrow++; + } + /* point to top scanline for output */ + if (r-orad >= 0) + scanout = scoutbar[(r-orad)%obarsize]; + else + scanout = NULL; +} + + +static void +scan2flush(void) /* flush output buffer */ +{ + int r; + + for (r = nrows-orad; r < nrows; r++) + if (fwritesscan(scoutbar[r%obarsize], NCSAMP, ncols, stdout) < 0) + break; + if (fflush(stdout) < 0) { + fprintf(stderr, "%s: write error at end of pass2\n", progname); + quit(1); + } +} + + +void quit(code) /* remove temporary file and exit */ int code; {