5 |
|
* program to convert between RADIANCE and Windows BMP file |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include <stdio.h> |
9 |
< |
#include <string.h> |
8 |
> |
#include <math.h> |
9 |
|
|
10 |
+ |
#include "rtio.h" |
11 |
|
#include "platform.h" |
12 |
|
#include "color.h" |
13 |
|
#include "tonemap.h" |
14 |
|
#include "resolu.h" |
15 |
|
#include "bmpfile.h" |
16 |
|
|
17 |
< |
int bradj = 0; /* brightness adjustment */ |
17 |
> |
int bradj = 0; /* brightness adjustment */ |
18 |
|
|
19 |
< |
double gamcor = 2.2; /* gamma correction value */ |
19 |
> |
double gamcor = 2.2; /* gamma correction value */ |
20 |
|
|
21 |
< |
char *progname; |
21 |
> |
char *progname; |
22 |
|
|
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); |
26 |
> |
static void rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri); |
27 |
|
static void bmp2rad(BMPReader *brd, FILE *rfp, int inv); |
28 |
|
|
29 |
+ |
static RGBPRIMP rgbinp = stdprims; /* RGB input primitives */ |
30 |
+ |
static RGBPRIMS myinprims; /* custom primitives holder */ |
31 |
|
|
32 |
+ |
static gethfunc headline; |
33 |
+ |
|
34 |
+ |
|
35 |
|
int |
36 |
|
main(int argc, char *argv[]) |
37 |
|
{ |
46 |
|
progname = argv[0]; |
47 |
|
|
48 |
|
for (i = 1; i < argc; i++) |
49 |
< |
if (argv[i][0] == '-') |
49 |
> |
if (argv[i][0] == '-' && argv[i][1]) |
50 |
|
switch (argv[i][1]) { |
51 |
|
case 'b': |
52 |
|
rgbp = NULL; |
77 |
|
case 'r': |
78 |
|
reverse = !reverse; |
79 |
|
break; |
75 |
– |
case '\0': |
76 |
– |
break; |
80 |
|
default: |
81 |
|
goto userr; |
82 |
|
} |
152 |
|
exit(1); |
153 |
|
} |
154 |
|
/* get header info. */ |
155 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
155 |
> |
if (getheader(stdin, headline, NULL) < 0 || |
156 |
|
!fgetsresolu(&rs, stdin)) |
157 |
|
quiterr("bad Radiance picture format"); |
158 |
|
/* initialize BMP header */ |
159 |
|
if (rgbp == NULL) { |
160 |
|
hdr = BMPmappedHeader(scanlen(&rs), |
161 |
|
numscans(&rs), 0, 256); |
162 |
+ |
/* |
163 |
|
if (outfile != NULL) |
164 |
|
hdr->compr = BI_RLE8; |
165 |
+ |
*/ |
166 |
|
} else |
167 |
|
hdr = BMPtruecolorHeader(scanlen(&rs), |
168 |
|
numscans(&rs), 0); |
169 |
|
if (hdr == NULL) |
170 |
|
quiterr("cannot initialize BMP header"); |
171 |
|
/* set up output direction */ |
172 |
< |
hdr->yIsDown = (rs.rt & YDECR) && |
168 |
< |
((outfile == NULL) | (hdr->compr == BI_RLE8)); |
172 |
> |
hdr->yIsDown = ((outfile == NULL) | (hdr->compr == BI_RLE8)); |
173 |
|
/* open BMP output */ |
174 |
|
if (outfile != NULL) |
175 |
|
wtr = BMPopenOutputFile(outfile, hdr); |
178 |
|
if (wtr == NULL) |
179 |
|
quiterr("cannot allocate writer structure"); |
180 |
|
/* convert file */ |
181 |
< |
rad2bmp(stdin, wtr, !hdr->yIsDown && rs.rt&YDECR, rgbp==NULL); |
181 |
> |
rad2bmp(stdin, wtr, !hdr->yIsDown, rgbp); |
182 |
|
/* flush output */ |
183 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
184 |
|
quiterr("error writing BMP output"); |
206 |
|
exit(0); |
207 |
|
} |
208 |
|
|
209 |
+ |
/* process header line (don't echo) */ |
210 |
+ |
static int |
211 |
+ |
headline(char *s, void *p) |
212 |
+ |
{ |
213 |
+ |
char fmt[MAXFMTLEN]; |
214 |
+ |
|
215 |
+ |
if (formatval(fmt, s)) { /* check if format string */ |
216 |
+ |
if (!strcmp(fmt,COLRFMT)) |
217 |
+ |
return(0); |
218 |
+ |
if (!strcmp(fmt,CIEFMT)) { |
219 |
+ |
rgbinp = TM_XYZPRIM; |
220 |
+ |
return(0); |
221 |
+ |
} |
222 |
+ |
if (!strcmp(fmt,SPECFMT)) |
223 |
+ |
return(0); |
224 |
+ |
return(-1); |
225 |
+ |
} |
226 |
+ |
if (isprims(s)) { /* get input primaries */ |
227 |
+ |
primsval(myinprims, s); |
228 |
+ |
rgbinp = myinprims; |
229 |
+ |
return(0); |
230 |
+ |
} |
231 |
+ |
if (isncomp(s)) { |
232 |
+ |
NCSAMP = ncompval(s); |
233 |
+ |
return(0); |
234 |
+ |
} |
235 |
+ |
if (iswlsplit(s)) { |
236 |
+ |
wlsplitval(WLPART, s); |
237 |
+ |
return(0); |
238 |
+ |
} |
239 |
+ |
/* should I grok colcorr also? */ |
240 |
+ |
return(0); |
241 |
+ |
} |
242 |
+ |
|
243 |
+ |
|
244 |
|
/* convert Radiance picture to BMP */ |
245 |
|
static void |
246 |
< |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, int gry) |
246 |
> |
rad2bmp(FILE *rfp, BMPWriter *bwr, int inv, RGBPRIMP monpri) |
247 |
|
{ |
248 |
+ |
int usexfm = 0; |
249 |
+ |
COLORMAT xfm; |
250 |
|
COLR *scanin; |
251 |
+ |
COLOR cval; |
252 |
|
int y, yend, ystp; |
253 |
|
int x; |
254 |
|
/* allocate scanline */ |
255 |
|
scanin = (COLR *)malloc(bwr->hdr->width*sizeof(COLR)); |
256 |
|
if (scanin == NULL) |
257 |
|
quiterr("out of memory in rad2bmp"); |
258 |
+ |
/* set up color conversion */ |
259 |
+ |
usexfm = (monpri != NULL ? rgbinp != monpri : |
260 |
+ |
rgbinp != TM_XYZPRIM && rgbinp != stdprims); |
261 |
+ |
if (usexfm) { |
262 |
+ |
RGBPRIMP destpri = monpri != NULL ? monpri : stdprims; |
263 |
+ |
double expcomp = pow(2.0, (double)bradj); |
264 |
+ |
if (rgbinp == TM_XYZPRIM) |
265 |
+ |
compxyz2rgbWBmat(xfm, destpri); |
266 |
+ |
else |
267 |
+ |
comprgb2rgbWBmat(xfm, rgbinp, destpri); |
268 |
+ |
for (y = 0; y < 3; y++) |
269 |
+ |
for (x = 0; x < 3; x++) |
270 |
+ |
xfm[y][x] *= expcomp; |
271 |
+ |
} |
272 |
|
/* convert image */ |
273 |
|
if (inv) { |
274 |
|
y = bwr->hdr->height - 1; |
279 |
|
} |
280 |
|
/* convert each scanline */ |
281 |
|
for ( ; y != yend; y += ystp) { |
282 |
< |
if (freadcolrs(scanin, bwr->hdr->width, rfp) < 0) |
282 |
> |
if (fread2colrs(scanin, bwr->hdr->width, rfp, NCSAMP, WLPART) < 0) |
283 |
|
quiterr("error reading Radiance picture"); |
284 |
< |
if (bradj) |
284 |
> |
if (usexfm) |
285 |
> |
for (x = bwr->hdr->width; x--; ) { |
286 |
> |
colr_color(cval, scanin[x]); |
287 |
> |
colortrans(cval, xfm, cval); |
288 |
> |
setcolr(scanin[x], colval(cval,RED), |
289 |
> |
colval(cval,GRN), |
290 |
> |
colval(cval,BLU)); |
291 |
> |
} |
292 |
> |
else if (bradj) |
293 |
|
shiftcolrs(scanin, bwr->hdr->width, bradj); |
294 |
< |
for (x = gry ? bwr->hdr->width : 0; x--; ) |
295 |
< |
scanin[x][GRN] = normbright(scanin[x]); |
294 |
> |
if (monpri == NULL && rgbinp != TM_XYZPRIM) |
295 |
> |
for (x = bwr->hdr->width; x--; ) |
296 |
> |
scanin[x][GRN] = normbright(scanin[x]); |
297 |
|
colrs_gambs(scanin, bwr->hdr->width); |
298 |
< |
if (gry) |
298 |
> |
if (monpri == NULL) |
299 |
|
for (x = bwr->hdr->width; x--; ) |
300 |
|
bwr->scanline[x] = scanin[x][GRN]; |
301 |
|
else |
360 |
|
int tmflags; |
361 |
|
BMPHeader *hdr; |
362 |
|
BMPWriter *wtr; |
298 |
– |
RESOLU rs; |
363 |
|
FILE *fp; |
364 |
|
int xr, yr; |
365 |
< |
BYTE *pa; |
365 |
> |
uby8 *pa; |
366 |
|
int i; |
367 |
|
/* check tone-mapping spec */ |
368 |
|
i = strlen(expec); |
385 |
|
fprintf(stderr, "%s: cannot open\n", fnin); |
386 |
|
exit(1); |
387 |
|
} |
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; |
388 |
|
/* tone-map picture */ |
389 |
|
if (tmMapPicture(&pa, &xr, &yr, tmflags, monpri, gamval, |
390 |
|
0., 0., fnin, fp) != TM_E_OK) |
392 |
|
/* initialize BMP header */ |
393 |
|
if (tmflags & TM_F_BW) { |
394 |
|
hdr = BMPmappedHeader(xr, yr, 0, 256); |
395 |
< |
hdr->compr = BI_RLE8; |
395 |
> |
if (fnout != NULL) |
396 |
> |
hdr->compr = BI_RLE8; |
397 |
|
} else |
398 |
|
hdr = BMPtruecolorHeader(xr, yr, 0); |
399 |
|
if (hdr == NULL) |
407 |
|
quiterr("cannot allocate writer structure"); |
408 |
|
/* write to BMP file */ |
409 |
|
while (wtr->yscan < yr) { |
410 |
< |
BYTE *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
411 |
< |
((rs.rt & YDECR) ? |
354 |
< |
(yr-1 - wtr->yscan) : |
355 |
< |
wtr->yscan); |
410 |
> |
uby8 *scn = pa + xr*((tmflags & TM_F_BW) ? 1 : 3)* |
411 |
> |
(yr-1 - wtr->yscan); |
412 |
|
if (tmflags & TM_F_BW) |
413 |
|
memcpy((void *)wtr->scanline, (void *)scn, xr); |
414 |
|
else |
424 |
|
if (fflush((FILE *)wtr->c_data) < 0) |
425 |
|
quiterr("error writing BMP output"); |
426 |
|
/* clean up */ |
427 |
+ |
if (fnin != NULL) |
428 |
+ |
fclose(fp); |
429 |
|
free((void *)pa); |
430 |
|
BMPcloseOutput(wtr); |
431 |
|
} |