10 |
|
|
11 |
|
#include "platform.h" |
12 |
|
#include "color.h" |
13 |
+ |
#include "tonemap.h" |
14 |
|
#include "resolu.h" |
15 |
|
#include "bmpfile.h" |
16 |
|
|
20 |
|
|
21 |
|
char *progname; |
22 |
|
|
23 |
< |
static void quiterr(const char *); |
23 |
> |
static void quiterr(const char *err); |
24 |
> |
static void tmap2bmp(char *fnin, char *fnout, char *expec, |
25 |
> |
RGBPRIMP monpri, double gamval); |
26 |
|
static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry); |
27 |
|
static void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
28 |
|
|
30 |
|
int |
31 |
|
main(int argc, char *argv[]) |
32 |
|
{ |
33 |
< |
char *inpfile=NULL, *outfile=NULL; |
34 |
< |
int gryflag = 0; |
35 |
< |
int reverse = 0; |
36 |
< |
RESOLU rs; |
37 |
< |
int i; |
33 |
> |
char *inpfile=NULL, *outfile=NULL; |
34 |
> |
char *expec = NULL; |
35 |
> |
int reverse = 0; |
36 |
> |
RGBPRIMP rgbp = stdprims; |
37 |
> |
RGBPRIMS myprims; |
38 |
> |
RESOLU rs; |
39 |
> |
int i; |
40 |
|
|
41 |
|
progname = argv[0]; |
42 |
|
|
44 |
|
if (argv[i][0] == '-') |
45 |
|
switch (argv[i][1]) { |
46 |
|
case 'b': |
47 |
< |
gryflag = 1; |
47 |
> |
rgbp = NULL; |
48 |
|
break; |
49 |
|
case 'g': |
50 |
|
gamcor = atof(argv[++i]); |
51 |
|
break; |
52 |
|
case 'e': |
53 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
54 |
+ |
expec = argv[++i]; |
55 |
+ |
else |
56 |
+ |
bradj = atoi(argv[++i]); |
57 |
+ |
break; |
58 |
+ |
case 'p': |
59 |
+ |
if (argc-i < 9) |
60 |
|
goto userr; |
61 |
< |
bradj = atoi(argv[++i]); |
61 |
> |
myprims[RED][CIEX] = atof(argv[++i]); |
62 |
> |
myprims[RED][CIEY] = atof(argv[++i]); |
63 |
> |
myprims[GRN][CIEX] = atof(argv[++i]); |
64 |
> |
myprims[GRN][CIEY] = atof(argv[++i]); |
65 |
> |
myprims[BLU][CIEX] = atof(argv[++i]); |
66 |
> |
myprims[BLU][CIEY] = atof(argv[++i]); |
67 |
> |
myprims[WHT][CIEX] = atof(argv[++i]); |
68 |
> |
myprims[WHT][CIEY] = atof(argv[++i]); |
69 |
> |
if (rgbp == stdprims) |
70 |
> |
rgbp = myprims; |
71 |
|
break; |
72 |
|
case 'r': |
73 |
|
reverse = !reverse; |
92 |
|
|
93 |
|
if (i == argc-2 && strcmp(argv[i+1], "-")) |
94 |
|
outfile = argv[i+1]; |
95 |
+ |
/* check for tone-mapping */ |
96 |
+ |
if (expec != NULL) { |
97 |
+ |
if (reverse) |
98 |
+ |
goto userr; |
99 |
+ |
tmap2bmp(inpfile, outfile, expec, rgbp, gamcor); |
100 |
+ |
return(0); |
101 |
+ |
} |
102 |
|
|
103 |
|
setcolrgam(gamcor); /* set up conversion */ |
104 |
|
|
153 |
|
!fgetsresolu(&rs, stdin)) |
154 |
|
quiterr("bad Radiance picture format"); |
155 |
|
/* initialize BMP header */ |
156 |
< |
if (gryflag) { |
157 |
< |
hdr = BMPmappedHeader(numscans(&rs), |
158 |
< |
scanlen(&rs), 0, 256); |
156 |
> |
if (rgbp == NULL) { |
157 |
> |
hdr = BMPmappedHeader(scanlen(&rs), |
158 |
> |
numscans(&rs), 0, 256); |
159 |
|
if (outfile != NULL) |
160 |
|
hdr->compr = BI_RLE8; |
161 |
|
} else |
162 |
< |
hdr = BMPtruecolorHeader(numscans(&rs), |
163 |
< |
scanlen(&rs), 0); |
162 |
> |
hdr = BMPtruecolorHeader(scanlen(&rs), |
163 |
> |
numscans(&rs), 0); |
164 |
|
if (hdr == NULL) |
165 |
|
quiterr("cannot initialize BMP header"); |
166 |
|
/* set up output direction */ |
174 |
|
if (wtr == NULL) |
175 |
|
quiterr("cannot allocate writer structure"); |
176 |
|
/* convert file */ |
177 |
< |
rad2bmp(stdin, wtr, !hdr->yIsDown && (rs.rt&YDECR), gryflag); |
177 |
> |
rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL); |
178 |
|
/* flush output */ |
179 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
180 |
|
quiterr("error writing BMP output"); |
181 |
|
BMPcloseOutput(wtr); |
182 |
|
} |
183 |
< |
exit(0); /* success */ |
183 |
> |
return(0); /* success */ |
184 |
|
userr: |
185 |
|
fprintf(stderr, |
186 |
< |
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
186 |
> |
"Usage: %s [-b][-g gamma][-e spec][-p xr yr xg yg xb yb xw yw] [input|- [output]]\n", |
187 |
|
progname); |
188 |
< |
exit(1); |
189 |
< |
return(1); /* gratis return */ |
188 |
> |
fprintf(stderr, |
189 |
> |
" or: %s -r [-g gamma][-e +/-stops] [input|- [output]]\n", |
190 |
> |
progname); |
191 |
> |
return(1); |
192 |
|
} |
193 |
|
|
194 |
|
/* print message and exit */ |
286 |
|
} |
287 |
|
/* clean up */ |
288 |
|
free((void *)scanout); |
289 |
+ |
} |
290 |
+ |
|
291 |
+ |
/* Tone-map and convert Radiance picture */ |
292 |
+ |
static void |
293 |
+ |
tmap2bmp(char *fnin, char *fnout, char *expec, RGBPRIMP monpri, double gamval) |
294 |
+ |
{ |
295 |
+ |
int tmflags; |
296 |
+ |
BMPHeader *hdr; |
297 |
+ |
BMPWriter *wtr; |
298 |
+ |
RESOLU rs; |
299 |
+ |
FILE *fp; |
300 |
+ |
int xr, yr; |
301 |
+ |
BYTE *pa; |
302 |
+ |
int i; |
303 |
+ |
/* check tone-mapping spec */ |
304 |
+ |
i = strlen(expec); |
305 |
+ |
if (i && !strncmp(expec, "auto", i)) |
306 |
+ |
tmflags = TM_F_CAMERA; |
307 |
+ |
else if (i && !strncmp(expec, "human", i)) |
308 |
+ |
tmflags = TM_F_HUMAN & ~TM_F_UNIMPL; |
309 |
+ |
else if (i && !strncmp(expec, "linear", i)) |
310 |
+ |
tmflags = TM_F_LINEAR; |
311 |
+ |
else |
312 |
+ |
quiterr("illegal exposure specification (auto|human|linear)"); |
313 |
+ |
if (monpri == NULL) { |
314 |
+ |
tmflags |= TM_F_BW; |
315 |
+ |
monpri = stdprims; |
316 |
+ |
} |
317 |
+ |
/* open Radiance input */ |
318 |
+ |
if (fnin == NULL) |
319 |
+ |
fp = stdin; |
320 |
+ |
else if ((fp = fopen(fnin, "r")) == NULL) { |
321 |
+ |
fprintf(stderr, "%s: cannot open\n", fnin); |
322 |
+ |
exit(1); |
323 |
+ |
} |
324 |
+ |
/* get picture orientation */ |
325 |
+ |
if (fnin != NULL) { |
326 |
+ |
if (getheader(fp, NULL, NULL) < 0 || !fgetsresolu(&rs, fp)) |
327 |
+ |
quiterr("bad Radiance picture format"); |
328 |
+ |
rewind(fp); |
329 |
+ |
} else /* assume stdin has normal orient */ |
330 |
+ |
rs.rt = PIXSTANDARD; |
331 |
+ |
/* tone-map picture */ |
332 |
+ |
if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval, |
333 |
+ |
0., 0., fnin, fp) != TM_E_OK) |
334 |
+ |
exit(1); |
335 |
+ |
/* initialize BMP header */ |
336 |
+ |
if (tmflags & TM_F_BW) { |
337 |
+ |
hdr = BMPmappedHeader(xr, yr, 0, 256); |
338 |
+ |
hdr->compr = BI_RLE8; |
339 |
+ |
} else |
340 |
+ |
hdr = BMPtruecolorHeader(xr, yr, 0); |
341 |
+ |
if (hdr == NULL) |
342 |
+ |
quiterr("cannot initialize BMP header"); |
343 |
+ |
/* open BMP output */ |
344 |
+ |
if (fnout != NULL) |
345 |
+ |
wtr = BMPopenOutputFile(fnout, hdr); |
346 |
+ |
else |
347 |
+ |
wtr = BMPopenOutputStream(stdout, hdr); |
348 |
+ |
if (wtr == NULL) |
349 |
+ |
quiterr("cannot allocate writer structure"); |
350 |
+ |
/* write to BMP file */ |
351 |
+ |
while (wtr->yscan < yr) { |
352 |
+ |
BYTE *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
353 |
+ |
((rs.rt & YDECR) ? |
354 |
+ |
(yr-1 - wtr->yscan) : |
355 |
+ |
wtr->yscan); |
356 |
+ |
if (tmflags & TM_F_BW) |
357 |
+ |
memcpy((void *)wtr->scanline, (void *)scn, xr); |
358 |
+ |
else |
359 |
+ |
for (i = xr; i--; ) { |
360 |
+ |
wtr->scanline[3*i] = scn[3*i+BLU]; |
361 |
+ |
wtr->scanline[3*i+1] = scn[3*i+GRN]; |
362 |
+ |
wtr->scanline[3*i+2] = scn[3*i+RED]; |
363 |
+ |
} |
364 |
+ |
if ((i = BMPwriteScanline(wtr)) != BIR_OK) |
365 |
+ |
quiterr(BMPerrorMessage(i)); |
366 |
+ |
} |
367 |
+ |
/* flush output */ |
368 |
+ |
if (fflush((FILE *)wtr->c_data) < 0) |
369 |
+ |
quiterr("error writing BMP output"); |
370 |
+ |
/* clean up */ |
371 |
+ |
free((void *)pa); |
372 |
+ |
BMPcloseOutput(wtr); |
373 |
|
} |