1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_bmp.c,v 2.9 2004/09/23 18:00:54 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* program to convert between RADIANCE and Windows BMP file |
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 */ |
19 |
|
20 |
double gamcor = 2.2; /* gamma correction value */ |
21 |
|
22 |
char *progname; |
23 |
|
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 |
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] == '-' && argv[i][1]) |
51 |
switch (argv[i][1]) { |
52 |
case 'b': |
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 |
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; |
81 |
default: |
82 |
goto userr; |
83 |
} |
84 |
else |
85 |
break; |
86 |
|
87 |
if (i < argc-2) |
88 |
goto userr; |
89 |
|
90 |
SET_FILE_BINARY(stdin); |
91 |
SET_FILE_BINARY(stdout); |
92 |
SET_DEFAULT_BINARY(); |
93 |
|
94 |
if (i <= argc-1 && strcmp(argv[i], "-")) |
95 |
inpfile = argv[i]; |
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 |
|
109 |
if (reverse) { |
110 |
BMPReader *rdr; |
111 |
/* open BMP file or stream */ |
112 |
if (inpfile != NULL) |
113 |
rdr = BMPopenInputFile(inpfile); |
114 |
else |
115 |
rdr = BMPopenInputStream(stdin); |
116 |
|
117 |
if (rdr == NULL) { |
118 |
fprintf(stderr, "%s: cannot open or recognize BMP\n", |
119 |
inpfile != NULL ? inpfile : "<stdin>"); |
120 |
exit(1); |
121 |
} |
122 |
/* open Radiance output */ |
123 |
if (outfile != NULL && freopen(outfile, "w", stdout) == NULL) { |
124 |
fprintf(stderr, "%s: cannot open for output\n", |
125 |
outfile); |
126 |
exit(1); |
127 |
} |
128 |
/* put Radiance header */ |
129 |
newheader("RADIANCE", stdout); |
130 |
printargs(i, argv, stdout); |
131 |
fputformat(COLRFMT, stdout); |
132 |
putchar('\n'); |
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); |
144 |
if (fflush(stdout) < 0) |
145 |
quiterr("error writing Radiance output"); |
146 |
} else { |
147 |
BMPHeader *hdr; |
148 |
BMPWriter *wtr; |
149 |
/* open Radiance input */ |
150 |
if (inpfile != NULL && freopen(inpfile, "r", stdin) == NULL) { |
151 |
fprintf(stderr, "%s: cannot open input file\n", |
152 |
inpfile); |
153 |
exit(1); |
154 |
} |
155 |
/* get header info. */ |
156 |
if (getheader(stdin, headline, NULL) < 0 || |
157 |
!fgetsresolu(&rs, stdin)) |
158 |
quiterr("bad Radiance picture format"); |
159 |
/* initialize BMP header */ |
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 |
/* 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, rgbp); |
183 |
/* flush output */ |
184 |
if (fflush((FILE *)wtr->c_data) < 0) |
185 |
quiterr("error writing BMP output"); |
186 |
BMPcloseOutput(wtr); |
187 |
} |
188 |
return(0); /* success */ |
189 |
userr: |
190 |
fprintf(stderr, |
191 |
"Usage: %s [-b][-g gamma][-e spec][-p xr yr xg yg xb yb xw yw] [input|- [output]]\n", |
192 |
progname); |
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 |
static void |
201 |
quiterr(const char *err) |
202 |
{ |
203 |
if (err != NULL) { |
204 |
fprintf(stderr, "%s: %s\n", progname, err); |
205 |
exit(1); |
206 |
} |
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[32]; |
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 |
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; |
265 |
ystp = -1; yend = -1; |
266 |
} else { |
267 |
y = 0; |
268 |
ystp = 1; yend = bwr->hdr->height; |
269 |
} |
270 |
/* convert each scanline */ |
271 |
for ( ; y != yend; y += ystp) { |
272 |
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
273 |
quiterr("error reading Radiance picture"); |
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 |
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 (monpri == NULL) |
289 |
for (x = bwr->hdr->width; x--; ) |
290 |
bwr->scanline[x] = scanin[x][GRN]; |
291 |
else |
292 |
for (x = bwr->hdr->width; x--; ) { |
293 |
bwr->scanline[3*x] = scanin[x][BLU]; |
294 |
bwr->scanline[3*x+1] = scanin[x][GRN]; |
295 |
bwr->scanline[3*x+2] = scanin[x][RED]; |
296 |
} |
297 |
bwr->yscan = y; |
298 |
x = BMPwriteScanline(bwr); |
299 |
if (x != BIR_OK) |
300 |
quiterr(BMPerrorMessage(x)); |
301 |
} |
302 |
/* free scanline */ |
303 |
free((void *)scanin); |
304 |
} |
305 |
|
306 |
/* convert BMP file to Radiance */ |
307 |
static void |
308 |
bmp2rad(BMPReader *brd, FILE *rfp, int inv) |
309 |
{ |
310 |
COLR *scanout; |
311 |
int y, yend, ystp; |
312 |
int x; |
313 |
/* allocate scanline */ |
314 |
scanout = (COLR *)malloc(brd->hdr->width*sizeof(COLR)); |
315 |
if (scanout == NULL) |
316 |
quiterr("out of memory in bmp2rad"); |
317 |
/* convert image */ |
318 |
if (inv) { |
319 |
y = brd->hdr->height - 1; |
320 |
ystp = -1; yend = -1; |
321 |
} else { |
322 |
y = 0; |
323 |
ystp = 1; yend = brd->hdr->height; |
324 |
} |
325 |
/* convert each scanline */ |
326 |
for ( ; y != yend; y += ystp) { |
327 |
x = BMPseekScanline(y, brd); |
328 |
if (x != BIR_OK) |
329 |
quiterr(BMPerrorMessage(x)); |
330 |
for (x = brd->hdr->width; x--; ) { |
331 |
RGBquad rgbq = BMPdecodePixel(x, brd); |
332 |
scanout[x][RED] = rgbq.r; |
333 |
scanout[x][GRN] = rgbq.g; |
334 |
scanout[x][BLU] = rgbq.b; |
335 |
} |
336 |
gambs_colrs(scanout, brd->hdr->width); |
337 |
if (bradj) |
338 |
shiftcolrs(scanout, brd->hdr->width, bradj); |
339 |
if (fwritecolrs(scanout, brd->hdr->width, rfp) < 0) |
340 |
quiterr("error writing Radiance picture"); |
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 |
BYTE *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 |
BYTE *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 |
} |