--- ray/src/px/ra_bmp.c 2004/04/30 17:00:29 2.4 +++ ray/src/px/ra_bmp.c 2004/10/04 17:44:22 2.10 @@ -1,11 +1,12 @@ #ifndef lint -static const char RCSid[] = "$Id: ra_bmp.c,v 2.4 2004/04/30 17:00:29 greg Exp $"; +static const char RCSid[] = "$Id: ra_bmp.c,v 2.10 2004/10/04 17:44:22 greg Exp $"; #endif /* * program to convert between RADIANCE and Windows BMP file */ #include +#include #include #include "platform.h" @@ -14,19 +15,24 @@ static const char RCSid[] = "$Id: ra_bmp.c,v 2.4 2004/ #include "resolu.h" #include "bmpfile.h" -int bradj = 0; /* brightness adjustment */ +int bradj = 0; /* brightness adjustment */ -double gamcor = 2.2; /* gamma correction value */ +double gamcor = 2.2; /* gamma correction value */ -char *progname; +char *progname; static void quiterr(const char *err); static void tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIMP monpri, double gamval); -static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry); +static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri); static void bmp2rad(BMPReader *brd, FILE *rfp, int inv); +static RGBPRIMP rgbinp = stdprims; /* RGB input primitives */ +static RGBPRIMS myinprims; /* custom primitives holder */ +static gethfunc headline; + + int main(int argc, char *argv[]) { @@ -41,7 +47,7 @@ main(int argc, char *argv[]) progname = argv[0]; for (i = 1; i < argc; i++) - if (argv[i][0] == '-') + if (argv[i][0] == '-' && argv[i][1]) switch (argv[i][1]) { case 'b': rgbp = NULL; @@ -72,8 +78,6 @@ main(int argc, char *argv[]) case 'r': reverse = !reverse; break; - case '\0': - break; default: goto userr; } @@ -149,23 +153,24 @@ main(int argc, char *argv[]) exit(1); } /* get header info. */ - if (checkheader(stdin, COLRFMT, NULL) < 0 || + if (getheader(stdin, headline, NULL) < 0 || !fgetsresolu(&rs, stdin)) quiterr("bad Radiance picture format"); /* initialize BMP header */ if (rgbp == NULL) { hdr = BMPmappedHeader(scanlen(&rs), numscans(&rs), 0, 256); + /* if (outfile != NULL) hdr->compr = BI_RLE8; + */ } else hdr = BMPtruecolorHeader(scanlen(&rs), numscans(&rs), 0); if (hdr == NULL) quiterr("cannot initialize BMP header"); /* set up output direction */ - hdr->yIsDown = (rs.rt & YDECR) && - ((outfile == NULL) | (hdr->compr == BI_RLE8)); + hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8)); /* open BMP output */ if (outfile != NULL) wtr = BMPopenOutputFile(outfile, hdr); @@ -174,7 +179,7 @@ main(int argc, char *argv[]) if (wtr == NULL) quiterr("cannot allocate writer structure"); /* convert file */ - rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL); + rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp); /* flush output */ if (fflush((FILE *)wtr->c_data) < 0) quiterr("error writing BMP output"); @@ -202,17 +207,58 @@ quiterr(const char *err) exit(0); } +/* process header line (don't echo) */ +static int +headline(char *s, void *p) +{ + char fmt[32]; + + if (formatval(fmt, s)) { /* check if format string */ + if (!strcmp(fmt,COLRFMT)) + return(0); + if (!strcmp(fmt,CIEFMT)) { + rgbinp = TM_XYZPRIM; + return(0); + } + return(-1); + } + if (isprims(s)) { /* get input primaries */ + primsval(myinprims, s); + rgbinp = myinprims; + return(0); + } + /* should I grok colcorr also? */ + return(0); +} + + /* convert Radiance picture to BMP */ static void -rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) +rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri) { + int usexfm = 0; + COLORMAT xfm; COLR *scanin; + COLOR cval; int y, yend, ystp; int x; /* allocate scanline */ scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); if (scanin == NULL) quiterr("out of memory in rad2bmp"); + /* set up color conversion */ + usexfm = (monpri != NULL ? rgbinp != monpri : + rgbinp != TM_XYZPRIM && rgbinp != stdprims); + if (usexfm) { + double expcomp = pow(2.0, (double)bradj); + if (rgbinp == TM_XYZPRIM) + compxyz2rgbWBmat(xfm, monpri); + else + comprgb2rgbWBmat(xfm, rgbinp, monpri); + for (y = 0; y < 3; y++) + for (x = 0; x < 3; x++) + xfm[y][x] *= expcomp; + } /* convert image */ if (inv) { y = bwr->hdr->height - 1; @@ -225,12 +271,21 @@ rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) for ( ; y != yend; y += ystp) { if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) quiterr("error reading Radiance picture"); - if (bradj) + if (usexfm) + for (x = bwr->hdr->width; x--; ) { + colr_color(cval, scanin[x]); + colortrans(cval, xfm, cval); + setcolr(scanin[x], colval(cval,RED), + colval(cval,GRN), + colval(cval,BLU)); + } + else if (bradj) shiftcolrs(scanin, bwr->hdr->width, bradj); - for (x = gry ? bwr->hdr->width : 0; x--; ) - scanin[x][GRN] = normbright(scanin[x]); + if (monpri == NULL && rgbinp != TM_XYZPRIM) + for (x = bwr->hdr->width; x--; ) + scanin[x][GRN] = normbright(scanin[x]); colrs_gambs(scanin, bwr->hdr->width); - if (gry) + if (monpri == NULL) for (x = bwr->hdr->width; x--; ) bwr->scanline[x] = scanin[x][GRN]; else @@ -295,7 +350,6 @@ tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM int tmflags; BMPHeader *hdr; BMPWriter *wtr; - RESOLU rs; FILE *fp; int xr, yr; BYTE *pa; @@ -321,13 +375,6 @@ tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM fprintf(stderr, "%s: cannot open\n", fnin); exit(1); } - /* get picture orientation */ - if (fnin != NULL) { - if (getheader(fp, NULL, NULL) < 0 || !fgetsresolu(&rs, fp)) - quiterr("bad Radiance picture format"); - rewind(fp); - } else /* assume stdin has normal orient */ - rs.rt = PIXSTANDARD; /* tone-map picture */ if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval, 0., 0., fnin, fp) != TM_E_OK) @@ -335,7 +382,8 @@ tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM /* initialize BMP header */ if (tmflags & TM_F_BW) { hdr = BMPmappedHeader(xr, yr, 0, 256); - hdr->compr = BI_RLE8; + if (fnout != NULL) + hdr->compr = BI_RLE8; } else hdr = BMPtruecolorHeader(xr, yr, 0); if (hdr == NULL) @@ -350,9 +398,7 @@ tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM /* write to BMP file */ while (wtr->yscan < yr) { BYTE *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* - ((rs.rt & YDECR) ? - (yr-1 - wtr->yscan) : - wtr->yscan); + (yr-1 - wtr->yscan); if (tmflags & TM_F_BW) memcpy((void *)wtr->scanline, (void *)scn, xr); else @@ -368,6 +414,8 @@ tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIM if (fflush((FILE *)wtr->c_data) < 0) quiterr("error writing BMP output"); /* clean up */ + if (fnin != NULL) + fclose(fp); free((void *)pa); BMPcloseOutput(wtr); }