--- ray/src/px/pfilt.c 1995/05/31 08:50:27 2.14 +++ ray/src/px/pfilt.c 2004/03/28 20:33:14 2.29 @@ -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: pfilt.c,v 2.29 2004/03/28 20:33:14 schorsch Exp $"; #endif - /* * pfilt.c - program to post-process picture file. * @@ -11,21 +8,23 @@ static char SCCSid[] = "$SunId$ LBL"; * 6/23/93 Added additional buffers for value spreading */ -#include "standard.h" +#include "copyright.h" #include +#include +#include "platform.h" +#include "standard.h" +#include "rtio.h" #include "color.h" - -#include "resolu.h" - +#include "view.h" #include "paths.h" +#include "pfilt.h" -extern float *matchlamp(); #define FEQ(a,b) ((a) >= .98*(b) && (a) <= 1.02*(b)) -double CHECKRAD = 1.5; /* radius to check for filtering */ +double CHECKRAD = 2.0; /* radius to check for filtering */ #define THRESHRAD 5.0 /* maximum sample spread in output */ @@ -64,6 +63,12 @@ int correctaspect = 0; /* aspect ratio correction? * int wrongformat = 0; +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 */ @@ -79,13 +84,23 @@ int orad = 0; /* output window radius */ char *progname; +static gethfunc headline; +static double rgb_bright(COLOR clr); +static double xyz_bright(COLOR clr); +static void copyfile(FILE *in, FILE *out); +static void pass1(FILE *in); +static void pass2(FILE *in); +static void scan2init(void); +static void scan2sync(int r); +static void scan2flush(void); -main(argc, argv) -int argc; -char **argv; + +int +main( + int argc, + char **argv +) { - extern long ftell(); - extern int quit(), headline(); FILE *fin; float *lampcolor; char *lamptype = NULL; @@ -93,18 +108,19 @@ char **argv; double outaspect = 0.0; double d; int i, j; -#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); if (signal(SIGINT, quit) == SIG_IGN) signal(SIGINT, SIG_IGN); +#ifdef SIGHUP if (signal(SIGHUP, quit) == SIG_IGN) signal(SIGHUP, SIG_IGN); +#endif signal(SIGTERM, quit); +#ifdef SIGPIPE signal(SIGPIPE, quit); +#endif #ifdef SIGXCPU signal(SIGXCPU, quit); signal(SIGXFSZ, quit); @@ -147,7 +163,7 @@ char **argv; fprintf(stderr, "%s: exposure out of range\n", argv[0]); - exit(1); + quit(1); } switch (argv[i][2]) { case '\0': @@ -197,7 +213,7 @@ char **argv; case 'm': thresh = atof(argv[++i]); if (rad <= FTINY) - rad = 1.0; + rad = 0.6; break; case 'b': rad = thresh = 0.0; @@ -267,6 +283,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; @@ -298,29 +317,64 @@ char **argv; } pass2(fin); - quit(0); + quit(estatus); + return estatus; /* pro forma return */ } -headline(s) /* process line from header */ -char *s; +static double +rgb_bright( + COLOR clr +) { + return(bright(clr)); +} + + +static double +xyz_bright( + COLOR clr +) +{ + return(clr[CIEY]); +} + + +double (*ourbright)() = rgb_bright; + + +static int +headline( /* process line from header */ + char *s, + void *p +) +{ char fmt[32]; fputs(s, stdout); /* copy to output */ if (isaspect(s)) /* get aspect ratio */ inpaspect *= aspectval(s); - else if (isexpos(s)) + else if (isexpos(s)) /* get exposure */ hotlvl *= exposval(s); - else if (isformat(s)) { - formatval(fmt, s); - wrongformat = strcmp(fmt, COLRFMT); - } + 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 + wrongformat = !globmatch(PICFMT, fmt); + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; + return(0); } -copyfile(in, out) /* copy a file */ -register FILE *in, *out; +static void +copyfile( /* copy a file */ + register FILE *in, + register FILE *out +) { register int c; @@ -334,8 +388,10 @@ register FILE *in, *out; } -pass1(in) /* first pass of picture file */ -FILE *in; +static void +pass1( /* first pass of picture file */ + FILE *in +) { int i; COLOR *scan; @@ -358,16 +414,19 @@ FILE *in; progname, (int)(100L*i/yres)); yres = i; y_r = (double)nrows/yres; + estatus++; break; } pass1scan(scan, i); } - free((char *)scan); + free((void *)scan); } -pass2(in) /* last pass on file, write to stdout */ -FILE *in; +static void +pass2( /* last pass on file, write to stdout */ + FILE *in +) { int yread; int ycent, xcent; @@ -377,7 +436,7 @@ FILE *in; 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], @@ -394,7 +453,7 @@ FILE *in; if (obarsize > 0) scan2sync(r); for (c = 0; c < ncols; c++) { - xcent = (long)c*xres/ncols; + xcent = (c+.5)*xres/ncols; if (thresh > FTINY) dothresh(xcent, ycent, c, r); else if (rad > FTINY) @@ -417,7 +476,8 @@ FILE *in; } -scan2init() /* prepare scanline arrays */ +static void +scan2init(void) /* prepare scanline arrays */ { COLOR ctmp; double d; @@ -430,9 +490,9 @@ scan2init() /* prepare scanline arrays */ rad *= (y_r + x_c)/2.0; if (thresh > FTINY) { - xrad = CHECKRAD*THRESHRAD*rad/x_c + xbrad; - yrad = CHECKRAD*THRESHRAD*rad/y_r + ybrad; 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; @@ -455,12 +515,12 @@ scan2init() /* prepare scanline arrays */ if (obarsize > 0) { scoutbar = (COLOR **)malloc(obarsize*sizeof(COLOR *)); greybar = (float **)malloc(obarsize*sizeof(float *)); - if (scoutbar == NULL | greybar == NULL) + if ((scoutbar == NULL) | (greybar == NULL)) goto memerr; for (i = 0; i < obarsize; i++) { scoutbar[i] = (COLOR *)malloc(ncols*sizeof(COLOR)); greybar[i] = (float *)malloc(ncols*sizeof(float)); - if (scoutbar[i] == NULL | greybar[i] == NULL) + if ((scoutbar[i] == NULL) | (greybar[i] == NULL)) goto memerr; } } else { @@ -475,7 +535,7 @@ scan2init() /* prepare scanline arrays */ fputaspect(d, stdout); } /* record exposure */ - d = bright(exposure); + d = (*ourbright)(exposure); if (!FEQ(d,1.0)) fputexpos(d, stdout); /* record color correction */ @@ -494,22 +554,24 @@ memerr: } -scan2sync(r) /* synchronize grey averages and output scan */ -int r; +static void +scan2sync( /* synchronize grey averages and output scan */ + int r +) { static int nextrow = 0; COLOR ctmp; int ybot; register int c; /* average input scanlines */ - while (nextrow < r+orad && nextrow < nrows) { - ybot = (long)nextrow*yres/nrows; + while (nextrow <= r+orad && nextrow < nrows) { + ybot = (nextrow+.5)*yres/nrows; for (c = 0; c < ncols; c++) { - dobox(ctmp, (int)((long)c*xres/ncols),ybot, c,nextrow); - greybar[nextrow%obarsize][c] = bright(ctmp); + dobox(ctmp, (int)((c+.5)*xres/ncols),ybot, c,nextrow); + greybar[nextrow%obarsize][c] = (*ourbright)(ctmp); } /* and zero output scanline */ - bzero((char *)scoutbar[nextrow%obarsize], ncols*sizeof(COLOR)); + memset((char *)scoutbar[nextrow%obarsize], '\0', ncols*sizeof(COLOR)); nextrow++; } /* point to top scanline for output */ @@ -520,7 +582,8 @@ int r; } -scan2flush() /* flush output buffer */ +static void +scan2flush(void) /* flush output buffer */ { register int r; @@ -529,11 +592,12 @@ scan2flush() /* flush output buffer */ break; if (fflush(stdout) < 0) { fprintf(stderr, "%s: write error at end of pass2\n", progname); - exit(1); + quit(1); } } +void quit(code) /* remove temporary file and exit */ int code; {