--- ray/src/px/ra_rgbe.c 2003/06/05 19:29:34 2.12 +++ ray/src/px/ra_rgbe.c 2024/09/10 20:24:42 2.23 @@ -1,41 +1,40 @@ #ifndef lint -static const char RCSid[] = "$Id: ra_rgbe.c,v 2.12 2003/06/05 19:29:34 schorsch Exp $"; +static const char RCSid[] = "$Id: ra_rgbe.c,v 2.23 2024/09/10 20:24:42 greg Exp $"; #endif /* * program to convert from RADIANCE RLE to flat format */ -#include #include -#include #include "platform.h" +#include "rtio.h" +#include "paths.h" #include "color.h" #include "resolu.h" -extern int addhline(); +#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) -#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 *headlines = NULL; /* current header info. */ +int headlen1 = 0; /* length of initial frame header */ +int headlen = 0; /* current header length */ +char fmt[MAXFMTLEN]; /* input format */ char *progname; +static gethfunc addhline; +static int transfer(char *ospec); +static int loadheader(FILE *fp); -main(argc, argv) -int argc; -char *argv[]; + +int +main(int argc, char *argv[]) { char *ospec; int i; @@ -88,10 +87,12 @@ userr: } -transfer(ospec) /* transfer a Radiance picture */ -char *ospec; +static int +transfer( /* transfer a Radiance picture */ + char *ospec +) { - char oname[128]; + char oname[PATH_MAX]; FILE *fp; int order; int xmax, ymax; @@ -108,20 +109,27 @@ char *ospec; if (findframe && findframe < frameno) return(0); /* allocate scanline */ - scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); + scanin = (COLR *)malloc(xmax*sizeof(COLR)); if (scanin == NULL) { perror(progname); exit(1); } /* skip frame? */ if (findframe > frameno) { - for (y = ymax; y--; ) + if (NCSAMP > 3) { + if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) { + perror(progname); + exit(1); + } + } else + for (y = ymax; y--; ) if (freadcolrs(scanin, xmax, stdin) < 0) { fprintf(stderr, "%s: error reading input picture\n", progname); exit(1); } + free(scanin); return(1); } /* open output file/command */ @@ -151,7 +159,8 @@ char *ospec; } } SET_FILE_BINARY(fp); - dumpheader(fp); /* put out header */ + newheader("RADIANCE", fp); /* put out header */ + dumpheader(fp); fputs(progname, fp); if (bradj) fprintf(fp, " -e %+d", bradj); @@ -160,47 +169,69 @@ char *ospec; fputc('\n', fp); if (bradj) fputexpos(pow(2.0, (double)bradj), fp); + if (frameno) + fprintf(fp, "FRAME=%d\n", frameno); + if (fmt[0]) + fputformat(fmt, fp); fputc('\n', fp); fputresolu(order, xmax, ymax, fp); /* transfer picture */ for (y = ymax; y--; ) { - if (freadcolrs(scanin, xmax, stdin) < 0) { + if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 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, fp); - else - fwritecolrs(scanin, xmax, fp); - if (ferror(fp)) { - fprintf(stderr, "%s: error writing output to \"%s\"\n", - progname, oname); - exit(1); - } + if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : + (fwritecolrs(scanin, xmax, fp) < 0)) + goto writerr; } - /* clean up */ + free(scanin); /* clean up */ + if (fflush(fp) == EOF) + goto writerr; if (oname[0] == '!') pclose(fp); else if (ospec != NULL) fclose(fp); return(1); +writerr: + fprintf(stderr, "%s: error writing output to \"%s\"\n", + progname, oname); + exit(1); } -int -addhline(s) /* add a line to our info. header */ -char *s; +static int +addhline( /* add a line to our info. header */ + char *s, + void *p +) { - char fmt[32]; int n; - if (formatval(fmt, s)) - fmterr += !globmatch(PICFMT, fmt); - else if (!strncmp(s, "FRAME=", 6)) + if (isheadid(s)) + return(0); + if (!strncmp(s, "FRAME=", 6)) { frameno = atoi(s+6); + return(0); + } + if (formatval(fmt, s)) { + if (!strcmp(fmt, SPECFMT)) + strcpy(fmt, COLRFMT); + else + fmterr += !globmatch(PICFMT, fmt); + return(0); + } + if (isncomp(s)) { + NCSAMP = ncompval(s); + return(NCSAMP - 3); + } + if (iswlsplit(s)) { + wlsplitval(WLPART, s); + return(0); + } n = strlen(s); if (headlen) headlines = (char *)realloc((void *)headlines, headlen+n+1); @@ -216,14 +247,16 @@ char *s; } -loadheader(fp) /* load an info. header into memory */ -FILE *fp; +static int +loadheader( /* load an info. header into memory */ + FILE *fp +) { fmterr = 0; frameno = 0; - if (headlen) { /* free old header */ - free(headlines); - headlen = 0; - } + /* revert to initial header length */ + if (!headlen1) headlen1 = headlen; + else headlen = headlen1; + if (getheader(fp, addhline, NULL) < 0) return(0); if (fmterr)