--- ray/src/px/ra_rgbe.c 1998/01/23 09:28:29 2.7 +++ ray/src/px/ra_rgbe.c 1998/01/23 11:38:39 2.8 @@ -17,12 +17,24 @@ static char SCCSid[] = "$SunId$ LBL"; #include #endif -extern char *malloc(); +extern char *malloc(), *realloc(), *tempbuffer(); +extern int addhline(); +#define dumpheader(fp) fwrite(headlines, 1, headlen, fp) + int bradj = 0; /* brightness adjustment */ int doflat = 1; /* produce flat file */ +int force = 0; /* force file overwrite? */ + +int findframe = 0; /* find a specific frame? */ + +int frameno = 0; /* current frame number */ +int fmterr = 0; /* got input format error */ +char *headlines; /* current header info. */ +int headlen; /* current header length */ + char *progname; @@ -30,6 +42,7 @@ main(argc, argv) int argc; char *argv[]; { + char *ospec; int i; progname = argv[0]; @@ -45,89 +58,182 @@ char *argv[]; goto userr; bradj = atoi(argv[++i]); break; + case 'n': + findframe = atoi(argv[++i]); + break; + case 'f': + force++; + break; + case '\0': + goto gotfile; default: goto userr; } else break; - +gotfile: if (i < argc-2) goto userr; - if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { + if (i <= argc-1 && strcmp(argv[i], "-") && + freopen(argv[i], "r", stdin) == NULL) { fprintf(stderr, "%s: can't open input \"%s\"\n", progname, argv[i]); exit(1); } - if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { - fprintf(stderr, "%s: can't open output \"%s\"\n", - progname, argv[i+1]); - exit(1); - } #ifdef MSDOS setmode(fileno(stdin), O_BINARY); - setmode(fileno(stdout), O_BINARY); #endif - transfer(); + ospec = i==argc-2 ? argv[i+1] : (char *)NULL; + while (transfer(ospec)) + ; exit(0); userr: - fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n", + fprintf(stderr, + "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", progname); exit(1); } -quiterr(err) /* print message and exit */ -char *err; +transfer(ospec) /* transfer a Radiance picture */ +char *ospec; { - if (err != NULL) { - fprintf(stderr, "%s: %s\n", progname, err); - exit(1); - } - exit(0); -} - - -transfer() /* transfer Radiance picture */ -{ - static char ourfmt[LPICFMT+1] = PICFMT; + char oname[128]; + FILE *fp; int order; int xmax, ymax; COLR *scanin; int y; - /* get header info. */ - if ((y = checkheader(stdin, ourfmt, stdout)) < 0 || - (order = fgetresolu(&xmax, &ymax, stdin)) < 0) - quiterr("bad picture format"); - if (!y) - strcpy(ourfmt, COLRFMT); - fputs(progname, stdout); + /* get header info. */ + if (!(y = loadheader(stdin))) + return(0); + if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { + fprintf(stderr, "%s: bad input format\n", progname); + exit(1); + } + /* did we pass the target frame? */ + if (findframe && findframe < frameno) + return(0); + /* allocate scanline */ + scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); + if (scanin == NULL) { + perror(progname); + exit(1); + } + /* skip frame? */ + if (findframe > frameno) { + for (y = ymax; y--; ) + if (freadcolrs(scanin, xmax, stdin) < 0) { + fprintf(stderr, + "%s: error reading input picture\n", + progname); + exit(1); + } + return(1); + } + /* open output file/command */ + if (ospec == NULL) { + strcpy(oname, ""); + fp = stdout; + } else { + sprintf(oname, ospec, frameno); + if (oname[0] == '!') { + if ((fp = popen(oname+1, "w")) == NULL) { + fprintf(stderr, "%s: cannot start \"%s\"\n", + progname, oname); + exit(1); + } + } else { + if (!force && access(oname, 0) >= 0) { + fprintf(stderr, + "%s: output file \"%s\" exists\n", + progname, oname); + exit(1); + } + if ((fp = fopen(oname, "w")) == NULL) { + fprintf(stderr, "%s: ", progname); + perror(oname); + exit(1); + } + } + } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif + dumpheader(fp); /* put out header */ + fputs(progname, fp); if (bradj) - fprintf(stdout, " -e %+d", bradj); + fprintf(fp, " -e %+d", bradj); if (!doflat) - fputs(" -r", stdout); - fputc('\n', stdout); + fputs(" -r", fp); + fputc('\n', fp); if (bradj) - fputexpos(pow(2.0, (double)bradj), stdout); - fputformat(ourfmt, stdout); - fputc('\n', stdout); - fputresolu(order, xmax, ymax, stdout); - /* allocate scanline */ - scanin = (COLR *)malloc(xmax*sizeof(COLR)); - if (scanin == NULL) - quiterr("out of memory in transfer"); - /* convert image */ + fputexpos(pow(2.0, (double)bradj), fp); + fputc('\n', fp); + fputresolu(order, xmax, ymax, fp); + /* transfer picture */ for (y = ymax; y--; ) { - if (freadcolrs(scanin, xmax, stdin) < 0) - quiterr("error reading input picture"); + if (freadcolrs(scanin, xmax, stdin) < 0) { + fprintf(stderr, "%s: error reading input picture\n", + progname); + exit(1); + } if (bradj) shiftcolrs(scanin, xmax, bradj); if (doflat) - fwrite((char *)scanin, sizeof(COLR), xmax, stdout); + fwrite((char *)scanin, sizeof(COLR), xmax, fp); else - fwritecolrs(scanin, xmax, stdout); - if (ferror(stdout)) - quiterr("error writing output picture"); + fwritecolrs(scanin, xmax, fp); + if (ferror(fp)) { + fprintf(stderr, "%s: error writing output to \"%s\"\n", + progname, oname); + exit(1); + } } - /* free scanline */ - free((char *)scanin); + /* clean up */ + if (oname[0] == '!') + pclose(fp); + else if (ospec != NULL) + fclose(fp); + return(1); +} + + +addhline(s) /* add a line to our info. header */ +char *s; +{ + char fmt[32]; + int n; + + if (formatval(fmt, s)) + fmterr += !globmatch(PICFMT, fmt); + else if (!strncmp(s, "FRAME=", 6)) + frameno = atoi(s+6); + n = strlen(s); + if (headlen) + headlines = (char *)realloc(headlines, headlen+n+1); + else + headlines = (char *)malloc(n+1); + if (headlines == NULL) { + perror(progname); + exit(1); + } + strcpy(headlines+headlen, s); + headlen += n; +} + + +loadheader(fp) /* load an info. header into memory */ +FILE *fp; +{ + fmterr = 0; frameno = 0; + if (headlen) { /* free old header */ + free(headlines); + headlen = 0; + } + if (getheader(fp, addhline, NULL) < 0) + return(0); + if (fmterr) + return(-1); + return(1); }