| 6 |
|
*/ |
| 7 |
|
|
| 8 |
|
#include <stdio.h> |
| 9 |
+ |
#include <math.h> |
| 10 |
+ |
#include <string.h> |
| 11 |
+ |
|
| 12 |
|
#include "platform.h" |
| 13 |
|
#include "color.h" |
| 14 |
+ |
#include "tonemap.h" |
| 15 |
|
#include "resolu.h" |
| 16 |
|
#include "bmpfile.h" |
| 17 |
|
|
| 18 |
< |
int bradj = 0; /* brightness adjustment */ |
| 18 |
> |
int bradj = 0; /* brightness adjustment */ |
| 19 |
|
|
| 20 |
< |
double gamcor = 2.2; /* gamma correction value */ |
| 20 |
> |
double gamcor = 2.2; /* gamma correction value */ |
| 21 |
|
|
| 22 |
< |
char *progname; |
| 22 |
> |
char *progname; |
| 23 |
|
|
| 24 |
< |
void quiterr(const char *); |
| 25 |
< |
void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry); |
| 26 |
< |
void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
| 24 |
> |
static void quiterr(const char *err); |
| 25 |
> |
static void tmap2bmp(char *fnin, char *fnout, char *expec, |
| 26 |
> |
RGBPRIMP monpri, double gamval); |
| 27 |
> |
static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri); |
| 28 |
> |
static void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
| 29 |
|
|
| 30 |
+ |
static RGBPRIMP rgbinp = stdprims; /* RGB input primitives */ |
| 31 |
+ |
static RGBPRIMS myinprims; /* custom primitives holder */ |
| 32 |
|
|
| 33 |
+ |
static gethfunc headline; |
| 34 |
+ |
|
| 35 |
+ |
|
| 36 |
|
int |
| 37 |
|
main(int argc, char *argv[]) |
| 38 |
|
{ |
| 39 |
< |
char *inpfile=NULL, *outfile=NULL; |
| 40 |
< |
int gryflag = 0; |
| 41 |
< |
int reverse = 0; |
| 42 |
< |
RESOLU rs; |
| 43 |
< |
int i; |
| 39 |
> |
char *inpfile=NULL, *outfile=NULL; |
| 40 |
> |
char *expec = NULL; |
| 41 |
> |
int reverse = 0; |
| 42 |
> |
RGBPRIMP rgbp = stdprims; |
| 43 |
> |
RGBPRIMS myprims; |
| 44 |
> |
RESOLU rs; |
| 45 |
> |
int i; |
| 46 |
|
|
| 47 |
|
progname = argv[0]; |
| 48 |
|
|
| 49 |
|
for (i = 1; i < argc; i++) |
| 50 |
< |
if (argv[i][0] == '-') |
| 50 |
> |
if (argv[i][0] == '-' && argv[i][1]) |
| 51 |
|
switch (argv[i][1]) { |
| 52 |
|
case 'b': |
| 53 |
< |
gryflag = 1; |
| 53 |
> |
rgbp = NULL; |
| 54 |
|
break; |
| 55 |
|
case 'g': |
| 56 |
|
gamcor = atof(argv[++i]); |
| 57 |
|
break; |
| 58 |
|
case 'e': |
| 59 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 60 |
+ |
expec = argv[++i]; |
| 61 |
+ |
else |
| 62 |
+ |
bradj = atoi(argv[++i]); |
| 63 |
+ |
break; |
| 64 |
+ |
case 'p': |
| 65 |
+ |
if (argc-i < 9) |
| 66 |
|
goto userr; |
| 67 |
< |
bradj = atoi(argv[++i]); |
| 67 |
> |
myprims[RED][CIEX] = atof(argv[++i]); |
| 68 |
> |
myprims[RED][CIEY] = atof(argv[++i]); |
| 69 |
> |
myprims[GRN][CIEX] = atof(argv[++i]); |
| 70 |
> |
myprims[GRN][CIEY] = atof(argv[++i]); |
| 71 |
> |
myprims[BLU][CIEX] = atof(argv[++i]); |
| 72 |
> |
myprims[BLU][CIEY] = atof(argv[++i]); |
| 73 |
> |
myprims[WHT][CIEX] = atof(argv[++i]); |
| 74 |
> |
myprims[WHT][CIEY] = atof(argv[++i]); |
| 75 |
> |
if (rgbp == stdprims) |
| 76 |
> |
rgbp = myprims; |
| 77 |
|
break; |
| 78 |
|
case 'r': |
| 79 |
|
reverse = !reverse; |
| 80 |
|
break; |
| 53 |
– |
case '\0': |
| 54 |
– |
break; |
| 81 |
|
default: |
| 82 |
|
goto userr; |
| 83 |
|
} |
| 96 |
|
|
| 97 |
|
if (i == argc-2 && strcmp(argv[i+1], "-")) |
| 98 |
|
outfile = argv[i+1]; |
| 99 |
+ |
/* check for tone-mapping */ |
| 100 |
+ |
if (expec != NULL) { |
| 101 |
+ |
if (reverse) |
| 102 |
+ |
goto userr; |
| 103 |
+ |
tmap2bmp(inpfile, outfile, expec, rgbp, gamcor); |
| 104 |
+ |
return(0); |
| 105 |
+ |
} |
| 106 |
|
|
| 107 |
|
setcolrgam(gamcor); /* set up conversion */ |
| 108 |
|
|
| 133 |
|
rs.xr = rdr->hdr->width; |
| 134 |
|
rs.yr = rdr->hdr->height; |
| 135 |
|
rs.rt = YMAJOR; |
| 136 |
+ |
/* write scans downward if we can */ |
| 137 |
|
if (rdr->hdr->yIsDown || inpfile != NULL) |
| 138 |
|
rs.rt |= YDECR; |
| 139 |
|
fputsresolu(&rs, stdout); |
| 140 |
|
/* convert file */ |
| 141 |
|
bmp2rad(rdr, stdout, !rdr->hdr->yIsDown && inpfile!=NULL); |
| 142 |
|
/* flush output */ |
| 143 |
< |
BMPcloseInput(rdr); /* COMMENT OUT LATER */ |
| 143 |
> |
BMPcloseInput(rdr); |
| 144 |
|
if (fflush(stdout) < 0) |
| 145 |
|
quiterr("error writing Radiance output"); |
| 146 |
|
} else { |
| 153 |
|
exit(1); |
| 154 |
|
} |
| 155 |
|
/* get header info. */ |
| 156 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
| 156 |
> |
if (getheader(stdin, headline, NULL) < 0 || |
| 157 |
|
!fgetsresolu(&rs, stdin)) |
| 158 |
|
quiterr("bad Radiance picture format"); |
| 159 |
|
/* initialize BMP header */ |
| 160 |
< |
if (gryflag) |
| 161 |
< |
hdr = BMPmappedHeader(numscans(&rs), |
| 162 |
< |
scanlen(&rs), 0, 256); |
| 163 |
< |
else |
| 164 |
< |
hdr = BMPtruecolorHeader(numscans(&rs), |
| 165 |
< |
scanlen(&rs), 0); |
| 160 |
> |
if (rgbp == NULL) { |
| 161 |
> |
hdr = BMPmappedHeader(scanlen(&rs), |
| 162 |
> |
numscans(&rs), 0, 256); |
| 163 |
> |
/* |
| 164 |
> |
if (outfile != NULL) |
| 165 |
> |
hdr->compr = BI_RLE8; |
| 166 |
> |
*/ |
| 167 |
> |
} else |
| 168 |
> |
hdr = BMPtruecolorHeader(scanlen(&rs), |
| 169 |
> |
numscans(&rs), 0); |
| 170 |
|
if (hdr == NULL) |
| 171 |
|
quiterr("cannot initialize BMP header"); |
| 172 |
< |
hdr->yIsDown = (rs.rt & YDECR) && outfile == NULL; |
| 172 |
> |
/* set up output direction */ |
| 173 |
> |
hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8)); |
| 174 |
|
/* open BMP output */ |
| 175 |
|
if (outfile != NULL) |
| 176 |
|
wtr = BMPopenOutputFile(outfile, hdr); |
| 177 |
|
else |
| 178 |
|
wtr = BMPopenOutputStream(stdout, hdr); |
| 179 |
+ |
if (wtr == NULL) |
| 180 |
+ |
quiterr("cannot allocate writer structure"); |
| 181 |
|
/* convert file */ |
| 182 |
< |
rad2bmp(stdin, wtr, !hdr->yIsDown && (rs.rt&YDECR), gryflag); |
| 182 |
> |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp); |
| 183 |
|
/* flush output */ |
| 184 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
| 185 |
|
quiterr("error writing BMP output"); |
| 186 |
< |
BMPcloseOutput(wtr); /* COMMENT OUT LATER */ |
| 186 |
> |
BMPcloseOutput(wtr); |
| 187 |
|
} |
| 188 |
< |
exit(0); /* success */ |
| 188 |
> |
return(0); /* success */ |
| 189 |
|
userr: |
| 190 |
|
fprintf(stderr, |
| 191 |
< |
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
| 191 |
> |
"Usage: %s [-b][-g gamma][-e spec][-p xr yr xg yg xb yb xw yw] [input|- [output]]\n", |
| 192 |
|
progname); |
| 193 |
< |
exit(1); |
| 194 |
< |
return(1); /* to keep compiler happy */ |
| 193 |
> |
fprintf(stderr, |
| 194 |
> |
" or: %s -r [-g gamma][-e +/-stops] [input|- [output]]\n", |
| 195 |
> |
progname); |
| 196 |
> |
return(1); |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
/* print message and exit */ |
| 200 |
< |
void |
| 200 |
> |
static void |
| 201 |
|
quiterr(const char *err) |
| 202 |
|
{ |
| 203 |
|
if (err != NULL) { |
| 207 |
|
exit(0); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
+ |
/* process header line (don't echo) */ |
| 211 |
+ |
static int |
| 212 |
+ |
headline(char *s, void *p) |
| 213 |
+ |
{ |
| 214 |
+ |
char fmt[MAXFMTLEN]; |
| 215 |
+ |
|
| 216 |
+ |
if (formatval(fmt, s)) { /* check if format string */ |
| 217 |
+ |
if (!strcmp(fmt,COLRFMT)) |
| 218 |
+ |
return(0); |
| 219 |
+ |
if (!strcmp(fmt,CIEFMT)) { |
| 220 |
+ |
rgbinp = TM_XYZPRIM; |
| 221 |
+ |
return(0); |
| 222 |
+ |
} |
| 223 |
+ |
return(-1); |
| 224 |
+ |
} |
| 225 |
+ |
if (isprims(s)) { /* get input primaries */ |
| 226 |
+ |
primsval(myinprims, s); |
| 227 |
+ |
rgbinp = myinprims; |
| 228 |
+ |
return(0); |
| 229 |
+ |
} |
| 230 |
+ |
/* should I grok colcorr also? */ |
| 231 |
+ |
return(0); |
| 232 |
+ |
} |
| 233 |
+ |
|
| 234 |
+ |
|
| 235 |
|
/* convert Radiance picture to BMP */ |
| 236 |
< |
void |
| 237 |
< |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) |
| 236 |
> |
static void |
| 237 |
> |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri) |
| 238 |
|
{ |
| 239 |
+ |
int usexfm = 0; |
| 240 |
+ |
COLORMAT xfm; |
| 241 |
|
COLR *scanin; |
| 242 |
+ |
COLOR cval; |
| 243 |
|
int y, yend, ystp; |
| 244 |
|
int x; |
| 245 |
|
/* allocate scanline */ |
| 246 |
|
scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); |
| 247 |
|
if (scanin == NULL) |
| 248 |
|
quiterr("out of memory in rad2bmp"); |
| 249 |
+ |
/* set up color conversion */ |
| 250 |
+ |
usexfm = (monpri != NULL ? rgbinp != monpri : |
| 251 |
+ |
rgbinp != TM_XYZPRIM && rgbinp != stdprims); |
| 252 |
+ |
if (usexfm) { |
| 253 |
+ |
double expcomp = pow(2.0, (double)bradj); |
| 254 |
+ |
if (rgbinp == TM_XYZPRIM) |
| 255 |
+ |
compxyz2rgbWBmat(xfm, monpri); |
| 256 |
+ |
else |
| 257 |
+ |
comprgb2rgbWBmat(xfm, rgbinp, monpri); |
| 258 |
+ |
for (y = 0; y < 3; y++) |
| 259 |
+ |
for (x = 0; x < 3; x++) |
| 260 |
+ |
xfm[y][x] *= expcomp; |
| 261 |
+ |
} |
| 262 |
|
/* convert image */ |
| 263 |
|
if (inv) { |
| 264 |
|
y = bwr->hdr->height - 1; |
| 271 |
|
for ( ; y != yend; y += ystp) { |
| 272 |
|
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
| 273 |
|
quiterr("error reading Radiance picture"); |
| 274 |
< |
if (bradj) |
| 274 |
> |
if (usexfm) |
| 275 |
> |
for (x = bwr->hdr->width; x--; ) { |
| 276 |
> |
colr_color(cval, scanin[x]); |
| 277 |
> |
colortrans(cval, xfm, cval); |
| 278 |
> |
setcolr(scanin[x], colval(cval,RED), |
| 279 |
> |
colval(cval,GRN), |
| 280 |
> |
colval(cval,BLU)); |
| 281 |
> |
} |
| 282 |
> |
else if (bradj) |
| 283 |
|
shiftcolrs(scanin, bwr->hdr->width, bradj); |
| 284 |
< |
for (x = gry ? bwr->hdr->width : 0; x--; ) |
| 285 |
< |
scanin[x][GRN] = normbright(scanin[x]); |
| 284 |
> |
if (monpri == NULL && rgbinp != TM_XYZPRIM) |
| 285 |
> |
for (x = bwr->hdr->width; x--; ) |
| 286 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
| 287 |
|
colrs_gambs(scanin, bwr->hdr->width); |
| 288 |
< |
if (gry) |
| 288 |
> |
if (monpri == NULL) |
| 289 |
|
for (x = bwr->hdr->width; x--; ) |
| 290 |
|
bwr->scanline[x] = scanin[x][GRN]; |
| 291 |
|
else |
| 304 |
|
} |
| 305 |
|
|
| 306 |
|
/* convert BMP file to Radiance */ |
| 307 |
< |
void |
| 307 |
> |
static void |
| 308 |
|
bmp2rad(BMPReader *brd, FILE *rfp, int inv) |
| 309 |
|
{ |
| 310 |
|
COLR *scanout; |
| 341 |
|
} |
| 342 |
|
/* clean up */ |
| 343 |
|
free((void *)scanout); |
| 344 |
+ |
} |
| 345 |
+ |
|
| 346 |
+ |
/* Tone-map and convert Radiance picture */ |
| 347 |
+ |
static void |
| 348 |
+ |
tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIMP monpri, double gamval) |
| 349 |
+ |
{ |
| 350 |
+ |
int tmflags; |
| 351 |
+ |
BMPHeader *hdr; |
| 352 |
+ |
BMPWriter *wtr; |
| 353 |
+ |
FILE *fp; |
| 354 |
+ |
int xr, yr; |
| 355 |
+ |
uby8 *pa; |
| 356 |
+ |
int i; |
| 357 |
+ |
/* check tone-mapping spec */ |
| 358 |
+ |
i = strlen(expec); |
| 359 |
+ |
if (i && !strncmp(expec, "auto", i)) |
| 360 |
+ |
tmflags = TM_F_CAMERA; |
| 361 |
+ |
else if (i && !strncmp(expec, "human", i)) |
| 362 |
+ |
tmflags = TM_F_HUMAN & ~TM_F_UNIMPL; |
| 363 |
+ |
else if (i && !strncmp(expec, "linear", i)) |
| 364 |
+ |
tmflags = TM_F_LINEAR; |
| 365 |
+ |
else |
| 366 |
+ |
quiterr("illegal exposure specification (auto|human|linear)"); |
| 367 |
+ |
if (monpri == NULL) { |
| 368 |
+ |
tmflags |= TM_F_BW; |
| 369 |
+ |
monpri = stdprims; |
| 370 |
+ |
} |
| 371 |
+ |
/* open Radiance input */ |
| 372 |
+ |
if (fnin == NULL) |
| 373 |
+ |
fp = stdin; |
| 374 |
+ |
else if ((fp = fopen(fnin, "r")) == NULL) { |
| 375 |
+ |
fprintf(stderr, "%s: cannot open\n", fnin); |
| 376 |
+ |
exit(1); |
| 377 |
+ |
} |
| 378 |
+ |
/* tone-map picture */ |
| 379 |
+ |
if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval, |
| 380 |
+ |
0., 0., fnin, fp) != TM_E_OK) |
| 381 |
+ |
exit(1); |
| 382 |
+ |
/* initialize BMP header */ |
| 383 |
+ |
if (tmflags & TM_F_BW) { |
| 384 |
+ |
hdr = BMPmappedHeader(xr, yr, 0, 256); |
| 385 |
+ |
if (fnout != NULL) |
| 386 |
+ |
hdr->compr = BI_RLE8; |
| 387 |
+ |
} else |
| 388 |
+ |
hdr = BMPtruecolorHeader(xr, yr, 0); |
| 389 |
+ |
if (hdr == NULL) |
| 390 |
+ |
quiterr("cannot initialize BMP header"); |
| 391 |
+ |
/* open BMP output */ |
| 392 |
+ |
if (fnout != NULL) |
| 393 |
+ |
wtr = BMPopenOutputFile(fnout, hdr); |
| 394 |
+ |
else |
| 395 |
+ |
wtr = BMPopenOutputStream(stdout, hdr); |
| 396 |
+ |
if (wtr == NULL) |
| 397 |
+ |
quiterr("cannot allocate writer structure"); |
| 398 |
+ |
/* write to BMP file */ |
| 399 |
+ |
while (wtr->yscan < yr) { |
| 400 |
+ |
uby8 *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
| 401 |
+ |
(yr-1 - wtr->yscan); |
| 402 |
+ |
if (tmflags & TM_F_BW) |
| 403 |
+ |
memcpy((void *)wtr->scanline, (void *)scn, xr); |
| 404 |
+ |
else |
| 405 |
+ |
for (i = xr; i--; ) { |
| 406 |
+ |
wtr->scanline[3*i] = scn[3*i+BLU]; |
| 407 |
+ |
wtr->scanline[3*i+1] = scn[3*i+GRN]; |
| 408 |
+ |
wtr->scanline[3*i+2] = scn[3*i+RED]; |
| 409 |
+ |
} |
| 410 |
+ |
if ((i = BMPwriteScanline(wtr)) != BIR_OK) |
| 411 |
+ |
quiterr(BMPerrorMessage(i)); |
| 412 |
+ |
} |
| 413 |
+ |
/* flush output */ |
| 414 |
+ |
if (fflush((FILE *)wtr->c_data) < 0) |
| 415 |
+ |
quiterr("error writing BMP output"); |
| 416 |
+ |
/* clean up */ |
| 417 |
+ |
if (fnin != NULL) |
| 418 |
+ |
fclose(fp); |
| 419 |
+ |
free((void *)pa); |
| 420 |
+ |
BMPcloseOutput(wtr); |
| 421 |
|
} |