--- ray/src/px/ra_bn.c 1990/02/09 13:59:17 1.3 +++ ray/src/px/ra_bn.c 2003/06/05 19:29:34 2.8 @@ -1,9 +1,6 @@ -/* Copyright (c) 1986 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ra_bn.c,v 2.8 2003/06/05 19:29:34 schorsch Exp $"; #endif - /* * ra_bn.c - program to convert between RADIANCE and barneyscan picture format. * @@ -11,12 +8,16 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include +#include +#include "platform.h" #include "color.h" +#include "resolu.h" -extern double atof(), pow(); +double gamcor = 2.0; /* gamma correction */ -double gamma = 2.0; /* gamma correction */ +int bradj = 0; /* brightness adjustment */ char *progname; @@ -33,23 +34,32 @@ char *argv[]; { int reverse = 0; int i; - + SET_DEFAULT_BINARY(); + SET_FILE_BINARY(stdin); + SET_FILE_BINARY(stdout); progname = argv[0]; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { case 'g': - gamma = atof(argv[++i]); + gamcor = atof(argv[++i]); break; case 'r': reverse = !reverse; break; + case 'e': + if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + goto userr; + bradj = atoi(argv[++i]); + break; default: goto userr; } else break; + /* set gamma correction */ + setcolrgam(gamcor); if (reverse) { if (i > argc-1 || i < argc-2) @@ -66,9 +76,11 @@ char *argv[]; quiterr(errmsg); } /* put header */ + newheader("RADIANCE", stdout); printargs(i, argv, rafp); + fputformat(COLRFMT, rafp); putc('\n', rafp); - fputresolu(YMAJOR|YDECR, xmax, ymax, rafp); + fprtresolu(xmax, ymax, rafp); /* convert file */ bn2ra(); } else { @@ -82,8 +94,8 @@ char *argv[]; quiterr(errmsg); } /* get header */ - getheader(rafp, NULL); - if (fgetresolu(&xmax, &ymax, rafp) != (YMAJOR|YDECR)) + if (checkheader(rafp, COLRFMT, NULL) < 0 || + fgetresolu(&xmax, &ymax, rafp) < 0) quiterr("bad RADIANCE format"); if (openbarney(argv[i+1], "w") < 0) { sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); @@ -94,9 +106,10 @@ char *argv[]; } quiterr(NULL); userr: - fprintf(stderr, "Usage: %s {input|-} output\n", progname); - fprintf(stderr, " or: %s -r [-g gamma] input [output|-]\n", + fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n", progname); + fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n", + progname); exit(1); } @@ -165,50 +178,51 @@ register FILE *fp; ra2bn() /* convert radiance to barneyscan */ { - unsigned char gmap[1024]; - register int i,k,c; - register COLOR *inline; + register int i; + register COLR *inl; int j; - if ((inline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) + if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) quiterr("out of memory"); - for (i = 0; i < 1024; i++) - gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); for (j = 0; j < ymax; j++) { - if (freadscan(inline, xmax, rafp) < 0) + if (freadcolrs(inl, xmax, rafp) < 0) quiterr("error reading RADIANCE file"); - for (i = 0; i < xmax; i++) - for (k = 0; k < 3; k++) { - c = 1024.*colval(inline[i],k); - if (c >= 1024) - c = 1023; - putc(gmap[c], bnfp[k]); - } + if (bradj) + shiftcolrs(inl, xmax, bradj); + colrs_gambs(inl, xmax); + for (i = 0; i < xmax; i++) { + putc(inl[i][RED], bnfp[0]); + putc(inl[i][GRN], bnfp[1]); + putc(inl[i][BLU], bnfp[2]); + } + if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2])) + quiterr("error writing Barney files"); } - free((char *)inline); + free((void *)inl); } bn2ra() /* convert barneyscan to radiance */ { - float gmap[256]; - register int i,k,c; - register COLOR *outline; + register int i; + register COLR *outline; int j; - if ((outline = (COLOR *)malloc(xmax*sizeof(COLOR))) == NULL) + if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) quiterr("out of memory"); - for (i = 0; i < 256; i++) - gmap[i] = pow((i+.5)/256., gamma); for (j = 0; j < ymax; j++) { - for (i = 0; i < xmax; i++) - for (k = 0; k < 3; k++) - if ((c = getc(bnfp[k])) == EOF) - quiterr("error reading barney file"); - else - colval(outline[i],k) = gmap[c]; - if (fwritescan(outline, xmax, rafp) < 0) + for (i = 0; i < xmax; i++) { + outline[i][RED] = getc(bnfp[0]); + outline[i][GRN] = getc(bnfp[1]); + outline[i][BLU] = getc(bnfp[2]); + } + if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2])) + quiterr("error reading barney file"); + gambs_colrs(outline, xmax); + if (bradj) + shiftcolrs(outline, xmax, bradj); + if (fwritecolrs(outline, xmax, rafp) < 0) quiterr("error writing RADIANCE file"); } - free((char *)outline); + free((void *)outline); }